Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//glibc_2.17-322.el7_9/malloc/malloc.c
/* Set size/use field */
#define set_head(p, s) ((p)->size = (s))
static void*
_int_malloc(mstate av, size_t bytes)
{
...
use_top:
/*
If large enough, split off the chunk bordering the end of memory
(held in av->top). Note that this is in accord with the best-fit
search rule. In effect, av->top is treated as larger (and thus
less well fitting) than any other available chunk since it can
be extended to be as large as necessary (up to system
limitations).
We require that av->top always exists (i.e., has size >=
MINSIZE) after initialization, so if it would otherwise be
exhausted by current request, it is replenished. (The main
reason for ensuring it exists is that we may need MINSIZE space
to put in fenceposts in sysmalloc.)
*/
victim = av->top;
size = chunksize(victim);
if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
remainder_size = size - nb;
remainder = chunk_at_offset(victim, nb);
av->top = remainder;
set_head(victim, nb | PREV_INUSE |
(av != &main_arena ? NON_MAIN_ARENA : 0));
[c2] set_head(remainder, remainder_size | PREV_INUSE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment