diff --git a/arraylist.hpp b/arraylist.hpp index 4884952..bf18b83 100644 --- a/arraylist.hpp +++ b/arraylist.hpp @@ -13,6 +13,13 @@ struct Array_List { length = initial_capacity; } + ~Array_List() { + if (data) { + free(data); + data = nullptr; + } + } + type* begin() { return data; } diff --git a/bucket_allocator.hpp b/bucket_allocator.hpp index b9ee9ba..39b562d 100644 --- a/bucket_allocator.hpp +++ b/bucket_allocator.hpp @@ -48,10 +48,18 @@ public: } ~Bucket_Allocator() { - for (int i = 0; i < bucket_count; ++i) { - free(buckets[i]); + for (int i = 0; i < latest_bucket-1; ++i) { + for (int j = 0; j < bucket_size; ++j) { + ::delete (buckets[i]+j); + } } - ::free(buckets); + + for (int i = 0; i < next_index_in_latest_bucket; ++i) { + ::delete (buckets[latest_bucket]+i); + } + + ::delete[] buckets; + free_list.~Array_List(); } type* allocate(unsigned int amount = 1) { @@ -84,13 +92,9 @@ public: } } - void free(type* obj) { + void free_object(type* obj) { + delete obj; free_list.append(obj); } - void reset() { - latest_bucket = 0; - next_index_in_latest_bucket = 0; - next_bucket_index = 0; - } }; diff --git a/build.bat b/build.bat index 6bf3653..099d8a2 100644 --- a/build.bat +++ b/build.bat @@ -11,7 +11,7 @@ cl^ ../test.cpp^ /Fe%exeName% /MP /openmp /W3 /std:c++latest^ /nologo /EHsc /Z7^ - /link /incremental /debug:fastlink + /link /incremental if %errorlevel% == 0 ( echo. diff --git a/hashmap.hpp b/hashmap.hpp index 2a7ebea..4dbac49 100644 --- a/hashmap.hpp +++ b/hashmap.hpp @@ -30,6 +30,13 @@ struct Hash_Map { data = (HM_Cell*)calloc(initial_capacity, sizeof(HM_Cell)); } + ~Hash_Map() { + if (data) { + free(data); + data = nullptr; + } + } + int get_index_of_living_cell_if_it_exists(key_type key, u64 hash_val) { // int index = hash_val & (current_capacity - 1); int index = hash_val % current_capacity; diff --git a/test.cpp b/test.cpp index f097bb3..4ce3e4c 100644 --- a/test.cpp +++ b/test.cpp @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) { *a = 1; printf("%d\n", *a); - ba.free(a); + ba.free_object(a); int* b = ba.allocate(); *b = 2;