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
23 #define ssizeof(foo) (ssize_t)sizeof(foo)
24 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
26 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
27 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
28 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
29 #define p_dupstr(p, len) xmemdupstr((p), (len))
30 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
34 # define p_delete(mem_pp) \
36 typeof(**(mem_pp)) **__ptr = (mem_pp); \
43 # define p_delete(mem_p) \
45 void *__ptr = (mem_p); \
47 *(void **)__ptr = NULL; \
52 static inline void *xmalloc(ssize_t size) {
58 mem = calloc(size, 1);
64 static inline void xmemfree(void **ptr) {
68 static inline void xrealloc(void **ptr, ssize_t newsize) {
72 *ptr = realloc(*ptr, newsize);
78 static inline void *xmemdup(const void *src, ssize_t size) {
79 return memcpy(xmalloc(size), src, size);
82 static inline void *xmemdupstr(const void *src, ssize_t len) {
83 char *res = memcpy(xmalloc(len + 1), src, len);
89 #define DO_INIT(type, prefix) \
90 static inline type * prefix##_init(type *var) { \
94 #define DO_WIPE(type, prefix) \
95 static inline void prefix##_wipe(type *var __attribute__((unused))) { }
97 #define DO_NEW(type, prefix) \
98 static inline type * prefix##_new(void) { \
99 return prefix##_init(p_new(type, 1)); \
101 #define DO_DELETE(type, prefix) \
102 static inline void __attribute__((nonnull)) \
103 prefix##_delete(type **var) { \
105 prefix##_wipe(*var); \
110 #endif /* MUTT_LIB_LIB_MEM_H */