-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalloc.h
67 lines (56 loc) · 1.57 KB
/
alloc.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
#ifndef ALLOC_H
#define ALLOC_H
#include <cstdio>
#include <cwchar>
class linear_allocator_base {
protected:
void * const base;
std::size_t bytes = 0;
protected:
linear_allocator_base (std::size_t reserve, std::size_t commit);
~linear_allocator_base ();
bool commit (std::size_t n);
};
template <typename T>
class linear_allocator : linear_allocator_base {
T * next;
T * prev;
inline bool commit (std::size_t n) {
n += this->next - (T *) this->base;
n *= sizeof (T);
if (n > this->bytes) {
return this->linear_allocator_base::commit (n);
} else
return true;
}
public:
explicit linear_allocator (std::size_t reserve = 0x100'0000, std::size_t commit = 0x1000 / sizeof (T))
: linear_allocator_base (sizeof (T) * reserve, sizeof (T) * commit)
, next (static_cast <T *> (this->base))
, prev (nullptr) {}
inline T * alloc (std::size_t n) {
if (this->commit (n)) {
this->prev = this->next;
this->next += n;
return this->prev;
} else
return nullptr;
}
inline T * resize (T * p, std::size_t n) {
if (p == this->prev) {
if (this->commit (n)) {
this->next = this->prev + n;
return p;
}
} else {
if (auto q = this->alloc (n)) {
if (p) {
std::wmemcpy (q, p, n);
}
return q;
}
}
return nullptr;
}
};
#endif