Callers currently must use init_foo_slab() at runtime before
accessing a slab. For global slabs, it's much nicer if we
can initialize them in BSS, so that each user does not have
to add code to check-and-initialize.

Signed-off-by: Jeff King <>
The calling convention is kind of weird. It goes:

  struct foo_slab foo = COMMIT_SLAB_INIT(1, foo);

We need to know the size of the slab's element-type in the initializer
(and we grab it from sizeof(**foo.slab).  Another option would be:

  struct foo_slab foo = COMMIT_SLAB_INIT(1, void *);

which is simpler, but requires the user to repeat the type of the slab
(and if they get it wrong, bad things happen).

Yet another option would be to simply let a zero-initialized slab be
acceptable, and have slab_at check whether "stride" is initialized (and
if not, init to 1).

 commit-slab.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/commit-slab.h b/commit-slab.h
index cc114b5..375c9c7 100644
--- a/commit-slab.h
+++ b/commit-slab.h
@@ -117,4 +117,16 @@ static int stat_ ##slabname## realloc
  * catch because GCC silently parses it by default.
+ * Statically initialize a commit slab named "var". Note that this
+ * evaluates "stride" multiple times! Example:
+ *
+ *   struct indegree indegrees = COMMIT_SLAB_INIT(1, indegrees);
+ *
+ */
+#define COMMIT_SLAB_INIT(stride, var) { \
+       COMMIT_SLAB_SIZE / sizeof(**((var).slab)) / (stride), \
+       (stride), 0, NULL \
 #endif /* COMMIT_SLAB_H */

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to
More majordomo info at

Reply via email to