Anklang 0.3.0-460-gc4ef46ba
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#ifndef __ASE_LOFT_HH__
3#define __ASE_LOFT_HH__
4
5#include <ase/cxxaux.hh>
6
7namespace Ase {
8
10namespace Loft {
11
13enum Flags : size_t {
14 PREFAULT_PAGES = 1,
15};
16
17} // Loft
18
20struct LoftFree {
21 size_t size = 0;
22 void (*dtor_) (void*) = nullptr;
23 void operator() (void*);
24};
25
27template<typename T>
29
31LoftPtr<void> loft_alloc (size_t size, size_t align = 0);
32
34LoftPtr<void> loft_calloc (size_t nelem, size_t elemsize, size_t align = 0);
35
37template<class T, class ...Args>
38LoftPtr<T> loft_make_unique (Args &&...args);
39
41size_t loft_bucket_size (size_t nbytes);
42
44struct LoftConfig {
45 size_t preallocate = 0;
46 size_t watermark = 0;
47 Loft::Flags flags = Loft::Flags (0);
48};
49
51void loft_set_config (const LoftConfig &config);
52
54void loft_get_config (LoftConfig &config);
55
57void loft_set_notifier (const std::function<void()> &lowmem);
58
60size_t loft_grow_preallocate (size_t preallocation_amount = 0);
61
62
64struct LoftStats {
65 std::vector<std::pair<size_t,size_t>> buckets; // size,count
66 size_t narenas = 0;
67 size_t available = 0;
68 size_t allocated = 0;
69 size_t maxchunk = 0;
70};
71
73void loft_get_stats (LoftStats &stats);
74
76String loft_stats_string (const LoftStats &stats);
77
78namespace Loft {
79
82protected:
83 static void* loft_btalloc (size_t size, size_t align);
84 static void loft_btfree (void *p, size_t size);
85};
86
88template<typename T>
90public:
91 using value_type = T;
92 Allocator () noexcept = default;
93 template<typename U> Allocator (const Allocator<U>& other) noexcept {}
94 bool operator== (const Allocator<T> &o) const noexcept { return true; }
95 bool operator!= (const Allocator<T> &o) const noexcept { return !this->operator== (o); }
96 constexpr void deallocate (T *p, size_t n) const noexcept { loft_btfree (p, n); }
97 [[nodiscard]] constexpr T*
98 allocate (std::size_t n) const
99 {
100 void *const mem = loft_btalloc (n * sizeof (T), alignof (T));
101 if (!mem)
102 throw std::bad_alloc();
103 return reinterpret_cast<T*> (mem);
104 }
105 static constexpr bool allows_read_after_free = true;
106};
107
108} // Loft
109
110// == implementations ==
111template<class T, class ...Args> inline LoftPtr<T>
112loft_make_unique (Args &&...args)
113{
114 LoftPtr<void> vp = loft_alloc (sizeof (T), alignof (T));
115 if (!vp) return {};
116 T *const t = new (vp.get()) T (std::forward<Args> (args)...);
117 LoftFree lfree = vp.get_deleter();
118 void *vptr = vp.release();
119 ASE_ASSERT_WARN (t == vptr); // require T* == void*
120 lfree.dtor_ = [] (void *p) {
121 T *const t = static_cast<T*> (p);
122 t->~T();
123 };
124 // dprintf (2, "--LOFT: %s: ctor: %s: size=%zd ptr=%p dtor_=%p\n", __func__, typeid_name<T>().c_str(), lfree.size, t, lfree.dtor_);
125 return std::unique_ptr<T,LoftFree> (t, lfree);
126}
127
128} // Ase
129
130#endif /* __ASE_LOFT_HH__ */
Allocator for STL containers.
Definition loft.hh:89
#define ASE_ASSERT_WARN(expr)
Issue an assertion warning if expr evaluates to false.
Definition cxxaux.hh:94
T get_deleter(T... args)
T get(T... args)
Flags
Flags for allocator behavior.
Definition loft.hh:13
The Anklang C++ API namespace.
Definition api.hh:9
size_t maxchunk
Memory preallocated in Arenas.
Definition loft.hh:69
size_t available
Total number of arenas.
Definition loft.hh:67
size_t allocated
Memory still allocatable from Arenas.
Definition loft.hh:68
LoftPtr< T > loft_make_unique(Args &&...args)
Construct an object T from Loft memory, wrapped in a unique_ptr.
Definition loft.hh:112
size_t preallocate
Amount of preallocated available memory.
Definition loft.hh:45
size_t watermark
Watermark to trigger async preallocation.
Definition loft.hh:46
Configuration for Loft allocations.
Definition loft.hh:44
Statistics for Loft allocations.
Definition loft.hh:64
T release(T... args)
A std::unique_ptr<> deleter for Loft allocations.
Definition loft.hh:20
Internal Helper.
Definition loft.hh:81
static void loft_btfree(void *p, size_t size)
Free memory allocted from loft_btalloc().
Definition loft.cc:488
static void * loft_btalloc(size_t size, size_t align)
Alloc in a fashion similar to malloc(), using boundary tags.
Definition loft.cc:460