This is a list of some common functions used internally. Note that some functions are in fact defined using the #define
directive. So, changes to call parameters are in fact retained after the call. Also, it is assumed that MALLOC_DEBUG is not set.
Acquires an arena and locks the corresponding mutex. ar_ptr
is set to point to the corresponding arena. size
is just a hint as to how much memory will be required immediately.
/*
sysmalloc handles malloc cases requiring more memory from the system.
On entry, it is assumed that av->top does not have enough
space to service request for nb bytes, thus requiring that av->top
be extended or replaced.
*
If perturb_byte
(tunable parameter for malloc using M_PERTURB
) is non-zero(by default it is 0), sets the n
bytes pointed to by p
to be equal to perturb_byte
^ 0xff.
If perturb_byte
(tunable parameter for malloc using M_PERTURB
) is non-zero(by default it is 0), sets the n
bytes pointed to by p
to be equal to perturb_byte
.
/*
Initialize a malloc_state struct.
This is called only from within malloc_consolidate, which needs
be called in the same contexts anyway. It is never called directly
outside of malloc_consolidate because some optimizing compilers try
to inline it at all call points, which turns out not to be an
optimization at all. (Inlining it in malloc_consolidate is fine though.)
*/
- For non fast bins, create empty circular linked lists for each bin.
- Set
FASTCHUNKS_BIT
flag forav
. - Initialize
av->top
to the first unsorted chunk.
This is a defined macro which removes a chunk from a bin.
- Check if chunk size is equal to the previous size set in the next chunk. Else, an error("corrupted size vs. prev_size") is thrown.
- Check if
P->fd->bk == P
andP->bk->fd == P
. Else, an error("corrupted double-linked list") is thrown. - Adjust forward and backward pointers of neighoring chunks(in list) to facilitate removal:
- Set
P->fd->bk
=P->bk
. - Set
P->bk->fd
=P->fd
.
- Set
This is a specialized version of free().
- Chech if
global_max_fast
is 0(av
not initialized) or not. If it is 0, callmalloc_init_state
withav
as parameter and return. - If
global_max_fast
is non-zero, clear theFASTCHUNKS_BIT
forav
. - Iterate on the fastbin array from first to last indices:
- Get a lock on the current fastbin chunk and proceed if not null.
- If previous chunk(by memory) is not in use, call
unlink
on the previous chunk. - If next chunk(by memory) is not top chunk:
- If next chunk is not in use, call
unlink
on the next chunk. - Merge the chunk with previous, next(by memory), if any is free, and then add the consolidated chunk to the head of unsorted bin.
- If next chunk is not in use, call
- If next chunk(by memory) was a top chunk, merge the chunks appropriately into a single top chunk.
Note: The check for 'in use' is done using PREV_IN_USE
flag. Hence, other fastbin chunks won't identified as free here.