source src/offmap.c
| Line | Flow | Count | Block(s) | Source |
|---|---|---|---|---|
| 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 "offmap.h" | ||
| 9 | - | |||
| 10 | - | #define kmalloc git__malloc | ||
| 11 | - | #define kcalloc git__calloc | ||
| 12 | - | #define krealloc git__realloc | ||
| 13 | - | #define kreallocarray git__reallocarray | ||
| 14 | - | #define kfree git__free | ||
| 15 | - | #include "khash.h" | ||
| 16 | - | |||
| 17 | - | __KHASH_TYPE(off, off64_t, void *) | ||
| 18 | - | |||
| 19 | 50234 | 2,2,2,2,2,2,2 | __KHASH_IMPL(off, static kh_inline, off64_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal) | |
| 20 | - | |||
| 21 | - | |||
| 22 | 2601 | 2 | int git_offmap_new(git_offmap **out) | |
| 23 | - | { | ||
| 24 | 2601 | 2 | *out = kh_init(off); | |
| 25 | 2601 | 3,4 | GIT_ERROR_CHECK_ALLOC(*out); | |
| 26 | - | |||
| 27 | 2601 | 5 | return 0; | |
| 28 | - | } | ||
| 29 | - | |||
| 30 | 2601 | 2 | void git_offmap_free(git_offmap *map) | |
| 31 | - | { | ||
| 32 | 2601 | 2 | kh_destroy(off, map); | |
| 33 | 2601 | 3 | } | |
| 34 | - | |||
| 35 | ##### | 2 | void git_offmap_clear(git_offmap *map) | |
| 36 | - | { | ||
| 37 | ##### | 2 | kh_clear(off, map); | |
| 38 | ##### | 3 | } | |
| 39 | - | |||
| 40 | ##### | 2 | size_t git_offmap_size(git_offmap *map) | |
| 41 | - | { | ||
| 42 | ##### | 2 | return kh_size(map); | |
| 43 | - | } | ||
| 44 | - | |||
| 45 | 44969 | 2 | void *git_offmap_get(git_offmap *map, const off64_t key) | |
| 46 | - | { | ||
| 47 | 44969 | 2 | size_t idx = kh_get(off, map, key); | |
| 48 | 44969 | 3,4 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
| 49 | 34692 | 5 | return NULL; | |
| 50 | 10277 | 6 | return kh_val(map, idx); | |
| 51 | - | } | ||
| 52 | - | |||
| 53 | 5265 | 2 | int git_offmap_set(git_offmap *map, const off64_t key, void *value) | |
| 54 | - | { | ||
| 55 | - | size_t idx; | ||
| 56 | - | int rval; | ||
| 57 | - | |||
| 58 | 5265 | 2 | idx = kh_put(off, map, key, &rval); | |
| 59 | 5265 | 3 | if (rval < 0) | |
| 60 | ##### | 4 | return -1; | |
| 61 | - | |||
| 62 | 5265 | 5 | if (rval == 0) | |
| 63 | ##### | 6 | kh_key(map, idx) = key; | |
| 64 | - | |||
| 65 | 5265 | 7 | kh_val(map, idx) = value; | |
| 66 | - | |||
| 67 | 5265 | 7 | return 0; | |
| 68 | - | } | ||
| 69 | - | |||
| 70 | ##### | 2 | int git_offmap_delete(git_offmap *map, const off64_t key) | |
| 71 | - | { | ||
| 72 | ##### | 2 | khiter_t idx = kh_get(off, map, key); | |
| 73 | ##### | 3 | if (idx == kh_end(map)) | |
| 74 | ##### | 4 | return GIT_ENOTFOUND; | |
| 75 | ##### | 5 | kh_del(off, map, idx); | |
| 76 | ##### | 6 | return 0; | |
| 77 | - | } | ||
| 78 | - | |||
| 79 | 5265 | 2 | int git_offmap_exists(git_offmap *map, const off64_t key) | |
| 80 | - | { | ||
| 81 | 5265 | 2 | return kh_get(off, map, key) != kh_end(map); | |
| 82 | - | } | ||
| 83 | - | |||
| 84 | ![]() |
7866 | 2 | int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key) |
| 85 | - | { | ||
| 86 | 7866 | 2 | size_t i = *iter; | |
| 87 | - | |||
| 88 | 13173 | 2,4,5 | while (i < map->n_buckets && !kh_exist(map, i)) | |
| 89 | 5307 | 3 | i++; | |
| 90 | - | |||
| 91 | 7866 | 6 | if (i >= map->n_buckets) | |
| 92 | 2601 | 7 | return GIT_ITEROVER; | |
| 93 | - | |||
| 94 | 5265 | 8 | if (key) | |
| 95 | ##### | 9 | *key = kh_key(map, i); | |
| 96 | 5265 | 10 | if (value) | |
| 97 | 5265 | 11 | *value = kh_value(map, i); | |
| 98 | 5265 | 12 | *iter = ++i; | |
| 99 | - | |||
| 100 | 5265 | 12 | return 0; | |
| 101 | - | } |