diff options
author | Toke Høiland-Jørgensen <toke@toke.dk> | 2020-11-24 02:32:13 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2020-11-24 02:36:31 +0100 |
commit | db2d29073acfd4086fca18ba43a5ed6baccaa8ad (patch) | |
tree | 59901d070b0c1c1ba6a1e42fc4957898c15820d6 /lib | |
parent | 3347aaafec5b269637604d1ea5f5969797beadea (diff) |
lib/slab: introduce sl_allocz() function and use it in Babel
The babel protocol code was initialising objects returned from the slab
allocator by assigning to each of the struct members individually, but
wasn't touching the NODE member while doing so. This leads to warnings on
debug builds since commit:
baac7009063d ("List expensive check.")
To fix this, introduce an sl_allocz() variant of the slab allocator which
will zero out the memory before returning it, and switch all the babel call
sites to use this version. The overhead for doing this should be negligible
for small objects, and in the case of babel, the largest object being
allocated was being zeroed anyway, so we can drop the memset in
babel_read_tlv().
Diffstat (limited to 'lib')
-rw-r--r-- | lib/resource.h | 1 | ||||
-rw-r--r-- | lib/slab.c | 24 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/resource.h b/lib/resource.h index ad17d9ed..b56bcff5 100644 --- a/lib/resource.h +++ b/lib/resource.h @@ -83,6 +83,7 @@ typedef struct slab slab; slab *sl_new(pool *, unsigned size); void *sl_alloc(slab *); +void *sl_allocz(slab *); void sl_free(slab *, void *); /* @@ -88,6 +88,14 @@ sl_alloc(slab *s) return o->data; } +void * +sl_allocz(slab *s) +{ + void *obj = sl_alloc(s); + memset(obj, 0, s->size); + return obj; +} + void sl_free(slab *s, void *oo) { @@ -279,6 +287,22 @@ no_partial: } /** + * sl_allocz - allocate an object from Slab and zero it + * @s: slab + * + * sl_allocz() allocates space for a single object from the + * Slab and returns a pointer to the object after zeroing out + * the object memory. + */ +void * +sl_allocz(slab *s) +{ + void *obj = sl_alloc(s); + memset(obj, 0, s->data_size); + return obj; +} + +/** * sl_free - return a free object back to a Slab * @s: slab * @oo: object returned by sl_alloc() |