From 6e961567434f50abf850963873988c3365098681 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 2 Nov 2024 10:06:14 -0400 Subject: alloc: New for_arena() macro to iterate over allocated objects --- src/alloc.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/alloc.h') diff --git a/src/alloc.h b/src/alloc.h index af8b391..4d68111 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -283,6 +283,29 @@ void arena_clear(struct arena *arena); */ void arena_destroy(struct arena *arena); +/** + * Loop over every allocated object in an arena. + * + * @type + * The object type. + * @item + * The induction variable name. + * @arena + * The arena to loop over. + */ +#define for_arena(type, item, arena) \ + for_arena_ (type, _i_##item, item, (arena)) + +#define for_arena_(type, i, item, arena) \ + for (size_t i = 0; i < arena->nslabs; ++i) \ + for (type *item = NULL; (item = slab_next(arena->slabs[i], item));) + +/** + * @internal + * Get the next allocated object in a slab. + */ +void *slab_next(struct slab *slab, void *ptr); + /** * An arena allocator for flexibly-sized types. */ @@ -392,4 +415,21 @@ void varena_clear(struct varena *varena); */ void varena_destroy(struct varena *varena); +/** + * Loop over every allocated object in a varena. + * + * @type + * The object type. + * @item + * The induction variable name. + * @varena + * The varena to loop over. + */ +#define for_varena(type, item, varena) \ + for_varena_ (type, _j_##item, item, (varena)) + +#define for_varena_(type, j, item, varena) \ + for (size_t j = 0; j < varena->narenas; ++j) \ + for_arena (type, item, &varena->arenas[j]) + #endif // BFS_ALLOC_H -- cgit v1.2.3