-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLinearAllocator.h
139 lines (112 loc) · 3.91 KB
/
LinearAllocator.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
//
// LinearAllocator.h
//
#pragma once
#include <atomic>
#include "MemoryUtil.h"
namespace cb
{
/// Thread safe linear allocator.
///@note If alignment is zero then it'll auto align.
template <int Alignment = 0>
class LinearAllocator
{
public:
explicit LinearAllocator(uint32_t size);
LinearAllocator(uint8_t* data, uint32_t size);
~LinearAllocator();
uint8_t* alloc(uint32_t bytes, uint32_t alignment = Alignment);
void dealloc(uint8_t* p);
void deallocAll();
void resize(uint32_t size);
size_t size() const;
private:
uint32_t m_size; // total size of the allocated memory
uint8_t* m_start;
std::atomic<uint32_t> m_current;
LinearAllocator(const LinearAllocator&) = delete;
void operator=(const LinearAllocator&) = delete;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <int Alignment>
inline LinearAllocator<Alignment>::LinearAllocator(uint32_t size)
: m_size(size + size % (Alignment ? Alignment : sizeof(uint32_t))) // aligned to the given alignment or to 4 bytes
, m_start(new uint8_t[m_size]())
, m_current(0)
{
assert(m_current.is_lock_free());
assert(m_size % sizeof(uint32_t) == 0);
}
template <int Alignment>
inline LinearAllocator<Alignment>::LinearAllocator(uint8_t* data, uint32_t size)
: m_size(size)
, m_start(data)
, m_current(0)
{
assert(m_current.is_lock_free());
assert(m_size % sizeof(uint32_t) == 0);
}
template <int Alignment>
inline LinearAllocator<Alignment>::~LinearAllocator()
{
delete[] m_start;
}
template <int Alignment>
inline uint8_t* LinearAllocator<Alignment>::alloc(uint32_t bytes, uint32_t alignment)
{
assert(bytes);
uint8_t* currentOffset;
if (Alignment)
{
assert(alignment == Alignment);
// always allocate aligned data
const uint32_t kAlignment = alignment ? Alignment : 1; // condition just to suppress warnings
bytes += bytes % kAlignment;
uint32_t current = m_current.fetch_add(bytes, std::memory_order_relaxed);
currentOffset = m_start + current;
assert(cb::mem::alignForwardPadding(currentOffset, alignment) == 0);
assert((current + bytes) < m_size);
}
else
{
// must align by adding padding
uint32_t current, padding;
do
{
current = m_current.load(std::memory_order_acquire);
currentOffset = m_start + current;
padding = cb::mem::alignForwardPadding(currentOffset, alignment);
currentOffset += padding;
assert((current + padding + bytes) < m_size);
// retry if the current offset has changed
} while (!m_current.compare_exchange_weak(current, current + padding + bytes, std::memory_order_release));
}
return currentOffset;
}
template <int Alignment>
inline void LinearAllocator<Alignment>::dealloc(uint8_t*)
{
// must use deallocAll
assert(false);
}
template <int Alignment>
inline void LinearAllocator<Alignment>::deallocAll()
{
m_current.store(0, std::memory_order_release);
}
template <int Alignment>
inline void LinearAllocator<Alignment>::resize(uint32_t size)
{
if (size == m_size)
return;
deallocAll();
delete[] m_start;
m_size = size + size % (Alignment ? Alignment : sizeof(uint32_t));
m_start = new uint8_t[m_size]();
}
template <int Alignment>
inline size_t LinearAllocator<Alignment>::size() const
{
return m_current;
}
} // namespace cb