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];
174 data->m_size = reqSize;
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") <<
" ");
433 destStart.Write (o.
Begin (), o.
End ());
458 else if (newStart <=
m_end)
566 uint32_t sz =
sizeof (uint32_t)
579 uint32_t* p =
reinterpret_cast<uint32_t *
> (buffer);
583 if (size + 4 <= maxSize)
595 if (size + 4 <= maxSize)
598 *p++ = dataStartLength;
606 if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
608 size += (dataStartLength + 3) & (~3);
610 p += (((dataStartLength + 3) & (~3))/4);
619 if (size + 4 <= maxSize)
622 *p++ = dataEndLength;
630 if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
651 const uint32_t* p =
reinterpret_cast<const uint32_t *
> (buffer);
652 uint32_t sizeCheck = size-4;
655 uint32_t zeroDataLength = *p++;
663 uint32_t dataStartLength = *p++;
667 NS_ASSERT (sizeCheck >= dataStartLength);
668 Begin ().
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataStartLength);
669 p += (((dataStartLength+3)&(~3))/4);
670 sizeCheck -= ((dataStartLength+3)&(~3));
674 uint32_t dataEndLength = *p++;
680 tmp.
Prev (dataEndLength);
681 tmp.
Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataEndLength);
684 sizeCheck -= ((dataEndLength+3)&(~3));
689 return (sizeCheck != 0) ? 0 : 1;
699 *
const_cast<Buffer *
> (
this) = tmp;
726 uint32_t left = tmpsize;
747 uint32_t originalSize = size;
757 uint32_t left = tmpsize;
774 return originalSize - size;
802 return m_current == m_dataEnd;
808 return m_current == m_dataStart;
815 for (uint32_t i =
start; i < end; i++)
828 return i >= m_dataStart &&
829 !(i >= m_zeroStart && i < m_zeroEnd) &&
845 GetWriteErrorMessage ());
850 start.m_current += toCopy;
857 memset (&
m_data[m_current], 0, toCopy);
858 start.m_current += toCopy;
864 uint8_t *to = &
m_data[m_current];
865 memcpy (to, from, toCopy);
873 WriteU8 (
data & 0xff);
875 WriteU8 (
data & 0xff);
881 WriteU8 (
data & 0xff);
883 WriteU8 (
data & 0xff);
885 WriteU8 (
data & 0xff);
887 WriteU8 (
data & 0xff);
893 WriteU8 (
data & 0xff);
895 WriteU8 (
data & 0xff);
897 WriteU8 (
data & 0xff);
899 WriteU8 (
data & 0xff);
901 WriteU8 (
data & 0xff);
903 WriteU8 (
data & 0xff);
905 WriteU8 (
data & 0xff);
907 WriteU8 (
data & 0xff);
913 WriteU8 ((
data >> 0) & 0xff);
914 WriteU8 ((
data >> 8) & 0xff);
920 WriteU8 ((
data >> 0) & 0xff);
921 WriteU8 ((
data >> 8) & 0xff);
922 WriteU8 ((
data >> 16) & 0xff);
923 WriteU8 ((
data >> 24) & 0xff);
929 WriteU8 ((
data >> 0) & 0xff);
930 WriteU8 ((
data >> 8) & 0xff);
931 WriteU8 ((
data >> 16) & 0xff);
932 WriteU8 ((
data >> 24) & 0xff);
933 WriteU8 ((
data >> 32) & 0xff);
934 WriteU8 ((
data >> 40) & 0xff);
935 WriteU8 ((
data >> 48) & 0xff);
936 WriteU8 ((
data >> 56) & 0xff);
943 WriteU8 ((
data >> 56) & 0xff);
944 WriteU8 ((
data >> 48) & 0xff);
945 WriteU8 ((
data >> 40) & 0xff);
946 WriteU8 ((
data >> 32) & 0xff);
947 WriteU8 ((
data >> 24) & 0xff);
948 WriteU8 ((
data >> 16) & 0xff);
949 WriteU8 ((
data >> 8) & 0xff);
950 WriteU8 ((
data >> 0) & 0xff);
957 GetWriteErrorMessage ());
959 if (m_current <= m_zeroStart)
965 to = &
m_data[m_current - (m_zeroEnd - m_zeroStart)];
967 memcpy (to, buffer, size);
975 uint8_t byte0 = ReadU8 ();
976 uint8_t byte1 = ReadU8 ();
977 uint8_t byte2 = ReadU8 ();
978 uint8_t byte3 = ReadU8 ();
979 uint32_t
data = byte3;
992 uint8_t byte0 = ReadU8 ();
993 uint8_t byte1 = ReadU8 ();
994 uint8_t byte2 = ReadU8 ();
995 uint8_t byte3 = ReadU8 ();
996 uint8_t byte4 = ReadU8 ();
997 uint8_t byte5 = ReadU8 ();
998 uint8_t byte6 = ReadU8 ();
999 uint8_t byte7 = ReadU8 ();
1000 uint64_t
data = byte7;
1022 uint16_t retval = 0;
1023 retval |= ReadU8 ();
1025 retval |= ReadU8 ();
1032 uint32_t retval = 0;
1033 retval |= ReadU8 ();
1035 retval |= ReadU8 ();
1037 retval |= ReadU8 ();
1039 retval |= ReadU8 ();
1046 uint64_t retval = 0;
1047 retval |= ReadU8 ();
1049 retval |= ReadU8 ();
1051 retval |= ReadU8 ();
1053 retval |= ReadU8 ();
1055 retval |= ReadU8 ();
1057 retval |= ReadU8 ();
1059 retval |= ReadU8 ();
1061 retval |= ReadU8 ();
1068 uint8_t byte0 = ReadU8 ();
1069 uint8_t byte1 = ReadU8 ();
1070 uint16_t
data = byte1;
1079 uint8_t byte0 = ReadU8 ();
1080 uint8_t byte1 = ReadU8 ();
1081 uint8_t byte2 = ReadU8 ();
1082 uint8_t byte3 = ReadU8 ();
1083 uint32_t
data = byte3;
1096 uint8_t byte0 = ReadU8 ();
1097 uint8_t byte1 = ReadU8 ();
1098 uint8_t byte2 = ReadU8 ();
1099 uint8_t byte3 = ReadU8 ();
1100 uint8_t byte4 = ReadU8 ();
1101 uint8_t byte5 = ReadU8 ();
1102 uint8_t byte6 = ReadU8 ();
1103 uint8_t byte7 = ReadU8 ();
1104 uint64_t
data = byte7;
1126 for (uint32_t i = 0; i < size; i++)
1128 buffer[i] = ReadU8 ();
1136 return CalculateIpChecksum (size, 0);
1144 uint32_t sum = initialChecksum;
1146 for (
int j = 0; j < size/2; j++)
1153 sum = (sum & 0xffff) + (sum >> 16);
1161 return m_dataEnd - m_dataStart;
1168 return m_dataEnd - m_current;
1176 std::string str =
"You have attempted to read beyond the bounds of the " 1177 "available buffer space. This usually indicates that a " 1178 "Header::Deserialize or Trailer::Deserialize method " 1179 "is trying to read data which was not written by " 1180 "a Header::Serialize or Trailer::Serialize method. " 1181 "In short: check the code of your Serialize and Deserialize " 1190 if (m_current < m_dataStart)
1192 str =
"You have attempted to write before the start of the available " 1193 "buffer space. This usually indicates that Trailer::GetSerializedSize " 1194 "returned a size which is too small compared to what Trailer::Serialize " 1195 "is actually using.";
1197 else if (m_current >= m_dataEnd)
1199 str =
"You have attempted to write after the end of the available " 1200 "buffer space. This usually indicates that Header::GetSerializedSize " 1201 "returned a size which is too small compared to what Header::Serialize " 1202 "is actually using.";
1206 NS_ASSERT (m_current >= m_zeroStart && m_current < m_zeroEnd);
1207 str =
"You have attempted to write inside the payload area of the " 1208 "buffer. This usually indicates that your Serialize method uses more " 1209 "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)
void AddAtStart(uint32_t start)
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 "...
uint32_t GetInternalEnd(void) const
Get the buffer end position.
void RemoveAtEnd(uint32_t end)
uint8_t * m_data
a pointer to the underlying byte buffer.
void RemoveAtStart(uint32_t start)
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
uint32_t GetInternalSize(void) const
Get the buffer real size.
bool CheckNoZero(uint32_t start, uint32_t end) const
Checks that the [start, end) is not in the "virtual zero area".
uint32_t m_size
the size of the m_data field below.
uint32_t GetRemainingSize(void) const
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.
uint8_t const * PeekData(void) const
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 ...
iterator in a Buffer instance
uint32_t GetDistanceFrom(Iterator const &o) const
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 ...
void Prev(void)
go backward by one byte
std::string GetReadErrorMessage(void) const
Returns an appropriate message indicating a read error.
static struct anonymous_namespace{buffer.cc}::Zeroes g_zeroes
Zero-filled buffer.
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
void WriteU16(uint16_t data)
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
static struct Buffer::Data * Create(uint32_t size)
Create a buffer data storage.
uint32_t GetSize(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 AddAtEnd(uint32_t end)
std::vector< struct Buffer::Data * > FreeList
Container for buffer data.
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.
std::string GetWriteErrorMessage(void) const
Returns an appropriate message indicating a write error.
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 Serialize(uint8_t *buffer, uint32_t maxSize) const
uint32_t m_zeroAreaStart
offset to the start of the virtual zero area from the start of m_data->m_data
bool Check(uint32_t i) const
Checks that the buffer position is not in the "virtual zero area".
Buffer::Iterator End(void) const
void WriteHtolsbU16(uint16_t data)
static struct Buffer::Data * Allocate(uint32_t reqSize)
Allocate a buffer data storage.
const uint32_t size
buffer size
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)
#define LOG_INTERNAL_STATE(y)
Buffer CreateFullCopy(void) const
Create a full copy of the buffer, including all the internal structures.
static void Deallocate(struct Buffer::Data *data)
Deallocate the buffer memory.
uint32_t GetSize(void) const
Buffer CreateFragment(uint32_t start, uint32_t length) const
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)
bool CheckInternalState(void) const
Checks the internal buffer structures consistency.
uint16_t ReadLsbtohU16(void)
uint64_t ReadLsbtohU64(void)
void WriteU32(uint32_t data)
void TransformIntoRealBuffer(void) const
Transform a "Virtual byte buffer" into a "Real byte buffer".
Buffer::Iterator Begin(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)