21 #include "ns3/assert.h"
26 #define LOG_INTERNAL_STATE(y) \
27 NS_LOG_LOGIC (y << "start="<<m_start<<", end="<<m_end<<", zero start="<<m_zeroAreaStart<< \
28 ", zero end="<<m_zeroAreaEnd<<", count="<<m_data->m_count<<", size="<<m_data->m_size<< \
29 ", dirty start="<<m_data->m_dirtyStart<<", dirty end="<<m_data->m_dirtyEnd)
38 memset (buffer, 0, size);
50 #ifdef BUFFER_FREE_LIST
68 #define MAGIC_DESTROYED (~(long) 0)
69 #define IS_UNINITIALIZED(x) (x == (Buffer::FreeList*)0)
70 #define IS_DESTROYED(x) (x == (Buffer::FreeList*)MAGIC_DESTROYED)
71 #define IS_INITIALIZED(x) (!IS_UNINITIALIZED (x) && !IS_DESTROYED (x))
72 #define DESTROYED ((Buffer::FreeList*)MAGIC_DESTROYED)
73 #define UNINITIALIZED ((Buffer::FreeList*)0)
76 struct Buffer::LocalStaticDestructor Buffer::g_localStaticDestructor;
78 Buffer::LocalStaticDestructor::~LocalStaticDestructor(void)
83 for (Buffer::FreeList::iterator i =
g_freeList->begin ();
127 struct Buffer::Data *data =
g_freeList->back ();
129 if (data->m_size >= dataSize)
167 uint32_t size = reqSize - 1 +
sizeof (
struct Buffer::Data);
168 uint8_t *b =
new uint8_t [size];
180 uint8_t *buf =
reinterpret_cast<uint8_t *
> (
data);
199 if (initialize ==
true)
222 m_start <= m_data->m_size &&
223 m_zeroAreaStart <= m_data->m_size;
225 bool ok =
m_data->
m_count > 0 && offsetsOk && dirtyOk && internalSizeOk;
229 ", " << (offsetsOk ?
"true" :
"false") <<
230 ", " << (dirtyOk ?
"true" :
"false") <<
231 ", " << (internalSizeOk ?
"true" :
"false") <<
" ");
311 if (
m_start >= start && !isDirty)
336 int32_t delta = start -
m_start;
472 else if (newStart <=
m_end)
490 m_zeroAreaStart =
m_end;
580 uint32_t sz =
sizeof (uint32_t)
593 uint32_t* p =
reinterpret_cast<uint32_t *
> (buffer);
597 if (size + 4 <= maxSize)
609 if (size + 4 <= maxSize)
612 *p++ = dataStartLength;
620 if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
622 size += (dataStartLength + 3) & (~3);
624 p += (((dataStartLength + 3) & (~3))/4);
633 if (size + 4 <= maxSize)
636 *p++ = dataEndLength;
644 if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
646 size += (dataEndLength + 3) & (~3);
648 p += (((dataEndLength + 3) & (~3))/4);
663 const uint32_t* p =
reinterpret_cast<const uint32_t *
> (buffer);
664 uint32_t sizeCheck = size-4;
667 uint32_t zeroDataLength = *p++;
675 uint32_t dataStartLength = *p++;
679 NS_ASSERT (sizeCheck >= dataStartLength);
680 Begin ().
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataStartLength);
681 p += (((dataStartLength+3)&(~3))/4);
682 sizeCheck -= ((dataStartLength+3)&(~3));
686 uint32_t dataEndLength = *p++;
692 tmp.
Prev (dataEndLength);
693 tmp.
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataEndLength);
694 p += (((dataEndLength+3)&(~3))/4);
695 sizeCheck -= ((dataEndLength+3)&(~3));
700 return (sizeCheck != 0) ? 0 : 1;
723 *
const_cast<Buffer *
> (
this) = tmp;
750 uint32_t left = tmpsize;
771 uint32_t originalSize = size;
781 uint32_t left = tmpsize;
798 return originalSize - size;
826 return m_current == m_dataEnd;
832 return m_current == m_dataStart;
840 for (uint32_t i = start; i < end; i++)
853 return i >= m_dataStart &&
854 !(i >= m_zeroStart && i < m_zeroEnd) &&
870 GetWriteErrorMessage ());
882 memset (&
m_data[m_current], 0, toCopy);
889 uint8_t *to = &
m_data[m_current];
890 memcpy (to, from, toCopy);
898 WriteU8 (data & 0xff);
900 WriteU8 (data & 0xff);
906 WriteU8 (data & 0xff);
908 WriteU8 (data & 0xff);
910 WriteU8 (data & 0xff);
912 WriteU8 (data & 0xff);
918 WriteU8 (data & 0xff);
920 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);
938 WriteU8 ((data >> 0) & 0xff);
939 WriteU8 ((data >> 8) & 0xff);
945 WriteU8 ((data >> 0) & 0xff);
946 WriteU8 ((data >> 8) & 0xff);
947 WriteU8 ((data >> 16) & 0xff);
948 WriteU8 ((data >> 24) & 0xff);
954 WriteU8 ((data >> 0) & 0xff);
955 WriteU8 ((data >> 8) & 0xff);
956 WriteU8 ((data >> 16) & 0xff);
957 WriteU8 ((data >> 24) & 0xff);
958 WriteU8 ((data >> 32) & 0xff);
959 WriteU8 ((data >> 40) & 0xff);
960 WriteU8 ((data >> 48) & 0xff);
961 WriteU8 ((data >> 56) & 0xff);
968 WriteU8 ((data >> 56) & 0xff);
969 WriteU8 ((data >> 48) & 0xff);
970 WriteU8 ((data >> 40) & 0xff);
971 WriteU8 ((data >> 32) & 0xff);
972 WriteU8 ((data >> 24) & 0xff);
973 WriteU8 ((data >> 16) & 0xff);
974 WriteU8 ((data >> 8) & 0xff);
975 WriteU8 ((data >> 0) & 0xff);
982 GetWriteErrorMessage ());
984 if (m_current <= m_zeroStart)
990 to = &
m_data[m_current - (m_zeroEnd - m_zeroStart)];
992 memcpy (to, buffer, size);
1000 uint8_t byte0 = ReadU8 ();
1001 uint8_t byte1 = ReadU8 ();
1002 uint8_t byte2 = ReadU8 ();
1003 uint8_t byte3 = ReadU8 ();
1004 uint32_t data = byte3;
1017 uint8_t byte0 = ReadU8 ();
1018 uint8_t byte1 = ReadU8 ();
1019 uint8_t byte2 = ReadU8 ();
1020 uint8_t byte3 = ReadU8 ();
1021 uint8_t byte4 = ReadU8 ();
1022 uint8_t byte5 = ReadU8 ();
1023 uint8_t byte6 = ReadU8 ();
1024 uint8_t byte7 = ReadU8 ();
1025 uint64_t data = byte7;
1047 uint16_t retval = 0;
1048 retval |= ReadU8 ();
1050 retval |= ReadU8 ();
1057 uint32_t retval = 0;
1058 retval |= ReadU8 ();
1060 retval |= ReadU8 ();
1062 retval |= ReadU8 ();
1064 retval |= ReadU8 ();
1071 uint64_t retval = 0;
1072 retval |= ReadU8 ();
1074 retval |= ReadU8 ();
1076 retval |= ReadU8 ();
1078 retval |= ReadU8 ();
1080 retval |= ReadU8 ();
1082 retval |= ReadU8 ();
1084 retval |= ReadU8 ();
1086 retval |= ReadU8 ();
1093 uint8_t byte0 = ReadU8 ();
1094 uint8_t byte1 = ReadU8 ();
1095 uint16_t data = byte1;
1104 uint8_t byte0 = ReadU8 ();
1105 uint8_t byte1 = ReadU8 ();
1106 uint8_t byte2 = ReadU8 ();
1107 uint8_t byte3 = ReadU8 ();
1108 uint32_t data = byte3;
1121 uint8_t byte0 = ReadU8 ();
1122 uint8_t byte1 = ReadU8 ();
1123 uint8_t byte2 = ReadU8 ();
1124 uint8_t byte3 = ReadU8 ();
1125 uint8_t byte4 = ReadU8 ();
1126 uint8_t byte5 = ReadU8 ();
1127 uint8_t byte6 = ReadU8 ();
1128 uint8_t byte7 = ReadU8 ();
1129 uint64_t data = byte7;
1151 for (uint32_t i = 0; i < size; i++)
1153 buffer[i] = ReadU8 ();
1161 return CalculateIpChecksum (size, 0);
1169 uint32_t sum = initialChecksum;
1171 for (
int j = 0; j < size/2; j++)
1178 sum = (sum & 0xffff) + (sum >> 16);
1186 return m_dataEnd - m_dataStart;
1194 std::string str =
"You have attempted to read beyond the bounds of the "
1195 "available buffer space. This usually indicates that a "
1196 "Header::Deserialize or Trailer::Deserialize method "
1197 "is trying to read data which was not written by "
1198 "a Header::Serialize or Trailer::Serialize method. "
1199 "In short: check the code of your Serialize and Deserialize "
1208 if (m_current < m_dataStart)
1210 str =
"You have attempted to write before the start of the available "
1211 "buffer space. This usually indicates that Trailer::GetSerializedSize "
1212 "returned a size which is too small compared to what Trailer::Serialize "
1213 "is actually using.";
1215 else if (m_current >= m_dataEnd)
1217 str =
"You have attempted to write after the end of the available "
1218 "buffer space. This usually indicates that Header::GetSerializedSize "
1219 "returned a size which is too small compared to what Header::Serialize "
1220 "is actually using.";
1224 NS_ASSERT (m_current >= m_zeroStart && m_current < m_zeroEnd);
1225 str =
"You have attempted to write inside the payload area of the "
1226 "buffer. This usually indicates that your Serialize method uses more "
1227 "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)
void WriteHtonU64(uint64_t data)
#define NS_LOG_FUNCTION(parameters)
void RemoveAtEnd(uint32_t end)
void RemoveAtStart(uint32_t start)
static struct anonymous_namespace{buffer.cc}::Zeroes g_zeroes
uint64_t ReadNtohU64(void)
#define NS_ASSERT(condition)
uint32_t GetSerializedSize(void) const
bool CheckInternalState(void) const
Buffer & operator=(Buffer const &o)
uint32_t GetDistanceFrom(Iterator const &o) const
iterator in a Buffer instance
uint32_t SlowReadNtohU32(void)
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
This data structure is variable-sized through its last member whose size is determined at allocation ...
std::string GetWriteErrorMessage(void) const
void Prev(void)
go backward by one byte
void WriteU16(uint16_t data)
static struct Buffer::Data * Create(uint32_t size)
Buffer::Iterator End(void) const
uint8_t const * PeekData(void) const
NS_LOG_COMPONENT_DEFINE("Buffer")
void WriteU64(uint64_t data)
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
void TransformIntoRealBuffer(void) const
Buffer::Iterator Begin(void) const
bool Check(uint32_t i) const
bool CheckNoZero(uint32_t start, uint32_t end) const
std::string GetReadErrorMessage(void) const
static void Recycle(struct Buffer::Data *data)
void Read(uint8_t *buffer, uint32_t size)
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)
#define NS_ASSERT_MSG(condition, message)
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
void WriteU8(uint8_t data)
ns3::ByteTagListDataFreeList g_freeList
uint32_t m_maxZeroAreaStart
void WriteHtolsbU64(uint64_t data)
Buffer CreateFullCopy(void) const
#define LOG_INTERNAL_STATE(y)
static void Deallocate(struct Buffer::Data *data)
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
uint16_t ReadLsbtohU16(void)
bool AddAtStart(uint32_t start)
int32_t GetCurrentStartOffset(void) const
uint64_t ReadLsbtohU64(void)
void WriteU32(uint32_t data)
uint32_t GetInternalEnd(void) const
uint32_t GetSize(void) const
static uint32_t g_maxSize
uint32_t ReadLsbtohU32(void)
void WriteHtolsbU32(uint32_t data)