#ifndef MUTT_LIB_LIB_MEM_H
#define MUTT_LIB_LIB_MEM_H
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
+#define ssizeof(foo) (ssize_t)sizeof(foo)
+#define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
#define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
#define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
return res;
}
+
+#define DO_INIT(type, prefix) \
+ static inline type * prefix##_init(type *var) { \
+ p_clear(var, 1); \
+ return var; \
+ }
+#define DO_WIPE(type, prefix) \
+ static inline void prefix##_wipe(type *var __attribute__((unused))) { }
+
+#define DO_NEW(type, prefix) \
+ static inline type * prefix##_new(void) { \
+ return prefix##_init(p_new(type, 1)); \
+ }
+#define DO_DELETE(type, prefix) \
+ static inline void __attribute__((nonnull)) \
+ prefix##_delete(type **var) { \
+ if (*var) { \
+ prefix##_wipe(*var); \
+ p_delete(var); \
+ } \
+ }
+
#endif /* MUTT_LIB_LIB_MEM_H */