1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
/***************************************************************************/
/* */
/* ftccache.c */
/* */
/* The FreeType internal cache interface (body). */
/* */
/* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, */
/* 2011 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
/* modified, and distributed under the terms of the FreeType project */
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
/* this file you indicate that you have read the license and */
/* understand and accept it fully. */
/* */
/***************************************************************************/
#include <ft2build.h>
#include "ftcmanag.h"
#include FT_INTERNAL_OBJECTS_H
#include FT_INTERNAL_DEBUG_H
#include "ftccback.h"
#include "ftcerror.h"
#undef FT_COMPONENT
#define FT_COMPONENT trace_cache
#define FTC_HASH_MAX_LOAD 2
#define FTC_HASH_MIN_LOAD 1
#define FTC_HASH_SUB_LOAD ( FTC_HASH_MAX_LOAD - FTC_HASH_MIN_LOAD )
/* this one _must_ be a power of 2! */
#define FTC_HASH_INITIAL_SIZE 8
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** CACHE NODE DEFINITIONS *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
/* add a new node to the head of the manager's circular MRU list */
static void
ftc_node_mru_link( FTC_Node node,
FTC_Manager manager )
{
void *nl = &manager->nodes_list;
FTC_MruNode_Prepend( (FTC_MruNode*)nl,
(FTC_MruNode)node );
manager->num_nodes++;
}
/* remove a node from the manager's MRU list */
static void
ftc_node_mru_unlink( FTC_Node node,
FTC_Manager manager )
{
void *nl = &manager->nodes_list;
FTC_MruNode_Remove( (FTC_MruNode*)nl,
(FTC_MruNode)node );
manager->num_nodes--;
}
#ifndef FTC_INLINE
/* move a node to the head of the manager's MRU list */
static void
ftc_node_mru_up( FTC_Node node,
FTC_Manager manager )
{
FTC_MruNode_Up( (FTC_MruNode*)&manager->nodes_list,
(FTC_MruNode)node );
}
/* get a top bucket for specified hash from cache,
* body for FTC_NODE__TOP_FOR_HASH( cache, hash )
*/
FT_LOCAL_DEF( FTC_Node* )
ftc_get_top_node_for_hash( FTC_Cache cache,
FT_PtrDist hash )
{
FTC_Node* pnode;
FT_UInt idx;
idx = (FT_UInt)( hash & cache->mask );
if ( idx < cache->p )
idx = (FT_UInt)( hash & ( 2 * cache->mask + 1 ) );
pnode = cache->buckets + idx;
return pnode;
}
#endif /* !FTC_INLINE */
/* Note that this function cannot fail. If we cannot re-size the
* buckets array appropriately, we simply degrade the hash table's
* performance!
*/
static void
ftc_cache_resize( FTC_Cache cache )
{
for (;;)
{
FTC_Node node, *pnode;
FT_UFast p = cache->p;
FT_UFast mask = cache->mask;
FT_UFast count = mask + p + 1; /* number of buckets */
/* do we need to shrink the buckets array? */
if ( cache->slack < 0 )
{
FTC_Node new_list = NULL;
/* try to expand the buckets array _before_ splitting
* the bucket lists
*/
if ( p >= mask )
{
FT_Memory memory = cache->memory;
FT_Error error;
/* if we can't expand the array, leave immediately */
if ( FT_RENEW_ARRAY( cache->buckets,
( mask + 1 ) * 2, ( mask + 1 ) * 4 ) )
break;
}
/* split a single bucket */
pnode = cache->buckets + p;
for (;;)
{
node = *pnode;
if ( node == NULL )
break;
if ( node->hash & ( mask + 1 ) )
{
*pnode = node->link;
node->link = new_list;
new_list = node;
}
else
pnode = &node->link;
}
cache->buckets[p + mask + 1] = new_list;
cache->slack += FTC_HASH_MAX_LOAD;
if ( p >= mask )
{
cache->mask = 2 * mask + 1;
cache->p = 0;
}
else
cache->p = p + 1;
}
/* do we need to expand the buckets array? */
else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
{
FT_UFast old_index = p + mask;
FTC_Node* pold;
if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
break;
if ( p == 0 )
{
FT_Memory memory = cache->memory;
FT_Error error;
/* if we can't shrink the array, leave immediately */
if ( FT_RENEW_ARRAY( cache->buckets,
( mask + 1 ) * 2, mask + 1 ) )
break;
cache->mask >>= 1;
p = cache->mask;
}
else
p--;
pnode = cache->buckets + p;
while ( *pnode )
pnode = &(*pnode)->link;
pold = cache->buckets + old_index;
*pnode = *pold;
*pold = NULL;
cache->slack -= FTC_HASH_MAX_LOAD;
cache->p = p;
}
/* otherwise, the hash table is balanced */
else
break;
}
}
/* remove a node from its cache's hash table */
static void
ftc_node_hash_unlink( FTC_Node node0,
FTC_Cache cache )
{
FTC_Node *pnode = FTC_NODE__TOP_FOR_HASH( cache, node0->hash );
for (;;)
{
FTC_Node node = *pnode;
if ( node == NULL )
{
FT_TRACE0(( "ftc_node_hash_unlink: unknown node\n" ));
return;
}
if ( node == node0 )
break;
pnode = &(*pnode)->link;
}
*pnode = node0->link;
node0->link = NULL;
cache->slack++;
ftc_cache_resize( cache );
}
/* add a node to the `top' of its cache's hash table */
static void
ftc_node_hash_link( FTC_Node node,
FTC_Cache cache )
{
FTC_Node *pnode = FTC_NODE__TOP_FOR_HASH( cache, node->hash );
node->link = *pnode;
*pnode = node;
cache->slack--;
ftc_cache_resize( cache );
}
/* remove a node from the cache manager */
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
FT_BASE_DEF( void )
#else
FT_LOCAL_DEF( void )
#endif
ftc_node_destroy( FTC_Node node,
FTC_Manager manager )
{
FTC_Cache cache;
#ifdef FT_DEBUG_ERROR
/* find node's cache */
if ( node->cache_index >= manager->num_caches )
{
FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
return;
}
#endif
cache = manager->caches[node->cache_index];
#ifdef FT_DEBUG_ERROR
if ( cache == NULL )
{
FT_TRACE0(( "ftc_node_destroy: invalid node handle\n" ));
return;
}
#endif
manager->cur_weight -= cache->clazz.node_weight( node, cache );
/* remove node from mru list */
ftc_node_mru_unlink( node, manager );
/* remove node from cache's hash table */
ftc_node_hash_unlink( node, cache );
/* now finalize it */
cache->clazz.node_free( node, cache );
#if 0
/* check, just in case of general corruption :-) */
if ( manager->num_nodes == 0 )
FT_TRACE0(( "ftc_node_destroy: invalid cache node count (%d)\n",
manager->num_nodes ));
#endif
}
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** ABSTRACT CACHE CLASS *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
FT_LOCAL_DEF( FT_Error )
FTC_Cache_Init( FTC_Cache cache )
{
return ftc_cache_init( cache );
}
FT_LOCAL_DEF( FT_Error )
ftc_cache_init( FTC_Cache cache )
{
FT_Memory memory = cache->memory;
FT_Error error;
cache->p = 0;
cache->mask = FTC_HASH_INITIAL_SIZE - 1;
cache->slack = FTC_HASH_INITIAL_SIZE * FTC_HASH_MAX_LOAD;
(void)FT_NEW_ARRAY( cache->buckets, FTC_HASH_INITIAL_SIZE * 2 );
return error;
}
static void
FTC_Cache_Clear( FTC_Cache cache )
{
if ( cache && cache->buckets )
{
FTC_Manager manager = cache->manager;
FT_UFast i;
FT_UFast count;
count = cache->p + cache->mask + 1;
for ( i = 0; i < count; i++ )
{
FTC_Node *pnode = cache->buckets + i, next, node = *pnode;
while ( node )
{
next = node->link;
node->link = NULL;
/* remove node from mru list */
ftc_node_mru_unlink( node, manager );
/* now finalize it */
manager->cur_weight -= cache->clazz.node_weight( node, cache );
cache->clazz.node_free( node, cache );
node = next;
}
cache->buckets[i] = NULL;
}
ftc_cache_resize( cache );
}
}
FT_LOCAL_DEF( void )
ftc_cache_done( FTC_Cache cache )
{
if ( cache->memory )
{
FT_Memory memory = cache->memory;
FTC_Cache_Clear( cache );
FT_FREE( cache->buckets );
cache->mask = 0;
cache->p = 0;
cache->slack = 0;
cache->memory = NULL;
}
}
FT_LOCAL_DEF( void )
FTC_Cache_Done( FTC_Cache cache )
{
ftc_cache_done( cache );
}
static void
ftc_cache_add( FTC_Cache cache,
FT_PtrDist hash,
FTC_Node node )
{
node->hash = hash;
node->cache_index = (FT_UInt16)cache->index;
node->ref_count = 0;
ftc_node_hash_link( node, cache );
ftc_node_mru_link( node, cache->manager );
{
FTC_Manager manager = cache->manager;
manager->cur_weight += cache->clazz.node_weight( node, cache );
if ( manager->cur_weight >= manager->max_weight )
{
node->ref_count++;
FTC_Manager_Compress( manager );
node->ref_count--;
}
}
}
FT_LOCAL_DEF( FT_Error )
FTC_Cache_NewNode( FTC_Cache cache,
FT_PtrDist hash,
FT_Pointer query,
FTC_Node *anode )
{
FT_Error error;
FTC_Node node;
/*
* We use the FTC_CACHE_TRYLOOP macros to support out-of-memory
* errors (OOM) correctly, i.e., by flushing the cache progressively
* in order to make more room.
*/
FTC_CACHE_TRYLOOP( cache )
{
error = cache->clazz.node_new( &node, query, cache );
}
FTC_CACHE_TRYLOOP_END( NULL );
if ( error )
node = NULL;
else
{
/* don't assume that the cache has the same number of buckets, since
* our allocation request might have triggered global cache flushing
*/
ftc_cache_add( cache, hash, node );
}
*anode = node;
return error;
}
#ifndef FTC_INLINE
FT_LOCAL_DEF( FT_Error )
FTC_Cache_Lookup( FTC_Cache cache,
FT_PtrDist hash,
FT_Pointer query,
FTC_Node *anode )
{
FTC_Node* bucket;
FTC_Node* pnode;
FTC_Node node;
FT_Error error = FTC_Err_Ok;
FT_Bool list_changed = FALSE;
FTC_Node_CompareFunc compare = cache->clazz.node_compare;
if ( cache == NULL || anode == NULL )
return FTC_Err_Invalid_Argument;
/* Go to the `top' node of the list sharing same masked hash */
bucket = pnode = FTC_NODE__TOP_FOR_HASH( cache, hash );
/* Lookup a node with exactly same hash and queried properties. */
/* NOTE: _nodcomp() may change the linked list to reduce memory. */
for (;;)
{
node = *pnode;
if ( node == NULL )
goto NewNode;
if ( node->hash == hash &&
compare( node, query, cache, &list_changed ) )
break;
pnode = &node->link;
}
if ( list_changed )
{
/* Update bucket by modified linked list */
bucket = pnode = FTC_NODE__TOP_FOR_HASH( cache, hash );
/* Update pnode by modified linked list */
while ( *pnode != node )
{
if ( *pnode == NULL )
{
FT_ERROR(( "FTC_Cache_Lookup: oops!!! node missing\n" ));
goto NewNode;
}
else
pnode = &((*pnode)->link);
}
}
/* Reorder the list to move the found node to the `top' */
if ( node != *bucket )
{
*pnode = node->link;
node->link = *bucket;
*bucket = node;
}
/* move to head of MRU list */
{
FTC_Manager manager = cache->manager;
if ( node != manager->nodes_list )
ftc_node_mru_up( node, manager );
}
*anode = node;
return error;
NewNode:
return FTC_Cache_NewNode( cache, hash, query, anode );
}
#endif /* !FTC_INLINE */
FT_LOCAL_DEF( void )
FTC_Cache_RemoveFaceID( FTC_Cache cache,
FTC_FaceID face_id )
{
FT_UFast i, count;
FTC_Manager manager = cache->manager;
FTC_Node frees = NULL;
count = cache->p + cache->mask + 1;
for ( i = 0; i < count; i++ )
{
FTC_Node* bucket = cache->buckets + i;
FTC_Node* pnode = bucket;
for ( ;; )
{
FTC_Node node = *pnode;
FT_Bool list_changed = FALSE;
if ( node == NULL )
break;
if ( cache->clazz.node_remove_faceid( node, face_id,
cache, &list_changed ) )
{
*pnode = node->link;
node->link = frees;
frees = node;
}
else
pnode = &node->link;
}
}
/* remove all nodes in the free list */
while ( frees )
{
FTC_Node node;
node = frees;
frees = node->link;
manager->cur_weight -= cache->clazz.node_weight( node, cache );
ftc_node_mru_unlink( node, manager );
cache->clazz.node_free( node, cache );
cache->slack++;
}
ftc_cache_resize( cache );
}
/* END */
|