A Discrete-Event Network Simulator
API
tag-buffer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 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 "tag-buffer.h"
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include <cstring>
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("TagBuffer");
28 
29 #ifndef TAG_BUFFER_USE_INLINE
30 
31 void
32 TagBuffer::WriteU8 (uint8_t v)
33 {
34  NS_LOG_FUNCTION (this << static_cast<uint32_t> (v));
35  NS_ASSERT (m_current + 1 <= m_end);
36  *m_current = v;
37  m_current++;
38 }
39 
40 void
41 TagBuffer::WriteU16 (uint16_t data)
42 {
43  NS_LOG_FUNCTION (this << data);
44  WriteU8 ((data >> 0) & 0xff);
45  WriteU8 ((data >> 8) & 0xff);
46 }
47 void
48 TagBuffer::WriteU32 (uint32_t data)
49 {
50  NS_LOG_FUNCTION (this << data);
51  WriteU8 ((data >> 0) & 0xff);
52  WriteU8 ((data >> 8) & 0xff);
53  WriteU8 ((data >> 16) & 0xff);
54  WriteU8 ((data >> 24) & 0xff);
55 }
56 
57 
58 uint8_t
59 TagBuffer::ReadU8 (void)
60 {
61  NS_LOG_FUNCTION (this);
62  NS_ASSERT (m_current + 1 <= m_end);
63  uint8_t v;
64  v = *m_current;
65  m_current++;
66  return v;
67 }
68 
69 uint16_t
70 TagBuffer::ReadU16 (void)
71 {
72  NS_LOG_FUNCTION (this);
73  uint8_t byte0 = ReadU8 ();
74  uint8_t byte1 = ReadU8 ();
75  uint16_t data = byte1;
76  data <<= 8;
77  data |= byte0;
78  return data;
79 }
80 uint32_t
81 TagBuffer::ReadU32 (void)
82 {
83  NS_LOG_FUNCTION (this);
84  uint8_t byte0 = ReadU8 ();
85  uint8_t byte1 = ReadU8 ();
86  uint8_t byte2 = ReadU8 ();
87  uint8_t byte3 = ReadU8 ();
88  uint32_t data = byte3;
89  data <<= 8;
90  data |= byte2;
91  data <<= 8;
92  data |= byte1;
93  data <<= 8;
94  data |= byte0;
95  return data;
96 }
97 
98 #endif /* TAG_BUFFER_USE_INLINE */
99 
100 
101 void
103 {
104  NS_LOG_FUNCTION (this << data);
105  WriteU8 ((data >> 0) & 0xff);
106  WriteU8 ((data >> 8) & 0xff);
107  WriteU8 ((data >> 16) & 0xff);
108  WriteU8 ((data >> 24) & 0xff);
109  WriteU8 ((data >> 32) & 0xff);
110  WriteU8 ((data >> 40) & 0xff);
111  WriteU8 ((data >> 48) & 0xff);
112  WriteU8 ((data >> 56) & 0xff);
113 }
114 void
116 {
117  NS_LOG_FUNCTION (this << v);
118  uint8_t *buf = (uint8_t *)&v;
119  for (uint32_t i = 0; i < sizeof (double); ++i, ++buf)
120  {
121  WriteU8 (*buf);
122  }
123 }
124 void
125 TagBuffer::Write (const uint8_t *buffer, uint32_t size)
126 {
127  NS_LOG_FUNCTION (this << &buffer << size);
128  for (uint32_t i = 0; i < size; ++i, ++buffer)
129  {
130  WriteU8 (*buffer);
131  }
132 }
133 uint64_t
135 {
136  NS_LOG_FUNCTION (this);
137  uint8_t byte0 = ReadU8 ();
138  uint8_t byte1 = ReadU8 ();
139  uint8_t byte2 = ReadU8 ();
140  uint8_t byte3 = ReadU8 ();
141  uint8_t byte4 = ReadU8 ();
142  uint8_t byte5 = ReadU8 ();
143  uint8_t byte6 = ReadU8 ();
144  uint8_t byte7 = ReadU8 ();
145  uint64_t data = byte7;
146  data <<= 8;
147  data |= byte6;
148  data <<= 8;
149  data |= byte5;
150  data <<= 8;
151  data |= byte4;
152  data <<= 8;
153  data |= byte3;
154  data <<= 8;
155  data |= byte2;
156  data <<= 8;
157  data |= byte1;
158  data <<= 8;
159  data |= byte0;
160 
161  return data;
162 }
163 double
165 {
166  NS_LOG_FUNCTION (this);
167  double v;
168  uint8_t *buf = (uint8_t *)&v;
169  for (uint32_t i = 0; i < sizeof (double); ++i, ++buf)
170  {
171  *buf = ReadU8 ();
172  }
173  return v;
174 }
175 void
176 TagBuffer::Read (uint8_t *buffer, uint32_t size)
177 {
178  NS_LOG_FUNCTION (this << &buffer << size);
179  std::memcpy (buffer, m_current, size);
180  m_current += size;
182 }
183 TagBuffer::TagBuffer (uint8_t *start, uint8_t *end)
184  : m_current (start),
185  m_end (end)
186 {
187  NS_LOG_FUNCTION (this << &start << &end);
188 }
189 
190 void
191 TagBuffer::TrimAtEnd (uint32_t trim)
192 {
193  NS_LOG_FUNCTION (this << trim);
194  NS_ASSERT (m_current <= (m_end - trim));
195  m_end -= trim;
196 }
197 
198 void
200 {
201  NS_LOG_FUNCTION (this << &o);
202  NS_ASSERT (o.m_end >= o.m_current);
204  uintptr_t size = o.m_end - o.m_current;
205  NS_ASSERT (size <= (uintptr_t)(m_end - m_current));
206  std::memcpy (m_current, o.m_current, size);
207  m_current += size;
208 }
209 
210 } // namespace ns3
211 
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void WriteU64(uint64_t v)
Definition: tag-buffer.cc:102
def start()
Definition: core.py:1855
void WriteDouble(double v)
Definition: tag-buffer.cc:115
#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:204
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
Definition: tag-buffer.cc:191
uint8_t * m_current
current TagBuffer position
Definition: tag-buffer.h:159
TAG_BUFFER_INLINE uint32_t ReadU32(void)
Definition: tag-buffer.h:215
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
double ReadDouble(void)
Definition: tag-buffer.cc:164
uint8_t * m_end
end TagBuffer position
Definition: tag-buffer.h:160
uint8_t data[writeSize]
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:186
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint64_t ReadU64(void)
Definition: tag-buffer.cc:134
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
read and write tag data
Definition: tag-buffer.h:51
TagBuffer(uint8_t *start, uint8_t *end)
Constructor.
Definition: tag-buffer.cc:183
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:205
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
Definition: tag-buffer.cc:199