-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackvector.h
More file actions
executable file
·289 lines (254 loc) · 9.69 KB
/
stackvector.h
File metadata and controls
executable file
·289 lines (254 loc) · 9.69 KB
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
/*
Copyright 2020 Jacek Piszczek
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#pragma once
#include <cstdio>
#include <string>
#include <emul/emulregs.h>
#include <exec/tasks.h>
#include <proto/exec.h>
#include <alloca.h>
#include <functional>
#if defined(DEBUG) && DEBUG
extern "C" { void dprintf(const char *,...); };
#define SVOUT printf
#else
#define SVOUT(...)
#endif
/* Helper class aiming to streamline creation of temporary vectors for OBArray object iterations
** in ObjectiveC++ applications, but may have other uses too. The memory for the vector is allocated
** either on stack (if there's enough of it to spare AND the object itself was also allocated
** on stack) or heap. */
template <typename T> class StackVector
{
public:
/* MUST be inlined or alloca() would fail to survive past this method */
__attribute__((always_inline)) StackVector(const size_t size, const size_t mustLeaveStackSizeForScope = (16 * 1024), bool callConstructorsDestructors = true)
: _size(size), _callFree(false), _callConstructorsDestructors(callConstructorsDestructors)
{
const size_t needBytes = size * sizeof(T);
bool onStack = canReserveStack(needBytes, mustLeaveStackSizeForScope) ;
if (onStack) {
#if defined(DEBUG) && DEBUG
struct Task *t = FindTask(NULL);
ULONG usedStack = 0, usedStackAfterAlloca = 0;
NewGetTaskAttrsA(t, &usedStack, sizeof (usedStack), TASKINFOTYPE_USEDSTACKSIZE, NULL);
_memory = static_cast<T*>(alloca(needBytes));
NewGetTaskAttrsA(t, &usedStackAfterAlloca, sizeof (usedStackAfterAlloca), TASKINFOTYPE_USEDSTACKSIZE, NULL);
SVOUT("%s: allocated on stack %p, alloca using stack? %d stack usage grew by %d\n", __PRETTY_FUNCTION__, _memory, isAllocatedOnStack(), usedStackAfterAlloca - usedStack);
#else
_memory = static_cast<T*>(alloca(needBytes));
#endif
}
else {
_memory = static_cast<T*>(malloc(needBytes));
_callFree = true;
SVOUT("%s: allocated on heap %p\n", __PRETTY_FUNCTION__, _memory);
}
if (_callConstructorsDestructors && _memory) {
for (size_t i = 0; i < size; i++) {
new (&_memory[i]) T ();
}
}
}
StackVector() = delete;
~StackVector()
{
if (_callConstructorsDestructors && _memory) {
for (size_t i = 0; i < _size; i++) {
(&_memory[i])->~T();
}
}
if (_callFree)
{
SVOUT("%s: freeing heap %p..\n", __PRETTY_FUNCTION__, _memory);
free(_memory);
}
else
{
SVOUT("%s: memory was alloca'd\n", __PRETTY_FUNCTION__);
}
}
size_t count() const { return _size; }
bool isValid() const { return _memory != nullptr && _size > 0; }
// Invalid when called from another thread than the one that constructed the object
bool isAllocatedOnStack() const { return isStackAddress(FindTask(0), _memory); }
// Iterates over the vector using a lambda
void forEach(std::function<void(T& member, size_t index)>&& onEach) {
if (_memory) {
for (size_t idx = 0; idx < _size; idx++) {
onEach(_memory[idx], idx);
}
}
}
void forEach(std::function<void(const T& member, size_t index)>&& onEach) const {
if (_memory) {
for (size_t idx = 0; idx < _size; idx++) {
onEach(_memory[idx], idx);
}
}
}
void whileEach(std::function<bool(T& member, size_t index)>&& onEach) {
if (_memory) {
for (size_t idx = 0; idx < _size; idx++) {
if (!onEach(_memory[idx], idx))
break;
}
}
}
void whileEach(std::function<bool(const T& member, size_t index)>&& onEach) const {
if (_memory) {
for (size_t idx = 0; idx < _size; idx++) {
if (!onEach(_memory[idx], idx))
break;
}
}
}
T& operator[](size_t index) {
#ifdef STACKVECTORDEBUG
if (index >= _size)
{
SVOUT("%s: Access at %d outside of size %d\n", __PRETTY_FUNCTION__, index, _size);
}
#endif
return _memory[index];
}
T const & operator[] (size_t index) const {
#ifdef STACKVECTORDEBUG
if (index >= _size)
{
SVOUT("%s: Access at %d outside of size %d\n", __PRETTY_FUNCTION__, index, _size);
}
#endif
return _memory[index];
}
protected:
bool canReserveStack(const size_t size, const size_t mustLeaveStackSizeForScope) const
{
struct Task *t = FindTask(NULL);
if (isStackAddress(t, const_cast<StackVector<T>*>(this)))
{
ULONG usedStack = 0;
if (0 != NewGetTaskAttrsA(t, &usedStack, sizeof (usedStack), TASKINFOTYPE_USEDSTACKSIZE, NULL))
{
struct ETask *e = t->tc_ETask;
ULONG lower = ULONG(e->PPCSPLower);
ULONG current = ULONG(e->PPCSPUpper) - usedStack;
SVOUT("%s: 'this' was allocated on stack; lower %p current %p current-size %p\n", __PRETTY_FUNCTION__, lower, current, current - size);
if ((lower + mustLeaveStackSizeForScope) < (current - size))
return true;
}
}
return false;
}
bool isStackAddress(struct Task *t, void *address) const
{
struct ETask *e = t->tc_ETask;
SVOUT("%s: lower %p upper %p addr %p \n", __PRETTY_FUNCTION__, e->PPCSPLower, e->PPCSPUpper, address);
return (address > e->PPCSPLower) && (address < e->PPCSPUpper);
}
T *_memory;
size_t _size;
bool _callFree : 1;
bool _callConstructorsDestructors : 1;
};
#ifdef __OBJC__
#import <ob/OBArray.h>
#import <mui/MUIFamily.h>
class IDVector : public StackVector<id>
{
public:
IDVector(size_t size) : StackVector<id>(size, 32 * 1024, false) { };
};
/*
** GCC doesn't really compile fast enumeration in ObjectiveC++ files, so this can be used to replace it.
** Example:
** BOOL found = NO;
** FastEnumerator<OBString*> objects(stringarray,[&found](OBString* &string, size_t index) {
** if ([string isEqualToString:@"string we're looking for"]) {
** found = YES; return false;
** }
** return true; // keep going
** });
*/
template <typename O> class FastEnumerator : protected StackVector<O>
{
public:
FastEnumerator(OBArray *arrayToEnumerate, std::function<bool(O& member, size_t index)> && enumCallback) : StackVector<O>([arrayToEnumerate count], 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory];
StackVector<O>::whileEach(std::move(enumCallback));
}
};
FastEnumerator(OBArray *arrayToEnumerate, OBRange && range, std::function<bool(O& member, size_t index)> && enumCallback) : StackVector<O>(range.length, 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory inRange:range];
StackVector<O>::whileEach(std::move(enumCallback));
}
};
FastEnumerator() = delete;
~FastEnumerator() = default;
};
template <typename O> class FastFamilyEnumerator : protected StackVector<O>
{
public:
FastFamilyEnumerator(id<MUIFamily> arrayToEnumerate, std::function<bool(O& member, size_t index)> && enumCallback) : StackVector<O>([arrayToEnumerate count], 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory];
StackVector<O>::whileEach(std::move(enumCallback));
}
};
FastFamilyEnumerator(id<MUIFamily> arrayToEnumerate, OBRange && range, std::function<bool(O& member, size_t index)> && enumCallback) : StackVector<O>(range.length, 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory inRange:range];
StackVector<O>::whileEach(std::move(enumCallback));
}
};
FastFamilyEnumerator() = delete;
~FastFamilyEnumerator() = default;
};
template <typename O> class FastForEach: protected StackVector<O>
{
public:
FastForEach(OBArray *arrayToEnumerate, std::function<void(O& member, size_t index)> && enumCallback) : StackVector<O>([arrayToEnumerate count], 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory];
StackVector<O>::forEach(std::move(enumCallback));
}
};
FastForEach(OBArray *arrayToEnumerate, OBRange && range, std::function<void(O& member, size_t index)> && enumCallback) : StackVector<O>(range.length, 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory inRange:range];
StackVector<O>::forEach(std::move(enumCallback));
}
};
FastForEach() = delete;
~FastForEach() = default;
};
template <typename O> class FastFamilyForEach : protected StackVector<O>
{
public:
FastFamilyForEach(id<MUIFamily> arrayToEnumerate, std::function<void(O& member, size_t index)> && enumCallback) : StackVector<O>([arrayToEnumerate count], 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory];
StackVector<O>::forEach(std::move(enumCallback));
}
};
FastFamilyForEach(id<MUIFamily> arrayToEnumerate, OBRange && range, std::function<void(O& member, size_t index)> && enumCallback) : StackVector<O>(range.length, 32 * 1024, false) {
if (StackVector<O>::_memory) {
[arrayToEnumerate getObjects:StackVector<O>::_memory inRange:range];
StackVector<O>::forEach(std::move(enumCallback));
}
};
FastFamilyForEach() = delete;
~FastFamilyForEach() = default;
};
#endif