move delta cache into struct got_pack
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
diff --git a/lib/got_pack_lib.h b/lib/got_pack_lib.h
index 7810cfb..61b5c9d 100644
--- a/lib/got_pack_lib.h
+++ b/lib/got_pack_lib.h
@@ -14,6 +14,19 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+struct got_delta_cache_entry {
+ off_t data_offset;
+ uint8_t *delta_buf;
+ size_t delta_len;
+};
+
+#define GOT_DELTA_CACHE_SIZE 1024
+
+struct got_delta_cache {
+ char *path_packfile;
+ struct got_delta_cache_entry deltas[GOT_DELTA_CACHE_SIZE];
+};
+
/* A pack file segment mapped with mmap(2). */
struct got_pack_mapping {
TAILQ_ENTRY(got_pack_mapping) entry;
@@ -33,7 +46,10 @@ struct got_pack {
FILE *packfile;
size_t filesize;
int nmappings;
+
TAILQ_HEAD(, got_pack_mapping) mappings;
+
+ struct got_delta_cache delta_cache;
};
const struct got_error *got_pack_close(struct got_pack *);
diff --git a/lib/got_repository_lib.h b/lib/got_repository_lib.h
index 410d1e0..683bfdd 100644
--- a/lib/got_repository_lib.h
+++ b/lib/got_repository_lib.h
@@ -14,19 +14,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-struct got_delta_cache_entry {
- off_t data_offset;
- uint8_t *delta_buf;
- size_t delta_len;
-};
-
-#define GOT_DELTA_CACHE_SIZE 1024
-
-struct got_delta_cache {
- char *path_packfile;
- struct got_delta_cache_entry deltas[GOT_DELTA_CACHE_SIZE];
-};
-
#define GOT_PACKIDX_CACHE_SIZE 64
#define GOT_PACK_CACHE_SIZE GOT_PACKIDX_CACHE_SIZE
@@ -39,7 +26,4 @@ struct got_repository {
/* Open file handles, memory maps, and cached deltas for pack files. */
struct got_pack packs[GOT_PACK_CACHE_SIZE];
-
- /* XXX TODO move into packs[] */
- struct got_delta_cache delta_cache[GOT_DELTA_CACHE_SIZE];
};
diff --git a/lib/pack.c b/lib/pack.c
index 2554b19..cdb8863 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -580,6 +580,8 @@ 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)) {
struct got_pack_mapping *map = TAILQ_FIRST(&pack->mappings);
@@ -597,6 +599,19 @@ got_pack_close(struct got_pack *pack)
pack->path_packfile = NULL;
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;
+ entry->data_offset = 0;
+ free(entry->delta_buf);
+ entry->delta_buf = NULL;
+ entry->delta_len = 0;
+ }
+
return err;
}
@@ -1163,67 +1178,24 @@ add_delta_cache_entry(struct got_delta_cache *cache, off_t data_offset,
const struct got_error *
cache_delta(off_t data_offset, uint8_t *delta_buf, size_t delta_len,
- const char *path_packfile, struct got_repository *repo)
+ struct got_pack *pack)
{
- struct got_delta_cache *cache;
- int i;
-
- for (i = 0; i < nitems(repo->delta_cache); i++) {
- cache = &repo->delta_cache[i];
- if (cache->path_packfile == NULL)
- break;
- if (strcmp(cache->path_packfile, path_packfile) == 0)
- return add_delta_cache_entry(cache, data_offset,
- delta_buf, delta_len);
- }
-
- if (i == nitems(repo->delta_cache)) {
- int j;
- cache = &repo->delta_cache[i - 1];
- free(cache->path_packfile);
- cache->path_packfile = NULL;
- for (j = 0; j < nitems(cache->deltas); j++) {
- struct got_delta_cache_entry *entry = &cache->deltas[j];
- if (entry->data_offset == 0)
- break;
- clear_delta_cache_entry(entry);
- }
- memmove(&repo->delta_cache[1], &repo->delta_cache[0],
- sizeof(repo->delta_cache) - sizeof(repo->delta_cache[0]));
- i = 0;
- }
-
- cache = &repo->delta_cache[i];
- cache->path_packfile = strdup(path_packfile);
- if (cache->path_packfile == NULL)
- return got_error(GOT_ERR_NO_MEM);
- return add_delta_cache_entry(cache, data_offset, delta_buf, delta_len);
+ 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, const char *path_packfile, struct got_repository *repo)
+get_cached_delta(uint8_t **delta_buf, size_t *delta_len, off_t data_offset,
+ struct got_pack *pack)
{
- struct got_delta_cache *cache;
struct got_delta_cache_entry *entry;
int i;
*delta_buf = NULL;
*delta_len = 0;
- for (i = 0; i < nitems(repo->delta_cache); i++) {
- cache = &repo->delta_cache[i];
- if (cache->path_packfile == NULL)
- return;
- if (strcmp(cache->path_packfile, path_packfile) == 0)
- break;
- }
-
- if (i == nitems(repo->delta_cache))
- return;
-
- for (i = 0; i < nitems(cache->deltas); i++) {
- entry = &cache->deltas[i];
+ for (i = 0; i < nitems(pack->delta_cache.deltas); i++) {
+ entry = &pack->delta_cache.deltas[i];
if (entry->data_offset == 0)
break;
if (entry->data_offset == data_offset) {
@@ -1236,8 +1208,7 @@ get_cached_delta(uint8_t **delta_buf, size_t *delta_len,
static const struct got_error *
dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
- FILE *outfile, FILE *packfile, const char *path_packfile,
- struct got_repository *repo)
+ FILE *outfile, struct got_pack *pack, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_delta *delta;
@@ -1253,7 +1224,7 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
/* We process small enough files entirely in memory for speed. */
- err = get_delta_chain_max_size(&max_size, deltas, packfile);
+ err = get_delta_chain_max_size(&max_size, deltas, pack->packfile);
if (err)
return err;
if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
@@ -1290,17 +1261,17 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
goto done;
}
- if (fseeko(packfile, delta->offset + delta->tslen,
+ if (fseeko(pack->packfile, delta->offset + delta->tslen,
SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
if (base_file)
err = got_inflate_to_file(&base_len,
- packfile, base_file);
+ pack->packfile, base_file);
else {
err = got_inflate_to_mem(&base_buf, &base_len,
- packfile);
+ pack->packfile);
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
@@ -1320,9 +1291,9 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
}
get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
- path_packfile, repo);
+ pack);
if (delta_buf == NULL) {
- if (fseeko(packfile, delta->data_offset, SEEK_SET)
+ if (fseeko(pack->packfile, delta->data_offset, SEEK_SET)
!= 0) {
err = got_error_from_errno();
goto done;
@@ -1330,12 +1301,12 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
/* Delta streams should always fit in memory. */
err = got_inflate_to_mem(&delta_buf, &delta_len,
- packfile);
+ pack->packfile);
if (err)
goto done;
err = cache_delta(delta->data_offset, delta_buf,
- delta_len, path_packfile, repo);
+ delta_len, pack);
if (err)
goto done;
}
@@ -1390,7 +1361,7 @@ done:
static const struct got_error *
dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
- struct got_delta_chain *deltas, FILE *packfile, const char *path_packfile,
+ struct got_delta_chain *deltas, struct got_pack *pack,
struct got_repository *repo)
{
const struct got_error *err = NULL;
@@ -1406,7 +1377,7 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
if (SIMPLEQ_EMPTY(&deltas->entries))
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
- err = get_delta_chain_max_size(&max_size, deltas, packfile);
+ err = get_delta_chain_max_size(&max_size, deltas, pack->packfile);
if (err)
return err;
accum_buf = malloc(max_size);
@@ -1430,13 +1401,13 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
goto done;
}
- if (fseeko(packfile, delta->offset + delta->tslen,
+ if (fseeko(pack->packfile, delta->offset + delta->tslen,
SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_mem(&base_buf, &base_len,
- packfile);
+ pack->packfile);
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
@@ -1453,9 +1424,9 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
}
get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
- path_packfile, repo);
+ pack);
if (delta_buf == NULL) {
- if (fseeko(packfile, delta->data_offset, SEEK_SET)
+ if (fseeko(pack->packfile, delta->data_offset, SEEK_SET)
!= 0) {
err = got_error_from_errno();
goto done;
@@ -1463,12 +1434,12 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
/* Delta streams should always fit in memory. */
err = got_inflate_to_mem(&delta_buf, &delta_len,
- packfile);
+ pack->packfile);
if (err)
goto done;
err = cache_delta(delta->data_offset, delta_buf,
- delta_len, path_packfile, repo);
+ delta_len, pack);
if (err)
goto done;
}
@@ -1533,7 +1504,7 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
err = got_inflate_to_file(&obj->size, pack->packfile, *f);
} else
err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
- pack->packfile, obj->path_packfile, repo);
+ pack, repo);
done:
if (err && *f)
fclose(*f);
@@ -1566,8 +1537,8 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
err = got_inflate_to_mem(buf, len, pack->packfile);
} else
- err = dump_delta_chain_to_mem(buf, len, &obj->deltas,
- pack->packfile, obj->path_packfile, repo);
+ err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack,
+ repo);
done:
if (packfile)
fclose(packfile);
diff --git a/lib/repository.c b/lib/repository.c
index c8d7850..d1aca1b 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -214,26 +214,6 @@ got_repo_close(struct got_repository *repo)
got_pack_close(&repo->packs[i]);
}
- for (i = 0; i < nitems(repo->delta_cache); i++) {
- struct got_delta_cache *cache = &repo->delta_cache[i];
- int j;
-
- if (cache->path_packfile == NULL)
- break;
- free(cache->path_packfile);
- cache->path_packfile = NULL;
-
- for (j = 0; j < nitems(cache->deltas); j++) {
- struct got_delta_cache_entry *entry = &cache->deltas[j];
- if (entry->data_offset == 0)
- break;
- entry->data_offset = 0;
- free(entry->delta_buf);
- entry->delta_buf = NULL;
- entry->delta_len = 0;
- }
- }
-
free(repo->path);
free(repo->path_git_dir);
free(repo);