Memory management
1. Introduction
QMCkl takes its long-lived memory through functions that receive the context, so that every block allocated this way is registered in the context and released when the context is destroyed. Kernels that need a scratch buffer take it from the C allocator and free it before returning. The allocation functions are private to the library: no allocation function appears in the public header, and arrays cross the API as plain C pointers.
An implementation of the library is free to replace those functions, for
instance to return aligned memory, pinned memory, or device memory, without
changing the code that calls them. This one asks posix_memalign for 64-byte aligned
blocks when HAVE_HPC and HAVE_POSIX_MEMALIGN are defined, and calls
plain malloc otherwise.
As with malloc, an allocation that fails returns NULL.
Example of usage:
2. Memory data structure for the context
The QMCkl library maintains detailed information about all memory allocations made through the context. This tracking enables automatic cleanup when the context is destroyed and provides visibility into memory usage patterns.
Every time a new block of memory is allocated, the information
relative to the allocation is stored in a free qmckl_memory_info_struct
slot of the allocation table.
A qmckl_memory_info_struct holds two fields: the pointer to the memory
block and its size in bytes.
This metadata structure can be extended in specialized implementations to support additional allocation strategies or memory types without changing the public API.
typedef struct qmckl_memory_info_struct {
size_t size;
void* pointer;
} qmckl_memory_info_struct;
static const qmckl_memory_info_struct qmckl_memory_info_struct_zero =
{
.size = (size_t) 0,
.pointer = NULL
};
The memory element of the context is a data structure which
contains an array of qmckl_memory_info_struct, the size of the
array, and the number of allocated blocks. This dynamic array grows
as needed to accommodate new allocations, providing a complete registry
of all memory managed by the context.
3. Passing info to allocation routines
Allocation parameters are passed to the allocation routine in an instance
of a qmckl_memory_info_struct. This structure-based approach allows for
extensibility: new allocation parameters can be added to the structure in
future versions without changing function signatures, and the request reads
as a description of what is allocated.
4. Allocation/deallocation functions
4.1. Memory allocation
Memory allocation inside the library should be done with
qmckl_malloc. This function provides a centralized allocation mechanism
that allows the library to choose how and where memory will be allocated.
A pointer is returned to the caller, similar to standard malloc.
The context is passed to the function so that the library can store data related to the allocation inside the context. In this particular implementation of the library, we maintain a registry of allocated pointers so that all memory can be properly freed when the library is de-initialized. This prevents memory leaks and ensures clean shutdown.
If the allocation fails, the NULL pointer is returned, following standard
C conventions. Callers should always check for NULL returns and handle
allocation failures appropriately.
For security and deterministic behavior, the allocated memory block is
zeroed using memset before being returned to the caller.
A request of size zero returns NULL: the registry marks a free slot by a
zero size, so a block of size zero cannot be tracked.
void* qmckl_malloc(qmckl_context context,
const qmckl_memory_info_struct info);
The steps of qmckl_malloc are:
- The function takes two parameters: a
qmckl_contextand aqmckl_memory_info_structcontaining the desired size of the memory block to allocate. - The function asserts that the provided
qmckl_contextis valid, withqmckl_context_check, and retrieves theqmckl_context_structpointer from it. The assertion disappears when the library is compiled withNDEBUG. - A request of size zero returns
NULLat once: the allocation table marks a free slot with a zero size, so such a block could not be tracked. - The memory is allocated with
posix_memalignand an alignment of 64 bytes whenHAVE_HPCandHAVE_POSIX_MEMALIGNare defined, and withmallocotherwise. If the allocation fails, the function returnsNULL. - The block is zeroed with
memset. - The function takes the lock of the context.
- If the allocation table is full, it doubles the table with
realloc, zeroes the new half and updatesarray_size. If that reallocation fails, the lock is released, the block is freed andNULLis returned. - The function scans the table for the first free slot, stores the pointer
and the size there, and increments
n_allocated. - The lock is released and the pointer is returned.
qmckl_free takes the context because the context holds the registry of
allocations: the block is released and its entry in the registry is
cleared. Freeing a pointer the context does not know is an error, not a
silent no-op.
qmckl_exit_code qmckl_free(qmckl_context context,
void * const ptr);
Here's a step-by-step explanation of the qmckl_free function:
- The function takes two parameters: a
qmckl_contextand a pointer to the memory block (ptr) that needs to be deallocated. - The function checks if the provided
qmckl_contextis valid, using theqmckl_context_checkfunction. If it is not valid, it returns an error codeQMCKL_INVALID_CONTEXTusing theqmckl_failwithfunction. - The function checks if the provided pointer is
NULL. If it is, it returns an error codeQMCKL_INVALID_ARG_2using theqmckl_failwithfunction. - The
qmckl_context_structpointer is retrieved from the providedqmckl_context. - The function acquires a lock on the
qmckl_contextusingqmckl_lock. - Inside the locked section, the function searches for the pointer in
the element array of the
qmckl_memory_struct. - If the pointer is not found in the array, it releases the lock and
returns an error code
QMCKL_INVALID_ARG_2using theqmckl_failwithfunction. - If the pointer is found, the memory block is deallocated using the
standard
freefunction. - The
qmckl_memory_info_structat the found position is reset toqmckl_memory_info_struct_zero. This marks the slot as available for future allocations.
- The
n_allocatedmember of theqmckl_memory_structis decremented by one, as the memory block has been deallocated. - The function releases the lock on the
qmckl_contextusingqmckl_unlock. - Finally, the function returns
QMCKL_SUCCESSto indicate successful deallocation of the memory block.
5. Get the size of a memory block
The context keeps the size of every block allocated with qmckl_malloc.
qmckl_get_malloc_info returns it, and fails with QMCKL_INVALID_ARG_2
when the pointer is not registered in the context.
qmckl_exit_code
qmckl_get_malloc_info(qmckl_context context,
const void* pointer,
qmckl_memory_info_struct* info);