diff options
Diffstat (limited to 'src/nvim/garray.c')
-rw-r--r-- | src/nvim/garray.c | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/nvim/garray.c b/src/nvim/garray.c index ac5f9155b1..2cef08ef5f 100644 --- a/src/nvim/garray.c +++ b/src/nvim/garray.c @@ -6,6 +6,7 @@ #include "nvim/vim.h" #include "nvim/ascii.h" +#include "nvim/log.h" #include "nvim/misc2.h" #include "nvim/memory.h" #include "nvim/path.h" @@ -52,7 +53,21 @@ void ga_init(garray_T *gap, int itemsize, int growsize) gap->ga_maxlen = 0; gap->ga_len = 0; gap->ga_itemsize = itemsize; - gap->ga_growsize = growsize; + ga_set_growsize(gap, growsize); +} + +/// A setter for the growsize that guarantees it will be at least 1. +/// +/// @param gap +/// @param growsize +void ga_set_growsize(garray_T *gap, int growsize) +{ + if (growsize < 1) { + WLOG("trying to set an invalid ga_growsize: %d", growsize); + gap->ga_growsize = 1; + } else { + gap->ga_growsize = growsize; + } } /// Make room in growing array "gap" for at least "n" items. @@ -66,17 +81,24 @@ void ga_grow(garray_T *gap, int n) return; } - // the garray grows by at least growsize (do we have a MIN macro somewhere?) - n = (n < gap->ga_growsize) ? gap->ga_growsize : n; + if (gap->ga_growsize < 1) { + WLOG("ga_growsize(%d) is less than 1", gap->ga_growsize); + } + + // the garray grows by at least growsize + if (n < gap->ga_growsize) { + n = gap->ga_growsize; + } + int new_maxlen = gap->ga_len + n; - size_t new_size = (size_t)(gap->ga_itemsize * (gap->ga_len + n)); + size_t new_size = (size_t)(gap->ga_itemsize * new_maxlen); size_t old_size = (size_t)(gap->ga_itemsize * gap->ga_maxlen); // reallocate and clear the new memory - char_u *pp = xrealloc(gap->ga_data, new_size); + char *pp = xrealloc(gap->ga_data, new_size); memset(pp + old_size, 0, new_size - old_size); - gap->ga_maxlen = gap->ga_len + n; + gap->ga_maxlen = new_maxlen; gap->ga_data = pp; } @@ -175,10 +197,7 @@ void ga_concat(garray_T *gap, const char_u *restrict s) /// @param c void ga_append(garray_T *gap, char c) { - ga_grow(gap, 1); - char *str = gap->ga_data; - str[gap->ga_len] = c; - gap->ga_len++; + GA_APPEND(char, gap, c); } #if defined(UNIX) || defined(WIN3264) || defined(PROTO) |