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