Commit e1ad6ebcf6e7b7a707020aa0829c05a16e8a24a4

Stefan Sperling 2018-03-17T02:12:34

remove path_packfile from struct got_delta_cache; add a counter instead

diff --git a/lib/got_pack_lib.h b/lib/got_pack_lib.h
index 61b5c9d..0cea059 100644
--- a/lib/got_pack_lib.h
+++ b/lib/got_pack_lib.h
@@ -23,7 +23,7 @@ struct got_delta_cache_entry {
 #define GOT_DELTA_CACHE_SIZE	1024
 
 struct got_delta_cache {
-	char *path_packfile;
+	int nentries;
 	struct got_delta_cache_entry deltas[GOT_DELTA_CACHE_SIZE];
 };
 
diff --git a/lib/pack.c b/lib/pack.c
index cdb8863..4975158 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -580,7 +580,6 @@ const struct got_error *
 got_pack_close(struct got_pack *pack)
 {
 	const struct got_error *err = NULL;
-	struct got_delta_cache *cache = &pack->delta_cache;
 	int i;
 
 	while (!TAILQ_EMPTY(&pack->mappings)) {
@@ -600,17 +599,15 @@ got_pack_close(struct got_pack *pack)
 		pack->filesize = 0;
 	}
 
-	free(cache->path_packfile);
-	cache->path_packfile = NULL;
-	for (i = 0; i < nitems(cache->deltas); i++) {
-		struct got_delta_cache_entry *entry = &cache->deltas[i];
-		if (entry->data_offset == 0)
-			break;
+	for (i = 0; i < pack->delta_cache.nentries; i++) {
+		struct got_delta_cache_entry *entry;
+		entry = &pack->delta_cache.deltas[i];
 		entry->data_offset = 0;
 		free(entry->delta_buf);
 		entry->delta_buf = NULL;
 		entry->delta_len = 0;
 	}
+	pack->delta_cache.nentries = 0;
 
 	return err;
 }
@@ -1149,41 +1146,31 @@ clear_delta_cache_entry(struct got_delta_cache_entry *entry)
 }
 
 const struct got_error *
-add_delta_cache_entry(struct got_delta_cache *cache, off_t data_offset,
-    uint8_t *delta_buf, size_t delta_len)
+cache_delta(off_t data_offset, uint8_t *delta_buf, size_t delta_len,
+    struct got_pack *pack)
 {
 	int i;
+	struct got_delta_cache *cache = &pack->delta_cache;
 	struct got_delta_cache_entry *entry;
 
-	for (i = 0; i < nitems(cache->deltas); i++) {
-		entry = &cache->deltas[i];
-		if (entry->data_offset == 0)
-			break;
-	}
-
-	if (i == nitems(cache->deltas)) {
-		entry = &cache->deltas[i - 1];
+	if (cache->nentries == nitems(cache->deltas)) {
+		entry = &cache->deltas[cache->nentries - 1];
 		clear_delta_cache_entry(entry);
+		cache->nentries--;
 		memmove(&cache->deltas[1], &cache->deltas[0],
 		    sizeof(cache->deltas) - sizeof(cache->deltas[0]));
 		i = 0;
-	}
+	} else
+		i = cache->nentries;
 
 	entry = &cache->deltas[i];
 	entry->data_offset = data_offset;
 	entry->delta_buf = delta_buf;
 	entry->delta_len = delta_len;
+	cache->nentries++;
 	return NULL;
 }
 
-const struct got_error *
-cache_delta(off_t data_offset, uint8_t *delta_buf, size_t delta_len,
-    struct got_pack *pack)
-{
-	return add_delta_cache_entry(&pack->delta_cache, data_offset,
-	    delta_buf, delta_len);
-}
-
 void
 get_cached_delta(uint8_t **delta_buf, size_t *delta_len, off_t data_offset,
     struct got_pack *pack)