| 384 | |
| 385 | memcpy(new_ptr, ptr, BAN::Math::min(old_alloc_size, size)); |
| 386 | |
| 387 | free(ptr); |
| 388 | |
| 389 | return new_ptr; |
| 390 | } |
| 391 | |
| 392 | void* calloc(size_t nmemb, size_t size) |
| 393 | { |
| 394 | if (BAN::Math::will_multiplication_overflow(nmemb, size)) |
| 395 | { |
| 396 | errno = ENOMEM; |
| 397 | return nullptr; |
| 398 | } |
| 399 | |
| 400 | const size_t total = nmemb * size; |
| 401 | |
| 402 | void* ptr = malloc(total); |
| 403 | if (ptr == nullptr) |
| 404 | return nullptr; |
| 405 | |
| 406 | if (total < s_mmap_threshold) |
| 407 | memset(ptr, 0, total); |
| 408 | return ptr; |
| 409 | } |
| 410 | |
| 411 | void* reallocarray(void* ptr, size_t nmemb, size_t size) |
| 412 | { |
| 413 | if (BAN::Math::will_multiplication_overflow(nmemb, size)) |
| 414 | { |
| 415 | errno = ENOMEM; |
| 416 | return nullptr; |
| 417 | } |
| 418 | |
| 419 | return realloc(ptr, nmemb * size); |
| 420 | } |
| 421 | |
| 422 | void* aligned_alloc(size_t alignment, size_t size) |
| 423 | { |
no test coverage detected