diff options
Diffstat (limited to 'src/nvim/lib/kvec.h')
-rw-r--r-- | src/nvim/lib/kvec.h | 113 |
1 files changed, 55 insertions, 58 deletions
diff --git a/src/nvim/lib/kvec.h b/src/nvim/lib/kvec.h index 0a7260b2e5..eb2d9bbb77 100644 --- a/src/nvim/lib/kvec.h +++ b/src/nvim/lib/kvec.h @@ -1,5 +1,3 @@ -// uncrustify:off - // The MIT License // // Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk> @@ -46,25 +44,25 @@ #include "nvim/os/os_defs.h" #define kv_roundup32(x) \ - ((--(x)), \ - ((x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16), \ - (++(x))) + ((--(x)), \ + ((x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16), \ + (++(x))) #define KV_INITIAL_VALUE { .size = 0, .capacity = 0, .items = NULL } #define kvec_t(type) \ - struct { \ - size_t size; \ - size_t capacity; \ - type *items; \ - } + struct { \ + size_t size; \ + size_t capacity; \ + type *items; \ + } #define kv_init(v) ((v).size = (v).capacity = 0, (v).items = 0) #define kv_destroy(v) \ - do { \ - xfree((v).items); \ - kv_init(v); \ - } while (0) + do { \ + xfree((v).items); \ + kv_init(v); \ + } while (0) #define kv_A(v, i) ((v).items[(i)]) #define kv_pop(v) ((v).items[--(v).size]) #define kv_size(v) ((v).size) @@ -81,34 +79,34 @@ #define kv_drop(v, n) ((v).size -= (n)) #define kv_resize(v, s) \ - ((v).capacity = (s), \ - (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity)) + ((v).capacity = (s), \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity)) #define kv_resize_full(v) \ - kv_resize(v, (v).capacity ? (v).capacity << 1 : 8) + kv_resize(v, (v).capacity ? (v).capacity << 1 : 8) #define kv_copy(v1, v0) \ - do { \ - if ((v1).capacity < (v0).size) { \ - kv_resize(v1, (v0).size); \ - } \ - (v1).size = (v0).size; \ - memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ - } while (0) + do { \ + if ((v1).capacity < (v0).size) { \ + kv_resize(v1, (v0).size); \ + } \ + (v1).size = (v0).size; \ + memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ + } while (0) #define kv_pushp(v) \ - ((((v).size == (v).capacity) ? (kv_resize_full(v), 0) : 0), \ - ((v).items + ((v).size++))) + ((((v).size == (v).capacity) ? (kv_resize_full(v), 0) : 0), \ + ((v).items + ((v).size++))) #define kv_push(v, x) \ - (*kv_pushp(v) = (x)) + (*kv_pushp(v) = (x)) #define kv_a(v, i) \ - (*(((v).capacity <= (size_t) (i) \ + (*(((v).capacity <= (size_t)(i) \ ? ((v).capacity = (v).size = (i) + 1, \ kv_roundup32((v).capacity), \ kv_resize((v), (v).capacity), 0UL) \ - : ((v).size <= (size_t) (i) \ + : ((v).size <= (size_t)(i) \ ? (v).size = (i) + 1 \ : 0UL)), \ &(v).items[(i)])) @@ -122,24 +120,23 @@ /// @param[in] type Type of vector elements. /// @param[in] init_size Number of the elements in the initial array. #define kvec_withinit_t(type, INIT_SIZE) \ - struct { \ - size_t size; \ - size_t capacity; \ - type *items; \ - type init_array[INIT_SIZE]; \ - } + struct { \ + size_t size; \ + size_t capacity; \ + type *items; \ + type init_array[INIT_SIZE]; \ + } /// Initialize vector with preallocated array /// /// @param[out] v Vector to initialize. #define kvi_init(v) \ - ((v).capacity = ARRAY_SIZE((v).init_array), \ - (v).size = 0, \ - (v).items = (v).init_array) + ((v).capacity = ARRAY_SIZE((v).init_array), \ + (v).size = 0, \ + (v).items = (v).init_array) /// Move data to a new destination and free source -static inline void *_memcpy_free(void *const restrict dest, - void *const restrict src, +static inline void *_memcpy_free(void *const restrict dest, void *const restrict src, const size_t size) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET FUNC_ATTR_ALWAYS_INLINE { @@ -158,15 +155,15 @@ static inline void *_memcpy_free(void *const restrict dest, /// @param[out] v Vector to resize. /// @param[in] s New size. #define kvi_resize(v, s) \ - ((v).capacity = ((s) > ARRAY_SIZE((v).init_array) \ + ((v).capacity = ((s) > ARRAY_SIZE((v).init_array) \ ? (s) \ : ARRAY_SIZE((v).init_array)), \ - (v).items = ((v).capacity == ARRAY_SIZE((v).init_array) \ + (v).items = ((v).capacity == ARRAY_SIZE((v).init_array) \ ? ((v).items == (v).init_array \ ? (v).items \ : _memcpy_free((v).init_array, (v).items, \ (v).size * sizeof((v).items[0]))) \ - : ((v).items == (v).init_array \ + : ((v).items == (v).init_array \ ? memcpy(xmalloc((v).capacity * sizeof((v).items[0])), \ (v).items, \ (v).size * sizeof((v).items[0])) \ @@ -177,13 +174,13 @@ static inline void *_memcpy_free(void *const restrict dest, /// /// @param[out] v Vector to resize. #define kvi_resize_full(v) \ - /* ARRAY_SIZE((v).init_array) is the minimal capacity of this vector. */ \ - /* Thus when vector is full capacity may not be zero and it is safe */ \ - /* not to bother with checking whether (v).capacity is 0. But now */ \ - /* capacity is not guaranteed to have size that is a power of 2, it is */ \ - /* hard to fix this here and is not very necessary if users will use */ \ - /* 2^x initial array size. */ \ - kvi_resize(v, (v).capacity << 1) + /* ARRAY_SIZE((v).init_array) is the minimal capacity of this vector. */ \ + /* Thus when vector is full capacity may not be zero and it is safe */ \ + /* not to bother with checking whether (v).capacity is 0. But now */ \ + /* capacity is not guaranteed to have size that is a power of 2, it is */ \ + /* hard to fix this here and is not very necessary if users will use */ \ + /* 2^x initial array size. */ \ + kvi_resize(v, (v).capacity << 1) /// Get location where to store new element to a vector with preallocated array /// @@ -191,24 +188,24 @@ static inline void *_memcpy_free(void *const restrict dest, /// /// @return Pointer to the place where new value should be stored. #define kvi_pushp(v) \ - ((((v).size == (v).capacity) ? (kvi_resize_full(v), 0) : 0), \ - ((v).items + ((v).size++))) + ((((v).size == (v).capacity) ? (kvi_resize_full(v), 0) : 0), \ + ((v).items + ((v).size++))) /// Push value to a vector with preallocated array /// /// @param[out] v Vector to push to. /// @param[in] x Value to push. #define kvi_push(v, x) \ - (*kvi_pushp(v) = (x)) + (*kvi_pushp(v) = (x)) /// Free array of elements of a vector with preallocated array if needed /// /// @param[out] v Vector to free. #define kvi_destroy(v) \ - do { \ - if ((v).items != (v).init_array) { \ - XFREE_CLEAR((v).items); \ - } \ - } while (0) + do { \ + if ((v).items != (v).init_array) { \ + XFREE_CLEAR((v).items); \ + } \ + } while (0) #endif // NVIM_LIB_KVEC_H |