ftccache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /***************************************************************************/
  2. /* */
  3. /* ftccache.h */
  4. /* */
  5. /* FreeType internal cache interface (specification). */
  6. /* */
  7. /* Copyright 2000-2001, 2002 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. #ifndef __FTCCACHE_H__
  18. #define __FTCCACHE_H__
  19. /* define to allow cache lookup inlining */
  20. #define FTC_CACHE_USE_INLINE
  21. FT_BEGIN_HEADER
  22. /* handle to cache object */
  23. typedef struct FTC_CacheRec_* FTC_Cache;
  24. /* handle to cache class */
  25. typedef const struct FTC_Cache_ClassRec_* FTC_Cache_Class;
  26. /* handle to cache node family */
  27. typedef struct FTC_FamilyRec_* FTC_Family;
  28. /* handle to cache root query */
  29. typedef struct FTC_QueryRec_* FTC_Query;
  30. /*************************************************************************/
  31. /*************************************************************************/
  32. /***** *****/
  33. /***** CACHE NODE DEFINITIONS *****/
  34. /***** *****/
  35. /*************************************************************************/
  36. /*************************************************************************/
  37. /*************************************************************************/
  38. /* */
  39. /* Each cache controls one or more cache nodes. Each node is part of */
  40. /* the global_lru list of the manager. Its `data' field however is used */
  41. /* as a reference count for now. */
  42. /* */
  43. /* A node can be anything, depending on the type of information held by */
  44. /* the cache. It can be an individual glyph image, a set of bitmaps */
  45. /* glyphs for a given size, some metrics, etc. */
  46. /* */
  47. /*************************************************************************/
  48. /* structure size should be 20 bytes on 32-bits machines */
  49. typedef struct FTC_NodeRec_
  50. {
  51. FTC_Node mru_next; /* circular mru list pointer */
  52. FTC_Node mru_prev; /* circular mru list pointer */
  53. FTC_Node link; /* used for hashing */
  54. FT_UInt32 hash; /* used for hashing too */
  55. FT_UShort fam_index; /* index of family the node belongs to */
  56. FT_Short ref_count; /* reference count for this node */
  57. } FTC_NodeRec;
  58. #define FTC_NODE( x ) ( (FTC_Node)(x) )
  59. #define FTC_NODE_P( x ) ( (FTC_Node*)(x) )
  60. /*************************************************************************/
  61. /* */
  62. /* These functions are exported so that they can be called from */
  63. /* user-provided cache classes; otherwise, they are really part of the */
  64. /* cache sub-system internals. */
  65. /* */
  66. /* can be used as a FTC_Node_DoneFunc */
  67. FT_EXPORT( void )
  68. ftc_node_done( FTC_Node node,
  69. FTC_Cache cache );
  70. /* reserved for manager's use */
  71. FT_EXPORT( void )
  72. ftc_node_destroy( FTC_Node node,
  73. FTC_Manager manager );
  74. /*************************************************************************/
  75. /*************************************************************************/
  76. /***** *****/
  77. /***** CACHE QUERY DEFINITIONS *****/
  78. /***** *****/
  79. /*************************************************************************/
  80. /*************************************************************************/
  81. /* A structure modelling a cache node query. The following fields must */
  82. /* all be set by the @FTC_Family_CompareFunc method of a cache's family */
  83. /* list. */
  84. /* */
  85. typedef struct FTC_QueryRec_
  86. {
  87. FTC_Family family;
  88. FT_UFast hash;
  89. } FTC_QueryRec;
  90. #define FTC_QUERY( x ) ( (FTC_Query)(x) )
  91. #define FTC_QUERY_P( x ) ( (FTC_Query*)(x) )
  92. /*************************************************************************/
  93. /*************************************************************************/
  94. /***** *****/
  95. /***** CACHE FAMILY DEFINITIONS *****/
  96. /***** *****/
  97. /*************************************************************************/
  98. /*************************************************************************/
  99. typedef struct FTC_FamilyRec_
  100. {
  101. FT_LruNodeRec lru;
  102. FTC_Cache cache;
  103. FT_UInt num_nodes;
  104. FT_UInt fam_index;
  105. } FTC_FamilyRec;
  106. #define FTC_FAMILY( x ) ( (FTC_Family)(x) )
  107. #define FTC_FAMILY_P( x ) ( (FTC_Family*)(x) )
  108. /*************************************************************************/
  109. /* */
  110. /* These functions are exported so that they can be called from */
  111. /* user-provided cache classes; otherwise, they are really part of the */
  112. /* cache sub-system internals. */
  113. /* */
  114. /* must be called by any FTC_Node_InitFunc routine */
  115. FT_EXPORT( FT_Error )
  116. ftc_family_init( FTC_Family family,
  117. FTC_Query query,
  118. FTC_Cache cache );
  119. /* can be used as a FTC_Family_DoneFunc; otherwise, must be called */
  120. /* by any family finalizer function */
  121. FT_EXPORT( void )
  122. ftc_family_done( FTC_Family family );
  123. /*************************************************************************/
  124. /*************************************************************************/
  125. /***** *****/
  126. /***** CACHE DEFINITIONS *****/
  127. /***** *****/
  128. /*************************************************************************/
  129. /*************************************************************************/
  130. /* each cache really implements a dynamic hash table to manage its nodes */
  131. typedef struct FTC_CacheRec_
  132. {
  133. FTC_Manager manager;
  134. FT_Memory memory;
  135. FTC_Cache_Class clazz;
  136. FT_UInt cache_index; /* in manager's table */
  137. FT_Pointer cache_data; /* used by cache node methods */
  138. FT_UFast p;
  139. FT_UFast mask;
  140. FT_Long slack;
  141. FTC_Node* buckets;
  142. FT_LruList_ClassRec family_class;
  143. FT_LruList families;
  144. } FTC_CacheRec;
  145. #define FTC_CACHE( x ) ( (FTC_Cache)(x) )
  146. #define FTC_CACHE_P( x ) ( (FTC_Cache*)(x) )
  147. /* initialize a given cache */
  148. typedef FT_Error
  149. (*FTC_Cache_InitFunc)( FTC_Cache cache );
  150. /* clear a cache */
  151. typedef void
  152. (*FTC_Cache_ClearFunc)( FTC_Cache cache );
  153. /* finalize a given cache */
  154. typedef void
  155. (*FTC_Cache_DoneFunc)( FTC_Cache cache );
  156. typedef FT_Error
  157. (*FTC_Family_InitFunc)( FTC_Family family,
  158. FTC_Query query,
  159. FTC_Cache cache );
  160. typedef FT_Int
  161. (*FTC_Family_CompareFunc)( FTC_Family family,
  162. FTC_Query query );
  163. typedef void
  164. (*FTC_Family_DoneFunc)( FTC_Family family,
  165. FTC_Cache cache );
  166. /* initialize a new cache node */
  167. typedef FT_Error
  168. (*FTC_Node_InitFunc)( FTC_Node node,
  169. FT_Pointer type,
  170. FTC_Cache cache );
  171. /* compute the weight of a given cache node */
  172. typedef FT_ULong
  173. (*FTC_Node_WeightFunc)( FTC_Node node,
  174. FTC_Cache cache );
  175. /* compare a node to a given key pair */
  176. typedef FT_Bool
  177. (*FTC_Node_CompareFunc)( FTC_Node node,
  178. FT_Pointer key,
  179. FTC_Cache cache );
  180. /* finalize a given cache node */
  181. typedef void
  182. (*FTC_Node_DoneFunc)( FTC_Node node,
  183. FTC_Cache cache );
  184. typedef struct FTC_Cache_ClassRec_
  185. {
  186. FT_UInt cache_size;
  187. FTC_Cache_InitFunc cache_init;
  188. FTC_Cache_ClearFunc cache_clear;
  189. FTC_Cache_DoneFunc cache_done;
  190. FT_UInt family_size;
  191. FTC_Family_InitFunc family_init;
  192. FTC_Family_CompareFunc family_compare;
  193. FTC_Family_DoneFunc family_done;
  194. FT_UInt node_size;
  195. FTC_Node_InitFunc node_init;
  196. FTC_Node_WeightFunc node_weight;
  197. FTC_Node_CompareFunc node_compare;
  198. FTC_Node_DoneFunc node_done;
  199. } FTC_Cache_ClassRec;
  200. /* */
  201. /*************************************************************************/
  202. /* */
  203. /* These functions are exported so that they can be called from */
  204. /* user-provided cache classes; otherwise, they are really part of the */
  205. /* cache sub-system internals. */
  206. /* */
  207. /* can be used directly as FTC_Cache_DoneFunc(), or called by custom */
  208. /* cache finalizers */
  209. FT_EXPORT( void )
  210. ftc_cache_done( FTC_Cache cache );
  211. /* can be used directly as FTC_Cache_ClearFunc(), or called by custom */
  212. /* cache clear routines */
  213. FT_EXPORT( void )
  214. ftc_cache_clear( FTC_Cache cache );
  215. /* initalize the hash table within the cache */
  216. FT_EXPORT( FT_Error )
  217. ftc_cache_init( FTC_Cache cache );
  218. /* can be called when the key's hash value has been computed */
  219. FT_EXPORT( FT_Error )
  220. ftc_cache_lookup( FTC_Cache cache,
  221. FTC_Query query,
  222. FTC_Node *anode );
  223. /* */
  224. FT_END_HEADER
  225. #endif /* __FTCCACHE_H__ */
  226. /* END */