obj * slab_alloc(slab* slab_allocator) { if (0 >= slab_allocator->size) { _alloc_chunk(slab_allocator); } slab_allocator->size--; obj *object = slab_allocator->head; slab_allocator->head = object->next; return object; }
slab * create_slab_allocator(int size) { if (0 >= size) { perror("Size is less than or equal to 0"); exit(-1); } else { slab *slab_allocator = malloc(sizeof(slab)); slab_allocator->size = size; slab_allocator->chunk_size = size; _alloc_chunk(slab_allocator); } }
static uint32_t _split_table(struct btree *btree, struct btree_table *table, char *key, uint32_t *offset) { memcpy(key, table->items[TABLE_SIZE / 2].key, KEY_MAX_LENGTH); *offset = table->items[TABLE_SIZE / 2].offset; struct btree_table *new_table = _alloc_table(); new_table->size = table->size - TABLE_SIZE / 2 - 1; table->size = TABLE_SIZE / 2; memcpy(new_table->items, &table->items[TABLE_SIZE / 2 + 1], (new_table->size + 1) * sizeof(struct btree_item)); uint32_t new_table_offset = _alloc_chunk(btree, sizeof *new_table); _flush_table(btree, new_table, new_table_offset); return new_table_offset; }
uint32_t _insert_toplevel(struct btree *btree, uint32_t *table_offset, struct slice *sk, struct slice *sv) { uint32_t offset = 0; uint32_t ret = 0; uint32_t right_child = 0; if (*table_offset != 0) { ret = _insert_table(btree, *table_offset, sk, sv); struct btree_table *table = _get_table(btree, *table_offset); if (table->size < TABLE_SIZE-1) { _put_table(btree, table, *table_offset); return ret; } right_child = _split_table(btree, table, sk->data, &offset); _flush_table(btree, table, *table_offset); } else { ret = offset = _insert_data(btree, sv); } /* create new top level table */ struct btree_table *new_table = _alloc_table(); new_table->size = 1; memcpy(new_table->items[0].key, sk->data, sk->len); new_table->items[0].offset = offset; new_table->items[0].child = *table_offset; new_table->items[1].child = right_child; uint32_t new_table_offset = _alloc_chunk(btree, sizeof *new_table); _flush_table(btree, new_table, new_table_offset); *table_offset = new_table_offset; return ret; }