+static inline void xrealloc(void **ptr, ssize_t newsize) {
+ if (newsize <= 0) {
+ p_delete(ptr);
+ } else {
+ *ptr = realloc(*ptr, newsize);
+ if (!*ptr)
+ abort();
+ }
+}
+
+static inline void *xmemdup(const void *src, ssize_t size) {
+ return memcpy(xmalloc(size), src, size);
+}
+
+static inline void *xmemdupstr(const void *src, ssize_t len) {
+ char *res = memcpy(xmalloc(len + 1), src, len);
+ res[len] = '\0';
+ 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); \
+ } \
+ }
+