Blame


1 0d6fb46a 2022-01-09 op /*
2 0d6fb46a 2022-01-09 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
3 0d6fb46a 2022-01-09 op *
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.
7 0d6fb46a 2022-01-09 op *
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.
15 0d6fb46a 2022-01-09 op */
16 0d6fb46a 2022-01-09 op
17 0d6fb46a 2022-01-09 op #include "compat.h"
18 0d6fb46a 2022-01-09 op
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>
24 0d6fb46a 2022-01-09 op
25 0d6fb46a 2022-01-09 op /*
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
28 0d6fb46a 2022-01-09 op */
29 0d6fb46a 2022-01-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
30 0d6fb46a 2022-01-09 op
31 0d6fb46a 2022-01-09 op /*
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.
36 0d6fb46a 2022-01-09 op */
37 0d6fb46a 2022-01-09 op #define getpagesize() (1UL << 12)
38 0d6fb46a 2022-01-09 op
39 0d6fb46a 2022-01-09 op void *
40 0d6fb46a 2022-01-09 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
41 0d6fb46a 2022-01-09 op {
42 0d6fb46a 2022-01-09 op size_t oldsize, newsize;
43 0d6fb46a 2022-01-09 op void *newptr;
44 0d6fb46a 2022-01-09 op
45 0d6fb46a 2022-01-09 op if (ptr == NULL)
46 0d6fb46a 2022-01-09 op return calloc(newnmemb, size);
47 0d6fb46a 2022-01-09 op
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;
51 0d6fb46a 2022-01-09 op return NULL;
52 0d6fb46a 2022-01-09 op }
53 0d6fb46a 2022-01-09 op newsize = newnmemb * size;
54 0d6fb46a 2022-01-09 op
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;
58 0d6fb46a 2022-01-09 op return NULL;
59 0d6fb46a 2022-01-09 op }
60 0d6fb46a 2022-01-09 op oldsize = oldnmemb * size;
61 0d6fb46a 2022-01-09 op
62 0d6fb46a 2022-01-09 op /*
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.
65 0d6fb46a 2022-01-09 op */
66 0d6fb46a 2022-01-09 op if (newsize <= oldsize) {
67 0d6fb46a 2022-01-09 op size_t d = oldsize - newsize;
68 0d6fb46a 2022-01-09 op
69 0d6fb46a 2022-01-09 op if (d < oldsize / 2 && d < getpagesize()) {
70 0d6fb46a 2022-01-09 op memset((char *)ptr + newsize, 0, d);
71 0d6fb46a 2022-01-09 op return ptr;
72 0d6fb46a 2022-01-09 op }
73 0d6fb46a 2022-01-09 op }
74 0d6fb46a 2022-01-09 op
75 0d6fb46a 2022-01-09 op newptr = malloc(newsize);
76 0d6fb46a 2022-01-09 op if (newptr == NULL)
77 0d6fb46a 2022-01-09 op return NULL;
78 0d6fb46a 2022-01-09 op
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);
82 0d6fb46a 2022-01-09 op } else
83 0d6fb46a 2022-01-09 op memcpy(newptr, ptr, newsize);
84 0d6fb46a 2022-01-09 op
85 0d6fb46a 2022-01-09 op explicit_bzero(ptr, oldsize);
86 0d6fb46a 2022-01-09 op free(ptr);
87 0d6fb46a 2022-01-09 op
88 0d6fb46a 2022-01-09 op return newptr;
89 0d6fb46a 2022-01-09 op }