From 5ed74cfb7c67f79441343ec90548f333dad1729b Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Wed, 11 Jun 2014 03:01:46 -0300 Subject: Introduce ga_append_via_ptr() and GA_APPEND_VIA_PTR() Similar to GA_APPEND(). Replaces this pattern: ga_grow(&ga, 1); item_type *p = ((item_type *)ga.ga_data) + ga.ga_len; p->field1 = v1; p->field2 = v2; ga.ga_len++; --- src/nvim/garray.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/nvim/garray.h') diff --git a/src/nvim/garray.h b/src/nvim/garray.h index ed5e2dbada..5a90da21c2 100644 --- a/src/nvim/garray.h +++ b/src/nvim/garray.h @@ -1,6 +1,8 @@ #ifndef NVIM_GARRAY_H #define NVIM_GARRAY_H +#include "nvim/log.h" + /// Structure used for growing arrays. /// This is used to store information that only grows, is deleted all at /// once, and needs to be accessed by index. See ga_clear() and ga_grow(). @@ -22,7 +24,20 @@ typedef struct growarray { ((item_type *)(gap)->ga_data)[(gap)->ga_len++] = (item); \ } while (0) +#define GA_APPEND_VIA_PTR(item_type, gap) \ + ga_append_via_ptr(gap, sizeof(item_type)) + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "garray.h.generated.h" #endif + +static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size) +{ + if ((int)item_size != gap->ga_itemsize) { + ELOG("wrong item size in garray(%d), should be %d", item_size); + } + ga_grow(gap, 1); + return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++); +} + #endif // NVIM_GARRAY_H -- cgit