libmemalloc
v4.0.00
Modern Memory Allocator
|
Core memory management components for libmemalloc. More...
Files | |
file | libmemalloc.c |
Core memory management components for libmemalloc. | |
file | libmemalloc.h |
Classes | |
struct | block_header_t |
Represents the header for a memory block. More... | |
struct | mem_arena_t |
Represents a memory arena with its own free lists. More... | |
struct | mmap_t |
Tracks memory-mapped regions for large allocations. More... | |
struct | gc_thread_t |
Orchestrates the background mark-and-sweep garbage collector. More... | |
struct | mem_allocator_t |
Manages dynamic memory allocation. More... | |
Macros | |
#define | __ALIGN |
Defines structure alignment and enforces the target byte order. | |
#define | __LIBMEMALLOC_API |
Defines the default visibility attribute for exported symbols. | |
#define | __LIBMEMALLOC_MALLOC |
Annotates allocator functions that return newly allocated memory. | |
#define | __LIBMEMALLOC_REALLOC |
Annotates reallocator functions that may return a pointer to resized memory. | |
#define | PTR_ERR(x) ((void *)((intptr_t)(x))) |
Encodes a negative error code as an error pointer. | |
#define | ALIGN(x) (((x) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U)) |
Aligns a given value to the nearest memory boundary. | |
#define | GC_INTERVAL_MS (uint16_t)(100U) |
Default interval in milliseconds between GC cycles. | |
Enumerations | |
enum | allocation_strategy_t { FIRST_FIT = (uint8_t)(0u) , NEXT_FIT = (uint8_t)(1u) , BEST_FIT = (uint8_t)(2u) } |
Defines allocation strategies for memory management. More... | |
Functions | |
void * | MEM_memset (void *const source, const int value, const size_t size) |
Fills a memory block with a specified byte value using optimized operations. | |
void * | MEM_memcpy (void *const dest, const void *src, const size_t size) |
Copies a memory block between buffers using optimized operations. | |
int | MEM_allocatorInit (mem_allocator_t *const allocator) |
Initializes the memory allocator and its internal structures. | |
void * | MEM_allocMallocFirstFit (mem_allocator_t *const allocator, const size_t size, const char *const var) |
Allocates memory using the FIRST_FIT strategy. | |
void * | MEM_allocMallocBestFit (mem_allocator_t *const allocator, const size_t size, const char *const var) |
Allocates memory using the BEST_FIT strategy. | |
void * | MEM_allocMallocNextFit (mem_allocator_t *const allocator, const size_t size, const char *const var) |
Allocates memory using the NEXT_FIT strategy. | |
void * | MEM_allocMalloc (mem_allocator_t *const allocator, const size_t size, const char *const var, const allocation_strategy_t strategy) |
Allocates memory using the specified strategy. | |
void * | MEM_allocCalloc (mem_allocator_t *const allocator, const size_t size, const char *const var, const allocation_strategy_t strategy) |
Allocates and zero‐initializes memory using the specified strategy. | |
void * | MEM_allocRealloc (mem_allocator_t *const allocator, void *const ptr, const size_t new_size, const char *const var, const allocation_strategy_t strategy) |
Reallocates memory with safety checks using the specified strategy. | |
int | MEM_allocFree (mem_allocator_t *const allocator, void *const ptr, const char *const var) |
Releases allocated memory back to the heap. | |
int | MEM_enableGc (mem_allocator_t *const allocator) |
Start or signal the garbage collector thread. | |
int | MEM_disableGc (mem_allocator_t *const allocator) |
Stop the garbage collector thread and perform a final collection. | |
Core memory management components for libmemalloc.
#define __ALIGN |
Defines structure alignment and enforces the target byte order.
When using GCC or Clang, expands to attribute((scalar_storage_order("<host-endian>"), aligned(ARCH_ALIGNMENT))) where <host-endian> fica automaticamente ajustado para "little-endian" ou "big-endian" conforme o compilador. Em compiladores sem suporte a esse atributo, expande para nada.
#define __LIBMEMALLOC_API |
Defines the default visibility attribute for exported symbols.
When using GCC or compatible compilers, this macro expands to attribute((visibility("default"))) to ensure that symbols are exported (visible) in the resulting shared library (.so). For compilers that do not support this attribute, it expands to nothing.
#define __LIBMEMALLOC_MALLOC |
Annotates allocator functions that return newly allocated memory.
When supported by the compiler (GCC/Clang), expands to attribute((malloc, alloc_size(2), warn_unused_result)) which tells the compiler the function behaves like malloc, that the 2nd parameter is the allocation size, and that the return value should not be ignored. Otherwise, expands to nothing.
#define __LIBMEMALLOC_REALLOC |
Annotates reallocator functions that may return a pointer to resized memory.
When supported by the compiler (GCC/Clang), expands to attribute((alloc_size(3), warn_unused_result)) which tells the compiler that the 3rd parameter specifies the new size of the allocation and that the return value should not be ignored. Otherwise, expands to nothing.
#define ALIGN | ( | x | ) | (((x) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U)) |
Aligns a given value to the nearest memory boundary.
[in] | x | Value to be aligned. |
Uses bitwise operations to align the value to the nearest multiple of the current architecture's alignment.
#define GC_INTERVAL_MS (uint16_t)(100U) |
Default interval in milliseconds between GC cycles.
Defines the time (in milliseconds) the garbage collector thread sleeps between each mark-and-sweep cycle. Adjusting this value impacts how frequently memory is reclaimed versus the CPU overhead of running the GC.
#define PTR_ERR | ( | x | ) | ((void *)((intptr_t)(x))) |
Encodes a negative error code as an error pointer.
[in] | x | Integer error code (negative errno value). |
Defines allocation strategies for memory management.
This enum specifies the available strategies for memory block allocation within the heap.
Enumerator | |
---|---|
FIRST_FIT | Allocate the first available block |
NEXT_FIT | Continue searching from the last allocation |
BEST_FIT | Use the smallest block that fits the request |
int MEM_allocatorInit | ( | mem_allocator_t *const | allocator | ) |
Initializes the memory allocator and its internal structures.
This function prepares the allocator’s heap by:
[in] | allocator | Pointer to a mem_allocator_t instance to initialize. |
EXIT_SUCCESS | Allocator initialized successfully. |
-EINVAL | Invalid allocator pointer. |
-ENOMEM | Heap alignment or arena/bin allocation failed. |
void * MEM_allocCalloc | ( | mem_allocator_t *const | allocator, |
const size_t | size, | ||
const char *const | var, | ||
const allocation_strategy_t | strategy | ||
) |
Allocates and zero‐initializes memory using the specified strategy.
This function locks the GC mutex, invokes MEM_allocatorCalloc() with the given strategy
, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | size | Number of bytes requested. |
[in] | var | Variable name for debugging. |
[in] | strategy | Allocation strategy. |
int MEM_allocFree | ( | mem_allocator_t *const | allocator, |
void *const | ptr, | ||
const char *const | var | ||
) |
Releases allocated memory back to the heap.
This function locks the GC mutex, invokes MEM_allocatorFree() with automatically supplied FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | ptr | Pointer to memory to free. |
[in] | var | Variable name for debugging. |
void * MEM_allocMalloc | ( | mem_allocator_t *const | allocator, |
const size_t | size, | ||
const char *const | var, | ||
const allocation_strategy_t | strategy | ||
) |
Allocates memory using the specified strategy.
This function locks the GC mutex, invokes MEM_allocatorMalloc() with the given strategy
, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | size | Number of bytes requested |
[in] | var | Variable name for debugging. |
[in] | strategy | Allocation strategy. |
void * MEM_allocMallocBestFit | ( | mem_allocator_t *const | allocator, |
const size_t | size, | ||
const char *const | var | ||
) |
Allocates memory using the BEST_FIT strategy.
This function locks the GC mutex, invokes MEM_allocatorMalloc() with BEST_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | size | Number of bytes requested. |
[in] | var | Variable name for debugging. |
void * MEM_allocMallocFirstFit | ( | mem_allocator_t *const | allocator, |
const size_t | size, | ||
const char *const | var | ||
) |
Allocates memory using the FIRST_FIT strategy.
This function locks the GC mutex, invokes MEM_allocatorMalloc() with FIRST_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | size | Number of bytes requested. |
[in] | var | Variable name for debugging. |
void * MEM_allocMallocNextFit | ( | mem_allocator_t *const | allocator, |
const size_t | size, | ||
const char *const | var | ||
) |
Allocates memory using the NEXT_FIT strategy.
This function locks the GC mutex, invokes MEM_allocatorMalloc() with NEXT_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | size | Number of bytes requested. |
[in] | var | Variable name for debugging. |
void * MEM_allocRealloc | ( | mem_allocator_t *const | allocator, |
void *const | ptr, | ||
const size_t | new_size, | ||
const char *const | var, | ||
const allocation_strategy_t | strategy | ||
) |
Reallocates memory with safety checks using the specified strategy.
This function locks the GC mutex, invokes MEM_allocatorRealloc() with the given strategy
, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
[in] | allocator | Memory allocator context. |
[in] | ptr | Pointer to existing memory. |
[in] | new_size | New requested size. |
[in] | var | Variable name for debugging. |
[in] | strategy | Allocation strategy. |
ptr
), or an error‐encoded pointer (via PTR_ERR()) on failure. int MEM_disableGc | ( | mem_allocator_t *const | allocator | ) |
Stop the garbage collector thread and perform a final collection.
This function simply wraps MEM_stopGc(), signaling the GC thread to exit, joining it, and running a final mark-and-sweep cycle on the caller thread.
[in] | allocator | Memory allocator context |
int MEM_enableGc | ( | mem_allocator_t *const | allocator | ) |
Start or signal the garbage collector thread.
This function simply wraps MEM_runGc(), starting the GC thread if needed or signaling it to perform a collection cycle.
[in] | allocator | Memory allocator context. |
void * MEM_memcpy | ( | void *const | dest, |
const void * | src, | ||
const size_t | size | ||
) |
Copies a memory block between buffers using optimized operations.
This function copies size
bytes from the source buffer to the destination buffer. It first advances the destination pointer to meet the architecture’s alignment requirements, then copies data in ARCH_ALIGNMENT-sized chunks using 64-bit loads and stores, with prefetch hints for both source and destination cache lines. Any remaining bytes at the end are copied.
[in] | dest | Destination buffer pointer. |
[in] | src | Source buffer pointer. |
[in] | size | Number of bytes to copy. |
dest | Original Pointer on successful operation. |
-EINVAL | Invalid src or size . |
void * MEM_memset | ( | void *const | source, |
const int | value, | ||
const size_t | size | ||
) |
Fills a memory block with a specified byte value using optimized operations.
This function sets each byte in the given memory region to the specified value. It first advances the pointer to meet the architecture’s alignment requirements, then writes data in ARCH_ALIGNMENT-sized chunks using 64-bit stores multiplied by PREFETCH_MULT, with prefetch hints for cache lines. Any remaining bytes at the end are filled one by one.
[in] | source | Pointer to the memory block to fill. |
[in] | value | Byte value to set (0–255). |
[in] | size | Number of bytes to set. |
dest | Original Pointer on successful operation. |
-EINVAL | Invalid src or size . |