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