2 xalloc.h -- malloc and related fuctions with out of memory checking
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc., Foundation,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef __MESHLINK_XALLOC_H__
21 #define __MESHLINK_XALLOC_H__
23 static inline void *xmalloc(size_t n) __attribute__((__malloc__));
24 static inline void *xmalloc(size_t n) {
31 static inline void *xzalloc(size_t n) __attribute__((__malloc__));
32 static inline void *xzalloc(size_t n) {
33 void *p = calloc(1, n);
39 static inline void *xrealloc(void *p, size_t n) {
46 static inline char *xstrdup(const char *s) __attribute__((__malloc__));
47 static inline char *xstrdup(const char *s) {
54 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
57 int result = vsnprintf(buf, sizeof buf, fmt, ap);
62 int result = vasprintf(strp, fmt, ap);
69 static inline int xasprintf(char **strp, const char *fmt, ...) __attribute__((__format__(printf, 2, 3)));
70 static inline int xasprintf(char **strp, const char *fmt, ...) {
73 int result = xvasprintf(strp, fmt, ap);