src/common/buffer.h
changeset 122 6b8f1eda5c57
parent 110 9ac6d63bfe33
child 131 f4fb87e77034
equal deleted inserted replaced
121:f8bc1a370b82 122:6b8f1eda5c57
    46     public:
    46     public:
    47         inline Iterator ();
    47         inline Iterator ();
    48         /**
    48         /**
    49          * go forward by one byte
    49          * go forward by one byte
    50          */
    50          */
    51         inline void next (void);
    51         inline void Next (void);
    52         /**
    52         /**
    53          * go backward by one byte
    53          * go backward by one byte
    54          */
    54          */
    55         inline void prev (void);
    55         inline void Prev (void);
    56         /**
    56         /**
    57          * \param delta number of bytes to go forward
    57          * \param delta number of bytes to go forward
    58          */
    58          */
    59         inline void next (uint32_t delta);
    59         inline void Next (uint32_t delta);
    60         /**
    60         /**
    61          * \param delta number of bytes to go backward
    61          * \param delta number of bytes to go backward
    62          */
    62          */
    63         inline void prev (uint32_t delta);
    63         inline void Prev (uint32_t delta);
    64         /**
    64         /**
    65          * \param o the second iterator
    65          * \param o the second iterator
    66          * \return number of bytes included between the two iterators
    66          * \return number of bytes included between the two iterators
    67          *
    67          *
    68          * This method works only if the two iterators point
    68          * This method works only if the two iterators point
    69          * to the same underlying buffer. Debug builds ensure
    69          * to the same underlying buffer. Debug builds ensure
    70          * this with an assert.
    70          * this with an assert.
    71          */
    71          */
    72         inline int32_t getDistanceFrom (Iterator const &o) const;
    72         inline int32_t GetDistanceFrom (Iterator const &o) const;
    73         
    73         
    74         /**
    74         /**
    75          * \return true if this iterator points to the end of the byte array.
    75          * \return true if this iterator points to the end of the byte array.
    76          *     false otherwise.
    76          *     false otherwise.
    77          */
    77          */
    78         inline bool isEnd (void) const;
    78         inline bool IsEnd (void) const;
    79         /**
    79         /**
    80          * \return true if this iterator points to the start of the byte array.
    80          * \return true if this iterator points to the start of the byte array.
    81          *     false otherwise.
    81          *     false otherwise.
    82          */
    82          */
    83         inline bool isStart (void) const;
    83         inline bool IsStart (void) const;
    84 
    84 
    85         /**
    85         /**
    86          * \param data data to write in buffer
    86          * \param data data to write in buffer
    87          *
    87          *
    88          * Write the data in buffer and avance the iterator position
    88          * Write the data in buffer and avance the iterator position
    89          * by one byte.
    89          * by one byte.
    90          */
    90          */
    91         inline void writeU8 (uint8_t  data);
    91         inline void WriteU8 (uint8_t  data);
    92         /**
    92         /**
    93          * \param data data to write in buffer
    93          * \param data data to write in buffer
    94          * \param len number of times data must be written in buffer
    94          * \param len number of times data must be written in buffer
    95          *
    95          *
    96          * Write the data in buffer len times and avance the iterator position
    96          * Write the data in buffer len times and avance the iterator position
    97          * by len byte.
    97          * by len byte.
    98          */
    98          */
    99         inline void writeU8 (uint8_t data, uint32_t len);
    99         inline void WriteU8 (uint8_t data, uint32_t len);
   100         /**
   100         /**
   101          * \param data data to write in buffer
   101          * \param data data to write in buffer
   102          *
   102          *
   103          * Write the data in buffer and avance the iterator position
   103          * Write the data in buffer and avance the iterator position
   104          * by two bytes. The format of the data written in the byte
   104          * by two bytes. The format of the data written in the byte
   105          * buffer is non-portable. We only ensure that readU16 will
   105          * buffer is non-portable. We only ensure that readU16 will
   106          * return exactly what we wrote with writeU16 if the program
   106          * return exactly what we wrote with writeU16 if the program
   107          * is run on the same machine.
   107          * is run on the same machine.
   108          */
   108          */
   109         inline void writeU16 (uint16_t data);
   109         inline void WriteU16 (uint16_t data);
   110         /**
   110         /**
   111          * \param data data to write in buffer
   111          * \param data data to write in buffer
   112          *
   112          *
   113          * Write the data in buffer and avance the iterator position
   113          * Write the data in buffer and avance the iterator position
   114          * by four bytes. The format of the data written in the byte
   114          * by four bytes. The format of the data written in the byte
   115          * buffer is non-portable. We only ensure that readU32 will
   115          * buffer is non-portable. We only ensure that readU32 will
   116          * return exactly what we wrote with writeU32 if the program
   116          * return exactly what we wrote with writeU32 if the program
   117          * is run on the same machine.
   117          * is run on the same machine.
   118          */
   118          */
   119         inline void writeU32 (uint32_t data);
   119         inline void WriteU32 (uint32_t data);
   120         /**
   120         /**
   121          * \param data data to write in buffer
   121          * \param data data to write in buffer
   122          *
   122          *
   123          * Write the data in buffer and avance the iterator position
   123          * Write the data in buffer and avance the iterator position
   124          * by eight bytes. The format of the data written in the byte
   124          * by eight bytes. The format of the data written in the byte
   125          * buffer is non-portable. We only ensure that readU64 will
   125          * buffer is non-portable. We only ensure that readU64 will
   126          * return exactly what we wrote with writeU64 if the program
   126          * return exactly what we wrote with writeU64 if the program
   127          * is run on the same machine.
   127          * is run on the same machine.
   128          */
   128          */
   129         inline void writeU64 (uint64_t data);
   129         inline void WriteU64 (uint64_t data);
   130         /**
   130         /**
   131          * \param data data to write in buffer
   131          * \param data data to write in buffer
   132          *
   132          *
   133          * Write the data in buffer and avance the iterator position
   133          * Write the data in buffer and avance the iterator position
   134          * by two bytes. The data is written in network order and the
   134          * by two bytes. The data is written in network order and the
   135          * input data is expected to be in host order.
   135          * input data is expected to be in host order.
   136          */
   136          */
   137         inline void writeHtonU16 (uint16_t data);
   137         inline void WriteHtonU16 (uint16_t data);
   138         /**
   138         /**
   139          * \param data data to write in buffer
   139          * \param data data to write in buffer
   140          *
   140          *
   141          * Write the data in buffer and avance the iterator position
   141          * Write the data in buffer and avance the iterator position
   142          * by four bytes. The data is written in network order and the
   142          * by four bytes. The data is written in network order and the
   143          * input data is expected to be in host order.
   143          * input data is expected to be in host order.
   144          */
   144          */
   145         inline void writeHtonU32 (uint32_t data);
   145         inline void WriteHtonU32 (uint32_t data);
   146         /**
   146         /**
   147          * \param data data to write in buffer
   147          * \param data data to write in buffer
   148          *
   148          *
   149          * Write the data in buffer and avance the iterator position
   149          * Write the data in buffer and avance the iterator position
   150          * by eight bytes. The data is written in network order and the
   150          * by eight bytes. The data is written in network order and the
   151          * input data is expected to be in host order.
   151          * input data is expected to be in host order.
   152          */
   152          */
   153         inline void writeHtonU64 (uint64_t data);
   153         inline void WriteHtonU64 (uint64_t data);
   154         /**
   154         /**
   155          * \param buffer a byte buffer to copy in the internal buffer.
   155          * \param buffer a byte buffer to copy in the internal buffer.
   156          * \param size number of bytes to copy.
   156          * \param size number of bytes to copy.
   157          *
   157          *
   158          * Write the data in buffer and avance the iterator position
   158          * Write the data in buffer and avance the iterator position
   159          * by size bytes.
   159          * by size bytes.
   160          */
   160          */
   161         inline void write (uint8_t const*buffer, uint16_t size);
   161         inline void Write (uint8_t const*buffer, uint16_t size);
   162         /**
   162         /**
   163          * \param start the start of the data to copy
   163          * \param start the start of the data to copy
   164          * \param end the end of the data to copy
   164          * \param end the end of the data to copy
   165          *
   165          *
   166          * Write the data delimited by start and end in internal buffer 
   166          * Write the data delimited by start and end in internal buffer 
   168          * copied.
   168          * copied.
   169          * The input interators _must_ not point to the same Buffer as
   169          * The input interators _must_ not point to the same Buffer as
   170          * we do to avoid overlapping copies. This is enforced 
   170          * we do to avoid overlapping copies. This is enforced 
   171          * in debug builds by asserts.
   171          * in debug builds by asserts.
   172          */
   172          */
   173         inline void write (Iterator start, Iterator end);
   173         inline void Write (Iterator start, Iterator end);
   174 
   174 
   175         /**
   175         /**
   176          * \return the byte read in the buffer.
   176          * \return the byte read in the buffer.
   177          *
   177          *
   178          * Read data and advance the Iterator by the number of bytes
   178          * Read data and advance the Iterator by the number of bytes
   179          * read.
   179          * read.
   180          */
   180          */
   181         inline uint8_t  readU8 (void);
   181         inline uint8_t  ReadU8 (void);
   182         /**
   182         /**
   183          * \return the two bytes read in the buffer.
   183          * \return the two bytes read in the buffer.
   184          *
   184          *
   185          * Read data and advance the Iterator by the number of bytes
   185          * Read data and advance the Iterator by the number of bytes
   186          * read.
   186          * read.
   187          * The data is read in the format written by writeU16.
   187          * The data is read in the format written by writeU16.
   188          */
   188          */
   189         inline uint16_t readU16 (void);
   189         inline uint16_t ReadU16 (void);
   190         /**
   190         /**
   191          * \return the four bytes read in the buffer.
   191          * \return the four bytes read in the buffer.
   192          *
   192          *
   193          * Read data and advance the Iterator by the number of bytes
   193          * Read data and advance the Iterator by the number of bytes
   194          * read.
   194          * read.
   195          * The data is read in the format written by writeU32.
   195          * The data is read in the format written by writeU32.
   196          */
   196          */
   197         inline uint32_t readU32 (void);
   197         inline uint32_t ReadU32 (void);
   198         /**
   198         /**
   199          * \return the eight bytes read in the buffer.
   199          * \return the eight bytes read in the buffer.
   200          *
   200          *
   201          * Read data and advance the Iterator by the number of bytes
   201          * Read data and advance the Iterator by the number of bytes
   202          * read.
   202          * read.
   203          * The data is read in the format written by writeU64.
   203          * The data is read in the format written by writeU64.
   204          */
   204          */
   205         inline uint64_t readU64 (void);
   205         inline uint64_t ReadU64 (void);
   206         /**
   206         /**
   207          * \return the two bytes read in the buffer.
   207          * \return the two bytes read in the buffer.
   208          *
   208          *
   209          * Read data and advance the Iterator by the number of bytes
   209          * Read data and advance the Iterator by the number of bytes
   210          * read.
   210          * read.
   211          * The data is read in network format and return in host format.
   211          * The data is read in network format and return in host format.
   212          */
   212          */
   213         inline uint16_t readNtohU16 (void);
   213         inline uint16_t ReadNtohU16 (void);
   214         /**
   214         /**
   215          * \return the four bytes read in the buffer.
   215          * \return the four bytes read in the buffer.
   216          *
   216          *
   217          * Read data and advance the Iterator by the number of bytes
   217          * Read data and advance the Iterator by the number of bytes
   218          * read.
   218          * read.
   219          * The data is read in network format and return in host format.
   219          * The data is read in network format and return in host format.
   220          */
   220          */
   221         inline uint32_t readNtohU32 (void);
   221         inline uint32_t ReadNtohU32 (void);
   222         /**
   222         /**
   223          * \return the eight bytes read in the buffer.
   223          * \return the eight bytes read in the buffer.
   224          *
   224          *
   225          * Read data and advance the Iterator by the number of bytes
   225          * Read data and advance the Iterator by the number of bytes
   226          * read.
   226          * read.
   227          * The data is read in network format and return in host format.
   227          * The data is read in network format and return in host format.
   228          */
   228          */
   229         inline uint64_t readNtohU64 (void);
   229         inline uint64_t ReadNtohU64 (void);
   230         /**
   230         /**
   231          * \param buffer buffer to copy data into
   231          * \param buffer buffer to copy data into
   232          * \param size number of bytes to copy
   232          * \param size number of bytes to copy
   233          *
   233          *
   234          * Copy size bytes of data from the internal buffer to the
   234          * Copy size bytes of data from the internal buffer to the
   235          * input buffer and avance the Iterator by the number of
   235          * input buffer and avance the Iterator by the number of
   236          * bytes read.
   236          * bytes read.
   237          */
   237          */
   238         inline void read (uint8_t *buffer, uint16_t size);
   238         inline void Read (uint8_t *buffer, uint16_t size);
   239     private:
   239     private:
   240         friend class Buffer;
   240         friend class Buffer;
   241         inline Iterator (Buffer const*buffer, uint32_t m_current);
   241         inline Iterator (Buffer const*buffer, uint32_t m_current);
   242         inline uint32_t getIndex (uint32_t n);
   242         inline uint32_t GetIndex (uint32_t n);
   243         uint32_t m_zeroStart;
   243         uint32_t m_zeroStart;
   244         uint32_t m_zeroEnd;
   244         uint32_t m_zeroEnd;
   245         uint32_t m_dataEnd;
   245         uint32_t m_dataEnd;
   246         uint32_t m_current;
   246         uint32_t m_current;
   247         uint8_t *m_data;
   247         uint8_t *m_data;
   248     };
   248     };
   249 
   249 
   250     /**
   250     /**
   251      * \return the number of bytes stored in this buffer.
   251      * \return the number of bytes stored in this buffer.
   252      */
   252      */
   253     inline uint32_t getSize (void) const;
   253     inline uint32_t GetSize (void) const;
   254 
   254 
   255     /**
   255     /**
   256      * \return a pointer to the start of the internal 
   256      * \return a pointer to the start of the internal 
   257      * byte buffer.
   257      * byte buffer.
   258      *
   258      *
   259      * The returned pointer points to an area of
   259      * The returned pointer points to an area of
   260      * memory which is ns3::Buffer::getSize () bytes big.
   260      * memory which is ns3::Buffer::GetSize () bytes big.
   261      * Please, try to never ever use this method. It is really
   261      * Please, try to never ever use this method. It is really
   262      * evil and is present only for a few specific uses.
   262      * evil and is present only for a few specific uses.
   263      */
   263      */
   264     uint8_t const*peekData (void) const;
   264     uint8_t const*PeekData (void) const;
   265 
   265 
   266     /**
   266     /**
   267      * \param start size to reserve
   267      * \param start size to reserve
   268      *
   268      *
   269      * Add bytes at the start of the Buffer. The
   269      * Add bytes at the start of the Buffer. The
   270      * content of these bytes is undefined but debugging
   270      * content of these bytes is undefined but debugging
   271      * builds initialize them to 0x33.
   271      * builds initialize them to 0x33.
   272      * Any call to this method invalidates any Iterator
   272      * Any call to this method invalidates any Iterator
   273      * pointing to this Buffer.
   273      * pointing to this Buffer.
   274      */
   274      */
   275     void addAtStart (uint32_t start);
   275     void AddAtStart (uint32_t start);
   276     /**
   276     /**
   277      * \param end size to reserve
   277      * \param end size to reserve
   278      *
   278      *
   279      * Add bytes at the end of the Buffer. The
   279      * Add bytes at the end of the Buffer. The
   280      * content of these bytes is undefined but debugging
   280      * content of these bytes is undefined but debugging
   281      * builds initialize them to 0x33.
   281      * builds initialize them to 0x33.
   282      * Any call to this method invalidates any Iterator
   282      * Any call to this method invalidates any Iterator
   283      * pointing to this Buffer.
   283      * pointing to this Buffer.
   284      */
   284      */
   285     void addAtEnd (uint32_t end);
   285     void AddAtEnd (uint32_t end);
   286     /**
   286     /**
   287      * \param start size to remove
   287      * \param start size to remove
   288      *
   288      *
   289      * Remove bytes at the start of the Buffer.
   289      * Remove bytes at the start of the Buffer.
   290      * Any call to this method invalidates any Iterator
   290      * Any call to this method invalidates any Iterator
   291      * pointing to this Buffer.
   291      * pointing to this Buffer.
   292      */
   292      */
   293     void removeAtStart (uint32_t start);
   293     void RemoveAtStart (uint32_t start);
   294     /**
   294     /**
   295      * \param end size to remove
   295      * \param end size to remove
   296      *
   296      *
   297      * Remove bytes at the end of the Buffer.
   297      * Remove bytes at the end of the Buffer.
   298      * Any call to this method invalidates any Iterator
   298      * Any call to this method invalidates any Iterator
   299      * pointing to this Buffer.
   299      * pointing to this Buffer.
   300      */
   300      */
   301     void removeAtEnd (uint32_t end);
   301     void RemoveAtEnd (uint32_t end);
   302 
   302 
   303     /**
   303     /**
   304      * \param start offset from start of packet
   304      * \param start offset from start of packet
   305      * \param length
   305      * \param length
   306      *
   306      *
   307      * \return a fragment of size length starting at offset
   307      * \return a fragment of size length starting at offset
   308      * start.
   308      * start.
   309      */
   309      */
   310     Buffer createFragment (uint32_t start, uint32_t length) const;
   310     Buffer CreateFragment (uint32_t start, uint32_t length) const;
   311 
   311 
   312     /**
   312     /**
   313      * \return an Iterator which points to the
   313      * \return an Iterator which points to the
   314      * start of this Buffer.
   314      * start of this Buffer.
   315      */
   315      */
   316     inline Buffer::Iterator begin (void) const;
   316     inline Buffer::Iterator Begin (void) const;
   317     /**
   317     /**
   318      * \return an Iterator which points to the
   318      * \return an Iterator which points to the
   319      * end of this Buffer.
   319      * end of this Buffer.
   320      */
   320      */
   321     inline Buffer::Iterator end (void) const;
   321     inline Buffer::Iterator End (void) const;
   322 
   322 
   323     inline Buffer (Buffer const &o);
   323     inline Buffer (Buffer const &o);
   324     inline Buffer &operator = (Buffer const &o);
   324     inline Buffer &operator = (Buffer const &o);
   325     inline Buffer ();
   325     inline Buffer ();
   326     inline Buffer (uint32_t dataSize);
   326     inline Buffer (uint32_t dataSize);
   334         uint32_t m_dirtySize;
   334         uint32_t m_dirtySize;
   335         uint8_t m_data[1];
   335         uint8_t m_data[1];
   336     };
   336     };
   337     typedef std::vector<struct Buffer::BufferData*> BufferDataList;
   337     typedef std::vector<struct Buffer::BufferData*> BufferDataList;
   338 
   338 
   339     inline uint8_t *getStart (void) const;
   339     inline uint8_t *GetStart (void) const;
   340     void transformIntoRealBuffer (void) const;
   340     void TransformIntoRealBuffer (void) const;
   341     static void recycle (struct Buffer::BufferData *data);
   341     static void Recycle (struct Buffer::BufferData *data);
   342     static struct Buffer::BufferData *create (void);
   342     static struct Buffer::BufferData *Create (void);
   343     static struct Buffer::BufferData *allocate (uint32_t size, uint32_t start);
   343     static struct Buffer::BufferData *Allocate (uint32_t size, uint32_t start);
   344     static void deallocate (struct Buffer::BufferData *data);
   344     static void Deallocate (struct Buffer::BufferData *data);
   345 
   345 
   346     static BufferDataList m_freeList;
   346     static BufferDataList m_freeList;
   347     static uint32_t m_maxTotalAddStart;
   347     static uint32_t m_maxTotalAddStart;
   348     static uint32_t m_maxTotalAddEnd;
   348     static uint32_t m_maxTotalAddEnd;
   349 
   349 
   364 #include <cassert>
   364 #include <cassert>
   365 
   365 
   366 namespace ns3 {
   366 namespace ns3 {
   367 
   367 
   368 Buffer::Buffer ()
   368 Buffer::Buffer ()
   369     : m_data (Buffer::create ()),
   369     : m_data (Buffer::Create ()),
   370       m_zeroAreaSize (0),
   370       m_zeroAreaSize (0),
   371       m_start (m_maxTotalAddStart),
   371       m_start (m_maxTotalAddStart),
   372       m_size (0)
   372       m_size (0)
   373 {
   373 {
   374     if (m_start > m_data->m_size) {
   374     if (m_start > m_data->m_size) {
   376     }
   376     }
   377     assert (m_start <= m_data->m_size);
   377     assert (m_start <= m_data->m_size);
   378 }
   378 }
   379 
   379 
   380 Buffer::Buffer (uint32_t dataSize)
   380 Buffer::Buffer (uint32_t dataSize)
   381     : m_data (Buffer::create ()),
   381     : m_data (Buffer::Create ()),
   382       m_zeroAreaSize (dataSize),
   382       m_zeroAreaSize (dataSize),
   383       m_start (m_maxTotalAddStart),
   383       m_start (m_maxTotalAddStart),
   384       m_size (0)
   384       m_size (0)
   385 {
   385 {
   386     if (m_start > m_data->m_size) {
   386     if (m_start > m_data->m_size) {
   405 {
   405 {
   406     if (m_data != o.m_data) {
   406     if (m_data != o.m_data) {
   407         // not assignment to self.
   407         // not assignment to self.
   408         m_data->m_count--;
   408         m_data->m_count--;
   409         if (m_data->m_count == 0) {
   409         if (m_data->m_count == 0) {
   410             recycle (m_data);
   410             Recycle (m_data);
   411         }
   411         }
   412         m_data = o.m_data;
   412         m_data = o.m_data;
   413         m_data->m_count++;
   413         m_data->m_count++;
   414     }
   414     }
   415     m_zeroAreaSize = o.m_zeroAreaSize;
   415     m_zeroAreaSize = o.m_zeroAreaSize;
   421 
   421 
   422 Buffer::~Buffer ()
   422 Buffer::~Buffer ()
   423 {
   423 {
   424     m_data->m_count--;
   424     m_data->m_count--;
   425     if (m_data->m_count == 0) {
   425     if (m_data->m_count == 0) {
   426         recycle (m_data);
   426         Recycle (m_data);
   427     }
   427     }
   428 }
   428 }
   429 
   429 
   430 
   430 
   431 uint8_t *
   431 uint8_t *
   432 Buffer::getStart (void) const
   432 Buffer::GetStart (void) const
   433 {
   433 {
   434     return m_data->m_data + m_start;
   434     return m_data->m_data + m_start;
   435 }
   435 }
   436 
   436 
   437 uint32_t 
   437 uint32_t 
   438 Buffer::getSize (void) const
   438 Buffer::GetSize (void) const
   439 {
   439 {
   440     return m_size + m_zeroAreaSize;
   440     return m_size + m_zeroAreaSize;
   441 }
   441 }
   442 
   442 
   443 Buffer::Iterator 
   443 Buffer::Iterator 
   444 Buffer::begin (void) const
   444 Buffer::Begin (void) const
   445 {
   445 {
   446     return Buffer::Iterator (this, 0);
   446     return Buffer::Iterator (this, 0);
   447 }
   447 }
   448 Buffer::Iterator 
   448 Buffer::Iterator 
   449 Buffer::end (void) const
   449 Buffer::End (void) const
   450 {
   450 {
   451     return Buffer::Iterator (this, getSize ());
   451     return Buffer::Iterator (this, GetSize ());
   452 }
   452 }
   453 
   453 
   454 
   454 
   455 Buffer::Iterator::Iterator ()
   455 Buffer::Iterator::Iterator ()
   456     : m_zeroStart (0),
   456     : m_zeroStart (0),
   460       m_data (0)
   460       m_data (0)
   461 {}
   461 {}
   462 Buffer::Iterator::Iterator (Buffer const*buffer, uint32_t current)
   462 Buffer::Iterator::Iterator (Buffer const*buffer, uint32_t current)
   463     : m_zeroStart (buffer->m_data->m_initialStart-buffer->m_start),
   463     : m_zeroStart (buffer->m_data->m_initialStart-buffer->m_start),
   464       m_zeroEnd (m_zeroStart+buffer->m_zeroAreaSize),
   464       m_zeroEnd (m_zeroStart+buffer->m_zeroAreaSize),
   465       m_dataEnd (buffer->getSize ()),
   465       m_dataEnd (buffer->GetSize ()),
   466       m_current (current),
   466       m_current (current),
   467       m_data (buffer->m_data->m_data+buffer->m_start)
   467       m_data (buffer->m_data->m_data+buffer->m_start)
   468 {}
   468 {}
   469 
   469 
   470 void 
   470 void 
   471 Buffer::Iterator::next (void)
   471 Buffer::Iterator::Next (void)
   472 {
   472 {
   473     assert (m_current + 1 <= m_dataEnd);
   473     assert (m_current + 1 <= m_dataEnd);
   474     m_current++;
   474     m_current++;
   475 }
   475 }
   476 void 
   476 void 
   477 Buffer::Iterator::prev (void)
   477 Buffer::Iterator::Prev (void)
   478 {
   478 {
   479     assert (m_current >= 1);
   479     assert (m_current >= 1);
   480     m_current--;
   480     m_current--;
   481 }
   481 }
   482 void 
   482 void 
   483 Buffer::Iterator::next (uint32_t delta)
   483 Buffer::Iterator::Next (uint32_t delta)
   484 {
   484 {
   485     assert (m_current + delta <= m_dataEnd);
   485     assert (m_current + delta <= m_dataEnd);
   486     m_current += delta;
   486     m_current += delta;
   487 }
   487 }
   488 void 
   488 void 
   489 Buffer::Iterator::prev (uint32_t delta)
   489 Buffer::Iterator::Prev (uint32_t delta)
   490 {
   490 {
   491     assert (m_current >= delta);
   491     assert (m_current >= delta);
   492     m_current -= delta;
   492     m_current -= delta;
   493 }
   493 }
   494 int32_t
   494 int32_t
   495 Buffer::Iterator::getDistanceFrom (Iterator const &o) const
   495 Buffer::Iterator::GetDistanceFrom (Iterator const &o) const
   496 {
   496 {
   497     assert (m_data == o.m_data);
   497     assert (m_data == o.m_data);
   498     int32_t start = m_current;
   498     int32_t start = m_current;
   499     int32_t end = o.m_current;
   499     int32_t end = o.m_current;
   500     return end - start;
   500     return end - start;
   501 }
   501 }
   502 
   502 
   503 bool 
   503 bool 
   504 Buffer::Iterator::isEnd (void) const
   504 Buffer::Iterator::IsEnd (void) const
   505 {
   505 {
   506     return m_current == m_dataEnd;
   506     return m_current == m_dataEnd;
   507 }
   507 }
   508 bool 
   508 bool 
   509 Buffer::Iterator::isStart (void) const
   509 Buffer::Iterator::IsStart (void) const
   510 {
   510 {
   511     return m_current == 0;
   511     return m_current == 0;
   512 }
   512 }
   513 
   513 
   514 uint32_t
   514 uint32_t
   515 Buffer::Iterator::getIndex (uint32_t n)
   515 Buffer::Iterator::GetIndex (uint32_t n)
   516 {
   516 {
   517     assert ( 
   517     assert ( 
   518         (m_current + n <= m_dataEnd) &&
   518         (m_current + n <= m_dataEnd) &&
   519         ((m_current + n <= m_zeroStart) ||
   519         ((m_current + n <= m_zeroStart) ||
   520          (m_current >= m_zeroEnd))
   520          (m_current >= m_zeroEnd))
   528     return index;
   528     return index;
   529 }
   529 }
   530 
   530 
   531 
   531 
   532 void 
   532 void 
   533 Buffer::Iterator::write (Iterator start, Iterator end)
   533 Buffer::Iterator::Write (Iterator start, Iterator end)
   534 {
   534 {
   535     assert (start.m_data == end.m_data);
   535     assert (start.m_data == end.m_data);
   536     assert (start.m_current <= end.m_current);
   536     assert (start.m_current <= end.m_current);
   537     assert (m_data != start.m_data);
   537     assert (m_data != start.m_data);
   538     uint32_t size = end.m_current - start.m_current;
   538     uint32_t size = end.m_current - start.m_current;
   539     uint8_t *src = start.m_data + start.getIndex (size);
   539     uint8_t *src = start.m_data + start.GetIndex (size);
   540     uint8_t *dest = m_data + getIndex (size);
   540     uint8_t *dest = m_data + GetIndex (size);
   541     memcpy (dest, src, size);
   541     memcpy (dest, src, size);
   542     m_current += size;
   542     m_current += size;
   543 }
   543 }
   544 
   544 
   545 void 
   545 void 
   546 Buffer::Iterator::writeU8 (uint8_t  data, uint32_t len)
   546 Buffer::Iterator::WriteU8 (uint8_t  data, uint32_t len)
   547 {
   547 {
   548     uint8_t *current = m_data + getIndex (len);
   548     uint8_t *current = m_data + GetIndex (len);
   549     memset (current, data, len);
   549     memset (current, data, len);
   550     m_current += len;
   550     m_current += len;
   551 }
   551 }
   552 void 
   552 void 
   553 Buffer::Iterator::writeU8  (uint8_t  data)
   553 Buffer::Iterator::WriteU8  (uint8_t  data)
   554 {
   554 {
   555     m_data[getIndex (1)] = data;
   555     m_data[GetIndex (1)] = data;
   556     m_current++;
   556     m_current++;
   557 }
   557 }
   558 void 
   558 void 
   559 Buffer::Iterator::writeU16 (uint16_t data)
   559 Buffer::Iterator::WriteU16 (uint16_t data)
   560 {
   560 {
   561     uint16_t *buffer = (uint16_t *)(m_data + getIndex (2));
   561     uint16_t *buffer = (uint16_t *)(m_data + GetIndex (2));
   562     *buffer = data;
   562     *buffer = data;
   563     m_current += 2;
   563     m_current += 2;
   564 }
   564 }
   565 void 
   565 void 
   566 Buffer::Iterator::writeU32 (uint32_t data)
   566 Buffer::Iterator::WriteU32 (uint32_t data)
   567 {
   567 {
   568     uint32_t *buffer = (uint32_t *)(m_data + getIndex (4));
   568     uint32_t *buffer = (uint32_t *)(m_data + GetIndex (4));
   569     *buffer = data;
   569     *buffer = data;
   570     m_current += 4;
   570     m_current += 4;
   571 }
   571 }
   572 void 
   572 void 
   573 Buffer::Iterator::writeU64 (uint64_t data)
   573 Buffer::Iterator::WriteU64 (uint64_t data)
   574 {
   574 {
   575     uint64_t *buffer = (uint64_t *)(m_data + getIndex (8));
   575     uint64_t *buffer = (uint64_t *)(m_data + GetIndex (8));
   576     *buffer = data;
   576     *buffer = data;
   577     m_current += 8;
   577     m_current += 8;
   578 }
   578 }
   579 void 
   579 void 
   580 Buffer::Iterator::writeHtonU16 (uint16_t data)
   580 Buffer::Iterator::WriteHtonU16 (uint16_t data)
   581 {
   581 {
   582     uint8_t *current = m_data + getIndex (2);
   582     uint8_t *current = m_data + GetIndex (2);
   583     *(current+0) = (data >> 8) & 0xff;
   583     *(current+0) = (data >> 8) & 0xff;
   584     *(current+1) = (data >> 0) & 0xff;
   584     *(current+1) = (data >> 0) & 0xff;
   585     m_current += 2;
   585     m_current += 2;
   586 }
   586 }
   587 void 
   587 void 
   588 Buffer::Iterator::writeHtonU32 (uint32_t data)
   588 Buffer::Iterator::WriteHtonU32 (uint32_t data)
   589 {
   589 {
   590     uint8_t *current = m_data + getIndex (4);
   590     uint8_t *current = m_data + GetIndex (4);
   591     *(current+0) = (data >> 24) & 0xff;
   591     *(current+0) = (data >> 24) & 0xff;
   592     *(current+1) = (data >> 16) & 0xff;
   592     *(current+1) = (data >> 16) & 0xff;
   593     *(current+2) = (data >> 8) & 0xff;
   593     *(current+2) = (data >> 8) & 0xff;
   594     *(current+3) = (data >> 0) & 0xff;
   594     *(current+3) = (data >> 0) & 0xff;
   595     m_current += 4;
   595     m_current += 4;
   596 }
   596 }
   597 void 
   597 void 
   598 Buffer::Iterator::writeHtonU64 (uint64_t data)
   598 Buffer::Iterator::WriteHtonU64 (uint64_t data)
   599 {
   599 {
   600     uint8_t *current = m_data + getIndex (8);
   600     uint8_t *current = m_data + GetIndex (8);
   601     *(current+0) = (data >> 56) & 0xff;
   601     *(current+0) = (data >> 56) & 0xff;
   602     *(current+1) = (data >> 48) & 0xff;
   602     *(current+1) = (data >> 48) & 0xff;
   603     *(current+2) = (data >> 40) & 0xff;
   603     *(current+2) = (data >> 40) & 0xff;
   604     *(current+3) = (data >> 32) & 0xff;
   604     *(current+3) = (data >> 32) & 0xff;
   605     *(current+4) = (data >> 24) & 0xff;
   605     *(current+4) = (data >> 24) & 0xff;
   607     *(current+6) = (data >> 8) & 0xff;
   607     *(current+6) = (data >> 8) & 0xff;
   608     *(current+7) = (data >> 0) & 0xff;
   608     *(current+7) = (data >> 0) & 0xff;
   609     m_current += 8;
   609     m_current += 8;
   610 }
   610 }
   611 void 
   611 void 
   612 Buffer::Iterator::write (uint8_t const*buffer, uint16_t size)
   612 Buffer::Iterator::Write (uint8_t const*buffer, uint16_t size)
   613 {
   613 {
   614     uint8_t *current = m_data + getIndex (size);
   614     uint8_t *current = m_data + GetIndex (size);
   615     memcpy (current, buffer, size);
   615     memcpy (current, buffer, size);
   616     m_current += size;
   616     m_current += size;
   617 }
   617 }
   618 
   618 
   619 uint8_t  
   619 uint8_t  
   620 Buffer::Iterator::readU8 (void)
   620 Buffer::Iterator::ReadU8 (void)
   621 {
   621 {
   622     uint8_t data = m_data[getIndex(1)];
   622     uint8_t data = m_data[GetIndex(1)];
   623     m_current++;
   623     m_current++;
   624     return data;
   624     return data;
   625 }
   625 }
   626 uint16_t 
   626 uint16_t 
   627 Buffer::Iterator::readU16 (void)
   627 Buffer::Iterator::ReadU16 (void)
   628 {
   628 {
   629     uint16_t *buffer = reinterpret_cast<uint16_t *>(m_data + getIndex (2));
   629     uint16_t *buffer = reinterpret_cast<uint16_t *>(m_data + GetIndex (2));
   630     m_current += 2;
   630     m_current += 2;
   631     return *buffer;
   631     return *buffer;
   632 }
   632 }
   633 uint32_t 
   633 uint32_t 
   634 Buffer::Iterator::readU32 (void)
   634 Buffer::Iterator::ReadU32 (void)
   635 {
   635 {
   636     uint32_t *buffer = reinterpret_cast<uint32_t *>(m_data + getIndex (4));
   636     uint32_t *buffer = reinterpret_cast<uint32_t *>(m_data + GetIndex (4));
   637     m_current += 4;
   637     m_current += 4;
   638     return *buffer;
   638     return *buffer;
   639 }
   639 }
   640 uint64_t 
   640 uint64_t 
   641 Buffer::Iterator::readU64 (void)
   641 Buffer::Iterator::ReadU64 (void)
   642 {
   642 {
   643     uint64_t *buffer = reinterpret_cast<uint64_t *>(m_data + getIndex (8));
   643     uint64_t *buffer = reinterpret_cast<uint64_t *>(m_data + GetIndex (8));
   644     m_current += 8;
   644     m_current += 8;
   645     return *buffer;
   645     return *buffer;
   646 }
   646 }
   647 uint16_t 
   647 uint16_t 
   648 Buffer::Iterator::readNtohU16 (void)
   648 Buffer::Iterator::ReadNtohU16 (void)
   649 {
   649 {
   650     uint8_t *current = m_data + getIndex (2);
   650     uint8_t *current = m_data + GetIndex (2);
   651     uint16_t retval = 0;
   651     uint16_t retval = 0;
   652     retval |= static_cast<uint16_t> (current[0]) << 8;
   652     retval |= static_cast<uint16_t> (current[0]) << 8;
   653     retval |= static_cast<uint16_t> (current[1]) << 0;
   653     retval |= static_cast<uint16_t> (current[1]) << 0;
   654     m_current += 2;
   654     m_current += 2;
   655     return retval;
   655     return retval;
   656 }
   656 }
   657 uint32_t 
   657 uint32_t 
   658 Buffer::Iterator::readNtohU32 (void)
   658 Buffer::Iterator::ReadNtohU32 (void)
   659 {
   659 {
   660     uint8_t *current = m_data + getIndex (4);
   660     uint8_t *current = m_data + GetIndex (4);
   661     uint32_t retval = 0;
   661     uint32_t retval = 0;
   662     retval |= static_cast<uint32_t> (current[0]) << 24;
   662     retval |= static_cast<uint32_t> (current[0]) << 24;
   663     retval |= static_cast<uint32_t> (current[1]) << 16;
   663     retval |= static_cast<uint32_t> (current[1]) << 16;
   664     retval |= static_cast<uint32_t> (current[2]) << 8;
   664     retval |= static_cast<uint32_t> (current[2]) << 8;
   665     retval |= static_cast<uint32_t> (current[3]) << 0;
   665     retval |= static_cast<uint32_t> (current[3]) << 0;
   666     m_current += 4;
   666     m_current += 4;
   667     return retval;
   667     return retval;
   668 }
   668 }
   669 uint64_t 
   669 uint64_t 
   670 Buffer::Iterator::readNtohU64 (void)
   670 Buffer::Iterator::ReadNtohU64 (void)
   671 {
   671 {
   672     uint8_t *current = m_data + getIndex (8);
   672     uint8_t *current = m_data + GetIndex (8);
   673     uint64_t retval = 0;
   673     uint64_t retval = 0;
   674     retval |= static_cast<uint64_t> (current[0]) << 56;
   674     retval |= static_cast<uint64_t> (current[0]) << 56;
   675     retval |= static_cast<uint64_t> (current[1]) << 48;
   675     retval |= static_cast<uint64_t> (current[1]) << 48;
   676     retval |= static_cast<uint64_t> (current[2]) << 40;
   676     retval |= static_cast<uint64_t> (current[2]) << 40;
   677     retval |= static_cast<uint64_t> (current[3]) << 32;
   677     retval |= static_cast<uint64_t> (current[3]) << 32;
   681     retval |= static_cast<uint64_t> (current[7]) << 0;
   681     retval |= static_cast<uint64_t> (current[7]) << 0;
   682     m_current += 8;
   682     m_current += 8;
   683     return retval;
   683     return retval;
   684 }
   684 }
   685 void 
   685 void 
   686 Buffer::Iterator::read (uint8_t *buffer, uint16_t size)
   686 Buffer::Iterator::Read (uint8_t *buffer, uint16_t size)
   687 {
   687 {
   688     uint8_t *current = m_data + getIndex (size);
   688     uint8_t *current = m_data + GetIndex (size);
   689     memcpy (buffer, current, size);
   689     memcpy (buffer, current, size);
   690     m_current += size;
   690     m_current += size;
   691 }
   691 }
   692 
   692 
   693 }; // namespace ns3
   693 }; // namespace ns3