slab.c
void *
kmem_cache_alloc(kmem_cache_t cp, int flags) {
    void *buf;

    // grow the cache if necessary...
    if (cp->slabs == NULL) 
        kmem_cache_grow(cp);

    if (cp->slabs->bufcount == cp->slab_maxbuf)
        kmem_cache_grow(cp);

    // if this is a small object
    if (cp->size <= SLAB_SMALL_OBJ_SZ) {
        buf = cp->slabs->free_list;
        cp->slabs->free_list = *((void**)buf);
        cp->slabs->bufcount++;   
    }
    else {
        kmem_bufctl_t bufctl = cp->slabs->free_list;
        cp->slabs->free_list = bufctl->next;
        buf = bufctl->buf;
        cp->slabs->bufcount++;
    }

    // if slab is empty
    if (cp->slabs->bufcount == cp->slab_maxbuf)
        __slab_move_to_back(cp, cp->slabs);

    return buf;
}
/* Requests an allocated object from the cache. 

    @cp cache pointer
    @flags flags KM_SLEEP or KM_NOSLEEP
*/
Requests an allocated object from the cache.
Requests an allocated object from the cache .
void *
kmem_cache_alloc(kmem_cache_t cp, int flags) {
    void *buf;
    if (cp->slabs == NULL) 
        kmem_cache_grow(cp);
    if (cp->slabs->bufcount == cp->slab_maxbuf)
        kmem_cache_grow(cp);
    if (cp->size <= SLAB_SMALL_OBJ_SZ) {
        buf = cp->slabs->free_list;
        cp->slabs->free_list = *((void**)buf);
        cp->slabs->bufcount++;   
    }
    else {
        kmem_bufctl_t bufctl = cp->slabs->free_list;
        cp->slabs->free_list = bufctl->next;
        buf = bufctl->buf;
        cp->slabs->bufcount++;
    }
    if (cp->slabs->bufcount == cp->slab_maxbuf)
        __slab_move_to_back(cp, cp->slabs);
    return buf;
}
void * kmem_cache_alloc ( kmem_cache_t cp , int flags ) { void * buf ; if ( cp -> slabs == NULL ) kmem_cache_grow ( cp ) ; if ( cp -> slabs -> bufcount == cp -> slab_maxbuf ) kmem_cache_grow ( cp ) ; if ( cp -> size <= SLAB_SMALL_OBJ_SZ ) { buf = cp -> slabs -> free_list ; cp -> slabs -> free_list = * ( ( void * * ) buf ) ; cp -> slabs -> bufcount ++ ; } else { kmem_bufctl_t bufctl = cp -> slabs -> free_list ; cp -> slabs -> free_list = bufctl -> next ; buf = bufctl -> buf ; cp -> slabs -> bufcount ++ ; } if ( cp -> slabs -> bufcount == cp -> slab_maxbuf ) __slab_move_to_back ( cp , cp -> slabs ) ; return buf ; }
Requests an allocated object from the cache.
Requests an allocated object from the cache .
// grow the cache if necessary... // if this is a small object // if slab is empty