1 /* $TOG: patcache.c /main/8 1997/06/12 11:51:59 barstow $ */
5 Copyright (c) 1991 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
29 /* $XFree86: xc/lib/font/util/patcache.c,v 3.0.4.1 1997/07/05 15:55:37 dawes Exp $ */
32 * Author: Keith Packard, MIT X Consortium
36 #include <fontstruct.h>
39 * Static sized hash table for looking up font name patterns
41 * LRU entries, reusing old entries
47 #define UNSET (NENTRIES+1)
49 typedef unsigned char EntryPtr;
51 typedef struct _FontPatternCacheEntry {
52 struct _FontPatternCacheEntry *next, **prev;
56 FontPtr pFont; /* associated font */
57 } FontPatternCacheEntryRec, *FontPatternCacheEntryPtr;
59 typedef struct _FontPatternCache {
60 FontPatternCacheEntryPtr buckets[NBUCKETS];
61 FontPatternCacheEntryRec entries[NENTRIES];
62 FontPatternCacheEntryPtr free;
63 } FontPatternCacheRec;
65 /* Create and initialize cache */
67 MakeFontPatternCache ()
69 FontPatternCachePtr cache;
71 cache = (FontPatternCachePtr) xalloc (sizeof *cache);
74 for (i = 0; i < NENTRIES; i++) {
75 cache->entries[i].patlen = 0;
76 cache->entries[i].pattern = 0;
77 cache->entries[i].pFont = 0;
79 EmptyFontPatternCache (cache);
85 FreeFontPatternCache (cache)
86 FontPatternCachePtr cache;
90 for (i = 0; i < NENTRIES; i++)
91 xfree (cache->entries[i].pattern);
95 /* compute id for string */
105 hash = (hash << 1) ^ *string++;
111 /* Empty cache (for rehash) */
113 EmptyFontPatternCache (cache)
114 FontPatternCachePtr cache;
118 for (i = 0; i < NBUCKETS; i++)
119 cache->buckets[i] = 0;
120 for (i = 0; i < NENTRIES; i++)
122 cache->entries[i].next = &cache->entries[i+1];
123 cache->entries[i].prev = 0;
124 cache->entries[i].pFont = 0;
125 xfree (cache->entries[i].pattern);
126 cache->entries[i].pattern = 0;
127 cache->entries[i].patlen = 0;
129 cache->free = &cache->entries[0];
130 cache->entries[NENTRIES - 1].next = 0;
135 CacheFontPattern (cache, pattern, patlen, pFont)
136 FontPatternCachePtr cache;
141 FontPatternCacheEntryPtr e;
145 newpat = (char *) xalloc (patlen);
151 cache->free = e->next;
159 e = &cache->entries[i];
161 e->next->prev = e->prev;
166 memcpy (newpat, pattern, patlen);
169 /* link to new hash chain */
170 e->hash = Hash (pattern, patlen);
171 i = e->hash % NBUCKETS;
172 e->next = cache->buckets[i];
174 e->next->prev = &(e->next);
175 cache->buckets[i] = e;
176 e->prev = &(cache->buckets[i]);
180 /* find matching entry */
182 FindCachedFontPattern (cache, pattern, patlen)
183 FontPatternCachePtr cache;
189 FontPatternCacheEntryPtr e;
191 hash = Hash (pattern, patlen);
193 for (e = cache->buckets[i]; e; e = e->next)
195 if (e->patlen == patlen && e->hash == hash &&
196 !memcmp (e->pattern, pattern, patlen))
205 RemoveCachedFontPattern (cache, pFont)
206 FontPatternCachePtr cache;
209 FontPatternCacheEntryPtr e;
212 for (i = 0; i < NENTRIES; i++)
214 if ((e = &cache->entries[i])->pFont == pFont)
218 e->next->prev = e->prev;
220 e->next = cache->free;