libmemalloc  v3.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
libmemalloc.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2025 Rafael V. Volkmer
3 * SPDX-FileCopyrightText: <rafael.v.volkmer@gmail.com>
4 * SPDX-License-Identifier: MIT
5 */
6
24#pragma once
25
26/*< C++ Compatibility >*/
27#ifdef __cplusplus
28extern "C"
29{
30#endif
31
36/*< Dependencies >*/
37#include <pthread.h>
38#include <stdatomic.h>
39#include <stdbool.h>
40#include <stdint.h>
41#include <string.h>
42
43/*< Implemented >*/
44#include "memalloc_utils.h"
45
59#define GC_INTERVAL_MS (uint16_t)(100U)
60
78typedef enum AllocationStrategy
79{
80 FIRST_FIT = (uint8_t)(0u),
81 NEXT_FIT = (uint8_t)(1u),
82 BEST_FIT = (uint8_t)(2u)
84
109__LIBMEMALLOC_API void *MEM_memset(void *const source,
110 const int value,
111 const size_t size);
112
132__LIBMEMALLOC_API void *MEM_memcpy(void *const dest,
133 const void *src,
134 const size_t size);
135
152__LIBMEMALLOC_API void *MEM_allocFirstFit(const size_t size)
154
167__LIBMEMALLOC_API void *MEM_allocBestFit(const size_t size)
169
182__LIBMEMALLOC_API void *MEM_allocNextFit(const size_t size)
184
198__LIBMEMALLOC_API void *MEM_alloc(const size_t size,
199 const allocation_strategy_t strategy)
201
215__LIBMEMALLOC_API void *MEM_calloc(const size_t size,
216 const allocation_strategy_t strategy)
218
233__LIBMEMALLOC_API void *MEM_realloc(void *const ptr,
234 const size_t new_size,
235 const allocation_strategy_t strategy)
237
250__LIBMEMALLOC_API int MEM_free(void *const ptr);
251
256#if defined(GARBACE_COLLECTOR)
257
269__LIBMEMALLOC_API int MEM_enableGc(mem_allocator_t *const allocator);
270
282__LIBMEMALLOC_API int MEM_disableGc(mem_allocator_t *const allocator);
283
284#endif
285
286/*< C++ Compatibility >*/
287#ifdef __cplusplus
288}
289#endif
290
293/*< end of header file >*/
void * MEM_calloc(const size_t size, const allocation_strategy_t strategy)
Allocates and zero‐initializes memory using the specified strategy.
Definition libmemalloc.c:4035
allocation_strategy_t
Defines allocation strategies for memory management.
Definition libmemalloc.h:76
void * MEM_allocFirstFit(const size_t size)
Allocates memory using the FIRST_FIT strategy.
Definition libmemalloc.c:3877
int MEM_free(void *const ptr)
Releases allocated memory back to the heap.
Definition libmemalloc.c:4118
#define __LIBMEMALLOC_REALLOC
Annotates reallocator functions that may return a pointer to resized memory.
Definition memalloc_utils.h:385
#define __LIBMEMALLOC_API
Defines the default visibility attribute for exported symbols.
Definition memalloc_utils.h:224
void * MEM_alloc(const size_t size, const allocation_strategy_t strategy)
Allocates memory using the specified strategy.
Definition libmemalloc.c:3995
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.
Definition libmemalloc.c:1204
void * MEM_allocBestFit(const size_t size)
Allocates memory using the BEST_FIT strategy.
Definition libmemalloc.c:3916
void * MEM_allocNextFit(const size_t size)
Allocates memory using the NEXT_FIT strategy.
Definition libmemalloc.c:3955
void * MEM_memcpy(void *const dest, const void *src, const size_t size)
Copies a memory block between buffers using optimized operations.
Definition libmemalloc.c:1287
#define __LIBMEMALLOC_MALLOC
Annotates allocator functions that return newly allocated memory.
Definition memalloc_utils.h:360
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.
Definition libmemalloc.c:4076
@ NEXT_FIT
Definition libmemalloc.h:78
@ FIRST_FIT
Definition libmemalloc.h:77
@ BEST_FIT
Definition libmemalloc.h:79
Manages dynamic memory allocation.
Definition libmemalloc.c:353