summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/librdkafka-2.1.0/src/rdsysqueue.h
blob: ecba4154eb5c8405435bf27facd3b994b28d7816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * librd - Rapid Development C library
 *
 * Copyright (c) 2012-2013, Magnus Edenhill
 * Copyright (c) 2012-2013, Andreas Öman
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


/*

 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _RDSYSQUEUE_H_
#define _RDSYSQUEUE_H_

#include "queue.h"

/*
 * Complete missing LIST-ops
 */

#ifndef LIST_FOREACH
#define LIST_FOREACH(var, head, field)                                         \
        for ((var) = ((head)->lh_first); (var); (var) = ((var)->field.le_next))
#endif

#ifndef LIST_EMPTY
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
#endif

#ifndef LIST_FIRST
#define LIST_FIRST(head) ((head)->lh_first)
#endif

#ifndef LIST_NEXT
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#endif

#ifndef LIST_INSERT_BEFORE
#define LIST_INSERT_BEFORE(listelm, elm, field)                                \
        do {                                                                   \
                (elm)->field.le_prev      = (listelm)->field.le_prev;          \
                (elm)->field.le_next      = (listelm);                         \
                *(listelm)->field.le_prev = (elm);                             \
                (listelm)->field.le_prev  = &(elm)->field.le_next;             \
        } while (/*CONSTCOND*/ 0)
#endif

/*
 * Complete missing TAILQ-ops
 */

#ifndef TAILQ_HEAD_INITIALIZER
#define TAILQ_HEAD_INITIALIZER(head)                                           \
        { NULL, &(head).tqh_first }
#endif

#ifndef TAILQ_INSERT_BEFORE
#define TAILQ_INSERT_BEFORE(listelm, elm, field)                               \
        do {                                                                   \
                (elm)->field.tqe_prev      = (listelm)->field.tqe_prev;        \
                (elm)->field.tqe_next      = (listelm);                        \
                *(listelm)->field.tqe_prev = (elm);                            \
                (listelm)->field.tqe_prev  = &(elm)->field.tqe_next;           \
        } while (0)
#endif

#ifndef TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)                                        \
        for ((var) = ((head)->tqh_first); (var);                               \
             (var) = ((var)->field.tqe_next))
#endif

#ifndef TAILQ_EMPTY
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
#endif

#ifndef TAILQ_FIRST
#define TAILQ_FIRST(head) ((head)->tqh_first)
#endif

#ifndef TAILQ_NEXT
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#endif

#ifndef TAILQ_LAST
#define TAILQ_LAST(head, headname)                                             \
        (*(((struct headname *)((head)->tqh_last))->tqh_last))
#endif

#ifndef TAILQ_PREV
#define TAILQ_PREV(elm, headname, field)                                       \
        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#endif

#ifndef TAILQ_FOREACH_SAFE
/*
 * TAILQ_FOREACH_SAFE() provides a traversal where the current iterated element
 * may be freed or unlinked.
 * It does not allow freeing or modifying any other element in the list,
 * at least not the next element.
 */
#define TAILQ_FOREACH_SAFE(elm, head, field, tmpelm)                           \
        for ((elm) = TAILQ_FIRST(head);                                        \
             (elm) && ((tmpelm) = TAILQ_NEXT((elm), field), 1);                \
             (elm) = (tmpelm))
#endif

/*
 * In Mac OS 10.4 and earlier TAILQ_FOREACH_REVERSE was defined
 * differently, redefined it.
 */
#ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1050
#undef TAILQ_FOREACH_REVERSE
#endif
#endif

#ifndef TAILQ_FOREACH_REVERSE
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)                      \
        for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));   \
             (var);                                                            \
             (var) =                                                           \
                 (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
#endif


/**
 * Treat the TAILQ as a circular list and return the previous/next entry,
 * possibly wrapping to the end/beginning.
 */
#define TAILQ_CIRC_PREV(var, head, headname, field)                            \
        ((var) != TAILQ_FIRST(head) ? TAILQ_PREV(var, headname, field)         \
                                    : TAILQ_LAST(head, headname))

#define TAILQ_CIRC_NEXT(var, head, headname, field)                            \
        ((var) != TAILQ_LAST(head, headname) ? TAILQ_NEXT(var, field)          \
                                             : TAILQ_FIRST(head))

/*
 * Some extra functions for LIST manipulation
 */

#define LIST_INSERT_SORTED(head, elm, elmtype, field, cmpfunc)                 \
        do {                                                                   \
                if (LIST_EMPTY(head)) {                                        \
                        LIST_INSERT_HEAD(head, elm, field);                    \
                } else {                                                       \
                        elmtype _tmp;                                          \
                        LIST_FOREACH(_tmp, head, field) {                      \
                                if (cmpfunc(elm, _tmp) < 0) {                  \
                                        LIST_INSERT_BEFORE(_tmp, elm, field);  \
                                        break;                                 \
                                }                                              \
                                if (!LIST_NEXT(_tmp, field)) {                 \
                                        LIST_INSERT_AFTER(_tmp, elm, field);   \
                                        break;                                 \
                                }                                              \
                        }                                                      \
                }                                                              \
        } while (0)

#ifndef TAILQ_INSERT_SORTED
#define TAILQ_INSERT_SORTED(head, elm, elmtype, field, cmpfunc)                \
        do {                                                                   \
                if (TAILQ_FIRST(head) == NULL) {                               \
                        TAILQ_INSERT_HEAD(head, elm, field);                   \
                } else {                                                       \
                        elmtype _tmp;                                          \
                        TAILQ_FOREACH(_tmp, head, field) {                     \
                                if (cmpfunc(elm, _tmp) < 0) {                  \
                                        TAILQ_INSERT_BEFORE(_tmp, elm, field); \
                                        break;                                 \
                                }                                              \
                                if (!TAILQ_NEXT(_tmp, field)) {                \
                                        TAILQ_INSERT_AFTER(head, _tmp, elm,    \
                                                           field);             \
                                        break;                                 \
                                }                                              \
                        }                                                      \
                }                                                              \
        } while (0)
#endif

/**
 * @brief Add all elements from \p srchead to \p dsthead using sort
 *        comparator \p cmpfunc.
 *        \p src will be re-initialized on completion.
 */
#define TAILQ_CONCAT_SORTED(dsthead, srchead, elmtype, field, cmpfunc)         \
        do {                                                                   \
                elmtype _cstmp;                                                \
                elmtype _cstmp2;                                               \
                if (TAILQ_EMPTY(dsthead)) {                                    \
                        TAILQ_CONCAT(dsthead, srchead, field);                 \
                        break;                                                 \
                }                                                              \
                TAILQ_FOREACH_SAFE(_cstmp, srchead, field, _cstmp2) {          \
                        TAILQ_INSERT_SORTED(dsthead, _cstmp, elmtype, field,   \
                                            cmpfunc);                          \
                }                                                              \
                TAILQ_INIT(srchead);                                           \
        } while (0)

#define TAILQ_MOVE(newhead, oldhead, field)                                    \
        do {                                                                   \
                if (TAILQ_FIRST(oldhead)) {                                    \
                        TAILQ_FIRST(oldhead)->field.tqe_prev =                 \
                            &(newhead)->tqh_first;                             \
                        (newhead)->tqh_first = (oldhead)->tqh_first;           \
                        (newhead)->tqh_last  = (oldhead)->tqh_last;            \
                        TAILQ_INIT(oldhead);                                   \
                } else                                                         \
                        TAILQ_INIT(newhead);                                   \
        } while (/*CONSTCOND*/ 0)


/* @brief Prepend \p shead to \p dhead */
#define TAILQ_PREPEND(dhead, shead, headname, field)                           \
        do {                                                                   \
                if (unlikely(TAILQ_EMPTY(dhead))) {                            \
                        TAILQ_MOVE(dhead, shead, field);                       \
                } else if (likely(!TAILQ_EMPTY(shead))) {                      \
                        TAILQ_LAST(shead, headname)->field.tqe_next =          \
                            TAILQ_FIRST(dhead);                                \
                        TAILQ_FIRST(dhead)->field.tqe_prev =                   \
                            &TAILQ_LAST(shead, headname)->field.tqe_next;      \
                        TAILQ_FIRST(shead)->field.tqe_prev =                   \
                            &(dhead)->tqh_first;                               \
                        TAILQ_FIRST(dhead) = TAILQ_FIRST(shead);               \
                        TAILQ_INIT(shead);                                     \
                }                                                              \
        } while (0)

/* @brief Insert \p shead after element \p listelm in \p dhead */
#define TAILQ_INSERT_LIST(dhead, listelm, shead, headname, elmtype, field)       \
        do {                                                                     \
                if (TAILQ_LAST(dhead, headname) == listelm) {                    \
                        TAILQ_CONCAT(dhead, shead, field);                       \
                } else {                                                         \
                        elmtype _elm              = TAILQ_FIRST(shead);          \
                        elmtype _last             = TAILQ_LAST(shead, headname); \
                        elmtype _aft              = TAILQ_NEXT(listelm, field);  \
                        (listelm)->field.tqe_next = _elm;                        \
                        _elm->field.tqe_prev      = &(listelm)->field.tqe_next;  \
                        _last->field.tqe_next     = _aft;                        \
                        _aft->field.tqe_prev      = &_last->field.tqe_next;      \
                        TAILQ_INIT((shead));                                     \
                }                                                                \
        } while (0)

/* @brief Insert \p shead before element \p listelm in \p dhead */
#define TAILQ_INSERT_LIST_BEFORE(dhead, insert_before, shead, headname,        \
                                 elmtype, field)                               \
        do {                                                                   \
                if (TAILQ_FIRST(dhead) == insert_before) {                     \
                        TAILQ_PREPEND(dhead, shead, headname, field);          \
                } else {                                                       \
                        elmtype _first = TAILQ_FIRST(shead);                   \
                        elmtype _last  = TAILQ_LAST(shead, headname);          \
                        elmtype _dprev =                                       \
                            TAILQ_PREV(insert_before, headname, field);        \
                        _last->field.tqe_next  = insert_before;                \
                        _dprev->field.tqe_next = _first;                       \
                        (insert_before)->field.tqe_prev =                      \
                            &_last->field.tqe_next;                            \
                        _first->field.tqe_prev = &(_dprev)->field.tqe_next;    \
                        TAILQ_INIT((shead));                                   \
                }                                                              \
        } while (0)

#ifndef SIMPLEQ_HEAD
#define SIMPLEQ_HEAD(name, type)                                               \
        struct name {                                                          \
                struct type *sqh_first;                                        \
                struct type **sqh_last;                                        \
        }
#endif

#ifndef SIMPLEQ_ENTRY
#define SIMPLEQ_ENTRY(type)                                                    \
        struct {                                                               \
                struct type *sqe_next;                                         \
        }
#endif

#ifndef SIMPLEQ_FIRST
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
#endif

#ifndef SIMPLEQ_REMOVE_HEAD
#define SIMPLEQ_REMOVE_HEAD(head, field)                                       \
        do {                                                                   \
                if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == \
                    NULL)                                                      \
                        (head)->sqh_last = &(head)->sqh_first;                 \
        } while (0)
#endif

#ifndef SIMPLEQ_INSERT_TAIL
#define SIMPLEQ_INSERT_TAIL(head, elm, field)                                  \
        do {                                                                   \
                (elm)->field.sqe_next = NULL;                                  \
                *(head)->sqh_last     = (elm);                                 \
                (head)->sqh_last      = &(elm)->field.sqe_next;                \
        } while (0)
#endif

#ifndef SIMPLEQ_INIT
#define SIMPLEQ_INIT(head)                                                     \
        do {                                                                   \
                (head)->sqh_first = NULL;                                      \
                (head)->sqh_last  = &(head)->sqh_first;                        \
        } while (0)
#endif

#ifndef SIMPLEQ_INSERT_HEAD
#define SIMPLEQ_INSERT_HEAD(head, elm, field)                                  \
        do {                                                                   \
                if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)       \
                        (head)->sqh_last = &(elm)->field.sqe_next;             \
                (head)->sqh_first = (elm);                                     \
        } while (0)
#endif

#ifndef SIMPLEQ_FOREACH
#define SIMPLEQ_FOREACH(var, head, field)                                      \
        for ((var) = SIMPLEQ_FIRST(head); (var) != SIMPLEQ_END(head);          \
             (var) = SIMPLEQ_NEXT(var, field))
#endif

#ifndef SIMPLEQ_INSERT_AFTER
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field)                        \
        do {                                                                   \
                if (((elm)->field.sqe_next = (listelm)->field.sqe_next) ==     \
                    NULL)                                                      \
                        (head)->sqh_last = &(elm)->field.sqe_next;             \
                (listelm)->field.sqe_next = (elm);                             \
        } while (0)
#endif

#ifndef SIMPLEQ_END
#define SIMPLEQ_END(head) NULL
#endif

#ifndef SIMPLEQ_NEXT
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
#endif

#ifndef SIMPLEQ_HEAD_INITIALIZER
#define SIMPLEQ_HEAD_INITIALIZER(head)                                         \
        { NULL, &(head).sqh_first }
#endif

#ifndef SIMPLEQ_EMPTY
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
#endif



#endif /* _RDSYSQUEUE_H_ */