-
Notifications
You must be signed in to change notification settings - Fork 12
/
memory.c
416 lines (384 loc) · 11.3 KB
/
memory.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "citrine.h"
uint64_t ctr_gc_alloc;
uint64_t ctr_gc_memlimit;
/**
* Heap Object, represents dynamic memory.
*/
struct memBlock {
size_t size;
void* space;
};
typedef struct memBlock memBlock;
memBlock* memBlocks = NULL;
size_t numberOfMemBlocks = 0;
size_t maxNumberOfMemBlocks = 0;
char* ctr_pool_alloc( ctr_size podSize );
void ctr_pool_dealloc( void* ptr );
void ctr_pool_init();
int ctr_pool_bucket( ctr_size size );
/**
* Heap allocate raw memory
* Allocates a slice of memory having the specified size in bytes.
* The memory will be zeroed (calloc is used).
*
* If the specified number of bytes cannot be allocated, the program
* will end with exit 1.
*
* If the specified number of bytes causes the total number of allocated
* bytes to exceed the GC thresold, the garbage collector will attempt to free
* memory.
*
* If the specified number of bytes causes the total number of allocated
* bytes to exceed the predetermined memory limit, the program will exit with
* return code 1.
*
* This function will track the allocated bytes to monitor memory
* management.
*
* @param uintptr_t size memory size
*
* @return void*
*/
void* ctr_heap_allocate( size_t size ) {
void* slice_of_memory;
size_t* block_width;
int q = sizeof( size_t );
size += q;
size = ctr_pool_bucket( size );
/* Check whether we can afford to allocate this much */
ctr_gc_alloc += size;
if (ctr_gc_memlimit < ctr_gc_alloc) {
printf( CTR_MERR_OOM, (unsigned long) size );
exit(1);
}
/* Perform allocation and check result */
slice_of_memory = ctr_pool_alloc( size );
if ( slice_of_memory == NULL ) {
printf( CTR_MERR_MALLOC, (unsigned long) size );
exit(1);
}
/* Store the width of the memory block in the slice itself so we can always find it */
block_width = (size_t*) slice_of_memory;
*(block_width) = size;
/* Now move the new memory pointer behind the blockwidth */
slice_of_memory = (void*) ((char*) slice_of_memory + q);
return slice_of_memory;
}
/**
* Returns the current memory block number so you can rewind
* the tracked memory allocator back to this point later on.
* Used for deserializing.
*/
size_t ctr_heap_tracker_memoryblocknumber() {
return numberOfMemBlocks;
}
/**
* Rewinds memory block number to a previously stored
* position, i.e. sequence number.
*/
size_t ctr_heap_tracker_rewind( size_t memoryBlockNumber ) {
size_t i;
i = 0;
while ( numberOfMemBlocks > memoryBlockNumber) {
ctr_heap_free(memBlocks[ --numberOfMemBlocks ].space);
i ++;
}
return i;
}
/**
* Allocates memory on heap and tracks it for clean-up when
* the program ends.
*/
void* ctr_heap_allocate_tracked( size_t size ) {
void* space;
space = ctr_heap_allocate( size );
if ( numberOfMemBlocks >= maxNumberOfMemBlocks ) {
if ( memBlocks == NULL ) {
memBlocks = ctr_heap_allocate( sizeof( memBlock ) );
maxNumberOfMemBlocks = 1;
} else {
maxNumberOfMemBlocks += 10;
memBlocks = ctr_heap_reallocate( memBlocks, ( sizeof( memBlock ) * ( maxNumberOfMemBlocks ) ) );
}
}
memBlocks[ numberOfMemBlocks ].space = space;
memBlocks[ numberOfMemBlocks ].size = size;
numberOfMemBlocks ++;
return space;
}
/**
* Reallocates tracked memory on heap.
* You need to provide a tracking ID.
*/
void* ctr_heap_reallocate_tracked( size_t tracking_id, size_t size ) {
void* space;
space = memBlocks[ tracking_id ].space;
space = ctr_heap_reallocate( space, size );
memBlocks[ tracking_id ].space = space;
memBlocks[ tracking_id ].size = size;
return space;
}
/**
* Returns the latest tracking ID after tracking allocation.
*/
size_t ctr_heap_get_latest_tracking_id() {
return numberOfMemBlocks - 1;
}
/**
* Frees all tracked memory blocks.
*/
void ctr_heap_free_rest() {
size_t i;
for ( i = 0; i < numberOfMemBlocks; i ++) {
ctr_heap_free( memBlocks[i].space );
}
ctr_heap_free( memBlocks );
memBlocks = NULL;
numberOfMemBlocks = 0;
maxNumberOfMemBlocks = 0;
}
/**
* Heap free memory
*
* Frees the memory pointed to by the specified pointer and deducts
* the specified size from the allocation bookkeepting variable.
*
* @param void* ptr pointer to memory to be freed
*
* @return void
*/
int ctr_gc_clean_free = 0;
void ctr_heap_free( void* ptr ) {
if (ptr == NULL) return;
size_t* block_width;
int q = sizeof( size_t );
size_t size;
/* find the correct size of this memory block and move pointer back */
ptr = (void*) ((char*) ptr - q);
if (ptr == NULL) return;
block_width = (size_t*) ptr;
size = *(block_width);
if (ctr_gc_clean_free) {
memset(ptr,0,size);
}
ctr_pool_dealloc( ptr );
if (size > ctr_gc_alloc) {
ctr_print_error("[WARNING] Freeing more memory than allocated.", -1);
ctr_gc_alloc = 0;
} else {
ctr_gc_alloc -= size;
}
}
/**
* Memory Management Adjust Memory Block Size (re-allocation)
* Re-allocates Memory Block.
*
* Given the old pointer, the desired size, the original size and
* the purpose for allocation, this function will attempt to
* re-allocate the memory block.
*/
void* ctr_heap_reallocate(void* oldptr, size_t size ) {
char* nptr;
size_t old_size;
size_t* block_width;
/* correct the required size new block to include block width */
int q = sizeof( size_t );
size += q;
size = ctr_pool_bucket( size );
/* move the pointer back to begin of block */
oldptr = (void*) ((char*) oldptr - q);
/* read memory size at beginning of old block */
block_width = (size_t*) oldptr;
old_size = *(block_width);
/* if somehow the requested size is less than the old size */
/* (because of bucketing) just return same memory block */
/* otherwise upon copying memory contents we will cross */
/* boundaries. */
if (size <= old_size) {
return (void*) ((char*) oldptr + q);
}
/* update the ledger */
ctr_gc_alloc = ( ctr_gc_alloc - old_size ) + size;
/* re-allocate memory */
nptr = ctr_pool_alloc( size );
if ( nptr == NULL ) {
printf( CTR_MERR_MALLOC, (unsigned long) size );
exit(1);
}
memcpy( nptr, oldptr, old_size );
ctr_pool_dealloc(oldptr);
/* store the size of the new block at the beginning */
block_width = (size_t*) nptr;
*(block_width) = size;
/* 'hop' the memory pointer over the block width part */
nptr = (void*) ((char*) nptr + q);
return (void*) nptr;
}
/**
* @internal
*
* Casts a string object to a cstring.
* Given an object with a string value, this function
* will return a C-string representing the bytes contained
* in the String Object. This function will explicitly end
* the returned set of bytes with a \0 byte for use
* in traditional C string functions.
*
* Warning: this function 'leaks' memory, you have to ctr_heap_free() it.
* It will allocate the necessary resources to store the string.
* To free this memory you'll need to call ctr_heap_free
* passing the pointer and the number of bytes ( value.svalue->vlen ).
*
* @note
* This function removes NULL-bytes from the resulting C-string
* to avoid NUL-byte-injections.
*
* @param ctr_object* stringObject CtrString object instance to cast
*
* @return char*
*/
char* ctr_heap_allocate_cstring( ctr_object* stringObject ) {
char* cstring;
char* stringBytes;
ctr_size length;
ctr_size i, j;
stringBytes = stringObject->value.svalue->value;
length = stringObject->value.svalue->vlen;
cstring = ctr_heap_allocate( ( length + 1 ) * sizeof( char ) );
j = 0;
for (i = 0; i < length; i++) {
if ( stringBytes[i] == '\0' ) {
continue;
}
cstring[j++] = stringBytes[i];
}
cstring[j] = '\0';
return cstring;
}
/**
* Pool Allocator
*
* The Pool Allocator improves the performance of memory allocation in Citrine.
* To activate the pool allocator, set the 4th bit (i.e. 8) of the Broom Garbage Collector
* using 'Broom mode:'.
*/
int usePools = 0;
char* spodmem;
char* mpodmem;
char* lpodmem;
char** freeslist;
char** freemlist;
char** freellist;
ctr_size freespods = 0;
ctr_size freempods = 0;
ctr_size freelpods = 0;
ctr_size spods = 0;
ctr_size mpods = 0 ;
ctr_size lpods = 0 ;
ctr_size spodCount = 0;
ctr_size mpodCount = 0;
ctr_size lpodCount = 0;
ctr_size spod = 32;
ctr_size mpod = 64;
ctr_size lpod = 128;
/**
* Initializes the Pool Allocator with the specified memory limit.
* By default the Pool Allocator will use 50% of the specified memory
* for pool allocation.
*/
void ctr_pool_init( ctr_size pool ) {
if (usePools) return; /* You cannot init twice */
usePools = 1;
/* If pool is too small exit with error code */
if (pool < 300) {
printf( CTR_MERR_POOL );
exit(1);
}
ctr_size poolSize = pool / 3;
spods = (poolSize / spod) - 1;
mpods = (poolSize / mpod) - 1;
lpods = (poolSize / lpod) - 1;
spodmem = malloc( poolSize );
mpodmem = malloc( poolSize );
lpodmem = malloc( poolSize );
if (spodmem == NULL || mpodmem == NULL || lpodmem == NULL) {
printf( CTR_MERR_POOL );
exit(1);
}
freeslist = (char**) malloc(sizeof(char**) * spods);
freemlist = (char**) malloc(sizeof(char**) * mpods);
freellist = (char**) malloc(sizeof(char**) * lpods);
}
/**
* Given the size of the requested memory this function will return the
* resulting POD size to be used. There are 3 pod sizes available.
*
* <= 32 bytes: Small sized POD (spod)
* > 32 bytes and <= 64 bytes Medium sized POD (mpod)
* > 64 bytes and <= 128 bytes Large sized POD (lpod)
*
* If the size fits in one of these pods (<= lpod) but no pods are available
* anymore in the pool, the size will be adjusted to size + 1, so
* 32 will become 33. That way, the pool will be able to separate
* these custom allocations from pods. Otherwise you will get double frees.
*/
int ctr_pool_bucket( ctr_size size ) {
if ( size > 0 && size <= spod && spodCount<spods) {
size = spod;
} else if ( size > spod && size <= mpod && mpodCount<mpods) {
size = mpod;
} else if ( size > mpod && size <= lpod && lpodCount<lpods) {
size = lpod;
} else if (size == spod || size == mpod || size == lpod) {
size = size + 1; /* identify as custom size (avoid ambiguity 32 -> 33) otherwise double free(). */
}
return size;
}
/**
* Returns a pointer to a block of memory allocated using either
* the pool or the OS.
*/
char* ctr_pool_alloc( ctr_size podSize ) {
if (!usePools) return (char*) calloc(podSize, 1);
char* memblock = NULL;
if (podSize == spod && freespods>0) {
memblock = (char*) (freeslist[--freespods]);
} else if (podSize == spod && spodCount<spods) {
memblock = (spodmem + ((spodCount++)*spod));
} else if (podSize == mpod && freempods>0) {
memblock = (char*) (freemlist[--freempods]);
} else if (podSize == mpod && mpodCount<mpods) {
memblock = (mpodmem + ((mpodCount++)*mpod));
} else if (podSize == lpod && freelpods>0) {
memblock = (char*) (freellist[--freelpods]);
} else if (podSize == lpod && lpodCount<lpods) {
memblock = (lpodmem + ((lpodCount++)*lpod));
} else {
memblock = (char*) calloc(podSize, 1);
}
return (char*) memblock;
}
/**
* Deallocates memory using the pool or the OS.
*/
void ctr_pool_dealloc( void* ptr ) {
if (ptr == NULL) return;
if (!usePools) {
free(ptr);
return;
}
ctr_size podSize;
podSize = *((ctr_size*) ptr);
if (podSize == spod && freespods<spods) {
freeslist[freespods++] = (char*) ptr;
memset(ptr,0,spod);
} else if (podSize == mpod && freempods<mpods) {
freemlist[freempods++] = (char*) ptr;
memset(ptr,0,mpod);
} else if (podSize == lpod && freelpods<lpods) {
freellist[freelpods++] = (char*) ptr;
memset(ptr,0,lpod);
} else {
free(ptr);
}
}