Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
loft.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#pragma once
3
4#include <ase/cxxaux.hh>
5
6namespace Ase {
7
9namespace Loft {
10
12enum Flags : size_t {
13 PREFAULT_PAGES = 1,
14};
15
16} // Loft
17
19struct LoftFree {
20 size_t size = 0;
21 void (*dtor_) (void*) = nullptr;
22 void operator() (void*);
23};
24
26template<typename T>
28
30LoftPtr<void> loft_alloc (size_t size, size_t align = 0);
31
33LoftPtr<void> loft_calloc (size_t nelem, size_t elemsize, size_t align = 0);
34
36template<class T, class ...Args>
37LoftPtr<T> loft_make_unique (Args &&...args);
38
40size_t loft_bucket_size (size_t nbytes);
41
43struct LoftConfig {
44 size_t preallocate = 0;
45 size_t watermark = 0;
46 Loft::Flags flags = Loft::Flags (0);
47};
48
50void loft_set_config (const LoftConfig &config);
51
53void loft_get_config (LoftConfig &config);
54
56void loft_set_notifier (const std::function<void()> &lowmem);
57
59void loft_set_growth_notifier (const std::function<void(size_t total,size_t needed)> &willgrow);
60
62size_t loft_grow_preallocate (size_t preallocation_amount = 0);
63
64
66struct LoftStats {
67 std::vector<std::pair<size_t,size_t>> buckets; // size,count
68 size_t narenas = 0;
69 size_t available = 0;
70 size_t allocated = 0;
71 size_t maxchunk = 0;
72};
73
75void loft_get_stats (LoftStats &stats);
76
78String loft_stats_string (const LoftStats &stats);
79
80namespace Loft {
81
84protected:
85 static void* loft_btalloc (size_t size, size_t align);
86 static void loft_btfree (void *p, size_t size);
87};
88
90template<typename T>
92public:
93 using value_type = T;
94 Allocator () noexcept = default;
95 template<typename U> Allocator (const Allocator<U>& other) noexcept {}
96 bool operator== (const Allocator<T> &o) const noexcept { return true; }
97 bool operator!= (const Allocator<T> &o) const noexcept { return !this->operator== (o); }
98 constexpr void deallocate (T *p, size_t n) const noexcept { loft_btfree (p, n); }
99 [[nodiscard]] constexpr T*
100 allocate (std::size_t n) const
101 {
102 void *const mem = loft_btalloc (n * sizeof (T), alignof (T));
103 if (!mem)
104 throw std::bad_alloc();
105 return reinterpret_cast<T*> (mem);
106 }
107 static constexpr bool allows_read_after_free = true;
108};
109
110} // Loft
111
112// == implementations ==
113template<class T, class ...Args> inline LoftPtr<T>
114loft_make_unique (Args &&...args)
115{
116 LoftPtr<void> vp = loft_alloc (sizeof (T), alignof (T));
117 if (!vp) return {};
118 T *const t = new (vp.get()) T (std::forward<Args> (args)...);
119 LoftFree lfree = vp.get_deleter();
120 void *vptr = vp.release();
121 ASE_ASSERT (t == vptr); // require T* == void*
122 lfree.dtor_ = [] (void *p) {
123 T *const t = static_cast<T*> (p);
124 t->~T();
125 };
126 // dprintf (2, "--LOFT: %s: ctor: %s: size=%zd ptr=%p dtor_=%p\n", __func__, typeid_name<T>().c_str(), lfree.size, t, lfree.dtor_);
127 return std::unique_ptr<T,LoftFree> (t, lfree);
128}
129
130} // Ase
131
Allocator for STL containers.
Definition loft.hh:91
#define ASE_ASSERT(expr)
Issue an assertion warning if expr evaluates to false.
Definition cxxaux.hh:90
T get_deleter(T... args)
T get(T... args)
Flags
Flags for allocator behavior.
Definition loft.hh:12
The Anklang C++ API namespace.
Definition api.hh:8
size_t maxchunk
Memory preallocated in Arenas.
Definition loft.hh:71
size_t available
Total number of arenas.
Definition loft.hh:69
size_t allocated
Memory still allocatable from Arenas.
Definition loft.hh:70
LoftPtr< T > loft_make_unique(Args &&...args)
Construct an object T from Loft memory, wrapped in a unique_ptr.
Definition loft.hh:114
size_t preallocate
Amount of preallocated available memory.
Definition loft.hh:44
size_t watermark
Watermark to trigger async preallocation.
Definition loft.hh:45
Configuration for Loft allocations.
Definition loft.hh:43
Statistics for Loft allocations.
Definition loft.hh:66
T release(T... args)
A std::unique_ptr<> deleter for Loft allocations.
Definition loft.hh:19
Internal Helper.
Definition loft.hh:83
static void loft_btfree(void *p, size_t size)
Free memory allocted from loft_btalloc().
Definition loft.cc:492
static void * loft_btalloc(size_t size, size_t align)
Alloc in a fashion similar to malloc(), using boundary tags.
Definition loft.cc:464