about summary refs log tree commit diff
path: root/sysdeps/generic/dl-tunables-parse.h
blob: 92b173112f7d15e07d1ce2b5504df18b3017a63f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Helper functions to handle tunable strings.
   Copyright (C) 2023-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#ifndef _DL_TUNABLES_PARSE_H
#define _DL_TUNABLES_PARSE_H 1

#include <assert.h>
#include <string.h>

/* Compare the contents of STRVAL with STR of size LEN.  The STR might not
   be null-terminated.   */
static __always_inline bool
tunable_strcmp (const struct tunable_str_t *strval, const char *str,
		size_t len)
{
  return strval->len == len && memcmp (strval->str, str, len) == 0;
}
#define tunable_strcmp_cte(__tunable, __str) \
 tunable_strcmp (&__tunable->strval, __str, sizeof (__str) - 1)

/*
   Helper functions to iterate over a tunable string composed by multiple
   suboptions separated by commaxi; this is a common pattern for CPU.  Each
   suboptions is return in the form of { address, size } (no null terminated).
   For instance:

     struct tunable_str_comma_t ts;
     tunable_str_comma_init (&ts, valp);

     struct tunable_str_t t;
     while (tunable_str_comma_next (&ts, &t))
      {
	_dl_printf ("[%s] %.*s (%d)\n",
		    __func__,
		    (int) tstr.len,
		    tstr.str,
		    (int) tstr.len);

        if (tunable_str_comma_strcmp (&t, opt, opt1_len))
	  {
	    [...]
	  }
	else if (tunable_str_comma_strcmp_cte (&t, "opt2"))
	  {
	    [...]
	  }
      }

   NB: These function are expected to be called from tunable callback
   functions along with tunable_val_t with string types.
*/

struct tunable_str_comma_state_t
{
  const char *p;
  size_t plen;
  size_t maxplen;
};

struct tunable_str_comma_t
{
  const char *str;
  size_t len;
  bool disable;
};

static inline void
tunable_str_comma_init (struct tunable_str_comma_state_t *state,
			tunable_val_t *valp)
{
  assert (valp->strval.str != NULL);
  state->p = valp->strval.str;
  state->plen = 0;
  state->maxplen = valp->strval.len;
}

static inline bool
tunable_str_comma_next (struct tunable_str_comma_state_t *state,
			struct tunable_str_comma_t *str)
{
  if (*state->p == '\0' || state->plen >= state->maxplen)
    return false;

  const char *c;
  for (c = state->p; *c != ','; c++, state->plen++)
    if (*c == '\0' || state->plen == state->maxplen)
      break;

  str->str = state->p;
  str->len = c - state->p;

  if (str->len > 0)
    {
      str->disable = *str->str == '-';
      if (str->disable)
	{
	  str->str = str->str + 1;
	  str->len = str->len - 1;
	}
    }

  state->p = c + 1;
  state->plen++;

  return true;
}

/* Compare the contents of T with STR of size LEN.  The STR might not be
   null-terminated.   */
static __always_inline bool
tunable_str_comma_strcmp (const struct tunable_str_comma_t *t, const char *str,
			  size_t len)
{
  return t->len == len && memcmp (t->str, str, len) == 0;
}
#define tunable_str_comma_strcmp_cte(__t, __str) \
  tunable_str_comma_strcmp (__t, __str, sizeof (__str) - 1)

#endif