tadd portable reallocarray - numtools - perform numerical operations on vectors… | |
git clone git://src.adamsgaard.dk/numtools | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 3ff913401e46517fdf53d9e51ff330c0fb4bbda4 | |
parent 46e27e9ab6b642fc1ac57cda2e7eacb43956ee24 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Sat, 11 Sep 2021 20:57:35 +0200 | |
add portable reallocarray | |
Diffstat: | |
M Makefile | 1 + | |
M util.h | 2 ++ | |
A xreallocarray.c | 19 +++++++++++++++++++ | |
3 files changed, 22 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
t@@ -25,6 +25,7 @@ SRC =\ | |
COMPATSRC =\ | |
strnlen.c\ | |
strlcpy.c\ | |
+ xreallocarray.c\ | |
COMPATOBJ = ${COMPATSRC:.c=.o} | |
diff --git a/util.h b/util.h | |
t@@ -17,6 +17,8 @@ size_t strlcpy(char *dst, const char *src, size_t dsize); | |
#undef strnlen | |
size_t strnlen(const char *str, size_t maxlen); | |
+void * xreallocarray(void *m, size_t n, size_t s); | |
+ | |
size_t allocarr(double **arr, const char *str, size_t maxlen); | |
int scannextval(char **str, double *val); | |
void printarr(double *arr, size_t len); | |
diff --git a/xreallocarray.c b/xreallocarray.c | |
t@@ -0,0 +1,19 @@ | |
+#include <err.h> | |
+#include <stdlib.h> | |
+ | |
+void * | |
+xreallocarray(void *m, size_t n, size_t s) | |
+{ | |
+ void *nm; | |
+ | |
+ if (n == 0 || s == 0) { | |
+ free(m); | |
+ return NULL; | |
+ } | |
+ if (s && n > (size_t) - 1 / s) | |
+ err(1, "realloc: overflow"); | |
+ if (!(nm = realloc(m, n * s))) | |
+ err(1, "realloc"); | |
+ | |
+ return nm; | |
+} |