add m_strchrnul that does what GNU strchrnul does: search for 'c' or
[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 #define countof(foo)            (ssizeof(foo) / ssizeof(foo[0]))
29
30 #define p_new(type, count)      ((type *)xmalloc(sizeof(type) * (count)))
31 #define p_clear(p, count)       ((void)memset((p), 0, sizeof(*(p)) * (count)))
32 #define p_dup(p, count)         xmemdup((p), sizeof(*(p)) * (count))
33 #define p_dupstr(p, len)        xmemdupstr((p), (len))
34 #define p_realloc(pp, count)    xrealloc((void*)(pp), sizeof(**(pp)) * (count))
35
36 #ifdef __GNUC__
37
38 #  define p_delete(mem_pp)                          \
39         do {                                        \
40             typeof(**(mem_pp)) **__ptr = (mem_pp);  \
41             free(*__ptr);                           \
42             *__ptr = NULL;                          \
43         } while(0)
44
45 #else
46
47 #  define p_delete(mem_p)                           \
48         do {                                        \
49             void *__ptr = (mem_p);                  \
50             free(*__ptr);                           \
51             *(void **)__ptr = NULL;                 \
52         } while (0)
53
54 #endif
55
56 static inline void *xmalloc(ssize_t size) {
57     void *mem;
58
59     if (size <= 0)
60         return NULL;
61
62     mem = calloc(size, 1);
63     if (!mem)
64         abort();
65     return mem;
66 }
67
68 static inline void xmemfree(void **ptr) {
69     p_delete(ptr);
70 }
71
72 static inline void xrealloc(void **ptr, ssize_t newsize) {
73     if (newsize <= 0) {
74         p_delete(ptr);
75     } else {
76         *ptr = realloc(*ptr, newsize);
77         if (!*ptr)
78             abort();
79     }
80 }
81
82 static inline void *xmemdup(const void *src, ssize_t size) {
83     return memcpy(xmalloc(size), src, size);
84 }
85
86 static inline void *xmemdupstr(const void *src, ssize_t len) {
87     char *res = memcpy(xmalloc(len + 1), src, len);
88     res[len] = '\0';
89     return res;
90 }
91
92
93 #define DO_INIT(type, prefix) \
94     static inline type * prefix##_init(type *var) {         \
95         p_clear(var, 1);                                    \
96         return var;                                         \
97     }
98 #define DO_WIPE(type, prefix) \
99     static inline void prefix##_wipe(type *var __attribute__((unused))) { }
100
101 #define DO_NEW(type, prefix) \
102     static inline type * prefix##_new(void) {               \
103         return prefix##_init(p_new(type, 1));               \
104     }
105 #define DO_DELETE(type, prefix) \
106     static inline void __attribute__((nonnull))             \
107     prefix##_delete(type **var) {                           \
108         if (*var) {                                         \
109             prefix##_wipe(*var);                            \
110             p_delete(var);                                  \
111         }                                                   \
112     }
113
114 #endif /* MUTT_LIB_LIB_MEM_H */