A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006,2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef BUFFER_H
9#define BUFFER_H
10
11#include "ns3/assert.h"
12#include "ns3/deprecated.h"
13
14#include <iterator>
15#include <ostream>
16#include <stdint.h>
17#include <type_traits>
18#include <vector>
19
20#define BUFFER_FREE_LIST 1
21
22namespace ns3
23{
24
25/**
26 * @ingroup packet
27 * Concept matching a forward iterator whose value type is uint8_t.
28 */
29template <typename It>
31 std::forward_iterator<It> &&
32 std::is_same_v<typename std::iterator_traits<It>::value_type, uint8_t>;
33
34/**
35 * @ingroup packet
36 *
37 * @brief automatically resized byte buffer
38 *
39 * This represents a buffer of bytes. Its size is
40 * automatically adjusted to hold any data prepended
41 * or appended by the user. Its implementation is optimized
42 * to ensure that the number of buffer resizes is minimized,
43 * by creating new Buffers of the maximum size ever used.
44 * The correct maximum size is learned at runtime during use by
45 * recording the maximum size of each packet.
46 *
47 * Some of the features of this class depend on the byte order
48 * of the device running ns-3 (big-endian or little-endian).
49 * For example, `WriteU16/32/64` on a little-endian host should
50 * work as `WriteHton16/32/64` on a big-endian host and vice versa.
51 * At the moment, all systems supported by ns-3 are little-endian,
52 * and behavior on big-endian systems is not tested. Hence,
53 * special care is needed when running ns-3 on a big-endian host.
54 *
55 * @internal
56 * The implementation of the Buffer class uses a COW (Copy On Write)
57 * technique to ensure that the underlying data buffer which holds
58 * the data bytes is shared among a lot of Buffer instances despite
59 * data being added or removed from them.
60 *
61 * When multiple Buffer instances hold a reference to the same
62 * underlying BufferData object, they must be able to detect when
63 * the operation they want to perform should trigger a copy of the
64 * BufferData. If the BufferData::m_count field is one, it means that
65 * there exist only one instance of Buffer which references the
66 * BufferData instance so, it is safe to modify it. It is also
67 * safe to modify the content of a BufferData if the modification
68 * falls outside of the "dirty area" defined by the BufferData.
69 * In every other case, the BufferData must be copied before
70 * being modified.
71 *
72 * To understand the way the Buffer::Add and Buffer::Remove methods
73 * work, you first need to understand the "virtual offsets" used to
74 * keep track of the content of buffers. Each Buffer instance
75 * contains real data bytes in its BufferData instance but it also
76 * contains "virtual zero data" which typically is used to represent
77 * application-level payload. No memory is allocated to store the
78 * zero bytes of application-level payload unless the user fragments
79 * a Buffer: this application-level payload is kept track of with
80 * a pair of integers which describe where in the buffer content
81 * the "virtual zero area" starts and ends.
82 *
83 * @verbatim
84 * ***: unused bytes
85 * xxx: bytes "added" at the front of the zero area
86 * ...: bytes "added" at the back of the zero area
87 * 000: virtual zero bytes
88 *
89 * Real byte buffer: |********xxxxxxxxxxxx.........*****|
90 * |--------^ m_start
91 * |-------------------^ m_zeroAreaStart
92 * |-----------------------------^ m_end - (m_zeroAreaEnd - m_zeroAreaStart)
93 * Virtual byte buffer: |xxxxxxxxxxxx0000000000000.........|
94 * |--------^ m_start
95 * |--------------------^ m_zeroAreaStart
96 * |---------------------------------^ m_zeroAreaEnd
97 * |------------------------------------------^ m_end
98 * @endverbatim
99 *
100 * A simple state invariant is that m_start <= m_zeroStart <= m_zeroEnd <= m_end
101 */
103{
104 public:
105 /**
106 * @brief iterator in a Buffer instance
107 */
109 {
110 public:
111 inline Iterator();
112 /**
113 * go forward by one byte
114 */
115 inline void Next();
116 /**
117 * go backward by one byte
118 */
119 inline void Prev();
120 /**
121 * @param delta number of bytes to go forward
122 */
123 inline void Next(uint32_t delta);
124 /**
125 * @param delta number of bytes to go backward
126 */
127 inline void Prev(uint32_t delta);
128 /**
129 * @param o the second iterator
130 * @return number of bytes included between the two iterators
131 *
132 * This method works only if the two iterators point
133 * to the same underlying buffer. Debug builds ensure
134 * this with an assert.
135 *
136 * @hidecaller
137 */
138 uint32_t GetDistanceFrom(const Iterator& o) const;
139
140 /**
141 * @return true if this iterator points to the end of the byte array.
142 * false otherwise.
143 */
144 bool IsEnd() const;
145 /**
146 * @return true if this iterator points to the start of the byte array.
147 * false otherwise.
148 */
149 bool IsStart() const;
150
151 /**
152 * @param data data to write in buffer
153 *
154 * Write the data in buffer and advance the iterator position
155 * by one byte.
156 *
157 * @hidecaller
158 */
159 inline void WriteU8(uint8_t data);
160 /**
161 * @param data data to write in buffer
162 * @param len number of times data must be written in buffer
163 *
164 * Write the data in buffer len times and advance the iterator position
165 * by len byte.
166 *
167 * @hidecaller
168 */
169 inline void WriteU8(uint8_t data, uint32_t len);
170 /**
171 * @param data data to write in buffer
172 *
173 * Write the data in buffer and advance the iterator position
174 * by two bytes. The format of the data written in the byte
175 * buffer is little-endian. We ensure that ReadU16 will
176 * return exactly what we wrote with WriteU16 if the program
177 * is run on the same machine.
178 */
179 void WriteU16(uint16_t data);
180 /**
181 * @param data data to write in buffer
182 *
183 * Write the data in buffer and advance the iterator position
184 * by four bytes. The format of the data written in the byte
185 * buffer is little-endian. We ensure that ReadU32 will
186 * return exactly what we wrote with WriteU32 if the program
187 * is run on the same machine.
188 */
189 void WriteU32(uint32_t data);
190 /**
191 * @param data data to write in buffer
192 *
193 * Write the data in buffer and advance the iterator position
194 * by eight bytes. The format of the data written in the byte
195 * buffer is little-endian. We ensure that ReadU64 will
196 * return exactly what we wrote with WriteU64 if the program
197 * is run on the same machine.
198 */
199 void WriteU64(uint64_t data);
200 /**
201 * @param data data to write in buffer
202 *
203 * Write the data in buffer and advance the iterator position
204 * by two bytes. The data is written in least significant byte order and the
205 * input data is expected to be in host order.
206 */
207 NS_DEPRECATED_3_48("Use Buffer::Iterator::WriteU16 instead")
208 void WriteHtolsbU16(uint16_t data);
209 /**
210 * @param data data to write in buffer
211 *
212 * Write the data in buffer and advance the iterator position
213 * by four bytes. The data is written in least significant byte order and the
214 * input data is expected to be in host order.
215 */
216 NS_DEPRECATED_3_48("Use Buffer::Iterator::WriteU32 instead")
218 /**
219 * @param data data to write in buffer
220 *
221 * Write the data in buffer and advance the iterator position
222 * by eight bytes. The data is written in least significant byte order and the
223 * input data is expected to be in host order.
224 */
225 NS_DEPRECATED_3_48("Use Buffer::Iterator::WriteU64 instead")
226 void WriteHtolsbU64(uint64_t data);
227 /**
228 * @param data data to write in buffer
229 *
230 * Write the data in buffer and advance the iterator position
231 * by two bytes. The data is written in network order (big-endian)
232 * and the input data is expected to be in host order.
233 *
234 * @hidecaller
235 */
236 inline void WriteHtonU16(uint16_t data);
237 /**
238 * @param data data to write in buffer
239 *
240 * Write the data in buffer and advance the iterator position
241 * by four bytes. The data is written in network order (big-endian)
242 * and the input data is expected to be in host order.
243 */
244 inline void WriteHtonU32(uint32_t data);
245 /**
246 * @param data data to write in buffer
247 *
248 * Write the data in buffer and advance the iterator position
249 * by eight bytes. The data is written in network order (big-endian)
250 * and the input data is expected to be in host order.
251 */
252 void WriteHtonU64(uint64_t data);
253 /**
254 * @param buffer a byte buffer to copy in the internal buffer.
255 * @param size number of bytes to copy.
256 *
257 * Write the data in buffer and advance the iterator position
258 * by size bytes.
259 */
260 void Write(const uint8_t* buffer, uint32_t size);
261 /**
262 * @param start the start of the data to copy
263 * @param end the end of the data to copy
264 *
265 * Write the data delimited by start and end in internal buffer
266 * and advance the iterator position by the number of bytes
267 * copied.
268 * The input iterators _must_ not point to the same Buffer as
269 * we do to avoid overlapping copies. This is enforced
270 * in debug builds by asserts.
271 */
272 void Write(Iterator start, Iterator end);
273
274 /**
275 * @brief Write a range of uint8_t values into the buffer.
276 *
277 * @tparam Iter a forward iterator with value type uint8_t
278 * @param first start of the range to copy in
279 * @param last one-past-the-end of the range to copy in
280 *
281 * Advances the iterator position by std::distance(first, last) bytes.
282 */
283 template <Uint8tForwardIterator Iter>
284 void Write(Iter first, Iter last)
285 {
286 for (; first != last; ++first)
287 {
288 WriteU8(*first);
289 }
290 }
291
292 /**
293 * @return the byte read in the buffer.
294 *
295 * Read data, but do not advance the Iterator read.
296 */
297 inline uint8_t PeekU8();
298
299 /**
300 * @return the byte read in the buffer.
301 *
302 * Read data and advance the Iterator by the number of bytes
303 * read.
304 *
305 * @hidecaller
306 */
307 inline uint8_t ReadU8();
308 /**
309 * @return the two bytes read in the buffer.
310 *
311 * Read data and advance the Iterator by the number of bytes
312 * read.
313 * The data is read in the format written by WriteU16.
314 */
315 inline uint16_t ReadU16();
316 /**
317 * @return the four bytes read in the buffer.
318 *
319 * Read data and advance the Iterator by the number of bytes
320 * read.
321 * The data is read in the format written by WriteU32.
322 */
324 /**
325 * @return the eight bytes read in the buffer.
326 *
327 * Read data and advance the Iterator by the number of bytes
328 * read.
329 * The data is read in the format written by WriteU64.
330 */
331 uint64_t ReadU64();
332 /**
333 * @return the two bytes read in the buffer.
334 *
335 * Read data and advance the Iterator by the number of bytes
336 * read.
337 * The data is read in network format and returned in host format.
338 *
339 * @hidecaller
340 */
341 inline uint16_t ReadNtohU16();
342 /**
343 * @return the four bytes read in the buffer.
344 *
345 * Read data and advance the Iterator by the number of bytes
346 * read.
347 * The data is read in network format and returned in host format.
348 */
349 inline uint32_t ReadNtohU32();
350 /**
351 * @return the eight bytes read in the buffer.
352 *
353 * Read data and advance the Iterator by the number of bytes
354 * read.
355 * The data is read in network format and returned in host format.
356 */
357 uint64_t ReadNtohU64();
358 /**
359 * @return the two bytes read in the buffer.
360 *
361 * Read data and advance the Iterator by the number of bytes
362 * read.
363 * The data is read in least significant byte format and returned in host format.
364 */
365 NS_DEPRECATED_3_48("Use Buffer::Iterator::ReadU16 instead")
366 uint16_t ReadLsbtohU16();
367 /**
368 * @return the four bytes read in the buffer.
369 *
370 * Read data and advance the Iterator by the number of bytes
371 * read.
372 * The data is read in least significant byte format and returned in host format.
373 */
374 NS_DEPRECATED_3_48("Use Buffer::Iterator::ReadU32 instead")
376 /**
377 * @return the eight bytes read in the buffer.
378 *
379 * Read data and advance the Iterator by the number of bytes
380 * read.
381 * The data is read in least significant byte format and returned in host format.
382 */
383 NS_DEPRECATED_3_48("Use Buffer::Iterator::ReadU64 instead")
384 uint64_t ReadLsbtohU64();
385 /**
386 * @param buffer buffer to copy data into
387 * @param size number of bytes to copy
388 *
389 * Copy size bytes of data from the internal buffer to the
390 * input buffer and advance the Iterator by the number of
391 * bytes read.
392 */
393 void Read(uint8_t* buffer, uint32_t size);
394
395 /**
396 * @brief Read into a range of uint8_t storage from the buffer.
397 *
398 * @tparam Iter a forward iterator with value type uint8_t
399 * @param first start of the destination range
400 * @param last one-past-the-end of the destination range
401 *
402 * Advances the iterator position by std::distance(first, last) bytes.
403 */
404 template <Uint8tForwardIterator Iter>
405 void Read(Iter first, Iter last)
406 {
407 for (; first != last; ++first)
408 {
409 *first = ReadU8();
410 }
411 }
412
413 /**
414 * @param start start iterator of the buffer to copy data into
415 * @param size number of bytes to copy
416 *
417 * Copy size bytes of data from the internal buffer to the input buffer via
418 * the provided iterator and advance the Iterator by the number of bytes
419 * read.
420 */
421 inline void Read(Iterator start, uint32_t size);
422
423 /**
424 * @brief Calculate the checksum.
425 * @param size size of the buffer.
426 * @return checksum
427 */
428 uint16_t CalculateIpChecksum(uint16_t size);
429
430 /**
431 * @brief Calculate the checksum.
432 * @param size size of the buffer.
433 * @param initialChecksum initial value
434 * @return checksum
435 */
436 uint16_t CalculateIpChecksum(uint16_t size, uint32_t initialChecksum);
437
438 /**
439 * @returns the size of the underlying buffer we are iterating
440 */
441 uint32_t GetSize() const;
442
443 /**
444 * @returns the size left to read of the underlying buffer we are iterating
445 */
447
448 private:
449 /// Friend class
450 friend class Buffer;
451 /**
452 * Constructor - initializes the iterator to point to the buffer start
453 *
454 * @param buffer the buffer this iterator refers to
455 */
456 inline Iterator(const Buffer* buffer);
457 /**
458 * Constructor - initializes the iterator to point to the buffer end
459 *
460 * @param buffer the buffer this iterator refers to
461 * @param dummy not used param
462 */
463 inline Iterator(const Buffer* buffer, bool dummy);
464 /**
465 * Initializes the iterator values
466 *
467 * @param buffer the buffer this iterator refers to
468 */
469 inline void Construct(const Buffer* buffer);
470 /**
471 * Checks that the [start, end) is not in the "virtual zero area".
472 *
473 * @param start start buffer position
474 * @param end end buffer position
475 * @returns true if [start, end) is not in the "virtual zero area".
476 */
477 bool CheckNoZero(uint32_t start, uint32_t end) const;
478 /**
479 * Checks that the buffer position is not in the "virtual zero area".
480 *
481 * @param i buffer position
482 * @returns true if not in the "virtual zero area".
483 */
484 bool Check(uint32_t i) const;
485 /**
486 * @return the two bytes read in the buffer.
487 *
488 * Read data and advance the Iterator by the number of bytes
489 * read.
490 * The data is read in network format and returned in host format.
491 *
492 * @warning this is the slow version, please use ReadNtohU16 ()
493 */
494 uint16_t SlowReadNtohU16();
495 /**
496 * @return the four bytes read in the buffer.
497 *
498 * Read data and advance the Iterator by the number of bytes
499 * read.
500 * The data is read in network format and returned in host format.
501 *
502 * @warning this is the slow version, please use ReadNtohU32 ()
503 */
505 /**
506 * @brief Returns an appropriate message indicating a read error
507 * @returns the error message
508 */
509 std::string GetReadErrorMessage() const;
510 /**
511 * @brief Returns an appropriate message indicating a write error
512 *
513 * The message depends on the actual Buffer::Iterator status.
514 *
515 * @returns the error message
516 */
517 std::string GetWriteErrorMessage() const;
518
519 /**
520 * offset in virtual bytes from the start of the data buffer to the
521 * start of the "virtual zero area".
522 */
524 /**
525 * offset in virtual bytes from the start of the data buffer to the
526 * end of the "virtual zero area".
527 */
529 /**
530 * offset in virtual bytes from the start of the data buffer to the
531 * start of the data which can be read by this iterator
532 */
534 /**
535 * offset in virtual bytes from the start of the data buffer to the
536 * end of the data which can be read by this iterator
537 */
539 /**
540 * offset in virtual bytes from the start of the data buffer to the
541 * current position represented by this iterator.
542 */
544 /**
545 * a pointer to the underlying byte buffer. All offsets are relative
546 * to this pointer.
547 */
548 uint8_t* m_data;
549 };
550
551 /**
552 * @return the number of bytes stored in this buffer.
553 */
554 inline uint32_t GetSize() const;
555
556 /**
557 * @return a pointer to the start of the internal
558 * byte buffer.
559 *
560 * The returned pointer points to an area of
561 * memory which is ns3::Buffer::GetSize () bytes big.
562 * Please, try to never ever use this method. It is really
563 * evil and is present only for a few specific uses.
564 */
565 const uint8_t* PeekData() const;
566
567 /**
568 * @param start size to reserve
569 *
570 * Add bytes at the start of the Buffer. The
571 * content of these bytes is undefined but debugging
572 * builds initialize them to 0x33.
573 * Any call to this method invalidates any Iterator
574 * pointing to this Buffer.
575 */
576 void AddAtStart(uint32_t start);
577 /**
578 * @param end size to reserve
579 *
580 * Add bytes at the end of the Buffer. The
581 * content of these bytes is undefined but debugging
582 * builds initialize them to 0x33.
583 * Any call to this method invalidates any Iterator
584 * pointing to this Buffer.
585 */
586 void AddAtEnd(uint32_t end);
587
588 /**
589 * @param o the buffer to append to the end of this buffer.
590 *
591 * Add bytes at the end of the Buffer.
592 * Any call to this method invalidates any Iterator
593 * pointing to this Buffer.
594 */
595 void AddAtEnd(const Buffer& o);
596 /**
597 * @param start size to remove
598 *
599 * Remove bytes at the start of the Buffer.
600 * Any call to this method invalidates any Iterator
601 * pointing to this Buffer.
602 */
603 void RemoveAtStart(uint32_t start);
604 /**
605 * @param end size to remove
606 *
607 * Remove bytes at the end of the Buffer.
608 * Any call to this method invalidates any Iterator
609 * pointing to this Buffer.
610 */
611 void RemoveAtEnd(uint32_t end);
612
613 /**
614 * @param start offset from start of packet
615 * @param length
616 *
617 * @return a fragment of size length starting at offset
618 * start.
619 */
620 Buffer CreateFragment(uint32_t start, uint32_t length) const;
621
622 /**
623 * @return an Iterator which points to the
624 * start of this Buffer.
625 */
626 inline Buffer::Iterator Begin() const;
627 /**
628 * @return an Iterator which points to the
629 * end of this Buffer.
630 */
631 inline Buffer::Iterator End() const;
632
633 /**
634 * @brief Return the number of bytes required for serialization.
635 * @return the number of bytes.
636 */
638
639 /**
640 * This buffer's contents are serialized into the raw
641 * character buffer parameter. Note: The zero length
642 * data is not copied entirely. Only the length of
643 * zero byte data is serialized.
644 *
645 * @param buffer points to serialization buffer
646 * @param maxSize max number of bytes to write
647 * @return number of serialized bytes on success, 0 on failure
648 */
649 uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
650
651 /**
652 * The raw character buffer is deserialized and all the
653 * data is placed into this buffer.
654 *
655 * @param buffer points to buffer for deserialization
656 * @param size number of bytes to deserialize
657 * @return number of deserialized bytes on success, 0 on failure
658 */
659 uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
660
661 /**
662 * Copy the specified amount of data from the buffer to the given output stream.
663 *
664 * @param os the output stream
665 * @param size the maximum amount of bytes to copy. If zero, nothing is copied.
666 */
667 void CopyData(std::ostream* os, uint32_t size) const;
668
669 /**
670 * Copy the specified amount of data from the buffer to the given buffer.
671 *
672 * @param buffer the output buffer
673 * @param size the maximum amount of bytes to copy. If zero, nothing is copied.
674 * @returns the amount of bytes copied
675 */
676 uint32_t CopyData(uint8_t* buffer, uint32_t size) const;
677
678 /**
679 * @brief Copy constructor
680 * @param o the buffer to copy
681 */
682 inline Buffer(const Buffer& o);
683 /**
684 * @brief Assignment operator
685 * @param o the buffer to copy
686 * @return a reference to the buffer
687 */
688 Buffer& operator=(const Buffer& o);
689 Buffer();
690 /**
691 * @brief Constructor
692 *
693 * The buffer will be initialized with zeroes up to its size.
694 *
695 * @param dataSize the buffer size
696 */
697 Buffer(uint32_t dataSize);
698 /**
699 * @brief Constructor
700 *
701 * If initialize is set to true, the buffer will be initialized
702 * with zeroes up to its size.
703 *
704 * @param dataSize the buffer size.
705 * @param initialize initialize the buffer with zeroes.
706 */
707 Buffer(uint32_t dataSize, bool initialize);
708 ~Buffer();
709
710 private:
711 /**
712 * This data structure is variable-sized through its last member whose size
713 * is determined at allocation time and stored in the m_size field.
714 *
715 * The so-called "dirty area" describes the area in the buffer which
716 * has been reserved and used by a user. Multiple Buffer instances
717 * may reference the same Buffer::Data object instance and may
718 * reference different parts of the underlying byte buffer. The
719 * "dirty area" is union of all the areas referenced by the Buffer
720 * instances which reference the same BufferData instance.
721 * New user data can be safely written only outside of the "dirty
722 * area" if the reference count is higher than 1 (that is, if
723 * more than one Buffer instance references the same BufferData).
724 */
725 struct Data
726 {
727 /**
728 * The reference count of an instance of this data structure.
729 * Each buffer which references an instance holds a count.
730 */
732 /**
733 * the size of the m_data field below.
734 */
736 /**
737 * offset from the start of the m_data field below to the
738 * start of the area in which user bytes were written.
739 */
741 /**
742 * offset from the start of the m_data field below to the
743 * end of the area in which user bytes were written.
744 */
746 /**
747 * The real data buffer holds _at least_ one byte.
748 * Its real size is stored in the m_size field.
749 */
750 uint8_t m_data[1];
751 };
752
753 /**
754 * @brief Create a full copy of the buffer, including
755 * all the internal structures.
756 *
757 * @returns a copy of the buffer
758 */
759 Buffer CreateFullCopy() const;
760
761 /**
762 * @brief Transform a "Virtual byte buffer" into a "Real byte buffer"
763 */
764 void TransformIntoRealBuffer() const;
765 /**
766 * @brief Checks the internal buffer structures consistency
767 *
768 * Used only for debugging purposes.
769 *
770 * @returns true if the buffer status is consistent.
771 */
772 bool CheckInternalState() const;
773
774 /**
775 * @brief Initializes the buffer with a number of zeroes.
776 *
777 * @param zeroSize the zeroes size
778 */
779 void Initialize(uint32_t zeroSize);
780
781 /**
782 * @brief Get the buffer real size.
783 * @warning The real size is the actual memory used by the buffer.
784 * @returns the memory used by the buffer.
785 */
787
788 /**
789 * @brief Get the buffer end position.
790 * @returns the buffer end index.
791 */
792 uint32_t GetInternalEnd() const;
793
794 /**
795 * @brief Recycle the buffer memory
796 * @param data the buffer data storage
797 */
798 static void Recycle(Buffer::Data* data);
799 /**
800 * @brief Create a buffer data storage
801 * @param size the storage size to create
802 * @returns a pointer to the created buffer storage
803 */
804 static Buffer::Data* Create(uint32_t size);
805 /**
806 * @brief Allocate a buffer data storage
807 * @param reqSize the storage size to create
808 * @returns a pointer to the allocated buffer storage
809 */
810 static Buffer::Data* Allocate(uint32_t reqSize);
811 /**
812 * @brief Deallocate the buffer memory
813 * @param data the buffer data storage
814 */
815 static void Deallocate(Buffer::Data* data);
816
817 Data* m_data; //!< the buffer data storage
818
819 /**
820 * keep track of the maximum value of m_zeroAreaStart across
821 * the lifetime of a Buffer instance. This variable is used
822 * purely as a source of information for the heuristics which
823 * decide on the position of the zero area in new buffers.
824 * It is read from the Buffer destructor to update the global
825 * heuristic data and these global heuristic data are used from
826 * the Buffer constructor to choose an initial value for
827 * m_zeroAreaStart.
828 */
830 /**
831 * location in a newly-allocated buffer where you should start
832 * writing data. i.e., m_start should be initialized to this
833 * value.
834 */
836
837 /**
838 * offset to the start of the virtual zero area from the start
839 * of m_data->m_data
840 */
842 /**
843 * offset to the end of the virtual zero area from the start
844 * of m_data->m_data
845 */
847 /**
848 * offset to the start of the data referenced by this Buffer
849 * instance from the start of m_data->m_data
850 */
852 /**
853 * offset to the end of the data referenced by this Buffer
854 * instance from the start of m_data->m_data
855 */
857
858#ifdef BUFFER_FREE_LIST
859 /// Container for buffer data
860 typedef std::vector<Buffer::Data*> FreeList;
861
862 /// Local static destructor structure
867
868 static uint32_t g_maxSize; //!< Max observed data size
869 static FreeList* g_freeList; //!< Buffer data container
870 static LocalStaticDestructor g_localStaticDestructor; //!< Local static destructor
871#endif
872};
873
874} // namespace ns3
875
876#include "ns3/assert.h"
877
878#include <cstring>
879
880namespace ns3
881{
882
884 : m_zeroStart(0),
885 m_zeroEnd(0),
886 m_dataStart(0),
887 m_dataEnd(0),
888 m_current(0),
889 m_data(nullptr)
890{
891}
892
894{
895 Construct(buffer);
897}
898
899Buffer::Iterator::Iterator(const Buffer* buffer, bool dummy)
900{
901 Construct(buffer);
903}
904
905void
907{
909 m_zeroEnd = buffer->m_zeroAreaEnd;
910 m_dataStart = buffer->m_start;
911 m_dataEnd = buffer->m_end;
912 m_data = buffer->m_data->m_data;
913}
914
915void
921
922void
928
929void
931{
932 NS_ASSERT(m_current + delta <= m_dataEnd);
933 m_current += delta;
934}
935
936void
938{
939 NS_ASSERT(m_current >= delta);
940 m_current -= delta;
941}
942
943void
945{
947
949 {
951 m_current++;
952 }
953 else
954 {
956 m_current++;
957 }
958}
959
960void
962{
964 if (m_current <= m_zeroStart)
965 {
966 std::memset(&(m_data[m_current]), data, len);
967 m_current += len;
968 }
969 else
970 {
971 uint8_t* buffer = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
972 std::memset(buffer, data, len);
973 m_current += len;
974 }
975}
976
977void
979{
981 uint8_t* buffer;
982 if (m_current + 2 <= m_zeroStart)
983 {
984 buffer = &m_data[m_current];
985 }
986 else
987 {
988 buffer = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
989 }
990 buffer[0] = (data >> 8) & 0xff;
991 buffer[1] = (data >> 0) & 0xff;
992 m_current += 2;
993}
994
995void
997{
999
1000 uint8_t* buffer;
1001 if (m_current + 4 <= m_zeroStart)
1002 {
1003 buffer = &m_data[m_current];
1004 }
1005 else
1006 {
1007 buffer = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
1008 }
1009 buffer[0] = (data >> 24) & 0xff;
1010 buffer[1] = (data >> 16) & 0xff;
1011 buffer[2] = (data >> 8) & 0xff;
1012 buffer[3] = (data >> 0) & 0xff;
1013 m_current += 4;
1014}
1015
1016uint16_t
1018{
1019 uint8_t* buffer;
1020 if (m_current + 2 <= m_zeroStart)
1021 {
1022 buffer = &m_data[m_current];
1023 }
1024 else if (m_current >= m_zeroEnd)
1025 {
1026 buffer = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
1027 }
1028 else
1029 {
1030 return SlowReadNtohU16();
1031 }
1032 uint16_t retval = 0;
1033 retval |= buffer[0];
1034 retval <<= 8;
1035 retval |= buffer[1];
1036 m_current += 2;
1037 return retval;
1038}
1039
1042{
1043 uint8_t* buffer;
1044 if (m_current + 4 <= m_zeroStart)
1045 {
1046 buffer = &m_data[m_current];
1047 }
1048 else if (m_current >= m_zeroEnd)
1049 {
1050 buffer = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
1051 }
1052 else
1053 {
1054 return SlowReadNtohU32();
1055 }
1056 uint32_t retval = 0;
1057 retval |= buffer[0];
1058 retval <<= 8;
1059 retval |= buffer[1];
1060 retval <<= 8;
1061 retval |= buffer[2];
1062 retval <<= 8;
1063 retval |= buffer[3];
1064 m_current += 4;
1065 return retval;
1066}
1067
1068uint8_t
1070{
1072
1073 if (m_current < m_zeroStart)
1074 {
1075 uint8_t data = m_data[m_current];
1076 return data;
1077 }
1078 else if (m_current < m_zeroEnd)
1079 {
1080 return 0;
1081 }
1082 else
1083 {
1084 uint8_t data = m_data[m_current - (m_zeroEnd - m_zeroStart)];
1085 return data;
1086 }
1087}
1088
1089uint8_t
1091{
1092 uint8_t ret = PeekU8();
1093 m_current++;
1094 return ret;
1095}
1096
1097uint16_t
1099{
1100 uint8_t byte0 = ReadU8();
1101 uint8_t byte1 = ReadU8();
1102 uint16_t data = byte1;
1103 data <<= 8;
1104 data |= byte0;
1105
1106 return data;
1107}
1108
1109void
1111{
1112 Buffer::Iterator end = *this;
1113 end.Next(size);
1114
1115 start.Write(*this, end);
1116}
1117
1129
1132{
1133 return m_end - m_start;
1134}
1135
1138{
1140 return Buffer::Iterator(this);
1141}
1142
1145{
1147 return Buffer::Iterator(this, false);
1148}
1149
1150} // namespace ns3
1151
1152#endif /* BUFFER_H */
iterator in a Buffer instance
Definition buffer.h:109
void WriteU64(uint64_t data)
Definition buffer.cc:868
uint32_t GetRemainingSize() const
Definition buffer.cc:1160
void Construct(const Buffer *buffer)
Initializes the iterator values.
Definition buffer.h:906
uint64_t ReadU64()
Definition buffer.cc:971
void WriteU32(uint32_t data)
Definition buffer.cc:855
uint32_t m_dataEnd
offset in virtual bytes from the start of the data buffer to the end of the data which can be read by...
Definition buffer.h:538
void Write(Iter first, Iter last)
Write a range of uint8_t values into the buffer.
Definition buffer.h:284
void WriteHtonU64(uint64_t data)
Definition buffer.cc:921
void WriteHtolsbU16(uint16_t data)
Definition buffer.cc:889
uint32_t m_dataStart
offset in virtual bytes from the start of the data buffer to the start of the data which can be read ...
Definition buffer.h:533
uint64_t ReadNtohU64()
Definition buffer.cc:1028
std::string GetReadErrorMessage() const
Returns an appropriate message indicating a read error.
Definition buffer.cc:1167
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition buffer.cc:1122
void WriteU8(uint8_t data)
Definition buffer.h:944
uint16_t SlowReadNtohU16()
Definition buffer.cc:1002
uint8_t * m_data
a pointer to the underlying byte buffer.
Definition buffer.h:548
void WriteHtolsbU32(uint32_t data)
Definition buffer.cc:897
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:935
uint32_t m_zeroEnd
offset in virtual bytes from the start of the data buffer to the end of the "virtual zero area".
Definition buffer.h:528
uint16_t ReadLsbtohU16()
Definition buffer.cc:1051
bool Check(uint32_t i) const
Checks that the buffer position is not in the "virtual zero area".
Definition buffer.cc:805
friend class Buffer
Friend class.
Definition buffer.h:450
void WriteU16(uint16_t data)
Definition buffer.cc:846
uint32_t m_current
offset in virtual bytes from the start of the data buffer to the current position represented by this...
Definition buffer.h:543
uint32_t SlowReadNtohU32()
Definition buffer.cc:1013
void Read(uint8_t *buffer, uint32_t size)
Definition buffer.cc:1112
uint64_t ReadLsbtohU64()
Definition buffer.cc:1081
void WriteHtolsbU64(uint64_t data)
Definition buffer.cc:907
void WriteHtonU16(uint16_t data)
Definition buffer.h:978
uint32_t m_zeroStart
offset in virtual bytes from the start of the data buffer to the start of the "virtual zero area".
Definition buffer.h:523
bool CheckNoZero(uint32_t start, uint32_t end) const
Checks that the [start, end) is not in the "virtual zero area".
Definition buffer.cc:797
uint32_t ReadNtohU32()
Definition buffer.h:1041
bool IsEnd() const
Definition buffer.cc:783
bool IsStart() const
Definition buffer.cc:790
uint32_t ReadU32()
Definition buffer.cc:953
void WriteHtonU32(uint32_t data)
Definition buffer.h:996
uint16_t ReadNtohU16()
Definition buffer.h:1017
uint32_t GetDistanceFrom(const Iterator &o) const
Definition buffer.cc:767
void Prev()
go backward by one byte
Definition buffer.h:923
uint32_t GetSize() const
Definition buffer.cc:1153
uint16_t ReadU16()
Definition buffer.h:1098
std::string GetWriteErrorMessage() const
Returns an appropriate message indicating a write error.
Definition buffer.cc:1181
uint32_t ReadLsbtohU32()
Definition buffer.cc:1063
void Next()
go forward by one byte
Definition buffer.h:916
uint32_t GetSerializedSize() const
Return the number of bytes required for serialization.
Definition buffer.cc:555
std::vector< Buffer::Data * > FreeList
Container for buffer data.
Definition buffer.h:860
uint32_t GetInternalEnd() const
Get the buffer end position.
Definition buffer.cc:296
uint32_t m_end
offset to the end of the data referenced by this Buffer instance from the start of m_data->m_data
Definition buffer.h:856
static Buffer::Data * Allocate(uint32_t reqSize)
Allocate a buffer data storage.
Definition buffer.cc:155
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition buffer.cc:518
static FreeList * g_freeList
Buffer data container.
Definition buffer.h:869
Data * m_data
the buffer data storage
Definition buffer.h:817
static Buffer::Data * Create(uint32_t size)
Create a buffer data storage.
Definition buffer.cc:109
static uint32_t g_maxSize
Max observed data size.
Definition buffer.h:868
static LocalStaticDestructor g_localStaticDestructor
Local static destructor.
Definition buffer.h:870
uint32_t m_zeroAreaEnd
offset to the end of the virtual zero area from the start of m_data->m_data
Definition buffer.h:846
uint32_t m_maxZeroAreaStart
keep track of the maximum value of m_zeroAreaStart across the lifetime of a Buffer instance.
Definition buffer.h:829
static uint32_t g_recommendedStart
location in a newly-allocated buffer where you should start writing data.
Definition buffer.h:835
static void Deallocate(Buffer::Data *data)
Deallocate the buffer memory.
Definition buffer.cc:173
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
Definition buffer.cc:700
void RemoveAtEnd(uint32_t end)
Definition buffer.cc:482
void TransformIntoRealBuffer() const
Transform a "Virtual byte buffer" into a "Real byte buffer".
Definition buffer.cc:680
uint32_t GetSize() const
Definition buffer.h:1131
void AddAtStart(uint32_t start)
Definition buffer.cc:303
uint32_t GetInternalSize() const
Get the buffer real size.
Definition buffer.cc:289
Buffer::Iterator Begin() const
Definition buffer.h:1137
void AddAtEnd(uint32_t end)
Definition buffer.cc:349
Buffer::Iterator End() const
Definition buffer.h:1144
Buffer & operator=(const Buffer &o)
Assignment operator.
Definition buffer.cc:252
Buffer(const Buffer &o)
Copy constructor.
Definition buffer.h:1118
Buffer CreateFullCopy() const
Create a full copy of the buffer, including all the internal structures.
Definition buffer.cc:530
static void Recycle(Buffer::Data *data)
Recycle the buffer memory.
Definition buffer.cc:90
bool CheckInternalState() const
Checks the internal buffer structures consistency.
Definition buffer.cc:203
void Initialize(uint32_t zeroSize)
Initializes the buffer with a number of zeroes.
Definition buffer.cc:237
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
The raw character buffer is deserialized and all the data is placed into this buffer.
Definition buffer.cc:635
void RemoveAtStart(uint32_t start)
Definition buffer.cc:436
uint32_t m_zeroAreaStart
offset to the start of the virtual zero area from the start of m_data->m_data
Definition buffer.h:841
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
This buffer's contents are serialized into the raw character buffer parameter.
Definition buffer.cc:571
const uint8_t * PeekData() const
Definition buffer.cc:690
uint32_t m_start
offset to the start of the data referenced by this Buffer instance from the start of m_data->m_data
Definition buffer.h:851
Concept matching a forward iterator whose value type is uint8_t.
Definition buffer.h:30
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:76
#define NS_DEPRECATED_3_48(msg)
Tag for things deprecated in version ns-3.48.
Definition deprecated.h:98
Definition first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
This data structure is variable-sized through its last member whose size is determined at allocation ...
Definition buffer.h:726
uint8_t m_data[1]
The real data buffer holds at least one byte.
Definition buffer.h:750
uint32_t m_dirtyEnd
offset from the start of the m_data field below to the end of the area in which user bytes were writt...
Definition buffer.h:745
uint32_t m_count
The reference count of an instance of this data structure.
Definition buffer.h:731
uint32_t m_size
the size of the m_data field below.
Definition buffer.h:735
uint32_t m_dirtyStart
offset from the start of the m_data field below to the start of the area in which user bytes were wri...
Definition buffer.h:740
Local static destructor structure.
Definition buffer.h:864