2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
20 #ifndef MUTT_LIB_LIB_MEM_H
21 #define MUTT_LIB_LIB_MEM_H
27 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
28 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
29 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
30 #define p_dupstr(p, len) xmemdupstr((p), (len))
31 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
35 # define p_delete(mem_pp) \
37 typeof(**(mem_pp)) **__ptr = (mem_pp); \
44 # define p_delete(mem_p) \
46 void *__ptr = (mem_p); \
48 *(void **)__ptr = NULL; \
53 static inline void *xmalloc(ssize_t size) {
59 mem = calloc(size, 1);
65 static inline void xmemfree(void **ptr) {
69 static inline void xrealloc(void **ptr, ssize_t newsize) {
73 *ptr = realloc(*ptr, newsize);
79 static inline void *xmemdup(const void *src, ssize_t size) {
80 return memcpy(xmalloc(size), src, size);
83 static inline void *xmemdupstr(const void *src, ssize_t len) {
84 char *res = memcpy(xmalloc(len + 1), src, len);
89 #endif /* MUTT_LIB_LIB_MEM_H */