diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-08-27 20:00:28 +0200 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-08-27 20:14:07 +0200 |
commit | 191adce8751e728111a3b1c3e9b2c02fe9f5d775 (patch) | |
tree | 0d93d9df9aaca365ad0a9e20f27ab4f13a76143f /src | |
parent | c9ffa84bde45021c40623553822916fb3d13b20a (diff) | |
download | libgit2-191adce8751e728111a3b1c3e9b2c02fe9f5d775.tar.gz |
vector: Teach git_vector_uniq() to free while deduplicating
Diffstat (limited to 'src')
-rw-r--r-- | src/util.h | 5 | ||||
-rw-r--r-- | src/vector.c | 9 | ||||
-rw-r--r-- | src/vector.h | 2 |
3 files changed, 11 insertions, 5 deletions
diff --git a/src/util.h b/src/util.h index a784390c1..bd93b46b5 100644 --- a/src/util.h +++ b/src/util.h @@ -82,7 +82,10 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size) return new_ptr; } -#define git__free(ptr) free(ptr) +GIT_INLINE(void) git__free(void *ptr) +{ + free(ptr); +} #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \ ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2))) diff --git a/src/vector.c b/src/vector.c index 5ba2fab18..362e7b0c0 100644 --- a/src/vector.c +++ b/src/vector.c @@ -220,7 +220,7 @@ void git_vector_pop(git_vector *v) v->length--; } -void git_vector_uniq(git_vector *v) +void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *)) { git_vector_cmp cmp; size_t i, j; @@ -232,9 +232,12 @@ void git_vector_uniq(git_vector *v) cmp = v->_cmp ? v->_cmp : strict_comparison; for (i = 0, j = 1 ; j < v->length; ++j) - if (!cmp(v->contents[i], v->contents[j])) + if (!cmp(v->contents[i], v->contents[j])) { + if (git_free_cb) + git_free_cb(v->contents[i]); + v->contents[i] = v->contents[j]; - else + } else v->contents[++i] = v->contents[j]; v->length -= j - i - 1; diff --git a/src/vector.h b/src/vector.h index 1bda9c93d..cca846f43 100644 --- a/src/vector.h +++ b/src/vector.h @@ -71,7 +71,7 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **old, void *new)); int git_vector_remove(git_vector *v, size_t idx); void git_vector_pop(git_vector *v); -void git_vector_uniq(git_vector *v); +void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *)); void git_vector_remove_matching( git_vector *v, int (*match)(const git_vector *v, size_t idx)); |