Macros
ringbuf.h File Reference
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include "nvim/memory.h"
#include "nvim/func_attr.h"

Go to the source code of this file.

Macros

#define _RINGBUF_LENGTH(rb)
 
#define _RINGBUF_NEXT(rb, var)   ((var) == (rb)->buf_end ? (rb)->buf : (var) + 1)
 
#define _RINGBUF_PREV(rb, var)   ((var) == (rb)->buf ? (rb)->buf_end : (var) - 1)
 
#define RINGBUF_FORALL(rb, RBType, varname)
 
#define RINGBUF_ITER_BACK(rb, RBType, varname)
 
#define RINGBUF_TYPEDEF(TypeName, RBType)
 
#define RINGBUF_DUMMY_FREE(item)
 
#define RINGBUF_STATIC(scope, TypeName, RBType, varname, rbsize)
 
#define RINGBUF_INIT(TypeName, funcprefix, RBType, rbfree)
 

Macro Definition Documentation

#define _RINGBUF_LENGTH (   rb)
Value:
((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))
return NULL
Definition: eval.c:22212
char *const buf
Definition: encode.c:231

Macros-based ring buffer implementation.

Supported functions:

  • new: allocates new ring buffer.
  • dealloc: free ring buffer itself.
  • free: free ring buffer and all its elements.
  • push: adds element to the end of the buffer.
  • length: get buffer length.
  • size: size of the ring buffer.
  • idx: get element at given index.
  • idx_p: get pointer to the element at given index.
  • insert: insert element at given position.
  • remove: remove element from given position.
#define _RINGBUF_NEXT (   rb,
  var 
)    ((var) == (rb)->buf_end ? (rb)->buf : (var) + 1)
#define _RINGBUF_PREV (   rb,
  var 
)    ((var) == (rb)->buf ? (rb)->buf_end : (var) - 1)
#define RINGBUF_DUMMY_FREE (   item)

Dummy item free macros, for use in RINGBUF_INIT

This macros actually does nothing.

Parameters
[in]itemItem to be freed.
#define RINGBUF_FORALL (   rb,
  RBType,
  varname 
)
Value:
size_t varname##_length_fa_ = _RINGBUF_LENGTH(rb); \
for (RBType *varname = ((rb)->first == NULL ? (rb)->next : (rb)->first); \
varname##_length_fa_; \
(varname = _RINGBUF_NEXT(rb, varname)), \
varname##_length_fa_--)
return NULL
Definition: eval.c:22212
for(size_t i=1;i< ARRAY_SIZE(argv);i++)
Definition: typval.c:1215
#define _RINGBUF_LENGTH(rb)
Definition: ringbuf.h:26
#define _RINGBUF_NEXT(rb, var)
Definition: ringbuf.h:32

Iterate over all ringbuf values

Parameters
rbRing buffer to iterate over.
RBTypeType of the ring buffer element.
varnameVariable name.
#define RINGBUF_INIT (   TypeName,
  funcprefix,
  RBType,
  rbfree 
)

Initialize a new ring buffer

Parameters
TypeNameRing buffer type name. Actual type name will be {TypeName}RingBuffer.
funcprefixPrefix for all ring buffer functions. Function name will look like {funcprefix}_rb_{function_name}.
RBTypeType of the single ring buffer element.
rbfreeFunction used to free ring buffer element. May be a macros like #define RBFREE(item) (to skip freeing).

Intended function signature: void *rbfree(RBType *);

#define RINGBUF_ITER_BACK (   rb,
  RBType,
  varname 
)
Value:
size_t varname##_length_ib_ = _RINGBUF_LENGTH(rb); \
for (varname = ((rb)->next == (rb)->buf ? (rb)->buf_end : (rb)->next - 1); \
varname##_length_ib_; \
(varname = _RINGBUF_PREV(rb, varname)), \
varname##_length_ib_--)
for(size_t i=1;i< ARRAY_SIZE(argv);i++)
Definition: typval.c:1215
char *const buf
Definition: encode.c:231
#define _RINGBUF_LENGTH(rb)
Definition: ringbuf.h:26
#define _RINGBUF_PREV(rb, var)
Definition: ringbuf.h:34

Iterate over all ringbuf values, from end to the beginning

Unlike previous RINGBUF_FORALL uses already defined variable, in place of defining variable in the cycle body.

Parameters
rbRing buffer to iterate over.
RBTypeType of the ring buffer element.
varnameVariable name.
#define RINGBUF_STATIC (   scope,
  TypeName,
  RBType,
  varname,
  rbsize 
)
Value:
static RBType _##varname##_buf[rbsize]; \
scope TypeName##RingBuffer varname = { \
.buf = _##varname##_buf, \
.next = _##varname##_buf, \
.first = NULL, \
.buf_end = _##varname##_buf + rbsize - 1, \
};
#define _(x)
Definition: gettext.h:15
return NULL
Definition: eval.c:22212

Static ring buffer

Warning
Ring buffers created with this macros must neither be freed nor deallocated.
Parameters
scopeRing buffer scope.
TypeNameRing buffer type name.
RBTypeType of the single ring buffer element.
varnameVariable name.
rbsizeRing buffer size.
#define RINGBUF_TYPEDEF (   TypeName,
  RBType 
)
Value:
typedef struct { \
RBType *buf; \
RBType *next; \
RBType *first; \
RBType *buf_end; \
} TypeName##RingBuffer;
char *const buf
Definition: encode.c:231

Define a ring buffer structure

Parameters
TypeNameRing buffer type name. Actual type name will be {TypeName}RingBuffer.
RBTypeType of the single ring buffer element.