21#include "ns3/assert.h"
24#define LOG_INTERNAL_STATE(y) \
25 NS_LOG_LOGIC(y << "start=" << m_start << ", end=" << m_end \
26 << ", zero start=" << m_zeroAreaStart << ", zero end=" << m_zeroAreaEnd \
27 << ", count=" << m_data->m_count << ", size=" << m_data->m_size \
28 << ", dirty start=" << m_data->m_dirtyStart \
29 << ", dirty end=" << m_data->m_dirtyEnd)
43 memset(buffer, 0, size);
58#ifdef BUFFER_FREE_LIST
76#define MAGIC_DESTROYED (~(long)0)
77#define IS_UNINITIALIZED(x) (x == (Buffer::FreeList*)0)
78#define IS_DESTROYED(x) (x == (Buffer::FreeList*)MAGIC_DESTROYED)
79#define IS_INITIALIZED(x) (!IS_UNINITIALIZED(x) && !IS_DESTROYED(x))
80#define DESTROYED ((Buffer::FreeList*)MAGIC_DESTROYED)
81#define UNINITIALIZED ((Buffer::FreeList*)0)
84struct Buffer::LocalStaticDestructor Buffer::g_localStaticDestructor;
134 if (
data->m_size >= dataSize)
173 uint8_t* b =
new uint8_t[size];
175 data->m_size = reqSize;
185 uint8_t* buf =
reinterpret_cast<uint8_t*
>(
data);
204 if (initialize ==
true)
227 m_start <= m_data->m_size &&
228 m_zeroAreaStart <= m_data->m_size;
230 bool ok =
m_data->
m_count > 0 && offsetsOk && dirtyOk && internalSizeOk;
234 ", " << (offsetsOk ?
"true" :
"false") <<
235 ", " << (dirtyOk ?
"true" :
"false") <<
236 ", " << (internalSizeOk ?
"true" :
"false") <<
" ");
316 if (
m_start >= start && !isDirty)
464 else if (newStart <=
m_end)
586 if (size + 4 <= maxSize)
598 if (size + 4 <= maxSize)
601 *p++ = dataStartLength;
609 if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
611 size += (dataStartLength + 3) & (~3);
613 p += (((dataStartLength + 3) & (~3)) / 4);
622 if (size + 4 <= maxSize)
625 *p++ = dataEndLength;
633 if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
672 p += (((dataStartLength + 3) & (~3)) / 4);
673 sizeCheck -= ((dataStartLength + 3) & (~3));
683 tmp.
Prev(dataEndLength);
684 tmp.
Write(
reinterpret_cast<uint8_t*
>(
const_cast<uint32_t*
>(p)), dataEndLength);
687 sizeCheck -= ((dataEndLength + 3) & (~3));
692 return (sizeCheck != 0) ? 0 : 1;
701 *
const_cast<Buffer*
>(
this) = tmp;
731 os->write(g_zeroes.
buffer, toWrite);
762 memcpy(buffer, g_zeroes.
buffer, toWrite);
775 return originalSize - size;
802 return m_current == m_dataEnd;
809 return m_current == m_dataStart;
816 return !(start < m_dataStart || end > m_dataEnd ||
817 (end > m_zeroStart && start < m_zeroEnd && m_zeroEnd != m_zeroStart && start != end));
824 return i >= m_dataStart && !(i >= m_zeroStart && i < m_zeroEnd) && i <= m_dataEnd;
837 NS_ASSERT_MSG(CheckNoZero(m_current, m_current + size), GetWriteErrorMessage());
838 if (start.m_current <= start.m_zeroStart)
840 uint32_t toCopy = std::min(size, start.m_zeroStart - start.m_current);
841 memcpy(&
m_data[m_current], &start.m_data[start.m_current], toCopy);
842 start.m_current += toCopy;
846 if (start.m_current <= start.m_zeroEnd)
848 uint32_t toCopy = std::min(size, start.m_zeroEnd - start.m_current);
849 memset(&
m_data[m_current], 0, toCopy);
850 start.m_current += toCopy;
854 uint32_t toCopy = std::min(size, start.m_dataEnd - start.m_current);
855 uint8_t* from = &start.m_data[start.m_current - (start.m_zeroEnd - start.m_zeroStart)];
856 uint8_t* to = &
m_data[m_current];
857 memcpy(to, from, toCopy);
865 WriteU8(
data & 0xff);
867 WriteU8(
data & 0xff);
874 WriteU8(
data & 0xff);
876 WriteU8(
data & 0xff);
878 WriteU8(
data & 0xff);
880 WriteU8(
data & 0xff);
887 WriteU8(
data & 0xff);
889 WriteU8(
data & 0xff);
891 WriteU8(
data & 0xff);
893 WriteU8(
data & 0xff);
895 WriteU8(
data & 0xff);
897 WriteU8(
data & 0xff);
899 WriteU8(
data & 0xff);
901 WriteU8(
data & 0xff);
908 WriteU8((
data >> 0) & 0xff);
909 WriteU8((
data >> 8) & 0xff);
916 WriteU8((
data >> 0) & 0xff);
917 WriteU8((
data >> 8) & 0xff);
918 WriteU8((
data >> 16) & 0xff);
919 WriteU8((
data >> 24) & 0xff);
926 WriteU8((
data >> 0) & 0xff);
927 WriteU8((
data >> 8) & 0xff);
928 WriteU8((
data >> 16) & 0xff);
929 WriteU8((
data >> 24) & 0xff);
930 WriteU8((
data >> 32) & 0xff);
931 WriteU8((
data >> 40) & 0xff);
932 WriteU8((
data >> 48) & 0xff);
933 WriteU8((
data >> 56) & 0xff);
940 WriteU8((
data >> 56) & 0xff);
941 WriteU8((
data >> 48) & 0xff);
942 WriteU8((
data >> 40) & 0xff);
943 WriteU8((
data >> 32) & 0xff);
944 WriteU8((
data >> 24) & 0xff);
945 WriteU8((
data >> 16) & 0xff);
946 WriteU8((
data >> 8) & 0xff);
947 WriteU8((
data >> 0) & 0xff);
954 NS_ASSERT_MSG(CheckNoZero(m_current, size), GetWriteErrorMessage());
956 if (m_current <= m_zeroStart)
962 to = &
m_data[m_current - (m_zeroEnd - m_zeroStart)];
964 memcpy(to, buffer, size);
972 uint8_t byte0 = ReadU8();
973 uint8_t byte1 = ReadU8();
974 uint8_t byte2 = ReadU8();
975 uint8_t byte3 = ReadU8();
990 uint8_t byte0 = ReadU8();
991 uint8_t byte1 = ReadU8();
992 uint8_t byte2 = ReadU8();
993 uint8_t byte3 = ReadU8();
994 uint8_t byte4 = ReadU8();
995 uint8_t byte5 = ReadU8();
996 uint8_t byte6 = ReadU8();
997 uint8_t byte7 = ReadU8();
998 uint64_t
data = byte7;
1021 uint16_t retval = 0;
1047 uint64_t retval = 0;
1070 uint8_t byte0 = ReadU8();
1071 uint8_t byte1 = ReadU8();
1072 uint16_t
data = byte1;
1082 uint8_t byte0 = ReadU8();
1083 uint8_t byte1 = ReadU8();
1084 uint8_t byte2 = ReadU8();
1085 uint8_t byte3 = ReadU8();
1100 uint8_t byte0 = ReadU8();
1101 uint8_t byte1 = ReadU8();
1102 uint8_t byte2 = ReadU8();
1103 uint8_t byte3 = ReadU8();
1104 uint8_t byte4 = ReadU8();
1105 uint8_t byte5 = ReadU8();
1106 uint8_t byte6 = ReadU8();
1107 uint8_t byte7 = ReadU8();
1108 uint64_t
data = byte7;
1131 for (
uint32_t i = 0; i < size; i++)
1133 buffer[i] = ReadU8();
1141 return CalculateIpChecksum(size, 0);
1151 for (
int j = 0; j < size / 2; j++)
1163 sum = (sum & 0xffff) + (sum >> 16);
1172 return m_dataEnd - m_dataStart;
1179 return m_dataEnd - m_current;
1186 std::string str =
"You have attempted to read beyond the bounds of the "
1187 "available buffer space. This usually indicates that a "
1188 "Header::Deserialize or Trailer::Deserialize method "
1189 "is trying to read data which was not written by "
1190 "a Header::Serialize or Trailer::Serialize method. "
1191 "In short: check the code of your Serialize and Deserialize "
1201 if (m_current < m_dataStart)
1203 str =
"You have attempted to write before the start of the available "
1204 "buffer space. This usually indicates that Trailer::GetSerializedSize "
1205 "returned a size which is too small compared to what Trailer::Serialize "
1206 "is actually using.";
1208 else if (m_current >= m_dataEnd)
1210 str =
"You have attempted to write after the end of the available "
1211 "buffer space. This usually indicates that Header::GetSerializedSize "
1212 "returned a size which is too small compared to what Header::Serialize "
1213 "is actually using.";
1217 NS_ASSERT(m_current >= m_zeroStart && m_current < m_zeroEnd);
1218 str =
"You have attempted to write inside the payload area of the "
1219 "buffer. This usually indicates that your Serialize method uses more "
1220 "buffer space than what your GetSerialized method returned.";
#define LOG_INTERNAL_STATE(y)
#define IS_UNINITIALIZED(x)
#define IS_INITIALIZED(x)
iterator in a Buffer instance
void WriteU64(uint64_t data)
uint32_t GetRemainingSize() const
void WriteU32(uint32_t data)
void WriteHtonU64(uint64_t data)
void WriteHtolsbU16(uint16_t data)
std::string GetReadErrorMessage() const
Returns an appropriate message indicating a read error.
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
void WriteU8(uint8_t data)
uint16_t SlowReadNtohU16()
uint8_t * m_data
a pointer to the underlying byte buffer.
void WriteHtolsbU32(uint32_t data)
void Write(const 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".
bool Check(uint32_t i) const
Checks that the buffer position is not in the "virtual zero area".
void WriteU16(uint16_t data)
uint32_t m_current
offset in virtual bytes from the start of the data buffer to the current position represented by this...
uint32_t SlowReadNtohU32()
void Read(uint8_t *buffer, uint32_t size)
void WriteHtolsbU64(uint64_t data)
uint32_t m_zeroStart
offset in virtual bytes from the start of the data buffer to the start of the "virtual zero area".
bool CheckNoZero(uint32_t start, uint32_t end) const
Checks that the [start, end) is not in the "virtual zero area".
uint32_t GetDistanceFrom(const Iterator &o) const
void Prev()
go backward by one byte
std::string GetWriteErrorMessage() const
Returns an appropriate message indicating a write error.
automatically resized byte buffer
uint32_t GetSerializedSize() const
Return the number of bytes required for serialization.
uint32_t GetInternalEnd() const
Get the buffer end position.
uint32_t m_end
offset to the end of the data referenced by this Buffer instance from the start of m_data->m_data
Buffer CreateFragment(uint32_t start, uint32_t length) const
static FreeList * g_freeList
Buffer data container.
static struct Buffer::Data * Create(uint32_t size)
Create a buffer data storage.
static uint32_t g_maxSize
Max observed data size.
uint32_t m_zeroAreaEnd
offset to the end of the virtual zero area from the start of m_data->m_data
std::vector< struct Buffer::Data * > FreeList
Container for buffer data.
static void Deallocate(struct Buffer::Data *data)
Deallocate the buffer memory.
uint32_t m_maxZeroAreaStart
keep track of the maximum value of m_zeroAreaStart across the lifetime of a Buffer instance.
static uint32_t g_recommendedStart
location in a newly-allocated buffer where you should start writing data.
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
static struct Buffer::Data * Allocate(uint32_t reqSize)
Allocate a buffer data storage.
void RemoveAtEnd(uint32_t end)
void TransformIntoRealBuffer() const
Transform a "Virtual byte buffer" into a "Real byte buffer".
void AddAtStart(uint32_t start)
uint32_t GetInternalSize() const
Get the buffer real size.
Buffer::Iterator Begin() const
void AddAtEnd(uint32_t end)
Buffer::Iterator End() const
Buffer & operator=(const Buffer &o)
Assignment operator.
static void Recycle(struct Buffer::Data *data)
Recycle the buffer memory.
struct Data * m_data
the buffer data storage
Buffer CreateFullCopy() const
Create a full copy of the buffer, including all the internal structures.
bool CheckInternalState() const
Checks the internal buffer structures consistency.
void Initialize(uint32_t zeroSize)
Initializes the buffer with a number of zeroes.
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
void RemoveAtStart(uint32_t start)
uint32_t m_zeroAreaStart
offset to the start of the virtual zero area from the start of m_data->m_data
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
const uint8_t * PeekData() const
uint32_t m_start
offset to the start of the data referenced by this Buffer instance from the start of m_data->m_data
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
struct anonymous_namespace{buffer.cc}::Zeroes g_zeroes
Zero-filled buffer.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
char buffer[1000]
buffer containing zero values
const uint32_t size
buffer size
This data structure is variable-sized through its last member whose size is determined at allocation ...
uint8_t m_data[1]
The real data buffer holds at least one byte.
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 m_count
The reference count of an instance of this data structure.
uint32_t m_size
the size of the m_data field below.
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...
Local static destructor structure.