ABI Contract
compatmalloc exports every symbol that a glibc-linked program may reference for dynamic memory management. This page documents each exported function, its parameters, return values, and error behavior.
All functions use the C ABI (extern "C").
Standard C allocator
malloc
void *malloc(size_t size);
Allocate size bytes of uninitialized memory.
- Parameters:
size-- number of bytes to allocate. - Returns: Pointer to the allocated memory, aligned to at least 16 bytes. Returns
NULLon failure (anderrnois set toENOMEMby the underlying mapping). - Special case:
malloc(0)returns a valid, unique, non-NULLpointer (to a 1-byte internal allocation). This pointer must be passed tofree().
free
void free(void *ptr);
Release memory previously returned by malloc, calloc, realloc, or an alignment function.
- Parameters:
ptr-- pointer to free. IfNULL, the call is a no-op. - Behavior on double-free: If the
write-after-free-checkfeature is enabled and the metadata table detects the pointer was already freed, the allocator writes a diagnostic to stderr and callsabort(). - Behavior on invalid pointer: Pointers not recognized by any arena or the large allocator are silently ignored for compatibility.
realloc
void *realloc(void *ptr, size_t size);
Change the size of a previously allocated block.
- Parameters:
ptr-- pointer to the existing allocation. IfNULL, behaves likemalloc(size).size-- new size in bytes. If0, behaves likefree(ptr)and returnsNULL.
- Returns: Pointer to the resized block (may differ from
ptr). ReturnsNULLon failure; the original block is left unchanged. - Copy behavior: When a new block is allocated,
min(old_size, new_size)bytes are copied from the old block.
calloc
void *calloc(size_t nmemb, size_t size);
Allocate zeroed memory for an array.
- Parameters:
nmemb-- number of elements.size-- size of each element.
- Returns: Pointer to zero-initialized memory. Returns
NULLif the multiplicationnmemb * sizeoverflows or if allocation fails. - Overflow protection: Uses
checked_mulinternally. On overflow, setserrnotoENOMEMand returnsNULL.
POSIX alignment APIs
posix_memalign
int posix_memalign(void **memptr, size_t alignment, size_t size);
Allocate memory with a specified alignment (POSIX).
- Parameters:
memptr-- output pointer. Must not beNULL.alignment-- must be a power of two and at leastsizeof(void *)(8 bytes on 64-bit).size-- number of bytes.
- Returns:
0on success (pointer stored in*memptr).EINVALifmemptrisNULLoralignmentis invalid.ENOMEMif allocation fails.
aligned_alloc
void *aligned_alloc(size_t alignment, size_t size);
Allocate memory with a specified alignment (C11).
- Parameters:
alignment-- must be a power of two.size-- must be a multiple ofalignment(unlesssizeis0).
- Returns: Aligned pointer, or
NULLon failure (witherrnoset toEINVALorENOMEM).
memalign
void *memalign(size_t alignment, size_t size);
Allocate memory with a specified alignment (legacy).
- Parameters:
alignment-- must be a power of two.size-- number of bytes.
- Returns: Aligned pointer, or
NULLon failure.
valloc
void *valloc(size_t size);
Allocate page-aligned memory.
- Parameters:
size-- number of bytes. - Returns: Pointer aligned to the system page size (4096 bytes), or
NULLon failure.
pvalloc
void *pvalloc(size_t size);
Allocate page-aligned memory, rounding the size up to a page boundary.
- Parameters:
size-- number of bytes (rounded up to the next multiple of the page size). - Returns: Page-aligned pointer, or
NULLon failure.
GNU extensions
malloc_usable_size
size_t malloc_usable_size(void *ptr);
Return the usable size of an allocation.
- Parameters:
ptr-- pointer returned by an allocator function. IfNULL, returns0. - Returns: The number of usable bytes in the allocation. This may be larger than the originally requested size (due to size-class rounding) but programs must not rely on the excess bytes persisting across
realloc.
mallopt
int mallopt(int param, int value);
Set allocator tuning parameters.
- Behavior: Accepts all calls and returns
1(success) but performs no action. Provided solely for binary compatibility with programs that callmallopt.
mallinfo / mallinfo2
struct mallinfo mallinfo(void);
struct mallinfo2 mallinfo2(void); // Linux only
Return allocator statistics.
- Behavior: Returns a zeroed struct. Provided solely for binary compatibility.
mallinfo2is only exported on Linux targets.
Minimum alignment
All allocations are aligned to at least 16 bytes (MIN_ALIGN), which matches max_align_t on 64-bit Linux. This guarantees correct alignment for any C data type.