summaryrefslogtreecommitdiffstats
path: root/src/alloc.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-11-02 10:06:14 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-11-04 12:26:37 -0500
commit6e961567434f50abf850963873988c3365098681 (patch)
treecdc83e8788b61944e9c8ffd5680d9aedcbedf4fd /src/alloc.h
parent624d63410a919f8cf9f8f757b02ca8b91111e969 (diff)
downloadbfs-6e961567434f50abf850963873988c3365098681.tar.xz
alloc: New for_arena() macro to iterate over allocated objects
Diffstat (limited to 'src/alloc.h')
-rw-r--r--src/alloc.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/alloc.h b/src/alloc.h
index af8b391..4d68111 100644
--- a/src/alloc.h
+++ b/src/alloc.h
@@ -284,6 +284,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.
*/
struct varena {
@@ -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