void * kmem_get_obj(struct kmem_cache_t *cachep) { struct slab *slabp = NULL; void *obj = NULL; repeat: slabp = kmem_get_slab(cachep); /*printf("slabp = %x.", slabp);*/ obj = slab_get_obj(cachep, slabp); if(obj == NULL) { if(cachep->nr_pages >= pow(2,MAX_SLAB_PAGES)) { return NULL; } else { cache_grow(cachep); goto repeat; } } return obj; }
/* Add an element to the target hash table */ int ht_add(struct hashtable *t, void *key, void *data) { int ret; unsigned int index; /* If the element isn't in the table ht_insert() will store * the index of the free ht_ele in the integer pointer by *index */ ret = ht_insert(t, key, &index); if (ret != HT_OK) return ret; /* Allocates the memory and stores key */ #ifdef AHT_USE_SLAB if ((t->table[index] = slab_get_obj(t->cache)) == NULL) #else if ((t->table[index] = malloc(sizeof(struct ht_ele))) == NULL) #endif /* AHT_USE_SLAB */ return HT_NOMEM; /* Store the pointers */ t->table[index]->key = key; t->table[index]->data = data; t->used++; return HT_OK; }
void * kmem_get_obj(kmem_cache_t *cachep) { struct slab *slabp = NULL; void *obj = NULL; repeat: slabp = kmem_get_slab(cachep); obj = slab_get_obj(cachep, slabp); if(obj) { return obj; } /* get obj is NULL but this kmem_cache_t has free objs */ else { if(obj == NULL && cachep->nr_frees > 0) { return NULL; } else { if(cachep->nr_pages >= pow(2,MAX_SLAB_PAGES)) { return NULL; } else { cache_grow(cachep); goto repeat; } } } return obj; }