/* Allocating more than the slab size will cause bad things to happen. Do not do that. */ void *slab_alloc (size_t bytes) { if (bytes > slab_size) return NULL; // align pointer here based on amount to be allocated if (cur + bytes >= end) { if (last->next == NULL) { slab_new (); } else { slab_next (); } } void *ret = cur; cur += bytes; return ret; }
void *__heap_alloc(heap_t * self, size_t size, const char *file, int line) { if (unlikely(self == NULL)) throw_unexpected(HEAP_NULL); size = max(align(size, self->alloc_size), self->alloc_size); size_t slab_pos = size / self->alloc_size - 1; if (unlikely(self->slab_size < slab_pos)) throw_unexpected(HEAP_ALLOC); if (unlikely(self->slab[slab_pos] == NULL)) self->slab[slab_pos] = slab_new(size, 0); return slab_alloc(self->slab[slab_pos]); }
/* Initialize the slab allocator system as a whole. */ void slab_init (size_t size) { slab_size = (size ? size : DEFAULT_SLAB_SIZE); total_size = 0; last = NULL; head = slab_new (); }