1 /***************************************************************************/
5 /* FreeType utility file for memory and list management (body). */
7 /* Copyright 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_MEMORY_H
25 /*************************************************************************/
27 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
28 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
29 /* messages during execution. */
32 #define FT_COMPONENT trace_memory
35 /*************************************************************************/
36 /*************************************************************************/
37 /*************************************************************************/
40 /***** M E M O R Y M A N A G E M E N T *****/
43 /*************************************************************************/
44 /*************************************************************************/
45 /*************************************************************************/
47 /* documentation is in ftmemory.h */
49 FT_BASE_DEF( FT_Error )
50 FT_Alloc( FT_Memory memory,
58 *P = memory->alloc( memory, size );
61 FT_ERROR(( "FT_Alloc:" ));
62 FT_ERROR(( " Out of memory? (%ld requested)\n",
65 return FT_Err_Out_Of_Memory;
67 FT_MEM_ZERO( *P, size );
72 FT_TRACE7(( "FT_Alloc:" ));
73 FT_TRACE7(( " size = %ld, block = 0x%08p, ref = 0x%08p\n",
80 /* documentation is in ftmemory.h */
82 FT_BASE_DEF( FT_Error )
83 FT_Realloc( FT_Memory memory,
93 /* if the original pointer is NULL, call FT_Alloc() */
95 return FT_Alloc( memory, size, P );
97 /* if the new block if zero-sized, clear the current one */
100 FT_Free( memory, P );
104 Q = memory->realloc( memory, current, size, *P );
108 if ( size > current )
109 FT_MEM_ZERO( (char*)Q + current, size - current );
115 FT_ERROR(( "FT_Realloc:" ));
116 FT_ERROR(( " Failed (current %ld, requested %ld)\n",
118 return FT_Err_Out_Of_Memory;
122 /* documentation is in ftmemory.h */
125 FT_Free( FT_Memory memory,
128 FT_TRACE7(( "FT_Free:" ));
129 FT_TRACE7(( " Freeing block 0x%08p, ref 0x%08p\n",
130 P, P ? *P : (void*)0 ));
134 memory->free( memory, *P );
140 /*************************************************************************/
141 /*************************************************************************/
142 /*************************************************************************/
145 /***** D O U B L Y L I N K E D L I S T S *****/
148 /*************************************************************************/
149 /*************************************************************************/
150 /*************************************************************************/
153 #define FT_COMPONENT trace_list
155 /* documentation is in ftlist.h */
157 FT_EXPORT_DEF( FT_ListNode )
158 FT_List_Find( FT_List list,
167 if ( cur->data == data )
173 return (FT_ListNode)0;
177 /* documentation is in ftlist.h */
179 FT_EXPORT_DEF( void )
180 FT_List_Add( FT_List list,
183 FT_ListNode before = list->tail;
198 /* documentation is in ftlist.h */
200 FT_EXPORT_DEF( void )
201 FT_List_Insert( FT_List list,
204 FT_ListNode after = list->head;
219 /* documentation is in ftlist.h */
221 FT_EXPORT_DEF( void )
222 FT_List_Remove( FT_List list,
225 FT_ListNode before, after;
232 before->next = after;
237 after->prev = before;
243 /* documentation is in ftlist.h */
245 FT_EXPORT_DEF( void )
246 FT_List_Up( FT_List list,
249 FT_ListNode before, after;
255 /* check whether we are already on top of the list */
259 before->next = after;
262 after->prev = before;
267 node->next = list->head;
268 list->head->prev = node;
273 /* documentation is in ftlist.h */
275 FT_EXPORT_DEF( FT_Error )
276 FT_List_Iterate( FT_List list,
277 FT_List_Iterator iterator,
280 FT_ListNode cur = list->head;
281 FT_Error error = FT_Err_Ok;
286 FT_ListNode next = cur->next;
289 error = iterator( cur, user );
300 /* documentation is in ftlist.h */
302 FT_EXPORT_DEF( void )
303 FT_List_Finalize( FT_List list,
304 FT_List_Destructor destroy,
314 FT_ListNode next = cur->next;
315 void* data = cur->data;
319 destroy( memory, data, user );