summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/freetype/src/cache/ftccache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdparty/freetype/src/cache/ftccache.c')
-rw-r--r--src/3rdparty/freetype/src/cache/ftccache.c149
1 files changed, 64 insertions, 85 deletions
diff --git a/src/3rdparty/freetype/src/cache/ftccache.c b/src/3rdparty/freetype/src/cache/ftccache.c
index 1c8e0f3221..e0698557b7 100644
--- a/src/3rdparty/freetype/src/cache/ftccache.c
+++ b/src/3rdparty/freetype/src/cache/ftccache.c
@@ -4,7 +4,7 @@
*
* The FreeType internal cache interface (body).
*
- * Copyright (C) 2000-2020 by
+ * Copyright (C) 2000-2023 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
@@ -90,15 +90,14 @@
ftc_get_top_node_for_hash( FTC_Cache cache,
FT_Offset hash )
{
- FTC_Node* pnode;
FT_Offset idx;
idx = hash & cache->mask;
- if ( idx < cache->p )
- idx = hash & ( 2 * cache->mask + 1 );
- pnode = cache->buckets + idx;
- return pnode;
+ if ( idx >= cache->p )
+ idx = hash & ( cache->mask >> 1 );
+
+ return cache->buckets + idx;
}
#endif /* !FTC_INLINE */
@@ -114,12 +113,12 @@
for (;;)
{
FTC_Node node, *pnode;
- FT_UFast p = cache->p;
- FT_UFast mask = cache->mask;
- FT_UFast count = mask + p + 1; /* number of buckets */
+ FT_UFast p = cache->p;
+ FT_UFast size = cache->mask + 1; /* available size */
+ FT_UFast half = size >> 1;
- /* do we need to shrink the buckets array? */
+ /* do we need to expand the buckets array? */
if ( cache->slack < 0 )
{
FTC_Node new_list = NULL;
@@ -128,20 +127,22 @@
/* try to expand the buckets array _before_ splitting
* the bucket lists
*/
- if ( p >= mask )
+ if ( p == size )
{
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 ) )
+ if ( FT_QRENEW_ARRAY( cache->buckets, size, size * 2 ) )
break;
+
+ cache->mask = 2 * size - 1;
+ half = size;
}
- /* split a single bucket */
- pnode = cache->buckets + p;
+ /* the bucket to split */
+ pnode = cache->buckets + p - half;
for (;;)
{
@@ -149,7 +150,7 @@
if ( !node )
break;
- if ( node->hash & ( mask + 1 ) )
+ if ( node->hash & half )
{
*pnode = node->link;
node->link = new_list;
@@ -159,56 +160,50 @@
pnode = &node->link;
}
- cache->buckets[p + mask + 1] = new_list;
+ cache->buckets[p] = new_list;
cache->slack += FTC_HASH_MAX_LOAD;
+ cache->p = p + 1;
- if ( p >= mask )
- {
- cache->mask = 2 * mask + 1;
- cache->p = 0;
- }
- else
- cache->p = p + 1;
+ FT_TRACE2(( "ftc_cache_resize: cache %u increased to %u hashes\n",
+ cache->index, cache->p ));
}
- /* do we need to expand the buckets array? */
- else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
+ /* do we need to shrink the buckets array? */
+ else if ( cache->slack > (FT_Long)p * FTC_HASH_SUB_LOAD )
{
- FT_UFast old_index = p + mask;
- FTC_Node* pold;
+ FTC_Node old_list = cache->buckets[--p];
- if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
+ if ( p < FTC_HASH_INITIAL_SIZE )
break;
- if ( p == 0 )
+ if ( p == half )
{
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 ) )
+ if ( FT_QRENEW_ARRAY( cache->buckets, size, half ) )
break;
- cache->mask >>= 1;
- p = cache->mask;
+ cache->mask = half - 1;
}
- else
- p--;
- pnode = cache->buckets + p;
+ /* the bucket to merge */
+ pnode = cache->buckets + p - half;
+
while ( *pnode )
pnode = &(*pnode)->link;
- pold = cache->buckets + old_index;
- *pnode = *pold;
- *pold = NULL;
+ *pnode = old_list;
cache->slack -= FTC_HASH_MAX_LOAD;
cache->p = p;
+
+ FT_TRACE2(( "ftc_cache_resize: cache %u decreased to %u hashes\n",
+ cache->index, cache->p ));
}
/* otherwise, the hash table is balanced */
@@ -240,7 +235,7 @@
if ( node == node0 )
break;
- pnode = &(*pnode)->link;
+ pnode = &node->link;
}
*pnode = node0->link;
@@ -308,7 +303,7 @@
#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",
+ FT_TRACE0(( "ftc_node_destroy: invalid cache node count (%u)\n",
manager->num_nodes ));
#endif
}
@@ -324,43 +319,44 @@
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->p = FTC_HASH_INITIAL_SIZE;
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 );
+ FT_MEM_NEW_ARRAY( cache->buckets, FTC_HASH_INITIAL_SIZE );
return error;
}
- static void
- FTC_Cache_Clear( FTC_Cache cache )
+ FT_LOCAL_DEF( FT_Error )
+ FTC_Cache_Init( FTC_Cache cache )
+ {
+ return ftc_cache_init( cache );
+ }
+
+
+ FT_LOCAL_DEF( void )
+ ftc_cache_done( FTC_Cache cache )
{
- if ( cache && cache->buckets )
+ FT_Memory memory = cache->memory;
+
+
+ if ( cache->buckets )
{
FTC_Manager manager = cache->manager;
+ FT_UFast count = cache->p;
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;
+ FTC_Node node = cache->buckets[i], next;
while ( node )
@@ -377,30 +373,14 @@
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;
+ FT_FREE( cache->buckets );
- cache->memory = NULL;
- }
+ cache->p = 0;
+ cache->mask = 0;
+ cache->slack = 0;
}
@@ -417,7 +397,7 @@
FTC_Node node )
{
node->hash = hash;
- node->cache_index = (FT_UInt16)cache->index;
+ node->cache_index = (FT_UShort)cache->index;
node->ref_count = 0;
ftc_node_hash_link( node, cache );
@@ -459,7 +439,7 @@
{
error = cache->clazz.node_new( &node, query, cache );
}
- FTC_CACHE_TRYLOOP_END( NULL );
+ FTC_CACHE_TRYLOOP_END( NULL )
if ( error )
node = NULL;
@@ -528,7 +508,7 @@
goto NewNode;
}
else
- pnode = &((*pnode)->link);
+ pnode = &(*pnode)->link;
}
}
@@ -563,16 +543,15 @@
FTC_Cache_RemoveFaceID( FTC_Cache cache,
FTC_FaceID face_id )
{
- FT_UFast i, count;
FTC_Manager manager = cache->manager;
FTC_Node frees = NULL;
+ FT_UFast count = cache->p;
+ FT_UFast i;
- count = cache->p + cache->mask + 1;
for ( i = 0; i < count; i++ )
{
- FTC_Node* bucket = cache->buckets + i;
- FTC_Node* pnode = bucket;
+ FTC_Node* pnode = cache->buckets + i;
for (;;)