Blame


1 ab1569ee 2023-05-06 op /* $OpenBSD: recallocarray.c,v 1.2 2021/03/18 11:16:58 claudio Exp $ */
2 ab1569ee 2023-05-06 op /*
3 ab1569ee 2023-05-06 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
4 ab1569ee 2023-05-06 op *
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.
8 ab1569ee 2023-05-06 op *
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.
16 ab1569ee 2023-05-06 op */
17 ab1569ee 2023-05-06 op
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>
23 ab1569ee 2023-05-06 op
24 ab1569ee 2023-05-06 op /*
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
27 ab1569ee 2023-05-06 op */
28 ab1569ee 2023-05-06 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
29 ab1569ee 2023-05-06 op
30 ab1569ee 2023-05-06 op /*
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.
35 ab1569ee 2023-05-06 op */
36 ab1569ee 2023-05-06 op #define getpagesize() (1UL << 12)
37 ab1569ee 2023-05-06 op
38 ab1569ee 2023-05-06 op void *
39 ab1569ee 2023-05-06 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
40 ab1569ee 2023-05-06 op {
41 ab1569ee 2023-05-06 op size_t oldsize, newsize;
42 ab1569ee 2023-05-06 op void *newptr;
43 ab1569ee 2023-05-06 op
44 ab1569ee 2023-05-06 op if (ptr == NULL)
45 ab1569ee 2023-05-06 op return calloc(newnmemb, size);
46 ab1569ee 2023-05-06 op
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;
50 ab1569ee 2023-05-06 op return NULL;
51 ab1569ee 2023-05-06 op }
52 ab1569ee 2023-05-06 op newsize = newnmemb * size;
53 ab1569ee 2023-05-06 op
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;
57 ab1569ee 2023-05-06 op return NULL;
58 ab1569ee 2023-05-06 op }
59 ab1569ee 2023-05-06 op oldsize = oldnmemb * size;
60 ab1569ee 2023-05-06 op
61 ab1569ee 2023-05-06 op /*
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.
64 ab1569ee 2023-05-06 op */
65 ab1569ee 2023-05-06 op if (newsize <= oldsize) {
66 ab1569ee 2023-05-06 op size_t d = oldsize - newsize;
67 ab1569ee 2023-05-06 op
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);
70 ab1569ee 2023-05-06 op return ptr;
71 ab1569ee 2023-05-06 op }
72 ab1569ee 2023-05-06 op }
73 ab1569ee 2023-05-06 op
74 ab1569ee 2023-05-06 op newptr = malloc(newsize);
75 ab1569ee 2023-05-06 op if (newptr == NULL)
76 ab1569ee 2023-05-06 op return NULL;
77 ab1569ee 2023-05-06 op
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);
81 ab1569ee 2023-05-06 op } else
82 ab1569ee 2023-05-06 op memcpy(newptr, ptr, newsize);
83 ab1569ee 2023-05-06 op
84 ab1569ee 2023-05-06 op explicit_bzero(ptr, oldsize);
85 ab1569ee 2023-05-06 op free(ptr);
86 ab1569ee 2023-05-06 op
87 ab1569ee 2023-05-06 op return newptr;
88 ab1569ee 2023-05-06 op }