1 /***************************************************************************/
5 /* Unix-specific FreeType low-level system interface (body). */
7 /* Copyright 1996-2000 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 /***************************************************************************/
20 #include <freetype/internal/ftdebug.h>
21 #include <freetype/ftsystem.h>
22 #include <freetype/fterrors.h>
23 #include <freetype/fttypes.h>
30 /* memory-mapping includes and definitions */
40 /*************************************************************************/
42 /* The prototype for munmap() is not provided on SunOS. This needs to */
43 /* have a check added later to see if the GNU C library is being used. */
44 /* If so, then this prototype is not needed. */
46 #if defined( __sun__ ) && !defined( SVR4 ) && !defined( __SVR4 )
47 extern int munmap( caddr_t addr
,
62 /*************************************************************************/
64 /* MEMORY MANAGEMENT INTERFACE */
66 /*************************************************************************/
69 /*************************************************************************/
75 /* The memory allocation function. */
78 /* memory :: A pointer to the memory object. */
79 /* size :: The requested size in bytes. */
82 /* block :: The address of newly allocated block. */
85 void* ft_alloc( FT_Memory memory
,
90 return malloc( size
);
94 /*************************************************************************/
100 /* The memory reallocation function. */
103 /* memory :: A pointer to the memory object. */
105 /* cur_size :: The current size of the allocated memory block. */
107 /* new_size :: The newly requested size in bytes. */
109 /* block :: The current address of the block in memory. */
112 /* The address of the reallocated memory block. */
115 void* ft_realloc( FT_Memory memory
,
121 FT_UNUSED( cur_size
);
123 return realloc( block
, new_size
);
127 /*************************************************************************/
133 /* The memory release function. */
136 /* memory :: A pointer to the memory object. */
138 /* block :: The address of block in memory to be freed. */
141 void ft_free( FT_Memory memory
,
150 /*************************************************************************/
152 /* RESOURCE MANAGEMENT INTERFACE */
154 /*************************************************************************/
157 /*************************************************************************/
159 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
160 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
161 /* messages during execution. */
164 #define FT_COMPONENT trace_io
166 /* We use the macro STREAM_FILE for convenience to extract the */
167 /* system-specific stream handle from a given FreeType stream object */
168 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
171 /*************************************************************************/
174 /* ft_close_stream */
177 /* The function to close a stream. */
180 /* stream :: A pointer to the stream object. */
183 void ft_close_stream( FT_Stream stream
)
185 munmap ( stream
->descriptor
.pointer
, stream
->size
);
187 stream
->descriptor
.pointer
= NULL
;
193 /*************************************************************************/
199 /* Creates a new stream object. */
202 /* filepathname :: The name of the stream (usually a file) to be */
205 /* stream :: A pointer to the stream object. */
208 /* FreeType error code. 0 means success. */
210 FT_EXPORT_FUNC( FT_Error
) FT_New_Stream( const char* filepathname
,
214 struct stat stat_buf
;
218 return FT_Err_Invalid_Stream_Handle
;
221 file
= open( filepathname
, O_RDONLY
);
224 FT_ERROR(( "FT_New_Stream:" ));
225 FT_ERROR(( " could not open `%s'\n", filepathname
));
226 return FT_Err_Cannot_Open_Resource
;
229 if ( fstat( file
, &stat_buf
) < 0 )
231 FT_ERROR(( "FT_New_Stream:" ));
232 FT_ERROR(( " could not `fstat' file `%s'\n", filepathname
));
236 stream
->size
= stat_buf
.st_size
;
238 stream
->base
= mmap( NULL
,
241 MAP_FILE
| MAP_PRIVATE
,
245 if ( (long)stream
->base
== -1 )
247 FT_ERROR(( "FT_New_Stream:" ));
248 FT_ERROR(( " could not `mmap' file `%s'\n", filepathname
));
254 stream
->descriptor
.pointer
= stream
->base
;
255 stream
->pathname
.pointer
= (char*)filepathname
;
257 stream
->close
= ft_close_stream
;
260 FT_TRACE1(( "FT_New_Stream:" ));
261 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
262 filepathname
, stream
->size
));
273 return FT_Err_Cannot_Open_Stream
;
277 /*************************************************************************/
283 /* Creates a new memory object. */
286 /* A pointer to the new memory object. 0 in case of error. */
288 FT_EXPORT_FUNC( FT_Memory
) FT_New_Memory( void )
293 memory
= (FT_Memory
)malloc( sizeof ( *memory
) );
297 memory
->alloc
= ft_alloc
;
298 memory
->realloc
= ft_realloc
;
299 memory
->free
= ft_free
;