void heap_init(size_t max_heap_size) { #ifdef DEBUG printf("allocate heap size == %d\n", max_heap_size); printf("sizeof(Busy_Header) == %zu\n", sizeof(Busy_Header)); printf("sizeof(Free_Header) == %zu\n", sizeof(Free_Header)); printf("BUSY_BIT == %x\n", BUSY_BIT); printf("SIZEMASK == %x\n", SIZEMASK); #endif if ( heap!=NULL ) heap_shutdown(); heap_size = (int)max_heap_size; heap = morecore(max_heap_size); #ifdef DEBUG if ( heap==NULL ) { fprintf(stderr, "Cannot allocate %zu bytes of memory for heap\n", max_heap_size + sizeof(Busy_Header)); } else { fprintf(stderr, "morecore returns %p\n", heap); } #endif freelist = heap; freelist->size = (uint32_t)max_heap_size & SIZEMASK; // mask off upper bit to say free freelist->next = NULL; }
static void teardown() { verify_heap(); heap_shutdown(); }