-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathAllocator.h
172 lines (137 loc) · 4.04 KB
/
Allocator.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
#ifndef AllocatorH
#define AllocatorH
#include <memory>
namespace Moya {
template <class T, std::size_t growSize = 1024>
class MemoryPool
{
struct Block
{
Block *next;
};
class Buffer
{
static const std::size_t blockSize = sizeof(T) > sizeof(Block) ? sizeof(T) : sizeof(Block);
uint8_t data[blockSize * growSize];
public:
Buffer *const next;
Buffer(Buffer *next) :
next(next)
{
}
T *getBlock(std::size_t index)
{
return reinterpret_cast<T *>(&data[blockSize * index]);
}
};
Block *firstFreeBlock = nullptr;
Buffer *firstBuffer = nullptr;
std::size_t bufferedBlocks = growSize;
public:
MemoryPool() = default;
MemoryPool(MemoryPool &&memoryPool) = delete;
MemoryPool(const MemoryPool &memoryPool) = delete;
MemoryPool operator =(MemoryPool &&memoryPool) = delete;
MemoryPool operator =(const MemoryPool &memoryPool) = delete;
~MemoryPool()
{
while (firstBuffer) {
Buffer *buffer = firstBuffer;
firstBuffer = buffer->next;
delete buffer;
}
}
T *allocate()
{
if (firstFreeBlock) {
Block *block = firstFreeBlock;
firstFreeBlock = block->next;
return reinterpret_cast<T *>(block);
}
if (bufferedBlocks >= growSize) {
firstBuffer = new Buffer(firstBuffer);
bufferedBlocks = 0;
}
return firstBuffer->getBlock(bufferedBlocks++);
}
void deallocate(T *pointer)
{
Block *block = reinterpret_cast<Block *>(pointer);
block->next = firstFreeBlock;
firstFreeBlock = block;
}
};
template <class T, std::size_t growSize = 1024>
class Allocator : private MemoryPool<T, growSize>
{
#if defined(_WIN32) && defined(ENABLE_OLD_WIN32_SUPPORT)
Allocator *copyAllocator = nullptr;
std::allocator<T> *rebindAllocator = nullptr;
#endif
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef Allocator<U, growSize> other;
};
#if defined(_WIN32) && defined(ENABLE_OLD_WIN32_SUPPORT)
Allocator() = default;
Allocator(Allocator &allocator) :
copyAllocator(&allocator)
{
}
template <class U>
Allocator(const Allocator<U, growSize> &other)
{
if (!std::is_same<T, U>::value)
rebindAllocator = new std::allocator<T>();
}
~Allocator()
{
delete rebindAllocator;
}
#endif
pointer allocate(size_type n, const void *hint = 0)
{
#if defined(_WIN32) && defined(ENABLE_OLD_WIN32_SUPPORT)
if (copyAllocator)
return copyAllocator->allocate(n, hint);
if (rebindAllocator)
return rebindAllocator->allocate(n, hint);
#endif
if (n != 1 || hint)
throw std::bad_alloc();
return MemoryPool<T, growSize>::allocate();
}
void deallocate(pointer p, size_type n)
{
#if defined(_WIN32) && defined(ENABLE_OLD_WIN32_SUPPORT)
if (copyAllocator) {
copyAllocator->deallocate(p, n);
return;
}
if (rebindAllocator) {
rebindAllocator->deallocate(p, n);
return;
}
#endif
MemoryPool<T, growSize>::deallocate(p);
}
void construct(pointer p, const_reference val)
{
new (p) T(val);
}
void destroy(pointer p)
{
p->~T();
}
};
}
#endif