malloc, mallocalign, mallocz, free, realloc, calloc, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, malloctopoolblock – memory allocator
void* malloc(ulong size)
void* mallocalign(ulong size, ulong align, long offset, ulong span)
void* mallocz(ulong size, int clr)
void free(void *ptr)
void* realloc(void *ptr, ulong size)
void* calloc(ulong nelem, ulong elsize)
ulong msize(void *ptr)
void setmalloctag(void *ptr, uintptr tag)
uintptr getmalloctag(void *ptr)
void setrealloctag(void *ptr, uintptr tag)
uintptr getrealloctag(void *ptr)
provide a simple memory allocation package.
returns a pointer to a new block of at least
The block is suitably aligned for storage of any type of object.
No two active pointers from
will have the same value.
returns a valid pointer rather than null.
The argument to
is a pointer to a block previously allocated by
this space is made available for further allocation.
It is legal to free a null pointer; the effect is a no-op.
The contents of the space returned by
except that if
is non-zero, the memory returned will be zeroed.
allocates a block of at least
bytes of memory respecting alignment contraints.
the returned pointer is aligned to be equal to
byte block allocated will not span a
changes the size of the block pointed to by
bytes and returns a pointer to the (possibly moved)
The contents will be unchanged up to the
lesser of the new and old sizes.
takes on special meanings when one or both arguments are zero:
returns a pointer to the newly-allocated memory
no-op; returns null
allocates space for
an array of
elements of size
The space is initialized to zeros.
frees such a block.
When a block is allocated, sometimes there is some extra unused space at the end.
grows the block to encompass this unused space and returns the new number
of bytes that may be used.
The memory allocator maintains two word-sized fields
associated with each block, the “malloc tag” and the “realloc tag”.
By convention, the malloc tag is the PC that allocated the block,
and the realloc tag the PC that last reallocated the block.
These may be set or examined with
When allocating blocks directly with
these tags will be set properly.
If a custom allocator wrapper is used,
the allocator wrapper can set the tags
itself (usually by passing the result of
to provide more useful information about
the source of allocation.
takes the address of a block returned by
and returns the address of the corresponding
block allocated by the
return 0 if there is no available memory.
is likely to be set.
If the allocated blocks have no malloc or realloc tags,
can be used to scan the storage arena for inconsistencies
such as data written beyond the bounds of allocated blocks.
It is often useful to combine this with setting
mainmem->flags |= POOL_NOREUSE;
at the beginning of your program.
This will cause malloc not to reallocate blocks even
once they are freed;
will then detect writes to freed blocks.
can be used to obtain traces of malloc execution; see
The different specification of
User errors can corrupt the storage arena.
The most common gaffes are (1) freeing an already freed block,
(2) storing beyond the bounds of an allocated block, and (3)
freeing data that was not obtained from the allocator.
detect such corruption, they abort.