libmemalloc  v3.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
Libmemalloc

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.
 

Detailed Description

Core memory management components for libmemalloc.

Architecture, alignment, and attribute utilities for libmemalloc.

Macro Definition Documentation

◆ __ALIGN

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

◆ __GC_COLD

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

◆ __GC_HOT

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

◆ __LIBMEMALLOC_API

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

◆ __LIBMEMALLOC_INTERNAL_MALLOC

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

◆ __LIBMEMALLOC_INTERNAL_REALLOC

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

◆ __LIBMEMALLOC_MALLOC

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

◆ __LIBMEMALLOC_REALLOC

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

◆ __UNUSED

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

◆ ALIGN

#define ALIGN (   val_)    (((val_) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U))

Aligns a given value to the nearest memory boundary.

Parameters
[in]val_Value to be aligned.

Uses bitwise operations to align the value to the nearest multiple of the current architecture's alignment.

◆ ASSUME_ALIGNED

#define ASSUME_ALIGNED (   ptr_,
  align_ 
)    (align_)

Tell the compiler the pointer has a fixed alignment.

Parameters
[in]ptr_Pointer value.
[in]align_Alignment in bytes (power of two).
Returns
The same pointer, possibly carrying alignment/aliasing info.

Uses __builtin_assume_aligned() when available; otherwise returns ptr unchanged. Passing a wrong alignment is UB.

◆ GC_INTERVAL_MS

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

◆ LIKELY

#define LIKELY (   cond_)    (!!(cond_))

Compiler hint for likely branch prediction.

Parameters
[in]cond_Condition that is likely to be true.

Helps the compiler optimize branch prediction by indicating the condition is expected to be true.

◆ PREFETCH_R

#define PREFETCH_R (   addr_)    ((void)0)

Read prefetch hint.

Parameters
[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.

◆ PREFETCH_W

#define PREFETCH_W (   addr_)    ((void)0)

Write prefetch hint.

Parameters
[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.

◆ PTR_ERR

#define PTR_ERR (   ptr_)    ((void *)((intptr_t)(ptr_)))

Encodes a negative error code as an error pointer.

Parameters
[in]ptr_Integer error code (negative errno value).
Returns
A pointer whose value encodes the error code. To test and extract the code, use IS_ERR() and (intptr_t)ptr respectively.

◆ UNLIKELY

#define UNLIKELY (   cond_)    (!!(x))

Compiler hint for unlikely branch prediction.

Parameters
[in]cond_Condition that is unlikely to be true.

Helps the compiler optimize branch prediction by indicating the condition is expected to be false.

Enumeration Type Documentation

◆ allocation_strategy_t

Defines allocation strategies for memory management.

This enum specifies the available strategies for memory block allocation within the heap.

Fields:
  • FIRST_FIT – First available block allocation
  • NEXT_FIT – Continue from last allocation
  • BEST_FIT – Smallest block fitting the request
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

Function Documentation

◆ MEM_alloc()

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.

Parameters
[in]sizeNumber of bytes requested
[in]strategyAllocation strategy.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

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.

Parameters
[in]sizeNumber of bytes requested
[in]strategyAllocation strategy.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

◆ MEM_allocBestFit()

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

◆ MEM_allocFirstFit()

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

◆ MEM_allocNextFit()

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

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.

Parameters
[in]sizeNumber of bytes requested.
Returns
Pointer to the allocated user memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

◆ MEM_calloc()

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.

Parameters
[in]sizeNumber of bytes requested.
[in]strategyAllocation strategy.
Returns
Pointer to the allocated zeroed memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

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.

Parameters
[in]sizeNumber of bytes requested.
[in]varVariable name for debugging.
Returns
Pointer to the allocated zeroed memory on success, or an error‐encoded pointer (via PTR_ERR()) on failure.

◆ MEM_free()

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.

Parameters
[in]ptrPointer to memory to free.
Returns
EXIT_SUCCESS on success, negative error code on failure.

This function locks the GC mutex, invokes MEM_freeOp() with automatically supplied FILE, LINE, and variable name for debugging, then unlocks the mutex.

Parameters
[in]ptrPointer to memory to free.
Returns
EXIT_SUCCESS on success, negative error code on failure.

◆ MEM_memcpy()

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.

Parameters
[in]destDestination buffer pointer.
[in]srcSource buffer pointer.
[in]sizeNumber of bytes to copy.
Returns
Original dest pointer on success, an error-encoded pointer (via PTR_ERR()) on failure.
Return values
destOriginal Pointer on successful operation.
-EINVALInvalid src or size.

◆ MEM_memset()

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.

Parameters
[in]sourcePointer to the memory block to fill.
[in]valueByte value to set (0–255).
[in]sizeNumber of bytes to set.
Returns
Original source pointer on success, an error‐encoded pointer (via PTR_ERR()) on failure.
Return values
destOriginal Pointer on successful operation.
-EINVALInvalid src or size.

◆ MEM_realloc()

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.

Parameters
[in]ptrPointer to existing memory.
[in]new_sizeNew requested size.
[in]strategyAllocation strategy.
Returns
Pointer to the reallocated memory on success (may be same as 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.

Parameters
[in]ptrPointer to existing memory.
[in]new_sizeNew requested size.
[in]strategyAllocation strategy.
Returns
Pointer to the reallocated memory on success (may be same as ptr), or an error‐encoded pointer (via PTR_ERR()) on failure.