]> git.ipfire.org Git - thirdparty/git.git/blame - xdiff/xprepare.c
xdiff: introduce xdl_calloc
[thirdparty/git.git] / xdiff / xprepare.c
CommitLineData
3443546f
LT
1/*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003 Davide Libenzi
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
48425792
TZ
16 * License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
3443546f
LT
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 */
22
23#include "xinclude.h"
24
25
3443546f 26#define XDL_KPDIS_RUN 4
ca557aff 27#define XDL_MAX_EQLIMIT 1024
9b28d554 28#define XDL_SIMSCAN_WINDOW 100
86abba80
TRC
29#define XDL_GUESS_NLINES1 256
30#define XDL_GUESS_NLINES2 20
3443546f
LT
31
32
33typedef struct s_xdlclass {
34 struct s_xdlclass *next;
35 unsigned long ha;
36 char const *line;
37 long size;
38 long idx;
27af01d5 39 long len1, len2;
3443546f
LT
40} xdlclass_t;
41
42typedef struct s_xdlclassifier {
43 unsigned int hbits;
44 long hsize;
45 xdlclass_t **rchash;
46 chastore_t ncha;
27af01d5
TRC
47 xdlclass_t **rcrecs;
48 long alloc;
3443546f 49 long count;
0d21efa5 50 long flags;
3443546f
LT
51} xdlclassifier_t;
52
53
54
55
0d21efa5 56static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
3443546f 57static void xdl_free_classifier(xdlclassifier_t *cf);
27af01d5
TRC
58static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
59 unsigned int hbits, xrecord_t *rec);
60static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
61 xdlclassifier_t *cf, xdfile_t *xdf);
62static void xdl_free_ctx(xdfile_t *xdf);
63static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
27af01d5 64static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f 65static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
27af01d5 66static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f
LT
67
68
69
70
0d21efa5 71static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
0d21efa5
JS
72 cf->flags = flags;
73
3443546f
LT
74 cf->hbits = xdl_hashbits((unsigned int) size);
75 cf->hsize = 1 << cf->hbits;
76
77 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
78
79 return -1;
80 }
18aae7e2 81 if (!(cf->rchash = xdl_calloc(cf->hsize, sizeof(*cf->rchash)))) {
3443546f
LT
82
83 xdl_cha_free(&cf->ncha);
84 return -1;
85 }
3443546f 86
27af01d5 87 cf->alloc = size;
abf04bda 88 if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
27af01d5
TRC
89
90 xdl_free(cf->rchash);
91 xdl_cha_free(&cf->ncha);
92 return -1;
93 }
94
3443546f
LT
95 cf->count = 0;
96
97 return 0;
98}
99
100
101static void xdl_free_classifier(xdlclassifier_t *cf) {
102
27af01d5 103 xdl_free(cf->rcrecs);
3443546f
LT
104 xdl_free(cf->rchash);
105 xdl_cha_free(&cf->ncha);
106}
107
108
27af01d5
TRC
109static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
110 unsigned int hbits, xrecord_t *rec) {
3443546f
LT
111 long hi;
112 char const *line;
113 xdlclass_t *rcrec;
27af01d5 114 xdlclass_t **rcrecs;
3443546f
LT
115
116 line = rec->ptr;
117 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
118 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
0d21efa5
JS
119 if (rcrec->ha == rec->ha &&
120 xdl_recmatch(rcrec->line, rcrec->size,
121 rec->ptr, rec->size, cf->flags))
3443546f
LT
122 break;
123
124 if (!rcrec) {
125 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
126
127 return -1;
128 }
129 rcrec->idx = cf->count++;
27af01d5
TRC
130 if (cf->count > cf->alloc) {
131 cf->alloc *= 2;
132 if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
133
134 return -1;
135 }
136 cf->rcrecs = rcrecs;
137 }
138 cf->rcrecs[rcrec->idx] = rcrec;
3443546f
LT
139 rcrec->line = line;
140 rcrec->size = rec->size;
141 rcrec->ha = rec->ha;
27af01d5 142 rcrec->len1 = rcrec->len2 = 0;
3443546f
LT
143 rcrec->next = cf->rchash[hi];
144 cf->rchash[hi] = rcrec;
145 }
146
27af01d5
TRC
147 (pass == 1) ? rcrec->len1++ : rcrec->len2++;
148
3443546f
LT
149 rec->ha = (unsigned long) rcrec->idx;
150
151 hi = (long) XDL_HASHLONG(rec->ha, hbits);
152 rec->next = rhash[hi];
153 rhash[hi] = rec;
154
155 return 0;
156}
157
158
27af01d5 159static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
160 xdlclassifier_t *cf, xdfile_t *xdf) {
161 unsigned int hbits;
452f4fa5 162 long nrec, hsize, bsize;
3443546f
LT
163 unsigned long hav;
164 char const *blk, *cur, *top, *prev;
165 xrecord_t *crec;
166 xrecord_t **recs, **rrecs;
167 xrecord_t **rhash;
168 unsigned long *ha;
169 char *rchg;
170 long *rindex;
171
159607a8
TRC
172 ha = NULL;
173 rindex = NULL;
174 rchg = NULL;
175 rhash = NULL;
176 recs = NULL;
3443546f 177
159607a8
TRC
178 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
179 goto abort;
abf04bda 180 if (!XDL_ALLOC_ARRAY(recs, narec))
159607a8 181 goto abort;
3443546f 182
663c5ad0
PW
183 hbits = xdl_hashbits((unsigned int) narec);
184 hsize = 1 << hbits;
18aae7e2 185 if (!(rhash = xdl_calloc(hsize, sizeof(*rhash))))
663c5ad0 186 goto abort;
3443546f
LT
187
188 nrec = 0;
afe8a907 189 if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
739864b1 190 for (top = blk + bsize; cur < top; ) {
3443546f 191 prev = cur;
0d21efa5 192 hav = xdl_hash_record(&cur, top, xpp->flags);
3443546f
LT
193 if (nrec >= narec) {
194 narec *= 2;
159607a8
TRC
195 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
196 goto abort;
3443546f
LT
197 recs = rrecs;
198 }
159607a8
TRC
199 if (!(crec = xdl_cha_alloc(&xdf->rcha)))
200 goto abort;
3443546f
LT
201 crec->ptr = prev;
202 crec->size = (long) (cur - prev);
203 crec->ha = hav;
204 recs[nrec++] = crec;
663c5ad0 205 if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
159607a8 206 goto abort;
3443546f
LT
207 }
208 }
209
18aae7e2 210 if (!(rchg = xdl_calloc((nrec + 2), sizeof(*rchg))))
159607a8 211 goto abort;
3443546f 212
b82dd3f7
PW
213 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
214 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
abf04bda 215 if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
b82dd3f7 216 goto abort;
abf04bda 217 if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
b82dd3f7
PW
218 goto abort;
219 }
3443546f
LT
220
221 xdf->nrec = nrec;
222 xdf->recs = recs;
223 xdf->hbits = hbits;
224 xdf->rhash = rhash;
225 xdf->rchg = rchg + 1;
226 xdf->rindex = rindex;
227 xdf->nreff = 0;
228 xdf->ha = ha;
229 xdf->dstart = 0;
230 xdf->dend = nrec - 1;
231
232 return 0;
159607a8
TRC
233
234abort:
235 xdl_free(ha);
236 xdl_free(rindex);
237 xdl_free(rchg);
238 xdl_free(rhash);
239 xdl_free(recs);
240 xdl_cha_free(&xdf->rcha);
241 return -1;
3443546f
LT
242}
243
244
245static void xdl_free_ctx(xdfile_t *xdf) {
246
247 xdl_free(xdf->rhash);
248 xdl_free(xdf->rindex);
249 xdl_free(xdf->rchg - 1);
250 xdl_free(xdf->ha);
251 xdl_free(xdf->recs);
252 xdl_cha_free(&xdf->rcha);
253}
254
255
256int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
257 xdfenv_t *xe) {
86abba80 258 long enl1, enl2, sample;
3443546f
LT
259 xdlclassifier_t cf;
260
2738bc3f
TRC
261 memset(&cf, 0, sizeof(cf));
262
86abba80
TRC
263 /*
264 * For histogram diff, we can afford a smaller sample size and
265 * thus a poorer estimate of the number of lines, as the hash
266 * table (rhash) won't be filled up/grown. The number of lines
267 * (nrecs) will be updated correctly anyway by
268 * xdl_prepare_ctx().
269 */
307ab20b
JH
270 sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
271 ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
86abba80
TRC
272
273 enl1 = xdl_guess_lines(mf1, sample) + 1;
274 enl2 = xdl_guess_lines(mf2, sample) + 1;
3443546f 275
663c5ad0 276 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
3443546f 277 return -1;
3443546f 278
27af01d5 279 if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
3443546f
LT
280
281 xdl_free_classifier(&cf);
282 return -1;
283 }
27af01d5 284 if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
3443546f
LT
285
286 xdl_free_ctx(&xe->xdf1);
287 xdl_free_classifier(&cf);
288 return -1;
289 }
290
307ab20b
JH
291 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
292 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
293 xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
3443546f
LT
294
295 xdl_free_ctx(&xe->xdf2);
296 xdl_free_ctx(&xe->xdf1);
87f16258 297 xdl_free_classifier(&cf);
3443546f
LT
298 return -1;
299 }
300
663c5ad0 301 xdl_free_classifier(&cf);
27af01d5 302
3443546f
LT
303 return 0;
304}
305
306
307void xdl_free_env(xdfenv_t *xe) {
308
309 xdl_free_ctx(&xe->xdf2);
310 xdl_free_ctx(&xe->xdf1);
311}
312
313
314static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
ca557aff
DL
315 long r, rdis0, rpdis0, rdis1, rpdis1;
316
9b28d554
DL
317 /*
318 * Limits the window the is examined during the similar-lines
319 * scan. The loops below stops when dis[i - r] == 1 (line that
320 * has no match), but there are corner cases where the loop
321 * proceed all the way to the extremities by causing huge
322 * performance penalties in case of big files.
323 */
324 if (i - s > XDL_SIMSCAN_WINDOW)
325 s = i - XDL_SIMSCAN_WINDOW;
326 if (e - i > XDL_SIMSCAN_WINDOW)
327 e = i + XDL_SIMSCAN_WINDOW;
328
ca557aff
DL
329 /*
330 * Scans the lines before 'i' to find a run of lines that either
331 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
332 * Note that we always call this function with dis[i] > 1, so the
333 * current line (i) is already a multimatch line.
334 */
335 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
3443546f 336 if (!dis[i - r])
ca557aff 337 rdis0++;
3443546f 338 else if (dis[i - r] == 2)
ca557aff 339 rpdis0++;
3443546f
LT
340 else
341 break;
342 }
ca557aff
DL
343 /*
344 * If the run before the line 'i' found only multimatch lines, we
345 * return 0 and hence we don't make the current line (i) discarded.
346 * We want to discard multimatch lines only when they appear in the
347 * middle of runs with nomatch lines (dis[j] == 0).
348 */
349 if (rdis0 == 0)
350 return 0;
351 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
3443546f 352 if (!dis[i + r])
ca557aff 353 rdis1++;
3443546f 354 else if (dis[i + r] == 2)
ca557aff 355 rpdis1++;
3443546f
LT
356 else
357 break;
358 }
ca557aff
DL
359 /*
360 * If the run after the line 'i' found only multimatch lines, we
361 * return 0 and hence we don't make the current line (i) discarded.
362 */
363 if (rdis1 == 0)
364 return 0;
365 rdis1 += rdis0;
366 rpdis1 += rpdis0;
367
368 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
3443546f
LT
369}
370
371
372/*
373 * Try to reduce the problem complexity, discard records that have no
374 * matches on the other file. Also, lines that have multiple matches
375 * might be potentially discarded if they happear in a run of discardable.
376 */
27af01d5 377static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
c5aa9068 378 long i, nm, nreff, mlim;
3443546f 379 xrecord_t **recs;
27af01d5 380 xdlclass_t *rcrec;
3443546f
LT
381 char *dis, *dis1, *dis2;
382
18aae7e2 383 if (!(dis = xdl_calloc(xdf1->nrec + xdf2->nrec + 2, sizeof(*dis)))) {
3443546f
LT
384
385 return -1;
386 }
3443546f
LT
387 dis1 = dis;
388 dis2 = dis1 + xdf1->nrec + 1;
389
c5aa9068
RS
390 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
391 mlim = XDL_MAX_EQLIMIT;
3443546f 392 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
27af01d5
TRC
393 rcrec = cf->rcrecs[(*recs)->ha];
394 nm = rcrec ? rcrec->len2 : 0;
c5aa9068 395 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
396 }
397
c5aa9068
RS
398 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
399 mlim = XDL_MAX_EQLIMIT;
3443546f 400 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
27af01d5
TRC
401 rcrec = cf->rcrecs[(*recs)->ha];
402 nm = rcrec ? rcrec->len1 : 0;
c5aa9068 403 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
404 }
405
406 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
407 i <= xdf1->dend; i++, recs++) {
408 if (dis1[i] == 1 ||
409 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
410 xdf1->rindex[nreff] = i;
411 xdf1->ha[nreff] = (*recs)->ha;
412 nreff++;
413 } else
414 xdf1->rchg[i] = 1;
415 }
416 xdf1->nreff = nreff;
417
418 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
419 i <= xdf2->dend; i++, recs++) {
420 if (dis2[i] == 1 ||
421 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
422 xdf2->rindex[nreff] = i;
423 xdf2->ha[nreff] = (*recs)->ha;
424 nreff++;
425 } else
426 xdf2->rchg[i] = 1;
427 }
428 xdf2->nreff = nreff;
429
430 xdl_free(dis);
431
432 return 0;
433}
434
435
436/*
437 * Early trim initial and terminal matching records.
438 */
439static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
440 long i, lim;
441 xrecord_t **recs1, **recs2;
442
443 recs1 = xdf1->recs;
444 recs2 = xdf2->recs;
445 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
446 i++, recs1++, recs2++)
447 if ((*recs1)->ha != (*recs2)->ha)
448 break;
449
450 xdf1->dstart = xdf2->dstart = i;
451
452 recs1 = xdf1->recs + xdf1->nrec - 1;
453 recs2 = xdf2->recs + xdf2->nrec - 1;
454 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
455 if ((*recs1)->ha != (*recs2)->ha)
456 break;
457
458 xdf1->dend = xdf1->nrec - i - 1;
459 xdf2->dend = xdf2->nrec - i - 1;
460
461 return 0;
462}
463
464
27af01d5 465static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
3443546f
LT
466
467 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
27af01d5 468 xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
3443546f
LT
469
470 return -1;
471 }
472
473 return 0;
474}