Created
July 7, 2011 05:03
-
-
Save rctay/1068912 to your computer and use it in GitHub Desktop.
[gsoc-diff] malloc()/free() once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment