Try reallocating memory, and return NULL if failed. * '*usable' is set to the usable size if non NULL. */
| 198 | /* Try reallocating memory, and return NULL if failed. |
| 199 | * '*usable' is set to the usable size if non NULL. */ |
| 200 | void *ztryrealloc_usable(void *ptr, size_t size, size_t *usable) { |
| 201 | ASSERT_NO_SIZE_OVERFLOW(size); |
| 202 | #ifndef HAVE_MALLOC_SIZE |
| 203 | void *realptr; |
| 204 | #endif |
| 205 | size_t oldsize; |
| 206 | void *newptr; |
| 207 | |
| 208 | /* not allocating anything, just redirect to free. */ |
| 209 | if (size == 0 && ptr != NULL) { |
| 210 | zfree(ptr); |
| 211 | if (usable) *usable = 0; |
| 212 | return NULL; |
| 213 | } |
| 214 | /* Not freeing anything, just redirect to malloc. */ |
| 215 | if (ptr == NULL) |
| 216 | return ztrymalloc_usable(size, usable); |
| 217 | |
| 218 | #ifdef HAVE_MALLOC_SIZE |
| 219 | oldsize = zmalloc_size(ptr); |
| 220 | newptr = realloc(ptr,size); |
| 221 | if (newptr == NULL) { |
| 222 | if (usable) *usable = 0; |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | update_zmalloc_stat_free(oldsize); |
| 227 | size = zmalloc_size(newptr); |
| 228 | update_zmalloc_stat_alloc(size); |
| 229 | if (usable) *usable = size; |
| 230 | return newptr; |
| 231 | #else |
| 232 | realptr = (char*)ptr-PREFIX_SIZE; |
| 233 | oldsize = *((size_t*)realptr); |
| 234 | newptr = realloc(realptr,size+PREFIX_SIZE); |
| 235 | if (newptr == NULL) { |
| 236 | if (usable) *usable = 0; |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | *((size_t*)newptr) = size; |
| 241 | update_zmalloc_stat_free(oldsize); |
| 242 | update_zmalloc_stat_alloc(size); |
| 243 | if (usable) *usable = size; |
| 244 | return (char*)newptr+PREFIX_SIZE; |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | /* Reallocate memory and zero it or panic */ |
| 249 | void *zrealloc(void *ptr, size_t size) { |
no test coverage detected