ftutil.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /***************************************************************************/
  2. /* */
  3. /* ftutil.c */
  4. /* */
  5. /* FreeType utility file for memory and list management (body). */
  6. /* */
  7. /* Copyright 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. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_MEMORY_H
  20. #include FT_LIST_H
  21. /*************************************************************************/
  22. /* */
  23. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  24. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  25. /* messages during execution. */
  26. /* */
  27. #undef FT_COMPONENT
  28. #define FT_COMPONENT trace_memory
  29. /*************************************************************************/
  30. /*************************************************************************/
  31. /*************************************************************************/
  32. /***** *****/
  33. /***** *****/
  34. /***** M E M O R Y M A N A G E M E N T *****/
  35. /***** *****/
  36. /***** *****/
  37. /*************************************************************************/
  38. /*************************************************************************/
  39. /*************************************************************************/
  40. /* documentation is in ftmemory.h */
  41. FT_BASE_DEF( FT_Error )
  42. FT_Alloc( FT_Memory memory,
  43. FT_Long size,
  44. void* *P )
  45. {
  46. FT_ASSERT( P != 0 );
  47. if ( size > 0 )
  48. {
  49. *P = memory->alloc( memory, size );
  50. if ( !*P )
  51. {
  52. FT_ERROR(( "FT_Alloc:" ));
  53. FT_ERROR(( " Out of memory? (%ld requested)\n",
  54. size ));
  55. return FT_Err_Out_Of_Memory;
  56. }
  57. FT_MEM_ZERO( *P, size );
  58. }
  59. else
  60. *P = NULL;
  61. FT_TRACE7(( "FT_Alloc:" ));
  62. FT_TRACE7(( " size = %ld, block = 0x%08p, ref = 0x%08p\n",
  63. size, *P, P ));
  64. return FT_Err_Ok;
  65. }
  66. /* documentation is in ftmemory.h */
  67. FT_BASE_DEF( FT_Error )
  68. FT_Realloc( FT_Memory memory,
  69. FT_Long current,
  70. FT_Long size,
  71. void** P )
  72. {
  73. void* Q;
  74. FT_ASSERT( P != 0 );
  75. /* if the original pointer is NULL, call FT_Alloc() */
  76. if ( !*P )
  77. return FT_Alloc( memory, size, P );
  78. /* if the new block if zero-sized, clear the current one */
  79. if ( size <= 0 )
  80. {
  81. FT_Free( memory, P );
  82. return FT_Err_Ok;
  83. }
  84. Q = memory->realloc( memory, current, size, *P );
  85. if ( !Q )
  86. goto Fail;
  87. if ( size > current )
  88. FT_MEM_ZERO( (char*)Q + current, size - current );
  89. *P = Q;
  90. return FT_Err_Ok;
  91. Fail:
  92. FT_ERROR(( "FT_Realloc:" ));
  93. FT_ERROR(( " Failed (current %ld, requested %ld)\n",
  94. current, size ));
  95. return FT_Err_Out_Of_Memory;
  96. }
  97. /* documentation is in ftmemory.h */
  98. FT_BASE_DEF( void )
  99. FT_Free( FT_Memory memory,
  100. void** P )
  101. {
  102. FT_TRACE7(( "FT_Free:" ));
  103. FT_TRACE7(( " Freeing block 0x%08p, ref 0x%08p\n",
  104. P, P ? *P : (void*)0 ));
  105. if ( P && *P )
  106. {
  107. memory->free( memory, *P );
  108. *P = 0;
  109. }
  110. }
  111. /*************************************************************************/
  112. /*************************************************************************/
  113. /*************************************************************************/
  114. /***** *****/
  115. /***** *****/
  116. /***** D O U B L Y L I N K E D L I S T S *****/
  117. /***** *****/
  118. /***** *****/
  119. /*************************************************************************/
  120. /*************************************************************************/
  121. /*************************************************************************/
  122. #undef FT_COMPONENT
  123. #define FT_COMPONENT trace_list
  124. /* documentation is in ftlist.h */
  125. FT_EXPORT_DEF( FT_ListNode )
  126. FT_List_Find( FT_List list,
  127. void* data )
  128. {
  129. FT_ListNode cur;
  130. cur = list->head;
  131. while ( cur )
  132. {
  133. if ( cur->data == data )
  134. return cur;
  135. cur = cur->next;
  136. }
  137. return (FT_ListNode)0;
  138. }
  139. /* documentation is in ftlist.h */
  140. FT_EXPORT_DEF( void )
  141. FT_List_Add( FT_List list,
  142. FT_ListNode node )
  143. {
  144. FT_ListNode before = list->tail;
  145. node->next = 0;
  146. node->prev = before;
  147. if ( before )
  148. before->next = node;
  149. else
  150. list->head = node;
  151. list->tail = node;
  152. }
  153. /* documentation is in ftlist.h */
  154. FT_EXPORT_DEF( void )
  155. FT_List_Insert( FT_List list,
  156. FT_ListNode node )
  157. {
  158. FT_ListNode after = list->head;
  159. node->next = after;
  160. node->prev = 0;
  161. if ( !after )
  162. list->tail = node;
  163. else
  164. after->prev = node;
  165. list->head = node;
  166. }
  167. /* documentation is in ftlist.h */
  168. FT_EXPORT_DEF( void )
  169. FT_List_Remove( FT_List list,
  170. FT_ListNode node )
  171. {
  172. FT_ListNode before, after;
  173. before = node->prev;
  174. after = node->next;
  175. if ( before )
  176. before->next = after;
  177. else
  178. list->head = after;
  179. if ( after )
  180. after->prev = before;
  181. else
  182. list->tail = before;
  183. }
  184. /* documentation is in ftlist.h */
  185. FT_EXPORT_DEF( void )
  186. FT_List_Up( FT_List list,
  187. FT_ListNode node )
  188. {
  189. FT_ListNode before, after;
  190. before = node->prev;
  191. after = node->next;
  192. /* check whether we are already on top of the list */
  193. if ( !before )
  194. return;
  195. before->next = after;
  196. if ( after )
  197. after->prev = before;
  198. else
  199. list->tail = before;
  200. node->prev = 0;
  201. node->next = list->head;
  202. list->head->prev = node;
  203. list->head = node;
  204. }
  205. /* documentation is in ftlist.h */
  206. FT_EXPORT_DEF( FT_Error )
  207. FT_List_Iterate( FT_List list,
  208. FT_List_Iterator iterator,
  209. void* user )
  210. {
  211. FT_ListNode cur = list->head;
  212. FT_Error error = FT_Err_Ok;
  213. while ( cur )
  214. {
  215. FT_ListNode next = cur->next;
  216. error = iterator( cur, user );
  217. if ( error )
  218. break;
  219. cur = next;
  220. }
  221. return error;
  222. }
  223. /* documentation is in ftlist.h */
  224. FT_EXPORT_DEF( void )
  225. FT_List_Finalize( FT_List list,
  226. FT_List_Destructor destroy,
  227. FT_Memory memory,
  228. void* user )
  229. {
  230. FT_ListNode cur;
  231. cur = list->head;
  232. while ( cur )
  233. {
  234. FT_ListNode next = cur->next;
  235. void* data = cur->data;
  236. if ( destroy )
  237. destroy( memory, data, user );
  238. FT_FREE( cur );
  239. cur = next;
  240. }
  241. list->head = 0;
  242. list->tail = 0;
  243. }
  244. /* END */