#include <string.h>
#include <unistd.h>
-static inline void *xmalloc(ssize_t size) {
- void *mem;
-
- if (size <= 0)
- return NULL;
-
- mem = calloc(size, 1);
- if (!mem)
- abort();
- return mem;
-}
-
-static inline void *xrealloc(void *mem, ssize_t newsize) {
- mem = realloc(mem, newsize);
- if (!mem)
- abort();
- return mem;
-}
-
-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 = xmalloc(len + 1);
- memcpy(res, src, len);
- res[len] = '\0';
- return res;
-}
-
#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))
#define p_dupstr(p, len) xmemdupstr((p), (len))
+#define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
#ifdef __GNUC__
# define p_delete(mem_pp) \
- ({ \
+ do { \
typeof(**(mem_pp)) **__ptr = (mem_pp); \
free(*__ptr); \
*__ptr = NULL; \
- })
+ } while(0)
#else
#endif
+static inline void *xmalloc(ssize_t size) {
+ void *mem;
+
+ if (size <= 0)
+ return NULL;
+
+ mem = calloc(size, 1);
+ if (!mem)
+ abort();
+ return mem;
+}
+
static inline void xmemfree(void **ptr) {
p_delete(ptr);
}
+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;
+}
+
#endif /* MUTT_LIB_LIB_MEM_H */