From 783f1271992af3bb6b2413339514e283ad819f9f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 28 Jul 2021 22:34:51 -0700 Subject: Improved memory allocation/error checking helper functions. Also reworked stderr so that it prints to the console on exit. --- utils.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 utils.c (limited to 'utils.c') diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..1444894 --- /dev/null +++ b/utils.c @@ -0,0 +1,57 @@ +// +// utils.c +// Copyright 2021 Bruce Hill +// Released under the MIT license with the Commons Clause +// +// This file contains implementations of some convenience functions for more +// easily error checking. +// + +#include +#include +#include + +// +// If the given argument is nonnegative, print the error message and exit with +// failure. Otherwise, return the given argument. +// +int check_nonnegative(int negative_err, const char *err_msg, ...) +{ + if (negative_err < 0) { + va_list args; + va_start(args, err_msg); + verr(EXIT_FAILURE, err_msg, args); + va_end(args); + } + return negative_err; +} + +// +// If the given argument is NULL, print the error message and exit with +// failure. Otherwise return the given argument. +// +void *check_nonnull(void *p, const char *err_msg, ...) +{ + if (p == NULL) { + va_list args; + va_start(args, err_msg); + verr(EXIT_FAILURE, err_msg, args); + va_end(args); + } + return p; +} + +// +// For a given pointer to a memory-allocated pointer, free its memory and set +// the pointer to NULL. (This is a safer alternative to free() that +// automatically NULLs out the pointer so it can't be used after freeing) +// +void delete(void *p) +{ + if (*(void**)p != NULL) { + free(*(void**)p); + *(void**)p = NULL; + } +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 -- cgit v1.2.3