ftlru.h 8.0 KB

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