allocate/deallocate buffer by hand.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu Apr 10 14:08:14 2008 -0700 (22 months ago)
changeset 29059fa2f44cdafa
parent 2901 81ac5e25e8c2
child 2906 5a7ae076410e
child 2908 4ebd61a5612c
allocate/deallocate buffer by hand.
src/common/tags.cc
src/common/tags.h
     1.1 --- a/src/common/tags.cc	Wed Apr 09 10:04:16 2008 -0700
     1.2 +++ b/src/common/tags.cc	Thu Apr 10 14:08:14 2008 -0700
     1.3 @@ -20,59 +20,29 @@
     1.4  #include "tags.h"
     1.5  #include <string.h>
     1.6  #include "ns3/fatal-error.h"
     1.7 +#include "ns3/log.h"
     1.8  
     1.9  namespace ns3 {
    1.10  
    1.11 -#ifdef USE_FREE_LIST
    1.12 -
    1.13 -struct Tags::TagData *Tags::gFree = 0;
    1.14 -uint32_t Tags::gN_free = 0;
    1.15 +NS_LOG_COMPONENT_DEFINE ("Tags");
    1.16  
    1.17  struct Tags::TagData *
    1.18 -Tags::AllocData (void) const
    1.19 +Tags::AllocData (uint32_t size) const
    1.20  {
    1.21    struct Tags::TagData *retval;
    1.22 -  if (gFree != 0) 
    1.23 -    {
    1.24 -      retval = gFree;
    1.25 -      gFree = gFree->m_next;
    1.26 -      gN_free--;
    1.27 -    } 
    1.28 -  else 
    1.29 -    {
    1.30 -      retval = new struct Tags::TagData ();
    1.31 -    }
    1.32 +  retval = new struct Tags::TagData ();
    1.33 +  retval->m_data = new uint8_t [size];
    1.34 +  NS_LOG_DEBUG ("alloc " << retval << " in " << (int *)retval->m_data);
    1.35    return retval;
    1.36  }
    1.37  
    1.38  void
    1.39  Tags::FreeData (struct TagData *data) const
    1.40  {
    1.41 -  if (gN_free > 1000) 
    1.42 -    {
    1.43 -      delete data;
    1.44 -      return;
    1.45 -    }
    1.46 -  gN_free++;
    1.47 -  data->m_next = gFree;
    1.48 -  data->m_id = 0;
    1.49 -  gFree = data;
    1.50 -}
    1.51 -#else
    1.52 -struct Tags::TagData *
    1.53 -Tags::AllocData (void) const
    1.54 -{
    1.55 -  struct Tags::TagData *retval;
    1.56 -  retval = new struct Tags::TagData ();
    1.57 -  return retval;
    1.58 -}
    1.59 -
    1.60 -void
    1.61 -Tags::FreeData (struct TagData *data) const
    1.62 -{
    1.63 +  NS_LOG_DEBUG ("free " << data << " in " << (int *)data->m_data);
    1.64 +  delete [] data->m_data;
    1.65    delete data;
    1.66  }
    1.67 -#endif
    1.68  
    1.69  bool
    1.70  Tags::Remove (uint32_t id)
    1.71 @@ -103,7 +73,7 @@
    1.72             */
    1.73            continue;
    1.74          }
    1.75 -      struct TagData *copy = AllocData ();
    1.76 +      struct TagData *copy = AllocData (Tags::SIZE);
    1.77        copy->m_id = cur->m_id;
    1.78        copy->m_count = 1;
    1.79        copy->m_next = 0;
    1.80 @@ -183,7 +153,7 @@
    1.81          }
    1.82        bytesRead += uidStringSize;
    1.83        uint32_t uid = TagRegistry::GetUidFromUidString (uidString);
    1.84 -      struct TagData *newStart = AllocData ();
    1.85 +      struct TagData *newStart = AllocData (Tags::SIZE);
    1.86        newStart->m_count = 1;
    1.87        newStart->m_next = 0;
    1.88        newStart->m_id = uid;
     2.1 --- a/src/common/tags.h	Wed Apr 09 10:04:16 2008 -0700
     2.2 +++ b/src/common/tags.h	Thu Apr 10 14:08:14 2008 -0700
     2.3 @@ -63,19 +63,16 @@
     2.4    };
     2.5  private:
     2.6    struct TagData {
     2.7 -      uint8_t m_data[Tags::SIZE];
     2.8 -      struct TagData *m_next;
     2.9 -      uint32_t m_id;
    2.10 -      uint32_t m_count;
    2.11 +    struct TagData *m_next;
    2.12 +    uint32_t m_id;
    2.13 +    uint32_t m_count;
    2.14 +    uint8_t *m_data;
    2.15    };
    2.16  
    2.17    bool Remove (uint32_t id);
    2.18 -  struct Tags::TagData *AllocData (void) const;
    2.19 +  struct Tags::TagData *AllocData (uint32_t size) const;
    2.20    void FreeData (struct TagData *data) const;
    2.21  
    2.22 -  static struct Tags::TagData *gFree;
    2.23 -  static uint32_t gN_free;
    2.24 -
    2.25    struct TagData *m_next;
    2.26  };
    2.27  
    2.28 @@ -108,11 +105,11 @@
    2.29      {
    2.30        NS_ASSERT (cur->m_id != T::GetUid ());
    2.31      }
    2.32 -  struct TagData *newStart = AllocData ();
    2.33 +  struct TagData *newStart = AllocData (Tags::SIZE);
    2.34    newStart->m_count = 1;
    2.35    newStart->m_next = 0;
    2.36    newStart->m_id = T::GetUid ();
    2.37 -  void *buf = &newStart->m_data;
    2.38 +  void *buf = newStart->m_data;
    2.39    new (buf) T (tag);
    2.40    newStart->m_next = m_next;
    2.41    const_cast<Tags *> (this)->m_next = newStart;
    2.42 @@ -152,7 +149,7 @@
    2.43        if (cur->m_id == T::GetUid ()) 
    2.44          {
    2.45            /* found tag */
    2.46 -          T *data = reinterpret_cast<T *> (&cur->m_data);
    2.47 +          T *data = reinterpret_cast<T *> (cur->m_data);
    2.48            tag = T (*data);
    2.49            return true;
    2.50          }