numtools

perform numerical operations on vectors and matrices in unix pipes
git clone git://src.adamsgaard.dk/numtools # fast
git clone https://src.adamsgaard.dk/numtools.git # slow
Log | Files | Refs | README | LICENSE Back to index

commit 3ff913401e46517fdf53d9e51ff330c0fb4bbda4
parent 46e27e9ab6b642fc1ac57cda2e7eacb43956ee24
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Sat, 11 Sep 2021 20:57:35 +0200

add portable reallocarray

Diffstat:
MMakefile | 1+
Mutil.h | 2++
Axreallocarray.c | 19+++++++++++++++++++
3 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,6 +25,7 @@ SRC =\ COMPATSRC =\ strnlen.c\ strlcpy.c\ + xreallocarray.c\ COMPATOBJ = ${COMPATSRC:.c=.o} diff --git a/util.h b/util.h @@ -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 @@ -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; +}