1*2b949d04SAndroid Build Coastguard Worker /***********************************************************
2*2b949d04SAndroid Build Coastguard Worker * Copyright 1987, 1998 The Open Group
3*2b949d04SAndroid Build Coastguard Worker *
4*2b949d04SAndroid Build Coastguard Worker * Permission to use, copy, modify, distribute, and sell this software and its
5*2b949d04SAndroid Build Coastguard Worker * documentation for any purpose is hereby granted without fee, provided that
6*2b949d04SAndroid Build Coastguard Worker * the above copyright notice appear in all copies and that both that
7*2b949d04SAndroid Build Coastguard Worker * copyright notice and this permission notice appear in supporting
8*2b949d04SAndroid Build Coastguard Worker * documentation.
9*2b949d04SAndroid Build Coastguard Worker *
10*2b949d04SAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be included in
11*2b949d04SAndroid Build Coastguard Worker * all copies or substantial portions of the Software.
12*2b949d04SAndroid Build Coastguard Worker *
13*2b949d04SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*2b949d04SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*2b949d04SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*2b949d04SAndroid Build Coastguard Worker * OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17*2b949d04SAndroid Build Coastguard Worker * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18*2b949d04SAndroid Build Coastguard Worker * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19*2b949d04SAndroid Build Coastguard Worker *
20*2b949d04SAndroid Build Coastguard Worker * Except as contained in this notice, the name of The Open Group shall not be
21*2b949d04SAndroid Build Coastguard Worker * used in advertising or otherwise to promote the sale, use or other dealings
22*2b949d04SAndroid Build Coastguard Worker * in this Software without prior written authorization from The Open Group.
23*2b949d04SAndroid Build Coastguard Worker *
24*2b949d04SAndroid Build Coastguard Worker *
25*2b949d04SAndroid Build Coastguard Worker * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
26*2b949d04SAndroid Build Coastguard Worker *
27*2b949d04SAndroid Build Coastguard Worker * All Rights Reserved
28*2b949d04SAndroid Build Coastguard Worker *
29*2b949d04SAndroid Build Coastguard Worker * Permission to use, copy, modify, and distribute this software and its
30*2b949d04SAndroid Build Coastguard Worker * documentation for any purpose and without fee is hereby granted,
31*2b949d04SAndroid Build Coastguard Worker * provided that the above copyright notice appear in all copies and that
32*2b949d04SAndroid Build Coastguard Worker * both that copyright notice and this permission notice appear in
33*2b949d04SAndroid Build Coastguard Worker * supporting documentation, and that the name of Digital not be
34*2b949d04SAndroid Build Coastguard Worker * used in advertising or publicity pertaining to distribution of the
35*2b949d04SAndroid Build Coastguard Worker * software without specific, written prior permission.
36*2b949d04SAndroid Build Coastguard Worker *
37*2b949d04SAndroid Build Coastguard Worker * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38*2b949d04SAndroid Build Coastguard Worker * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39*2b949d04SAndroid Build Coastguard Worker * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40*2b949d04SAndroid Build Coastguard Worker * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41*2b949d04SAndroid Build Coastguard Worker * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42*2b949d04SAndroid Build Coastguard Worker * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43*2b949d04SAndroid Build Coastguard Worker * SOFTWARE.
44*2b949d04SAndroid Build Coastguard Worker *
45*2b949d04SAndroid Build Coastguard Worker ******************************************************************/
46*2b949d04SAndroid Build Coastguard Worker
47*2b949d04SAndroid Build Coastguard Worker /************************************************************
48*2b949d04SAndroid Build Coastguard Worker * Copyright 1994 by Silicon Graphics Computer Systems, Inc.
49*2b949d04SAndroid Build Coastguard Worker *
50*2b949d04SAndroid Build Coastguard Worker * Permission to use, copy, modify, and distribute this
51*2b949d04SAndroid Build Coastguard Worker * software and its documentation for any purpose and without
52*2b949d04SAndroid Build Coastguard Worker * fee is hereby granted, provided that the above copyright
53*2b949d04SAndroid Build Coastguard Worker * notice appear in all copies and that both that copyright
54*2b949d04SAndroid Build Coastguard Worker * notice and this permission notice appear in supporting
55*2b949d04SAndroid Build Coastguard Worker * documentation, and that the name of Silicon Graphics not be
56*2b949d04SAndroid Build Coastguard Worker * used in advertising or publicity pertaining to distribution
57*2b949d04SAndroid Build Coastguard Worker * of the software without specific prior written permission.
58*2b949d04SAndroid Build Coastguard Worker * Silicon Graphics makes no representation about the suitability
59*2b949d04SAndroid Build Coastguard Worker * of this software for any purpose. It is provided "as is"
60*2b949d04SAndroid Build Coastguard Worker * without any express or implied warranty.
61*2b949d04SAndroid Build Coastguard Worker *
62*2b949d04SAndroid Build Coastguard Worker * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
63*2b949d04SAndroid Build Coastguard Worker * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64*2b949d04SAndroid Build Coastguard Worker * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
65*2b949d04SAndroid Build Coastguard Worker * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
66*2b949d04SAndroid Build Coastguard Worker * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
67*2b949d04SAndroid Build Coastguard Worker * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
68*2b949d04SAndroid Build Coastguard Worker * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
69*2b949d04SAndroid Build Coastguard Worker * THE USE OR PERFORMANCE OF THIS SOFTWARE.
70*2b949d04SAndroid Build Coastguard Worker *
71*2b949d04SAndroid Build Coastguard Worker ********************************************************/
72*2b949d04SAndroid Build Coastguard Worker
73*2b949d04SAndroid Build Coastguard Worker #include "config.h"
74*2b949d04SAndroid Build Coastguard Worker
75*2b949d04SAndroid Build Coastguard Worker #include <assert.h>
76*2b949d04SAndroid Build Coastguard Worker #include <inttypes.h>
77*2b949d04SAndroid Build Coastguard Worker #include <stdbool.h>
78*2b949d04SAndroid Build Coastguard Worker #include <string.h>
79*2b949d04SAndroid Build Coastguard Worker
80*2b949d04SAndroid Build Coastguard Worker #include "atom.h"
81*2b949d04SAndroid Build Coastguard Worker #include "darray.h"
82*2b949d04SAndroid Build Coastguard Worker #include "utils.h"
83*2b949d04SAndroid Build Coastguard Worker
84*2b949d04SAndroid Build Coastguard Worker /* FNV-1a (http://www.isthe.com/chongo/tech/comp/fnv/). */
85*2b949d04SAndroid Build Coastguard Worker static inline uint32_t
hash_buf(const char * string,size_t len)86*2b949d04SAndroid Build Coastguard Worker hash_buf(const char *string, size_t len)
87*2b949d04SAndroid Build Coastguard Worker {
88*2b949d04SAndroid Build Coastguard Worker uint32_t hash = 2166136261u;
89*2b949d04SAndroid Build Coastguard Worker for (size_t i = 0; i < (len + 1) / 2; i++) {
90*2b949d04SAndroid Build Coastguard Worker hash ^= (uint8_t) string[i];
91*2b949d04SAndroid Build Coastguard Worker hash *= 0x01000193;
92*2b949d04SAndroid Build Coastguard Worker hash ^= (uint8_t) string[len - 1 - i];
93*2b949d04SAndroid Build Coastguard Worker hash *= 0x01000193;
94*2b949d04SAndroid Build Coastguard Worker }
95*2b949d04SAndroid Build Coastguard Worker return hash;
96*2b949d04SAndroid Build Coastguard Worker }
97*2b949d04SAndroid Build Coastguard Worker
98*2b949d04SAndroid Build Coastguard Worker /*
99*2b949d04SAndroid Build Coastguard Worker * The atom table is an insert-only linear probing hash table
100*2b949d04SAndroid Build Coastguard Worker * mapping strings to atoms. Another array maps the atoms to
101*2b949d04SAndroid Build Coastguard Worker * strings. The atom value is the position in the strings array.
102*2b949d04SAndroid Build Coastguard Worker */
103*2b949d04SAndroid Build Coastguard Worker struct atom_table {
104*2b949d04SAndroid Build Coastguard Worker xkb_atom_t *index;
105*2b949d04SAndroid Build Coastguard Worker size_t index_size;
106*2b949d04SAndroid Build Coastguard Worker darray(char *) strings;
107*2b949d04SAndroid Build Coastguard Worker };
108*2b949d04SAndroid Build Coastguard Worker
109*2b949d04SAndroid Build Coastguard Worker struct atom_table *
atom_table_new(void)110*2b949d04SAndroid Build Coastguard Worker atom_table_new(void)
111*2b949d04SAndroid Build Coastguard Worker {
112*2b949d04SAndroid Build Coastguard Worker struct atom_table *table = calloc(1, sizeof(*table));
113*2b949d04SAndroid Build Coastguard Worker if (!table)
114*2b949d04SAndroid Build Coastguard Worker return NULL;
115*2b949d04SAndroid Build Coastguard Worker
116*2b949d04SAndroid Build Coastguard Worker darray_init(table->strings);
117*2b949d04SAndroid Build Coastguard Worker darray_append(table->strings, NULL);
118*2b949d04SAndroid Build Coastguard Worker table->index_size = 4;
119*2b949d04SAndroid Build Coastguard Worker table->index = calloc(table->index_size, sizeof(*table->index));
120*2b949d04SAndroid Build Coastguard Worker
121*2b949d04SAndroid Build Coastguard Worker return table;
122*2b949d04SAndroid Build Coastguard Worker }
123*2b949d04SAndroid Build Coastguard Worker
124*2b949d04SAndroid Build Coastguard Worker void
atom_table_free(struct atom_table * table)125*2b949d04SAndroid Build Coastguard Worker atom_table_free(struct atom_table *table)
126*2b949d04SAndroid Build Coastguard Worker {
127*2b949d04SAndroid Build Coastguard Worker if (!table)
128*2b949d04SAndroid Build Coastguard Worker return;
129*2b949d04SAndroid Build Coastguard Worker
130*2b949d04SAndroid Build Coastguard Worker char **string;
131*2b949d04SAndroid Build Coastguard Worker darray_foreach(string, table->strings)
132*2b949d04SAndroid Build Coastguard Worker free(*string);
133*2b949d04SAndroid Build Coastguard Worker darray_free(table->strings);
134*2b949d04SAndroid Build Coastguard Worker free(table->index);
135*2b949d04SAndroid Build Coastguard Worker free(table);
136*2b949d04SAndroid Build Coastguard Worker }
137*2b949d04SAndroid Build Coastguard Worker
138*2b949d04SAndroid Build Coastguard Worker const char *
atom_text(struct atom_table * table,xkb_atom_t atom)139*2b949d04SAndroid Build Coastguard Worker atom_text(struct atom_table *table, xkb_atom_t atom)
140*2b949d04SAndroid Build Coastguard Worker {
141*2b949d04SAndroid Build Coastguard Worker assert(atom < darray_size(table->strings));
142*2b949d04SAndroid Build Coastguard Worker return darray_item(table->strings, atom);
143*2b949d04SAndroid Build Coastguard Worker }
144*2b949d04SAndroid Build Coastguard Worker
145*2b949d04SAndroid Build Coastguard Worker xkb_atom_t
atom_intern(struct atom_table * table,const char * string,size_t len,bool add)146*2b949d04SAndroid Build Coastguard Worker atom_intern(struct atom_table *table, const char *string, size_t len, bool add)
147*2b949d04SAndroid Build Coastguard Worker {
148*2b949d04SAndroid Build Coastguard Worker if (darray_size(table->strings) > 0.80 * table->index_size) {
149*2b949d04SAndroid Build Coastguard Worker table->index_size *= 2;
150*2b949d04SAndroid Build Coastguard Worker table->index = realloc(table->index, table->index_size * sizeof(*table->index));
151*2b949d04SAndroid Build Coastguard Worker memset(table->index, 0, table->index_size * sizeof(*table->index));
152*2b949d04SAndroid Build Coastguard Worker for (size_t j = 1; j < darray_size(table->strings); j++) {
153*2b949d04SAndroid Build Coastguard Worker const char *s = darray_item(table->strings, j);
154*2b949d04SAndroid Build Coastguard Worker uint32_t hash = hash_buf(s, strlen(s));
155*2b949d04SAndroid Build Coastguard Worker for (size_t i = 0; i < table->index_size; i++) {
156*2b949d04SAndroid Build Coastguard Worker size_t index_pos = (hash + i) & (table->index_size - 1);
157*2b949d04SAndroid Build Coastguard Worker if (index_pos == 0)
158*2b949d04SAndroid Build Coastguard Worker continue;
159*2b949d04SAndroid Build Coastguard Worker
160*2b949d04SAndroid Build Coastguard Worker xkb_atom_t atom = table->index[index_pos];
161*2b949d04SAndroid Build Coastguard Worker if (atom == XKB_ATOM_NONE) {
162*2b949d04SAndroid Build Coastguard Worker table->index[index_pos] = j;
163*2b949d04SAndroid Build Coastguard Worker break;
164*2b949d04SAndroid Build Coastguard Worker }
165*2b949d04SAndroid Build Coastguard Worker }
166*2b949d04SAndroid Build Coastguard Worker }
167*2b949d04SAndroid Build Coastguard Worker }
168*2b949d04SAndroid Build Coastguard Worker
169*2b949d04SAndroid Build Coastguard Worker uint32_t hash = hash_buf(string, len);
170*2b949d04SAndroid Build Coastguard Worker for (size_t i = 0; i < table->index_size; i++) {
171*2b949d04SAndroid Build Coastguard Worker size_t index_pos = (hash + i) & (table->index_size - 1);
172*2b949d04SAndroid Build Coastguard Worker if (index_pos == 0)
173*2b949d04SAndroid Build Coastguard Worker continue;
174*2b949d04SAndroid Build Coastguard Worker
175*2b949d04SAndroid Build Coastguard Worker xkb_atom_t existing_atom = table->index[index_pos];
176*2b949d04SAndroid Build Coastguard Worker if (existing_atom == XKB_ATOM_NONE) {
177*2b949d04SAndroid Build Coastguard Worker if (add) {
178*2b949d04SAndroid Build Coastguard Worker xkb_atom_t new_atom = darray_size(table->strings);
179*2b949d04SAndroid Build Coastguard Worker darray_append(table->strings, strndup(string, len));
180*2b949d04SAndroid Build Coastguard Worker table->index[index_pos] = new_atom;
181*2b949d04SAndroid Build Coastguard Worker return new_atom;
182*2b949d04SAndroid Build Coastguard Worker } else {
183*2b949d04SAndroid Build Coastguard Worker return XKB_ATOM_NONE;
184*2b949d04SAndroid Build Coastguard Worker }
185*2b949d04SAndroid Build Coastguard Worker }
186*2b949d04SAndroid Build Coastguard Worker
187*2b949d04SAndroid Build Coastguard Worker const char *existing_value = darray_item(table->strings, existing_atom);
188*2b949d04SAndroid Build Coastguard Worker if (strncmp(existing_value, string, len) == 0 && existing_value[len] == '\0')
189*2b949d04SAndroid Build Coastguard Worker return existing_atom;
190*2b949d04SAndroid Build Coastguard Worker }
191*2b949d04SAndroid Build Coastguard Worker
192*2b949d04SAndroid Build Coastguard Worker assert(!"couldn't find an empty slot during probing");
193*2b949d04SAndroid Build Coastguard Worker return XKB_ATOM_NONE;
194*2b949d04SAndroid Build Coastguard Worker }
195