1 /****************************************************************************
5 * FreeType internal cache interface (specification).
7 * Copyright (C) 2000-2020 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
22 #include <freetype/internal/compiler-macros.h>
27 #define FTC_FACE_ID_HASH( i ) \
28 ( ( (FT_Offset)(i) >> 3 ) ^ ( (FT_Offset)(i) << 7 ) )
30 /* handle to cache object */
31 typedef struct FTC_CacheRec_* FTC_Cache;
33 /* handle to cache class */
34 typedef const struct FTC_CacheClassRec_* FTC_CacheClass;
37 /*************************************************************************/
38 /*************************************************************************/
40 /***** CACHE NODE DEFINITIONS *****/
42 /*************************************************************************/
43 /*************************************************************************/
45 /**************************************************************************
47 * Each cache controls one or more cache nodes. Each node is part of
48 * the global_lru list of the manager. Its `data' field however is used
49 * as a reference count for now.
51 * A node can be anything, depending on the type of information held by
52 * the cache. It can be an individual glyph image, a set of bitmaps
53 * glyphs for a given size, some metrics, etc.
57 /* structure size should be 20 bytes on 32-bits machines */
58 typedef struct FTC_NodeRec_
60 FTC_MruNodeRec mru; /* circular mru list pointer */
61 FTC_Node link; /* used for hashing */
62 FT_Offset hash; /* used for hashing too */
63 FT_UShort cache_index; /* index of cache the node belongs to */
64 FT_Short ref_count; /* reference count for this node */
69 #define FTC_NODE( x ) ( (FTC_Node)(x) )
70 #define FTC_NODE_P( x ) ( (FTC_Node*)(x) )
72 #define FTC_NODE_NEXT( x ) FTC_NODE( (x)->mru.next )
73 #define FTC_NODE_PREV( x ) FTC_NODE( (x)->mru.prev )
76 #define FTC_NODE_TOP_FOR_HASH( cache, hash ) \
77 ( ( cache )->buckets + \
78 ( ( ( ( hash ) & ( cache )->mask ) < ( cache )->p ) \
79 ? ( ( hash ) & ( ( cache )->mask * 2 + 1 ) ) \
80 : ( ( hash ) & ( cache )->mask ) ) )
83 ftc_get_top_node_for_hash( FTC_Cache cache,
85 #define FTC_NODE_TOP_FOR_HASH( cache, hash ) \
86 ftc_get_top_node_for_hash( ( cache ), ( hash ) )
90 /*************************************************************************/
91 /*************************************************************************/
93 /***** CACHE DEFINITIONS *****/
95 /*************************************************************************/
96 /*************************************************************************/
98 /* initialize a new cache node */
100 (*FTC_Node_NewFunc)( FTC_Node *pnode,
105 (*FTC_Node_WeightFunc)( FTC_Node node,
108 /* compare a node to a given key pair */
110 (*FTC_Node_CompareFunc)( FTC_Node node,
113 FT_Bool* list_changed );
117 (*FTC_Node_FreeFunc)( FTC_Node node,
121 (*FTC_Cache_InitFunc)( FTC_Cache cache );
124 (*FTC_Cache_DoneFunc)( FTC_Cache cache );
127 typedef struct FTC_CacheClassRec_
129 FTC_Node_NewFunc node_new;
130 FTC_Node_WeightFunc node_weight;
131 FTC_Node_CompareFunc node_compare;
132 FTC_Node_CompareFunc node_remove_faceid;
133 FTC_Node_FreeFunc node_free;
135 FT_Offset cache_size;
136 FTC_Cache_InitFunc cache_init;
137 FTC_Cache_DoneFunc cache_done;
142 /* each cache really implements a dynamic hash table to manage its nodes */
143 typedef struct FTC_CacheRec_
150 FTC_CacheClassRec clazz; /* local copy, for speed */
154 FT_UInt index; /* in manager's table */
156 FTC_CacheClass org_class; /* original class pointer */
161 #define FTC_CACHE( x ) ( (FTC_Cache)(x) )
162 #define FTC_CACHE_P( x ) ( (FTC_Cache*)(x) )
165 /* default cache initialize */
167 FTC_Cache_Init( FTC_Cache cache );
169 /* default cache finalizer */
171 FTC_Cache_Done( FTC_Cache cache );
173 /* Call this function to look up the cache. If no corresponding
174 * node is found, a new one is automatically created. This function
175 * is capable of flushing the cache adequately to make room for the
181 FTC_Cache_Lookup( FTC_Cache cache,
188 FTC_Cache_NewNode( FTC_Cache cache,
193 /* Remove all nodes that relate to a given face_id. This is useful
194 * when un-installing fonts. Note that if a cache node relates to
195 * the face_id but is locked (i.e., has `ref_count > 0'), the node
196 * will _not_ be destroyed, but its internal face_id reference will
199 * The final result will be that the node will never come back
200 * in further lookup requests, and will be flushed on demand from
201 * the cache normally when its reference count reaches 0.
204 FTC_Cache_RemoveFaceID( FTC_Cache cache,
205 FTC_FaceID face_id );
210 #define FTC_CACHE_LOOKUP_CMP( cache, nodecmp, hash, query, node, error ) \
212 FTC_Node *_bucket, *_pnode, _node; \
213 FTC_Cache _cache = FTC_CACHE(cache); \
214 FT_Offset _hash = (FT_Offset)(hash); \
215 FTC_Node_CompareFunc _nodcomp = (FTC_Node_CompareFunc)(nodecmp); \
216 FT_Bool _list_changed = FALSE; \
222 /* Go to the `top' node of the list sharing same masked hash */ \
223 _bucket = _pnode = FTC_NODE_TOP_FOR_HASH( _cache, _hash ); \
225 /* Look up a node with identical hash and queried properties. */ \
226 /* NOTE: _nodcomp() may change the linked list to reduce memory. */ \
233 if ( _node->hash == _hash && \
234 _nodcomp( _node, query, _cache, &_list_changed ) ) \
237 _pnode = &_node->link; \
240 if ( _list_changed ) \
242 /* Update _bucket by possibly modified linked list */ \
243 _bucket = _pnode = FTC_NODE_TOP_FOR_HASH( _cache, _hash ); \
245 /* Update _pnode by possibly modified linked list */ \
246 while ( *_pnode != _node ) \
250 FT_ERROR(( "FTC_CACHE_LOOKUP_CMP: oops!!! node missing\n" )); \
254 _pnode = &((*_pnode)->link); \
258 /* Reorder the list to move the found node to the `top' */ \
259 if ( _node != *_bucket ) \
261 *_pnode = _node->link; \
262 _node->link = *_bucket; \
266 /* Update MRU list */ \
268 FTC_Manager _manager = _cache->manager; \
269 void* _nl = &_manager->nodes_list; \
272 if ( _node != _manager->nodes_list ) \
273 FTC_MruNode_Up( (FTC_MruNode*)_nl, \
274 (FTC_MruNode)_node ); \
279 error = FTC_Cache_NewNode( _cache, _hash, query, &_node ); \
285 #else /* !FTC_INLINE */
287 #define FTC_CACHE_LOOKUP_CMP( cache, nodecmp, hash, query, node, error ) \
289 error = FTC_Cache_Lookup( FTC_CACHE( cache ), hash, query, \
290 (FTC_Node*)&(node) ); \
293 #endif /* !FTC_INLINE */
297 * This macro, together with FTC_CACHE_TRYLOOP_END, defines a retry
298 * loop to flush the cache repeatedly in case of memory overflows.
300 * It is used when creating a new cache node, or within a lookup
301 * that needs to allocate data (e.g. the sbit cache lookup).
306 * FTC_CACHE_TRYLOOP( cache )
307 * error = load_data( ... );
308 * FTC_CACHE_TRYLOOP_END()
312 #define FTC_CACHE_TRYLOOP( cache ) \
314 FTC_Manager _try_manager = FTC_CACHE( cache )->manager; \
315 FT_UInt _try_count = 4; \
323 #define FTC_CACHE_TRYLOOP_END( list_changed ) \
324 if ( !error || FT_ERR_NEQ( error, Out_Of_Memory ) ) \
327 _try_done = FTC_Manager_FlushN( _try_manager, _try_count ); \
328 if ( _try_done > 0 && list_changed != NULL ) \
329 *(FT_Bool*)( list_changed ) = TRUE; \
331 if ( _try_done == 0 ) \
334 if ( _try_done == _try_count ) \
337 if ( _try_count < _try_done || \
338 _try_count > _try_manager->num_nodes ) \
339 _try_count = _try_manager->num_nodes; \
349 #endif /* FTCCACHE_H_ */