diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2016-05-09 21:10:45 +0200 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2016-05-09 21:10:45 +0200 |
| commit | f2c8d1beffe88fd0a8e23ecb350ab22a3259c185 (patch) | |
| tree | 15c35b70693d4033f0db8b4830b137e24d541d4e | |
| parent | use stdbool instead of bool typedef (diff) | |
| download | gensvm-f2c8d1beffe88fd0a8e23ecb350ab22a3259c185.tar.gz gensvm-f2c8d1beffe88fd0a8e23ecb350ab22a3259c185.zip | |
Add functions for safe memory allocation
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/gensvm_memory.h | 26 | ||||
| -rw-r--r-- | include/globals.h | 11 | ||||
| -rw-r--r-- | src/GenSVMtrain.c | 1 | ||||
| -rw-r--r-- | src/gensvm_init.c | 1 | ||||
| -rw-r--r-- | src/gensvm_io.c | 1 | ||||
| -rw-r--r-- | src/gensvm_memory.c | 101 | ||||
| -rw-r--r-- | src/gensvm_train.c | 1 |
8 files changed, 137 insertions, 7 deletions
@@ -36,6 +36,7 @@ lib/libgensvm.a: \ src/gensvm_kernel.o \ src/gensvm_lapack.o \ src/gensvm_matrix.o \ + src/gensvm_memory.o \ src/gensvm_pred.o \ src/gensvm_strutil.o \ src/gensvm_sv.o \ @@ -49,6 +50,7 @@ lib/libgensvm.a: \ src/gensvm_init.o \ src/gensvm_io.o \ src/gensvm_matrix.o \ + src/gensvm_memory.o \ src/gensvm_kernel.o \ src/gensvm_lapack.o \ src/gensvm_pred.o \ diff --git a/include/gensvm_memory.h b/include/gensvm_memory.h new file mode 100644 index 0000000..bc4aae9 --- /dev/null +++ b/include/gensvm_memory.h @@ -0,0 +1,26 @@ +/** + * @file gensvm_memory.h + * @author Gertjan van den Burg + * @date May, 2016 + * @brief Global definitions + * + */ + +#ifndef GENSVM_MEMORY_H +#define GENSVM_MEMORY_H + +#define Calloc(type, size) \ + mycalloc(__FILE__, __LINE__, size, sizeof(type)) +#define Malloc(type, size) \ + mymalloc(__FILE__, __LINE__, (size)*sizeof(type)) +#define Realloc(var, type, size) \ + myrealloc(__FILE__, __LINE__, (size)*sizeof(type), var) +#define Memset(var, type, size) \ + memset(var, 0, (size)*sizeof(type)) + +void *mycalloc(const char *file, int line, unsigned long size, + size_t typesize); +void *mymalloc(const char *file, int line, unsigned long size); +void *myrealloc(const char *file, int line, unsigned long size, void *var); + +#endif diff --git a/include/globals.h b/include/globals.h index eab1d9f..becde35 100644 --- a/include/globals.h +++ b/include/globals.h @@ -6,9 +6,8 @@ * * @details * This header file contains defines and includes which are used in many - * parts of the program. Most notable are the Calloc, Malloc and Memset - * defines, which are commonly used to allocate memory. These functions - * are shorthands for their lowercase counterparts. + * parts of the program. Most notably, it includes the gensvm_memory.h header + * which defines functions for safe memory allocation. * * Furthermore, a maximum and minimum function are defined here. These * functions have their own include guards, to ensure potential linked @@ -24,11 +23,9 @@ #include <stdbool.h> #include <string.h> -#define MAX_LINE_LENGTH 1024 +#include "gensvm_memory.h" -#define Calloc(type, n) (type *)calloc((n), sizeof(type)) -#define Malloc(type, n) (type *)malloc((n)*sizeof(type)) -#define Memset(var, type, n) memset(var, 0, (n)*sizeof(type)) +#define MAX_LINE_LENGTH 1024 #ifndef MIN_MAX_DEFINE #define MIN_MAX_DEFINE diff --git a/src/GenSVMtrain.c b/src/GenSVMtrain.c index b9dc250..3bb9c09 100644 --- a/src/GenSVMtrain.c +++ b/src/GenSVMtrain.c @@ -14,6 +14,7 @@ #include <time.h> #include <math.h> +#include "globals.h" #include "libGenSVM.h" #include "gensvm.h" #include "gensvm_io.h" diff --git a/src/gensvm_init.c b/src/gensvm_init.c index 228e9fe..3b3f130 100644 --- a/src/gensvm_init.c +++ b/src/gensvm_init.c @@ -15,6 +15,7 @@ #include <math.h> +#include "globals.h" #include "gensvm.h" #include "gensvm_init.h" diff --git a/src/gensvm_io.c b/src/gensvm_io.c index d81f19b..c4798d8 100644 --- a/src/gensvm_io.c +++ b/src/gensvm_io.c @@ -10,6 +10,7 @@ * */ +#include "globals.h" #include "gensvm.h" #include "gensvm_io.h" #include "gensvm_matrix.h" 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 <stdlib.h>. 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 <stdlib.h>. 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 <stdlib.h>. 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; +} diff --git a/src/gensvm_train.c b/src/gensvm_train.c index 8c32809..371970a 100644 --- a/src/gensvm_train.c +++ b/src/gensvm_train.c @@ -13,6 +13,7 @@ #include <math.h> #include <cblas.h> +#include "globals.h" #include "libGenSVM.h" #include "gensvm.h" #include "gensvm_lapack.h" |
