std1.0.0-beta-7 SafeC docs

std :: alloc::slab

Struct std::SlabAllocator

#include <std/alloc/slab.h> source

struct SlabAllocator {
    &heap unsigned char base;
    int owned;
    ?&SlabLink_ free_head;
    unsigned long obj_size;
    unsigned long cap;
    unsigned long used;
    int has_tags;
}

Fields

§&heap unsigned char basesource

heap-backed backing buffer

§int ownedsource

1 if SlabAllocator::new allocated base (destroy() frees it)

§?&SlabLink_ free_headsource

head of the embedded free list (walked inside unsafe)

§unsigned long obj_sizesource

slot size: the requested size rounded up to a multiple of 8

§unsigned long capsource

max number of objects

§unsigned long usedsource

currently allocated count

§int has_tagssource

1 if each slot has an extra 8-byte double-free-detection tag ahead of it (SlabAllocator::new, where this allocator sized+owns 'base' itself, so growing the per-slot stride is safe) — 0 if not (SlabAllocator::over, where 'base' is caller-provided and sized to the documented obj_size*count contract, which adding a tag would silently overrun).

Methods

§static struct SlabAllocator over([]unsigned char buffer, unsigned long obj_size)source · definition

A slab over a caller-provided buffer. Slots are obj_size rounded up to a multiple of 8 (at least 8, to hold the free-list link) and start 8-byte aligned, so 'cap' can come out below buffer.len / obj_size when that rounding doesn't fit. No double-free detection (see has_tags) — the buffer size contract can't grow to fit a tag.

§static struct SlabAllocator new(unsigned long obj_size, unsigned long count)source · definition

A heap-backed slab of count objects (double-free-detecting).

§?&heap unsigned char alloc(unsigned long size, unsigned long align)source · definition

Allocate one object (all obj_size bytes of it) for size bytes aligned to align. null if the slab is full, size > obj_size, or align is above max_align() or not a power of two (0 is taken as 1).

§void dealloc(?&heap unsigned char ptr)source · definition

Return an object to the pool (null: no-op). Aborts with a diagnostic on double-free (SlabAllocator::new-created allocators only — see has_tags above).

§void reset()source · definition

Free every slot at once (slots handed out before become invalid).

§unsigned long available() constsource · definition

Return the number of free slots remaining.

§unsigned long max_align() constsource · definition

The largest alignment alloc honours: the largest power of two dividing obj_size, at most 8 (given an 8-aligned buffer: always for SlabAllocator::new).

§void drop()source · definition

Free the backing buffer (only valid for heap-backed allocators from SlabAllocator::new).

§void destroy() consumesource · definition

= drop()