Refactor allocator to use offsets instead of absolute pointers
JLANG_malloc now returns size_t offsets relative to the heap buffer, making all references stable across heap growth (realloc). Object API updated accordingly: constructors return offsets, obj_print/obj_free receive (allocator, offset). Added heap auto-grow when out of space.
This commit is contained in:
20
src/main.c
20
src/main.c
@@ -7,24 +7,18 @@ int main() {
|
||||
printf("memoryPtr=%p\n", allocPtr->memory);
|
||||
printf("size=%zu\n", allocPtr->size);
|
||||
|
||||
// Create object
|
||||
Object *intVar1 = obj_new_int(allocPtr, 66);
|
||||
obj_print(intVar1);
|
||||
|
||||
Object *stringVar1 = obj_new_string(allocPtr, "\nHello world!\n");
|
||||
obj_print(stringVar1);
|
||||
size_t stringVar1 = obj_new_string(allocPtr, "\nHello world!\n");
|
||||
obj_print(allocPtr, stringVar1);
|
||||
|
||||
JLANG_visualize(allocPtr);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Object *intVar1 = obj_new_int(allocPtr, 66);
|
||||
obj_print(intVar1);
|
||||
// obj_free(allocPtr, stringVar1);
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
obj_new_string(allocPtr, "FFFFFFFFFFFFFF");
|
||||
}
|
||||
|
||||
obj_free(allocPtr, intVar1);
|
||||
void* newPtr = JLANG_malloc(allocPtr, 8);
|
||||
|
||||
obj_print(stringVar1);
|
||||
obj_print(allocPtr, stringVar1);
|
||||
JLANG_visualize(allocPtr);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user