diff --git a/modules/common/chowdsp_data_structures/Allocators/chowdsp_ArenaAllocator.h b/modules/common/chowdsp_data_structures/Allocators/chowdsp_ArenaAllocator.h index 43ec713a2..52f5dbfd8 100644 --- a/modules/common/chowdsp_data_structures/Allocators/chowdsp_ArenaAllocator.h +++ b/modules/common/chowdsp_data_structures/Allocators/chowdsp_ArenaAllocator.h @@ -5,6 +5,7 @@ namespace chowdsp { /** A simple arena allocator */ +template > class ArenaAllocator { public: @@ -100,7 +101,7 @@ class ArenaAllocator } private: - std::vector raw_data {}; + MemoryResourceType raw_data {}; size_t bytes_used = 0; }; } // namespace chowdsp diff --git a/modules/common/chowdsp_data_structures/Allocators/chowdsp_ChainedArenaAllocator.h b/modules/common/chowdsp_data_structures/Allocators/chowdsp_ChainedArenaAllocator.h index c096a75c8..958a1980e 100644 --- a/modules/common/chowdsp_data_structures/Allocators/chowdsp_ChainedArenaAllocator.h +++ b/modules/common/chowdsp_data_structures/Allocators/chowdsp_ChainedArenaAllocator.h @@ -78,7 +78,7 @@ class ChainedArenaAllocator } /** Returns the arena currently being used */ - ArenaAllocator& get_current_arena() + ArenaAllocator<>& get_current_arena() { jassert (current_arena != arenas.end()); return *current_arena; @@ -111,8 +111,8 @@ class ChainedArenaAllocator } ChainedArenaAllocator& alloc; - const std::forward_list::iterator arena_at_start; - ArenaAllocator::Frame arena_frame; + const std::forward_list>::iterator arena_at_start; + ArenaAllocator<>::Frame arena_frame; }; /** Creates a frame for this allocator */ @@ -139,8 +139,8 @@ class ChainedArenaAllocator get_current_arena().clear(); } - std::forward_list arenas {}; - std::forward_list::iterator current_arena {}; + std::forward_list> arenas {}; + std::forward_list>::iterator current_arena {}; size_t arena_size_bytes = 0; size_t arena_count = 0; }; diff --git a/modules/dsp/chowdsp_compressor/Compressor/chowdsp_CompressorGainComputer.h b/modules/dsp/chowdsp_compressor/Compressor/chowdsp_CompressorGainComputer.h index ef8314273..35010b5a8 100644 --- a/modules/dsp/chowdsp_compressor/Compressor/chowdsp_CompressorGainComputer.h +++ b/modules/dsp/chowdsp_compressor/Compressor/chowdsp_CompressorGainComputer.h @@ -84,7 +84,7 @@ class GainComputer */ void processBlock (const BufferView& levelBuffer, const BufferView& gainBuffer, - ArenaAllocator* arena = nullptr) noexcept + ArenaAllocator<>* arena = nullptr) noexcept { jassert (levelBuffer.getNumSamples() == gainBuffer.getNumSamples()); diff --git a/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.cpp b/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.cpp index c49be72a6..d1400068f 100644 --- a/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.cpp +++ b/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.cpp @@ -85,7 +85,7 @@ void SmoothedBufferValue::setRampLength (double } template -void SmoothedBufferValue::process (int numSamples, ArenaAllocator& alloc) +void SmoothedBufferValue::process (int numSamples, ArenaAllocator<>& alloc) { bufferData = alloc.allocate (numSamples, bufferAlignment); jassert (bufferData != nullptr); // arena allocator is out of memory! @@ -114,7 +114,7 @@ void SmoothedBufferValue::process (int numSample } template -void SmoothedBufferValue::process (FloatType value, int numSamples, ArenaAllocator& alloc) +void SmoothedBufferValue::process (FloatType value, int numSamples, ArenaAllocator<>& alloc) { bufferData = alloc.allocate (numSamples, bufferAlignment); jassert (bufferData != nullptr); // arena allocator is out of memory! diff --git a/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.h b/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.h index b22d8e0c6..ad4894591 100644 --- a/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.h +++ b/modules/dsp/chowdsp_dsp_data_structures/Other/chowdsp_SmoothedBufferValue.h @@ -78,14 +78,14 @@ class SmoothedBufferValue * Please don't call this function if the parameter handle hasn't been set! */ void process (int numSamples); - void process (int numSamples, ArenaAllocator& alloc); + void process (int numSamples, ArenaAllocator<>& alloc); /** * Process smoothing for the input value. * If smoothing an audio parameter, it is recommended to use a parameter handle instead! */ void process (FloatType value, int numSamples); - void process (FloatType value, int numSamples, ArenaAllocator& alloc); + void process (FloatType value, int numSamples, ArenaAllocator<>& alloc); /** Returns a pointer to the current smoothed buffer. */ [[nodiscard]] const FloatType* getSmoothedBuffer() const { return bufferData; } diff --git a/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp index 97d6ed048..f131cec56 100644 --- a/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp +++ b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp @@ -3,42 +3,92 @@ TEST_CASE ("Arena Allocator Test", "[common][data-structures]") { - chowdsp::ArenaAllocator allocator { 150 }; - - // allocate doubles + SECTION ("With std::vector") { - auto* some_doubles = allocator.allocate (10); - REQUIRE ((void*) some_doubles == allocator.data (0)); - REQUIRE (allocator.get_bytes_used() == 80); - } + chowdsp::ArenaAllocator allocator { 150 }; + + // allocate doubles + { + auto* some_doubles = allocator.allocate (10); + REQUIRE ((void*) some_doubles == allocator.data (0)); + REQUIRE (allocator.get_bytes_used() == 80); + } + + // allocate ints + { + auto* some_ints = allocator.allocate (10); + REQUIRE ((void*) some_ints == allocator.data (80)); + REQUIRE (allocator.get_bytes_used() == 120); + } + + // allocate with stack frame + { + const auto frame = allocator.create_frame(); + auto* some_chars = allocator.allocate (30); + juce::ignoreUnused (some_chars); + REQUIRE (allocator.get_bytes_used() == 150); + } - // allocate ints - { - auto* some_ints = allocator.allocate (10); - REQUIRE ((void*) some_ints == allocator.data (80)); REQUIRE (allocator.get_bytes_used() == 120); + + // aligned allocation + { + auto* some_data = allocator.allocate (1, 16); + REQUIRE (juce::snapPointerToAlignment (some_data, (size_t) 16) == some_data); + } + + // overfull allocation + REQUIRE (allocator.allocate (100) == nullptr); + + // clear allocator + allocator.clear(); + REQUIRE (allocator.get_bytes_used() == 0); } - // allocate with stack frame + SECTION ("With std::array") { - const auto frame = allocator.create_frame(); - auto* some_chars = allocator.allocate (30); - juce::ignoreUnused (some_chars); - REQUIRE (allocator.get_bytes_used() == 150); - } + struct ArrayMemoryResource : std::array + { + void resize (size_t N, std::byte value = {}) { juce::ignoreUnused (N, value); } // NOLINT + }; - REQUIRE (allocator.get_bytes_used() == 120); + chowdsp::ArenaAllocator allocator { 150 }; - // aligned allocation - { - auto* some_data = allocator.allocate (1, 16); - REQUIRE (juce::snapPointerToAlignment (some_data, (size_t) 16) == some_data); - } + // allocate doubles + { + auto* some_doubles = allocator.allocate (10); + REQUIRE ((void*) some_doubles == allocator.data (0)); + REQUIRE (allocator.get_bytes_used() == 80); + } - // overfull allocation - REQUIRE (allocator.allocate (100) == nullptr); + // allocate ints + { + auto* some_ints = allocator.allocate (10); + REQUIRE ((void*) some_ints == allocator.data (80)); + REQUIRE (allocator.get_bytes_used() == 120); + } - // clear allocator - allocator.clear(); - REQUIRE (allocator.get_bytes_used() == 0); + // allocate with stack frame + { + const auto frame = allocator.create_frame(); + auto* some_chars = allocator.allocate (30); + juce::ignoreUnused (some_chars); + REQUIRE (allocator.get_bytes_used() == 150); + } + + REQUIRE (allocator.get_bytes_used() == 120); + + // aligned allocation + { + auto* some_data = allocator.allocate (1, 16); + REQUIRE (juce::snapPointerToAlignment (some_data, (size_t) 16) == some_data); + } + + // overfull allocation + REQUIRE (allocator.allocate (100) == nullptr); + + // clear allocator + allocator.clear(); + REQUIRE (allocator.get_bytes_used() == 0); + } }