/src/libgit2/src/util/array.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef INCLUDE_array_h__ |
8 | | #define INCLUDE_array_h__ |
9 | | |
10 | | #include "git2_util.h" |
11 | | |
12 | | /* |
13 | | * Use this to declare a typesafe resizable array of items, a la: |
14 | | * |
15 | | * git_array_t(int) my_ints = GIT_ARRAY_INIT; |
16 | | * ... |
17 | | * int *i = git_array_alloc(my_ints); |
18 | | * GIT_ERROR_CHECK_ALLOC(i); |
19 | | * ... |
20 | | * git_array_clear(my_ints); |
21 | | * |
22 | | * You may also want to do things like: |
23 | | * |
24 | | * typedef git_array_t(my_struct) my_struct_array_t; |
25 | | */ |
26 | 0 | #define git_array_t(type) struct { type *ptr; size_t size, asize; } |
27 | | |
28 | 2.30k | #define GIT_ARRAY_INIT { NULL, 0, 0 } |
29 | | |
30 | | #define git_array_init(a) \ |
31 | 24.1k | do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0) |
32 | | |
33 | | #define git_array_init_to_size(a, desired) \ |
34 | 4.03k | do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0) |
35 | | |
36 | | #define git_array_dispose(a) \ |
37 | 3.62k | do { git__free((a).ptr); } while (0) |
38 | | |
39 | | #define git_array_clear(a) \ |
40 | 24.1k | do { git__free((a).ptr); git_array_init(a); } while (0) |
41 | | |
42 | 4.02k | #define GIT_ERROR_CHECK_ARRAY(a) GIT_ERROR_CHECK_ALLOC((a).ptr) |
43 | | |
44 | | GIT_INLINE(void *) git_array__alloc(void *arr, size_t *size, size_t *asize, size_t item_size) |
45 | 11.7k | { |
46 | 11.7k | size_t new_size; |
47 | 11.7k | void *new_array; |
48 | | |
49 | 11.7k | if (*size < *asize) |
50 | 0 | return arr; |
51 | | |
52 | 11.7k | if (*size < 8) { |
53 | 8.37k | new_size = 8; |
54 | 8.37k | } else { |
55 | 3.34k | if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) |
56 | 0 | goto on_oom; |
57 | | |
58 | 3.34k | new_size /= 2; |
59 | 3.34k | } |
60 | | |
61 | 11.7k | if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) |
62 | 0 | goto on_oom; |
63 | | |
64 | 11.7k | *asize = new_size; |
65 | | |
66 | 11.7k | return new_array; |
67 | | |
68 | 0 | on_oom: |
69 | 0 | git__free(arr); |
70 | 0 | *size = 0; |
71 | 0 | *asize = 0; |
72 | 0 | return NULL; |
73 | 11.7k | } Unexecuted instantiation: commit_graph.c:git_array__alloc Unexecuted instantiation: merge_driver.c:git_array__alloc Unexecuted instantiation: merge_file.c:git_array__alloc Unexecuted instantiation: mwindow.c:git_array__alloc Unexecuted instantiation: object_api.c:git_array__alloc Unexecuted instantiation: odb.c:git_array__alloc Unexecuted instantiation: odb_loose.c:git_array__alloc Unexecuted instantiation: odb_pack.c:git_array__alloc Unexecuted instantiation: oid.c:git_array__alloc Unexecuted instantiation: pack.c:git_array__alloc Unexecuted instantiation: repository.c:git_array__alloc Unexecuted instantiation: revparse.c:git_array__alloc Unexecuted instantiation: revwalk.c:git_array__alloc Unexecuted instantiation: settings.c:git_array__alloc Unexecuted instantiation: submodule.c:git_array__alloc Unexecuted instantiation: tag.c:git_array__alloc Unexecuted instantiation: http.c:git_array__alloc Unexecuted instantiation: smart_protocol.c:git_array__alloc Line | Count | Source | 45 | 145 | { | 46 | 145 | size_t new_size; | 47 | 145 | void *new_array; | 48 | | | 49 | 145 | if (*size < *asize) | 50 | 0 | return arr; | 51 | | | 52 | 145 | if (*size < 8) { | 53 | 0 | new_size = 8; | 54 | 145 | } else { | 55 | 145 | if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) | 56 | 0 | goto on_oom; | 57 | | | 58 | 145 | new_size /= 2; | 59 | 145 | } | 60 | | | 61 | 145 | if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) | 62 | 0 | goto on_oom; | 63 | | | 64 | 145 | *asize = new_size; | 65 | | | 66 | 145 | return new_array; | 67 | | | 68 | 0 | on_oom: | 69 | 0 | git__free(arr); | 70 | 0 | *size = 0; | 71 | 0 | *asize = 0; | 72 | 0 | return NULL; | 73 | 145 | } |
Unexecuted instantiation: worktree.c:git_array__alloc Unexecuted instantiation: annotated_commit.c:git_array__alloc Unexecuted instantiation: attr.c:git_array__alloc Unexecuted instantiation: attr_file.c:git_array__alloc Unexecuted instantiation: attrcache.c:git_array__alloc Unexecuted instantiation: blob.c:git_array__alloc Unexecuted instantiation: branch.c:git_array__alloc Unexecuted instantiation: cache.c:git_array__alloc Unexecuted instantiation: checkout.c:git_array__alloc Unexecuted instantiation: clone.c:git_array__alloc commit.c:git_array__alloc Line | Count | Source | 45 | 225 | { | 46 | 225 | size_t new_size; | 47 | 225 | void *new_array; | 48 | | | 49 | 225 | if (*size < *asize) | 50 | 0 | return arr; | 51 | | | 52 | 225 | if (*size < 8) { | 53 | 38 | new_size = 8; | 54 | 187 | } else { | 55 | 187 | if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) | 56 | 0 | goto on_oom; | 57 | | | 58 | 187 | new_size /= 2; | 59 | 187 | } | 60 | | | 61 | 225 | if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) | 62 | 0 | goto on_oom; | 63 | | | 64 | 225 | *asize = new_size; | 65 | | | 66 | 225 | return new_array; | 67 | | | 68 | 0 | on_oom: | 69 | 0 | git__free(arr); | 70 | 0 | *size = 0; | 71 | 0 | *asize = 0; | 72 | 0 | return NULL; | 73 | 225 | } |
Unexecuted instantiation: commit_list.c:git_array__alloc Unexecuted instantiation: config.c:git_array__alloc Unexecuted instantiation: config_cache.c:git_array__alloc Unexecuted instantiation: config_file.c:git_array__alloc Unexecuted instantiation: config_list.c:git_array__alloc Unexecuted instantiation: config_parse.c:git_array__alloc Unexecuted instantiation: config_snapshot.c:git_array__alloc Unexecuted instantiation: delta.c:git_array__alloc Unexecuted instantiation: diff.c:git_array__alloc Unexecuted instantiation: diff_driver.c:git_array__alloc Unexecuted instantiation: diff_generate.c:git_array__alloc Unexecuted instantiation: diff_print.c:git_array__alloc Unexecuted instantiation: diff_tform.c:git_array__alloc Unexecuted instantiation: email.c:git_array__alloc Unexecuted instantiation: filter.c:git_array__alloc Unexecuted instantiation: grafts.c:git_array__alloc Unexecuted instantiation: index.c:git_array__alloc Unexecuted instantiation: indexer.c:git_array__alloc iterator.c:git_array__alloc Line | Count | Source | 45 | 7.56k | { | 46 | 7.56k | size_t new_size; | 47 | 7.56k | void *new_array; | 48 | | | 49 | 7.56k | if (*size < *asize) | 50 | 0 | return arr; | 51 | | | 52 | 7.56k | if (*size < 8) { | 53 | 7.56k | new_size = 8; | 54 | 7.56k | } else { | 55 | 0 | if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) | 56 | 0 | goto on_oom; | 57 | | | 58 | 0 | new_size /= 2; | 59 | 0 | } | 60 | | | 61 | 7.56k | if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) | 62 | 0 | goto on_oom; | 63 | | | 64 | 7.56k | *asize = new_size; | 65 | | | 66 | 7.56k | return new_array; | 67 | | | 68 | 0 | on_oom: | 69 | 0 | git__free(arr); | 70 | 0 | *size = 0; | 71 | 0 | *asize = 0; | 72 | 0 | return NULL; | 73 | 7.56k | } |
Unexecuted instantiation: mailmap.c:git_array__alloc Unexecuted instantiation: merge.c:git_array__alloc Unexecuted instantiation: midx.c:git_array__alloc Unexecuted instantiation: object.c:git_array__alloc Unexecuted instantiation: oidarray.c:git_array__alloc Unexecuted instantiation: pack-objects.c:git_array__alloc Unexecuted instantiation: patch.c:git_array__alloc Unexecuted instantiation: patch_generate.c:git_array__alloc Unexecuted instantiation: path.c:git_array__alloc Unexecuted instantiation: pathspec.c:git_array__alloc Unexecuted instantiation: push.c:git_array__alloc Unexecuted instantiation: refdb.c:git_array__alloc Unexecuted instantiation: refdb_fs.c:git_array__alloc Unexecuted instantiation: reflog.c:git_array__alloc Unexecuted instantiation: refs.c:git_array__alloc Unexecuted instantiation: remote.c:git_array__alloc Unexecuted instantiation: signature.c:git_array__alloc Unexecuted instantiation: transaction.c:git_array__alloc Unexecuted instantiation: local.c:git_array__alloc Unexecuted instantiation: smart.c:git_array__alloc Unexecuted instantiation: smart_pkt.c:git_array__alloc Unexecuted instantiation: tree-cache.c:git_array__alloc Unexecuted instantiation: crlf.c:git_array__alloc Unexecuted instantiation: diff_file.c:git_array__alloc Unexecuted instantiation: diff_stats.c:git_array__alloc Unexecuted instantiation: diff_xdiff.c:git_array__alloc Unexecuted instantiation: fetch.c:git_array__alloc Unexecuted instantiation: fetchhead.c:git_array__alloc Unexecuted instantiation: graph.c:git_array__alloc Unexecuted instantiation: ignore.c:git_array__alloc Unexecuted instantiation: patch_parse_fuzzer.c:git_array__alloc patch_parse.c:git_array__alloc Line | Count | Source | 45 | 3.77k | { | 46 | 3.77k | size_t new_size; | 47 | 3.77k | void *new_array; | 48 | | | 49 | 3.77k | if (*size < *asize) | 50 | 0 | return arr; | 51 | | | 52 | 3.77k | if (*size < 8) { | 53 | 768 | new_size = 8; | 54 | 3.01k | } else { | 55 | 3.01k | if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) | 56 | 0 | goto on_oom; | 57 | | | 58 | 3.01k | new_size /= 2; | 59 | 3.01k | } | 60 | | | 61 | 3.77k | if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) | 62 | 0 | goto on_oom; | 63 | | | 64 | 3.77k | *asize = new_size; | 65 | | | 66 | 3.77k | return new_array; | 67 | | | 68 | 0 | on_oom: | 69 | 0 | git__free(arr); | 70 | 0 | *size = 0; | 71 | 0 | *asize = 0; | 72 | 0 | return NULL; | 73 | 3.77k | } |
Unexecuted instantiation: objects_fuzzer.c:git_array__alloc Unexecuted instantiation: config_mem.c:git_array__alloc Unexecuted instantiation: odb_mempack.c:git_array__alloc |
74 | | |
75 | | #define git_array_alloc(a) \ |
76 | 8.71M | (((a).size < (a).asize || \ |
77 | 8.71M | ((a).ptr = git_array__alloc((a).ptr, &(a).size, &(a).asize, sizeof(*(a).ptr))) != NULL) ? &(a).ptr[(a).size++] : (void *)NULL) |
78 | | |
79 | 0 | #define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL) |
80 | | |
81 | 22.6k | #define git_array_pop(a) ((a).size ? &(a).ptr[--(a).size] : (void *)NULL) |
82 | | |
83 | 2.55k | #define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : (void *)NULL) |
84 | | |
85 | 30.6k | #define git_array_size(a) (a).size |
86 | | |
87 | 0 | #define git_array_valid_index(a, i) ((i) < (a).size) |
88 | | |
89 | | #define git_array_foreach(a, i, element) \ |
90 | 8.64M | for ((i) = 0; (i) < (a).size && ((element) = &(a).ptr[(i)]); (i)++) |
91 | | |
92 | | typedef int (*git_array_compare_cb)(const void *, const void *); |
93 | | |
94 | | GIT_INLINE(int) git_array__search( |
95 | | size_t *out, |
96 | | void *array_ptr, |
97 | | size_t item_size, |
98 | | size_t array_len, |
99 | | git_array_compare_cb compare, |
100 | | const void *key) |
101 | 0 | { |
102 | 0 | size_t lim; |
103 | 0 | unsigned char *part, *array = array_ptr, *base = array_ptr; |
104 | 0 | int cmp = -1; |
105 | |
|
106 | 0 | for (lim = array_len; lim != 0; lim >>= 1) { |
107 | 0 | part = base + (lim >> 1) * item_size; |
108 | 0 | cmp = (*compare)(key, part); |
109 | |
|
110 | 0 | if (cmp == 0) { |
111 | 0 | base = part; |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | if (cmp > 0) { /* key > p; take right partition */ |
115 | 0 | base = part + 1 * item_size; |
116 | 0 | lim--; |
117 | 0 | } /* else take left partition */ |
118 | 0 | } |
119 | |
|
120 | 0 | if (out) |
121 | 0 | *out = (base - array) / item_size; |
122 | |
|
123 | 0 | return (cmp == 0) ? 0 : GIT_ENOTFOUND; |
124 | 0 | } Unexecuted instantiation: commit_graph.c:git_array__search Unexecuted instantiation: merge_driver.c:git_array__search Unexecuted instantiation: merge_file.c:git_array__search Unexecuted instantiation: mwindow.c:git_array__search Unexecuted instantiation: object_api.c:git_array__search Unexecuted instantiation: odb.c:git_array__search Unexecuted instantiation: odb_loose.c:git_array__search Unexecuted instantiation: odb_pack.c:git_array__search Unexecuted instantiation: oid.c:git_array__search Unexecuted instantiation: pack.c:git_array__search Unexecuted instantiation: repository.c:git_array__search Unexecuted instantiation: revparse.c:git_array__search Unexecuted instantiation: revwalk.c:git_array__search Unexecuted instantiation: settings.c:git_array__search Unexecuted instantiation: submodule.c:git_array__search Unexecuted instantiation: tag.c:git_array__search Unexecuted instantiation: http.c:git_array__search Unexecuted instantiation: smart_protocol.c:git_array__search Unexecuted instantiation: tree.c:git_array__search Unexecuted instantiation: worktree.c:git_array__search Unexecuted instantiation: annotated_commit.c:git_array__search Unexecuted instantiation: attr.c:git_array__search Unexecuted instantiation: attr_file.c:git_array__search Unexecuted instantiation: attrcache.c:git_array__search Unexecuted instantiation: blob.c:git_array__search Unexecuted instantiation: branch.c:git_array__search Unexecuted instantiation: cache.c:git_array__search Unexecuted instantiation: checkout.c:git_array__search Unexecuted instantiation: clone.c:git_array__search Unexecuted instantiation: commit.c:git_array__search Unexecuted instantiation: commit_list.c:git_array__search Unexecuted instantiation: config.c:git_array__search Unexecuted instantiation: config_cache.c:git_array__search Unexecuted instantiation: config_file.c:git_array__search Unexecuted instantiation: config_list.c:git_array__search Unexecuted instantiation: config_parse.c:git_array__search Unexecuted instantiation: config_snapshot.c:git_array__search Unexecuted instantiation: delta.c:git_array__search Unexecuted instantiation: diff.c:git_array__search Unexecuted instantiation: diff_driver.c:git_array__search Unexecuted instantiation: diff_generate.c:git_array__search Unexecuted instantiation: diff_print.c:git_array__search Unexecuted instantiation: diff_tform.c:git_array__search Unexecuted instantiation: email.c:git_array__search Unexecuted instantiation: filter.c:git_array__search Unexecuted instantiation: grafts.c:git_array__search Unexecuted instantiation: index.c:git_array__search Unexecuted instantiation: indexer.c:git_array__search Unexecuted instantiation: iterator.c:git_array__search Unexecuted instantiation: mailmap.c:git_array__search Unexecuted instantiation: merge.c:git_array__search Unexecuted instantiation: midx.c:git_array__search Unexecuted instantiation: object.c:git_array__search Unexecuted instantiation: oidarray.c:git_array__search Unexecuted instantiation: pack-objects.c:git_array__search Unexecuted instantiation: patch.c:git_array__search Unexecuted instantiation: patch_generate.c:git_array__search Unexecuted instantiation: path.c:git_array__search Unexecuted instantiation: pathspec.c:git_array__search Unexecuted instantiation: push.c:git_array__search Unexecuted instantiation: refdb.c:git_array__search Unexecuted instantiation: refdb_fs.c:git_array__search Unexecuted instantiation: reflog.c:git_array__search Unexecuted instantiation: refs.c:git_array__search Unexecuted instantiation: remote.c:git_array__search Unexecuted instantiation: signature.c:git_array__search Unexecuted instantiation: transaction.c:git_array__search Unexecuted instantiation: local.c:git_array__search Unexecuted instantiation: smart.c:git_array__search Unexecuted instantiation: smart_pkt.c:git_array__search Unexecuted instantiation: tree-cache.c:git_array__search Unexecuted instantiation: crlf.c:git_array__search Unexecuted instantiation: diff_file.c:git_array__search Unexecuted instantiation: diff_stats.c:git_array__search Unexecuted instantiation: diff_xdiff.c:git_array__search Unexecuted instantiation: fetch.c:git_array__search Unexecuted instantiation: fetchhead.c:git_array__search Unexecuted instantiation: graph.c:git_array__search Unexecuted instantiation: ignore.c:git_array__search Unexecuted instantiation: patch_parse_fuzzer.c:git_array__search Unexecuted instantiation: patch_parse.c:git_array__search Unexecuted instantiation: objects_fuzzer.c:git_array__search Unexecuted instantiation: config_mem.c:git_array__search Unexecuted instantiation: odb_mempack.c:git_array__search |
125 | | |
126 | | #define git_array_search(out, a, cmp, key) \ |
127 | 0 | git_array__search(out, (a).ptr, sizeof(*(a).ptr), (a).size, \ |
128 | 0 | (cmp), (key)) |
129 | | |
130 | | #endif |