1*37da2899SCharles.Forsyth /***************************************************************************/ 2*37da2899SCharles.Forsyth /* */ 3*37da2899SCharles.Forsyth /* ftlru.h */ 4*37da2899SCharles.Forsyth /* */ 5*37da2899SCharles.Forsyth /* Simple LRU list-cache (specification). */ 6*37da2899SCharles.Forsyth /* */ 7*37da2899SCharles.Forsyth /* Copyright 2000-2001 by */ 8*37da2899SCharles.Forsyth /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9*37da2899SCharles.Forsyth /* */ 10*37da2899SCharles.Forsyth /* This file is part of the FreeType project, and may only be used, */ 11*37da2899SCharles.Forsyth /* modified, and distributed under the terms of the FreeType project */ 12*37da2899SCharles.Forsyth /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13*37da2899SCharles.Forsyth /* this file you indicate that you have read the license and */ 14*37da2899SCharles.Forsyth /* understand and accept it fully. */ 15*37da2899SCharles.Forsyth /* */ 16*37da2899SCharles.Forsyth /***************************************************************************/ 17*37da2899SCharles.Forsyth 18*37da2899SCharles.Forsyth 19*37da2899SCharles.Forsyth /*************************************************************************/ 20*37da2899SCharles.Forsyth /* */ 21*37da2899SCharles.Forsyth /* An LRU is a list that cannot hold more than a certain number of */ 22*37da2899SCharles.Forsyth /* elements (`max_elements'). All elements in the list are sorted in */ 23*37da2899SCharles.Forsyth /* least-recently-used order, i.e., the `oldest' element is at the tail */ 24*37da2899SCharles.Forsyth /* of the list. */ 25*37da2899SCharles.Forsyth /* */ 26*37da2899SCharles.Forsyth /* When doing a lookup (either through `Lookup()' or `Lookup_Node()'), */ 27*37da2899SCharles.Forsyth /* the list is searched for an element with the corresponding key. If */ 28*37da2899SCharles.Forsyth /* it is found, the element is moved to the head of the list and is */ 29*37da2899SCharles.Forsyth /* returned. */ 30*37da2899SCharles.Forsyth /* */ 31*37da2899SCharles.Forsyth /* If no corresponding element is found, the lookup routine will try to */ 32*37da2899SCharles.Forsyth /* obtain a new element with the relevant key. If the list is already */ 33*37da2899SCharles.Forsyth /* full, the oldest element from the list is discarded and replaced by a */ 34*37da2899SCharles.Forsyth /* new one; a new element is added to the list otherwise. */ 35*37da2899SCharles.Forsyth /* */ 36*37da2899SCharles.Forsyth /* Note that it is possible to pre-allocate the element list nodes. */ 37*37da2899SCharles.Forsyth /* This is handy if `max_elements' is sufficiently small, as it saves */ 38*37da2899SCharles.Forsyth /* allocations/releases during the lookup process. */ 39*37da2899SCharles.Forsyth /* */ 40*37da2899SCharles.Forsyth /*************************************************************************/ 41*37da2899SCharles.Forsyth 42*37da2899SCharles.Forsyth 43*37da2899SCharles.Forsyth /*************************************************************************/ 44*37da2899SCharles.Forsyth /*************************************************************************/ 45*37da2899SCharles.Forsyth /*************************************************************************/ 46*37da2899SCharles.Forsyth /*************************************************************************/ 47*37da2899SCharles.Forsyth /*************************************************************************/ 48*37da2899SCharles.Forsyth /********* *********/ 49*37da2899SCharles.Forsyth /********* WARNING, THIS IS BETA CODE. *********/ 50*37da2899SCharles.Forsyth /********* *********/ 51*37da2899SCharles.Forsyth /*************************************************************************/ 52*37da2899SCharles.Forsyth /*************************************************************************/ 53*37da2899SCharles.Forsyth /*************************************************************************/ 54*37da2899SCharles.Forsyth /*************************************************************************/ 55*37da2899SCharles.Forsyth /*************************************************************************/ 56*37da2899SCharles.Forsyth 57*37da2899SCharles.Forsyth 58*37da2899SCharles.Forsyth #ifndef __FTLRU_H__ 59*37da2899SCharles.Forsyth #define __FTLRU_H__ 60*37da2899SCharles.Forsyth 61*37da2899SCharles.Forsyth 62*37da2899SCharles.Forsyth #include <ft2build.h> 63*37da2899SCharles.Forsyth #include FT_FREETYPE_H 64*37da2899SCharles.Forsyth 65*37da2899SCharles.Forsyth 66*37da2899SCharles.Forsyth FT_BEGIN_HEADER 67*37da2899SCharles.Forsyth 68*37da2899SCharles.Forsyth 69*37da2899SCharles.Forsyth /* generic list key type */ 70*37da2899SCharles.Forsyth typedef FT_Pointer FT_LruKey; 71*37da2899SCharles.Forsyth 72*37da2899SCharles.Forsyth /* a list list handle */ 73*37da2899SCharles.Forsyth typedef struct FT_LruListRec_* FT_LruList; 74*37da2899SCharles.Forsyth 75*37da2899SCharles.Forsyth /* a list class handle */ 76*37da2899SCharles.Forsyth typedef const struct FT_LruList_ClassRec_* FT_LruList_Class; 77*37da2899SCharles.Forsyth 78*37da2899SCharles.Forsyth /* a list node handle */ 79*37da2899SCharles.Forsyth typedef struct FT_LruNodeRec_* FT_LruNode; 80*37da2899SCharles.Forsyth 81*37da2899SCharles.Forsyth /* the list node structure */ 82*37da2899SCharles.Forsyth typedef struct FT_LruNodeRec_ 83*37da2899SCharles.Forsyth { 84*37da2899SCharles.Forsyth FT_LruNode next; 85*37da2899SCharles.Forsyth FT_LruKey key; 86*37da2899SCharles.Forsyth 87*37da2899SCharles.Forsyth } FT_LruNodeRec; 88*37da2899SCharles.Forsyth 89*37da2899SCharles.Forsyth 90*37da2899SCharles.Forsyth /* the list structure */ 91*37da2899SCharles.Forsyth typedef struct FT_LruListRec_ 92*37da2899SCharles.Forsyth { 93*37da2899SCharles.Forsyth FT_Memory memory; 94*37da2899SCharles.Forsyth FT_LruList_Class clazz; 95*37da2899SCharles.Forsyth FT_LruNode nodes; 96*37da2899SCharles.Forsyth FT_UInt max_nodes; 97*37da2899SCharles.Forsyth FT_UInt num_nodes; 98*37da2899SCharles.Forsyth FT_Pointer data; 99*37da2899SCharles.Forsyth 100*37da2899SCharles.Forsyth } FT_LruListRec; 101*37da2899SCharles.Forsyth 102*37da2899SCharles.Forsyth 103*37da2899SCharles.Forsyth /* initialize a list list */ 104*37da2899SCharles.Forsyth typedef FT_Error 105*37da2899SCharles.Forsyth (*FT_LruList_InitFunc)( FT_LruList list ); 106*37da2899SCharles.Forsyth 107*37da2899SCharles.Forsyth /* finalize a list list */ 108*37da2899SCharles.Forsyth typedef void 109*37da2899SCharles.Forsyth (*FT_LruList_DoneFunc)( FT_LruList list ); 110*37da2899SCharles.Forsyth 111*37da2899SCharles.Forsyth /* this method is used to initialize a new list element node */ 112*37da2899SCharles.Forsyth typedef FT_Error 113*37da2899SCharles.Forsyth (*FT_LruNode_InitFunc)( FT_LruNode node, 114*37da2899SCharles.Forsyth FT_LruKey key, 115*37da2899SCharles.Forsyth FT_Pointer data ); 116*37da2899SCharles.Forsyth 117*37da2899SCharles.Forsyth /* this method is used to finalize a given list element node */ 118*37da2899SCharles.Forsyth typedef void 119*37da2899SCharles.Forsyth (*FT_LruNode_DoneFunc)( FT_LruNode node, 120*37da2899SCharles.Forsyth FT_Pointer data ); 121*37da2899SCharles.Forsyth 122*37da2899SCharles.Forsyth /* If defined, this method is called when the list if full */ 123*37da2899SCharles.Forsyth /* during the lookup process -- it is used to change the contents */ 124*37da2899SCharles.Forsyth /* of a list element node instead of calling `done_element()', */ 125*37da2899SCharles.Forsyth /* then `init_element()'. Set it to 0 for default behaviour. */ 126*37da2899SCharles.Forsyth typedef FT_Error 127*37da2899SCharles.Forsyth (*FT_LruNode_FlushFunc)( FT_LruNode node, 128*37da2899SCharles.Forsyth FT_LruKey new_key, 129*37da2899SCharles.Forsyth FT_Pointer data ); 130*37da2899SCharles.Forsyth 131*37da2899SCharles.Forsyth /* If defined, this method is used to compare a list element node */ 132*37da2899SCharles.Forsyth /* with a given key during a lookup. If set to 0, the `key' */ 133*37da2899SCharles.Forsyth /* fields will be directly compared instead. */ 134*37da2899SCharles.Forsyth typedef FT_Bool 135*37da2899SCharles.Forsyth (*FT_LruNode_CompareFunc)( FT_LruNode node, 136*37da2899SCharles.Forsyth FT_LruKey key, 137*37da2899SCharles.Forsyth FT_Pointer data ); 138*37da2899SCharles.Forsyth 139*37da2899SCharles.Forsyth /* A selector is used to indicate whether a given list element node */ 140*37da2899SCharles.Forsyth /* is part of a selection for FT_LruList_Remove_Selection(). The */ 141*37da2899SCharles.Forsyth /* functrion must return true (i.e., non-null) to indicate that the */ 142*37da2899SCharles.Forsyth /* node is part of it. */ 143*37da2899SCharles.Forsyth typedef FT_Bool 144*37da2899SCharles.Forsyth (*FT_LruNode_SelectFunc)( FT_LruNode node, 145*37da2899SCharles.Forsyth FT_Pointer data, 146*37da2899SCharles.Forsyth FT_Pointer list_data ); 147*37da2899SCharles.Forsyth 148*37da2899SCharles.Forsyth /* LRU class */ 149*37da2899SCharles.Forsyth typedef struct FT_LruList_ClassRec_ 150*37da2899SCharles.Forsyth { 151*37da2899SCharles.Forsyth FT_UInt list_size; 152*37da2899SCharles.Forsyth FT_LruList_InitFunc list_init; /* optional */ 153*37da2899SCharles.Forsyth FT_LruList_DoneFunc list_done; /* optional */ 154*37da2899SCharles.Forsyth 155*37da2899SCharles.Forsyth FT_UInt node_size; 156*37da2899SCharles.Forsyth FT_LruNode_InitFunc node_init; /* MANDATORY */ 157*37da2899SCharles.Forsyth FT_LruNode_DoneFunc node_done; /* optional */ 158*37da2899SCharles.Forsyth FT_LruNode_FlushFunc node_flush; /* optional */ 159*37da2899SCharles.Forsyth FT_LruNode_CompareFunc node_compare; /* optional */ 160*37da2899SCharles.Forsyth 161*37da2899SCharles.Forsyth } FT_LruList_ClassRec; 162*37da2899SCharles.Forsyth 163*37da2899SCharles.Forsyth 164*37da2899SCharles.Forsyth /* The following functions must be exported in the case where */ 165*37da2899SCharles.Forsyth /* applications would want to write their own cache classes. */ 166*37da2899SCharles.Forsyth 167*37da2899SCharles.Forsyth FT_EXPORT( FT_Error ) 168*37da2899SCharles.Forsyth FT_LruList_New( FT_LruList_Class clazz, 169*37da2899SCharles.Forsyth FT_UInt max_elements, 170*37da2899SCharles.Forsyth FT_Pointer user_data, 171*37da2899SCharles.Forsyth FT_Memory memory, 172*37da2899SCharles.Forsyth FT_LruList *alist ); 173*37da2899SCharles.Forsyth 174*37da2899SCharles.Forsyth FT_EXPORT( void ) 175*37da2899SCharles.Forsyth FT_LruList_Reset( FT_LruList list ); 176*37da2899SCharles.Forsyth 177*37da2899SCharles.Forsyth FT_EXPORT( void ) 178*37da2899SCharles.Forsyth FT_LruList_Destroy ( FT_LruList list ); 179*37da2899SCharles.Forsyth 180*37da2899SCharles.Forsyth FT_EXPORT( FT_Error ) 181*37da2899SCharles.Forsyth FT_LruList_Lookup( FT_LruList list, 182*37da2899SCharles.Forsyth FT_LruKey key, 183*37da2899SCharles.Forsyth FT_LruNode *anode ); 184*37da2899SCharles.Forsyth 185*37da2899SCharles.Forsyth FT_EXPORT( void ) 186*37da2899SCharles.Forsyth FT_LruList_Remove( FT_LruList list, 187*37da2899SCharles.Forsyth FT_LruNode node ); 188*37da2899SCharles.Forsyth 189*37da2899SCharles.Forsyth FT_EXPORT( void ) 190*37da2899SCharles.Forsyth FT_LruList_Remove_Selection( FT_LruList list, 191*37da2899SCharles.Forsyth FT_LruNode_SelectFunc select_func, 192*37da2899SCharles.Forsyth FT_Pointer select_data ); 193*37da2899SCharles.Forsyth 194*37da2899SCharles.Forsyth /* */ 195*37da2899SCharles.Forsyth 196*37da2899SCharles.Forsyth FT_END_HEADER 197*37da2899SCharles.Forsyth 198*37da2899SCharles.Forsyth 199*37da2899SCharles.Forsyth #endif /* __FTLRU_H__ */ 200*37da2899SCharles.Forsyth 201*37da2899SCharles.Forsyth 202*37da2899SCharles.Forsyth /* END */ 203