2 Copyright (c) 2008-2017, Troy D. Hanson http://troydhanson.github.com/uthash/
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
15 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 /* a ring-buffer implementation using macros
26 #ifndef UTRINGBUFFER_H
27 #define UTRINGBUFFER_H
29 #define UTRINGBUFFER_VERSION 2.0.2
33 #include "utarray.h" // for "UT_icd"
36 unsigned i
; /* index of next available slot; wraps at n */
37 unsigned n
; /* capacity */
38 unsigned char f
; /* full */
39 UT_icd icd
; /* initializer, copy and destructor functions */
40 char *d
; /* n slots of size icd->sz */
43 #define utringbuffer_init(a, _n, _icd) do { \
44 memset(a, 0, sizeof(UT_ringbuffer)); \
47 if ((a)->n) { (a)->d = (char*)malloc((a)->n * (_icd)->sz); } \
50 #define utringbuffer_clear(a) do { \
51 if ((a)->icd.dtor) { \
54 for (_ut_i = 0; _ut_i < (a)->n; ++_ut_i) { \
55 (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i)); \
59 for (_ut_i = 0; _ut_i < (a)->i; ++_ut_i) { \
60 (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i)); \
68 #define utringbuffer_done(a) do { \
69 utringbuffer_clear(a); \
70 free((a)->d); (a)->d = NULL; \
74 #define utringbuffer_new(a,n,_icd) do { \
75 a = (UT_ringbuffer*)malloc(sizeof(UT_ringbuffer)); \
76 utringbuffer_init(a, n, _icd); \
79 #define utringbuffer_free(a) do { \
80 utringbuffer_done(a); \
84 #define utringbuffer_push_back(a,p) do { \
85 if ((a)->icd.dtor && (a)->f) { (a)->icd.dtor(_utringbuffer_internalptr(a,(a)->i)); } \
86 if ((a)->icd.copy) { (a)->icd.copy( _utringbuffer_internalptr(a,(a)->i), p); } \
87 else { memcpy(_utringbuffer_internalptr(a,(a)->i), p, (a)->icd.sz); }; \
88 if (++(a)->i == (a)->n) { (a)->i = 0; (a)->f = 1; } \
91 #define utringbuffer_len(a) ((a)->f ? (a)->n : (a)->i)
92 #define utringbuffer_empty(a) ((a)->i == 0 && !(a)->f)
93 #define utringbuffer_full(a) ((a)->f != 0)
95 #define _utringbuffer_real_idx(a,j) ((a)->f ? ((j) + (a)->i) % (a)->n : (j))
96 #define _utringbuffer_internalptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j))))
97 #define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
99 #define _utringbuffer_fake_idx(a,j) ((a)->f ? ((j) + (a)->n - (a)->i) % (a)->n : (j))
100 #define _utringbuffer_internalidx(a,e) (((char*)(e) >= (a)->d) ? (((char*)(e) - (a)->d)/(a)->icd.sz) : -1)
101 #define utringbuffer_eltidx(a,e) _utringbuffer_fake_idx(a, _utringbuffer_internalidx(a,e))
103 #define utringbuffer_front(a) utringbuffer_eltptr(a,0)
104 #define utringbuffer_next(a,e) ((e)==NULL ? utringbuffer_front(a) : utringbuffer_eltptr(a, utringbuffer_eltidx(a,e)+1))
105 #define utringbuffer_prev(a,e) ((e)==NULL ? utringbuffer_back(a) : utringbuffer_eltptr(a, utringbuffer_eltidx(a,e)-1))
106 #define utringbuffer_back(a) (utringbuffer_empty(a) ? NULL : utringbuffer_eltptr(a, utringbuffer_len(a) - 1))
108 #endif /* UTRINGBUFFER_H */