diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -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); +} |