1 /* 2 * Copyright 2003-2012 Gentoo Foundation 3 * Distributed under the terms of the GNU General Public License v2 4 * 5 * Copyright 2003-2012 Ned Ludd - <solar@gentoo.org> 6 * Copyright 2004-2012 Mike Frysinger - <vapier@gentoo.org> 7 */ 8 9 #ifndef __XFUNCS_H__ 10 #define __XFUNCS_H__ 11 12 char *xstrdup(const char *s); 13 void *xmalloc(size_t size); 14 void *xzalloc(size_t size); 15 void *xrealloc(void *ptr, size_t size); 16 void xstrncat(char **dst, const char *src, size_t *curr_len, size_t n); 17 #define xstrcat(dst,src,curr_len) xstrncat(dst,src,curr_len,0) 18 void xchrcat(char **dst, const char append, size_t *curr_len); 19 20 void *xmemdup(const void *src, size_t n); 21 22 typedef struct { 23 void **eles; 24 size_t num; 25 } array_t; 26 void xarraypush(array_t *array, const void *ele, size_t ele_len); 27 #define xarraypush_str(arr, ele) xarraypush(arr, ele, strlen(ele) + 1 /*NUL*/) 28 void xarrayfree(array_t *array); 29 #define xrealloc_array(ptr, size, ele_size) xrealloc(ptr, (size) * (ele_size)) 30 /* The assignment after the check is unfortunate as we do a non-NULL check (we 31 * already do not permit pushing of NULL pointers), but we can't put it in the 32 * increment phase as that will cause a load beyond the bounds of valid memory. 33 */ 34 #define array_for_each(arr, n, ele) \ 35 for (n = 0, ele = array_cnt(arr) ? arr->eles[n] : NULL; \ 36 n < array_cnt(arr) && (ele = arr->eles[n]); \ 37 ++n) 38 #define array_init_decl { .eles = NULL, .num = 0, } 39 #define array_cnt(arr) (arr)->num 40 char *array_flatten_str(array_t *array); 41 42 #endif 43