Commit 6bef87be901a62ffff4a5e52932fdab9c0d8cb27

Stefan Sperling 2018-09-11T11:23:44

move object cache out of repository.c to its own file

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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
diff --git a/got/Makefile b/got/Makefile
index e8932c8..8ebd171 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -2,9 +2,10 @@
 
 PROG=		got
 SRCS=		got.c blame.c commit_graph.c delta.c diff.c diffoffset.c \
-		diffreg.c error.c fileindex.c object.c object_idcache.c \
-		object_idset.c object_parse.c opentemp.c path.c pack.c \
-		privsep.c reference.c repository.c sha1.c worktree.c inflate.c
+		diffreg.c error.c fileindex.c object.c object_cache.c \
+		object_idcache.c object_idset.c object_parse.c opentemp.c \
+		path.c pack.c privsep.c reference.c repository.c sha1.c \
+		worktree.c inflate.c
 
 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib \
 	-DGOT_LIBEXECDIR=${GOT_LIBEXECDIR}
diff --git a/lib/got_lib_object_cache.h b/lib/got_lib_object_cache.h
new file mode 100644
index 0000000..871bc3c
--- /dev/null
+++ b/lib/got_lib_object_cache.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+enum got_object_cache_type {
+	GOT_OBJECT_CACHE_TYPE_OBJ,
+	GOT_OBJECT_CACHE_TYPE_TREE,
+	GOT_OBJECT_CACHE_TYPE_COMMIT,
+};
+
+struct got_object_cache_entry {
+	struct got_object_id id;
+	union {
+		struct got_object *obj;
+		struct got_tree_object *tree;
+		struct got_commit_object *commit;
+	} data;
+};
+
+struct got_object_cache {
+	enum got_object_cache_type type;
+	struct got_object_idcache *idcache;
+	size_t size;
+	int cache_hit;
+	int cache_miss;
+};
+
+const struct got_error *got_object_cache_init(struct got_object_cache *,
+    enum got_object_cache_type);
+const struct got_error *got_object_cache_add(struct got_object_cache *,
+    struct got_object_id *, void *);
+void *got_object_cache_get(struct got_object_cache *, struct got_object_id *);
+void got_object_cache_close(struct got_object_cache *);
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index d663456..4d2e819 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -17,33 +17,6 @@
 #define GOT_PACKIDX_CACHE_SIZE	64
 #define GOT_PACK_CACHE_SIZE	GOT_PACKIDX_CACHE_SIZE
 
-#define GOT_OBJECT_CACHE_SIZE_OBJ	1024
-#define GOT_OBJECT_CACHE_SIZE_TREE	128
-#define GOT_OBJECT_CACHE_SIZE_COMMIT	512
-
-enum got_object_chache_type {
-	GOT_OBJECT_CACHE_TYPE_OBJ,
-	GOT_OBJECT_CACHE_TYPE_TREE,
-	GOT_OBJECT_CACHE_TYPE_COMMIT,
-};
-
-struct got_object_cache_entry {
-	struct got_object_id id;
-	union {
-		struct got_object *obj;
-		struct got_tree_object *tree;
-		struct got_commit_object *commit;
-	} data;
-};
-
-struct got_object_cache {
-	enum got_object_chache_type type;
-	struct got_object_idcache *idcache;
-	size_t size;
-	int cache_hit;
-	int cache_miss;
-};
-
 struct got_repository {
 	char *path;
 	char *path_git_dir;
diff --git a/lib/object.c b/lib/object.c
index da1f8b5..70d904c 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -47,6 +47,8 @@
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
 #include "got_lib_privsep.h"
+#include "got_lib_object_idcache.h"
+#include "got_lib_object_cache.h"
 #include "got_lib_object_parse.h"
 #include "got_lib_repository.h"
 
diff --git a/lib/object_cache.c b/lib/object_cache.c
new file mode 100644
index 0000000..086dccc
--- /dev/null
+++ b/lib/object_cache.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/time.h>
+#include <sys/queue.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sha1.h>
+#include <zlib.h>
+
+#include "got_error.h"
+#include "got_object.h"
+
+#include "got_lib_delta.h"
+#include "got_lib_inflate.h"
+#include "got_lib_object.h"
+#include "got_lib_object_idcache.h"
+#include "got_lib_object_cache.h"
+
+#define GOT_OBJECT_CACHE_SIZE_OBJ	1024
+#define GOT_OBJECT_CACHE_SIZE_TREE	128
+#define GOT_OBJECT_CACHE_SIZE_COMMIT	512
+
+const struct got_error *
+got_object_cache_init(struct got_object_cache *cache,
+    enum got_object_cache_type type)
+{
+	size_t size;
+
+	switch (type) {
+	case GOT_OBJECT_CACHE_TYPE_OBJ:
+		size = GOT_OBJECT_CACHE_SIZE_OBJ;
+		break;
+	case GOT_OBJECT_CACHE_TYPE_TREE:
+		size = GOT_OBJECT_CACHE_SIZE_TREE;
+		break;
+	case GOT_OBJECT_CACHE_TYPE_COMMIT:
+		size = GOT_OBJECT_CACHE_SIZE_COMMIT;
+		break;
+	}
+
+	cache->idcache = got_object_idcache_alloc(size);
+	if (cache->idcache == NULL)
+		return got_error_from_errno();
+	cache->type = type;
+	cache->size = size;
+	return NULL;
+}
+
+const struct got_error *
+got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
+{
+	const struct got_error *err = NULL;
+	struct got_object_cache_entry *ce;
+	int nelem;
+
+	nelem = got_object_idcache_num_elements(cache->idcache);
+	if (nelem >= cache->size) {
+		err = got_object_idcache_remove_least_used((void **)&ce,
+		    cache->idcache);
+		if (err)
+			return err;
+		switch (cache->type) {
+		case GOT_OBJECT_CACHE_TYPE_OBJ:
+			got_object_close(ce->data.obj);
+			break;
+		case GOT_OBJECT_CACHE_TYPE_TREE:
+			got_object_tree_close(ce->data.tree);
+			break;
+		case GOT_OBJECT_CACHE_TYPE_COMMIT:
+			got_object_commit_close(ce->data.commit);
+			break;
+		}
+		free(ce);
+	}
+
+	ce = calloc(1, sizeof(*ce));
+	if (ce == NULL)
+		return got_error_from_errno();
+	memcpy(&ce->id, id, sizeof(ce->id));
+	switch (cache->type) {
+	case GOT_OBJECT_CACHE_TYPE_OBJ:
+		ce->data.obj = (struct got_object *)item;
+		break;
+	case GOT_OBJECT_CACHE_TYPE_TREE:
+		ce->data.tree = (struct got_tree_object *)item;
+		break;
+	case GOT_OBJECT_CACHE_TYPE_COMMIT:
+		ce->data.commit = (struct got_commit_object *)item;
+		break;
+	}
+
+	err = got_object_idcache_add(cache->idcache, id, ce);
+	if (err) {
+		if (err->code == GOT_ERR_OBJ_EXISTS) {
+			free(ce);
+			err = NULL;
+		}
+	}
+	return err;
+}
+
+void *
+got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
+{
+	struct got_object_cache_entry *ce;
+
+	ce = got_object_idcache_get(cache->idcache, id);
+	if (ce) {
+		cache->cache_hit++;
+		switch (cache->type) {
+		case GOT_OBJECT_CACHE_TYPE_OBJ:
+			return ce->data.obj;
+		case GOT_OBJECT_CACHE_TYPE_TREE:
+			return ce->data.tree;
+		case GOT_OBJECT_CACHE_TYPE_COMMIT:
+			return ce->data.commit;
+		}
+	}
+
+	cache->cache_miss++;
+	return NULL;
+}
+
+#if 0
+static void
+print_cache_stats(struct got_object_cache *cache, const char *name)
+{
+	fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
+	    name, got_object_idcache_num_elements(cache->idcache),
+	    cache->cache_hit, cache->cache_miss);
+}
+
+void check_refcount(struct got_object_id *id, void *data, void *arg)
+{
+	struct got_object_cache *cache = arg;
+	struct got_object_cache_entry *ce = data;
+	struct got_object *obj;
+	struct got_tree_object *tree;
+	struct got_commit_object *commit;
+	char *id_str;
+
+	if (got_object_id_str(&id_str, id) != NULL)
+		return;
+
+	switch (cache->type) {
+	case GOT_OBJECT_CACHE_TYPE_OBJ:
+		obj = ce->data.obj;
+		if (obj->refcnt == 1)
+			break;
+		fprintf(stderr, "object %s has %d unclaimed references\n",
+		    id_str, obj->refcnt - 1);
+		break;
+	case GOT_OBJECT_CACHE_TYPE_TREE:
+		tree = ce->data.tree;
+		if (tree->refcnt == 1)
+			break;
+		fprintf(stderr, "tree %s has %d unclaimed references\n",
+		    id_str, tree->refcnt - 1);
+		break;
+	case GOT_OBJECT_CACHE_TYPE_COMMIT:
+		commit = ce->data.commit;
+		if (commit->refcnt == 1)
+			break;
+		fprintf(stderr, "commit %s has %d unclaimed references\n",
+		    id_str, commit->refcnt);
+		break;
+	}
+	free(id_str);
+}
+#endif
+
+void
+got_object_cache_close(struct got_object_cache *cache)
+{
+#if 0
+	switch (cache->type) {
+	case GOT_OBJECT_CACHE_TYPE_OBJ:
+		print_cache_stats(cache, "object");
+		break;
+	case GOT_OBJECT_CACHE_TYPE_TREE:
+		print_cache_stats(cache, "tree");
+		break;
+	case GOT_OBJECT_CACHE_TYPE_COMMIT:
+		print_cache_stats(cache, "commit");
+		break;
+	}
+
+	got_object_idcache_for_each(cache->idcache, check_refcount, cache);
+#endif
+
+	if (cache->idcache) {
+		got_object_idcache_free(cache->idcache);
+		cache->idcache = NULL;
+	}
+	cache->size = 0;
+}
diff --git a/lib/object_parse.c b/lib/object_parse.c
index fc364fd..0918775 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -46,6 +46,7 @@
 #include "got_lib_pack.h"
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
+#include "got_lib_object_cache.h"
 #include "got_lib_repository.h"
 
 #ifndef nitems
diff --git a/lib/repository.c b/lib/repository.c
index f0fb855..682e940 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -46,9 +46,9 @@
 #include "got_lib_object.h"
 #include "got_lib_pack.h"
 #include "got_lib_privsep.h"
-#include "got_lib_repository.h"
 #include "got_lib_worktree.h"
-#include "got_lib_object_idcache.h"
+#include "got_lib_object_cache.h"
+#include "got_lib_repository.h"
 
 #ifndef nitems
 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
@@ -166,68 +166,13 @@ done:
 
 }
 
-#ifndef GOT_NO_OBJ_CACHE
-static const struct got_error *
-cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
-{
-	const struct got_error *err = NULL;
-	struct got_object_cache_entry *ce;
-	int nelem;
-
-	nelem = got_object_idcache_num_elements(cache->idcache);
-	if (nelem >= cache->size) {
-		err = got_object_idcache_remove_least_used((void **)&ce,
-		    cache->idcache);
-		if (err)
-			return err;
-		switch (cache->type) {
-		case GOT_OBJECT_CACHE_TYPE_OBJ:
-			got_object_close(ce->data.obj);
-			break;
-		case GOT_OBJECT_CACHE_TYPE_TREE:
-			got_object_tree_close(ce->data.tree);
-			break;
-		case GOT_OBJECT_CACHE_TYPE_COMMIT:
-			got_object_commit_close(ce->data.commit);
-			break;
-		}
-		free(ce);
-	}
-
-	ce = calloc(1, sizeof(*ce));
-	if (ce == NULL)
-		return got_error_from_errno();
-	memcpy(&ce->id, id, sizeof(ce->id));
-	switch (cache->type) {
-	case GOT_OBJECT_CACHE_TYPE_OBJ:
-		ce->data.obj = (struct got_object *)item;
-		break;
-	case GOT_OBJECT_CACHE_TYPE_TREE:
-		ce->data.tree = (struct got_tree_object *)item;
-		break;
-	case GOT_OBJECT_CACHE_TYPE_COMMIT:
-		ce->data.commit = (struct got_commit_object *)item;
-		break;
-	}
-
-	err = got_object_idcache_add(cache->idcache, id, ce);
-	if (err) {
-		if (err->code == GOT_ERR_OBJ_EXISTS) {
-			free(ce);
-			err = NULL;
-		}
-	}
-	return err;
-}
-#endif
-
 const struct got_error *
 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
     struct got_object *obj)
 {
 #ifndef GOT_NO_OBJ_CACHE
 	const struct got_error *err = NULL;
-	err = cache_add(&repo->objcache, id, obj);
+	err = got_object_cache_add(&repo->objcache, id, obj);
 	if (err)
 		return err;
 	obj->refcnt++;
@@ -239,16 +184,7 @@ struct got_object *
 got_repo_get_cached_object(struct got_repository *repo,
     struct got_object_id *id)
 {
-	struct got_object_cache_entry *ce;
-
-	ce = got_object_idcache_get(repo->objcache.idcache, id);
-	if (ce) {
-		repo->objcache.cache_hit++;
-		return ce->data.obj;
-	}
-
-	repo->objcache.cache_miss++;
-	return NULL;
+	return (struct got_object *)got_object_cache_get(&repo->objcache, id);
 }
 
 const struct got_error *
@@ -257,7 +193,7 @@ got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
 {
 #ifndef GOT_NO_OBJ_CACHE
 	const struct got_error *err = NULL;
-	err = cache_add(&repo->treecache, id, tree);
+	err = got_object_cache_add(&repo->treecache, id, tree);
 	if (err)
 		return err;
 	tree->refcnt++;
@@ -269,16 +205,8 @@ struct got_tree_object *
 got_repo_get_cached_tree(struct got_repository *repo,
     struct got_object_id *id)
 {
-	struct got_object_cache_entry *ce;
-
-	ce = got_object_idcache_get(repo->treecache.idcache, id);
-	if (ce) {
-		repo->treecache.cache_hit++;
-		return ce->data.tree;
-	}
-
-	repo->treecache.cache_miss++;
-	return NULL;
+	return (struct got_tree_object *)got_object_cache_get(
+	    &repo->treecache, id);
 }
 
 const struct got_error *
@@ -287,7 +215,7 @@ got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
 {
 #ifndef GOT_NO_OBJ_CACHE
 	const struct got_error *err = NULL;
-	err = cache_add(&repo->commitcache, id, commit);
+	err = got_object_cache_add(&repo->commitcache, id, commit);
 	if (err)
 		return err;
 
@@ -300,16 +228,8 @@ struct got_commit_object *
 got_repo_get_cached_commit(struct got_repository *repo,
     struct got_object_id *id)
 {
-	struct got_object_cache_entry *ce;
-
-	ce = got_object_idcache_get(repo->commitcache.idcache, id);
-	if (ce) {
-		repo->commitcache.cache_hit++;
-		return ce->data.commit;
-	}
-
-	repo->commitcache.cache_miss++;
-	return NULL;
+	return (struct got_commit_object *)got_object_cache_get(
+	    &repo->commitcache, id);
 }
 
 const struct got_error *
@@ -423,31 +343,18 @@ got_repo_open(struct got_repository **repop, const char *path)
 		repo->privsep_children[i].imsg_fd = -1;
 	}
 
-	repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
-	repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
-	repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
-	if (repo->objcache.idcache == NULL) {
-		err = got_error_from_errno();
+	err = got_object_cache_init(&repo->objcache,
+	    GOT_OBJECT_CACHE_TYPE_OBJ);
+	if (err)
 		goto done;
-	}
-
-	repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
-	repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
-	repo->treecache.idcache =
-	    got_object_idcache_alloc(repo->treecache.size);
-	if (repo->treecache.idcache == NULL) {
-		err = got_error_from_errno();
+	err = got_object_cache_init(&repo->treecache,
+	    GOT_OBJECT_CACHE_TYPE_TREE);
+	if (err)
 		goto done;
-	}
-
-	repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
-	repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
-	repo->commitcache.idcache =
-	    got_object_idcache_alloc(repo->commitcache.size);
-	if (repo->commitcache.idcache == NULL) {
-		err = got_error_from_errno();
+	err = got_object_cache_init(&repo->commitcache,
+	    GOT_OBJECT_CACHE_TYPE_COMMIT);
+	if (err)
 		goto done;
-	}
 
 	normpath = got_path_normalize(abspath);
 	if (normpath == NULL) {
@@ -483,54 +390,6 @@ done:
 	return err;
 }
 
-#if 0
-static void
-print_cache_stats(struct got_object_cache *cache, const char *name)
-{
-	fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
-	    name, got_object_idcache_num_elements(cache->idcache),
-	    cache->cache_hit, cache->cache_miss);
-}
-
-void check_refcount(struct got_object_id *id, void *data, void *arg)
-{
-	struct got_object_cache *cache = arg;
-	struct got_object_cache_entry *ce = data;
-	struct got_object *obj;
-	struct got_tree_object *tree;
-	struct got_commit_object *commit;
-	char *id_str;
-
-	if (got_object_id_str(&id_str, id) != NULL)
-		return;
-
-	switch (cache->type) {
-	case GOT_OBJECT_CACHE_TYPE_OBJ:
-		obj = ce->data.obj;
-		if (obj->refcnt == 1)
-			break;
-		fprintf(stderr, "object %s has %d unclaimed references\n",
-		    id_str, obj->refcnt - 1);
-		break;
-	case GOT_OBJECT_CACHE_TYPE_TREE:
-		tree = ce->data.tree;
-		if (tree->refcnt == 1)
-			break;
-		fprintf(stderr, "tree %s has %d unclaimed references\n",
-		    id_str, tree->refcnt - 1);
-		break;
-	case GOT_OBJECT_CACHE_TYPE_COMMIT:
-		commit = ce->data.commit;
-		if (commit->refcnt == 1)
-			break;
-		fprintf(stderr, "commit %s has %d unclaimed references\n",
-		    id_str, commit->refcnt);
-		break;
-	}
-	free(id_str);
-}
-#endif
-
 const struct got_error *
 got_repo_close(struct got_repository *repo)
 {
@@ -552,24 +411,9 @@ got_repo_close(struct got_repository *repo)
 	free(repo->path);
 	free(repo->path_git_dir);
 
-#if 0
-	print_cache_stats(&repo->objcache, "object");
-	print_cache_stats(&repo->treecache, "tree");
-	print_cache_stats(&repo->commitcache, "commit");
-	got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
-	    &repo->objcache);
-	got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
-	    &repo->treecache);
-	got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
-	    &repo->commitcache);
-#endif
-
-	if (repo->objcache.idcache)
-		got_object_idcache_free(repo->objcache.idcache);
-	if (repo->treecache.idcache)
-		got_object_idcache_free(repo->treecache.idcache);
-	if (repo->commitcache.idcache)
-		got_object_idcache_free(repo->commitcache.idcache);
+	got_object_cache_close(&repo->objcache);
+	got_object_cache_close(&repo->treecache);
+	got_object_cache_close(&repo->commitcache);
 
 	for (i = 0; i < nitems(repo->privsep_children); i++) {
 		if (repo->privsep_children[i].imsg_fd == -1)
diff --git a/regress/idset/Makefile b/regress/idset/Makefile
index 2b4ff87..f2a6a6e 100644
--- a/regress/idset/Makefile
+++ b/regress/idset/Makefile
@@ -2,7 +2,7 @@
 
 PROG = idset_test
 SRCS = error.c object.c privsep.c sha1.c pack.c inflate.c path.c opentemp.c \
-	delta.c repository.c reference.c worktree.c fileindex.c \
+	delta.c repository.c reference.c worktree.c fileindex.c object_cache.c \
 	object_idcache.c object_idset.c object_parse.c idset_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index babe389..a1659fd 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -1,10 +1,10 @@
 .PATH:${.CURDIR}/../../lib
 
 PROG = repository_test
-SRCS = path.c repository.c error.c reference.c object.c object_idcache.c \
-	object_idset.c object_parse.c opentemp.c sha1.c diff.c diffreg.c \
-	pack.c privsep.c delta.c fileindex.c worktree.c inflate.c \
-	repository_test.c
+SRCS = path.c repository.c error.c reference.c object.c object_cache.c \
+	object_idcache.c object_idset.c object_parse.c opentemp.c sha1.c \
+	diff.c diffreg.c pack.c privsep.c delta.c fileindex.c worktree.c \
+	inflate.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib \
 	-DGOT_LIBEXECDIR=${GOT_LIBEXECDIR}
diff --git a/regress/worktree/Makefile b/regress/worktree/Makefile
index c4b341e..325c30c 100644
--- a/regress/worktree/Makefile
+++ b/regress/worktree/Makefile
@@ -1,9 +1,9 @@
 .PATH:${.CURDIR}/../../lib
 
 PROG = worktree_test
-SRCS = worktree.c repository.c object.c object_idcache.c object_idset.c \
-	object_parse.c opentemp.c path.c error.c reference.c sha1.c pack.c \
-	privsep.c delta.c inflate.c fileindex.c worktree_test.c
+SRCS = worktree.c repository.c object.c object_cache.c object_idcache.c \
+	object_idset.c object_parse.c opentemp.c path.c error.c reference.c \
+	sha1.c pack.c privsep.c delta.c inflate.c fileindex.c worktree_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib \
 	-DGOT_LIBEXECDIR=${GOT_LIBEXECDIR}
diff --git a/tog/Makefile b/tog/Makefile
index ae6ee69..8b38788 100644
--- a/tog/Makefile
+++ b/tog/Makefile
@@ -2,10 +2,10 @@
 
 PROG=		tog
 SRCS=		tog.c blame.c commit_graph.c delta.c diff.c diffoffset.c \
-		diffreg.c error.c fileindex.c object.c object_idcache.c \
-		object_idset.c object_parse.c opentemp.c path.c pack.c \
-		privsep.c reference.c repository.c sha1.c worktree.c \
-		utf8.c inflate.c
+		diffreg.c error.c fileindex.c object.c object_cache.c \
+		object_idcache.c object_idset.c object_parse.c opentemp.c \
+		path.c pack.c privsep.c reference.c repository.c sha1.c \
+		worktree.c utf8.c inflate.c
 
 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib \
 	-DGOT_LIBEXECDIR=${GOT_LIBEXECDIR}