/* init slab * struct slab + obj_num * (unsigned int)*/ static int slab_init(kmem_cache_t *cachep, struct slab *slabp, void *args) { if(cachep == NULL || slabp == NULL || args == NULL) { return -1; } int i = 0; void *ptr = args; slabp->free = 0; slabp->inuse = 0; INIT_LIST_HEAD(&(slabp->list)); ptr += (cachep->obj_num + 1) * sizeof(unsigned int); slabp->s_mem = ptr; for(i = 0; i < cachep->obj_num; ++i) { slab_bufctl(slabp)[i] = i + 1; } slab_bufctl(slabp)[i - 1] = BUFCTL_END; return 0; }
void * slab_get_obj(struct kmem_cache_t *cachep, struct slab *slabp) { if(cachep == NULL || slabp == NULL) return NULL; struct list_head *item = NULL; if(slabp->free == BUFCTL_END) { printf("not free obj."); return NULL; } void *objp = index_to_obj(cachep, slabp, slabp->free); unsigned int next = slab_bufctl(slabp)[slabp->free]; /*printf("next = %d",next);*/ slabp->inuse++; slabp->free = next; /* move to the full list*/ if(slabp->inuse == cachep->obj_num) { item = &(slabp->list); list_del(item); list_add(item, &(cachep->lists.full)); } return objp; }
static int slab_free_obj(kmem_cache_t *cachep, struct slab *slabp, void *objp) { if(cachep == NULL || slabp == NULL || objp == NULL) { return -1; } struct list_head *item = NULL; unsigned int objnr = obj_to_index(cachep, slabp, objp); if(objnr > cachep->obj_num) { return -2; } slab_bufctl(slabp)[objnr] = slabp->free; set_slab_free(slabp, objnr); dec_slab_use(slabp); /* move to the free list */ if(slabp->inuse == 0) { item = &(slabp->list); list_del(item); list_add(item, &(cachep->kmem_lists.free)); } inc_cache_free(cachep); return 0; }
static void * slab_get_obj(kmem_cache_t *cachep, struct slab *slabp) { if(cachep == NULL || slabp == NULL) { return NULL; } struct list_head *item = NULL; if(slabp->free == BUFCTL_END) { DD("not free obj."); return NULL; } void *objp = index_to_obj(cachep, slabp, slabp->free); uint32_t next = slab_bufctl(slabp)[slabp->free]; inc_slab_use(slabp); set_slab_free(slabp, next); /* move to the full list*/ if(slabp->inuse == cachep->obj_num) { item = &(slabp->list); list_del(item); list_add(item, &(cachep->kmem_lists.full)); } dec_cache_free(cachep); return objp; }
/* init slab * struct slab + obj_num * (unsigned int)*/ int slab_init(struct kmem_cache_t *cachep, struct slab *slabp, void *args) { int i = 0; void *ptr = args; /*slabp->free = cachep->obj_num;*/ slabp->free = 0; slabp->inuse = 0; INIT_LIST_HEAD(&(slabp->list)); /*printf("obj num = %d.",cachep->obj_num);*/ ptr += (cachep->obj_num + 1) * sizeof(unsigned int); slabp->s_mem = ptr; for(i = 0;i < cachep->obj_num; ++i) { slab_bufctl(slabp)[i] = i + 1; } slab_bufctl(slabp)[i - 1] = BUFCTL_END; return 0; }
int print_slab_info(struct slab *slabp) { if(slabp == NULL) { return -1; } printf("s_mem = %x.", slabp->s_mem); printf("free = %d.", slabp->free); printf("inuse = %d.\n", slabp->inuse); int tmp = slabp->free; while(tmp != BUFCTL_END) { printf("next = %d.",tmp); tmp = slab_bufctl(slabp)[tmp]; } printf("\n"); return 0; }
int slab_free_obj(struct kmem_cache_t *cachep, struct slab *slabp, void *objp) { if(cachep == NULL || slabp == NULL || objp == NULL) return -1; struct list_head *item = NULL; unsigned int objnr = obj_to_index(cachep, slabp, objp); if(objnr > cachep->obj_num) return -2; slab_bufctl(slabp)[objnr] = slabp->free; slabp->free = objnr; slabp->inuse--; /* move to the free list */ if(slabp->inuse == 0) { item = &(slabp->list); list_del(item); list_add(item, &(cachep->lists.free)); } return 0; }