1 ab1569ee 2023-05-06 op /* $OpenBSD: recallocarray.c,v 1.2 2021/03/18 11:16:58 claudio Exp $ */
3 ab1569ee 2023-05-06 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
5 ab1569ee 2023-05-06 op * Permission to use, copy, modify, and distribute this software for any
6 ab1569ee 2023-05-06 op * purpose with or without fee is hereby granted, provided that the above
7 ab1569ee 2023-05-06 op * copyright notice and this permission notice appear in all copies.
9 ab1569ee 2023-05-06 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ab1569ee 2023-05-06 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ab1569ee 2023-05-06 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ab1569ee 2023-05-06 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ab1569ee 2023-05-06 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ab1569ee 2023-05-06 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ab1569ee 2023-05-06 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ab1569ee 2023-05-06 op #include <errno.h>
19 ab1569ee 2023-05-06 op #include <stdlib.h>
20 ab1569ee 2023-05-06 op #include <stdint.h>
21 ab1569ee 2023-05-06 op #include <string.h>
22 ab1569ee 2023-05-06 op #include <unistd.h>
25 ab1569ee 2023-05-06 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
26 ab1569ee 2023-05-06 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
28 ab1569ee 2023-05-06 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
31 ab1569ee 2023-05-06 op * Even though specified in POSIX, the PAGESIZE and PAGE_SIZE
32 ab1569ee 2023-05-06 op * macros have very poor portability. Since we only use this
33 ab1569ee 2023-05-06 op * to avoid free() overhead for small shrinking, simply pick
34 ab1569ee 2023-05-06 op * an arbitrary number.
36 ab1569ee 2023-05-06 op #define getpagesize() (1UL << 12)
39 ab1569ee 2023-05-06 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
41 ab1569ee 2023-05-06 op size_t oldsize, newsize;
44 ab1569ee 2023-05-06 op if (ptr == NULL)
45 ab1569ee 2023-05-06 op return calloc(newnmemb, size);
47 ab1569ee 2023-05-06 op if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
48 ab1569ee 2023-05-06 op newnmemb > 0 && SIZE_MAX / newnmemb < size) {
49 ab1569ee 2023-05-06 op errno = ENOMEM;
52 ab1569ee 2023-05-06 op newsize = newnmemb * size;
54 ab1569ee 2023-05-06 op if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
55 ab1569ee 2023-05-06 op oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
56 ab1569ee 2023-05-06 op errno = EINVAL;
59 ab1569ee 2023-05-06 op oldsize = oldnmemb * size;
62 ab1569ee 2023-05-06 op * Don't bother too much if we're shrinking just a bit,
63 ab1569ee 2023-05-06 op * we do not shrink for series of small steps, oh well.
65 ab1569ee 2023-05-06 op if (newsize <= oldsize) {
66 ab1569ee 2023-05-06 op size_t d = oldsize - newsize;
68 ab1569ee 2023-05-06 op if (d < oldsize / 2 && d < (size_t)getpagesize()) {
69 ab1569ee 2023-05-06 op memset((char *)ptr + newsize, 0, d);
74 ab1569ee 2023-05-06 op newptr = malloc(newsize);
75 ab1569ee 2023-05-06 op if (newptr == NULL)
78 ab1569ee 2023-05-06 op if (newsize > oldsize) {
79 ab1569ee 2023-05-06 op memcpy(newptr, ptr, oldsize);
80 ab1569ee 2023-05-06 op memset((char *)newptr + oldsize, 0, newsize - oldsize);
82 ab1569ee 2023-05-06 op memcpy(newptr, ptr, newsize);
84 ab1569ee 2023-05-06 op explicit_bzero(ptr, oldsize);
87 ab1569ee 2023-05-06 op return newptr;