Debian Codegraph @

src/atom.c

raw
1 /*
2 * For MIT-open-group:
3 * Copyright 1987, 1998 The Open Group
4 *
5 * For LicenseRef-digital-equipment-corporation:
6 * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
7 *
8 * For HPND:
9 * Copyright 1994 by Silicon Graphics Computer Systems, Inc.
10 *
11 * SPDX-License-Identifier: MIT-open-group AND LicenseRef-digital-equipment-corporation AND HPND
12 */
13
14 #include "config.h"
15
16 #include <assert.h>
17 #include <string.h>
18
19 #include "atom.h"
20 #include "darray.h"
21 #include "utils.h"
22
23 /* FNV-1a (http://www.isthe.com/chongo/tech/comp/fnv/). */
24 static inline uint32_t
25 hash_buf(const char *string, size_t len)
26 {
27 uint32_t hash = UINT32_C(2166136261);
28 for (size_t i = 0; i < (len + 1) / 2; i++) {
29 hash ^= (uint8_t) string[i];
30 hash *= 0x01000193;
31 hash ^= (uint8_t) string[len - 1 - i];
32 hash *= 0x01000193;
33 }
34 return hash;
35 }
36
37 /*
38 * The atom table is an insert-only linear probing hash table
39 * mapping strings to atoms. Another array maps the atoms to
40 * strings. The atom value is the position in the strings array.
41 */
42 struct atom_table {
43 xkb_atom_t *index;
44 size_t index_size;
45 darray(char *) strings;
46 };
47
48 struct atom_table *
49 atom_table_new(void)
50 {
51 struct atom_table *table = calloc(1, sizeof(*table));
52 if (!table)
53 return NULL;
54
55 darray_init(table->strings);
56 darray_append(table->strings, NULL);
57 table->index_size = 4;
58 table->index = calloc(table->index_size, sizeof(*table->index));
59
60 return table;
61 }
62
63 void
64 atom_table_free(struct atom_table *table)
65 {
66 if (!table)
67 return;
68
69 char **string;
70 darray_foreach(string, table->strings)
71 free(*string);
72 darray_free(table->strings);
73 free(table->index);
74 free(table);
75 }
76
77 darray_size_t
78 atom_table_size(struct atom_table *table)
79 {
80 return darray_size(table->strings);
81 }
82
83 const char *
84 atom_text(struct atom_table *table, xkb_atom_t atom)
85 {
86 assert(atom < darray_size(table->strings));
87 return darray_item(table->strings, atom);
88 }
89
90 xkb_atom_t
91 atom_intern(struct atom_table *table, const char *string, size_t len, bool add)
92 {
93 /* len(string) > 0.8 * index_size */
94 if (darray_size(table->strings) > (table->index_size / 5) * 4) {
95 table->index_size *= 2;
96 table->index = realloc(table->index, table->index_size * sizeof(*table->index));
97 memset(table->index, 0, table->index_size * sizeof(*table->index));
98 for (darray_size_t j = 1; j < darray_size(table->strings); j++) {
99 const char *s = darray_item(table->strings, j);
100 uint32_t hash = hash_buf(s, strlen(s));
101 for (size_t i = 0; i < table->index_size; i++) {
102 size_t index_pos = (hash + i) & (table->index_size - 1);
103 if (index_pos == 0)
104 continue;
105
106 xkb_atom_t atom = table->index[index_pos];
107 if (atom == XKB_ATOM_NONE) {
108 table->index[index_pos] = j;
109 break;
110 }
111 }
112 }
113 }
114
115 uint32_t hash = hash_buf(string, len);
116 for (size_t i = 0; i < table->index_size; i++) {
117 size_t index_pos = (hash + i) & (table->index_size - 1);
118 if (index_pos == 0)
119 continue;
120
121 xkb_atom_t existing_atom = table->index[index_pos];
122 if (existing_atom == XKB_ATOM_NONE) {
123 if (add) {
124 xkb_atom_t new_atom = darray_size(table->strings);
125 darray_append(table->strings, strndup(string, len));
126 table->index[index_pos] = new_atom;
127 return new_atom;
128 } else {
129 return XKB_ATOM_NONE;
130 }
131 }
132
133 const char *existing_value = darray_item(table->strings, existing_atom);
134 if (strncmp(existing_value, string, len) == 0 && existing_value[len] == '\0')
135 return existing_atom;
136 }
137
138 assert(!"couldn't find an empty slot during probing");
139 return XKB_ATOM_NONE;
140 }