A Discrete-Event Network Simulator
API
buffer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "buffer.h"
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 
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)
28 
29 namespace {
30 
35 static struct Zeroes
36 {
37  Zeroes ()
38  : size (1000)
39  {
40  memset (buffer, 0, size);
41  }
42  char buffer[1000];
43  const uint32_t size;
44 } g_zeroes;
45 
46 }
47 
48 namespace ns3 {
49 
50 NS_LOG_COMPONENT_DEFINE ("Buffer");
51 
52 
53 uint32_t Buffer::g_recommendedStart = 0;
54 #ifdef BUFFER_FREE_LIST
55 /* The following macros are pretty evil but they are needed to allow us to
56  * keep track of 3 possible states for the g_freeList variable:
57  * - uninitialized means that no one has created a buffer yet
58  * so no one has created the associated free list (it is created
59  * on-demand when the first buffer is created)
60  * - initialized means that the free list exists and is valid
61  * - destroyed means that the static destructors of this compilation unit
62  * have run so, the free list has been cleared from its content
63  * The key is that in destroyed state, we are careful not re-create it
64  * which is a typical weakness of lazy evaluation schemes which use
65  * '0' as a special value to indicate both un-initialized and destroyed.
66  * Note that it is important to use '0' as the marker for un-initialized state
67  * because the variable holding this state information is initialized to zero
68  * which the compiler assigns to zero-memory which is initialized to _zero_
69  * before the constructors run so this ensures perfect handling of crazy
70  * constructor orderings.
71  */
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)
78 uint32_t Buffer::g_maxSize = 0;
80 struct Buffer::LocalStaticDestructor Buffer::g_localStaticDestructor;
81 
83 {
84  NS_LOG_FUNCTION (this);
86  {
87  for (Buffer::FreeList::iterator i = g_freeList->begin ();
88  i != g_freeList->end (); i++)
89  {
90  Buffer::Deallocate (*i);
91  }
92  delete g_freeList;
94  }
95 }
96 
97 void
99 {
101  NS_ASSERT (data->m_count == 0);
103  g_maxSize = std::max (g_maxSize, data->m_size);
104  /* feed into free list */
105  if (data->m_size < g_maxSize ||
107  g_freeList->size () > 1000)
108  {
110  }
111  else
112  {
114  g_freeList->push_back (data);
115  }
116 }
117 
118 Buffer::Data *
119 Buffer::Create (uint32_t dataSize)
120 {
121  NS_LOG_FUNCTION (dataSize);
122  /* try to find a buffer correctly sized. */
124  {
125  g_freeList = new Buffer::FreeList ();
126  }
127  else if (IS_INITIALIZED (g_freeList))
128  {
129  while (!g_freeList->empty ())
130  {
131  struct Buffer::Data *data = g_freeList->back ();
132  g_freeList->pop_back ();
133  if (data->m_size >= dataSize)
134  {
135  data->m_count = 1;
136  return data;
137  }
139  }
140  }
141  struct Buffer::Data *data = Buffer::Allocate (dataSize);
142  NS_ASSERT (data->m_count == 1);
143  return data;
144 }
145 #else /* BUFFER_FREE_LIST */
146 void
148 {
150  NS_ASSERT (data->m_count == 0);
151  Deallocate (data);
152 }
153 
154 Buffer::Data *
155 Buffer::Create (uint32_t size)
156 {
157  NS_LOG_FUNCTION (size);
158  return Allocate (size);
159 }
160 #endif /* BUFFER_FREE_LIST */
161 
162 struct Buffer::Data *
163 Buffer::Allocate (uint32_t reqSize)
164 {
165  NS_LOG_FUNCTION (reqSize);
166  if (reqSize == 0)
167  {
168  reqSize = 1;
169  }
170  NS_ASSERT (reqSize >= 1);
171  uint32_t size = reqSize - 1 + sizeof (struct Buffer::Data);
172  uint8_t *b = new uint8_t [size];
173  struct Buffer::Data *data = reinterpret_cast<struct Buffer::Data*>(b);
174  data->m_size = reqSize;
175  data->m_count = 1;
176  return data;
177 }
178 
179 void
181 {
183  NS_ASSERT (data->m_count == 0);
184  uint8_t *buf = reinterpret_cast<uint8_t *> (data);
185  delete [] buf;
186 }
187 
189 {
190  NS_LOG_FUNCTION (this);
191  Initialize (0);
192 }
193 
194 Buffer::Buffer (uint32_t dataSize)
195 {
196  NS_LOG_FUNCTION (this << dataSize);
197  Initialize (dataSize);
198 }
199 
200 Buffer::Buffer (uint32_t dataSize, bool initialize)
201 {
202  NS_LOG_FUNCTION (this << dataSize << initialize);
203  if (initialize == true)
204  {
205  Initialize (dataSize);
206  }
207 }
208 
209 bool
211 {
212  NS_LOG_FUNCTION (this);
213 #if 0
214  // If you want to modify any code in this file, enable this checking code.
215  // Otherwise, there is not much point is enabling it because the
216  // current implementation has been fairly seriously tested and the cost
217  // of this constant checking is pretty high, even for a debug build.
218  bool offsetsOk =
221  m_zeroAreaEnd <= m_end;
222  bool dirtyOk =
224  m_end <= m_data->m_dirtyEnd;
225  bool internalSizeOk = m_end - (m_zeroAreaEnd - m_zeroAreaStart) <= m_data->m_size &&
226  m_start <= m_data->m_size &&
227  m_zeroAreaStart <= m_data->m_size;
228 
229  bool ok = m_data->m_count > 0 && offsetsOk && dirtyOk && internalSizeOk;
230  if (!ok)
231  {
232  LOG_INTERNAL_STATE ("check " << this <<
233  ", " << (offsetsOk ? "true" : "false") <<
234  ", " << (dirtyOk ? "true" : "false") <<
235  ", " << (internalSizeOk ? "true" : "false") << " ");
236  }
237  return ok;
238 #else
239  return true;
240 #endif
241 }
242 
243 void
244 Buffer::Initialize (uint32_t zeroSize)
245 {
246  NS_LOG_FUNCTION (this << zeroSize);
247  m_data = Buffer::Create (0);
251  m_zeroAreaEnd = m_zeroAreaStart + zeroSize;
256 }
257 
258 Buffer &
260 {
262  if (m_data != o.m_data)
263  {
264  // not assignment to self.
265  m_data->m_count--;
266  if (m_data->m_count == 0)
267  {
268  Recycle (m_data);
269  }
270  m_data = o.m_data;
271  m_data->m_count++;
272  }
277  m_start = o.m_start;
278  m_end = o.m_end;
280  return *this;
281 }
282 
284 {
285  NS_LOG_FUNCTION (this);
288  m_data->m_count--;
289  if (m_data->m_count == 0)
290  {
291  Recycle (m_data);
292  }
293 }
294 
295 uint32_t
297 {
298  NS_LOG_FUNCTION (this);
300 }
301 uint32_t
303 {
304  NS_LOG_FUNCTION (this);
305  return m_end - (m_zeroAreaEnd - m_zeroAreaStart);
306 }
307 
308 void
310 {
311  NS_LOG_FUNCTION (this << start);
313  bool isDirty = m_data->m_count > 1 && m_start > m_data->m_dirtyStart;
314  if (m_start >= start && !isDirty)
315  {
316  /* enough space in the buffer and not dirty.
317  * To add: |..|
318  * Before: |*****---------***|
319  * After: |***..---------***|
320  */
322  m_start -= start;
323  // update dirty area
325  }
326  else
327  {
328  uint32_t newSize = GetInternalSize () + start;
329  struct Buffer::Data *newData = Buffer::Create (newSize);
330  memcpy (newData->m_data + start, m_data->m_data + m_start, GetInternalSize ());
331  m_data->m_count--;
332  if (m_data->m_count == 0)
333  {
335  }
336  m_data = newData;
337 
338  int32_t delta = start - m_start;
339  m_start += delta;
340  m_zeroAreaStart += delta;
341  m_zeroAreaEnd += delta;
342  m_end += delta;
343  m_start -= start;
344 
345  // update dirty area
348  }
350  LOG_INTERNAL_STATE ("add start=" << start << ", ");
352 }
353 void
354 Buffer::AddAtEnd (uint32_t end)
355 {
356  NS_LOG_FUNCTION (this << end);
358  bool isDirty = m_data->m_count > 1 && m_end < m_data->m_dirtyEnd;
359  if (GetInternalEnd () + end <= m_data->m_size && !isDirty)
360  {
361  /* enough space in buffer and not dirty
362  * Add: |...|
363  * Before: |**----*****|
364  * After: |**----...**|
365  */
367  m_end += end;
368  // update dirty area.
370  }
371  else
372  {
373  uint32_t newSize = GetInternalSize () + end;
374  struct Buffer::Data *newData = Buffer::Create (newSize);
375  memcpy (newData->m_data, m_data->m_data + m_start, GetInternalSize ());
376  m_data->m_count--;
377  if (m_data->m_count == 0)
378  {
380  }
381  m_data = newData;
382 
383  int32_t delta = -m_start;
384  m_zeroAreaStart += delta;
385  m_zeroAreaEnd += delta;
386  m_end += delta;
387  m_start += delta;
388  m_end += end;
389 
390  // update dirty area
393  }
395  LOG_INTERNAL_STATE ("add end=" << end << ", ");
397 }
398 
399 void
401 {
402  NS_LOG_FUNCTION (this << &o);
403  if (m_data->m_count == 1 &&
404  m_end == m_zeroAreaEnd &&
405  m_end == m_data->m_dirtyEnd &&
406  o.m_start == o.m_zeroAreaStart &&
407  o.m_zeroAreaEnd - o.m_zeroAreaStart > 0)
408  {
414  uint32_t zeroSize = o.m_zeroAreaEnd - o.m_zeroAreaStart;
415  m_zeroAreaEnd += zeroSize;
418  uint32_t endData = o.m_end - o.m_zeroAreaEnd;
419  AddAtEnd (endData);
420  Buffer::Iterator dst = End ();
421  dst.Prev (endData);
422  Buffer::Iterator src = o.End ();
423  src.Prev (endData);
424  dst.Write (src, o.End ());
426  return;
427  }
428 
429  *this = CreateFullCopy ();
430  AddAtEnd (o.GetSize ());
431  Buffer::Iterator destStart = End ();
432  destStart.Prev (o.GetSize ());
433  destStart.Write (o.Begin (), o.End ());
435 }
436 
437 void
439 {
440  NS_LOG_FUNCTION (this << start);
442  uint32_t newStart = m_start + start;
443  if (newStart <= m_zeroAreaStart)
444  {
445  /* only remove start of buffer
446  */
447  m_start = newStart;
448  }
449  else if (newStart <= m_zeroAreaEnd)
450  {
451  /* remove start of buffer _and_ start of zero area
452  */
453  uint32_t delta = newStart - m_zeroAreaStart;
455  m_zeroAreaEnd -= delta;
456  m_end -= delta;
457  }
458  else if (newStart <= m_end)
459  {
460  /* remove start of buffer, complete zero area, and part
461  * of end of buffer
462  */
463  NS_ASSERT (m_end >= start);
464  uint32_t zeroSize = m_zeroAreaEnd - m_zeroAreaStart;
465  m_start = newStart - zeroSize;
466  m_end -= zeroSize;
469  }
470  else
471  {
472  /* remove all buffer */
474  m_start = m_end;
477  }
479  LOG_INTERNAL_STATE ("rem start=" << start << ", ");
481 }
482 void
483 Buffer::RemoveAtEnd (uint32_t end)
484 {
485  NS_LOG_FUNCTION (this << end);
487  uint32_t newEnd = m_end - std::min (end, m_end - m_start);
488  if (newEnd > m_zeroAreaEnd)
489  {
490  /* remove part of end of buffer */
491  m_end = newEnd;
492  }
493  else if (newEnd > m_zeroAreaStart)
494  {
495  /* remove end of buffer, part of zero area */
496  m_end = newEnd;
497  m_zeroAreaEnd = newEnd;
498  }
499  else if (newEnd > m_start)
500  {
501  /* remove end of buffer, zero area, part of start of buffer */
502  m_end = newEnd;
503  m_zeroAreaEnd = newEnd;
504  m_zeroAreaStart = newEnd;
505  }
506  else
507  {
508  /* remove all buffer */
509  m_end = m_start;
512  }
514  LOG_INTERNAL_STATE ("rem end=" << end << ", ");
516 }
517 
518 Buffer
519 Buffer::CreateFragment (uint32_t start, uint32_t length) const
520 {
521  NS_LOG_FUNCTION (this << start << length);
523  Buffer tmp = *this;
524  tmp.RemoveAtStart (start);
525  tmp.RemoveAtEnd (GetSize () - (start + length));
527  return tmp;
528 }
529 
530 Buffer
532 {
533  NS_LOG_FUNCTION (this);
535  if (m_zeroAreaEnd - m_zeroAreaStart != 0)
536  {
537  Buffer tmp;
540  uint32_t dataStart = m_zeroAreaStart - m_start;
541  tmp.AddAtStart (dataStart);
542  tmp.Begin ().Write (m_data->m_data+m_start, dataStart);
543  uint32_t dataEnd = m_end - m_zeroAreaEnd;
544  tmp.AddAtEnd (dataEnd);
545  Buffer::Iterator i = tmp.End ();
546  i.Prev (dataEnd);
547  i.Write (m_data->m_data+m_zeroAreaStart,dataEnd);
548  NS_ASSERT (tmp.CheckInternalState ());
549  return tmp;
550  }
552  return *this;
553 }
554 
555 uint32_t
557 {
558  NS_LOG_FUNCTION (this);
559  uint32_t dataStart = (m_zeroAreaStart - m_start + 3) & (~0x3);
560  uint32_t dataEnd = (m_end - m_zeroAreaEnd + 3) & (~0x3);
561 
562  // total size 4-bytes for dataStart length
563  // + X number of bytes for dataStart
564  // + 4-bytes for dataEnd length
565  // + X number of bytes for dataEnd
566  uint32_t sz = sizeof (uint32_t)
567  + sizeof (uint32_t)
568  + dataStart
569  + sizeof (uint32_t)
570  + dataEnd;
571 
572  return sz;
573 }
574 
575 uint32_t
576 Buffer::Serialize (uint8_t* buffer, uint32_t maxSize) const
577 {
578  NS_LOG_FUNCTION (this << &buffer << maxSize);
579  uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
580  uint32_t size = 0;
581 
582  // Add the zero data length
583  if (size + 4 <= maxSize)
584  {
585  size += 4;
587  }
588  else
589  {
590  return 0;
591  }
592 
593  // Add the length of actual start data
594  uint32_t dataStartLength = m_zeroAreaStart - m_start;
595  if (size + 4 <= maxSize)
596  {
597  size += 4;
598  *p++ = dataStartLength;
599  }
600  else
601  {
602  return 0;
603  }
604 
605  // Add the actual data
606  if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
607  {
608  size += (dataStartLength + 3) & (~3);
609  memcpy (p, m_data->m_data + m_start, dataStartLength);
610  p += (((dataStartLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
611  }
612  else
613  {
614  return 0;
615  }
616 
617  // Add the length of the actual end data
618  uint32_t dataEndLength = m_end - m_zeroAreaEnd;
619  if (size + 4 <= maxSize)
620  {
621  size += 4;
622  *p++ = dataEndLength;
623  }
624  else
625  {
626  return 0;
627  }
628 
629  // Add the actual data
630  if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
631  {
632  // The following line is unnecessary.
633  // size += (dataEndLength + 3) & (~3);
634  memcpy (p, m_data->m_data+m_zeroAreaStart, dataEndLength);
635  // The following line is unnecessary.
636  // p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
637  }
638  else
639  {
640  return 0;
641  }
642 
643  // Serialized everything successfully
644  return 1;
645 }
646 
647 uint32_t
648 Buffer::Deserialize (const uint8_t *buffer, uint32_t size)
649 {
650  NS_LOG_FUNCTION (this << &buffer << size);
651  const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
652  uint32_t sizeCheck = size-4;
653 
654  NS_ASSERT (sizeCheck >= 4);
655  uint32_t zeroDataLength = *p++;
656  sizeCheck -= 4;
657 
658  // Create zero bytes
659  Initialize (zeroDataLength);
660 
661  // Add start data
662  NS_ASSERT (sizeCheck >= 4);
663  uint32_t dataStartLength = *p++;
664  sizeCheck -= 4;
665  AddAtStart (dataStartLength);
666 
667  NS_ASSERT (sizeCheck >= dataStartLength);
668  Begin ().Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataStartLength);
669  p += (((dataStartLength+3)&(~3))/4); // Advance p, insuring 4 byte boundary
670  sizeCheck -= ((dataStartLength+3)&(~3));
671 
672  // Add end data
673  NS_ASSERT (sizeCheck >= 4);
674  uint32_t dataEndLength = *p++;
675  sizeCheck -= 4;
676  AddAtEnd (dataEndLength);
677 
678  NS_ASSERT (sizeCheck >= dataEndLength);
679  Buffer::Iterator tmp = End ();
680  tmp.Prev (dataEndLength);
681  tmp.Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataEndLength);
682  // The following line is unnecessary.
683  // p += (((dataEndLength+3)&(~3))/4); // Advance p, insuring 4 byte boundary
684  sizeCheck -= ((dataEndLength+3)&(~3));
685 
686  NS_ASSERT (sizeCheck == 0);
687  // return zero if buffer did not
688  // contain a complete message
689  return (sizeCheck != 0) ? 0 : 1;
690 }
691 
692 
693 void
695 {
696  NS_LOG_FUNCTION (this);
698  Buffer tmp = CreateFullCopy ();
699  *const_cast<Buffer *> (this) = tmp;
701 }
702 
703 
704 uint8_t const*
705 Buffer::PeekData (void) const
706 {
707  NS_LOG_FUNCTION (this);
711  return m_data->m_data + m_start;
712 }
713 
714 void
715 Buffer::CopyData (std::ostream *os, uint32_t size) const
716 {
717  NS_LOG_FUNCTION (this << &os << size);
718  if (size > 0)
719  {
720  uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size);
721  os->write ((const char*)(m_data->m_data + m_start), tmpsize);
722  if (size > tmpsize)
723  {
724  size -= m_zeroAreaStart-m_start;
725  tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size);
726  uint32_t left = tmpsize;
727  while (left > 0)
728  {
729  uint32_t toWrite = std::min (left, g_zeroes.size);
730  os->write (g_zeroes.buffer, toWrite);
731  left -= toWrite;
732  }
733  if (size > tmpsize)
734  {
735  size -= tmpsize;
736  tmpsize = std::min (m_end - m_zeroAreaEnd, size);
737  os->write ((const char*)(m_data->m_data + m_zeroAreaStart), tmpsize);
738  }
739  }
740  }
741 }
742 
743 uint32_t
744 Buffer::CopyData (uint8_t *buffer, uint32_t size) const
745 {
746  NS_LOG_FUNCTION (this << &buffer << size);
747  uint32_t originalSize = size;
748  if (size > 0)
749  {
750  uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size);
751  memcpy (buffer, (const char*)(m_data->m_data + m_start), tmpsize);
752  buffer += tmpsize;
753  size -= tmpsize;
754  if (size > 0)
755  {
756  tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size);
757  uint32_t left = tmpsize;
758  while (left > 0)
759  {
760  uint32_t toWrite = std::min (left, g_zeroes.size);
761  memcpy (buffer, g_zeroes.buffer, toWrite);
762  left -= toWrite;
763  buffer += toWrite;
764  }
765  size -= tmpsize;
766  if (size > 0)
767  {
768  tmpsize = std::min (m_end - m_zeroAreaEnd, size);
769  memcpy (buffer, (const char*)(m_data->m_data + m_zeroAreaStart), tmpsize);
770  size -= tmpsize;
771  }
772  }
773  }
774  return originalSize - size;
775 }
776 
777 /******************************************************
778  * The buffer iterator below.
779  ******************************************************/
780 
781 
782 uint32_t
784 {
785  NS_LOG_FUNCTION (this << &o);
786  NS_ASSERT (m_data == o.m_data);
787  int32_t diff = m_current - o.m_current;
788  if (diff < 0)
789  {
790  return -diff;
791  }
792  else
793  {
794  return diff;
795  }
796 }
797 
798 bool
800 {
801  NS_LOG_FUNCTION (this);
802  return m_current == m_dataEnd;
803 }
804 bool
806 {
807  NS_LOG_FUNCTION (this);
808  return m_current == m_dataStart;
809 }
810 
811 bool
812 Buffer::Iterator::CheckNoZero (uint32_t start, uint32_t end) const
813 {
814  NS_LOG_FUNCTION (this << &start << &end);
815  for (uint32_t i = start; i < end; i++)
816  {
817  if (!Check (i))
818  {
819  return false;
820  }
821  }
822  return true;
823 }
824 bool
825 Buffer::Iterator::Check (uint32_t i) const
826 {
827  NS_LOG_FUNCTION (this << &i);
828  return i >= m_dataStart &&
829  !(i >= m_zeroStart && i < m_zeroEnd) &&
830  i <= m_dataEnd;
831 }
832 
833 
834 void
836 {
837  NS_LOG_FUNCTION (this << &start << &end);
838  NS_ASSERT (start.m_data == end.m_data);
839  NS_ASSERT (start.m_current <= end.m_current);
840  NS_ASSERT (start.m_zeroStart == end.m_zeroStart);
841  NS_ASSERT (start.m_zeroEnd == end.m_zeroEnd);
842  NS_ASSERT (m_data != start.m_data);
843  uint32_t size = end.m_current - start.m_current;
844  NS_ASSERT_MSG (CheckNoZero (m_current, m_current + size),
845  GetWriteErrorMessage ());
846  if (start.m_current <= start.m_zeroStart)
847  {
848  uint32_t toCopy = std::min (size, start.m_zeroStart - start.m_current);
849  memcpy (&m_data[m_current], &start.m_data[start.m_current], toCopy);
850  start.m_current += toCopy;
851  m_current += toCopy;
852  size -= toCopy;
853  }
854  if (start.m_current <= start.m_zeroEnd)
855  {
856  uint32_t toCopy = std::min (size, start.m_zeroEnd - start.m_current);
857  memset (&m_data[m_current], 0, toCopy);
858  start.m_current += toCopy;
859  m_current += toCopy;
860  size -= toCopy;
861  }
862  uint32_t toCopy = std::min (size, start.m_dataEnd - start.m_current);
863  uint8_t *from = &start.m_data[start.m_current - (start.m_zeroEnd-start.m_zeroStart)];
864  uint8_t *to = &m_data[m_current];
865  memcpy (to, from, toCopy);
866  m_current += toCopy;
867 }
868 
869 void
871 {
872  NS_LOG_FUNCTION (this << data);
873  WriteU8 (data & 0xff);
874  data >>= 8;
875  WriteU8 (data & 0xff);
876 }
877 void
879 {
880  NS_LOG_FUNCTION (this << data);
881  WriteU8 (data & 0xff);
882  data >>= 8;
883  WriteU8 (data & 0xff);
884  data >>= 8;
885  WriteU8 (data & 0xff);
886  data >>= 8;
887  WriteU8 (data & 0xff);
888 }
889 void
891 {
892  NS_LOG_FUNCTION (this << data);
893  WriteU8 (data & 0xff);
894  data >>= 8;
895  WriteU8 (data & 0xff);
896  data >>= 8;
897  WriteU8 (data & 0xff);
898  data >>= 8;
899  WriteU8 (data & 0xff);
900  data >>= 8;
901  WriteU8 (data & 0xff);
902  data >>= 8;
903  WriteU8 (data & 0xff);
904  data >>= 8;
905  WriteU8 (data & 0xff);
906  data >>= 8;
907  WriteU8 (data & 0xff);
908 }
909 void
911 {
912  NS_LOG_FUNCTION (this << data);
913  WriteU8 ((data >> 0) & 0xff);
914  WriteU8 ((data >> 8) & 0xff);
915 }
916 void
918 {
919  NS_LOG_FUNCTION (this << data);
920  WriteU8 ((data >> 0) & 0xff);
921  WriteU8 ((data >> 8) & 0xff);
922  WriteU8 ((data >> 16) & 0xff);
923  WriteU8 ((data >> 24) & 0xff);
924 }
925 void
927 {
928  NS_LOG_FUNCTION (this << data);
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);
937 }
938 
939 void
941 {
942  NS_LOG_FUNCTION (this << data);
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);
951 }
952 void
953 Buffer::Iterator::Write (uint8_t const*buffer, uint32_t size)
954 {
955  NS_LOG_FUNCTION (this << &buffer << size);
956  NS_ASSERT_MSG (CheckNoZero (m_current, size),
957  GetWriteErrorMessage ());
958  uint8_t *to;
959  if (m_current <= m_zeroStart)
960  {
961  to = &m_data[m_current];
962  }
963  else
964  {
965  to = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
966  }
967  memcpy (to, buffer, size);
968  m_current += size;
969 }
970 
971 uint32_t
973 {
974  NS_LOG_FUNCTION (this);
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;
980  data <<= 8;
981  data |= byte2;
982  data <<= 8;
983  data |= byte1;
984  data <<= 8;
985  data |= byte0;
986  return data;
987 }
988 uint64_t
990 {
991  NS_LOG_FUNCTION (this);
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;
1001  data <<= 8;
1002  data |= byte6;
1003  data <<= 8;
1004  data |= byte5;
1005  data <<= 8;
1006  data |= byte4;
1007  data <<= 8;
1008  data |= byte3;
1009  data <<= 8;
1010  data |= byte2;
1011  data <<= 8;
1012  data |= byte1;
1013  data <<= 8;
1014  data |= byte0;
1015 
1016  return data;
1017 }
1018 uint16_t
1020 {
1021  NS_LOG_FUNCTION (this);
1022  uint16_t retval = 0;
1023  retval |= ReadU8 ();
1024  retval <<= 8;
1025  retval |= ReadU8 ();
1026  return retval;
1027 }
1028 uint32_t
1030 {
1031  NS_LOG_FUNCTION (this);
1032  uint32_t retval = 0;
1033  retval |= ReadU8 ();
1034  retval <<= 8;
1035  retval |= ReadU8 ();
1036  retval <<= 8;
1037  retval |= ReadU8 ();
1038  retval <<= 8;
1039  retval |= ReadU8 ();
1040  return retval;
1041 }
1042 uint64_t
1044 {
1045  NS_LOG_FUNCTION (this);
1046  uint64_t retval = 0;
1047  retval |= ReadU8 ();
1048  retval <<= 8;
1049  retval |= ReadU8 ();
1050  retval <<= 8;
1051  retval |= ReadU8 ();
1052  retval <<= 8;
1053  retval |= ReadU8 ();
1054  retval <<= 8;
1055  retval |= ReadU8 ();
1056  retval <<= 8;
1057  retval |= ReadU8 ();
1058  retval <<= 8;
1059  retval |= ReadU8 ();
1060  retval <<= 8;
1061  retval |= ReadU8 ();
1062  return retval;
1063 }
1064 uint16_t
1066 {
1067  NS_LOG_FUNCTION (this);
1068  uint8_t byte0 = ReadU8 ();
1069  uint8_t byte1 = ReadU8 ();
1070  uint16_t data = byte1;
1071  data <<= 8;
1072  data |= byte0;
1073  return data;
1074 }
1075 uint32_t
1077 {
1078  NS_LOG_FUNCTION (this);
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;
1084  data <<= 8;
1085  data |= byte2;
1086  data <<= 8;
1087  data |= byte1;
1088  data <<= 8;
1089  data |= byte0;
1090  return data;
1091 }
1092 uint64_t
1094 {
1095  NS_LOG_FUNCTION (this);
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;
1105  data <<= 8;
1106  data |= byte6;
1107  data <<= 8;
1108  data |= byte5;
1109  data <<= 8;
1110  data |= byte4;
1111  data <<= 8;
1112  data |= byte3;
1113  data <<= 8;
1114  data |= byte2;
1115  data <<= 8;
1116  data |= byte1;
1117  data <<= 8;
1118  data |= byte0;
1119 
1120  return data;
1121 }
1122 void
1123 Buffer::Iterator::Read (uint8_t *buffer, uint32_t size)
1124 {
1125  NS_LOG_FUNCTION (this << &buffer << size);
1126  for (uint32_t i = 0; i < size; i++)
1127  {
1128  buffer[i] = ReadU8 ();
1129  }
1130 }
1131 
1132 uint16_t
1134 {
1135  NS_LOG_FUNCTION (this << size);
1136  return CalculateIpChecksum (size, 0);
1137 }
1138 
1139 uint16_t
1140 Buffer::Iterator::CalculateIpChecksum (uint16_t size, uint32_t initialChecksum)
1141 {
1142  NS_LOG_FUNCTION (this << size << initialChecksum);
1143  /* see RFC 1071 to understand this code. */
1144  uint32_t sum = initialChecksum;
1145 
1146  for (int j = 0; j < size/2; j++)
1147  sum += ReadU16 ();
1148 
1149  if (size & 1)
1150  sum += ReadU8 ();
1151 
1152  while (sum >> 16)
1153  sum = (sum & 0xffff) + (sum >> 16);
1154  return ~sum;
1155 }
1156 
1157 uint32_t
1159 {
1160  NS_LOG_FUNCTION (this);
1161  return m_dataEnd - m_dataStart;
1162 }
1163 
1164 uint32_t
1166 {
1167  NS_LOG_FUNCTION (this);
1168  return m_dataEnd - m_current;
1169 }
1170 
1171 
1172 std::string
1174 {
1175  NS_LOG_FUNCTION (this);
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 "
1182  "methods.";
1183  return str;
1184 }
1185 std::string
1187 {
1188  NS_LOG_FUNCTION (this);
1189  std::string str;
1190  if (m_current < m_dataStart)
1191  {
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.";
1196  }
1197  else if (m_current >= m_dataEnd)
1198  {
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.";
1203  }
1204  else
1205  {
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.";
1210  }
1211  return str;
1212 }
1213 
1214 
1215 } // namespace ns3
1216 
1217 
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1133
uint16_t SlowReadNtohU16(void)
Definition: buffer.cc:1019
void Initialize(uint32_t zeroSize)
Initializes the buffer with a number of zeroes.
Definition: buffer.cc:244
void WriteHtonU64(uint64_t data)
Definition: buffer.cc:940
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
bool IsEnd(void) const
Definition: buffer.cc:799
uint32_t m_end
offset to the end of the data referenced by this Buffer instance from the start of m_data->m_data ...
Definition: buffer.h:791
uint32_t ReadU32(void)
Definition: buffer.cc:972
#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.
Definition: buffer.cc:302
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:483
uint8_t * m_data
a pointer to the underlying byte buffer.
Definition: buffer.h:484
void RemoveAtStart(uint32_t start)
Definition: buffer.cc:438
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
#define min(a, b)
Definition: 80211b.c:42
uint32_t GetInternalSize(void) const
Get the buffer real size.
Definition: buffer.cc:296
bool CheckNoZero(uint32_t start, uint32_t end) const
Checks that the [start, end) is not in the "virtual zero area".
Definition: buffer.cc:812
uint32_t m_size
the size of the m_data field below.
Definition: buffer.h:670
uint32_t GetRemainingSize(void) const
Definition: buffer.cc:1165
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1844
uint64_t ReadNtohU64(void)
Definition: buffer.cc:1043
uint32_t m_count
The reference count of an instance of this data structure.
Definition: buffer.h:666
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint8_t const * PeekData(void) const
Definition: buffer.cc:705
Buffer & operator=(Buffer const &o)
Assignment operator.
Definition: buffer.cc:259
Local static destructor structure.
Definition: buffer.h:797
uint32_t m_start
offset to the start of the data referenced by this Buffer instance from the start of m_data->m_data ...
Definition: buffer.h:786
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetDistanceFrom(Iterator const &o) const
Definition: buffer.cc:783
uint32_t SlowReadNtohU32(void)
Definition: buffer.cc:1029
#define IS_INITIALIZED(x)
Definition: buffer.cc:75
uint32_t m_zeroStart
offset in virtual bytes from the start of the data buffer to the start of the "virtual zero area"...
Definition: buffer.h:459
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...
Definition: buffer.h:680
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:648
uint8_t m_data[1]
The real data buffer holds at least one byte.
Definition: buffer.h:685
#define max(a, b)
Definition: 80211b.c:43
This data structure is variable-sized through its last member whose size is determined at allocation ...
Definition: buffer.h:660
void Prev(void)
go backward by one byte
Definition: buffer.h:851
std::string GetReadErrorMessage(void) const
Returns an appropriate message indicating a read error.
Definition: buffer.cc:1173
static struct anonymous_namespace{buffer.cc}::Zeroes g_zeroes
Zero-filled buffer.
uint8_t data[writeSize]
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
Definition: buffer.cc:715
void WriteU16(uint16_t data)
Definition: buffer.cc:870
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
Definition: buffer.cc:556
static struct Buffer::Data * Create(uint32_t size)
Create a buffer data storage.
Definition: buffer.cc:119
#define DESTROYED
Definition: buffer.cc:76
uint32_t GetSize(void) const
Definition: buffer.cc:1158
uint32_t m_current
offset in virtual bytes from the start of the data buffer to the current position represented by this...
Definition: buffer.h:479
void WriteU64(uint64_t data)
Definition: buffer.cc:890
static FreeList * g_freeList
Buffer data container.
Definition: buffer.h:802
#define IS_DESTROYED(x)
Definition: buffer.cc:74
void AddAtEnd(uint32_t end)
Definition: buffer.cc:354
std::vector< struct Buffer::Data * > FreeList
Container for buffer data.
Definition: buffer.h:795
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool IsStart(void) const
Definition: buffer.cc:805
static void Recycle(struct Buffer::Data *data)
Recycle the buffer memory.
Definition: buffer.cc:98
static uint32_t g_maxSize
Max observed data size.
Definition: buffer.h:801
std::string GetWriteErrorMessage(void) const
Returns an appropriate message indicating a write error.
Definition: buffer.cc:1186
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1123
uint32_t m_zeroEnd
offset in virtual bytes from the start of the data buffer to the end of the "virtual zero area"...
Definition: buffer.h:464
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Definition: buffer.cc:576
uint32_t m_zeroAreaStart
offset to the start of the virtual zero area from the start of m_data->m_data
Definition: buffer.h:776
uint64_t ReadU64(void)
Definition: buffer.cc:989
bool Check(uint32_t i) const
Checks that the buffer position is not in the "virtual zero area".
Definition: buffer.cc:825
Buffer::Iterator End(void) const
Definition: buffer.h:1075
void WriteHtolsbU16(uint16_t data)
Definition: buffer.cc:910
static struct Buffer::Data * Allocate(uint32_t reqSize)
Allocate a buffer data storage.
Definition: buffer.cc:163
const uint32_t size
buffer size
Definition: buffer.cc:43
struct Data * m_data
the buffer data storage
Definition: buffer.h:752
char buffer[1000]
buffer containing zero values
Definition: buffer.cc:42
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...
Definition: buffer.h:675
if(desigRtr==addrLocal)
void WriteU8(uint8_t data)
Definition: buffer.h:869
#define IS_UNINITIALIZED(x)
Definition: buffer.cc:73
uint32_t m_maxZeroAreaStart
keep track of the maximum value of m_zeroAreaStart across the lifetime of a Buffer instance...
Definition: buffer.h:764
void WriteHtolsbU64(uint64_t data)
Definition: buffer.cc:926
#define LOG_INTERNAL_STATE(y)
Definition: buffer.cc:24
Buffer CreateFullCopy(void) const
Create a full copy of the buffer, including all the internal structures.
Definition: buffer.cc:531
static void Deallocate(struct Buffer::Data *data)
Deallocate the buffer memory.
Definition: buffer.cc:180
uint32_t GetSize(void) const
Definition: buffer.h:1063
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition: buffer.cc:519
static uint32_t g_recommendedStart
location in a newly-allocated buffer where you should start writing data.
Definition: buffer.h:770
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
bool CheckInternalState(void) const
Checks the internal buffer structures consistency.
Definition: buffer.cc:210
uint16_t ReadLsbtohU16(void)
Definition: buffer.cc:1065
uint64_t ReadLsbtohU64(void)
Definition: buffer.cc:1093
void WriteU32(uint32_t data)
Definition: buffer.cc:878
void TransformIntoRealBuffer(void) const
Transform a "Virtual byte buffer" into a "Real byte buffer".
Definition: buffer.cc:694
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
uint32_t ReadLsbtohU32(void)
Definition: buffer.cc:1076
uint32_t m_zeroAreaEnd
offset to the end of the virtual zero area from the start of m_data->m_data
Definition: buffer.h:781
void WriteHtolsbU32(uint32_t data)
Definition: buffer.cc:917