1 /* Helper functions to handle tunable strings.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef _DL_TUNABLES_PARSE_H
20 #define _DL_TUNABLES_PARSE_H 1
25 /* Compare the contents of STRVAL with STR of size LEN. The STR might not
26 be null-terminated. */
27 static __always_inline
bool
28 tunable_strcmp (const struct tunable_str_t
*strval
, const char *str
,
31 return strval
->len
== len
&& memcmp (strval
->str
, str
, len
) == 0;
33 #define tunable_strcmp_cte(__tunable, __str) \
34 tunable_strcmp (&__tunable->strval, __str, sizeof (__str) - 1)
37 Helper functions to iterate over a tunable string composed by multiple
38 suboptions separated by commaxi; this is a common pattern for CPU. Each
39 suboptions is return in the form of { address, size } (no null terminated).
42 struct tunable_str_comma_t ts;
43 tunable_str_comma_init (&ts, valp);
45 struct tunable_str_t t;
46 while (tunable_str_comma_next (&ts, &t))
48 _dl_printf ("[%s] %.*s (%d)\n",
54 if (tunable_str_comma_strcmp (&t, opt, opt1_len))
58 else if (tunable_str_comma_strcmp_cte (&t, "opt2"))
64 NB: These function are expected to be called from tunable callback
65 functions along with tunable_val_t with string types.
68 struct tunable_str_comma_state_t
75 struct tunable_str_comma_t
83 tunable_str_comma_init (struct tunable_str_comma_state_t
*state
,
86 assert (valp
->strval
.str
!= NULL
);
87 state
->p
= valp
->strval
.str
;
89 state
->maxplen
= valp
->strval
.len
;
93 tunable_str_comma_next (struct tunable_str_comma_state_t
*state
,
94 struct tunable_str_comma_t
*str
)
96 if (*state
->p
== '\0' || state
->plen
>= state
->maxplen
)
100 for (c
= state
->p
; *c
!= ','; c
++, state
->plen
++)
101 if (*c
== '\0' || state
->plen
== state
->maxplen
)
105 str
->len
= c
- state
->p
;
109 str
->disable
= *str
->str
== '-';
112 str
->str
= str
->str
+ 1;
113 str
->len
= str
->len
- 1;
123 /* Compare the contents of T with STR of size LEN. The STR might not be
125 static __always_inline
bool
126 tunable_str_comma_strcmp (const struct tunable_str_comma_t
*t
, const char *str
,
129 return t
->len
== len
&& memcmp (t
->str
, str
, len
) == 0;
131 #define tunable_str_comma_strcmp_cte(__t, __str) \
132 tunable_str_comma_strcmp (__t, __str, sizeof (__str) - 1)