Glibc中realloc流程分析
2018-04-20
这只是个简单的记录。其实使用流程图更好一些,但是我懒得做了。
void *realloc(void *ptr, int bytes);
void *newp;
int nb = request2size(bytes);
int oldsize = chunksize(ptr);
- if(bytes == 0)
- free(oldp)
- return(0)
- if(ptr == NULL)
- newp = malloc(bytes)
- return(newp)
- else
- if(oldsize >= nb)
- GOTO:Split
- if(use_topbin)
- split(top)
- merge(ptr, top)
- return(newp)
- if(!inuse(next_chunk))
- unlink(next_chunk)
- merge(ptr, next_chunk)
- GOTO:Split
- other
- newp = malloc(nb -
MALLOC_ALIGN_MASK
) - if(newp == next_chunk)
- GOTO:Split
- memcpy(ptr, newp, size)
- free(ptr)
- return(newp)
- newp = malloc(nb -
- if(oldsize >= nb)
- Split
- if(remainder <
MIN_SIZE
)- set_head(newp)
- return(newp)
- else
- split(remainder)
- set_head(remainder)
- set_head(newp)
- free(remainder)
- return(newp)
- if(remainder <