From 62a72eadac8eddefb1db6d80ad243a898a2bcf50 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Fri, 9 Nov 2018 17:17:27 +0100 Subject: [PATCH] Make possible to release allocated memory when clearing containers --- src/containers/List.h | 17 ++++++++++---- src/containers/Map.h | 53 +++++++++++++++++++------------------------ src/containers/Set.h | 50 ++++++++++++++++------------------------ 3 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/containers/List.h b/src/containers/List.h index 2e09887f..1b1f40aa 100755 --- a/src/containers/List.h +++ b/src/containers/List.h @@ -243,10 +243,7 @@ class List { if (mCapacity > 0) { // Clear the list - clear(); - - // Release the memory allocated on the heap - mAllocator.release(mBuffer, mCapacity * sizeof(T)); + clear(true); } } @@ -365,7 +362,7 @@ class List { } /// Clear the list - void clear() { + void clear(bool releaseMemory = false) { // Call the destructor of each element for (uint i=0; i < mSize; i++) { @@ -373,6 +370,16 @@ class List { } mSize = 0; + + // If we need to release all the allocated memory + if (releaseMemory && mCapacity > 0) { + + // Release the memory allocated on the heap + mAllocator.release(mBuffer, mCapacity * sizeof(T)); + + mBuffer = nullptr; + mCapacity = 0; + } } /// Return the number of elements in the list diff --git a/src/containers/Map.h b/src/containers/Map.h index ff20b4a3..d341954c 100755 --- a/src/containers/Map.h +++ b/src/containers/Map.h @@ -240,32 +240,6 @@ class Map { return number; } - /// Clear and reset the map - void reset() { - - // If elements have been allocated - if (mCapacity > 0) { - - // Clear the map - clear(); - - // Destroy the entries - for (int i=0; i < mCapacity; i++) { - mEntries[i].~Entry(); - } - - mAllocator.release(mBuckets, mCapacity * sizeof(int)); - mAllocator.release(mEntries, mCapacity * sizeof(Entry)); - - mNbUsedEntries = 0; - mNbFreeEntries = 0; - mCapacity = 0; - mBuckets = nullptr; - mEntries = nullptr; - mFreeIndex = -1; - } - } - public: /// Class Iterator @@ -423,7 +397,7 @@ class Map { /// Destructor ~Map() { - reset(); + clear(true); } /// Allocate memory for a given number of elements @@ -577,11 +551,13 @@ class Map { } /// Clear the map - void clear() { + void clear(bool releaseMemory = false) { if (mNbUsedEntries > 0) { + // Remove the key/value pair of each entry for (int i=0; i < mCapacity; i++) { + mBuckets[i] = -1; mEntries[i].next = -1; if (mEntries[i].keyValue != nullptr) { @@ -596,6 +572,23 @@ class Map { mNbFreeEntries = 0; } + // If elements have been allocated + if (releaseMemory && mCapacity > 0) { + + // Destroy the entries + for (int i=0; i < mCapacity; i++) { + mEntries[i].~Entry(); + } + + // Release memory + mAllocator.release(mBuckets, mCapacity * sizeof(int)); + mAllocator.release(mEntries, mCapacity * sizeof(Entry)); + + mCapacity = 0; + mBuckets = nullptr; + mEntries = nullptr; + } + assert(size() == 0); } @@ -709,8 +702,8 @@ class Map { // Check for self assignment if (this != &map) { - // Reset the map - reset(); + // Clear the map + clear(true); if (map.mCapacity > 0) { diff --git a/src/containers/Set.h b/src/containers/Set.h index fce0a051..77279fdb 100755 --- a/src/containers/Set.h +++ b/src/containers/Set.h @@ -245,32 +245,6 @@ class Set { return number; } - /// Clear and reset the set - void reset() { - - // If elements have been allocated - if (mCapacity > 0) { - - // Clear the list - clear(); - - // Destroy the entries - for (int i=0; i < mCapacity; i++) { - mEntries[i].~Entry(); - } - - mAllocator.release(mBuckets, mCapacity * sizeof(int)); - mAllocator.release(mEntries, mCapacity * sizeof(Entry)); - - mNbUsedEntries = 0; - mNbFreeEntries = 0; - mCapacity = 0; - mBuckets = nullptr; - mEntries = nullptr; - mFreeIndex = -1; - } - } - public: /// Class Iterator @@ -429,7 +403,7 @@ class Set { /// Destructor ~Set() { - reset(); + clear(true); } /// Allocate memory for a given number of elements @@ -572,7 +546,7 @@ class Set { } /// Clear the set - void clear() { + void clear(bool releaseMemory = false) { if (mNbUsedEntries > 0) { @@ -591,6 +565,22 @@ class Set { mNbFreeEntries = 0; } + // If elements have been allocated + if (releaseMemory && mCapacity > 0) { + + // Destroy the entries + for (int i=0; i < mCapacity; i++) { + mEntries[i].~Entry(); + } + + mAllocator.release(mBuckets, mCapacity * sizeof(int)); + mAllocator.release(mEntries, mCapacity * sizeof(Entry)); + + mCapacity = 0; + mBuckets = nullptr; + mEntries = nullptr; + } + assert(size() == 0); } @@ -660,8 +650,8 @@ class Set { // Check for self assignment if (this != &set) { - // Reset the set - reset(); + // Clear the set + clear(true); if (set.mCapacity > 0) {