patcache.c revision 7f7f5e4e
123a0898aSmrg/* $Xorg: patcache.c,v 1.4 2001/02/09 02:04:04 xorgcvs Exp $ */ 223a0898aSmrg 323a0898aSmrg/* 423a0898aSmrg 523a0898aSmrgCopyright 1991, 1998 The Open Group 623a0898aSmrg 723a0898aSmrgPermission to use, copy, modify, distribute, and sell this software and its 823a0898aSmrgdocumentation for any purpose is hereby granted without fee, provided that 923a0898aSmrgthe above copyright notice appear in all copies and that both that 1023a0898aSmrgcopyright notice and this permission notice appear in supporting 1123a0898aSmrgdocumentation. 1223a0898aSmrg 1323a0898aSmrgThe above copyright notice and this permission notice shall be included in 1423a0898aSmrgall copies or substantial portions of the Software. 1523a0898aSmrg 1623a0898aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1723a0898aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1823a0898aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1923a0898aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2023a0898aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2123a0898aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2223a0898aSmrg 2323a0898aSmrgExcept as contained in this notice, the name of The Open Group shall not be 2423a0898aSmrgused in advertising or otherwise to promote the sale, use or other dealings 2523a0898aSmrgin this Software without prior written authorization from The Open Group. 2623a0898aSmrg 2723a0898aSmrg*/ 2823a0898aSmrg/* $XFree86: xc/lib/font/util/patcache.c,v 3.4 2001/01/17 19:43:33 dawes Exp $ */ 2923a0898aSmrg 3023a0898aSmrg/* 3123a0898aSmrg * Author: Keith Packard, MIT X Consortium 3223a0898aSmrg */ 3323a0898aSmrg 3423a0898aSmrg#ifdef HAVE_CONFIG_H 3523a0898aSmrg#include <config.h> 3623a0898aSmrg#endif 3723a0898aSmrg#include <X11/fonts/fontmisc.h> 3823a0898aSmrg#include <X11/fonts/fontstruct.h> 3923a0898aSmrg 4023a0898aSmrg/* 4123a0898aSmrg * Static sized hash table for looking up font name patterns 4223a0898aSmrg * 4323a0898aSmrg * LRU entries, reusing old entries 4423a0898aSmrg */ 4523a0898aSmrg 4623a0898aSmrg#define NBUCKETS 16 4723a0898aSmrg#define NENTRIES 64 4823a0898aSmrg 4923a0898aSmrg#define UNSET (NENTRIES+1) 5023a0898aSmrg 5123a0898aSmrgtypedef unsigned char EntryPtr; 5223a0898aSmrg 5323a0898aSmrgtypedef struct _FontPatternCacheEntry { 5423a0898aSmrg struct _FontPatternCacheEntry *next, **prev; 5523a0898aSmrg short patlen; 5623a0898aSmrg char *pattern; 5723a0898aSmrg int hash; 5823a0898aSmrg FontPtr pFont; /* associated font */ 5923a0898aSmrg} FontPatternCacheEntryRec, *FontPatternCacheEntryPtr; 6023a0898aSmrg 6123a0898aSmrgtypedef struct _FontPatternCache { 6223a0898aSmrg FontPatternCacheEntryPtr buckets[NBUCKETS]; 6323a0898aSmrg FontPatternCacheEntryRec entries[NENTRIES]; 6423a0898aSmrg FontPatternCacheEntryPtr free; 6523a0898aSmrg} FontPatternCacheRec; 6623a0898aSmrg 6723a0898aSmrg/* Empty cache (for rehash) */ 6823a0898aSmrgvoid 6923a0898aSmrgEmptyFontPatternCache (FontPatternCachePtr cache) 7023a0898aSmrg{ 7123a0898aSmrg int i; 7223a0898aSmrg 7323a0898aSmrg for (i = 0; i < NBUCKETS; i++) 7423a0898aSmrg cache->buckets[i] = 0; 7523a0898aSmrg for (i = 0; i < NENTRIES; i++) 7623a0898aSmrg { 7723a0898aSmrg cache->entries[i].next = &cache->entries[i+1]; 7823a0898aSmrg cache->entries[i].prev = 0; 7923a0898aSmrg cache->entries[i].pFont = 0; 807f7f5e4eSmrg free (cache->entries[i].pattern); 8123a0898aSmrg cache->entries[i].pattern = 0; 8223a0898aSmrg cache->entries[i].patlen = 0; 8323a0898aSmrg } 8423a0898aSmrg cache->free = &cache->entries[0]; 8523a0898aSmrg cache->entries[NENTRIES - 1].next = 0; 8623a0898aSmrg} 8723a0898aSmrg 8823a0898aSmrg/* Create and initialize cache */ 8923a0898aSmrgFontPatternCachePtr 9023a0898aSmrgMakeFontPatternCache (void) 9123a0898aSmrg{ 9223a0898aSmrg FontPatternCachePtr cache; 9323a0898aSmrg int i; 947f7f5e4eSmrg cache = malloc (sizeof *cache); 9523a0898aSmrg if (!cache) 9623a0898aSmrg return 0; 9723a0898aSmrg for (i = 0; i < NENTRIES; i++) { 9823a0898aSmrg cache->entries[i].patlen = 0; 9923a0898aSmrg cache->entries[i].pattern = 0; 10023a0898aSmrg cache->entries[i].pFont = 0; 10123a0898aSmrg } 10223a0898aSmrg EmptyFontPatternCache (cache); 10323a0898aSmrg return cache; 10423a0898aSmrg} 10523a0898aSmrg 10623a0898aSmrg/* toss cache */ 10723a0898aSmrgvoid 10823a0898aSmrgFreeFontPatternCache (FontPatternCachePtr cache) 10923a0898aSmrg{ 11023a0898aSmrg int i; 11123a0898aSmrg 11223a0898aSmrg for (i = 0; i < NENTRIES; i++) 1137f7f5e4eSmrg free (cache->entries[i].pattern); 1147f7f5e4eSmrg free (cache); 11523a0898aSmrg} 11623a0898aSmrg 11723a0898aSmrg/* compute id for string */ 11823a0898aSmrgstatic int 11923a0898aSmrgHash (const char *string, int len) 12023a0898aSmrg{ 12123a0898aSmrg int hash; 12223a0898aSmrg 12323a0898aSmrg hash = 0; 12423a0898aSmrg while (len--) 12523a0898aSmrg hash = (hash << 1) ^ *string++; 12623a0898aSmrg if (hash < 0) 12723a0898aSmrg hash = -hash; 12823a0898aSmrg return hash; 12923a0898aSmrg} 13023a0898aSmrg 13123a0898aSmrg/* add entry */ 13223a0898aSmrgvoid 13323a0898aSmrgCacheFontPattern (FontPatternCachePtr cache, 13423a0898aSmrg char *pattern, 13523a0898aSmrg int patlen, 13623a0898aSmrg FontPtr pFont) 13723a0898aSmrg{ 13823a0898aSmrg FontPatternCacheEntryPtr e; 13923a0898aSmrg char *newpat; 14023a0898aSmrg int i; 14123a0898aSmrg 1427f7f5e4eSmrg newpat = malloc (patlen); 14323a0898aSmrg if (!newpat) 14423a0898aSmrg return; 14523a0898aSmrg if (cache->free) 14623a0898aSmrg { 14723a0898aSmrg e = cache->free; 14823a0898aSmrg cache->free = e->next; 14923a0898aSmrg } 15023a0898aSmrg else 15123a0898aSmrg { 15223a0898aSmrg i = rand (); 15323a0898aSmrg if (i < 0) 15423a0898aSmrg i = -i; 15523a0898aSmrg i %= NENTRIES; 15623a0898aSmrg e = &cache->entries[i]; 15723a0898aSmrg if (e->next) 15823a0898aSmrg e->next->prev = e->prev; 15923a0898aSmrg *e->prev = e->next; 1607f7f5e4eSmrg free (e->pattern); 16123a0898aSmrg } 16223a0898aSmrg /* set pattern */ 16323a0898aSmrg memcpy (newpat, pattern, patlen); 16423a0898aSmrg e->pattern = newpat; 16523a0898aSmrg e->patlen = patlen; 16623a0898aSmrg /* link to new hash chain */ 16723a0898aSmrg e->hash = Hash (pattern, patlen); 16823a0898aSmrg i = e->hash % NBUCKETS; 16923a0898aSmrg e->next = cache->buckets[i]; 17023a0898aSmrg if (e->next) 17123a0898aSmrg e->next->prev = &(e->next); 17223a0898aSmrg cache->buckets[i] = e; 17323a0898aSmrg e->prev = &(cache->buckets[i]); 17423a0898aSmrg e->pFont = pFont; 17523a0898aSmrg} 17623a0898aSmrg 17723a0898aSmrg/* find matching entry */ 17823a0898aSmrgFontPtr 17923a0898aSmrgFindCachedFontPattern (FontPatternCachePtr cache, 18023a0898aSmrg char *pattern, 18123a0898aSmrg int patlen) 18223a0898aSmrg{ 18323a0898aSmrg int hash; 18423a0898aSmrg int i; 18523a0898aSmrg FontPatternCacheEntryPtr e; 18623a0898aSmrg 18723a0898aSmrg hash = Hash (pattern, patlen); 18823a0898aSmrg i = hash % NBUCKETS; 18923a0898aSmrg for (e = cache->buckets[i]; e; e = e->next) 19023a0898aSmrg { 19123a0898aSmrg if (e->patlen == patlen && e->hash == hash && 19223a0898aSmrg !memcmp (e->pattern, pattern, patlen)) 19323a0898aSmrg { 19423a0898aSmrg return e->pFont; 19523a0898aSmrg } 19623a0898aSmrg } 19723a0898aSmrg return 0; 19823a0898aSmrg} 19923a0898aSmrg 20023a0898aSmrgvoid 20123a0898aSmrgRemoveCachedFontPattern (FontPatternCachePtr cache, 20223a0898aSmrg FontPtr pFont) 20323a0898aSmrg{ 20423a0898aSmrg FontPatternCacheEntryPtr e; 20523a0898aSmrg int i; 20623a0898aSmrg 20723a0898aSmrg for (i = 0; i < NENTRIES; i++) 20823a0898aSmrg { 20923a0898aSmrg if ((e = &cache->entries[i])->pFont == pFont) 21023a0898aSmrg { 21123a0898aSmrg e->pFont = 0; 21223a0898aSmrg if (e->next) 21323a0898aSmrg e->next->prev = e->prev; 21423a0898aSmrg *e->prev = e->next; 21523a0898aSmrg e->next = cache->free; 21623a0898aSmrg cache->free = e; 2177f7f5e4eSmrg free (e->pattern); 21823a0898aSmrg e->pattern = 0; 21923a0898aSmrg } 22023a0898aSmrg } 22123a0898aSmrg} 222