simplify mutt_parse_date
[apps/madmutt.git] / lib-lib / mem.h
index c95feac..9ff2589 100644 (file)
@@ -1,19 +1,20 @@
 /*
- *  This program is free software; you can redistribute it and/or modify it
- *  under the terms of the GNU General Public License as published by the Free
- *  Software Foundation; either version 2 of the License, or (at your option)
- *  any later version.
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful, but
- *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- *  for more details.
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
  *
- * Author: Pierre Habouzit <madcoder@debian.org>
+ *  Copyright © 2006 Pierre Habouzit
  */
 
 #ifndef MUTT_LIB_LIB_MEM_H
@@ -23,6 +24,9 @@
 #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)))
 #define p_dup(p, count)         xmemdup((p), sizeof(*(p)) * (count))
@@ -85,4 +89,26 @@ static inline void *xmemdupstr(const void *src, ssize_t len) {
     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 */