00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <ft2build.h>
00020
00021 #include <ftconfig.h>
00022 #include FT_INTERNAL_DEBUG_H
00023 #include FT_SYSTEM_H
00024 #include FT_ERRORS_H
00025 #include FT_TYPES_H
00026 #include FT_INTERNAL_OBJECTS_H
00027
00028
00029 #ifdef HAVE_UNISTD_H
00030 #include <unistd.h>
00031 #endif
00032
00033 #include <sys/mman.h>
00034 #ifndef MAP_FILE
00035 #define MAP_FILE 0x00
00036 #endif
00037
00038 #ifdef MUNMAP_USES_VOIDP
00039 #define MUNMAP_ARG_CAST void *
00040 #else
00041 #define MUNMAP_ARG_CAST char *
00042 #endif
00043
00044 #ifdef NEED_MUNMAP_DECL
00045
00046 #ifdef __cplusplus
00047 extern "C"
00048 #else
00049 extern
00050 #endif
00051 int
00052 munmap( char* addr,
00053 int len );
00054
00055 #define MUNMAP_ARG_CAST char *
00056
00057 #endif
00058
00059
00060 #include <sys/types.h>
00061 #include <sys/stat.h>
00062
00063 #ifdef HAVE_FCNTL_H
00064 #include <fcntl.h>
00065 #endif
00066
00067 #include <stdio.h>
00068 #include <stdlib.h>
00069 #include <string.h>
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 FT_CALLBACK_DEF( void* )
00096 ft_alloc( FT_Memory memory,
00097 long size )
00098 {
00099 FT_UNUSED( memory );
00100
00101 return malloc( size );
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 FT_CALLBACK_DEF( void* )
00126 ft_realloc( FT_Memory memory,
00127 long cur_size,
00128 long new_size,
00129 void* block )
00130 {
00131 FT_UNUSED( memory );
00132 FT_UNUSED( cur_size );
00133
00134 return realloc( block, new_size );
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 FT_CALLBACK_DEF( void )
00152 ft_free( FT_Memory memory,
00153 void* block )
00154 {
00155 FT_UNUSED( memory );
00156
00157 free( block );
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 #undef FT_COMPONENT
00175 #define FT_COMPONENT trace_io
00176
00177
00178
00179 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 FT_CALLBACK_DEF( void )
00194 ft_close_stream( FT_Stream stream )
00195 {
00196 munmap( (MUNMAP_ARG_CAST)stream->descriptor.pointer, stream->size );
00197
00198 stream->descriptor.pointer = NULL;
00199 stream->size = 0;
00200 stream->base = 0;
00201 }
00202
00203
00204
00205
00206 FT_BASE_DEF( FT_Error )
00207 FT_Stream_Open( FT_Stream stream,
00208 const char* filepathname )
00209 {
00210 int file;
00211 struct stat stat_buf;
00212
00213
00214 if ( !stream )
00215 return FT_Err_Invalid_Stream_Handle;
00216
00217
00218 file = open( filepathname, O_RDONLY );
00219 if ( file < 0 )
00220 {
00221 FT_ERROR(( "FT_Stream_Open:" ));
00222 FT_ERROR(( " could not open `%s'\n", filepathname ));
00223 return FT_Err_Cannot_Open_Resource;
00224 }
00225
00226 if ( fstat( file, &stat_buf ) < 0 )
00227 {
00228 FT_ERROR(( "FT_Stream_Open:" ));
00229 FT_ERROR(( " could not `fstat' file `%s'\n", filepathname ));
00230 goto Fail_Map;
00231 }
00232
00233 stream->size = stat_buf.st_size;
00234 stream->pos = 0;
00235 stream->base = (unsigned char *)mmap( NULL,
00236 stream->size,
00237 PROT_READ,
00238 MAP_FILE | MAP_PRIVATE,
00239 file,
00240 0 );
00241
00242 if ( (long)stream->base == -1 )
00243 {
00244 FT_ERROR(( "FT_Stream_Open:" ));
00245 FT_ERROR(( " could not `mmap' file `%s'\n", filepathname ));
00246 goto Fail_Map;
00247 }
00248
00249 close( file );
00250
00251 stream->descriptor.pointer = stream->base;
00252 stream->pathname.pointer = (char*)filepathname;
00253
00254 stream->close = ft_close_stream;
00255 stream->read = 0;
00256
00257 FT_TRACE1(( "FT_Stream_Open:" ));
00258 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
00259 filepathname, stream->size ));
00260
00261 return FT_Err_Ok;
00262
00263 Fail_Map:
00264 close( file );
00265
00266 stream->base = NULL;
00267 stream->size = 0;
00268 stream->pos = 0;
00269
00270 return FT_Err_Cannot_Open_Stream;
00271 }
00272
00273
00274 #ifdef FT_DEBUG_MEMORY
00275
00276 extern FT_Int
00277 ft_mem_debug_init( FT_Memory memory );
00278
00279 extern void
00280 ft_mem_debug_done( FT_Memory memory );
00281
00282 #endif
00283
00284
00285
00286
00287 FT_BASE_DEF( FT_Memory )
00288 FT_New_Memory( void )
00289 {
00290 FT_Memory memory;
00291
00292
00293 memory = (FT_Memory)malloc( sizeof ( *memory ) );
00294 if ( memory )
00295 {
00296 memory->user = 0;
00297 memory->alloc = ft_alloc;
00298 memory->realloc = ft_realloc;
00299 memory->free = ft_free;
00300 #ifdef FT_DEBUG_MEMORY
00301 ft_mem_debug_init( memory );
00302 #endif
00303 }
00304
00305 return memory;
00306 }
00307
00308
00309
00310
00311 FT_BASE_DEF( void )
00312 FT_Done_Memory( FT_Memory memory )
00313 {
00314 #ifdef FT_DEBUG_MEMORY
00315 ft_mem_debug_done( memory );
00316 #endif
00317 memory->free( memory, memory );
00318 }
00319
00320
00321