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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#ifndef _JSYSTEM_JGADGET_VECTOR_H
#define _JSYSTEM_JGADGET_VECTOR_H
#include "JSystem/JGadget/allocator.h"
#include "algorithm.h"
#include "msl_memory.h"
namespace JGadget {
namespace vector {
u32 extend_default(u32, u32, u32);
typedef u32 (*ExtendFunc)(u32, u32, u32);
} // namespace vector
template <typename T, class Allocator = JGadget::TAllocator<T> /***/>
struct TVector {
struct TDestructed_deallocate_ {
TDestructed_deallocate_(JGadget::TAllocator<T>& alloc, T* pointer)
{
mAllocator = &alloc;
mPointer = pointer;
}
~TDestructed_deallocate_() { mAllocator->deallocate(mPointer, 0); }
void set(T* p) { mPointer = p; }
Allocator* mAllocator;
T* mPointer;
};
~TVector() {
Confirm();
clear();
delete mBegin;
}
void insert(T* position, u32 count, T const& value)
{
if (!count) {
return;
}
void** v = Insert_raw(position, count); // insert `count` values before element at `position`
if (v != this->mEnd) {
for (int i = 0; i != count; i++) {
if (v) {
*v = value;
}
v++;
}
}
}
T* Insert_raw(T* position, u32 count)
{
// purpose: to make space for `count` many elements at the supplied location
// returns: pointer to location for new items
T* const pFirst = position;
// JUT_DEBUG_ASSERT((mBegin<=pItFirst_)&&(pItFirst_<=mEnd), 0x1be);
// it's assumed the pointer is to something already in the vector, or pointing to the end
if (count == 0) {
return position;
}
// can we fit in the space already allocated?
if (count + size() <= mCapacity) {
// get the theoretical new end
void** newEnd = pFirst + count;
// if there exists items in the current vector past where we will insert these items
// then we need to move them to be at the end of the vector
if (newEnd < mEnd) {
// get a pointer to the `count` many values that need to be pushed to the end
void** pOverwrittenValues = mEnd - count;
// copy `count` many values to the end of the vector
std::uninitialized_copy(pOverwrittenValues, mEnd, mEnd);
// copy the remaining values that need to be shifted
// copying backwards so we don't move a value into the range we're copying from, erasing data
std::copy_backward(pFirst, pOverwrittenValues, mEnd);
// destroy everything from pFirst -> newEnd, this treats the inserted items as "uninitialized"
DestroyElement_(pFirst, newEnd);
// increment count
mEnd += count;
// return pointer to new items
return position;
} else {
// position + count >= mEnd
// else our values that we want to add will write beyond the current mEnd
// copy the values that exist at our pointer to the newEnd, which is position + count, making room for our `count` many
// items
std::uninitialized_copy(pFirst, mEnd, newEnd);
// uninitialize the values that used to be there
DestroyElement_(pFirst, mEnd);
// increment count
mEnd += count;
// return pointer to new items
return position;
}
}
// count + size() > mCapacity
// we need more space
// figure out how much space we'll need
u32 newSize = GetSize_extend_(count);
// allocate that space
void** newDataPointer = mAllocator.allocate(newSize, 0);
// make sure that data was actually allocated
if (!newDataPointer) {
// return end pointer so we know not to actually assign any values
return end();
}
// this struct will deallocate the specified data pointer when destroyed
// If we end up throwing an exception, it'll deallocate our new data pointer, no leaks!
TDestructed_deallocate_ destructionDeallocator(mAllocator, newDataPointer);
// copy all the beginning of our data up to our pointer to the new data
void** const endOfCopy = std::uninitialized_copy(mBegin, pFirst, newDataPointer);
// copy the rest of the data to fit at the end of our new data
// this leaves a gap of `count` many items in our new data
std::uninitialized_copy(pFirst, mEnd, endOfCopy + count);
// destroy all our current elements, the other elements should be living in the new data
// and we're about to deallocate our
DestroyElement_all_();
// everything should be set, so now we can deallocate our old data pointer
// when this func exits
destructionDeallocator.set(mBegin);
// set our new vector member variables
mEnd = newDataPointer + (mEnd - mBegin + count);
mBegin = newDataPointer;
mCapacity = newSize;
// return where the gap of `count` many items lives
return endOfCopy;
}
void** insert(T* position, const T& value)
{
u32 posOffset = position - mBegin;
// insert one value of `value` at `position`
insert(position, 1, value);
return mBegin + posOffset; // return pointer to new value at position
}
void assign(u32, const T&);
void resize(u32, const T&);
void Resize_raw(u32);
void operator=(const TVector<T>& rhs);
size_t GetSize_extend_(size_t count) const
{
u32 oldSize = size();
u32 neededNewSpace = oldSize + count;
u32 extendedSize = mExtend(capacity(), oldSize, count);
return neededNewSpace > extendedSize ? neededNewSpace : extendedSize;
}
T* begin() { return mBegin; }
T* const begin() const { return mBegin; }
T* end() { return mEnd; }
T* const end() const { return mEnd; }
size_t capacity() const { return mCapacity; }
inline size_t size() const
{
if (begin() == NULL) {
return 0;
}
return ((int)mEnd - (int)mBegin) / 4;
}
void DestroyElement_(T* pFirst, T* pLast)
{
void** iter = pFirst;
while (iter != pLast) {
mAllocator.destroy(iter);
++iter;
}
}
void DestroyElement_all_() { DestroyElement_(mBegin, mEnd); }
void Confirm() const {}
void clear() {
erase(begin(), end());
}
T* erase(T* start, T* end) {
T* ppvVar3 = std::copy(end, TVector::end(), start);
DestroyElement_(ppvVar3, mEnd);
mEnd = ppvVar3;
return start;
}
/* 0x00 */ Allocator mAllocator;
/* 0x04 */ T* mBegin;
/* 0x08 */ T* mEnd;
/* 0x0C */ size_t mCapacity;
/* 0x10 */ vector::ExtendFunc mExtend;
};
// clang-format off
struct TVector_pointer_void : public TVector<void*, TAllocator<void*> > {
TVector_pointer_void(const JGadget::TAllocator<void*>& allocator);
TVector_pointer_void(u32, void* const&, const JGadget::TAllocator<void*>& allocator); // unused/inlined
~TVector_pointer_void();
void** erase(void**, void**);
void** insert(void**, void* const&);
void clear() { erase(begin(), end()); }
void push_back(const void*& value) { insert(end(), (void* const&)value); }
/* 0x00 */ /* TVector */
};
// clang-format on
template <typename T>
struct TVector_pointer : public TVector_pointer_void {
TVector_pointer(const TAllocator<void*>& allocator)
: TVector_pointer_void(allocator)
{
}
~TVector_pointer() { }
const T* begin() const { return (const T*)TVector_pointer_void::begin(); }
T* begin() { return (T*)TVector_pointer_void::begin(); }
const T* end() const { return (const T*)TVector_pointer_void::end(); }
T* end() { return (T*)TVector_pointer_void::end(); }
void push_back(const T& value) { TVector_pointer_void::push_back((const void*&)value); }
/* 0x00 */ /* TVector_pointer_void */
};
// clang-format off
typedef JGadget::TVector<void*, JGadget::TAllocator<void*> > TVPVBase;
// clang-format on
// template <>
// void TVPVBase::insert(void** values, u32 count, void* const& defaultValue)
// {
// if (!count) {
// return;
// }
// void** v = Insert_raw(values, count);
// if (v != this->mEnd) {
// for (int i = 0; i != count; i++) {
// if (v) {
// *v = defaultValue;
// }
// v++;
// }
// }
// }
} // namespace JGadget
#endif
|