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