1 /***************************************************************************/
2 /* */
3 /* ftsystem.c */
4 /* */
5 /* ANSI-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 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 */
23 /* necessary. */
24 /* */
25 /*************************************************************************/
26
27 /* This is the Inferno version */
28
29
30 #include <ft2build.h>
31 #include FT_CONFIG_CONFIG_H
32 #include FT_INTERNAL_DEBUG_H
33 #include FT_SYSTEM_H
34 #include FT_ERRORS_H
35 #include FT_TYPES_H
36
37 #include "kernel.h"
38
39 /*#include <stdio.h>*/
40 /*#include <stdlib.h>*/
41
42
43 /*************************************************************************/
44 /* */
45 /* MEMORY MANAGEMENT INTERFACE */
46 /* */
47 /*************************************************************************/
48
49 /*************************************************************************/
50 /* */
51 /* It is not necessary to do any error checking for the */
52 /* allocation-related functions. This will be done by the higher level */
53 /* routines like FT_Alloc() or FT_Realloc(). */
54 /* */
55 /*************************************************************************/
56
57
58 /*************************************************************************/
59 /* */
60 /* <Function> */
61 /* ft_alloc */
62 /* */
63 /* <Description> */
64 /* The memory allocation function. */
65 /* */
66 /* <Input> */
67 /* memory :: A pointer to the memory object. */
68 /* */
69 /* size :: The requested size in bytes. */
70 /* */
71 /* <Return> */
72 /* The address of newly allocated block. */
73 /* */
74 FT_CALLBACK_DEF( void* )
ft_alloc(FT_Memory memory,long size)75 ft_alloc( FT_Memory memory,
76 long size )
77 {
78 FT_UNUSED( memory );
79
80 return malloc( size );
81 }
82
83
84 /*************************************************************************/
85 /* */
86 /* <Function> */
87 /* ft_realloc */
88 /* */
89 /* <Description> */
90 /* The memory reallocation function. */
91 /* */
92 /* <Input> */
93 /* memory :: A pointer to the memory object. */
94 /* */
95 /* cur_size :: The current size of the allocated memory block. */
96 /* */
97 /* new_size :: The newly requested size in bytes. */
98 /* */
99 /* block :: The current address of the block in memory. */
100 /* */
101 /* <Return> */
102 /* The address of the reallocated memory block. */
103 /* */
104 FT_CALLBACK_DEF( void* )
ft_realloc(FT_Memory memory,long cur_size,long new_size,void * block)105 ft_realloc( FT_Memory memory,
106 long cur_size,
107 long new_size,
108 void* block )
109 {
110 FT_UNUSED( memory );
111 FT_UNUSED( cur_size );
112
113 return realloc( block, new_size );
114 }
115
116
117 /*************************************************************************/
118 /* */
119 /* <Function> */
120 /* ft_free */
121 /* */
122 /* <Description> */
123 /* The memory release function. */
124 /* */
125 /* <Input> */
126 /* memory :: A pointer to the memory object. */
127 /* */
128 /* block :: The address of block in memory to be freed. */
129 /* */
130 FT_CALLBACK_DEF( void )
ft_free(FT_Memory memory,void * block)131 ft_free( FT_Memory memory,
132 void* block )
133 {
134 FT_UNUSED( memory );
135
136 free( block );
137 }
138
139
140 /*************************************************************************/
141 /* */
142 /* RESOURCE MANAGEMENT INTERFACE */
143 /* */
144 /*************************************************************************/
145
146
147 /*************************************************************************/
148 /* */
149 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
150 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
151 /* messages during execution. */
152 /* */
153 #undef FT_COMPONENT
154 #define FT_COMPONENT trace_io
155
156 /* We use the macro STREAM_FD for convenience to extract the */
157 /* fd from a given FreeType stream object */
158 #define STREAM_FD( stream ) ( (int)stream->descriptor.pointer )
159 #define CLOSED_FD (void*)-1
160
161
162 /*************************************************************************/
163 /* */
164 /* <Function> */
165 /* ft_ansi_stream_close */
166 /* */
167 /* <Description> */
168 /* The function to close a stream. */
169 /* */
170 /* <Input> */
171 /* stream :: A pointer to the stream object. */
172 /* */
173 FT_CALLBACK_DEF( void )
ft_ansi_stream_close(FT_Stream stream)174 ft_ansi_stream_close( FT_Stream stream )
175 {
176 kclose( STREAM_FD( stream ) );
177
178 stream->descriptor.pointer = CLOSED_FD;
179 stream->size = 0;
180 stream->base = 0;
181 }
182
183
184 /*************************************************************************/
185 /* */
186 /* <Function> */
187 /* ft_ansi_stream_io */
188 /* */
189 /* <Description> */
190 /* The function to open a stream. */
191 /* */
192 /* <Input> */
193 /* stream :: A pointer to the stream object. */
194 /* */
195 /* offset :: The position in the data stream to start reading. */
196 /* */
197 /* buffer :: The address of buffer to store the read data. */
198 /* */
199 /* count :: The number of bytes to read from the stream. */
200 /* */
201 /* <Return> */
202 /* The number of bytes actually read. */
203 /* */
204 FT_CALLBACK_DEF( unsigned long )
ft_ansi_stream_io(FT_Stream stream,unsigned long offset,unsigned char * buffer,unsigned long count)205 ft_ansi_stream_io( FT_Stream stream,
206 unsigned long offset,
207 unsigned char* buffer,
208 unsigned long count )
209 {
210 int fd;
211
212 fd = STREAM_FD( stream );
213 kseek( fd, offset, SEEK_SET );
214 if(count == 0)
215 return 0;
216 return (unsigned long)kread( fd, buffer, count);
217 }
218
219
220 /* documentation is in ftobjs.h */
221
222 FT_EXPORT_DEF( FT_Error )
FT_Stream_Open(FT_Stream stream,const char * filepathname)223 FT_Stream_Open( FT_Stream stream, const char* filepathname)
224 {
225 Dir *dir;
226 int file;
227
228 if ( !stream )
229 return FT_Err_Invalid_Stream_Handle;
230 file = kopen( (char*)filepathname, OREAD);
231 if ( file < 0) {
232 FT_ERROR(( "FT_Stream_Open:" ));
233 FT_ERROR(( " could not open `%s'\n", filepathname ));
234 return FT_Err_Cannot_Open_Resource;
235 }
236 dir = kdirfstat(file);
237 if (dir == nil) {
238 kclose(file);
239 FT_ERROR(( "FT_Stream_Open:" ));
240 FT_ERROR(( " could not stat `%s'\n", filepathname ));
241 return FT_Err_Cannot_Open_Resource;
242 }
243 stream->size = dir->length;
244 free(dir);
245
246 stream->descriptor.pointer = (void*)file;
247 stream->pathname.pointer = (char*)filepathname;
248 stream->pos = 0;
249
250 stream->read = ft_ansi_stream_io;
251 stream->close = ft_ansi_stream_close;
252
253 FT_TRACE1(( "FT_Stream_Open:" ));
254 FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
255 filepathname, stream->size ));
256
257 return FT_Err_Ok;
258 }
259
260
261 #ifdef FT_DEBUG_MEMORY
262
263 extern FT_Int
264 ft_mem_debug_init( FT_Memory memory );
265
266 extern void
267 ft_mem_debug_done( FT_Memory memory );
268
269 #endif
270
271
272 /* documentation is in ftobjs.h */
273
274 FT_EXPORT_DEF( FT_Memory )
FT_New_Memory(void)275 FT_New_Memory( void )
276 {
277 FT_Memory memory;
278
279
280 memory = (FT_Memory)malloc( sizeof ( *memory ) );
281 if ( memory )
282 {
283 memory->user = 0;
284 memory->alloc = ft_alloc;
285 memory->realloc = ft_realloc;
286 memory->free = ft_free;
287 #ifdef FT_DEBUG_MEMORY
288 ft_mem_debug_init( memory );
289 #endif
290 }
291
292 return memory;
293 }
294
295
296 /* documentation is in ftobjs.h */
297
298 FT_EXPORT_DEF( void )
FT_Done_Memory(FT_Memory memory)299 FT_Done_Memory( FT_Memory memory )
300 {
301 #ifdef FT_DEBUG_MEMORY
302 ft_mem_debug_done( memory );
303 #endif
304 memory->free( memory, memory );
305 }
306
307
308 /* END */
309