Requires >= g++-9
$ make all
Creates 8 libraries that can be linked to a program to replace the default memory allocator.
libllheap.o
statically-linkable allocator with optimal performance without statistics or debugging.libllheap-debug.o
statically-linkable allocator with debugging.libllheap-stats.o
statically-linkable allocator with statistics.libllheap-stats-debug.o
statically-linkable allocator with debugging and statistics.libllheap.so
dynamically-linkable allocator with optimal performance without statistics or debugging.libllheap-debug.so
dynamically-linkable allocator with debugging.libllheap-stats.so
dynamically-linkable allocator with statistics.libllheap-stats-debug.so
dynamically-linkable allocator with debugging and statistics.
The Makefile has building options.
__FASTLOOKUP__
(default) use O(1) table lookup from allocation size to bucket size for small allocations.__OWNERSHIP__
(default) return freed memory to owner thread.__RETURNSPIN__
(not default) use spinlock for mutual exclusion versus lockfree stack.__NULL_0_ALLOC__
(default) return an allocation addresses for a 0-sized allocation rather than a null pointer.
llheap is a fast concurrent (heap-per-thread) memory allocator for managed programing languages with low latency at the cost of a slightly larger memory footprints. The implementation provides the following GNUC library routines.
void * malloc( size_t size );
void * calloc( size_t dimension, size_t size );
void * realloc( void * oaddr, size_t size );
void * reallocarray( void * oaddr, size_t dimension, size_t size );
void free( void * addr );
void * memalign( size_t alignment, size_t size );
void * aligned_alloc( size_t alignment, size_t size );
int posix_memalign( void ** memptr, size_t alignment, size_t size );
void * valloc( size_t size );
void * pvalloc( size_t size );
int mallopt( int option, int value );
size_t malloc_usable_size( void * addr );
void malloc_stats( void );
int malloc_info( int options, FILE * fp );
Unsupported routines.
struct mallinfo mallinfo( void );
int malloc_trim( size_t );
void * malloc_get_state( void );
int malloc_set_state( void * );
The llheap objectives are:
- thread-safe,
- fast concurrent allocation/free with or without statistics/debugging,
- making zero-fill and alignment sticky properties preserved by realloc,
- extend semantics of existing allocator operations and provide new operations to simplify allocation and increase safety,
- achieve performance comparable to the best allocators in common use.
malloc
remembers the original allocation size separate from the actual allocation size.calloc
sets the sticky zero-fill property.memalign
,aligned_alloc
,posix_memalign
,valloc
andpvalloc
set the sticky alignment property, remembering the specified alignment size.realloc
andreallocarray
preserve sticky properties across copying.malloc_stats
prints detailed statistics of allocation/free operations when linked with a statistic version.- Existence of shell variable
MALLOC_STATS
implicitly callsmalloc_stats
at program termination.
The following allocation features are available with llheap.
extends calloc
for dynamic array allocation without zero-filling the memory (faster than calloc
).
Parameters:
dimension
: number of array objectselemSize
: size of array object
Return: address of the dynamic array or NULL if allocation fails.
extends realloc
for resizing an allocation without copying previous data into the new allocation (faster than realloc
).
No sticky properties are preserved.
Parameters:
oaddr
: address to be resizedsize
: new allocation size (smaller or larger than previous)
Return: address of the old or new storage with the specified new size or NULL if size is zero.
extends resize
for an array allocation (faster than reallocarray
).
No sticky properties are preserved.
Parameters:
oaddr
: address to be resizeddimension
: number of array objectselemSize
: size of array object
Return: address of the old or new storage with the specified new size or NULL if size is zero.
extends aalloc
with an alignment.
Sets sticky alignment property.
Parameters:
alignment
: alignmentdimension
: number of array objectselemSize
: size of array object
Return: address of the aligned dynamic array or NULL if either dimension
or elemSize
are zero.
extends amemalign
with zero fill.
Sets sticky zero-fill and alignment property.
Parameters:
alignment
: alignmentdimension
: number of array objectselemSize
: size of array object
Return: address of the aligned, zero-filled dynamic array or NULL if either dimension or elemSize
are zero.
extends resize
with an alignment.
No sticky properties are preserved.
Parameters:
oaddr
: address to be resizednalignment
: new alignmentsize
: new allocation size (smaller or larger than previous)
Return: address of the aligned old or new storage with the specified new size or NULL if size is zero.
extends resizearray
with an alignment.
No sticky properties are preserved.
oaddr
: address to be resizednalignment
: new alignmentdimension
: number of array objectselemSize
: new size of array object (smaller or larger than previous)
Return: address of the aligned old or new storage with the specified new size or NULL if the resize fails.
extends realloc
with an alignment.
All sticky properties are preserved.
Parameters:
oaddr
: address to be resizednalignment
: new alignmentsize
: new allocation size (smaller or larger than previous)
Return: address of the aligned old or new storage with the specified new size or NULL if size is zero.
extends resizearray
with an alignment.
All sticky properties are preserved.
oaddr
: address to be resizednalignment
: new alignmentdimension
: number of array objectselemSize
: new size of array object (smaller or larger than previous)
Return: address of the aligned old or new storage with the specified new size or NULL if the resize fails.
These routines are called once during llheap startup to set specific limits before an application starts. Setting these value early is essential because allocations can occur from the dynamic loader and other libraries before application code executes. To set a value, define a specific routine in an application and return the desired value, e.g.:
size_t malloc_extend() { return 16 * 1024 * 1024; }
return the number of bytes to extend the sbrk
area when there is insufficient free storage to service an allocation request.
Return: heap extension size used throughout a program.
return the crossover allocation size from the sbrk
area to separate mapped areas.
Can be changed dynamically with mallopt
and M_MMAP_THRESHOLD
.
Return: crossover point used throughout a program.
return the amount subtracted from the global unfreed program storage to adjust for unreleased storage from routines like printf
(debug only).
Return: new subtraction amount and called by malloc_stats
.
returns the requested size of a dynamic object, which is updated when an object is resized. See also malloc_usable_size
.
Parameters:
addr
: address of allocated object.
Return: request size or zero if addr
is NULL.
returns the object alignment, where the minimal alignment is 16 bytes.
Parameters:
addr
: address of an allocated object.
Return: alignment of the given object, where objects not allocated with alignment return the minimal allocation alignment.
returns if the object is zero filled.
Parameters:
addr
: address of an allocated object.
Return: true if the zero-fill sticky property is set and false otherwise.
returns if the object is from a remote heap (OWNERSHIP
only).
Parameters:
addr
: address of an allocated object.
Return: true if the object belongs to another heap and false otherwise.
set the file descriptor for malloc_stats
writes (default stdout
).
Return: previous file descriptor
clear the statistics counters for all thread heaps.
extends malloc_stats
to only print statistics for the heap associated with the executing thread.