Macros
ringbuf.h File Reference
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "nvim/func_attr.h"
#include "nvim/memory.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

◆ _RINGBUF_LENGTH

#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))

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.

◆ _RINGBUF_NEXT

#define _RINGBUF_NEXT (   rb,
  var 
)    ((var) == (rb)->buf_end ? (rb)->buf : (var) + 1)

◆ _RINGBUF_PREV

#define _RINGBUF_PREV (   rb,
  var 
)    ((var) == (rb)->buf ? (rb)->buf_end : (var) - 1)

◆ RINGBUF_DUMMY_FREE

#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.

◆ RINGBUF_FORALL

#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_--)

Iterate over all ringbuf values

Parameters
rbRing buffer to iterate over.
RBTypeType of the ring buffer element.
varnameVariable name.

◆ RINGBUF_INIT

#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 *);

◆ RINGBUF_ITER_BACK

#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_--)

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.

◆ RINGBUF_STATIC

#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, \
};

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.

◆ RINGBUF_TYPEDEF

#define RINGBUF_TYPEDEF (   TypeName,
  RBType 
)
Value:
typedef struct { \
RBType *buf; \
RBType *next; \
RBType *first; \
RBType *buf_end; \
} TypeName##RingBuffer;

Define a ring buffer structure

Parameters
TypeNameRing buffer type name. Actual type name will be {TypeName}RingBuffer.
RBTypeType of the single ring buffer element.
_
#define _(x)
Definition: gettext.h:20
buf
char *const buf
Definition: encode.c:235
next
m next
Definition: window.c:6850
_RINGBUF_NEXT
#define _RINGBUF_NEXT(rb, var)
Definition: ringbuf.h:37
_RINGBUF_LENGTH
#define _RINGBUF_LENGTH(rb)
Definition: ringbuf.h:26
_RINGBUF_PREV
#define _RINGBUF_PREV(rb, var)
Definition: ringbuf.h:39
NULL
return NULL
Definition: eval.c:10355