Skip to content

Commit 18aae7e

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

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ typedef struct s_bdiffparam {
120120

121121

122122
#define xdl_malloc(x) xmalloc(x)
123+
#define xdl_calloc(n, sz) xcalloc(n, sz)
123124
#define xdl_free(ptr) free(ptr)
124125
#define xdl_realloc(ptr,x) xrealloc(ptr,x)
125126

xdiff/xhistogram.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
251251
int line1, int count1, int line2, int count2)
252252
{
253253
int b_ptr;
254-
int sz, ret = -1;
254+
int ret = -1;
255255
struct histindex index;
256256

257257
memset(&index, 0, sizeof(index));
@@ -265,23 +265,19 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
265265
index.rcha.head = NULL;
266266

267267
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))))
271271
goto cleanup;
272-
memset(index.records, 0, sz);
273272

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))))
277276
goto cleanup;
278-
memset(index.line_map, 0, sz);
279277

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))))
283280
goto cleanup;
284-
memset(index.next_ptrs, 0, sz);
285281

286282
/* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
287283
if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0)

xdiff/xpatience.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,9 @@ 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 = (struct entry *)
155-
xdl_malloc(result->alloc * sizeof(struct entry));
154+
result->entries = xdl_calloc(result->alloc, sizeof(*result->entries));
156155
if (!result->entries)
157156
return -1;
158-
memset(result->entries, 0, result->alloc * sizeof(struct entry));
159157

160158
/* First, fill with entries from the first file */
161159
while (count1--)

xdiff/xprepare.c

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

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

8383
xdl_cha_free(&cf->ncha);
8484
return -1;
8585
}
86-
memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
8786

8887
cf->alloc = size;
8988
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_
183182

184183
hbits = xdl_hashbits((unsigned int) narec);
185184
hsize = 1 << hbits;
186-
if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
185+
if (!(rhash = xdl_calloc(hsize, sizeof(*rhash))))
187186
goto abort;
188-
memset(rhash, 0, hsize * sizeof(xrecord_t *));
189187

190188
nrec = 0;
191189
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_
209207
}
210208
}
211209

212-
if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
210+
if (!(rchg = xdl_calloc((nrec + 2), sizeof(*rchg))))
213211
goto abort;
214-
memset(rchg, 0, (nrec + 2) * sizeof(char));
215212

216213
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
217214
(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
383380
xdlclass_t *rcrec;
384381
char *dis, *dis1, *dis2;
385382

386-
if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
383+
if (!(dis = xdl_calloc(xdf1->nrec + xdf2->nrec + 2, sizeof(*dis)))) {
387384

388385
return -1;
389386
}
390-
memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
391387
dis1 = dis;
392388
dis2 = dis1 + xdf1->nrec + 1;
393389

0 commit comments

Comments
 (0)