static int xdl_init_classifier(xdlclassifier_t *cf, long size) { long i; cf->hbits = xdl_hashbits((unsigned int) size); cf->hsize = 1 << cf->hbits; if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) { return -1; } if (!(cf->rchash = (xdlclass_t **) malloc(cf->hsize * sizeof(xdlclass_t *)))) { xdl_cha_free(&cf->ncha); return -1; } for (i = 0; i < cf->hsize; i++) cf->rchash[i] = NULL; cf->count = 0; return 0; }
static int find_lcs(xpparam_t const *xpp, xdfenv_t *env, struct region *lcs, int line1, int count1, int line2, int count2) { int b_ptr; int sz, ret = -1; struct histindex index; memset(&index, 0, sizeof(index)); index.env = env; index.xpp = xpp; index.records = NULL; index.line_map = NULL; /* in case of early xdl_cha_free() */ index.rcha.head = NULL; index.table_bits = xdl_hashbits(count1); sz = index.records_size = 1 << index.table_bits; sz *= sizeof(struct record *); if (!(index.records = (struct record **) xdl_malloc(sz))) goto cleanup; memset(index.records, 0, sz); sz = index.line_map_size = count1; sz *= sizeof(struct record *); if (!(index.line_map = (struct record **) xdl_malloc(sz))) goto cleanup; memset(index.line_map, 0, sz); sz = index.line_map_size; sz *= sizeof(unsigned int); if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz))) goto cleanup; memset(index.next_ptrs, 0, sz); /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0) goto cleanup; index.ptr_shift = line1; index.max_chain_length = 64; if (scanA(&index, line1, count1)) goto cleanup; index.cnt = index.max_chain_length + 1; for (b_ptr = line2; b_ptr <= LINE_END(2); ) b_ptr = try_lcs(&index, lcs, b_ptr, line1, count1, line2, count2); if (index.has_common && index.max_chain_length < index.cnt) ret = 1; else ret = 0; cleanup: free_index(&index); return ret; }
static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env, int line1, int count1, int line2, int count2) { struct histindex index; struct region lcs; int sz; int result = -1; if (count1 <= 0 && count2 <= 0) return 0; if (LINE_END(1) >= MAX_PTR) return -1; if (!count1) { while(count2--) env->xdf2.rchg[line2++ - 1] = 1; return 0; } else if (!count2) { while(count1--) env->xdf1.rchg[line1++ - 1] = 1; return 0; } memset(&index, 0, sizeof(index)); index.env = env; index.xpp = xpp; index.records = NULL; index.line_map = NULL; /* in case of early xdl_cha_free() */ index.rcha.head = NULL; index.table_bits = xdl_hashbits(count1); sz = index.records_size = 1 << index.table_bits; sz *= sizeof(struct record *); if (!(index.records = (struct record **) xdl_malloc(sz))) goto cleanup; memset(index.records, 0, sz); sz = index.line_map_size = count1; sz *= sizeof(struct record *); if (!(index.line_map = (struct record **) xdl_malloc(sz))) goto cleanup; memset(index.line_map, 0, sz); sz = index.line_map_size; sz *= sizeof(unsigned int); if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz))) goto cleanup; memset(index.next_ptrs, 0, sz); /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0) goto cleanup; index.ptr_shift = line1; index.max_chain_length = 64; memset(&lcs, 0, sizeof(lcs)); if (find_lcs(&index, &lcs, line1, count1, line2, count2)) result = fall_back_to_classic_diff(&index, line1, count1, line2, count2); else { if (lcs.begin1 == 0 && lcs.begin2 == 0) { while (count1--) env->xdf1.rchg[line1++ - 1] = 1; while (count2--) env->xdf2.rchg[line2++ - 1] = 1; result = 0; } else { result = histogram_diff(xpp, env, line1, lcs.begin1 - line1, line2, lcs.begin2 - line2); if (result) goto cleanup; result = histogram_diff(xpp, env, lcs.end1 + 1, LINE_END(1) - lcs.end1, lcs.end2 + 1, LINE_END(2) - lcs.end2); if (result) goto cleanup; } } cleanup: xdl_free(index.records); xdl_free(index.line_map); xdl_free(index.next_ptrs); xdl_cha_free(&index.rcha); return result; }
static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdlclassifier_t *cf, xdfile_t *xdf) { unsigned int hbits; long i, nrec, hsize, bsize; unsigned long hav; char const *blk, *cur, *top, *prev; xrecord_t *crec; xrecord_t **recs, **rrecs; xrecord_t **rhash; unsigned long *ha; char *rchg; long *rindex; if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) { return -1; } if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) { xdl_cha_free(&xdf->rcha); return -1; } hbits = xdl_hashbits((unsigned int) narec); hsize = 1 << hbits; if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) { xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } for (i = 0; i < hsize; i++) rhash[i] = NULL; nrec = 0; if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { for (top = blk + bsize;;) { if (cur >= top) { if (!(cur = blk = xdl_mmfile_next(mf, &bsize))) break; top = blk + bsize; } prev = cur; hav = xdl_hash_record(&cur, top, xpp->flags); if (nrec >= narec) { narec *= 2; if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) { xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } recs = rrecs; } if (!(crec = xdl_cha_alloc(&xdf->rcha))) { xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } crec->ptr = prev; crec->size = (long) (cur - prev); crec->ha = hav; recs[nrec++] = crec; if (xdl_classify_record(cf, rhash, hbits, crec) < 0) { xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } } } if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) { xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } memset(rchg, 0, (nrec + 2) * sizeof(char)); if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) { xdl_free(rchg); xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) { xdl_free(rindex); xdl_free(rchg); xdl_free(rhash); xdl_free(recs); xdl_cha_free(&xdf->rcha); return -1; } xdf->nrec = nrec; xdf->recs = recs; xdf->hbits = hbits; xdf->rhash = rhash; xdf->rchg = rchg + 1; xdf->rindex = rindex; xdf->nreff = 0; xdf->ha = ha; xdf->dstart = 0; xdf->dend = nrec - 1; return 0; }