1
0

ftmemory.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /***************************************************************************/
  2. /* */
  3. /* ftmemory.h */
  4. /* */
  5. /* The FreeType memory management macros (specification). */
  6. /* */
  7. /* Copyright 1996-2002, 2004-2007, 2010, 2013 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. #ifndef __FTMEMORY_H__
  18. #define __FTMEMORY_H__
  19. #include <ft2build.h>
  20. #include FT_CONFIG_CONFIG_H
  21. #include FT_TYPES_H
  22. FT_BEGIN_HEADER
  23. /*************************************************************************/
  24. /* */
  25. /* <Macro> */
  26. /* FT_SET_ERROR */
  27. /* */
  28. /* <Description> */
  29. /* This macro is used to set an implicit `error' variable to a given */
  30. /* expression's value (usually a function call), and convert it to a */
  31. /* boolean which is set whenever the value is != 0. */
  32. /* */
  33. #undef FT_SET_ERROR
  34. #define FT_SET_ERROR( expression ) \
  35. ( ( error = (expression) ) != 0 )
  36. /*************************************************************************/
  37. /*************************************************************************/
  38. /*************************************************************************/
  39. /**** ****/
  40. /**** ****/
  41. /**** M E M O R Y ****/
  42. /**** ****/
  43. /**** ****/
  44. /*************************************************************************/
  45. /*************************************************************************/
  46. /*************************************************************************/
  47. /*
  48. * C++ refuses to handle statements like p = (void*)anything, with `p' a
  49. * typed pointer. Since we don't have a `typeof' operator in standard
  50. * C++, we have to use a template to emulate it.
  51. */
  52. #ifdef __cplusplus
  53. extern "C++"
  54. template <typename T> inline T*
  55. cplusplus_typeof( T*,
  56. void *v )
  57. {
  58. return static_cast <T*> ( v );
  59. }
  60. #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
  61. #else
  62. #define FT_ASSIGNP( p, val ) (p) = (val)
  63. #endif
  64. #ifdef FT_DEBUG_MEMORY
  65. FT_BASE( const char* ) _ft_debug_file;
  66. FT_BASE( long ) _ft_debug_lineno;
  67. #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
  68. _ft_debug_lineno = __LINE__, \
  69. (exp) )
  70. #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
  71. _ft_debug_lineno = __LINE__, \
  72. FT_ASSIGNP( p, exp ) )
  73. #else /* !FT_DEBUG_MEMORY */
  74. #define FT_DEBUG_INNER( exp ) (exp)
  75. #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
  76. #endif /* !FT_DEBUG_MEMORY */
  77. /*
  78. * The allocation functions return a pointer, and the error code
  79. * is written to through the `p_error' parameter. See below for
  80. * for documentation.
  81. */
  82. FT_BASE( FT_Pointer )
  83. ft_mem_alloc( FT_Memory memory,
  84. FT_Long size,
  85. FT_Error *p_error );
  86. FT_BASE( FT_Pointer )
  87. ft_mem_qalloc( FT_Memory memory,
  88. FT_Long size,
  89. FT_Error *p_error );
  90. FT_BASE( FT_Pointer )
  91. ft_mem_realloc( FT_Memory memory,
  92. FT_Long item_size,
  93. FT_Long cur_count,
  94. FT_Long new_count,
  95. void* block,
  96. FT_Error *p_error );
  97. FT_BASE( FT_Pointer )
  98. ft_mem_qrealloc( FT_Memory memory,
  99. FT_Long item_size,
  100. FT_Long cur_count,
  101. FT_Long new_count,
  102. void* block,
  103. FT_Error *p_error );
  104. FT_BASE( void )
  105. ft_mem_free( FT_Memory memory,
  106. const void* P );
  107. #define FT_MEM_ALLOC( ptr, size ) \
  108. FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \
  109. (FT_Long)(size), \
  110. &error ) )
  111. #define FT_MEM_FREE( ptr ) \
  112. FT_BEGIN_STMNT \
  113. ft_mem_free( memory, (ptr) ); \
  114. (ptr) = NULL; \
  115. FT_END_STMNT
  116. #define FT_MEM_NEW( ptr ) \
  117. FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
  118. #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
  119. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
  120. 1, \
  121. (FT_Long)(cursz), \
  122. (FT_Long)(newsz), \
  123. (ptr), \
  124. &error ) )
  125. #define FT_MEM_QALLOC( ptr, size ) \
  126. FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \
  127. (FT_Long)(size), \
  128. &error ) )
  129. #define FT_MEM_QNEW( ptr ) \
  130. FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
  131. #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
  132. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
  133. 1, \
  134. (FT_Long)(cursz), \
  135. (FT_Long)(newsz), \
  136. (ptr), \
  137. &error ) )
  138. #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
  139. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
  140. (FT_Long)(item_size), \
  141. 0, \
  142. (FT_Long)(count), \
  143. NULL, \
  144. &error ) )
  145. #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  146. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
  147. (FT_Long)(itmsz), \
  148. (FT_Long)(oldcnt), \
  149. (FT_Long)(newcnt), \
  150. (ptr), \
  151. &error ) )
  152. #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
  153. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
  154. (FT_Long)(item_size), \
  155. 0, \
  156. (FT_Long)(count), \
  157. NULL, \
  158. &error ) )
  159. #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
  160. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
  161. (FT_Long)(itmsz), \
  162. (FT_Long)(oldcnt), \
  163. (FT_Long)(newcnt), \
  164. (ptr), \
  165. &error ) )
  166. #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
  167. #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
  168. #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
  169. #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
  170. #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
  171. #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
  172. #define FT_ARRAY_ZERO( dest, count ) \
  173. FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
  174. #define FT_ARRAY_COPY( dest, source, count ) \
  175. FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
  176. #define FT_ARRAY_MOVE( dest, source, count ) \
  177. FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
  178. /*
  179. * Return the maximum number of addressable elements in an array.
  180. * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
  181. * any problems.
  182. */
  183. #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
  184. #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
  185. /*************************************************************************/
  186. /* */
  187. /* The following functions macros expect that their pointer argument is */
  188. /* _typed_ in order to automatically compute array element sizes. */
  189. /* */
  190. #define FT_MEM_NEW_ARRAY( ptr, count ) \
  191. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
  192. sizeof ( *(ptr) ), \
  193. 0, \
  194. (FT_Long)(count), \
  195. NULL, \
  196. &error ) )
  197. #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
  198. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
  199. sizeof ( *(ptr) ), \
  200. (FT_Long)(cursz), \
  201. (FT_Long)(newsz), \
  202. (ptr), \
  203. &error ) )
  204. #define FT_MEM_QNEW_ARRAY( ptr, count ) \
  205. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
  206. sizeof ( *(ptr) ), \
  207. 0, \
  208. (FT_Long)(count), \
  209. NULL, \
  210. &error ) )
  211. #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
  212. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
  213. sizeof ( *(ptr) ), \
  214. (FT_Long)(cursz), \
  215. (FT_Long)(newsz), \
  216. (ptr), \
  217. &error ) )
  218. #define FT_ALLOC( ptr, size ) \
  219. FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
  220. #define FT_REALLOC( ptr, cursz, newsz ) \
  221. FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
  222. #define FT_ALLOC_MULT( ptr, count, item_size ) \
  223. FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
  224. #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  225. FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
  226. newcnt, itmsz ) )
  227. #define FT_QALLOC( ptr, size ) \
  228. FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
  229. #define FT_QREALLOC( ptr, cursz, newsz ) \
  230. FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
  231. #define FT_QALLOC_MULT( ptr, count, item_size ) \
  232. FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
  233. #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  234. FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
  235. newcnt, itmsz ) )
  236. #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
  237. #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
  238. #define FT_NEW_ARRAY( ptr, count ) \
  239. FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
  240. #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
  241. FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
  242. #define FT_QNEW( ptr ) \
  243. FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
  244. #define FT_QNEW_ARRAY( ptr, count ) \
  245. FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
  246. #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
  247. FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
  248. FT_BASE( FT_Pointer )
  249. ft_mem_strdup( FT_Memory memory,
  250. const char* str,
  251. FT_Error *p_error );
  252. FT_BASE( FT_Pointer )
  253. ft_mem_dup( FT_Memory memory,
  254. const void* address,
  255. FT_ULong size,
  256. FT_Error *p_error );
  257. #define FT_MEM_STRDUP( dst, str ) \
  258. (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
  259. #define FT_STRDUP( dst, str ) \
  260. FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
  261. #define FT_MEM_DUP( dst, address, size ) \
  262. (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
  263. #define FT_DUP( dst, address, size ) \
  264. FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
  265. /* Return >= 1 if a truncation occurs. */
  266. /* Return 0 if the source string fits the buffer. */
  267. /* This is *not* the same as strlcpy(). */
  268. FT_BASE( FT_Int )
  269. ft_mem_strcpyn( char* dst,
  270. const char* src,
  271. FT_ULong size );
  272. #define FT_STRCPYN( dst, src, size ) \
  273. ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
  274. /* */
  275. FT_END_HEADER
  276. #endif /* __FTMEMORY_H__ */
  277. /* END */