/src/compiler/android-ndk/jni/freetype/src/base/ftutil.c

http://ftk.googlecode.com/ · C · 501 lines · 313 code · 125 blank · 63 comment · 49 complexity · 708a2cd5d3ebb1bc3443de79cd7b2e3e MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftutil.c */
  4. /* */
  5. /* FreeType utility file for memory and list management (body). */
  6. /* */
  7. /* Copyright 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. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_MEMORY_H
  20. #include FT_INTERNAL_OBJECTS_H
  21. #include FT_LIST_H
  22. /*************************************************************************/
  23. /* */
  24. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  25. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  26. /* messages during execution. */
  27. /* */
  28. #undef FT_COMPONENT
  29. #define FT_COMPONENT trace_memory
  30. /*************************************************************************/
  31. /*************************************************************************/
  32. /*************************************************************************/
  33. /***** *****/
  34. /***** *****/
  35. /***** M E M O R Y M A N A G E M E N T *****/
  36. /***** *****/
  37. /***** *****/
  38. /*************************************************************************/
  39. /*************************************************************************/
  40. /*************************************************************************/
  41. FT_BASE_DEF( FT_Pointer )
  42. ft_mem_alloc( FT_Memory memory,
  43. FT_Long size,
  44. FT_Error *p_error )
  45. {
  46. FT_Error error;
  47. FT_Pointer block = ft_mem_qalloc( memory, size, &error );
  48. if ( !error && size > 0 )
  49. FT_MEM_ZERO( block, size );
  50. *p_error = error;
  51. return block;
  52. }
  53. FT_BASE_DEF( FT_Pointer )
  54. ft_mem_qalloc( FT_Memory memory,
  55. FT_Long size,
  56. FT_Error *p_error )
  57. {
  58. FT_Error error = FT_Err_Ok;
  59. FT_Pointer block = NULL;
  60. if ( size > 0 )
  61. {
  62. block = memory->alloc( memory, size );
  63. if ( block == NULL )
  64. error = FT_Err_Out_Of_Memory;
  65. }
  66. else if ( size < 0 )
  67. {
  68. /* may help catch/prevent security issues */
  69. error = FT_Err_Invalid_Argument;
  70. }
  71. *p_error = error;
  72. return block;
  73. }
  74. FT_BASE_DEF( FT_Pointer )
  75. ft_mem_realloc( FT_Memory memory,
  76. FT_Long item_size,
  77. FT_Long cur_count,
  78. FT_Long new_count,
  79. void* block,
  80. FT_Error *p_error )
  81. {
  82. FT_Error error = FT_Err_Ok;
  83. block = ft_mem_qrealloc( memory, item_size,
  84. cur_count, new_count, block, &error );
  85. if ( !error && new_count > cur_count )
  86. FT_MEM_ZERO( (char*)block + cur_count * item_size,
  87. ( new_count - cur_count ) * item_size );
  88. *p_error = error;
  89. return block;
  90. }
  91. FT_BASE_DEF( FT_Pointer )
  92. ft_mem_qrealloc( FT_Memory memory,
  93. FT_Long item_size,
  94. FT_Long cur_count,
  95. FT_Long new_count,
  96. void* block,
  97. FT_Error *p_error )
  98. {
  99. FT_Error error = FT_Err_Ok;
  100. /* Note that we now accept `item_size == 0' as a valid parameter, in
  101. * order to cover very weird cases where an ALLOC_MULT macro would be
  102. * called.
  103. */
  104. if ( cur_count < 0 || new_count < 0 || item_size < 0 )
  105. {
  106. /* may help catch/prevent nasty security issues */
  107. error = FT_Err_Invalid_Argument;
  108. }
  109. else if ( new_count == 0 || item_size == 0 )
  110. {
  111. ft_mem_free( memory, block );
  112. block = NULL;
  113. }
  114. else if ( new_count > FT_INT_MAX/item_size )
  115. {
  116. error = FT_Err_Array_Too_Large;
  117. }
  118. else if ( cur_count == 0 )
  119. {
  120. FT_ASSERT( block == NULL );
  121. block = ft_mem_alloc( memory, new_count*item_size, &error );
  122. }
  123. else
  124. {
  125. FT_Pointer block2;
  126. FT_Long cur_size = cur_count*item_size;
  127. FT_Long new_size = new_count*item_size;
  128. block2 = memory->realloc( memory, cur_size, new_size, block );
  129. if ( block2 == NULL )
  130. error = FT_Err_Out_Of_Memory;
  131. else
  132. block = block2;
  133. }
  134. *p_error = error;
  135. return block;
  136. }
  137. FT_BASE_DEF( void )
  138. ft_mem_free( FT_Memory memory,
  139. const void *P )
  140. {
  141. if ( P )
  142. memory->free( memory, (void*)P );
  143. }
  144. FT_BASE_DEF( FT_Pointer )
  145. ft_mem_dup( FT_Memory memory,
  146. const void* address,
  147. FT_ULong size,
  148. FT_Error *p_error )
  149. {
  150. FT_Error error;
  151. FT_Pointer p = ft_mem_qalloc( memory, size, &error );
  152. if ( !error && address )
  153. ft_memcpy( p, address, size );
  154. *p_error = error;
  155. return p;
  156. }
  157. FT_BASE_DEF( FT_Pointer )
  158. ft_mem_strdup( FT_Memory memory,
  159. const char* str,
  160. FT_Error *p_error )
  161. {
  162. FT_ULong len = str ? (FT_ULong)ft_strlen( str ) + 1
  163. : 0;
  164. return ft_mem_dup( memory, str, len, p_error );
  165. }
  166. FT_BASE_DEF( FT_Int )
  167. ft_mem_strcpyn( char* dst,
  168. const char* src,
  169. FT_ULong size )
  170. {
  171. while ( size > 1 && *src != 0 )
  172. {
  173. *dst++ = *src++;
  174. size--;
  175. }
  176. *dst = 0; /* always zero-terminate */
  177. return *src != 0;
  178. }
  179. /*************************************************************************/
  180. /*************************************************************************/
  181. /*************************************************************************/
  182. /***** *****/
  183. /***** *****/
  184. /***** D O U B L Y L I N K E D L I S T S *****/
  185. /***** *****/
  186. /***** *****/
  187. /*************************************************************************/
  188. /*************************************************************************/
  189. /*************************************************************************/
  190. #undef FT_COMPONENT
  191. #define FT_COMPONENT trace_list
  192. /* documentation is in ftlist.h */
  193. FT_EXPORT_DEF( FT_ListNode )
  194. FT_List_Find( FT_List list,
  195. void* data )
  196. {
  197. FT_ListNode cur;
  198. cur = list->head;
  199. while ( cur )
  200. {
  201. if ( cur->data == data )
  202. return cur;
  203. cur = cur->next;
  204. }
  205. return (FT_ListNode)0;
  206. }
  207. /* documentation is in ftlist.h */
  208. FT_EXPORT_DEF( void )
  209. FT_List_Add( FT_List list,
  210. FT_ListNode node )
  211. {
  212. FT_ListNode before = list->tail;
  213. node->next = 0;
  214. node->prev = before;
  215. if ( before )
  216. before->next = node;
  217. else
  218. list->head = node;
  219. list->tail = node;
  220. }
  221. /* documentation is in ftlist.h */
  222. FT_EXPORT_DEF( void )
  223. FT_List_Insert( FT_List list,
  224. FT_ListNode node )
  225. {
  226. FT_ListNode after = list->head;
  227. node->next = after;
  228. node->prev = 0;
  229. if ( !after )
  230. list->tail = node;
  231. else
  232. after->prev = node;
  233. list->head = node;
  234. }
  235. /* documentation is in ftlist.h */
  236. FT_EXPORT_DEF( void )
  237. FT_List_Remove( FT_List list,
  238. FT_ListNode node )
  239. {
  240. FT_ListNode before, after;
  241. before = node->prev;
  242. after = node->next;
  243. if ( before )
  244. before->next = after;
  245. else
  246. list->head = after;
  247. if ( after )
  248. after->prev = before;
  249. else
  250. list->tail = before;
  251. }
  252. /* documentation is in ftlist.h */
  253. FT_EXPORT_DEF( void )
  254. FT_List_Up( FT_List list,
  255. FT_ListNode node )
  256. {
  257. FT_ListNode before, after;
  258. before = node->prev;
  259. after = node->next;
  260. /* check whether we are already on top of the list */
  261. if ( !before )
  262. return;
  263. before->next = after;
  264. if ( after )
  265. after->prev = before;
  266. else
  267. list->tail = before;
  268. node->prev = 0;
  269. node->next = list->head;
  270. list->head->prev = node;
  271. list->head = node;
  272. }
  273. /* documentation is in ftlist.h */
  274. FT_EXPORT_DEF( FT_Error )
  275. FT_List_Iterate( FT_List list,
  276. FT_List_Iterator iterator,
  277. void* user )
  278. {
  279. FT_ListNode cur = list->head;
  280. FT_Error error = FT_Err_Ok;
  281. while ( cur )
  282. {
  283. FT_ListNode next = cur->next;
  284. error = iterator( cur, user );
  285. if ( error )
  286. break;
  287. cur = next;
  288. }
  289. return error;
  290. }
  291. /* documentation is in ftlist.h */
  292. FT_EXPORT_DEF( void )
  293. FT_List_Finalize( FT_List list,
  294. FT_List_Destructor destroy,
  295. FT_Memory memory,
  296. void* user )
  297. {
  298. FT_ListNode cur;
  299. cur = list->head;
  300. while ( cur )
  301. {
  302. FT_ListNode next = cur->next;
  303. void* data = cur->data;
  304. if ( destroy )
  305. destroy( memory, data, user );
  306. FT_FREE( cur );
  307. cur = next;
  308. }
  309. list->head = 0;
  310. list->tail = 0;
  311. }
  312. FT_BASE_DEF( FT_UInt32 )
  313. ft_highpow2( FT_UInt32 value )
  314. {
  315. FT_UInt32 value2;
  316. /*
  317. * We simply clear the lowest bit in each iteration. When
  318. * we reach 0, we know that the previous value was our result.
  319. */
  320. for ( ;; )
  321. {
  322. value2 = value & (value - 1); /* clear lowest bit */
  323. if ( value2 == 0 )
  324. break;
  325. value = value2;
  326. }
  327. return value;
  328. }
  329. #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
  330. FT_BASE_DEF( FT_Error )
  331. FT_Alloc( FT_Memory memory,
  332. FT_Long size,
  333. void* *P )
  334. {
  335. FT_Error error;
  336. (void)FT_ALLOC( *P, size );
  337. return error;
  338. }
  339. FT_BASE_DEF( FT_Error )
  340. FT_QAlloc( FT_Memory memory,
  341. FT_Long size,
  342. void* *p )
  343. {
  344. FT_Error error;
  345. (void)FT_QALLOC( *p, size );
  346. return error;
  347. }
  348. FT_BASE_DEF( FT_Error )
  349. FT_Realloc( FT_Memory memory,
  350. FT_Long current,
  351. FT_Long size,
  352. void* *P )
  353. {
  354. FT_Error error;
  355. (void)FT_REALLOC( *P, current, size );
  356. return error;
  357. }
  358. FT_BASE_DEF( FT_Error )
  359. FT_QRealloc( FT_Memory memory,
  360. FT_Long current,
  361. FT_Long size,
  362. void* *p )
  363. {
  364. FT_Error error;
  365. (void)FT_QREALLOC( *p, current, size );
  366. return error;
  367. }
  368. FT_BASE_DEF( void )
  369. FT_Free( FT_Memory memory,
  370. void* *P )
  371. {
  372. if ( *P )
  373. FT_MEM_FREE( *P );
  374. }
  375. #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
  376. /* END */