]>
Commit | Line | Data |
---|---|---|
cabec872 RR |
1 | /***************************************************************************/ |
2 | /* */ | |
3 | /* ftsystem.c */ | |
4 | /* */ | |
5 | /* Unix-specific FreeType low-level system interface (body). */ | |
6 | /* */ | |
7 | /* Copyright 1996-2000 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 <ftconfig.h> | |
20 | #include <freetype/internal/ftdebug.h> | |
21 | #include <freetype/ftsystem.h> | |
22 | #include <freetype/fterrors.h> | |
23 | #include <freetype/fttypes.h> | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | ||
29 | ||
30 | /* memory-mapping includes and definitions */ | |
31 | #ifdef HAVE_UNISTD_H | |
32 | #include <unistd.h> | |
33 | #endif | |
34 | ||
35 | #include <sys/mman.h> | |
36 | #ifndef MAP_FILE | |
37 | #define MAP_FILE 0x00 | |
38 | #endif | |
39 | ||
40 | /*************************************************************************/ | |
41 | /* */ | |
42 | /* The prototype for munmap() is not provided on SunOS. This needs to */ | |
43 | /* have a check added later to see if the GNU C library is being used. */ | |
44 | /* If so, then this prototype is not needed. */ | |
45 | /* */ | |
46 | #if defined( __sun__ ) && !defined( SVR4 ) && !defined( __SVR4 ) | |
47 | extern int munmap( caddr_t addr, | |
48 | int len ); | |
49 | #endif | |
50 | ||
51 | #include <sys/stat.h> | |
52 | ||
53 | #ifdef HAVE_FCNTL_H | |
54 | #include <fcntl.h> | |
55 | #endif | |
56 | ||
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
60 | ||
61 | ||
62 | /*************************************************************************/ | |
63 | /* */ | |
64 | /* MEMORY MANAGEMENT INTERFACE */ | |
65 | /* */ | |
66 | /*************************************************************************/ | |
67 | ||
68 | ||
69 | /*************************************************************************/ | |
70 | /* */ | |
71 | /* <Function> */ | |
72 | /* ft_alloc */ | |
73 | /* */ | |
74 | /* <Description> */ | |
75 | /* The memory allocation function. */ | |
76 | /* */ | |
77 | /* <Input> */ | |
78 | /* memory :: A pointer to the memory object. */ | |
79 | /* size :: The requested size in bytes. */ | |
80 | /* */ | |
81 | /* <Return> */ | |
82 | /* block :: The address of newly allocated block. */ | |
83 | /* */ | |
84 | static | |
85 | void* ft_alloc( FT_Memory memory, | |
86 | long size ) | |
87 | { | |
88 | FT_UNUSED( memory ); | |
89 | ||
90 | return malloc( size ); | |
91 | } | |
92 | ||
93 | ||
94 | /*************************************************************************/ | |
95 | /* */ | |
96 | /* <Function> */ | |
97 | /* ft_realloc */ | |
98 | /* */ | |
99 | /* <Description> */ | |
100 | /* The memory reallocation function. */ | |
101 | /* */ | |
102 | /* <Input> */ | |
103 | /* memory :: A pointer to the memory object. */ | |
104 | /* */ | |
105 | /* cur_size :: The current size of the allocated memory block. */ | |
106 | /* */ | |
107 | /* new_size :: The newly requested size in bytes. */ | |
108 | /* */ | |
109 | /* block :: The current address of the block in memory. */ | |
110 | /* */ | |
111 | /* <Return> */ | |
112 | /* The address of the reallocated memory block. */ | |
113 | /* */ | |
114 | static | |
115 | void* ft_realloc( FT_Memory memory, | |
116 | long cur_size, | |
117 | long new_size, | |
118 | void* block ) | |
119 | { | |
120 | FT_UNUSED( memory ); | |
121 | FT_UNUSED( cur_size ); | |
122 | ||
123 | return realloc( block, new_size ); | |
124 | } | |
125 | ||
126 | ||
127 | /*************************************************************************/ | |
128 | /* */ | |
129 | /* <Function> */ | |
130 | /* ft_free */ | |
131 | /* */ | |
132 | /* <Description> */ | |
133 | /* The memory release function. */ | |
134 | /* */ | |
135 | /* <Input> */ | |
136 | /* memory :: A pointer to the memory object. */ | |
137 | /* */ | |
138 | /* block :: The address of block in memory to be freed. */ | |
139 | /* */ | |
140 | static | |
141 | void ft_free( FT_Memory memory, | |
142 | void* block ) | |
143 | { | |
144 | FT_UNUSED( memory ); | |
145 | ||
146 | free( block ); | |
147 | } | |
148 | ||
149 | ||
150 | /*************************************************************************/ | |
151 | /* */ | |
152 | /* RESOURCE MANAGEMENT INTERFACE */ | |
153 | /* */ | |
154 | /*************************************************************************/ | |
155 | ||
156 | ||
157 | /*************************************************************************/ | |
158 | /* */ | |
159 | /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ | |
160 | /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ | |
161 | /* messages during execution. */ | |
162 | /* */ | |
163 | #undef FT_COMPONENT | |
164 | #define FT_COMPONENT trace_io | |
165 | ||
166 | /* We use the macro STREAM_FILE for convenience to extract the */ | |
167 | /* system-specific stream handle from a given FreeType stream object */ | |
168 | #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer ) | |
169 | ||
170 | ||
171 | /*************************************************************************/ | |
172 | /* */ | |
173 | /* <Function> */ | |
174 | /* ft_close_stream */ | |
175 | /* */ | |
176 | /* <Description> */ | |
177 | /* The function to close a stream. */ | |
178 | /* */ | |
179 | /* <Input> */ | |
180 | /* stream :: A pointer to the stream object. */ | |
181 | /* */ | |
182 | static | |
183 | void ft_close_stream( FT_Stream stream ) | |
184 | { | |
185 | munmap ( stream->descriptor.pointer, stream->size ); | |
186 | ||
187 | stream->descriptor.pointer = NULL; | |
188 | stream->size = 0; | |
189 | stream->base = 0; | |
190 | } | |
191 | ||
192 | ||
193 | /*************************************************************************/ | |
194 | /* */ | |
195 | /* <Function> */ | |
196 | /* FT_New_Stream */ | |
197 | /* */ | |
198 | /* <Description> */ | |
199 | /* Creates a new stream object. */ | |
200 | /* */ | |
201 | /* <Input> */ | |
202 | /* filepathname :: The name of the stream (usually a file) to be */ | |
203 | /* opened. */ | |
204 | /* */ | |
205 | /* stream :: A pointer to the stream object. */ | |
206 | /* */ | |
207 | /* <Return> */ | |
208 | /* FreeType error code. 0 means success. */ | |
209 | /* */ | |
210 | FT_EXPORT_FUNC( FT_Error ) FT_New_Stream( const char* filepathname, | |
211 | FT_Stream stream ) | |
212 | { | |
213 | int file; | |
214 | struct stat stat_buf; | |
215 | ||
216 | ||
217 | if ( !stream ) | |
218 | return FT_Err_Invalid_Stream_Handle; | |
219 | ||
220 | /* open the file */ | |
221 | file = open( filepathname, O_RDONLY ); | |
222 | if ( file < 0 ) | |
223 | { | |
224 | FT_ERROR(( "FT_New_Stream:" )); | |
225 | FT_ERROR(( " could not open `%s'\n", filepathname )); | |
226 | return FT_Err_Cannot_Open_Resource; | |
227 | } | |
228 | ||
229 | if ( fstat( file, &stat_buf ) < 0 ) | |
230 | { | |
231 | FT_ERROR(( "FT_New_Stream:" )); | |
232 | FT_ERROR(( " could not `fstat' file `%s'\n", filepathname )); | |
233 | goto Fail_Map; | |
234 | } | |
235 | ||
236 | stream->size = stat_buf.st_size; | |
237 | stream->pos = 0; | |
238 | stream->base = mmap( NULL, | |
239 | stream->size, | |
240 | PROT_READ, | |
241 | MAP_FILE | MAP_PRIVATE, | |
242 | file, | |
243 | 0 ); | |
244 | ||
245 | if ( (long)stream->base == -1 ) | |
246 | { | |
247 | FT_ERROR(( "FT_New_Stream:" )); | |
248 | FT_ERROR(( " could not `mmap' file `%s'\n", filepathname )); | |
249 | goto Fail_Map; | |
250 | } | |
251 | ||
252 | close( file ); | |
253 | ||
254 | stream->descriptor.pointer = stream->base; | |
255 | stream->pathname.pointer = (char*)filepathname; | |
256 | ||
257 | stream->close = ft_close_stream; | |
258 | stream->read = 0; | |
259 | ||
260 | FT_TRACE1(( "FT_New_Stream:" )); | |
261 | FT_TRACE1(( " opened `%s' (%d bytes) successfully\n", | |
262 | filepathname, stream->size )); | |
263 | ||
264 | return FT_Err_Ok; | |
265 | ||
266 | Fail_Map: | |
267 | close( file ); | |
268 | ||
269 | stream->base = NULL; | |
270 | stream->size = 0; | |
271 | stream->pos = 0; | |
272 | ||
273 | return FT_Err_Cannot_Open_Stream; | |
274 | } | |
275 | ||
276 | ||
277 | /*************************************************************************/ | |
278 | /* */ | |
279 | /* <Function> */ | |
280 | /* FT_New_Memory */ | |
281 | /* */ | |
282 | /* <Description> */ | |
283 | /* Creates a new memory object. */ | |
284 | /* */ | |
285 | /* <Return> */ | |
286 | /* A pointer to the new memory object. 0 in case of error. */ | |
287 | /* */ | |
288 | FT_EXPORT_FUNC( FT_Memory ) 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 | } | |
301 | ||
302 | return memory; | |
303 | } | |
304 | ||
305 | ||
306 | /* END */ |