Bumping manifests a=b2g-bump
[gecko.git] / modules / freetype2 / src / cache / ftcmru.c
blobdc8b4cc3970912dead7166f14110a5baeab8de64
1 /***************************************************************************/
2 /* */
3 /* ftcmru.c */
4 /* */
5 /* FreeType MRU support (body). */
6 /* */
7 /* Copyright 2003, 2004, 2006, 2009 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 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_CACHE_H
21 #include "ftcmru.h"
22 #include FT_INTERNAL_OBJECTS_H
23 #include FT_INTERNAL_DEBUG_H
25 #include "ftcerror.h"
28 FT_LOCAL_DEF( void )
29 FTC_MruNode_Prepend( FTC_MruNode *plist,
30 FTC_MruNode node )
32 FTC_MruNode first = *plist;
35 if ( first )
37 FTC_MruNode last = first->prev;
40 #ifdef FT_DEBUG_ERROR
42 FTC_MruNode cnode = first;
47 if ( cnode == node )
49 fprintf( stderr, "FTC_MruNode_Prepend: invalid action\n" );
50 exit( 2 );
52 cnode = cnode->next;
54 } while ( cnode != first );
56 #endif
58 first->prev = node;
59 last->next = node;
60 node->next = first;
61 node->prev = last;
63 else
65 node->next = node;
66 node->prev = node;
68 *plist = node;
72 FT_LOCAL_DEF( void )
73 FTC_MruNode_Up( FTC_MruNode *plist,
74 FTC_MruNode node )
76 FTC_MruNode first = *plist;
79 FT_ASSERT( first != NULL );
81 if ( first != node )
83 FTC_MruNode prev, next, last;
86 #ifdef FT_DEBUG_ERROR
88 FTC_MruNode cnode = first;
91 if ( cnode == node )
92 goto Ok;
93 cnode = cnode->next;
95 } while ( cnode != first );
97 fprintf( stderr, "FTC_MruNode_Up: invalid action\n" );
98 exit( 2 );
99 Ok:
101 #endif
102 prev = node->prev;
103 next = node->next;
105 prev->next = next;
106 next->prev = prev;
108 last = first->prev;
110 last->next = node;
111 first->prev = node;
113 node->next = first;
114 node->prev = last;
116 *plist = node;
121 FT_LOCAL_DEF( void )
122 FTC_MruNode_Remove( FTC_MruNode *plist,
123 FTC_MruNode node )
125 FTC_MruNode first = *plist;
126 FTC_MruNode prev, next;
129 FT_ASSERT( first != NULL );
131 #ifdef FT_DEBUG_ERROR
133 FTC_MruNode cnode = first;
138 if ( cnode == node )
139 goto Ok;
140 cnode = cnode->next;
142 } while ( cnode != first );
144 fprintf( stderr, "FTC_MruNode_Remove: invalid action\n" );
145 exit( 2 );
148 #endif
150 prev = node->prev;
151 next = node->next;
153 prev->next = next;
154 next->prev = prev;
156 if ( node == next )
158 FT_ASSERT( first == node );
159 FT_ASSERT( prev == node );
161 *plist = NULL;
163 else if ( node == first )
164 *plist = next;
168 FT_LOCAL_DEF( void )
169 FTC_MruList_Init( FTC_MruList list,
170 FTC_MruListClass clazz,
171 FT_UInt max_nodes,
172 FT_Pointer data,
173 FT_Memory memory )
175 list->num_nodes = 0;
176 list->max_nodes = max_nodes;
177 list->nodes = NULL;
178 list->clazz = *clazz;
179 list->data = data;
180 list->memory = memory;
184 FT_LOCAL_DEF( void )
185 FTC_MruList_Reset( FTC_MruList list )
187 while ( list->nodes )
188 FTC_MruList_Remove( list, list->nodes );
190 FT_ASSERT( list->num_nodes == 0 );
194 FT_LOCAL_DEF( void )
195 FTC_MruList_Done( FTC_MruList list )
197 FTC_MruList_Reset( list );
201 #ifndef FTC_INLINE
202 FT_LOCAL_DEF( FTC_MruNode )
203 FTC_MruList_Find( FTC_MruList list,
204 FT_Pointer key )
206 FTC_MruNode_CompareFunc compare = list->clazz.node_compare;
207 FTC_MruNode first, node;
210 first = list->nodes;
211 node = NULL;
213 if ( first )
215 node = first;
218 if ( compare( node, key ) )
220 if ( node != first )
221 FTC_MruNode_Up( &list->nodes, node );
223 return node;
226 node = node->next;
228 } while ( node != first);
231 return NULL;
233 #endif
235 FT_LOCAL_DEF( FT_Error )
236 FTC_MruList_New( FTC_MruList list,
237 FT_Pointer key,
238 FTC_MruNode *anode )
240 FT_Error error;
241 FTC_MruNode node = NULL;
242 FT_Memory memory = list->memory;
245 if ( list->num_nodes >= list->max_nodes && list->max_nodes > 0 )
247 node = list->nodes->prev;
249 FT_ASSERT( node );
251 if ( list->clazz.node_reset )
253 FTC_MruNode_Up( &list->nodes, node );
255 error = list->clazz.node_reset( node, key, list->data );
256 if ( !error )
257 goto Exit;
260 FTC_MruNode_Remove( &list->nodes, node );
261 list->num_nodes--;
263 if ( list->clazz.node_done )
264 list->clazz.node_done( node, list->data );
266 else if ( FT_ALLOC( node, list->clazz.node_size ) )
267 goto Exit;
269 error = list->clazz.node_init( node, key, list->data );
270 if ( error )
271 goto Fail;
273 FTC_MruNode_Prepend( &list->nodes, node );
274 list->num_nodes++;
276 Exit:
277 *anode = node;
278 return error;
280 Fail:
281 if ( list->clazz.node_done )
282 list->clazz.node_done( node, list->data );
284 FT_FREE( node );
285 goto Exit;
289 #ifndef FTC_INLINE
290 FT_LOCAL_DEF( FT_Error )
291 FTC_MruList_Lookup( FTC_MruList list,
292 FT_Pointer key,
293 FTC_MruNode *anode )
295 FTC_MruNode node;
298 node = FTC_MruList_Find( list, key );
299 if ( node == NULL )
300 return FTC_MruList_New( list, key, anode );
302 *anode = node;
303 return 0;
305 #endif /* FTC_INLINE */
307 FT_LOCAL_DEF( void )
308 FTC_MruList_Remove( FTC_MruList list,
309 FTC_MruNode node )
311 FTC_MruNode_Remove( &list->nodes, node );
312 list->num_nodes--;
315 FT_Memory memory = list->memory;
318 if ( list->clazz.node_done )
319 list->clazz.node_done( node, list->data );
321 FT_FREE( node );
326 FT_LOCAL_DEF( void )
327 FTC_MruList_RemoveSelection( FTC_MruList list,
328 FTC_MruNode_CompareFunc selection,
329 FT_Pointer key )
331 FTC_MruNode first, node, next;
334 first = list->nodes;
335 while ( first && ( selection == NULL || selection( first, key ) ) )
337 FTC_MruList_Remove( list, first );
338 first = list->nodes;
341 if ( first )
343 node = first->next;
344 while ( node != first )
346 next = node->next;
348 if ( selection( node, key ) )
349 FTC_MruList_Remove( list, node );
351 node = next;
357 /* END */