lib/aos/paging3.c
errval_t paging_alloc(struct paging_state *st, void **buf, size_t bytes)
{
    errval_t err;
    struct v_region_metadata *node = st->v_space_list; 
    if (bytes % BASE_PAGE_SIZE != 0) {
        bytes += BASE_PAGE_SIZE - (bytes % BASE_PAGE_SIZE);
    }
    
    struct v_region_metadata *prev = NULL;

    while (node) {
        if (node->type == Nodetype_Free && node->size >= bytes) {
            *buf = (void*) node->base;
           
	    // need to add to the allocated list 
            if (slab_freecount(&st->slabs) < 4 && !st->refillingslabs) {
                st->refillingslabs = true;
                err = st->slabs.refill_func(&st->slabs);
                st->refillingslabs = false;
                
                if (err_is_fail(err)) {
                    debug_printf("paging_map_fixed_attr: slab_refill failed\n");
                    return err;
                }
            }
            struct v_region_metadata *allocated = slab_alloc(&st->slabs);
            allocated->type = Nodetype_Allocated;
            allocated->base = node->base;
            allocated->size = bytes;

            // need to insert at the head
            if (prev == NULL) {
                st->v_space_list = allocated;
            }
            else {
                prev->next = allocated;
            }
                allocated->next = node;

                node->base += bytes; 
                node->size -= bytes;
                if (node->size == 0) {
                    allocated->next = node->next;
                    slab_free(&st->slabs, node);
                }
                return SYS_ERR_OK;
            }
        prev = node;
        node = node->next;
    }
    *buf = NULL;
    return LIB_ERR_OUT_OF_VIRTUAL_ADDR;
}
/**
 *
 * TODO(M2): Implement this function
 * \brief Find a bit of free virtual address space that is large enough to
 *        accomodate a buffer of size `bytes`.
 */
Implement this function
\brief Find a bit of free virtual address space that is large enough to
accomodate a buffer of size `bytes`.
Implement this function \ brief Find a bit of free virtual address space that is large enough to accomodate a buffer of size ` bytes ` .
errval_t paging_alloc(struct paging_state *st, void **buf, size_t bytes)
{
    errval_t err;
    struct v_region_metadata *node = st->v_space_list; 
    if (bytes % BASE_PAGE_SIZE != 0) {
        bytes += BASE_PAGE_SIZE - (bytes % BASE_PAGE_SIZE);
    }
    struct v_region_metadata *prev = NULL;
    while (node) {
        if (node->type == Nodetype_Free && node->size >= bytes) {
            *buf = (void*) node->base;
            if (slab_freecount(&st->slabs) < 4 && !st->refillingslabs) {
                st->refillingslabs = true;
                err = st->slabs.refill_func(&st->slabs);
                st->refillingslabs = false;
                if (err_is_fail(err)) {
                    debug_printf("paging_map_fixed_attr: slab_refill failed\n");
                    return err;
                }
            }
            struct v_region_metadata *allocated = slab_alloc(&st->slabs);
            allocated->type = Nodetype_Allocated;
            allocated->base = node->base;
            allocated->size = bytes;
            if (prev == NULL) {
                st->v_space_list = allocated;
            }
            else {
                prev->next = allocated;
            }
                allocated->next = node;
                node->base += bytes; 
                node->size -= bytes;
                if (node->size == 0) {
                    allocated->next = node->next;
                    slab_free(&st->slabs, node);
                }
                return SYS_ERR_OK;
            }
        prev = node;
        node = node->next;
    }
    *buf = NULL;
    return LIB_ERR_OUT_OF_VIRTUAL_ADDR;
}
errval_t paging_alloc ( struct paging_state * st , void * * buf , size_t bytes ) { errval_t err ; struct v_region_metadata * node = st -> v_space_list ; if ( bytes % BASE_PAGE_SIZE != 0 ) { bytes += BASE_PAGE_SIZE - ( bytes % BASE_PAGE_SIZE ) ; } struct v_region_metadata * prev = NULL ; while ( node ) { if ( node -> type == Nodetype_Free && node -> size >= bytes ) { * buf = ( void * ) node -> base ; if ( slab_freecount ( & st -> slabs ) < 4 && ! st -> refillingslabs ) { st -> refillingslabs = true ; err = st -> slabs . refill_func ( & st -> slabs ) ; st -> refillingslabs = false ; if ( err_is_fail ( err ) ) { debug_printf ( " \n " ) ; return err ; } } struct v_region_metadata * allocated = slab_alloc ( & st -> slabs ) ; allocated -> type = Nodetype_Allocated ; allocated -> base = node -> base ; allocated -> size = bytes ; if ( prev == NULL ) { st -> v_space_list = allocated ; } else { prev -> next = allocated ; } allocated -> next = node ; node -> base += bytes ; node -> size -= bytes ; if ( node -> size == 0 ) { allocated -> next = node -> next ; slab_free ( & st -> slabs , node ) ; } return SYS_ERR_OK ; } prev = node ; node = node -> next ; } * buf = NULL ; return LIB_ERR_OUT_OF_VIRTUAL_ADDR ; }
TODO(M2): Implement this function
 \brief Find a bit of free virtual address space that is large enough to
        accomodate a buffer of size `bytes`.
TODO ( M2 ) : Implement this function \ brief Find a bit of free virtual address space that is large enough to accomodate a buffer of size ` bytes ` .
// need to add to the allocated list  // need to insert at the head