From f055e4a13b9b6e0f9e4b8394c11a330547916233 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 22 Jun 2008 13:30:39 +0200 Subject: [PATCH] Add p_grow. Signed-off-by: Pierre Habouzit --- common/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/util.h b/common/util.h index 32177264..466e077b 100644 --- a/common/util.h +++ b/common/util.h @@ -64,10 +64,23 @@ typedef struct #define ssizeof(foo) (ssize_t)sizeof(foo) #define countof(foo) (ssizeof(foo) / ssizeof(foo[0])) +#define p_alloc_nr(x) (((x) + 16) * 3 / 2) #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count))) #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count))) #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count)) #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count)) +#define p_grow(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__