/* * each malloc request first check size, if size >1024 ,get from free list and return * else check if have fitted size in bin[size-1],if yes, return * if not, check if can get from free list,if yes, return (may be need split) * if not, check if there exist next free chunk in bin[biggersize],if yes, split and return * if not, return NULL, out of heap error */ void *malloc(size_t size) { uint32_t n = (uint32_t) size & SIZEMASK; size_t actual_size =(uint32_t) align_to_word_boundary(size_with_header(n)); Busy_Header *b; if (actual_size >BIN_SIZE) { Free_Header *q = freelist_malloc(actual_size); if (q == NULL) { #ifdef DEBUG printf("out of heap"); #endif return NULL; } b = (Busy_Header *)q; b->size |= BUSY_BIT; return b; } else { Free_Header *q = next_small_free(actual_size); if (q == NULL) { #ifdef DEBUG printf("out of heap"); #endif return NULL; } b = (Busy_Header *)q; b->size |= BUSY_BIT; return b; } return NULL; }
//Allocation heap_object *gc_alloc(object_metadata *metadata, size_t size) { if ( start_of_heap==NULL ) { gc_init(DEFAULT_MAX_HEAP_SIZE); } size = align_to_word_boundary(size); heap_object *p = gc_raw_alloc(size); memset(p, 0, size); p->metadata = metadata; p->size = (uint32_t)size; return p; }
void *malloc(size_t size) { if ( heap==NULL ) { heap_init(DEFAULT_MAX_HEAP_SIZE); } if (freelist == NULL) { return NULL; // out of heap } uint32_t n = (uint32_t) size & SIZEMASK; n = (uint32_t) align_to_word_boundary(size_with_header(n)); Free_Header *chunk = nextfree(n); Busy_Header *b = (Busy_Header *) chunk; b->size |= BUSY_BIT; // get busy! turn on busy bit at top of size field return b; }
/* Allocate an object per the indicated size, which must included heap_object / header info. * The object is zeroed out and the header is initialized. */ heap_object *gc_alloc(object_metadata *metadata, size_t size) { if (heap == NULL ) { gc_init(DEFAULT_MAX_HEAP_SIZE); } size = align_to_word_boundary(size); heap_object *p = gc_raw_alloc(size); if ( p==NULL ) return NULL; memset(p, 0, size); // wipe out object's data space and the header p->magic = MAGIC_NUMBER; // a safety measure; if not magic num, we didn't alloc p->metadata = metadata; // make sure object knows where its metadata is p->size = (uint32_t)size; return p; }
static inline size_t request2size(size_t n) { return align_to_word_boundary(size_with_header(n)); }