Skip to content
PageAllocatorPageSize

PageAllocatorPageSize

Description

Query the system page size in bytes. Cached after first call so repeated lookups are cheap.

Success

Returns the OS page size (typically 4096 on x86_64).

Failure

Returns 4096 if the platform query fails.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    size PageAllocatorPageSize(void) {
        if (!cached_page_size) {
            cached_page_size = page_allocator_query_page_size();
    static size page_allocator_effective_alignment(const Allocator *alloc) {
        size requested = alloc ? alloc->alignment : 0;
        size page_size = PageAllocatorPageSize();
        if (requested < page_size) {
            return page_size;
    
    static void *page_allocator_allocate(Allocator *alloc, size bytes, bool zeroed) {
        size  page_size = PageAllocatorPageSize();
        size  rounded;
        void *ptr;
        if (new_size == 0) {
            size align     = page_allocator_effective_alignment(alloc);
            size page_size = PageAllocatorPageSize();
            size rounded =
                align > page_size ? ((old_size + align - 1) & ~(align - 1)) : page_allocator_round_up(old_size, page_size);
    
            size align     = page_allocator_effective_alignment(alloc);
            size page_size = PageAllocatorPageSize();
            size rounded =
                align > page_size ? ((old_size + align - 1) & ~(align - 1)) : page_allocator_round_up(old_size, page_size);
    static void page_allocator_deallocate(Allocator *alloc, void *ptr, size bytes) {
        size align     = page_allocator_effective_alignment(alloc);
        size page_size = PageAllocatorPageSize();
        size rounded = align > page_size ? ((bytes + align - 1) & ~(align - 1)) : page_allocator_round_up(bytes, page_size);
        page_allocator_unmap(ptr, rounded);
    
    static size arena_chunk_size_for(size need_bytes) {
        size page    = PageAllocatorPageSize();
        size minimum = MISRA_ARENA_DEFAULT_CHUNK_SIZE;
        size wanted  = need_bytes > minimum ? need_bytes : minimum;
    static bool heap_grow_bin(int bin) {
        size  slot_size = heap_bin_size(bin);
        size  page_size = PageAllocatorPageSize();
        char *page      = (char *)AllocatorAlloc(&g_heap.page, page_size, false);
        if (!page) {
    
    static bool test_page_size_query(void) {
        size ps = PageAllocatorPageSize();
        bool ok = (ps >= 4096) && ((ps & (ps - 1)) == 0);
        if (!ok) {
    static bool test_realloc_grow_then_shrink(void) {
        Allocator alloc = PageAllocator();
        size      page  = PageAllocatorPageSize();
        char     *ptr   = (char *)AllocatorAlloc(&alloc, 64, true);
        bool      ok    = (ptr != NULL);
        // portably.
        Allocator alloc = PageAllocatorAligned(64);
        size      page  = PageAllocatorPageSize();
        void     *ptr   = AllocatorAlloc(&alloc, 1024, true);
        bool      ok    = (ptr != NULL) && (((uintptr_t)ptr & (page - 1u)) == 0);
Last updated on