src/ucx/allocator.c

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

mercurial