Onesize Memory Allocator¶
Overview¶
The Onesize Memory Allocator module provides a simple and efficient interface for allocating and managing fixed-size memory blocks. It is designed for high-performance allocation and deallocation in scenarios where all memory requests are of a uniform size or can be rounded to a fixed size.
Key Features¶
Fixed-Size Memory Allocation:
Handles memory allocation requests up to a predefined block size.
Allocations are fast due to the simplicity of a singly-linked free list.
Efficient Memory Management:
Reclaims memory by returning freed blocks to the front of the free list.
Supports batch memory reclamation with a function to free all allocated blocks.
Diagnostics and Debugging:
Utilities to inspect memory usage and iterate over allocated memory blocks with a callback function.
Tracks the minimum number of free blocks available to monitor resource usage.
Concurrency and Thread Safety:
Critical section callbacks allow safe usage in multi-threaded environments.
Custom Configuration:
Supports custom memory pools, alignment requirements, and critical section handlers.
Usage Scenarios¶
The Onesize Memory Allocator is ideal for systems that require:
Uniform Memory Allocation: Efficient handling of memory blocks of consistent sizes.
Real-Time Performance: Fast allocation and deallocation with minimal overhead.
Thread-Safe Memory Management: Safe operations in multi-threaded environments using critical section management.
Design Considerations¶
Memory Allocation:
The allocator (
am_onesize) maintains a singly-linked free list to store unused memory blocks.Blocks are allocated from the front of the free list, ensuring fast retrieval.
Configuration Options:
Customizable block size and alignment settings are defined during construction.
Critical section entry and exit callbacks provide thread safety.
Diagnostics:
Functions are provided to query the number of free blocks, the minimum free block count, and the total block size.
Iteration over allocated blocks allows inspection of memory usage for debugging.
Module Configuration¶
The module configuration (am_onesize_cfg) specifies:
Memory Pool: The memory pool from which blocks are allocated.
Block Size and Alignment: The size and alignment of memory blocks.
Critical Section Callbacks: Ensure thread safety during allocation and deallocation.
System Integration¶
The module integrates seamlessly into memory management systems for embedded and real-time applications. Key integration points include:
Initialization: Use
am_onesize_ctorto configure and initialize the allocator.Memory Allocation: Allocate memory using
am_onesize_allocateand free blocks witham_onesize_free.Diagnostics: Inspect memory usage with utilities like
am_onesize_get_nfreeandam_onesize_iterate_over_allocated.
Limitations¶
Only supports allocation requests up to the configured block size.
Does not support dynamic resizing of blocks after initialization.