LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2005,2006,2007 INRIA | 3 * Copyright (c) 2005,2006,2007 INRIA |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 20 matching lines...) Expand all Loading... |
31 #define LOG_INTERNAL_STATE(y)
\ | 31 #define LOG_INTERNAL_STATE(y)
\ |
32 NS_LOG_LOGIC (y << "start="<<m_start<<", end="<<m_end<<", zero start="<<m_zeroAr
eaStart<< \ | 32 NS_LOG_LOGIC (y << "start="<<m_start<<", end="<<m_end<<", zero start="<<m_zeroAr
eaStart<< \ |
33 ", zero end="<<m_zeroAreaEnd<<", count="<<m_data->m_count<<", size="<<
m_data->m_size<< \ | 33 ", zero end="<<m_zeroAreaEnd<<", count="<<m_data->m_count<<", size="<<
m_data->m_size<< \ |
34 ", dirty start="<<m_data->m_dirtyStart<<", dirty end="<<m_data->m_dirt
yEnd) | 34 ", dirty start="<<m_data->m_dirtyStart<<", dirty end="<<m_data->m_dirt
yEnd) |
35 | 35 |
36 #ifdef BUFFER_HEURISTICS | 36 #ifdef BUFFER_HEURISTICS |
37 #define HEURISTICS(x) x | 37 #define HEURISTICS(x) x |
38 #else | 38 #else |
39 #define HEURISTICS(x) | 39 #define HEURISTICS(x) |
40 #endif | 40 #endif |
| 41 |
| 42 namespace { |
| 43 |
| 44 static struct Zeroes |
| 45 { |
| 46 Zeroes () |
| 47 : size (1000) |
| 48 { |
| 49 memset (buffer, 0, size); |
| 50 } |
| 51 char buffer[1000]; |
| 52 const uint32_t size; |
| 53 } g_zeroes; |
| 54 |
| 55 } |
41 | 56 |
42 //#define PRINT_STATS 1 | 57 //#define PRINT_STATS 1 |
43 | 58 |
44 namespace ns3 { | 59 namespace ns3 { |
45 | 60 |
46 /** | 61 /** |
47 * This data structure is variable-sized through its last member whose size | 62 * This data structure is variable-sized through its last member whose size |
48 * is determined at allocation time and stored in the m_size field. | 63 * is determined at allocation time and stored in the m_size field. |
49 * | 64 * |
50 * The so-called "dirty area" describes the area in the buffer which | 65 * The so-called "dirty area" describes the area in the buffer which |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 NS_LOG_FUNCTION (this); | 261 NS_LOG_FUNCTION (this); |
247 Initialize (0); | 262 Initialize (0); |
248 } | 263 } |
249 | 264 |
250 Buffer::Buffer (uint32_t dataSize) | 265 Buffer::Buffer (uint32_t dataSize) |
251 { | 266 { |
252 NS_LOG_FUNCTION (this << dataSize); | 267 NS_LOG_FUNCTION (this << dataSize); |
253 Initialize (dataSize); | 268 Initialize (dataSize); |
254 } | 269 } |
255 | 270 |
| 271 Buffer::Buffer (uint32_t dataSize, bool initialize) |
| 272 { |
| 273 NS_LOG_FUNCTION (this << dataSize << initialize); |
| 274 if (initialize == true) |
| 275 { |
| 276 Initialize (dataSize); |
| 277 } |
| 278 } |
| 279 |
256 bool | 280 bool |
257 Buffer::CheckInternalState (void) const | 281 Buffer::CheckInternalState (void) const |
258 { | 282 { |
259 bool offsetsOk =· | 283 bool offsetsOk =· |
260 m_start <= m_zeroAreaStart && | 284 m_start <= m_zeroAreaStart && |
261 m_zeroAreaStart <= m_zeroAreaEnd && | 285 m_zeroAreaStart <= m_zeroAreaEnd && |
262 m_zeroAreaEnd <= m_end; | 286 m_zeroAreaEnd <= m_end; |
263 bool dirtyOk = | 287 bool dirtyOk = |
264 m_start >= m_data->m_dirtyStart && | 288 m_start >= m_data->m_dirtyStart && |
265 m_end <= m_data->m_dirtyEnd; | 289 m_end <= m_data->m_dirtyEnd; |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 Buffer::CopyData(std::ostream *os, uint32_t size) const | 844 Buffer::CopyData(std::ostream *os, uint32_t size) const |
821 { | 845 { |
822 if (size > 0) | 846 if (size > 0) |
823 { | 847 { |
824 uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size); | 848 uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size); |
825 os->write((const char*)(m_data->m_data + m_start), tmpsize); | 849 os->write((const char*)(m_data->m_data + m_start), tmpsize); |
826 if (size > tmpsize)· | 850 if (size > tmpsize)· |
827 {· | 851 {· |
828 size -= m_zeroAreaStart-m_start; | 852 size -= m_zeroAreaStart-m_start; |
829 tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size); | 853 tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size); |
830 char zero = 0; | 854 uint32_t left = tmpsize; |
831 for (uint32_t i = 0; i < tmpsize; ++i) | 855 while (left > 0) |
832 { | 856 { |
833 os->write (&zero, 1); | 857 uint32_t toWrite = std::min (left, g_zeroes.size); |
| 858 os->write (g_zeroes.buffer, toWrite); |
| 859 left -= toWrite; |
834 } | 860 } |
835 if (size > tmpsize) | 861 if (size > tmpsize) |
836 { | 862 { |
837 size -= tmpsize; | 863 size -= tmpsize; |
838 tmpsize = std::min (m_end - m_zeroAreaEnd, size); | 864 tmpsize = std::min (m_end - m_zeroAreaEnd, size); |
839 os->write ((const char*)(m_data->m_data + m_zeroAreaStart), tmpsiz
e);· | 865 os->write ((const char*)(m_data->m_data + m_zeroAreaStart), tmpsiz
e);· |
840 } | 866 } |
841 } | 867 } |
842 } | 868 } |
| 869 } |
| 870 |
| 871 uint32_t· |
| 872 Buffer::CopyData (uint8_t *buffer, uint32_t size) const |
| 873 { |
| 874 uint32_t originalSize = size; |
| 875 if (size > 0) |
| 876 { |
| 877 uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size); |
| 878 memcpy (buffer, (const char*)(m_data->m_data + m_start), tmpsize); |
| 879 buffer += tmpsize; |
| 880 if (size > tmpsize)· |
| 881 {· |
| 882 size -= m_zeroAreaStart-m_start; |
| 883 tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size); |
| 884 uint32_t left = tmpsize; |
| 885 while (left > 0) |
| 886 { |
| 887 uint32_t toWrite = std::min (left, g_zeroes.size); |
| 888 memcpy (buffer, g_zeroes.buffer, toWrite); |
| 889 left -= toWrite; |
| 890 buffer += toWrite; |
| 891 } |
| 892 if (size > tmpsize) |
| 893 { |
| 894 size -= tmpsize; |
| 895 tmpsize = std::min (m_end - m_zeroAreaEnd, size); |
| 896 memcpy (buffer, (const char*)(m_data->m_data + m_zeroAreaStart), t
mpsize); |
| 897 } |
| 898 } |
| 899 } |
| 900 return originalSize - size; |
843 } | 901 } |
844 | 902 |
845 /****************************************************** | 903 /****************************************************** |
846 * The buffer iterator below. | 904 * The buffer iterator below. |
847 ******************************************************/ | 905 ******************************************************/ |
848 | 906 |
849 | 907 |
850 Buffer::Iterator::Iterator () | 908 Buffer::Iterator::Iterator () |
851 : m_zeroStart (0), | 909 : m_zeroStart (0), |
852 m_zeroEnd (0), | 910 m_zeroEnd (0), |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1565 : TestSuite ("buffer", UNIT) | 1623 : TestSuite ("buffer", UNIT) |
1566 { | 1624 { |
1567 AddTestCase (new BufferTest); | 1625 AddTestCase (new BufferTest); |
1568 } | 1626 } |
1569 | 1627 |
1570 BufferTestSuite g_bufferTestSuite; | 1628 BufferTestSuite g_bufferTestSuite; |
1571 | 1629 |
1572 } // namespace ns3 | 1630 } // namespace ns3 |
1573 | 1631 |
1574 | 1632 |
LEFT | RIGHT |