FIX: list buffer size is not correctly returned on ext4_listxattr calls.
[lwext4.git] / lwext4 / tree.h
1 /*      $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $  */
2 /*      $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $    */
3 /* $FreeBSD$ */
4
5 /*-
6  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef _SYS_TREE_H_
31 #define _SYS_TREE_H_
32
33 #ifdef __GNUC__
34 #ifndef __unused
35 #define __unused __attribute__ ((__unused__))
36 #endif
37 #endif
38 /*
39  * This file defines data structures for different types of trees:
40  * splay trees and red-black trees.
41  *
42  * A splay tree is a self-organizing data structure.  Every operation
43  * on the tree causes a splay to happen.  The splay moves the requested
44  * node to the root of the tree and partly rebalances it.
45  *
46  * This has the benefit that request locality causes faster lookups as
47  * the requested nodes move to the top of the tree.  On the other hand,
48  * every lookup causes memory writes.
49  *
50  * The Balance Theorem bounds the total access time for m operations
51  * and n inserts on an initially empty tree as O((m + n)lg n).  The
52  * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
53  *
54  * A red-black tree is a binary search tree with the node color as an
55  * extra attribute.  It fulfills a set of conditions:
56  *      - every search path from the root to a leaf consists of the
57  *        same number of black nodes,
58  *      - each red node (except for the root) has a black parent,
59  *      - each leaf node is black.
60  *
61  * Every operation on a red-black tree is bounded as O(lg n).
62  * The maximum height of a red-black tree is 2lg (n+1).
63  */
64
65 #define SPLAY_HEAD(name, type)                                          \
66 struct name {                                                           \
67         struct type *sph_root; /* root of the tree */                   \
68 }
69
70 #define SPLAY_INITIALIZER(root)                                         \
71         { NULL }
72
73 #define SPLAY_INIT(root) do {                                           \
74         (root)->sph_root = NULL;                                        \
75 } while (/*CONSTCOND*/ 0)
76
77 #define SPLAY_ENTRY(type)                                               \
78 struct {                                                                \
79         struct type *spe_left; /* left element */                       \
80         struct type *spe_right; /* right element */                     \
81 }
82
83 #define SPLAY_LEFT(elm, field)          (elm)->field.spe_left
84 #define SPLAY_RIGHT(elm, field)         (elm)->field.spe_right
85 #define SPLAY_ROOT(head)                (head)->sph_root
86 #define SPLAY_EMPTY(head)               (SPLAY_ROOT(head) == NULL)
87
88 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
89 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do {                       \
90         SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field);  \
91         SPLAY_RIGHT(tmp, field) = (head)->sph_root;                     \
92         (head)->sph_root = tmp;                                         \
93 } while (/*CONSTCOND*/ 0)
94         
95 #define SPLAY_ROTATE_LEFT(head, tmp, field) do {                        \
96         SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field);  \
97         SPLAY_LEFT(tmp, field) = (head)->sph_root;                      \
98         (head)->sph_root = tmp;                                         \
99 } while (/*CONSTCOND*/ 0)
100
101 #define SPLAY_LINKLEFT(head, tmp, field) do {                           \
102         SPLAY_LEFT(tmp, field) = (head)->sph_root;                      \
103         tmp = (head)->sph_root;                                         \
104         (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);         \
105 } while (/*CONSTCOND*/ 0)
106
107 #define SPLAY_LINKRIGHT(head, tmp, field) do {                          \
108         SPLAY_RIGHT(tmp, field) = (head)->sph_root;                     \
109         tmp = (head)->sph_root;                                         \
110         (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);        \
111 } while (/*CONSTCOND*/ 0)
112
113 #define SPLAY_ASSEMBLE(head, node, left, right, field) do {             \
114         SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
115         SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
116         SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
117         SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
118 } while (/*CONSTCOND*/ 0)
119
120 /* Generates prototypes and inline functions */
121
122 #define SPLAY_PROTOTYPE(name, type, field, cmp)                         \
123 void name##_SPLAY(struct name *, struct type *);                        \
124 void name##_SPLAY_MINMAX(struct name *, int);                           \
125 struct type *name##_SPLAY_INSERT(struct name *, struct type *);         \
126 struct type *name##_SPLAY_REMOVE(struct name *, struct type *);         \
127                                                                         \
128 /* Finds the node with the same key as elm */                           \
129 static __inline struct type *                                           \
130 name##_SPLAY_FIND(struct name *head, struct type *elm)                  \
131 {                                                                       \
132         if (SPLAY_EMPTY(head))                                          \
133                 return(NULL);                                           \
134         name##_SPLAY(head, elm);                                        \
135         if ((cmp)(elm, (head)->sph_root) == 0)                          \
136                 return (head->sph_root);                                \
137         return (NULL);                                                  \
138 }                                                                       \
139                                                                         \
140 static __inline struct type *                                           \
141 name##_SPLAY_NEXT(struct name *head, struct type *elm)                  \
142 {                                                                       \
143         name##_SPLAY(head, elm);                                        \
144         if (SPLAY_RIGHT(elm, field) != NULL) {                          \
145                 elm = SPLAY_RIGHT(elm, field);                          \
146                 while (SPLAY_LEFT(elm, field) != NULL) {                \
147                         elm = SPLAY_LEFT(elm, field);                   \
148                 }                                                       \
149         } else                                                          \
150                 elm = NULL;                                             \
151         return (elm);                                                   \
152 }                                                                       \
153                                                                         \
154 static __inline struct type *                                           \
155 name##_SPLAY_MIN_MAX(struct name *head, int val)                        \
156 {                                                                       \
157         name##_SPLAY_MINMAX(head, val);                                 \
158         return (SPLAY_ROOT(head));                                      \
159 }
160
161 /* Main splay operation.
162  * Moves node close to the key of elm to top
163  */
164 #define SPLAY_GENERATE(name, type, field, cmp)                          \
165 struct type *                                                           \
166 name##_SPLAY_INSERT(struct name *head, struct type *elm)                \
167 {                                                                       \
168     if (SPLAY_EMPTY(head)) {                                            \
169             SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL;    \
170     } else {                                                            \
171             int __comp;                                                 \
172             name##_SPLAY(head, elm);                                    \
173             __comp = (cmp)(elm, (head)->sph_root);                      \
174             if(__comp < 0) {                                            \
175                     SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
176                     SPLAY_RIGHT(elm, field) = (head)->sph_root;         \
177                     SPLAY_LEFT((head)->sph_root, field) = NULL;         \
178             } else if (__comp > 0) {                                    \
179                     SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
180                     SPLAY_LEFT(elm, field) = (head)->sph_root;          \
181                     SPLAY_RIGHT((head)->sph_root, field) = NULL;        \
182             } else                                                      \
183                     return ((head)->sph_root);                          \
184     }                                                                   \
185     (head)->sph_root = (elm);                                           \
186     return (NULL);                                                      \
187 }                                                                       \
188                                                                         \
189 struct type *                                                           \
190 name##_SPLAY_REMOVE(struct name *head, struct type *elm)                \
191 {                                                                       \
192         struct type *__tmp;                                             \
193         if (SPLAY_EMPTY(head))                                          \
194                 return (NULL);                                          \
195         name##_SPLAY(head, elm);                                        \
196         if ((cmp)(elm, (head)->sph_root) == 0) {                        \
197                 if (SPLAY_LEFT((head)->sph_root, field) == NULL) {      \
198                         (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
199                 } else {                                                \
200                         __tmp = SPLAY_RIGHT((head)->sph_root, field);   \
201                         (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
202                         name##_SPLAY(head, elm);                        \
203                         SPLAY_RIGHT((head)->sph_root, field) = __tmp;   \
204                 }                                                       \
205                 return (elm);                                           \
206         }                                                               \
207         return (NULL);                                                  \
208 }                                                                       \
209                                                                         \
210 void                                                                    \
211 name##_SPLAY(struct name *head, struct type *elm)                       \
212 {                                                                       \
213         struct type __node, *__left, *__right, *__tmp;                  \
214         int __comp;                                                     \
215 \
216         SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
217         __left = __right = &__node;                                     \
218 \
219         while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) {          \
220                 if (__comp < 0) {                                       \
221                         __tmp = SPLAY_LEFT((head)->sph_root, field);    \
222                         if (__tmp == NULL)                              \
223                                 break;                                  \
224                         if ((cmp)(elm, __tmp) < 0){                     \
225                                 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
226                                 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
227                                         break;                          \
228                         }                                               \
229                         SPLAY_LINKLEFT(head, __right, field);           \
230                 } else if (__comp > 0) {                                \
231                         __tmp = SPLAY_RIGHT((head)->sph_root, field);   \
232                         if (__tmp == NULL)                              \
233                                 break;                                  \
234                         if ((cmp)(elm, __tmp) > 0){                     \
235                                 SPLAY_ROTATE_LEFT(head, __tmp, field);  \
236                                 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
237                                         break;                          \
238                         }                                               \
239                         SPLAY_LINKRIGHT(head, __left, field);           \
240                 }                                                       \
241         }                                                               \
242         SPLAY_ASSEMBLE(head, &__node, __left, __right, field);          \
243 }                                                                       \
244                                                                         \
245 /* Splay with either the minimum or the maximum element                 \
246  * Used to find minimum or maximum element in tree.                     \
247  */                                                                     \
248 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
249 {                                                                       \
250         struct type __node, *__left, *__right, *__tmp;                  \
251 \
252         SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
253         __left = __right = &__node;                                     \
254 \
255         while (1) {                                                     \
256                 if (__comp < 0) {                                       \
257                         __tmp = SPLAY_LEFT((head)->sph_root, field);    \
258                         if (__tmp == NULL)                              \
259                                 break;                                  \
260                         if (__comp < 0){                                \
261                                 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
262                                 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
263                                         break;                          \
264                         }                                               \
265                         SPLAY_LINKLEFT(head, __right, field);           \
266                 } else if (__comp > 0) {                                \
267                         __tmp = SPLAY_RIGHT((head)->sph_root, field);   \
268                         if (__tmp == NULL)                              \
269                                 break;                                  \
270                         if (__comp > 0) {                               \
271                                 SPLAY_ROTATE_LEFT(head, __tmp, field);  \
272                                 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
273                                         break;                          \
274                         }                                               \
275                         SPLAY_LINKRIGHT(head, __left, field);           \
276                 }                                                       \
277         }                                                               \
278         SPLAY_ASSEMBLE(head, &__node, __left, __right, field);          \
279 }
280
281 #define SPLAY_NEGINF    -1
282 #define SPLAY_INF       1
283
284 #define SPLAY_INSERT(name, x, y)        name##_SPLAY_INSERT(x, y)
285 #define SPLAY_REMOVE(name, x, y)        name##_SPLAY_REMOVE(x, y)
286 #define SPLAY_FIND(name, x, y)          name##_SPLAY_FIND(x, y)
287 #define SPLAY_NEXT(name, x, y)          name##_SPLAY_NEXT(x, y)
288 #define SPLAY_MIN(name, x)              (SPLAY_EMPTY(x) ? NULL  \
289                                         : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
290 #define SPLAY_MAX(name, x)              (SPLAY_EMPTY(x) ? NULL  \
291                                         : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
292
293 #define SPLAY_FOREACH(x, name, head)                                    \
294         for ((x) = SPLAY_MIN(name, head);                               \
295              (x) != NULL;                                               \
296              (x) = SPLAY_NEXT(name, head, x))
297
298 /* Macros that define a red-black tree */
299 #define RB_HEAD(name, type)                                             \
300 struct name {                                                           \
301         struct type *rbh_root; /* root of the tree */                   \
302 }
303
304 #define RB_INITIALIZER(root)                                            \
305         { NULL }
306
307 #define RB_INIT(root) do {                                              \
308         (root)->rbh_root = NULL;                                        \
309 } while (/*CONSTCOND*/ 0)
310
311 #define RB_BLACK        0
312 #define RB_RED          1
313 #define RB_ENTRY(type)                                                  \
314 struct {                                                                \
315         struct type *rbe_left;          /* left element */              \
316         struct type *rbe_right;         /* right element */             \
317         struct type *rbe_parent;        /* parent element */            \
318         int rbe_color;                  /* node color */                \
319 }
320
321 #define RB_LEFT(elm, field)             (elm)->field.rbe_left
322 #define RB_RIGHT(elm, field)            (elm)->field.rbe_right
323 #define RB_PARENT(elm, field)           (elm)->field.rbe_parent
324 #define RB_COLOR(elm, field)            (elm)->field.rbe_color
325 #define RB_ROOT(head)                   (head)->rbh_root
326 #define RB_EMPTY(head)                  (RB_ROOT(head) == NULL)
327
328 #define RB_SET(elm, parent, field) do {                                 \
329         RB_PARENT(elm, field) = parent;                                 \
330         RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL;              \
331         RB_COLOR(elm, field) = RB_RED;                                  \
332 } while (/*CONSTCOND*/ 0)
333
334 #define RB_SET_BLACKRED(black, red, field) do {                         \
335         RB_COLOR(black, field) = RB_BLACK;                              \
336         RB_COLOR(red, field) = RB_RED;                                  \
337 } while (/*CONSTCOND*/ 0)
338
339 #ifndef RB_AUGMENT
340 #define RB_AUGMENT(x)   do {} while (0)
341 #endif
342
343 #define RB_ROTATE_LEFT(head, elm, tmp, field) do {                      \
344         (tmp) = RB_RIGHT(elm, field);                                   \
345         if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) {     \
346                 RB_PARENT(RB_LEFT(tmp, field), field) = (elm);          \
347         }                                                               \
348         RB_AUGMENT(elm);                                                \
349         if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {  \
350                 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))     \
351                         RB_LEFT(RB_PARENT(elm, field), field) = (tmp);  \
352                 else                                                    \
353                         RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
354         } else                                                          \
355                 (head)->rbh_root = (tmp);                               \
356         RB_LEFT(tmp, field) = (elm);                                    \
357         RB_PARENT(elm, field) = (tmp);                                  \
358         RB_AUGMENT(tmp);                                                \
359         if ((RB_PARENT(tmp, field)))                                    \
360                 RB_AUGMENT(RB_PARENT(tmp, field));                      \
361 } while (/*CONSTCOND*/ 0)
362
363 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do {                     \
364         (tmp) = RB_LEFT(elm, field);                                    \
365         if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) {     \
366                 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm);         \
367         }                                                               \
368         RB_AUGMENT(elm);                                                \
369         if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {  \
370                 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))     \
371                         RB_LEFT(RB_PARENT(elm, field), field) = (tmp);  \
372                 else                                                    \
373                         RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
374         } else                                                          \
375                 (head)->rbh_root = (tmp);                               \
376         RB_RIGHT(tmp, field) = (elm);                                   \
377         RB_PARENT(elm, field) = (tmp);                                  \
378         RB_AUGMENT(tmp);                                                \
379         if ((RB_PARENT(tmp, field)))                                    \
380                 RB_AUGMENT(RB_PARENT(tmp, field));                      \
381 } while (/*CONSTCOND*/ 0)
382
383 /* Generates prototypes and inline functions */
384 #define RB_PROTOTYPE(name, type, field, cmp)                            \
385         RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
386 #define RB_PROTOTYPE_STATIC(name, type, field, cmp)                     \
387         RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
388 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr)             \
389         RB_PROTOTYPE_INSERT_COLOR(name, type, attr);                    \
390         RB_PROTOTYPE_REMOVE_COLOR(name, type, attr);                    \
391         RB_PROTOTYPE_INSERT(name, type, attr);                          \
392         RB_PROTOTYPE_REMOVE(name, type, attr);                          \
393         RB_PROTOTYPE_FIND(name, type, attr);                            \
394         RB_PROTOTYPE_NFIND(name, type, attr);                           \
395         RB_PROTOTYPE_NEXT(name, type, attr);                            \
396         RB_PROTOTYPE_PREV(name, type, attr);                            \
397         RB_PROTOTYPE_MINMAX(name, type, attr);
398 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr)                     \
399         attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
400 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr)                     \
401         attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *)
402 #define RB_PROTOTYPE_REMOVE(name, type, attr)                           \
403         attr struct type *name##_RB_REMOVE(struct name *, struct type *)
404 #define RB_PROTOTYPE_INSERT(name, type, attr)                           \
405         attr struct type *name##_RB_INSERT(struct name *, struct type *)
406 #define RB_PROTOTYPE_FIND(name, type, attr)                             \
407         attr struct type *name##_RB_FIND(struct name *, struct type *)
408 #define RB_PROTOTYPE_NFIND(name, type, attr)                            \
409         attr struct type *name##_RB_NFIND(struct name *, struct type *)
410 #define RB_PROTOTYPE_NEXT(name, type, attr)                             \
411         attr struct type *name##_RB_NEXT(struct type *)
412 #define RB_PROTOTYPE_PREV(name, type, attr)                             \
413         attr struct type *name##_RB_PREV(struct type *)
414 #define RB_PROTOTYPE_MINMAX(name, type, attr)                           \
415         attr struct type *name##_RB_MINMAX(struct name *, int)
416
417 /* Main rb operation.
418  * Moves node close to the key of elm to top
419  */
420 #define RB_GENERATE(name, type, field, cmp)                             \
421         RB_GENERATE_INTERNAL(name, type, field, cmp,)
422 #define RB_GENERATE_STATIC(name, type, field, cmp)                      \
423         RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
424 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr)              \
425         RB_GENERATE_INSERT_COLOR(name, type, field, attr)               \
426         RB_GENERATE_REMOVE_COLOR(name, type, field, attr)               \
427         RB_GENERATE_INSERT(name, type, field, cmp, attr)                \
428         RB_GENERATE_REMOVE(name, type, field, attr)                     \
429         RB_GENERATE_FIND(name, type, field, cmp, attr)                  \
430         RB_GENERATE_NFIND(name, type, field, cmp, attr)                 \
431         RB_GENERATE_NEXT(name, type, field, attr)                       \
432         RB_GENERATE_PREV(name, type, field, attr)                       \
433         RB_GENERATE_MINMAX(name, type, field, attr)
434
435 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr)               \
436 attr void                                                               \
437 name##_RB_INSERT_COLOR(struct name *head, struct type *elm)             \
438 {                                                                       \
439         struct type *parent, *gparent, *tmp;                            \
440         while ((parent = RB_PARENT(elm, field)) != NULL &&              \
441             RB_COLOR(parent, field) == RB_RED) {                        \
442                 gparent = RB_PARENT(parent, field);                     \
443                 if (parent == RB_LEFT(gparent, field)) {                \
444                         tmp = RB_RIGHT(gparent, field);                 \
445                         if (tmp && RB_COLOR(tmp, field) == RB_RED) {    \
446                                 RB_COLOR(tmp, field) = RB_BLACK;        \
447                                 RB_SET_BLACKRED(parent, gparent, field);\
448                                 elm = gparent;                          \
449                                 continue;                               \
450                         }                                               \
451                         if (RB_RIGHT(parent, field) == elm) {           \
452                                 RB_ROTATE_LEFT(head, parent, tmp, field);\
453                                 tmp = parent;                           \
454                                 parent = elm;                           \
455                                 elm = tmp;                              \
456                         }                                               \
457                         RB_SET_BLACKRED(parent, gparent, field);        \
458                         RB_ROTATE_RIGHT(head, gparent, tmp, field);     \
459                 } else {                                                \
460                         tmp = RB_LEFT(gparent, field);                  \
461                         if (tmp && RB_COLOR(tmp, field) == RB_RED) {    \
462                                 RB_COLOR(tmp, field) = RB_BLACK;        \
463                                 RB_SET_BLACKRED(parent, gparent, field);\
464                                 elm = gparent;                          \
465                                 continue;                               \
466                         }                                               \
467                         if (RB_LEFT(parent, field) == elm) {            \
468                                 RB_ROTATE_RIGHT(head, parent, tmp, field);\
469                                 tmp = parent;                           \
470                                 parent = elm;                           \
471                                 elm = tmp;                              \
472                         }                                               \
473                         RB_SET_BLACKRED(parent, gparent, field);        \
474                         RB_ROTATE_LEFT(head, gparent, tmp, field);      \
475                 }                                                       \
476         }                                                               \
477         RB_COLOR(head->rbh_root, field) = RB_BLACK;                     \
478 }
479
480 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr)               \
481 attr void                                                               \
482 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
483 {                                                                       \
484         struct type *tmp;                                               \
485         while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) &&     \
486             elm != RB_ROOT(head)) {                                     \
487                 if (RB_LEFT(parent, field) == elm) {                    \
488                         tmp = RB_RIGHT(parent, field);                  \
489                         if (RB_COLOR(tmp, field) == RB_RED) {           \
490                                 RB_SET_BLACKRED(tmp, parent, field);    \
491                                 RB_ROTATE_LEFT(head, parent, tmp, field);\
492                                 tmp = RB_RIGHT(parent, field);          \
493                         }                                               \
494                         if ((RB_LEFT(tmp, field) == NULL ||             \
495                             RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
496                             (RB_RIGHT(tmp, field) == NULL ||            \
497                             RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
498                                 RB_COLOR(tmp, field) = RB_RED;          \
499                                 elm = parent;                           \
500                                 parent = RB_PARENT(elm, field);         \
501                         } else {                                        \
502                                 if (RB_RIGHT(tmp, field) == NULL ||     \
503                                     RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
504                                         struct type *oleft;             \
505                                         if ((oleft = RB_LEFT(tmp, field)) \
506                                             != NULL)                    \
507                                                 RB_COLOR(oleft, field) = RB_BLACK;\
508                                         RB_COLOR(tmp, field) = RB_RED;  \
509                                         RB_ROTATE_RIGHT(head, tmp, oleft, field);\
510                                         tmp = RB_RIGHT(parent, field);  \
511                                 }                                       \
512                                 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
513                                 RB_COLOR(parent, field) = RB_BLACK;     \
514                                 if (RB_RIGHT(tmp, field))               \
515                                         RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
516                                 RB_ROTATE_LEFT(head, parent, tmp, field);\
517                                 elm = RB_ROOT(head);                    \
518                                 break;                                  \
519                         }                                               \
520                 } else {                                                \
521                         tmp = RB_LEFT(parent, field);                   \
522                         if (RB_COLOR(tmp, field) == RB_RED) {           \
523                                 RB_SET_BLACKRED(tmp, parent, field);    \
524                                 RB_ROTATE_RIGHT(head, parent, tmp, field);\
525                                 tmp = RB_LEFT(parent, field);           \
526                         }                                               \
527                         if ((RB_LEFT(tmp, field) == NULL ||             \
528                             RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
529                             (RB_RIGHT(tmp, field) == NULL ||            \
530                             RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
531                                 RB_COLOR(tmp, field) = RB_RED;          \
532                                 elm = parent;                           \
533                                 parent = RB_PARENT(elm, field);         \
534                         } else {                                        \
535                                 if (RB_LEFT(tmp, field) == NULL ||      \
536                                     RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
537                                         struct type *oright;            \
538                                         if ((oright = RB_RIGHT(tmp, field)) \
539                                             != NULL)                    \
540                                                 RB_COLOR(oright, field) = RB_BLACK;\
541                                         RB_COLOR(tmp, field) = RB_RED;  \
542                                         RB_ROTATE_LEFT(head, tmp, oright, field);\
543                                         tmp = RB_LEFT(parent, field);   \
544                                 }                                       \
545                                 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
546                                 RB_COLOR(parent, field) = RB_BLACK;     \
547                                 if (RB_LEFT(tmp, field))                \
548                                         RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
549                                 RB_ROTATE_RIGHT(head, parent, tmp, field);\
550                                 elm = RB_ROOT(head);                    \
551                                 break;                                  \
552                         }                                               \
553                 }                                                       \
554         }                                                               \
555         if (elm)                                                        \
556                 RB_COLOR(elm, field) = RB_BLACK;                        \
557 }
558
559 #define RB_GENERATE_REMOVE(name, type, field, attr)                     \
560 attr struct type *                                                      \
561 name##_RB_REMOVE(struct name *head, struct type *elm)                   \
562 {                                                                       \
563         struct type *child, *parent, *old = elm;                        \
564         int color;                                                      \
565         if (RB_LEFT(elm, field) == NULL)                                \
566                 child = RB_RIGHT(elm, field);                           \
567         else if (RB_RIGHT(elm, field) == NULL)                          \
568                 child = RB_LEFT(elm, field);                            \
569         else {                                                          \
570                 struct type *left;                                      \
571                 elm = RB_RIGHT(elm, field);                             \
572                 while ((left = RB_LEFT(elm, field)) != NULL)            \
573                         elm = left;                                     \
574                 child = RB_RIGHT(elm, field);                           \
575                 parent = RB_PARENT(elm, field);                         \
576                 color = RB_COLOR(elm, field);                           \
577                 if (child)                                              \
578                         RB_PARENT(child, field) = parent;               \
579                 if (parent) {                                           \
580                         if (RB_LEFT(parent, field) == elm)              \
581                                 RB_LEFT(parent, field) = child;         \
582                         else                                            \
583                                 RB_RIGHT(parent, field) = child;        \
584                         RB_AUGMENT(parent);                             \
585                 } else                                                  \
586                         RB_ROOT(head) = child;                          \
587                 if (RB_PARENT(elm, field) == old)                       \
588                         parent = elm;                                   \
589                 (elm)->field = (old)->field;                            \
590                 if (RB_PARENT(old, field)) {                            \
591                         if (RB_LEFT(RB_PARENT(old, field), field) == old)\
592                                 RB_LEFT(RB_PARENT(old, field), field) = elm;\
593                         else                                            \
594                                 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
595                         RB_AUGMENT(RB_PARENT(old, field));              \
596                 } else                                                  \
597                         RB_ROOT(head) = elm;                            \
598                 RB_PARENT(RB_LEFT(old, field), field) = elm;            \
599                 if (RB_RIGHT(old, field))                               \
600                         RB_PARENT(RB_RIGHT(old, field), field) = elm;   \
601                 if (parent) {                                           \
602                         left = parent;                                  \
603                         do {                                            \
604                                 RB_AUGMENT(left);                       \
605                         } while ((left = RB_PARENT(left, field)) != NULL); \
606                 }                                                       \
607                 goto color;                                             \
608         }                                                               \
609         parent = RB_PARENT(elm, field);                                 \
610         color = RB_COLOR(elm, field);                                   \
611         if (child)                                                      \
612                 RB_PARENT(child, field) = parent;                       \
613         if (parent) {                                                   \
614                 if (RB_LEFT(parent, field) == elm)                      \
615                         RB_LEFT(parent, field) = child;                 \
616                 else                                                    \
617                         RB_RIGHT(parent, field) = child;                \
618                 RB_AUGMENT(parent);                                     \
619         } else                                                          \
620                 RB_ROOT(head) = child;                                  \
621 color:                                                                  \
622         if (color == RB_BLACK)                                          \
623                 name##_RB_REMOVE_COLOR(head, parent, child);            \
624         return (old);                                                   \
625 }                                                                       \
626
627 #define RB_GENERATE_INSERT(name, type, field, cmp, attr)                \
628 /* Inserts a node into the RB tree */                                   \
629 attr struct type *                                                      \
630 name##_RB_INSERT(struct name *head, struct type *elm)                   \
631 {                                                                       \
632         struct type *tmp;                                               \
633         struct type *parent = NULL;                                     \
634         int comp = 0;                                                   \
635         tmp = RB_ROOT(head);                                            \
636         while (tmp) {                                                   \
637                 parent = tmp;                                           \
638                 comp = (cmp)(elm, parent);                              \
639                 if (comp < 0)                                           \
640                         tmp = RB_LEFT(tmp, field);                      \
641                 else if (comp > 0)                                      \
642                         tmp = RB_RIGHT(tmp, field);                     \
643                 else                                                    \
644                         return (tmp);                                   \
645         }                                                               \
646         RB_SET(elm, parent, field);                                     \
647         if (parent != NULL) {                                           \
648                 if (comp < 0)                                           \
649                         RB_LEFT(parent, field) = elm;                   \
650                 else                                                    \
651                         RB_RIGHT(parent, field) = elm;                  \
652                 RB_AUGMENT(parent);                                     \
653         } else                                                          \
654                 RB_ROOT(head) = elm;                                    \
655         name##_RB_INSERT_COLOR(head, elm);                              \
656         return (NULL);                                                  \
657 }
658
659 #define RB_GENERATE_FIND(name, type, field, cmp, attr)                  \
660 /* Finds the node with the same key as elm */                           \
661 attr struct type *                                                      \
662 name##_RB_FIND(struct name *head, struct type *elm)                     \
663 {                                                                       \
664         struct type *tmp = RB_ROOT(head);                               \
665         int comp;                                                       \
666         while (tmp) {                                                   \
667                 comp = cmp(elm, tmp);                                   \
668                 if (comp < 0)                                           \
669                         tmp = RB_LEFT(tmp, field);                      \
670                 else if (comp > 0)                                      \
671                         tmp = RB_RIGHT(tmp, field);                     \
672                 else                                                    \
673                         return (tmp);                                   \
674         }                                                               \
675         return (NULL);                                                  \
676 }
677
678 #define RB_GENERATE_NFIND(name, type, field, cmp, attr)                 \
679 /* Finds the first node greater than or equal to the search key */      \
680 attr struct type *                                                      \
681 name##_RB_NFIND(struct name *head, struct type *elm)                    \
682 {                                                                       \
683         struct type *tmp = RB_ROOT(head);                               \
684         struct type *res = NULL;                                        \
685         int comp;                                                       \
686         while (tmp) {                                                   \
687                 comp = cmp(elm, tmp);                                   \
688                 if (comp < 0) {                                         \
689                         res = tmp;                                      \
690                         tmp = RB_LEFT(tmp, field);                      \
691                 }                                                       \
692                 else if (comp > 0)                                      \
693                         tmp = RB_RIGHT(tmp, field);                     \
694                 else                                                    \
695                         return (tmp);                                   \
696         }                                                               \
697         return (res);                                                   \
698 }
699
700 #define RB_GENERATE_NEXT(name, type, field, attr)                       \
701 /* ARGSUSED */                                                          \
702 attr struct type *                                                      \
703 name##_RB_NEXT(struct type *elm)                                        \
704 {                                                                       \
705         if (RB_RIGHT(elm, field)) {                                     \
706                 elm = RB_RIGHT(elm, field);                             \
707                 while (RB_LEFT(elm, field))                             \
708                         elm = RB_LEFT(elm, field);                      \
709         } else {                                                        \
710                 if (RB_PARENT(elm, field) &&                            \
711                     (elm == RB_LEFT(RB_PARENT(elm, field), field)))     \
712                         elm = RB_PARENT(elm, field);                    \
713                 else {                                                  \
714                         while (RB_PARENT(elm, field) &&                 \
715                             (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
716                                 elm = RB_PARENT(elm, field);            \
717                         elm = RB_PARENT(elm, field);                    \
718                 }                                                       \
719         }                                                               \
720         return (elm);                                                   \
721 }
722
723 #define RB_GENERATE_PREV(name, type, field, attr)                       \
724 /* ARGSUSED */                                                          \
725 attr struct type *                                                      \
726 name##_RB_PREV(struct type *elm)                                        \
727 {                                                                       \
728         if (RB_LEFT(elm, field)) {                                      \
729                 elm = RB_LEFT(elm, field);                              \
730                 while (RB_RIGHT(elm, field))                            \
731                         elm = RB_RIGHT(elm, field);                     \
732         } else {                                                        \
733                 if (RB_PARENT(elm, field) &&                            \
734                     (elm == RB_RIGHT(RB_PARENT(elm, field), field)))    \
735                         elm = RB_PARENT(elm, field);                    \
736                 else {                                                  \
737                         while (RB_PARENT(elm, field) &&                 \
738                             (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
739                                 elm = RB_PARENT(elm, field);            \
740                         elm = RB_PARENT(elm, field);                    \
741                 }                                                       \
742         }                                                               \
743         return (elm);                                                   \
744 }
745
746 #define RB_GENERATE_MINMAX(name, type, field, attr)                     \
747 attr struct type *                                                      \
748 name##_RB_MINMAX(struct name *head, int val)                            \
749 {                                                                       \
750         struct type *tmp = RB_ROOT(head);                               \
751         struct type *parent = NULL;                                     \
752         while (tmp) {                                                   \
753                 parent = tmp;                                           \
754                 if (val < 0)                                            \
755                         tmp = RB_LEFT(tmp, field);                      \
756                 else                                                    \
757                         tmp = RB_RIGHT(tmp, field);                     \
758         }                                                               \
759         return (parent);                                                \
760 }
761
762 #define RB_NEGINF       -1
763 #define RB_INF  1
764
765 #define RB_INSERT(name, x, y)   name##_RB_INSERT(x, y)
766 #define RB_REMOVE(name, x, y)   name##_RB_REMOVE(x, y)
767 #define RB_FIND(name, x, y)     name##_RB_FIND(x, y)
768 #define RB_NFIND(name, x, y)    name##_RB_NFIND(x, y)
769 #define RB_NEXT(name, x, y)     name##_RB_NEXT(y)
770 #define RB_PREV(name, x, y)     name##_RB_PREV(y)
771 #define RB_MIN(name, x)         name##_RB_MINMAX(x, RB_NEGINF)
772 #define RB_MAX(name, x)         name##_RB_MINMAX(x, RB_INF)
773
774 #define RB_FOREACH(x, name, head)                                       \
775         for ((x) = RB_MIN(name, head);                                  \
776              (x) != NULL;                                               \
777              (x) = name##_RB_NEXT(x))
778
779 #define RB_FOREACH_FROM(x, name, y)                                     \
780         for ((x) = (y);                                                 \
781             ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);    \
782              (x) = (y))
783
784 #define RB_FOREACH_SAFE(x, name, head, y)                               \
785         for ((x) = RB_MIN(name, head);                                  \
786             ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);    \
787              (x) = (y))
788
789 #define RB_FOREACH_REVERSE(x, name, head)                               \
790         for ((x) = RB_MAX(name, head);                                  \
791              (x) != NULL;                                               \
792              (x) = name##_RB_PREV(x))
793
794 #define RB_FOREACH_REVERSE_FROM(x, name, y)                             \
795         for ((x) = (y);                                                 \
796             ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);    \
797              (x) = (y))
798
799 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y)                       \
800         for ((x) = RB_MAX(name, head);                                  \
801             ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);    \
802              (x) = (y))
803
804 #endif  /* _SYS_TREE_H_ */