From: Michael D. Lowis Date: Tue, 21 Apr 2015 02:26:11 +0000 (-0400) Subject: updated heap.c to increase the slot count to account for the object header X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=c634437d12c1060d54a98a4f33d20cf280367d3d;p=archive%2Fatc.git updated heap.c to increase the slot count to account for the object header --- diff --git a/source/runtime/gc.c b/source/runtime/gc.c index a21f909..a5dd555 100644 --- a/source/runtime/gc.c +++ b/source/runtime/gc.c @@ -4,11 +4,6 @@ #include #include -//typedef struct { -// uint64_t objmap; -// uint8_t data[]; -//} obj_t; - typedef struct root_t { struct root_t* next; uintptr_t* address; @@ -41,20 +36,14 @@ void gc_add_root(void* address, size_t size) void* gc_object(uint64_t objmap, size_t num_slots) { - (void)objmap; - (void)num_slots; - return NULL; -// obj_t* obj = heap_allocate(Heap, num_slots+1); -// obj->objmap = objmap; -// obj++; -// return obj; + return heap_allocate(Heap, objmap, num_slots); } void* gc_allocate(size_t size) { size_t slot_sz = sizeof(uintptr_t); size_t num_slots = (size + slot_sz - 1) / slot_sz; - return heap_allocate(Heap, UINT16_MAX, num_slots + 1); + return heap_allocate(Heap, UINT16_MAX, num_slots); } static void gc_scan_object(void* object) { diff --git a/source/runtime/heap.c b/source/runtime/heap.c index cc72f5c..86a14ef 100644 --- a/source/runtime/heap.c +++ b/source/runtime/heap.c @@ -87,8 +87,8 @@ static void* allocate_large_block(heap_t* heap, uint64_t ptrmap, uintptr_t num_s void* heap_allocate(heap_t* heap, uint64_t ptrmap, uintptr_t num_slots) { void* obj = NULL; - if (num_slots <= MAX_NUM_SLOTS) { - obj = allocate_small_block(heap, ptrmap, num_slots); + if (num_slots+1 <= MAX_NUM_SLOTS) { + obj = allocate_small_block(heap, ptrmap, num_slots+1); } else { obj = allocate_large_block(heap, ptrmap, num_slots); } diff --git a/tests/test_heap.c b/tests/test_heap.c index b43fc46..2f47828 100644 --- a/tests/test_heap.c +++ b/tests/test_heap.c @@ -19,19 +19,19 @@ TEST_SUITE(Heap) { TEST(Verify_Allocate_should_allocate_a_new_segment_if_the_subheap_is_empty) { heap_t* heap = heap_create(); CHECK(NULL != heap_allocate(heap, 0, 1)); - CHECK(heap->heaps[0].available != NULL); + CHECK(heap->heaps[1].available != NULL); heap_destroy(heap); } TEST(Verify_Allocate_should_allocate_a_new_segment_if_the_current_segment_is_full) { heap_t* heap = heap_create(); CHECK(NULL != heap_allocate(heap, 0, 1)); - heap->heaps[0].available->blockmap[0] = 1 << ((sizeof(uint16_t) * 8) - 1); - heap->heaps[0].available->blockmap[16] = 1 << ((sizeof(uint16_t) * 8) - 1); + heap->heaps[1].available->blockmap[0] = 1 << ((sizeof(uint16_t) * 8) - 1); + heap->heaps[1].available->blockmap[16] = 1 << ((sizeof(uint16_t) * 8) - 1); CHECK(NULL != heap_allocate(heap, 0, 1)); - CHECK(heap->heaps[0].available == NULL); - CHECK(heap->heaps[0].full != NULL); - CHECK(heap->heaps[0].full->next == NULL); + CHECK(heap->heaps[1].available == NULL); + CHECK(heap->heaps[1].full != NULL); + CHECK(heap->heaps[1].full->next == NULL); heap_destroy(heap); }