libroot: add reallocarray() from POSIX.1-2024

Change-Id: I75d469ad9a01eb221ca607830589a7794eff71e8
Reviewed-on: https://review.haiku-os.org/c/haiku/+/8512
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
This commit is contained in:
Jérôme Duval 2024-10-29 18:12:22 +01:00 committed by waddlesplash
parent 592a3b6921
commit 30179dd326
3 changed files with 23 additions and 0 deletions

View File

@ -51,6 +51,7 @@ extern void *malloc(size_t size);
extern int posix_memalign(void **_pointer, size_t alignment, size_t size);
extern void *aligned_alloc(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
extern void *realloc(void *oldPointer, size_t newSize);
extern void *reallocarray(void *ptr, size_t nelem, size_t elsize);
/* process termination */
extern void abort(void) __attribute__((noreturn));

View File

@ -28,6 +28,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
qsort.c
radixsort.c
random.c
reallocarray.cpp
realpath.cpp
strfmon.c
strtol.c

View File

@ -0,0 +1,21 @@
/*
* Copyright 2024, Jérôme Duval, jerome.duval@gmail.com
* Distributed under the terms of the MIT License.
*/
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
extern "C" void*
reallocarray(void* ptr, size_t nelem, size_t elsize)
{
if (elsize != 0 && nelem != 0 && nelem > SIZE_MAX / elsize) {
errno = ENOMEM;
return NULL;
}
return realloc(ptr, nelem * elsize);
}