1 /***************************************************************************/
5 /* ANSI-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 /***************************************************************************/
18 /*************************************************************************/
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 */
25 /*************************************************************************/
28 #include <freetype/config/ftconfig.h>
29 #include <freetype/internal/ftdebug.h>
30 #include <freetype/ftsystem.h>
31 #include <freetype/fterrors.h>
32 #include <freetype/fttypes.h>
39 /*************************************************************************/
41 /* MEMORY MANAGEMENT INTERFACE */
43 /*************************************************************************/
45 /*************************************************************************/
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(). */
51 /*************************************************************************/
54 /*************************************************************************/
60 /* The memory allocation function. */
63 /* memory :: A pointer to the memory object. */
65 /* size :: The requested size in bytes. */
68 /* block :: The address of newly allocated block. */
71 void* ft_alloc( FT_Memory memory
,
76 return malloc( size
);
80 /*************************************************************************/
86 /* The memory reallocation function. */
89 /* memory :: A pointer to the memory object. */
91 /* cur_size :: The current size of the allocated memory block. */
93 /* new_size :: The newly requested size in bytes. */
95 /* block :: The current address of the block in memory. */
98 /* The address of the reallocated memory block. */
101 void* ft_realloc( FT_Memory memory
,
107 FT_UNUSED( cur_size
);
109 return realloc( block
, new_size
);
113 /*************************************************************************/
119 /* The memory release function. */
122 /* memory :: A pointer to the memory object. */
124 /* block :: The address of block in memory to be freed. */
127 void ft_free( FT_Memory memory
,
136 /*************************************************************************/
138 /* RESOURCE MANAGEMENT INTERFACE */
140 /*************************************************************************/
143 /*************************************************************************/
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. */
150 #define FT_COMPONENT trace_io
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 )
157 /*************************************************************************/
160 /* ft_close_stream */
163 /* The function to close a stream. */
166 /* stream :: A pointer to the stream object. */
169 void ft_close_stream( FT_Stream stream
)
171 fclose( STREAM_FILE( stream
) );
173 stream
->descriptor
.pointer
= NULL
;
179 /*************************************************************************/
185 /* The function to open a stream. */
188 /* stream :: A pointer to the stream object. */
190 /* offset :: The position in the data stream to start reading. */
192 /* buffer :: The address of buffer to store the read data. */
194 /* count :: The number of bytes to read from the stream. */
197 /* The number of bytes actually read. */
200 unsigned long ft_io_stream( FT_Stream stream
,
201 unsigned long offset
,
202 unsigned char* buffer
,
203 unsigned long count
)
208 file
= STREAM_FILE( stream
);
210 fseek( file
, offset
, SEEK_SET
);
212 return (unsigned long)fread( buffer
, 1, count
, file
);
216 /*************************************************************************/
222 /* Creates a new stream object. */
225 /* filepathname :: The name of the stream (usually a file) to be */
228 /* stream :: A pointer to the stream object. */
231 /* FreeType error code. 0 means success. */
233 FT_EXPORT_FUNC( FT_Error
) FT_New_Stream( const char* filepathname
,
240 return FT_Err_Invalid_Stream_Handle
;
242 file
= fopen( filepathname
, "rb" );
245 FT_ERROR(( "FT_New_Stream:" ));
246 FT_ERROR(( " could not open `%s'\n", filepathname
));
248 return FT_Err_Cannot_Open_Resource
;
251 fseek( file
, 0, SEEK_END
);
252 stream
->size
= ftell( file
);
253 fseek( file
, 0, SEEK_SET
);
255 stream
->descriptor
.pointer
= file
;
256 stream
->pathname
.pointer
= (char*)filepathname
;
259 stream
->read
= ft_io_stream
;
260 stream
->close
= ft_close_stream
;
262 FT_TRACE1(( "FT_New_Stream:" ));
263 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
264 filepathname
, stream
->size
));
270 /*************************************************************************/
276 /* Creates a new memory object. */
279 /* A pointer to the new memory object. 0 in case of error. */
281 FT_EXPORT_FUNC( FT_Memory
) FT_New_Memory( void )
286 memory
= (FT_Memory
)malloc( sizeof ( *memory
) );
290 memory
->alloc
= ft_alloc
;
291 memory
->realloc
= ft_realloc
;
292 memory
->free
= ft_free
;