fixes build system in german (utf-8) locale
[cmus.git] / xmalloc.h
blob511408658a4b21c7a7db35ce48074e37e4f26b56
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #ifndef _XMALLOC_H
21 #define _XMALLOC_H
23 #include <compiler.h>
25 #include <stdlib.h>
26 #include <string.h>
28 extern void malloc_fail(void) __NORETURN;
30 #define xnew(type, n) (type *)xmalloc(sizeof(type) * (n))
31 #define xnew0(type, n) (type *)xmalloc0(sizeof(type) * (n))
32 #define xrenew(type, mem, n) (type *)xrealloc(mem, sizeof(type) * (n))
34 static inline void * __MALLOC xmalloc(size_t size)
36 void *ptr = malloc(size);
38 if (unlikely(ptr == NULL))
39 malloc_fail();
40 return ptr;
43 static inline void * __MALLOC xmalloc0(size_t size)
45 void *ptr = calloc(1, size);
47 if (unlikely(ptr == NULL))
48 malloc_fail();
49 return ptr;
52 static inline void * __MALLOC xrealloc(void *ptr, size_t size)
54 ptr = realloc(ptr, size);
55 if (unlikely(ptr == NULL))
56 malloc_fail();
57 return ptr;
60 static inline char * __MALLOC xstrdup(const char *str)
62 char *s = strdup(str);
64 if (unlikely(s == NULL))
65 malloc_fail();
66 return s;
69 extern char * __MALLOC xstrndup(const char *str, size_t n);
70 extern char **str_array_add(char **a, int *allocp, int *posp, char *str);
72 static inline char * __MALLOC xxstrdup(const char *str)
74 if (str == NULL)
75 return NULL;
76 return xstrdup(str);
79 static inline void free_str_array(char **array)
81 int i;
83 if (array == NULL)
84 return;
85 for (i = 0; array[i]; i++)
86 free(array[i]);
87 free(array);
90 #endif