A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("TagBuffer");
26 
27 namespace ns3 {
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
102 TagBuffer::WriteU64 (uint64_t data)
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  for (uint32_t i = 0; i < size; ++i, ++buffer)
180  {
181  *buffer = ReadU8 ();
182  }
183 }
184 TagBuffer::TagBuffer (uint8_t *start, uint8_t *end)
185  : m_current (start),
186  m_end (end)
187 {
188  NS_LOG_FUNCTION (this << &start << &end);
189 }
190 
191 void
192 TagBuffer::TrimAtEnd (uint32_t trim)
193 {
194  NS_LOG_FUNCTION (this << trim);
195  NS_ASSERT (m_current <= (m_end - trim));
196  m_end -= trim;
197 }
198 
199 void
201 {
202  NS_LOG_FUNCTION (this << &o);
203  NS_ASSERT (o.m_end >= o.m_current);
205  uintptr_t size = o.m_end - o.m_current;
206  NS_ASSERT (size <= (uintptr_t)(m_end - m_current));
207  std::memcpy (m_current, o.m_current, size);
208  m_current += size;
209 }
210 
211 } // namespace ns3
212 
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void WriteU64(uint64_t v)
Definition: tag-buffer.cc:102
void WriteDouble(double v)
Definition: tag-buffer.cc:115
#define NS_ASSERT(condition)
Definition: assert.h:64
void TrimAtEnd(uint32_t trim)
Definition: tag-buffer.cc:192
uint8_t * m_current
Definition: tag-buffer.h:143
TAG_BUFFER_INLINE uint32_t ReadU32(void)
Definition: tag-buffer.h:199
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:179
double ReadDouble(void)
Definition: tag-buffer.cc:164
uint8_t * m_end
Definition: tag-buffer.h:144
uint8_t data[writeSize]
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:170
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:164
NS_LOG_COMPONENT_DEFINE("TagBuffer")
uint64_t ReadU64(void)
Definition: tag-buffer.cc:134
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:156
read and write tag data
Definition: tag-buffer.h:51
TagBuffer(uint8_t *start, uint8_t *end)
Definition: tag-buffer.cc:184
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:189
void CopyFrom(TagBuffer o)
Definition: tag-buffer.cc:200