summaryrefslogtreecommitdiff
path: root/src/cache/ftcchunk.c
blob: e6288bc6c0f5c4a3a5884b650ef89b0c6787135d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***************************************************************************/
/*                                                                         */
/*  ftcchunk.c                                                             */
/*                                                                         */
/*    FreeType chunk cache cache (body).                                   */
/*                                                                         */
/*  Copyright 2000-2001 by                                                 */
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/


#include <ft2build.h>
#include FT_CACHE_H
#include FT_CACHE_INTERNAL_CHUNK_H
#include FT_LIST_H
#include FT_ERRORS_H
#include FT_INTERNAL_OBJECTS_H

#include "ftcerror.h"


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                      GLYPH NODES                              *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/

#define  FTC_CSET_CHUNK_INDEX(cset,gindex)   \
                  ( (gindex) / (cset)->item_count )
                  
#define  FTC_CSET_START(cset,gindex)    \
                  ( FTC_CSET_CHUNK_INDEX(cset,gindex) * (cset)->item_count )

#define  FTC_CSET_HASH(cset,gindex)  \
             ((FT_UFast)( ((cset)->hash << 16) | \
                          (FTC_CSET_CHUNK_INDEX(cset,gindex) & 0xFFFF) ))

  /* create a new chunk node, setting its cache index and ref count */
  FT_EXPORT_DEF( FT_Error )
  ftc_chunk_node_init( FTC_ChunkNode  cnode,
                       FTC_ChunkSet   cset,
                       FT_UInt        gindex,
                       FT_Bool        alloc )
  {
    FTC_ChunkCache  ccache = cset->ccache;
    FT_Error        error  = 0;
    FT_UInt         len;
    FT_UInt         start  = FTC_CSET_START(cset,gindex);

    cnode->cset       = cset;
    cnode->node.hash  = FTC_CSET_HASH(cset,gindex);
    cnode->item_start = start;

    len = cset->item_total - start;
    if ( len > cset->item_count )
      len = cset->item_count;

    cnode->item_count = len;
    
    if ( alloc )
    {
      FT_Memory  memory = ccache->cache.memory;
     
      error = MEM_Alloc( cnode->items, cset->item_size * cnode->item_count ); 
    }

    if   (!error )
      cset->num_chunks++;
      
    return error;
  }


  FT_EXPORT_DEF( void )
  ftc_chunk_node_done( FTC_ChunkNode  cnode )
  {
    FTC_ChunkSet  cset  = cnode->cset;
    FT_Memory     memory = cset->ccache->cache.memory;

    /* destroy the node */
    FREE( cnode->items );
    cnode->item_count = 0;
    cnode->item_start = 0;

    /* remove from parent set table - eventually destroy the set */
    if ( --cset->num_chunks <= 0 )
      FT_LruList_Remove( cset->ccache->cset_lru, (FT_LruNode) cset );
  }



  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                      CHUNK SETS                               *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/


  FT_EXPORT_DEF( FT_Error )
  ftc_chunk_set_init( FTC_ChunkSet    cset,
                      FT_UInt         item_size,
                      FT_UInt         item_count,
                      FT_UInt         item_total,
                      FTC_ChunkCache  cache )
  {
    cset->ccache     = cache;
    cset->num_chunks = 0;

    cset->item_total = item_total;
    cset->item_size  = item_size;
    cset->item_count = item_count;

    return 0;
  }


  FT_EXPORT_DEF( void )
  ftc_chunk_set_done( FTC_ChunkSet  cset )
  {
    /* nothing for now */
    FT_UNUSED( cset );
  }


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                      CHUNK CACHES                             *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/


  FT_EXPORT_DEF( void )
  ftc_chunk_cache_done(  FTC_ChunkCache  ccache )
  {
    ftc_cache_done( FTC_CACHE(ccache) );

    /* simply delete all remaining glyph sets */
    if ( ccache->cset_lru )
    {
      FT_LruList_Destroy( ccache->cset_lru );
      ccache->cset_lru = NULL;
    }
  }



  FT_EXPORT_DEF( FT_Error )
  ftc_chunk_cache_init( FTC_ChunkCache    ccache,
                        FT_LruList_Class  cset_class )
  {
    FT_Error  error;

    error = ftc_cache_init( FTC_CACHE(ccache) );
    if (error) goto Exit;

    error = FT_LruList_New( cset_class, 0, ccache,
                            ccache->cache.memory,
                            &ccache->cset_lru );
  Exit:
    return error;
  }



  FT_EXPORT_DEF( FT_Error )
  ftc_chunk_cache_lookup( FTC_ChunkCache   ccache,
                          FTC_ChunkQuery   query,
                          FTC_ChunkNode   *anode )
  {
    FT_LruNode    node;
    FT_Error      error;
    
    error = FT_LruList_Lookup( ccache->cset_lru, query, &node );
    if ( !error )
    {
      FTC_ChunkSet  cset  = FTC_CHUNK_SET(node);
      FT_UFast      hash  = FTC_CSET_HASH( cset, query->gindex );

      error = ftc_cache_lookup_node( FTC_CACHE(ccache), hash, query,
                                     FTC_NODE_P(anode) );
    }
    return error;
  }


/* END */