Sync with trunk head
[reactos.git] / lib / 3rdparty / freetype / src / cache / ftccache.c
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 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include "ftcmanag.h"
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_DEBUG_H
23
24 #include "ftccback.h"
25 #include "ftcerror.h"
26
27
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 )
31
32 /* this one _must_ be a power of 2! */
33 #define FTC_HASH_INITIAL_SIZE 8
34
35
36 /*************************************************************************/
37 /*************************************************************************/
38 /***** *****/
39 /***** CACHE NODE DEFINITIONS *****/
40 /***** *****/
41 /*************************************************************************/
42 /*************************************************************************/
43
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 )
48 {
49 void *nl = &manager->nodes_list;
50
51
52 FTC_MruNode_Prepend( (FTC_MruNode*)nl,
53 (FTC_MruNode)node );
54 manager->num_nodes++;
55 }
56
57
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 )
62 {
63 void *nl = &manager->nodes_list;
64
65
66 FTC_MruNode_Remove( (FTC_MruNode*)nl,
67 (FTC_MruNode)node );
68 manager->num_nodes--;
69 }
70
71
72 #ifndef FTC_INLINE
73
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 )
78 {
79 FTC_MruNode_Up( (FTC_MruNode*)&manager->nodes_list,
80 (FTC_MruNode)node );
81 }
82
83 #endif /* !FTC_INLINE */
84
85
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!
89 */
90 static void
91 ftc_cache_resize( FTC_Cache cache )
92 {
93 for (;;)
94 {
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 */
99
100
101 /* do we need to shrink the buckets array? */
102 if ( cache->slack < 0 )
103 {
104 FTC_Node new_list = NULL;
105
106
107 /* try to expand the buckets array _before_ splitting
108 * the bucket lists
109 */
110 if ( p >= mask )
111 {
112 FT_Memory memory = cache->memory;
113 FT_Error error;
114
115
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;
119 }
120
121 /* split a single bucket */
122 pnode = cache->buckets + p;
123
124 for (;;)
125 {
126 node = *pnode;
127 if ( node == NULL )
128 break;
129
130 if ( node->hash & ( mask + 1 ) )
131 {
132 *pnode = node->link;
133 node->link = new_list;
134 new_list = node;
135 }
136 else
137 pnode = &node->link;
138 }
139
140 cache->buckets[p + mask + 1] = new_list;
141
142 cache->slack += FTC_HASH_MAX_LOAD;
143
144 if ( p >= mask )
145 {
146 cache->mask = 2 * mask + 1;
147 cache->p = 0;
148 }
149 else
150 cache->p = p + 1;
151 }
152
153 /* do we need to expand the buckets array? */
154 else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
155 {
156 FT_UInt old_index = p + mask;
157 FTC_Node* pold;
158
159
160 if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
161 break;
162
163 if ( p == 0 )
164 {
165 FT_Memory memory = cache->memory;
166 FT_Error error;
167
168
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;
173
174 cache->mask >>= 1;
175 p = cache->mask;
176 }
177 else
178 p--;
179
180 pnode = cache->buckets + p;
181 while ( *pnode )
182 pnode = &(*pnode)->link;
183
184 pold = cache->buckets + old_index;
185 *pnode = *pold;
186 *pold = NULL;
187
188 cache->slack -= FTC_HASH_MAX_LOAD;
189 cache->p = p;
190 }
191 else /* the hash table is balanced */
192 break;
193 }
194 }
195
196
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 )
201 {
202 FTC_Node *pnode;
203 FT_UInt idx;
204
205
206 idx = (FT_UInt)( node0->hash & cache->mask );
207 if ( idx < cache->p )
208 idx = (FT_UInt)( node0->hash & ( 2 * cache->mask + 1 ) );
209
210 pnode = cache->buckets + idx;
211
212 for (;;)
213 {
214 FTC_Node node = *pnode;
215
216
217 if ( node == NULL )
218 {
219 FT_ERROR(( "ftc_node_hash_unlink: unknown node!\n" ));
220 return;
221 }
222
223 if ( node == node0 )
224 break;
225
226 pnode = &(*pnode)->link;
227 }
228
229 *pnode = node0->link;
230 node0->link = NULL;
231
232 cache->slack++;
233 ftc_cache_resize( cache );
234 }
235
236
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 )
241 {
242 FTC_Node *pnode;
243 FT_UInt idx;
244
245
246 idx = (FT_UInt)( node->hash & cache->mask );
247 if ( idx < cache->p )
248 idx = (FT_UInt)( node->hash & (2 * cache->mask + 1 ) );
249
250 pnode = cache->buckets + idx;
251
252 node->link = *pnode;
253 *pnode = node;
254
255 cache->slack--;
256 ftc_cache_resize( cache );
257 }
258
259
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 )
268 {
269 FTC_Cache cache;
270
271
272 #ifdef FT_DEBUG_ERROR
273 /* find node's cache */
274 if ( node->cache_index >= manager->num_caches )
275 {
276 FT_ERROR(( "ftc_node_destroy: invalid node handle\n" ));
277 return;
278 }
279 #endif
280
281 cache = manager->caches[node->cache_index];
282
283 #ifdef FT_DEBUG_ERROR
284 if ( cache == NULL )
285 {
286 FT_ERROR(( "ftc_node_destroy: invalid node handle\n" ));
287 return;
288 }
289 #endif
290
291 manager->cur_weight -= cache->clazz.node_weight( node, cache );
292
293 /* remove node from mru list */
294 ftc_node_mru_unlink( node, manager );
295
296 /* remove node from cache's hash table */
297 ftc_node_hash_unlink( node, cache );
298
299 /* now finalize it */
300 cache->clazz.node_free( node, cache );
301
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
308 }
309
310
311 /*************************************************************************/
312 /*************************************************************************/
313 /***** *****/
314 /***** ABSTRACT CACHE CLASS *****/
315 /***** *****/
316 /*************************************************************************/
317 /*************************************************************************/
318
319
320 FT_LOCAL_DEF( FT_Error )
321 FTC_Cache_Init( FTC_Cache cache )
322 {
323 return ftc_cache_init( cache );
324 }
325
326
327 FT_LOCAL_DEF( FT_Error )
328 ftc_cache_init( FTC_Cache cache )
329 {
330 FT_Memory memory = cache->memory;
331 FT_Error error;
332
333
334 cache->p = 0;
335 cache->mask = FTC_HASH_INITIAL_SIZE - 1;
336 cache->slack = FTC_HASH_INITIAL_SIZE * FTC_HASH_MAX_LOAD;
337
338 (void)FT_NEW_ARRAY( cache->buckets, FTC_HASH_INITIAL_SIZE * 2 );
339 return error;
340 }
341
342
343 static void
344 FTC_Cache_Clear( FTC_Cache cache )
345 {
346 if ( cache )
347 {
348 FTC_Manager manager = cache->manager;
349 FT_UFast i;
350 FT_UInt count;
351
352
353 count = cache->p + cache->mask + 1;
354
355 for ( i = 0; i < count; i++ )
356 {
357 FTC_Node *pnode = cache->buckets + i, next, node = *pnode;
358
359
360 while ( node )
361 {
362 next = node->link;
363 node->link = NULL;
364
365 /* remove node from mru list */
366 ftc_node_mru_unlink( node, manager );
367
368 /* now finalize it */
369 manager->cur_weight -= cache->clazz.node_weight( node, cache );
370
371 cache->clazz.node_free( node, cache );
372 node = next;
373 }
374 cache->buckets[i] = NULL;
375 }
376 ftc_cache_resize( cache );
377 }
378 }
379
380
381 FT_LOCAL_DEF( void )
382 ftc_cache_done( FTC_Cache cache )
383 {
384 if ( cache->memory )
385 {
386 FT_Memory memory = cache->memory;
387
388
389 FTC_Cache_Clear( cache );
390
391 FT_FREE( cache->buckets );
392 cache->mask = 0;
393 cache->p = 0;
394 cache->slack = 0;
395
396 cache->memory = NULL;
397 }
398 }
399
400
401 FT_LOCAL_DEF( void )
402 FTC_Cache_Done( FTC_Cache cache )
403 {
404 ftc_cache_done( cache );
405 }
406
407
408 static void
409 ftc_cache_add( FTC_Cache cache,
410 FT_UInt32 hash,
411 FTC_Node node )
412 {
413 node->hash = hash;
414 node->cache_index = (FT_UInt16) cache->index;
415 node->ref_count = 0;
416
417 ftc_node_hash_link( node, cache );
418 ftc_node_mru_link( node, cache->manager );
419
420 {
421 FTC_Manager manager = cache->manager;
422
423
424 manager->cur_weight += cache->clazz.node_weight( node, cache );
425
426 if ( manager->cur_weight >= manager->max_weight )
427 {
428 node->ref_count++;
429 FTC_Manager_Compress( manager );
430 node->ref_count--;
431 }
432 }
433 }
434
435
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 )
441 {
442 FT_Error error;
443 FTC_Node node;
444
445
446 /*
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.
450 */
451
452 FTC_CACHE_TRYLOOP( cache )
453 {
454 error = cache->clazz.node_new( &node, query, cache );
455 }
456 FTC_CACHE_TRYLOOP_END();
457
458 if ( error )
459 node = NULL;
460 else
461 {
462 /* don't assume that the cache has the same number of buckets, since
463 * our allocation request might have triggered global cache flushing
464 */
465 ftc_cache_add( cache, hash, node );
466 }
467
468 *anode = node;
469 return error;
470 }
471
472
473 #ifndef FTC_INLINE
474
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 )
480 {
481 FT_UFast idx;
482 FTC_Node* bucket;
483 FTC_Node* pnode;
484 FTC_Node node;
485 FT_Error error = 0;
486
487 FTC_Node_CompareFunc compare = cache->clazz.node_compare;
488
489
490 if ( cache == NULL || anode == NULL )
491 return FT_Err_Invalid_Argument;
492
493 idx = hash & cache->mask;
494 if ( idx < cache->p )
495 idx = hash & ( cache->mask * 2 + 1 );
496
497 bucket = cache->buckets + idx;
498 pnode = bucket;
499 for (;;)
500 {
501 node = *pnode;
502 if ( node == NULL )
503 goto NewNode;
504
505 if ( node->hash == hash && compare( node, query, cache ) )
506 break;
507
508 pnode = &node->link;
509 }
510
511 if ( node != *bucket )
512 {
513 *pnode = node->link;
514 node->link = *bucket;
515 *bucket = node;
516 }
517
518 /* move to head of MRU list */
519 {
520 FTC_Manager manager = cache->manager;
521
522
523 if ( node != manager->nodes_list )
524 ftc_node_mru_up( node, manager );
525 }
526 *anode = node;
527 return error;
528
529 NewNode:
530 return FTC_Cache_NewNode( cache, hash, query, anode );
531 }
532
533 #endif /* !FTC_INLINE */
534
535
536 FT_LOCAL_DEF( void )
537 FTC_Cache_RemoveFaceID( FTC_Cache cache,
538 FTC_FaceID face_id )
539 {
540 FT_UFast i, count;
541 FTC_Manager manager = cache->manager;
542 FTC_Node frees = NULL;
543
544
545 count = cache->p + cache->mask;
546 for ( i = 0; i < count; i++ )
547 {
548 FTC_Node* bucket = cache->buckets + i;
549 FTC_Node* pnode = bucket;
550
551
552 for ( ;; )
553 {
554 FTC_Node node = *pnode;
555
556
557 if ( node == NULL )
558 break;
559
560 if ( cache->clazz.node_remove_faceid( node, face_id, cache ) )
561 {
562 *pnode = node->link;
563 node->link = frees;
564 frees = node;
565 }
566 else
567 pnode = &node->link;
568 }
569 }
570
571 /* remove all nodes in the free list */
572 while ( frees )
573 {
574 FTC_Node node;
575
576
577 node = frees;
578 frees = node->link;
579
580 manager->cur_weight -= cache->clazz.node_weight( node, cache );
581 ftc_node_mru_unlink( node, manager );
582
583 cache->clazz.node_free( node, cache );
584
585 cache->slack++;
586 }
587
588 ftc_cache_resize( cache );
589 }
590
591
592 /* END */