deps/libgit2/src/util/sortedcache.c

filter: Statementstmt 47.7% 86 / 180 Branchbran 32.9% 29 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 42.9% 115 / 268 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/idxmap.c ↑ Summary deps/libgit2/src/util/array.h »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "sortedcache.h"
9  
10 85 int git_sortedcache_new(
11 git_sortedcache **out,
12 size_t item_path_offset,
13 git_sortedcache_free_item_fn free_item,
14 void *free_item_payload,
15 git_vector_cmp item_cmp,
16 const char *path)
17 {
18 git_sortedcache *sc;
19 size_t pathlen, alloclen;
20  
21 85 ▶ pathlen = path ? strlen(path) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pathlen = path ? strlen(path) : 0; 85 0 85
22  
23 85 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen); 0 85 85
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen); 0 85 85
24 85 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 85 85
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 85 85
25 85 sc = git__calloc(1, alloclen);
26 85 ▶ GIT_ERROR_CHECK_ALLOC(sc);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(sc); 0 85 85
27  
28 170 if (git_pool_init(&sc->pool, 1) < 0 ||
29 170 ▶ git_vector_init(&sc->items, 4, item_cmp) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_vector_init(&sc->items, 4, item_cmp) < 0 || 0 85 85
30 85 git_strmap_new(&sc->map) < 0)
31 0 goto fail;
32  
33 85 ▶ if (git_rwlock_init(&sc->lock)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_rwlock_init(&sc->lock)) { 0 85 85
34 0 git_error_set(GIT_ERROR_OS, "failed to initialize lock");
35 0 goto fail;
36 }
37  
38 85 sc->item_path_offset = item_path_offset;
39 85 sc->free_item = free_item;
40 85 sc->free_item_payload = free_item_payload;
41 85 GIT_REFCOUNT_INC(sc);
42 85 ▶ if (pathlen)
Branch: 1 branch
BranchTrueFalseTotal
if (pathlen) 85 0 85
43 85 memcpy(sc->path, path, pathlen);
44  
45 85 *out = sc;
46 85 return 0;
47  
48 0 fail:
49 0 git_strmap_free(sc->map);
50 0 git_vector_free(&sc->items);
51 0 git_pool_clear(&sc->pool);
52 0 git__free(sc);
53 0 return -1;
54 }
55  
56 0 void git_sortedcache_incref(git_sortedcache *sc)
57 {
58 0 GIT_REFCOUNT_INC(sc);
59 0 }
60  
61 0 const char *git_sortedcache_path(git_sortedcache *sc)
62 {
63 0 return sc->path;
64 }
65  
66 413 static void sortedcache_clear(git_sortedcache *sc)
67 {
68 413 git_strmap_clear(sc->map);
69  
70 413 ▶ if (sc->free_item) {
Branch: 1 branch
BranchTrueFalseTotal
if (sc->free_item) { 0 413 413
71 size_t i;
72 void *item;
73  
74 0 ▶ git_vector_foreach(&sc->items, i, item) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&sc->items, i, item) { 0 0 0
75 0 sc->free_item(sc->free_item_payload, item);
76 }
77 }
78  
79 413 git_vector_clear(&sc->items);
80  
81 413 git_pool_clear(&sc->pool);
82 413 }
83  
84 80 static void sortedcache_free(git_sortedcache *sc)
85 {
86 /* acquire write lock to make sure everyone else is done */
87 80 ▶ if (git_sortedcache_wlock(sc) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_sortedcache_wlock(sc) < 0) 0 80 80
88 0 return;
89  
90 80 sortedcache_clear(sc);
91 80 git_vector_free(&sc->items);
92 80 git_strmap_free(sc->map);
93  
94 80 git_sortedcache_wunlock(sc);
95  
96 80 git_rwlock_free(&sc->lock);
97 80 git__free(sc);
98 }
99  
100 80 void git_sortedcache_free(git_sortedcache *sc)
101 {
102 80 ▶ if (!sc)
Branch: 1 branch
BranchTrueFalseTotal
if (!sc) 0 80 80
103 0 return;
104 80 ▶ GIT_REFCOUNT_DEC(sc, sortedcache_free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(sc, sortedcache_free); 80 0 80
GIT_REFCOUNT_DEC(sc, sortedcache_free); 80 0 80
105 }
106  
107 0 static int sortedcache_copy_item(void *payload, void *tgt_item, void *src_item)
108 {
109 0 git_sortedcache *sc = payload;
110 /* path will already have been copied by upsert */
111 0 memcpy(tgt_item, src_item, sc->item_path_offset);
112 0 return 0;
113 }
114  
115 /* copy a sorted cache */
116 31 int git_sortedcache_copy(
117 git_sortedcache **out,
118 git_sortedcache *src,
119 bool lock,
120 int (*copy_item)(void *payload, void *tgt_item, void *src_item),
121 void *payload)
122 {
123 31 int error = 0;
124 git_sortedcache *tgt;
125 size_t i;
126 void *src_item, *tgt_item;
127  
128 /* just use memcpy if no special copy fn is passed in */
129 31 ▶ if (!copy_item) {
Branch: 1 branch
BranchTrueFalseTotal
if (!copy_item) { 31 0 31
130 31 copy_item = sortedcache_copy_item;
131 31 payload = src;
132 }
133  
134 31 ▶ if ((error = git_sortedcache_new(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_new( 0 31 31
135 &tgt, src->item_path_offset,
136 src->free_item, src->free_item_payload,
137 31 src->items._cmp, src->path)) < 0)
138 0 return error;
139  
140 31 ▶ if (lock && git_sortedcache_rlock(src) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (lock && git_sortedcache_rlock(src) < 0) { 31 0 31
if (lock && git_sortedcache_rlock(src) < 0) { 0 31 31
141 0 git_sortedcache_free(tgt);
142 0 return -1;
143 }
144  
145 31 ▶ git_vector_foreach(&src->items, i, src_item) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&src->items, i, src_item) { 0 31 31
146 0 char *path = ((char *)src_item) + src->item_path_offset;
147  
148 0 if ((error = git_sortedcache_upsert(&tgt_item, tgt, path)) < 0 ||
149 0 (error = copy_item(payload, tgt_item, src_item)) < 0)
150 break;
151 }
152  
153 31 ▶ if (lock)
Branch: 1 branch
BranchTrueFalseTotal
if (lock) 31 0 31
154 31 git_sortedcache_runlock(src);
155 31 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 31 31
156 0 git_sortedcache_free(tgt);
157  
158 31 ▶ *out = !error ? tgt : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*out = !error ? tgt : NULL; 31 0 31
159  
160 31 return error;
161 }
162  
163 /* lock sortedcache while making modifications */
164 754 int git_sortedcache_wlock(git_sortedcache *sc)
165 {
166 754 GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
167  
168 754 ▶ if (git_rwlock_wrlock(&sc->lock) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_rwlock_wrlock(&sc->lock) < 0) { 0 754 754
169 0 git_error_set(GIT_ERROR_OS, "unable to acquire write lock on cache");
170 0 return -1;
171 }
172 754 return 0;
173 }
174  
175 /* unlock sorted cache when done with modifications */
176 754 void git_sortedcache_wunlock(git_sortedcache *sc)
177 {
178 754 git_vector_sort(&sc->items);
179 754 git_rwlock_wrunlock(&sc->lock);
180 754 }
181  
182 /* lock sortedcache for read */
183 295 int git_sortedcache_rlock(git_sortedcache *sc)
184 {
185 295 GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
186  
187 295 ▶ if (git_rwlock_rdlock(&sc->lock) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_rwlock_rdlock(&sc->lock) < 0) { 0 295 295
188 0 git_error_set(GIT_ERROR_OS, "unable to acquire read lock on cache");
189 0 return -1;
190 }
191 295 return 0;
192 }
193  
194 /* unlock sorted cache when done reading */
195 295 void git_sortedcache_runlock(git_sortedcache *sc)
196 {
197 295 GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
198 295 git_rwlock_rdunlock(&sc->lock);
199 295 }
200  
201 /* if the file has changed, lock cache and load file contents into buf;
202 * returns <0 on error, >0 if file has not changed
203 */
204 333 int git_sortedcache_lockandload(git_sortedcache *sc, git_str *buf)
205 {
206 int error, fd;
207 struct stat st;
208  
209 333 ▶ if ((error = git_sortedcache_wlock(sc)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_wlock(sc)) < 0) 0 333 333
210 0 return error;
211  
212 333 ▶ if ((error = git_futils_filestamp_check(&sc->stamp, sc->path)) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_futils_filestamp_check(&sc->stamp, sc->path)) <= 0) 333 0 333
213 333 goto unlock;
214  
215 0 ▶ if ((fd = git_futils_open_ro(sc->path)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = git_futils_open_ro(sc->path)) < 0) { 0 0 0
216 0 error = fd;
217 0 goto unlock;
218 }
219  
220 0 ▶ if (p_fstat(fd, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_fstat(fd, &st) < 0) { 0 0 0
221 0 git_error_set(GIT_ERROR_OS, "failed to stat file");
222 0 error = -1;
223 0 (void)p_close(fd);
224 0 goto unlock;
225 }
226  
227 0 ▶ if (!git__is_sizet(st.st_size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git__is_sizet(st.st_size)) { 0 0 0
228 0 git_error_set(GIT_ERROR_INVALID, "unable to load file larger than size_t");
229 0 error = -1;
230 0 (void)p_close(fd);
231 0 goto unlock;
232 }
233  
234 0 ▶ if (buf)
Branch: 1 branch
BranchTrueFalseTotal
if (buf) 0 0 0
235 0 error = git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size);
236  
237 0 (void)p_close(fd);
238  
239 0 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 0 0
240 0 goto unlock;
241  
242 0 return 1; /* return 1 -> file needs reload and was successfully loaded */
243  
244 333 unlock:
245 333 git_sortedcache_wunlock(sc);
246 333 return error;
247 }
248  
249 0 void git_sortedcache_updated(git_sortedcache *sc)
250 {
251 /* update filestamp to latest value */
252 0 git_futils_filestamp_check(&sc->stamp, sc->path);
253 0 }
254  
255 /* release all items in sorted cache */
256 333 int git_sortedcache_clear(git_sortedcache *sc, bool wlock)
257 {
258 333 ▶ if (wlock && git_sortedcache_wlock(sc) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (wlock && git_sortedcache_wlock(sc) < 0) 333 0 333
if (wlock && git_sortedcache_wlock(sc) < 0) 0 333 333
259 0 return -1;
260  
261 333 sortedcache_clear(sc);
262  
263 333 ▶ if (wlock)
Branch: 1 branch
BranchTrueFalseTotal
if (wlock) 333 0 333
264 333 git_sortedcache_wunlock(sc);
265  
266 333 return 0;
267 }
268  
269 /* find and/or insert item, returning pointer to item data */
270 0 int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
271 {
272 size_t keylen, itemlen;
273 0 int error = 0;
274 char *item_key;
275 void *item;
276  
277 0 ▶ if ((item = git_strmap_get(sc->map, key)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((item = git_strmap_get(sc->map, key)) != NULL) 0 0 0
278 0 goto done;
279  
280 0 keylen = strlen(key);
281 0 itemlen = sc->item_path_offset + keylen + 1;
282 0 itemlen = (itemlen + 7) & ~7;
283  
284 0 ▶ if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) { 0 0 0
285 /* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */
286 0 error = -1;
287 0 goto done;
288 }
289  
290 /* one strange thing is that even if the vector or hash table insert
291 * fail, there is no way to free the pool item so we just abandon it
292 */
293  
294 0 item_key = ((char *)item) + sc->item_path_offset;
295 0 memcpy(item_key, key, keylen);
296  
297 0 ▶ if ((error = git_strmap_set(sc->map, item_key, item)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_strmap_set(sc->map, item_key, item)) < 0) 0 0 0
298 0 goto done;
299  
300 0 ▶ if ((error = git_vector_insert(&sc->items, item)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&sc->items, item)) < 0) 0 0 0
301 0 git_strmap_delete(sc->map, item_key);
302  
303 0 done:
304 0 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 0 0 0
305 0 ▶ *out = !error ? item : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*out = !error ? item : NULL; 0 0 0
306 0 return error;
307 }
308  
309 /* lookup item by key */
310 308 void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key)
311 {
312 308 return git_strmap_get(sc->map, key);
313 }
314  
315 /* find out how many items are in the cache */
316 128 size_t git_sortedcache_entrycount(const git_sortedcache *sc)
317 {
318 128 return git_vector_length(&sc->items);
319 }
320  
321 /* lookup item by index */
322 0 void *git_sortedcache_entry(git_sortedcache *sc, size_t pos)
323 {
324 /* make sure the items are sorted so this gets the correct item */
325 0 ▶ if (!git_vector_is_sorted(&sc->items))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_vector_is_sorted(&sc->items)) 0 0 0
326 0 git_vector_sort(&sc->items);
327  
328 0 return git_vector_get(&sc->items, pos);
329 }
330  
331 /* helper struct so bsearch callback can know offset + key value for cmp */
332 struct sortedcache_magic_key {
333 size_t offset;
334 const char *key;
335 };
336  
337 0 static int sortedcache_magic_cmp(const void *key, const void *value)
338 {
339 0 const struct sortedcache_magic_key *magic = key;
340 0 const char *value_key = ((const char *)value) + magic->offset;
341 0 return strcmp(magic->key, value_key);
342 }
343  
344 /* lookup index of item by key */
345 8 int git_sortedcache_lookup_index(
346 size_t *out, git_sortedcache *sc, const char *key)
347 {
348 struct sortedcache_magic_key magic;
349  
350 8 magic.offset = sc->item_path_offset;
351 8 magic.key = key;
352  
353 8 return git_vector_bsearch2(out, &sc->items, sortedcache_magic_cmp, &magic);
354 }
355  
356 /* remove entry from cache */
357 0 int git_sortedcache_remove(git_sortedcache *sc, size_t pos)
358 {
359 char *item;
360  
361 /*
362 * Because of pool allocation, this can't actually remove the item,
363 * but we can remove it from the items vector and the hash table.
364 */
365  
366 0 ▶ if ((item = git_vector_get(&sc->items, pos)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((item = git_vector_get(&sc->items, pos)) == NULL) { 0 0 0
367 0 git_error_set(GIT_ERROR_INVALID, "removing item out of range");
368 0 return GIT_ENOTFOUND;
369 }
370  
371 0 (void)git_vector_remove(&sc->items, pos);
372  
373 0 git_strmap_delete(sc->map, item + sc->item_path_offset);
374  
375 0 ▶ if (sc->free_item)
Branch: 1 branch
BranchTrueFalseTotal
if (sc->free_item) 0 0 0
376 0 sc->free_item(sc->free_item_payload, item);
377  
378 0 return 0;
379 }
380  
« deps/libgit2/src/libgit2/idxmap.c ↑ Summary deps/libgit2/src/util/array.h »