update for HEAD-2003050101
[reactos.git] / lib / freetype / builds / vms / ftsystem.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftsystem.c                                                             */
4 /*                                                                         */
5 /*    VMS-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 #include <ft2build.h>
20   /* we use our special ftconfig.h file, not the standard one */
21 #include <ftconfig.h>
22 #include FT_INTERNAL_DEBUG_H
23 #include FT_SYSTEM_H
24 #include FT_ERRORS_H
25 #include FT_TYPES_H
26 #include FT_INTERNAL_OBJECTS_H
27
28   /* memory-mapping includes and definitions */
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include <sys/mman.h>
34 #ifndef MAP_FILE
35 #define MAP_FILE  0x00
36 #endif
37
38 #ifdef MUNMAP_USES_VOIDP
39 #define MUNMAP_ARG_CAST  void *
40 #else
41 #define MUNMAP_ARG_CAST  char *
42 #endif
43
44 #ifdef NEED_MUNMAP_DECL
45
46 #ifdef __cplusplus
47   extern "C"
48 #else
49   extern
50 #endif
51   int
52   munmap( char*  addr,
53           int    len );
54
55 #define MUNMAP_ARG_CAST  char *
56
57 #endif /* NEED_DECLARATION_MUNMAP */
58
59
60 #include <sys/types.h>
61 #include <sys/stat.h>
62
63 #ifdef HAVE_FCNTL_H
64 #include <fcntl.h>
65 #endif
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70
71
72   /*************************************************************************/
73   /*                                                                       */
74   /*                       MEMORY MANAGEMENT INTERFACE                     */
75   /*                                                                       */
76   /*************************************************************************/
77
78
79   /*************************************************************************/
80   /*                                                                       */
81   /* <Function>                                                            */
82   /*    ft_alloc                                                           */
83   /*                                                                       */
84   /* <Description>                                                         */
85   /*    The memory allocation function.                                    */
86   /*                                                                       */
87   /* <Input>                                                               */
88   /*    memory :: A pointer to the memory object.                          */
89   /*                                                                       */
90   /*    size   :: The requested size in bytes.                             */
91   /*                                                                       */
92   /* <Return>                                                              */
93   /*    The address of newly allocated block.                              */
94   /*                                                                       */
95   FT_CALLBACK_DEF( void* )
96   ft_alloc( FT_Memory  memory,
97             long       size )
98   {
99     FT_UNUSED( memory );
100
101     return malloc( size );
102   }
103
104
105   /*************************************************************************/
106   /*                                                                       */
107   /* <Function>                                                            */
108   /*    ft_realloc                                                         */
109   /*                                                                       */
110   /* <Description>                                                         */
111   /*    The memory reallocation function.                                  */
112   /*                                                                       */
113   /* <Input>                                                               */
114   /*    memory   :: A pointer to the memory object.                        */
115   /*                                                                       */
116   /*    cur_size :: The current size of the allocated memory block.        */
117   /*                                                                       */
118   /*    new_size :: The newly requested size in bytes.                     */
119   /*                                                                       */
120   /*    block    :: The current address of the block in memory.            */
121   /*                                                                       */
122   /* <Return>                                                              */
123   /*    The address of the reallocated memory block.                       */
124   /*                                                                       */
125   FT_CALLBACK_DEF( void* )
126   ft_realloc( FT_Memory  memory,
127               long       cur_size,
128               long       new_size,
129               void*      block )
130   {
131     FT_UNUSED( memory );
132     FT_UNUSED( cur_size );
133
134     return realloc( block, new_size );
135   }
136
137
138   /*************************************************************************/
139   /*                                                                       */
140   /* <Function>                                                            */
141   /*    ft_free                                                            */
142   /*                                                                       */
143   /* <Description>                                                         */
144   /*    The memory release function.                                       */
145   /*                                                                       */
146   /* <Input>                                                               */
147   /*    memory :: A pointer to the memory object.                          */
148   /*                                                                       */
149   /*    block  :: The address of block in memory to be freed.              */
150   /*                                                                       */
151   FT_CALLBACK_DEF( void )
152   ft_free( FT_Memory  memory,
153            void*      block )
154   {
155     FT_UNUSED( memory );
156
157     free( block );
158   }
159
160
161   /*************************************************************************/
162   /*                                                                       */
163   /*                     RESOURCE MANAGEMENT INTERFACE                     */
164   /*                                                                       */
165   /*************************************************************************/
166
167
168   /*************************************************************************/
169   /*                                                                       */
170   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
171   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
172   /* messages during execution.                                            */
173   /*                                                                       */
174 #undef  FT_COMPONENT
175 #define FT_COMPONENT  trace_io
176
177   /* We use the macro STREAM_FILE for convenience to extract the       */
178   /* system-specific stream handle from a given FreeType stream object */
179 #define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
180
181
182   /*************************************************************************/
183   /*                                                                       */
184   /* <Function>                                                            */
185   /*    ft_close_stream                                                    */
186   /*                                                                       */
187   /* <Description>                                                         */
188   /*    The function to close a stream.                                    */
189   /*                                                                       */
190   /* <Input>                                                               */
191   /*    stream :: A pointer to the stream object.                          */
192   /*                                                                       */
193   FT_CALLBACK_DEF( void )
194   ft_close_stream( FT_Stream  stream )
195   {
196     munmap( (MUNMAP_ARG_CAST)stream->descriptor.pointer, stream->size );
197
198     stream->descriptor.pointer = NULL;
199     stream->size               = 0;
200     stream->base               = 0;
201   }
202
203
204   /* documentation is in ftobjs.h */
205
206   FT_EXPORT_DEF( FT_Error )
207   FT_Stream_Open( FT_Stream    stream,
208                   const char*  filepathname )
209   {
210     int          file;
211     struct stat  stat_buf;
212
213
214     if ( !stream )
215       return FT_Err_Invalid_Stream_Handle;
216
217     /* open the file */
218     file = open( filepathname, O_RDONLY );
219     if ( file < 0 )
220     {
221       FT_ERROR(( "FT_Stream_Open:" ));
222       FT_ERROR(( " could not open `%s'\n", filepathname ));
223       return FT_Err_Cannot_Open_Resource;
224     }
225
226     if ( fstat( file, &stat_buf ) < 0 )
227     {
228       FT_ERROR(( "FT_Stream_Open:" ));
229       FT_ERROR(( " could not `fstat' file `%s'\n", filepathname ));
230       goto Fail_Map;
231     }
232
233     stream->size = stat_buf.st_size;
234     stream->pos  = 0;
235     stream->base = (unsigned char *)mmap( NULL,
236                                           stream->size,
237                                           PROT_READ,
238                                           MAP_FILE | MAP_PRIVATE,
239                                           file,
240                                           0 );
241
242     if ( (long)stream->base == -1 )
243     {
244       FT_ERROR(( "FT_Stream_Open:" ));
245       FT_ERROR(( " could not `mmap' file `%s'\n", filepathname ));
246       goto Fail_Map;
247     }
248
249     close( file );
250
251     stream->descriptor.pointer = stream->base;
252     stream->pathname.pointer   = (char*)filepathname;
253
254     stream->close = ft_close_stream;
255     stream->read  = 0;
256
257     FT_TRACE1(( "FT_Stream_Open:" ));
258     FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
259                 filepathname, stream->size ));
260
261     return FT_Err_Ok;
262
263   Fail_Map:
264     close( file );
265
266     stream->base = NULL;
267     stream->size = 0;
268     stream->pos  = 0;
269
270     return FT_Err_Cannot_Open_Stream;
271   }
272
273
274 #ifdef FT_DEBUG_MEMORY
275
276   extern FT_Int
277   ft_mem_debug_init( FT_Memory  memory );
278
279   extern void
280   ft_mem_debug_done( FT_Memory  memory );
281
282 #endif
283
284
285   /* documentation is in ftobjs.h */
286
287   FT_EXPORT_DEF( FT_Memory )
288   FT_New_Memory( void )
289   {
290     FT_Memory  memory;
291
292
293     memory = (FT_Memory)malloc( sizeof ( *memory ) );
294     if ( memory )
295     {
296       memory->user    = 0;
297       memory->alloc   = ft_alloc;
298       memory->realloc = ft_realloc;
299       memory->free    = ft_free;
300 #ifdef FT_DEBUG_MEMORY
301       ft_mem_debug_init( memory );
302 #endif
303     }
304
305     return memory;
306   }
307
308
309   /* documentation is in ftobjs.h */
310
311   FT_EXPORT_DEF( void )
312   FT_Done_Memory( FT_Memory  memory )
313   {
314 #ifdef FT_DEBUG_MEMORY
315     ft_mem_debug_done( memory );
316 #endif
317     memory->free( memory, memory );
318   }
319
320
321 /* END */