refactor: enable formatting for files under lib

This commit is contained in:
Dundar Göc
2021-10-12 17:23:50 +02:00
parent 29b718d04c
commit f98b8d2d44
6 changed files with 986 additions and 964 deletions

View File

@@ -1,5 +1,3 @@
// uncrustify:off
/*-
* Copyright 1997-1999, 2001, John-Mark Gurney.
* 2008-2009, Attractive Chaos <attractor@live.co.uk>
@@ -36,10 +34,10 @@
#ifndef NVIM_LIB_KBTREE_H
#define NVIM_LIB_KBTREE_H
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "nvim/memory.h"
@@ -48,7 +46,7 @@
#define __KB_KEY(type, x) (x->key)
#define __KB_PTR(btr, x) (x->ptr)
#define __KB_TREE_T(name,key_t,T) \
#define __KB_TREE_T(name, key_t, T) \
typedef struct kbnode_##name##_s kbnode_##name##_t; \
struct kbnode_##name##_s { \
int32_t n; \
@@ -71,12 +69,12 @@
} kbitr_##name##_t; \
#define __kb_destroy(kbnode_t,b) do { \
#define __kb_destroy(kbnode_t, b) do { \
int i; \
unsigned int max = 8; \
kbnode_t *x, **top, **stack = 0; \
if (b->root) { \
top = stack = (kbnode_t**)xcalloc(max, sizeof(kbnode_t*)); \
top = stack = (kbnode_t **)xcalloc(max, sizeof(kbnode_t *)); \
*top++ = (b)->root; \
while (top != stack) { \
x = *--top; \
@@ -85,7 +83,7 @@
if (__KB_PTR(b, x)[i]) { \
if (top - stack == (int)max) { \
max <<= 1; \
stack = (kbnode_t**)xrealloc(stack, max * sizeof(kbnode_t*)); \
stack = (kbnode_t **)xrealloc(stack, max * sizeof(kbnode_t *)); \
top = stack + (max>>1); \
} \
*top++ = __KB_PTR(b, x)[i]; \
@@ -94,10 +92,11 @@
} \
} \
XFREE_CLEAR(stack); \
} while (0)
} while (0)
#define __KB_GET_AUX1(name, key_t, kbnode_t, __cmp) \
static inline int __kb_getp_aux_##name(const kbnode_t * __restrict x, key_t * __restrict k, int *r) \
static inline int __kb_getp_aux_##name(const kbnode_t * __restrict x, key_t * __restrict k, \
int *r) \
{ \
int tr, *rr, begin = 0, end = x->n; \
if (x->n == 0) return -1; \
@@ -134,7 +133,8 @@
}
#define __KB_INTERVAL(name, key_t, kbnode_t) \
static inline void kb_intervalp_##name(kbtree_##name##_t *b, key_t * __restrict k, key_t **lower, key_t **upper) \
static inline void kb_intervalp_##name(kbtree_##name##_t *b, key_t * __restrict k, key_t **lower, \
key_t **upper) \
{ \
if (!b->root) { \
return; \
@@ -164,16 +164,18 @@
static inline void __kb_split_##name(kbtree_##name##_t *b, kbnode_t *x, int i, kbnode_t *y) \
{ \
kbnode_t *z; \
z = (kbnode_t*)xcalloc(1, y->is_internal? ILEN : sizeof(kbnode_##name##_t)); \
z = (kbnode_t *)xcalloc(1, y->is_internal? ILEN : sizeof(kbnode_##name##_t)); \
++b->n_nodes; \
z->is_internal = y->is_internal; \
z->n = T - 1; \
memcpy(__KB_KEY(key_t, z), &__KB_KEY(key_t, y)[T], sizeof(key_t) * (T - 1)); \
if (y->is_internal) memcpy(__KB_PTR(b, z), &__KB_PTR(b, y)[T], sizeof(void*) * T); \
if (y->is_internal) memcpy(__KB_PTR(b, z), &__KB_PTR(b, y)[T], sizeof(void *) * T); \
y->n = T - 1; \
memmove(&__KB_PTR(b, x)[i + 2], &__KB_PTR(b, x)[i + 1], sizeof(void*) * (unsigned int)(x->n - i)); \
memmove(&__KB_PTR(b, x)[i + 2], &__KB_PTR(b, \
x)[i + 1], sizeof(void *) * (unsigned int)(x->n - i)); \
__KB_PTR(b, x)[i + 1] = z; \
memmove(&__KB_KEY(key_t, x)[i + 1], &__KB_KEY(key_t, x)[i], sizeof(key_t) * (unsigned int)(x->n - i)); \
memmove(&__KB_KEY(key_t, x)[i + 1], &__KB_KEY(key_t, x)[i], \
sizeof(key_t) * (unsigned int)(x->n - i)); \
__KB_KEY(key_t, x)[i] = __KB_KEY(key_t, y)[T - 1]; \
++x->n; \
} \
@@ -184,7 +186,9 @@
if (x->is_internal == 0) { \
i = __kb_getp_aux_##name(x, k, 0); \
if (i != x->n - 1) \
memmove(&__KB_KEY(key_t, x)[i + 2], &__KB_KEY(key_t, x)[i + 1], (unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_KEY(key_t, x)[i + 2], &__KB_KEY(key_t, \
x)[i + 1], \
(unsigned int)(x->n - i - 1) * sizeof(key_t)); \
ret = &__KB_KEY(key_t, x)[i + 1]; \
*ret = *k; \
++x->n; \
@@ -201,7 +205,7 @@
static inline key_t *kb_putp_##name(kbtree_##name##_t *b, key_t * __restrict k) \
{ \
if (!b->root) { \
b->root = (kbnode_t*)xcalloc(1, ILEN); \
b->root = (kbnode_t *)xcalloc(1, ILEN); \
++b->n_nodes; \
} \
kbnode_t *r, *s; \
@@ -209,7 +213,7 @@
r = b->root; \
if (r->n == 2 * T - 1) { \
++b->n_nodes; \
s = (kbnode_t*)xcalloc(1, ILEN); \
s = (kbnode_t *)xcalloc(1, ILEN); \
b->root = s; s->is_internal = 1; s->n = 0; \
__KB_PTR(b, s)[0] = r; \
__kb_split_##name(b, s, 0, r); \
@@ -224,7 +228,8 @@
#define __KB_DEL(name, key_t, kbnode_t, T) \
static inline key_t __kb_delp_aux_##name(kbtree_##name##_t *b, kbnode_t *x, key_t * __restrict k, int s) \
static inline key_t __kb_delp_aux_##name(kbtree_##name##_t *b, kbnode_t *x, key_t * __restrict k, \
int s) \
{ \
int yn, zn, i, r = 0; \
kbnode_t *xp, *y, *z; \
@@ -237,7 +242,9 @@
if (x->is_internal == 0) { \
if (s == 2) ++i; \
kp = __KB_KEY(key_t, x)[i]; \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, x)[i + 1], (unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, \
x)[i + 1], \
(unsigned int)(x->n - i - 1) * sizeof(key_t)); \
--x->n; \
return kp; \
} \
@@ -256,10 +263,16 @@
y = __KB_PTR(b, x)[i]; z = __KB_PTR(b, x)[i + 1]; \
__KB_KEY(key_t, y)[y->n++] = *k; \
memmove(&__KB_KEY(key_t, y)[y->n], __KB_KEY(key_t, z), (unsigned int)z->n * sizeof(key_t)); \
if (y->is_internal) memmove(&__KB_PTR(b, y)[y->n], __KB_PTR(b, z), (unsigned int)(z->n + 1) * sizeof(void*)); \
if (y->is_internal) memmove(&__KB_PTR(b, y)[y->n], __KB_PTR(b, \
z), \
(unsigned int)(z->n + 1) * sizeof(void *)); \
y->n += z->n; \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, x)[i + 1], (unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i + 1], &__KB_PTR(b, x)[i + 2], (unsigned int)(x->n - i - 1) * sizeof(void*)); \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, \
x)[i + 1], \
(unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i + 1], &__KB_PTR(b, \
x)[i + 2], \
(unsigned int)(x->n - i - 1) * sizeof(void *)); \
--x->n; \
XFREE_CLEAR(z); \
return __kb_delp_aux_##name(b, y, k, s); \
@@ -269,7 +282,9 @@
if ((xp = __KB_PTR(b, x)[i])->n == T - 1) { \
if (i > 0 && (y = __KB_PTR(b, x)[i - 1])->n >= T) { \
memmove(&__KB_KEY(key_t, xp)[1], __KB_KEY(key_t, xp), (unsigned int)xp->n * sizeof(key_t)); \
if (xp->is_internal) memmove(&__KB_PTR(b, xp)[1], __KB_PTR(b, xp), (unsigned int)(xp->n + 1) * sizeof(void*)); \
if (xp->is_internal) memmove(&__KB_PTR(b, xp)[1], __KB_PTR(b, \
xp), \
(unsigned int)(xp->n + 1) * sizeof(void *)); \
__KB_KEY(key_t, xp)[0] = __KB_KEY(key_t, x)[i - 1]; \
__KB_KEY(key_t, x)[i - 1] = __KB_KEY(key_t, y)[y->n - 1]; \
if (xp->is_internal) __KB_PTR(b, xp)[0] = __KB_PTR(b, y)[y->n]; \
@@ -280,24 +295,38 @@
if (xp->is_internal) __KB_PTR(b, xp)[xp->n] = __KB_PTR(b, y)[0]; \
--y->n; \
memmove(__KB_KEY(key_t, y), &__KB_KEY(key_t, y)[1], (unsigned int)y->n * sizeof(key_t)); \
if (y->is_internal) memmove(__KB_PTR(b, y), &__KB_PTR(b, y)[1], (unsigned int)(y->n + 1) * sizeof(void*)); \
if (y->is_internal) memmove(__KB_PTR(b, y), &__KB_PTR(b, \
y)[1], \
(unsigned int)(y->n + 1) * sizeof(void *)); \
} else if (i > 0 && (y = __KB_PTR(b, x)[i - 1])->n == T - 1) { \
__KB_KEY(key_t, y)[y->n++] = __KB_KEY(key_t, x)[i - 1]; \
memmove(&__KB_KEY(key_t, y)[y->n], __KB_KEY(key_t, xp), (unsigned int)xp->n * sizeof(key_t)); \
if (y->is_internal) memmove(&__KB_PTR(b, y)[y->n], __KB_PTR(b, xp), (unsigned int)(xp->n + 1) * sizeof(void*)); \
memmove(&__KB_KEY(key_t, y)[y->n], __KB_KEY(key_t, xp), \
(unsigned int)xp->n * sizeof(key_t)); \
if (y->is_internal) memmove(&__KB_PTR(b, y)[y->n], __KB_PTR(b, \
xp), \
(unsigned int)(xp->n + 1) * sizeof(void *)); \
y->n += xp->n; \
memmove(&__KB_KEY(key_t, x)[i - 1], &__KB_KEY(key_t, x)[i], (unsigned int)(x->n - i) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i], &__KB_PTR(b, x)[i + 1], (unsigned int)(x->n - i) * sizeof(void*)); \
memmove(&__KB_KEY(key_t, x)[i - 1], &__KB_KEY(key_t, \
x)[i], \
(unsigned int)(x->n - i) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i], &__KB_PTR(b, \
x)[i + 1], (unsigned int)(x->n - i) * sizeof(void *)); \
--x->n; \
XFREE_CLEAR(xp); \
xp = y; \
} else if (i < x->n && (y = __KB_PTR(b, x)[i + 1])->n == T - 1) { \
__KB_KEY(key_t, xp)[xp->n++] = __KB_KEY(key_t, x)[i]; \
memmove(&__KB_KEY(key_t, xp)[xp->n], __KB_KEY(key_t, y), (unsigned int)y->n * sizeof(key_t)); \
if (xp->is_internal) memmove(&__KB_PTR(b, xp)[xp->n], __KB_PTR(b, y), (unsigned int)(y->n + 1) * sizeof(void*)); \
memmove(&__KB_KEY(key_t, xp)[xp->n], __KB_KEY(key_t, y), \
(unsigned int)y->n * sizeof(key_t)); \
if (xp->is_internal) memmove(&__KB_PTR(b, xp)[xp->n], __KB_PTR(b, y), \
(unsigned int)(y->n + 1) * sizeof(void *)); \
xp->n += y->n; \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, x)[i + 1], (unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i + 1], &__KB_PTR(b, x)[i + 2], (unsigned int)(x->n - i - 1) * sizeof(void*)); \
memmove(&__KB_KEY(key_t, x)[i], &__KB_KEY(key_t, \
x)[i + 1], \
(unsigned int)(x->n - i - 1) * sizeof(key_t)); \
memmove(&__KB_PTR(b, x)[i + 1], &__KB_PTR(b, \
x)[i + 2], \
(unsigned int)(x->n - i - 1) * sizeof(void *)); \
--x->n; \
XFREE_CLEAR(y); \
} \
@@ -373,7 +402,8 @@
if (itr->p->x && itr->p->i >= 0) return 1; \
} \
} \
static inline int kb_itr_getp_##name(kbtree_##name##_t *b, key_t * __restrict k, kbitr_##name##_t *itr) \
static inline int kb_itr_getp_##name(kbtree_##name##_t *b, key_t * __restrict k, \
kbitr_##name##_t *itr) \
{ \
if (b->n_keys == 0) { \
itr->p = NULL; \
@@ -396,7 +426,7 @@
} \
static inline int kb_itr_get_##name(kbtree_##name##_t *b, key_t k, kbitr_##name##_t *itr) \
{ \
return kb_itr_getp_##name(b,&k,itr); \
return kb_itr_getp_##name(b, &k, itr); \
} \
static inline void kb_del_itr_##name(kbtree_##name##_t *b, kbitr_##name##_t *itr) \
{ \
@@ -406,7 +436,8 @@
}
#define KBTREE_INIT(name, key_t, __cmp, T) \
KBTREE_INIT_IMPL(name, key_t, kbnode_##name##_t, __cmp, T, (sizeof(kbnode_##name##_t)+(2*T)*sizeof(void *)))
KBTREE_INIT_IMPL(name, key_t, kbnode_##name##_t, __cmp, T, \
(sizeof(kbnode_##name##_t)+(2*T)*sizeof(void *)))
#define KBTREE_INIT_IMPL(name, key_t, kbnode_t, __cmp, T, ILEN) \
__KB_TREE_T(name, key_t, T) \

View File

@@ -1,5 +1,3 @@
// uncrustify:off
/* The MIT License
Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
@@ -23,14 +21,14 @@
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
*/
/*
Example:
#include "nvim/khash.h"
KHASH_MAP_INIT_INT(32, char)
int main() {
#include "nvim/khash.h"
KHASH_MAP_INIT_INT(32, char)
int main() {
int ret, is_missing;
khiter_t k;
khash_t(32) *h = kh_init(32);
@@ -44,8 +42,8 @@ int main() {
if (kh_exist(h, k)) kh_value(h, k) = 1;
kh_destroy(32, h);
return 0;
}
*/
}
*/
/*
2013-05-02 (0.2.8):
@@ -113,7 +111,7 @@ int main() {
2008-08-31 (0.1.1):
* Added destructor
*/
*/
#ifndef NVIM_LIB_KHASH_H
@@ -127,16 +125,15 @@ int main() {
#define AC_VERSION_KHASH_H "0.2.8"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include "nvim/memory.h"
#include <stdlib.h>
#include <string.h>
#include "nvim/func_attr.h"
#include "nvim/memory.h"
/* compiler specific configuration */
// compiler specific configuration
#if UINT_MAX == 0xffffffffu
typedef unsigned int khint32_t;
@@ -151,9 +148,9 @@ typedef unsigned long long khint64_t;
#endif
#ifdef _MSC_VER
#define kh_inline __inline
# define kh_inline __inline
#else
#define kh_inline inline
# define kh_inline inline
#endif
typedef khint32_t khint_t;
@@ -170,20 +167,21 @@ typedef khint_t khiter_t;
#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)
#ifndef kroundup32
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
# define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, \
++(x))
#endif
#ifndef kcalloc
#define kcalloc(N,Z) xcalloc(N,Z)
# define kcalloc(N, Z) xcalloc(N, Z)
#endif
#ifndef kmalloc
#define kmalloc(Z) xmalloc(Z)
# define kmalloc(Z) xmalloc(Z)
#endif
#ifndef krealloc
#define krealloc(P,Z) xrealloc(P,Z)
# define krealloc(P, Z) xrealloc(P, Z)
#endif
#ifndef kfree
#define kfree(P) XFREE_CLEAR(P)
# define kfree(P) XFREE_CLEAR(P)
#endif
#define __ac_HASH_UPPER 0.77
@@ -211,7 +209,7 @@ typedef khint_t khiter_t;
SCOPE kh_##name##_t *kh_init_##name(void) \
REAL_FATTR_UNUSED; \
SCOPE kh_##name##_t *kh_init_##name(void) { \
return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \
return (kh_##name##_t *)kcalloc(1, sizeof(kh_##name##_t)); \
} \
SCOPE void kh_dealloc_##name(kh_##name##_t *h) \
REAL_FATTR_UNUSED; \
@@ -276,17 +274,16 @@ typedef khint_t khiter_t;
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) { \
j = 0; \
} else { /* hash table size to be changed (shrink or expand); rehash */ \
new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) \
new_flags = (khint32_t *)kmalloc(__ac_fsize(new_n_buckets) \
* sizeof(khint32_t)); \
memset(new_flags, 0xaa, \
__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \
khkey_t *new_keys = (khkey_t*)krealloc( \
(void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
khkey_t *new_keys = (khkey_t *)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
h->keys = new_keys; \
if (kh_is_map) { \
khval_t *new_vals = (khval_t*)krealloc( \
(void *)h->vals, new_n_buckets * sizeof(khval_t)); \
khval_t *new_vals = \
(khval_t *)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
h->vals = new_vals; \
} \
} /* otherwise shrink */ \
@@ -337,10 +334,10 @@ typedef khint_t khiter_t;
} \
} \
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
h->keys = (khkey_t*)krealloc((void *)h->keys, \
h->keys = (khkey_t *)krealloc((void *)h->keys, \
new_n_buckets * sizeof(khkey_t)); \
if (kh_is_map) { \
h->vals = (khval_t*)krealloc((void *)h->vals, \
h->vals = (khval_t *)krealloc((void *)h->vals, \
new_n_buckets * sizeof(khval_t)); \
} \
} \
@@ -431,7 +428,7 @@ typedef khint_t khiter_t;
#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
/* --- BEGIN OF HASH FUNCTIONS --- */
// --- BEGIN OF HASH FUNCTIONS ---
/*! @function
@abstract Integer hash function
@@ -461,7 +458,9 @@ typedef khint_t khiter_t;
static kh_inline khint_t __ac_X31_hash_string(const char *s)
{
khint_t h = (khint_t)*s;
if (h) for (++s ; *s; ++s) h = (h << 5) - h + (uint8_t)*s;
if (h) {
for (++s ; *s; ++s) { h = (h << 5) - h + (uint8_t)*s; }
}
return h;
}
/*! @function
@@ -487,9 +486,9 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
}
#define kh_int_hash_func2(k) __ac_Wang_hash((khint_t)key)
/* --- END OF HASH FUNCTIONS --- */
// --- END OF HASH FUNCTIONS ---
/* Other convenient macros... */
// Other convenient macros...
/*!
@abstract Type of the hash table.
@@ -630,9 +629,9 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
*/
#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
if (!kh_exist(h,__i)) continue; \
(kvar) = kh_key(h,__i); \
(vvar) = kh_val(h,__i); \
if (!kh_exist(h, __i)) continue; \
(kvar) = kh_key(h, __i); \
(vvar) = kh_val(h, __i); \
code; \
} }
@@ -644,8 +643,8 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
*/
#define kh_foreach_value(h, vvar, code) { khint_t __i; \
for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
if (!kh_exist(h,__i)) continue; \
(vvar) = kh_val(h,__i); \
if (!kh_exist(h, __i)) continue; \
(vvar) = kh_val(h, __i); \
code; \
} }
@@ -667,7 +666,7 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
} \
}
/* More conenient interfaces */
// More conenient interfaces
/*! @function
@abstract Instantiate a hash set containing integer keys

View File

@@ -1,5 +1,3 @@
// uncrustify:off
/* The MIT License
Copyright (c) 2008-2009, by Attractive Chaos <attractor@live.co.uk>
@@ -23,16 +21,16 @@
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
*/
#ifndef _AC_KLIST_H
#define _AC_KLIST_H
#include <stdlib.h>
#include <assert.h>
#include <stdlib.h>
#include "nvim/memory.h"
#include "nvim/func_attr.h"
#include "nvim/memory.h"
#define KMEMPOOL_INIT(name, kmptype_t, kmpfree_f) \

View File

@@ -1,5 +1,3 @@
// uncrustify:off
// The MIT License
//
// Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk>
@@ -104,11 +102,11 @@
(*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)]))
@@ -138,8 +136,7 @@
(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
{

View File

@@ -1,5 +1,3 @@
// uncrustify:off
// Queue implemented by circularly-linked list.
//
// Adapted from libuv. Simpler and more efficient than klist.h for implementing

View File

@@ -1,5 +1,3 @@
// uncrustify:off
/// Macros-based ring buffer implementation.
///
/// Supported functions:
@@ -17,19 +15,24 @@
#ifndef NVIM_LIB_RINGBUF_H
#define NVIM_LIB_RINGBUF_H
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "nvim/memory.h"
#include "nvim/func_attr.h"
#include "nvim/memory.h"
#define _RINGBUF_LENGTH(rb) \
((rb)->first == NULL ? 0 \
: ((rb)->next == (rb)->first) ? (size_t) ((rb)->buf_end - (rb)->buf) + 1 \
: ((rb)->next > (rb)->first) ? (size_t) ((rb)->next - (rb)->first) \
: (size_t) ((rb)->next - (rb)->buf + (rb)->buf_end - (rb)->first + 1))
: ((rb)->next == (rb)->first) ? (size_t)((rb)->buf_end - (rb)->buf) + 1 \
: ((rb)->next > \
(rb)->first) ? (size_t)((rb)->next - \
(rb)->first) \
: (size_t)((rb)->\
next - (rb)->buf + \
(rb)->buf_end - \
(rb)->first + 1))
#define _RINGBUF_NEXT(rb, var) \
((var) == (rb)->buf_end ? (rb)->buf : (var) + 1)
@@ -69,12 +72,12 @@
/// `{TypeName}RingBuffer`.
/// @param RBType Type of the single ring buffer element.
#define RINGBUF_TYPEDEF(TypeName, RBType) \
typedef struct { \
typedef struct { \
RBType *buf; \
RBType *next; \
RBType *first; \
RBType *buf_end; \
} TypeName##RingBuffer;
} TypeName##RingBuffer;
/// Dummy item free macros, for use in RINGBUF_INIT
///
@@ -94,13 +97,13 @@ typedef struct { \
/// @param varname Variable name.
/// @param rbsize Ring buffer size.
#define RINGBUF_STATIC(scope, TypeName, RBType, varname, rbsize) \
static RBType _##varname##_buf[rbsize]; \
scope TypeName##RingBuffer varname = { \
static RBType _##varname##_buf[rbsize]; \
scope TypeName##RingBuffer varname = { \
.buf = _##varname##_buf, \
.next = _##varname##_buf, \
.first = NULL, \
.buf_end = _##varname##_buf + rbsize - 1, \
};
};
/// Initialize a new ring buffer
///
@@ -114,10 +117,10 @@ scope TypeName##RingBuffer varname = { \
///
/// Intended function signature: `void *rbfree(RBType *)`;
#define RINGBUF_INIT(TypeName, funcprefix, RBType, rbfree) \
static inline TypeName##RingBuffer funcprefix##_rb_new(const size_t size) \
static inline TypeName##RingBuffer funcprefix##_rb_new(const size_t size) \
REAL_FATTR_WARN_UNUSED_RESULT; \
static inline TypeName##RingBuffer funcprefix##_rb_new(const size_t size) \
{ \
static inline TypeName##RingBuffer funcprefix##_rb_new(const size_t size) \
{ \
assert(size != 0); \
RBType *buf = xmalloc(size * sizeof(RBType)); \
return (TypeName##RingBuffer) { \
@@ -126,12 +129,12 @@ static inline TypeName##RingBuffer funcprefix##_rb_new(const size_t size) \
.first = NULL, \
.buf_end = buf + size - 1, \
}; \
} \
} \
\
static inline void funcprefix##_rb_free(TypeName##RingBuffer *const rb) \
static inline void funcprefix##_rb_free(TypeName##RingBuffer *const rb) \
REAL_FATTR_UNUSED; \
static inline void funcprefix##_rb_free(TypeName##RingBuffer *const rb) \
{ \
static inline void funcprefix##_rb_free(TypeName##RingBuffer *const rb) \
{ \
if (rb == NULL) { \
return; \
} \
@@ -139,21 +142,21 @@ static inline void funcprefix##_rb_free(TypeName##RingBuffer *const rb) \
rbfree(rbitem); \
} \
XFREE_CLEAR(rb->buf); \
} \
} \
\
static inline void funcprefix##_rb_dealloc(TypeName##RingBuffer *const rb) \
static inline void funcprefix##_rb_dealloc(TypeName##RingBuffer *const rb) \
REAL_FATTR_UNUSED; \
static inline void funcprefix##_rb_dealloc(TypeName##RingBuffer *const rb) \
{ \
static inline void funcprefix##_rb_dealloc(TypeName##RingBuffer *const rb) \
{ \
XFREE_CLEAR(rb->buf); \
} \
} \
\
static inline void funcprefix##_rb_push(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_push(TypeName##RingBuffer *const rb, \
RBType item) \
REAL_FATTR_NONNULL_ARG(1); \
static inline void funcprefix##_rb_push(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_push(TypeName##RingBuffer *const rb, \
RBType item) \
{ \
{ \
if (rb->next == rb->first) { \
rbfree(rb->first); \
rb->first = _RINGBUF_NEXT(rb, rb->first); \
@@ -162,14 +165,14 @@ static inline void funcprefix##_rb_push(TypeName##RingBuffer *const rb, \
} \
*rb->next = item; \
rb->next = _RINGBUF_NEXT(rb, rb->next); \
} \
} \
\
static inline ptrdiff_t funcprefix##_rb_find_idx( \
const TypeName##RingBuffer *const rb, const RBType *const item_p) \
static inline ptrdiff_t funcprefix##_rb_find_idx(const TypeName##RingBuffer *const rb, \
const RBType *const item_p) \
REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE REAL_FATTR_UNUSED; \
static inline ptrdiff_t funcprefix##_rb_find_idx( \
const TypeName##RingBuffer *const rb, const RBType *const item_p) \
{ \
static inline ptrdiff_t funcprefix##_rb_find_idx(const TypeName##RingBuffer *const rb, \
const RBType *const item_p) \
{ \
assert(rb->buf <= item_p); \
assert(rb->buf_end >= item_p); \
if (rb->first == NULL) { \
@@ -179,32 +182,28 @@ static inline ptrdiff_t funcprefix##_rb_find_idx( \
} else { \
return item_p - rb->buf + rb->buf_end - rb->first + 1; \
} \
} \
} \
\
static inline size_t funcprefix##_rb_size( \
const TypeName##RingBuffer *const rb) \
static inline size_t funcprefix##_rb_size(const TypeName##RingBuffer *const rb) \
REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE; \
static inline size_t funcprefix##_rb_size( \
const TypeName##RingBuffer *const rb) \
{ \
return (size_t) (rb->buf_end - rb->buf) + 1; \
} \
static inline size_t funcprefix##_rb_size(const TypeName##RingBuffer *const rb) \
{ \
return (size_t)(rb->buf_end - rb->buf) + 1; \
} \
\
static inline size_t funcprefix##_rb_length( \
const TypeName##RingBuffer *const rb) \
static inline size_t funcprefix##_rb_length(const TypeName##RingBuffer *const rb) \
REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE; \
static inline size_t funcprefix##_rb_length( \
const TypeName##RingBuffer *const rb) \
{ \
static inline size_t funcprefix##_rb_length(const TypeName##RingBuffer *const rb) \
{ \
return _RINGBUF_LENGTH(rb); \
} \
} \
\
static inline RBType *funcprefix##_rb_idx_p( \
const TypeName##RingBuffer *const rb, const size_t idx) \
static inline RBType *funcprefix##_rb_idx_p(const TypeName##RingBuffer *const rb, \
const size_t idx) \
REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE; \
static inline RBType *funcprefix##_rb_idx_p( \
const TypeName##RingBuffer *const rb, const size_t idx) \
{ \
static inline RBType *funcprefix##_rb_idx_p(const TypeName##RingBuffer *const rb, \
const size_t idx) \
{ \
assert(idx <= funcprefix##_rb_size(rb)); \
assert(idx <= funcprefix##_rb_length(rb)); \
if (rb->first + idx > rb->buf_end) { \
@@ -212,25 +211,25 @@ static inline RBType *funcprefix##_rb_idx_p( \
} else { \
return rb->first + idx; \
} \
} \
} \
\
static inline RBType funcprefix##_rb_idx(const TypeName##RingBuffer *const rb, \
static inline RBType funcprefix##_rb_idx(const TypeName##RingBuffer *const rb, \
const size_t idx) \
REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE REAL_FATTR_UNUSED; \
static inline RBType funcprefix##_rb_idx(const TypeName##RingBuffer *const rb, \
static inline RBType funcprefix##_rb_idx(const TypeName##RingBuffer *const rb, \
const size_t idx) \
{ \
{ \
return *funcprefix##_rb_idx_p(rb, idx); \
} \
} \
\
static inline void funcprefix##_rb_insert(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_insert(TypeName##RingBuffer *const rb, \
const size_t idx, \
RBType item) \
REAL_FATTR_NONNULL_ARG(1) REAL_FATTR_UNUSED; \
static inline void funcprefix##_rb_insert(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_insert(TypeName##RingBuffer *const rb, \
const size_t idx, \
RBType item) \
{ \
{ \
assert(idx <= funcprefix##_rb_size(rb)); \
assert(idx <= funcprefix##_rb_length(rb)); \
const size_t length = funcprefix##_rb_length(rb); \
@@ -248,29 +247,29 @@ static inline void funcprefix##_rb_insert(TypeName##RingBuffer *const rb, \
} \
if (insertpos < rb->next) { \
memmove(insertpos + 1, insertpos, \
(size_t) ((uintptr_t) rb->next - (uintptr_t) insertpos)); \
(size_t)((uintptr_t)rb->next - (uintptr_t)insertpos)); \
} else { \
assert(insertpos > rb->first); \
assert(rb->next <= rb->first); \
memmove(rb->buf + 1, rb->buf, \
(size_t) ((uintptr_t) rb->next - (uintptr_t) rb->buf)); \
(size_t)((uintptr_t)rb->next - (uintptr_t)rb->buf)); \
*rb->buf = *rb->buf_end; \
memmove(insertpos + 1, insertpos, \
(size_t) ((uintptr_t) (rb->buf_end + 1) - (uintptr_t) insertpos)); \
(size_t)((uintptr_t)(rb->buf_end + 1) - (uintptr_t)insertpos)); \
} \
*insertpos = item; \
if (length == funcprefix##_rb_size(rb)) { \
rb->first = _RINGBUF_NEXT(rb, rb->first); \
} \
rb->next = _RINGBUF_NEXT(rb, rb->next); \
} \
} \
\
static inline void funcprefix##_rb_remove(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_remove(TypeName##RingBuffer *const rb, \
const size_t idx) \
REAL_FATTR_NONNULL_ARG(1) REAL_FATTR_UNUSED; \
static inline void funcprefix##_rb_remove(TypeName##RingBuffer *const rb, \
static inline void funcprefix##_rb_remove(TypeName##RingBuffer *const rb, \
const size_t idx) \
{ \
{ \
assert(idx < funcprefix##_rb_size(rb)); \
assert(idx < funcprefix##_rb_length(rb)); \
RBType *const rmpos = funcprefix##_rb_idx_p(rb, idx); \
@@ -291,18 +290,18 @@ static inline void funcprefix##_rb_remove(TypeName##RingBuffer *const rb, \
assert(rmpos > rb->first); \
assert(rmpos <= _RINGBUF_PREV(rb, rb->next)); \
memmove(rb->first + 1, rb->first, \
(size_t) ((uintptr_t) rmpos - (uintptr_t) rb->first)); \
(size_t)((uintptr_t)rmpos - (uintptr_t)rb->first)); \
rb->first = _RINGBUF_NEXT(rb, rb->first); \
} else if (rmpos < rb->next) { \
memmove(rmpos, rmpos + 1, \
(size_t) ((uintptr_t) rb->next - (uintptr_t) rmpos)); \
(size_t)((uintptr_t)rb->next - (uintptr_t)rmpos)); \
rb->next = _RINGBUF_PREV(rb, rb->next); \
} else { \
assert(rb->first < rb->buf_end); \
memmove(rb->first + 1, rb->first, \
(size_t) ((uintptr_t) rmpos - (uintptr_t) rb->first)); \
(size_t)((uintptr_t)rmpos - (uintptr_t)rb->first)); \
rb->first = _RINGBUF_NEXT(rb, rb->first); \
} \
}
}
#endif // NVIM_LIB_RINGBUF_H