Index: stable/12/share/man/man3/sigevent.3 =================================================================== --- stable/12/share/man/man3/sigevent.3 (revision 358816) +++ stable/12/share/man/man3/sigevent.3 (revision 358817) @@ -1,127 +1,132 @@ .\" -*- nroff -*- .\" .\" Copyright (c) 2016 John H. Baldwin .\" 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 AUTHOR 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 AUTHOR 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. .\" .\" $FreeBSD$ .\" -.Dd July 15, 2016 +.Dd February 25, 2020 .Dt SIGEVENT 3 .Os .Sh NAME .Nm sigevent .Nd "asynchronous event notification" .Sh SYNOPSIS .In signal.h .Sh DESCRIPTION Some operations permit threads to request asychronous notification of events via a .Vt struct sigevent structure. This structure contains several fields that describe the requested notification: .Bl -column ".Vt void (*)(union sigval)" ".Va sigev_notify_kevent_flags" .It Sy Type Ta Sy Member Ta Sy Description .It Vt int Ta sigev_notify Ta notification method .It Vt int Ta sigev_signo Ta signal number .It Vt union sigval Ta sigev_value Ta signal value .It Vt int Ta sigev_notify_kqueue Ta .Xr kqueue 2 file descriptor .It Vt unsigned short Ta sigev_notify_kevent_flags Ta kevent flags .It Vt lwpid_t Ta sigev_notify_thread_id Ta LWP ID .It Vt void (*)(union sigval) Ta sigev_notify_function Ta callback function pointer .It Vt pthread_attr_t * Ta sigev_notify_attributes Ta callback thread attributes .El .Pp The .Va sigev_notify field specifies the notification method used when the event triggers: .Bl -tag -width ".Dv SIGEV_THREAD_ID" .It Dv SIGEV_NONE No notification is sent. .It Dv SIGEV_SIGNAL The signal .Va sigev_signo is queued as a real-time signal to the calling process. The value stored in .Va sigev_value will be present in the .Va si_value of the .Vt siginfo_t structure of the queued signal. .It Dv SIGEV_THREAD The notification function in .Va sigev_notify_function is called in a separate thread context. The thread is created with the attributes specified in .Va *sigev_notify_attributes . The value stored in .Va sigev_value is passed as the sole argument to .Va sigev_notify_function . If .Va sigev_notify_attributes is .Dv NULL , the thread is created with default attributes. .It Dv SIGEV_KEVENT A new kevent is posted to the kqueue .Va sigev_notify_kqueue . The .Va udata member of the kevent structure contains the value stored in .Va sigev_value . The meaning of other fields in the kevent are specific to the type of triggered event. .It Dv SIGEV_THREAD_ID The signal .Va sigev_signo is queued to the thread whose LWP ID is .Va sigev_notify_thread_id . The value stored in .Va sigev_value will be present in the .Va si_value of the .Vt siginfo_t structure of the queued signal. .El .Sh NOTES Note that programs wishing to use .Dv SIGEV_THREAD notifications must link against the .Lb librt . .Sh SEE ALSO .Xr aio_read 2 , .Xr mq_notify 2 , .Xr timer_create 2 , .Xr siginfo 3 +.Sh HISTORY +The +.Va sigevent +structure first appeared in +.Fx 3.3 . .Sh STANDARDS The .Vt struct sigevent type conforms to .St -p1003.1-2004 . Index: stable/12/share/man/man3/stdarg.3 =================================================================== --- stable/12/share/man/man3/stdarg.3 (revision 358816) +++ stable/12/share/man/man3/stdarg.3 (revision 358817) @@ -1,237 +1,249 @@ .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the American National Standards Committee X3, on Information .\" Processing Systems. .\" .\" 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. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. .\" .\" @(#)stdarg.3 8.1 (Berkeley) 6/5/93 .\" $FreeBSD$ .\" -.Dd October 25, 2002 +.Dd February 25, 2020 .Dt STDARG 3 .Os .Sh NAME .Nm stdarg .Nd variable argument lists .Sh SYNOPSIS .In stdarg.h .Ft void .Fn va_start "va_list ap" last .Ft type .Fn va_arg "va_list ap" type .Ft void .Fn va_copy "va_list dest" "va_list src" .Ft void .Fn va_end "va_list ap" .Sh DESCRIPTION A function may be called with a varying number of arguments of varying types. The include file .In stdarg.h declares a type .Pq Em va_list and defines four macros for stepping through a list of arguments whose number and types are not known to the called function. .Pp The called function must declare an object of type .Em va_list which is used by the macros .Fn va_start , .Fn va_arg , .Fn va_copy , and .Fn va_end . .Pp The .Fn va_start macro initializes .Fa ap for subsequent use by .Fn va_arg , .Fn va_copy , and .Fn va_end , and must be called first. .Pp The parameter .Fa last is the name of the last parameter before the variable argument list, i.e., the last parameter of which the calling function knows the type. .Pp Because the address of this parameter is used in the .Fn va_start macro, it should not be declared as a register variable, or as a function or an array type. .Pp The .Fn va_arg macro expands to an expression that has the type and value of the next argument in the call. The parameter .Fa ap is the .Em va_list Fa ap initialized by .Fn va_start or .Fn va_copy . Each call to .Fn va_arg modifies .Fa ap so that the next call returns the next argument. The parameter .Fa type is a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to .Fa type . .Pp If there is no next argument, or if .Fa type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur. .Pp The first use of the .Fn va_arg macro after that of the .Fn va_start macro returns the argument after .Fa last . Successive invocations return the values of the remaining arguments. .Pp The .Fn va_copy macro copies a variable argument list, previously initialized by .Fn va_start , from .Fa src to .Fa dest . The state is preserved such that it is equivalent to calling .Fn va_start with the same second argument used with .Fa src , and calling .Fn va_arg the same number of times as called with .Fa src . .Pp The .Fn va_end macro cleans up any state associated with the variable argument list .Fa ap . .Pp Each invocation of .Fn va_start or .Fn va_copy must be paired with a corresponding invocation of .Fn va_end in the same function. .Sh RETURN VALUES The .Fn va_arg macro returns the value of the next argument. .Pp The .Fn va_start , .Fn va_copy , and .Fn va_end macros return no value. .Sh EXAMPLES The function .Em foo takes a string of format characters and prints out the argument associated with each format character based on the type. .Bd -literal -offset indent void foo(char *fmt, ...) { va_list ap; int d; char c, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case 's': /* string */ s = va_arg(ap, char *); printf("string %s\en", s); break; case 'd': /* int */ d = va_arg(ap, int); printf("int %d\en", d); break; case 'c': /* char */ /* Note: char is promoted to int. */ c = va_arg(ap, int); printf("char %c\en", c); break; } va_end(ap); } .Ed .Sh COMPATIBILITY These macros are .Em not compatible with the historic macros they replace. A backward compatible version can be found in the include file .In varargs.h . .Sh STANDARDS The .Fn va_start , .Fn va_arg , .Fn va_copy , and .Fn va_end macros conform to +.St -isoC-99 . +.Sh HISTORY +The +.Fn va_start , +.Fn va_arg +and +.Fn va_end +macros were introduced in +.St -ansiC . +The +.Fn va_copy +macro was introduced in .St -isoC-99 . .Sh BUGS Unlike the .Em varargs macros, the .Nm macros do not permit programmers to code a function with no fixed arguments. This problem generates work mainly when converting .Em varargs code to .Nm code, but it also creates difficulties for variadic functions that wish to pass all of their arguments on to a function that takes a .Em va_list argument, such as .Xr vfprintf 3 . Index: stable/12/share/man/man3/tree.3 =================================================================== --- stable/12/share/man/man3/tree.3 (revision 358816) +++ stable/12/share/man/man3/tree.3 (revision 358817) @@ -1,606 +1,609 @@ .\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ .\" .\" Copyright 2002 Niels Provos .\" 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. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by Niels Provos. .\" 4. The name of the author may not be used to endorse or promote products .\" derived from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. .\" .\" $FreeBSD$ .\" -.Dd January 24, 2015 +.Dd February 25, 2020 .Dt TREE 3 .Os .Sh NAME .Nm SPLAY_PROTOTYPE , .Nm SPLAY_GENERATE , .Nm SPLAY_ENTRY , .Nm SPLAY_HEAD , .Nm SPLAY_INITIALIZER , .Nm SPLAY_ROOT , .Nm SPLAY_EMPTY , .Nm SPLAY_NEXT , .Nm SPLAY_MIN , .Nm SPLAY_MAX , .Nm SPLAY_FIND , .Nm SPLAY_LEFT , .Nm SPLAY_RIGHT , .Nm SPLAY_FOREACH , .Nm SPLAY_INIT , .Nm SPLAY_INSERT , .Nm SPLAY_REMOVE , .Nm RB_PROTOTYPE , .Nm RB_PROTOTYPE_STATIC , .Nm RB_PROTOTYPE_INSERT , .Nm RB_PROTOTYPE_INSERT_COLOR , .Nm RB_PROTOTYPE_REMOVE , .Nm RB_PROTOTYPE_REMOVE_COLOR , .Nm RB_PROTOTYPE_FIND , .Nm RB_PROTOTYPE_NFIND , .Nm RB_PROTOTYPE_NEXT , .Nm RB_PROTOTYPE_PREV , .Nm RB_PROTOTYPE_MINMAX , .Nm RB_GENERATE , .Nm RB_GENERATE_STATIC , .Nm RB_GENERATE_INSERT , .Nm RB_GENERATE_INSERT_COLOR , .Nm RB_GENERATE_REMOVE , .Nm RB_GENERATE_REMOVE_COLOR , .Nm RB_GENERATE_FIND , .Nm RB_GENERATE_NFIND , .Nm RB_GENERATE_NEXT , .Nm RB_GENERATE_PREV , .Nm RB_GENERATE_MINMAX , .Nm RB_ENTRY , .Nm RB_HEAD , .Nm RB_INITIALIZER , .Nm RB_ROOT , .Nm RB_EMPTY , .Nm RB_NEXT , .Nm RB_PREV , .Nm RB_MIN , .Nm RB_MAX , .Nm RB_FIND , .Nm RB_NFIND , .Nm RB_LEFT , .Nm RB_RIGHT , .Nm RB_PARENT , .Nm RB_FOREACH , .Nm RB_FOREACH_FROM , .Nm RB_FOREACH_SAFE , .Nm RB_FOREACH_REVERSE , .Nm RB_FOREACH_REVERSE_FROM , .Nm RB_FOREACH_REVERSE_SAFE , .Nm RB_INIT , .Nm RB_INSERT , .Nm RB_REMOVE .Nd "implementations of splay and red-black trees" .Sh SYNOPSIS .In sys/tree.h .Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP .Fn SPLAY_GENERATE NAME TYPE FIELD CMP .Fn SPLAY_ENTRY TYPE .Fn SPLAY_HEAD HEADNAME TYPE .Ft "struct TYPE *" .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head" .Fn SPLAY_ROOT "SPLAY_HEAD *head" .Ft bool .Fn SPLAY_EMPTY "SPLAY_HEAD *head" .Ft "struct TYPE *" .Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn SPLAY_MIN NAME "SPLAY_HEAD *head" .Ft "struct TYPE *" .Fn SPLAY_MAX NAME "SPLAY_HEAD *head" .Ft "struct TYPE *" .Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME" .Ft "struct TYPE *" .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME" .Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head" .Ft void .Fn SPLAY_INIT "SPLAY_HEAD *head" .Ft "struct TYPE *" .Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Fn RB_PROTOTYPE NAME TYPE FIELD CMP .Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP .Fn RB_PROTOTYPE_INSERT NAME TYPE ATTR .Fn RB_PROTOTYPE_INSERT_COLOR NAME TYPE ATTR .Fn RB_PROTOTYPE_REMOVE NAME TYPE ATTR .Fn RB_PROTOTYPE_REMOVE_COLOR NAME TYPE ATTR .Fn RB_PROTOTYPE_FIND NAME TYPE ATTR .Fn RB_PROTOTYPE_NFIND NAME TYPE ATTR .Fn RB_PROTOTYPE_NEXT NAME TYPE ATTR .Fn RB_PROTOTYPE_PREV NAME TYPE ATTR .Fn RB_PROTOTYPE_MINMAX NAME TYPE ATTR .Fn RB_GENERATE NAME TYPE FIELD CMP .Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP .Fn RB_GENERATE_INSERT NAME TYPE FIELD CMP ATTR .Fn RB_GENERATE_INSERT_COLOR NAME TYPE FIELD ATTR .Fn RB_GENERATE_REMOVE NAME TYPE FIELD ATTR .Fn RB_GENERATE_REMOVE_COLOR NAME TYPE FIELD ATTR .Fn RB_GENERATE_FIND NAME TYPE FIELD CMP ATTR .Fn RB_GENERATE_NFIND NAME TYPE FIELD CMP ATTR .Fn RB_GENERATE_NEXT NAME TYPE FIELD ATTR .Fn RB_GENERATE_PREV NAME TYPE FIELD ATTR .Fn RB_GENERATE_MINMAX NAME TYPE FIELD ATTR .Fn RB_ENTRY TYPE .Fn RB_HEAD HEADNAME TYPE .Fn RB_INITIALIZER "RB_HEAD *head" .Ft "struct TYPE *" .Fn RB_ROOT "RB_HEAD *head" .Ft "bool" .Fn RB_EMPTY "RB_HEAD *head" .Ft "struct TYPE *" .Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn RB_MIN NAME "RB_HEAD *head" .Ft "struct TYPE *" .Fn RB_MAX NAME "RB_HEAD *head" .Ft "struct TYPE *" .Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME" .Ft "struct TYPE *" .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME" .Ft "struct TYPE *" .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME" .Fn RB_FOREACH VARNAME NAME "RB_HEAD *head" .Fn RB_FOREACH_FROM "VARNAME" "NAME" "POS_VARNAME" .Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME" .Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head" .Fn RB_FOREACH_REVERSE_FROM "VARNAME" "NAME" "POS_VARNAME" .Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME" .Ft void .Fn RB_INIT "RB_HEAD *head" .Ft "struct TYPE *" .Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm" .Ft "struct TYPE *" .Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm" .Sh DESCRIPTION These macros define data structures for different types of trees: splay trees and red-black trees. .Pp In the macro definitions, .Fa TYPE is the name tag of a user defined structure that must contain a field of type .Vt SPLAY_ENTRY , or .Vt RB_ENTRY , named .Fa ENTRYNAME . The argument .Fa HEADNAME is the name tag of a user defined structure that must be declared using the macros .Fn SPLAY_HEAD , or .Fn RB_HEAD . The argument .Fa NAME has to be a unique name prefix for every tree that is defined. .Pp The function prototypes are declared with .Fn SPLAY_PROTOTYPE , .Fn RB_PROTOTYPE , or .Fn RB_PROTOTYPE_STATIC . The function bodies are generated with .Fn SPLAY_GENERATE , .Fn RB_GENERATE , or .Fn RB_GENERATE_STATIC . See the examples below for further explanation of how these macros are used. .Sh SPLAY TREES A splay tree is a self-organizing data structure. Every operation on the tree causes a splay to happen. The splay moves the requested node to the root of the tree and partly rebalances it. .Pp This has the benefit that request locality causes faster lookups as the requested nodes move to the top of the tree. On the other hand, every lookup causes memory writes. .Pp The Balance Theorem bounds the total access time for .Ar m operations and .Ar n inserts on an initially empty tree as .Fn O "\*[lp]m + n\*[rp]lg n" . The amortized cost for a sequence of .Ar m accesses to a splay tree is .Fn O "lg n" . .Pp A splay tree is headed by a structure defined by the .Fn SPLAY_HEAD macro. A structure is declared as follows: .Bd -ragged -offset indent .Fn SPLAY_HEAD HEADNAME TYPE .Va head ; .Ed .Pp where .Fa HEADNAME is the name of the structure to be defined, and struct .Fa TYPE is the type of the elements to be inserted into the tree. .Pp The .Fn SPLAY_ENTRY macro declares a structure that allows elements to be connected in the tree. .Pp In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the .Fn SPLAY_PROTOTYPE macro, where .Fa NAME is a unique identifier for this particular tree. The .Fa TYPE argument is the type of the structure that is being managed by the tree. The .Fa FIELD argument is the name of the element defined by .Fn SPLAY_ENTRY . .Pp The function bodies are generated with the .Fn SPLAY_GENERATE macro. It takes the same arguments as the .Fn SPLAY_PROTOTYPE macro, but should be used only once. .Pp Finally, the .Fa CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type .Vt "struct TYPE *" . If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. .Pp The .Fn SPLAY_INIT macro initializes the tree referenced by .Fa head . .Pp The splay tree can also be initialized statically by using the .Fn SPLAY_INITIALIZER macro like this: .Bd -ragged -offset indent .Fn SPLAY_HEAD HEADNAME TYPE .Va head = .Fn SPLAY_INITIALIZER &head ; .Ed .Pp The .Fn SPLAY_INSERT macro inserts the new element .Fa elm into the tree. .Pp The .Fn SPLAY_REMOVE macro removes the element .Fa elm from the tree pointed by .Fa head . .Pp The .Fn SPLAY_FIND macro can be used to find a particular element in the tree. .Bd -literal -offset indent struct TYPE find, *res; find.key = 30; res = SPLAY_FIND(NAME, head, &find); .Ed .Pp The .Fn SPLAY_ROOT , .Fn SPLAY_MIN , .Fn SPLAY_MAX , and .Fn SPLAY_NEXT macros can be used to traverse the tree: .Bd -literal -offset indent for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np)) .Ed .Pp Or, for simplicity, one can use the .Fn SPLAY_FOREACH macro: .Bd -ragged -offset indent .Fn SPLAY_FOREACH np NAME head .Ed .Pp The .Fn SPLAY_EMPTY macro should be used to check whether a splay tree is empty. .Sh RED-BLACK TREES A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions: .Bl -enum -offset indent .It Every search path from the root to a leaf consists of the same number of black nodes. .It Each red node (except for the root) has a black parent. .It Each leaf node is black. .El .Pp Every operation on a red-black tree is bounded as .Fn O "lg n" . The maximum height of a red-black tree is .Fn 2lg "n + 1" . .Pp A red-black tree is headed by a structure defined by the .Fn RB_HEAD macro. A structure is declared as follows: .Bd -ragged -offset indent .Fn RB_HEAD HEADNAME TYPE .Va head ; .Ed .Pp where .Fa HEADNAME is the name of the structure to be defined, and struct .Fa TYPE is the type of the elements to be inserted into the tree. .Pp The .Fn RB_ENTRY macro declares a structure that allows elements to be connected in the tree. .Pp In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the .Fn RB_PROTOTYPE or .Fn RB_PROTOTYPE_STATIC macro, where .Fa NAME is a unique identifier for this particular tree. The .Fa TYPE argument is the type of the structure that is being managed by the tree. The .Fa FIELD argument is the name of the element defined by .Fn RB_ENTRY . Individual prototypes can be declared with .Fn RB_PROTOTYPE_INSERT , .Fn RB_PROTOTYPE_INSERT_COLOR , .Fn RB_PROTOTYPE_REMOVE , .Fn RB_PROTOTYPE_REMOVE_COLOR , .Fn RB_PROTOTYPE_FIND , .Fn RB_PROTOTYPE_NFIND , .Fn RB_PROTOTYPE_NEXT , .Fn RB_PROTOTYPE_PREV , and .Fn RB_PROTOTYPE_MINMAX in case not all functions are required. The individual prototype macros expect .Fa NAME , .Fa TYPE , and .Fa ATTR arguments. The .Fa ATTR argument must be empty for global functions or .Fa static for static functions. .Pp The function bodies are generated with the .Fn RB_GENERATE or .Fn RB_GENERATE_STATIC macro. These macros take the same arguments as the .Fn RB_PROTOTYPE and .Fn RB_PROTOTYPE_STATIC macros, but should be used only once. As an alternative individual function bodies are generated with the .Fn RB_GENERATE_INSERT , .Fn RB_GENERATE_INSERT_COLOR , .Fn RB_GENERATE_REMOVE , .Fn RB_GENERATE_REMOVE_COLOR , .Fn RB_GENERATE_FIND , .Fn RB_GENERATE_NFIND , .Fn RB_GENERATE_NEXT , .Fn RB_GENERATE_PREV , and .Fn RB_GENERATE_MINMAX macros. .Pp Finally, the .Fa CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type .Vt "struct TYPE *" . If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. .Pp The .Fn RB_INIT macro initializes the tree referenced by .Fa head . .Pp The red-black tree can also be initialized statically by using the .Fn RB_INITIALIZER macro like this: .Bd -ragged -offset indent .Fn RB_HEAD HEADNAME TYPE .Va head = .Fn RB_INITIALIZER &head ; .Ed .Pp The .Fn RB_INSERT macro inserts the new element .Fa elm into the tree. .Pp The .Fn RB_REMOVE macro removes the element .Fa elm from the tree pointed by .Fa head . .Pp The .Fn RB_FIND and .Fn RB_NFIND macros can be used to find a particular element in the tree. .Bd -literal -offset indent struct TYPE find, *res; find.key = 30; res = RB_FIND(NAME, head, &find); .Ed .Pp The .Fn RB_ROOT , .Fn RB_MIN , .Fn RB_MAX , .Fn RB_NEXT , and .Fn RB_PREV macros can be used to traverse the tree: .Pp .Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))" .Pp Or, for simplicity, one can use the .Fn RB_FOREACH or .Fn RB_FOREACH_REVERSE macro: .Bd -ragged -offset indent .Fn RB_FOREACH np NAME head .Ed .Pp The macros .Fn RB_FOREACH_SAFE and .Fn RB_FOREACH_REVERSE_SAFE traverse the tree referenced by head in a forward or reverse direction respectively, assigning each element in turn to np. However, unlike their unsafe counterparts, they permit both the removal of np as well as freeing it from within the loop safely without interfering with the traversal. .Pp Both .Fn RB_FOREACH_FROM and .Fn RB_FOREACH_REVERSE_FROM may be used to continue an interrupted traversal in a forward or reverse direction respectively. The head pointer is not required. The pointer to the node from where to resume the traversal should be passed as their last argument, and will be overwritten to provide safe traversal. .Pp The .Fn RB_EMPTY macro should be used to check whether a red-black tree is empty. .Sh NOTES Trying to free a tree in the following way is a common error: .Bd -literal -offset indent SPLAY_FOREACH(var, NAME, head) { SPLAY_REMOVE(NAME, head, var); free(var); } free(head); .Ed .Pp Since .Va var is freed, the .Fn FOREACH macro refers to a pointer that may have been reallocated already. Proper code needs a second variable. .Bd -literal -offset indent for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { nxt = SPLAY_NEXT(NAME, head, var); SPLAY_REMOVE(NAME, head, var); free(var); } .Ed .Pp Both .Fn RB_INSERT and .Fn SPLAY_INSERT return .Dv NULL if the element was inserted in the tree successfully, otherwise they return a pointer to the element with the colliding key. .Pp Accordingly, .Fn RB_REMOVE and .Fn SPLAY_REMOVE return the pointer to the removed element otherwise they return .Dv NULL to indicate an error. .Sh SEE ALSO .Xr queue 3 +.Sh HISTORY +The tree macros first appeared in +.Fx 4.6 . .Sh AUTHORS The author of the tree macros is .An Niels Provos . Index: stable/12 =================================================================== --- stable/12 (revision 358816) +++ stable/12 (revision 358817) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r358352