1.1 --- a/src/common/tags.cc Thu Apr 10 14:08:14 2008 -0700
1.2 +++ b/src/common/tags.cc Fri Apr 11 11:29:49 2008 -0700
1.3 @@ -20,29 +20,59 @@
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 -NS_LOG_COMPONENT_DEFINE ("Tags");
1.12 +#ifdef USE_FREE_LIST
1.13 +
1.14 +struct Tags::TagData *Tags::gFree = 0;
1.15 +uint32_t Tags::gN_free = 0;
1.16
1.17 struct Tags::TagData *
1.18 -Tags::AllocData (uint32_t size) const
1.19 +Tags::AllocData (void) const
1.20 {
1.21 struct Tags::TagData *retval;
1.22 - retval = new struct Tags::TagData ();
1.23 - retval->m_data = new uint8_t [size];
1.24 - NS_LOG_DEBUG ("alloc " << retval << " in " << (int *)retval->m_data);
1.25 + if (gFree != 0)
1.26 + {
1.27 + retval = gFree;
1.28 + gFree = gFree->m_next;
1.29 + gN_free--;
1.30 + }
1.31 + else
1.32 + {
1.33 + retval = new struct Tags::TagData ();
1.34 + }
1.35 return retval;
1.36 }
1.37
1.38 void
1.39 Tags::FreeData (struct TagData *data) const
1.40 {
1.41 - NS_LOG_DEBUG ("free " << data << " in " << (int *)data->m_data);
1.42 - delete [] data->m_data;
1.43 + if (gN_free > 1000)
1.44 + {
1.45 + delete data;
1.46 + return;
1.47 + }
1.48 + gN_free++;
1.49 + data->m_next = gFree;
1.50 + data->m_id = 0;
1.51 + gFree = data;
1.52 +}
1.53 +#else
1.54 +struct Tags::TagData *
1.55 +Tags::AllocData (void) const
1.56 +{
1.57 + struct Tags::TagData *retval;
1.58 + retval = new struct Tags::TagData ();
1.59 + return retval;
1.60 +}
1.61 +
1.62 +void
1.63 +Tags::FreeData (struct TagData *data) const
1.64 +{
1.65 delete data;
1.66 }
1.67 +#endif
1.68
1.69 bool
1.70 Tags::Remove (uint32_t id)
1.71 @@ -73,7 +103,7 @@
1.72 */
1.73 continue;
1.74 }
1.75 - struct TagData *copy = AllocData (Tags::SIZE);
1.76 + struct TagData *copy = AllocData ();
1.77 copy->m_id = cur->m_id;
1.78 copy->m_count = 1;
1.79 copy->m_next = 0;
1.80 @@ -153,7 +183,7 @@
1.81 }
1.82 bytesRead += uidStringSize;
1.83 uint32_t uid = TagRegistry::GetUidFromUidString (uidString);
1.84 - struct TagData *newStart = AllocData (Tags::SIZE);
1.85 + struct TagData *newStart = AllocData ();
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 Thu Apr 10 14:08:14 2008 -0700
2.2 +++ b/src/common/tags.h Fri Apr 11 11:29:49 2008 -0700
2.3 @@ -63,16 +63,19 @@
2.4 };
2.5 private:
2.6 struct TagData {
2.7 - struct TagData *m_next;
2.8 - uint32_t m_id;
2.9 - uint32_t m_count;
2.10 - uint8_t *m_data;
2.11 + uint8_t m_data[Tags::SIZE];
2.12 + struct TagData *m_next;
2.13 + uint32_t m_id;
2.14 + uint32_t m_count;
2.15 };
2.16
2.17 bool Remove (uint32_t id);
2.18 - struct Tags::TagData *AllocData (uint32_t size) const;
2.19 + struct Tags::TagData *AllocData (void) 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 @@ -105,11 +108,11 @@
2.29 {
2.30 NS_ASSERT (cur->m_id != T::GetUid ());
2.31 }
2.32 - struct TagData *newStart = AllocData (Tags::SIZE);
2.33 + struct TagData *newStart = AllocData ();
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 @@ -149,7 +152,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 }