MiniGUI API Reference (MiniGUI-Processes)  v5.0.6
A mature and proven cross-platform GUI system for embedded and smart IoT devices
Macros | Functions
Slice Memory Allocator

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...
 

Detailed Description

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:

char *mem[10000];
int i;
// Allocate 10000 blocks.
for (i = 0; i < 10000; i++)
{
mem[i] = mg_slice_alloc (50);
// Fill in the memory with some junk.
for (j = 0; j < 50; j++)
mem[i][j] = i * j;
}
// Now free all of the blocks.
for (i = 0; i < 10000; i++)
mg_slice_free (50, mem[i]);

And here is an example for using the slice allocator with data structures:

MyStruct *array;
// Allocate one block, using the mg_slice_new() macro.
array = mg_slice_new (MyStruct);
// We can now use array just like a normal pointer to a structure.
array->data = NULL;
array->len = 0;
array->alloc = 0;
array->zero_terminated = (zero_terminated ? 1 : 0);
array->clear = (clear ? 1 : 0);
array->elt_size = elt_size;
// We can free the block, so it can be reused.
mg_slice_delete (MyStruct, array);

Macro Definition Documentation

◆ mg_slice_delete

#define mg_slice_delete (   type,
  mem 
)
Value:
do { \
if (1) mg_slice_free (sizeof (type), (mem)); \
else (void) ((type*) 0 == (mem)); \
} while(0)

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.

Parameters
typeThe type of the block to free, typically a structure name.
memA pointer to the block to free.
Returns
None.

Since: 4.0.0

Definition at line 5006 of file minigui.h.

◆ mg_slice_delete_chain

#define mg_slice_delete_chain (   type,
  mem_chain,
  next 
)
Value:
do { \
if (1) mg_slice_free_chain_with_offset (sizeof (type), \
(mem_chain), G_STRUCT_OFFSET (type, next)); \
else (void) ((type*) 0 == (mem_chain)); \
} while(0)

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.

Parameters
typeThe type of the mem_chain blocks.
mem_chainA pointer to the first block of the chain.
nextThe field name of the next pointer in type.

Since: 4.0.0

Definition at line 5036 of file minigui.h.

◆ mg_slice_dup

#define mg_slice_dup (   type,
  mem 
)
Value:
(1 ? (type*) mg_slice_copy (sizeof (type), (mem)) \
: ((void) ((type*) 0 == (mem)), (type*) 0))

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.

Parameters
typeThe type to duplicate, typically a structure name.
memThe memory to copy into the allocated block.
Returns
A pointer to the allocated block, cast to a pointer to type.

Since: 4.0.0

Definition at line 4980 of file minigui.h.

◆ mg_slice_new

#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.

Parameters
typethe type to allocate, typically a structure name.
Returns
A pointer to the allocated block, cast to a pointer to type.

Since: 4.0.0

Definition at line 4917 of file minigui.h.

◆ mg_slice_new0

#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.

Parameters
typeThe type to allocate, typically a structure name.
Returns
A pointer to the allocated block, cast to a pointer to type.

Since: 4.0.0

Definition at line 4944 of file minigui.h.

Function Documentation

◆ mg_slice_alloc()

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.

Parameters
block_sizeThe number of bytes to allocate.
Returns
A pointer to the allocated memory block, which will be NULL if and only if mem_size is 0.

Since: 4.0.0

◆ mg_slice_alloc0()

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.

Parameters
block_sizeThe number of bytes to allocate.
Returns
A pointer to the allocated block, which will be NULL if and only if mem_size is 0.

Since: 4.0.0

◆ mg_slice_copy()

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.

Parameters
block_sizeThe number of bytes to allocate.
mem_blockThe memory to copy.
Returns
A pointer to the allocated memory block, which will be NULL if and only if mem_size is 0.
Note
mem_block must be non-NULL if block_size is non-zero.

Since: 4.0.0

◆ mg_slice_free()

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.

Parameters
block_sizeThe size of the block.
mem_blockA pointer to the block to free.

Since: 4.0.0

◆ mg_slice_free_chain_with_offset()

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.

Parameters
block_sizeThe size of the blocks.
mem_chainA pointer to the first block of the chain.
next_offsetThe offset of the next field in the blocks.

Since: 4.0.0

NULL
#define NULL
A value indicates null pointer.
Definition: common.h:369
mg_slice_free
MG_EXPORT void mg_slice_free(size_t block_size, void *mem_block)
Free a block of memory.
mg_slice_alloc
MG_EXPORT void * mg_slice_alloc(size_t block_size)
Allocate a slice memory.
mg_slice_free_chain_with_offset
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.
mg_slice_delete
#define mg_slice_delete(type, mem)
Free a block of memory.
Definition: minigui.h:5006
mg_slice_copy
MG_EXPORT void * mg_slice_copy(size_t block_size, const void *mem_block)
Allocate and copy a slice.
mg_slice_new
#define mg_slice_new(type)
The macro to allocate a slice memory for a structure.
Definition: minigui.h:4917