More string and buffer functions.
[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 #define ssizeof(foo)            (ssize_t)sizeof(foo)
24 #define countof(foo)            (ssizeof(foo) / ssizeof(foo[0]))
25
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))
31
32 #define p_alloc_nr(x) (((x) + 16) * 3 / 2)
33
34 #define p_allocgrow(pp, goalnb, allocnb)                  \
35   do {                                                    \
36       if ((goalnb) > *(allocnb)) {                        \
37           if (p_alloc_nr(*(allocnb)) < (goalnb)) {        \
38               *(allocnb) = (goalnb);                      \
39           } else {                                        \
40               *(allocnb) = p_alloc_nr(*(allocnb));        \
41           }                                               \
42           p_realloc(pp, *(allocnb));                      \
43       }                                                   \
44   } while (0)
45
46 #ifdef __GNUC__
47
48 #  define p_delete(mem_pp)                          \
49         do {                                        \
50             typeof(**(mem_pp)) **__ptr = (mem_pp);  \
51             free(*__ptr);                           \
52             *__ptr = NULL;                          \
53         } while(0)
54
55 #else
56
57 #  define p_delete(mem_p)                           \
58         do {                                        \
59             void *__ptr = (mem_p);                  \
60             free(*__ptr);                           \
61             *(void **)__ptr = NULL;                 \
62         } while (0)
63
64 #endif
65
66 static inline void *xmalloc(ssize_t size) {
67     void *mem;
68
69     if (size <= 0)
70         return NULL;
71
72     mem = calloc(size, 1);
73     if (!mem)
74         abort();
75     return mem;
76 }
77
78 static inline void xmemfree(void **ptr) {
79     p_delete(ptr);
80 }
81
82 static inline void xrealloc(void **ptr, ssize_t newsize) {
83     if (newsize <= 0) {
84         p_delete(ptr);
85     } else {
86         *ptr = realloc(*ptr, newsize);
87         if (!*ptr)
88             abort();
89     }
90 }
91
92 static inline void *xmemdup(const void *src, ssize_t size) {
93     return memcpy(xmalloc(size), src, size);
94 }
95
96 static inline void *xmemdupstr(const void *src, ssize_t len) {
97     char *res = memcpy(xmalloc(len + 1), src, len);
98     res[len] = '\0';
99     return res;
100 }
101
102
103 #define DO_REFCNT(type, prefix)                             \
104     static inline type *prefix##_new(void) {                \
105         type *res = prefix##_init(p_new(type, 1));          \
106         res->refcnt = 1;                                    \
107         return res;                                         \
108     }                                                       \
109     static inline type *prefix##_dup(type *t) {             \
110         t->refcnt++;                                        \
111         return t;                                           \
112     }                                                       \
113     static inline void prefix##_delete(type **tp) {         \
114         if (*tp) {                                          \
115             if (--(*tp)->refcnt > 0) {                      \
116                 *tp = NULL;                                 \
117             } else {                                        \
118                 prefix##_wipe(*tp);                         \
119                 p_delete(tp);                               \
120             }                                               \
121         }                                                   \
122     }
123
124 #define DO_INIT(type, prefix) \
125     static inline type * prefix##_init(type *var) {         \
126         p_clear(var, 1);                                    \
127         return var;                                         \
128     }
129 #define DO_WIPE(type, prefix) \
130     static inline void prefix##_wipe(type *var __attribute__((unused))) { }
131
132 #define DO_NEW(type, prefix) \
133     static inline type * prefix##_new(void) {               \
134         return prefix##_init(p_new(type, 1));               \
135     }
136 #define DO_DELETE(type, prefix) \
137     static inline void __attribute__((nonnull))             \
138     prefix##_delete(type **var) {                           \
139         if (*var) {                                         \
140             prefix##_wipe(*var);                            \
141             p_delete(var);                                  \
142         }                                                   \
143     }
144
145 #endif /* MUTT_LIB_LIB_MEM_H */