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

« « « Anklang Documentation
Loading...
Searching...
No Matches
memory.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/utils.hh>
5
6namespace Ase {
7
9namespace FastMemory {
10
12inline constexpr size_t cache_line_size = 64;
13
14} // FastMemory
15
16// Allocate cache-line aligned memory block from fast memory pool, MT-Safe.
17void* fast_mem_alloc (size_t size);
18// Free a memory block allocated with aligned_malloc(), MT-Safe.
19void fast_mem_free (void *mem);
20
22template<typename T, size_t ALIGNMENT = FastMemory::cache_line_size>
25 static_assert (ALIGNMENT <= FastMemory::cache_line_size);
26 static_assert ((ALIGNMENT & (ALIGNMENT - 1)) == 0);
27 static_assert (alignof (T) <= ALIGNMENT);
28 const size_t n_elements_ = 0;
29 T *const data_ = nullptr;
31protected:
32 void range_check (size_t n) const
33 {
34 if (n >= n_elements_)
35 throw std::out_of_range (string_format ("FastMemArray::range_check: n >= size(): %u >= %u", n, size()));
36 }
37public:
38 FastMemArray (size_t n_elements) :
39 n_elements_ (n_elements),
40 data_ ((T*) fast_mem_alloc (sizeof (T) * n_elements_))
41 {
42 std::fill (begin(), end(), T());
43 }
44 FastMemArray (const std::vector<T>& elements) :
45 n_elements_ (elements.size()),
46 data_ ((T*) fast_mem_alloc (sizeof (T) * n_elements_))
47 {
48 std::copy (elements.begin(), elements.end(), begin());
49 }
51 {
53 fast_mem_free (data_);
54 }
55 T* begin () { return &data_[0]; }
56 T* end () { return &data_[n_elements_]; }
57 size_t size () const { return n_elements_; }
58 T& operator[] (size_t n) { return data_[n]; }
59 const T& operator[] (size_t n) const { return data_[n]; }
60 T& at (size_t n) { range_check (n); return data_[n]; }
61 const T& at (size_t n) const { range_check (n); return data_[n]; }
62};
63
64namespace FastMemory {
65
66// == NewDeleteBase ==
68 static constexpr const std::align_val_t __cxxalignment = std::align_val_t (__STDCPP_DEFAULT_NEW_ALIGNMENT__);
69 static void delete_ (void *ptr, std::size_t sz, std::align_val_t al);
70 static void* new_ (std::size_t sz, std::align_val_t al);
71public:
72 // 'static' is implicit for operator new/delete
73 void* operator new (std::size_t sz) { return new_ (sz, __cxxalignment); }
74 void* operator new[] (std::size_t sz) { return new_ (sz, __cxxalignment); }
75 void* operator new (std::size_t sz, std::align_val_t al) { return new_ (sz, al); }
76 void* operator new[] (std::size_t sz, std::align_val_t al) { return new_ (sz, al); }
77 // variants without size_t MUST NOT be defined for the following to be used
78 void operator delete (void *ptr, std::size_t sz) { delete_ (ptr, sz, __cxxalignment); }
79 void operator delete[] (void *ptr, std::size_t sz) { delete_ (ptr, sz, __cxxalignment); }
80 void operator delete (void *ptr, std::size_t sz, std::align_val_t al) { delete_ (ptr, sz, al); }
81 void operator delete[] (void *ptr, std::size_t sz, std::align_val_t al) { delete_ (ptr, sz, al); }
82};
83
85struct Allocator;
87
89struct Block {
90 void *const block_start = nullptr;
91 const uint32 block_length = 0;
92 Block& operator= (const Block &src) { this->~Block(); new (this) Block (src); return *this; }
93 /*ctor*/ Block (void *b, uint32 l) : block_start (b), block_length (l) {}
94 /*copy*/ Block (const Block &src) = default;
95 /*dflt*/ Block () = default;
96};
97
99struct Arena {
101 explicit Arena (uint32 mem_size, uint32 alignment = cache_line_size);
103 size_t alignment () const;
105 uint64 location () const;
107 uint64 reserved () const;
109 Block allocate (uint32 length) const;
110 Block allocate (uint32 length, std::nothrow_t) const;
112 void release (Block allocatedblock) const;
113protected:
114 explicit Arena (AllocatorP);
116};
117
119struct HugePage {
122 size_t alignment () const { return start_ ? size_t (1) << __builtin_ctz (size_t (start_)) : 0; }
123 size_t size () const { return size_; }
124 char* mem () const { return (char*) start_; }
125 static HugePageP allocate (size_t minimum_alignment, size_t bytelength);
126protected:
127 void *const start_;
128 const size_t size_;
129 explicit HugePage (void *m, size_t s);
130 virtual ~HugePage () {}
131};
132using HugePageP = HugePage::HugePageP;
133
134} // FastMemory
135
137class CString final {
138 uint quark_ = 0;
139 constexpr void qset (uint quark) noexcept { quark_ = quark; }
140public:
141 using size_type = std::string::size_type;
142 using const_iterator = std::string::const_iterator;
143 using const_reference = std::string::const_reference;
144 using const_reverse_iterator = std::string::const_reverse_iterator;
145 /*dtor*/ ~CString () noexcept { qset (0); }
146 /*ctor*/ CString (const char *c) { assign (c); }
147 /*ctor*/ CString (const char *c, size_type s) { assign (c, s); }
148 /*ctor*/ CString (const std::string &s) { assign (s); }
149 /*ctor*/ constexpr CString () noexcept = default;
150 /*copy*/ constexpr CString (const CString &c) noexcept { assign (c); }
151 /*move*/ constexpr CString (CString &&c) noexcept { assign (c); }
152 constexpr CString& operator= (const CString &c) noexcept { return assign (c); }
153 constexpr CString& operator= (CString &&c) noexcept { return assign (c); }
154 CString& operator= (const std::string &s) noexcept { return assign (s); }
155 CString& operator= (const char *c) noexcept { return assign (c); }
156 CString& operator= (char ch) noexcept { return assign (std::addressof (ch), 1); }
157 CString& operator= (std::initializer_list<char> l) { return assign (l.begin(), l.size()); }
158 constexpr CString& assign (const CString &c) noexcept { qset (c.quark_); return *this; }
159 CString& assign (const std::string &s) noexcept;
160 CString& assign (const char *c, size_type s) { return assign (std::string (c, s)); }
161 CString& assign (const char *c) noexcept { return assign (std::string (c)); }
162 CString& assign (size_type count, char ch) noexcept { return this->operator= (std::string (count, ch)); }
163 const std::string& string () const;
164 const char* c_str () const noexcept { return string().c_str(); }
165 const char* data () const noexcept { return string().data(); }
166 const_reference at (size_type pos) const { return string().at (pos); }
167 const_reference operator[] (size_type pos) const { return string().operator[] (pos); }
168 size_type capacity () const noexcept { return string().capacity(); }
169 size_type length () const noexcept { return string().length(); }
170 bool empty () const noexcept { return string().empty(); }
171 size_type size () const noexcept { return string().size(); }
172 const_iterator begin () const noexcept { return string().begin(); }
173 const_iterator cbegin () const noexcept { return string().cbegin(); }
174 const_iterator end () const noexcept { return string().end(); }
175 const_iterator cend () const noexcept { return string().cend(); }
176 const_reverse_iterator rbegin () const noexcept { return string().rbegin(); }
177 const_reverse_iterator crbegin () const noexcept { return string().crbegin(); }
178 const_reverse_iterator rend () const noexcept { return string().rend(); }
179 const_reverse_iterator crend () const noexcept { return string().crend(); }
180 /*conv*/ operator std::string () const noexcept { return string(); }
181 friend bool operator== (CString a, CString b) { return a.quark_ == b.quark_; }
182 friend bool operator== (CString a, const std::string &b) { return a.string() == b; }
183 friend bool operator== (const std::string &a, CString b) { return a == b.string(); }
184 friend bool operator== (CString a, const char *b) { return a.string() == b; }
185 friend bool operator== (const char *a, CString b) { return a == b.string(); }
186 friend std::strong_ordering operator<=> (CString a, CString b) { return a.string() <=> b.string(); }
187 friend std::strong_ordering operator<=> (CString a, const String &b) { return a.string() <=> b; }
188 friend std::strong_ordering operator<=> (const String &a, CString b) { return a <=> b.string(); }
189 friend std::strong_ordering operator<=> (CString a, const char *b) { return a.string() <=> b; }
190 friend std::strong_ordering operator<=> (const char *a, CString b) { return a <=> b.string(); }
191 friend std::string operator+ (const std::string &s, CString c) { return s + c.string(); }
192 friend std::string operator+ (CString c, const std::string &s) { return c.string() + s; }
193 static CString lookup (const std::string &s);
194 static uint temp_quark_impl (CString c);
195 static CString temp_quark_impl (uint maybequark);
196 friend std::ostream& operator<< (std::ostream &os, CString c) { os << c.string(); return os; }
197 static constexpr const std::string::size_type npos = -1;
198};
200
201} // Ase
202
203namespace std {
204template<>
207 size_t
208 operator() (const ::Ase::CString &cs) const
209 {
210 return ::std::hash<::std::string>{} (cs.string());
211 }
212};
213} // std
214
T addressof(T... args)
T at(T... args)
T begin(T... args)
T c_str(T... args)
T capacity(T... args)
Compact, deduplicating string variant for constant strings.
Definition memory.hh:137
static CString lookup(const std::string &s)
Lookup a previously existing CString for a std::string s.
Definition memory.cc:631
const std::string & string() const
Convert CString into a std::string.
Definition memory.cc:640
Array with cache-line-alignment containing a fixed numer of PODs.
Definition memory.hh:23
T copy(T... args)
#define ASE_CLASS_NON_COPYABLE(ClassName)
Delete copy ctor and assignment operator.
Definition cxxaux.hh:110
T data(T... args)
T empty(T... args)
T end(T... args)
T fill(T... args)
constexpr size_t cache_line_size
Minimum alignment >= cache line size, see getconf LEVEL1_DCACHE_LINESIZE.
Definition memory.hh:12
The Anklang C++ API namespace.
Definition api.hh:8
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:24
uint32_t uint32
A 32-bit unsigned integer.
Definition cxxaux.hh:23
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:17
T operator()(T... args)
T rbegin(T... args)
T rend(T... args)
T length(T... args)
Memory area (over-)aligned to cache size and utilizing huge pages.
Definition memory.hh:99
uint64 location() const
Address of memory area.
Definition memory.cc:340
size_t alignment() const
Alignment for block addresses and length.
Definition memory.cc:352
uint64 reserved() const
Reserved memory area in bytes.
Definition memory.cc:346
Block allocate(uint32 length) const
Create a memory block from cache-line aligned memory area, MT-Unsafe.
Definition memory.cc:371
void release(Block allocatedblock) const
Realease a previously allocated block, MT-Unsafe.
Definition memory.cc:380
AllocatorP fma
Identifier for the associated memory allocator.
Definition memory.hh:115
Reference for an allocated memory block.
Definition memory.hh:89
Interface to the OS huge page allocator.
Definition memory.hh:119
char * mem() const
Allocated memroy area.
Definition memory.hh:124
size_t alignment() const
Alignment of the memory area.
Definition memory.hh:122
static HugePageP allocate(size_t minimum_alignment, size_t bytelength)
Try to allocate a HugePage >= bytelength with minimum_alignment, usual sizes are 2MB.
Definition memory.cc:52
size_t size() const
Size in bytes of the memroy area.
Definition memory.hh:123
typedef size_t