1 /***************************************************************************/
5 /* The FreeType internal cache interface (body). */
7 /* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 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. */
16 /***************************************************************************/
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_DEBUG_H
28 #define FT_COMPONENT trace_cache
31 #define FTC_HASH_MAX_LOAD 2
32 #define FTC_HASH_MIN_LOAD 1
33 #define FTC_HASH_SUB_LOAD ( FTC_HASH_MAX_LOAD - FTC_HASH_MIN_LOAD )
35 /* this one _must_ be a power of 2! */
36 #define FTC_HASH_INITIAL_SIZE 8
39 /*************************************************************************/
40 /*************************************************************************/
42 /***** CACHE NODE DEFINITIONS *****/
44 /*************************************************************************/
45 /*************************************************************************/
47 /* add a new node to the head of the manager's circular MRU list */
49 ftc_node_mru_link( FTC_Node node
,
52 void *nl
= &manager
->nodes_list
;
55 FTC_MruNode_Prepend( (FTC_MruNode
*)nl
,
61 /* remove a node from the manager's MRU list */
63 ftc_node_mru_unlink( FTC_Node node
,
66 void *nl
= &manager
->nodes_list
;
69 FTC_MruNode_Remove( (FTC_MruNode
*)nl
,
77 /* move a node to the head of the manager's MRU list */
79 ftc_node_mru_up( FTC_Node node
,
82 FTC_MruNode_Up( (FTC_MruNode
*)&manager
->nodes_list
,
86 #endif /* !FTC_INLINE */
89 /* Note that this function cannot fail. If we cannot re-size the
90 * buckets array appropriately, we simply degrade the hash table's
94 ftc_cache_resize( FTC_Cache cache
)
98 FTC_Node node
, *pnode
;
99 FT_UFast p
= cache
->p
;
100 FT_UFast mask
= cache
->mask
;
101 FT_UFast count
= mask
+ p
+ 1; /* number of buckets */
104 /* do we need to shrink the buckets array? */
105 if ( cache
->slack
< 0 )
107 FTC_Node new_list
= NULL
;
110 /* try to expand the buckets array _before_ splitting
115 FT_Memory memory
= cache
->memory
;
119 /* if we can't expand the array, leave immediately */
120 if ( FT_RENEW_ARRAY( cache
->buckets
, (mask
+1)*2, (mask
+1)*4 ) )
124 /* split a single bucket */
125 pnode
= cache
->buckets
+ p
;
133 if ( node
->hash
& ( mask
+ 1 ) )
136 node
->link
= new_list
;
143 cache
->buckets
[p
+ mask
+ 1] = new_list
;
145 cache
->slack
+= FTC_HASH_MAX_LOAD
;
149 cache
->mask
= 2 * mask
+ 1;
156 /* do we need to expand the buckets array? */
157 else if ( cache
->slack
> (FT_Long
)count
* FTC_HASH_SUB_LOAD
)
159 FT_UFast old_index
= p
+ mask
;
163 if ( old_index
+ 1 <= FTC_HASH_INITIAL_SIZE
)
168 FT_Memory memory
= cache
->memory
;
172 /* if we can't shrink the array, leave immediately */
173 if ( FT_RENEW_ARRAY( cache
->buckets
,
174 ( mask
+ 1 ) * 2, mask
+ 1 ) )
183 pnode
= cache
->buckets
+ p
;
185 pnode
= &(*pnode
)->link
;
187 pold
= cache
->buckets
+ old_index
;
191 cache
->slack
-= FTC_HASH_MAX_LOAD
;
194 else /* the hash table is balanced */
200 /* remove a node from its cache's hash table */
202 ftc_node_hash_unlink( FTC_Node node0
,
209 idx
= (FT_UInt
)( node0
->hash
& cache
->mask
);
210 if ( idx
< cache
->p
)
211 idx
= (FT_UInt
)( node0
->hash
& ( 2 * cache
->mask
+ 1 ) );
213 pnode
= cache
->buckets
+ idx
;
217 FTC_Node node
= *pnode
;
222 FT_TRACE0(( "ftc_node_hash_unlink: unknown node\n" ));
229 pnode
= &(*pnode
)->link
;
232 *pnode
= node0
->link
;
236 ftc_cache_resize( cache
);
240 /* add a node to the `top' of its cache's hash table */
242 ftc_node_hash_link( FTC_Node node
,
249 idx
= (FT_UInt
)( node
->hash
& cache
->mask
);
250 if ( idx
< cache
->p
)
251 idx
= (FT_UInt
)( node
->hash
& (2 * cache
->mask
+ 1 ) );
253 pnode
= cache
->buckets
+ idx
;
259 ftc_cache_resize( cache
);
263 /* remove a node from the cache manager */
264 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
269 ftc_node_destroy( FTC_Node node
,
270 FTC_Manager manager
)
275 #ifdef FT_DEBUG_ERROR
276 /* find node's cache */
277 if ( node
->cache_index
>= manager
->num_caches
)
279 FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
284 cache
= manager
->caches
[node
->cache_index
];
286 #ifdef FT_DEBUG_ERROR
289 FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
294 manager
->cur_weight
-= cache
->clazz
.node_weight( node
, cache
);
296 /* remove node from mru list */
297 ftc_node_mru_unlink( node
, manager
);
299 /* remove node from cache's hash table */
300 ftc_node_hash_unlink( node
, cache
);
302 /* now finalize it */
303 cache
->clazz
.node_free( node
, cache
);
306 /* check, just in case of general corruption :-) */
307 if ( manager
->num_nodes
== 0 )
308 FT_TRACE0(( "ftc_node_destroy: invalid cache node count (%d)\n",
309 manager
->num_nodes
));
314 /*************************************************************************/
315 /*************************************************************************/
317 /***** ABSTRACT CACHE CLASS *****/
319 /*************************************************************************/
320 /*************************************************************************/
323 FT_LOCAL_DEF( FT_Error
)
324 FTC_Cache_Init( FTC_Cache cache
)
326 return ftc_cache_init( cache
);
330 FT_LOCAL_DEF( FT_Error
)
331 ftc_cache_init( FTC_Cache cache
)
333 FT_Memory memory
= cache
->memory
;
338 cache
->mask
= FTC_HASH_INITIAL_SIZE
- 1;
339 cache
->slack
= FTC_HASH_INITIAL_SIZE
* FTC_HASH_MAX_LOAD
;
341 (void)FT_NEW_ARRAY( cache
->buckets
, FTC_HASH_INITIAL_SIZE
* 2 );
347 FTC_Cache_Clear( FTC_Cache cache
)
351 FTC_Manager manager
= cache
->manager
;
356 count
= cache
->p
+ cache
->mask
+ 1;
358 for ( i
= 0; i
< count
; i
++ )
360 FTC_Node
*pnode
= cache
->buckets
+ i
, next
, node
= *pnode
;
368 /* remove node from mru list */
369 ftc_node_mru_unlink( node
, manager
);
371 /* now finalize it */
372 manager
->cur_weight
-= cache
->clazz
.node_weight( node
, cache
);
374 cache
->clazz
.node_free( node
, cache
);
377 cache
->buckets
[i
] = NULL
;
379 ftc_cache_resize( cache
);
385 ftc_cache_done( FTC_Cache cache
)
389 FT_Memory memory
= cache
->memory
;
392 FTC_Cache_Clear( cache
);
394 FT_FREE( cache
->buckets
);
399 cache
->memory
= NULL
;
405 FTC_Cache_Done( FTC_Cache cache
)
407 ftc_cache_done( cache
);
412 ftc_cache_add( FTC_Cache cache
,
417 node
->cache_index
= (FT_UInt16
) cache
->index
;
420 ftc_node_hash_link( node
, cache
);
421 ftc_node_mru_link( node
, cache
->manager
);
424 FTC_Manager manager
= cache
->manager
;
427 manager
->cur_weight
+= cache
->clazz
.node_weight( node
, cache
);
429 if ( manager
->cur_weight
>= manager
->max_weight
)
432 FTC_Manager_Compress( manager
);
439 FT_LOCAL_DEF( FT_Error
)
440 FTC_Cache_NewNode( FTC_Cache cache
,
450 * We use the FTC_CACHE_TRYLOOP macros to support out-of-memory
451 * errors (OOM) correctly, i.e., by flushing the cache progressively
452 * in order to make more room.
455 FTC_CACHE_TRYLOOP( cache
)
457 error
= cache
->clazz
.node_new( &node
, query
, cache
);
459 FTC_CACHE_TRYLOOP_END();
465 /* don't assume that the cache has the same number of buckets, since
466 * our allocation request might have triggered global cache flushing
468 ftc_cache_add( cache
, hash
, node
);
478 FT_LOCAL_DEF( FT_Error
)
479 FTC_Cache_Lookup( FTC_Cache cache
,
490 FTC_Node_CompareFunc compare
= cache
->clazz
.node_compare
;
493 if ( cache
== NULL
|| anode
== NULL
)
494 return FT_Err_Invalid_Argument
;
496 idx
= hash
& cache
->mask
;
497 if ( idx
< cache
->p
)
498 idx
= hash
& ( cache
->mask
* 2 + 1 );
500 bucket
= cache
->buckets
+ idx
;
508 if ( node
->hash
== hash
&& compare( node
, query
, cache
) )
514 if ( node
!= *bucket
)
517 node
->link
= *bucket
;
521 /* move to head of MRU list */
523 FTC_Manager manager
= cache
->manager
;
526 if ( node
!= manager
->nodes_list
)
527 ftc_node_mru_up( node
, manager
);
533 return FTC_Cache_NewNode( cache
, hash
, query
, anode
);
536 #endif /* !FTC_INLINE */
540 FTC_Cache_RemoveFaceID( FTC_Cache cache
,
544 FTC_Manager manager
= cache
->manager
;
545 FTC_Node frees
= NULL
;
548 count
= cache
->p
+ cache
->mask
;
549 for ( i
= 0; i
< count
; i
++ )
551 FTC_Node
* bucket
= cache
->buckets
+ i
;
552 FTC_Node
* pnode
= bucket
;
557 FTC_Node node
= *pnode
;
563 if ( cache
->clazz
.node_remove_faceid( node
, face_id
, cache
) )
574 /* remove all nodes in the free list */
583 manager
->cur_weight
-= cache
->clazz
.node_weight( node
, cache
);
584 ftc_node_mru_unlink( node
, manager
);
586 cache
->clazz
.node_free( node
, cache
);
591 ftc_cache_resize( cache
);