xdiff: introduce xdl_calloc

In preparation for introducing XDL_CALLOC_ARRAY() use calloc() to obtain zeroed out memory rather than malloc() followed by memset(). To try and keep the lines a reasonable length this commit also stops casting the pointer returned by calloc() as this is unnecessary. 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 18aae7e21e1a88c75adb38aa2555e0b1928b137d
4 files changed +15 -24
xdiff/xdiff.h
+1
@@ -120,6 +120,7 @@ typedef struct s_bdiffparam {
120
121
122 #define xdl_malloc(x) xmalloc(x)
123 +#define xdl_calloc(n, sz) xcalloc(n, sz)
124 #define xdl_free(ptr) free(ptr)
125 #define xdl_realloc(ptr,x) xrealloc(ptr,x)
126
xdiff/xhistogram.c
+9 -13
@@ -251,7 +251,7 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
251 int line1, int count1, int line2, int count2)
252 {
253 int b_ptr;
254 - int sz, ret = -1;
254 + int ret = -1;
255 struct histindex index;
256
257 memset(&index, 0, sizeof(index));
@@ -265,23 +265,19 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
265 index.rcha.head = NULL;
266
267 index.table_bits = xdl_hashbits(count1);
268 - sz = index.records_size = 1 << index.table_bits;
269 - sz *= sizeof(struct record *);
270 - if (!(index.records = (struct record **) xdl_malloc(sz)))
268 + index.records_size = 1 << index.table_bits;
269 + if (!(index.records = xdl_calloc(index.records_size,
270 + sizeof(*index.records))))
271 goto cleanup;
272 - memset(index.records, 0, sz);
272
274 - sz = index.line_map_size = count1;
275 - sz *= sizeof(struct record *);
276 - if (!(index.line_map = (struct record **) xdl_malloc(sz)))
273 + index.line_map_size = count1;
274 + if (!(index.line_map = xdl_calloc(index.line_map_size,
275 + sizeof(*index.line_map))))
276 goto cleanup;
278 - memset(index.line_map, 0, sz);
277
280 - sz = index.line_map_size;
281 - sz *= sizeof(unsigned int);
282 - if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz)))
278 + if (!(index.next_ptrs = xdl_calloc(index.line_map_size,
279 + sizeof(*index.next_ptrs))))
280 goto cleanup;
284 - memset(index.next_ptrs, 0, sz);
281
282 /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
283 if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0)
xdiff/xpatience.c
+1 -3
@@ -151,11 +151,9 @@ 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 = (struct entry *)
155 - xdl_malloc(result->alloc * sizeof(struct entry));
154 + result->entries = xdl_calloc(result->alloc, sizeof(*result->entries));
155 if (!result->entries)
156 return -1;
158 - memset(result->entries, 0, result->alloc * sizeof(struct entry));
157
158 /* First, fill with entries from the first file */
159 while (count1--)
xdiff/xprepare.c
+4 -8
@@ -78,12 +78,11 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
78
79 return -1;
80 }
81 - if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
81 + if (!(cf->rchash = xdl_calloc(cf->hsize, sizeof(*cf->rchash)))) {
82
83 xdl_cha_free(&cf->ncha);
84 return -1;
85 }
86 - memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
86
87 cf->alloc = size;
88 if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
@@ -183,9 +182,8 @@ 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;
186 - if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
185 + if (!(rhash = xdl_calloc(hsize, sizeof(*rhash))))
186 goto abort;
188 - memset(rhash, 0, hsize * sizeof(xrecord_t *));
187
188 nrec = 0;
189 if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
@@ -209,9 +207,8 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
207 }
208 }
209
212 - if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
210 + if (!(rchg = xdl_calloc((nrec + 2), sizeof(*rchg))))
211 goto abort;
214 - memset(rchg, 0, (nrec + 2) * sizeof(char));
212
213 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
214 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
@@ -383,11 +380,10 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
380 xdlclass_t *rcrec;
381 char *dis, *dis1, *dis2;
382
386 - if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
383 + if (!(dis = xdl_calloc(xdf1->nrec + xdf2->nrec + 2, sizeof(*dis)))) {
384
385 return -1;
386 }
390 - memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
387 dis1 = dis;
388 dis2 = dis1 + xdf1->nrec + 1;
389