From f707cda2a773f37667482d8526bd3f3d8d0c5f35 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 11 Apr 2008 11:29:49 -0700 Subject: [PATCH] Backed out changeset 9fa2f44cdafa --- src/common/tags.cc | 48 +++++++++++++++++++++++++++++++++++++--------- src/common/tags.h | 19 ++++++++++-------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/common/tags.cc b/src/common/tags.cc index ce1059cfb..a719417a4 100644 --- a/src/common/tags.cc +++ b/src/common/tags.cc @@ -20,29 +20,59 @@ #include "tags.h" #include #include "ns3/fatal-error.h" -#include "ns3/log.h" namespace ns3 { -NS_LOG_COMPONENT_DEFINE ("Tags"); +#ifdef USE_FREE_LIST + +struct Tags::TagData *Tags::gFree = 0; +uint32_t Tags::gN_free = 0; struct Tags::TagData * -Tags::AllocData (uint32_t size) const +Tags::AllocData (void) const { struct Tags::TagData *retval; + if (gFree != 0) + { + retval = gFree; + gFree = gFree->m_next; + gN_free--; + } + else + { + retval = new struct Tags::TagData (); + } + 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 (); - 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 { - NS_LOG_DEBUG ("free " << data << " in " << (int *)data->m_data); - delete [] data->m_data; delete data; } +#endif bool Tags::Remove (uint32_t id) @@ -73,7 +103,7 @@ Tags::Remove (uint32_t id) */ continue; } - struct TagData *copy = AllocData (Tags::SIZE); + struct TagData *copy = AllocData (); copy->m_id = cur->m_id; copy->m_count = 1; copy->m_next = 0; @@ -153,7 +183,7 @@ Tags::Deserialize (Buffer::Iterator i) } bytesRead += uidStringSize; uint32_t uid = TagRegistry::GetUidFromUidString (uidString); - struct TagData *newStart = AllocData (Tags::SIZE); + struct TagData *newStart = AllocData (); newStart->m_count = 1; newStart->m_next = 0; newStart->m_id = uid; diff --git a/src/common/tags.h b/src/common/tags.h index 9e156aaae..a88b6a6e0 100644 --- a/src/common/tags.h +++ b/src/common/tags.h @@ -63,16 +63,19 @@ public: }; private: struct TagData { - struct TagData *m_next; - uint32_t m_id; - uint32_t m_count; - uint8_t *m_data; + uint8_t m_data[Tags::SIZE]; + struct TagData *m_next; + uint32_t m_id; + uint32_t m_count; }; bool Remove (uint32_t id); - struct Tags::TagData *AllocData (uint32_t size) const; + struct Tags::TagData *AllocData (void) const; void FreeData (struct TagData *data) const; + static struct Tags::TagData *gFree; + static uint32_t gN_free; + struct TagData *m_next; }; @@ -105,11 +108,11 @@ Tags::Add (T const&tag) const { NS_ASSERT (cur->m_id != T::GetUid ()); } - struct TagData *newStart = AllocData (Tags::SIZE); + struct TagData *newStart = AllocData (); 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 (this)->m_next = newStart; @@ -149,7 +152,7 @@ Tags::Peek (T &tag) const if (cur->m_id == T::GetUid ()) { /* found tag */ - T *data = reinterpret_cast (cur->m_data); + T *data = reinterpret_cast (&cur->m_data); tag = T (*data); return true; }