-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.hpp
35 lines (35 loc) · 885 Bytes
/
alloc.hpp
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
#ifndef ALLOC_HPP
#define ALLOC_HPP
#include <cstddef>
#include <cstdint>
#include <cassert>
#include <utility>
class alloc {
template <typename T>
friend class deferred_ptr;
friend class deferred_heap;
bool alive;
uint8_t *data = nullptr;
size_t size;
void (*dtor)(void*) = nullptr;
void allocate(size_t size);
void deallocate();
void destroy();
public:
~alloc();
template <typename T, typename ...Args>
T *construct(Args ...args)
{
assert(sizeof(T) <= size && "Incorrect data size for type T");
T *ptr = new (data) T(std::forward<T>(args)...);
dtor = [](void*x){static_cast<T*>(x)->~T();};
return ptr;
}
alloc(size_t size);
bool operator==(const alloc &other) const noexcept;
alloc(const alloc &other) = delete;
const alloc &operator=(const alloc &other) = delete;
alloc(alloc &&other) noexcept;
const alloc &operator=(alloc &&other) noexcept;
};
#endif