X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fxalloc.h;fp=src%2Fxalloc.h;h=db2e531549b7db13b94ef36e24a0bf9ae787f6b2;hb=ff03bb9b0a744530e1145fef656644987a10d62d;hp=0000000000000000000000000000000000000000;hpb=56c51e94a620dd91eeb510176b9c970af9a9a372;p=tinc diff --git a/src/xalloc.h b/src/xalloc.h new file mode 100644 index 00000000..db2e5315 --- /dev/null +++ b/src/xalloc.h @@ -0,0 +1,37 @@ +/* + xalloc.h -- 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$ +*/ + +#ifndef __XALLOC_H__ +#define __XALLOC_H__ + +#define new(object) ({(object) = xmalloc(sizeof *(object));}) +#define dim(object, count) ({(object) = xmalloc(sizeof *(object) * (count));}) +#define redim(object, count) ({(object) = xrealloc((object), sizeof *(object) * (count));}) +#define copy(object) ({typeof(object) _copy; *(_copy = xmalloc(sizeof *(object))) = *(object); _copy;}) +#define clear(object) ({memset((object), 0, sizeof *(object));}) +#define replace(string, replacement) ({if(string) free(string); (string) = (replacement) ? xstrdup(replacement) : NULL;}) + +void *xmalloc(size_t n) __attribute__ ((__malloc__)); +void *xrealloc(void *p, size_t n) __attribute__ ((__malloc__)); +char *xstrdup(const char *s) __attribute__ ((__malloc__)); + +#endif