summaryrefslogtreecommitdiff
path: root/memory.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-10-11 08:32:09 +0200
committertomsmeding <tom.smeding@gmail.com>2017-10-11 08:32:09 +0200
commit7549d07933091417b225d094c1648e1382287f93 (patch)
treef533b99745beb808d1cbe226a38e0232076d7b53 /memory.c
Initial
Diffstat (limited to 'memory.c')
-rw-r--r--memory.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/memory.c b/memory.c
new file mode 100644
index 0000000..8734ab2
--- /dev/null
+++ b/memory.c
@@ -0,0 +1,33 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdarg.h>
+#include <assert.h>
+#include "global.h"
+#include "memory.h"
+
+void* check_after_allocation(const char *func,size_t num,size_t sz,void *ptr){
+ if(ptr==NULL){
+ fprintf(stderr,"Allocation failed: %s(%zu * %zuB = %zu)",func,num,sz,num*sz);
+ exit(1);
+ }
+ return ptr;
+}
+
+void* check_after_allocation_str(const char *func,void *ptr){
+ if(ptr==NULL){
+ fprintf(stderr,"Allocation failed: %s()",func);
+ exit(1);
+ }
+ return ptr;
+}
+
+__attribute__((format (printf, 2, 3)))
+int memory_asprintf_wrapper(char **ret,const char *format,...){
+ assert(ret!=NULL);
+ va_list ap;
+ va_start(ap,format);
+ int len=vasprintf(ret,format,ap);
+ va_end(ap);
+ check_after_allocation_str("asprintf",*ret);
+ return len;
+}