Cleaned up dynamic memory management, explicit pointer zeroing from destroy()

This commit is contained in:
Pavel Kirienko
2014-02-19 00:39:58 +04:00
parent 2f9c0087ef
commit 0d50ec077c
4 changed files with 53 additions and 15 deletions
+24 -8
View File
@@ -46,6 +46,26 @@ class Map : Noncopyable
IsDynamicallyAllocatable<KVGroup>::check();
}
static KVGroup* instantiate(IAllocator* allocator)
{
assert(allocator);
void* const praw = allocator->allocate(sizeof(KVGroup));
if (praw == NULL)
return NULL;
return new (praw) KVGroup();
}
static void destroy(KVGroup*& obj, IAllocator* allocator)
{
assert(allocator);
if (obj != NULL)
{
obj->~KVGroup();
allocator->deallocate(obj);
obj = NULL;
}
}
KVPair* find(const Key& key)
{
for (int i = 0; i < NUM_KV; i++)
@@ -140,8 +160,7 @@ class Map : Noncopyable
if (remove_this)
{
list_.remove(p);
p->~KVGroup();
allocator_->deallocate(p);
KVGroup::destroy(p, allocator_);
}
p = next;
}
@@ -186,14 +205,11 @@ public:
return &kv->value;
}
void* const praw = allocator_->allocate(sizeof(KVGroup));
if (praw == NULL)
KVGroup* const kvg = KVGroup::instantiate(allocator_);
if (kvg == NULL)
return NULL;
KVGroup* const kvg = new (praw) KVGroup();
assert(kvg);
kvg->kvs[0] = KVPair(key, value);
list_.insert(kvg);
kvg->kvs[0] = KVPair(key, value);
return &kvg->kvs[0].value;
}
@@ -53,6 +53,8 @@ public:
IsDynamicallyAllocatable<Entry>::check();
}
static void destroy(Entry*& obj, IAllocator* allocator);
bool isExpired(uint64_t monotonic_timestamp) const { return monotonic_timestamp > monotonic_deadline; }
bool qosHigherThan(const CanFrame& rhs_frame, Qos rhs_qos) const;
@@ -104,7 +106,7 @@ public:
void push(const CanFrame& frame, uint64_t monotonic_tx_deadline, Qos qos);
Entry* peek(); // Modifier
void remove(Entry* entry);
void remove(Entry*& entry);
bool topPriorityHigherOrEqual(const CanFrame& rhs_frame) const;