more work in the lib-mime. begin to "rewr^H^Had" the code in rfc2231.c
[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 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))
32
33 #ifdef __GNUC__
34
35 #  define p_delete(mem_pp)                          \
36         do {                                        \
37             typeof(**(mem_pp)) **__ptr = (mem_pp);  \
38             free(*__ptr);                           \
39             *__ptr = NULL;                          \
40         } while(0)
41
42 #else
43
44 #  define p_delete(mem_p)                           \
45         do {                                        \
46             void *__ptr = (mem_p);                  \
47             free(*__ptr);                           \
48             *(void **)__ptr = NULL;                 \
49         } while (0)
50
51 #endif
52
53 static inline void *xmalloc(ssize_t size) {
54     void *mem;
55
56     if (size <= 0)
57         return NULL;
58
59     mem = calloc(size, 1);
60     if (!mem)
61         abort();
62     return mem;
63 }
64
65 static inline void xmemfree(void **ptr) {
66     p_delete(ptr);
67 }
68
69 static inline void xrealloc(void **ptr, ssize_t newsize) {
70     if (newsize <= 0) {
71         p_delete(ptr);
72     } else {
73         *ptr = realloc(*ptr, newsize);
74         if (!*ptr)
75             abort();
76     }
77 }
78
79 static inline void *xmemdup(const void *src, ssize_t size) {
80     return memcpy(xmalloc(size), src, size);
81 }
82
83 static inline void *xmemdupstr(const void *src, ssize_t len) {
84     char *res = memcpy(xmalloc(len + 1), src, len);
85     res[len] = '\0';
86     return res;
87 }
88
89
90 #define DO_INIT(type, prefix) \
91     static inline type * prefix##_init(type *var) {         \
92         p_clear(var, 1);                                    \
93         return var;                                         \
94     }
95 #define DO_WIPE(type, prefix) \
96     static inline void prefix##_wipe(type *var __attribute__((unused))) { }
97
98 #define DO_NEW(type, prefix) \
99     static inline type * prefix##_new(void) {               \
100         return prefix##_init(p_new(type, 1));               \
101     }
102 #define DO_DELETE(type, prefix) \
103     static inline void __attribute__((nonnull))             \
104     prefix##_delete(type **var) {                           \
105         if (*var) {                                         \
106             prefix##_wipe(*var);                            \
107             p_delete(var);                                  \
108         }                                                   \
109     }
110
111 #endif /* MUTT_LIB_LIB_MEM_H */