~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/builds/unix/ftsystem.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/*                                                                         */
 
3
/*  ftsystem.c                                                             */
 
4
/*                                                                         */
 
5
/*    Unix-specific FreeType low-level system interface (body).            */
 
6
/*                                                                         */
 
7
/*  Copyright 1996-2001, 2002, 2004 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
#include <errno.h>
 
71
 
 
72
 
 
73
  /*************************************************************************/
 
74
  /*                                                                       */
 
75
  /*                       MEMORY MANAGEMENT INTERFACE                     */
 
76
  /*                                                                       */
 
77
  /*************************************************************************/
 
78
 
 
79
 
 
80
  /*************************************************************************/
 
81
  /*                                                                       */
 
82
  /* <Function>                                                            */
 
83
  /*    ft_alloc                                                           */
 
84
  /*                                                                       */
 
85
  /* <Description>                                                         */
 
86
  /*    The memory allocation function.                                    */
 
87
  /*                                                                       */
 
88
  /* <Input>                                                               */
 
89
  /*    memory :: A pointer to the memory object.                          */
 
90
  /*                                                                       */
 
91
  /*    size   :: The requested size in bytes.                             */
 
92
  /*                                                                       */
 
93
  /* <Return>                                                              */
 
94
  /*    The address of newly allocated block.                              */
 
95
  /*                                                                       */
 
96
  FT_CALLBACK_DEF( void* )
 
97
  ft_alloc( FT_Memory  memory,
 
98
            long       size )
 
99
  {
 
100
    FT_UNUSED( memory );
 
101
 
 
102
    return malloc( size );
 
103
  }
 
104
 
 
105
 
 
106
  /*************************************************************************/
 
107
  /*                                                                       */
 
108
  /* <Function>                                                            */
 
109
  /*    ft_realloc                                                         */
 
110
  /*                                                                       */
 
111
  /* <Description>                                                         */
 
112
  /*    The memory reallocation function.                                  */
 
113
  /*                                                                       */
 
114
  /* <Input>                                                               */
 
115
  /*    memory   :: A pointer to the memory object.                        */
 
116
  /*                                                                       */
 
117
  /*    cur_size :: The current size of the allocated memory block.        */
 
118
  /*                                                                       */
 
119
  /*    new_size :: The newly requested size in bytes.                     */
 
120
  /*                                                                       */
 
121
  /*    block    :: The current address of the block in memory.            */
 
122
  /*                                                                       */
 
123
  /* <Return>                                                              */
 
124
  /*    The address of the reallocated memory block.                       */
 
125
  /*                                                                       */
 
126
  FT_CALLBACK_DEF( void* )
 
127
  ft_realloc( FT_Memory  memory,
 
128
              long       cur_size,
 
129
              long       new_size,
 
130
              void*      block )
 
131
  {
 
132
    FT_UNUSED( memory );
 
133
    FT_UNUSED( cur_size );
 
134
 
 
135
    return realloc( block, new_size );
 
136
  }
 
137
 
 
138
 
 
139
  /*************************************************************************/
 
140
  /*                                                                       */
 
141
  /* <Function>                                                            */
 
142
  /*    ft_free                                                            */
 
143
  /*                                                                       */
 
144
  /* <Description>                                                         */
 
145
  /*    The memory release function.                                       */
 
146
  /*                                                                       */
 
147
  /* <Input>                                                               */
 
148
  /*    memory :: A pointer to the memory object.                          */
 
149
  /*                                                                       */
 
150
  /*    block  :: The address of block in memory to be freed.              */
 
151
  /*                                                                       */
 
152
  FT_CALLBACK_DEF( void )
 
153
  ft_free( FT_Memory  memory,
 
154
           void*      block )
 
155
  {
 
156
    FT_UNUSED( memory );
 
157
 
 
158
    free( block );
 
159
  }
 
160
 
 
161
 
 
162
  /*************************************************************************/
 
163
  /*                                                                       */
 
164
  /*                     RESOURCE MANAGEMENT INTERFACE                     */
 
165
  /*                                                                       */
 
166
  /*************************************************************************/
 
167
 
 
168
 
 
169
  /*************************************************************************/
 
170
  /*                                                                       */
 
171
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
 
172
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
 
173
  /* messages during execution.                                            */
 
174
  /*                                                                       */
 
175
#undef  FT_COMPONENT
 
176
#define FT_COMPONENT  trace_io
 
177
 
 
178
  /* We use the macro STREAM_FILE for convenience to extract the       */
 
179
  /* system-specific stream handle from a given FreeType stream object */
 
180
#define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
 
181
 
 
182
 
 
183
  /*************************************************************************/
 
184
  /*                                                                       */
 
185
  /* <Function>                                                            */
 
186
  /*    ft_close_stream_by_munmap                                          */
 
187
  /*                                                                       */
 
188
  /* <Description>                                                         */
 
189
  /*    The function to close a stream which is opened by mmap.            */
 
190
  /*                                                                       */
 
191
  /* <Input>                                                               */
 
192
  /*    stream :: A pointer to the stream object.                          */
 
193
  /*                                                                       */
 
194
  FT_CALLBACK_DEF( void )
 
195
  ft_close_stream_by_munmap( FT_Stream  stream )
 
196
  {
 
197
    munmap( (MUNMAP_ARG_CAST)stream->descriptor.pointer, stream->size );
 
198
 
 
199
    stream->descriptor.pointer = NULL;
 
200
    stream->size               = 0;
 
201
    stream->base               = 0;
 
202
  }
 
203
 
 
204
 
 
205
  /*************************************************************************/
 
206
  /*                                                                       */
 
207
  /* <Function>                                                            */
 
208
  /*    ft_close_stream_by_free                                            */
 
209
  /*                                                                       */
 
210
  /* <Description>                                                         */
 
211
  /*    The function to close a stream which is created by ft_alloc.       */
 
212
  /*                                                                       */
 
213
  /* <Input>                                                               */
 
214
  /*    stream :: A pointer to the stream object.                          */
 
215
  /*                                                                       */
 
216
  FT_CALLBACK_DEF( void )
 
217
  ft_close_stream_by_free( FT_Stream  stream )
 
218
  {
 
219
    ft_free( NULL, stream->descriptor.pointer );
 
220
 
 
221
    stream->descriptor.pointer = NULL;
 
222
    stream->size               = 0;
 
223
    stream->base               = 0;
 
224
  }
 
225
 
 
226
 
 
227
  /* documentation is in ftobjs.h */
 
228
 
 
229
  FT_EXPORT_DEF( FT_Error )
 
230
  FT_Stream_Open( FT_Stream    stream,
 
231
                  const char*  filepathname )
 
232
  {
 
233
    int          file;
 
234
    struct stat  stat_buf;
 
235
 
 
236
 
 
237
    if ( !stream )
 
238
      return FT_Err_Invalid_Stream_Handle;
 
239
 
 
240
    /* open the file */
 
241
    file = open( filepathname, O_RDONLY );
 
242
    if ( file < 0 )
 
243
    {
 
244
      FT_ERROR(( "FT_Stream_Open:" ));
 
245
      FT_ERROR(( " could not open `%s'\n", filepathname ));
 
246
      return FT_Err_Cannot_Open_Resource;
 
247
    }
 
248
 
 
249
    /* Here we ensure that a "fork" will _not_ duplicate   */
 
250
    /* our opened input streams on Unix.  This is critical */
 
251
    /* since it avoids some (possible) access control      */
 
252
    /* issues and cleans up the kernel file table a bit.   */
 
253
    /*                                                     */
 
254
#ifdef F_SETFD
 
255
#ifdef FD_CLOEXEC
 
256
    (void)fcntl( file, F_SETFD, FD_CLOEXEC );
 
257
#else
 
258
    (void)fcntl( file, F_SETFD, 1 );
 
259
#endif /* FD_CLOEXEC */
 
260
#endif /* F_SETFD */
 
261
 
 
262
    if ( fstat( file, &stat_buf ) < 0 )
 
263
    {
 
264
      FT_ERROR(( "FT_Stream_Open:" ));
 
265
      FT_ERROR(( " could not `fstat' file `%s'\n", filepathname ));
 
266
      goto Fail_Map;
 
267
    }
 
268
 
 
269
    stream->size = stat_buf.st_size;
 
270
    stream->pos  = 0;
 
271
    stream->base = (unsigned char *)mmap( NULL,
 
272
                                          stream->size,
 
273
                                          PROT_READ,
 
274
                                          MAP_FILE | MAP_PRIVATE,
 
275
                                          file,
 
276
                                          0 );
 
277
 
 
278
    if ( (long)stream->base != -1 )
 
279
      stream->close = ft_close_stream_by_munmap;
 
280
    else
 
281
    {
 
282
      ssize_t  total_read_count;
 
283
    
 
284
 
 
285
      FT_ERROR(( "FT_Stream_Open:" ));
 
286
      FT_ERROR(( " could not `mmap' file `%s'\n", filepathname ));
 
287
      
 
288
      stream->base = ft_alloc( NULL, stream->size );
 
289
      
 
290
      if ( !stream->base )
 
291
      {
 
292
        FT_ERROR(( "FT_Stream_Open:" ));
 
293
        FT_ERROR(( " could not `alloc' memory\n" ));
 
294
        goto Fail_Map;
 
295
      }
 
296
      
 
297
      total_read_count = 0;
 
298
      do {
 
299
        ssize_t  read_count;
 
300
 
 
301
 
 
302
        read_count = read( file, 
 
303
                           stream->base + total_read_count, 
 
304
                           stream->size - total_read_count );
 
305
 
 
306
        if ( ( read_count == -1 ) )
 
307
        {
 
308
          if ( errno == EINTR )
 
309
            continue;
 
310
 
 
311
          FT_ERROR(( "FT_Stream_Open:" ));
 
312
          FT_ERROR(( " error while `read'ing file `%s'\n", filepathname ));
 
313
          goto Fail_Read;
 
314
        }
 
315
 
 
316
        total_read_count += read_count;
 
317
 
 
318
      } while ( total_read_count != stream->size );
 
319
 
 
320
      stream->close = ft_close_stream_by_free;
 
321
    }
 
322
 
 
323
    close( file );
 
324
 
 
325
    stream->descriptor.pointer = stream->base;
 
326
    stream->pathname.pointer   = (char*)filepathname;
 
327
 
 
328
    stream->read = 0;
 
329
 
 
330
    FT_TRACE1(( "FT_Stream_Open:" ));
 
331
    FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
 
332
                filepathname, stream->size ));
 
333
 
 
334
    return FT_Err_Ok;
 
335
 
 
336
  Fail_Read:
 
337
    ft_free( NULL, stream->base );
 
338
 
 
339
  Fail_Map:
 
340
    close( file );
 
341
 
 
342
    stream->base = NULL;
 
343
    stream->size = 0;
 
344
    stream->pos  = 0;
 
345
 
 
346
    return FT_Err_Cannot_Open_Stream;
 
347
  }
 
348
 
 
349
 
 
350
#ifdef FT_DEBUG_MEMORY
 
351
 
 
352
  extern FT_Int
 
353
  ft_mem_debug_init( FT_Memory  memory );
 
354
 
 
355
  extern void
 
356
  ft_mem_debug_done( FT_Memory  memory );
 
357
 
 
358
#endif
 
359
 
 
360
 
 
361
  /* documentation is in ftobjs.h */
 
362
 
 
363
  FT_EXPORT_DEF( FT_Memory )
 
364
  FT_New_Memory( void )
 
365
  {
 
366
    FT_Memory  memory;
 
367
 
 
368
 
 
369
    memory = (FT_Memory)malloc( sizeof ( *memory ) );
 
370
    if ( memory )
 
371
    {
 
372
      memory->user    = 0;
 
373
      memory->alloc   = ft_alloc;
 
374
      memory->realloc = ft_realloc;
 
375
      memory->free    = ft_free;
 
376
#ifdef FT_DEBUG_MEMORY
 
377
      ft_mem_debug_init( memory );
 
378
#endif
 
379
    }
 
380
 
 
381
    return memory;
 
382
  }
 
383
 
 
384
 
 
385
  /* documentation is in ftobjs.h */
 
386
 
 
387
  FT_EXPORT_DEF( void )
 
388
  FT_Done_Memory( FT_Memory  memory )
 
389
  {
 
390
#ifdef FT_DEBUG_MEMORY
 
391
    ft_mem_debug_done( memory );
 
392
#endif
 
393
    memory->free( memory, memory );
 
394
  }
 
395
 
 
396
 
 
397
/* END */