1 /****************************************************************************
5 * The FreeType internal cache interface (body).
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.
20 #include <freetype/internal/ftobjs.h>
21 #include <freetype/internal/ftdebug.h>
27 #define FT_COMPONENT cache
30 #define FTC_HASH_MAX_LOAD 2
31 #define FTC_HASH_MIN_LOAD 1
32 #define FTC_HASH_SUB_LOAD ( FTC_HASH_MAX_LOAD - FTC_HASH_MIN_LOAD )
34 /* this one _must_ be a power of 2! */
35 #define FTC_HASH_INITIAL_SIZE 8
38 /*************************************************************************/
39 /*************************************************************************/
41 /***** CACHE NODE DEFINITIONS *****/
43 /*************************************************************************/
44 /*************************************************************************/
46 /* add a new node to the head of the manager's circular MRU list */
48 ftc_node_mru_link( FTC_Node node,
51 void *nl = &manager->nodes_list;
54 FTC_MruNode_Prepend( (FTC_MruNode*)nl,
60 /* remove a node from the manager's MRU list */
62 ftc_node_mru_unlink( FTC_Node node,
65 void *nl = &manager->nodes_list;
68 FTC_MruNode_Remove( (FTC_MruNode*)nl,
76 /* move a node to the head of the manager's MRU list */
78 ftc_node_mru_up( FTC_Node node,
81 FTC_MruNode_Up( (FTC_MruNode*)&manager->nodes_list,
86 /* get a top bucket for specified hash from cache,
87 * body for FTC_NODE_TOP_FOR_HASH( cache, hash )
89 FT_LOCAL_DEF( FTC_Node* )
90 ftc_get_top_node_for_hash( FTC_Cache cache,
97 idx = hash & cache->mask;
99 idx = hash & ( 2 * cache->mask + 1 );
100 pnode = cache->buckets + idx;
104 #endif /* !FTC_INLINE */
107 /* Note that this function cannot fail. If we cannot re-size the
108 * buckets array appropriately, we simply degrade the hash table's
112 ftc_cache_resize( FTC_Cache cache )
116 FTC_Node node, *pnode;
117 FT_UFast p = cache->p;
118 FT_UFast mask = cache->mask;
119 FT_UFast count = mask + p + 1; /* number of buckets */
122 /* do we need to shrink the buckets array? */
123 if ( cache->slack < 0 )
125 FTC_Node new_list = NULL;
128 /* try to expand the buckets array _before_ splitting
133 FT_Memory memory = cache->memory;
137 /* if we can't expand the array, leave immediately */
138 if ( FT_RENEW_ARRAY( cache->buckets,
139 ( mask + 1 ) * 2, ( mask + 1 ) * 4 ) )
143 /* split a single bucket */
144 pnode = cache->buckets + p;
152 if ( node->hash & ( mask + 1 ) )
155 node->link = new_list;
162 cache->buckets[p + mask + 1] = new_list;
164 cache->slack += FTC_HASH_MAX_LOAD;
168 cache->mask = 2 * mask + 1;
175 /* do we need to expand the buckets array? */
176 else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
178 FT_UFast old_index = p + mask;
182 if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
187 FT_Memory memory = cache->memory;
191 /* if we can't shrink the array, leave immediately */
192 if ( FT_RENEW_ARRAY( cache->buckets,
193 ( mask + 1 ) * 2, mask + 1 ) )
202 pnode = cache->buckets + p;
204 pnode = &(*pnode)->link;
206 pold = cache->buckets + old_index;
210 cache->slack -= FTC_HASH_MAX_LOAD;
214 /* otherwise, the hash table is balanced */
221 /* remove a node from its cache's hash table */
223 ftc_node_hash_unlink( FTC_Node node0,
226 FTC_Node *pnode = FTC_NODE_TOP_FOR_HASH( cache, node0->hash );
231 FTC_Node node = *pnode;
236 FT_TRACE0(( "ftc_node_hash_unlink: unknown node\n" ));
243 pnode = &(*pnode)->link;
246 *pnode = node0->link;
250 ftc_cache_resize( cache );
254 /* add a node to the `top' of its cache's hash table */
256 ftc_node_hash_link( FTC_Node node,
259 FTC_Node *pnode = FTC_NODE_TOP_FOR_HASH( cache, node->hash );
266 ftc_cache_resize( cache );
270 /* remove a node from the cache manager */
272 ftc_node_destroy( FTC_Node node,
273 FTC_Manager manager )
278 #ifdef FT_DEBUG_ERROR
279 /* find node's cache */
280 if ( node->cache_index >= manager->num_caches )
282 FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
287 cache = manager->caches[node->cache_index];
289 #ifdef FT_DEBUG_ERROR
292 FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
297 manager->cur_weight -= cache->clazz.node_weight( node, cache );
299 /* remove node from mru list */
300 ftc_node_mru_unlink( node, manager );
302 /* remove node from cache's hash table */
303 ftc_node_hash_unlink( node, cache );
305 /* now finalize it */
306 cache->clazz.node_free( node, cache );
309 /* check, just in case of general corruption :-) */
310 if ( manager->num_nodes == 0 )
311 FT_TRACE0(( "ftc_node_destroy: invalid cache node count (%d)\n",
312 manager->num_nodes ));
317 /*************************************************************************/
318 /*************************************************************************/
320 /***** ABSTRACT CACHE CLASS *****/
322 /*************************************************************************/
323 /*************************************************************************/
326 FT_LOCAL_DEF( FT_Error )
327 FTC_Cache_Init( FTC_Cache cache )
329 return ftc_cache_init( cache );
333 FT_LOCAL_DEF( FT_Error )
334 ftc_cache_init( FTC_Cache cache )
336 FT_Memory memory = cache->memory;
341 cache->mask = FTC_HASH_INITIAL_SIZE - 1;
342 cache->slack = FTC_HASH_INITIAL_SIZE * FTC_HASH_MAX_LOAD;
344 (void)FT_NEW_ARRAY( cache->buckets, FTC_HASH_INITIAL_SIZE * 2 );
350 FTC_Cache_Clear( FTC_Cache cache )
352 if ( cache && cache->buckets )
354 FTC_Manager manager = cache->manager;
359 count = cache->p + cache->mask + 1;
361 for ( i = 0; i < count; i++ )
363 FTC_Node *pnode = cache->buckets + i, next, node = *pnode;
371 /* remove node from mru list */
372 ftc_node_mru_unlink( node, manager );
374 /* now finalize it */
375 manager->cur_weight -= cache->clazz.node_weight( node, cache );
377 cache->clazz.node_free( node, cache );
380 cache->buckets[i] = NULL;
382 ftc_cache_resize( cache );
388 ftc_cache_done( FTC_Cache cache )
392 FT_Memory memory = cache->memory;
395 FTC_Cache_Clear( cache );
397 FT_FREE( cache->buckets );
402 cache->memory = NULL;
408 FTC_Cache_Done( FTC_Cache cache )
410 ftc_cache_done( cache );
415 ftc_cache_add( FTC_Cache cache,
420 node->cache_index = (FT_UInt16)cache->index;
423 ftc_node_hash_link( node, cache );
424 ftc_node_mru_link( node, cache->manager );
427 FTC_Manager manager = cache->manager;
430 manager->cur_weight += cache->clazz.node_weight( node, cache );
432 if ( manager->cur_weight >= manager->max_weight )
435 FTC_Manager_Compress( manager );
442 FT_LOCAL_DEF( FT_Error )
443 FTC_Cache_NewNode( FTC_Cache cache,
453 * We use the FTC_CACHE_TRYLOOP macros to support out-of-memory
454 * errors (OOM) correctly, i.e., by flushing the cache progressively
455 * in order to make more room.
458 FTC_CACHE_TRYLOOP( cache )
460 error = cache->clazz.node_new( &node, query, cache );
462 FTC_CACHE_TRYLOOP_END( NULL );
468 /* don't assume that the cache has the same number of buckets, since
469 * our allocation request might have triggered global cache flushing
471 ftc_cache_add( cache, hash, node );
481 FT_LOCAL_DEF( FT_Error )
482 FTC_Cache_Lookup( FTC_Cache cache,
490 FT_Error error = FT_Err_Ok;
491 FT_Bool list_changed = FALSE;
493 FTC_Node_CompareFunc compare = cache->clazz.node_compare;
496 if ( !cache || !anode )
497 return FT_THROW( Invalid_Argument );
499 /* Go to the `top' node of the list sharing same masked hash */
500 bucket = pnode = FTC_NODE_TOP_FOR_HASH( cache, hash );
502 /* Lookup a node with exactly same hash and queried properties. */
503 /* NOTE: _nodcomp() may change the linked list to reduce memory. */
510 if ( node->hash == hash &&
511 compare( node, query, cache, &list_changed ) )
519 /* Update bucket by modified linked list */
520 bucket = pnode = FTC_NODE_TOP_FOR_HASH( cache, hash );
522 /* Update pnode by modified linked list */
523 while ( *pnode != node )
527 FT_ERROR(( "FTC_Cache_Lookup: oops!!! node missing\n" ));
531 pnode = &((*pnode)->link);
535 /* Reorder the list to move the found node to the `top' */
536 if ( node != *bucket )
539 node->link = *bucket;
543 /* move to head of MRU list */
545 FTC_Manager manager = cache->manager;
548 if ( node != manager->nodes_list )
549 ftc_node_mru_up( node, manager );
556 return FTC_Cache_NewNode( cache, hash, query, anode );
559 #endif /* !FTC_INLINE */
563 FTC_Cache_RemoveFaceID( FTC_Cache cache,
567 FTC_Manager manager = cache->manager;
568 FTC_Node frees = NULL;
571 count = cache->p + cache->mask + 1;
572 for ( i = 0; i < count; i++ )
574 FTC_Node* bucket = cache->buckets + i;
575 FTC_Node* pnode = bucket;
580 FTC_Node node = *pnode;
581 FT_Bool list_changed = FALSE;
587 if ( cache->clazz.node_remove_faceid( node, face_id,
588 cache, &list_changed ) )
599 /* remove all nodes in the free list */
608 manager->cur_weight -= cache->clazz.node_weight( node, cache );
609 ftc_node_mru_unlink( node, manager );
611 cache->clazz.node_free( node, cache );
616 ftc_cache_resize( cache );