Skip to content

Support reallocarray(3) #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions h_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,17 @@ EXPORT void *h_realloc(void *old, size_t size) {
return new;
}

EXPORT void *h_reallocarray(void *ptr, size_t nmemb, size_t size) {
size_t bytes;

if (unlikely(__builtin_mul_overflow(nmemb, size, &bytes))) {
errno = ENOMEM;
return NULL;
}

return h_realloc(ptr, bytes);
}

EXPORT int h_posix_memalign(void **memptr, size_t alignment, size_t size) {
return alloc_aligned(memptr, alignment, size, sizeof(void *));
}
Expand Down
2 changes: 2 additions & 0 deletions include/h_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
#define h_malloc malloc
#define h_calloc calloc
#define h_realloc realloc
#define h_reallocarray reallocarray
#define h_aligned_alloc aligned_alloc
#define h_free free

Expand Down Expand Up @@ -51,6 +52,7 @@ extern "C" {
__attribute__((malloc)) __attribute__((alloc_size(1))) void *h_malloc(size_t size);
__attribute__((malloc)) __attribute__((alloc_size(1, 2))) void *h_calloc(size_t nmemb, size_t size);
__attribute__((alloc_size(2))) void *h_realloc(void *ptr, size_t size);
__attribute__((alloc_size(2, 3))) void *h_reallocarray(void *ptr, size_t nmemb, size_t size);
__attribute__((malloc)) __attribute__((alloc_size(2))) __attribute__((alloc_align(1)))
void *h_aligned_alloc(size_t alignment, size_t size);
void h_free(void *ptr);
Expand Down