]> mj.ucw.cz Git - pciutils.git/blob - lib/names-hash.c
ls-ecaps: Add decode support for IDE Extended Capability
[pciutils.git] / lib / names-hash.c
1 /*
2  *      The PCI Library -- ID to Name Hash
3  *
4  *      Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <string.h>
12
13 #include "internal.h"
14 #include "names.h"
15
16 struct id_bucket {
17   struct id_bucket *next;
18   unsigned int full;
19 };
20
21 #ifdef __GNUC__
22 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
23 #else
24 union id_align {
25   struct id_bucket *next;
26   unsigned int full;
27 };
28 #define BUCKET_ALIGNMENT sizeof(union id_align)
29 #endif
30 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
31
32 static void *id_alloc(struct pci_access *a, unsigned int size)
33 {
34   struct id_bucket *buck = a->current_id_bucket;
35   unsigned int pos;
36
37   if (!a->id_hash)
38     {
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);
41     }
42
43   if (!buck || buck->full + size > BUCKET_SIZE)
44     {
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));
49     }
50   pos = buck->full;
51   buck->full = BUCKET_ALIGN(buck->full + size);
52   return (byte *)buck + pos;
53 }
54
55 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
56 {
57   unsigned int h;
58
59   h = id12 ^ (id34 << 3) ^ (cat << 5);
60   return h % HASH_SIZE;
61 }
62
63 int
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)
65 {
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);
71
72   while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
73     n = n->next;
74   if (n)
75     return 1;
76   n = id_alloc(a, sizeof(struct id_entry) + len);
77   n->id12 = id12;
78   n->id34 = id34;
79   n->cat = cat;
80   n->src = src;
81   memcpy(n->name, text, len+1);
82   n->next = a->id_hash[h];
83   a->id_hash[h] = n;
84   return 0;
85 }
86
87 char
88 *pci_id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
89 {
90   struct id_entry *n, *best;
91   u32 id12 = id_pair(id1, id2);
92   u32 id34 = id_pair(id3, id4);
93
94   if (a->id_hash)
95     {
96       n = a->id_hash[id_hash(cat, id12, id34)];
97       best = NULL;
98       for (; n; n=n->next)
99         {
100           if (n->id12 != id12 || n->id34 != id34 || n->cat != cat)
101             continue;
102           if (n->src == SRC_LOCAL && (flags & PCI_LOOKUP_SKIP_LOCAL))
103             continue;
104           if (n->src == SRC_NET && !(flags & PCI_LOOKUP_NETWORK))
105             continue;
106           if (n->src == SRC_CACHE && !(flags & PCI_LOOKUP_CACHE))
107             continue;
108           if (n->src == SRC_HWDB && (flags & (PCI_LOOKUP_SKIP_LOCAL | PCI_LOOKUP_NO_HWDB)))
109             continue;
110           if (!best || best->src < n->src)
111             best = n;
112         }
113       if (best)
114         return best->name;
115     }
116   return NULL;
117 }
118
119 void
120 pci_id_hash_free(struct pci_access *a)
121 {
122   pci_mfree(a->id_hash);
123   a->id_hash = NULL;
124   while (a->current_id_bucket)
125     {
126       struct id_bucket *buck = a->current_id_bucket;
127       a->current_id_bucket = buck->next;
128       pci_mfree(buck);
129     }
130 }