Clustering
C++20 header-only: DBSCAN, HDBSCAN, k-means.
Loading...
Searching...
No Matches
linear_alloc.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <new>
4#include <type_traits>
5#include <utility>
6
7namespace clustering {
8
9template <typename T> class LinearAllocator {
10 static_assert(std::is_trivially_destructible_v<T>, "T must be trivially destructible");
11
12public:
14 LinearAllocator(std::size_t count)
15 : size(count * sizeof(T)), memory(new char[count * sizeof(T)]), next(memory) {}
16
17 ~LinearAllocator() { delete[] memory; }
18
21
23 T *allocate() {
24 if (std::cmp_greater_equal(next - memory, size)) {
25 throw std::bad_alloc();
26 }
27
28 // Placement new modifies storage at `next` even though tidy can't see
29 // it through the pointer; suppress the false-positive const suggestion.
30 T *const result = new (next) T; // NOLINT(misc-const-correctness)
31 next += sizeof(T);
32 return result;
33 }
34
36 void deallocate(T * /*ptr*/) {
37 // Do nothing because T is trivially destructible
38 }
39
41 void reset() { next = memory; }
42
44 bool isDeallocSupported() { return false; }
45
46private:
47 std::size_t size;
48 char *memory;
49 char *next;
50};
51
52} // namespace clustering
bool isDeallocSupported()
Reports that per-element deallocate is not supported (false for this allocator).
LinearAllocator & operator=(const LinearAllocator &)=delete
T * allocate()
Bump-allocates one T; throws std::bad_alloc when the arena is exhausted.
LinearAllocator(std::size_t count)
Reserves room for count objects of T from a single backing allocation.
void reset()
Rewinds the bump pointer, reclaiming every outstanding allocation in one shot.
LinearAllocator(const LinearAllocator &)=delete
void deallocate(T *)
No-op: trivial destructibility lets per-element reclamation be skipped.