Skip to content

Commit 0f60955

Browse files
committed
Merge branch 'pw/xdiff-alloc'
Add a level of redirection to array allocation API in xdiff part, to make it easier to share with the libgit2 project. * pw/xdiff-alloc: xdiff: introduce XDL_ALLOC_GROW() xdiff: introduce XDL_CALLOC_ARRAY() xdiff: introduce xdl_calloc xdiff: introduce XDL_ALLOC_ARRAY()
2 parents acbec18 + f7b587b commit 0f60955

File tree

8 files changed

+60
-50
lines changed

8 files changed

+60
-50
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/xdiffi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
337337
* One is to store the forward path and one to store the backward path.
338338
*/
339339
ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
340-
if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
340+
if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) {
341341

342342
xdl_free_env(xe);
343343
return -1;

xdiff/xhistogram.c

Lines changed: 6 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,16 @@ 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 (!XDL_CALLOC_ARRAY(index.records, index.records_size))
271270
goto cleanup;
272-
memset(index.records, 0, sz);
273271

274-
sz = index.line_map_size = count1;
275-
sz *= sizeof(struct record *);
276-
if (!(index.line_map = (struct record **) xdl_malloc(sz)))
272+
index.line_map_size = count1;
273+
if (!XDL_CALLOC_ARRAY(index.line_map, index.line_map_size))
277274
goto cleanup;
278-
memset(index.line_map, 0, sz);
279275

280-
sz = index.line_map_size;
281-
sz *= sizeof(unsigned int);
282-
if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz)))
276+
if (!XDL_CALLOC_ARRAY(index.next_ptrs, index.line_map_size))
283277
goto cleanup;
284-
memset(index.next_ptrs, 0, sz);
285278

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

xdiff/xmacros.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,23 @@ do { \
4949
((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \
5050
} while (0)
5151

52+
/* Allocate an array of nr elements, returns NULL on failure */
53+
#define XDL_ALLOC_ARRAY(p, nr) \
54+
((p) = SIZE_MAX / sizeof(*(p)) >= (size_t)(nr) \
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+
/*
62+
* Ensure array p can accommodate at least nr elements, growing the
63+
* array and updating alloc (which is the number of allocated
64+
* elements) as necessary. Frees p and returns -1 on failure, returns
65+
* 0 on success
66+
*/
67+
#define XDL_ALLOC_GROW(p, nr, alloc) \
68+
(-!((nr) <= (alloc) || \
69+
((p) = xdl_alloc_grow_helper((p), (nr), &(alloc), sizeof(*(p))))))
5270

5371
#endif /* #if !defined(XMACROS_H) */

xdiff/xpatience.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ 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));
156-
if (!result->entries)
154+
if (!XDL_CALLOC_ARRAY(result->entries, result->alloc))
157155
return -1;
158-
memset(result->entries, 0, result->alloc * sizeof(struct entry));
159156

160157
/* First, fill with entries from the first file */
161158
while (count1--)
@@ -200,7 +197,7 @@ static int binary_search(struct entry **sequence, int longest,
200197
*/
201198
static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
202199
{
203-
struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *));
200+
struct entry **sequence;
204201
int longest = 0, i;
205202
struct entry *entry;
206203

@@ -211,7 +208,7 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
211208
*/
212209
int anchor_i = -1;
213210

214-
if (!sequence)
211+
if (!XDL_ALLOC_ARRAY(sequence, map->nr))
215212
return -1;
216213

217214
for (entry = map->first; entry; entry = entry->next) {

xdiff/xprepare.c

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ 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 (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
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;
89-
if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
88+
if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
9089

9190
xdl_free(cf->rchash);
9291
xdl_cha_free(&cf->ncha);
@@ -112,7 +111,6 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t
112111
long hi;
113112
char const *line;
114113
xdlclass_t *rcrec;
115-
xdlclass_t **rcrecs;
116114

117115
line = rec->ptr;
118116
hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
@@ -128,14 +126,8 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t
128126
return -1;
129127
}
130128
rcrec->idx = cf->count++;
131-
if (cf->count > cf->alloc) {
132-
cf->alloc *= 2;
133-
if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
134-
129+
if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc))
135130
return -1;
136-
}
137-
cf->rcrecs = rcrecs;
138-
}
139131
cf->rcrecs[rcrec->idx] = rcrec;
140132
rcrec->line = line;
141133
rcrec->size = rec->size;
@@ -164,7 +156,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
164156
unsigned long hav;
165157
char const *blk, *cur, *top, *prev;
166158
xrecord_t *crec;
167-
xrecord_t **recs, **rrecs;
159+
xrecord_t **recs;
168160
xrecord_t **rhash;
169161
unsigned long *ha;
170162
char *rchg;
@@ -178,26 +170,21 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
178170

179171
if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
180172
goto abort;
181-
if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
173+
if (!XDL_ALLOC_ARRAY(recs, narec))
182174
goto abort;
183175

184176
hbits = xdl_hashbits((unsigned int) narec);
185177
hsize = 1 << hbits;
186-
if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
178+
if (!XDL_CALLOC_ARRAY(rhash, hsize))
187179
goto abort;
188-
memset(rhash, 0, hsize * sizeof(xrecord_t *));
189180

190181
nrec = 0;
191182
if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
192183
for (top = blk + bsize; cur < top; ) {
193184
prev = cur;
194185
hav = xdl_hash_record(&cur, top, xpp->flags);
195-
if (nrec >= narec) {
196-
narec *= 2;
197-
if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
198-
goto abort;
199-
recs = rrecs;
200-
}
186+
if (XDL_ALLOC_GROW(recs, nrec + 1, narec))
187+
goto abort;
201188
if (!(crec = xdl_cha_alloc(&xdf->rcha)))
202189
goto abort;
203190
crec->ptr = prev;
@@ -209,15 +196,14 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
209196
}
210197
}
211198

212-
if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
199+
if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
213200
goto abort;
214-
memset(rchg, 0, (nrec + 2) * sizeof(char));
215201

216202
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
217203
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
218-
if (!(rindex = xdl_malloc((nrec + 1) * sizeof(*rindex))))
204+
if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
219205
goto abort;
220-
if (!(ha = xdl_malloc((nrec + 1) * sizeof(*ha))))
206+
if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
221207
goto abort;
222208
}
223209

@@ -383,11 +369,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
383369
xdlclass_t *rcrec;
384370
char *dis, *dis1, *dis2;
385371

386-
if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
387-
372+
if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
388373
return -1;
389-
}
390-
memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
391374
dis1 = dis;
392375
dis2 = dis1 + xdf1->nrec + 1;
393376

xdiff/xutils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,20 @@ int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp,
432432

433433
return 0;
434434
}
435+
436+
void* xdl_alloc_grow_helper(void *p, long nr, long *alloc, size_t size)
437+
{
438+
void *tmp = NULL;
439+
size_t n = ((LONG_MAX - 16) / 2 >= *alloc) ? 2 * *alloc + 16 : LONG_MAX;
440+
if (nr > n)
441+
n = nr;
442+
if (SIZE_MAX / size >= n)
443+
tmp = xdl_realloc(p, n * size);
444+
if (tmp) {
445+
*alloc = n;
446+
} else {
447+
xdl_free(p);
448+
*alloc = 0;
449+
}
450+
return tmp;
451+
}

xdiff/xutils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
4242
int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp,
4343
int line1, int count1, int line2, int count2);
4444

45-
45+
/* Do not call this function, use XDL_ALLOC_GROW instead */
46+
void* xdl_alloc_grow_helper(void* p, long nr, long* alloc, size_t size);
4647

4748
#endif /* #if !defined(XUTILS_H) */

0 commit comments

Comments
 (0)