1
/***************************************************************************/
5
/* ANSI-specific FreeType low-level system interface (body). */
7
/* Copyright 1996-2001, 2002, 2006, 2008, 2009, 2010 by */
8
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16
/***************************************************************************/
18
/*************************************************************************/
20
/* This file contains the default interface used by FreeType to access */
21
/* low-level, i.e. memory management, i/o access as well as thread */
22
/* synchronisation. It can be replaced by user-specific routines if */
25
/*************************************************************************/
29
#include FT_CONFIG_CONFIG_H
30
#include FT_INTERNAL_DEBUG_H
31
#include FT_INTERNAL_STREAM_H
37
/*************************************************************************/
39
/* MEMORY MANAGEMENT INTERFACE */
41
/*************************************************************************/
43
/*************************************************************************/
45
/* It is not necessary to do any error checking for the */
46
/* allocation-related functions. This will be done by the higher level */
47
/* routines like ft_mem_alloc() or ft_mem_realloc(). */
49
/*************************************************************************/
52
/*************************************************************************/
58
/* The memory allocation function. */
61
/* memory :: A pointer to the memory object. */
63
/* size :: The requested size in bytes. */
66
/* The address of newly allocated block. */
68
FT_CALLBACK_DEF( void* )
69
ft_alloc( FT_Memory memory,
74
return ft_smalloc( size );
78
/*************************************************************************/
84
/* The memory reallocation function. */
87
/* memory :: A pointer to the memory object. */
89
/* cur_size :: The current size of the allocated memory block. */
91
/* new_size :: The newly requested size in bytes. */
93
/* block :: The current address of the block in memory. */
96
/* The address of the reallocated memory block. */
98
FT_CALLBACK_DEF( void* )
99
ft_realloc( FT_Memory memory,
105
FT_UNUSED( cur_size );
107
return ft_srealloc( block, new_size );
111
/*************************************************************************/
117
/* The memory release function. */
120
/* memory :: A pointer to the memory object. */
122
/* block :: The address of block in memory to be freed. */
124
FT_CALLBACK_DEF( void )
125
ft_free( FT_Memory memory,
134
/*************************************************************************/
136
/* RESOURCE MANAGEMENT INTERFACE */
138
/*************************************************************************/
141
/*************************************************************************/
143
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
144
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
145
/* messages during execution. */
148
#define FT_COMPONENT trace_io
150
/* We use the macro STREAM_FILE for convenience to extract the */
151
/* system-specific stream handle from a given FreeType stream object */
152
#define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer )
155
/*************************************************************************/
158
/* ft_ansi_stream_close */
161
/* The function to close a stream. */
164
/* stream :: A pointer to the stream object. */
166
FT_CALLBACK_DEF( void )
167
ft_ansi_stream_close( FT_Stream stream )
169
ft_fclose( STREAM_FILE( stream ) );
171
stream->descriptor.pointer = NULL;
177
/*************************************************************************/
180
/* ft_ansi_stream_io */
183
/* The function to open a stream. */
186
/* stream :: A pointer to the stream object. */
188
/* offset :: The position in the data stream to start reading. */
190
/* buffer :: The address of buffer to store the read data. */
192
/* count :: The number of bytes to read from the stream. */
195
/* The number of bytes actually read. If `count' is zero (this is, */
196
/* the function is used for seeking), a non-zero return value */
197
/* indicates an error. */
199
FT_CALLBACK_DEF( unsigned long )
200
ft_ansi_stream_io( FT_Stream stream,
201
unsigned long offset,
202
unsigned char* buffer,
203
unsigned long count )
208
if ( !count && offset > stream->size )
211
file = STREAM_FILE( stream );
213
if ( stream->pos != offset )
214
ft_fseek( file, offset, SEEK_SET );
216
return (unsigned long)ft_fread( buffer, 1, count, file );
220
/* documentation is in ftstream.h */
222
FT_BASE_DEF( FT_Error )
223
FT_Stream_Open( FT_Stream stream,
224
const char* filepathname )
230
return FT_Err_Invalid_Stream_Handle;
232
stream->descriptor.pointer = NULL;
233
stream->pathname.pointer = (char*)filepathname;
237
stream->close = NULL;
239
file = ft_fopen( filepathname, "rb" );
242
FT_ERROR(( "FT_Stream_Open:"
243
" could not open `%s'\n", filepathname ));
245
return FT_Err_Cannot_Open_Resource;
248
ft_fseek( file, 0, SEEK_END );
249
stream->size = ft_ftell( file );
252
FT_ERROR(( "FT_Stream_Open:" ));
253
FT_ERROR(( " opened `%s' but zero-sized\n", filepathname ));
255
return FT_Err_Cannot_Open_Stream;
257
ft_fseek( file, 0, SEEK_SET );
259
stream->descriptor.pointer = file;
260
stream->read = ft_ansi_stream_io;
261
stream->close = ft_ansi_stream_close;
263
FT_TRACE1(( "FT_Stream_Open:" ));
264
FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
265
filepathname, stream->size ));
271
#ifdef FT_DEBUG_MEMORY
274
ft_mem_debug_init( FT_Memory memory );
277
ft_mem_debug_done( FT_Memory memory );
282
/* documentation is in ftobjs.h */
284
FT_BASE_DEF( FT_Memory )
285
FT_New_Memory( void )
290
memory = (FT_Memory)ft_smalloc( sizeof ( *memory ) );
294
memory->alloc = ft_alloc;
295
memory->realloc = ft_realloc;
296
memory->free = ft_free;
297
#ifdef FT_DEBUG_MEMORY
298
ft_mem_debug_init( memory );
306
/* documentation is in ftobjs.h */
309
FT_Done_Memory( FT_Memory memory )
311
#ifdef FT_DEBUG_MEMORY
312
ft_mem_debug_done( memory );