indexer: use map iterator to delete expected OIDs To compute whether there are objects missing in a packfile, the indexer keeps around a map of OIDs that it still expects to see. This map does not store any values at all, but in fact the keys are owned by the map itself. Right now, we free these keys by iterating over the map and freeing the key itself, which is kind of awkward as keys are expected to be constant. We can make this a bit prettier by inserting the OID as value, too. As we already store the `NULL` pointer either way, this does not increase memory usage, but makes the code a tad more clear. Furthermore, we convert the previously existing map iteration via indices to make use of an iterator, instead.
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
diff --git a/src/indexer.c b/src/indexer.c
index 9ad78e8..6153cc2 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -19,6 +19,7 @@
#include "pack.h"
#include "filebuf.h"
#include "oid.h"
+#include "oidarray.h"
#include "oidmap.h"
#include "zstream.h"
#include "object.h"
@@ -321,7 +322,7 @@ static int add_expected_oid(git_indexer *idx, const git_oid *oid)
!git_oidmap_exists(idx->expected_oids, oid)) {
git_oid *dup = git__malloc(sizeof(*oid));
git_oid_cpy(dup, oid);
- return git_oidmap_set(idx->expected_oids, dup, NULL);
+ return git_oidmap_set(idx->expected_oids, dup, dup);
}
return 0;
@@ -330,7 +331,7 @@ static int add_expected_oid(git_indexer *idx, const git_oid *oid)
static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
{
git_object *object;
- size_t keyidx;
+ git_oid *expected;
int error;
if (obj->type != GIT_OBJECT_BLOB &&
@@ -342,11 +343,9 @@ static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
if ((error = git_object__from_raw(&object, obj->data, obj->len, obj->type)) < 0)
goto out;
- keyidx = git_oidmap_lookup_index(idx->expected_oids, &object->cached.oid);
- if (git_oidmap_valid_index(idx->expected_oids, keyidx)) {
- const git_oid *key = git_oidmap_key(idx->expected_oids, keyidx);
- git__free((git_oid *) key);
- git_oidmap_delete_at(idx->expected_oids, keyidx);
+ if ((expected = git_oidmap_get(idx->expected_oids, &object->cached.oid)) != NULL) {
+ git_oidmap_delete(idx->expected_oids, &object->cached.oid);
+ git__free(expected);
}
/*
@@ -1289,7 +1288,9 @@ on_error:
void git_indexer_free(git_indexer *idx)
{
- size_t pos;
+ const git_oid *key;
+ git_oid *value;
+ size_t iter;
if (idx == NULL)
return;
@@ -1318,14 +1319,9 @@ void git_indexer_free(git_indexer *idx)
git_mutex_unlock(&git__mwindow_mutex);
}
- for (pos = git_oidmap_begin(idx->expected_oids);
- pos != git_oidmap_end(idx->expected_oids); pos++)
- {
- if (git_oidmap_has_data(idx->expected_oids, pos)) {
- git__free((git_oid *) git_oidmap_key(idx->expected_oids, pos));
- git_oidmap_delete_at(idx->expected_oids, pos);
- }
- }
+ iter = 0;
+ while (git_oidmap_iterate((void **) &value, idx->expected_oids, &iter, &key) == 0)
+ git__free(value);
git_hash_ctx_cleanup(&idx->trailer);
git_hash_ctx_cleanup(&idx->hash_ctx);