eightbitraptor[wiki]

Walking the Heap

This function walks the heap - page at a time. The callback provided

// objspace - the struct that holds the heaps and bookkeeping information about them
// callback - our callback function ()
// data - don't know what this is used for yet.
static void
objspace_each_objects_without_setup(rb_objspace_t *objspace, each_obj_callback *callback, void *data)
{
    // basically an unsigned long long I think
    size_t i;
    // a pointer to a heap page object
    struct heap_page *page;
    // declare 2 RVALUE's objects pstart and pend. Initialize pstart to NULL
    RVALUE *pstart = NULL, *pend;

    i = 0;

    //heap allocated pages => the count of allocated pages stored in the objspace struct
    while (i < heap_allocated_pages) {

        //decrement the counter if
        //  - the counter is a positive integer AND
        //  - the starting page is lower than the previous page's starting address
        while (0 < i && pstart < heap_pages_sorted[i-1]->start)              i--;
        // increment the counter if
        //  - the counter is less than the count of allocated pages AND
        //  - the starting page is higher than
        while (i < heap_allocated_pages && heap_pages_sorted[i]->start <= pstart) i++;
        if (heap_allocated_pages <= i) break;

        page = heap_pages_sorted[i];

        pstart = page->start;
        pend = pstart + page->total_slots;

        if ((*callback)(pstart, pend, sizeof(RVALUE), data)) {
          break;
        }
    }
}