2 0d6fb46a 2022-01-09 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
4 0d6fb46a 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 0d6fb46a 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 0d6fb46a 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 0d6fb46a 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0d6fb46a 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0d6fb46a 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0d6fb46a 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0d6fb46a 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0d6fb46a 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0d6fb46a 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0d6fb46a 2022-01-09 op #include "compat.h"
19 0d6fb46a 2022-01-09 op #include <errno.h>
20 0d6fb46a 2022-01-09 op #include <stdlib.h>
21 0d6fb46a 2022-01-09 op #include <stdint.h>
22 0d6fb46a 2022-01-09 op #include <string.h>
23 0d6fb46a 2022-01-09 op #include <unistd.h>
26 0d6fb46a 2022-01-09 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
27 0d6fb46a 2022-01-09 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
29 0d6fb46a 2022-01-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
32 0d6fb46a 2022-01-09 op * Even though specified in POSIX, the PAGESIZE and PAGE_SIZE
33 0d6fb46a 2022-01-09 op * macros have very poor portability. Since we only use this
34 0d6fb46a 2022-01-09 op * to avoid free() overhead for small shrinking, simply pick
35 0d6fb46a 2022-01-09 op * an arbitrary number.
37 0d6fb46a 2022-01-09 op #define getpagesize() (1UL << 12)
40 0d6fb46a 2022-01-09 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
42 0d6fb46a 2022-01-09 op size_t oldsize, newsize;
45 0d6fb46a 2022-01-09 op if (ptr == NULL)
46 0d6fb46a 2022-01-09 op return calloc(newnmemb, size);
48 0d6fb46a 2022-01-09 op if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
49 0d6fb46a 2022-01-09 op newnmemb > 0 && SIZE_MAX / newnmemb < size) {
50 0d6fb46a 2022-01-09 op errno = ENOMEM;
53 0d6fb46a 2022-01-09 op newsize = newnmemb * size;
55 0d6fb46a 2022-01-09 op if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
56 0d6fb46a 2022-01-09 op oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
57 0d6fb46a 2022-01-09 op errno = EINVAL;
60 0d6fb46a 2022-01-09 op oldsize = oldnmemb * size;
63 0d6fb46a 2022-01-09 op * Don't bother too much if we're shrinking just a bit,
64 0d6fb46a 2022-01-09 op * we do not shrink for series of small steps, oh well.
66 0d6fb46a 2022-01-09 op if (newsize <= oldsize) {
67 0d6fb46a 2022-01-09 op size_t d = oldsize - newsize;
69 0d6fb46a 2022-01-09 op if (d < oldsize / 2 && d < getpagesize()) {
70 0d6fb46a 2022-01-09 op memset((char *)ptr + newsize, 0, d);
75 0d6fb46a 2022-01-09 op newptr = malloc(newsize);
76 0d6fb46a 2022-01-09 op if (newptr == NULL)
79 0d6fb46a 2022-01-09 op if (newsize > oldsize) {
80 0d6fb46a 2022-01-09 op memcpy(newptr, ptr, oldsize);
81 0d6fb46a 2022-01-09 op memset((char *)newptr + oldsize, 0, newsize - oldsize);
83 0d6fb46a 2022-01-09 op memcpy(newptr, ptr, newsize);
85 0d6fb46a 2022-01-09 op explicit_bzero(ptr, oldsize);
88 0d6fb46a 2022-01-09 op return newptr;