Blame


1 6bef87be 2018-09-11 stsp /*
2 6bef87be 2018-09-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 6bef87be 2018-09-11 stsp *
4 6bef87be 2018-09-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 6bef87be 2018-09-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 6bef87be 2018-09-11 stsp * copyright notice and this permission notice appear in all copies.
7 6bef87be 2018-09-11 stsp *
8 6bef87be 2018-09-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6bef87be 2018-09-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6bef87be 2018-09-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6bef87be 2018-09-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6bef87be 2018-09-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6bef87be 2018-09-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6bef87be 2018-09-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 6bef87be 2018-09-11 stsp */
16 6bef87be 2018-09-11 stsp
17 6bef87be 2018-09-11 stsp #include <sys/time.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 8ab9215c 2021-10-15 thomas #include <sys/resource.h>
20 6bef87be 2018-09-11 stsp
21 6bef87be 2018-09-11 stsp #include <stdio.h>
22 6bef87be 2018-09-11 stsp #include <stdlib.h>
23 a60c9e77 2019-05-22 stsp #include <stdint.h>
24 6bef87be 2018-09-11 stsp #include <string.h>
25 56e0773d 2019-11-28 stsp #include <limits.h>
26 588a8092 2023-02-23 thomas #include <sha1.h>
27 588a8092 2023-02-23 thomas #include <sha2.h>
28 6bef87be 2018-09-11 stsp #include <zlib.h>
29 6bef87be 2018-09-11 stsp
30 6bef87be 2018-09-11 stsp #include "got_error.h"
31 6bef87be 2018-09-11 stsp #include "got_object.h"
32 6bef87be 2018-09-11 stsp
33 6bef87be 2018-09-11 stsp #include "got_lib_delta.h"
34 6bef87be 2018-09-11 stsp #include "got_lib_inflate.h"
35 6bef87be 2018-09-11 stsp #include "got_lib_object.h"
36 f054b67a 2018-11-05 stsp #include "got_lib_object_idset.h"
37 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
38 6bef87be 2018-09-11 stsp
39 a60c9e77 2019-05-22 stsp /*
40 a60c9e77 2019-05-22 stsp * XXX This should be reworked to track cache size and usage in bytes,
41 a60c9e77 2019-05-22 stsp * rather than tracking N elements capped to a maximum element size.
42 a60c9e77 2019-05-22 stsp */
43 9bccfa63 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_OBJ 256
44 9185b863 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_TREE 256
45 9bccfa63 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
46 46387cfb 2022-12-01 thomas #define GOT_OBJECT_CACHE_SIZE_TAG 256
47 3efd8e31 2022-10-23 thomas #define GOT_OBJECT_CACHE_SIZE_RAW 16
48 a60c9e77 2019-05-22 stsp #define GOT_OBJECT_CACHE_MAX_ELEM_SIZE 1048576 /* 1 MB */
49 6bef87be 2018-09-11 stsp
50 6bef87be 2018-09-11 stsp const struct got_error *
51 6bef87be 2018-09-11 stsp got_object_cache_init(struct got_object_cache *cache,
52 6bef87be 2018-09-11 stsp enum got_object_cache_type type)
53 6bef87be 2018-09-11 stsp {
54 8ab9215c 2021-10-15 thomas struct rlimit rl;
55 8ab9215c 2021-10-15 thomas
56 dab9d9b6 2018-11-05 stsp memset(cache, 0, sizeof(*cache));
57 dab9d9b6 2018-11-05 stsp
58 f054b67a 2018-11-05 stsp cache->idset = got_object_idset_alloc();
59 f054b67a 2018-11-05 stsp if (cache->idset == NULL)
60 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
61 f054b67a 2018-11-05 stsp
62 f054b67a 2018-11-05 stsp cache->type = type;
63 6bef87be 2018-09-11 stsp switch (type) {
64 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
65 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
66 6bef87be 2018-09-11 stsp break;
67 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
68 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
69 6bef87be 2018-09-11 stsp break;
70 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
71 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
72 6bef87be 2018-09-11 stsp break;
73 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
74 f4a881ce 2018-11-17 stsp cache->size = GOT_OBJECT_CACHE_SIZE_TAG;
75 f4a881ce 2018-11-17 stsp break;
76 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
77 8ab9215c 2021-10-15 thomas if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
78 8ab9215c 2021-10-15 thomas return got_error_from_errno("getrlimit");
79 8ab9215c 2021-10-15 thomas cache->size = GOT_OBJECT_CACHE_SIZE_RAW;
80 8ab9215c 2021-10-15 thomas if (cache->size > rl.rlim_cur / 16)
81 8ab9215c 2021-10-15 thomas cache->size = rl.rlim_cur / 16;
82 8ab9215c 2021-10-15 thomas break;
83 6bef87be 2018-09-11 stsp }
84 6bef87be 2018-09-11 stsp return NULL;
85 a60c9e77 2019-05-22 stsp }
86 a60c9e77 2019-05-22 stsp
87 ef20f542 2022-06-26 thomas static size_t
88 a60c9e77 2019-05-22 stsp get_size_obj(struct got_object *obj)
89 a60c9e77 2019-05-22 stsp {
90 a60c9e77 2019-05-22 stsp size_t size = sizeof(*obj);
91 a60c9e77 2019-05-22 stsp struct got_delta *delta;
92 a60c9e77 2019-05-22 stsp
93 a60c9e77 2019-05-22 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
94 a60c9e77 2019-05-22 stsp return size;
95 a60c9e77 2019-05-22 stsp
96 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &obj->deltas.entries, entry) {
97 42c69117 2019-11-10 stsp if (SIZE_MAX - sizeof(*delta) < size)
98 a60c9e77 2019-05-22 stsp return SIZE_MAX;
99 42c69117 2019-11-10 stsp size += sizeof(*delta);
100 a60c9e77 2019-05-22 stsp }
101 a60c9e77 2019-05-22 stsp
102 a60c9e77 2019-05-22 stsp return size;
103 a60c9e77 2019-05-22 stsp }
104 a60c9e77 2019-05-22 stsp
105 ef20f542 2022-06-26 thomas static size_t
106 a60c9e77 2019-05-22 stsp get_size_tree(struct got_tree_object *tree)
107 a60c9e77 2019-05-22 stsp {
108 a60c9e77 2019-05-22 stsp size_t size = sizeof(*tree);
109 a60c9e77 2019-05-22 stsp
110 56e0773d 2019-11-28 stsp size += sizeof(struct got_tree_entry) * tree->nentries;
111 a60c9e77 2019-05-22 stsp return size;
112 a60c9e77 2019-05-22 stsp }
113 a60c9e77 2019-05-22 stsp
114 ef20f542 2022-06-26 thomas static size_t
115 a60c9e77 2019-05-22 stsp get_size_commit(struct got_commit_object *commit)
116 a60c9e77 2019-05-22 stsp {
117 a60c9e77 2019-05-22 stsp size_t size = sizeof(*commit);
118 a60c9e77 2019-05-22 stsp struct got_object_qid *qid;
119 a60c9e77 2019-05-22 stsp
120 a60c9e77 2019-05-22 stsp size += sizeof(*commit->tree_id);
121 a60c9e77 2019-05-22 stsp size += strlen(commit->author);
122 a60c9e77 2019-05-22 stsp size += strlen(commit->committer);
123 a60c9e77 2019-05-22 stsp size += strlen(commit->logmsg);
124 a60c9e77 2019-05-22 stsp
125 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry)
126 ec242592 2022-04-22 thomas size += sizeof(*qid) + sizeof(qid->id);
127 a60c9e77 2019-05-22 stsp
128 a60c9e77 2019-05-22 stsp return size;
129 6bef87be 2018-09-11 stsp }
130 6bef87be 2018-09-11 stsp
131 ef20f542 2022-06-26 thomas static size_t
132 a60c9e77 2019-05-22 stsp get_size_tag(struct got_tag_object *tag)
133 a60c9e77 2019-05-22 stsp {
134 a60c9e77 2019-05-22 stsp size_t size = sizeof(*tag);
135 a60c9e77 2019-05-22 stsp
136 a60c9e77 2019-05-22 stsp size += strlen(tag->tag);
137 a60c9e77 2019-05-22 stsp size += strlen(tag->tagger);
138 a60c9e77 2019-05-22 stsp size += strlen(tag->tagmsg);
139 a60c9e77 2019-05-22 stsp
140 a60c9e77 2019-05-22 stsp return size;
141 a60c9e77 2019-05-22 stsp }
142 a60c9e77 2019-05-22 stsp
143 ef20f542 2022-06-26 thomas static size_t
144 8ab9215c 2021-10-15 thomas get_size_raw(struct got_raw_object *raw)
145 8ab9215c 2021-10-15 thomas {
146 8ab9215c 2021-10-15 thomas return sizeof(*raw);
147 8ab9215c 2021-10-15 thomas }
148 8ab9215c 2021-10-15 thomas
149 6bef87be 2018-09-11 stsp const struct got_error *
150 48b4f239 2021-12-31 thomas got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id,
151 48b4f239 2021-12-31 thomas void *item)
152 6bef87be 2018-09-11 stsp {
153 6bef87be 2018-09-11 stsp const struct got_error *err = NULL;
154 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
155 6bef87be 2018-09-11 stsp int nelem;
156 a60c9e77 2019-05-22 stsp size_t size;
157 6bef87be 2018-09-11 stsp
158 a60c9e77 2019-05-22 stsp switch (cache->type) {
159 a60c9e77 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
160 a60c9e77 2019-05-22 stsp size = get_size_obj((struct got_object *)item);
161 a60c9e77 2019-05-22 stsp break;
162 a60c9e77 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
163 a60c9e77 2019-05-22 stsp size = get_size_tree((struct got_tree_object *)item);
164 a60c9e77 2019-05-22 stsp break;
165 a60c9e77 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
166 a60c9e77 2019-05-22 stsp size = get_size_commit((struct got_commit_object *)item);
167 a60c9e77 2019-05-22 stsp break;
168 a60c9e77 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
169 a60c9e77 2019-05-22 stsp size = get_size_tag((struct got_tag_object *)item);
170 a60c9e77 2019-05-22 stsp break;
171 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
172 8ab9215c 2021-10-15 thomas size = get_size_raw((struct got_raw_object *)item);
173 8ab9215c 2021-10-15 thomas break;
174 a0de39f3 2019-08-09 stsp default:
175 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_OBJ_TYPE);
176 a60c9e77 2019-05-22 stsp }
177 a60c9e77 2019-05-22 stsp
178 01cd7614 2019-05-22 stsp if (size > GOT_OBJECT_CACHE_MAX_ELEM_SIZE) {
179 01cd7614 2019-05-22 stsp #ifdef GOT_OBJ_CACHE_DEBUG
180 01cd7614 2019-05-22 stsp char *id_str;
181 01cd7614 2019-05-22 stsp if (got_object_id_str(&id_str, id) != NULL)
182 01cd7614 2019-05-22 stsp return got_error_from_errno("got_object_id_str");
183 01cd7614 2019-05-22 stsp fprintf(stderr, "%s: not caching ", getprogname());
184 01cd7614 2019-05-22 stsp switch (cache->type) {
185 01cd7614 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
186 01cd7614 2019-05-22 stsp fprintf(stderr, "object");
187 01cd7614 2019-05-22 stsp break;
188 01cd7614 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
189 01cd7614 2019-05-22 stsp fprintf(stderr, "tree");
190 01cd7614 2019-05-22 stsp break;
191 01cd7614 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
192 01cd7614 2019-05-22 stsp fprintf(stderr, "commit");
193 01cd7614 2019-05-22 stsp break;
194 01cd7614 2019-05-22 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
195 01cd7614 2019-05-22 stsp fprintf(stderr, "tag");
196 01cd7614 2019-05-22 stsp break;
197 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
198 8ab9215c 2021-10-15 thomas fprintf(stderr, "raw");
199 8ab9215c 2021-10-15 thomas break;
200 01cd7614 2019-05-22 stsp }
201 01cd7614 2019-05-22 stsp fprintf(stderr, " %s (%zd bytes; %zd MB)\n", id_str, size,
202 01cd7614 2019-05-22 stsp size/1024/1024);
203 01cd7614 2019-05-22 stsp free(id_str);
204 01cd7614 2019-05-22 stsp #endif
205 01cd7614 2019-05-22 stsp cache->cache_toolarge++;
206 79c99a64 2019-05-23 stsp return got_error(GOT_ERR_OBJ_TOO_LARGE);
207 01cd7614 2019-05-22 stsp }
208 a60c9e77 2019-05-22 stsp
209 f054b67a 2018-11-05 stsp nelem = got_object_idset_num_elements(cache->idset);
210 6bef87be 2018-09-11 stsp if (nelem >= cache->size) {
211 f054b67a 2018-11-05 stsp err = got_object_idset_remove((void **)&ce,
212 f054b67a 2018-11-05 stsp cache->idset, NULL);
213 6bef87be 2018-09-11 stsp if (err)
214 6bef87be 2018-09-11 stsp return err;
215 6bef87be 2018-09-11 stsp switch (cache->type) {
216 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
217 6bef87be 2018-09-11 stsp got_object_close(ce->data.obj);
218 6bef87be 2018-09-11 stsp break;
219 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
220 6bef87be 2018-09-11 stsp got_object_tree_close(ce->data.tree);
221 6bef87be 2018-09-11 stsp break;
222 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
223 6bef87be 2018-09-11 stsp got_object_commit_close(ce->data.commit);
224 6bef87be 2018-09-11 stsp break;
225 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
226 f4a881ce 2018-11-17 stsp got_object_tag_close(ce->data.tag);
227 f4a881ce 2018-11-17 stsp break;
228 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
229 8ab9215c 2021-10-15 thomas got_object_raw_close(ce->data.raw);
230 8ab9215c 2021-10-15 thomas break;
231 6bef87be 2018-09-11 stsp }
232 f1461b8c 2022-08-30 thomas memset(ce, 0, sizeof(*ce));
233 315fa2b2 2018-09-15 stsp cache->cache_evict++;
234 f1461b8c 2022-08-30 thomas } else {
235 f1461b8c 2022-08-30 thomas ce = malloc(sizeof(*ce));
236 f1461b8c 2022-08-30 thomas if (ce == NULL)
237 f1461b8c 2022-08-30 thomas return got_error_from_errno("malloc");
238 6bef87be 2018-09-11 stsp }
239 6bef87be 2018-09-11 stsp
240 6bef87be 2018-09-11 stsp memcpy(&ce->id, id, sizeof(ce->id));
241 6bef87be 2018-09-11 stsp switch (cache->type) {
242 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
243 6bef87be 2018-09-11 stsp ce->data.obj = (struct got_object *)item;
244 6bef87be 2018-09-11 stsp break;
245 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
246 6bef87be 2018-09-11 stsp ce->data.tree = (struct got_tree_object *)item;
247 6bef87be 2018-09-11 stsp break;
248 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
249 6bef87be 2018-09-11 stsp ce->data.commit = (struct got_commit_object *)item;
250 6bef87be 2018-09-11 stsp break;
251 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
252 f4a881ce 2018-11-17 stsp ce->data.tag = (struct got_tag_object *)item;
253 f4a881ce 2018-11-17 stsp break;
254 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
255 8ab9215c 2021-10-15 thomas ce->data.raw = (struct got_raw_object *)item;
256 8ab9215c 2021-10-15 thomas break;
257 6bef87be 2018-09-11 stsp }
258 6bef87be 2018-09-11 stsp
259 f054b67a 2018-11-05 stsp err = got_object_idset_add(cache->idset, id, ce);
260 79c99a64 2019-05-23 stsp if (err)
261 79c99a64 2019-05-23 stsp free(ce);
262 cadc72f0 2022-08-30 thomas else if (size > cache->max_cached_size)
263 cadc72f0 2022-08-30 thomas cache->max_cached_size = size;
264 6bef87be 2018-09-11 stsp return err;
265 6bef87be 2018-09-11 stsp }
266 6bef87be 2018-09-11 stsp
267 6bef87be 2018-09-11 stsp void *
268 6bef87be 2018-09-11 stsp got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
269 6bef87be 2018-09-11 stsp {
270 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
271 6bef87be 2018-09-11 stsp
272 221e79cd 2018-09-16 stsp cache->cache_searches++;
273 f054b67a 2018-11-05 stsp ce = got_object_idset_get(cache->idset, id);
274 6bef87be 2018-09-11 stsp if (ce) {
275 6bef87be 2018-09-11 stsp cache->cache_hit++;
276 6bef87be 2018-09-11 stsp switch (cache->type) {
277 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
278 6bef87be 2018-09-11 stsp return ce->data.obj;
279 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
280 6bef87be 2018-09-11 stsp return ce->data.tree;
281 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
282 6bef87be 2018-09-11 stsp return ce->data.commit;
283 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
284 f4a881ce 2018-11-17 stsp return ce->data.tag;
285 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
286 8ab9215c 2021-10-15 thomas return ce->data.raw;
287 6bef87be 2018-09-11 stsp }
288 6bef87be 2018-09-11 stsp }
289 6bef87be 2018-09-11 stsp
290 6bef87be 2018-09-11 stsp cache->cache_miss++;
291 6bef87be 2018-09-11 stsp return NULL;
292 6bef87be 2018-09-11 stsp }
293 6bef87be 2018-09-11 stsp
294 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
295 6bef87be 2018-09-11 stsp static void
296 6bef87be 2018-09-11 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
297 6bef87be 2018-09-11 stsp {
298 221e79cd 2018-09-16 stsp fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
299 cadc72f0 2022-08-30 thomas "%d missed, %d evicted, %d too large, max cached %zd bytes\n",
300 cadc72f0 2022-08-30 thomas getprogname(), name,
301 ce971618 2022-08-31 thomas cache->idset ? got_object_idset_num_elements(cache->idset) : -1,
302 221e79cd 2018-09-16 stsp cache->cache_searches, cache->cache_hit,
303 cadc72f0 2022-08-30 thomas cache->cache_miss, cache->cache_evict, cache->cache_toolarge,
304 cadc72f0 2022-08-30 thomas cache->max_cached_size);
305 6bef87be 2018-09-11 stsp }
306 6bef87be 2018-09-11 stsp
307 30b23f54 2022-08-30 thomas static const struct got_error *
308 cb103d04 2018-11-07 stsp check_refcount(struct got_object_id *id, void *data, void *arg)
309 6bef87be 2018-09-11 stsp {
310 6bef87be 2018-09-11 stsp struct got_object_cache *cache = arg;
311 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce = data;
312 6bef87be 2018-09-11 stsp struct got_object *obj;
313 6bef87be 2018-09-11 stsp struct got_tree_object *tree;
314 6bef87be 2018-09-11 stsp struct got_commit_object *commit;
315 f4a881ce 2018-11-17 stsp struct got_tag_object *tag;
316 8ab9215c 2021-10-15 thomas struct got_raw_object *raw;
317 6bef87be 2018-09-11 stsp char *id_str;
318 6bef87be 2018-09-11 stsp
319 6bef87be 2018-09-11 stsp if (got_object_id_str(&id_str, id) != NULL)
320 cb103d04 2018-11-07 stsp return NULL;
321 6bef87be 2018-09-11 stsp
322 6bef87be 2018-09-11 stsp switch (cache->type) {
323 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
324 6bef87be 2018-09-11 stsp obj = ce->data.obj;
325 6bef87be 2018-09-11 stsp if (obj->refcnt == 1)
326 6bef87be 2018-09-11 stsp break;
327 6bef87be 2018-09-11 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
328 6bef87be 2018-09-11 stsp id_str, obj->refcnt - 1);
329 6bef87be 2018-09-11 stsp break;
330 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
331 6bef87be 2018-09-11 stsp tree = ce->data.tree;
332 6bef87be 2018-09-11 stsp if (tree->refcnt == 1)
333 6bef87be 2018-09-11 stsp break;
334 6bef87be 2018-09-11 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
335 6bef87be 2018-09-11 stsp id_str, tree->refcnt - 1);
336 6bef87be 2018-09-11 stsp break;
337 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
338 6bef87be 2018-09-11 stsp commit = ce->data.commit;
339 6bef87be 2018-09-11 stsp if (commit->refcnt == 1)
340 6bef87be 2018-09-11 stsp break;
341 6bef87be 2018-09-11 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
342 414611d9 2018-09-19 stsp id_str, commit->refcnt - 1);
343 6bef87be 2018-09-11 stsp break;
344 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
345 f4a881ce 2018-11-17 stsp tag = ce->data.tag;
346 f4a881ce 2018-11-17 stsp if (tag->refcnt == 1)
347 f4a881ce 2018-11-17 stsp break;
348 f4a881ce 2018-11-17 stsp fprintf(stderr, "tag %s has %d unclaimed references\n",
349 f4a881ce 2018-11-17 stsp id_str, tag->refcnt - 1);
350 f4a881ce 2018-11-17 stsp break;
351 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
352 8ab9215c 2021-10-15 thomas raw = ce->data.raw;
353 8ab9215c 2021-10-15 thomas if (raw->refcnt == 1)
354 8ab9215c 2021-10-15 thomas break;
355 8ab9215c 2021-10-15 thomas fprintf(stderr, "raw %s has %d unclaimed references\n",
356 8ab9215c 2021-10-15 thomas id_str, raw->refcnt - 1);
357 8ab9215c 2021-10-15 thomas break;
358 6bef87be 2018-09-11 stsp }
359 6bef87be 2018-09-11 stsp free(id_str);
360 cb103d04 2018-11-07 stsp return NULL;
361 6bef87be 2018-09-11 stsp }
362 6bef87be 2018-09-11 stsp #endif
363 e476ee5e 2022-09-05 thomas
364 e476ee5e 2022-09-05 thomas static const struct got_error *
365 e476ee5e 2022-09-05 thomas free_entry(struct got_object_id *id, void *data, void *arg)
366 e476ee5e 2022-09-05 thomas {
367 e476ee5e 2022-09-05 thomas struct got_object_cache *cache = arg;
368 e476ee5e 2022-09-05 thomas struct got_object_cache_entry *ce = data;
369 e476ee5e 2022-09-05 thomas
370 e476ee5e 2022-09-05 thomas switch (cache->type) {
371 e476ee5e 2022-09-05 thomas case GOT_OBJECT_CACHE_TYPE_OBJ:
372 e476ee5e 2022-09-05 thomas got_object_close(ce->data.obj);
373 e476ee5e 2022-09-05 thomas break;
374 e476ee5e 2022-09-05 thomas case GOT_OBJECT_CACHE_TYPE_TREE:
375 e476ee5e 2022-09-05 thomas got_object_tree_close(ce->data.tree);
376 e476ee5e 2022-09-05 thomas break;
377 e476ee5e 2022-09-05 thomas case GOT_OBJECT_CACHE_TYPE_COMMIT:
378 e476ee5e 2022-09-05 thomas got_object_commit_close(ce->data.commit);
379 e476ee5e 2022-09-05 thomas break;
380 e476ee5e 2022-09-05 thomas case GOT_OBJECT_CACHE_TYPE_TAG:
381 e476ee5e 2022-09-05 thomas got_object_tag_close(ce->data.tag);
382 e476ee5e 2022-09-05 thomas break;
383 e476ee5e 2022-09-05 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
384 e476ee5e 2022-09-05 thomas got_object_raw_close(ce->data.raw);
385 e476ee5e 2022-09-05 thomas break;
386 e476ee5e 2022-09-05 thomas }
387 6bef87be 2018-09-11 stsp
388 e476ee5e 2022-09-05 thomas free(ce);
389 e476ee5e 2022-09-05 thomas
390 e476ee5e 2022-09-05 thomas return NULL;
391 e476ee5e 2022-09-05 thomas }
392 e476ee5e 2022-09-05 thomas
393 6bef87be 2018-09-11 stsp void
394 6bef87be 2018-09-11 stsp got_object_cache_close(struct got_object_cache *cache)
395 6bef87be 2018-09-11 stsp {
396 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
397 6bef87be 2018-09-11 stsp switch (cache->type) {
398 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
399 6bef87be 2018-09-11 stsp print_cache_stats(cache, "object");
400 6bef87be 2018-09-11 stsp break;
401 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
402 6bef87be 2018-09-11 stsp print_cache_stats(cache, "tree");
403 6bef87be 2018-09-11 stsp break;
404 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
405 6bef87be 2018-09-11 stsp print_cache_stats(cache, "commit");
406 6bef87be 2018-09-11 stsp break;
407 2ee32b23 2019-05-15 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
408 2ee32b23 2019-05-15 stsp print_cache_stats(cache, "tag");
409 2ee32b23 2019-05-15 stsp break;
410 8ab9215c 2021-10-15 thomas case GOT_OBJECT_CACHE_TYPE_RAW:
411 8ab9215c 2021-10-15 thomas print_cache_stats(cache, "raw");
412 8ab9215c 2021-10-15 thomas break;
413 6bef87be 2018-09-11 stsp }
414 6bef87be 2018-09-11 stsp
415 ce971618 2022-08-31 thomas if (cache->idset)
416 ce971618 2022-08-31 thomas got_object_idset_for_each(cache->idset, check_refcount, cache);
417 6bef87be 2018-09-11 stsp #endif
418 6bef87be 2018-09-11 stsp
419 f054b67a 2018-11-05 stsp if (cache->idset) {
420 e476ee5e 2022-09-05 thomas got_object_idset_for_each(cache->idset, free_entry, cache);
421 f054b67a 2018-11-05 stsp got_object_idset_free(cache->idset);
422 f054b67a 2018-11-05 stsp cache->idset = NULL;
423 6bef87be 2018-09-11 stsp }
424 6bef87be 2018-09-11 stsp cache->size = 0;
425 6bef87be 2018-09-11 stsp }