ftsystem_inf.c 13 KB

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