Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / freetype2 / src / cache / ftccache.c
blobf3e699c3850d33e3e69fdcf491248990f5968e88
1 /***************************************************************************/
2 /* */
3 /* ftccache.c */
4 /* */
5 /* The FreeType internal cache interface (body). */
6 /* */
7 /* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006, 2007 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
19 #include <ft2build.h>
20 #include "ftcmanag.h"
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_DEBUG_H
24 #include "ftccback.h"
25 #include "ftcerror.h"
28 #define FTC_HASH_MAX_LOAD 2
29 #define FTC_HASH_MIN_LOAD 1
30 #define FTC_HASH_SUB_LOAD ( FTC_HASH_MAX_LOAD - FTC_HASH_MIN_LOAD )
32 /* this one _must_ be a power of 2! */
33 #define FTC_HASH_INITIAL_SIZE 8
36 /*************************************************************************/
37 /*************************************************************************/
38 /***** *****/
39 /***** CACHE NODE DEFINITIONS *****/
40 /***** *****/
41 /*************************************************************************/
42 /*************************************************************************/
44 /* add a new node to the head of the manager's circular MRU list */
45 static void
46 ftc_node_mru_link( FTC_Node node,
47 FTC_Manager manager )
49 void *nl = &manager->nodes_list;
52 FTC_MruNode_Prepend( (FTC_MruNode*)nl,
53 (FTC_MruNode)node );
54 manager->num_nodes++;
58 /* remove a node from the manager's MRU list */
59 static void
60 ftc_node_mru_unlink( FTC_Node node,
61 FTC_Manager manager )
63 void *nl = &manager->nodes_list;
66 FTC_MruNode_Remove( (FTC_MruNode*)nl,
67 (FTC_MruNode)node );
68 manager->num_nodes--;
72 #ifndef FTC_INLINE
74 /* move a node to the head of the manager's MRU list */
75 static void
76 ftc_node_mru_up( FTC_Node node,
77 FTC_Manager manager )
79 FTC_MruNode_Up( (FTC_MruNode*)&manager->nodes_list,
80 (FTC_MruNode)node );
83 #endif /* !FTC_INLINE */
86 /* Note that this function cannot fail. If we cannot re-size the
87 * buckets array appropriately, we simply degrade the hash table's
88 * performance!
90 static void
91 ftc_cache_resize( FTC_Cache cache )
93 for (;;)
95 FTC_Node node, *pnode;
96 FT_UInt p = cache->p;
97 FT_UInt mask = cache->mask;
98 FT_UInt count = mask + p + 1; /* number of buckets */
101 /* do we need to shrink the buckets array? */
102 if ( cache->slack < 0 )
104 FTC_Node new_list = NULL;
107 /* try to expand the buckets array _before_ splitting
108 * the bucket lists
110 if ( p >= mask )
112 FT_Memory memory = cache->memory;
113 FT_Error error;
116 /* if we can't expand the array, leave immediately */
117 if ( FT_RENEW_ARRAY( cache->buckets, (mask+1)*2, (mask+1)*4 ) )
118 break;
121 /* split a single bucket */
122 pnode = cache->buckets + p;
124 for (;;)
126 node = *pnode;
127 if ( node == NULL )
128 break;
130 if ( node->hash & ( mask + 1 ) )
132 *pnode = node->link;
133 node->link = new_list;
134 new_list = node;
136 else
137 pnode = &node->link;
140 cache->buckets[p + mask + 1] = new_list;
142 cache->slack += FTC_HASH_MAX_LOAD;
144 if ( p >= mask )
146 cache->mask = 2 * mask + 1;
147 cache->p = 0;
149 else
150 cache->p = p + 1;
153 /* do we need to expand the buckets array? */
154 else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
156 FT_UInt old_index = p + mask;
157 FTC_Node* pold;
160 if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
161 break;
163 if ( p == 0 )
165 FT_Memory memory = cache->memory;
166 FT_Error error;
169 /* if we can't shrink the array, leave immediately */
170 if ( FT_RENEW_ARRAY( cache->buckets,
171 ( mask + 1 ) * 2, mask + 1 ) )
172 break;
174 cache->mask >>= 1;
175 p = cache->mask;
177 else
178 p--;
180 pnode = cache->buckets + p;
181 while ( *pnode )
182 pnode = &(*pnode)->link;
184 pold = cache->buckets + old_index;
185 *pnode = *pold;
186 *pold = NULL;
188 cache->slack -= FTC_HASH_MAX_LOAD;
189 cache->p = p;
191 else /* the hash table is balanced */
192 break;
197 /* remove a node from its cache's hash table */
198 static void
199 ftc_node_hash_unlink( FTC_Node node0,
200 FTC_Cache cache )
202 FTC_Node *pnode;
203 FT_UInt idx;
206 idx = (FT_UInt)( node0->hash & cache->mask );
207 if ( idx < cache->p )
208 idx = (FT_UInt)( node0->hash & ( 2 * cache->mask + 1 ) );
210 pnode = cache->buckets + idx;
212 for (;;)
214 FTC_Node node = *pnode;
217 if ( node == NULL )
219 FT_ERROR(( "ftc_node_hash_unlink: unknown node!\n" ));
220 return;
223 if ( node == node0 )
224 break;
226 pnode = &(*pnode)->link;
229 *pnode = node0->link;
230 node0->link = NULL;
232 cache->slack++;
233 ftc_cache_resize( cache );
237 /* add a node to the `top' of its cache's hash table */
238 static void
239 ftc_node_hash_link( FTC_Node node,
240 FTC_Cache cache )
242 FTC_Node *pnode;
243 FT_UInt idx;
246 idx = (FT_UInt)( node->hash & cache->mask );
247 if ( idx < cache->p )
248 idx = (FT_UInt)( node->hash & (2 * cache->mask + 1 ) );
250 pnode = cache->buckets + idx;
252 node->link = *pnode;
253 *pnode = node;
255 cache->slack--;
256 ftc_cache_resize( cache );
260 /* remove a node from the cache manager */
261 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
262 FT_BASE_DEF( void )
263 #else
264 FT_LOCAL_DEF( void )
265 #endif
266 ftc_node_destroy( FTC_Node node,
267 FTC_Manager manager )
269 FTC_Cache cache;
272 #ifdef FT_DEBUG_ERROR
273 /* find node's cache */
274 if ( node->cache_index >= manager->num_caches )
276 FT_ERROR(( "ftc_node_destroy: invalid node handle\n" ));
277 return;
279 #endif
281 cache = manager->caches[node->cache_index];
283 #ifdef FT_DEBUG_ERROR
284 if ( cache == NULL )
286 FT_ERROR(( "ftc_node_destroy: invalid node handle\n" ));
287 return;
289 #endif
291 manager->cur_weight -= cache->clazz.node_weight( node, cache );
293 /* remove node from mru list */
294 ftc_node_mru_unlink( node, manager );
296 /* remove node from cache's hash table */
297 ftc_node_hash_unlink( node, cache );
299 /* now finalize it */
300 cache->clazz.node_free( node, cache );
302 #if 0
303 /* check, just in case of general corruption :-) */
304 if ( manager->num_nodes == 0 )
305 FT_ERROR(( "ftc_node_destroy: invalid cache node count! = %d\n",
306 manager->num_nodes ));
307 #endif
311 /*************************************************************************/
312 /*************************************************************************/
313 /***** *****/
314 /***** ABSTRACT CACHE CLASS *****/
315 /***** *****/
316 /*************************************************************************/
317 /*************************************************************************/
320 FT_LOCAL_DEF( FT_Error )
321 FTC_Cache_Init( FTC_Cache cache )
323 return ftc_cache_init( cache );
327 FT_LOCAL_DEF( FT_Error )
328 ftc_cache_init( FTC_Cache cache )
330 FT_Memory memory = cache->memory;
331 FT_Error error;
334 cache->p = 0;
335 cache->mask = FTC_HASH_INITIAL_SIZE - 1;
336 cache->slack = FTC_HASH_INITIAL_SIZE * FTC_HASH_MAX_LOAD;
338 (void)FT_NEW_ARRAY( cache->buckets, FTC_HASH_INITIAL_SIZE * 2 );
339 return error;
343 static void
344 FTC_Cache_Clear( FTC_Cache cache )
346 if ( cache )
348 FTC_Manager manager = cache->manager;
349 FT_UFast i;
350 FT_UInt count;
353 count = cache->p + cache->mask + 1;
355 for ( i = 0; i < count; i++ )
357 FTC_Node *pnode = cache->buckets + i, next, node = *pnode;
360 while ( node )
362 next = node->link;
363 node->link = NULL;
365 /* remove node from mru list */
366 ftc_node_mru_unlink( node, manager );
368 /* now finalize it */
369 manager->cur_weight -= cache->clazz.node_weight( node, cache );
371 cache->clazz.node_free( node, cache );
372 node = next;
374 cache->buckets[i] = NULL;
376 ftc_cache_resize( cache );
381 FT_LOCAL_DEF( void )
382 ftc_cache_done( FTC_Cache cache )
384 if ( cache->memory )
386 FT_Memory memory = cache->memory;
389 FTC_Cache_Clear( cache );
391 FT_FREE( cache->buckets );
392 cache->mask = 0;
393 cache->p = 0;
394 cache->slack = 0;
396 cache->memory = NULL;
401 FT_LOCAL_DEF( void )
402 FTC_Cache_Done( FTC_Cache cache )
404 ftc_cache_done( cache );
408 static void
409 ftc_cache_add( FTC_Cache cache,
410 FT_UInt32 hash,
411 FTC_Node node )
413 node->hash = hash;
414 node->cache_index = (FT_UInt16) cache->index;
415 node->ref_count = 0;
417 ftc_node_hash_link( node, cache );
418 ftc_node_mru_link( node, cache->manager );
421 FTC_Manager manager = cache->manager;
424 manager->cur_weight += cache->clazz.node_weight( node, cache );
426 if ( manager->cur_weight >= manager->max_weight )
428 node->ref_count++;
429 FTC_Manager_Compress( manager );
430 node->ref_count--;
436 FT_LOCAL_DEF( FT_Error )
437 FTC_Cache_NewNode( FTC_Cache cache,
438 FT_UInt32 hash,
439 FT_Pointer query,
440 FTC_Node *anode )
442 FT_Error error;
443 FTC_Node node;
447 * We use the FTC_CACHE_TRYLOOP macros to support out-of-memory
448 * errors (OOM) correctly, i.e., by flushing the cache progressively
449 * in order to make more room.
452 FTC_CACHE_TRYLOOP( cache )
454 error = cache->clazz.node_new( &node, query, cache );
456 FTC_CACHE_TRYLOOP_END();
458 if ( error )
459 node = NULL;
460 else
462 /* don't assume that the cache has the same number of buckets, since
463 * our allocation request might have triggered global cache flushing
465 ftc_cache_add( cache, hash, node );
468 *anode = node;
469 return error;
473 #ifndef FTC_INLINE
475 FT_LOCAL_DEF( FT_Error )
476 FTC_Cache_Lookup( FTC_Cache cache,
477 FT_UInt32 hash,
478 FT_Pointer query,
479 FTC_Node *anode )
481 FT_UFast idx;
482 FTC_Node* bucket;
483 FTC_Node* pnode;
484 FTC_Node node;
485 FT_Error error = 0;
487 FTC_Node_CompareFunc compare = cache->clazz.node_compare;
490 if ( cache == NULL || anode == NULL )
491 return FT_Err_Invalid_Argument;
493 idx = hash & cache->mask;
494 if ( idx < cache->p )
495 idx = hash & ( cache->mask * 2 + 1 );
497 bucket = cache->buckets + idx;
498 pnode = bucket;
499 for (;;)
501 node = *pnode;
502 if ( node == NULL )
503 goto NewNode;
505 if ( node->hash == hash && compare( node, query, cache ) )
506 break;
508 pnode = &node->link;
511 if ( node != *bucket )
513 *pnode = node->link;
514 node->link = *bucket;
515 *bucket = node;
518 /* move to head of MRU list */
520 FTC_Manager manager = cache->manager;
523 if ( node != manager->nodes_list )
524 ftc_node_mru_up( node, manager );
526 *anode = node;
527 return error;
529 NewNode:
530 return FTC_Cache_NewNode( cache, hash, query, anode );
533 #endif /* !FTC_INLINE */
536 FT_LOCAL_DEF( void )
537 FTC_Cache_RemoveFaceID( FTC_Cache cache,
538 FTC_FaceID face_id )
540 FT_UFast i, count;
541 FTC_Manager manager = cache->manager;
542 FTC_Node frees = NULL;
545 count = cache->p + cache->mask;
546 for ( i = 0; i < count; i++ )
548 FTC_Node* bucket = cache->buckets + i;
549 FTC_Node* pnode = bucket;
552 for ( ;; )
554 FTC_Node node = *pnode;
557 if ( node == NULL )
558 break;
560 if ( cache->clazz.node_remove_faceid( node, face_id, cache ) )
562 *pnode = node->link;
563 node->link = frees;
564 frees = node;
566 else
567 pnode = &node->link;
571 /* remove all nodes in the free list */
572 while ( frees )
574 FTC_Node node;
577 node = frees;
578 frees = node->link;
580 manager->cur_weight -= cache->clazz.node_weight( node, cache );
581 ftc_node_mru_unlink( node, manager );
583 cache->clazz.node_free( node, cache );
585 cache->slack++;
588 ftc_cache_resize( cache );
592 /* END */