Commit 9694d9ba794b36ebc9fc186c43464a08c2f84c4d

Patrick Steinhardt 2017-01-25T14:09:17

khash: avoid using `kh_foreach`/`kh_foreach_value` directly

diff --git a/src/cache.c b/src/cache.c
index e8fd207..e7a565e 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -53,7 +53,7 @@ void git_cache_dump_stats(git_cache *cache)
 	printf("Cache %p: %"PRIuZ" items cached, %"PRIdZ" bytes\n",
 		cache, git_cache_size(cache), cache->used_memory);
 
-	kh_foreach_value(cache->map, object, {
+	git_oidmap_foreach_value(cache->map, object, {
 		char oid_str[9];
 		printf(" %s%c %s (%"PRIuZ")\n",
 			git_object_type2string(object->type),
@@ -84,7 +84,7 @@ static void clear_cache(git_cache *cache)
 	if (git_cache_size(cache) == 0)
 		return;
 
-	kh_foreach_value(cache->map, evict, {
+	git_oidmap_foreach_value(cache->map, evict, {
 		git_cached_obj_decref(evict);
 	});
 
diff --git a/src/indexer.c b/src/indexer.c
index 606de2e..8b5fa6e 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -1106,8 +1106,9 @@ void git_indexer_free(git_indexer *idx)
 
 	if (idx->pack->idx_cache) {
 		struct git_pack_entry *pentry;
-		kh_foreach_value(
-			idx->pack->idx_cache, pentry, { git__free(pentry); });
+		git_oidmap_foreach_value(idx->pack->idx_cache, pentry, {
+			git__free(pentry);
+		});
 
 		git_oidmap_free(idx->pack->idx_cache);
 	}
diff --git a/src/odb_mempack.c b/src/odb_mempack.c
index 84ed9c1..b9bdf0b 100644
--- a/src/odb_mempack.c
+++ b/src/odb_mempack.c
@@ -149,7 +149,7 @@ void git_mempack_reset(git_odb_backend *_backend)
 	struct memory_packer_db *db = (struct memory_packer_db *)_backend;
 	struct memobject *object = NULL;
 
-	kh_foreach_value(db->objects, object, {
+	git_oidmap_foreach_value(db->objects, object, {
 		git__free(object);
 	});
 
diff --git a/src/pack.c b/src/pack.c
index 243719d..0f2fa5c 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -78,13 +78,12 @@ static void free_cache_object(void *o)
 
 static void cache_free(git_pack_cache *cache)
 {
-	khiter_t k;
+	git_pack_cache_entry *entry;
 
 	if (cache->entries) {
-		for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) {
-			if (kh_exist(cache->entries, k))
-				free_cache_object(kh_value(cache->entries, k));
-		}
+		git_offmap_foreach_value(cache->entries, entry, {
+			free_cache_object(entry);
+		});
 
 		git_offmap_free(cache->entries);
 		cache->entries = NULL;
@@ -135,18 +134,13 @@ static void free_lowest_entry(git_pack_cache *cache)
 	git_pack_cache_entry *entry;
 	khiter_t k;
 
-	for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) {
-		if (!kh_exist(cache->entries, k))
-			continue;
-
-		entry = kh_value(cache->entries, k);
-
+	git_offmap_foreach(cache->entries, k, entry, {
 		if (entry && entry->refcount.val == 0) {
 			cache->memory_used -= entry->raw.len;
 			kh_del(off, cache->entries, k);
 			free_cache_object(entry);
 		}
-	}
+	});
 }
 
 static int cache_add(
diff --git a/src/revwalk.c b/src/revwalk.c
index 8c370bc..c5fdb7e 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -702,7 +702,7 @@ void git_revwalk_reset(git_revwalk *walk)
 
 	assert(walk);
 
-	kh_foreach_value(walk->commits, commit, {
+	git_oidmap_foreach_value(walk->commits, commit, {
 		commit->seen = 0;
 		commit->in_degree = 0;
 		commit->topo_delay = 0;
diff --git a/src/transaction.c b/src/transaction.c
index 2c8a1e8..ffe374a 100644
--- a/src/transaction.c
+++ b/src/transaction.c
@@ -323,7 +323,6 @@ static int update_target(git_refdb *db, transaction_node *node)
 int git_transaction_commit(git_transaction *tx)
 {
 	transaction_node *node;
-	git_strmap_iter pos;
 	int error = 0;
 
 	assert(tx);
@@ -335,11 +334,7 @@ int git_transaction_commit(git_transaction *tx)
 		return error;
 	}
 
-	for (pos = kh_begin(tx->locks); pos < kh_end(tx->locks); pos++) {
-		if (!git_strmap_has_data(tx->locks, pos))
-			continue;
-
-		node = git_strmap_value_at(tx->locks, pos);
+	git_strmap_foreach_value(tx->locks, node, {
 		if (node->reflog) {
 			if ((error = tx->db->backend->reflog_write(tx->db->backend, node->reflog)) < 0)
 				return error;
@@ -349,7 +344,7 @@ int git_transaction_commit(git_transaction *tx)
 			if ((error = update_target(tx->db, node)) < 0)
 				return error;
 		}
-	}
+	});
 
 	return 0;
 }
@@ -358,7 +353,6 @@ void git_transaction_free(git_transaction *tx)
 {
 	transaction_node *node;
 	git_pool pool;
-	git_strmap_iter pos;
 
 	assert(tx);
 
@@ -373,16 +367,12 @@ void git_transaction_free(git_transaction *tx)
 	}
 
 	/* start by unlocking the ones we've left hanging, if any */
-	for (pos = kh_begin(tx->locks); pos < kh_end(tx->locks); pos++) {
-		if (!git_strmap_has_data(tx->locks, pos))
-			continue;
-
-		node = git_strmap_value_at(tx->locks, pos);
+	git_strmap_foreach_value(tx->locks, node, {
 		if (node->committed)
 			continue;
 
 		git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL);
-	}
+	});
 
 	git_refdb_free(tx->db);
 	git_strmap_free(tx->locks);