]> git.mdlowis.com Git - archive/atc.git/commitdiff
updated heap.c to increase the slot count to account for the object header
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Apr 2015 02:26:11 +0000 (22:26 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Apr 2015 02:26:11 +0000 (22:26 -0400)
source/runtime/gc.c
source/runtime/heap.c
tests/test_heap.c

index a21f90972eae0ae5ba2cf3a6ab0b1499112e27a2..a5dd55598ba17f0fd6210f3d99d269f40019a90b 100644 (file)
@@ -4,11 +4,6 @@
 #include <stdlib.h>
 #include <setjmp.h>
 
-//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) {
index cc72f5c9cd3faf187e06d1beec26c6a3c0ad3a82..86a14ef441f3c4c0489a25348a550cb1c8c79833 100644 (file)
@@ -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);
     }
index b43fc46a03fa6f2a8f146318909e7c7f3ab78808..2f47828a2b1aee8902bdc3c612027e5b072109bf 100644 (file)
@@ -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);
     }