-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc.c
393 lines (329 loc) · 9.03 KB
/
gc.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
#include "gc.h"
// TODO: Unload registers to stack
static header_t empty = {0, NULL};
static header_t *free_list = ∅
static header_t *used_list = NULL;
static uintptr_t stack_start;
static uintptr_t stack_end;
static int initialized = 0;
void print_lists()
{
printf("\nUsed list:\n");
for (header_t *curr = used_list; curr != NULL; curr = curr->next)
{
printf("Block at %p, size: %zu\n", (void *)curr, curr->size);
}
printf("\nFree list:\n");
for (header_t *curr = free_list; curr != ∅ curr = curr->next)
{
printf("Block at %p, size: %zu\n", (void *)curr, curr->size);
}
printf("\n");
}
// Check if a pointer is valid (not null, 0 aligned, and within a reasonable range)
static inline int is_pointer_valid(void *ptr)
{
uintptr_t p = (uintptr_t)ptr;
return IS_ALIGNED(p) && p >= 0x1000 && p < 0x7fffffffffff;
}
// Set the LSB as marked in the next pointer as it is unused
static inline void set_marked(header_t *block)
{
if (block && block->next)
{
block->next = (header_t *)((uintptr_t)(block->next) | MARKED_MASK);
}
}
// Clear the marked bit
static inline void clear_marked(header_t *block)
{
if (block && block->next)
{
block->next = (header_t *)((uintptr_t)(block->next) & ~MARKED_MASK);
}
}
// Return 1 if the block is marked else 0
static inline int is_marked(header_t *block)
{
return block && block->next && ((uintptr_t)(block->next) & MARKED_MASK);
}
// Get the actual pointer from the next pointer by using doing bitwise AND with the inverted mask
static inline header_t *get_actual_pointer(header_t *block)
{
return block ? (header_t *)((uintptr_t)(block->next) & ~MARKED_MASK) : NULL;
}
// Helper function to check if two blocks are adjacent
static inline int is_adjacent(header_t *first, header_t *second)
{
// Check if the end of the first block is the same as the start of the second block
return (char *)first + (first->size * sizeof(header_t)) == (char *)second;
}
static void add_to_free_list(header_t *block)
{
if (!block)
return;
// Initialize the block
block->next = NULL;
// If free list is empty, add the block
if (free_list == &empty)
{
free_list = block;
block->next = ∅
return;
}
header_t *curr = free_list;
header_t *prev = NULL;
// Find appropriate position by looking at the address
while (curr != &empty && curr < block)
{
prev = curr;
curr = curr->next;
}
// Case 1: Insert at the beginning by coallescing with the previous block if possible
if (prev && is_adjacent(prev, block))
{
prev->size += block->size;
block = prev;
}
else if (prev)
{
prev->next = block;
}
else
{
free_list = block;
}
// Case 2: Try to coalesce with next block
if (curr != &empty && is_adjacent(block, curr))
{
block->size += curr->size;
block->next = curr->next;
}
else
{
block->next = curr;
}
printf("Coalescing complete - block at %p with size %zu\n", (void *)block, block->size);
}
// Split a block into two if the remainder is large enough
static header_t *split_block(header_t *block, size_t size)
{
if (!block || block->size <= size)
return block;
// Only split if the remainder is large enough
if (block->size - size > 1)
{
header_t *new_block = (header_t *)((char *)block + size * sizeof(header_t));
new_block->size = block->size - size;
block->size = size;
printf("Split block: original at %p (size %zu), new at %p (size %zu)\n",
(void *)block, block->size, (void *)new_block, new_block->size);
return new_block;
}
return NULL;
}
// Uses sbrk to allocate more memory (can cause conflicts with malloc/calloc) *can be replaced with mmap for larger allocations)
static header_t *get_more_memory(size_t size)
{
size_t alloc_size = size * sizeof(header_t);
if (alloc_size < MIN_PAGE_SIZE)
{
alloc_size = MIN_PAGE_SIZE;
}
header_t *block = sbrk(alloc_size);
if (block == (void *)-1)
{
return NULL;
}
block->size = alloc_size / sizeof(header_t);
block->next = NULL;
printf("Allocated new memory block at %p, size %zu\n", (void *)block, block->size);
return block;
}
void *gc_alloc(size_t size)
{
if (size == 0)
return NULL;
// Calculate required size including header
size_t total_size = (size + sizeof(header_t) - 1) / sizeof(header_t) + 1;
// Find a free block
header_t *curr = free_list;
header_t *prev = NULL;
while (curr != &empty && curr->size < total_size)
{
prev = curr;
curr = curr->next;
}
// If no suitable block found, get more memory
if (curr == &empty)
{
curr = get_more_memory(total_size);
if (!curr)
return NULL;
add_to_free_list(curr);
prev = NULL;
curr = free_list; // Start search again with new block
}
// Remove from free list
if (prev)
{
prev->next = curr->next;
}
else
{
free_list = curr->next;
}
// Split block if it's too large
header_t *remainder = split_block(curr, total_size);
if (remainder)
{
add_to_free_list(remainder);
}
// Add to used list
curr->next = used_list;
used_list = curr;
printf("Allocated block at %p, size %zu\n", (void *)curr, curr->size);
return (void *)(curr + 1);
}
// Scan the stack range for pointers to the heap and mark them
static void scan_range(void *start, void *end)
{
if (!start || !end || start >= end)
return;
uintptr_t *curr = (uintptr_t *)start;
const uintptr_t *limit = (uintptr_t *)end;
while (curr < limit)
{
if (is_pointer_valid((void *)*curr))
{
for (header_t *block = used_list; block != NULL; block = get_actual_pointer(block))
{
// Check if the pointer is within the block
void *block_start = (void *)(block + 1);
void *block_end = (void *)((char *)block_start + (block->size - 1) * sizeof(header_t));
if ((void *)*curr >= block_start && (void *)*curr < block_end)
{
// Mark the block
set_marked(block);
break;
}
}
}
curr++;
}
}
// Scan the heap for pointers to other blocks and mark them
static void scan_heap()
{
for (header_t *block = used_list; block != NULL; block = get_actual_pointer(block))
{
void *block_start = (void *)(block + 1);
void *block_end = (void *)((char *)block_start + (block->size - 1) * sizeof(header_t));
scan_range(block_start, block_end);
}
}
// Mark and sweep garbage collection
void gc()
{
if (!initialized)
{
initialized = 1;
get_stack_pointer();
}
printf("\nStarting garbage collection...\n");
if (stack_start == 0 || stack_end == 0 || stack_start >= stack_end)
{
printf("Invalid stack pointers, skipping GC\n");
return;
}
// Clear all marks first
for (header_t *block = used_list; block != NULL; block = get_actual_pointer(block))
{
clear_marked(block);
}
printf("Scanning stack range: %p to %p\n", (void *)stack_start, (void *)stack_end);
scan_range((void *)stack_start, (void *)stack_end);
printf("Scanning heap for pointers...\n");
scan_heap();
// Sweep phase
header_t *curr = used_list;
header_t *prev = NULL;
while (curr != NULL)
{
header_t *next = get_actual_pointer(curr);
// If the block is not marked, free it
if (!is_marked(curr))
{
if (prev == NULL)
{
used_list = next;
}
else
{
prev->next = next;
}
header_t *to_free = curr;
curr = next;
printf("Collecting block at %p (size %zu)\n", (void *)to_free, to_free->size);
add_to_free_list(to_free);
}
else
{
clear_marked(curr);
prev = curr;
curr = next;
}
}
printf("Garbage collection completed\n");
}
// Get the current stack pointer and stack range using inline assembly and pthread functions
void get_stack_pointer()
{
void *stack_ptr;
#if defined(__x86_64__)
asm volatile("movq %%rsp, %0" : "=r"(stack_ptr));
#elif defined(__i386__)
asm volatile("movl %%esp, %0" : "=r"(stack_ptr));
#endif
pthread_attr_t attrs;
if (pthread_getattr_np(pthread_self(), &attrs) != 0)
{
fprintf(stderr, "Failed to get thread attributes\n");
return;
}
void *stack_addr;
size_t stack_size;
if (pthread_attr_getstack(&attrs, &stack_addr, &stack_size) != 0)
{
fprintf(stderr, "Failed to get stack information\n");
pthread_attr_destroy(&attrs);
return;
}
stack_start = (uintptr_t)stack_ptr;
stack_end = (uintptr_t)stack_addr + stack_size;
pthread_attr_destroy(&attrs);
printf("Current stack pointer: %p\n", stack_ptr);
printf("Stack base address: %p\n", stack_addr);
printf("Stack top address: %p\n", (void *)((char *)stack_addr + stack_size));
printf("Stack size: %zu bytes\n", stack_size);
}
void gc_destroy()
{
header_t *curr = used_list;
while (curr != NULL)
{
header_t *next = get_actual_pointer(curr);
printf("Freeing block at %p (size %zu)\n", (void *)curr, curr->size);
curr = next;
}
used_list = NULL;
curr = free_list;
while (curr != &empty)
{
header_t *next = curr->next;
printf("Freeing block at %p (size %zu)\n", (void *)curr, curr->size);
curr = next;
}
free_list = ∅
printf("All memory freed\n");
}