Skip to content

Commit 848fd5a

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 18aae7e commit 848fd5a

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

xdiff/xhistogram.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,14 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
266266

267267
index.table_bits = xdl_hashbits(count1);
268268
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))
271270
goto cleanup;
272271

273272
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))
276274
goto cleanup;
277275

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))
280277
goto cleanup;
281278

282279
/* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */

xdiff/xmacros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ do { \
5555
? xdl_malloc((nr) * sizeof(*(p))) \
5656
: NULL)
5757

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+
5861
#endif /* #if !defined(XMACROS_H) */

xdiff/xpatience.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ static int fill_hashmap(mmfile_t *file1, mmfile_t *file2,
151151

152152
/* We know exactly how large we want the hash map */
153153
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))
156155
return -1;
157156

158157
/* First, fill with entries from the first file */

xdiff/xprepare.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
7878

7979
return -1;
8080
}
81-
if (!(cf->rchash = xdl_calloc(cf->hsize, sizeof(*cf->rchash)))) {
81+
if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
8282

8383
xdl_cha_free(&cf->ncha);
8484
return -1;
@@ -182,7 +182,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
182182

183183
hbits = xdl_hashbits((unsigned int) narec);
184184
hsize = 1 << hbits;
185-
if (!(rhash = xdl_calloc(hsize, sizeof(*rhash))))
185+
if (!XDL_CALLOC_ARRAY(rhash, hsize))
186186
goto abort;
187187

188188
nrec = 0;
@@ -207,7 +207,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
207207
}
208208
}
209209

210-
if (!(rchg = xdl_calloc((nrec + 2), sizeof(*rchg))))
210+
if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
211211
goto abort;
212212

213213
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
380380
xdlclass_t *rcrec;
381381
char *dis, *dis1, *dis2;
382382

383-
if (!(dis = xdl_calloc(xdf1->nrec + xdf2->nrec + 2, sizeof(*dis)))) {
384-
383+
if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
385384
return -1;
386-
}
387385
dis1 = dis;
388386
dis2 = dis1 + xdf1->nrec + 1;
389387

0 commit comments

Comments
 (0)