git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 01/15] commit-slab.h: code split

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 19, 2018, 05:28 UTC
Message-ID
<20180519052831.12603-2-pclouds@gmail.com>
In-Reply-To
<20180519052831.12603-1-pclouds@gmail.com>

The struct declaration and implementation macros are moved to commit-slab-hdr.h and commit-slab-impl.h respectively.

This right now is not needed for current users but if we make a public
commit-slab type, we may want to avoid including the slab
implementation in a header file which gets replicated in every c file
that includes it.
---
 commit-slab-decl.h |  30 ++++++++++++
 commit-slab-impl.h |  91 +++++++++++++++++++++++++++++++++++
 commit-slab.h      | 115 +++------------------------------------------
 3 files changed, 127 insertions(+), 109 deletions(-)
 create mode 100644 commit-slab-decl.h
 create mode 100644 commit-slab-impl.h
diff --git a/commit-slab-decl.h b/commit-slab-decl.h
new file mode 100644
index 0000000000..fb5220fb7d
--- /dev/null
+++ b/commit-slab-decl.h
@@ -0,0 +1,30 @@
+#ifndef COMMIT_SLAB_HDR_H
+#define COMMIT_SLAB_HDR_H
+
+/* allocate ~512kB at once, allowing for malloc overhead */
+#ifndef COMMIT_SLAB_SIZE
+#define COMMIT_SLAB_SIZE (512*1024-32)
+#endif
+
+#define declare_commit_slab(slabname, elemtype) 			\
+									\
+struct slabname {							\
+	unsigned slab_size;						\
+	unsigned stride;						\
+	unsigned slab_count;						\
+	elemtype **slab;						\
+}
+
+/*
+ * 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_HDR_H */
diff --git a/commit-slab-impl.h b/commit-slab-impl.h
new file mode 100644
index 0000000000..234d9ee5f0
--- /dev/null
+++ b/commit-slab-impl.h
@@ -0,0 +1,91 @@
+#ifndef COMMIT_SLAB_IMPL_H
+#define COMMIT_SLAB_IMPL_H
+
+#define MAYBE_UNUSED __attribute__((__unused__))
+
+#define implement_commit_slab(slabname, elemtype) 			\
+									\
+static int stat_ ##slabname## realloc;					\
+									\
+static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
+						   unsigned stride)	\
+{									\
+	unsigned int elem_size;						\
+	if (!stride)							\
+		stride = 1;						\
+	s->stride = stride;						\
+	elem_size = sizeof(elemtype) * stride;				\
+	s->slab_size = COMMIT_SLAB_SIZE / elem_size;			\
+	s->slab_count = 0;						\
+	s->slab = NULL;							\
+}									\
+									\
+static MAYBE_UNUSED void init_ ##slabname(struct slabname *s)		\
+{									\
+	init_ ##slabname## _with_stride(s, 1);				\
+}									\
+									\
+static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s)		\
+{									\
+	unsigned int i;							\
+	for (i = 0; i < s->slab_count; i++)				\
+		free(s->slab[i]);					\
+	s->slab_count = 0;						\
+	FREE_AND_NULL(s->slab);						\
+}									\
+									\
+static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s,	\
+						  const struct commit *c, \
+						  int add_if_missing)   \
+{									\
+	unsigned int nth_slab, nth_slot;				\
+									\
+	nth_slab = c->index / s->slab_size;				\
+	nth_slot = c->index % s->slab_size;				\
+									\
+	if (s->slab_count <= nth_slab) {				\
+		unsigned int i;						\
+		if (!add_if_missing)					\
+			return NULL;					\
+		REALLOC_ARRAY(s->slab, nth_slab + 1);			\
+		stat_ ##slabname## realloc++;				\
+		for (i = s->slab_count; i <= nth_slab; i++)		\
+			s->slab[i] = NULL;				\
+		s->slab_count = nth_slab + 1;				\
+	}								\
+	if (!s->slab[nth_slab]) {					\
+		if (!add_if_missing)					\
+			return NULL;					\
+		s->slab[nth_slab] = xcalloc(s->slab_size,		\
+					    sizeof(**s->slab) * s->stride);		\
+	}								\
+	return &s->slab[nth_slab][nth_slot * s->stride];		\
+}									\
+									\
+static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s,	\
+					     const struct commit *c)	\
+{									\
+	return slabname##_at_peek(s, c, 1);				\
+}									\
+									\
+static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s,	\
+					     const struct commit *c)	\
+{									\
+	return slabname##_at_peek(s, c, 0);				\
+}									\
+									\
+struct slabname
+
+/*
+ * Note that this redundant forward declaration is required
+ * to allow a terminating semicolon, which makes instantiations look
+ * like function declarations.  I.e., the expansion of
+ *
+ *    implement_commit_slab(indegree, int);
+ *
+ * ends in 'struct indegree;'.  This would otherwise
+ * be a syntax error according (at least) to ISO C.  It's hard to
+ * catch because GCC silently parses it by default.
+ */
+
+#endif	/* COMMIT_SLAB_IMPL_H */
diff --git a/commit-slab.h b/commit-slab.h
index dcaab8ca04..32aa2c0e46 100644
--- a/commit-slab.h
+++ b/commit-slab.h
@@ -1,6 +1,9 @@
 #ifndef COMMIT_SLAB_H
 #define COMMIT_SLAB_H
 
+#include "commit-slab-decl.h"
+#include "commit-slab-impl.h"
+
 /*
  * define_commit_slab(slabname, elemtype) creates boilerplate code to define
  * a new struct (struct slabname) that is used to associate a piece of data
@@ -41,114 +44,8 @@
  *   leaking memory.
  */
 
-/* allocate ~512kB at once, allowing for malloc overhead */
-#ifndef COMMIT_SLAB_SIZE
-#define COMMIT_SLAB_SIZE (512*1024-32)
-#endif
-
-#define MAYBE_UNUSED __attribute__((__unused__))
-
-#define define_commit_slab(slabname, elemtype) 				\
-									\
-struct slabname {							\
-	unsigned slab_size;						\
-	unsigned stride;						\
-	unsigned slab_count;						\
-	elemtype **slab;						\
-};									\
-static int stat_ ##slabname## realloc;					\
-									\
-static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
-						   unsigned stride)	\
-{									\
-	unsigned int elem_size;						\
-	if (!stride)							\
-		stride = 1;						\
-	s->stride = stride;						\
-	elem_size = sizeof(elemtype) * stride;				\
-	s->slab_size = COMMIT_SLAB_SIZE / elem_size;			\
-	s->slab_count = 0;						\
-	s->slab = NULL;							\
-}									\
-									\
-static MAYBE_UNUSED void init_ ##slabname(struct slabname *s)		\
-{									\
-	init_ ##slabname## _with_stride(s, 1);				\
-}									\
-									\
-static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s)		\
-{									\
-	unsigned int i;							\
-	for (i = 0; i < s->slab_count; i++)				\
-		free(s->slab[i]);					\
-	s->slab_count = 0;						\
-	FREE_AND_NULL(s->slab);						\
-}									\
-									\
-static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s,	\
-						  const struct commit *c, \
-						  int add_if_missing)   \
-{									\
-	unsigned int nth_slab, nth_slot;				\
-									\
-	nth_slab = c->index / s->slab_size;				\
-	nth_slot = c->index % s->slab_size;				\
-									\
-	if (s->slab_count <= nth_slab) {				\
-		unsigned int i;						\
-		if (!add_if_missing)					\
-			return NULL;					\
-		REALLOC_ARRAY(s->slab, nth_slab + 1);			\
-		stat_ ##slabname## realloc++;				\
-		for (i = s->slab_count; i <= nth_slab; i++)		\
-			s->slab[i] = NULL;				\
-		s->slab_count = nth_slab + 1;				\
-	}								\
-	if (!s->slab[nth_slab]) {					\
-		if (!add_if_missing)					\
-			return NULL;					\
-		s->slab[nth_slab] = xcalloc(s->slab_size,		\
-					    sizeof(**s->slab) * s->stride);		\
-	}								\
-	return &s->slab[nth_slab][nth_slot * s->stride];		\
-}									\
-									\
-static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s,	\
-					     const struct commit *c)	\
-{									\
-	return slabname##_at_peek(s, c, 1);				\
-}									\
-									\
-static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s,	\
-					     const struct commit *c)	\
-{									\
-	return slabname##_at_peek(s, c, 0);				\
-}									\
-									\
-struct slabname
-
-/*
- * Note that this redundant forward declaration is required
- * to allow a terminating semicolon, which makes instantiations look
- * like function declarations.  I.e., the expansion of
- *
- *    define_commit_slab(indegree, int);
- *
- * ends in 'struct indegree;'.  This would otherwise
- * be a syntax error according (at least) to ISO C.  It's hard to
- * 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 \
-}
+#define define_commit_slab(slabname, elemtype) \
+	declare_commit_slab(slabname, elemtype); \
+	implement_commit_slab(slabname, elemtype)
 
 #endif /* COMMIT_SLAB_H */
-- 
2.17.0.705.g3525833791
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 66 of 82 in “Die commit->util, die!”
  1. 00/12 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 12, 2018
  2. 02/12 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  3. 05/12 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 12, 2018
  4. Jeff KingMay 12, 2018
  5. 04/12 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 12, 2018
  6. Jeff KingMay 12, 2018
  7. Junio C HamanoMay 12, 2018
  8. Duy NguyenMay 12, 2018
  9. 06/12 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 12, 2018
  10. Jeff KingMay 12, 2018
  11. Junio C HamanoMay 12, 2018
  12. Duy NguyenMay 12, 2018
  13. Jeff KingMay 12, 2018
  14. 10/12 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  15. 12/12 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 12, 2018
  16. 11/12 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  17. 09/12 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  18. 01/12 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  19. Jeff KingMay 12, 2018
  20. Duy NguyenMay 12, 2018
  21. 07/12 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  22. Junio C HamanoMay 12, 2018
  23. 08/12 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  24. 03/12 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  25. Jeff KingMay 12, 2018
  26. Jeff KingMay 12, 2018
  27. Duy NguyenMay 12, 2018
  28. Jeff KingMay 12, 2018
  29. Duy NguyenMay 12, 2018
  30. Jeff KingMay 12, 2018
  31. Jakub NarebskiMay 12, 2018
  32. Duy NguyenMay 13, 2018
  33. 00/14 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 13, 2018
  34. 01/14 commit-slab.h: code splitNguyễn Thái Ngọc Duy, May 13, 2018
  35. Junio C HamanoMay 13, 2018
  36. 02/14 commit-slab: support shared commit-slabNguyễn Thái Ngọc Duy, May 13, 2018
  37. Junio C HamanoMay 13, 2018
  38. 03/14 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  39. Junio C HamanoMay 13, 2018
  40. 06/14 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 13, 2018
  41. Junio C HamanoMay 14, 2018
  42. 12/14 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  43. 07/14 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 13, 2018
  44. 14/14 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 13, 2018
  45. Junio C HamanoMay 14, 2018
  46. Duy NguyenMay 14, 2018
  47. Duy NguyenMay 14, 2018
  48. Derrick StoleeMay 14, 2018
  49. Duy NguyenMay 19, 2018
  50. 08/14 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 13, 2018
  51. Junio C HamanoMay 14, 2018
  52. Junio C HamanoMay 14, 2018
  53. 13/14 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  54. Junio C HamanoMay 18, 2018
  55. Ramsay JonesMay 18, 2018
  56. 11/14 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  57. Junio C HamanoMay 14, 2018
  58. Duy NguyenMay 19, 2018
  59. 09/14 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  60. Junio C HamanoMay 14, 2018
  61. 05/14 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  62. 10/14 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  63. 04/14 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  64. Junio C HamanoMay 13, 2018
  65. 00/15 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 19, 2018
  66. 01/15 commit-slab.h: code splitNguyễn Thái Ngọc Duy, May 19, 2018
  67. Junio C HamanoMay 21, 2018
  68. 02/15 commit-slab: support shared commit-slabNguyễn Thái Ngọc Duy, May 19, 2018
  69. 04/15 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  70. 08/15 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 19, 2018
  71. 09/15 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  72. 07/15 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 19, 2018
  73. 13/15 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  74. 15/15 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 19, 2018
  75. 14/15 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  76. 06/15 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 19, 2018
  77. 12/15 show-branch: note about its object flags usageNguyễn Thái Ngọc Duy, May 19, 2018
  78. 11/15 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  79. 10/15 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  80. 03/15 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  81. 05/15 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  82. Stefan BellerMay 22, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.