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