robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// The MIT License 2// 3// Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk> 4// 5// Permission is hereby granted, free of charge, to any person obtaining 6// a copy of this software and associated documentation files (the 7// "Software"), to deal in the Software without restriction, including 8// without limitation the rights to use, copy, modify, merge, publish, 9// distribute, sublicense, and/or sell copies of the Software, and to 10// permit persons to whom the Software is furnished to do so, subject to 11// the following conditions: 12// 13// The above copyright notice and this permission notice shall be 14// included in all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23// SOFTWARE. 24 25// An example: 26// 27// #include "kvec.h" 28// int main() { 29// kvec_t(int) array = KV_INITIAL_VALUE; 30// kv_push(array, 10); // append 31// kv_a(array, 20) = 5; // dynamic 32// kv_A(array, 20) = 4; // static 33// kv_destroy(array); 34// return 0; 35// } 36 37#ifndef NVIM_LIB_KVEC_H 38#define NVIM_LIB_KVEC_H 39 40#include <assert.h> 41#include <stdint.h> 42#include <stdlib.h> 43#include <string.h> 44 45#include "nvim/memory.h" 46#include "nvim/os/os_defs.h" 47 48#define kv_roundup32 (x ) \ 49 ((--(x)), \ 50 ((x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16), \ 51 (++(x))) 52 53#define KV_INITIAL_VALUE { .size = 0, .capacity = 0, .items = NULL } 54 55#define kvec_t (type ) \ 56 struct { \ 57 size_t size; \ 58 size_t capacity; \ 59 type *items; \ 60 } 61 62#define kv_init (v ) ((v).size = (v).capacity = 0, (v).items = 0) 63#define kv_destroy (v ) \ 64 do { \ 65 xfree((v).items); \ 66 kv_init(v); \ 67 } while (0) 68#define kv_A (v ,i ) ((v).items[(i)]) 69#define kv_pop (v ) ((v).items[--(v).size]) 70#define kv_size (v ) ((v).size) 71#define kv_max (v ) ((v).capacity) 72#define kv_Z (v ,i ) kv_A(v, kv_size(v) - (i) - 1) 73#define kv_last (v ) kv_Z(v, 0) 74 75/// Drop last n items from kvec without resizing 76/// 77/// Previously spelled as `(void)kv_pop(v)`, repeated n times. 78/// 79/// @param[out] v Kvec to drop items from. 80/// @param[in] n Number of elements to drop. 81#define kv_drop (v ,n ) ((v).size -= (n)) 82 83#define kv_resize (v ,s ) \ 84 ((v).capacity = (s), \ 85 (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity)) 86 87#define kv_resize_full (v ) \ 88 kv_resize(v, (v).capacity ? (v).capacity << 1 : 8) 89 90#define kv_copy (v1 ,v0 ) \ 91 do { \ 92 if ((v1).capacity < (v0).size) { \ 93 kv_resize(v1, (v0).size); \ 94 } \ 95 (v1).size = (v0).size; \ 96 memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ 97 } while (0) 98 99/// fit at least "len" more items 100#define kv_ensure_space (v ,len ) \ 101 do { \ 102 assert((len) <= SIZE_MAX - (v).size); \ 103 if ((v).capacity < (v).size + (len)) { \ 104 (v).capacity = (v).size + (len); \ 105 kv_roundup32((v).capacity); \ 106 kv_resize((v), (v).capacity); \ 107 } \ 108 } while (0) 109 110#define kv_concat_len (v ,data ,len ) \ 111 do { \ 112 if ((len) > 0) { \ 113 kv_ensure_space(v, len); \ 114 assert((v).items); \ 115/* kv_roundup32() only ORs bits in, so kv_ensure_space() leaves capacity >= size+len. */ \ 116/* coverity[overrun-buffer-arg] */ \ 117memcpy ((v ).items + (v ).size , (data ),sizeof ((v ).items [0 ])* (len )); \ 118 (v ).size = (v ).size + (len ); \ 119 } \ 120 }while (0 ) 121 122/// Appends a string to `v`, without its NUL. 123#define kv_concat (v ,str ) kv_concat_len(v, str, strlen(str)) 124/// Appends all items of `v0` to `v1`. 125#define kv_splice (v1 ,v0 ) kv_concat_len(v1, (v0).items, (v0).size) 126 127#define kv_pushp (v ) \ 128 ((((v).size == (v).capacity) ? (kv_resize_full(v), 0) : 0), \ 129 ((v).items + ((v).size++))) 130 131#define kv_push (v ,x ) \ 132 (*kv_pushp(v) = (x)) 133 134#define kv_pushp_c (v ) ((v).items + ((v).size++)) 135#define kv_push_c (v ,x ) (*kv_pushp_c(v) = (x)) 136 137#define kv_a (v ,i ) \ 138 (*(((v).capacity <= (size_t)(i) \ 139 ? ((v).capacity = (v).size = (i) + 1, \ 140 kv_roundup32((v).capacity), \ 141 kv_resize((v), (v).capacity), 0UL) \ 142 : ((v).size <= (size_t)(i) \ 143 ? (v).size = (i) + 1 \ 144 : 0UL)), \ 145 &(v).items[(i)])) 146 147#define kv_shift (v ,i ,n ) ((v).size -= (n), (i) < (v).size \ 148 && memmove(&kv_A(v, (i)), &kv_A(v, (i)+(n)), \ 149 ((v).size-(i))*sizeof(kv_A(v, i)))) 150 151/// Type of a vector with a few first members allocated on stack 152/// 153/// Is compatible with #kv_A, #kv_pop, #kv_size, #kv_max, #kv_last. 154/// Is not compatible with #kv_resize, #kv_resize_full, #kv_copy, #kv_push, 155/// #kv_pushp, #kv_a, #kv_destroy. 156/// 157/// @param[in] type Type of vector elements. 158/// @param[in] init_size Number of the elements in the initial array. 159#define kvec_withinit_t (type ,INIT_SIZE ) \ 160 struct { \ 161 size_t size; \ 162 size_t capacity; \ 163 type *items; \ 164 type init_array[INIT_SIZE]; \ 165 } 166 167#define KVI_INITIAL_VALUE (v ) { \ 168 .size = 0, \ 169 .capacity = ARRAY_SIZE((v).init_array), \ 170 .items = (v).init_array \ 171} 172 173/// Initialize vector with preallocated array 174/// 175/// @param[out] v Vector to initialize. 176#define kvi_init (v ) \ 177 ((v).capacity = ARRAY_SIZE((v).init_array), \ 178 (v).size = 0, \ 179 (v).items = (v).init_array) 180 181static inline void * _memcpy_free (void * restrictdest ,void * restrictsrc ,size_t size ) 182REAL_FATTR_NONNULL_ALL REAL_FATTR_NONNULL_RET REAL_FATTR_ALWAYS_INLINE ; 183 184/// Move data to a new destination and free source 185static inline void * _memcpy_free (void * const restrictdest ,void * const restrictsrc , 186const size_t size ) 187{ 188memcpy (dest ,src ,size ); 189XFREE_CLEAR (src ); 190return dest ; 191} 192 193/// Resize vector with preallocated array 194/// 195/// @note May not resize to an array smaller then init_array: if requested, 196/// init_array will be used. 197/// 198/// @param[out] v Vector to resize. 199/// @param[in] s New size. 200#define kvi_resize (v ,s ) \ 201 ((v).capacity = ((s) > ARRAY_SIZE((v).init_array) \ 202 ? (s) \ 203 : ARRAY_SIZE((v).init_array)), \ 204 (v).items = ((v).capacity == ARRAY_SIZE((v).init_array) \ 205 ? ((v).items == (v).init_array \ 206 ? (v).items \ 207 : _memcpy_free((v).init_array, (v).items, \ 208 (v).size * sizeof((v).items[0]))) \ 209 : ((v).items == (v).init_array \ 210 ? memcpy(xmalloc((v).capacity * sizeof((v).items[0])), \ 211 (v).items, \ 212 (v).size * sizeof((v).items[0])) \ 213 : xrealloc((v).items, \ 214 (v).capacity * sizeof((v).items[0]))))) 215 216/// Resize vector with preallocated array when it is full 217/// 218/// @param[out] v Vector to resize. 219#define kvi_resize_full (v ) \ 220/* ARRAY_SIZE((v).init_array) is the minimal capacity of this vector. */ \ 221/* Thus when vector is full capacity may not be zero and it is safe */ \ 222/* not to bother with checking whether (v).capacity is 0. But now */ \ 223/* capacity is not guaranteed to have size that is a power of 2, it is */ \ 224/* hard to fix this here and is not very necessary if users will use */ \ 225/* 2^x initial array size. */ \ 226 kvi_resize(v, (v).capacity << 1) 227 228/// fit at least "len" more items 229#define kvi_ensure_more_space (v ,len ) \ 230 do { \ 231 if ((v).capacity < (v).size + (len)) { \ 232 (v).capacity = (v).size + (len); \ 233 kv_roundup32((v).capacity); \ 234 kvi_resize((v), (v).capacity); \ 235 } \ 236 } while (0) 237 238#define kvi_concat_len (v ,data ,len ) \ 239 do { \ 240 if ((len) > 0) { \ 241 kvi_ensure_more_space(v, len); \ 242 assert((v).items); \ 243 memcpy((v).items + (v).size, (data), sizeof((v).items[0]) * (len)); \ 244 (v).size = (v).size + (len); \ 245 } \ 246 } while (0) 247 248#define kvi_concat (v ,str ) kvi_concat_len(v, str, strlen(str)) 249#define kvi_splice (v1 ,v0 ) kvi_concat_len(v1, (v0).items, (v0).size) 250 251/// Get location where to store new element to a vector with preallocated array 252/// 253/// @param[in,out] v Vector to push to. 254/// 255/// @return Pointer to the place where new value should be stored. 256#define kvi_pushp (v ) \ 257 ((((v).size == (v).capacity) ? (kvi_resize_full(v), 0) : 0), \ 258 ((v).items + ((v).size++))) 259 260/// Push value to a vector with preallocated array 261/// 262/// @param[out] v Vector to push to. 263/// @param[in] x Value to push. 264#define kvi_push (v ,x ) \ 265 (*kvi_pushp(v) = (x)) 266 267/// Copy a vector to a preallocated vector 268/// 269/// @param[out] v1 destination 270/// @param[in] v0 source (can be either vector or preallocated vector) 271#define kvi_copy (v1 ,v0 ) \ 272 do { \ 273 if ((v1).capacity < (v0).size) { \ 274 kvi_resize(v1, (v0).size); \ 275 } \ 276 (v1).size = (v0).size; \ 277 memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ 278 } while (0) 279 280/// Free array of elements of a vector with preallocated array if needed 281/// 282/// @param[out] v Vector to free. 283#define kvi_destroy (v ) \ 284 do { \ 285 if ((v).items != (v).init_array) { \ 286 XFREE_CLEAR((v).items); \ 287 } \ 288 } while (0) 289 290#endif // NVIM_LIB_KVEC_H