X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fxalloc.c;fp=src%2Fxalloc.c;h=80699f9bc84cfa1c121a0ebdea37a11a5a8d0972;hb=ff03bb9b0a744530e1145fef656644987a10d62d;hp=0000000000000000000000000000000000000000;hpb=56c51e94a620dd91eeb510176b9c970af9a9a372;p=tinc diff --git a/src/xalloc.c b/src/xalloc.c new file mode 100644 index 00000000..80699f9b --- /dev/null +++ b/src/xalloc.c @@ -0,0 +1,77 @@ +/* + xalloc.c -- safe memory allocation functions + + Copyright (C) 2003-2004 Guus Sliepen , + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id$ +*/ + +#include +#include +#include + +#include "xalloc.h" + +static void xalloc_fail(void) { + fprintf(stderr, "Memory exhausted\n"); + exit(1); +} + +void (*xalloc_fail_func)(void) = xalloc_fail; + +void *xmalloc(size_t n) { + void *p; + + p = malloc(n); + + if(!p) + xalloc_fail_func(); + + return p; +} + +void *xrealloc(void *p, size_t n) { + p = realloc(p, n); + + if(!p) + xalloc_fail_func(); + + return p; +} + +void *xcalloc(size_t n, size_t s) { + void *p; + + p = calloc(n, s); + + if(!p) + xalloc_fail_func(); + + return p; +} + +char *xstrdup(const char *s) { + char *p; + + p = strdup(s); + + if(!p) + xalloc_fail_func(); + + return p; +} +