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