ucx/allocator.c

changeset 101
7b3a3130be44
parent 49
2f71f4ee247a
equal deleted inserted replaced
100:d2bd73d28ff1 101:7b3a3130be44
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 nelem,
52 size_t n 51 size_t n
53 ) { 52 ) {
54 return calloc(nelem, n); 53 return calloc(nelem, n);
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
73 &cx_default_allocator_class, 71 &cx_default_allocator_class,
74 NULL 72 NULL
75 }; 73 };
76 CxAllocator *cxDefaultAllocator = &cx_default_allocator; 74 CxAllocator *cxDefaultAllocator = &cx_default_allocator;
77 75
78 76 #undef cx_reallocate
79 int cx_reallocate( 77 int cx_reallocate(
80 void **mem, 78 void **mem,
81 size_t n 79 size_t n
82 ) { 80 ) {
83 void *nmem = realloc(*mem, n); 81 void *nmem = realloc(*mem, n);
84 if (nmem == NULL) { 82 if (nmem == NULL) {
85 return 1; 83 return 1; // LCOV_EXCL_LINE
86 } else { 84 } else {
87 *mem = nmem; 85 *mem = nmem;
88 return 0; 86 return 0;
87 }
88 }
89
90 #undef cx_reallocatearray
91 int cx_reallocatearray(
92 void **mem,
93 size_t nmemb,
94 size_t size
95 ) {
96 size_t n;
97 if (cx_szmul(nmemb, size, &n)) {
98 errno = EOVERFLOW;
99 return 1;
100 } else {
101 void *nmem = realloc(*mem, n);
102 if (nmem == NULL) {
103 return 1; // LCOV_EXCL_LINE
104 } else {
105 *mem = nmem;
106 return 0;
107 }
89 } 108 }
90 } 109 }
91 110
92 // IMPLEMENTATION OF HIGH LEVEL API 111 // IMPLEMENTATION OF HIGH LEVEL API
93 112
104 size_t n 123 size_t n
105 ) { 124 ) {
106 return allocator->cl->realloc(allocator->data, mem, n); 125 return allocator->cl->realloc(allocator->data, mem, n);
107 } 126 }
108 127
128 void *cxReallocArray(
129 const CxAllocator *allocator,
130 void *mem,
131 size_t nmemb,
132 size_t size
133 ) {
134 size_t n;
135 if (cx_szmul(nmemb, size, &n)) {
136 errno = EOVERFLOW;
137 return NULL;
138 } else {
139 return allocator->cl->realloc(allocator->data, mem, n);
140 }
141 }
142
143 #undef cxReallocate
109 int cxReallocate( 144 int cxReallocate(
110 const CxAllocator *allocator, 145 const CxAllocator *allocator,
111 void **mem, 146 void **mem,
112 size_t n 147 size_t n
113 ) { 148 ) {
114 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); 149 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
115 if (nmem == NULL) { 150 if (nmem == NULL) {
116 return 1; 151 return 1; // LCOV_EXCL_LINE
152 } else {
153 *mem = nmem;
154 return 0;
155 }
156 }
157
158 #undef cxReallocateArray
159 int cxReallocateArray(
160 const CxAllocator *allocator,
161 void **mem,
162 size_t nmemb,
163 size_t size
164 ) {
165 void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
166 if (nmem == NULL) {
167 return 1; // LCOV_EXCL_LINE
117 } else { 168 } else {
118 *mem = nmem; 169 *mem = nmem;
119 return 0; 170 return 0;
120 } 171 }
121 } 172 }

mercurial