libmemalloc  v4.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
libmemalloc.h File Reference
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
Include dependency graph for libmemalloc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

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

Implements memory allocator with advanced features:

  • Garbage collection (mark & sweep)
  • Multiple allocation strategies (First/Best/Next Fit)
Version
v1.0.00
Date
23.08.2025
Author
Rafael V. Volkmer rafae.nosp@m.l.v..nosp@m.volkm.nosp@m.er@g.nosp@m.mail..nosp@m.com