src/runtime/alloc.c
/// Moves to the next block with open lines, and moves to the first open line.
/// Prioritizes recycling nonempty blocks over allocating into empty ones.
/// Returns true if it found one, false if there are no more blocks with open lines.
Moves to the next block with open lines, and moves to the first open line.
Prioritizes recycling nonempty blocks over allocating into empty ones.
Returns true if it found one, false if there are no more blocks with open lines.
Moves to the next block with open lines and moves to the first open line . Prioritizes recycling nonempty blocks over allocating into empty ones . Returns true if it found one false if there are no more blocks with open lines .
static bool next_block() {
    uint32_t block_idx;
    bool empty;
    if (local_alloc.block) {
        block_idx = (local_alloc.block - all_blocks[0].start) / BLOCK_SIZE;
        empty = all_blocks[block_idx].empty;
    } else {
        block_idx = -1;
        empty = false;
    }
    if (!empty) {
        while (++block_idx < num_blocks) {
            BlockMetadata block = all_blocks[block_idx];
            if (block.available && !block.empty) {
                local_alloc.block = block.start;
                local_alloc.ptr = local_alloc.end = block.start + BLOCK_SIZE;
                all_blocks[block_idx].available = false;
                if (next_line()) {
                    return true;
                } else {
                    continue;
                }
            }
        }
        block_idx = -1;
    }
    while (++block_idx < num_blocks) {
        BlockMetadata block = all_blocks[block_idx];
        if (block.available && block.empty) {
            local_alloc.block = local_alloc.end = block.start;
            local_alloc.ptr = block.start + BLOCK_SIZE;
            all_blocks[block_idx].available = false;
            return true;
        }
    }
    return false;
}
static bool next_block ( ) { uint32_t block_idx ; bool empty ; if ( local_alloc . block ) { block_idx = ( local_alloc . block - all_blocks [ 0 ] . start ) / BLOCK_SIZE ; empty = all_blocks [ block_idx ] . empty ; } else { block_idx = -1 ; empty = false ; } if ( ! empty ) { while ( ++ block_idx < num_blocks ) { BlockMetadata block = all_blocks [ block_idx ] ; if ( block . available && ! block . empty ) { local_alloc . block = block . start ; local_alloc . ptr = local_alloc . end = block . start + BLOCK_SIZE ; all_blocks [ block_idx ] . available = false ; if ( next_line ( ) ) { return true ; } else { continue ; } } } block_idx = -1 ; } while ( ++ block_idx < num_blocks ) { BlockMetadata block = all_blocks [ block_idx ] ; if ( block . available && block . empty ) { local_alloc . block = local_alloc . end = block . start ; local_alloc . ptr = block . start + BLOCK_SIZE ; all_blocks [ block_idx ] . available = false ; return true ; } } return false ; }
Moves to the next block with open lines, and moves to the first open line.
Moves to the next block with open lines and moves to the first open line .
// Search for an open block // Try to recycle a block first // printf("Attempting to recycle block starting at block %u\n", block_idx); // This block doesn't have open lines // printf("Skipping full block %u\n", block_idx); // printf("Skipping block %u\n", block_idx); // We couldn't find a block to recycle, so find an empty block starting at the top // Try to find an empty block // printf("Attempting to find empty block starting at block %u\n", block_idx); // If it's empty, don't bother calling next_line(), just use the whole block // printf("Skipping block %u\n", block_idx); // We couldn't find anything