Document Replay Gain
[cmus.git] / xmalloc.h
blobb22c5822f04b4fda3f84669d34ce8db27212e533
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 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 char * __MALLOC xstrndup(const char *str, size_t n);
71 static inline void free_str_array(char **array)
73 int i;
75 if (array == NULL)
76 return;
77 for (i = 0; array[i]; i++)
78 free(array[i]);
79 free(array);
82 #endif