summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-08-18 20:52:28 +0200
committertomsmeding <tom.smeding@gmail.com>2016-08-18 20:52:28 +0200
commit2c30522aa65126ebacfd52f7b38a2e24682d7065 (patch)
tree12baf2dfe17cc86f3c9711a700f6f9bbd3764275 /util.c
parent235ffca9db0c2ba0d1ecbc79bdfb3dcdfca939c7 (diff)
Second
Diffstat (limited to 'util.c')
-rw-r--r--util.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..d6890d6
--- /dev/null
+++ b/util.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+
+char* copystring(const char *s){
+ size_t len=strlen(s);
+ char *s2=malloc(len+1,char);
+ memcpy(s2,s,len+1);
+ return s2;
+}
+
+char* copybufasstring(const char *b,int length){
+ char *s=malloc(length+1,char);
+ memcpy(s,b,length);
+ s[length]='\0';
+ return s;
+}
+
+
+__attribute__((noreturn)) void outofmem(void){
+ fprintf(stderr,"OUT OF MEMORY!\n");
+ exit(2);
+}
+
+
+void* malloccheck(size_t n){
+ void *p=mallocreal(n);
+ if(!p)outofmem();
+ return p;
+}
+
+void* calloccheck(size_t n,size_t s){
+ void *p=callocreal(n,s);
+ if(!p)outofmem();
+ return p;
+}
+
+void* realloccheck(void *p,size_t n){
+ p=reallocreal(p,n);
+ if(!p)outofmem();
+ return p;
+}
+
+
+
+#undef malloc
+#undef calloc
+#undef realloc
+
+void* mallocreal(size_t n){
+ return malloc(n);
+}
+
+void* callocreal(size_t n,size_t s){
+ return calloc(n,s);
+}
+
+void* reallocreal(void *p,size_t n){
+ return realloc(p,n);
+}