mba/allocator.c

filter: Statementstmt 44.2% 23 / 52 Branchbran 27.5% 11 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 36.9% 34 / 92 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mba/suba.c ↑ Summary mba/varray.c »
1 /* allocator - allocate and free 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 <errno.h>
27  
28 #include "mba/allocator.h"
29 #include "mba/suba.h"
30 #include "mba/msgno.h"
31  
32 void *
33 96 allocator_alloc(struct allocator *al, size_t size, int zero)
34 {
35 void *p;
36  
37 96 ▶ if (!al) {
Branch: 1 branch
BranchTrueFalseTotal
if (!al) { 96 0 96
38 96 ▶ al = global_allocator ? global_allocator : stdlib_allocator;
Branch: 1 branch
BranchTrueFalseTotal
al = global_allocator ? global_allocator : stdlib_allocator; 0 96 96
39 }
40  
41 96 ▶ if (al->tail) { /* fn ptr in shared mem may be invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (al->tail) { /* fn ptr in shared mem may be invalid */ 0 96 96
42 0 p = suba_alloc(al, size, zero);
43 } else {
44 96 p = al->alloc(al, size, zero);
45 }
46 96 ▶ if (p == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) { 0 96 96
47 0 AMSG("");
48 }
49  
50 96 return p;
51 }
52 void *
53 0 allocator_realloc(struct allocator *al, void *obj, size_t size)
54 {
55 void *p;
56  
57 0 ▶ if (!al) {
Branch: 1 branch
BranchTrueFalseTotal
if (!al) { 0 0 0
58 0 ▶ al = global_allocator ? global_allocator : stdlib_allocator;
Branch: 1 branch
BranchTrueFalseTotal
al = global_allocator ? global_allocator : stdlib_allocator; 0 0 0
59 }
60  
61 0 ▶ if (al->tail) { /* fn ptr in shared mem may be invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (al->tail) { /* fn ptr in shared mem may be invalid */ 0 0 0
62 0 p = suba_realloc(al, obj, size);
63 } else {
64 0 p = al->realloc(al, obj, size);
65 }
66 0 ▶ if (p == NULL && size) {
Branch: 2 branches
BranchTrueFalseTotal
if (p == NULL && size) { 0 0 0
if (p == NULL && size) { 0 0 0
67 0 AMSG("");
68 }
69  
70 0 return p;
71 }
72 int
73 96 allocator_free(void *al0, void *obj)
74 {
75 96 struct allocator *al = al0;
76  
77 96 ▶ if (!al) {
Branch: 1 branch
BranchTrueFalseTotal
if (!al) { 96 0 96
78 96 ▶ al = global_allocator ? global_allocator : stdlib_allocator;
Branch: 1 branch
BranchTrueFalseTotal
al = global_allocator ? global_allocator : stdlib_allocator; 0 96 96
79 }
80  
81 96 ▶ if (al->tail) { /* fn ptr in shared mem may be invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (al->tail) { /* fn ptr in shared mem may be invalid */ 0 96 96
82 0 ▶ if (suba_free(al, obj) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (suba_free(al, obj) == -1) { 0 0 0
83 0 AMSG("");
84 0 return -1;
85 }
86 96 ▶ } else if (al->free(al, obj) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (al->free(al, obj) == -1) { 0 96 96
87 0 AMSG("");
88 0 return -1;
89 }
90  
91 96 return 0;
92 }
93 void
94 0 allocator_set_reclaim(struct allocator *al, reclaim_fn recl, void *arg)
95 {
96 0 ▶ if (!al) {
Branch: 1 branch
BranchTrueFalseTotal
if (!al) { 0 0 0
97 0 ▶ if (global_allocator) {
Branch: 1 branch
BranchTrueFalseTotal
if (global_allocator) { 0 0 0
98 0 al = global_allocator;
99 } else {
100 0 return; /* stdlib_allocator does not support reclaim_fn */
101 }
102 }
103  
104 0 al->reclaim = recl;
105 0 al->reclaim_arg = arg;
106 }
107  
108 void *
109 96 stdlib_alloc(struct allocator *al, size_t size, int zero)
110 {
111 void *p;
112  
113 96 ▶ if (zero) {
Branch: 1 branch
BranchTrueFalseTotal
if (zero) { 66 30 96
114 66 p = calloc(1, size);
115 } else {
116 30 p = malloc(size);
117 }
118 96 ▶ if (p == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) { 0 96 96
119 0 PMNO(errno);
120 0 return NULL;
121 }
122  
123 (void)al;
124 96 return p;
125 }
126 void *
127 0 stdlib_realloc(struct allocator *al, void *obj, size_t size)
128 {
129 void *p;
130  
131 0 ▶ if ((p = realloc(obj, size)) == NULL && size) {
Branch: 2 branches
BranchTrueFalseTotal
if ((p = realloc(obj, size)) == NULL && size) { 0 0 0
if ((p = realloc(obj, size)) == NULL && size) { 0 0 0
132 0 PMNO(errno);
133 }
134  
135 (void)al;
136 0 return p;
137 }
138 int
139 96 stdlib_free(void *al, void *obj)
140 {
141 96 free(obj);
142 (void)al;
143 96 return 0;
144 }
145  
146 struct allocator stdlib_allocator0 = {
147 "", 0, 0, 0, 0, 0, 0, 0,
148 &stdlib_alloc,
149 &stdlib_realloc,
150 &stdlib_free,
151 NULL, NULL, 0, 0
152 };
153  
154 struct allocator *stdlib_allocator = &stdlib_allocator0;
155 struct allocator *global_allocator = NULL;
156  
« mba/suba.c ↑ Summary mba/varray.c »