diff --git a/modules/common/chowdsp_data_structures/Structures/chowdsp_StackAllocator.h b/modules/common/chowdsp_data_structures/Structures/chowdsp_ArenaAllocator.h similarity index 76% rename from modules/common/chowdsp_data_structures/Structures/chowdsp_StackAllocator.h rename to modules/common/chowdsp_data_structures/Structures/chowdsp_ArenaAllocator.h index dace2307a..bc68138d5 100644 --- a/modules/common/chowdsp_data_structures/Structures/chowdsp_StackAllocator.h +++ b/modules/common/chowdsp_data_structures/Structures/chowdsp_ArenaAllocator.h @@ -4,17 +4,23 @@ namespace chowdsp { -/** A simple stack allocator */ -class StackAllocator +/** A simple arena allocator */ +class ArenaAllocator { public: - StackAllocator() = default; + ArenaAllocator() = default; - StackAllocator (const StackAllocator&) = delete; - StackAllocator& operator= (const StackAllocator&) = delete; + /** Constructs the arena with an initial allocated size. */ + explicit ArenaAllocator (size_t size_in_bytes) + { + reset (size_in_bytes); + } + + ArenaAllocator (const ArenaAllocator&) = delete; + ArenaAllocator& operator= (const ArenaAllocator&) = delete; - StackAllocator (StackAllocator&&) noexcept = default; - StackAllocator& operator= (StackAllocator&&) noexcept = default; + ArenaAllocator (ArenaAllocator&&) noexcept = default; + ArenaAllocator& operator= (ArenaAllocator&&) noexcept = default; /** Re-allocates the internal buffer with a given number of bytes */ void reset (size_t new_size_bytes) @@ -64,20 +70,20 @@ class StackAllocator * Once the frame goes out of scope, the allocator will be reset * to whatever it's state was at the beginning of the frame. */ - struct StackAllocatorFrame + struct ArenaAllocatorFrame { - explicit StackAllocatorFrame (StackAllocator& allocator) + explicit ArenaAllocatorFrame (ArenaAllocator& allocator) : alloc (allocator), bytes_used_at_start (alloc.bytes_used) { } - ~StackAllocatorFrame() + ~ArenaAllocatorFrame() { alloc.bytes_used = bytes_used_at_start; } - StackAllocator& alloc; + ArenaAllocator& alloc; const size_t bytes_used_at_start; }; diff --git a/modules/common/chowdsp_data_structures/chowdsp_data_structures.h b/modules/common/chowdsp_data_structures/chowdsp_data_structures.h index 4dfb76223..19eb89a1e 100644 --- a/modules/common/chowdsp_data_structures/chowdsp_data_structures.h +++ b/modules/common/chowdsp_data_structures/chowdsp_data_structures.h @@ -29,7 +29,7 @@ BEGIN_JUCE_MODULE_DECLARATION #include "Structures/chowdsp_OptionalPointer.h" #include "Structures/chowdsp_RawObject.h" #include "Structures/chowdsp_SmallVector.h" -#include "Structures/chowdsp_StackAllocator.h" +#include "Structures/chowdsp_ArenaAllocator.h" #include "Structures/chowdsp_StringLiteral.h" #include "Helpers/chowdsp_ArrayHelpers.h" diff --git a/tests/common_tests/chowdsp_data_structures_test/StackAllocatorTest.cpp b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp similarity index 85% rename from tests/common_tests/chowdsp_data_structures_test/StackAllocatorTest.cpp rename to tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp index bc80f1567..e848c1cb1 100644 --- a/tests/common_tests/chowdsp_data_structures_test/StackAllocatorTest.cpp +++ b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp @@ -1,10 +1,9 @@ #include #include -TEST_CASE ("Stack Allocator Test", "[common][data-structures]") +TEST_CASE ("Arena Allocator Test", "[common][data-structures]") { - chowdsp::StackAllocator allocator; - allocator.reset (150); + chowdsp::ArenaAllocator allocator { 150 }; // allocate doubles { @@ -22,7 +21,7 @@ TEST_CASE ("Stack Allocator Test", "[common][data-structures]") // allocate with stack frame { - chowdsp::StackAllocator::StackAllocatorFrame frame { allocator }; + chowdsp::ArenaAllocator::ArenaAllocatorFrame frame { allocator }; auto* some_chars = allocator.allocate (30); juce::ignoreUnused (some_chars); REQUIRE (allocator.get_bytes_used() == 150); diff --git a/tests/common_tests/chowdsp_data_structures_test/CMakeLists.txt b/tests/common_tests/chowdsp_data_structures_test/CMakeLists.txt index c0f77cb53..e16ac00a9 100644 --- a/tests/common_tests/chowdsp_data_structures_test/CMakeLists.txt +++ b/tests/common_tests/chowdsp_data_structures_test/CMakeLists.txt @@ -10,7 +10,7 @@ target_sources(chowdsp_data_structures_test LocalPointerTest.cpp OptionalPointerTest.cpp SmallVectorTest.cpp - StackAllocatorTest.cpp + ArenaAllocatorTest.cpp StringLiteralTest.cpp TupleHelpersTest.cpp VectorHelpersTest.cpp