Commit 54f20211c589e3522ed62f05814143d5450a49a7

Stefan Sperling 2018-06-22T10:00:23

introduce struct got_object_cache

diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index 6fc9d6a..db28762 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -19,11 +19,18 @@
 
 #define GOT_OBJECT_CACHE_SIZE	8192
 
-struct got_objcache_entry {
+struct got_object_cache_entry {
 	struct got_object_id id;
 	struct got_object *obj;
 };
 
+struct got_object_cache {
+	struct got_object_idset *set;
+	int ncached;
+	int cache_hit;
+	int cache_miss;
+};
+
 struct got_repository {
 	char *path;
 	char *path_git_dir;
@@ -34,10 +41,7 @@ struct got_repository {
 	/* Open file handles for pack files. */
 	struct got_pack packs[GOT_PACK_CACHE_SIZE];
 
-	struct got_object_idset *objcache;
-	int ncached;
-	int cache_hit;
-	int cache_miss;
+	struct got_object_cache objcache;
 };
 
 const struct got_error*
diff --git a/lib/repository.c b/lib/repository.c
index 28df53b..9a2fb62 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -154,16 +154,16 @@ got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
     struct got_object *obj)
 {
 	const struct got_error *err = NULL;
-	struct got_objcache_entry *ce;
+	struct got_object_cache_entry *ce;
 
-	if (repo->ncached >= GOT_OBJECT_CACHE_SIZE) {
+	if (repo->objcache.ncached >= GOT_OBJECT_CACHE_SIZE) {
 		err = got_object_idset_remove_random((void **)&ce,
-		    repo->objcache);
+		    repo->objcache.set);
 		if (err)
 			return err;
 		got_object_close(ce->obj);
 		free(ce);
-		repo->ncached--;
+		repo->objcache.ncached--;
 	}
 
 	ce = calloc(1, sizeof(*ce));
@@ -171,7 +171,7 @@ got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
 		return got_error_from_errno();
 	memcpy(&ce->id, id, sizeof(ce->id));
 	ce->obj = obj;
-	err = got_object_idset_add(NULL, repo->objcache, id, ce);
+	err = got_object_idset_add(NULL, repo->objcache.set, id, ce);
 	if (err) {
 		if (err->code == GOT_ERR_OBJ_EXISTS) {
 			free(ce);
@@ -179,7 +179,7 @@ got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
 		}
 	} else {
 		obj->refcnt++;
-		repo->ncached++;
+		repo->objcache.ncached++;
 	}
 
 	return err;
@@ -189,14 +189,14 @@ struct got_object *
 got_repo_get_cached_object(struct got_repository *repo,
     struct got_object_id *id)
 {
-	struct got_objcache_entry *ce;
+	struct got_object_cache_entry *ce;
 
-	ce = got_object_idset_get(repo->objcache, id);
+	ce = got_object_idset_get(repo->objcache.set, id);
 	if (ce) {
-		repo->cache_hit++;
+		repo->objcache.cache_hit++;
 		return ce->obj;
 	}
-	repo->cache_miss++;
+	repo->objcache.cache_miss++;
 	return NULL;
 }
 
@@ -220,8 +220,8 @@ got_repo_open(struct got_repository **ret, const char *path)
 		goto done;
 	}
 
-	repo->objcache = got_object_idset_alloc();
-	if (repo->objcache == NULL) {
+	repo->objcache.set = got_object_idset_alloc();
+	if (repo->objcache.set == NULL) {
 		err = got_error_from_errno();
 		goto done;
 	}
@@ -299,7 +299,7 @@ got_repo_close(struct got_repository *repo)
 
 	free(repo->path);
 	free(repo->path_git_dir);
-	if (repo->objcache)
-		got_object_idset_free(repo->objcache);
+	if (repo->objcache.set)
+		got_object_idset_free(repo->objcache.set);
 	free(repo);
 }