elf: Do not duplicate the GLIBC_TUNABLES string
[glibc.git] / elf / dl-tunables.h
blob5d5ee2c3aa3e23e37d8cf444cde411b7d7593abf
1 /* The tunable framework. See the README to know how to use the tunable in
2 a glibc module.
4 Copyright (C) 2016-2023 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
21 #ifndef _TUNABLES_H_
22 #define _TUNABLES_H_
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <stdint.h>
28 typedef intmax_t tunable_num_t;
30 typedef union
32 tunable_num_t numval;
33 struct tunable_str_t
35 const char *str;
36 size_t len;
37 } strval;
38 } tunable_val_t;
40 typedef void (*tunable_callback_t) (tunable_val_t *);
42 /* Full name for a tunable is top_ns.tunable_ns.id. */
43 #define TUNABLE_NAME_S(top,ns,id) #top "." #ns "." #id
45 #define TUNABLE_ENUM_NAME(__top,__ns,__id) TUNABLE_ENUM_NAME1 (__top,__ns,__id)
46 #define TUNABLE_ENUM_NAME1(__top,__ns,__id) __top ## _ ## __ns ## _ ## __id
48 #include "dl-tunable-list.h"
50 extern void __tunables_init (char **);
51 extern void __tunables_print (void);
52 extern bool __tunable_is_initialized (tunable_id_t);
53 extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
54 extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *,
55 tunable_num_t *);
56 extern void __tunable_get_default (tunable_id_t id, void *valp);
57 rtld_hidden_proto (__tunables_init)
58 rtld_hidden_proto (__tunables_print)
59 rtld_hidden_proto (__tunable_is_initialized)
60 rtld_hidden_proto (__tunable_get_val)
61 rtld_hidden_proto (__tunable_set_val)
62 rtld_hidden_proto (__tunable_get_default)
64 /* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and
65 TUNABLE_NAMESPACE are defined. This is useful shorthand to get and set
66 tunables within a module. */
67 #if defined TOP_NAMESPACE && defined TUNABLE_NAMESPACE
68 # define TUNABLE_IS_INITIALIZED(__id) \
69 TUNABLE_IS_INITIALIZED_FULL(TOP_NAMESPACE, TUNABLE_NAMESPACE, __id)
70 # define TUNABLE_GET_DEFAULT(__id, __type) \
71 TUNABLE_GET_DEFAULT_FULL(TOP_NAMESPACE, TUNABLE_NAMESPACE,__id, __type)
72 # define TUNABLE_GET(__id, __type, __cb) \
73 TUNABLE_GET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __cb)
74 # define TUNABLE_SET(__id, __val) \
75 TUNABLE_SET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __val)
76 # define TUNABLE_SET_WITH_BOUNDS(__id, __val, __min, __max) \
77 TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \
78 __val, __min, __max)
79 #else
80 # define TUNABLE_IS_INITIALIZED(__top, __ns, __id) \
81 TUNABLE_IS_INITIALIZED_FULL(__top, __ns, __id)
82 # define TUNABLE_GET_DEFAULT(__top, __ns, __type) \
83 TUNABLE_GET_DEFAULT_FULL(__top, __ns, __id, __type)
84 # define TUNABLE_GET(__top, __ns, __id, __type, __cb) \
85 TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb)
86 # define TUNABLE_SET(__top, __ns, __id, __val) \
87 TUNABLE_SET_FULL (__top, __ns, __id, __val)
88 # define TUNABLE_SET_WITH_BOUNDS(__top, __ns, __id, __val, __min, __max) \
89 TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __val, __min, __max)
90 #endif
92 /* Return whether the tunable was initialized by the environment variable. */
93 #define TUNABLE_IS_INITIALIZED_FULL(__top, __ns, __id) \
94 ({ \
95 tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \
96 __tunable_is_initialized (id); \
99 /* Return the default value of the tunable. */
100 #define TUNABLE_GET_DEFAULT_FULL(__top, __ns, __id, __type) \
101 ({ \
102 tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \
103 __type __ret; \
104 __tunable_get_default (id, &__ret); \
105 __ret; \
108 /* Get and return a tunable value. If the tunable was set externally and __CB
109 is defined then call __CB before returning the value. */
110 #define TUNABLE_GET_FULL(__top, __ns, __id, __type, __cb) \
111 ({ \
112 tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \
113 __type ret; \
114 __tunable_get_val (id, &ret, __cb); \
115 ret; \
118 /* Set a tunable value. */
119 #define TUNABLE_SET_FULL(__top, __ns, __id, __val) \
120 ({ \
121 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
122 & (tunable_val_t) {.numval = __val}, NULL, NULL); \
125 /* Set a tunable value together with min/max values. */
126 #define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id,__val, __min, __max) \
127 ({ \
128 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
129 & (tunable_val_t) {.numval = __val}, \
130 & (tunable_num_t) {__min}, \
131 & (tunable_num_t) {__max}); \
134 /* Namespace sanity for callback functions. Use this macro to keep the
135 namespace of the modules clean. */
136 #define TUNABLE_CALLBACK(__name) _dl_tunable_ ## __name
138 static __always_inline bool
139 tunable_val_lt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
141 if (unsigned_cmp)
142 return (uintmax_t) lhs < (uintmax_t) rhs;
143 else
144 return lhs < rhs;
147 static __always_inline bool
148 tunable_val_gt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
150 if (unsigned_cmp)
151 return (uintmax_t) lhs > (uintmax_t) rhs;
152 else
153 return lhs > rhs;
156 /* Compare two name strings, bounded by the name hardcoded in glibc. */
157 static __always_inline bool
158 tunable_is_name (const char *orig, const char *envname)
160 for (;*orig != '\0' && *envname != '\0'; envname++, orig++)
161 if (*orig != *envname)
162 break;
164 /* The ENVNAME is immediately followed by a value. */
165 if (*orig == '\0' && *envname == '=')
166 return true;
167 else
168 return false;
171 #endif