A Discrete-Event Network Simulator
API
tag-buffer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "tag-buffer.h"
20 
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 
24 #include <cstring>
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("TagBuffer");
30 
31 #ifndef TAG_BUFFER_USE_INLINE
32 
33 void
34 TagBuffer::WriteU8(uint8_t v)
35 {
36  NS_LOG_FUNCTION(this << static_cast<uint32_t>(v));
37  NS_ASSERT(m_current + 1 <= m_end);
38  *m_current = v;
39  m_current++;
40 }
41 
42 void
43 TagBuffer::WriteU16(uint16_t data)
44 {
45  NS_LOG_FUNCTION(this << data);
46  WriteU8((data >> 0) & 0xff);
47  WriteU8((data >> 8) & 0xff);
48 }
49 
50 void
51 TagBuffer::WriteU32(uint32_t data)
52 {
53  NS_LOG_FUNCTION(this << data);
54  WriteU8((data >> 0) & 0xff);
55  WriteU8((data >> 8) & 0xff);
56  WriteU8((data >> 16) & 0xff);
57  WriteU8((data >> 24) & 0xff);
58 }
59 
60 uint8_t
62 {
63  NS_LOG_FUNCTION(this);
64  NS_ASSERT(m_current + 1 <= m_end);
65  uint8_t v;
66  v = *m_current;
67  m_current++;
68  return v;
69 }
70 
71 uint16_t
73 {
74  NS_LOG_FUNCTION(this);
75  uint8_t byte0 = ReadU8();
76  uint8_t byte1 = ReadU8();
77  uint16_t data = byte1;
78  data <<= 8;
79  data |= byte0;
80  return data;
81 }
82 
83 uint32_t
85 {
86  NS_LOG_FUNCTION(this);
87  uint8_t byte0 = ReadU8();
88  uint8_t byte1 = ReadU8();
89  uint8_t byte2 = ReadU8();
90  uint8_t byte3 = ReadU8();
91  uint32_t data = byte3;
92  data <<= 8;
93  data |= byte2;
94  data <<= 8;
95  data |= byte1;
96  data <<= 8;
97  data |= byte0;
98  return data;
99 }
100 
101 #endif /* TAG_BUFFER_USE_INLINE */
102 
103 void
105 {
106  NS_LOG_FUNCTION(this << data);
107  WriteU8((data >> 0) & 0xff);
108  WriteU8((data >> 8) & 0xff);
109  WriteU8((data >> 16) & 0xff);
110  WriteU8((data >> 24) & 0xff);
111  WriteU8((data >> 32) & 0xff);
112  WriteU8((data >> 40) & 0xff);
113  WriteU8((data >> 48) & 0xff);
114  WriteU8((data >> 56) & 0xff);
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION(this << v);
121  auto buf = (uint8_t*)&v;
122  for (uint32_t i = 0; i < sizeof(double); ++i, ++buf)
123  {
124  WriteU8(*buf);
125  }
126 }
127 
128 void
129 TagBuffer::Write(const uint8_t* buffer, uint32_t size)
130 {
131  NS_LOG_FUNCTION(this << &buffer << size);
132  for (uint32_t i = 0; i < size; ++i, ++buffer)
133  {
134  WriteU8(*buffer);
135  }
136 }
137 
138 uint64_t
140 {
141  NS_LOG_FUNCTION(this);
142  uint8_t byte0 = ReadU8();
143  uint8_t byte1 = ReadU8();
144  uint8_t byte2 = ReadU8();
145  uint8_t byte3 = ReadU8();
146  uint8_t byte4 = ReadU8();
147  uint8_t byte5 = ReadU8();
148  uint8_t byte6 = ReadU8();
149  uint8_t byte7 = ReadU8();
150  uint64_t data = byte7;
151  data <<= 8;
152  data |= byte6;
153  data <<= 8;
154  data |= byte5;
155  data <<= 8;
156  data |= byte4;
157  data <<= 8;
158  data |= byte3;
159  data <<= 8;
160  data |= byte2;
161  data <<= 8;
162  data |= byte1;
163  data <<= 8;
164  data |= byte0;
165 
166  return data;
167 }
168 
169 double
171 {
172  NS_LOG_FUNCTION(this);
173  double v;
174  auto buf = (uint8_t*)&v;
175  for (uint32_t i = 0; i < sizeof(double); ++i, ++buf)
176  {
177  *buf = ReadU8();
178  }
179  return v;
180 }
181 
182 void
183 TagBuffer::Read(uint8_t* buffer, uint32_t size)
184 {
185  NS_LOG_FUNCTION(this << &buffer << size);
186  std::memcpy(buffer, m_current, size);
187  m_current += size;
189 }
190 
191 TagBuffer::TagBuffer(uint8_t* start, uint8_t* end)
192  : m_current(start),
193  m_end(end)
194 {
195  NS_LOG_FUNCTION(this << &start << &end);
196 }
197 
198 void
199 TagBuffer::TrimAtEnd(uint32_t trim)
200 {
201  NS_LOG_FUNCTION(this << trim);
202  NS_ASSERT(m_current <= (m_end - trim));
203  m_end -= trim;
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION(this << &o);
210  NS_ASSERT(o.m_end >= o.m_current);
212  uintptr_t size = o.m_end - o.m_current;
213  NS_ASSERT(size <= (uintptr_t)(m_end - m_current));
214  std::memcpy(m_current, o.m_current, size);
215  m_current += size;
216 }
217 
218 } // namespace ns3
read and write tag data
Definition: tag-buffer.h:52
TagBuffer(uint8_t *start, uint8_t *end)
Constructor.
Definition: tag-buffer.cc:191
TAG_BUFFER_INLINE uint32_t ReadU32()
Definition: tag-buffer.h:217
void WriteU64(uint64_t v)
Definition: tag-buffer.cc:104
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
Definition: tag-buffer.cc:199
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:183
void WriteDouble(double v)
Definition: tag-buffer.cc:118
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition: tag-buffer.h:196
TAG_BUFFER_INLINE uint16_t ReadU16()
Definition: tag-buffer.h:206
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:187
uint64_t ReadU64()
Definition: tag-buffer.cc:139
uint8_t * m_current
current TagBuffer position
Definition: tag-buffer.h:158
uint8_t * m_end
end TagBuffer position
Definition: tag-buffer.h:159
double ReadDouble()
Definition: tag-buffer.cc:170
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:129
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
Definition: tag-buffer.cc:207
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]