X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fmem.h;h=8d904b3d7b607abaab41092a10c1bfce59742d96;hp=c462c193061226a8919de206890e23d2aa4b1982;hb=a72bbb26973d73ce98c1dde62f3a114e2bb6589c;hpb=7f7a0be369840b290248e5b0302beb447fa1b3cd diff --git a/lib-lib/mem.h b/lib-lib/mem.h index c462c19..8d904b3 100644 --- a/lib-lib/mem.h +++ b/lib-lib/mem.h @@ -1,17 +1,18 @@ /* - * 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. * * Copyright © 2006 Pierre Habouzit */ @@ -19,9 +20,8 @@ #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])) #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count))) #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count))) @@ -29,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) \ @@ -85,4 +99,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 */