From abbd0b6280738332e195d5c37430feae1dbd0d5e Mon Sep 17 00:00:00 2001 From: Felix Brendel Date: Sun, 10 Nov 2019 00:47:19 +0100 Subject: [PATCH] made more consistent --- arraylist.hpp | 18 ++++++++++++++--- bucket_allocator.hpp | 47 +++++++++++++++++++++++++------------------- build.sh | 2 +- test.cpp | 13 +++++------- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/arraylist.hpp b/arraylist.hpp index bf18b83..709bd07 100644 --- a/arraylist.hpp +++ b/arraylist.hpp @@ -14,10 +14,20 @@ struct Array_List { } ~Array_List() { - if (data) { - free(data); - data = nullptr; + free(data); + data = 0; + } + + Array_List clone() { + Array_List ret; + ret.length = length; + ret.next_index = next_index; + + ret.data = (type*)malloc(length * sizeof(type)); + for (int i = 0; i < next_index; ++i) { + ret.data[i] = data[i]; } + return ret; } type* begin() { @@ -79,6 +89,8 @@ struct Array_List { void sort(int left=-1, int right=-1) { if (left == -1) { + if (next_index == 0) + return; sort(0, next_index - 1); return; } else if (left == right) { diff --git a/bucket_allocator.hpp b/bucket_allocator.hpp index 39b562d..6f0864d 100644 --- a/bucket_allocator.hpp +++ b/bucket_allocator.hpp @@ -1,21 +1,19 @@ #include "arraylist.hpp" -template +template class Bucket_Allocator { - int latest_bucket; int next_index_in_latest_bucket; - int bucket_count; int next_bucket_index; + int bucket_count; + unsigned int bucket_size; Array_List free_list; type** buckets; void expand() { + // printf("realloc time\n"); // realloc time buckets = (type**)realloc(buckets, bucket_count * 2 * sizeof(type*)); - for (int i = bucket_count; i < bucket_count * 2; ++i) { - buckets[i] = (type*)malloc(bucket_size * sizeof(type)); - } bucket_count *= 2; } @@ -25,6 +23,7 @@ class Bucket_Allocator { if (next_bucket_index >= bucket_count) { expand(); } + buckets[next_bucket_index] = (type*)malloc(bucket_size * sizeof(type)); } void increment_pointers(int amount = 1) { @@ -35,31 +34,40 @@ class Bucket_Allocator { } public: - Bucket_Allocator(unsigned int initial_bucket_count = 1) { - latest_bucket = 0; + Bucket_Allocator(unsigned int bucket_size, unsigned int initial_bucket_count) { + this->bucket_size = bucket_size; next_index_in_latest_bucket = 0; next_bucket_index = 0; bucket_count = initial_bucket_count; buckets = (type**)malloc(bucket_count * sizeof(type*)); - for (int i = 0; i < bucket_count; ++i) { - buckets[i] = (type*)malloc(bucket_size * sizeof(type)); - } + buckets[0] = (type*)malloc(bucket_size * sizeof(type)); } ~Bucket_Allocator() { - for (int i = 0; i < latest_bucket-1; ++i) { + for (int i = 0; i <= next_bucket_index; ++i) { + free(buckets[i]); + } + + free(buckets); + } + + template + void for_each(proc p) { + free_list.sort(); + type* val; + for (int i = 0; i < next_bucket_index; ++i) { for (int j = 0; j < bucket_size; ++j) { - ::delete (buckets[i]+j); + val = buckets[i]+j; + if (free_list.sorted_find(val) == -1) + p(val); } } - - for (int i = 0; i < next_index_in_latest_bucket; ++i) { - ::delete (buckets[latest_bucket]+i); + for (int j = 0; j < next_index_in_latest_bucket; ++j) { + val = buckets[next_bucket_index]+j; + if (free_list.sorted_find(val) == -1) + p(val); } - - ::delete[] buckets; - free_list.~Array_List(); } type* allocate(unsigned int amount = 1) { @@ -93,7 +101,6 @@ public: } void free_object(type* obj) { - delete obj; free_list.append(obj); } diff --git a/build.sh b/build.sh index 30eba47..cf2f47c 100755 --- a/build.sh +++ b/build.sh @@ -7,7 +7,7 @@ pushd $SCRIPTPATH > /dev/null time clang++ -D_DEBUG -D_PROFILING -fpermissive test.cpp -g -o ./ftb --std=c++17 || exit 1 echo "" -time ./ftb +time valgrind --leak-check=full ./ftb popd > /dev/null unset TIMEFORMAT diff --git a/test.cpp b/test.cpp index 4ce3e4c..f02a800 100644 --- a/test.cpp +++ b/test.cpp @@ -4,33 +4,30 @@ #include "bucket_allocator.hpp" int main(int argc, char* argv[]) { - Bucket_Allocator ba; + Bucket_Allocator ba(2, 1); int* a = ba.allocate(); *a = 1; - printf("%d\n", *a); - ba.free_object(a); int* b = ba.allocate(); *b = 2; - printf("%d\n", *b); int* c = ba.allocate(); *c = 3; - printf("%d\n", *c); int* d = ba.allocate(); *d = 4; - printf("%d\n", *d); int* e = ba.allocate(); *e = 5; - printf("%d\n", *e); int* f = ba.allocate(); *f = 6; - printf("%d\n", *f); + + ba.for_each([](int* i){ + printf("%d\n", *i); + }); return 0; }