MemorySlice

Description Memory slices provide a space-efficient and multi-processing scalable way to allocate equal-sized pieces of memory, just like the original GMemChunks (from GLib <= 2.8), while avoiding their excessive memory-waste, scalability and performance problems. To achieve these goals, the slice allocator uses a sophisticated, layered design that has been inspired by Bonwick's slab allocator [6]. 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, g_slice_new() and g_slice_alloc() will automatically delegate to the system malloc() implementation. For newly written code it is recommended to use the new g_slice API instead of g_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.

Members

Static functions

alloc
void* alloc(gsize blockSize)

Allocates a block of memory from the slice allocator. The block adress 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 G_SLICE=always-malloc environment variable. Since 2.10

alloc0
void* alloc0(gsize blockSize)

Allocates a block of memory via g_slice_alloc() and initialize the returned memory to 0. Note that the underlying slice allocation mechanism can be changed with the G_SLICE=always-malloc environment variable. Since 2.10

copy
void* copy(gsize blockSize, void* memBlock)

Allocates a block of memory from the slice allocator and copies block_size bytes into it from mem_block. Since 2.14

free1
void free1(gsize blockSize, void* memBlock)

Frees a block of memory. The memory must have been allocated via g_slice_alloc() or g_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 G_DEBUG=gc-friendly environment variable, also see G_SLICE for related debugging options. Since 2.10

freeChainWithOffset
void freeChainWithOffset(gsize blockSize, void* memChain, gsize nextOffset)

Frees a linked list of memory blocks of structure type type. The memory blocks must be equal-sized, allocated via g_slice_alloc() or g_slice_alloc0() and linked together by a next pointer (similar to GSList). The offset of the next field in each block is passed as third argument. Note that the exact release behaviour can be changed with the G_DEBUG=gc-friendly environment variable, also see G_SLICE for related debugging options. Since 2.10

Meta