1 /***************************************************************************/ 2 /* */ 3 /* ftsystem.c */ 4 /* */ 5 /* ANSI-specific FreeType low-level system interface (body). */ 6 /* */ 7 /* Copyright 1996-2001, 2002 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 /*************************************************************************/ 19 /* */ 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 */ 23 /* necessary. */ 24 /* */ 25 /*************************************************************************/ 26 27 28 #include <ft2build.h> 29 #include FT_CONFIG_CONFIG_H 30 #include FT_INTERNAL_DEBUG_H 31 #include FT_SYSTEM_H 32 #include FT_ERRORS_H 33 #include FT_TYPES_H 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 38 39 /*************************************************************************/ 40 /* */ 41 /* MEMORY MANAGEMENT INTERFACE */ 42 /* */ 43 /*************************************************************************/ 44 45 /*************************************************************************/ 46 /* */ 47 /* It is not necessary to do any error checking for the */ 48 /* allocation-related functions. This will be done by the higher level */ 49 /* routines like FT_Alloc() or FT_Realloc(). */ 50 /* */ 51 /*************************************************************************/ 52 53 54 /*************************************************************************/ 55 /* */ 56 /* <Function> */ 57 /* ft_alloc */ 58 /* */ 59 /* <Description> */ 60 /* The memory allocation function. */ 61 /* */ 62 /* <Input> */ 63 /* memory :: A pointer to the memory object. */ 64 /* */ 65 /* size :: The requested size in bytes. */ 66 /* */ 67 /* <Return> */ 68 /* The address of newly allocated block. */ 69 /* */ 70 FT_CALLBACK_DEF( void* ) ft_alloc(FT_Memory memory,long size)71 ft_alloc( FT_Memory memory, 72 long size ) 73 { 74 FT_UNUSED( memory ); 75 76 return malloc( size ); 77 } 78 79 80 /*************************************************************************/ 81 /* */ 82 /* <Function> */ 83 /* ft_realloc */ 84 /* */ 85 /* <Description> */ 86 /* The memory reallocation function. */ 87 /* */ 88 /* <Input> */ 89 /* memory :: A pointer to the memory object. */ 90 /* */ 91 /* cur_size :: The current size of the allocated memory block. */ 92 /* */ 93 /* new_size :: The newly requested size in bytes. */ 94 /* */ 95 /* block :: The current address of the block in memory. */ 96 /* */ 97 /* <Return> */ 98 /* The address of the reallocated memory block. */ 99 /* */ 100 FT_CALLBACK_DEF( void* ) ft_realloc(FT_Memory memory,long cur_size,long new_size,void * block)101 ft_realloc( FT_Memory memory, 102 long cur_size, 103 long new_size, 104 void* block ) 105 { 106 FT_UNUSED( memory ); 107 FT_UNUSED( cur_size ); 108 109 return realloc( block, new_size ); 110 } 111 112 113 /*************************************************************************/ 114 /* */ 115 /* <Function> */ 116 /* ft_free */ 117 /* */ 118 /* <Description> */ 119 /* The memory release function. */ 120 /* */ 121 /* <Input> */ 122 /* memory :: A pointer to the memory object. */ 123 /* */ 124 /* block :: The address of block in memory to be freed. */ 125 /* */ 126 FT_CALLBACK_DEF( void ) ft_free(FT_Memory memory,void * block)127 ft_free( FT_Memory memory, 128 void* block ) 129 { 130 FT_UNUSED( memory ); 131 132 free( block ); 133 } 134 135 136 /*************************************************************************/ 137 /* */ 138 /* RESOURCE MANAGEMENT INTERFACE */ 139 /* */ 140 /*************************************************************************/ 141 142 143 /*************************************************************************/ 144 /* */ 145 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 146 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 147 /* messages during execution. */ 148 /* */ 149 #undef FT_COMPONENT 150 #define FT_COMPONENT trace_io 151 152 /* We use the macro STREAM_FILE for convenience to extract the */ 153 /* system-specific stream handle from a given FreeType stream object */ 154 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer ) 155 156 157 /*************************************************************************/ 158 /* */ 159 /* <Function> */ 160 /* ft_ansi_stream_close */ 161 /* */ 162 /* <Description> */ 163 /* The function to close a stream. */ 164 /* */ 165 /* <Input> */ 166 /* stream :: A pointer to the stream object. */ 167 /* */ 168 FT_CALLBACK_DEF( void ) ft_ansi_stream_close(FT_Stream stream)169 ft_ansi_stream_close( FT_Stream stream ) 170 { 171 fclose( STREAM_FILE( stream ) ); 172 173 stream->descriptor.pointer = NULL; 174 stream->size = 0; 175 stream->base = 0; 176 } 177 178 179 /*************************************************************************/ 180 /* */ 181 /* <Function> */ 182 /* ft_ansi_stream_io */ 183 /* */ 184 /* <Description> */ 185 /* The function to open a stream. */ 186 /* */ 187 /* <Input> */ 188 /* stream :: A pointer to the stream object. */ 189 /* */ 190 /* offset :: The position in the data stream to start reading. */ 191 /* */ 192 /* buffer :: The address of buffer to store the read data. */ 193 /* */ 194 /* count :: The number of bytes to read from the stream. */ 195 /* */ 196 /* <Return> */ 197 /* The number of bytes actually read. */ 198 /* */ 199 FT_CALLBACK_DEF( unsigned long ) ft_ansi_stream_io(FT_Stream stream,unsigned long offset,unsigned char * buffer,unsigned long count)200 ft_ansi_stream_io( FT_Stream stream, 201 unsigned long offset, 202 unsigned char* buffer, 203 unsigned long count ) 204 { 205 FILE* file; 206 207 208 file = STREAM_FILE( stream ); 209 210 fseek( file, offset, SEEK_SET ); 211 212 return (unsigned long)fread( buffer, 1, count, file ); 213 } 214 215 216 /* documentation is in ftobjs.h */ 217 218 FT_EXPORT_DEF( FT_Error ) FT_Stream_Open(FT_Stream stream,const char * filepathname)219 FT_Stream_Open( FT_Stream stream, 220 const char* filepathname ) 221 { 222 FILE* file; 223 224 225 if ( !stream ) 226 return FT_Err_Invalid_Stream_Handle; 227 228 file = fopen( filepathname, "rb" ); 229 if ( !file ) 230 { 231 FT_ERROR(( "FT_Stream_Open:" )); 232 FT_ERROR(( " could not open `%s'\n", filepathname )); 233 234 return FT_Err_Cannot_Open_Resource; 235 } 236 237 fseek( file, 0, SEEK_END ); 238 stream->size = ftell( file ); 239 fseek( file, 0, SEEK_SET ); 240 241 stream->descriptor.pointer = file; 242 stream->pathname.pointer = (char*)filepathname; 243 stream->pos = 0; 244 245 stream->read = ft_ansi_stream_io; 246 stream->close = ft_ansi_stream_close; 247 248 FT_TRACE1(( "FT_Stream_Open:" )); 249 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n", 250 filepathname, stream->size )); 251 252 return FT_Err_Ok; 253 } 254 255 256 #ifdef FT_DEBUG_MEMORY 257 258 extern FT_Int 259 ft_mem_debug_init( FT_Memory memory ); 260 261 extern void 262 ft_mem_debug_done( FT_Memory memory ); 263 264 #endif 265 266 267 /* documentation is in ftobjs.h */ 268 269 FT_EXPORT_DEF( FT_Memory ) FT_New_Memory(void)270 FT_New_Memory( void ) 271 { 272 FT_Memory memory; 273 274 275 memory = (FT_Memory)malloc( sizeof ( *memory ) ); 276 if ( memory ) 277 { 278 memory->user = 0; 279 memory->alloc = ft_alloc; 280 memory->realloc = ft_realloc; 281 memory->free = ft_free; 282 #ifdef FT_DEBUG_MEMORY 283 ft_mem_debug_init( memory ); 284 #endif 285 } 286 287 return memory; 288 } 289 290 291 /* documentation is in ftobjs.h */ 292 293 FT_EXPORT_DEF( void ) FT_Done_Memory(FT_Memory memory)294 FT_Done_Memory( FT_Memory memory ) 295 { 296 #ifdef FT_DEBUG_MEMORY 297 ft_mem_debug_done( memory ); 298 #endif 299 memory->free( memory, memory ); 300 } 301 302 303 /* END */ 304