|
libmemalloc
v3.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 |
| file | memalloc_utils.h |
Macros | |
| #define | GC_INTERVAL_MS (uint16_t)(100U) |
| Default interval in milliseconds between GC cycles. | |
| #define | ALIGN(val_) (((val_) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U)) |
| Aligns a given value to the nearest memory boundary. | |
| #define | PTR_ERR(ptr_) ((void *)((intptr_t)(ptr_))) |
| Encodes a negative error code as an error pointer. | |
| #define | __LIBMEMALLOC_API |
| Defines the default visibility attribute for exported symbols. | |
| #define | __ALIGN |
| Defines structure alignment and enforces the target byte order. | |
| #define | __UNUSED |
| Marks a symbol as potentially unused. | |
| #define | __GC_HOT |
| Marks garbage-collector functions as “hot” (performance critical). | |
| #define | __GC_COLD |
| Marks garbage-collector functions as “cold” (infrequently used). | |
| #define | __LIBMEMALLOC_INTERNAL_MALLOC |
| Annotates allocator functions that return newly allocated memory. | |
| #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 | __LIBMEMALLOC_INTERNAL_REALLOC |
| Annotates reallocator functions that may return a pointer to resized memory. | |
| #define | LIKELY(cond_) (!!(cond_)) |
| Compiler hint for likely branch prediction. | |
| #define | UNLIKELY(cond_) (!!(x)) |
| Compiler hint for unlikely branch prediction. | |
| #define | PREFETCH_R(addr_) ((void)0) |
| Read prefetch hint. | |
| #define | PREFETCH_W(addr_) ((void)0) |
| Write prefetch hint. | |
| #define | ASSUME_ALIGNED(ptr_, align_) (align_) |
| Tell the compiler the pointer has a fixed alignment. | |
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. | |
| void * | MEM_allocFirstFit (const size_t size) |
| Allocates memory using the FIRST_FIT strategy. | |
| void * | MEM_allocBestFit (const size_t size) |
| Allocates memory using the BEST_FIT strategy. | |
| void * | MEM_allocNextFit (const size_t size) |
| Allocates memory using the NEXT_FIT strategy. | |
| void * | MEM_alloc (const size_t size, const allocation_strategy_t strategy) |
| Allocates memory using the specified strategy. | |
| void * | MEM_calloc (const size_t size, const allocation_strategy_t strategy) |
| Allocates and zero‐initializes memory using the specified strategy. | |
| void * | MEM_realloc (void *const ptr, const size_t new_size, const allocation_strategy_t strategy) |
| Reallocates memory with safety checks using the specified strategy. | |
| int | MEM_free (void *const ptr) |
| Releases allocated memory back to the heap. | |
Core memory management components for libmemalloc.
Architecture, alignment, and attribute utilities 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 __GC_COLD |
Marks garbage-collector functions as “cold” (infrequently used).
When supported by the compiler (GCC/Clang), expands to attribute((cold)), informing the optimizer that the annotated function is unlikely to execute frequently and may be placed out-of-line to reduce code size in hot paths. Otherwise, expands to nothing.
| #define __GC_HOT |
Marks garbage-collector functions as “hot” (performance critical).
When supported by the compiler (GCC/Clang), expands to attribute((hot)), informing the optimizer that the annotated function is on a performance-critical path and should be optimized accordingly. Otherwise, expands to nothing.
| #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_INTERNAL_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_INTERNAL_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 __LIBMEMALLOC_MALLOC |
Annotates allocator functions that return newly allocated memory.
When supported by the compiler (GCC/Clang), expands to attribute((malloc, alloc_size(1), 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(2), 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 __UNUSED |
Marks a symbol as potentially unused.
When supported by the compiler (GCC/Clang), expands to attribute((unused)), suppressing “unused variable” warnings. On other compilers it expands to nothing.
| #define ALIGN | ( | val_ | ) | (((val_) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U)) |
Aligns a given value to the nearest memory boundary.
| [in] | val_ | Value to be aligned. |
Uses bitwise operations to align the value to the nearest multiple of the current architecture's alignment.
| #define ASSUME_ALIGNED | ( | ptr_, | |
| align_ | |||
| ) | (align_) |
Tell the compiler the pointer has a fixed alignment.
| [in] | ptr_ | Pointer value. |
| [in] | align_ | Alignment in bytes (power of two). |
Uses __builtin_assume_aligned() when available; otherwise returns ptr unchanged. Passing a wrong alignment is UB.
| #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 LIKELY | ( | cond_ | ) | (!!(cond_)) |
Compiler hint for likely branch prediction.
| [in] | cond_ | Condition that is likely to be true. |
Helps the compiler optimize branch prediction by indicating the condition is expected to be true.
| #define PREFETCH_R | ( | addr_ | ) | ((void)0) |
Read prefetch hint.
| [in] | addr_ | Address expected to be read soon (may be NULL). |
Wraps __builtin_prefetch when available; otherwise a no-op. Uses default locality level 1.
| #define PREFETCH_W | ( | addr_ | ) | ((void)0) |
Write prefetch hint.
| [in] | addr_ | Address expected to be written soon (may be NULL). |
Wraps __builtin_prefetch when available; otherwise a no-op. Uses default locality level 1.
| #define PTR_ERR | ( | ptr_ | ) | ((void *)((intptr_t)(ptr_))) |
Encodes a negative error code as an error pointer.
| [in] | ptr_ | Integer error code (negative errno value). |
| #define UNLIKELY | ( | cond_ | ) | (!!(x)) |
Compiler hint for unlikely branch prediction.
| [in] | cond_ | Condition that is unlikely to be true. |
Helps the compiler optimize branch prediction by indicating the condition is expected to be false.
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 |
| void * MEM_alloc | ( | const size_t | size, |
| 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] | size | Number of bytes requested |
| [in] | strategy | Allocation strategy. |
This function locks the GC mutex, invokes MEM_allocOp() with the given strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | size | Number of bytes requested |
| [in] | strategy | Allocation strategy. |
| void * MEM_allocBestFit | ( | const size_t | size | ) |
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] | size | Number of bytes requested. |
This function locks the GC mutex, invokes MEM_allocOp() with BEST_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | size | Number of bytes requested. |
| void * MEM_allocFirstFit | ( | const size_t | size | ) |
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] | size | Number of bytes requested. |
This function locks the GC mutex, invokes MEM_allocOp() with FIRST_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | size | Number of bytes requested. |
| void * MEM_allocNextFit | ( | const size_t | size | ) |
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] | size | Number of bytes requested. |
This function locks the GC mutex, invokes MEM_allocOp() with NEXT_FIT strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | size | Number of bytes requested. |
| void * MEM_calloc | ( | const size_t | size, |
| 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] | size | Number of bytes requested. |
| [in] | strategy | Allocation strategy. |
This function locks the GC mutex, invokes MEM_callocOp() with the given strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | size | Number of bytes requested. |
| [in] | var | Variable name for debugging. |
| int MEM_free | ( | void *const | ptr | ) |
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] | ptr | Pointer to memory to free. |
This function locks the GC mutex, invokes MEM_freeOp() with automatically supplied FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | ptr | Pointer to memory to free. |
| 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. |
| void * MEM_realloc | ( | void *const | ptr, |
| const size_t | new_size, | ||
| 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] | ptr | Pointer to existing memory. |
| [in] | new_size | New requested size. |
| [in] | strategy | Allocation strategy. |
ptr), or an error‐encoded pointer (via PTR_ERR()) on failure.This function locks the GC mutex, invokes MEM_reallocOp() with the given strategy, automatically supplying FILE, LINE, and variable name for debugging, then unlocks the mutex.
| [in] | ptr | Pointer to existing memory. |
| [in] | new_size | New requested size. |
| [in] | strategy | Allocation strategy. |
ptr), or an error‐encoded pointer (via PTR_ERR()) on failure.