Visualization Library 2.0.0-b5

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ftccache.h
Go to the documentation of this file.
1 /***************************************************************************/
2 /* */
3 /* ftccache.h */
4 /* */
5 /* FreeType internal cache interface (specification). */
6 /* */
7 /* Copyright 2000-2007, 2009-2011, 2013 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 #ifndef __FTCCACHE_H__
20 #define __FTCCACHE_H__
21 
22 
23 #include "ftcmru.h"
24 
26 
27 #define _FTC_FACE_ID_HASH( i ) \
28  ((FT_PtrDist)(( (FT_PtrDist)(i) >> 3 ) ^ ( (FT_PtrDist)(i) << 7 )))
29 
30  /* handle to cache object */
31  typedef struct FTC_CacheRec_* FTC_Cache;
32 
33  /* handle to cache class */
34  typedef const struct FTC_CacheClassRec_* FTC_CacheClass;
35 
36 
37  /*************************************************************************/
38  /*************************************************************************/
39  /***** *****/
40  /***** CACHE NODE DEFINITIONS *****/
41  /***** *****/
42  /*************************************************************************/
43  /*************************************************************************/
44 
45  /*************************************************************************/
46  /* */
47  /* Each cache controls one or more cache nodes. Each node is part of */
48  /* the global_lru list of the manager. Its `data' field however is used */
49  /* as a reference count for now. */
50  /* */
51  /* A node can be anything, depending on the type of information held by */
52  /* the cache. It can be an individual glyph image, a set of bitmaps */
53  /* glyphs for a given size, some metrics, etc. */
54  /* */
55  /*************************************************************************/
56 
57  /* structure size should be 20 bytes on 32-bits machines */
58  typedef struct FTC_NodeRec_
59  {
60  FTC_MruNodeRec mru; /* circular mru list pointer */
61  FTC_Node link; /* used for hashing */
62  FT_PtrDist hash; /* used for hashing too */
63  FT_UShort cache_index; /* index of cache the node belongs to */
64  FT_Short ref_count; /* reference count for this node */
65 
66  } FTC_NodeRec;
67 
68 
69 #define FTC_NODE( x ) ( (FTC_Node)(x) )
70 #define FTC_NODE_P( x ) ( (FTC_Node*)(x) )
71 
72 #define FTC_NODE__NEXT( x ) FTC_NODE( (x)->mru.next )
73 #define FTC_NODE__PREV( x ) FTC_NODE( (x)->mru.prev )
74 
75 #ifdef FTC_INLINE
76 #define FTC_NODE__TOP_FOR_HASH( cache, hash ) \
77  ( ( cache )->buckets + \
78  ( ( ( ( hash ) & ( cache )->mask ) < ( cache )->p ) \
79  ? ( ( hash ) & ( ( cache )->mask * 2 + 1 ) ) \
80  : ( ( hash ) & ( cache )->mask ) ) )
81 #else
82  FT_LOCAL( FTC_Node* )
83  ftc_get_top_node_for_hash( FTC_Cache cache,
84  FT_PtrDist hash );
85 #define FTC_NODE__TOP_FOR_HASH( cache, hash ) \
86  ftc_get_top_node_for_hash( ( cache ), ( hash ) )
87 #endif
88 
89 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
90  FT_BASE( void )
92  FTC_Manager manager );
93 #endif
94 
95 
96  /*************************************************************************/
97  /*************************************************************************/
98  /***** *****/
99  /***** CACHE DEFINITIONS *****/
100  /***** *****/
101  /*************************************************************************/
102  /*************************************************************************/
103 
104  /* initialize a new cache node */
105  typedef FT_Error
108  FTC_Cache cache );
109 
110  typedef FT_Offset
112  FTC_Cache cache );
113 
114  /* compare a node to a given key pair */
115  typedef FT_Bool
117  FT_Pointer key,
118  FTC_Cache cache,
119  FT_Bool* list_changed );
120 
121 
122  typedef void
124  FTC_Cache cache );
125 
126  typedef FT_Error
127  (*FTC_Cache_InitFunc)( FTC_Cache cache );
128 
129  typedef void
130  (*FTC_Cache_DoneFunc)( FTC_Cache cache );
131 
132 
133  typedef struct FTC_CacheClassRec_
134  {
140 
144 
146 
147 
148  /* each cache really implements a dynamic hash table to manage its nodes */
149  typedef struct FTC_CacheRec_
150  {
155 
156  FTC_CacheClassRec clazz; /* local copy, for speed */
157 
160  FT_UInt index; /* in manager's table */
161 
162  FTC_CacheClass org_class; /* original class pointer */
163 
164  } FTC_CacheRec;
165 
166 
167 #define FTC_CACHE( x ) ( (FTC_Cache)(x) )
168 #define FTC_CACHE_P( x ) ( (FTC_Cache*)(x) )
169 
170 
171  /* default cache initialize */
172  FT_LOCAL( FT_Error )
173  FTC_Cache_Init( FTC_Cache cache );
174 
175  /* default cache finalizer */
176  FT_LOCAL( void )
177  FTC_Cache_Done( FTC_Cache cache );
178 
179  /* Call this function to look up the cache. If no corresponding
180  * node is found, a new one is automatically created. This function
181  * is capable of flushing the cache adequately to make room for the
182  * new cache object.
183  */
184 
185 #ifndef FTC_INLINE
186  FT_LOCAL( FT_Error )
187  FTC_Cache_Lookup( FTC_Cache cache,
189  FT_Pointer query,
190  FTC_Node *anode );
191 #endif
192 
193  FT_LOCAL( FT_Error )
194  FTC_Cache_NewNode( FTC_Cache cache,
196  FT_Pointer query,
197  FTC_Node *anode );
198 
199  /* Remove all nodes that relate to a given face_id. This is useful
200  * when un-installing fonts. Note that if a cache node relates to
201  * the face_id but is locked (i.e., has `ref_count > 0'), the node
202  * will _not_ be destroyed, but its internal face_id reference will
203  * be modified.
204  *
205  * The final result will be that the node will never come back
206  * in further lookup requests, and will be flushed on demand from
207  * the cache normally when its reference count reaches 0.
208  */
209  FT_LOCAL( void )
210  FTC_Cache_RemoveFaceID( FTC_Cache cache,
211  FTC_FaceID face_id );
212 
213 
214 #ifdef FTC_INLINE
215 
216 #define FTC_CACHE_LOOKUP_CMP( cache, nodecmp, hash, query, node, error ) \
217  FT_BEGIN_STMNT \
218  FTC_Node *_bucket, *_pnode, _node; \
219  FTC_Cache _cache = FTC_CACHE(cache); \
220  FT_PtrDist _hash = (FT_PtrDist)(hash); \
221  FTC_Node_CompareFunc _nodcomp = (FTC_Node_CompareFunc)(nodecmp); \
222  FT_Bool _list_changed = FALSE; \
223  \
224  \
225  error = FT_Err_Ok; \
226  node = NULL; \
227  \
228  /* Go to the `top' node of the list sharing same masked hash */ \
229  _bucket = _pnode = FTC_NODE__TOP_FOR_HASH( _cache, _hash ); \
230  \
231  /* Look up a node with identical hash and queried properties. */ \
232  /* NOTE: _nodcomp() may change the linked list to reduce memory. */ \
233  for (;;) \
234  { \
235  _node = *_pnode; \
236  if ( _node == NULL ) \
237  goto _NewNode; \
238  \
239  if ( _node->hash == _hash && \
240  _nodcomp( _node, query, _cache, &_list_changed ) ) \
241  break; \
242  \
243  _pnode = &_node->link; \
244  } \
245  \
246  if ( _list_changed ) \
247  { \
248  /* Update _bucket by possibly modified linked list */ \
249  _bucket = _pnode = FTC_NODE__TOP_FOR_HASH( _cache, _hash ); \
250  \
251  /* Update _pnode by possibly modified linked list */ \
252  while ( *_pnode != _node ) \
253  { \
254  if ( *_pnode == NULL ) \
255  { \
256  FT_ERROR(( "FTC_CACHE_LOOKUP_CMP: oops!!! node missing\n" )); \
257  goto _NewNode; \
258  } \
259  else \
260  _pnode = &((*_pnode)->link); \
261  } \
262  } \
263  \
264  /* Reorder the list to move the found node to the `top' */ \
265  if ( _node != *_bucket ) \
266  { \
267  *_pnode = _node->link; \
268  _node->link = *_bucket; \
269  *_bucket = _node; \
270  } \
271  \
272  /* Update MRU list */ \
273  { \
274  FTC_Manager _manager = _cache->manager; \
275  void* _nl = &_manager->nodes_list; \
276  \
277  \
278  if ( _node != _manager->nodes_list ) \
279  FTC_MruNode_Up( (FTC_MruNode*)_nl, \
280  (FTC_MruNode)_node ); \
281  } \
282  goto _Ok; \
283  \
284  _NewNode: \
285  error = FTC_Cache_NewNode( _cache, _hash, query, &_node ); \
286  \
287  _Ok: \
288  node = _node; \
289  FT_END_STMNT
290 
291 #else /* !FTC_INLINE */
292 
293 #define FTC_CACHE_LOOKUP_CMP( cache, nodecmp, hash, query, node, error ) \
294  FT_BEGIN_STMNT \
295  error = FTC_Cache_Lookup( FTC_CACHE( cache ), hash, query, \
296  (FTC_Node*)&(node) ); \
297  FT_END_STMNT
298 
299 #endif /* !FTC_INLINE */
300 
301 
302  /*
303  * This macro, together with FTC_CACHE_TRYLOOP_END, defines a retry
304  * loop to flush the cache repeatedly in case of memory overflows.
305  *
306  * It is used when creating a new cache node, or within a lookup
307  * that needs to allocate data (e.g. the sbit cache lookup).
308  *
309  * Example:
310  *
311  * {
312  * FTC_CACHE_TRYLOOP( cache )
313  * error = load_data( ... );
314  * FTC_CACHE_TRYLOOP_END()
315  * }
316  *
317  */
318 #define FTC_CACHE_TRYLOOP( cache ) \
319  { \
320  FTC_Manager _try_manager = FTC_CACHE( cache )->manager; \
321  FT_UInt _try_count = 4; \
322  \
323  \
324  for (;;) \
325  { \
326  FT_UInt _try_done;
327 
328 
329 #define FTC_CACHE_TRYLOOP_END( list_changed ) \
330  if ( !error || FT_ERR_NEQ( error, Out_Of_Memory ) ) \
331  break; \
332  \
333  _try_done = FTC_Manager_FlushN( _try_manager, _try_count ); \
334  if ( _try_done > 0 && ( list_changed ) ) \
335  *(FT_Bool*)( list_changed ) = TRUE; \
336  \
337  if ( _try_done == 0 ) \
338  break; \
339  \
340  if ( _try_done == _try_count ) \
341  { \
342  _try_count *= 2; \
343  if ( _try_count < _try_done || \
344  _try_count > _try_manager->num_nodes ) \
345  _try_count = _try_manager->num_nodes; \
346  } \
347  } \
348  }
349 
350  /* */
351 
353 
354 
355 #endif /* __FTCCACHE_H__ */
356 
357 
358 /* END */
int FT_Error
Definition: fttypes.h:296
FT_Bool(* FTC_Node_CompareFunc)(FTC_Node node, FT_Pointer key, FTC_Cache cache, FT_Bool *list_changed)
Definition: ftccache.h:116
ft_ptrdiff_t FT_PtrDist
Definition: fttypes.h:333
signed long FT_Long
Definition: fttypes.h:238
FTC_CacheClassRec clazz
Definition: ftccache.h:156
FTC_Manager manager
Definition: ftccache.h:158
FT_UFast mask
Definition: ftccache.h:152
#define FT_END_HEADER
Definition: ftheader.h:54
FTC_Node link
Definition: ftccache.h:61
FT_Error(* FTC_Cache_InitFunc)(FTC_Cache cache)
Definition: ftccache.h:127
FT_UFast p
Definition: ftccache.h:151
FT_Short ref_count
Definition: ftccache.h:64
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
FT_UShort cache_index
Definition: ftccache.h:63
FT_BEGIN_HEADER typedef FT_Pointer FTC_FaceID
Definition: ftcache.h:171
FTC_Cache_DoneFunc cache_done
Definition: ftccache.h:143
FT_BEGIN_HEADER typedef unsigned char FT_Bool
Definition: fttypes.h:104
FT_Long slack
Definition: ftccache.h:153
FT_Offset(* FTC_Node_WeightFunc)(FTC_Node node, FTC_Cache cache)
Definition: ftccache.h:111
#define FT_BEGIN_HEADER
Definition: ftheader.h:36
FTC_Node_FreeFunc node_free
Definition: ftccache.h:139
FTC_Node * buckets
Definition: ftccache.h:154
FTC_CacheClass org_class
Definition: ftccache.h:162
FT_Offset cache_size
Definition: ftccache.h:141
#define FT_LOCAL(x)
Definition: ftconfig.h:235
#define FT_BASE(x)
Definition: ftconfig.h:247
FTC_Node_CompareFunc node_compare
Definition: ftccache.h:137
FT_Memory memory
Definition: ftccache.h:159
void(* FTC_Cache_DoneFunc)(FTC_Cache cache)
Definition: ftccache.h:130
FTC_Node_WeightFunc node_weight
Definition: ftccache.h:136
void * FT_Pointer
Definition: fttypes.h:307
struct FTC_CacheClassRec_ FTC_CacheClassRec
ftc_node_destroy(FTC_Node node, FTC_Manager manager)
Definition: ftccache.c:277
struct FTC_CacheRec_ * FTC_Cache
Definition: ftccache.h:31
FTC_Cache_Init(FTC_Cache cache)
Definition: ftccache.c:332
FTC_Cache_NewNode(FTC_Cache cache, FT_PtrDist hash, FT_Pointer query, FTC_Node *anode)
Definition: ftccache.c:448
unsigned int FT_UFast
Definition: ftconfig.h:148
FT_Error(* FTC_Node_NewFunc)(FTC_Node *pnode, FT_Pointer query, FTC_Cache cache)
Definition: ftccache.h:106
signed short FT_Short
Definition: fttypes.h:194
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:66
struct FTC_CacheRec_ FTC_CacheRec
FTC_Cache_RemoveFaceID(FTC_Cache cache, FTC_FaceID face_id)
Definition: ftccache.c:568
FTC_Node_NewFunc node_new
Definition: ftccache.h:135
FTC_Cache_Done(FTC_Cache cache)
Definition: ftccache.c:413
unsigned int FT_UInt
Definition: fttypes.h:227
FT_PtrDist hash
Definition: ftccache.h:62
FTC_Node_CompareFunc node_remove_faceid
Definition: ftccache.h:138
void(* FTC_Node_FreeFunc)(FTC_Node node, FTC_Cache cache)
Definition: ftccache.h:123
GLenum query
FTC_Cache_InitFunc cache_init
Definition: ftccache.h:142
unsigned short FT_UShort
Definition: fttypes.h:205
FT_UInt index
Definition: ftccache.h:160
const struct FTC_CacheClassRec_ * FTC_CacheClass
Definition: ftccache.h:34
struct FTC_NodeRec_ FTC_NodeRec
size_t FT_Offset
Definition: fttypes.h:320
FTC_MruNodeRec mru
Definition: ftccache.h:60