15
15
* - int *indegree_at(struct indegree *, struct commit *);
16
16
*
17
17
* This function locates the data associated with the given commit in
18
- * the indegree slab, and returns the pointer to it.
18
+ * the indegree slab, and returns the pointer to it. The location to
19
+ * store the data is allocated as necessary.
20
+ *
21
+ * - int *indegree_peek(struct indegree *, struct commit *);
22
+ *
23
+ * This function is similar to indegree_at(), but it will return NULL
24
+ * until a call to indegree_at() was made for the commit.
19
25
*
20
26
* - void init_indegree(struct indegree *);
21
27
* void init_indegree_with_stride(struct indegree *, int);
@@ -80,8 +86,9 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
80
86
s->slab = NULL; \
81
87
} \
82
88
\
83
- static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
84
- const struct commit *c) \
89
+ static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
90
+ const struct commit *c, \
91
+ int add_if_missing) \
85
92
{ \
86
93
int nth_slab, nth_slot; \
87
94
\
@@ -90,16 +97,33 @@ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
90
97
\
91
98
if (s->slab_count <= nth_slab) { \
92
99
int i; \
100
+ if (!add_if_missing) \
101
+ return NULL; \
93
102
REALLOC_ARRAY(s->slab, nth_slab + 1); \
94
103
stat_ ##slabname## realloc++; \
95
104
for (i = s->slab_count; i <= nth_slab; i++) \
96
105
s->slab[i] = NULL; \
97
106
s->slab_count = nth_slab + 1; \
98
107
} \
99
- if (!s->slab[nth_slab]) \
108
+ if (!s->slab[nth_slab]) { \
109
+ if (!add_if_missing) \
110
+ return NULL; \
100
111
s->slab[nth_slab] = xcalloc(s->slab_size, \
101
112
sizeof(**s->slab) * s->stride); \
102
- return &s->slab[nth_slab][nth_slot * s->stride]; \
113
+ } \
114
+ return &s->slab[nth_slab][nth_slot * s->stride]; \
115
+ } \
116
+ \
117
+ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
118
+ const struct commit *c) \
119
+ { \
120
+ return slabname##_at_peek(s, c, 1); \
121
+ } \
122
+ \
123
+ static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \
124
+ const struct commit *c) \
125
+ { \
126
+ return slabname##_at_peek(s, c, 0); \
103
127
} \
104
128
\
105
129
static int stat_ ##slabname## realloc
0 commit comments