public
Created

[gsoc-diff] malloc()/free() once

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
index ea7b82d..2e63235 100644
--- a/xdiff/xhistogram.c
+++ b/xdiff/xhistogram.c
@@ -237,12 +237,10 @@ static int fall_back_to_classic_diff(struct histindex *index,
line1, count1, line2, count2);
}
-static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
+static int histogram_diff(struct histindex *index,
int line1, int count1, int line2, int count2)
{
- struct histindex index;
struct region lcs;
- int sz;
int result = -1;
if (count1 <= 0 && count2 <= 0)
@@ -253,85 +251,57 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
if (!count1) {
while(count2--)
- env->xdf2.rchg[line2++ - 1] = 1;
+ index->env->xdf2.rchg[line2++ - 1] = 1;
return 0;
} else if (!count2) {
while(count1--)
- env->xdf1.rchg[line1++ - 1] = 1;
+ index->env->xdf1.rchg[line1++ - 1] = 1;
return 0;
}
- memset(&index, 0, sizeof(index));
+ index->recs_count = 0;
+ index->cnt = 0;
+ index->has_common = 0;
- index.env = env;
- index.xpp = xpp;
+ memset(index->table, 0, index->table_size * sizeof(int));
+ memset(index->recs, 0, index->recs_size * sizeof(long));
+ memset(index->next, 0, index->size * sizeof(int));
+ memset(index->rec_idxs, 0, index->size * sizeof(int));
- index.table = NULL;
- index.recs = NULL;
- index.next = NULL;
- index.rec_idxs = NULL;
-
- index.table_bits = xdl_hashbits(count1);
- sz = index.table_size = 1 << index.table_bits;
- sz *= sizeof(int);
- if (!(index.table = xdl_malloc(sz)))
- goto cleanup;
- memset(index.table, 0, sz);
-
- sz = index.recs_size = XDL_MAX(4, count1 >> 3);
- sz *= sizeof(long);
- if (!(index.recs = xdl_malloc(sz)))
- goto cleanup;
- memset(index.recs, 0, sz);
-
- sz = index.size = count1;
- sz *= sizeof(int);
- if (!(index.next = xdl_malloc(sz)))
- goto cleanup;
- memset(index.next, 0, sz);
-
- if (!(index.rec_idxs = xdl_malloc(sz)))
- goto cleanup;
- memset(index.rec_idxs, 0, sz);
-
- index.ptr_shift = line1;
- index.max_chain_length = 64;
+ 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);
+ if (find_lcs(index, &lcs, line1, count1, line2, count2))
+ result = fall_back_to_classic_diff(index, line1, count1, line2, count2);
else {
result = 0;
if (lcs.begin1 == 0 && lcs.begin2 == 0) {
int ptr;
for (ptr = 0; ptr < count1; ptr++)
- env->xdf1.rchg[line1 + ptr - 1] = 1;
+ index->env->xdf1.rchg[line1 + ptr - 1] = 1;
for (ptr = 0; ptr < count2; ptr++)
- env->xdf2.rchg[line2 + ptr - 1] = 1;
+ index->env->xdf2.rchg[line2 + ptr - 1] = 1;
} else {
- result = histogram_diff(xpp, env,
+ result = histogram_diff(index,
line1, lcs.begin1 - line1,
line2, lcs.begin2 - line2);
- result = histogram_diff(xpp, env,
+ result = histogram_diff(index,
lcs.end1 + 1, LINE_END(1) - lcs.end1,
lcs.end2 + 1, LINE_END(2) - lcs.end2);
result *= -1;
}
}
-cleanup:
- xdl_free(index.table);
- xdl_free(index.recs);
- xdl_free(index.next);
- xdl_free(index.rec_idxs);
-
return result;
}
int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2,
xpparam_t const *xpp, xdfenv_t *env)
{
+ struct histindex index;
int line1, line2, count1, count2;
+ int result = -1;
if (xdl_prepare_env(file1, file2, xpp, env) < 0)
return -1;
@@ -342,5 +312,37 @@ int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2,
reduce_common_start_end(xpp, env, &line1, &count1, &line2, &count2);
- return histogram_diff(xpp, env, line1, count1, line2, count2);
+ index.env = env;
+ index.xpp = xpp;
+
+ index.table = NULL;
+ index.recs = NULL;
+ index.next = NULL;
+ index.rec_idxs = NULL;
+
+ index.table_bits = xdl_hashbits(count1);
+ index.table_size = 1 << index.table_bits;
+ if (!(index.table = xdl_malloc(index.table_size * sizeof(int))))
+ goto cleanup;
+
+ index.recs_size = XDL_MAX(4, count1 >> 3);
+ if (!(index.recs = xdl_malloc(index.recs_size * sizeof(long))))
+ goto cleanup;
+
+ index.size = count1;
+ if (!(index.next = xdl_malloc(index.size * sizeof(int))))
+ goto cleanup;
+
+ if (!(index.rec_idxs = xdl_malloc(index.size * sizeof(int))))
+ goto cleanup;
+
+ result = histogram_diff(&index, line1, count1, line2, count2);
+
+cleanup:
+ xdl_free(index.table);
+ xdl_free(index.recs);
+ xdl_free(index.next);
+ xdl_free(index.rec_idxs);
+
+ return result;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.