-
Notifications
You must be signed in to change notification settings - Fork 7
/
RSandBox.c
388 lines (334 loc) · 12.7 KB
/
RSandBox.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* RSandBox.c
* Defines continue heap block,
* with random pointer allocation, deallocation,
* encryption and management table, etc.
* May be used like auto-release pool in obj-c.
* Author Kucheruavyu Ilya (kojiba@protonmail.com)
* 30.10.14 2014 Ukraine Kharkiv
* _ _ _ _
* | | (_|_) |
* | | _____ _ _| |__ __ _
* | |/ / _ \| | | '_ \ / _` |
* | < (_) | | | |_) | (_| |
* |_|\_\___/| |_|_.__/ \__,_|
* _/ |
* |__/
**/
#include "RSandBox.h"
#define toSandboxPtrs() setRMalloc (object->innerMalloc);\
setRCalloc (object->innerCalloc);\
setRRealloc(object->innerRealloc);\
setRFree (object->innerFree)
#if defined(RAY_SAND_BOX_THREAD_SAFE)
#define storeSandboxPtrs()
#define backSandboxPtrs()
#define toInnerSandboxPtrs()
#define sandboxMutex &object->mutex
#define RMutexLockSandbox() RMutexLock(sandboxMutex)
#define RMutexUnlockSandbox() RMutexUnlock(sandboxMutex)
#define isMutexDeadLocked !lockOrDeadlocked(sandboxMutex)
#else
#define storeSandboxPtrs() storePtrs()
#define backSandboxPtrs() backPtrs()
#define toInnerSandboxPtrs() toSandboxPtrs()
#define sandboxMutex
#define RMutexLockSandbox()
#define RMutexUnlockSandbox()
#endif
constructor (RSandBox), size_t sizeOfMemory, size_t descriptorsCount){
object = allocator(RSandBox);
if(object != nil) {
object->descriptorTable = arrayAllocator(RControlDescriptor, descriptorsCount);
object->memPart = makeRDataBytes(arrayAllocator(byte, sizeOfMemory), sizeOfMemory);
if(object->memPart != nil && object->descriptorTable != nil) {
object->classId = 4;
object->descriptorsTotal = descriptorsCount;
object->descriptorsCount = 0;
object->innerMalloc = getRMalloc();
object->innerRealloc = getRRealloc();
object->innerCalloc = getRCalloc();
object->innerFree = getRFree();
object->allocationMode = RSandBoxAllocationModeStandart;
object->delegate = nil;
#ifdef RAY_SAND_BOX_THREAD_SAFE
mutexWithType(&object->mutex, RMutexErrorCheck);
#endif
} else {
RError("RSandBox. Can't allocate descriptors table or memory part.", object);
}
}
return object;
}
destructor(RSandBox) {
disableSandBox(object);
RMutexLockSandbox();
if(object->allocationMode == RSandBoxAllocationModeRandom
|| object->allocationMode == RSandBoxAllocationModeDelegated) {
// totally fresh all to 0
flushAllToByte(object->memPart->data, object->memPart->size, 0);
deleter(object->memPart, RData);
flushAllToByte((byte *) object->descriptorTable, object->descriptorsTotal * sizeof(RControlDescriptor), 0);
deallocator(object->descriptorTable);
flushAllToByte((byte *) object, sizeof(RSandBox), 0);
} else {
// simple cleanup
deleter(object->memPart, RData);
deallocator(object->descriptorTable);
}
RMutexUnlockSandbox();
#ifdef RAY_SAND_BOX_THREAD_SAFE
RMutexDestroy(sandboxMutex);
#endif
}
method(size_t, memoryPlaced, RSandBox)) {
switch(object->allocationMode) {
case RSandBoxAllocationModeStandart : {
return object->descriptorTable[object->descriptorsCount - 1].memRange.start
+ object->descriptorTable[object->descriptorsCount - 1].memRange.size;
}
case RSandBoxAllocationModeRandom : {
size_t result = 0;
size_t iterator;
forAll(iterator, object->descriptorsCount) {
result += object->descriptorTable[iterator].memRange.size;
}
return result;
}
case RSandBoxAllocationModeDelegated : {
return object->delegate->memoryPlaced(object);
}
}
return 0;
}
printer(RSandBox) {
storeSandboxPtrs();
RMutexLockSandbox();
toInnerSandboxPtrs();
size_t iterator;
RPrintf("%s object - %p {\n", toString(RSandBox), object);
RPrintf("\t Mem total - %lu (bytes)\n", object->memPart->size);
size_t placed = 0;
if(object->allocationMode == RSandBoxAllocationModeStandart
|| object->allocationMode == RSandBoxAllocationModeRandom) {
placed = $(object, m(memoryPlaced, RSandBox)));
}
if(object->allocationMode == RSandBoxAllocationModeDelegated) {
placed = object->delegate->memoryPlaced(object);
}
RPrintf("\t Mem placed - %lu (bytes)\n", placed);
RPrintf("\t Mem free - %lu (bytes)\n", object->memPart->size - placed);
RPrintf("\t Descriptors total - %lu\n", object->descriptorsTotal);
RPrintf("\t Descriptors in use - %lu\n", object->descriptorsCount);
if(object->descriptorsCount > 0) {
RPrintf("\t\t Pointer | Start | Size \n");
forAll(iterator, object->descriptorsCount) {
RPrintf("\t\t %p | %lu | %lu \n",
(pointer) (object->memPart->data + object->descriptorTable[iterator].memRange.start),
object->descriptorTable[iterator].memRange.start,
object->descriptorTable[iterator].memRange.size);
}
}
RPrintLn("}\n");
RMutexUnlockSandbox();
backSandboxPtrs();
}
#pragma mark Workings
method(rbool, isRangeFree, RSandBox), RRange range) {
size_t iterator;
switch (object->allocationMode) {
case RSandBoxAllocationModeRandom : {
forAll(iterator, object->descriptorsCount) {
if(isOverlappingRRange(object->descriptorTable[iterator].memRange, range) == yes) {
return no;
}
}
return yes;
}
case RSandBoxAllocationModeStandart : {
// if start smaller than last size + start
if(range.start > object->descriptorTable[object->descriptorsCount - 1].memRange.size +
object->descriptorTable[object->descriptorsCount - 1].memRange.start) {
return yes;
} else {
return no;
}
}
case RSandBoxAllocationModeDelegated : {
return object->delegate->isRangeFree(object);
};
}
return yes;
}
method(void, addFilledRange, RSandBox), RRange range) {
object->descriptorTable[object->descriptorsCount].memRange = range;
++object->descriptorsCount;
}
method(size_t, rangeForPointer, RSandBox), pointer ptr) {
size_t shift = ptr - (pointer)(object->memPart->data);
if(shift > object->memPart->size) {
RError1("RSandBox. Pointer - %p wasn't allocated with sandBox.", object, ptr);
return object->descriptorsCount;
} else {
size_t iterator;
forAll(iterator, object->descriptorsCount) {
if(object->descriptorTable[iterator].memRange.start == shift) {
return iterator;
}
}
return object->descriptorsCount;
}
}
#pragma mark Main methods
method(pointer, malloc, RSandBox), size_t sizeInBytes) {
#ifdef RAY_SAND_BOX_THREAD_SAFE
if(isMutexDeadLocked) {
return object->innerMalloc(sizeInBytes);
}
#endif
if(sizeInBytes < object->memPart->size) {
storeSandboxPtrs()
RMutexLockSandbox();
toInnerSandboxPtrs();
if(object->descriptorsTotal == object->descriptorsCount + 1) {
object->descriptorTable = RReAlloc(object->descriptorTable, arraySize(RControlDescriptor, object->descriptorsTotal * 2));
if(object->descriptorTable != nil) {
object->descriptorsTotal *= 2;
} else {
RMutexUnlockSandbox();
backSandboxPtrs();
RError("RSandBox. Can't reallocate descriptors table.", object);
return nil;
}
}
RRange placeToAlloc;
placeToAlloc.size = sizeInBytes;
switch (object->allocationMode) {
case RSandBoxAllocationModeRandom : {
// based on std rand
placeToAlloc.start = rand() % (object->memPart->size - sizeInBytes);
while($(object, m(isRangeFree, RSandBox)), placeToAlloc) == no) {
placeToAlloc.start = rand() % (object->memPart->size - sizeInBytes);
}
} break;
case RSandBoxAllocationModeStandart : {
// if first
if(object->descriptorsCount == 0) {
placeToAlloc.start = 0;
// will be next to last
} else {
placeToAlloc.start =
object->descriptorTable[object->descriptorsCount - 1].memRange.start
+
object->descriptorTable[object->descriptorsCount - 1].memRange.size;
}
} break;
case RSandBoxAllocationModeDelegated : {
// based on delegate
placeToAlloc.start = object->delegate->rangeGenerator(object);
} break;
}
if(placeToAlloc.start + sizeInBytes < object->memPart->size) {
$(object, m(addFilledRange, RSandBox)), placeToAlloc);
RMutexUnlockSandbox();
backSandboxPtrs();
return object->memPart->data + placeToAlloc.start;
} elseError(
RError("RSandBox. Not enought memory.", object)
);
RMutexUnlockSandbox();
backSandboxPtrs();
} elseError(
RError("RSandBox. Size to allocate more than sand box total memory.", object)
);
return nil;
}
method(pointer, realloc, RSandBox), pointer ptr, size_t newSize) {
if(ptr == nil) {
return $(object, m(malloc, RSandBox)), newSize);
} else if (newSize == 0) {
$(object, m(free, RSandBox)), ptr);
return nil;
} else {
#ifdef RAY_SAND_BOX_THREAD_SAFE
if(isMutexDeadLocked) {
return object->innerRealloc(ptr, newSize);
} else {
#endif
pointer some = nil;
size_t iterator;
storeSandboxPtrs()
RMutexLockSandbox();
toInnerSandboxPtrs();
iterator = $(object, m(rangeForPointer, RSandBox)), ptr);
RMutexUnlockSandbox();
backSandboxPtrs();
if(iterator != object->descriptorsCount) {
some = $(object, m(malloc, RSandBox)), newSize);
if(some != nil) {
RMemCpy(some, ptr, object->descriptorTable[iterator].memRange.size);
$(object, m(free, RSandBox)), ptr);
return some;
}
}
#ifdef RAY_SAND_BOX_THREAD_SAFE
}
#endif
}
return nil;
}
method(pointer, calloc, RSandBox), size_t blockCount, size_t blockSize) {
#ifdef RAY_SAND_BOX_THREAD_SAFE
if(isMutexDeadLocked) {
return object->innerCalloc(blockCount, blockSize);
}
#endif
storeSandboxPtrs()
RMutexLockSandbox();
toInnerSandboxPtrs();
pointer some = $(object, m(malloc, RSandBox)), blockCount * blockSize);
flushAllToByte(some, blockCount * blockSize, 0);
RMutexUnlockSandbox();
backSandboxPtrs();
return some;
}
method(void, free, RSandBox), pointer ptr) {
if(ptr != nil) {
#ifdef RAY_SAND_BOX_THREAD_SAFE
if(isMutexDeadLocked) {
return object->innerFree(ptr);
}
#endif
storeSandboxPtrs()
RMutexLockSandbox();
toInnerSandboxPtrs();
size_t rangeIterator = $(object, m(rangeForPointer, RSandBox)), ptr);
if (rangeIterator != object->descriptorsCount) {
if(object->allocationMode == RSandBoxAllocationModeRandom
|| object->allocationMode == RSandBoxAllocationModeDelegated) {
// totally fresh all to 0
flushAllToByte(object->memPart->data + object->descriptorTable[rangeIterator].memRange.start, object->descriptorTable[rangeIterator].memRange.size, 0);
}
RMemMove(object->descriptorTable + rangeIterator, object->descriptorTable + rangeIterator + 1, (object->descriptorsTotal - rangeIterator) * sizeof(RControlDescriptor));
--object->descriptorsCount;
}
RMutexUnlockSandbox();
backSandboxPtrs();
} else {
RWarning("RSandBox. Free nil.", object);
}
}
#pragma mark Switch
void enableSandBox(RSandBox *object) {
RMutexLockSandbox();
setRMalloc (object->selfMalloc);
setRCalloc (object->selfCalloc);
setRRealloc(object->selfRealloc);
setRFree (object->selfFree);
RMutexUnlockSandbox();
}
void disableSandBox(RSandBox *object) {
RMutexLockSandbox();
toSandboxPtrs();
RMutexUnlockSandbox();
}