| 1 |
/* |
| 2 |
* Copyright 2008-2011 Various Authors |
| 3 |
* Copyright 2004-2005 Timo Hirvonen |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License as |
| 7 |
* published by the Free Software Foundation; either version 2 of the |
| 8 |
* License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef _XMALLOC_H |
| 20 |
#define _XMALLOC_H |
| 21 |
|
| 22 |
#include "compiler.h" |
| 23 |
#ifdef HAVE_CONFIG |
| 24 |
#include "config/xmalloc.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
void malloc_fail(void) __NORETURN; |
| 31 |
|
| 32 |
#define xnew(type, n) (type *)xmalloc(sizeof(type) * (n)) |
| 33 |
#define xnew0(type, n) (type *)xmalloc0(sizeof(type) * (n)) |
| 34 |
#define xrenew(type, mem, n) (type *)xrealloc(mem, sizeof(type) * (n)) |
| 35 |
|
| 36 |
static inline void * __MALLOC xmalloc(size_t size) |
| 37 |
{ |
| 38 |
void *ptr = malloc(size); |
| 39 |
|
| 40 |
if (unlikely(ptr == NULL)) |
| 41 |
malloc_fail(); |
| 42 |
return ptr; |
| 43 |
} |
| 44 |
|
| 45 |
static inline void * __MALLOC xmalloc0(size_t size) |
| 46 |
{ |
| 47 |
void *ptr = calloc(1, size); |
| 48 |
|
| 49 |
if (unlikely(ptr == NULL)) |
| 50 |
malloc_fail(); |
| 51 |
return ptr; |
| 52 |
} |
| 53 |
|
| 54 |
static inline void * __MALLOC xrealloc(void *ptr, size_t size) |
| 55 |
{ |
| 56 |
ptr = realloc(ptr, size); |
| 57 |
if (unlikely(ptr == NULL)) |
| 58 |
malloc_fail(); |
| 59 |
return ptr; |
| 60 |
} |
| 61 |
|
| 62 |
static inline char * __MALLOC xstrdup(const char *str) |
| 63 |
{ |
| 64 |
#ifdef HAVE_STRDUP |
| 65 |
char *s = strdup(str); |
| 66 |
if (unlikely(s == NULL)) |
| 67 |
malloc_fail(); |
| 68 |
return s; |
| 69 |
#else |
| 70 |
size_t size = strlen(str) + 1; |
| 71 |
void *ptr = xmalloc(size); |
| 72 |
return (char *) memcpy(ptr, str, size); |
| 73 |
#endif |
| 74 |
} |
| 75 |
|
| 76 |
#ifdef HAVE_STRNDUP |
| 77 |
static inline char * __MALLOC xstrndup(const char *str, size_t n) |
| 78 |
{ |
| 79 |
char *s = strndup(str, n); |
| 80 |
if (unlikely(s == NULL)) |
| 81 |
malloc_fail(); |
| 82 |
return s; |
| 83 |
} |
| 84 |
#else |
| 85 |
char * __MALLOC xstrndup(const char *str, size_t n); |
| 86 |
#endif |
| 87 |
|
| 88 |
static inline void free_str_array(char **array) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
|
| 92 |
if (array == NULL) |
| 93 |
return; |
| 94 |
for (i = 0; array[i]; i++) |
| 95 |
free(array[i]); |
| 96 |
free(array); |
| 97 |
} |
| 98 |
|
| 99 |
#endif |