* replacing if-then-else strcmp ladders with switch cases
@ 2020-06-12 18:05 jim.cromie
2020-06-13 5:02 ` Valdis Klētnieks
0 siblings, 1 reply; 3+ messages in thread
From: jim.cromie @ 2020-06-12 18:05 UTC (permalink / raw)
To: kernelnewbies
considering lib/dynamic_debug.c
we have
...
} else if (!strcmp(words[i], "module")) {
rc = check_set(&query->module, words[i+1], "module");
} else if (!strcmp(words[i], "format")) {
...
are there any built-in hash functions which would allow this ?
switch (keyword) {
case Hash("module"):
..... break;
case Hash("format"):
.... break;
default: return -EINVAL
}
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: replacing if-then-else strcmp ladders with switch cases
2020-06-12 18:05 replacing if-then-else strcmp ladders with switch cases jim.cromie
@ 2020-06-13 5:02 ` Valdis Klētnieks
2020-06-13 17:08 ` jim.cromie
0 siblings, 1 reply; 3+ messages in thread
From: Valdis Klētnieks @ 2020-06-13 5:02 UTC (permalink / raw)
To: jim.cromie; +Cc: kernelnewbies
[-- Attachment #1.1: Type: text/plain, Size: 733 bytes --]
On Fri, 12 Jun 2020 12:05:55 -0600, jim.cromie@gmail.com said:
> considering lib/dynamic_debug.c
> we have
>
> ...
> } else if (!strcmp(words[i], "module")) {
> rc = check_set(&query->module, words[i+1], "module");
> } else if (!strcmp(words[i], "format")) {
> ...
>
> are there any built-in hash functions which would allow this ?
>
> switch (keyword) {
> case Hash("module"):
> ..... break;
> case Hash("format"):
> .... break;
> default: return -EINVAL
> }
There's hash functions. But they're all cryptographic hashes that return
things that are far too many bits to use as the index of a switch.
Also, you have the problem that the cases of a switch have to be something
that can be evaluated at compile time....
[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]
[-- Attachment #2: Type: text/plain, Size: 170 bytes --]
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: replacing if-then-else strcmp ladders with switch cases
2020-06-13 5:02 ` Valdis Klētnieks
@ 2020-06-13 17:08 ` jim.cromie
0 siblings, 0 replies; 3+ messages in thread
From: jim.cromie @ 2020-06-13 17:08 UTC (permalink / raw)
To: Valdis Klētnieks; +Cc: kernelnewbies
On Fri, Jun 12, 2020 at 11:02 PM Valdis Klētnieks
<valdis.kletnieks@vt.edu> wrote:
>
> On Fri, 12 Jun 2020 12:05:55 -0600, jim.cromie@gmail.com said:
> > considering lib/dynamic_debug.c
> > we have
> >
> > ...
> > } else if (!strcmp(words[i], "module")) {
> > rc = check_set(&query->module, words[i+1], "module");
> > } else if (!strcmp(words[i], "format")) {
> > ...
> >
> > are there any built-in hash functions which would allow this ?
> >
> > switch (keyword) {
> > case Hash("module"):
> > ..... break;
that should be:
/* 2 compatible implementations */
#define chash(keywd) simple_hash_computed_by_compiler ( keywd )
#define rhash(keywd) simple_hash_computed_at_runtime ( keywd )
switch ( rhash (keyword) ) {
case chash("module"):
... break;
> There's hash functions. But they're all cryptographic hashes that return
> things that are far too many bits to use as the index of a switch.
>
> Also, you have the problem that the cases of a switch have to be something
> that can be evaluated at compile time....
ok, so they dont exist.
I suspect its doable, but needs big -fu, more than I got
that said, I could imagine a compile-time check to insure that
the simple-hash on a fixed dictionary yields no collision
DEFINE_DICTIONARY( "dyndbg.control.adverbs", "module", "file", "line", "func" )
but thats not essential, since if theyre in case:s,
the compiler would notice the collision,
and so chash() doesnt need to do collision detection either.
this would be slick, and I think usable more than once.
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-06-13 17:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-12 18:05 replacing if-then-else strcmp ladders with switch cases jim.cromie
2020-06-13 5:02 ` Valdis Klētnieks
2020-06-13 17:08 ` jim.cromie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).