THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7KB

  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 elemtype *slabname## _at_peek(struct slabname *s, \
  38. const struct commit *c, \
  39. int add_if_missing) \
  40. { \
  41. unsigned int nth_slab, nth_slot; \
  42. \
  43. nth_slab = c->index / s->slab_size; \
  44. nth_slot = c->index % s->slab_size; \
  45. \
  46. if (s->slab_count <= nth_slab) { \
  47. unsigned int i; \
  48. if (!add_if_missing) \
  49. return NULL; \
  50. REALLOC_ARRAY(s->slab, nth_slab + 1); \
  51. for (i = s->slab_count; i <= nth_slab; i++) \
  52. s->slab[i] = NULL; \
  53. s->slab_count = nth_slab + 1; \
  54. } \
  55. if (!s->slab[nth_slab]) { \
  56. if (!add_if_missing) \
  57. return NULL; \
  58. s->slab[nth_slab] = xcalloc(s->slab_size, \
  59. sizeof(**s->slab) * s->stride); \
  60. } \
  61. return &s->slab[nth_slab][nth_slot * s->stride]; \
  62. } \
  63. \
  64. scope elemtype *slabname## _at(struct slabname *s, \
  65. const struct commit *c) \
  66. { \
  67. return slabname##_at_peek(s, c, 1); \
  68. } \
  69. \
  70. scope elemtype *slabname## _peek(struct slabname *s, \
  71. const struct commit *c) \
  72. { \
  73. return slabname##_at_peek(s, c, 0); \
  74. } \
  75. \
  76. struct slabname
  77. /*
  78. * Note that this redundant forward declaration is required
  79. * to allow a terminating semicolon, which makes instantiations look
  80. * like function declarations. I.e., the expansion of
  81. *
  82. * implement_commit_slab(indegree, int, static);
  83. *
  84. * ends in 'struct indegree;'. This would otherwise
  85. * be a syntax error according (at least) to ISO C. It's hard to
  86. * catch because GCC silently parses it by default.
  87. */
  88. #endif /* COMMIT_SLAB_IMPL_H */