From ee8164f236885b6603701c75fbe3a18d23616cdb Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 2 May 2025 09:59:26 -0700 Subject: [PATCH 01/20] isolating tests --- bflibcpp/testbench/array_tests.hpp | 3 +-- bflibcpp/testbench/tests.cpp | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bflibcpp/testbench/array_tests.hpp b/bflibcpp/testbench/array_tests.hpp index b7793050..d1ebe1b1 100644 --- a/bflibcpp/testbench/array_tests.hpp +++ b/bflibcpp/testbench/array_tests.hpp @@ -310,10 +310,9 @@ BFTEST_COVERAGE_FUNC(array_tests, { BFTEST_LAUNCH(test_insertingAtRandomIndex); BFTEST_LAUNCH(test_releasecallback); BFTEST_LAUNCH(test_addanddelete); - //BFTEST_LAUNCH(test_arrayOfBFStrings); // FIXME: this should work if we change how array's allocate memory BFTEST_LAUNCH(test_arrayOfCStrings); BFTEST_LAUNCH(test_appendingArrays); - + //BFTEST_LAUNCH(test_arrayOfBFStrings); // FIXME: this should work if we change how array's allocate memory }) #endif // ARRAY_TESTS_HPP diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 77ef3eac..08919eb1 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,13 +22,16 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); BFTEST_SUITE_LAUNCH(queue_tests); BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); + */ BFTEST_SUITE_LAUNCH(array_tests); + /* BFTEST_SUITE_LAUNCH(vector_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(object_tests); @@ -38,5 +41,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); + */ }) From 9ee3869d5573aa63bc396d1f07604d22b405a7ae Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 2 May 2025 10:58:37 -0700 Subject: [PATCH 02/20] first attempt, crashes though --- bflibcpp/src/array.hpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index 342ea990..e92c495d 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -221,8 +221,7 @@ class Array : public Vector { * copies content of arr to the end of ours */ void append(const Array & arr) { - this->_address = this->reallocate(this->_address, this->_count + arr._count); - //memcpy(&this->_address[this->_count], &arr._address[0], arr._count); + this->_address = this->reallocate(this->_address, this->_count, this->_count + arr._count); for (int i = this->_count; i < this->_count + arr._count; i++) { this->_address[i] = arr._address[i - this->_count]; } @@ -233,7 +232,7 @@ class Array : public Vector { * Adds object at the end of the array */ int add(T obj) { - this->_address = this->reallocate(this->_address, this->_count + 1); + this->_address = this->reallocate(this->_address, this->_count, this->_count + 1); if (this->_address == NULL) { this->_count = 0; return -3; @@ -290,8 +289,8 @@ class Array : public Vector { * adjusts address memory to size */ void adjustMemorySize(S size) { + this->_address = this->reallocate(this->_address, this->_count, size); this->_count = size; - this->_address = this->reallocate(this->_address, this->_count); } /** @@ -305,14 +304,24 @@ class Array : public Vector { * uses malloc to allocate mem */ static T * allocate(S size) { - return (T *) malloc(sizeof(T) * size); + //return (T *) malloc(sizeof(T) * size); + return (T *) new T[size]; } /** * returns modified `addr` with `newsize` */ - static T * reallocate(T * addr, S newsize) { - return (T *) realloc(addr, sizeof(T) * newsize); + static T * reallocate(T * addr, S oldsize, S newsize) { + //return (T *) realloc(addr, sizeof(T) * newsize); + T * res = new T[newsize]; + for (S i = 0; i < oldsize && i < newsize; i++) { + res[i] = addr[i]; + addr[i] = NULL; + } + + delete addr; + + return res; } /** @@ -320,7 +329,8 @@ class Array : public Vector { * by allocate() */ static void deallocate(T * value) { - free((void *) value); + //free((void *) value); + delete value; } /** From 41f35ec014a762fcc5f2f8cf07d025b70ddd47b2 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 09:57:10 -0700 Subject: [PATCH 03/20] can use free store --- bflibcpp/src/array.hpp | 4 ++-- bflibcpp/testbench/array_tests.hpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index e92c495d..d8f8819a 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -319,7 +319,7 @@ class Array : public Vector { addr[i] = NULL; } - delete addr; + delete[] addr; return res; } @@ -330,7 +330,7 @@ class Array : public Vector { */ static void deallocate(T * value) { //free((void *) value); - delete value; + delete[] value; } /** diff --git a/bflibcpp/testbench/array_tests.hpp b/bflibcpp/testbench/array_tests.hpp index d1ebe1b1..89cd383f 100644 --- a/bflibcpp/testbench/array_tests.hpp +++ b/bflibcpp/testbench/array_tests.hpp @@ -41,6 +41,7 @@ BFTEST_UNIT_FUNC(test_Initializer, 1, { BFTEST_UNIT_FUNC(test_Contains, 1, { Array arr({1, 2, 3, 4}); + /* if (!arr.contains(2)) { result = 1; printf("arr should contain 2\n"); @@ -67,6 +68,7 @@ BFTEST_UNIT_FUNC(test_Contains, 1, { result = 1; printf("ch should contain 'world'\n"); } + */ }) //int test_ObjectAtIndex() { From 05c815c23ad0bfea19d78653f3bd813532cebc68 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 10:01:11 -0700 Subject: [PATCH 04/20] cleaning up --- bflibcpp/testbench/array_tests.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/bflibcpp/testbench/array_tests.hpp b/bflibcpp/testbench/array_tests.hpp index 89cd383f..62fa2711 100644 --- a/bflibcpp/testbench/array_tests.hpp +++ b/bflibcpp/testbench/array_tests.hpp @@ -41,7 +41,6 @@ BFTEST_UNIT_FUNC(test_Initializer, 1, { BFTEST_UNIT_FUNC(test_Contains, 1, { Array arr({1, 2, 3, 4}); - /* if (!arr.contains(2)) { result = 1; printf("arr should contain 2\n"); @@ -68,10 +67,8 @@ BFTEST_UNIT_FUNC(test_Contains, 1, { result = 1; printf("ch should contain 'world'\n"); } - */ }) -//int test_ObjectAtIndex() { BFTEST_UNIT_FUNC(test_ObjectAtIndex, 1, { Array d({1.1, 2.2, 3.3, 4.4, 5.5}); @@ -90,7 +87,6 @@ BFTEST_UNIT_FUNC(test_ObjectAtIndex, 1, { } }) -//int test_indexForObject() { BFTEST_UNIT_FUNC(test_indexForObject, 2<<10, { Array arr({1, 2, 3, 4}); @@ -122,7 +118,6 @@ BFTEST_UNIT_FUNC(test_indexForObject, 2<<10, { } }) -//int test_Count() { BFTEST_UNIT_FUNC(test_Count, 1, { Array a({1, 2, 3, 4, 5}); @@ -132,7 +127,6 @@ BFTEST_UNIT_FUNC(test_Count, 1, { } }) -//int test_Setter() { BFTEST_UNIT_FUNC(test_Setter, 1, { Array a; From d009fe8e89881ea5bb2e9c520292640f5b5d3429 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 10:03:27 -0700 Subject: [PATCH 05/20] can have an array of strings --- bflibcpp/src/array.hpp | 3 --- bflibcpp/testbench/array_tests.hpp | 16 ++++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index d8f8819a..72c0608d 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -304,7 +304,6 @@ class Array : public Vector { * uses malloc to allocate mem */ static T * allocate(S size) { - //return (T *) malloc(sizeof(T) * size); return (T *) new T[size]; } @@ -312,7 +311,6 @@ class Array : public Vector { * returns modified `addr` with `newsize` */ static T * reallocate(T * addr, S oldsize, S newsize) { - //return (T *) realloc(addr, sizeof(T) * newsize); T * res = new T[newsize]; for (S i = 0; i < oldsize && i < newsize; i++) { res[i] = addr[i]; @@ -329,7 +327,6 @@ class Array : public Vector { * by allocate() */ static void deallocate(T * value) { - //free((void *) value); delete[] value; } diff --git a/bflibcpp/testbench/array_tests.hpp b/bflibcpp/testbench/array_tests.hpp index 62fa2711..e71fedaf 100644 --- a/bflibcpp/testbench/array_tests.hpp +++ b/bflibcpp/testbench/array_tests.hpp @@ -273,13 +273,6 @@ BFTEST_UNIT_FUNC(test_arrayOfCStrings, 2<<8, { BF_ASSERT(b.contains(word)); }) -BFTEST_UNIT_FUNC(test_arrayOfBFStrings, 2<<8, { - Array a({"hello", "world"}); - Array b; - b.add("hello"); - b.add("world"); -}) - BFTEST_UNIT_FUNC(test_appendingArrays, 2 << 10, { int max_size = 2 << 10; Array c; @@ -295,6 +288,13 @@ BFTEST_UNIT_FUNC(test_appendingArrays, 2 << 10, { } }) +BFTEST_UNIT_FUNC(test_arrayOfBFStrings, 1, { + Array a({"hello", "world"}); + Array b; + b.add("hello"); + b.add("world"); +}) + BFTEST_COVERAGE_FUNC(array_tests, { BFTEST_LAUNCH(test_Initializer); BFTEST_LAUNCH(test_Contains); @@ -308,7 +308,7 @@ BFTEST_COVERAGE_FUNC(array_tests, { BFTEST_LAUNCH(test_addanddelete); BFTEST_LAUNCH(test_arrayOfCStrings); BFTEST_LAUNCH(test_appendingArrays); - //BFTEST_LAUNCH(test_arrayOfBFStrings); // FIXME: this should work if we change how array's allocate memory + BFTEST_LAUNCH(test_arrayOfBFStrings); // FIXME: this should work if we change how array's allocate memory }) #endif // ARRAY_TESTS_HPP From 23277649b533af0c49de9c56be0a6e0b29eb6a9e Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 10:10:20 -0700 Subject: [PATCH 06/20] Good test --- bflibcpp/testbench/tests.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 08919eb1..77ef3eac 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,16 +22,13 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ - /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); BFTEST_SUITE_LAUNCH(queue_tests); BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); - */ BFTEST_SUITE_LAUNCH(array_tests); - /* BFTEST_SUITE_LAUNCH(vector_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(object_tests); @@ -41,6 +38,5 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); - */ }) From 2b385f7387096d6f2265da6123c57469c9ed5a33 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 10:11:04 -0700 Subject: [PATCH 07/20] update todo --- bflibcpp/todo.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 0c65fcc1..086c8611 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -16,9 +16,10 @@ - [x] address compiler warning regarding ambiguous operations - [ ] String to integer - [ ] array memory optimization, improving virtual memory usage - - [ ] BF::Array to use new/delete for memory allocation + - [x] BF::Array to use new/delete for memory allocation - [ ] optimize String - [ ] when appending to string, grab more memory so we don't have keep calling realloc all the time + - [ ] benchmark test run - [x] collection sorting algorithm - [x] make sure there are no Delete() calls and must use BFRelease instead - [x] throw compiler warnings when using delete From 1814007325083f02d680beafe70f6ffa985c67fb Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 13:21:09 -0700 Subject: [PATCH 08/20] saving work --- bflibcpp/src/array.hpp | 13 ++++++++++--- bflibcpp/testbench/tests.cpp | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index 72c0608d..12600e48 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "access.hpp" #include "vector.hpp" @@ -27,7 +28,7 @@ namespace BF { * Objects stored in array are assumed to be owned by owner of array * object */ -template +template class Array : public Vector { public: virtual const char * className() const { @@ -39,6 +40,7 @@ class Array : public Vector { this->_count = 0; this->_callback = Array::comparisonDefault; this->_releasecb = NULL; + this->_capacity = 0; } /** @@ -180,7 +182,6 @@ class Array : public Vector { return res; } - /** * Prints the array from the first element to the last */ @@ -313,7 +314,7 @@ class Array : public Vector { static T * reallocate(T * addr, S oldsize, S newsize) { T * res = new T[newsize]; for (S i = 0; i < oldsize && i < newsize; i++) { - res[i] = addr[i]; + res[i] = std::move(addr[i]); addr[i] = NULL; } @@ -375,6 +376,12 @@ class Array : public Vector { /// Holds size of _address S _count; + /** + * will hold a certain amount of reserved space for the + * array elements. we will ask for more memory when _count > _capacity + */ + S _capacity; + /** * How we compare each item in the array */ diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 77ef3eac..08919eb1 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,13 +22,16 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); BFTEST_SUITE_LAUNCH(queue_tests); BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); + */ BFTEST_SUITE_LAUNCH(array_tests); + /* BFTEST_SUITE_LAUNCH(vector_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(object_tests); @@ -38,5 +41,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); + */ }) From dd37d088d0d617ac90e2635eb4d28c25d6203f94 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 13:22:50 -0700 Subject: [PATCH 09/20] saving work --- bflibcpp/src/sort.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/bflibcpp/src/sort.hpp b/bflibcpp/src/sort.hpp index abdf8d4a..a2689fba 100644 --- a/bflibcpp/src/sort.hpp +++ b/bflibcpp/src/sort.hpp @@ -8,7 +8,6 @@ #include "array.hpp" #include "list.hpp" -//#include "typesortstrategy.hpp" namespace BF { From b5433fefcc859f78e77acd357ace7cd503607db5 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 13:42:58 -0700 Subject: [PATCH 10/20] can build --- bflibcpp/src/array.hpp | 62 ++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index 12600e48..ed954573 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -28,7 +28,7 @@ namespace BF { * Objects stored in array are assumed to be owned by owner of array * object */ -template +template class Array : public Vector { public: virtual const char * className() const { @@ -40,7 +40,10 @@ class Array : public Vector { this->_count = 0; this->_callback = Array::comparisonDefault; this->_releasecb = NULL; + this->_capacity = 0; + this->_blockSize = 2 << 3; + this->_address = this->allocate(*this, this->_blockSize); } /** @@ -77,7 +80,8 @@ class Array : public Vector { } } - this->deallocate(this->_address); + //this->deallocate(this->_address); + this->deallocate(*this); this->_address = 0; this->_count = 0; } @@ -213,7 +217,7 @@ class Array : public Vector { */ void copyFromArray(const Array * arr) { this->removeAll(); - this->_address = (T *) this->allocate(arr->count()); + this->_address = (T *) this->allocate(*this, arr->count()); this->_count = arr->count(); memcpy(this->_address, arr->address(), this->_count); } @@ -222,7 +226,7 @@ class Array : public Vector { * copies content of arr to the end of ours */ void append(const Array & arr) { - this->_address = this->reallocate(this->_address, this->_count, this->_count + arr._count); + this->_address = this->reallocate(*this, this->_count, this->_count + arr._count); for (int i = this->_count; i < this->_count + arr._count; i++) { this->_address[i] = arr._address[i - this->_count]; } @@ -233,7 +237,7 @@ class Array : public Vector { * Adds object at the end of the array */ int add(T obj) { - this->_address = this->reallocate(this->_address, this->_count, this->_count + 1); + this->_address = this->reallocate(*this, this->_count, this->_count + 1); if (this->_address == NULL) { this->_count = 0; return -3; @@ -290,7 +294,7 @@ class Array : public Vector { * adjusts address memory to size */ void adjustMemorySize(S size) { - this->_address = this->reallocate(this->_address, this->_count, size); + this->_address = this->reallocate(*this, this->_count, size); this->_count = size; } @@ -304,21 +308,33 @@ class Array : public Vector { /** * uses malloc to allocate mem */ - static T * allocate(S size) { - return (T *) new T[size]; + static T * allocate(Array & array, S size) { + if (size < array._capacity) { + return array._address; + } else { + array._capacity = size; + return (T *) new T[size]; + } } /** * returns modified `addr` with `newsize` */ - static T * reallocate(T * addr, S oldsize, S newsize) { - T * res = new T[newsize]; - for (S i = 0; i < oldsize && i < newsize; i++) { - res[i] = std::move(addr[i]); - addr[i] = NULL; + //static T * reallocate(T * addr, S oldsize, S newsize) { + static T * reallocate(Array & array, S oldsize, S newsize) { + if (newsize < array._capacity) { + return array._address; + } + + S adjustNewSize = (((newsize / array._blockSize) + 1) * array._blockSize); + array._capacity = adjustNewSize; + T * res = new T[adjustNewSize]; + for (S i = 0; i < oldsize && i < adjustNewSize; i++) { + res[i] = std::move(array._address[i]); + array._address[i] = 0; } - delete[] addr; + delete[] array._address; return res; } @@ -327,8 +343,10 @@ class Array : public Vector { * Derived must make sure this follows the standard established * by allocate() */ - static void deallocate(T * value) { - delete[] value; + //static void deallocate(T * value) { + static void deallocate(Array & array) { + delete[] array._address; + //delete[] value; } /** @@ -336,7 +354,7 @@ class Array : public Vector { */ void saveArray(const T * array, S size) { this->removeAll(); - this->_address = (T *) this->allocate(size); + this->_address = (T *) this->allocate(*this, size); this->_count = size; if (this->_address) { @@ -355,7 +373,7 @@ class Array : public Vector { typename std::initializer_list::iterator itr; this->_count = list.size(); - this->_address = (T *) this->allocate(this->_count); + this->_address = (T *) this->allocate(*this, this->_count); if (this->_address) { S i = 0; @@ -382,6 +400,14 @@ class Array : public Vector { */ S _capacity; + /** + * block size of memory allocation + * + * each reallocation where count > capacity, will always + * allocate memory in blocks of blockSize + */ + S _blockSize; + /** * How we compare each item in the array */ From 184055431d7f9dfd35f64c5984ceafb75f74819a Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 14:19:07 -0700 Subject: [PATCH 11/20] cleaning up --- bflibcpp/src/array.hpp | 36 ++++++++++++++++--------------- bflibcpp/testbench/data_tests.hpp | 4 ++-- bflibcpp/testbench/tests.cpp | 4 ---- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index ed954573..7565d0ff 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -43,7 +43,7 @@ class Array : public Vector { this->_capacity = 0; this->_blockSize = 2 << 3; - this->_address = this->allocate(*this, this->_blockSize); + this->allocate(*this, this->_blockSize); } /** @@ -217,7 +217,7 @@ class Array : public Vector { */ void copyFromArray(const Array * arr) { this->removeAll(); - this->_address = (T *) this->allocate(*this, arr->count()); + this->allocate(*this, arr->count()); this->_count = arr->count(); memcpy(this->_address, arr->address(), this->_count); } @@ -226,7 +226,7 @@ class Array : public Vector { * copies content of arr to the end of ours */ void append(const Array & arr) { - this->_address = this->reallocate(*this, this->_count, this->_count + arr._count); + this->reallocate(*this, this->_count, this->_count + arr._count); for (int i = this->_count; i < this->_count + arr._count; i++) { this->_address[i] = arr._address[i - this->_count]; } @@ -237,7 +237,7 @@ class Array : public Vector { * Adds object at the end of the array */ int add(T obj) { - this->_address = this->reallocate(*this, this->_count, this->_count + 1); + this->reallocate(*this, this->_count, this->_count + 1); if (this->_address == NULL) { this->_count = 0; return -3; @@ -294,36 +294,36 @@ class Array : public Vector { * adjusts address memory to size */ void adjustMemorySize(S size) { - this->_address = this->reallocate(*this, this->_count, size); + this->reallocate(*this, this->_count, size); this->_count = size; } /** * Returns address of array */ - T * address() const { return this->_address; } + T * address() const { + if (this->_count == 0) return NULL; + return this->_address; + } private: /** * uses malloc to allocate mem */ - static T * allocate(Array & array, S size) { - if (size < array._capacity) { - return array._address; - } else { + static void allocate(Array & array, S size) { + if (size > array._capacity) { array._capacity = size; - return (T *) new T[size]; + array._address = (T *) new T[size]; } } /** * returns modified `addr` with `newsize` */ - //static T * reallocate(T * addr, S oldsize, S newsize) { - static T * reallocate(Array & array, S oldsize, S newsize) { + static void reallocate(Array & array, S oldsize, S newsize) { if (newsize < array._capacity) { - return array._address; + return; } S adjustNewSize = (((newsize / array._blockSize) + 1) * array._blockSize); @@ -335,8 +335,9 @@ class Array : public Vector { } delete[] array._address; + array._address = res; - return res; + return; } /** @@ -346,6 +347,7 @@ class Array : public Vector { //static void deallocate(T * value) { static void deallocate(Array & array) { delete[] array._address; + array._capacity = 0; //delete[] value; } @@ -354,7 +356,7 @@ class Array : public Vector { */ void saveArray(const T * array, S size) { this->removeAll(); - this->_address = (T *) this->allocate(*this, size); + this->allocate(*this, size); this->_count = size; if (this->_address) { @@ -373,7 +375,7 @@ class Array : public Vector { typename std::initializer_list::iterator itr; this->_count = list.size(); - this->_address = (T *) this->allocate(*this, this->_count); + this->allocate(*this, this->_count); if (this->_address) { S i = 0; diff --git a/bflibcpp/testbench/data_tests.hpp b/bflibcpp/testbench/data_tests.hpp index a8bbcb3f..2b202779 100644 --- a/bflibcpp/testbench/data_tests.hpp +++ b/bflibcpp/testbench/data_tests.hpp @@ -20,7 +20,7 @@ using namespace BF; BFTEST_UNIT_FUNC(test_datainit, 1, { Data buf0; BF_ASSERT(buf0.size() == 0); - BF_ASSERT(buf0.buffer() != NULL); + BF_ASSERT(buf0.buffer() == NULL); unsigned char bytes[10]; Data buf1(sizeof(bytes), bytes); @@ -36,7 +36,7 @@ BFTEST_UNIT_FUNC(test_datainit, 1, { Data * buf5 = new Data; BF_ASSERT(buf5->size() == 0); - BF_ASSERT(buf5->buffer() != NULL); + BF_ASSERT(buf5->buffer() == NULL); BFRelease(buf5); }) diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 08919eb1..77ef3eac 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,16 +22,13 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ - /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); BFTEST_SUITE_LAUNCH(queue_tests); BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); - */ BFTEST_SUITE_LAUNCH(array_tests); - /* BFTEST_SUITE_LAUNCH(vector_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(object_tests); @@ -41,6 +38,5 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); - */ }) From 5c7ee90677cbee087f187f06bcdb9c0c3fb7d051 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 14:41:55 -0700 Subject: [PATCH 12/20] using mem tools --- bflibcpp/src/array.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index 7565d0ff..8022bb43 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -17,6 +17,10 @@ #include "exception.hpp" #include "swap.hpp" +extern "C" { +#include +} + namespace BF { /** @@ -329,10 +333,14 @@ class Array : public Vector { S adjustNewSize = (((newsize / array._blockSize) + 1) * array._blockSize); array._capacity = adjustNewSize; T * res = new T[adjustNewSize]; + memcpy(res, array._address, sizeof(T) * oldsize); + memset(array._address, 0, sizeof(T) * oldsize); + /* for (S i = 0; i < oldsize && i < adjustNewSize; i++) { res[i] = std::move(array._address[i]); array._address[i] = 0; } + */ delete[] array._address; array._address = res; From 9cc84f3084c70b8f5500cece6b92cdcad0b1b37d Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 14:47:51 -0700 Subject: [PATCH 13/20] update makefile --- bfnet/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bfnet/makefile b/bfnet/makefile index 3f17673a..6969ef90 100644 --- a/bfnet/makefile +++ b/bfnet/makefile @@ -47,9 +47,9 @@ MAIN_FILE = testbench/tests.cpp endif # ($(CONFIG),...) dependencies: + cd ../bftest/ && make clean && make build CONFIG=release && make build CONFIG=debug cd ../bflibc/ && make clean && make build CONFIG=release && make build CONFIG=debug cd ../bflibcpp/ && make clean && make build CONFIG=release && make build CONFIG=debug - cd ../bftest/ && make clean && make build CONFIG=release && make build CONFIG=debug include makefiles/lib.mk LIBS_MAKEFILES_PATH:=$(shell dirname $(CURDIR))/makefiles From 5605af13945824a0971fbdb3ea245d6f30c45f23 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 16:16:12 -0700 Subject: [PATCH 14/20] update todo --- bflibcpp/src/array.hpp | 21 ++++----------------- bflibcpp/todo.md | 6 +++--- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index 8022bb43..c4c31990 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -306,15 +306,13 @@ class Array : public Vector { * Returns address of array */ T * address() const { + // capacity may be > 0 and _address may be allocated if (this->_count == 0) return NULL; return this->_address; } private: - /** - * uses malloc to allocate mem - */ static void allocate(Array & array, S size) { if (size > array._capacity) { array._capacity = size; @@ -322,25 +320,16 @@ class Array : public Vector { } } - /** - * returns modified `addr` with `newsize` - */ static void reallocate(Array & array, S oldsize, S newsize) { if (newsize < array._capacity) { return; } - S adjustNewSize = (((newsize / array._blockSize) + 1) * array._blockSize); - array._capacity = adjustNewSize; - T * res = new T[adjustNewSize]; + S adjustedNewSize = (((newsize / array._blockSize) + 1) * array._blockSize); + array._capacity = adjustedNewSize; + T * res = new T[adjustedNewSize]; memcpy(res, array._address, sizeof(T) * oldsize); memset(array._address, 0, sizeof(T) * oldsize); - /* - for (S i = 0; i < oldsize && i < adjustNewSize; i++) { - res[i] = std::move(array._address[i]); - array._address[i] = 0; - } - */ delete[] array._address; array._address = res; @@ -352,11 +341,9 @@ class Array : public Vector { * Derived must make sure this follows the standard established * by allocate() */ - //static void deallocate(T * value) { static void deallocate(Array & array) { delete[] array._address; array._capacity = 0; - //delete[] value; } /** diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 086c8611..17e88fb1 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -17,9 +17,9 @@ - [ ] String to integer - [ ] array memory optimization, improving virtual memory usage - [x] BF::Array to use new/delete for memory allocation - - [ ] optimize String - - [ ] when appending to string, grab more memory so we don't have keep calling realloc all the time - - [ ] benchmark test run + - [x] optimize String + - [x] when appending to string, grab more memory so we don't have keep calling realloc all the time + - [x] benchmark test run - [x] collection sorting algorithm - [x] make sure there are no Delete() calls and must use BFRelease instead - [x] throw compiler warnings when using delete From a4da6be4ff32b7624ef5d928f62ee970ae5dbfa2 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 21:13:56 -0700 Subject: [PATCH 15/20] can build --- bflibcpp/src/list.hpp | 2 +- bflibcpp/src/sort.hpp | 124 +++++++++++++++------------- bflibcpp/testbench/vector_tests.hpp | 2 +- 3 files changed, 70 insertions(+), 58 deletions(-) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 541d5f78..31ebcfbc 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -15,7 +15,7 @@ namespace BF { -template