Imported Upstream version 2.13.2
[platform/upstream/freetype2.git] / src / cache / ftcmanag.c
index 436d41f..94f8469 100644 (file)
@@ -4,7 +4,7 @@
  *
  *   FreeType Cache Manager (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,
   {
     FT_Error     error;
     FT_Memory    memory;
-    FTC_Manager  manager = 0;
+    FTC_Manager  manager = NULL;
 
 
     if ( !library )
 
     memory = library->memory;
 
-    if ( FT_NEW( manager ) )
+    if ( FT_QNEW( manager ) )
       goto Exit;
 
     if ( max_faces == 0 )
     manager->library      = library;
     manager->memory       = memory;
     manager->max_weight   = max_bytes;
+    manager->cur_weight   = 0;
 
     manager->request_face = requester;
     manager->request_data = req_data;
                       manager,
                       memory );
 
+    manager->nodes_list = NULL;
+    manager->num_nodes  = 0;
+    manager->num_caches = 0;
+
     *amanager = manager;
 
   Exit:
     memory = manager->memory;
 
     /* now discard all caches */
-    for (idx = manager->num_caches; idx-- > 0; )
+    for ( idx = manager->num_caches; idx-- > 0; )
     {
       FTC_Cache  cache = manager->caches[idx];
 
         FTC_Cache  cache = manager->caches[node->cache_index];
 
 
-        if ( (FT_UInt)node->cache_index >= manager->num_caches )
-          FT_TRACE0(( "FTC_Manager_Check: invalid node (cache index = %ld\n",
+        if ( node->cache_index >= manager->num_caches )
+          FT_TRACE0(( "FTC_Manager_Check: invalid node (cache index = %hu\n",
                       node->cache_index ));
         else
           weight += cache->clazz.node_weight( node, cache );
 
       if ( count != manager->num_nodes )
         FT_TRACE0(( "FTC_Manager_Check:"
-                    " invalid cache node count %d instead of %d\n",
+                    " invalid cache node count %u instead of %u\n",
                     manager->num_nodes, count ));
     }
   }
   FT_LOCAL_DEF( void )
   FTC_Manager_Compress( FTC_Manager  manager )
   {
-    FTC_Node   node, first;
+    FTC_Node   node, prev, first;
 
 
     if ( !manager )
 #ifdef FT_DEBUG_ERROR
     FTC_Manager_Check( manager );
 
-    FT_TRACE0(( "compressing, weight = %ld, max = %ld, nodes = %d\n",
+    FT_TRACE0(( "compressing, weight = %ld, max = %ld, nodes = %u\n",
                 manager->cur_weight, manager->max_weight,
                 manager->num_nodes ));
 #endif
       return;
 
     /* go to last node -- it's a circular list */
-    node = FTC_NODE_PREV( first );
+    prev = FTC_NODE_PREV( first );
     do
     {
-      FTC_Node  prev;
-
-
-      prev = ( node == first ) ? NULL : FTC_NODE_PREV( node );
+      node = prev;
+      prev = FTC_NODE_PREV( node );
 
       if ( node->ref_count <= 0 )
         ftc_node_destroy( node, manager );
 
-      node = prev;
-
-    } while ( node && manager->cur_weight > manager->max_weight );
+    } while ( node != first && manager->cur_weight > manager->max_weight );
   }
 
 
         goto Exit;
       }
 
-      if ( !FT_ALLOC( cache, clazz->cache_size ) )
+      if ( !FT_QALLOC( cache, clazz->cache_size ) )
       {
         cache->manager   = manager;
         cache->memory    = memory;
                       FT_UInt      count )
   {
     FTC_Node  first = manager->nodes_list;
-    FTC_Node  node;
-    FT_UInt   result;
+    FTC_Node  prev, node;
+    FT_UInt   result = 0;
 
 
     /* try to remove `count' nodes from the list */
-    if ( !first )  /* empty list! */
-      return 0;
+    if ( !first || !count )
+      return result;
 
-    /* go to last node - it's a circular list */
-    node = FTC_NODE_PREV(first);
-    for ( result = 0; result < count; )
+    /* go to last node -- it's a circular list */
+    prev = FTC_NODE_PREV( first );
+    do
     {
-      FTC_Node  prev = FTC_NODE_PREV( node );
-
+      node = prev;
+      prev = FTC_NODE_PREV( node );
 
       /* don't touch locked nodes */
       if ( node->ref_count <= 0 )
         ftc_node_destroy( node, manager );
         result++;
       }
+    } while ( node != first && result < count );
 
-      if ( node == first )
-        break;
-
-      node = prev;
-    }
-    return  result;
+    return result;
   }
 
 
   FTC_Node_Unref( FTC_Node     node,
                   FTC_Manager  manager )
   {
-    if ( node                                             &&
-         manager                                          &&
-         (FT_UInt)node->cache_index < manager->num_caches )
+    if ( node                                    &&
+         manager                                 &&
+         node->cache_index < manager->num_caches )
       node->ref_count--;
   }