util/slab: add slab_zalloc

A a variant that clears the allocated object to 0.

Cc: mesa-stable

Acked-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Reviewed-by: Rob Clark <robdclark@chromium.org>
Reviewed-by: Christian Gmeiner <christian.gmeiner@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15298>
This commit is contained in:
Pierre-Eric Pelloux-Prayer 2022-03-08 11:43:29 +01:00 committed by Marge Bot
parent 2cd30266f1
commit caeec6262d
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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 {