move parse.c into rfc822parse.c in the lib-mime as it's what it's about
[apps/madmutt.git] / lib-lib / mem.h
1 /*
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.
6  *
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.
11  *
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,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 #ifndef MUTT_LIB_LIB_MEM_H
21 #define MUTT_LIB_LIB_MEM_H
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #define ssizeof(foo)            (ssize_t)sizeof(foo)
28
29 #define p_new(type, count)      ((type *)xmalloc(sizeof(type) * (count)))
30 #define p_clear(p, count)       ((void)memset((p), 0, sizeof(*(p)) * (count)))
31 #define p_dup(p, count)         xmemdup((p), sizeof(*(p)) * (count))
32 #define p_dupstr(p, len)        xmemdupstr((p), (len))
33 #define p_realloc(pp, count)    xrealloc((void*)(pp), sizeof(**(pp)) * (count))
34
35 #ifdef __GNUC__
36
37 #  define p_delete(mem_pp)                          \
38         do {                                        \
39             typeof(**(mem_pp)) **__ptr = (mem_pp);  \
40             free(*__ptr);                           \
41             *__ptr = NULL;                          \
42         } while(0)
43
44 #else
45
46 #  define p_delete(mem_p)                           \
47         do {                                        \
48             void *__ptr = (mem_p);                  \
49             free(*__ptr);                           \
50             *(void **)__ptr = NULL;                 \
51         } while (0)
52
53 #endif
54
55 static inline void *xmalloc(ssize_t size) {
56     void *mem;
57
58     if (size <= 0)
59         return NULL;
60
61     mem = calloc(size, 1);
62     if (!mem)
63         abort();
64     return mem;
65 }
66
67 static inline void xmemfree(void **ptr) {
68     p_delete(ptr);
69 }
70
71 static inline void xrealloc(void **ptr, ssize_t newsize) {
72     if (newsize <= 0) {
73         p_delete(ptr);
74     } else {
75         *ptr = realloc(*ptr, newsize);
76         if (!*ptr)
77             abort();
78     }
79 }
80
81 static inline void *xmemdup(const void *src, ssize_t size) {
82     return memcpy(xmalloc(size), src, size);
83 }
84
85 static inline void *xmemdupstr(const void *src, ssize_t len) {
86     char *res = memcpy(xmalloc(len + 1), src, len);
87     res[len] = '\0';
88     return res;
89 }
90
91
92 #define DO_INIT(type, prefix) \
93     static inline type * prefix##_init(type *var) {         \
94         p_clear(var, 1);                                    \
95         return var;                                         \
96     }
97 #define DO_WIPE(type, prefix) \
98     static inline void prefix##_wipe(type *var __attribute__((unused))) { }
99
100 #define DO_NEW(type, prefix) \
101     static inline type * prefix##_new(void) {               \
102         return prefix##_init(p_new(type, 1));               \
103     }
104 #define DO_DELETE(type, prefix) \
105     static inline void __attribute__((nonnull))             \
106     prefix##_delete(type **var) {                           \
107         if (*var) {                                         \
108             prefix##_wipe(*var);                            \
109             p_delete(var);                                  \
110         }                                                   \
111     }
112
113 #endif /* MUTT_LIB_LIB_MEM_H */