The c-xforge library provides a set of allocators and functions for memory management. The library is designed for a compiler that supports the ANSI C (C99) standard. The features and interfaces of c-xforge rely on the capabilities of this standard.
Data type macros and constants with data type constraints are defined in the header file xfunc710n/xforge/xforge.h.
These macros provide aliases for standard data types.
| Macro | C Data Type | Description |
|---|---|---|
SShort |
short |
Signed short integer data type |
UShort |
unsigned short |
Unsigned short integer data type |
SInt |
int |
Signed integer data type |
UInt |
unsigned int |
Unsigned integer data type |
SLong |
long |
Signed long integer data type |
ULong |
unsigned long |
Unsigned long integer data type |
| Macro | C Data Type | Description |
|---|---|---|
Float |
float |
Single-precision floating-point number |
Double |
double |
Double-precision floating-point number |
| Macro | C Data Type | Size | Description |
|---|---|---|---|
SInt8 |
int8_t |
8 bits | Signed 8-bit integer data type |
UInt8 |
uint8_t |
8 bits | Unsigned 8-bit integer data type |
SInt16 |
int16_t |
16 bits | Signed 16-bit integer data type |
UInt16 |
uint16_t |
16 bits | Unsigned 16-bit integer data type |
SInt32 |
int32_t |
32 bits | Signed 32-bit integer data type |
UInt32 |
uint32_t |
32 bits | Unsigned 32-bit integer data type |
SInt64 |
int64_t |
64 bits | Signed 64-bit integer data type |
UInt64 |
uint64_t |
64 bits | Unsigned 64-bit integer data type |
| Macro | Value | Description |
|---|---|---|
Boolean |
bool |
Boolean data type |
False |
0 |
Boolean "false" |
True |
1 |
Boolean "true" |
| Macro | C Data Type | Description |
|---|---|---|
SPointer |
intptr_t |
Signed data type for storing pointers |
UPointer |
uintptr_t |
Unsigned data type for storing pointers |
| Macro | C Data Type | Description |
|---|---|---|
USize |
size_t |
Unsigned data type for sizes and counters |
| Macro | C Data Type | Description |
|---|---|---|
SChar |
char |
Signed character data type |
UChar |
unsigned char |
Unsigned character data type |
These macros provide aliases to standard data type limit constants.
| Macro | Value | Description |
|---|---|---|
MIN_SShort |
SHRT_MIN |
Minimum value of SShort |
MAX_SShort |
SHRT_MAX |
Maximum value of SShort |
MAX_UShort |
USHRT_MAX |
Maximum value of UShort |
MIN_SInt |
INT_MIN |
Minimum value of SInt |
MAX_SInt |
INT_MAX |
Maximum value of SInt |
MAX_UInt |
UINT_MAX |
Maximum value of UInt |
MIN_SLong |
LONG_MIN |
Minimum value of SLong |
MAX_SLong |
LONG_MAX |
Maximum value of SLong |
MAX_ULong |
ULONG_MAX |
Maximum value of ULong |
| Macro | Value | Description |
|---|---|---|
MIN_Float |
FLT_MIN |
Minimum positive value of Float |
MAX_Float |
FLT_MAX |
Maximum value of Float |
MIN_Double |
DBL_MIN |
Minimum positive value of Double |
MAX_Double |
DBL_MAX |
Maximum value of Double |
| Macro | Value | Description |
|---|---|---|
MIN_SInt8 |
INT8_MIN |
Minimum value of SInt8 |
MAX_SInt8 |
INT8_MAX |
Maximum value of SInt8 |
MAX_UInt8 |
UINT8_MAX |
Maximum value of UInt8 |
MIN_SInt16 |
INT16_MIN |
Minimum value of SInt16 |
MAX_SInt16 |
INT16_MAX |
Maximum value of SInt16 |
MAX_UInt16 |
UINT16_MAX |
Maximum value of UInt16 |
MIN_SInt32 |
INT32_MIN |
Minimum value of SInt32 |
MAX_SInt32 |
INT32_MAX |
Maximum value of SInt32 |
MAX_UInt32 |
UINT32_MAX |
Maximum value of UInt32 |
MIN_SInt64 |
INT64_MIN |
Minimum value of SInt64 |
MAX_SInt64 |
INT64_MAX |
Maximum value of SInt64 |
MAX_UInt64 |
UINT64_MAX |
Maximum value of UInt64 |
The c-xforge library includes four types of memory allocators (TLinearAllocator, TStackAllocator, TPoolAllocator, TFreeListAllocator) and provides an interface for using them (IAllocator).
The implementation field in the allocator structure determines which memory allocator implementation is being used.
| Macro | Value | Implementation |
|---|---|---|
IMPLEMENTATION_TLinearAllocator |
1 |
TLinearAllocator |
IMPLEMENTATION_TStackAllocator |
2 |
TStackAllocator |
IMPLEMENTATION_TPoolAllocator |
3 |
TPoolAllocator |
IMPLEMENTATION_TFreeListAllocator |
4 |
TFreeListAllocator |
The header file xfunc710n/xforge/memory/IAllocator.h defines the IAllocator memory allocator interface, which is used to abstract memory allocation and deallocation mechanisms in the c-xforge library.
The IAllocator interface allows for uniform operation with various allocator implementations (TLinearAllocator, TStackAllocator, TPoolAllocator, TFreeListAllocator).
A universal function for allocating memory via the IAllocator interface. Designed to request a memory block of arbitrary size from a specific allocator implementation.
void *
IAllocator_allocate(
IAllocator *allocator,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface. -
size- number of bytes to allocate.
- pointer to the allocated memory (void *) / NULL on error.
A universal function for deallocating memory via the IAllocator interface. Designed to free a memory block from a specific allocator implementation.
void
IAllocator_deallocate(
IAllocator *allocator,
void *pointer
);-
allocator- pointer to an object implementing the IAllocator interface. -
pointer- pointer to the memory block.
A universal function to reset the state of the selected allocator, where all previously allocated memory remains reserved within it and is retained for subsequent reuse.
void
IAllocator_reset(
IAllocator *allocator
);allocator- pointer to an object implementing the IAllocator interface.
The c-xforge library offers four implementations of memory allocators - TLinearAllocator, TStackAllocator, TPoolAllocator and TFreeListAllocator.
| Memory Allocator | Header File |
|---|---|
TLinearAllocator |
xfunc710n/xforge/memory/TLinearAllocator.h |
TStackAllocator |
xfunc710n/xforge/memory/TStackAllocator.h |
TPoolAllocator |
xfunc710n/xforge/memory/TPoolAllocator.h |
TFreeListAllocator |
xfunc710n/xforge/memory/TFreeListAllocator.h |
All allocators operate through a unified memory system, obtaining new resource pages when required. On top of this common foundation, each allocator implements its own memory management scheme, defining rules for allocation, deallocation and resource organization according to its specific strategy.
The header file xfunc710n/xforge/memory/TLinearAllocator.h contains the definition of the linear allocator (TLinearAllocator), which implements the IAllocator interface.
This allocator organizes memory allocation in a sequential (linear) order and does not support selective deallocation of individual blocks. Clearing is only possible by performing a complete reset.
The function creates a new TLinearAllocator.
TLinearAllocator *
new_TLinearAllocator(
IAllocator *allocator,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory allocation. If set to NULL, the standard malloc is used. -
size- resource size in bytes.
- Pointer to TLinearAllocator / NULL on error.
The function deallocates memory occupied by TLinearAllocator and all its internal structures.
void
dispose_TLinearAllocator(
TLinearAllocator *linearAllocator
);linearAllocator- pointer to TLinearAllocator.
Memory allocation function via TLinearAllocator. Designed to request a memory block of arbitrary size from TLinearAllocator.
void *
TLinearAllocator_allocate(
TLinearAllocator *linearAllocator,
USize size
);-
linearAllocator- pointer to TLinearAllocator. -
size- number of bytes to allocate.
- pointer to allocated memory (void *) / NULL on error.
Function to reset the state of TLinearAllocator, where all previously allocated memory remains reserved within it and is retained for subsequent reuse.
void
TLinearAllocator_reset(
TLinearAllocator *linearAllocator
);linearAllocator- pointer to TLinearAllocator.
The header file xfunc710n/xforge/memory/TStackAllocator.h contains the definition of the stack memory allocator (TStackAllocator), which implements the IAllocator interface.
The stack allocator allocates memory sequentially, similar to a linear allocator, and supports deallocation in reverse order (LIFO).
The function creates a new TStackAllocator.
TStackAllocator *
new_TStackAllocator(
IAllocator *allocator,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory allocation. If set to NULL, the standard malloc is used. -
size- resource size in bytes.
- Pointer to TStackAllocator / NULL on error.
The function deallocates memory occupied by TStackAllocator and all its internal structures.
void
dispose_TStackAllocator(
TStackAllocator *stackAllocator
);stackAllocator- pointer to TStackAllocator.
Memory allocation function via TStackAllocator. Designed to request a memory block of arbitrary size from TStackAllocator.
void *
TStackAllocator_allocate(
TStackAllocator *stackAllocator,
USize size
);-
stackAllocator- pointer to TStackAllocator. -
size- number of bytes to allocate.
- pointer to allocated memory (void *) / NULL on error.
Memory deallocation function via TStackAllocator. Designed to free a memory block from TStackAllocator. Deallocation is performed strictly in reverse order relative to allocation (LIFO). This means that only the most recently allocated block can be freed, and only after that, the previous ones. This approach ensures simple and fast allocator operation but imposes a limitation: arbitrary block deallocation is not supported.
void
TStackAllocator_deallocate(
TStackAllocator *stackAllocator,
void *pointer
);-
stackAllocator- pointer to TStackAllocator. -
pointer- pointer to the memory block.
Function to reset the state of TStackAllocator, where all previously allocated memory remains reserved within it and is retained for subsequent reuse.
void
TStackAllocator_reset(
TStackAllocator *stackAllocator
);stackAllocator- pointer to TStackAllocator.
The header file xfunc710n/xforge/memory/TPoolAllocator.h contains the definition of the pool-based memory allocator (TPoolAllocator), which implements the IAllocator interface.
The pool allocator allocates memory in blocks of equal size, which eliminates fragmentation and speeds up memory allocation and deallocation. Each block in the pool can be allocated or deallocated independently, making it suitable for scenarios with multiple objects of the same size.
The function creates a new TPoolAllocator.
TPoolAllocator *
new_TPoolAllocator(
IAllocator *allocator,
USize capacity,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory allocation. If set to NULL, the standard malloc is used. -
capacity- number of blocks in the resource. -
size- size of each block in bytes.
- Pointer to TPoolAllocator / NULL on error.
The function deallocates memory occupied by TPoolAllocator and all its internal structures.
void
dispose_TPoolAllocator(
TPoolAllocator *poolAllocator
);poolAllocator- pointer to TPoolAllocator.
Memory allocation function via TPoolAllocator. Designed to request a fixed-size memory block from TPoolAllocator.
void *
TPoolAllocator_allocate(
TPoolAllocator *poolAllocator,
USize size
);-
poolAllocator- pointer to TPoolAllocator. -
size- number of bytes to allocate. Must be less than or equal to the block size specified when creating the pool.
- pointer to allocated memory (void *) / NULL on error.
Memory deallocation function via TPoolAllocator. Designed to free a memory block from TPoolAllocator.
void
TPoolAllocator_deallocate(
TPoolAllocator *poolAllocator,
void *pointer
);-
poolAllocator- pointer to TPoolAllocator. -
pointer- pointer to the memory block.
Function to reset the state of TPoolAllocator, where all previously allocated memory remains reserved within it and is retained for subsequent reuse.
void
TPoolAllocator_reset(
TPoolAllocator *poolAllocator
);poolAllocator- pointer to TPoolAllocator.
The header file xfunc710n/xforge/memory/TFreeListAllocator.h contains the definition of the free list-based memory allocator (TFreeListAllocator), which implements the IAllocator interface.
The free list allocator manages a memory pool by dividing it into variable-size blocks and maintaining a linked list of free blocks. This allows for efficient allocation and deallocation of different-sized blocks, minimizing fragmentation through the merging of adjacent free blocks.
The function creates a new TFreeListAllocator.
TFreeListAllocator *
new_TFreeListAllocator(
IAllocator *allocator,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory allocation. If set to NULL, the standard malloc is used. -
size- resource size in bytes.
- Pointer to TFreeListAllocator / NULL on error.
The function deallocates memory occupied by TFreeListAllocator and all its internal structures.
void
dispose_TFreeListAllocator(
TFreeListAllocator *freeListAllocator
);freeListAllocator- pointer to TFreeListAllocator.
Memory allocation function via TFreeListAllocator. Designed to request a memory block of arbitrary size from TFreeListAllocator.
void *
TFreeListAllocator_allocate(
TFreeListAllocator *freeListAllocator,
USize size
);-
freeListAllocator- pointer to TFreeListAllocator. -
size- number of bytes to allocate.
- pointer to allocated memory (void *) / NULL on error.
Memory deallocation function via TFreeListAllocator. Designed to free a memory block from TFreeListAllocator. Upon deallocation, the allocator attempts to merge the freed block with adjacent free blocks to reduce fragmentation.
void
TFreeListAllocator_deallocate(
TFreeListAllocator *freeListAllocator,
void *pointer
);-
freeListAllocator- pointer to TFreeListAllocator. -
pointer- pointer to the memory block.
Function to reset the state of TFreeListAllocator, where all previously allocated memory remains reserved within it and is retained for subsequent reuse.
void
TFreeListAllocator_reset(
TFreeListAllocator *freeListAllocator
);freeListAllocator- pointer to TFreeListAllocator.
The header file xfunc710n/xforge/utility/memory/Allocator.h contains helper functions for memory management in the c-xforge project. These functions act as a wrapper over the allocator system, allowing the use of custom IAllocator implementations and standard C language functions.
The standard c-xforge function for dynamic memory allocation. The function requests a memory block of the specified size.
void *
allocate(
IAllocator *allocator,
USize size
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory allocation. If set to NULL, the standard malloc is used. -
size- memory block size in bytes.
- pointer to allocated memory (void *) / NULL on error.
The standard c-xforge function for deallocating previously allocated dynamic memory. The function accepts a pointer to a memory block and deallocates it.
void
deallocate(
IAllocator *allocator,
void *pointer
);-
allocator- pointer to an object implementing the IAllocator interface, which will be used for memory deallocation. If set to NULL, the standard free is used. -
pointer- pointer to the memory block.