lsusb: drop the genericstrtable hash tables

This seems like a premature ahead-of-time optimisation, which actually
hurts both code/binary size while providing no performance benefit.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
This commit is contained in:
Emil Velikov 2024-10-01 13:44:56 +01:00
parent d9e91b7dd0
commit 92547bee59
2 changed files with 1474 additions and 1614 deletions

167
names.c
View File

@ -25,41 +25,19 @@
#include "sysfs.h"
#define HASH1 0x10
#define HASH2 0x02
#define HASHSZ 512
static unsigned int hashnum(unsigned int num)
{
unsigned int mask1 = (unsigned int)HASH1 << 27, mask2 = (unsigned int)HASH2 << 27;
for (; mask1 >= HASH1; mask1 >>= 1, mask2 >>= 1)
if (num & mask1)
num ^= mask2;
return num & (HASHSZ-1);
}
/* ---------------------------------------------------------------------- */
static struct udev *udev = NULL;
static struct udev_hwdb *hwdb = NULL;
static struct genericstrtable *hiddescriptors_hash[HASHSZ] = { NULL, };
static struct genericstrtable *reports_hash[HASHSZ] = { NULL, };
static struct genericstrtable *huts_hash[HASHSZ] = { NULL, };
static struct genericstrtable *biass_hash[HASHSZ] = { NULL, };
static struct genericstrtable *physdess_hash[HASHSZ] = { NULL, };
static struct genericstrtable *hutus_hash[HASHSZ] = { NULL, };
static struct genericstrtable *langids_hash[HASHSZ] = { NULL, };
static struct genericstrtable *countrycodes_hash[HASHSZ] = { NULL, };
/* ---------------------------------------------------------------------- */
static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ],
static const char *names_genericstrtable(const struct genericstrtable *t,
unsigned int idx)
{
struct genericstrtable *h;
const struct genericstrtable *h;
for (h = t[hashnum(idx)]; h; h = h->next)
for (h = t; t->name; t++)
if (h->num == idx)
return h->name;
return NULL;
@ -67,42 +45,42 @@ static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ],
const char *names_hid(uint8_t hidd)
{
return names_genericstrtable(hiddescriptors_hash, hidd);
return names_genericstrtable(hiddescriptors, hidd);
}
const char *names_reporttag(uint8_t rt)
{
return names_genericstrtable(reports_hash, rt);
return names_genericstrtable(reports, rt);
}
const char *names_huts(unsigned int data)
{
return names_genericstrtable(huts_hash, data);
return names_genericstrtable(huts, data);
}
const char *names_hutus(unsigned int data)
{
return names_genericstrtable(hutus_hash, data);
return names_genericstrtable(hutus, data);
}
const char *names_langid(uint16_t langid)
{
return names_genericstrtable(langids_hash, langid);
return names_genericstrtable(langids, langid);
}
const char *names_physdes(uint8_t ph)
{
return names_genericstrtable(physdess_hash, ph);
return names_genericstrtable(physdess, ph);
}
const char *names_bias(uint8_t b)
{
return names_genericstrtable(biass_hash, b);
return names_genericstrtable(biass, b);
}
const char *names_countrycode(unsigned int countrycode)
{
return names_genericstrtable(countrycodes_hash, countrycode);
return names_genericstrtable(countrycodes, countrycode);
}
static const char *hwdb_get(const char *modalias, const char *key)
@ -260,131 +238,14 @@ void get_vendor_product_with_fallback(char *vendor, int vendor_len,
}
}
/* ---------------------------------------------------------------------- */
static int hash_genericstrtable(struct genericstrtable *t[HASHSZ],
struct genericstrtable *g)
{
struct genericstrtable *g_old;
unsigned int h = hashnum(g->num);
for (g_old = t[h]; g_old; g_old = g_old->next)
if (g_old->num == g->num)
return -1;
g->next = t[h];
t[h] = g;
return 0;
}
#define HASH_EACH(array, hash) \
for (i = 0; array[i].name; i++) { \
k = hash_genericstrtable(hash, &array[i]); \
if (k < 0) { \
r = k; \
}\
}
static int hash_tables(void)
{
int r = 0, k, i;
HASH_EACH(hiddescriptors, hiddescriptors_hash);
HASH_EACH(reports, reports_hash);
HASH_EACH(huts, huts_hash);
HASH_EACH(hutus, hutus_hash);
HASH_EACH(langids, langids_hash);
HASH_EACH(physdess, physdess_hash);
HASH_EACH(biass, biass_hash);
HASH_EACH(countrycodes, countrycodes_hash);
return r;
}
/* ---------------------------------------------------------------------- */
/*
static void print_tables(void)
{
int i;
struct audioterminal *at;
struct videoterminal *vt;
struct genericstrtable *li;
struct genericstrtable *hu;
printf("--------------------------------------------\n");
printf("\t\t Audio Terminals\n");
printf("--------------------------------------------\n");
for (i = 0; i < HASHSZ; i++) {
printf("hash: %d\n", i);
at = audioterminals_hash[i];
for (; at; at = at->next)
printf("\tentry: %s\n", at->name);
}
printf("--------------------------------------------\n");
printf("\t\t Video Terminals\n");
printf("--------------------------------------------\n");
for (i = 0; i < HASHSZ; i++) {
printf("hash: %d\n", i);
vt = videoterminals_hash[i];
for (; vt; vt = vt->next)
printf("\tentry: %s\n", vt->name);
}
printf("--------------------------------------------\n");
printf("\t\t Languages\n");
printf("--------------------------------------------\n");
for (i = 0; i < HASHSZ; i++) {
li = langids_hash[i];
if (li)
printf("hash: %d\n", i);
for (; li; li = li->next)
printf("\tid: %x, entry: %s\n", li->num, li->name);
}
printf("--------------------------------------------\n");
printf("\t\t Conutry Codes\n");
printf("--------------------------------------------\n");
for (i = 0; i < HASHSZ; i++) {
hu = countrycodes_hash[i];
if (hu)
printf("hash: %d\n", i);
for (; hu; hu = hu->next)
printf("\tid: %x, entry: %s\n", hu->num, hu->name);
}
printf("--------------------------------------------\n");
}
*/
int names_init(void)
{
int r;
udev = udev_new();
if (!udev)
r = -1;
else {
hwdb = udev_hwdb_new(udev);
if (!hwdb)
r = -1;
}
return -1;
r = hash_tables();
return r;
hwdb = udev_hwdb_new(udev);
return hwdb ? 0 : -1;
}
void names_exit(void)

2921
usb-spec.h

File diff suppressed because it is too large Load Diff