ucx/allocator.c

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
equal deleted inserted replaced
10:80f9d007cb52 11:0aa8cbd7912e
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/allocator.h" 29 #include "cx/allocator.h"
30 30
31 #include <errno.h>
32
31 __attribute__((__malloc__, __alloc_size__(2))) 33 __attribute__((__malloc__, __alloc_size__(2)))
32 static void *cx_malloc_stdlib( 34 static void *cx_malloc_stdlib(
33 __attribute__((__unused__)) void *d, 35 cx_attr_unused void *d,
34 size_t n 36 size_t n
35 ) { 37 ) {
36 return malloc(n); 38 return malloc(n);
37 } 39 }
38 40
39 __attribute__((__warn_unused_result__, __alloc_size__(3))) 41 __attribute__((__warn_unused_result__, __alloc_size__(3)))
40 static void *cx_realloc_stdlib( 42 static void *cx_realloc_stdlib(
41 __attribute__((__unused__)) void *d, 43 cx_attr_unused void *d,
42 void *mem, 44 void *mem,
43 size_t n 45 size_t n
44 ) { 46 ) {
45 return realloc(mem, n); 47 return realloc(mem, n);
46 } 48 }
47 49
48 __attribute__((__malloc__, __alloc_size__(2, 3))) 50 __attribute__((__malloc__, __alloc_size__(2, 3)))
49 static void *cx_calloc_stdlib( 51 static void *cx_calloc_stdlib(
50 __attribute__((__unused__)) void *d, 52 cx_attr_unused void *d,
51 size_t nelem, 53 size_t nelem,
52 size_t n 54 size_t n
53 ) { 55 ) {
54 return calloc(nelem, n); 56 return calloc(nelem, n);
55 } 57 }
56 58
57 __attribute__((__nonnull__)) 59 __attribute__((__nonnull__))
58 static void cx_free_stdlib( 60 static void cx_free_stdlib(
59 __attribute__((__unused__)) void *d, 61 cx_attr_unused void *d,
60 void *mem 62 void *mem
61 ) { 63 ) {
62 free(mem); 64 free(mem);
63 } 65 }
64 66
73 &cx_default_allocator_class, 75 &cx_default_allocator_class,
74 NULL 76 NULL
75 }; 77 };
76 CxAllocator *cxDefaultAllocator = &cx_default_allocator; 78 CxAllocator *cxDefaultAllocator = &cx_default_allocator;
77 79
78 80 #undef cx_reallocate
79 int cx_reallocate( 81 int cx_reallocate(
80 void **mem, 82 void **mem,
81 size_t n 83 size_t n
82 ) { 84 ) {
83 void *nmem = realloc(*mem, n); 85 void *nmem = realloc(*mem, n);
84 if (nmem == NULL) { 86 if (nmem == NULL) {
85 return 1; 87 return 1; // LCOV_EXCL_LINE
86 } else { 88 } else {
87 *mem = nmem; 89 *mem = nmem;
88 return 0; 90 return 0;
89 } 91 }
90 } 92 }
91 93
94 #undef cx_reallocatearray
95 int cx_reallocatearray(
96 void **mem,
97 size_t nmemb,
98 size_t size
99 ) {
100 size_t n;
101 if (cx_szmul(nmemb, size, &n)) {
102 errno = EOVERFLOW;
103 return 1;
104 } else {
105 void *nmem = realloc(*mem, n);
106 if (nmem == NULL) {
107 return 1; // LCOV_EXCL_LINE
108 } else {
109 *mem = nmem;
110 return 0;
111 }
112 }
113 }
114
92 // IMPLEMENTATION OF HIGH LEVEL API 115 // IMPLEMENTATION OF HIGH LEVEL API
93 116
94 void *cxMalloc( 117 void *cxMalloc(
95 CxAllocator const *allocator, 118 const CxAllocator *allocator,
96 size_t n 119 size_t n
97 ) { 120 ) {
98 return allocator->cl->malloc(allocator->data, n); 121 return allocator->cl->malloc(allocator->data, n);
99 } 122 }
100 123
101 void *cxRealloc( 124 void *cxRealloc(
102 CxAllocator const *allocator, 125 const CxAllocator *allocator,
103 void *mem, 126 void *mem,
104 size_t n 127 size_t n
105 ) { 128 ) {
106 return allocator->cl->realloc(allocator->data, mem, n); 129 return allocator->cl->realloc(allocator->data, mem, n);
107 } 130 }
108 131
132 void *cxReallocArray(
133 const CxAllocator *allocator,
134 void *mem,
135 size_t nmemb,
136 size_t size
137 ) {
138 size_t n;
139 if (cx_szmul(nmemb, size, &n)) {
140 errno = EOVERFLOW;
141 return NULL;
142 } else {
143 return allocator->cl->realloc(allocator->data, mem, n);
144 }
145 }
146
147 #undef cxReallocate
109 int cxReallocate( 148 int cxReallocate(
110 CxAllocator const *allocator, 149 const CxAllocator *allocator,
111 void **mem, 150 void **mem,
112 size_t n 151 size_t n
113 ) { 152 ) {
114 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); 153 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
115 if (nmem == NULL) { 154 if (nmem == NULL) {
116 return 1; 155 return 1; // LCOV_EXCL_LINE
156 } else {
157 *mem = nmem;
158 return 0;
159 }
160 }
161
162 #undef cxReallocateArray
163 int cxReallocateArray(
164 const CxAllocator *allocator,
165 void **mem,
166 size_t nmemb,
167 size_t size
168 ) {
169 void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
170 if (nmem == NULL) {
171 return 1; // LCOV_EXCL_LINE
117 } else { 172 } else {
118 *mem = nmem; 173 *mem = nmem;
119 return 0; 174 return 0;
120 } 175 }
121 } 176 }
122 177
123 void *cxCalloc( 178 void *cxCalloc(
124 CxAllocator const *allocator, 179 const CxAllocator *allocator,
125 size_t nelem, 180 size_t nelem,
126 size_t n 181 size_t n
127 ) { 182 ) {
128 return allocator->cl->calloc(allocator->data, nelem, n); 183 return allocator->cl->calloc(allocator->data, nelem, n);
129 } 184 }
130 185
131 void cxFree( 186 void cxFree(
132 CxAllocator const *allocator, 187 const CxAllocator *allocator,
133 void *mem 188 void *mem
134 ) { 189 ) {
135 allocator->cl->free(allocator->data, mem); 190 allocator->cl->free(allocator->data, mem);
136 } 191 }

mercurial