Re: replacing if-then-else strcmp ladders with switch cases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jun 12, 2020 at 11:02 PM Valdis Klētnieks
<valdis.kletnieks@xxxxxx> wrote:
>
> On Fri, 12 Jun 2020 12:05:55 -0600, jim.cromie@xxxxxxxxx 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@xxxxxxxxxxxxxxxxx
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]

  Powered by Linux