summaryrefslogtreecommitdiff
path: root/asl/tests/counting_allocator.hpp
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-05-26 00:47:54 +0200
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-05-26 00:48:06 +0200
commita1db1cd9e22e77041d5f1360f1d1ccdc52b86306 (patch)
treec1cc6dc9c17885a0789028f7a55c7126f33beee7 /asl/tests/counting_allocator.hpp
parent54b95b16629f0cd4bc30e6899e00019b3ab94012 (diff)
Implement chunked_buffer
Diffstat (limited to 'asl/tests/counting_allocator.hpp')
-rw-r--r--asl/tests/counting_allocator.hpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/asl/tests/counting_allocator.hpp b/asl/tests/counting_allocator.hpp
new file mode 100644
index 0000000..35dfcba
--- /dev/null
+++ b/asl/tests/counting_allocator.hpp
@@ -0,0 +1,48 @@
+// Copyright 2025 Steven Le Rouzic
+//
+// SPDX-License-Identifier: BSD-3-Clause
+
+#include "asl/memory/allocator.hpp"
+
+struct CountingAllocator
+{
+ struct Stats
+ {
+ isize_t alloc_count{};
+ isize_t realloc_count{};
+ isize_t dealloc_count{};
+ isize_t alive_bytes{};
+
+ [[nodiscard]] constexpr isize_t any_alloc_count() const
+ {
+ return alloc_count + realloc_count;
+ }
+ };
+
+ Stats* stats;
+
+ [[nodiscard]]
+ void* alloc(const asl::layout& layout) const
+ {
+ stats->alloc_count += 1;
+ stats->alive_bytes += layout.size;
+ return asl::GlobalHeap::alloc(layout);
+ }
+
+ void* realloc(void* ptr, const asl::layout& old, const asl::layout& new_layout) const
+ {
+ stats->realloc_count += 1;
+ stats->alive_bytes += new_layout.size - old.size;
+ return asl::GlobalHeap::realloc(ptr, old, new_layout);
+ }
+
+ void dealloc(void* ptr, const asl::layout& layout) const
+ {
+ stats->dealloc_count += 1;
+ stats->alive_bytes -= layout.size;
+ asl::GlobalHeap::dealloc(ptr, layout);
+ }
+
+ constexpr bool operator==(const CountingAllocator&) const { return true; }
+};
+static_assert(asl::allocator<CountingAllocator>);