/
mem.h
414 lines (342 loc) · 11.5 KB
/
mem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#pragma once
#include "def.h"
#include "god.h"
#include "atomic.h"
#include <assert.h>
#include <cstddef>
#include <stdlib.h>
#include <mutex>
#include <new>
#include <utility>
#include <type_traits>
#include <functional>
namespace co {
namespace xx {
struct __coapi Initializer {
Initializer();
~Initializer();
};
static Initializer g_initializer;
} // xx
constexpr size_t cache_line_size = L1_CACHE_LINE_SIZE;
// alloc @size bytes
__coapi void* alloc(size_t size);
// alloc @size bytes, @align byte aligned (align <= 1024)
__coapi void* alloc(size_t size, size_t align);
// alloc @size bytes, and zero-clear the memory
__coapi void* zalloc(size_t size);
// free the memory
// - @size: size of the memory
__coapi void free(void* p, size_t size);
// realloc the memory allocated by co::alloc() or co::realloc()
// - if p is NULL, it is equal to co::alloc(new_size)
// - @new_size must be greater than @old_size
__coapi void* realloc(void* p, size_t old_size, size_t new_size);
// Like realloc, but will not create a new allocation if there is not
// enough room to enlarge the memory allocation pointed to by p.
// The return value is p or NULL.
__coapi void* try_realloc(void* p, size_t old_size, size_t new_size);
__coapi char* strdup(const char* s);
// alloc memory and construct an object on it
// - T* p = co::make<T>(args)
template<typename T, typename... Args>
inline T* make(Args&&... args) {
return new (co::alloc(sizeof(T))) T(std::forward<Args>(args)...);
}
// delete the object created by co::make()
// - co::del((T*)p)
template<typename T>
inline void del(T* p, size_t n=sizeof(T)) {
if (p) { p->~T(); co::free((void*)p, n); }
}
// used internally by coost, do not call it
__coapi void* _salloc(size_t n);
__coapi void _dealloc(std::function<void()>&& f, int x);
// used internally by coost, do not call it
template<typename T, int N, typename... Args>
inline T* _smake(Args&&... args) {
static_assert(sizeof(T) <= 4096, "");
const auto p = _salloc(sizeof(T));
if (p) {
new(p) T(std::forward<Args>(args)...);
const bool x = god::is_trivially_destructible<T>();
if (!x) _dealloc([p](){ ((T*)p)->~T(); }, N);
}
return (T*)p;
}
// used internally by coost, do not call it
template<typename T, typename... Args>
inline T* _make_rootic(Args&&... args) {
return _smake<T, 0>(std::forward<Args>(args)...);
}
// used internally by coost, do not call it
template<typename T, typename... Args>
inline T* _make_static(Args&&... args) {
return _smake<T, 1>(std::forward<Args>(args)...);
}
// make non-dependent static object at the root level
template<typename T, typename... Args>
inline T* make_rootic(Args&&... args) {
return _smake<T, 2>(std::forward<Args>(args)...);
}
// make static object, which will be destructed automatically at exit
// - T* p = co::make_static<T>(args)
template<typename T, typename... Args>
inline T* make_static(Args&&... args) {
return _smake<T, 3>(std::forward<Args>(args)...);
}
// similar to std::unique_ptr
// - It is **not allowed** to create unique object from a nake pointer,
// use **make_unique** instead.
// - eg.
// auto s = co::make_unique<fastring>(32, 'x');
template<typename T>
class unique {
public:
constexpr unique() noexcept : _p(0) {}
constexpr unique(std::nullptr_t) noexcept : _p(0) {}
unique(unique& x) noexcept : _p(x._p) { x._p = 0; }
unique(unique&& x) noexcept : _p(x._p) { x._p = 0; }
~unique() { this->reset(); }
unique& operator=(unique&& x) {
if (&x != this) { this->reset(); _p = x._p; x._p = 0; }
return *this;
}
unique& operator=(unique& x) {
return this->operator=(std::move(x));
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
unique(unique<X>& x) noexcept : _p(x.get()) { *(void**)&x = 0; }
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
unique(unique<X>&& x) noexcept : _p(x.get()) { *(void**)&x = 0; }
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
unique& operator=(unique<X>&& x) {
if ((void*)&x != (void*)this) {
this->reset();
_p = x.get();
*(void**)&x = 0;
}
return *this;
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
unique& operator=(unique<X>& x) {
return this->operator=(std::move(x));
}
T* get() const noexcept { return _p; }
T* operator->() const { assert(_p); return _p; }
T& operator*() const { assert(_p); return *_p; }
bool operator==(T* p) const noexcept { return _p == p; }
bool operator!=(T* p) const noexcept { return _p != p; }
explicit operator bool() const noexcept { return _p != 0; }
void reset() {
if (_p) {
static_cast<void>(sizeof(T));
_p->~T();
co::free(_s, _s->n);
_p = 0;
}
}
void swap(unique& x) noexcept {
T* const p = _p;
_p = x._p;
x._p = p;
}
void swap(unique&& x) noexcept {
x.swap(*this);
}
private:
struct S { T o; size_t n; };
union { S* _s; T* _p; };
};
template<typename T, typename... Args>
inline unique<T> make_unique(Args&&... args) {
struct S { T o; size_t n; };
S* const s = (S*) co::alloc(sizeof(S));
if (s) {
new(s) T(std::forward<Args>(args)...);
s->n = sizeof(S);
}
unique<T> x;
*(void**)&x = s;
return x;
}
// similar to std::shared_ptr
// - It is **not allowed** to create shared object from a nake pointer,
// use **make_shared** instead.
// - eg.
// auto s = co::make_shared<fastring>(32, 'x');
template<typename T>
class shared {
public:
constexpr shared() noexcept : _p(0) {}
constexpr shared(std::nullptr_t) noexcept : _p(0) {}
shared(const shared& x) noexcept {
_s = x._s;
if (_s) atomic_inc(&_s->refn, mo_relaxed);
}
shared(shared&& x) noexcept {
_p = x._p;
x._p = 0;
}
~shared() { this->reset(); }
shared& operator=(const shared& x) {
if (&x != this) shared<T>(x).swap(*this);
return *this;
}
shared& operator=(shared&& x) {
if (&x != this) shared<T>(std::move(x)).swap(*this);
return *this;
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
shared(const shared<X>& x) noexcept {
_p = x.get();
if (_s) atomic_inc(&_s->refn, mo_relaxed);
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
shared(shared<X>&& x) noexcept {
_p = x.get();
*(void**)&x = 0;
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
shared& operator=(const shared<X>& x) {
if ((void*)&x != (void*)this) shared<T>(x).swap(*this);
return *this;
}
template<typename X, god::if_t<
god::is_base_of<T, X>() && god::has_virtual_destructor<T>(), int
> = 0>
shared& operator=(shared<X>&& x) {
if ((void*)&x != (void*)this) shared<T>(std::move(x)).swap(*this);
return *this;
}
T* get() const noexcept { return _p; }
T* operator->() const { assert(_p); return _p; }
T& operator*() const { assert(_p); return *_p; }
bool operator==(T* p) const noexcept { return _p == p; }
bool operator!=(T* p) const noexcept { return _p != p; }
explicit operator bool() const noexcept { return _p != 0; }
void reset() {
if (_s) {
if (atomic_dec(&_s->refn, mo_acq_rel) == 0) {
static_cast<void>(sizeof(T));
_p->~T();
co::free(_s, _s->size);
}
_p = 0;
}
}
size_t ref_count() const noexcept {
return _s ? atomic_load(&_s->refn, mo_relaxed) : 0;
}
size_t use_count() const noexcept {
return this->ref_count();
}
void swap(shared& x) noexcept {
T* const p = _p;
_p = x._p;
x._p = p;
}
void swap(shared&& x) noexcept {
x.swap(*this);
}
private:
struct S { T o; uint32 refn; uint32 size; };
union { S* _s; T* _p; };
};
template<typename T, typename... Args>
inline shared<T> make_shared(Args&&... args) {
struct S { T o; uint32 refn; uint32 size; };
S* const s = (S*) co::alloc(sizeof(S));
if (s) {
new(s) T(std::forward<Args>(args)...);
s->refn = 1;
s->size = sizeof(S);
}
shared<T> x;
*(void**)&x = s;
return x;
}
struct default_allocator {
static void* alloc(size_t n) {
return co::alloc(n);
}
static void free(void* p, size_t n) {
return co::free(p, n);
}
static void* realloc(void* p, size_t o, size_t n) {
return co::realloc(p, o, n);
}
};
struct system_allocator {
static void* alloc(size_t n) {
return ::malloc(n);
}
static void free(void* p, size_t) {
return ::free(p);
}
static void* realloc(void* p, size_t, size_t n) {
return ::realloc(p, n);
}
};
// allocator for STL, alternative to std::allocator
template<class T>
struct stl_allocator {
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using propagate_on_container_move_assignment = std::true_type;
using is_always_equal = std::true_type;
typedef value_type* pointer;
typedef value_type const* const_pointer;
typedef value_type& reference;
typedef value_type const& const_reference;
stl_allocator() noexcept = default;
stl_allocator(const stl_allocator&) noexcept = default;
template<class U> stl_allocator(const stl_allocator<U>&) noexcept {}
#if (__cplusplus >= 201703L) // C++17
T* allocate(size_type n) {
return static_cast<T*>(co::alloc(n * sizeof(T)));
}
T* allocate(size_type n, const void*) { return allocate(n); }
#else
pointer allocate(size_type n, const void* = 0) {
return static_cast<pointer>(co::alloc(n * sizeof(value_type)));
}
#endif
void deallocate(T* p, size_type n) { co::free(p, n * sizeof(T)); }
template<class U, class ...Args>
void construct(U* p, Args&& ...args) {
::new(p) U(std::forward<Args>(args)...);
}
template<class U>
void destroy(U* p) noexcept { p->~U(); }
template<class U> struct rebind { using other = stl_allocator<U>; };
pointer address(reference x) const noexcept { return &x; }
const_pointer address(const_reference x) const noexcept { return &x; }
size_type max_size() const noexcept {
return static_cast<size_t>(-1) / sizeof(value_type);
}
};
template<class T1, class T2>
constexpr bool operator==(const stl_allocator<T1>&, const stl_allocator<T2>&) noexcept {
return true;
}
template<class T1, class T2>
constexpr bool operator!=(const stl_allocator<T1>&, const stl_allocator<T2>&) noexcept {
return false;
}
} // co