diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-26 00:47:54 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-26 00:48:06 +0200 |
commit | a1db1cd9e22e77041d5f1360f1d1ccdc52b86306 (patch) | |
tree | c1cc6dc9c17885a0789028f7a55c7126f33beee7 /asl/tests | |
parent | 54b95b16629f0cd4bc30e6899e00019b3ab94012 (diff) |
Implement chunked_buffer
Diffstat (limited to 'asl/tests')
-rw-r--r-- | asl/tests/BUILD.bazel | 2 | ||||
-rw-r--r-- | asl/tests/counting_allocator.hpp | 48 |
2 files changed, 50 insertions, 0 deletions
diff --git a/asl/tests/BUILD.bazel b/asl/tests/BUILD.bazel index 016f037..2970377 100644 --- a/asl/tests/BUILD.bazel +++ b/asl/tests/BUILD.bazel @@ -9,10 +9,12 @@ package( cc_library( name = "utils", hdrs = [ + "counting_allocator.hpp", "types.hpp", ], deps = [ "//asl/base", + "//asl/memory:allocator", ], visibility = ["//asl:__subpackages__"], ) 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>); |