// Macro is evil, inline function is more reliable. static inline void insert_node(int level, void * bp) { char **flist_head = get_head(level); char **flist_tail = get_tail(level); if (!(*flist_head)) { // empty list *flist_head = bp; *flist_tail = bp; set_prev_free(bp, NULL); set_next_free(bp, NULL); } else { if ((char *)bp < (*flist_head)) { // insert at head set_prev_free(*flist_head, bp); set_next_free(bp, *flist_head); set_prev_free(bp, NULL); *flist_head = bp; } else if ((*flist_tail) < (char *)bp) { // insert to tail set_next_free(*flist_tail, bp); set_prev_free(bp, *flist_tail); set_next_free(bp, NULL); *flist_tail = bp; } else { // find some place in the list char * c = *flist_head; while (c < (char *)bp) { c = next_free(c); } set_next_free(prev_free(c), bp); set_prev_free(bp, prev_free(c)); set_prev_free(c, bp); set_next_free(bp, c); } } }
//--------------------------------------------------------------------------// //--------------------------------------------------------------------------// void MemPool::free(MemAddr_t addr){ if( !m_data ){ fprintf(stderr, "alloc(): Memory pool not initialized.\n"); return; } FreeList::iterator prev=prev_free(addr); if( m_data+prev->off == addr ){ // The previous free touches the current block - merge. } }
static inline void delete_node(int level, void * bp) { char **flist_head = get_head(level); char **flist_tail = get_tail(level); if (bp == *flist_head) { *flist_head = next_free(bp); if (*flist_head) { set_prev_free(*flist_head, NULL); } else { *flist_tail = NULL; } } else if (bp == *flist_tail) { *flist_tail = prev_free(bp); if (*flist_tail) { set_next_free(*flist_tail, NULL); } else { *flist_head = NULL; } } else { set_next_free(prev_free(bp), next_free(bp)); set_prev_free(next_free(bp), prev_free(bp)); } }
static void printblock(void *bp) { size_t hsize, halloc; hsize = GET_SIZE(HDRP(bp)); halloc = GET_ALLOC(HDRP(bp)); if (hsize == 0) { printf("%p: EOL, prev_alloc: [%d]\n", bp, IS_PREV_ALLOC(HDRP(bp))); return; } if (halloc){ printf("%p: header: [%u:%c], prev_alloc: [%d]\n", bp, (unsigned)hsize, (halloc ? 'a' : 'f'), IS_PREV_ALLOC(HDRP(bp))); } else { printf("%p: header: [%u:%c], footer: [%u, %c], prev[%p], next[%p], prev_alloc: [%d]\n", bp, (unsigned)hsize, (halloc ? 'a' : 'f'), GET_SIZE(FTRP(bp)), (GET_ALLOC(FTRP(bp)) ? 'a' : 'f'), prev_free(bp), next_free(bp), IS_PREV_ALLOC(HDRP(bp))); } }