A Discrete-Event Network Simulator
API
packet-tag-list.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 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 #ifndef PACKET_TAG_LIST_H
21 #define PACKET_TAG_LIST_H
22 
28 #include <stdint.h>
29 #include <ostream>
30 #include "ns3/type-id.h"
31 
32 namespace ns3 {
33 
34 class Tag;
35 
124 {
125 public:
141  struct TagData
142  {
143  struct TagData * next;
144  uint32_t count;
146  uint32_t size;
147  uint8_t data[1];
148  }; /* struct TagData */
149 
153  inline PacketTagList ();
162  inline PacketTagList (PacketTagList const &o);
172  inline PacketTagList &operator = (PacketTagList const &o);
178  inline ~PacketTagList ();
179 
185  void Add (Tag const&tag) const;
193  bool Remove (Tag &tag);
203  bool Replace (Tag &tag);
211  bool Peek (Tag &tag) const;
215  inline void RemoveAll (void);
219  const struct PacketTagList::TagData *Head (void) const;
225  uint32_t GetSerializedSize (void) const;
234  uint32_t Serialize (uint32_t* buffer, uint32_t maxSize) const;
243  uint32_t Deserialize (const uint32_t* buffer, uint32_t size);
244 
245 private:
253  static
254  TagData * CreateTagData (size_t dataSize);
255 
267  typedef bool (PacketTagList::*COWWriter)
268  (Tag & tag, bool preMerge,
269  struct TagData * cur, struct TagData ** prevNext);
277  bool COWTraverse (Tag & tag, PacketTagList::COWWriter Writer);
289  bool RemoveWriter (Tag & tag, bool preMerge,
290  struct TagData * cur, struct TagData ** prevNext);
302  bool ReplaceWriter (Tag & tag, bool preMerge,
303  struct TagData * cur, struct TagData ** prevNext);
304 
308  struct TagData *m_next;
309 };
310 
311 } // namespace ns3
312 
313 /****************************************************
314  * Implementation of inline methods for performance
315  ****************************************************/
316 
317 namespace ns3 {
318 
320  : m_next ()
321 {
322 }
323 
325  : m_next (o.m_next)
326 {
327  if (m_next != 0)
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 != 0)
344  {
345  m_next->count++;
346  }
347  return *this;
348 }
349 
351 {
352  RemoveAll ();
353 }
354 
355 void
357 {
358  struct TagData *prev = 0;
359  for (struct TagData *cur = m_next; cur != 0; cur = cur->next)
360  {
361  cur->count--;
362  if (cur->count > 0)
363  {
364  break;
365  }
366  if (prev != 0)
367  {
368  prev->~TagData ();
369  std::free (prev);
370  }
371  prev = cur;
372  }
373  if (prev != 0)
374  {
375  prev->~TagData ();
376  std::free (prev);
377  }
378  m_next = 0;
379 }
380 
381 } // namespace ns3
382 
383 #endif /* PACKET_TAG_LIST_H */
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::PacketTagList::Deserialize
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
Definition: packet-tag-list.cc:398
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::PacketTagList::TagData::data
uint8_t data[1]
Serialization buffer.
Definition: packet-tag-list.h:147
ns3::PacketTagList::TagData
Tree node for sharing serialized tags.
Definition: packet-tag-list.h:142
ns3::PacketTagList::Peek
bool Peek(Tag &tag) const
Find a tag and return its value.
Definition: packet-tag-list.cc:280
ns3::PacketTagList::COWWriter
bool(PacketTagList::* COWWriter)(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Typedef of method function pointer for copy-on-write operations.
Definition: packet-tag-list.h:268
ns3::PacketTagList::TagData::tid
TypeId tid
Type of the tag serialized into data.
Definition: packet-tag-list.h:145
prev
uint32_t prev
Definition: tcp-bbr-example.cc:67
ns3::Tag
tag a set of bytes in a packet
Definition: tag.h:37
ns3::PacketTagList::ReplaceWriter
bool ReplaceWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Replace.
Definition: packet-tag-list.cc:227
ns3::PacketTagList::Add
void Add(Tag const &tag) const
Add a tag to the head of this branch.
Definition: packet-tag-list.cc:260
ns3::PacketTagList::TagData::next
struct TagData * next
Pointer to next in list.
Definition: packet-tag-list.h:143
ns3::PacketTagList::CreateTagData
static TagData * CreateTagData(size_t dataSize)
Allocate and construct a TagData struct, sizing the data area large enough to serialize dataSize byte...
Definition: packet-tag-list.cc:38
ns3::PacketTagList::operator=
PacketTagList & operator=(PacketTagList const &o)
Assignment.
Definition: packet-tag-list.h:334
ns3::PacketTagList::RemoveAll
void RemoveAll(void)
Remove all tags from this list (up to the first merge).
Definition: packet-tag-list.h:356
ns3::PacketTagList::Replace
bool Replace(Tag &tag)
Replace the value of a tag.
Definition: packet-tag-list.cc:215
ns3::PacketTagList::COWTraverse
bool COWTraverse(Tag &tag, PacketTagList::COWWriter Writer)
Traverse the list implementing copy-on-write, using Writer.
Definition: packet-tag-list.cc:55
ns3::PacketTagList::TagData::size
uint32_t size
Size of the data buffer.
Definition: packet-tag-list.h:146
ns3::PacketTagList::Remove
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
Definition: packet-tag-list.cc:176
ns3::PacketTagList::m_next
struct TagData * m_next
Pointer to first TagData on the list.
Definition: packet-tag-list.h:308
ns3::PacketTagList::TagData::count
uint32_t count
Number of incoming links.
Definition: packet-tag-list.h:144
ns3::PacketTagList::Serialize
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
Definition: packet-tag-list.cc:329
ns3::PacketTagList
List of the packet tags stored in a packet.
Definition: packet-tag-list.h:124
ns3::PacketTagList::~PacketTagList
~PacketTagList()
Destructor.
Definition: packet-tag-list.h:350
ns3::PacketTagList::GetSerializedSize
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet-tag-list.cc:304
ns3::PacketTagList::RemoveWriter
bool RemoveWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Remove.
Definition: packet-tag-list.cc:183
ns3::PacketTagList::PacketTagList
PacketTagList()
Create a new PacketTagList.
Definition: packet-tag-list.h:319
ns3::PacketTagList::Head
const struct PacketTagList::TagData * Head(void) const
Definition: packet-tag-list.cc:298