21 #include "ns3/assert.h"
24 #define LOG_INTERNAL_STATE(y) \
25 NS_LOG_LOGIC (y << "start="<<m_start<<", end="<<m_end<<", zero start="<<m_zeroAreaStart<< \
26 ", zero end="<<m_zeroAreaEnd<<", count="<<m_data->m_count<<", size="<<m_data->m_size<< \
27 ", dirty start="<<m_data->m_dirtyStart<<", dirty end="<<m_data->m_dirtyEnd)
40 memset (buffer, 0, size);
54 #ifdef BUFFER_FREE_LIST
72 #define MAGIC_DESTROYED (~(long) 0)
73 #define IS_UNINITIALIZED(x) (x == (Buffer::FreeList*)0)
74 #define IS_DESTROYED(x) (x == (Buffer::FreeList*)MAGIC_DESTROYED)
75 #define IS_INITIALIZED(x) (!IS_UNINITIALIZED (x) && !IS_DESTROYED (x))
76 #define DESTROYED ((Buffer::FreeList*)MAGIC_DESTROYED)
77 #define UNINITIALIZED ((Buffer::FreeList*)0)
80 struct Buffer::LocalStaticDestructor Buffer::g_localStaticDestructor;
87 for (Buffer::FreeList::iterator i =
g_freeList->begin ();
133 if (data->
m_size >= dataSize)
162 struct Buffer::Data *
171 uint32_t size = reqSize - 1 +
sizeof (
struct Buffer::Data);
172 uint8_t *b =
new uint8_t [size];
184 uint8_t *buf =
reinterpret_cast<uint8_t *
> (
data);
203 if (initialize ==
true)
226 m_start <= m_data->m_size &&
227 m_zeroAreaStart <= m_data->m_size;
229 bool ok =
m_data->
m_count > 0 && offsetsOk && dirtyOk && internalSizeOk;
233 ", " << (offsetsOk ?
"true" :
"false") <<
234 ", " << (dirtyOk ?
"true" :
"false") <<
235 ", " << (internalSizeOk ?
"true" :
"false") <<
" ");
315 if (
m_start >= start && !isDirty)
340 int32_t delta = start -
m_start;
476 else if (newStart <=
m_end)
494 m_zeroAreaStart =
m_end;
584 uint32_t sz =
sizeof (uint32_t)
597 uint32_t* p =
reinterpret_cast<uint32_t *
> (buffer);
601 if (size + 4 <= maxSize)
613 if (size + 4 <= maxSize)
616 *p++ = dataStartLength;
624 if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
626 size += (dataStartLength + 3) & (~3);
628 p += (((dataStartLength + 3) & (~3))/4);
637 if (size + 4 <= maxSize)
640 *p++ = dataEndLength;
648 if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
650 size += (dataEndLength + 3) & (~3);
652 p += (((dataEndLength + 3) & (~3))/4);
667 const uint32_t* p =
reinterpret_cast<const uint32_t *
> (buffer);
668 uint32_t sizeCheck = size-4;
671 uint32_t zeroDataLength = *p++;
679 uint32_t dataStartLength = *p++;
683 NS_ASSERT (sizeCheck >= dataStartLength);
684 Begin ().
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataStartLength);
685 p += (((dataStartLength+3)&(~3))/4);
686 sizeCheck -= ((dataStartLength+3)&(~3));
690 uint32_t dataEndLength = *p++;
696 tmp.
Prev (dataEndLength);
697 tmp.
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataEndLength);
698 p += (((dataEndLength+3)&(~3))/4);
699 sizeCheck -= ((dataEndLength+3)&(~3));
704 return (sizeCheck != 0) ? 0 : 1;
727 *
const_cast<Buffer *
> (
this) = tmp;
754 uint32_t left = tmpsize;
775 uint32_t originalSize = size;
785 uint32_t left = tmpsize;
802 return originalSize - size;
830 return m_current == m_dataEnd;
836 return m_current == m_dataStart;
844 for (uint32_t i = start; i < end; i++)
857 return i >= m_dataStart &&
858 !(i >= m_zeroStart && i < m_zeroEnd) &&
874 GetWriteErrorMessage ());
886 memset (&
m_data[m_current], 0, toCopy);
893 uint8_t *to = &
m_data[m_current];
894 memcpy (to, from, toCopy);
902 WriteU8 (data & 0xff);
904 WriteU8 (data & 0xff);
910 WriteU8 (data & 0xff);
912 WriteU8 (data & 0xff);
914 WriteU8 (data & 0xff);
916 WriteU8 (data & 0xff);
922 WriteU8 (data & 0xff);
924 WriteU8 (data & 0xff);
926 WriteU8 (data & 0xff);
928 WriteU8 (data & 0xff);
930 WriteU8 (data & 0xff);
932 WriteU8 (data & 0xff);
934 WriteU8 (data & 0xff);
936 WriteU8 (data & 0xff);
942 WriteU8 ((data >> 0) & 0xff);
943 WriteU8 ((data >> 8) & 0xff);
949 WriteU8 ((data >> 0) & 0xff);
950 WriteU8 ((data >> 8) & 0xff);
951 WriteU8 ((data >> 16) & 0xff);
952 WriteU8 ((data >> 24) & 0xff);
958 WriteU8 ((data >> 0) & 0xff);
959 WriteU8 ((data >> 8) & 0xff);
960 WriteU8 ((data >> 16) & 0xff);
961 WriteU8 ((data >> 24) & 0xff);
962 WriteU8 ((data >> 32) & 0xff);
963 WriteU8 ((data >> 40) & 0xff);
964 WriteU8 ((data >> 48) & 0xff);
965 WriteU8 ((data >> 56) & 0xff);
972 WriteU8 ((data >> 56) & 0xff);
973 WriteU8 ((data >> 48) & 0xff);
974 WriteU8 ((data >> 40) & 0xff);
975 WriteU8 ((data >> 32) & 0xff);
976 WriteU8 ((data >> 24) & 0xff);
977 WriteU8 ((data >> 16) & 0xff);
978 WriteU8 ((data >> 8) & 0xff);
979 WriteU8 ((data >> 0) & 0xff);
986 GetWriteErrorMessage ());
988 if (m_current <= m_zeroStart)
994 to = &
m_data[m_current - (m_zeroEnd - m_zeroStart)];
996 memcpy (to, buffer, size);
1004 uint8_t byte0 = ReadU8 ();
1005 uint8_t byte1 = ReadU8 ();
1006 uint8_t byte2 = ReadU8 ();
1007 uint8_t byte3 = ReadU8 ();
1008 uint32_t
data = byte3;
1021 uint8_t byte0 = ReadU8 ();
1022 uint8_t byte1 = ReadU8 ();
1023 uint8_t byte2 = ReadU8 ();
1024 uint8_t byte3 = ReadU8 ();
1025 uint8_t byte4 = ReadU8 ();
1026 uint8_t byte5 = ReadU8 ();
1027 uint8_t byte6 = ReadU8 ();
1028 uint8_t byte7 = ReadU8 ();
1029 uint64_t
data = byte7;
1051 uint16_t retval = 0;
1052 retval |= ReadU8 ();
1054 retval |= ReadU8 ();
1061 uint32_t retval = 0;
1062 retval |= ReadU8 ();
1064 retval |= ReadU8 ();
1066 retval |= ReadU8 ();
1068 retval |= ReadU8 ();
1075 uint64_t retval = 0;
1076 retval |= ReadU8 ();
1078 retval |= ReadU8 ();
1080 retval |= ReadU8 ();
1082 retval |= ReadU8 ();
1084 retval |= ReadU8 ();
1086 retval |= ReadU8 ();
1088 retval |= ReadU8 ();
1090 retval |= ReadU8 ();
1097 uint8_t byte0 = ReadU8 ();
1098 uint8_t byte1 = ReadU8 ();
1099 uint16_t
data = byte1;
1108 uint8_t byte0 = ReadU8 ();
1109 uint8_t byte1 = ReadU8 ();
1110 uint8_t byte2 = ReadU8 ();
1111 uint8_t byte3 = ReadU8 ();
1112 uint32_t
data = byte3;
1125 uint8_t byte0 = ReadU8 ();
1126 uint8_t byte1 = ReadU8 ();
1127 uint8_t byte2 = ReadU8 ();
1128 uint8_t byte3 = ReadU8 ();
1129 uint8_t byte4 = ReadU8 ();
1130 uint8_t byte5 = ReadU8 ();
1131 uint8_t byte6 = ReadU8 ();
1132 uint8_t byte7 = ReadU8 ();
1133 uint64_t
data = byte7;
1155 for (uint32_t i = 0; i < size; i++)
1157 buffer[i] = ReadU8 ();
1165 return CalculateIpChecksum (size, 0);
1173 uint32_t sum = initialChecksum;
1175 for (
int j = 0; j < size/2; j++)
1182 sum = (sum & 0xffff) + (sum >> 16);
1190 return m_dataEnd - m_dataStart;
1198 std::string str =
"You have attempted to read beyond the bounds of the "
1199 "available buffer space. This usually indicates that a "
1200 "Header::Deserialize or Trailer::Deserialize method "
1201 "is trying to read data which was not written by "
1202 "a Header::Serialize or Trailer::Serialize method. "
1203 "In short: check the code of your Serialize and Deserialize "
1212 if (m_current < m_dataStart)
1214 str =
"You have attempted to write before the start of the available "
1215 "buffer space. This usually indicates that Trailer::GetSerializedSize "
1216 "returned a size which is too small compared to what Trailer::Serialize "
1217 "is actually using.";
1219 else if (m_current >= m_dataEnd)
1221 str =
"You have attempted to write after the end of the available "
1222 "buffer space. This usually indicates that Header::GetSerializedSize "
1223 "returned a size which is too small compared to what Header::Serialize "
1224 "is actually using.";
1228 NS_ASSERT (m_current >= m_zeroStart && m_current < m_zeroEnd);
1229 str =
"You have attempted to write inside the payload area of the "
1230 "buffer. This usually indicates that your Serialize method uses more "
1231 "buffer space than what your GetSerialized method returned.";
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
uint16_t SlowReadNtohU16(void)
void Initialize(uint32_t zeroSize)
Initializes the buffer with a number of zeroes.
void WriteHtonU64(uint64_t data)
uint32_t m_end
offset to the end of the data referenced by this Buffer instance from the start of m_data->m_data ...
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void RemoveAtEnd(uint32_t end)
uint8_t * m_data
a pointer to the underlying byte buffer.
void RemoveAtStart(uint32_t start)
uint32_t m_size
the size of the m_data field below.
automatically resized byte buffer
uint64_t ReadNtohU64(void)
uint32_t m_count
The reference count of an instance of this data structure.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
bool CheckInternalState(void) const
Checks the internal buffer structures consistency.
Buffer & operator=(Buffer const &o)
Assignment operator.
Local static destructor structure.
uint32_t m_start
offset to the start of the data referenced by this Buffer instance from the start of m_data->m_data ...
uint32_t GetDistanceFrom(Iterator const &o) const
iterator in a Buffer instance
uint32_t SlowReadNtohU32(void)
#define IS_INITIALIZED(x)
uint32_t m_zeroStart
offset in virtual bytes from the start of the data buffer to the start of the "virtual zero area"...
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...
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
uint8_t m_data[1]
The real data buffer holds at least one byte.
This data structure is variable-sized through its last member whose size is determined at allocation ...
std::string GetWriteErrorMessage(void) const
Returns an appropriate message indicating a write error.
void Prev(void)
go backward by one byte
static struct anonymous_namespace{buffer.cc}::Zeroes g_zeroes
Zero-filled buffer.
void WriteU16(uint16_t data)
static struct Buffer::Data * Create(uint32_t size)
Create a buffer data storage.
Buffer::Iterator End(void) const
uint8_t const * PeekData(void) const
uint32_t m_current
offset in virtual bytes from the start of the data buffer to the current position represented by this...
void WriteU64(uint64_t data)
static FreeList * g_freeList
Buffer data container.
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
int32_t GetCurrentEndOffset(void) const
Returns the current buffer end offset.
void TransformIntoRealBuffer(void) const
Transform a "Virtual byte buffer" into a "Real byte buffer".
Buffer::Iterator Begin(void) const
bool Check(uint32_t i) const
Checks that the buffer position is not in the "virtual zero area".
std::vector< struct Buffer::Data * > FreeList
Container for buffer data.
bool CheckNoZero(uint32_t start, uint32_t end) const
Checks that the [start, end) is not in the "virtual zero area".
std::string GetReadErrorMessage(void) const
Returns an appropriate message indicating a read error.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static void Recycle(struct Buffer::Data *data)
Recycle the buffer memory.
static uint32_t g_maxSize
Max observed data size.
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...
void Read(uint8_t *buffer, uint32_t size)
uint32_t m_zeroEnd
offset in virtual bytes from the start of the data buffer to the end of the "virtual zero area"...
uint32_t m_zeroAreaStart
offset to the start of the virtual zero area from the start of m_data->m_data
uint32_t GetSize(void) const
Buffer CreateFragment(uint32_t start, uint32_t length) const
bool AddAtEnd(uint32_t end)
void WriteHtolsbU16(uint16_t data)
static struct Buffer::Data * Allocate(uint32_t reqSize)
Allocate a buffer data storage.
const uint32_t size
buffer size
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
struct Data * m_data
the buffer data storage
char buffer[1000]
buffer containing zero values
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...
void WriteU8(uint8_t data)
#define IS_UNINITIALIZED(x)
uint32_t m_maxZeroAreaStart
keep track of the maximum value of m_zeroAreaStart across the lifetime of a Buffer instance...
void WriteHtolsbU64(uint64_t data)
Buffer CreateFullCopy(void) const
Create a full copy of the buffer, including all the internal structures.
#define LOG_INTERNAL_STATE(y)
static void Deallocate(struct Buffer::Data *data)
Deallocate the buffer memory.
static uint32_t g_recommendedStart
location in a newly-allocated buffer where you should start writing data.
void Write(uint8_t const *buffer, uint32_t size)
uint32_t GetInternalSize(void) const
Get the buffer real size.
uint16_t ReadLsbtohU16(void)
bool AddAtStart(uint32_t start)
int32_t GetCurrentStartOffset(void) const
Returns the current buffer start offset.
uint64_t ReadLsbtohU64(void)
void WriteU32(uint32_t data)
uint32_t GetInternalEnd(void) const
Get the buffer end position.
uint32_t GetSize(void) const
uint32_t ReadLsbtohU32(void)
uint32_t m_zeroAreaEnd
offset to the end of the virtual zero area from the start of m_data->m_data
void WriteHtolsbU32(uint32_t data)