1 /****************************************************************************
5 * ANSI-specific FreeType low-level system interface (body).
7 * Copyright (C) 1996-2023 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.
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
29 #include FT_CONFIG_CONFIG_H
30 #include <freetype/internal/ftdebug.h>
31 #include <freetype/internal/ftstream.h>
32 #include <freetype/ftsystem.h>
33 #include <freetype/fterrors.h>
34 #include <freetype/fttypes.h>
37 /**************************************************************************
39 * MEMORY MANAGEMENT INTERFACE
43 /**************************************************************************
45 * It is not necessary to do any error checking for the
46 * allocation-related functions. This will be done by the higher level
47 * routines like ft_mem_alloc() or ft_mem_realloc().
52 /**************************************************************************
58 * The memory allocation function.
62 * A pointer to the memory object.
65 * The requested size in bytes.
68 * The address of newly allocated block.
70 FT_CALLBACK_DEF( void* )
71 ft_alloc( FT_Memory memory,
76 return ft_smalloc( (size_t)size );
80 /**************************************************************************
86 * The memory reallocation function.
90 * A pointer to the memory object.
93 * The current size of the allocated memory block.
96 * The newly requested size in bytes.
99 * The current address of the block in memory.
102 * The address of the reallocated memory block.
104 FT_CALLBACK_DEF( void* )
105 ft_realloc( FT_Memory memory,
111 FT_UNUSED( cur_size );
113 return ft_srealloc( block, (size_t)new_size );
117 /**************************************************************************
123 * The memory release function.
127 * A pointer to the memory object.
130 * The address of block in memory to be freed.
132 FT_CALLBACK_DEF( void )
133 ft_free( FT_Memory memory,
142 /**************************************************************************
144 * RESOURCE MANAGEMENT INTERFACE
148 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
150 /**************************************************************************
152 * The macro FT_COMPONENT is used in trace mode. It is an implicit
153 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
154 * messages during execution.
157 #define FT_COMPONENT io
159 /* We use the macro STREAM_FILE for convenience to extract the */
160 /* system-specific stream handle from a given FreeType stream object */
161 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer )
164 /**************************************************************************
167 * ft_ansi_stream_close
170 * The function to close a stream.
174 * A pointer to the stream object.
176 FT_CALLBACK_DEF( void )
177 ft_ansi_stream_close( FT_Stream stream )
179 ft_fclose( STREAM_FILE( stream ) );
181 stream->descriptor.pointer = NULL;
187 /**************************************************************************
193 * The function to open a stream.
197 * A pointer to the stream object.
200 * The position in the data stream to start reading.
203 * The address of buffer to store the read data.
206 * The number of bytes to read from the stream.
209 * The number of bytes actually read. If `count' is zero (that is,
210 * the function is used for seeking), a non-zero return value
211 * indicates an error.
213 FT_CALLBACK_DEF( unsigned long )
214 ft_ansi_stream_io( FT_Stream stream,
215 unsigned long offset,
216 unsigned char* buffer,
217 unsigned long count )
222 if ( offset > stream->size && !count )
225 file = STREAM_FILE( stream );
227 if ( stream->pos != offset )
228 ft_fseek( file, (long)offset, SEEK_SET );
230 /* Avoid calling `fread` with `buffer=NULL` and `count=0`, */
231 /* which is undefined behaviour. */
235 return (unsigned long)ft_fread( buffer, 1, count, file );
239 /* documentation is in ftstream.h */
241 FT_BASE_DEF( FT_Error )
242 FT_Stream_Open( FT_Stream stream,
243 const char* filepathname )
249 return FT_THROW( Invalid_Stream_Handle );
251 stream->descriptor.pointer = NULL;
252 stream->pathname.pointer = (char*)filepathname;
256 stream->close = NULL;
258 file = ft_fopen( filepathname, "rb" );
261 FT_ERROR(( "FT_Stream_Open:"
262 " could not open `%s'\n", filepathname ));
264 return FT_THROW( Cannot_Open_Resource );
267 ft_fseek( file, 0, SEEK_END );
268 stream->size = (unsigned long)ft_ftell( file );
271 FT_ERROR(( "FT_Stream_Open:" ));
272 FT_ERROR(( " opened `%s' but zero-sized\n", filepathname ));
274 return FT_THROW( Cannot_Open_Stream );
276 ft_fseek( file, 0, SEEK_SET );
278 stream->descriptor.pointer = file;
279 stream->read = ft_ansi_stream_io;
280 stream->close = ft_ansi_stream_close;
282 FT_TRACE1(( "FT_Stream_Open:" ));
283 FT_TRACE1(( " opened `%s' (%ld bytes) successfully\n",
284 filepathname, stream->size ));
289 #endif /* !FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT */
291 #ifdef FT_DEBUG_MEMORY
294 ft_mem_debug_init( FT_Memory memory );
297 ft_mem_debug_done( FT_Memory memory );
302 /* documentation is in ftobjs.h */
304 FT_BASE_DEF( FT_Memory )
305 FT_New_Memory( void )
310 memory = (FT_Memory)ft_smalloc( sizeof ( *memory ) );
314 memory->alloc = ft_alloc;
315 memory->realloc = ft_realloc;
316 memory->free = ft_free;
317 #ifdef FT_DEBUG_MEMORY
318 ft_mem_debug_init( memory );
326 /* documentation is in ftobjs.h */
329 FT_Done_Memory( FT_Memory memory )
331 #ifdef FT_DEBUG_MEMORY
332 ft_mem_debug_done( memory );