3 Copyright 1990, 1994, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
28 * Author: Keith Packard, MIT X Consortium
31 /* lame atom replacement routines for font applications */
36 #include <X11/fonts/fontmisc.h>
39 typedef struct _AtomList {
44 } AtomListRec, *AtomListPtr;
46 static AtomListPtr *hashTable;
48 static int hashSize, hashUsed;
52 static AtomListPtr *reverseMap;
53 static int reverseMapSize;
57 Hash(const char *string, int len)
63 h = (h << 3) ^ *string++;
70 ResizeHashTable (void)
74 AtomListPtr *newHashTable;
83 newHashSize = hashSize * 2;
84 newHashTable = calloc (newHashSize, sizeof (AtomListPtr));
86 fprintf(stderr, "ResizeHashTable(): Error: Couldn't allocate"
87 " newHashTable (%ld)\n",
88 newHashSize * (unsigned long)sizeof (AtomListPtr));
91 newHashMask = newHashSize - 1;
92 newRehash = (newHashMask - 2);
93 for (i = 0; i < hashSize; i++)
97 h = (hashTable[i]->hash) & newHashMask;
100 r = hashTable[i]->hash % newRehash | 1;
103 if (h >= newHashSize)
105 } while (newHashTable[h]);
107 newHashTable[h] = hashTable[i];
111 hashTable = newHashTable;
112 hashSize = newHashSize;
113 hashMask = newHashMask;
119 ResizeReverseMap (void)
122 if (reverseMapSize == 0)
123 reverseMapSize = 1000;
126 reverseMap = realloc (reverseMap, reverseMapSize * sizeof (AtomListPtr));
128 fprintf(stderr, "ResizeReverseMap(): Error: Couldn't reallocate"
129 " reverseMap (%ld)\n",
130 reverseMapSize * (unsigned long)sizeof(AtomListPtr));
137 NameEqual (const char *a, const char *b, int l)
146 #pragma weak MakeAtom
150 MakeAtom(const char *string, unsigned len, int makeit)
157 hash = Hash (string, len);
163 if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
164 NameEqual (hashTable[h]->name, string, len))
166 return hashTable[h]->atom;
168 r = (hash % rehash) | 1;
176 if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
177 NameEqual (hashTable[h]->name, string, len))
179 return hashTable[h]->atom;
186 a = malloc (sizeof (AtomListRec) + len + 1);
188 fprintf(stderr, "MakeAtom(): Error: Couldn't allocate AtomListRec"
189 " (%ld)\n", (unsigned long)sizeof (AtomListRec) + len + 1);
192 a->name = (char *) (a + 1);
194 strncpy (a->name, string, len);
196 a->atom = ++lastAtom;
198 if (hashUsed >= hashSize / 2)
204 r = (hash % rehash) | 1;
209 } while (hashTable[h]);
214 if (reverseMapSize <= a->atom) {
215 if (!ResizeReverseMap())
218 reverseMap[a->atom] = a;
223 #pragma weak ValidAtom
229 return (atom != None) && (atom <= lastAtom);
233 #pragma weak NameForAtom
237 NameForAtom(Atom atom)
239 if (atom != None && atom <= lastAtom)
240 return reverseMap[atom]->name;