recallocarray.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 pgoyette /* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */
2 1.1.1.1.2.2 pgoyette /*
3 1.1.1.1.2.2 pgoyette * Copyright (c) 2008, 2017 Otto Moerbeek <otto (at) drijf.net>
4 1.1.1.1.2.2 pgoyette *
5 1.1.1.1.2.2 pgoyette * Permission to use, copy, modify, and distribute this software for any
6 1.1.1.1.2.2 pgoyette * purpose with or without fee is hereby granted, provided that the above
7 1.1.1.1.2.2 pgoyette * copyright notice and this permission notice appear in all copies.
8 1.1.1.1.2.2 pgoyette *
9 1.1.1.1.2.2 pgoyette * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1.1.1.2.2 pgoyette * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1.1.1.2.2 pgoyette * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1.1.1.2.2 pgoyette * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1.1.1.2.2 pgoyette * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1.1.1.2.2 pgoyette * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1.1.1.2.2 pgoyette * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1.1.1.2.2 pgoyette */
17 1.1.1.1.2.2 pgoyette
18 1.1.1.1.2.2 pgoyette #include <errno.h>
19 1.1.1.1.2.2 pgoyette #include <stdlib.h>
20 1.1.1.1.2.2 pgoyette #include <stdint.h>
21 1.1.1.1.2.2 pgoyette #include <string.h>
22 1.1.1.1.2.2 pgoyette #include <unistd.h>
23 1.1.1.1.2.2 pgoyette
24 1.1.1.1.2.2 pgoyette #include "compat.h"
25 1.1.1.1.2.2 pgoyette
26 1.1.1.1.2.2 pgoyette /*
27 1.1.1.1.2.2 pgoyette * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
28 1.1.1.1.2.2 pgoyette * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
29 1.1.1.1.2.2 pgoyette */
30 1.1.1.1.2.2 pgoyette #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
31 1.1.1.1.2.2 pgoyette
32 1.1.1.1.2.2 pgoyette void *
33 1.1.1.1.2.2 pgoyette recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
34 1.1.1.1.2.2 pgoyette {
35 1.1.1.1.2.2 pgoyette size_t oldsize, newsize;
36 1.1.1.1.2.2 pgoyette void *newptr;
37 1.1.1.1.2.2 pgoyette
38 1.1.1.1.2.2 pgoyette if (ptr == NULL)
39 1.1.1.1.2.2 pgoyette return calloc(newnmemb, size);
40 1.1.1.1.2.2 pgoyette
41 1.1.1.1.2.2 pgoyette if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
42 1.1.1.1.2.2 pgoyette newnmemb > 0 && SIZE_MAX / newnmemb < size) {
43 1.1.1.1.2.2 pgoyette errno = ENOMEM;
44 1.1.1.1.2.2 pgoyette return NULL;
45 1.1.1.1.2.2 pgoyette }
46 1.1.1.1.2.2 pgoyette newsize = newnmemb * size;
47 1.1.1.1.2.2 pgoyette
48 1.1.1.1.2.2 pgoyette if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
49 1.1.1.1.2.2 pgoyette oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
50 1.1.1.1.2.2 pgoyette errno = EINVAL;
51 1.1.1.1.2.2 pgoyette return NULL;
52 1.1.1.1.2.2 pgoyette }
53 1.1.1.1.2.2 pgoyette oldsize = oldnmemb * size;
54 1.1.1.1.2.2 pgoyette
55 1.1.1.1.2.2 pgoyette /*
56 1.1.1.1.2.2 pgoyette * Don't bother too much if we're shrinking just a bit,
57 1.1.1.1.2.2 pgoyette * we do not shrink for series of small steps, oh well.
58 1.1.1.1.2.2 pgoyette */
59 1.1.1.1.2.2 pgoyette if (newsize <= oldsize) {
60 1.1.1.1.2.2 pgoyette size_t d = oldsize - newsize;
61 1.1.1.1.2.2 pgoyette
62 1.1.1.1.2.2 pgoyette if (d < oldsize / 2 && d < (size_t)getpagesize()) {
63 1.1.1.1.2.2 pgoyette memset((char *)ptr + newsize, 0, d);
64 1.1.1.1.2.2 pgoyette return ptr;
65 1.1.1.1.2.2 pgoyette }
66 1.1.1.1.2.2 pgoyette }
67 1.1.1.1.2.2 pgoyette
68 1.1.1.1.2.2 pgoyette newptr = malloc(newsize);
69 1.1.1.1.2.2 pgoyette if (newptr == NULL)
70 1.1.1.1.2.2 pgoyette return NULL;
71 1.1.1.1.2.2 pgoyette
72 1.1.1.1.2.2 pgoyette if (newsize > oldsize) {
73 1.1.1.1.2.2 pgoyette memcpy(newptr, ptr, oldsize);
74 1.1.1.1.2.2 pgoyette memset((char *)newptr + oldsize, 0, newsize - oldsize);
75 1.1.1.1.2.2 pgoyette } else
76 1.1.1.1.2.2 pgoyette memcpy(newptr, ptr, newsize);
77 1.1.1.1.2.2 pgoyette
78 1.1.1.1.2.2 pgoyette explicit_bzero(ptr, oldsize);
79 1.1.1.1.2.2 pgoyette free(ptr);
80 1.1.1.1.2.2 pgoyette
81 1.1.1.1.2.2 pgoyette return newptr;
82 1.1.1.1.2.2 pgoyette }
83