X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fmem.h;h=d981a805b9420af0608b37243c6fd045ea8b6bf3;hp=9ff258918e2cb357c1f72c569d4661410c2be819;hb=6920eb5798f2d9f25e5ea1af2ba86122cf408bd1;hpb=e25b0de8c4e7d6aeeb467f8ea1ad8dd6b2a9470e diff --git a/lib-lib/mem.h b/lib-lib/mem.h index 9ff2589..d981a80 100644 --- a/lib-lib/mem.h +++ b/lib-lib/mem.h @@ -20,10 +20,6 @@ #ifndef MUTT_LIB_LIB_MEM_H #define MUTT_LIB_LIB_MEM_H -#include -#include -#include - #define ssizeof(foo) (ssize_t)sizeof(foo) #define countof(foo) (ssizeof(foo) / ssizeof(foo[0])) @@ -33,6 +29,20 @@ #define p_dupstr(p, len) xmemdupstr((p), (len)) #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count)) +#define p_alloc_nr(x) (((x) + 16) * 3 / 2) + +#define p_allocgrow(pp, goalnb, allocnb) \ + do { \ + if ((goalnb) > *(allocnb)) { \ + if (p_alloc_nr(*(allocnb)) < (goalnb)) { \ + *(allocnb) = (goalnb); \ + } else { \ + *(allocnb) = p_alloc_nr(*(allocnb)); \ + } \ + p_realloc(pp, *(allocnb)); \ + } \ + } while (0) + #ifdef __GNUC__ # define p_delete(mem_pp) \ @@ -90,6 +100,27 @@ static inline void *xmemdupstr(const void *src, ssize_t len) { } +#define DO_REFCNT(type, prefix) \ + static inline type *prefix##_new(void) { \ + type *res = prefix##_init(p_new(type, 1)); \ + res->refcnt = 1; \ + return res; \ + } \ + static inline type *prefix##_dup(type *t) { \ + t->refcnt++; \ + return t; \ + } \ + static inline void prefix##_delete(type **tp) { \ + if (*tp) { \ + if (--(*tp)->refcnt > 0) { \ + *tp = NULL; \ + } else { \ + prefix##_wipe(*tp); \ + p_delete(tp); \ + } \ + } \ + } + #define DO_INIT(type, prefix) \ static inline type * prefix##_init(type *var) { \ p_clear(var, 1); \