Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 1 | /***************************************************************************/ |
| 2 | /* */ |
| 3 | /* ftlru.c */ |
| 4 | /* */ |
| 5 | /* XXX */ |
| 6 | /* */ |
| 7 | /* Copyright 2000 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 | /***************************************************************************/ |
| 17 | |
| 18 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 19 | #include <cache/ftlru.h> |
| 20 | #include <freetype/internal/ftobjs.h> |
| 21 | #include <freetype/internal/ftlist.h> |
| 22 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 23 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 24 | static |
| 25 | void lru_build_free_list( FT_LruNode nodes, |
| 26 | FT_UInt count, |
| 27 | FT_List free_list ) |
| 28 | { |
| 29 | FT_LruNode node = nodes; |
| 30 | FT_LruNode limit = node + count; |
| 31 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 32 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 33 | free_list->head = free_list->tail = 0; |
| 34 | for ( ; node < limit; node++ ) |
| 35 | FT_List_Add( free_list, (FT_ListNode)node ); |
| 36 | } |
| 37 | |
| 38 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 39 | FT_EXPORT_FUNC( FT_Error ) FT_Lru_New( const FT_Lru_Class* clazz, |
| 40 | FT_UInt max_elements, |
David Turner | 3b2c50e | 2000-08-23 21:11:13 +0000 | [diff] [blame] | 41 | FT_Pointer user_data, |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 42 | FT_Memory memory, |
| 43 | FT_Bool pre_alloc, |
| 44 | FT_Lru* alru ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 45 | { |
| 46 | FT_Error error; |
| 47 | FT_Lru lru; |
| 48 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 49 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 50 | *alru = 0; |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 51 | if ( !ALLOC( lru, sizeof ( *lru ) ) ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 52 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 53 | if ( pre_alloc ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 54 | { |
| 55 | /* allocate static array of lru list nodes */ |
| 56 | if ( ALLOC_ARRAY( lru->nodes, max_elements, FT_LruNodeRec ) ) |
| 57 | { |
| 58 | FREE( lru ); |
| 59 | goto Exit; |
| 60 | } |
| 61 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 62 | /* build the `free_nodes' list from the array */ |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 63 | lru_build_free_list( lru->nodes, max_elements, &lru->free_nodes ); |
| 64 | } |
| 65 | |
| 66 | /* initialize common fields */ |
| 67 | lru->clazz = (FT_Lru_Class*)clazz; |
| 68 | lru->max_elements = max_elements; |
| 69 | lru->memory = memory; |
David Turner | 3b2c50e | 2000-08-23 21:11:13 +0000 | [diff] [blame] | 70 | lru->user_data = user_data; |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 71 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 72 | *alru = lru; |
| 73 | } |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 74 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 75 | Exit: |
| 76 | return error; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 81 | FT_EXPORT_DEF( void ) FT_Lru_Reset( FT_Lru lru ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 82 | { |
| 83 | FT_ListNode node = lru->elements.head; |
| 84 | FT_Lru_Class* clazz = lru->clazz; |
| 85 | FT_Memory memory = lru->memory; |
| 86 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 87 | |
| 88 | while ( node ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 89 | { |
| 90 | FT_ListNode next = node->next; |
| 91 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 92 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 93 | clazz->done_element( lru, (FT_LruNode)node ); |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 94 | if ( !lru->nodes ) |
| 95 | FREE( node ); |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 96 | |
| 97 | node = next; |
| 98 | } |
| 99 | |
| 100 | /* rebuild free list if necessary */ |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 101 | if ( lru->nodes ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 102 | lru_build_free_list( lru->nodes, lru->max_elements, &lru->free_nodes ); |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 103 | |
| 104 | lru->elements.head = lru->elements.tail = 0; |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 105 | lru->num_elements = 0; |
| 106 | } |
| 107 | |
| 108 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 109 | FT_EXPORT_DEF( void ) FT_Lru_Done( FT_Lru lru ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 110 | { |
| 111 | FT_Memory memory = lru->memory; |
| 112 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 113 | |
| 114 | FT_Lru_Reset( lru ); |
| 115 | FREE( lru ); |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 119 | FT_EXPORT_DEF( FT_Error ) FT_Lru_Lookup_Node( FT_Lru lru, |
| 120 | FT_LruKey key, |
| 121 | FT_LruNode* anode ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 122 | { |
| 123 | FT_Error error = 0; |
| 124 | FT_ListNode node = lru->elements.head; |
| 125 | FT_Lru_Class* clazz = lru->clazz; |
| 126 | FT_LruNode found = 0; |
| 127 | FT_Memory memory = lru->memory; |
| 128 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 129 | |
| 130 | if ( clazz->compare_element ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 131 | { |
| 132 | for ( ; node; node = node->next ) |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 133 | if ( clazz->compare_element( (FT_LruNode)node, key ) ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 134 | { |
| 135 | found = (FT_LruNode)node; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | for ( ; node; node = node->next ) |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 142 | if ( ((FT_LruNode)node)->key == key ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 143 | { |
| 144 | found = (FT_LruNode)node; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 149 | if ( !found ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 150 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 151 | /* we haven't found the relevant element. We will now try */ |
| 152 | /* to create a new one. */ |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 153 | if ( lru->num_elements >= lru->max_elements ) |
| 154 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 155 | /* this lru list is full; we will now flush */ |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 156 | /* the oldest node */ |
| 157 | FT_LruNode lru_node; |
| 158 | |
| 159 | |
| 160 | node = lru->elements.tail; |
| 161 | lru_node = (FT_LruNode)node; |
David Turner | 98d2701 | 2000-08-24 11:53:35 +0000 | [diff] [blame^] | 162 | found = lru_node; |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 163 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 164 | if ( clazz->flush_element ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 165 | error = clazz->flush_element( lru, lru_node, key ); |
| 166 | else |
| 167 | { |
| 168 | clazz->done_element( lru, lru_node ); |
| 169 | lru_node->key = key; |
| 170 | node->data = 0; |
| 171 | error = clazz->init_element( lru, lru_node ); |
| 172 | } |
| 173 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 174 | if ( !error ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 175 | { |
| 176 | /* now, move element to top of list */ |
| 177 | FT_List_Up( &lru->elements, node ); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | /* in case of error, the node must be discarded */ |
| 182 | FT_List_Remove( &lru->elements, node ); |
| 183 | lru->num_elements--; |
| 184 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 185 | if ( lru->nodes ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 186 | FT_List_Insert( &lru->free_nodes, node ); |
| 187 | else |
| 188 | FREE( lru_node ); |
David Turner | 98d2701 | 2000-08-24 11:53:35 +0000 | [diff] [blame^] | 189 | |
| 190 | found = 0; |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | FT_LruNode lru_node; |
| 196 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 197 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 198 | /* create a new lru list node, then the element for it */ |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 199 | if ( lru->nodes ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 200 | { |
| 201 | node = lru->free_nodes.head; |
| 202 | lru_node = (FT_LruNode)node; |
| 203 | lru_node->key = key; |
| 204 | |
| 205 | error = clazz->init_element( lru, lru_node ); |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 206 | if ( error ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 207 | goto Exit; |
| 208 | |
| 209 | FT_List_Remove( &lru->free_nodes, node ); |
| 210 | } |
| 211 | else |
| 212 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 213 | if ( ALLOC( lru_node, sizeof ( *lru_node ) ) ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 214 | goto Exit; |
| 215 | |
| 216 | lru_node->key = key; |
| 217 | error = clazz->init_element( lru, lru_node ); |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 218 | if ( error ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 219 | { |
| 220 | FREE( lru_node ); |
| 221 | goto Exit; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | found = lru_node; |
| 226 | node = (FT_ListNode)lru_node; |
| 227 | FT_List_Insert( &lru->elements, node ); |
| 228 | lru->num_elements++; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | Exit: |
| 233 | *anode = found; |
| 234 | return error; |
| 235 | } |
| 236 | |
| 237 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 238 | FT_EXPORT_DEF( FT_Error ) FT_Lru_Lookup( FT_Lru lru, |
| 239 | FT_LruKey key, |
| 240 | FT_Pointer* aobject ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 241 | { |
| 242 | FT_Error error; |
| 243 | FT_LruNode node; |
| 244 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 245 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 246 | *aobject = 0; |
| 247 | error = FT_Lru_Lookup_Node( lru, key, &node ); |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 248 | if ( !error ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 249 | *aobject = node->root.data; |
| 250 | |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 255 | FT_EXPORT_FUNC( void ) FT_Lru_Remove_Node( FT_Lru lru, |
| 256 | FT_LruNode node ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 257 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 258 | if ( lru->num_elements > 0 ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 259 | { |
| 260 | FT_List_Remove( &lru->elements, (FT_ListNode)node ); |
| 261 | lru->clazz->done_element( lru, node ); |
| 262 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 263 | if ( lru->nodes ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 264 | FT_List_Insert( &lru->free_nodes, (FT_ListNode)node ); |
| 265 | else |
| 266 | { |
| 267 | FT_Memory memory = lru->memory; |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | FREE( node ); |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 271 | } |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 272 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 273 | lru->num_elements--; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 278 | FT_EXPORT_FUNC( void ) FT_Lru_Remove_Selection( FT_Lru lru, |
| 279 | FT_Lru_Selector selector, |
| 280 | FT_Pointer data ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 281 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 282 | if ( lru->num_elements > 0 ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 283 | { |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 284 | FT_ListNode node = lru->elements.head; |
| 285 | FT_ListNode next; |
| 286 | |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 287 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 288 | while ( node ) |
David Turner | b466a76 | 2000-08-23 11:22:30 +0000 | [diff] [blame] | 289 | { |
| 290 | next = node->next; |
| 291 | if ( selector( lru, (FT_LruNode)node, data ) ) |
| 292 | { |
| 293 | /* remove this element from the list, and destroy it */ |
| 294 | FT_Lru_Remove_Node( lru, (FT_LruNode)node ); |
| 295 | } |
| 296 | node = next; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Werner Lemberg | 8728f29 | 2000-08-23 17:32:42 +0000 | [diff] [blame] | 301 | |
| 302 | /* END */ |