-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzAllocator.h
62 lines (47 loc) · 1.62 KB
/
zAllocator.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
#pragma once
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <new>
#include <utility>
#include "flatPool.h"
template <class T>
struct zAllocator : public std::allocator<T> {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
template <class U>
struct rebind {
using other = zAllocator<U>;
};
zAllocator() = default;
~zAllocator() = default;
zAllocator(zAllocator&&) = delete;
zAllocator(const zAllocator&) = delete;
zAllocator& operator=(zAllocator&& other) = delete;
zAllocator& operator=(const zAllocator& other) = delete;
// todo: custom allocator for unordered container
template <class U>
zAllocator(const zAllocator<U>&) noexcept {}
void deallocate(pointer p, size_type n = 1) { memPool_.deallocate(p); }
pointer allocate(size_type n, const void* hint = 0) { return memPool_.allocate(); }
void destroy(pointer p) { memPool_.deallocate(p); }
template <class U = T, class... Params>
void construct(U* p, Params&&... params) {
::new (p) U(std::forward<Params>(params)...);
}
void construct(pointer p, const T& value) { ::new (p) T(value); }
pointer address(reference ref) { return &ref; }
const_pointer address(const_reference cref) { return &cref; }
size_type max_size() const { return memPool_.max_size(); }
FlatPool<T>& get_pool() { return memPool_; }
private:
FlatPool<T> memPool_;
};