Skip to content

Commit c976b4f

Browse files
committed
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 parent 18cf569 commit c976b4f

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/indexer.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "pack.h"
2020
#include "filebuf.h"
2121
#include "oid.h"
22+
#include "oidarray.h"
2223
#include "oidmap.h"
2324
#include "zstream.h"
2425
#include "object.h"
@@ -321,7 +322,7 @@ static int add_expected_oid(git_indexer *idx, const git_oid *oid)
321322
!git_oidmap_exists(idx->expected_oids, oid)) {
322323
git_oid *dup = git__malloc(sizeof(*oid));
323324
git_oid_cpy(dup, oid);
324-
return git_oidmap_set(idx->expected_oids, dup, NULL);
325+
return git_oidmap_set(idx->expected_oids, dup, dup);
325326
}
326327

327328
return 0;
@@ -330,7 +331,7 @@ static int add_expected_oid(git_indexer *idx, const git_oid *oid)
330331
static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
331332
{
332333
git_object *object;
333-
size_t keyidx;
334+
git_oid *expected;
334335
int error;
335336

336337
if (obj->type != GIT_OBJECT_BLOB &&
@@ -342,11 +343,9 @@ static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
342343
if ((error = git_object__from_raw(&object, obj->data, obj->len, obj->type)) < 0)
343344
goto out;
344345

345-
keyidx = git_oidmap_lookup_index(idx->expected_oids, &object->cached.oid);
346-
if (git_oidmap_valid_index(idx->expected_oids, keyidx)) {
347-
const git_oid *key = git_oidmap_key(idx->expected_oids, keyidx);
348-
git__free((git_oid *) key);
349-
git_oidmap_delete_at(idx->expected_oids, keyidx);
346+
if ((expected = git_oidmap_get(idx->expected_oids, &object->cached.oid)) != NULL) {
347+
git_oidmap_delete(idx->expected_oids, &object->cached.oid);
348+
git__free(expected);
350349
}
351350

352351
/*
@@ -1289,7 +1288,9 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
12891288

12901289
void git_indexer_free(git_indexer *idx)
12911290
{
1292-
size_t pos;
1291+
const git_oid *key;
1292+
git_oid *value;
1293+
size_t iter;
12931294

12941295
if (idx == NULL)
12951296
return;
@@ -1318,14 +1319,9 @@ void git_indexer_free(git_indexer *idx)
13181319
git_mutex_unlock(&git__mwindow_mutex);
13191320
}
13201321

1321-
for (pos = git_oidmap_begin(idx->expected_oids);
1322-
pos != git_oidmap_end(idx->expected_oids); pos++)
1323-
{
1324-
if (git_oidmap_has_data(idx->expected_oids, pos)) {
1325-
git__free((git_oid *) git_oidmap_key(idx->expected_oids, pos));
1326-
git_oidmap_delete_at(idx->expected_oids, pos);
1327-
}
1328-
}
1322+
iter = 0;
1323+
while (git_oidmap_iterate((void **) &value, idx->expected_oids, &iter, &key) == 0)
1324+
git__free(value);
13291325

13301326
git_hash_ctx_cleanup(&idx->trailer);
13311327
git_hash_ctx_cleanup(&idx->hash_ctx);

0 commit comments

Comments
 (0)