diff --git a/src/util/slab.c b/src/util/slab.c index 1e778a12381..9dee236217b 100644 --- a/src/util/slab.c +++ b/src/util/slab.c @@ -110,6 +110,7 @@ slab_create_parent(struct slab_parent_pool *parent, parent->element_size = ALIGN_POT(sizeof(struct slab_element_header) + item_size, sizeof(intptr_t)); parent->num_elements = num_items; + parent->item_size = item_size; } void @@ -230,6 +231,18 @@ slab_alloc(struct slab_child_pool *pool) return &elt[1]; } +/** + * Same as slab_alloc but memset the returned object to 0. + */ +void * +slab_zalloc(struct slab_child_pool *pool) +{ + void *r = slab_alloc(pool); + if (r) + memset(r, 0, pool->parent->item_size); + return r; +} + /** * Free an object allocated from the slab. Single-threaded (i.e. the caller * must ensure that no operation happens on the same child pool in another diff --git a/src/util/slab.h b/src/util/slab.h index 969b0ec5c65..f6dcbe5c98c 100644 --- a/src/util/slab.h +++ b/src/util/slab.h @@ -55,6 +55,7 @@ struct slab_parent_pool { simple_mtx_t mutex; unsigned element_size; unsigned num_elements; + unsigned item_size; }; struct slab_child_pool { @@ -81,6 +82,7 @@ void slab_create_child(struct slab_child_pool *pool, struct slab_parent_pool *parent); void slab_destroy_child(struct slab_child_pool *pool); void *slab_alloc(struct slab_child_pool *pool); +void *slab_zalloc(struct slab_child_pool *pool); void slab_free(struct slab_child_pool *pool, void *ptr); struct slab_mempool {