Make possible to release allocated memory when clearing containers

This commit is contained in:
Daniel Chappuis 2018-11-09 17:17:27 +01:00
parent 5cdf66d009
commit 62a72eadac
3 changed files with 55 additions and 65 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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) {