3 Copyright 1991, 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
34 #include <X11/fonts/fontmisc.h>
35 #include <X11/fonts/fontstruct.h>
38 * Static sized hash table for looking up font name patterns
40 * LRU entries, reusing old entries
46 #define UNSET (NENTRIES+1)
48 typedef unsigned char EntryPtr;
50 typedef struct _FontPatternCacheEntry {
51 struct _FontPatternCacheEntry *next, **prev;
55 FontPtr pFont; /* associated font */
56 } FontPatternCacheEntryRec, *FontPatternCacheEntryPtr;
58 typedef struct _FontPatternCache {
59 FontPatternCacheEntryPtr buckets[NBUCKETS];
60 FontPatternCacheEntryRec entries[NENTRIES];
61 FontPatternCacheEntryPtr free;
62 } FontPatternCacheRec;
64 /* Empty cache (for rehash) */
66 EmptyFontPatternCache (FontPatternCachePtr cache)
70 for (i = 0; i < NBUCKETS; i++)
71 cache->buckets[i] = 0;
72 for (i = 0; i < NENTRIES; i++)
74 cache->entries[i].next = &cache->entries[i+1];
75 cache->entries[i].prev = 0;
76 cache->entries[i].pFont = 0;
77 free (cache->entries[i].pattern);
78 cache->entries[i].pattern = 0;
79 cache->entries[i].patlen = 0;
81 cache->free = &cache->entries[0];
82 cache->entries[NENTRIES - 1].next = 0;
85 /* Create and initialize cache */
87 MakeFontPatternCache (void)
89 FontPatternCachePtr cache;
91 cache = malloc (sizeof *cache);
94 for (i = 0; i < NENTRIES; i++) {
95 cache->entries[i].patlen = 0;
96 cache->entries[i].pattern = 0;
97 cache->entries[i].pFont = 0;
99 EmptyFontPatternCache (cache);
105 FreeFontPatternCache (FontPatternCachePtr cache)
109 for (i = 0; i < NENTRIES; i++)
110 free (cache->entries[i].pattern);
114 /* compute id for string */
116 Hash (const char *string, int len)
122 hash = (hash << 1) ^ *string++;
130 CacheFontPattern (FontPatternCachePtr cache,
135 FontPatternCacheEntryPtr e;
139 newpat = malloc (patlen);
145 cache->free = e->next;
153 e = &cache->entries[i];
155 e->next->prev = e->prev;
160 memcpy (newpat, pattern, patlen);
163 /* link to new hash chain */
164 e->hash = Hash (pattern, patlen);
165 i = e->hash % NBUCKETS;
166 e->next = cache->buckets[i];
168 e->next->prev = &(e->next);
169 cache->buckets[i] = e;
170 e->prev = &(cache->buckets[i]);
174 /* find matching entry */
176 FindCachedFontPattern (FontPatternCachePtr cache,
182 FontPatternCacheEntryPtr e;
184 hash = Hash (pattern, patlen);
186 for (e = cache->buckets[i]; e; e = e->next)
188 if (e->patlen == patlen && e->hash == hash &&
189 !memcmp (e->pattern, pattern, patlen))
198 RemoveCachedFontPattern (FontPatternCachePtr cache,
201 FontPatternCacheEntryPtr e;
204 for (i = 0; i < NENTRIES; i++)
206 if ((e = &cache->entries[i])->pFont == pFont)
210 e->next->prev = e->prev;
212 e->next = cache->free;