src/allocator.c

changeset 1541
d06aa9db0408
parent 1540
411cf6fd268f
equal deleted inserted replaced
1540:411cf6fd268f 1541:d06aa9db0408
106 106
107 int cx_reallocate_( 107 int cx_reallocate_(
108 void **mem, 108 void **mem,
109 size_t n 109 size_t n
110 ) { 110 ) {
111 if (n == 0) {
112 free(*mem);
113 *mem = NULL;
114 return 0;
115 }
111 void *nmem = realloc(*mem, n); 116 void *nmem = realloc(*mem, n);
112 if (nmem == NULL) { 117 if (nmem == NULL) {
113 return 1; // LCOV_EXCL_LINE 118 return 1; // LCOV_EXCL_LINE
114 } else { 119 } else {
115 *mem = nmem; 120 *mem = nmem;
120 int cx_reallocatearray_( 125 int cx_reallocatearray_(
121 void **mem, 126 void **mem,
122 size_t nmemb, 127 size_t nmemb,
123 size_t size 128 size_t size
124 ) { 129 ) {
130 if (nmemb == 0 || size == 0) {
131 free(*mem);
132 *mem = NULL;
133 return 0;
134 }
125 size_t n; 135 size_t n;
126 if (cx_szmul(nmemb, size, &n)) { 136 if (cx_szmul(nmemb, size, &n)) {
127 errno = EOVERFLOW; 137 errno = EOVERFLOW;
128 return 1; 138 return 1;
129 } else { 139 } else {
183 int cxReallocate_( 193 int cxReallocate_(
184 const CxAllocator *allocator, 194 const CxAllocator *allocator,
185 void **mem, 195 void **mem,
186 size_t n 196 size_t n
187 ) { 197 ) {
198 if (n == 0) {
199 cxFree(allocator, *mem);
200 *mem = NULL;
201 return 0;
202 }
188 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); 203 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
189 if (nmem == NULL) { 204 if (nmem == NULL) {
190 return 1; // LCOV_EXCL_LINE 205 return 1; // LCOV_EXCL_LINE
191 } else { 206 } else {
192 *mem = nmem; 207 *mem = nmem;
198 const CxAllocator *allocator, 213 const CxAllocator *allocator,
199 void **mem, 214 void **mem,
200 size_t nmemb, 215 size_t nmemb,
201 size_t size 216 size_t size
202 ) { 217 ) {
218 if (nmemb == 0 || size == 0) {
219 cxFree(allocator, *mem);
220 *mem = NULL;
221 return 0;
222 }
203 void *nmem = cxReallocArray(allocator, *mem, nmemb, size); 223 void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
204 if (nmem == NULL) { 224 if (nmem == NULL) {
205 return 1; // LCOV_EXCL_LINE 225 return 1; // LCOV_EXCL_LINE
206 } else { 226 } else {
207 *mem = nmem; 227 *mem = nmem;

mercurial