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