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