home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / ft-beta.zip / freetype / lib / ttcache.h < prev    next >
C/C++ Source or Header  |  1997-10-06  |  5KB  |  146 lines

  1. /*******************************************************************
  2.  *
  3.  *  ttcache.h                                                   1.0
  4.  *
  5.  *    Generic object cache     
  6.  *
  7.  *  Copyright 1996, 1997 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.  *  This component defines and implement object caches.
  18.  *
  19.  *  An object class is a structure layout that encapsulate one    
  20.  *  given type of data used by the FreeType engine. Each object     
  21.  *  class is completely described by :
  22.  *
  23.  *    - a 'root' or 'leading' structure containing the first
  24.  *      important fields of the class. The root structure is
  25.  *      always of fixed size.
  26.  *
  27.  *      It is implemented as a simple C structure, and may
  28.  *      contain several pointers to sub-tables that can be
  29.  *      sized and allocated dynamically.
  30.  *
  31.  *      examples : TFace, TInstance, TGlyph & TExecution_Context
  32.  *                 ( defined in 'ttobjs.h' )
  33.  *
  34.  *    - we make a difference between 'child' pointers and 'peer'
  35.  *      pointers. A 'child' pointer points to a sub-table that is
  36.  *      owned by the object, while a 'peer' pointer points to any
  37.  *      other kind of data the object isn't responsible for.
  38.  *
  39.  *      An object class is thus usually a 'tree' of 'child' tables.
  40.  *
  41.  *    - each object class needs a constructor and a destructor.
  42.  *
  43.  *      A constructor is a function which receives the address of
  44.  *      freshly allocated and zeroed object root structure and
  45.  *      'builds' all the valid child data that must be associated
  46.  *      to the object before it becomes 'valid'.
  47.  *
  48.  *      A destructor does the inverse job : given the address of
  49.  *      a valid object, it must discards all its child data and
  50.  *      zero its main fields (essentially the pointers and array
  51.  *      sizes found in the root fields).
  52.  *
  53.  *
  54.  *
  55.  *
  56.  *
  57.  *
  58.  *
  59.  *
  60.  *
  61.  *
  62.  *
  63.  *
  64.  ******************************************************************/
  65.  
  66. #ifndef TTCACHE_H
  67. #define TTCACHE_H
  68.  
  69. #include "ttmutex.h"
  70. #include "tterror.h"
  71. #include "ttlists.h"
  72.  
  73.   #ifdef __cplusplus
  74.   extern "C" {
  75.   #endif
  76.  
  77.   typedef  TT_Error  TConstructor( void* object,
  78.                                    void* parent );
  79.  
  80.   typedef  TT_Error  TDestructor ( void* object );
  81.  
  82.   typedef  TConstructor*  PConstructor;
  83.   typedef  TDestructor*   PDestructor;
  84.  
  85.   struct _TCache_Class
  86.   {
  87.     int               object_size;
  88.     int               idle_limit;
  89.     PConstructor      init;
  90.     PDestructor       done;
  91.   };
  92.   typedef struct _TCache_Class  TCache_Class;
  93.   typedef TCache_Class         *PCache_Class;
  94.  
  95.   /* A Cache class record holds the data necessary to define */
  96.   /* a cache kind.                                           */
  97.  
  98.   struct _TCache
  99.   {
  100.     PCache_Class    clazz;      /* 'class' is a reserved word in C++ */
  101.     TMutex*         lock;
  102.     TSingle_List    active;
  103.     TSingle_List    idle;
  104.     int             idle_count;
  105.   };
  106.   typedef struct _TCache  TCache;
  107.   typedef TCache         *PCache;
  108.  
  109.   /* An object cache holds two lists tracking the active and */
  110.   /* idle objects that are currently created and used by the */
  111.   /* engine. It can also be 'protected' by a mutex           */
  112.  
  113.   TT_Error  Cache_Create( PCache_Class clazz, 
  114.                           TCache*      cache,
  115.                           TMutex*      lock );
  116.   /* Initializes a new cache, of class 'clazz', pointed by 'cache', */
  117.   /* protected by the 'lock' mutex. Set 'lock' to NULL if the cache */
  118.   /* doesn't need protection                                        */
  119.  
  120.   TT_Error  Cache_Destroy( TCache* cache );
  121.   /* Destroys a cache and all its listed objects */
  122.  
  123.  
  124.  
  125.   TT_Error Cache_New( TCache*  cache,
  126.                       void**   new_object,
  127.                       void*    parent_object );
  128.   /* Extracts a new object from the cache */
  129.  
  130.   TT_Error  Cache_Done( TCache*  cache, void*  data );
  131.   /* Returns an object to the cache, or discards it depending */
  132.   /* on the cache class' 'idle_limit' field                   */
  133.  
  134.   #define CACHE_New( _cache, _newobj, _parent ) \
  135.             Cache_New( (TCache*)_cache, (void**)&_newobj, (void*)_parent )
  136.  
  137.   #define CACHE_Done( _cache, _obj ) \
  138.             Cache_Done( (TCache*)_cache, (void*)_obj )
  139.  
  140.   #ifdef __cplusplus
  141.   }
  142.   #endif
  143.  
  144. #endif /* TTCACHE_H */
  145.  
  146.