update for HEAD-2003050101
[reactos.git] / lib / freetype / builds / amiga / src / base / ftsystem.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftsystem.c                                                             */
4 /*                                                                         */
5 /*    Amiga-specific FreeType low-level system interface (body).           */
6 /*                                                                         */
7 /*  Copyright 1996-2001, 2002 by                                           */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
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.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17
18   /*************************************************************************/
19   /*                                                                       */
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.                                                      */
23   /*                                                                       */
24   /*************************************************************************/
25
26
27 // Maintained by Detlef Würkner <TetiSoft@apg.lahn.de>
28
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
32
33 #include <exec/memory.h>
34
35 #ifdef __GNUC__
36 // Avoid warnings "struct X declared inside parameter list"
37 #include <exec/devices.h>
38 #include <exec/io.h>
39 #include <exec/semaphores.h>
40 #include <dos/exall.h>
41 #endif
42
43 // Necessary with OS3.9 includes
44 #define __USE_SYSBASE
45
46 #include <proto/exec.h>
47 #include <proto/dos.h>
48
49 #ifndef __GNUC__
50 /* TetiSoft: Missing in alib_protos.h, see amiga.lib autodoc
51  * (These amiga.lib functions work under AmigaOS V33 and up)
52  */
53 extern APTR __asm
54 AsmCreatePool( register __d0 ULONG             memFlags,
55                register __d1 ULONG             puddleSize,
56                register __d2 ULONG             threshSize,
57                register __a6 struct ExecBase*  SysBase );
58
59 extern VOID __asm
60 AsmDeletePool( register __a0 APTR              poolHeader,
61                register __a6 struct ExecBase*  SysBase );
62
63 extern APTR __asm
64 AsmAllocPooled( register __a0 APTR              poolHeader,
65                 register __d0 ULONG             memSize,
66                 register __a6 struct ExecBase*  SysBase );
67
68 extern VOID __asm
69 AsmFreePooled( register __a0 APTR              poolHeader,
70                register __a1 APTR              memory,
71                register __d0 ULONG             memSize,
72                register __a6 struct ExecBase*  SysBase);
73 #endif
74
75
76 // TetiSoft: C implementation of AllocVecPooled (see autodoc exec/AllocPooled)
77 APTR
78 AllocVecPooled( APTR   poolHeader,
79                 ULONG  memSize )
80 {
81   ULONG  newSize = memSize + sizeof ( ULONG );
82 #ifdef __GNUC__
83   ULONG  *mem = AllocPooled( poolHeader, newSize );
84 #else
85   ULONG  *mem = AsmAllocPooled( poolHeader, newSize, SysBase );
86 #endif
87
88   if ( !mem )
89     return NULL;
90   *mem = newSize;
91   return mem + 1;
92 }
93
94
95 // TetiSoft: C implementation of FreeVecPooled (see autodoc exec/AllocPooled)
96 void
97 FreeVecPooled( APTR  poolHeader,
98                APTR  memory )
99 {
100   ULONG  *realmem = (ULONG *)memory - 1;
101
102 #ifdef __GNUC__
103   FreePooled( poolHeader, realmem, *realmem );
104 #else
105  AsmFreePooled( poolHeader, realmem, *realmem, SysBase );
106 #endif
107 }
108
109
110 #include <ft2build.h>
111 #include FT_CONFIG_CONFIG_H
112 #include FT_INTERNAL_DEBUG_H
113 #include FT_SYSTEM_H
114 #include FT_ERRORS_H
115 #include FT_TYPES_H
116
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120
121
122   /*************************************************************************/
123   /*                                                                       */
124   /*                       MEMORY MANAGEMENT INTERFACE                     */
125   /*                                                                       */
126   /*************************************************************************/
127
128   /*************************************************************************/
129   /*                                                                       */
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().                             */
133   /*                                                                       */
134   /*************************************************************************/
135
136
137   /*************************************************************************/
138   /*                                                                       */
139   /* <Function>                                                            */
140   /*    ft_alloc                                                           */
141   /*                                                                       */
142   /* <Description>                                                         */
143   /*    The memory allocation function.                                    */
144   /*                                                                       */
145   /* <Input>                                                               */
146   /*    memory :: A pointer to the memory object.                          */
147   /*                                                                       */
148   /*    size   :: The requested size in bytes.                             */
149   /*                                                                       */
150   /* <Return>                                                              */
151   /*    The address of newly allocated block.                              */
152   /*                                                                       */
153   FT_CALLBACK_DEF( void* )
154   ft_alloc( FT_Memory  memory,
155             long       size )
156   {
157 //  FT_UNUSED( memory );
158
159 //  return malloc( size );
160     return AllocVecPooled( memory->user, size );
161   }
162
163
164   /*************************************************************************/
165   /*                                                                       */
166   /* <Function>                                                            */
167   /*    ft_realloc                                                         */
168   /*                                                                       */
169   /* <Description>                                                         */
170   /*    The memory reallocation function.                                  */
171   /*                                                                       */
172   /* <Input>                                                               */
173   /*    memory   :: A pointer to the memory object.                        */
174   /*                                                                       */
175   /*    cur_size :: The current size of the allocated memory block.        */
176   /*                                                                       */
177   /*    new_size :: The newly requested size in bytes.                     */
178   /*                                                                       */
179   /*    block    :: The current address of the block in memory.            */
180   /*                                                                       */
181   /* <Return>                                                              */
182   /*    The address of the reallocated memory block.                       */
183   /*                                                                       */
184   FT_CALLBACK_DEF( void* )
185   ft_realloc( FT_Memory  memory,
186               long       cur_size,
187               long       new_size,
188               void*      block )
189   {
190 //  FT_UNUSED( memory );
191 //  FT_UNUSED( cur_size );
192
193 //  return realloc( block, new_size );
194
195     void* new_block;
196
197     new_block = AllocVecPooled ( memory->user, new_size );
198     if ( new_block != NULL )
199     {
200       CopyMem ( block, new_block,
201                 ( new_size > cur_size ) ? cur_size : new_size );
202       FreeVecPooled ( memory->user, block );
203     }
204     return new_block;
205   }
206
207
208   /*************************************************************************/
209   /*                                                                       */
210   /* <Function>                                                            */
211   /*    ft_free                                                            */
212   /*                                                                       */
213   /* <Description>                                                         */
214   /*    The memory release function.                                       */
215   /*                                                                       */
216   /* <Input>                                                               */
217   /*    memory :: A pointer to the memory object.                          */
218   /*                                                                       */
219   /*    block  :: The address of block in memory to be freed.              */
220   /*                                                                       */
221   FT_CALLBACK_DEF( void )
222   ft_free( FT_Memory  memory,
223            void*      block )
224   {
225 //  FT_UNUSED( memory );
226
227 //  free( block );
228
229     FreeVecPooled( memory->user, block );
230   }
231
232
233   /*************************************************************************/
234   /*                                                                       */
235   /*                     RESOURCE MANAGEMENT INTERFACE                     */
236   /*                                                                       */
237   /*************************************************************************/
238
239
240   /*************************************************************************/
241   /*                                                                       */
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.                                            */
245   /*                                                                       */
246 #undef  FT_COMPONENT
247 #define FT_COMPONENT  trace_io
248
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
253
254
255   /*************************************************************************/
256   /*                                                                       */
257   /* <Function>                                                            */
258   /*    ft_close_stream                                                    */
259   /*                                                                       */
260   /* <Description>                                                         */
261   /*    The function to close a stream.                                    */
262   /*                                                                       */
263   /* <Input>                                                               */
264   /*    stream :: A pointer to the stream object.                          */
265   /*                                                                       */
266   FT_CALLBACK_DEF( void )
267   ft_close_stream( FT_Stream  stream )
268   {
269 //  fclose( STREAM_FILE( stream ) );
270     Close( STREAM_FILE( stream ) );     // TetiSoft
271
272     stream->descriptor.pointer = NULL;
273     stream->size               = 0;
274     stream->base               = 0;
275   }
276
277
278   /*************************************************************************/
279   /*                                                                       */
280   /* <Function>                                                            */
281   /*    ft_io_stream                                                       */
282   /*                                                                       */
283   /* <Description>                                                         */
284   /*    The function to open a stream.                                     */
285   /*                                                                       */
286   /* <Input>                                                               */
287   /*    stream :: A pointer to the stream object.                          */
288   /*                                                                       */
289   /*    offset :: The position in the data stream to start reading.        */
290   /*                                                                       */
291   /*    buffer :: The address of buffer to store the read data.            */
292   /*                                                                       */
293   /*    count  :: The number of bytes to read from the stream.             */
294   /*                                                                       */
295   /* <Return>                                                              */
296   /*    The number of bytes actually read.                                 */
297   /*                                                                       */
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 )
303   {
304 //  FILE*  file;
305     BPTR   file;        // TetiSoft
306
307
308     file = STREAM_FILE( stream );
309
310 //  fseek( file, offset, SEEK_SET );
311     Seek( file, offset, OFFSET_BEGINNING );     // TetiSoft
312
313 //  return (unsigned long)fread( buffer, 1, count, file );
314     return (unsigned long)FRead( file, buffer, 1, count);
315   }
316
317
318   /* documentation is in ftobjs.h */
319
320   FT_EXPORT_DEF( FT_Error )
321   FT_Stream_Open( FT_Stream    stream,
322                   const char*  filepathname )
323   {
324 //  FILE*                  file;
325     BPTR                   file; // TetiSoft
326     struct FileInfoBlock*  fib;  // TetiSoft
327
328
329     if ( !stream )
330       return FT_Err_Invalid_Stream_Handle;
331
332 //  file = fopen( filepathname, "rb" );
333     file = Open( filepathname, MODE_OLDFILE );  // TetiSoft
334     if ( !file )
335     {
336       FT_ERROR(( "FT_Stream_Open:" ));
337       FT_ERROR(( " could not open `%s'\n", filepathname ));
338
339       return FT_Err_Cannot_Open_Resource;
340     }
341
342 //  fseek( file, 0, SEEK_END );
343 //  astream->size = ftell( file );
344 //  fseek( file, 0, SEEK_SET );
345     fib = AllocDosObject( DOS_FIB, NULL );
346     if ( !fib )
347     {
348       Close ( file );
349       FT_ERROR(( "FT_Stream_Open:" ));
350       FT_ERROR(( " could not open `%s'\n", filepathname ));
351
352       return FT_Err_Cannot_Open_Resource;
353     }
354     if ( !( ExamineFH( file, fib ) ) )
355     {
356       FreeDosObject( DOS_FIB, fib );
357       Close ( file );
358       FT_ERROR(( "FT_Stream_Open:" ));
359       FT_ERROR(( " could not open `%s'\n", filepathname ));
360
361       return FT_Err_Cannot_Open_Resource;
362     }
363     stream->size = fib->fib_Size;
364     FreeDosObject( DOS_FIB, fib );
365
366 //  stream->descriptor.pointer = file;
367     stream->descriptor.pointer = (void *)file;
368
369     stream->pathname.pointer   = (char*)filepathname;
370     stream->pos                = 0;
371
372     stream->read  = ft_io_stream;
373     stream->close = ft_close_stream;
374
375     FT_TRACE1(( "FT_Stream_Open:" ));
376     FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
377                 filepathname, stream->size ));
378
379     return FT_Err_Ok;
380   }
381
382
383 #ifdef FT_DEBUG_MEMORY
384
385   extern FT_Int
386   ft_mem_debug_init( FT_Memory  memory );
387
388   extern void
389   ft_mem_debug_done( FT_Memory  memory );
390
391 #endif
392
393
394   /* documentation is in ftobjs.h */
395
396   FT_EXPORT_DEF( FT_Memory )
397   FT_New_Memory( void )
398   {
399     FT_Memory  memory;
400
401
402 //  memory = (FT_Memory)malloc( sizeof ( *memory ) );
403     memory = (FT_Memory)AllocVec( sizeof ( *memory ), MEMF_PUBLIC );
404     if ( memory )
405     {
406 //    memory->user = 0;
407 #ifdef __GNUC__
408       memory->user = CreatePool( MEMF_PUBLIC, 2048, 2048 );
409 #else
410       memory->user = AsmCreatePool( MEMF_PUBLIC, 2048, 2048, SysBase );
411 #endif
412       if ( memory->user == NULL )
413       {
414         FreeVec( memory );
415         memory = NULL;
416       }
417       else
418       {
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 );
424 #endif
425       }
426     }
427
428     return memory;
429   }
430
431
432   /* documentation is in ftobjs.h */
433
434   FT_EXPORT_DEF( void )
435   FT_Done_Memory( FT_Memory  memory )
436   {
437 #ifdef FT_DEBUG_MEMORY
438     ft_mem_debug_done( memory );
439 #endif
440
441 #ifdef __GNUC__
442     DeletePool( memory->user );
443 #else
444     AsmDeletePool( memory->user, SysBase );
445 #endif
446     FreeVec( memory );
447   }
448
449
450 /* END */