summaryrefslogtreecommitdiff
path: root/src/cache/ftlru.c
blob: 4b912993a0d5fc8366034bab26f3c94ac1f31557 (plain)
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
/***************************************************************************/
/*                                                                         */
/*  ftlru.c                                                                */
/*                                                                         */
/*    Simple LRU list-cache (body).                                        */
/*                                                                         */
/*  Copyright 2000 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 <freetype/cache/ftlru.h>
#include <freetype/internal/ftobjs.h>
#include <freetype/internal/ftlist.h>


  static
  void  lru_build_free_list( FT_LruNode  nodes,
                             FT_UInt     count,
                             FT_List     free_list )
  {
    FT_LruNode  node  = nodes;
    FT_LruNode  limit = node + count;
    

    free_list->head = free_list->tail = 0;
    for ( ; node < limit; node++ )
      FT_List_Add( free_list, (FT_ListNode)node );
  }


  FT_EXPORT_FUNC( FT_Error )  FT_Lru_New( const FT_Lru_Class*  clazz,
                                          FT_UInt              max_elements,
                                          FT_Pointer           user_data,
                                          FT_Memory            memory,
                                          FT_Bool              pre_alloc,
                                          FT_Lru*              alru )
  {
    FT_Error  error;
    FT_Lru    lru;
    

    if ( !alru )
      return FT_Err_Invalid_Argument;

    *alru = 0;
    if ( !ALLOC( lru, sizeof ( *lru ) ) )
    {
      if ( pre_alloc )
      {
        /* allocate static array of lru list nodes */
        if ( ALLOC_ARRAY( lru->nodes, max_elements, FT_LruNodeRec ) )
        {
          FREE( lru );
          goto Exit;
        }
        
        /* build the `free_nodes' list from the array */
        lru_build_free_list( lru->nodes, max_elements, &lru->free_nodes );
      }
      
      /* initialize common fields */
      lru->clazz        = (FT_Lru_Class*)clazz;
      lru->max_elements = max_elements;
      lru->memory       = memory;
      lru->user_data    = user_data;

      *alru = lru;
    }

  Exit:
    return error;
  }


                                         
  FT_EXPORT_DEF( void )  FT_Lru_Reset( FT_Lru  lru )
  {
    FT_ListNode    node;
    FT_Lru_Class*  clazz;
    FT_Memory      memory;
    

    if ( !lru )
      return;

    node   = lru->elements.head;
    clazz  = lru->clazz;
    memory = lru->memory;

    while ( node )
    {
      FT_ListNode  next = node->next;
      

      clazz->done_element( lru, (FT_LruNode)node );
      if ( !lru->nodes )
        FREE( node );
      
      node = next;
    }
    
    /* rebuild free list if necessary */
    if ( lru->nodes )
      lru_build_free_list( lru->nodes, lru->max_elements, &lru->free_nodes );

    lru->elements.head = lru->elements.tail = 0;
    lru->num_elements  = 0;
  }


  FT_EXPORT_DEF( void )  FT_Lru_Done( FT_Lru  lru )
  {
    FT_Memory  memory;
    

    if ( !lru )
      return;

    memory = lru->memory;

    FT_Lru_Reset( lru );
    FREE( lru );
  }


  FT_EXPORT_DEF( FT_Error )  FT_Lru_Lookup_Node( FT_Lru       lru,
                                                 FT_LruKey    key,
                                                 FT_LruNode*  anode )
  {
    FT_Error       error = 0;
    FT_ListNode    node;
    FT_Lru_Class*  clazz;
    FT_LruNode     found = 0; 
    FT_Memory      memory;
    

    if ( !lru || !key || !anode )
      return FT_Err_Invalid_Argument;

    node   = lru->elements.head;
    clazz  = lru->clazz;
    memory = lru->memory;

    if ( clazz->compare_element )
    {
      for ( ; node; node = node->next )
        if ( clazz->compare_element( (FT_LruNode)node, key ) )
        {
          found = (FT_LruNode)node;
          break;
        }
    }
    else
    {
      for ( ; node; node = node->next )
        if ( ((FT_LruNode)node)->key == key )
        {
          found = (FT_LruNode)node;
          break;
        }
    }
   
    if ( !found )
    {
      /* we haven't found the relevant element.  We will now try */
      /* to create a new one.                                    */
      if ( lru->num_elements >= lru->max_elements )
      {
        /* this lru list is full; we will now flush */
        /* the oldest node                          */
        FT_LruNode  lru_node;
        
        
        node     = lru->elements.tail;
        lru_node = (FT_LruNode)node;
        found    = lru_node;
        
        if ( clazz->flush_element )
          error = clazz->flush_element( lru, lru_node, key );
        else
        {
          clazz->done_element( lru, lru_node );
          lru_node->key = key;
          node->data    = 0;
          error = clazz->init_element( lru, lru_node );
        }

        if ( !error )
        {
          /* now, move element to top of list */
          FT_List_Up( &lru->elements, node );
        }
        else
        {
          /* in case of error, the node must be discarded */
          FT_List_Remove( &lru->elements, node );
          lru->num_elements--;
        
          if ( lru->nodes )
            FT_List_Insert( &lru->free_nodes, node );
          else
            FREE( lru_node );
            
          found = 0;
        }
      }
      else
      { 
        FT_LruNode  lru_node;
        

        /* create a new lru list node, then the element for it */
        if ( lru->nodes )
        {
          node     = lru->free_nodes.head;
          lru_node = (FT_LruNode)node;
          lru_node->key = key;
          
          error = clazz->init_element( lru, lru_node );
          if ( error )
            goto Exit;
            
          FT_List_Remove( &lru->free_nodes, node );
        }
        else
        {
          if ( ALLOC( lru_node, sizeof ( *lru_node ) ) )
            goto Exit;
            
          lru_node->key = key;
          error = clazz->init_element( lru, lru_node );
          if ( error )
          {
            FREE( lru_node );
            goto Exit;
          }
        }
       
        found = lru_node; 
        node  = (FT_ListNode)lru_node;
        FT_List_Insert( &lru->elements, node );
        lru->num_elements++;
      }
    }
    
  Exit: 
    *anode = found;
    return error; 
  }


  FT_EXPORT_DEF( FT_Error )  FT_Lru_Lookup( FT_Lru       lru,
                                            FT_LruKey    key,
                                            FT_Pointer*  aobject )
  {
    FT_Error    error;
    FT_LruNode  node;
    

    /* check for valid `lru' and `key' delayed to FT_Lru_Lookup_Node() */

    if ( !aobject )
      return FT_Err_Invalid_Argument;

    *aobject = 0;
    error = FT_Lru_Lookup_Node( lru, key, &node );
    if ( !error )
      *aobject = node->root.data;
    
    return error;
  }


  FT_EXPORT_FUNC( void )  FT_Lru_Remove_Node( FT_Lru      lru,
                                              FT_LruNode  node )
  {
    if ( !lru || !node )
      return;

    if ( lru->num_elements > 0 )
    {
      FT_List_Remove( &lru->elements, (FT_ListNode)node );
      lru->clazz->done_element( lru, node );
      
      if ( lru->nodes )
        FT_List_Insert( &lru->free_nodes, (FT_ListNode)node );
      else
      {
        FT_Memory  memory = lru->memory;


        FREE( node );
      }

      lru->num_elements--;
    }
  }


  FT_EXPORT_FUNC( void )  FT_Lru_Remove_Selection( FT_Lru           lru,
                                                   FT_Lru_Selector  selector,
                                                   FT_Pointer       data )
  {
    if ( !lru || !selector )
      return;

    if ( lru->num_elements > 0 )
    {
      FT_ListNode  node = lru->elements.head;
      FT_ListNode  next;

      
      while ( node )
      {
        next = node->next;
        if ( selector( lru, (FT_LruNode)node, data ) )
        {
          /* remove this element from the list, and destroy it */
          FT_Lru_Remove_Node( lru, (FT_LruNode)node );
        }
        node = next;
      }
    }
  }


/* END */