aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/garray.h
diff options
context:
space:
mode:
authorFelipe Oliveira Carvalho <felipekde@gmail.com>2014-06-11 03:01:46 -0300
committerJustin M. Keyes <justinkz@gmail.com>2014-06-30 03:57:50 -0400
commit5ed74cfb7c67f79441343ec90548f333dad1729b (patch)
tree28b0265ba05af45cdaa983462564fbba186f135b /src/nvim/garray.h
parent45e7814e6aa8aacd8772056863d13770d4e30b48 (diff)
downloadrneovim-5ed74cfb7c67f79441343ec90548f333dad1729b.tar.gz
rneovim-5ed74cfb7c67f79441343ec90548f333dad1729b.tar.bz2
rneovim-5ed74cfb7c67f79441343ec90548f333dad1729b.zip
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++;
Diffstat (limited to 'src/nvim/garray.h')
-rw-r--r--src/nvim/garray.h15
1 files changed, 15 insertions, 0 deletions
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