summaryrefslogtreecommitdiff
path: root/src/IntAtom.c
blob: 467286b058255f9907d76f247858080118c992c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* $Xorg: IntAtom.c,v 1.5 2001/02/09 02:03:34 xorgcvs Exp $ */
/*

Copyright 1986, 1990, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

#define NEED_REPLIES
#include "Xlibint.h"

/* XXX this table def is duplicated in GetAtomNm.c, keep them consistent! */

#define TABLESIZE 64

typedef struct _Entry {
    unsigned long sig;
    Atom atom;
} EntryRec, *Entry;

#define RESERVED ((Entry) 1)

#define EntryName(e) ((char *)(e+1))

typedef struct _XDisplayAtoms {
    Entry table[TABLESIZE];
} AtomTable;

#define HASH(sig) ((sig) & (TABLESIZE-1))
#define REHASHVAL(sig) ((((sig) % (TABLESIZE-3)) + 2) | 1)
#define REHASH(idx,rehash) ((idx + rehash) & (TABLESIZE-1))

void
_XFreeAtomTable(dpy)
    Display *dpy;
{
    register Entry *table;
    register int i;
    register Entry e;

    if (dpy->atoms) {
	table = dpy->atoms->table;
	for (i = TABLESIZE; --i >= 0; ) {
	    if ((e = *table++) && (e != RESERVED))
		Xfree((char *)e);
	}
	Xfree((char *)dpy->atoms);
    }
}

static
Atom _XInternAtom(dpy, name, onlyIfExists, psig, pidx, pn)
    Display *dpy;
    char *name;
    Bool onlyIfExists;
    unsigned long *psig;
    int *pidx;
    int *pn;
{
    register AtomTable *atoms;
    register char *s1, c, *s2;
    register unsigned long sig;
    register int idx, i;
    Entry e;
    int n, firstidx, rehash;
    xInternAtomReq *req;

    /* look in the cache first */
    if (!(atoms = dpy->atoms)) {
	dpy->atoms = atoms = (AtomTable *)Xcalloc(1, sizeof(AtomTable));
	dpy->free_funcs->atoms = _XFreeAtomTable;
    }
    sig = 0;
    for (s1 = (char *)name; (c = *s1++); )
	sig += c;
    n = s1 - (char *)name - 1;
    if (atoms) {
	firstidx = idx = HASH(sig);
	while ((e = atoms->table[idx])) {
	    if (e != RESERVED && e->sig == sig) {
	    	for (i = n, s1 = (char *)name, s2 = EntryName(e); --i >= 0; ) {
		    if (*s1++ != *s2++)
		    	goto nomatch;
	    	}
	    	if (!*s2)
		    return e->atom;
	    }
nomatch:    if (idx == firstidx)
		rehash = REHASHVAL(sig);
	    idx = REHASH(idx, rehash);
	    if (idx == firstidx)
		break;
	}
    }
    *psig = sig;
    *pidx = idx;
    if (atoms && !atoms->table[idx])
	atoms->table[idx] = RESERVED; /* reserve slot */
    *pn = n;
    /* not found, go to the server */
    GetReq(InternAtom, req);
    req->nbytes = n;
    req->onlyIfExists = onlyIfExists;
    req->length += (n+3)>>2;
    Data(dpy, name, n);
    return None;
}

void
_XUpdateAtomCache(dpy, name, atom, sig, idx, n)
    Display *dpy;
    char *name;
    Atom atom;
    unsigned long sig;
    int idx;
    int n;
{
    Entry e, oe;
    register char *s1;
    register char c;
    int firstidx, rehash;

    if (!dpy->atoms) {
	if (idx < 0) {
	    dpy->atoms = (AtomTable *)Xcalloc(1, sizeof(AtomTable));
	    dpy->free_funcs->atoms = _XFreeAtomTable;
	}
	if (!dpy->atoms)
	    return;
    }
    if (!sig) {
	for (s1 = (char *)name; (c = *s1++); )
	    sig += c;
	n = s1 - (char *)name - 1;
	if (idx < 0) {
	    firstidx = idx = HASH(sig);
	    if (dpy->atoms->table[idx]) {
		rehash = REHASHVAL(sig);
		do
		    idx = REHASH(idx, rehash);
		while (idx != firstidx && dpy->atoms->table[idx]);
	    }
	}
    }
    e = (Entry)Xmalloc(sizeof(EntryRec) + n + 1);
    if (e) {
	e->sig = sig;
	e->atom = atom;
	strcpy(EntryName(e), name);
	if ((oe = dpy->atoms->table[idx]) && (oe != RESERVED))
	    Xfree((char *)oe);
	dpy->atoms->table[idx] = e;
    }
}

#if NeedFunctionPrototypes
Atom XInternAtom (
    Display *dpy,
    _Xconst char *name,
    Bool onlyIfExists)
#else
Atom XInternAtom (dpy, name, onlyIfExists)
    Display *dpy;
    char *name;
    Bool onlyIfExists;
#endif
{
    Atom atom;
    unsigned long sig;
    int idx, n;
    xInternAtomReply rep;

    if (!name)
	name = "";
    LockDisplay(dpy);
    if ((atom = _XInternAtom(dpy, name, onlyIfExists, &sig, &idx, &n))) {
	UnlockDisplay(dpy);
	return atom;
    }
    if (dpy->atoms && dpy->atoms->table[idx] == RESERVED)
	dpy->atoms->table[idx] = NULL; /* unreserve slot */
    if (_XReply (dpy, (xReply *)&rep, 0, xTrue)) {
	if ((atom = rep.atom))
	    _XUpdateAtomCache(dpy, name, atom, sig, idx, n);
    }
    UnlockDisplay(dpy);
    SyncHandle();
    return (rep.atom);
}

typedef struct {
    unsigned long start_seq;
    unsigned long stop_seq;
    char **names;
    Atom *atoms;
    int count;
    Status status;
} _XIntAtomState;

static
Bool _XIntAtomHandler(dpy, rep, buf, len, data)
    register Display *dpy;
    register xReply *rep;
    char *buf;
    int len;
    XPointer data;
{
    register _XIntAtomState *state;
    register int i, idx;
    xInternAtomReply replbuf;
    register xInternAtomReply *repl;

    state = (_XIntAtomState *)data;
    if (dpy->last_request_read < state->start_seq ||
	dpy->last_request_read > state->stop_seq)
	return False;
    for (i = 0; i < state->count; i++) {
	if (state->atoms[i] & 0x80000000) {
	    idx = ~state->atoms[i];
	    state->atoms[i] = None;
	    break;
	}
    }
    if (i >= state->count)
	return False;
    if (rep->generic.type == X_Error) {
	state->status = 0;
	return False;
    }
    repl = (xInternAtomReply *)
	_XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
			(SIZEOF(xInternAtomReply) - SIZEOF(xReply)) >> 2,
			True);
    if ((state->atoms[i] = repl->atom))
	_XUpdateAtomCache(dpy, state->names[i], (Atom) repl->atom,
			  (unsigned long)0, idx, 0);
    return True;
}

Status
XInternAtoms (dpy, names, count, onlyIfExists, atoms_return)
    Display *dpy;
    char **names;
    int count;
    Bool onlyIfExists;
    Atom *atoms_return;
{
    int i, idx, n, tidx;
    unsigned long sig;
    _XAsyncHandler async;
    _XIntAtomState async_state;
    int missed = -1;
    xInternAtomReply rep;

    LockDisplay(dpy);
    async_state.start_seq = dpy->request + 1;
    async_state.atoms = atoms_return;
    async_state.names = names;
    async_state.count = count - 1;
    async_state.status = 1;
    async.next = dpy->async_handlers;
    async.handler = _XIntAtomHandler;
    async.data = (XPointer)&async_state;
    dpy->async_handlers = &async;
    for (i = 0; i < count; i++) {
	if (!(atoms_return[i] = _XInternAtom(dpy, names[i], onlyIfExists,
					     &sig, &idx, &n))) {
	    missed = i;
	    atoms_return[i] = ~((Atom)idx);
	    async_state.stop_seq = dpy->request;
	}
    }
    if (missed >= 0) {
        if (dpy->atoms) {
	    /* unreserve anything we just reserved */
	    for (i = 0; i < count; i++) {
		if (atoms_return[i] & 0x80000000) {
		    tidx = ~atoms_return[i];
		    if (dpy->atoms->table[tidx] == RESERVED)
			dpy->atoms->table[tidx] = NULL;
		}
	    }
        }
	if (_XReply (dpy, (xReply *)&rep, 0, xTrue)) {
	    if ((atoms_return[missed] = rep.atom))
		_XUpdateAtomCache(dpy, names[missed], (Atom) rep.atom, 
				  sig, idx, n);
	} else {
	    atoms_return[missed] = None;
	    async_state.status = 0;
	}
    }
    DeqAsyncHandler(dpy, &async);
    UnlockDisplay(dpy);
    if (missed >= 0)
	SyncHandle();
    return async_state.status;
}