| 1 |
|
| /* varray - a variable sized array |
| 2 |
|
| * Copyright (c) 2003 Michael B. Allen <mba2000 ioplex.com> |
| 3 |
|
| * |
| 4 |
|
| * The MIT License |
| 5 |
|
| * |
| 6 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
|
| * copy of this software and associated documentation files (the "Software"), |
| 8 |
|
| * to deal in the Software without restriction, including without limitation |
| 9 |
|
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 |
|
| * and/or sell copies of the Software, and to permit persons to whom the |
| 11 |
|
| * Software is furnished to do so, subject to the following conditions: |
| 12 |
|
| * |
| 13 |
|
| * The above copyright notice and this permission notice shall be included |
| 14 |
|
| * in all copies or substantial portions of the Software. |
| 15 |
|
| * |
| 16 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
|
| * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
|
| * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
|
| * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 |
|
| * OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
|
| */ |
| 24 |
|
| |
| 25 |
|
| #include <stdlib.h> |
| 26 |
|
| #include <string.h> |
| 27 |
|
| #include <errno.h> |
| 28 |
|
| |
| 29 |
|
| #include "mba/iterator.h" |
| 30 |
|
| #include "mba/varray.h" |
| 31 |
|
| #include "mba/msgno.h" |
| 32 |
|
| |
| 33 |
|
| #define VAAL(va) ((struct allocator *)((va)->al ? (char *)(va) - (ptrdiff_t)(va)->al : NULL)) |
| 34 |
|
| #define BINSIZ(i) ((i) ? 1 << ((i) + (VARRAY_INIT_SIZE - 1)) : (1 << VARRAY_INIT_SIZE)) |
| 35 |
|
| |
| 36 |
|
| int |
| 37 |
60 |
| varray_init(struct varray *va, size_t membsize, struct allocator *al) |
| 38 |
|
| { |
| 39 |
60 |
▶ | if (va == NULL || membsize == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (va == NULL || membsize == 0) { |
60 |
0 |
60 |
| if (va == NULL || membsize == 0) { |
0 |
60 |
60 |
|
| 40 |
0 |
| PMNO(errno = EINVAL); |
| 41 |
0 |
| return -1; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
60 |
| memset(va, 0, sizeof *va); |
| 45 |
60 |
| va->size = membsize; |
| 46 |
60 |
▶ | if (al && al->tail) { /* al is a suba allocator */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (al && al->tail) { /* al is a suba allocator */ |
0 |
60 |
60 |
| if (al && al->tail) { /* al is a suba allocator */ |
0 |
0 |
0 |
|
| 47 |
0 |
| va->al = (char *)va - (char *)al; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
60 |
| return 0; |
| 51 |
|
| } |
| 52 |
|
| int |
| 53 |
30 |
| varray_deinit(struct varray *va) |
| 54 |
|
| { |
| 55 |
30 |
▶ | if (varray_release(va, 0) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (varray_release(va, 0) == -1) { |
0 |
30 |
30 |
|
| 56 |
0 |
| AMSG(""); |
| 57 |
0 |
| return -1; |
| 58 |
|
| } |
| 59 |
30 |
| return 0; |
| 60 |
|
| } |
| 61 |
|
| struct varray * |
| 62 |
30 |
| varray_new(size_t membsize, struct allocator *al) |
| 63 |
|
| { |
| 64 |
|
| struct varray *va; |
| 65 |
|
| |
| 66 |
30 |
▶ | if ((va = allocator_alloc(al, sizeof *va, 0)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((va = allocator_alloc(al, sizeof *va, 0)) == NULL) { |
0 |
30 |
30 |
|
| 67 |
0 |
| AMSG(""); |
| 68 |
0 |
| return NULL; |
| 69 |
|
| } |
| 70 |
30 |
▶ | if (varray_init(va, membsize, al) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (varray_init(va, membsize, al) == -1) { |
0 |
30 |
30 |
|
| 71 |
0 |
| AMSG(""); |
| 72 |
0 |
| allocator_free(al, va); |
| 73 |
0 |
| return NULL; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
30 |
| return va; |
| 77 |
|
| } |
| 78 |
|
| int |
| 79 |
30 |
| varray_del(void *va) |
| 80 |
|
| { |
| 81 |
30 |
| int ret = 0; |
| 82 |
|
| |
| 83 |
30 |
▶ | if (va) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va) { |
30 |
0 |
30 |
|
| 84 |
30 |
| ret += varray_release(va, 0); |
| 85 |
30 |
▶ | ret += allocator_free(VAAL((struct varray *)va), va); |
Branch: 1 branch
| Branch | True | False | Total |
| ret += allocator_free(VAAL((struct varray *)va), va); |
0 |
30 |
30 |
|
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
30 |
▶ | if (ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret) { |
0 |
30 |
30 |
|
| 89 |
0 |
| AMSG(""); |
| 90 |
0 |
| return -1; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
30 |
| return 0; |
| 94 |
|
| } |
| 95 |
|
| int |
| 96 |
60 |
| varray_release(struct varray *va, unsigned int from) |
| 97 |
|
| { |
| 98 |
|
| unsigned int r, i; |
| 99 |
60 |
| int ret = 0; |
| 100 |
|
| |
| 101 |
60 |
▶ | if (va == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va == NULL) { |
0 |
60 |
60 |
|
| 102 |
0 |
| return 0; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
60 |
| r = (1 << VARRAY_INIT_SIZE); |
| 106 |
60 |
▶ | for (i = 0; i < 16; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) { |
60 |
0 |
60 |
|
| 107 |
60 |
▶ | if (from <= r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (from <= r) { |
60 |
0 |
60 |
|
| 108 |
60 |
| break; |
| 109 |
|
| } |
| 110 |
0 |
| r *= 2; |
| 111 |
|
| } |
| 112 |
60 |
▶ | if (from != 0) i++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (from != 0) i++; |
0 |
60 |
60 |
|
| 113 |
1020 |
▶ | for ( ; i < 16; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i < 16; i++) { |
960 |
60 |
1020 |
|
| 114 |
960 |
▶ | if (va->bins[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va->bins[i]) { |
66 |
894 |
960 |
|
| 115 |
66 |
▶ | struct allocator *al = VAAL(va); |
Branch: 1 branch
| Branch | True | False | Total |
| struct allocator *al = VAAL(va); |
0 |
66 |
66 |
|
| 116 |
66 |
▶ | ret += allocator_free(al, ALADR(al, va->bins[i])); |
Branch: 3 branches
| Branch | True | False | Total |
| ret += allocator_free(al, ALADR(al, va->bins[i])); |
66 |
0 |
66 |
| ret += allocator_free(al, ALADR(al, va->bins[i])); |
0 |
66 |
66 |
| ret += allocator_free(al, ALADR(al, va->bins[i])); |
0 |
0 |
0 |
|
| 117 |
66 |
| va->bins[i] = 0; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
60 |
▶ | if (ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret) { |
0 |
60 |
60 |
|
| 122 |
0 |
| AMSG(""); |
| 123 |
0 |
| return -1; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
60 |
| return 0; |
| 127 |
|
| } |
| 128 |
|
| void * |
| 129 |
15041 |
| varray_get(struct varray *va, unsigned int idx) |
| 130 |
|
| { |
| 131 |
|
| unsigned int r, i, n; |
| 132 |
|
| struct allocator *al; |
| 133 |
|
| |
| 134 |
15041 |
▶ | if (va == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va == NULL) { |
0 |
15041 |
15041 |
|
| 135 |
0 |
| PMNO(errno = EINVAL); |
| 136 |
0 |
| return NULL; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
15041 |
| r = (1 << VARRAY_INIT_SIZE); /* First and second bins hold 32 then 64,128,256,... */ |
| 140 |
40511 |
▶ | for (i = 0; i < 16; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) { |
40511 |
0 |
40511 |
|
| 141 |
40511 |
▶ | if (r > idx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r > idx) { |
15041 |
25470 |
40511 |
|
| 142 |
15041 |
| break; |
| 143 |
|
| } |
| 144 |
25470 |
| r *= 2; |
| 145 |
|
| } |
| 146 |
15041 |
▶ | if (i == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == 16) { |
0 |
15041 |
15041 |
|
| 147 |
0 |
| PMNO(errno = ERANGE); |
| 148 |
0 |
| return NULL; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
15041 |
▶ | al = VAAL(va); |
Branch: 1 branch
| Branch | True | False | Total |
| al = VAAL(va); |
0 |
15041 |
15041 |
|
| 152 |
15041 |
▶ | n = BINSIZ(i); /* n is nmemb in bin i */ |
Branch: 1 branch
| Branch | True | False | Total |
| n = BINSIZ(i); /* n is nmemb in bin i */ |
9643 |
5398 |
15041 |
|
| 153 |
|
| |
| 154 |
15041 |
▶ | if (va->bins[i] == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va->bins[i] == 0) { |
66 |
14975 |
15041 |
|
| 155 |
66 |
| char *mem = allocator_alloc(al, n * va->size, 1); |
| 156 |
66 |
▶ | if (mem == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mem == NULL) { |
0 |
66 |
66 |
|
| 157 |
0 |
| AMSG(""); |
| 158 |
0 |
| return NULL; |
| 159 |
|
| } |
| 160 |
66 |
▶ | va->bins[i] = ALREF(al, mem); |
Branch: 3 branches
| Branch | True | False | Total |
| va->bins[i] = ALREF(al, mem); |
66 |
0 |
66 |
| va->bins[i] = ALREF(al, mem); |
0 |
66 |
66 |
| va->bins[i] = ALREF(al, mem); |
0 |
0 |
0 |
|
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
15041 |
▶ | return (char *)ALADR(al, va->bins[i]) + (i ? idx - n : idx) * va->size; |
Branch: 4 branches
| Branch | True | False | Total |
| return (char *)ALADR(al, va->bins[i]) + (i ? idx - n : idx) * va->size; |
15041 |
0 |
15041 |
| return (char *)ALADR(al, va->bins[i]) + (i ? idx - n : idx) * va->size; |
0 |
15041 |
15041 |
| return (char *)ALADR(al, va->bins[i]) + (i ? idx - n : idx) * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(al, va->bins[i]) + (i ? idx - n : idx) * va->size; |
9643 |
5398 |
15041 |
|
| 164 |
|
| } |
| 165 |
|
| int |
| 166 |
0 |
| varray_index(struct varray *va, void *elem) |
| 167 |
|
| { |
| 168 |
0 |
▶ | ref_t er = ALREF(VAAL(va), elem); |
Branch: 6 branches
| Branch | True | False | Total |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
| ref_t er = ALREF(VAAL(va), elem); |
0 |
0 |
0 |
|
| 169 |
|
| int i; |
| 170 |
|
| |
| 171 |
0 |
▶ | for (i = 0; i < 16; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) { |
0 |
0 |
0 |
|
| 172 |
0 |
▶ | if (va->bins[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (va->bins[i]) { |
0 |
0 |
0 |
|
| 173 |
0 |
▶ | size_t n = BINSIZ(i); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t n = BINSIZ(i); |
0 |
0 |
0 |
|
| 174 |
0 |
| ref_t start = va->bins[i]; |
| 175 |
0 |
| ref_t end = start + n * va->size; |
| 176 |
0 |
▶ | if (er >= start && er < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (er >= start && er < end) { |
0 |
0 |
0 |
| if (er >= start && er < end) { |
0 |
0 |
0 |
|
| 177 |
0 |
▶ | return (i ? n : 0) + ((er - start) / va->size); |
Branch: 1 branch
| Branch | True | False | Total |
| return (i ? n : 0) + ((er - start) / va->size); |
0 |
0 |
0 |
|
| 178 |
|
| } |
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
0 |
| PMNO(errno = EFAULT); |
| 183 |
0 |
| return -1; |
| 184 |
|
| } |
| 185 |
|
| void |
| 186 |
0 |
| varray_iterate(void *va, iter_t *iter) |
| 187 |
|
| { |
| 188 |
0 |
▶ | if (va && iter) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (va && iter) { |
0 |
0 |
0 |
| if (va && iter) { |
0 |
0 |
0 |
|
| 189 |
0 |
| iter->i1 = iter->i2 = 0; |
| 190 |
|
| } |
| 191 |
0 |
| } |
| 192 |
|
| void * |
| 193 |
0 |
| varray_next(void *va0, iter_t *iter) |
| 194 |
|
| { |
| 195 |
0 |
| struct varray *va = va0; |
| 196 |
|
| unsigned int n; |
| 197 |
|
| |
| 198 |
0 |
▶ | if (va == NULL || iter == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (va == NULL || iter == NULL) { |
0 |
0 |
0 |
| if (va == NULL || iter == NULL) { |
0 |
0 |
0 |
|
| 199 |
0 |
| PMNO(errno = EINVAL); |
| 200 |
0 |
| return NULL; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| /* n is nmemb in iter->i1 */ |
| 204 |
0 |
▶ | n = iter->i1 == 0 ? (1 << VARRAY_INIT_SIZE) : |
Branch: 1 branch
| Branch | True | False | Total |
| n = iter->i1 == 0 ? (1 << VARRAY_INIT_SIZE) : |
0 |
0 |
0 |
|
| 205 |
0 |
| 1 << (iter->i1 + (VARRAY_INIT_SIZE - 1)); |
| 206 |
|
| |
| 207 |
0 |
▶ | if (iter->i2 == n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter->i2 == n) { |
0 |
0 |
0 |
|
| 208 |
0 |
| iter->i2 = 0; |
| 209 |
0 |
| iter->i1++; |
| 210 |
|
| } |
| 211 |
0 |
▶ | while (va->bins[iter->i1] == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (va->bins[iter->i1] == 0) { |
0 |
0 |
0 |
|
| 212 |
0 |
| iter->i1++; |
| 213 |
0 |
▶ | if (iter->i1 == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter->i1 == 16) { |
0 |
0 |
0 |
|
| 214 |
0 |
| return NULL; |
| 215 |
|
| } |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
0 |
▶ | return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
Branch: 6 branches
| Branch | True | False | Total |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
| return (char *)ALADR(VAAL(va), va->bins[iter->i1]) + iter->i2++ * va->size; |
0 |
0 |
0 |
|
| 219 |
|
| } |
| 220 |
|
| |