aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2017-05-19 18:44:42 +0200
committerDuncaen <mail@duncano.de>2017-05-19 18:44:42 +0200
commitfe3d19bcc46016129cae9ae84d8df815286c2021 (patch)
tree2eb7049b7b19c001ad98617a76a2ed54a5767087
parent332065efb70839ca6e39b55b7dfba2a5f913f7c1 (diff)
downloadlobase-fe3d19bcc46016129cae9ae84d8df815286c2021.tar.gz
lib/libopenbsd: add recallocarray(3)
-rw-r--r--include/stdlib.h3
-rw-r--r--lib/libopenbsd/stdlib/Makefile.inc2
-rw-r--r--lib/libopenbsd/stdlib/recallocarray.c81
3 files changed, 84 insertions, 2 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index dedc357..3828118 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -43,7 +43,8 @@ uint32_t arc4random_uniform(uint32_t);
void arc4random_buf(void *, size_t)
__attribute__((__bounded__ (__string__,1,2)));
char *devname(dev_t, mode_t);
-void *reallocarray(void *optr, size_t nmemb, size_t size);
+void *reallocarray(void *, size_t, size_t);
+void *recallocarray(void *, size_t, size_t, size_t);
int heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
int radixsort(const unsigned char **, int, const unsigned char *, unsigned);
diff --git a/lib/libopenbsd/stdlib/Makefile.inc b/lib/libopenbsd/stdlib/Makefile.inc
index e49c320..cb744b7 100644
--- a/lib/libopenbsd/stdlib/Makefile.inc
+++ b/lib/libopenbsd/stdlib/Makefile.inc
@@ -4,6 +4,6 @@
VPATH+= ${LIBCSRCDIR}/stdlib
ifneq ($(HAVE_REALLOCARRAY),1)
-SRCS+= reallocarray.c
+SRCS+= reallocarray.c recallocarray.c
MAN+= malloc.3
endif
diff --git a/lib/libopenbsd/stdlib/recallocarray.c b/lib/libopenbsd/stdlib/recallocarray.c
new file mode 100644
index 0000000..a2f37fe
--- /dev/null
+++ b/lib/libopenbsd/stdlib/recallocarray.c
@@ -0,0 +1,81 @@
+/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */
+/*
+ * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
+
+void *
+recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
+{
+ size_t oldsize, newsize;
+ void *newptr;
+
+ if (ptr == NULL)
+ return calloc(newnmemb, size);
+
+ if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+ newnmemb > 0 && SIZE_MAX / newnmemb < size) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ newsize = newnmemb * size;
+
+ if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+ oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
+ errno = EINVAL;
+ return NULL;
+ }
+ oldsize = oldnmemb * size;
+
+ /*
+ * Don't bother too much if we're shrinking just a bit,
+ * we do not shrink for series of small steps, oh well.
+ */
+ if (newsize <= oldsize) {
+ size_t d = oldsize - newsize;
+
+ if (d < oldsize / 2 && d < getpagesize()) {
+ memset((char *)ptr + newsize, 0, d);
+ return ptr;
+ }
+ }
+
+ newptr = malloc(newsize);
+ if (newptr == NULL)
+ return NULL;
+
+ if (newsize > oldsize) {
+ memcpy(newptr, ptr, oldsize);
+ memset((char *)newptr + oldsize, 0, newsize - oldsize);
+ } else
+ memcpy(newptr, ptr, newsize);
+
+ explicit_bzero(ptr, oldsize);
+ free(ptr);
+
+ return newptr;
+}
+DEF_WEAK(recallocarray);