or1k: Add prctl wrapper to unwrap variadic args
[glibc.git] / sysdeps / generic / dl-tunables-parse.h
blob92b173112f7d15e07d1ce2b5504df18b3017a63f
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
22 #include <assert.h>
23 #include <string.h>
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,
29 size_t len)
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).
40 For instance:
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",
49 __func__,
50 (int) tstr.len,
51 tstr.str,
52 (int) tstr.len);
54 if (tunable_str_comma_strcmp (&t, opt, opt1_len))
56 [...]
58 else if (tunable_str_comma_strcmp_cte (&t, "opt2"))
60 [...]
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
70 const char *p;
71 size_t plen;
72 size_t maxplen;
75 struct tunable_str_comma_t
77 const char *str;
78 size_t len;
79 bool disable;
82 static inline void
83 tunable_str_comma_init (struct tunable_str_comma_state_t *state,
84 tunable_val_t *valp)
86 assert (valp->strval.str != NULL);
87 state->p = valp->strval.str;
88 state->plen = 0;
89 state->maxplen = valp->strval.len;
92 static inline bool
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)
97 return false;
99 const char *c;
100 for (c = state->p; *c != ','; c++, state->plen++)
101 if (*c == '\0' || state->plen == state->maxplen)
102 break;
104 str->str = state->p;
105 str->len = c - state->p;
107 if (str->len > 0)
109 str->disable = *str->str == '-';
110 if (str->disable)
112 str->str = str->str + 1;
113 str->len = str->len - 1;
117 state->p = c + 1;
118 state->plen++;
120 return true;
123 /* Compare the contents of T with STR of size LEN. The STR might not be
124 null-terminated. */
125 static __always_inline bool
126 tunable_str_comma_strcmp (const struct tunable_str_comma_t *t, const char *str,
127 size_t len)
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)
134 #endif