1 /***************************************************************************/
5 /* FreeType Cache Manager (specification). */
7 /* Copyright 2000-2001 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
19 /*************************************************************************/
21 /* A cache manager is in charge of the following: */
23 /* - Maintain a mapping between generic FTC_FaceIDs and live FT_Face */
24 /* objects. The mapping itself is performed through a user-provided */
25 /* callback. However, the manager maintains a small cache of FT_Face */
26 /* & FT_Size objects in order to speed up things considerably. */
28 /* - Manage one or more cache objects. Each cache is in charge of */
29 /* holding a varying number of `cache nodes'. Each cache node */
30 /* represents a minimal amount of individually accessible cached */
31 /* data. For example, a cache node can be an FT_Glyph image */
32 /* containing a vector outline, or some glyph metrics, or anything */
35 /* Each cache node has a certain size in bytes that is added to the */
36 /* total amount of `cache memory' within the manager. */
38 /* All cache nodes are located in a global LRU list, where the oldest */
39 /* node is at the tail of the list. */
41 /* Each node belongs to a single cache, and includes a reference */
42 /* count to avoid destroying it (due to caching). */
44 /*************************************************************************/
47 /*************************************************************************/
48 /*************************************************************************/
49 /*************************************************************************/
50 /*************************************************************************/
51 /*************************************************************************/
53 /********* WARNING, THIS IS BETA CODE. *********/
55 /*************************************************************************/
56 /*************************************************************************/
57 /*************************************************************************/
58 /*************************************************************************/
59 /*************************************************************************/
62 #ifndef __FTCMANAG_H__
63 #define __FTCMANAG_H__
68 #include FT_CACHE_INTERNAL_LRU_H
74 /*************************************************************************/
79 /*************************************************************************/
82 #define FTC_MAX_FACES_DEFAULT 2
83 #define FTC_MAX_SIZES_DEFAULT 4
84 #define FTC_MAX_BYTES_DEFAULT 200000L /* 200kByte by default! */
86 /* maximum number of caches registered in a single manager */
87 #define FTC_MAX_CACHES 16
90 /*************************************************************************/
96 /* The cache manager structure. */
99 /* library :: A handle to a FreeType library instance. */
101 /* faces_lru :: The lru list of FT_Face objects in the cache. */
103 /* sizes_lru :: The lru list of FT_Size objects in the cache. */
105 /* max_bytes :: The maximum number of bytes to be allocated in the */
106 /* cache. This is only related to the byte size of */
107 /* the nodes cached by the manager. */
109 /* num_bytes :: The current number of bytes allocated in the */
110 /* cache. Only related to the byte size of cached */
113 /* num_nodes :: The current number of nodes in the manager. */
115 /* global_lru :: The global lru list of all cache nodes. */
117 /* caches :: A table of installed/registered cache objects. */
119 /* request_data :: User-provided data passed to the requester. */
121 /* request_face :: User-provided function used to implement a mapping */
122 /* between abstract FTC_FaceIDs and real FT_Face */
125 typedef struct FTC_ManagerRec_
134 FT_ListRec global_lru;
135 FTC_Cache caches[FTC_MAX_CACHES];
137 FT_Pointer request_data;
138 FTC_Face_Requester request_face;
143 /*************************************************************************/
146 /* FTC_Manager_Compress */
149 /* This function is used to check the state of the cache manager if */
150 /* its `num_bytes' field is greater than its `max_bytes' field. It */
151 /* will flush as many old cache nodes as possible (ignoring cache */
152 /* nodes with a non-zero reference count). */
155 /* manager :: A handle to the cache manager. */
158 /* Client applications should not call this function directly. It is */
159 /* normally invoked by specific cache implementations. */
161 /* The reason this function is exported is to allow client-specific */
165 FTC_Manager_Compress( FTC_Manager manager );
168 /*************************************************************************/
169 /*************************************************************************/
171 /***** CACHE NODE DEFINITIONS *****/
173 /*************************************************************************/
174 /*************************************************************************/
177 /*************************************************************************/
179 /* Each cache controls one or more cache nodes. Each node is part of */
180 /* the global_lru list of the manager. Its `data' field however is used */
181 /* as a reference count for now. */
183 /* A node can be anything, depending on the type of information held by */
184 /* the cache. It can be an individual glyph image, a set of bitmaps */
185 /* glyphs for a given size, some metrics, etc. */
188 typedef FT_ListNodeRec FTC_CacheNodeRec;
189 typedef FTC_CacheNodeRec* FTC_CacheNode;
192 /* the field `cachenode.data' is typecast to this type */
193 typedef struct FTC_CacheNode_Data_
195 FT_UShort cache_index;
198 } FTC_CacheNode_Data;
201 /* return a pointer to FTC_CacheNode_Data contained in a */
202 /* CacheNode's `data' field */
203 #define FTC_CACHENODE_TO_DATA_P( n ) \
204 ( (FTC_CacheNode_Data*)&(n)->data )
206 #define FTC_LIST_TO_CACHENODE( n ) ( (FTC_CacheNode)(n) )
209 /*************************************************************************/
212 /* FTC_CacheNode_SizeFunc */
215 /* A function used to compute the total size in bytes of a given */
216 /* cache node. It is used by the cache manager to compute the number */
217 /* of old nodes to flush when the cache is full. */
220 /* node :: A handle to the target cache node. */
222 /* cache_data :: A generic pointer passed to the destructor. */
225 /* The size of a given cache node in bytes. */
228 (*FTC_CacheNode_SizeFunc)( FTC_CacheNode node,
229 FT_Pointer cache_data );
232 /*************************************************************************/
235 /* FTC_CacheNode_DestroyFunc */
238 /* A function used to destroy a given cache node. It is called by */
239 /* the manager when the cache is full and old nodes need to be */
243 /* node :: A handle to the target cache node. */
245 /* cache_data :: A generic pointer passed to the destructor. */
248 (*FTC_CacheNode_DestroyFunc)( FTC_CacheNode node,
249 FT_Pointer cache_data );
252 /*************************************************************************/
255 /* FTC_CacheNode_Class */
258 /* A very simple structure used to describe a cache node's class to */
259 /* the cache manager. */
262 /* size_node :: A function used to size the node. */
264 /* destroy_node :: A function used to destroy the node. */
267 /* The cache node class doesn't include a `new_node' function because */
268 /* the cache manager never allocates cache node directly; it */
269 /* delegates this task to its cache objects. */
271 typedef struct FTC_CacheNode_Class_
273 FTC_CacheNode_SizeFunc size_node;
274 FTC_CacheNode_DestroyFunc destroy_node;
276 } FTC_CacheNode_Class;
279 /*************************************************************************/
280 /*************************************************************************/
282 /***** CACHE DEFINITIONS *****/
284 /*************************************************************************/
285 /*************************************************************************/
288 /*************************************************************************/
291 /* FTC_Cache_InitFunc */
294 /* A function used to initialize a given cache object. */
297 /* cache :: A handle to the new cache. */
300 (*FTC_Cache_InitFunc)( FTC_Cache cache );
303 /*************************************************************************/
306 /* FTC_Cache_DoneFunc */
309 /* A function to finalize a given cache object. */
312 /* cache :: A handle to the target cache. */
315 (*FTC_Cache_DoneFunc)( FTC_Cache cache );
318 /*************************************************************************/
321 /* FTC_Cache_Class */
324 /* A structure used to describe a given cache object class to the */
328 /* cache_byte_size :: The size of the cache object in bytes. */
330 /* init_cache :: The cache object initializer. */
332 /* done_cache :: The cache object finalizer. */
334 struct FTC_Cache_Class_
336 FT_UInt cache_byte_size;
337 FTC_Cache_InitFunc init_cache;
338 FTC_Cache_DoneFunc done_cache;
342 /*************************************************************************/
348 /* A structure used to describe an abstract cache object. */
351 /* manager :: A handle to the parent cache manager. */
353 /* memory :: A handle to the memory manager. */
355 /* clazz :: A pointer to the cache class. */
357 /* node_clazz :: A pointer to the cache's node class. */
359 /* cache_index :: An index of the cache in the manager's table. */
361 /* cache_data :: Data passed to the cache node */
362 /* constructor/finalizer. */
364 typedef struct FTC_CacheRec_
368 FTC_Cache_Class* clazz;
369 FTC_CacheNode_Class* node_clazz;
371 FT_UInt cache_index; /* in manager's table */
372 FT_Pointer cache_data; /* passed to cache node methods */
382 #endif /* __FTCMANAG_H__ */