Skip to content
HEAP_BUCKET_NONE

HEAP_BUCKET_NONE

Description

Sentinel for “no bucket” / “list end” in the hash table and warm lists. 0xFFFFFFFFu is unreachable as a real bucket index since pages_cap is bounded well below 2^32.

Usage example (Cross-references)

Usage examples (Cross-references)
    static FORCE_INLINE u32 heap_hash_lookup(const HeapPage *pages, u32 mask, void *page_base) {
        if (!pages)
            return HEAP_BUCKET_NONE;
        u32 idx = heap_hash_bucket((u64)page_base, mask);
        while (pages[idx].page) {
            idx = (idx + 1u) & mask;
        }
        return HEAP_BUCKET_NONE;
    }
        HeapPage *pages      = heap->pages;
        u32       head       = heap->class_warm_head[cls];
        pages[idx].prev_warm = HEAP_BUCKET_NONE;
        pages[idx].next_warm = head;
        if (head != HEAP_BUCKET_NONE) {
        pages[idx].prev_warm = HEAP_BUCKET_NONE;
        pages[idx].next_warm = head;
        if (head != HEAP_BUCKET_NONE) {
            pages[head].prev_warm = idx;
        }
        u32       prev  = pages[idx].prev_warm;
        u32       next  = pages[idx].next_warm;
        if (prev != HEAP_BUCKET_NONE) {
            pages[prev].next_warm = next;
        } else {
            heap->class_warm_head[cls] = next;
        }
        if (next != HEAP_BUCKET_NONE) {
            pages[next].prev_warm = prev;
        }
            pages[next].prev_warm = prev;
        }
        pages[idx].prev_warm = HEAP_BUCKET_NONE;
        pages[idx].next_warm = HEAP_BUCKET_NONE;
    }
        }
        pages[idx].prev_warm = HEAP_BUCKET_NONE;
        pages[idx].next_warm = HEAP_BUCKET_NONE;
    }
                HeapPage *moved    = &pages[idx];
                u8        cls      = moved->class_idx;
                if (moved->prev_warm != HEAP_BUCKET_NONE) {
                    pages[moved->prev_warm].next_warm = idx;
                } else if (heap->class_warm_head[cls] == cursor) {
                    heap->class_warm_head[cls] = idx;
                }
                if (moved->next_warm != HEAP_BUCKET_NONE) {
                    pages[moved->next_warm].prev_warm = idx;
                }
        // Rebuild warm heads from scratch as we reinsert.
        for (u32 c = 0; c < HEAP_NUM_CLASSES; c++) {
            heap->class_warm_head[c] = HEAP_BUCKET_NONE;
        }
                // Has free slots -> belongs at warm-list head for its class.
                u32 head       = heap->class_warm_head[cls];
                desc.prev_warm = HEAP_BUCKET_NONE;
                desc.next_warm = head;
                fresh[ins]     = desc;
                desc.next_warm = head;
                fresh[ins]     = desc;
                if (head != HEAP_BUCKET_NONE) {
                    fresh[head].prev_warm = ins;
                }
            } else {
                // Full page -> not in any warm list.
                desc.prev_warm = HEAP_BUCKET_NONE;
                desc.next_warm = HEAP_BUCKET_NONE;
                fresh[ins]     = desc;
                // Full page -> not in any warm list.
                desc.prev_warm = HEAP_BUCKET_NONE;
                desc.next_warm = HEAP_BUCKET_NONE;
                fresh[ins]     = desc;
            }
                return i;
        }
        return HEAP_BUCKET_NONE;
    }
                return i;
        }
        return HEAP_BUCKET_NONE;
    }
        // IS the warm one the next alloc will use, so we keep it. If there's
        // any other warm page already, reclaiming this one is safe.
        if (heap->class_warm_head[cls] == idx && d->next_warm == HEAP_BUCKET_NONE) {
            return;
        }
            u32 new_cap = heap->pages_cap ? heap->pages_cap * 2u : HEAP_PAGES_INITIAL_CAP;
            if (!heap_hash_resize(heap, new_cap)) {
                return HEAP_BUCKET_NONE;
            }
        }
            base = os_page_map(&heap->base, HEAP_OS_PAGE_SIZE);
            if (!base)
                return HEAP_BUCKET_NONE;
        }
        // Recycled pages keep whatever bytes the previous user wrote into
                  .used_count = 0u,
                  .class_idx  = cls,
                  .prev_warm  = HEAP_BUCKET_NONE,
                  .next_warm  = HEAP_BUCKET_NONE,
            };
                  .class_idx  = cls,
                  .prev_warm  = HEAP_BUCKET_NONE,
                  .next_warm  = HEAP_BUCKET_NONE,
            };
            heap_set_tail_bits(desc.bitmap, slots, bm_words);
        // Try to reclaim a retained mapping of the same os_pages count.
        u32 fr_idx = heap_xl_freed_find_match(heap, (u32)os_pages);
        if (fr_idx != HEAP_BUCKET_NONE) {
            HeapPageXL ent         = heap_xl_freed_swap_remove(heap, fr_idx);
            ptr                    = ent.page;
            // head is guaranteed to have a free slot.
            u32 idx = self->class_warm_head[cls];
            if (idx == HEAP_BUCKET_NONE) {
                idx = heap_grow_class(self, cls);
                if (idx == HEAP_BUCKET_NONE) {
            if (idx == HEAP_BUCKET_NONE) {
                idx = heap_grow_class(self, cls);
                if (idx == HEAP_BUCKET_NONE) {
    #if FEATURE_ALLOC_STATS
                    self->base.stats.failed_allocations += 1u;
    // branch on it to choose the right free path.
    static size heap_recover_size(HeapAllocator *heap, void *ptr, u32 *idx_out, bool *is_xl_out) {
        *idx_out   = HEAP_BUCKET_NONE;
        *is_xl_out = false;
        if (!ptr)
        // trip the foreign-ptr abort, not silently fix up to the base.
        u32 xi = heap_xl_in_use_find(heap, ptr);
        if (xi != HEAP_BUCKET_NONE) {
            *idx_out   = xi;
            *is_xl_out = true;
        void *page_base = (void *)((u64)ptr & ~(u64)(HEAP_PAGE_SIZE - 1u));
        u32   b         = heap_hash_lookup(heap->pages, heap->pages_cap - 1u, page_base);
        if (b != HEAP_BUCKET_NONE) {
            *idx_out = b;
            return (size)heap_class_size[heap->pages[b].class_idx];
    // drift.
    #define HEAP_CLASS_WARM_HEAD_NONE                                                                                      \
        {HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
    #define HEAP_CLASS_WARM_HEAD_NONE                                                                                      \
        {HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
        {HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE}
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE}
    _Static_assert(HEAP_NUM_CLASSES == 8, "HEAP_CLASS_WARM_HEAD_NONE has 8 entries; sync with HEAP_NUM_CLASSES");
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE,                                                                                                 \
         HEAP_BUCKET_NONE}
    _Static_assert(HEAP_NUM_CLASSES == 8, "HEAP_CLASS_WARM_HEAD_NONE has 8 entries; sync with HEAP_NUM_CLASSES");
Last updated on