/src/compiler/android-ndk/jni/freetype/include/freetype/internal/ftmemory.h

http://ftk.googlecode.com/ · C++ Header · 368 lines · 201 code · 105 blank · 62 comment · 2 complexity · 3e7f19575062443fee63bccf6f8b1f04 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftmemory.h */
  4. /* */
  5. /* The FreeType memory management macros (specification). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2004, 2005, 2006, 2007 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; where `p'
  49. * is a typed pointer. Since we don't have a `typeof' operator in
  50. * standard C++, we have to use ugly casts.
  51. */
  52. #ifdef __cplusplus
  53. #define FT_ASSIGNP( p, val ) *((void**)&(p)) = (val)
  54. #else
  55. #define FT_ASSIGNP( p, val ) (p) = (val)
  56. #endif
  57. #ifdef FT_DEBUG_MEMORY
  58. FT_BASE( const char* ) _ft_debug_file;
  59. FT_BASE( long ) _ft_debug_lineno;
  60. #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
  61. _ft_debug_lineno = __LINE__, \
  62. (exp) )
  63. #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
  64. _ft_debug_lineno = __LINE__, \
  65. FT_ASSIGNP( p, exp ) )
  66. #else /* !FT_DEBUG_MEMORY */
  67. #define FT_DEBUG_INNER( exp ) (exp)
  68. #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
  69. #endif /* !FT_DEBUG_MEMORY */
  70. /*
  71. * The allocation functions return a pointer, and the error code
  72. * is written to through the `p_error' parameter. See below for
  73. * for documentation.
  74. */
  75. FT_BASE( FT_Pointer )
  76. ft_mem_alloc( FT_Memory memory,
  77. FT_Long size,
  78. FT_Error *p_error );
  79. FT_BASE( FT_Pointer )
  80. ft_mem_qalloc( FT_Memory memory,
  81. FT_Long size,
  82. FT_Error *p_error );
  83. FT_BASE( FT_Pointer )
  84. ft_mem_realloc( FT_Memory memory,
  85. FT_Long item_size,
  86. FT_Long cur_count,
  87. FT_Long new_count,
  88. void* block,
  89. FT_Error *p_error );
  90. FT_BASE( FT_Pointer )
  91. ft_mem_qrealloc( 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( void )
  98. ft_mem_free( FT_Memory memory,
  99. const void* P );
  100. #define FT_MEM_ALLOC( ptr, size ) \
  101. FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, (size), &error ) )
  102. #define FT_MEM_FREE( ptr ) \
  103. FT_BEGIN_STMNT \
  104. ft_mem_free( memory, (ptr) ); \
  105. (ptr) = NULL; \
  106. FT_END_STMNT
  107. #define FT_MEM_NEW( ptr ) \
  108. FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
  109. #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
  110. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, 1, \
  111. (cursz), (newsz), \
  112. (ptr), &error ) )
  113. #define FT_MEM_QALLOC( ptr, size ) \
  114. FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, (size), &error ) )
  115. #define FT_MEM_QNEW( ptr ) \
  116. FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
  117. #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
  118. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, 1, \
  119. (cursz), (newsz), \
  120. (ptr), &error ) )
  121. #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
  122. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
  123. (cursz), (newsz), \
  124. (ptr), &error ) )
  125. #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
  126. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (item_size), \
  127. 0, (count), \
  128. NULL, &error ) )
  129. #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  130. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (itmsz), \
  131. (oldcnt), (newcnt), \
  132. (ptr), &error ) )
  133. #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
  134. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (item_size), \
  135. 0, (count), \
  136. NULL, &error ) )
  137. #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
  138. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (itmsz), \
  139. (oldcnt), (newcnt), \
  140. (ptr), &error ) )
  141. #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
  142. #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
  143. #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
  144. #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
  145. #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
  146. #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
  147. #define FT_ARRAY_ZERO( dest, count ) \
  148. FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
  149. #define FT_ARRAY_COPY( dest, source, count ) \
  150. FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
  151. #define FT_ARRAY_MOVE( dest, source, count ) \
  152. FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
  153. /*
  154. * Return the maximum number of addressable elements in an array.
  155. * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
  156. * any problems.
  157. */
  158. #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
  159. #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
  160. /*************************************************************************/
  161. /* */
  162. /* The following functions macros expect that their pointer argument is */
  163. /* _typed_ in order to automatically compute array element sizes. */
  164. /* */
  165. #define FT_MEM_NEW_ARRAY( ptr, count ) \
  166. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
  167. 0, (count), \
  168. NULL, &error ) )
  169. #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
  170. FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
  171. (cursz), (newsz), \
  172. (ptr), &error ) )
  173. #define FT_MEM_QNEW_ARRAY( ptr, count ) \
  174. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
  175. 0, (count), \
  176. NULL, &error ) )
  177. #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
  178. FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
  179. (cursz), (newsz), \
  180. (ptr), &error ) )
  181. #define FT_ALLOC( ptr, size ) \
  182. FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
  183. #define FT_REALLOC( ptr, cursz, newsz ) \
  184. FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
  185. #define FT_ALLOC_MULT( ptr, count, item_size ) \
  186. FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
  187. #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  188. FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
  189. newcnt, itmsz ) )
  190. #define FT_QALLOC( ptr, size ) \
  191. FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
  192. #define FT_QREALLOC( ptr, cursz, newsz ) \
  193. FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
  194. #define FT_QALLOC_MULT( ptr, count, item_size ) \
  195. FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
  196. #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
  197. FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
  198. newcnt, itmsz ) )
  199. #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
  200. #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
  201. #define FT_NEW_ARRAY( ptr, count ) \
  202. FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
  203. #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
  204. FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
  205. #define FT_QNEW( ptr ) \
  206. FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
  207. #define FT_QNEW_ARRAY( ptr, count ) \
  208. FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
  209. #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
  210. FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
  211. #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
  212. FT_BASE( FT_Error )
  213. FT_Alloc( FT_Memory memory,
  214. FT_Long size,
  215. void* *P );
  216. FT_BASE( FT_Error )
  217. FT_QAlloc( FT_Memory memory,
  218. FT_Long size,
  219. void* *p );
  220. FT_BASE( FT_Error )
  221. FT_Realloc( FT_Memory memory,
  222. FT_Long current,
  223. FT_Long size,
  224. void* *P );
  225. FT_BASE( FT_Error )
  226. FT_QRealloc( FT_Memory memory,
  227. FT_Long current,
  228. FT_Long size,
  229. void* *p );
  230. FT_BASE( void )
  231. FT_Free( FT_Memory memory,
  232. void* *P );
  233. #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
  234. FT_BASE( FT_Pointer )
  235. ft_mem_strdup( FT_Memory memory,
  236. const char* str,
  237. FT_Error *p_error );
  238. FT_BASE( FT_Pointer )
  239. ft_mem_dup( FT_Memory memory,
  240. const void* address,
  241. FT_ULong size,
  242. FT_Error *p_error );
  243. #define FT_MEM_STRDUP( dst, str ) \
  244. (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
  245. #define FT_STRDUP( dst, str ) \
  246. FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
  247. #define FT_MEM_DUP( dst, address, size ) \
  248. (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
  249. #define FT_DUP( dst, address, size ) \
  250. FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
  251. /* Return >= 1 if a truncation occurs. */
  252. /* Return 0 if the source string fits the buffer. */
  253. /* This is *not* the same as strlcpy(). */
  254. FT_BASE( FT_Int )
  255. ft_mem_strcpyn( char* dst,
  256. const char* src,
  257. FT_ULong size );
  258. #define FT_STRCPYN( dst, src, size ) \
  259. ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
  260. /* */
  261. FT_END_HEADER
  262. #endif /* __FTMEMORY_H__ */
  263. /* END */