A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tag-buffer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "tag-buffer.h"
9
10#include "ns3/assert.h"
11#include "ns3/log.h"
12
13#include <cstring>
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("TagBuffer");
19
20#ifndef TAG_BUFFER_USE_INLINE
21
22void
23TagBuffer::WriteU8(uint8_t v)
24{
25 NS_LOG_FUNCTION(this << static_cast<uint32_t>(v));
27 *m_current = v;
28 m_current++;
29}
30
31void
33{
34 NS_LOG_FUNCTION(this << data);
35 WriteU8((data >> 0) & 0xff);
36 WriteU8((data >> 8) & 0xff);
37}
38
39void
41{
42 NS_LOG_FUNCTION(this << data);
43 WriteU8((data >> 0) & 0xff);
44 WriteU8((data >> 8) & 0xff);
45 WriteU8((data >> 16) & 0xff);
46 WriteU8((data >> 24) & 0xff);
47}
48
49uint8_t
51{
52 NS_LOG_FUNCTION(this);
54 uint8_t v;
55 v = *m_current;
56 m_current++;
57 return v;
58}
59
60uint16_t
62{
63 NS_LOG_FUNCTION(this);
64 uint8_t byte0 = ReadU8();
65 uint8_t byte1 = ReadU8();
66 uint16_t data = byte1;
67 data <<= 8;
68 data |= byte0;
69 return data;
70}
71
74{
75 NS_LOG_FUNCTION(this);
76 uint8_t byte0 = ReadU8();
77 uint8_t byte1 = ReadU8();
78 uint8_t byte2 = ReadU8();
79 uint8_t byte3 = ReadU8();
80 uint32_t data = byte3;
81 data <<= 8;
82 data |= byte2;
83 data <<= 8;
84 data |= byte1;
85 data <<= 8;
86 data |= byte0;
87 return data;
88}
89
90#endif /* TAG_BUFFER_USE_INLINE */
91
92void
94{
95 NS_LOG_FUNCTION(this << data);
96 WriteU8((data >> 0) & 0xff);
97 WriteU8((data >> 8) & 0xff);
98 WriteU8((data >> 16) & 0xff);
99 WriteU8((data >> 24) & 0xff);
100 WriteU8((data >> 32) & 0xff);
101 WriteU8((data >> 40) & 0xff);
102 WriteU8((data >> 48) & 0xff);
103 WriteU8((data >> 56) & 0xff);
104}
105
106void
108{
109 NS_LOG_FUNCTION(this << v);
110 auto buf = (uint8_t*)&v;
111 for (uint32_t i = 0; i < sizeof(double); ++i, ++buf)
112 {
113 WriteU8(*buf);
114 }
115}
116
117void
118TagBuffer::Write(const uint8_t* buffer, uint32_t size)
119{
120 NS_LOG_FUNCTION(this << &buffer << size);
121 for (uint32_t i = 0; i < size; ++i, ++buffer)
122 {
123 WriteU8(*buffer);
124 }
125}
126
127uint64_t
129{
130 NS_LOG_FUNCTION(this);
131 uint8_t byte0 = ReadU8();
132 uint8_t byte1 = ReadU8();
133 uint8_t byte2 = ReadU8();
134 uint8_t byte3 = ReadU8();
135 uint8_t byte4 = ReadU8();
136 uint8_t byte5 = ReadU8();
137 uint8_t byte6 = ReadU8();
138 uint8_t byte7 = ReadU8();
139 uint64_t data = byte7;
140 data <<= 8;
141 data |= byte6;
142 data <<= 8;
143 data |= byte5;
144 data <<= 8;
145 data |= byte4;
146 data <<= 8;
147 data |= byte3;
148 data <<= 8;
149 data |= byte2;
150 data <<= 8;
151 data |= byte1;
152 data <<= 8;
153 data |= byte0;
154
155 return data;
156}
157
158double
160{
161 NS_LOG_FUNCTION(this);
162 double v;
163 auto buf = (uint8_t*)&v;
164 for (uint32_t i = 0; i < sizeof(double); ++i, ++buf)
165 {
166 *buf = ReadU8();
167 }
168 return v;
169}
170
171void
172TagBuffer::Read(uint8_t* buffer, uint32_t size)
173{
174 NS_LOG_FUNCTION(this << &buffer << size);
175 std::memcpy(buffer, m_current, size);
176 m_current += size;
178}
179
180TagBuffer::TagBuffer(uint8_t* start, uint8_t* end)
181 : m_current(start),
182 m_end(end)
183{
184 NS_LOG_FUNCTION(this << &start << &end);
185}
186
187void
189{
190 NS_LOG_FUNCTION(this << trim);
191 NS_ASSERT(m_current <= (m_end - trim));
192 m_end -= trim;
193}
194
195void
197{
198 NS_LOG_FUNCTION(this << &o);
199 NS_ASSERT(o.m_end >= o.m_current);
201 uintptr_t size = o.m_end - o.m_current;
202 NS_ASSERT(size <= (uintptr_t)(m_end - m_current));
203 std::memcpy(m_current, o.m_current, size);
204 m_current += size;
205}
206
207} // namespace ns3
read and write tag data
Definition tag-buffer.h:41
TagBuffer(uint8_t *start, uint8_t *end)
Constructor.
TAG_BUFFER_INLINE uint32_t ReadU32()
Definition tag-buffer.h:206
void WriteU64(uint64_t v)
Definition tag-buffer.cc:93
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
void Read(uint8_t *buffer, uint32_t size)
void WriteDouble(double v)
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition tag-buffer.h:161
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition tag-buffer.h:185
TAG_BUFFER_INLINE uint16_t ReadU16()
Definition tag-buffer.h:195
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition tag-buffer.h:176
uint64_t ReadU64()
uint8_t * m_current
current TagBuffer position
Definition tag-buffer.h:147
uint8_t * m_end
end TagBuffer position
Definition tag-buffer.h:148
double ReadDouble()
void Write(const uint8_t *buffer, uint32_t size)
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition tag-buffer.h:169
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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]