1 /***************************************************************************/
5 /* Amiga-specific FreeType low-level system interface (body). */
7 /* Copyright 1996-2001, 2002 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 Amiga interface used by FreeType to access */
21 /* low-level, i.e. memory management, i/o access as well as thread */
22 /* synchronisation. */
24 /*************************************************************************/
27 // Maintained by Detlef Würkner <TetiSoft@apg.lahn.de>
29 // TetiSoft: Modified to avoid fopen() fclose() fread() fseek() ftell()
30 // malloc() realloc() and free() which can't be used in an amiga
31 // shared run-time library linked with libinit.o
33 #include <exec/memory.h>
36 // Avoid warnings "struct X declared inside parameter list"
37 #include <exec/devices.h>
39 #include <exec/semaphores.h>
40 #include <dos/exall.h>
43 // Necessary with OS3.9 includes
46 #include <proto/exec.h>
47 #include <proto/dos.h>
50 /* TetiSoft: Missing in alib_protos.h, see amiga.lib autodoc
51 * (These amiga.lib functions work under AmigaOS V33 and up)
54 AsmCreatePool( register __d0 ULONG memFlags,
55 register __d1 ULONG puddleSize,
56 register __d2 ULONG threshSize,
57 register __a6 struct ExecBase* SysBase );
60 AsmDeletePool( register __a0 APTR poolHeader,
61 register __a6 struct ExecBase* SysBase );
64 AsmAllocPooled( register __a0 APTR poolHeader,
65 register __d0 ULONG memSize,
66 register __a6 struct ExecBase* SysBase );
69 AsmFreePooled( register __a0 APTR poolHeader,
70 register __a1 APTR memory,
71 register __d0 ULONG memSize,
72 register __a6 struct ExecBase* SysBase);
76 // TetiSoft: C implementation of AllocVecPooled (see autodoc exec/AllocPooled)
78 AllocVecPooled( APTR poolHeader,
81 ULONG newSize = memSize + sizeof ( ULONG );
83 ULONG *mem = AllocPooled( poolHeader, newSize );
85 ULONG *mem = AsmAllocPooled( poolHeader, newSize, SysBase );
95 // TetiSoft: C implementation of FreeVecPooled (see autodoc exec/AllocPooled)
97 FreeVecPooled( APTR poolHeader,
100 ULONG *realmem = (ULONG *)memory - 1;
103 FreePooled( poolHeader, realmem, *realmem );
105 AsmFreePooled( poolHeader, realmem, *realmem, SysBase );
110 #include <ft2build.h>
111 #include FT_CONFIG_CONFIG_H
112 #include FT_INTERNAL_DEBUG_H
122 /*************************************************************************/
124 /* MEMORY MANAGEMENT INTERFACE */
126 /*************************************************************************/
128 /*************************************************************************/
130 /* It is not necessary to do any error checking for the */
131 /* allocation-related functions. This will be done by the higher level */
132 /* routines like FT_Alloc() or FT_Realloc(). */
134 /*************************************************************************/
137 /*************************************************************************/
143 /* The memory allocation function. */
146 /* memory :: A pointer to the memory object. */
148 /* size :: The requested size in bytes. */
151 /* The address of newly allocated block. */
153 FT_CALLBACK_DEF( void* )
154 ft_alloc( FT_Memory memory,
157 // FT_UNUSED( memory );
159 // return malloc( size );
160 return AllocVecPooled( memory->user, size );
164 /*************************************************************************/
170 /* The memory reallocation function. */
173 /* memory :: A pointer to the memory object. */
175 /* cur_size :: The current size of the allocated memory block. */
177 /* new_size :: The newly requested size in bytes. */
179 /* block :: The current address of the block in memory. */
182 /* The address of the reallocated memory block. */
184 FT_CALLBACK_DEF( void* )
185 ft_realloc( FT_Memory memory,
190 // FT_UNUSED( memory );
191 // FT_UNUSED( cur_size );
193 // return realloc( block, new_size );
197 new_block = AllocVecPooled ( memory->user, new_size );
198 if ( new_block != NULL )
200 CopyMem ( block, new_block,
201 ( new_size > cur_size ) ? cur_size : new_size );
202 FreeVecPooled ( memory->user, block );
208 /*************************************************************************/
214 /* The memory release function. */
217 /* memory :: A pointer to the memory object. */
219 /* block :: The address of block in memory to be freed. */
221 FT_CALLBACK_DEF( void )
222 ft_free( FT_Memory memory,
225 // FT_UNUSED( memory );
229 FreeVecPooled( memory->user, block );
233 /*************************************************************************/
235 /* RESOURCE MANAGEMENT INTERFACE */
237 /*************************************************************************/
240 /*************************************************************************/
242 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
243 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
244 /* messages during execution. */
247 #define FT_COMPONENT trace_io
249 /* We use the macro STREAM_FILE for convenience to extract the */
250 /* system-specific stream handle from a given FreeType stream object */
251 // #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
252 #define STREAM_FILE( stream ) ( (BPTR)stream->descriptor.pointer ) // TetiSoft
255 /*************************************************************************/
258 /* ft_close_stream */
261 /* The function to close a stream. */
264 /* stream :: A pointer to the stream object. */
266 FT_CALLBACK_DEF( void )
267 ft_close_stream( FT_Stream stream )
269 // fclose( STREAM_FILE( stream ) );
270 Close( STREAM_FILE( stream ) ); // TetiSoft
272 stream->descriptor.pointer = NULL;
278 /*************************************************************************/
284 /* The function to open a stream. */
287 /* stream :: A pointer to the stream object. */
289 /* offset :: The position in the data stream to start reading. */
291 /* buffer :: The address of buffer to store the read data. */
293 /* count :: The number of bytes to read from the stream. */
296 /* The number of bytes actually read. */
298 FT_CALLBACK_DEF( unsigned long )
299 ft_io_stream( FT_Stream stream,
300 unsigned long offset,
301 unsigned char* buffer,
302 unsigned long count )
305 BPTR file; // TetiSoft
308 file = STREAM_FILE( stream );
310 // fseek( file, offset, SEEK_SET );
311 Seek( file, offset, OFFSET_BEGINNING ); // TetiSoft
313 // return (unsigned long)fread( buffer, 1, count, file );
314 return (unsigned long)FRead( file, buffer, 1, count);
318 /* documentation is in ftobjs.h */
320 FT_EXPORT_DEF( FT_Error )
321 FT_Stream_Open( FT_Stream stream,
322 const char* filepathname )
325 BPTR file; // TetiSoft
326 struct FileInfoBlock* fib; // TetiSoft
330 return FT_Err_Invalid_Stream_Handle;
332 // file = fopen( filepathname, "rb" );
333 file = Open( filepathname, MODE_OLDFILE ); // TetiSoft
336 FT_ERROR(( "FT_Stream_Open:" ));
337 FT_ERROR(( " could not open `%s'\n", filepathname ));
339 return FT_Err_Cannot_Open_Resource;
342 // fseek( file, 0, SEEK_END );
343 // astream->size = ftell( file );
344 // fseek( file, 0, SEEK_SET );
345 fib = AllocDosObject( DOS_FIB, NULL );
349 FT_ERROR(( "FT_Stream_Open:" ));
350 FT_ERROR(( " could not open `%s'\n", filepathname ));
352 return FT_Err_Cannot_Open_Resource;
354 if ( !( ExamineFH( file, fib ) ) )
356 FreeDosObject( DOS_FIB, fib );
358 FT_ERROR(( "FT_Stream_Open:" ));
359 FT_ERROR(( " could not open `%s'\n", filepathname ));
361 return FT_Err_Cannot_Open_Resource;
363 stream->size = fib->fib_Size;
364 FreeDosObject( DOS_FIB, fib );
366 // stream->descriptor.pointer = file;
367 stream->descriptor.pointer = (void *)file;
369 stream->pathname.pointer = (char*)filepathname;
372 stream->read = ft_io_stream;
373 stream->close = ft_close_stream;
375 FT_TRACE1(( "FT_Stream_Open:" ));
376 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
377 filepathname, stream->size ));
383 #ifdef FT_DEBUG_MEMORY
386 ft_mem_debug_init( FT_Memory memory );
389 ft_mem_debug_done( FT_Memory memory );
394 /* documentation is in ftobjs.h */
396 FT_EXPORT_DEF( FT_Memory )
397 FT_New_Memory( void )
402 // memory = (FT_Memory)malloc( sizeof ( *memory ) );
403 memory = (FT_Memory)AllocVec( sizeof ( *memory ), MEMF_PUBLIC );
408 memory->user = CreatePool( MEMF_PUBLIC, 2048, 2048 );
410 memory->user = AsmCreatePool( MEMF_PUBLIC, 2048, 2048, SysBase );
412 if ( memory->user == NULL )
419 memory->alloc = ft_alloc;
420 memory->realloc = ft_realloc;
421 memory->free = ft_free;
422 #ifdef FT_DEBUG_MEMORY
423 ft_mem_debug_init( memory );
432 /* documentation is in ftobjs.h */
434 FT_EXPORT_DEF( void )
435 FT_Done_Memory( FT_Memory memory )
437 #ifdef FT_DEBUG_MEMORY
438 ft_mem_debug_done( memory );
442 DeletePool( memory->user );
444 AsmDeletePool( memory->user, SysBase );