1 /***************************************************************************/
5 /* ANSI-specific FreeType low-level system interface (body). */
7 /* Copyright 1996-2001 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 /*************************************************************************/
29 #include FT_CONFIG_CONFIG_H
30 #include FT_INTERNAL_DEBUG_H
40 /*************************************************************************/
42 /* MEMORY MANAGEMENT INTERFACE */
44 /*************************************************************************/
46 /*************************************************************************/
48 /* It is not necessary to do any error checking for the */
49 /* allocation-related functions. This will be done by the higher level */
50 /* routines like FT_Alloc() or FT_Realloc(). */
52 /*************************************************************************/
55 /*************************************************************************/
61 /* The memory allocation function. */
64 /* memory :: A pointer to the memory object. */
66 /* size :: The requested size in bytes. */
69 /* The address of newly allocated block. */
71 FT_CALLBACK_DEF( void* )
72 ft_alloc( FT_Memory memory,
77 return malloc( size );
81 /*************************************************************************/
87 /* The memory reallocation function. */
90 /* memory :: A pointer to the memory object. */
92 /* cur_size :: The current size of the allocated memory block. */
94 /* new_size :: The newly requested size in bytes. */
96 /* block :: The current address of the block in memory. */
99 /* The address of the reallocated memory block. */
101 FT_CALLBACK_DEF( void* )
102 ft_realloc( FT_Memory memory,
108 FT_UNUSED( cur_size );
110 return realloc( block, new_size );
114 /*************************************************************************/
120 /* The memory release function. */
123 /* memory :: A pointer to the memory object. */
125 /* block :: The address of block in memory to be freed. */
127 FT_CALLBACK_DEF( void )
128 ft_free( FT_Memory memory,
137 /*************************************************************************/
139 /* RESOURCE MANAGEMENT INTERFACE */
141 /*************************************************************************/
144 /*************************************************************************/
146 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
147 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
148 /* messages during execution. */
151 #define FT_COMPONENT trace_io
153 /* We use the macro STREAM_FILE for convenience to extract the */
154 /* system-specific stream handle from a given FreeType stream object */
155 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
158 /*************************************************************************/
161 /* ft_close_stream */
164 /* The function to close a stream. */
167 /* stream :: A pointer to the stream object. */
169 FT_CALLBACK_DEF( void )
170 ft_close_stream( FT_Stream stream )
172 fclose( STREAM_FILE( stream ) );
174 stream->descriptor.pointer = NULL;
180 /*************************************************************************/
186 /* The function to open a stream. */
189 /* stream :: A pointer to the stream object. */
191 /* offset :: The position in the data stream to start reading. */
193 /* buffer :: The address of buffer to store the read data. */
195 /* count :: The number of bytes to read from the stream. */
198 /* The number of bytes actually read. */
200 FT_CALLBACK_DEF( unsigned long )
201 ft_io_stream( FT_Stream stream,
202 unsigned long offset,
203 unsigned char* buffer,
204 unsigned long count )
209 file = STREAM_FILE( stream );
211 fseek( file, offset, SEEK_SET );
213 return (unsigned long)fread( buffer, 1, count, file );
217 /* documentation is in ftobjs.h */
219 FT_EXPORT_DEF( FT_Error )
220 FT_New_Stream( const char* filepathname,
227 return FT_Err_Invalid_Stream_Handle;
229 file = fopen( filepathname, "rb" );
232 FT_ERROR(( "FT_New_Stream:" ));
233 FT_ERROR(( " could not open `%s'\n", filepathname ));
235 return FT_Err_Cannot_Open_Resource;
238 fseek( file, 0, SEEK_END );
239 astream->size = ftell( file );
240 fseek( file, 0, SEEK_SET );
242 astream->descriptor.pointer = file;
243 astream->pathname.pointer = (char*)filepathname;
246 astream->read = ft_io_stream;
247 astream->close = ft_close_stream;
249 FT_TRACE1(( "FT_New_Stream:" ));
250 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
251 filepathname, astream->size ));
257 /* documentation is in ftobjs.h */
259 FT_EXPORT_DEF( FT_Memory )
260 FT_New_Memory( void )
265 memory = (FT_Memory)malloc( sizeof ( *memory ) );
269 memory->alloc = ft_alloc;
270 memory->realloc = ft_realloc;
271 memory->free = ft_free;
278 /* documentation is in ftobjs.h */
280 FT_EXPORT_DEF( void )
281 FT_Done_Memory( FT_Memory memory )
283 memory->free( memory, memory );