libmemalloc  v4.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
 

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.
 

Detailed Description

Core memory management components 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.

◆ __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_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(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_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(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.

◆ ALIGN

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

Aligns a given value to the nearest memory boundary.

Parameters
[in]xValue to be aligned.

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

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

◆ PTR_ERR

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

Encodes a negative error code as an error pointer.

Parameters
[in]xInteger 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.

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_allocatorInit()

int MEM_allocatorInit ( mem_allocator_t *const  allocator)

Initializes the memory allocator and its internal structures.

This function prepares the allocator’s heap by:

  • Reading and aligning the initial program break to ARCH_ALIGNMENT.
  • Allocating the arena array and free‐list bins via MEM_growUserHeap().
  • Zeroing out the free‐list bins.
  • Initializing the allocator fields (heap_start, heap_end, free_lists).
  • Setting up the garbage‐collector thread state and its mutex/cond.
  • Capturing the current thread’s stack bounds via MEM_stackBounds().
  • (If under Valgrind) creating a mempool for the allocator.
Parameters
[in]allocatorPointer to a mem_allocator_t instance to initialize.
Returns
Integer status code indicating initialization result.
Return values
EXIT_SUCCESSAllocator initialized successfully.
-EINVALInvalid allocator pointer.
-ENOMEMHeap alignment or arena/bin allocation failed.

◆ MEM_allocCalloc()

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.

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

◆ MEM_allocFree()

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.

Parameters
[in]allocatorMemory allocator context.
[in]ptrPointer to memory to free.
[in]varVariable name for debugging.
Returns
EXIT_SUCCESS on success, negative error code on failure.

◆ MEM_allocMalloc()

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.

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

◆ MEM_allocMallocBestFit()

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.

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

◆ MEM_allocMallocFirstFit()

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.

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

◆ MEM_allocMallocNextFit()

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.

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

◆ MEM_allocRealloc()

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.

Parameters
[in]allocatorMemory allocator context.
[in]ptrPointer to existing memory.
[in]new_sizeNew requested size.
[in]varVariable name for debugging.
[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.

◆ MEM_disableGc()

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.

Parameters
[in]allocatorMemory allocator context
Returns
EXIT_SUCCESS on success, negative error code on failure.

◆ MEM_enableGc()

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.

Parameters
[in]allocatorMemory allocator context.
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.