mba/suba.c

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 256 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mba/msgno.c ↑ Summary mba/allocator.c »
1 /* suba - sub-allocate memory from larger chunk of memory
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 <stdio.h>
28 #include <errno.h>
29  
30 #include "mba/suba.h"
31 #include "mba/dbug.h"
32 #include "mba/msgno.h"
33  
34 struct cell {
35 size_t size;
36 /* void *stk[4]; */
37 int line;
38 ref_t next; /* reference to next cell in free list */
39 };
40  
41 #define ALIGNMASK 7UL
42 #define ALIGNM(s) (((s) + ALIGNMASK) & ~ALIGNMASK)
43 #define POFF ALIGNM(offsetof(struct cell, next))
44 #define C2P(c) ((char *)(c) + POFF)
45 #define P2C(p) ((struct cell *)((char *)(p) - POFF))
46 #define ISADJ(c1,c2) ((struct cell *)(C2P(c1) + (c1)->size) == (struct cell *)(c2))
47 #define SREF(s,p) (ref_t)((char *)(p) - (char *)(s))
48 #define SADR(s,r) (void *)((char *)(s) + (r))
49 #define RECLAIM_DEPTH_MAX 2
50 #define SUBA_MAGIC "\xFF\x15\x15\x15SUBA"
51  
52 int suba_print_free_list(struct allocator *suba);
53  
54 void *
55 0 suba_addr(const struct allocator *suba, const ref_t ref)
56 {
57 0 ▶ if (suba && ref > 0 && ref <= suba->size) {
Branch: 3 branches
BranchTrueFalseTotal
if (suba && ref > 0 && ref <= suba->size) { 0 0 0
if (suba && ref > 0 && ref <= suba->size) { 0 0 0
if (suba && ref > 0 && ref <= suba->size) { 0 0 0
58 0 return (char *)suba + ref;
59 }
60 0 return NULL;
61 }
62 ref_t
63 0 suba_ref(const struct allocator *suba, const void *ptr)
64 {
65 0 ▶ if (suba && ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (suba && ptr) { 0 0 0
if (suba && ptr) { 0 0 0
66 0 ref_t ref = (char *)ptr - (char *)suba;
67 0 ▶ if (ref > 0 && ref <= suba->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref > 0 && ref <= suba->size) { 0 0 0
if (ref > 0 && ref <= suba->size) { 0 0 0
68 0 return ref;
69 }
70 }
71 0 return 0;
72 }
73  
74 struct allocator *
75 0 suba_init(void *mem, size_t size, int rst, size_t mincell)
76 {
77 0 struct allocator *suba = mem;
78 size_t hdrsiz;
79  
80 0 hdrsiz = ALIGNM(sizeof *suba);
81  
82 0 ▶ if (mem == NULL || size <= (hdrsiz + POFF) ||
Branch: 3 branches
BranchTrueFalseTotal
if (mem == NULL || size <= (hdrsiz + POFF) || 0 0 0
if (mem == NULL || size <= (hdrsiz + POFF) || 0 0 0
if (mem == NULL || size <= (hdrsiz + POFF) || 0 0 0
83 0 ▶ (!rst && memcmp(SUBA_MAGIC, suba->magic, 8)) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
(!rst && memcmp(SUBA_MAGIC, suba->magic, 8)) != 0) { 0 0 0
(!rst && memcmp(SUBA_MAGIC, suba->magic, 8)) != 0) { 0 0 0
84 0 PMNO(errno = EINVAL);
85 0 return NULL;
86 }
87  
88 0 ▶ if (rst) {
Branch: 1 branch
BranchTrueFalseTotal
if (rst) { 0 0 0
89 struct cell *c;
90  
91 0 memset(suba, 0, hdrsiz);
92 0 memcpy(suba->magic, SUBA_MAGIC, 8);
93 0 suba->tail = hdrsiz;
94 /* cell data must be large enough for next ref_t */
95 0 suba->mincell = ALIGNM(sizeof(size_t));
96 0 ▶ if (mincell > suba->mincell) {
Branch: 1 branch
BranchTrueFalseTotal
if (mincell > suba->mincell) { 0 0 0
97 0 suba->mincell = ALIGNM(mincell);
98 }
99 0 suba->size = suba->max_free = size;
100  
101 0 c = suba_addr(suba, hdrsiz);
102 0 c->size = size - (hdrsiz + POFF);
103 0 c->next = suba->tail;
104 }
105  
106 0 return suba;
107 }
108 void *
109 0 suba_alloc(struct allocator *suba, size_t size, int zero)
110 {
111 struct cell *c1, *c2, *c3;
112 0 size_t s = size;
113 0 int reclaim = 0;
114  
115 0 ▶ size = size < suba->mincell ? suba->mincell : ALIGNM(size);
Branch: 1 branch
BranchTrueFalseTotal
size = size < suba->mincell ? suba->mincell : ALIGNM(size); 0 0 0
116  
117 0 again:
118 0 ▶ if (reclaim) {
Branch: 1 branch
BranchTrueFalseTotal
if (reclaim) { 0 0 0
119 0 int progress = 0;
120  
121 0 ▶ if (suba->reclaim && suba->reclaim_depth <= RECLAIM_DEPTH_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
if (suba->reclaim && suba->reclaim_depth <= RECLAIM_DEPTH_MAX) { 0 0 0
if (suba->reclaim && suba->reclaim_depth <= RECLAIM_DEPTH_MAX) { 0 0 0
122 0 suba->reclaim_depth++;
123 0 progress = suba->reclaim(suba, suba->reclaim_arg, reclaim);
124 0 suba->reclaim_depth--;
125 }
126  
127 0 ▶ if (!progress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!progress) { 0 0 0
128 0 PMNO(errno = ENOMEM);
129 0 return NULL;
130 }
131 }
132  
133 0 c2 = SADR(suba, suba->tail);
134 for ( ;; ) {
135 0 c1 = c2;
136 0 ▶ if ((c2 = suba_addr(suba, c1->next)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((c2 = suba_addr(suba, c1->next)) == NULL) { 0 0 0
137 0 PMNF(errno = EFAULT, ": 0x%08x", c1->next);
138 0 return NULL;
139 }
140 0 ▶ if (c2->size >= size) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2->size >= size) { 0 0 0
141 0 break; /* found a cell large enough */
142 }
143 0 ▶ if (c1->next == suba->tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (c1->next == suba->tail) { 0 0 0
144 0 reclaim++;
145 0 goto again;
146 }
147 }
148  
149 0 ▶ if (c2->size > (POFF + size + suba->mincell)) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2->size > (POFF + size + suba->mincell)) { 0 0 0
150 /* split new cell */
151 0 c3 = (struct cell *)(C2P(c2) + size);
152 0 c3->size = c2->size - (size + POFF);
153 0 ▶ if (c1 == c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c1 == c2) { 0 0 0
154 0 c1 = c3;
155 } else {
156 0 c3->next = c2->next;
157 }
158 0 c1->next = SREF(suba, c3);
159 0 c2->size = size;
160 0 ▶ if (c2 == SADR(suba, suba->tail)) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2 == SADR(suba, suba->tail)) { 0 0 0
161 0 suba->tail = SREF(suba, c3);
162 }
163 0 ▶ } else if (c1->next == suba->tail) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c1->next == suba->tail) { 0 0 0
164 /* never use the last cell! */
165 0 reclaim++;
166 0 goto again;
167 } else { /* use the entire cell */
168 0 c1->next = c2->next;
169 }
170  
171 0 suba->alloc_total += POFF + c2->size;
172 0 suba->size_total += s;
173  
174 /*
175 dbug_stacktrace(c2->stk, 1, 4);
176  
177 suba_print_cell(suba, "ALLOC", c2);
178 */
179  
180 0 ▶ return zero ? memset(C2P(c2), 0, size) : C2P(c2);
Branch: 1 branch
BranchTrueFalseTotal
return zero ? memset(C2P(c2), 0, size) : C2P(c2); 0 0 0
181 }
182 int
183 0 suba_free(void *suba0, void *ptr)
184 {
185 0 struct allocator *suba = suba0;
186 struct cell *c1, *c2, *c3;
187 ref_t ref;
188 int j1, j2;
189  
190 0 ▶ if (!ptr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptr) return 0; 0 0 0
191  
192 0 ▶ if (!suba_ref(suba, ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!suba_ref(suba, ptr)) { 0 0 0
193 0 PMNO(errno = EFAULT);
194 0 return -1;
195 }
196 /* splice the cell back into the list */
197 0 c1 = SADR(suba, suba->tail);
198 0 c2 = P2C(ptr);
199 0 ▶ if (c2->size > suba->max_free || (ref = suba_ref(suba, c2)) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c2->size > suba->max_free || (ref = suba_ref(suba, c2)) == 0) { 0 0 0
if (c2->size > suba->max_free || (ref = suba_ref(suba, c2)) == 0) { 0 0 0
200 0 PMNF(errno = EINVAL, ": %p: %d", ptr, c2->size);
201 0 return -1;
202 }
203  
204 0 suba->free_total += POFF + c2->size;
205 /*
206 c2->stk[0] = NULL;
207  
208 suba_print_cell(suba, " FREE", c2);
209 */
210  
211 0 ▶ if (c2 > c1) { /* append to end of list */
Branch: 1 branch
BranchTrueFalseTotal
if (c2 > c1) { /* append to end of list */ 0 0 0
212 0 ▶ if (ISADJ(c1,c2)) { /* join with last cell */
Branch: 1 branch
BranchTrueFalseTotal
if (ISADJ(c1,c2)) { /* join with last cell */ 0 0 0
213 0 c1->size += POFF + c2->size;
214 0 return 0;
215 }
216 0 c2->next = c1->next;
217 0 suba->tail = c1->next = ref;
218  
219 0 return 0;
220 }
221  
222 0 ▶ while (c1->next < ref) { /* find insertion point */
Branch: 1 branch
BranchTrueFalseTotal
while (c1->next < ref) { /* find insertion point */ 0 0 0
223 0 ▶ if (c1->next < POFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (c1->next < POFF) { 0 0 0
224 0 PMNF(errno = EINVAL, ": next ref corrupted: %d", c1->next);
225 0 return -1;
226 }
227 0 c1 = SADR(suba, c1->next);
228 }
229 0 c3 = SADR(suba, c1->next);
230  
231 0 j1 = ISADJ(c1,c2); /* c1 and c2 need to be joined */
232 0 j2 = ISADJ(c2,c3); /* c2 and c3 need to be joined */
233  
234 0 ▶ if (j1) {
Branch: 1 branch
BranchTrueFalseTotal
if (j1) { 0 0 0
235 0 ▶ if (j2) { /* splice all three cells together */
Branch: 1 branch
BranchTrueFalseTotal
if (j2) { /* splice all three cells together */ 0 0 0
236 0 ▶ if (SREF(suba, c3) == suba->tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (SREF(suba, c3) == suba->tail) { 0 0 0
237 0 suba->tail = SREF(suba, c1);
238 }
239 0 c1->next = c3->next;
240 0 c1->size += POFF + c3->size;
241 }
242 0 c1->size += POFF + c2->size;
243 } else {
244 0 ▶ if (j2) {
Branch: 1 branch
BranchTrueFalseTotal
if (j2) { 0 0 0
245 0 ▶ if (SREF(suba, c3) == suba->tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (SREF(suba, c3) == suba->tail) { 0 0 0
246 0 suba->tail = ref;
247 }
248 0 ▶ c2->next = c3->next == SREF(suba, c3) ? ref : c3->next;
Branch: 1 branch
BranchTrueFalseTotal
c2->next = c3->next == SREF(suba, c3) ? ref : c3->next; 0 0 0
249 0 c2->size += POFF + c3->size;
250 } else {
251 0 c2->next = c1->next;
252 }
253 0 c1->next = ref;
254 }
255  
256 0 return 0;
257 }
258 void *
259 0 suba_realloc(struct allocator *suba, void *ptr, size_t size)
260 {
261 struct cell *c;
262 void *p;
263  
264 0 ▶ if (ptr == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == NULL) { 0 0 0
265 0 ▶ if ((p = suba_alloc(suba, size, 0)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((p = suba_alloc(suba, size, 0)) == NULL) { 0 0 0
266 0 AMSG("");
267 }
268 0 return p;
269 }
270 0 ▶ if (size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { 0 0 0
271 0 suba_free(suba, ptr);
272 0 return NULL;
273 }
274 0 c = P2C(ptr);
275 0 ▶ if (c->size < size || (c->size - ALIGNM(size)) > suba->mincell) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->size < size || (c->size - ALIGNM(size)) > suba->mincell) { 0 0 0
if (c->size < size || (c->size - ALIGNM(size)) > suba->mincell) { 0 0 0
276 0 p = suba_alloc(suba, size, 0);
277 } else {
278 0 return ptr;
279 }
280 0 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 0 0 0
281 0 memcpy(p, ptr, size);
282 0 suba_free(suba, ptr);
283 }
284  
285 0 return p;
286 }
287  
288 int
289 0 suba_print_cell(struct allocator *suba, const char *msg, struct cell *c)
290 {
291 0 ref_t ref = suba_ref(suba, c);
292 0 ▶ if (ref >= ALIGNM(sizeof *suba) && (ref + POFF + c->size) <= 10000000) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref >= ALIGNM(sizeof *suba) && (ref + POFF + c->size) <= 10000000) { 0 0 0
if (ref >= ALIGNM(sizeof *suba) && (ref + POFF + c->size) <= 10000000) { 0 0 0
293 //fprintf(stderr, "%s: %8u-%-8lu %8u %-8u\n", msg, ref, ref + POFF + c->size, c->size, c->next);
294 } else {
295 //fprintf(stderr, "%s: %8u-err %8u %-8u\n", msg, ref, c->size, c->next);
296 0 return 0;
297 }
298 0 return 1;
299 }
300 /*
301 int
302 suba_print_alloc_list(struct allocator *suba, FILE *stream)
303 {
304 struct cell *c, *tail = SADR(suba, suba->tail);
305  
306 c = (struct cell *)((char *)suba + ALIGNM(sizeof *suba));
307 while (c < tail) {
308 if (c->stk[0]) {
309 unsigned char buf[1024], *blim = buf + 1024;
310 unsigned char msg[16];
311 sprintf((char *)msg, "%d", c->size);
312 dbug_sprint_stacktrace(buf, blim, c->stk, 4, msg);
313 fputs((const char *)buf, stream);
314 fflush(stream);
315 }
316 c = (struct cell *)((char *)c + POFF + c->size);
317 }
318  
319 return 0;
320 }
321 */
322 int
323 0 suba_print_free_list(struct allocator *suba)
324 {
325 struct cell *c;
326 char buf[10];
327 0 int count = 0;
328 0 int ret = 1;
329  
330 0 c = suba_addr(suba, suba->tail);
331 0 ▶ while (c->next < suba->tail) {
Branch: 1 branch
BranchTrueFalseTotal
while (c->next < suba->tail) { 0 0 0
332 0 ▶ if (c->next < POFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->next < POFF) { 0 0 0
333 0 PMNF(errno = EINVAL, ": next ref corrupted: %d", c->next);
334 0 return -1;
335 }
336 0 c = suba_addr(suba, c->next);
337 0 sprintf(buf, "%d", count++);
338 0 ▶ if (!suba_print_cell(suba, buf, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!suba_print_cell(suba, buf, c)) { 0 0 0
339 0 ret = 0;
340 }
341 }
342 0 c = suba_addr(suba, c->next);
343 0 sprintf(buf, "%d", count++);
344 0 ▶ if (!suba_print_cell(suba, buf, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!suba_print_cell(suba, buf, c)) { 0 0 0
345 0 ret = 0;
346 }
347  
348 //fprintf(stderr, "count: start-end size next\n");
349  
350 0 return ret;
351 }
352  
« mba/msgno.c ↑ Summary mba/allocator.c »