MiniGUI API Reference (MiniGUI-Processes)
v5.0.6
A mature and proven cross-platform GUI system for embedded and smart IoT devices
|
An efficient way to allocate groups of equal-sized chunks of memory. More...
Macros | |
#define | mg_slice_new(type) ((type*)mg_slice_alloc(sizeof (type))) |
The macro to allocate a slice memory for a structure. More... | |
#define | mg_slice_new0(type) ((type*)mg_slice_alloc0(sizeof (type))) |
The macro to allocate a zero'd slice memory for a structure. More... | |
#define | mg_slice_dup(type, mem) |
Duplicate a structure. More... | |
#define | mg_slice_delete(type, mem) |
Free a block of memory. More... | |
#define | mg_slice_delete_chain(type, mem_chain, next) |
Free a linked list of memory blocks. More... | |
Functions | |
MG_EXPORT void * | mg_slice_alloc (size_t block_size) |
Allocate a slice memory. More... | |
MG_EXPORT void * | mg_slice_alloc0 (size_t block_size) |
Allocate a slice memory and initialize the memory to zero. More... | |
MG_EXPORT void * | mg_slice_copy (size_t block_size, const void *mem_block) |
Allocate and copy a slice. More... | |
MG_EXPORT void | mg_slice_free (size_t block_size, void *mem_block) |
Free a block of memory. More... | |
MG_EXPORT void | mg_slice_free_chain_with_offset (size_t block_size, void *mem_chain, size_t next_offset) |
Free a linked list of memory blocks. More... | |
An efficient way to allocate groups of equal-sized chunks of memory.
Memory slices provide a space-efficient and multi-processing scalable way to allocate equal-sized pieces of memory, just like the MiniGUI's block data heap (block_heap_fns). Relative to the standard malloc function and block data heap, this allocator can avoid excessive memory-waste, scalability and performance problems.
Note that this implementation is derived from LGPL'd glib.
To achieve these goals, the slice allocator uses a sophisticated, layered design that has been inspired by Bonwick's slab allocator (Bonwick94 Jeff Bonwick, The slab allocator: An object-caching kernel memory allocator. USENIX 1994, and Bonwick01 Bonwick and Jonathan Adams, Magazines and vmem: Extending the slab allocator to many cpu's and arbitrary resources. USENIX 2001)
It uses posix_memalign() to optimize allocations of many equally-sized chunks, and has per-thread free lists (the so-called magazine layer) to quickly satisfy allocation requests of already known structure sizes. This is accompanied by extra caching logic to keep freed memory around for some time before returning it to the system. Memory that is unused due to alignment constraints is used for cache colorization (random distribution of chunk addresses) to improve CPU cache utilization. The caching layer of the slice allocator adapts itself to high lock contention to improve scalability.
The slice allocator can allocate blocks as small as two pointers, and unlike malloc(), it does not reserve extra space per block. For large block sizes, mg_slice_new() and mg_slice_alloc() will automatically delegate to the system malloc() implementation. For newly written code it is recommended to use the new mg_slice
API instead of malloc() and friends, as long as objects are not resized during their lifetime and the object size used at allocation time is still available when freeing.
Here is an example for using the slice allocator:
And here is an example for using the slice allocator with data structures:
#define mg_slice_delete | ( | type, | |
mem | |||
) |
Free a block of memory.
This is a convenience macro to free a block of memory that has been allocated from the slice allocator.
It calls mg_slice_free() using sizeof(type)
as the block size.
Note that the exact release behaviour can be changed with the [MG_DEBUG=gc-friendly
][MG_DEBUG] environment variable, also see [MG_SLICE
][MG_SLICE] for related debugging options.
If mem is NULL, this macro does nothing.
type | The type of the block to free, typically a structure name. |
mem | A pointer to the block to free. |
Since: 4.0.0
#define mg_slice_delete_chain | ( | type, | |
mem_chain, | |||
next | |||
) |
Free a linked list of memory blocks.
This function frees a linked list of memory blocks of structure type type.
The memory blocks must be equal-sized, allocated via mg_slice_alloc() or mg_slice_alloc0() and linked together by a next pointer. The name of the next field in type is passed as third argument.
Note that the exact release behaviour can be changed with the [MG_DEBUG=gc-friendly
][MG_DEBUG] environment variable, also see [MG_SLICE
][MG_SLICE] for related debugging options.
If mem_chain is NULL, this function does nothing.
type | The type of the mem_chain blocks. |
mem_chain | A pointer to the first block of the chain. |
next | The field name of the next pointer in type. |
Since: 4.0.0
#define mg_slice_dup | ( | type, | |
mem | |||
) |
Duplicate a structure.
This is a convenience macro to duplicate a block of memory using the slice allocator.
It calls mg_slice_copy() with sizeof(type)
and casts the returned pointer to a pointer of the given type, avoiding a type cast in the source code. Note that the underlying slice allocation mechanism can be changed with the [MG_SLICE=always-malloc
][MG_SLICE] environment variable.
This can never return NULL.
type | The type to duplicate, typically a structure name. |
mem | The memory to copy into the allocated block. |
Since: 4.0.0
#define mg_slice_new | ( | type | ) | ((type*)mg_slice_alloc(sizeof (type))) |
The macro to allocate a slice memory for a structure.
This is a convenience macro to allocate a block of memory from the slice allocator.
It calls mg_slice_alloc() with sizeof(type)
and casts the returned pointer to a pointer of the given type, avoiding a type cast in the source code. Note that the underlying slice allocation mechanism can be changed with the [MG_SLICE=always-malloc
][MG_SLICE] environment variable.
This can never return NULL as the minimum allocation size from sizeof(type)
is 1 byte.
type | the type to allocate, typically a structure name. |
Since: 4.0.0
#define mg_slice_new0 | ( | type | ) | ((type*)mg_slice_alloc0(sizeof (type))) |
The macro to allocate a zero'd slice memory for a structure.
This is a convenience macro to allocate a block of memory from the slice allocator and set the memory to 0.
It calls mg_slice_alloc0() with sizeof(type)
and casts the returned pointer to a pointer of the given type, avoiding a type cast in the source code.
Note that the underlying slice allocation mechanism can be changed with the [MG_SLICE=always-malloc
][MG_SLICE] environment variable.
This can never return NULL as the minimum allocation size from sizeof(type)
is 1 byte.
type | The type to allocate, typically a structure name. |
Since: 4.0.0
void * mg_slice_alloc | ( | size_t | block_size | ) |
Allocate a slice memory.
Allocates a block of memory from the slice allocator. The block address handed out can be expected to be aligned to at least 1 * sizeof (void*), though in general slices are 2 * sizeof (void*) bytes aligned, if a malloc() fallback implementation is used instead, the alignment may be reduced in a libc dependent fashion. Note that the underlying slice allocation mechanism can be changed with the [MG_SLICE=always-malloc
][MG_SLICE] environment variable.
block_size | The number of bytes to allocate. |
Since: 4.0.0
void * mg_slice_alloc0 | ( | size_t | block_size | ) |
Allocate a slice memory and initialize the memory to zero.
Allocates a block of memory via mg_slice_alloc() and initializes the returned memory to 0. Note that the underlying slice allocation mechanism can be changed with the [MG_SLICE=always-malloc
][MG_SLICE] environment variable.
block_size | The number of bytes to allocate. |
Since: 4.0.0
void * mg_slice_copy | ( | size_t | block_size, |
const void * | mem_block | ||
) |
Allocate and copy a slice.
Allocates a block of memory from the slice allocator and copies block_size bytes into it from mem_block.
block_size | The number of bytes to allocate. |
mem_block | The memory to copy. |
Since: 4.0.0
void mg_slice_free | ( | size_t | block_size, |
void * | mem_block | ||
) |
Free a block of memory.
This function frees a block of memory. The memory must have been allocated via mg_slice_alloc or mg_slice_alloc0 and the block_size has to match the size specified upon allocation. Note that the exact release behaviour can be changed with the [MG_DEBUG=gc-friendly
][MG_DEBUG] environment variable, also see [MG_SLICE
][MG_SLICE] for related debugging options.
If mem_block is NULL, this function does nothing.
block_size | The size of the block. |
mem_block | A pointer to the block to free. |
Since: 4.0.0
void mg_slice_free_chain_with_offset | ( | size_t | block_size, |
void * | mem_chain, | ||
size_t | next_offset | ||
) |
Free a linked list of memory blocks.
The memory blocks must be equal-sized, allocated via mg_slice_alloc() or mg_slice_alloc0() and linked together by a next pointer. The offset of the next field in each block is passed as the third argument next_offset.
Note that the exact release behaviour can be changed with the [MG_DEBUG=gc-friendly
][MG_DEBUG] environment variable, also see [MG_SLICE
][MG_SLICE] for related debugging options.
If mem_chain is NULL, this function does nothing.
block_size | The size of the blocks. |
mem_chain | A pointer to the first block of the chain. |
next_offset | The offset of the next field in the blocks. |
Since: 4.0.0