aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGertjan van den Burg <gertjanvandenburg@gmail.com>2016-05-09 21:10:45 +0200
committerGertjan van den Burg <gertjanvandenburg@gmail.com>2016-05-09 21:10:45 +0200
commitf2c8d1beffe88fd0a8e23ecb350ab22a3259c185 (patch)
tree15c35b70693d4033f0db8b4830b137e24d541d4e /src
parentuse stdbool instead of bool typedef (diff)
downloadgensvm-f2c8d1beffe88fd0a8e23ecb350ab22a3259c185.tar.gz
gensvm-f2c8d1beffe88fd0a8e23ecb350ab22a3259c185.zip
Add functions for safe memory allocation
Diffstat (limited to 'src')
-rw-r--r--src/GenSVMtrain.c1
-rw-r--r--src/gensvm_init.c1
-rw-r--r--src/gensvm_io.c1
-rw-r--r--src/gensvm_memory.c101
-rw-r--r--src/gensvm_train.c1
5 files changed, 105 insertions, 0 deletions
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"