forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_allocator.h
207 lines (164 loc) · 4.95 KB
/
bucket_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
This is an allocator useful for doing temporary allocation of memory, where you don't want to deallocate until you are finished with everything.
It does not use multiple bucket sizes like a proper "bucket allocator", I just didn't find any other good name for it.
TODO: Probably should be renamed to linear_allocator
It is usually best to set the bucket size so high that you don't need to allocate many buckets in the common use case.
Note, this is a very memory-inefficient implementation if it overflows the buckets a lot, as it does not go back to earlier buckets to see if they have space for a smaller allocation if they have overflowed once. So you typically want large bucket sizes compared to the allocation sizes.
*/
struct memory_bucket
{
u8 *Data;
memory_bucket *Next;
};
struct bucket_allocator
{
size_t BucketSize;
size_t CurrentUsed;
memory_bucket *First;
memory_bucket *Current;
bucket_allocator() : BucketSize(0), CurrentUsed(0), First(nullptr), Current(nullptr)
{
}
void
Initialize(size_t BucketSize)
{
if(this->BucketSize != 0)
{
FatalError("ERROR (internal): Tried to initialize a bucket allocator twice.\n");
}
//TODO: Round up to multiple of 8?
this->BucketSize = BucketSize;
}
template<typename T> T*
Allocate(size_t Count)
{
if(Count == 0) return nullptr;
if(BucketSize == 0)
{
FatalError("ERROR (internal): Tried to allocate from an uninitialized bucket allocator.\n");
}
size_t RequestedSize = sizeof(T) * Count;
//NOTE: Pad to 64-bit aligned just to be safe. Not sure how important this is though. Should we also try to align to cache boundaries?
size_t Lack = 8 - RequestedSize % 8;
if(Lack != 8) RequestedSize += Lack;
if(!Current || CurrentUsed + RequestedSize > BucketSize)
{
while(RequestedSize > BucketSize)
{
//TODO: This is not a very good way of doing it, but in any case we should just initialize with so large a size that this does not become a problem. This is just a safety stopgap.
//TODO: We should add a way of catching that this happens, in debug mode.
BucketSize *= 2;
}
// Previous buckets are nonexisting or full. Create a new bucket
size_t AllocSize = sizeof(memory_bucket) + BucketSize;
u8 *Memory = AllocClearedArray(u8, AllocSize); //TODO: Should clearing be optional?
memory_bucket * NewBucket = (memory_bucket *)Memory;
NewBucket->Data = Memory + sizeof(memory_bucket);
NewBucket->Next = nullptr;
if(Current) Current->Next = NewBucket;
else First = NewBucket;
Current = NewBucket;
CurrentUsed = 0;
}
T *Result = (T *)(Current->Data + CurrentUsed);
CurrentUsed += RequestedSize;
return Result;
}
template<typename T> T *
Copy(const T* Source, size_t Count)
{
if(Count == 0) return nullptr;
T *Result = Allocate<T>(Count);
memcpy(Result, Source, Count*sizeof(T));
return Result;
}
char *
CopyString(const char *Source)
{
//NOTE: This is for copying 0-terminated strings only
return Copy(Source, strlen(Source)+1);
}
void
DeallocateAll()
{
if(!First) return;
memory_bucket *Bucket = First;
while(Bucket)
{
memory_bucket *Next = Bucket->Next;
free(Bucket);
Bucket = Next;
}
First = nullptr;
Current = nullptr;
CurrentUsed = 0;
}
};
//Array that does not own its data, but instead lives in "bucket memory".
template<typename T>
struct array
{
T *Data;
size_t Count;
array() : Data(nullptr), Count(0) {};
void
Allocate(bucket_allocator *Allocator, size_t Count)
{
Data = Allocator->Allocate<T>(Count);
this->Count = Count;
}
array(bucket_allocator *Allocator, size_t Count)
{
Allocate(Allocator, Count);
}
array<T>
Copy(bucket_allocator *Allocator) const
{
array<T> Result;
Result.Count = Count;
Result.Data = Allocator->Copy(Data, Count);
return Result;
}
template<class container>
void
CopyFrom(bucket_allocator *Allocator, container &Source)
{
Allocate(Allocator, Source.size());
size_t Idx = 0;
for(const T& Value : Source)
Data[Idx++] = Value;
}
inline T& operator[](size_t Index)
{
#ifdef MOBIUS_TEST_INDEX_OVERFLOW
if(Index >= Count) FatalError("Index overflow when indexing an array.\n");
#endif
return Data[Index];
}
inline const T& operator[](size_t Index) const { return Data[Index]; }
//NOTE: For C++11 - style iteration
inline T* begin() { return Data; }
inline T* end() { return Data + Count; }
inline const T* begin() const { return Data; }
inline const T* end() const { return Data + Count; }
inline size_t size() { return Count; }
};
template<typename T>
bool operator==
(const array<T> &A, const array<T> &B)
{
if(A.Count != B.Count) return false;
for(size_t Idx = 0; Idx < A.Count; ++Idx)
if(A[Idx] != B[Idx]) return false;
return true;
}
/*
template<typename T>
array<T> CopyDataToArray(bucket_allocator *Allocator, const T *Data, size_t Count)
{
array<T> Result;
Result.Count = Count;
Result.Data = Allocator->Copy(Data, Count);
return Result;
}
*/