/* * 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; }
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; }
static inline size_t request2size(size_t n) { return align_to_word_boundary(size_with_header(n)); }