2 * The PCI Library -- ID to Name Hash
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
17 struct id_bucket *next;
22 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
25 struct id_bucket *next;
28 #define BUCKET_ALIGNMENT sizeof(union id_align)
30 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
32 static void *id_alloc(struct pci_access *a, unsigned int size)
34 struct id_bucket *buck = a->current_id_bucket;
39 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
40 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
43 if (!buck || buck->full + size > BUCKET_SIZE)
45 buck = pci_malloc(a, BUCKET_SIZE);
46 buck->next = a->current_id_bucket;
47 a->current_id_bucket = buck;
48 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
51 buck->full = BUCKET_ALIGN(buck->full + size);
52 return (byte *)buck + pos;
55 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
59 h = id12 ^ (id34 << 3) ^ (cat << 5);
64 pci_id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text, enum id_entry_src src)
66 u32 id12 = id_pair(id1, id2);
67 u32 id34 = id_pair(id3, id4);
68 unsigned int h = id_hash(cat, id12, id34);
69 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
70 int len = strlen(text);
72 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
76 n = id_alloc(a, sizeof(struct id_entry) + len);
81 memcpy(n->name, text, len+1);
82 n->next = a->id_hash[h];
88 *pci_id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
90 struct id_entry *n, *best;
91 u32 id12 = id_pair(id1, id2);
92 u32 id34 = id_pair(id3, id4);
96 n = a->id_hash[id_hash(cat, id12, id34)];
100 if (n->id12 != id12 || n->id34 != id34 || n->cat != cat)
102 if (n->src == SRC_LOCAL && (flags & PCI_LOOKUP_SKIP_LOCAL))
104 if (n->src == SRC_NET && !(flags & PCI_LOOKUP_NETWORK))
106 if (n->src == SRC_CACHE && !(flags & PCI_LOOKUP_CACHE))
108 if (n->src == SRC_HWDB && (flags & (PCI_LOOKUP_SKIP_LOCAL | PCI_LOOKUP_NO_HWDB)))
110 if (!best || best->src < n->src)
120 pci_id_hash_free(struct pci_access *a)
122 pci_mfree(a->id_hash);
124 while (a->current_id_bucket)
126 struct id_bucket *buck = a->current_id_bucket;
127 a->current_id_bucket = buck->next;