From f2c8d1beffe88fd0a8e23ecb350ab22a3259c185 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Mon, 9 May 2016 21:10:45 +0200 Subject: Add functions for safe memory allocation --- src/gensvm_memory.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/gensvm_memory.c (limited to 'src/gensvm_memory.c') diff --git a/src/gensvm_memory.c b/src/gensvm_memory.c new file mode 100644 index 0000000..529ef79 --- /dev/null +++ b/src/gensvm_memory.c @@ -0,0 +1,101 @@ +/** + * @file gensvm_memory.c + * @author Gertjan van den Burg + * @date May, 2016 + * @brief Utility functions for memory allocation + * + */ + +#include "globals.h" // imports gensvm_memory.h +#include "gensvm_util.h" + +/** + * @brief Wrapper for calloc() which warns when allocation fails + * + * @details + * This is a wrapper function around calloc from . It tries to + * allocate the requested memory and checks if the memory was correctly + * allocated. If not, an error is printed using err(), which describes the + * file and linenumber and size failed to allocate. After this, the program + * exits. See also the defines in gensvm_memory.h. + * + * @note + * This function should not be used directly. Calloc() should be used. + * + * @param[in] file filename used for error printing + * @param[in] line line number used for error printing + * @param[in] size the size to allocate + * @param[in] typesize the size of the type to allocate + * @return the pointer to the memory allocated + */ +void *mycalloc(const char *file, int line, unsigned long size, + size_t typesize) +{ + void *ptr = calloc(size, typesize); + + if (!ptr) { + err("Could not allocate memory: %d bytes (%s:%d)\n", + size, file, line); + exit(EXIT_FAILURE); + } + return ptr; +} + +/** + * @brief Wrapper for malloc() which warns when allocation fails + * + * @details + * This is a wrapper function around malloc from . It tries to + * allocate the requested memory and checks if the memory was correctly + * allocated. If not, an error is printed using err(), which describes the + * file and linenumber and size failed to allocate. After this, the program + * exits. See also the defines in gensvm_memory.h. + * + * @note + * This function should not be used directly. Malloc() should be used. + * + * @param[in] file filename used for error printing + * @param[in] line line number used for error printing + * @param[in] size the size to allocate + * @return the pointer to the memory allocated + */ +void *mymalloc(const char *file, int line, unsigned long size) +{ + void *ptr = malloc(size); + if (!ptr) { + err("Could not allocate memory: %d bytes (%s:%d)\n", + size, file, line); + exit(EXIT_FAILURE); + } + return ptr; +} + +/** + * @brief Wrapper for realloc() which warns when allocation fails + * + * @details + * This is a wrapper function around realloc from . It tries to + * reallocate the requested memory and checks if the memory was correctly + * reallocated. If not, an error is printed using err(), which describes the + * file and linenumber and size failed to reallocate. After this, the program + * exits. See also the defines in gensvm_memory.h. + * + * @note + * This function should not be used directly. Realloc() should be used. + * + * @param[in] file filename used for error printing + * @param[in] line line number used for error printing + * @param[in] size the size to allocate + * @param[in] var the pointer to the memory to reallocate + * @return the pointer to the memory reallocated + */ +void *myrealloc(const char *file, int line, unsigned long size, void *var) +{ + void *ptr = realloc(var, size); + if (!ptr) { + err("Could not reallocate memory: %d bytes (%s:%d)\n", + size, file, line); + exit(EXIT_FAILURE); + } + return ptr; +} -- cgit v1.2.3