A Discrete-Event Network Simulator
API
packet-tag-list.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 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 #ifndef PACKET_TAG_LIST_H
20 #define PACKET_TAG_LIST_H
21 
27 #include "ns3/type-id.h"
28 
29 #include <ostream>
30 #include <stdint.h>
31 
32 namespace ns3
33 {
34 
35 class Tag;
36 
125 {
126  public:
142  struct TagData
143  {
145  uint32_t count;
147  uint32_t size;
148  uint8_t data[1];
149  };
150 
154  inline PacketTagList();
163  inline PacketTagList(const PacketTagList& o);
173  inline PacketTagList& operator=(const PacketTagList& o);
179  inline ~PacketTagList();
180 
186  void Add(const Tag& tag) const;
194  bool Remove(Tag& tag);
204  bool Replace(Tag& tag);
212  bool Peek(Tag& tag) const;
216  inline void RemoveAll();
220  const PacketTagList::TagData* Head() const;
226  uint32_t GetSerializedSize() const;
235  uint32_t Serialize(uint32_t* buffer, uint32_t maxSize) const;
244  uint32_t Deserialize(const uint32_t* buffer, uint32_t size);
245 
246  private:
254  static TagData* CreateTagData(size_t dataSize);
255 
267  typedef bool (PacketTagList::*COWWriter)(Tag& tag,
268  bool preMerge,
269  TagData* cur,
270  TagData** prevNext);
278  bool COWTraverse(Tag& tag, PacketTagList::COWWriter Writer);
290  bool RemoveWriter(Tag& tag, bool preMerge, TagData* cur, TagData** prevNext);
302  bool ReplaceWriter(Tag& tag, bool preMerge, TagData* cur, TagData** prevNext);
303 
308 };
309 
310 } // namespace ns3
311 
312 /****************************************************
313  * Implementation of inline methods for performance
314  ****************************************************/
315 
316 namespace ns3
317 {
318 
320  : m_next()
321 {
322 }
323 
325  : m_next(o.m_next)
326 {
327  if (m_next != nullptr)
328  {
329  m_next->count++;
330  }
331 }
332 
335 {
336  // self assignment
337  if (m_next == o.m_next)
338  {
339  return *this;
340  }
341  RemoveAll();
342  m_next = o.m_next;
343  if (m_next != nullptr)
344  {
345  m_next->count++;
346  }
347  return *this;
348 }
349 
351 {
352  RemoveAll();
353 }
354 
355 void
357 {
358  TagData* prev = nullptr;
359  for (TagData* cur = m_next; cur != nullptr; cur = cur->next)
360  {
361  cur->count--;
362  if (cur->count > 0)
363  {
364  break;
365  }
366  if (prev != nullptr)
367  {
368  prev->~TagData();
369  std::free(prev);
370  }
371  prev = cur;
372  }
373  if (prev != nullptr)
374  {
375  prev->~TagData();
376  std::free(prev);
377  }
378  m_next = nullptr;
379 }
380 
381 } // namespace ns3
382 
383 #endif /* PACKET_TAG_LIST_H */
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
void Add(const Tag &tag) const
Add a tag to the head of this branch.
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
bool ReplaceWriter(Tag &tag, bool preMerge, TagData *cur, TagData **prevNext)
Copy-on-write implementing Replace.
void RemoveAll()
Remove all tags from this list (up to the first merge).
bool Replace(Tag &tag)
Replace the value of a tag.
bool Peek(Tag &tag) const
Find a tag and return its value.
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
bool COWTraverse(Tag &tag, PacketTagList::COWWriter Writer)
Traverse the list implementing copy-on-write, using Writer.
bool RemoveWriter(Tag &tag, bool preMerge, TagData *cur, TagData **prevNext)
Copy-on-write implementing Remove.
const PacketTagList::TagData * Head() const
PacketTagList()
Create a new PacketTagList.
TagData * m_next
Pointer to first TagData on the list.
static TagData * CreateTagData(size_t dataSize)
Allocate and construct a TagData struct, sizing the data area large enough to serialize dataSize byte...
PacketTagList & operator=(const PacketTagList &o)
Assignment.
~PacketTagList()
Destructor.
bool(PacketTagList::* COWWriter)(Tag &tag, bool preMerge, TagData *cur, TagData **prevNext)
Typedef of method function pointer for copy-on-write operations.
tag a set of bytes in a packet
Definition: tag.h:39
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Tree node for sharing serialized tags.
TagData * next
Pointer to next in list.
TypeId tid
Type of the tag serialized into data.
uint32_t size
Size of the data buffer.
uint32_t count
Number of incoming links.
uint8_t data[1]
Serialization buffer.
uint32_t prev