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.

C
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.

C
void* qmckl_malloc(qmckl_context context,
                   const qmckl_memory_info_struct info);

The steps of qmckl_malloc are:

  1. The function takes two parameters: a qmckl_context and a qmckl_memory_info_struct containing the desired size of the memory block to allocate.
  2. The function asserts that the provided qmckl_context is valid, with qmckl_context_check, and retrieves the qmckl_context_struct pointer from it. The assertion disappears when the library is compiled with NDEBUG.
  3. A request of size zero returns NULL at once: the allocation table marks a free slot with a zero size, so such a block could not be tracked.
  4. The memory is allocated with posix_memalign and an alignment of 64 bytes when HAVE_HPC and HAVE_POSIX_MEMALIGN are defined, and with malloc otherwise. If the allocation fails, the function returns NULL.
  5. The block is zeroed with memset.
  6. The function takes the lock of the context.
  7. If the allocation table is full, it doubles the table with realloc, zeroes the new half and updates array_size. If that reallocation fails, the lock is released, the block is freed and NULL is returned.
  8. The function scans the table for the first free slot, stores the pointer and the size there, and increments n_allocated.
  9. 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.

C
qmckl_exit_code qmckl_free(qmckl_context context,
                           void * const ptr);

Here's a step-by-step explanation of the qmckl_free function:

  1. The function takes two parameters: a qmckl_context and a pointer to the memory block (ptr) that needs to be deallocated.
  2. The function checks if the provided qmckl_context is valid, using the qmckl_context_check function. If it is not valid, it returns an error code QMCKL_INVALID_CONTEXT using the qmckl_failwith function.
  3. The function checks if the provided pointer is NULL. If it is, it returns an error code QMCKL_INVALID_ARG_2 using the qmckl_failwith function.
  4. The qmckl_context_struct pointer is retrieved from the provided qmckl_context.
  5. The function acquires a lock on the qmckl_context using qmckl_lock.
  6. Inside the locked section, the function searches for the pointer in the element array of the qmckl_memory_struct.
  7. If the pointer is not found in the array, it releases the lock and returns an error code QMCKL_INVALID_ARG_2 using the qmckl_failwith function.
  8. If the pointer is found, the memory block is deallocated using the standard free function.
  9. The qmckl_memory_info_struct at the found position is reset to qmckl_memory_info_struct_zero. This marks the slot as available for future allocations.
  1. The n_allocated member of the qmckl_memory_struct is decremented by one, as the memory block has been deallocated.
  2. The function releases the lock on the qmckl_context using qmckl_unlock.
  3. Finally, the function returns QMCKL_SUCCESS to 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.

C
qmckl_exit_code
qmckl_get_malloc_info(qmckl_context context,
                      const void* pointer, 
                      qmckl_memory_info_struct* info);