Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
3 | * | ||
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
26 | * POSSIBILITY OF SUCH DAMAGE. | ||
27 | */ | ||
28 | |||
29 | #include "cx/allocator.h" | ||
30 | |||
31 | #include <errno.h> | ||
32 | |||
33 | 812 | static void *cx_malloc_stdlib( | |
34 | cx_attr_unused void *d, | ||
35 | size_t n | ||
36 | ) { | ||
37 | 812 | return malloc(n); | |
38 | } | ||
39 | |||
40 | 58 | static void *cx_realloc_stdlib( | |
41 | cx_attr_unused void *d, | ||
42 | void *mem, | ||
43 | size_t n | ||
44 | ) { | ||
45 | 58 | return realloc(mem, n); | |
46 | } | ||
47 | |||
48 | 174 | static void *cx_calloc_stdlib( | |
49 | cx_attr_unused void *d, | ||
50 | size_t nmemb, | ||
51 | size_t size | ||
52 | ) { | ||
53 | 174 | return calloc(nmemb, size); | |
54 | } | ||
55 | |||
56 | 766 | static void cx_free_stdlib( | |
57 | cx_attr_unused void *d, | ||
58 | void *mem | ||
59 | ) { | ||
60 | 766 | free(mem); | |
61 | 766 | } | |
62 | |||
63 | static cx_allocator_class cx_default_allocator_class = { | ||
64 | cx_malloc_stdlib, | ||
65 | cx_realloc_stdlib, | ||
66 | cx_calloc_stdlib, | ||
67 | cx_free_stdlib | ||
68 | }; | ||
69 | |||
70 | struct cx_allocator_s cx_default_allocator = { | ||
71 | &cx_default_allocator_class, | ||
72 | NULL | ||
73 | }; | ||
74 | const CxAllocator * const cxDefaultAllocator = &cx_default_allocator; | ||
75 | |||
76 | 1 | int cx_reallocate_( | |
77 | void **mem, | ||
78 | size_t n | ||
79 | ) { | ||
80 | 1 | void *nmem = realloc(*mem, n); | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (nmem == NULL) { |
82 | − | return 1; // LCOV_EXCL_LINE | |
83 | } else { | ||
84 | 1 | *mem = nmem; | |
85 | 1 | return 0; | |
86 | } | ||
87 | } | ||
88 | |||
89 | 2 | int cx_reallocatearray_( | |
90 | void **mem, | ||
91 | size_t nmemb, | ||
92 | size_t size | ||
93 | ) { | ||
94 | size_t n; | ||
95 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (cx_szmul(nmemb, size, &n)) { |
96 | 1 | errno = EOVERFLOW; | |
97 | 1 | return 1; | |
98 | } else { | ||
99 | 1 | void *nmem = realloc(*mem, n); | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (nmem == NULL) { |
101 | − | return 1; // LCOV_EXCL_LINE | |
102 | } else { | ||
103 | 1 | *mem = nmem; | |
104 | 1 | return 0; | |
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | // IMPLEMENTATION OF HIGH LEVEL API | ||
110 | |||
111 | 6485 | void *cxMalloc( | |
112 | const CxAllocator *allocator, | ||
113 | size_t n | ||
114 | ) { | ||
115 | 6485 | return allocator->cl->malloc(allocator->data, n); | |
116 | } | ||
117 | |||
118 | 256 | void *cxRealloc( | |
119 | const CxAllocator *allocator, | ||
120 | void *mem, | ||
121 | size_t n | ||
122 | ) { | ||
123 | 256 | return allocator->cl->realloc(allocator->data, mem, n); | |
124 | } | ||
125 | |||
126 | 4 | void *cxReallocArray( | |
127 | const CxAllocator *allocator, | ||
128 | void *mem, | ||
129 | size_t nmemb, | ||
130 | size_t size | ||
131 | ) { | ||
132 | size_t n; | ||
133 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | if (cx_szmul(nmemb, size, &n)) { |
134 | 2 | errno = EOVERFLOW; | |
135 | 2 | return NULL; | |
136 | } else { | ||
137 | 2 | return allocator->cl->realloc(allocator->data, mem, n); | |
138 | } | ||
139 | } | ||
140 | |||
141 | 47 | int cxReallocate_( | |
142 | const CxAllocator *allocator, | ||
143 | void **mem, | ||
144 | size_t n | ||
145 | ) { | ||
146 | 47 | void *nmem = allocator->cl->realloc(allocator->data, *mem, n); | |
147 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | if (nmem == NULL) { |
148 | − | return 1; // LCOV_EXCL_LINE | |
149 | } else { | ||
150 | 46 | *mem = nmem; | |
151 | 46 | return 0; | |
152 | } | ||
153 | } | ||
154 | |||
155 | 2 | int cxReallocateArray_( | |
156 | const CxAllocator *allocator, | ||
157 | void **mem, | ||
158 | size_t nmemb, | ||
159 | size_t size | ||
160 | ) { | ||
161 | 2 | void *nmem = cxReallocArray(allocator, *mem, nmemb, size); | |
162 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (nmem == NULL) { |
163 | − | return 1; // LCOV_EXCL_LINE | |
164 | } else { | ||
165 | 1 | *mem = nmem; | |
166 | 1 | return 0; | |
167 | } | ||
168 | } | ||
169 | |||
170 | 435 | void *cxCalloc( | |
171 | const CxAllocator *allocator, | ||
172 | size_t nmemb, | ||
173 | size_t size | ||
174 | ) { | ||
175 | 435 | return allocator->cl->calloc(allocator->data, nmemb, size); | |
176 | } | ||
177 | |||
178 | 6690 | void cxFree( | |
179 | const CxAllocator *allocator, | ||
180 | void *mem | ||
181 | ) { | ||
182 | 6690 | allocator->cl->free(allocator->data, mem); | |
183 | 6690 | } | |
184 |