00001 /***************************************************************************/ 00002 /* */ 00003 /* ftsystem.c */ 00004 /* */ 00005 /* ANSI-specific FreeType low-level system interface (body). */ 00006 /* */ 00007 /* Copyright 1996-2001, 2002, 2006, 2008, 2009 by */ 00008 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 00009 /* */ 00010 /* This file is part of the FreeType project, and may only be used, */ 00011 /* modified, and distributed under the terms of the FreeType project */ 00012 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 00013 /* this file you indicate that you have read the license and */ 00014 /* understand and accept it fully. */ 00015 /* */ 00016 /***************************************************************************/ 00017 00018 /*************************************************************************/ 00019 /* */ 00020 /* This file contains the default interface used by FreeType to access */ 00021 /* low-level, i.e. memory management, i/o access as well as thread */ 00022 /* synchronisation. It can be replaced by user-specific routines if */ 00023 /* necessary. */ 00024 /* */ 00025 /*************************************************************************/ 00026 00027 00028 #include <ft2build.h> 00029 #include FT_CONFIG_CONFIG_H 00030 #include FT_INTERNAL_DEBUG_H 00031 #include FT_INTERNAL_STREAM_H 00032 #include FT_SYSTEM_H 00033 #include FT_ERRORS_H 00034 #include FT_TYPES_H 00035 00036 00037 /*************************************************************************/ 00038 /* */ 00039 /* MEMORY MANAGEMENT INTERFACE */ 00040 /* */ 00041 /*************************************************************************/ 00042 00043 /*************************************************************************/ 00044 /* */ 00045 /* It is not necessary to do any error checking for the */ 00046 /* allocation-related functions. This will be done by the higher level */ 00047 /* routines like ft_mem_alloc() or ft_mem_realloc(). */ 00048 /* */ 00049 /*************************************************************************/ 00050 00051 00052 /*************************************************************************/ 00053 /* */ 00054 /* <Function> */ 00055 /* ft_alloc */ 00056 /* */ 00057 /* <Description> */ 00058 /* The memory allocation function. */ 00059 /* */ 00060 /* <Input> */ 00061 /* memory :: A pointer to the memory object. */ 00062 /* */ 00063 /* size :: The requested size in bytes. */ 00064 /* */ 00065 /* <Return> */ 00066 /* The address of newly allocated block. */ 00067 /* */ 00068 FT_CALLBACK_DEF( void* ) 00069 ft_alloc( FT_Memory memory, 00070 long size ) 00071 { 00072 FT_UNUSED( memory ); 00073 00074 return ft_smalloc( size ); 00075 } 00076 00077 00078 /*************************************************************************/ 00079 /* */ 00080 /* <Function> */ 00081 /* ft_realloc */ 00082 /* */ 00083 /* <Description> */ 00084 /* The memory reallocation function. */ 00085 /* */ 00086 /* <Input> */ 00087 /* memory :: A pointer to the memory object. */ 00088 /* */ 00089 /* cur_size :: The current size of the allocated memory block. */ 00090 /* */ 00091 /* new_size :: The newly requested size in bytes. */ 00092 /* */ 00093 /* block :: The current address of the block in memory. */ 00094 /* */ 00095 /* <Return> */ 00096 /* The address of the reallocated memory block. */ 00097 /* */ 00098 FT_CALLBACK_DEF( void* ) 00099 ft_realloc( FT_Memory memory, 00100 long cur_size, 00101 long new_size, 00102 void* block ) 00103 { 00104 FT_UNUSED( memory ); 00105 FT_UNUSED( cur_size ); 00106 00107 return ft_srealloc( block, new_size ); 00108 } 00109 00110 00111 /*************************************************************************/ 00112 /* */ 00113 /* <Function> */ 00114 /* ft_free */ 00115 /* */ 00116 /* <Description> */ 00117 /* The memory release function. */ 00118 /* */ 00119 /* <Input> */ 00120 /* memory :: A pointer to the memory object. */ 00121 /* */ 00122 /* block :: The address of block in memory to be freed. */ 00123 /* */ 00124 FT_CALLBACK_DEF( void ) 00125 ft_free( FT_Memory memory, 00126 void* block ) 00127 { 00128 FT_UNUSED( memory ); 00129 00130 ft_sfree( block ); 00131 } 00132 00133 00134 /*************************************************************************/ 00135 /* */ 00136 /* RESOURCE MANAGEMENT INTERFACE */ 00137 /* */ 00138 /*************************************************************************/ 00139 00140 00141 /*************************************************************************/ 00142 /* */ 00143 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 00144 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 00145 /* messages during execution. */ 00146 /* */ 00147 #undef FT_COMPONENT 00148 #define FT_COMPONENT trace_io 00149 00150 /* We use the macro STREAM_FILE for convenience to extract the */ 00151 /* system-specific stream handle from a given FreeType stream object */ 00152 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer ) 00153 00154 00155 /*************************************************************************/ 00156 /* */ 00157 /* <Function> */ 00158 /* ft_ansi_stream_close */ 00159 /* */ 00160 /* <Description> */ 00161 /* The function to close a stream. */ 00162 /* */ 00163 /* <Input> */ 00164 /* stream :: A pointer to the stream object. */ 00165 /* */ 00166 FT_CALLBACK_DEF( void ) 00167 ft_ansi_stream_close( FT_Stream stream ) 00168 { 00169 ft_fclose( STREAM_FILE( stream ) ); 00170 00171 stream->descriptor.pointer = NULL; 00172 stream->size = 0; 00173 stream->base = 0; 00174 } 00175 00176 00177 /*************************************************************************/ 00178 /* */ 00179 /* <Function> */ 00180 /* ft_ansi_stream_io */ 00181 /* */ 00182 /* <Description> */ 00183 /* The function to open a stream. */ 00184 /* */ 00185 /* <Input> */ 00186 /* stream :: A pointer to the stream object. */ 00187 /* */ 00188 /* offset :: The position in the data stream to start reading. */ 00189 /* */ 00190 /* buffer :: The address of buffer to store the read data. */ 00191 /* */ 00192 /* count :: The number of bytes to read from the stream. */ 00193 /* */ 00194 /* <Return> */ 00195 /* The number of bytes actually read. */ 00196 /* */ 00197 FT_CALLBACK_DEF( unsigned long ) 00198 ft_ansi_stream_io( FT_Stream stream, 00199 unsigned long offset, 00200 unsigned char* buffer, 00201 unsigned long count ) 00202 { 00203 FT_FILE* file; 00204 00205 00206 file = STREAM_FILE( stream ); 00207 00208 if ( stream->pos != offset ) 00209 ft_fseek( file, offset, SEEK_SET ); 00210 00211 return (unsigned long)ft_fread( buffer, 1, count, file ); 00212 } 00213 00214 00215 /* documentation is in ftstream.h */ 00216 00217 FT_BASE_DEF( FT_Error ) 00218 FT_Stream_Open( FT_Stream stream, 00219 const char* filepathname ) 00220 { 00221 FT_FILE* file; 00222 00223 00224 if ( !stream ) 00225 return FT_Err_Invalid_Stream_Handle; 00226 00227 file = ft_fopen( filepathname, "rb" ); 00228 if ( !file ) 00229 { 00230 FT_ERROR(( "FT_Stream_Open:" 00231 " could not open `%s'\n", filepathname )); 00232 00233 return FT_Err_Cannot_Open_Resource; 00234 } 00235 00236 ft_fseek( file, 0, SEEK_END ); 00237 stream->size = ft_ftell( file ); 00238 ft_fseek( file, 0, SEEK_SET ); 00239 00240 stream->descriptor.pointer = file; 00241 stream->pathname.pointer = (char*)filepathname; 00242 stream->pos = 0; 00243 00244 stream->read = ft_ansi_stream_io; 00245 stream->close = ft_ansi_stream_close; 00246 00247 FT_TRACE1(( "FT_Stream_Open:" )); 00248 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n", 00249 filepathname, stream->size )); 00250 00251 return FT_Err_Ok; 00252 } 00253 00254 00255 #ifdef FT_DEBUG_MEMORY 00256 00257 extern FT_Int 00258 ft_mem_debug_init( FT_Memory memory ); 00259 00260 extern void 00261 ft_mem_debug_done( FT_Memory memory ); 00262 00263 #endif 00264 00265 00266 /* documentation is in ftobjs.h */ 00267 00268 FT_BASE_DEF( FT_Memory ) 00269 FT_New_Memory( void ) 00270 { 00271 FT_Memory memory; 00272 00273 00274 memory = (FT_Memory)ft_smalloc( sizeof ( *memory ) ); 00275 if ( memory ) 00276 { 00277 memory->user = 0; 00278 memory->alloc = ft_alloc; 00279 memory->realloc = ft_realloc; 00280 memory->free = ft_free; 00281 #ifdef FT_DEBUG_MEMORY 00282 ft_mem_debug_init( memory ); 00283 #endif 00284 } 00285 00286 return memory; 00287 } 00288 00289 00290 /* documentation is in ftobjs.h */ 00291 00292 FT_BASE_DEF( void ) 00293 FT_Done_Memory( FT_Memory memory ) 00294 { 00295 #ifdef FT_DEBUG_MEMORY 00296 ft_mem_debug_done( memory ); 00297 #endif 00298 ft_sfree( memory ); 00299 } 00300 00301 00302 /* END */