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

  1. /*******************************************************************
  2.  *
  3.  *  ttcache.c                                                   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.  
  18. #include "ttengine.h"
  19. #include "ttcache.h"
  20. #include "ttmemory.h"
  21. #include "ttobjs.h"
  22.  
  23.  
  24.   TT_Error  Cache_Create( PCache_Class clazz,
  25.                           TCache*      cache,
  26.                           TMutex*      lock )
  27.   {
  28.     MUTEX_Create( cache->lock );
  29.  
  30.     cache->clazz      = clazz;
  31.     cache->lock       = lock;
  32.     cache->idle_count = 0;
  33.  
  34.     ZERO_List( cache->active );
  35.     ZERO_List( cache->idle );
  36.  
  37.     return TT_Err_Ok;
  38.   }
  39.  
  40.  
  41.   TT_Error  Cache_Destroy( TCache* cache )
  42.   {
  43.     PDestructor       destroy;
  44.     PList_Element     current;
  45.  
  46.     /* now destroy all active and idle listed objects */
  47.  
  48.     destroy = cache->clazz->done;
  49.  
  50.     /* active list */
  51.  
  52.     current = List_Extract( &cache->active );
  53.     while (current)
  54.     {
  55.       destroy( current->data );
  56.       FREE( current->data );
  57.  
  58.       Element_Done( current );
  59.  
  60.       current = List_Extract( &cache->active );
  61.     }
  62.  
  63.     /* idle list */
  64.  
  65.     current = List_Extract( &cache->idle );
  66.     while (current)
  67.     {
  68.       destroy( current->data );
  69.       FREE( current->data );
  70.  
  71.       Element_Done( current );
  72.  
  73.       current = List_Extract( &cache->idle );
  74.     }
  75.  
  76.     cache->clazz      = NULL;
  77.     cache->idle_count = 0;
  78.  
  79.     return TT_Err_Ok;
  80.   }
  81.  
  82.  
  83.   TT_Error Cache_New( TCache*  cache,
  84.                       void**   new_object,
  85.                       void*    parent_object )
  86.   {
  87.     TT_Error       error;
  88.     PList_Element  current;
  89.     PConstructor   build;
  90.     void*          object;
  91.  
  92.     MUTEX_Lock( *cache->lock );
  93.  
  94.     current = List_Extract( &cache->idle );
  95.     if (current)
  96.       cache->idle_count--;
  97.     else
  98.     {
  99.       /* if no object was found in the cache, create a new one */
  100.  
  101.       build  = cache->clazz->init;
  102.  
  103.       if ( MEM_Alloc( object, cache->clazz->object_size ) )
  104.         goto Memory_Fail;
  105.  
  106.       current = Element_New();
  107.       if (!current)
  108.         goto Memory_Fail;
  109.  
  110.       current->data = object;
  111.  
  112.       error = build( object, parent_object );
  113.       if (error)
  114.         goto Fail;
  115.     }
  116.  
  117.     List_Add( &cache->active, current );
  118.     *new_object = current->data;
  119.  
  120.     error = TT_Err_Ok;
  121.  
  122.   Exit:
  123.  
  124.     MUTEX_Release( *cache->lock );
  125.     return error;
  126.  
  127.   Memory_Fail:
  128.     error = TT_Err_Out_Of_Memory;
  129.  
  130.   Fail:
  131.     FREE( object );
  132.     goto Exit;
  133.   }
  134.  
  135.  
  136.   TT_Error  Cache_Done( TCache*  cache, void*  data )
  137.   {
  138.     TT_Error       error;
  139.     PList_Element  element;
  140.     int            limit;
  141.  
  142.     MUTEX_Lock( *cache->lock );
  143.  
  144.     element = List_Find( &cache->active, data );
  145.     if ( !element )
  146.     {
  147.       error = TT_Err_Unlisted_Object;
  148.       goto Exit;
  149.     }
  150.  
  151.     List_Remove( &cache->active, element );
  152.  
  153.     limit = cache->clazz->idle_limit;
  154.     if ( cache->idle_count >= limit )
  155.     {
  156.       /* destroy the object when the cache is full */
  157.  
  158.       cache->clazz->done( element->data );
  159.       FREE( element->data );
  160.  
  161.       Element_Done( element );
  162.     }
  163.     else
  164.     {
  165.       /* simply add the object to the idle list */
  166.  
  167.       List_Add( &cache->idle, element );
  168.       cache->idle_count++;
  169.     }
  170.  
  171.     error = TT_Err_Ok;
  172.  
  173.   Exit:
  174.     MUTEX_Release( *cache_lock );
  175.     return error;
  176.   }
  177.  
  178.