I would like to add the SPLAY_GENERATE_STATIC and SPLAY_PROTOTYPE_STATIC macros to tree.h. I think this is a good idea for two reasons: First, external linkage is potentially counter-productive when not required. Second, to increase interchangeability with the RB_* macros.
I prepared a commit to do just that: https://github.com/bitrig/bitrig/commit/51ec39f44b2ee96df0b114089dced6933bec7891 (patch included below for convenience) Comments, ok? cheers, natano --- commit 51ec39f44b2ee96df0b114089dced6933bec7891 Author: Martin Natano <[email protected]> Date: Sun Oct 5 20:22:58 2014 +0200 Add SPLAY_{GENERATE,PROTOTYPE}_STATIC. diff --git a/share/man/man3/Makefile b/share/man/man3/Makefile index 808c83e..e69a4ee 100644 --- a/share/man/man3/Makefile +++ b/share/man/man3/Makefile @@ -60,7 +60,8 @@ MLINKS+=stdarg.3 varargs.3 stdarg.3 va_arg.3 stdarg.3 va_end.3 MLINKS+=stdarg.3 va_start.3 stdarg.3 va_copy.3 MLINKS+=dlfcn.3 dlopen.3 dlfcn.3 dlclose.3 dlfcn.3 dlsym.3 dlfcn.3 dlctl.3 \ dlfcn.3 dlerror.3 dlfcn.3 dladdr.3 -MLINKS+=tree.3 SPLAY_PROTOTYPE.3 tree.3 SPLAY_GENERATE.3 \ +MLINKS+=tree.3 SPLAY_PROTOTYPE.3 tree.3 SPLAY_PROTOTYPE_STATIC.3 \ + tree.3 SPLAY_GENERATE.3 tree.3 SPLAY_GENERATE_STATIC.3 \ tree.3 SPLAY_ENTRY.3 tree.3 SPLAY_HEAD.3 \ tree.3 SPLAY_INITIALIZER.3 tree.3 SPLAY_ROOT.3 \ tree.3 SPLAY_EMPTY.3 tree.3 SPLAY_NEXT.3 \ diff --git a/share/man/man3/tree.3 b/share/man/man3/tree.3 index b5c3758..dfe3aeb 100644 --- a/share/man/man3/tree.3 +++ b/share/man/man3/tree.3 @@ -28,7 +28,9 @@ .Os .Sh NAME .Nm SPLAY_PROTOTYPE , +.Nm SPLAY_PROTOTYPE_STATIC , .Nm SPLAY_GENERATE , +.Nm SPLAY_GENERATE_STATIC , .Nm SPLAY_ENTRY , .Nm SPLAY_HEAD , .Nm SPLAY_INITIALIZER , @@ -74,7 +76,9 @@ .In sys/tree.h .Pp .Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP" +.Fn SPLAY_PROTOTYPE_STATIC "NAME" "TYPE" "FIELD" "CMP" .Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP" +.Fn SPLAY_GENERATE_STATIC "NAME" "TYPE" "FIELD" "CMP" .Fn SPLAY_ENTRY "TYPE" .Fn SPLAY_HEAD "HEADNAME" "TYPE" .Ft "struct TYPE *" @@ -166,11 +170,13 @@ has to be a unique name prefix for every tree that is defined. .Pp The function prototypes are declared with .Li SPLAY_PROTOTYPE , +.Li SPLAY_PROTOTYPE_STATIC , .Li RB_PROTOTYPE , or .Li RB_PROTOTYPE_STATIC . The function bodies are generated with .Li SPLAY_GENERATE , +.Li SPLAY_GENERATE_STATIC , .Li RB_GENERATE , or .Li RB_GENERATE_STATIC . @@ -227,10 +233,14 @@ argument is the name of the element defined by .Pp The function bodies are generated with the .Fn SPLAY_GENERATE -macro. -It takes the same arguments as the +or +.Fn SPLAY_GENERATE_STATIC +macros. +Theses macros take the same arguments as the .Fn SPLAY_PROTOTYPE -macro, but should be used only once. +and +.Fn SPLAY_PROTOTYPE_STATIC +macros, but should be used only once. .Pp Finally, the diff --git a/sys/sys/tree.h b/sys/sys/tree.h index ada7d90..bc1786f 100644 --- a/sys/sys/tree.h +++ b/sys/sys/tree.h @@ -123,11 +123,15 @@ struct { \ /* Generates prototypes and inline functions */ -#define SPLAY_PROTOTYPE(name, type, field, cmp) \ -void name##_SPLAY(struct name *, struct type *); \ -void name##_SPLAY_MINMAX(struct name *, int); \ -struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ -struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ +#define SPLAY_PROTOTYPE(name, type, field, cmp) \ + SPLAY_PROTOTYPE_INTERNAL(name, type, field, cmp,) +#define SPLAY_PROTOTYPE_STATIC(name, type, field, cmp) \ + SPLAY_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static) +#define SPLAY_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ +attr void name##_SPLAY(struct name *, struct type *); \ +attr void name##_SPLAY_MINMAX(struct name *, int); \ +attr struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ +attr struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ \ /* Finds the node with the same key as elm */ \ __attribute__((__unused__)) static __inline struct type * \ @@ -165,8 +169,12 @@ name##_SPLAY_MIN_MAX(struct name *head, int val) \ /* Main splay operation. * Moves node close to the key of elm to top */ -#define SPLAY_GENERATE(name, type, field, cmp) \ -struct type * \ +#define SPLAY_GENERATE(name, type, field, cmp) \ + SPLAY_GENERATE_INTERNAL(name, type, field, cmp,) +#define SPLAY_GENERATE_STATIC(name, type, field, cmp) \ + SPLAY_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static) +#define SPLAY_GENERATE_INTERNAL(name, type, field, cmp, attr) \ +attr struct type * \ name##_SPLAY_INSERT(struct name *head, struct type *elm) \ { \ if (SPLAY_EMPTY(head)) { \ @@ -190,7 +198,7 @@ name##_SPLAY_INSERT(struct name *head, struct type *elm) \ return (NULL); \ } \ \ -struct type * \ +attr struct type * \ name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ { \ struct type *__tmp; \ @@ -211,7 +219,7 @@ name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ return (NULL); \ } \ \ -void \ +attr void \ name##_SPLAY(struct name *head, struct type *elm) \ { \ struct type __node, *__left, *__right, *__tmp; \ @@ -249,7 +257,8 @@ name##_SPLAY(struct name *head, struct type *elm) \ /* Splay with either the minimum or the maximum element \ * Used to find minimum or maximum element in tree. \ */ \ -void name##_SPLAY_MINMAX(struct name *head, int __comp) \ +attr void \ +name##_SPLAY_MINMAX(struct name *head, int __comp) \ { \ struct type __node, *__left, *__right, *__tmp; \ \
