Internal Functions
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.
# arena_get (ar_ptr, size)
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 [TODO]
| |
# void alloc_perturb (char *p, size_t n)
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.
# void free_perturb (char *p, size_t n)
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.
# void malloc_init_state (mstate av)
| |
- For non fast bins, create empty circular linked lists for each bin.
- Set
FASTCHUNKS_BITflag forav. - Initialize
av->topto the first unsorted chunk.
# unlink(AV, P, BK, FD)
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 == PandP->bk->fd == P. Else, an error (“corrupted double-linked list”) is thrown. - Adjust forward and backward pointers of neighboring chunks (in list) to facilitate removal:
- Set
P->fd->bk=P->bk. - Set
P->bk->fd=P->fd.
- Set
# void malloc_consolidate(mstate av)
This is a specialized version of free().
- Chech if
global_max_fastis 0 (avnot initialized) or not. If it is 0, callmalloc_init_statewithavas parameter and return. - If
global_max_fastis non-zero, clear theFASTCHUNKS_BITforav. - 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
unlinkon the previous chunk. - If next chunk (by memory) is not top chunk:
- If next chunk is not in use, call
unlinkon 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.