void mm_free_nolock(MM *mm, void *x) {
if (x != NULL) {
if (x >= mm->start && x < (void *)((char *)mm + mm->size)) {
mm_mem_head *p = PTR_TO_HEAD(x);
size_t size = p->size;
if ((char *)p+size <= (char *)mm + mm->size) {
mm_free_bucket *b = (mm_free_bucket *)p;
b->next = NULL;
if (mm->free_list == NULL) {
mm->free_list = b;
} else {
mm_free_bucket *q = mm->free_list;
mm_free_bucket *prev = NULL;
mm_free_bucket *next = NULL;
while (q != NULL) {
if (b < q) {
next = q;
break;
}
prev = q;
q = q->next;
}
if (prev != NULL && (char *)prev+prev->size == (char *)b) {
if ((char *)next == (char *)b+size) {
/* merging with prev and next */
prev->size += size + next->size;
prev->next = next->next;
} else {
/* merging with prev */
prev->size += size;
}
} else {
if ((char *)next == (char *)b+size) {
/* merging with next */
b->size += next->size;
b->next = next->next;
} else {
/* don't merge */
b->next = next;
}
if (prev != NULL) {
prev->next = b;
} else {
mm->free_list = b;
}
}
}
mm->available += size;
}
}
}
时间:2009-06-13 20:29
来源:chinaunix
作者:hightman
原文链接