2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the Free
4 * Software Foundation; either version 2 of the License, or (at your option)
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * Author: Pierre Habouzit <madcoder@debian.org>
19 #ifndef MUTT_LIB_LIB_MEM_H
20 #define MUTT_LIB_LIB_MEM_H
26 static inline void *xmalloc(ssize_t size) {
32 mem = calloc(size, 1);
38 static inline void *xrealloc(void *mem, ssize_t newsize) {
39 mem = realloc(mem, newsize);
45 static inline void *xmemdup(const void *src, ssize_t size) {
46 return memcpy(xmalloc(size), src, size);
49 static inline void *xmemdupstr(const void *src, ssize_t len) {
50 char *res = xmalloc(len + 1);
51 memcpy(res, src, len);
56 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
57 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
58 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
59 #define p_dupstr(p, len) xmemdupstr((p), (len))
63 # define p_delete(mem_pp) \
65 typeof(**(mem_pp)) **__ptr = (mem_pp); \
72 # define p_delete(mem_p) \
74 void *__ptr = (mem_p); \
76 *(void **)__ptr = NULL; \
81 static inline void xmemfree(void **ptr) {
85 #endif /* MUTT_LIB_LIB_MEM_H */