18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
19 */ |
19 */ |
20 #include "tags.h" |
20 #include "tags.h" |
21 #include <string.h> |
21 #include <string.h> |
22 #include "ns3/fatal-error.h" |
22 #include "ns3/fatal-error.h" |
23 #include "ns3/log.h" |
|
24 |
23 |
25 namespace ns3 { |
24 namespace ns3 { |
26 |
25 |
27 NS_LOG_COMPONENT_DEFINE ("Tags"); |
26 #ifdef USE_FREE_LIST |
|
27 |
|
28 struct Tags::TagData *Tags::gFree = 0; |
|
29 uint32_t Tags::gN_free = 0; |
28 |
30 |
29 struct Tags::TagData * |
31 struct Tags::TagData * |
30 Tags::AllocData (uint32_t size) const |
32 Tags::AllocData (void) const |
|
33 { |
|
34 struct Tags::TagData *retval; |
|
35 if (gFree != 0) |
|
36 { |
|
37 retval = gFree; |
|
38 gFree = gFree->m_next; |
|
39 gN_free--; |
|
40 } |
|
41 else |
|
42 { |
|
43 retval = new struct Tags::TagData (); |
|
44 } |
|
45 return retval; |
|
46 } |
|
47 |
|
48 void |
|
49 Tags::FreeData (struct TagData *data) const |
|
50 { |
|
51 if (gN_free > 1000) |
|
52 { |
|
53 delete data; |
|
54 return; |
|
55 } |
|
56 gN_free++; |
|
57 data->m_next = gFree; |
|
58 data->m_id = 0; |
|
59 gFree = data; |
|
60 } |
|
61 #else |
|
62 struct Tags::TagData * |
|
63 Tags::AllocData (void) const |
31 { |
64 { |
32 struct Tags::TagData *retval; |
65 struct Tags::TagData *retval; |
33 retval = new struct Tags::TagData (); |
66 retval = new struct Tags::TagData (); |
34 retval->m_data = new uint8_t [size]; |
|
35 NS_LOG_DEBUG ("alloc " << retval << " in " << (int *)retval->m_data); |
|
36 return retval; |
67 return retval; |
37 } |
68 } |
38 |
69 |
39 void |
70 void |
40 Tags::FreeData (struct TagData *data) const |
71 Tags::FreeData (struct TagData *data) const |
41 { |
72 { |
42 NS_LOG_DEBUG ("free " << data << " in " << (int *)data->m_data); |
|
43 delete [] data->m_data; |
|
44 delete data; |
73 delete data; |
45 } |
74 } |
|
75 #endif |
46 |
76 |
47 bool |
77 bool |
48 Tags::Remove (uint32_t id) |
78 Tags::Remove (uint32_t id) |
49 { |
79 { |
50 bool found = false; |
80 bool found = false; |
71 * avoid copying each TagData located after the target id |
101 * avoid copying each TagData located after the target id |
72 * and just link the already-copied list to the next tag. |
102 * and just link the already-copied list to the next tag. |
73 */ |
103 */ |
74 continue; |
104 continue; |
75 } |
105 } |
76 struct TagData *copy = AllocData (Tags::SIZE); |
106 struct TagData *copy = AllocData (); |
77 copy->m_id = cur->m_id; |
107 copy->m_id = cur->m_id; |
78 copy->m_count = 1; |
108 copy->m_count = 1; |
79 copy->m_next = 0; |
109 copy->m_next = 0; |
80 memcpy (copy->m_data, cur->m_data, Tags::SIZE); |
110 memcpy (copy->m_data, cur->m_data, Tags::SIZE); |
81 *prevNext = copy; |
111 *prevNext = copy; |
151 uint32_t c = i.ReadU8 (); |
181 uint32_t c = i.ReadU8 (); |
152 uidString.push_back (c); |
182 uidString.push_back (c); |
153 } |
183 } |
154 bytesRead += uidStringSize; |
184 bytesRead += uidStringSize; |
155 uint32_t uid = TagRegistry::GetUidFromUidString (uidString); |
185 uint32_t uid = TagRegistry::GetUidFromUidString (uidString); |
156 struct TagData *newStart = AllocData (Tags::SIZE); |
186 struct TagData *newStart = AllocData (); |
157 newStart->m_count = 1; |
187 newStart->m_count = 1; |
158 newStart->m_next = 0; |
188 newStart->m_next = 0; |
159 newStart->m_id = uid; |
189 newStart->m_id = uid; |
160 bytesRead += TagRegistry::Deserialize (uid, newStart->m_data, i); |
190 bytesRead += TagRegistry::Deserialize (uid, newStart->m_data, i); |
161 newStart->m_next = m_next; |
191 newStart->m_next = m_next; |