1#if !defined(_FXFT_VERSION_) || _FXFT_VERSION_ == 2501
2/***************************************************************************/
3/*                                                                         */
4/*  ftsystem.c                                                             */
5/*                                                                         */
6/*    ANSI-specific FreeType low-level system interface (body).            */
7/*                                                                         */
8/*  Copyright 1996-2002, 2006, 2008-2011, 2013 by                          */
9/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
10/*                                                                         */
11/*  This file is part of the FreeType project, and may only be used,       */
12/*  modified, and distributed under the terms of the FreeType project      */
13/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
14/*  this file you indicate that you have read the license and              */
15/*  understand and accept it fully.                                        */
16/*                                                                         */
17/***************************************************************************/
18
19  /*************************************************************************/
20  /*                                                                       */
21  /* This file contains the default interface used by FreeType to access   */
22  /* low-level, i.e. memory management, i/o access as well as thread       */
23  /* synchronisation.  It can be replaced by user-specific routines if     */
24  /* necessary.                                                            */
25  /*                                                                       */
26  /*************************************************************************/
27
28#define FT2_BUILD_LIBRARY
29#include "../../include/ft2build.h"
30#include "../../include/freetype/config/ftconfig.h"
31#include "../../include/freetype/internal/ftdebug.h"
32#include "../../include/freetype/internal/ftstream.h"
33#include "../../include/freetype/ftsystem.h"
34#include "../../include/freetype/fterrors.h"
35#include "../../include/freetype/fttypes.h"
36
37
38  /*************************************************************************/
39  /*                                                                       */
40  /*                       MEMORY MANAGEMENT INTERFACE                     */
41  /*                                                                       */
42  /*************************************************************************/
43
44  /*************************************************************************/
45  /*                                                                       */
46  /* It is not necessary to do any error checking for the                  */
47  /* allocation-related functions.  This will be done by the higher level  */
48  /* routines like ft_mem_alloc() or ft_mem_realloc().                     */
49  /*                                                                       */
50  /*************************************************************************/
51
52#define _FOXIT_MEM_MANAGER_
53
54#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
55extern "C" {
56#endif
57
58/** Allocate number of bytes */
59void*	FXMEM_DefaultAlloc(int byte_size, int flags);
60
61/** Allocate with debug information */
62void*	FXMEM_DefaultAllocDebug(int size, int flags, const char* file, int line);
63
64void*	FXMEM_DefaultRealloc(void* pointer, int new_size, int flags);
65
66/** Free previously allocated memory */
67void	FXMEM_DefaultFree(void* pointer, int flags);
68
69#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
70}
71#endif
72  /*************************************************************************/
73  /*                                                                       */
74  /* <Function>                                                            */
75  /*    ft_alloc                                                           */
76  /*                                                                       */
77  /* <Description>                                                         */
78  /*    The memory allocation function.                                    */
79  /*                                                                       */
80  /* <Input>                                                               */
81  /*    memory :: A pointer to the memory object.                          */
82  /*                                                                       */
83  /*    size   :: The requested size in bytes.                             */
84  /*                                                                       */
85  /* <Return>                                                              */
86  /*    The address of newly allocated block.                              */
87  /*                                                                       */
88  FT_CALLBACK_DEF( void* )
89  ft_alloc( FT_Memory  memory,
90            long       size )
91  {
92    FT_UNUSED( memory );
93  #ifdef _FOXIT_MEM_MANAGER_
94	return FXMEM_DefaultAlloc(size, 0);
95#else
96    return ft_smalloc( size );
97#endif
98  }
99
100  /* XYQ 2006-10-12 */
101#ifdef _XYQ_MEM_DEBUG
102  FT_CALLBACK_DEF( void* )
103	  ft_allocdebug( FT_Memory  memory,
104	  long       size, const char* filename, int line)
105  {
106	  FT_UNUSED( memory );
107
108	  return FXMEM_DefaultAllocDebug( size, 0, filename, line );
109  }
110#endif
111
112  /*************************************************************************/
113  /*                                                                       */
114  /* <Function>                                                            */
115  /*    ft_realloc                                                         */
116  /*                                                                       */
117  /* <Description>                                                         */
118  /*    The memory reallocation function.                                  */
119  /*                                                                       */
120  /* <Input>                                                               */
121  /*    memory   :: A pointer to the memory object.                        */
122  /*                                                                       */
123  /*    cur_size :: The current size of the allocated memory block.        */
124  /*                                                                       */
125  /*    new_size :: The newly requested size in bytes.                     */
126  /*                                                                       */
127  /*    block    :: The current address of the block in memory.            */
128  /*                                                                       */
129  /* <Return>                                                              */
130  /*    The address of the reallocated memory block.                       */
131  /*                                                                       */
132  FT_CALLBACK_DEF( void* )
133  ft_realloc( FT_Memory  memory,
134              long       cur_size,
135              long       new_size,
136              void*      block )
137  {
138    FT_UNUSED( memory );
139    FT_UNUSED( cur_size );
140
141#ifdef _FOXIT_MEM_MANAGER_
142	return FXMEM_DefaultRealloc(block, new_size, 0);
143#else
144    return ft_srealloc( block, new_size );
145#endif
146  }
147
148
149  /*************************************************************************/
150  /*                                                                       */
151  /* <Function>                                                            */
152  /*    ft_free                                                            */
153  /*                                                                       */
154  /* <Description>                                                         */
155  /*    The memory release function.                                       */
156  /*                                                                       */
157  /* <Input>                                                               */
158  /*    memory  :: A pointer to the memory object.                         */
159  /*                                                                       */
160  /*    block   :: The address of block in memory to be freed.             */
161  /*                                                                       */
162  FT_CALLBACK_DEF( void )
163  ft_free( FT_Memory  memory,
164           void*      block )
165  {
166    FT_UNUSED( memory );
167
168#ifdef _FOXIT_MEM_MANAGER_
169	FXMEM_DefaultFree(block, 0);
170#else
171    ft_sfree( block );
172#endif
173  }
174
175
176  /*************************************************************************/
177  /*                                                                       */
178  /*                     RESOURCE MANAGEMENT INTERFACE                     */
179  /*                                                                       */
180  /*************************************************************************/
181
182#ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
183
184  /*************************************************************************/
185  /*                                                                       */
186  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
187  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
188  /* messages during execution.                                            */
189  /*                                                                       */
190#undef  FT_COMPONENT
191#define FT_COMPONENT  trace_io
192
193  /* We use the macro STREAM_FILE for convenience to extract the       */
194  /* system-specific stream handle from a given FreeType stream object */
195#define STREAM_FILE( stream )  ( (FT_FILE*)stream->descriptor.pointer )
196
197
198  /*************************************************************************/
199  /*                                                                       */
200  /* <Function>                                                            */
201  /*    ft_ansi_stream_close                                               */
202  /*                                                                       */
203  /* <Description>                                                         */
204  /*    The function to close a stream.                                    */
205  /*                                                                       */
206  /* <Input>                                                               */
207  /*    stream :: A pointer to the stream object.                          */
208  /*                                                                       */
209  FT_CALLBACK_DEF( void )
210  ft_ansi_stream_close( FT_Stream  stream )
211  {
212//#if _FX_COMPILER_ != _FX_IARCC_ && _FX_OS_ != _FX_PALMOS_
213    ft_fclose( STREAM_FILE( stream ) );
214
215    stream->descriptor.pointer = NULL;
216    stream->size               = 0;
217    stream->base               = 0;
218//#endif
219  }
220
221
222  /*************************************************************************/
223  /*                                                                       */
224  /* <Function>                                                            */
225  /*    ft_ansi_stream_io                                                  */
226  /*                                                                       */
227  /* <Description>                                                         */
228  /*    The function to open a stream.                                     */
229  /*                                                                       */
230  /* <Input>                                                               */
231  /*    stream :: A pointer to the stream object.                          */
232  /*                                                                       */
233  /*    offset :: The position in the data stream to start reading.        */
234  /*                                                                       */
235  /*    buffer :: The address of buffer to store the read data.            */
236  /*                                                                       */
237  /*    count  :: The number of bytes to read from the stream.             */
238  /*                                                                       */
239  /* <Return>                                                              */
240  /*    The number of bytes actually read.  If `count' is zero (this is,   */
241  /*    the function is used for seeking), a non-zero return value         */
242  /*    indicates an error.                                                */
243  /*                                                                       */
244  FT_CALLBACK_DEF( unsigned long )
245  ft_ansi_stream_io( FT_Stream       stream,
246                     unsigned long   offset,
247                     unsigned char*  buffer,
248                     unsigned long   count )
249  {
250//#if _FX_COMPILER_ != _FX_IARCC_ && _FX_OS_ != _FX_PALMOS_
251    FT_FILE*  file;
252
253
254    if ( !count && offset > stream->size )
255      return 1;
256
257    file = STREAM_FILE( stream );
258
259    if ( stream->pos != offset )
260      ft_fseek( file, offset, SEEK_SET );
261
262    return (unsigned long)ft_fread( buffer, 1, count, file );
263//#else
264//    return 0;
265//#endif
266  }
267
268
269  /* documentation is in ftstream.h */
270
271  FT_BASE_DEF( FT_Error )
272  FT_Stream_Open( FT_Stream    stream,
273                  const char*  filepathname )
274  {
275//#if _FX_COMPILER_ != _FX_IARCC_ && _FX_OS_ != _FX_PALMOS_
276    FT_FILE*  file;
277
278
279    if ( !stream )
280      return FT_THROW( Invalid_Stream_Handle );
281
282    stream->descriptor.pointer = NULL;
283    stream->pathname.pointer   = (char*)filepathname;
284    stream->base               = 0;
285    stream->pos                = 0;
286    stream->read               = NULL;
287    stream->close              = NULL;
288
289    file = ft_fopen( filepathname, "rb" );
290    if ( !file )
291    {
292      FT_ERROR(( "FT_Stream_Open:"
293                 " could not open `%s'\n", filepathname ));
294
295      return FT_THROW( Cannot_Open_Resource );
296    }
297
298    ft_fseek( file, 0, FXSYS_SEEK_END );
299    stream->size = ft_ftell( file );
300    if ( !stream->size )
301    {
302      FT_ERROR(( "FT_Stream_Open:" ));
303      FT_ERROR(( " opened `%s' but zero-sized\n", filepathname ));
304      ft_fclose( file );
305      return FT_THROW( Cannot_Open_Stream );
306    }
307    ft_fseek( file, 0, FXSYS_SEEK_SET );
308
309    stream->descriptor.pointer = file;
310    stream->read  = ft_ansi_stream_io;
311    stream->close = ft_ansi_stream_close;
312
313    FT_TRACE1(( "FT_Stream_Open:" ));
314    FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
315                filepathname, stream->size ));
316//#endif
317
318    return FT_Err_Ok;
319  }
320
321#endif /* !FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT */
322
323#ifdef FT_DEBUG_MEMORY
324
325  extern FT_Int
326  ft_mem_debug_init( FT_Memory  memory );
327
328  extern void
329  ft_mem_debug_done( FT_Memory  memory );
330
331#endif
332
333
334  /* documentation is in ftobjs.h */
335
336  FT_BASE_DEF( FT_Memory )
337  FT_New_Memory( void )
338  {
339    FT_Memory  memory;
340
341
342    memory = (FT_Memory)FXMEM_DefaultAlloc( sizeof ( *memory ), 0 );
343    if ( memory )
344    {
345      memory->user    = 0;
346      memory->alloc   = ft_alloc;
347#ifdef _XYQ_MEM_DEBUG
348	  memory->allocdebug = ft_allocdebug;
349#endif
350      memory->realloc = ft_realloc;
351      memory->free    = ft_free;
352#ifdef FT_DEBUG_MEMORY
353      ft_mem_debug_init( memory );
354#endif
355    }
356
357    return memory;
358  }
359
360
361  /* documentation is in ftobjs.h */
362
363  FT_BASE_DEF( void )
364  FT_Done_Memory( FT_Memory  memory )
365  {
366#ifdef FT_DEBUG_MEMORY
367    ft_mem_debug_done( memory );
368#endif
369    FXMEM_DefaultFree( memory, 0 );
370  }
371
372
373/* END */
374#endif
375
376