allocate/deallocate buffer by hand.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 10 Apr 2008 14:08:14 -0700
changeset 2905 9fa2f44cdafa
parent 2901 81ac5e25e8c2
child 2906 5a7ae076410e
child 2908 4ebd61a5612c
allocate/deallocate buffer by hand.
src/common/tags.cc
src/common/tags.h
--- a/src/common/tags.cc	Wed Apr 09 10:04:16 2008 -0700
+++ b/src/common/tags.cc	Thu Apr 10 14:08:14 2008 -0700
@@ -20,59 +20,29 @@
 #include "tags.h"
 #include <string.h>
 #include "ns3/fatal-error.h"
+#include "ns3/log.h"
 
 namespace ns3 {
 
-#ifdef USE_FREE_LIST
-
-struct Tags::TagData *Tags::gFree = 0;
-uint32_t Tags::gN_free = 0;
+NS_LOG_COMPONENT_DEFINE ("Tags");
 
 struct Tags::TagData *
-Tags::AllocData (void) const
+Tags::AllocData (uint32_t size) const
 {
   struct Tags::TagData *retval;
-  if (gFree != 0) 
-    {
-      retval = gFree;
-      gFree = gFree->m_next;
-      gN_free--;
-    } 
-  else 
-    {
-      retval = new struct Tags::TagData ();
-    }
+  retval = new struct Tags::TagData ();
+  retval->m_data = new uint8_t [size];
+  NS_LOG_DEBUG ("alloc " << retval << " in " << (int *)retval->m_data);
   return retval;
 }
 
 void
 Tags::FreeData (struct TagData *data) const
 {
-  if (gN_free > 1000) 
-    {
-      delete data;
-      return;
-    }
-  gN_free++;
-  data->m_next = gFree;
-  data->m_id = 0;
-  gFree = data;
-}
-#else
-struct Tags::TagData *
-Tags::AllocData (void) const
-{
-  struct Tags::TagData *retval;
-  retval = new struct Tags::TagData ();
-  return retval;
-}
-
-void
-Tags::FreeData (struct TagData *data) const
-{
+  NS_LOG_DEBUG ("free " << data << " in " << (int *)data->m_data);
+  delete [] data->m_data;
   delete data;
 }
-#endif
 
 bool
 Tags::Remove (uint32_t id)
@@ -103,7 +73,7 @@
            */
           continue;
         }
-      struct TagData *copy = AllocData ();
+      struct TagData *copy = AllocData (Tags::SIZE);
       copy->m_id = cur->m_id;
       copy->m_count = 1;
       copy->m_next = 0;
@@ -183,7 +153,7 @@
         }
       bytesRead += uidStringSize;
       uint32_t uid = TagRegistry::GetUidFromUidString (uidString);
-      struct TagData *newStart = AllocData ();
+      struct TagData *newStart = AllocData (Tags::SIZE);
       newStart->m_count = 1;
       newStart->m_next = 0;
       newStart->m_id = uid;
--- a/src/common/tags.h	Wed Apr 09 10:04:16 2008 -0700
+++ b/src/common/tags.h	Thu Apr 10 14:08:14 2008 -0700
@@ -63,19 +63,16 @@
   };
 private:
   struct TagData {
-      uint8_t m_data[Tags::SIZE];
-      struct TagData *m_next;
-      uint32_t m_id;
-      uint32_t m_count;
+    struct TagData *m_next;
+    uint32_t m_id;
+    uint32_t m_count;
+    uint8_t *m_data;
   };
 
   bool Remove (uint32_t id);
-  struct Tags::TagData *AllocData (void) const;
+  struct Tags::TagData *AllocData (uint32_t size) const;
   void FreeData (struct TagData *data) const;
 
-  static struct Tags::TagData *gFree;
-  static uint32_t gN_free;
-
   struct TagData *m_next;
 };
 
@@ -108,11 +105,11 @@
     {
       NS_ASSERT (cur->m_id != T::GetUid ());
     }
-  struct TagData *newStart = AllocData ();
+  struct TagData *newStart = AllocData (Tags::SIZE);
   newStart->m_count = 1;
   newStart->m_next = 0;
   newStart->m_id = T::GetUid ();
-  void *buf = &newStart->m_data;
+  void *buf = newStart->m_data;
   new (buf) T (tag);
   newStart->m_next = m_next;
   const_cast<Tags *> (this)->m_next = newStart;
@@ -152,7 +149,7 @@
       if (cur->m_id == T::GetUid ()) 
         {
           /* found tag */
-          T *data = reinterpret_cast<T *> (&cur->m_data);
+          T *data = reinterpret_cast<T *> (cur->m_data);
           tag = T (*data);
           return true;
         }