commit-slab-impl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef COMMIT_SLAB_IMPL_H
  2. #define COMMIT_SLAB_IMPL_H
  3. #include "git-compat-util.h"
  4. #define implement_static_commit_slab(slabname, elemtype) \
  5. implement_commit_slab(slabname, elemtype, MAYBE_UNUSED static)
  6. #define implement_shared_commit_slab(slabname, elemtype) \
  7. implement_commit_slab(slabname, elemtype, )
  8. #define implement_commit_slab(slabname, elemtype, scope) \
  9. \
  10. scope void init_ ##slabname## _with_stride(struct slabname *s, \
  11. unsigned stride) \
  12. { \
  13. unsigned int elem_size; \
  14. if (!stride) \
  15. stride = 1; \
  16. s->stride = stride; \
  17. elem_size = sizeof(elemtype) * stride; \
  18. s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
  19. s->slab_count = 0; \
  20. s->slab = NULL; \
  21. } \
  22. \
  23. scope void init_ ##slabname(struct slabname *s) \
  24. { \
  25. init_ ##slabname## _with_stride(s, 1); \
  26. } \
  27. \
  28. scope void clear_ ##slabname(struct slabname *s) \
  29. { \
  30. unsigned int i; \
  31. for (i = 0; i < s->slab_count; i++) \
  32. free(s->slab[i]); \
  33. s->slab_count = 0; \
  34. FREE_AND_NULL(s->slab); \
  35. } \
  36. \
  37. scope void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *)) \
  38. { \
  39. unsigned int i; \
  40. for (i = 0; i < s->slab_count; i++) { \
  41. unsigned int j; \
  42. if (!s->slab[i]) \
  43. continue; \
  44. for (j = 0; j < s->slab_size; j++) \
  45. free_fn(&s->slab[i][j * s->stride]); \
  46. } \
  47. clear_ ##slabname(s); \
  48. } \
  49. \
  50. scope elemtype *slabname## _at_peek(struct slabname *s, \
  51. const struct commit *c, \
  52. int add_if_missing) \
  53. { \
  54. unsigned int nth_slab, nth_slot; \
  55. \
  56. nth_slab = c->index / s->slab_size; \
  57. nth_slot = c->index % s->slab_size; \
  58. \
  59. if (s->slab_count <= nth_slab) { \
  60. unsigned int i; \
  61. if (!add_if_missing) \
  62. return NULL; \
  63. REALLOC_ARRAY(s->slab, nth_slab + 1); \
  64. for (i = s->slab_count; i <= nth_slab; i++) \
  65. s->slab[i] = NULL; \
  66. s->slab_count = nth_slab + 1; \
  67. } \
  68. if (!s->slab[nth_slab]) { \
  69. if (!add_if_missing) \
  70. return NULL; \
  71. s->slab[nth_slab] = xcalloc(s->slab_size, \
  72. sizeof(**s->slab) * s->stride); \
  73. } \
  74. return &s->slab[nth_slab][nth_slot * s->stride]; \
  75. } \
  76. \
  77. scope elemtype *slabname## _at(struct slabname *s, \
  78. const struct commit *c) \
  79. { \
  80. return slabname##_at_peek(s, c, 1); \
  81. } \
  82. \
  83. scope elemtype *slabname## _peek(struct slabname *s, \
  84. const struct commit *c) \
  85. { \
  86. return slabname##_at_peek(s, c, 0); \
  87. } \
  88. \
  89. struct slabname
  90. /*
  91. * Note that this redundant forward declaration is required
  92. * to allow a terminating semicolon, which makes instantiations look
  93. * like function declarations. I.e., the expansion of
  94. *
  95. * implement_commit_slab(indegree, int, static);
  96. *
  97. * ends in 'struct indegree;'. This would otherwise
  98. * be a syntax error according (at least) to ISO C. It's hard to
  99. * catch because GCC silently parses it by default.
  100. */
  101. #endif /* COMMIT_SLAB_IMPL_H */