🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
12 KiB

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