void tfree(void *vp) { CHUNK *p, *q; if (vp == NULL) return; p = TOCHUNK(vp); CLR_FREEBIT(p); q = p->s.l; if (q != NULL && GET_FREEBIT(q)) /* try to consolidate leftward */ { CLR_FREEBIT(q); q->s.r = p->s.r; p->s.r->s.l = q; SET_FREEBIT(q); p = q; } q = RIGHT(p); if (q != NULL && GET_FREEBIT(q)) /* try to consolidate rightward */ { CLR_FREEBIT(q); p->s.r = q->s.r; q->s.r->s.l = p; SET_FREEBIT(q); } SET_FREEBIT(p); }
void *tmalloc(unsigned nbytes) { CHUNK *p; unsigned size; if (bot == NULL) init(); size = sizeof(CHUNK) * ((nbytes+sizeof(CHUNK)-1)/sizeof(CHUNK) + 1); for (p = bot; p != NULL; p = RIGHT(p)) if (GET_FREEBIT(p) && CHUNKSIZE(p) >= size) break; if (p == NULL) return NULL; if (CHUNKSIZE(p) > size) /* create a remainder chunk */ { CHUNK *q, *pr; CLR_FREEBIT(p); /* required for pointer manipulation */ q = (CHUNK *)((char *)p + CHUNKSIZE(p) - size); pr = p->s.r; q->s.l = p; q->s.r = pr; p->s.r = q; pr->s.l = q; SET_FREEBIT(p); p = q; /* return q, not the remainder */ } CLR_FREEBIT(p); //what's going on here? return FROMCHUNK(p); }
void *tmalloc(unsigned nbytes) { CHUNK *p; unsigned size; if (bot == NULL) init(); size = sizeof(CHUNK) * ((nbytes+sizeof(CHUNK)-1)/sizeof(CHUNK) + 1); for (p = bot; p != NULL; p = RIGHT(p)) if (GET_FREEBIT(p) && CHUNKSIZE(p) >= size) break; if (p == NULL) return NULL; CLR_FREEBIT(p); if (CHUNKSIZE(p) > size) /* create a remainder chunk */ { CHUNK *q, *pr; q = (CHUNK *)(size + (char *)p); pr = p->s.r; q->s.l = p; q->s.r = pr; p->s.r = q; pr->s.l = q; SET_FREEBIT(q); } return FROMCHUNK(p); }