Skip to content
SLAB_ROUNDUP_POW2

SLAB_ROUNDUP_POW2

Description

Round a requested slot size up to the nearest power of two in [16, 4096]. Sub-16-byte requests round up to 16 (the MAX_ALIGN-derived minimum). Requests above 4096 fall through to 4096; the validator catches them at first grow when it compares against the actual OS page size.

This is the policy that lets callers write SlabAllocatorInit( sizeof(MyType)) for arbitrary type sizes – e.g. a 28-byte struct goes into 32-byte slots, like the way glibc rounds a 2-byte malloc up to its 24-byte minimum chunk.

Parameters

Name Direction Description
s in Requested slot size in bytes.

Success

Returns the smallest supported power-of-two slot size >= s, clamped to [16, 4096].

Failure

No runtime failure mode. For s > 4096 returns 4096; first-allocation validation aborts when the rounded slot does not fit in the OS page size.

Usage example (Cross-references)

Usage examples (Cross-references)
            .slabs_cap             = 0,                                                                                    \
            .bitmaps               = NULL,                                                                                 \
            .slot_size             = SLAB_ROUNDUP_POW2(slot_size_bytes),                                             \
            .slot_size_shift       = (u8)SLAB_SHIFT_FROM_SIZE(SLAB_ROUNDUP_POW2(slot_size_bytes)),             \
            .bitmap_words_per_slab = 0,                                                                                    \
            .bitmaps               = NULL,                                                                                 \
            .slot_size             = SLAB_ROUNDUP_POW2(slot_size_bytes),                                             \
            .slot_size_shift       = (u8)SLAB_SHIFT_FROM_SIZE(SLAB_ROUNDUP_POW2(slot_size_bytes)),             \
            .bitmap_words_per_slab = 0,                                                                                    \
            .page                  = PageAllocatorInit()                                                                   \
            .slabs_cap             = 0,                                                                                    \
            .bitmaps               = NULL,                                                                                 \
            .slot_size             = SLAB_ROUNDUP_POW2(slot_size_bytes),                                             \
            .slot_size_shift       = (u8)SLAB_SHIFT_FROM_SIZE(SLAB_ROUNDUP_POW2(slot_size_bytes)),             \
            .bitmap_words_per_slab = 0,                                                                                    \
            .bitmaps               = NULL,                                                                                 \
            .slot_size             = SLAB_ROUNDUP_POW2(slot_size_bytes),                                             \
            .slot_size_shift       = (u8)SLAB_SHIFT_FROM_SIZE(SLAB_ROUNDUP_POW2(slot_size_bytes)),             \
            .bitmap_words_per_slab = 0,                                                                                    \
            .page                  = PageAllocatorInit()                                                                   \
Last updated on