Skip to content

Instantly share code, notes, and snippets.

@saidelike
Last active July 6, 2021 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saidelike/602814f680a67ff2c4d509231ff0019c to your computer and use it in GitHub Desktop.
Save saidelike/602814f680a67ff2c4d509231ff0019c to your computer and use it in GitHub Desktop.
//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