ftsystem.c

Go to the documentation of this file.
00001 /***************************************************************************/
00002 /*                                                                         */
00003 /*  ftsystem.c                                                             */
00004 /*                                                                         */
00005 /*    VMS-specific FreeType low-level system interface (body).             */
00006 /*                                                                         */
00007 /*  Copyright 1996-2001, 2002, 2005 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 #include <ft2build.h>
00020   /* we use our special ftconfig.h file, not the standard one */
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   /* memory-mapping includes and definitions */
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 /* NEED_DECLARATION_MUNMAP */
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   /*                       MEMORY MANAGEMENT INTERFACE                     */
00075   /*                                                                       */
00076   /*************************************************************************/
00077 
00078 
00079   /*************************************************************************/
00080   /*                                                                       */
00081   /* <Function>                                                            */
00082   /*    ft_alloc                                                           */
00083   /*                                                                       */
00084   /* <Description>                                                         */
00085   /*    The memory allocation function.                                    */
00086   /*                                                                       */
00087   /* <Input>                                                               */
00088   /*    memory :: A pointer to the memory object.                          */
00089   /*                                                                       */
00090   /*    size   :: The requested size in bytes.                             */
00091   /*                                                                       */
00092   /* <Return>                                                              */
00093   /*    The address of newly allocated block.                              */
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   /* <Function>                                                            */
00108   /*    ft_realloc                                                         */
00109   /*                                                                       */
00110   /* <Description>                                                         */
00111   /*    The memory reallocation function.                                  */
00112   /*                                                                       */
00113   /* <Input>                                                               */
00114   /*    memory   :: A pointer to the memory object.                        */
00115   /*                                                                       */
00116   /*    cur_size :: The current size of the allocated memory block.        */
00117   /*                                                                       */
00118   /*    new_size :: The newly requested size in bytes.                     */
00119   /*                                                                       */
00120   /*    block    :: The current address of the block in memory.            */
00121   /*                                                                       */
00122   /* <Return>                                                              */
00123   /*    The address of the reallocated memory block.                       */
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   /* <Function>                                                            */
00141   /*    ft_free                                                            */
00142   /*                                                                       */
00143   /* <Description>                                                         */
00144   /*    The memory release function.                                       */
00145   /*                                                                       */
00146   /* <Input>                                                               */
00147   /*    memory :: A pointer to the memory object.                          */
00148   /*                                                                       */
00149   /*    block  :: The address of block in memory to be freed.              */
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   /*                     RESOURCE MANAGEMENT INTERFACE                     */
00164   /*                                                                       */
00165   /*************************************************************************/
00166 
00167 
00168   /*************************************************************************/
00169   /*                                                                       */
00170   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
00171   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
00172   /* messages during execution.                                            */
00173   /*                                                                       */
00174 #undef  FT_COMPONENT
00175 #define FT_COMPONENT  trace_io
00176 
00177   /* We use the macro STREAM_FILE for convenience to extract the       */
00178   /* system-specific stream handle from a given FreeType stream object */
00179 #define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
00180 
00181 
00182   /*************************************************************************/
00183   /*                                                                       */
00184   /* <Function>                                                            */
00185   /*    ft_close_stream                                                    */
00186   /*                                                                       */
00187   /* <Description>                                                         */
00188   /*    The function to close a stream.                                    */
00189   /*                                                                       */
00190   /* <Input>                                                               */
00191   /*    stream :: A pointer to the stream object.                          */
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   /* documentation is in ftobjs.h */
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     /* open the file */
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   /* documentation is in ftobjs.h */
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   /* documentation is in ftobjs.h */
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 /* END */

Generated on Tue Jul 5 14:13:34 2011 for ROOT_528-00b_version by  doxygen 1.5.1