xdiff: introduce XDL_CALLOC_ARRAY()

Add a helper for allocating an array and initialize the elements to zero. This is analogous to CALLOC_ARRAY() in the rest of the codebase but it returns NULL on allocation failures rather than dying to accommodate other users of libxdiff such as libgit2. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jul 8, 2022 at 16:25 UTC 848fd5ae5b6506df50ae872302d94b794dc57d51
4 files changed +11 -14
xdiff/xhistogram.c
+3 -6
@@ -266,17 +266,14 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
266
267 index.table_bits = xdl_hashbits(count1);
268 index.records_size = 1 << index.table_bits;
269 - if (!(index.records = xdl_calloc(index.records_size,
270 - sizeof(*index.records))))
269 + if (!XDL_CALLOC_ARRAY(index.records, index.records_size))
270 goto cleanup;
271
272 index.line_map_size = count1;
274 - if (!(index.line_map = xdl_calloc(index.line_map_size,
275 - sizeof(*index.line_map))))
273 + if (!XDL_CALLOC_ARRAY(index.line_map, index.line_map_size))
274 goto cleanup;
275
278 - if (!(index.next_ptrs = xdl_calloc(index.line_map_size,
279 - sizeof(*index.next_ptrs))))
276 + if (!XDL_CALLOC_ARRAY(index.next_ptrs, index.line_map_size))
277 goto cleanup;
278
279 /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
xdiff/xmacros.h
+3
@@ -55,4 +55,7 @@ do { \
55 ? xdl_malloc((nr) * sizeof(*(p))) \
56 : NULL)
57
58 +/* Allocate an array of nr zeroed out elements, returns NULL on failure */
59 +#define XDL_CALLOC_ARRAY(p, nr) ((p) = xdl_calloc(nr, sizeof(*(p))))
60 +
61 #endif /* #if !defined(XMACROS_H) */
xdiff/xpatience.c
+1 -2
@@ -151,8 +151,7 @@ static int fill_hashmap(mmfile_t *file1, mmfile_t *file2,
151
152 /* We know exactly how large we want the hash map */
153 result->alloc = count1 * 2;
154 - result->entries = xdl_calloc(result->alloc, sizeof(*result->entries));
155 - if (!result->entries)
154 + if (!XDL_CALLOC_ARRAY(result->entries, result->alloc))
155 return -1;
156
157 /* First, fill with entries from the first file */
xdiff/xprepare.c
+4 -6
@@ -78,7 +78,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
78
79 return -1;
80 }
81 - if (!(cf->rchash = xdl_calloc(cf->hsize, sizeof(*cf->rchash)))) {
81 + if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
82
83 xdl_cha_free(&cf->ncha);
84 return -1;
@@ -182,7 +182,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
182
183 hbits = xdl_hashbits((unsigned int) narec);
184 hsize = 1 << hbits;
185 - if (!(rhash = xdl_calloc(hsize, sizeof(*rhash))))
185 + if (!XDL_CALLOC_ARRAY(rhash, hsize))
186 goto abort;
187
188 nrec = 0;
@@ -207,7 +207,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
207 }
208 }
209
210 - if (!(rchg = xdl_calloc((nrec + 2), sizeof(*rchg))))
210 + if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
211 goto abort;
212
213 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
@@ -380,10 +380,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
380 xdlclass_t *rcrec;
381 char *dis, *dis1, *dis2;
382
383 - if (!(dis = xdl_calloc(xdf1->nrec + xdf2->nrec + 2, sizeof(*dis)))) {
384 -
383 + if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
384 return -1;
386 - }
385 dis1 = dis;
386 dis2 = dis1 + xdf1->nrec + 1;
387