1 /***************************************************************************/
5 /* FreeType utility file for memory and list management (body). */
7 /* Copyright 2002, 2004, 2005, 2006, 2007 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
22 #include FT_INTERNAL_OBJECTS_H
26 /*************************************************************************/
28 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
29 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
30 /* messages during execution. */
33 #define FT_COMPONENT trace_memory
36 /*************************************************************************/
37 /*************************************************************************/
38 /*************************************************************************/
41 /***** M E M O R Y M A N A G E M E N T *****/
44 /*************************************************************************/
45 /*************************************************************************/
46 /*************************************************************************/
49 FT_BASE_DEF( FT_Pointer
)
50 ft_mem_alloc( FT_Memory memory
,
55 FT_Pointer block
= ft_mem_qalloc( memory
, size
, &error
);
57 if ( !error
&& size
> 0 )
58 FT_MEM_ZERO( block
, size
);
65 FT_BASE_DEF( FT_Pointer
)
66 ft_mem_qalloc( FT_Memory memory
,
70 FT_Error error
= FT_Err_Ok
;
71 FT_Pointer block
= NULL
;
76 block
= memory
->alloc( memory
, size
);
78 error
= FT_Err_Out_Of_Memory
;
82 /* may help catch/prevent security issues */
83 error
= FT_Err_Invalid_Argument
;
91 FT_BASE_DEF( FT_Pointer
)
92 ft_mem_realloc( FT_Memory memory
,
99 FT_Error error
= FT_Err_Ok
;
101 block
= ft_mem_qrealloc( memory
, item_size
,
102 cur_count
, new_count
, block
, &error
);
103 if ( !error
&& new_count
> cur_count
)
104 FT_MEM_ZERO( (char*)block
+ cur_count
* item_size
,
105 ( new_count
- cur_count
) * item_size
);
112 FT_BASE_DEF( FT_Pointer
)
113 ft_mem_qrealloc( FT_Memory memory
,
120 FT_Error error
= FT_Err_Ok
;
123 /* Note that we now accept `item_size == 0' as a valid parameter, in
124 * order to cover very weird cases where an ALLOC_MULT macro would be
127 if ( cur_count
< 0 || new_count
< 0 || item_size
< 0 )
129 /* may help catch/prevent nasty security issues */
130 error
= FT_Err_Invalid_Argument
;
132 else if ( new_count
== 0 || item_size
== 0 )
134 ft_mem_free( memory
, block
);
137 else if ( new_count
> FT_INT_MAX
/item_size
)
139 error
= FT_Err_Array_Too_Large
;
141 else if ( cur_count
== 0 )
143 FT_ASSERT( block
== NULL
);
145 block
= ft_mem_alloc( memory
, new_count
*item_size
, &error
);
150 FT_Long cur_size
= cur_count
*item_size
;
151 FT_Long new_size
= new_count
*item_size
;
154 block2
= memory
->realloc( memory
, cur_size
, new_size
, block
);
155 if ( block2
== NULL
)
156 error
= FT_Err_Out_Of_Memory
;
167 ft_mem_free( FT_Memory memory
,
171 memory
->free( memory
, (void*)P
);
175 FT_BASE_DEF( FT_Pointer
)
176 ft_mem_dup( FT_Memory memory
,
182 FT_Pointer p
= ft_mem_qalloc( memory
, size
, &error
);
185 if ( !error
&& address
)
186 ft_memcpy( p
, address
, size
);
193 FT_BASE_DEF( FT_Pointer
)
194 ft_mem_strdup( FT_Memory memory
,
198 FT_ULong len
= str
? (FT_ULong
)ft_strlen( str
) + 1
202 return ft_mem_dup( memory
, str
, len
, p_error
);
206 FT_BASE_DEF( FT_Int
)
207 ft_mem_strcpyn( char* dst
,
211 while ( size
> 1 && *src
!= 0 )
217 *dst
= 0; /* always zero-terminate */
223 /*************************************************************************/
224 /*************************************************************************/
225 /*************************************************************************/
228 /***** D O U B L Y L I N K E D L I S T S *****/
231 /*************************************************************************/
232 /*************************************************************************/
233 /*************************************************************************/
236 #define FT_COMPONENT trace_list
238 /* documentation is in ftlist.h */
240 FT_EXPORT_DEF( FT_ListNode
)
241 FT_List_Find( FT_List list
,
250 if ( cur
->data
== data
)
256 return (FT_ListNode
)0;
260 /* documentation is in ftlist.h */
262 FT_EXPORT_DEF( void )
263 FT_List_Add( FT_List list
,
266 FT_ListNode before
= list
->tail
;
281 /* documentation is in ftlist.h */
283 FT_EXPORT_DEF( void )
284 FT_List_Insert( FT_List list
,
287 FT_ListNode after
= list
->head
;
302 /* documentation is in ftlist.h */
304 FT_EXPORT_DEF( void )
305 FT_List_Remove( FT_List list
,
308 FT_ListNode before
, after
;
315 before
->next
= after
;
320 after
->prev
= before
;
326 /* documentation is in ftlist.h */
328 FT_EXPORT_DEF( void )
329 FT_List_Up( FT_List list
,
332 FT_ListNode before
, after
;
338 /* check whether we are already on top of the list */
342 before
->next
= after
;
345 after
->prev
= before
;
350 node
->next
= list
->head
;
351 list
->head
->prev
= node
;
356 /* documentation is in ftlist.h */
358 FT_EXPORT_DEF( FT_Error
)
359 FT_List_Iterate( FT_List list
,
360 FT_List_Iterator iterator
,
363 FT_ListNode cur
= list
->head
;
364 FT_Error error
= FT_Err_Ok
;
369 FT_ListNode next
= cur
->next
;
372 error
= iterator( cur
, user
);
383 /* documentation is in ftlist.h */
385 FT_EXPORT_DEF( void )
386 FT_List_Finalize( FT_List list
,
387 FT_List_Destructor destroy
,
397 FT_ListNode next
= cur
->next
;
398 void* data
= cur
->data
;
402 destroy( memory
, data
, user
);
413 FT_BASE_DEF( FT_UInt32
)
414 ft_highpow2( FT_UInt32 value
)
420 * We simply clear the lowest bit in each iteration. When
421 * we reach 0, we know that the previous value was our result.
425 value2
= value
& (value
- 1); /* clear lowest bit */
435 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
437 FT_BASE_DEF( FT_Error
)
438 FT_Alloc( FT_Memory memory
,
445 (void)FT_ALLOC( *P
, size
);
450 FT_BASE_DEF( FT_Error
)
451 FT_QAlloc( FT_Memory memory
,
458 (void)FT_QALLOC( *p
, size
);
463 FT_BASE_DEF( FT_Error
)
464 FT_Realloc( FT_Memory memory
,
472 (void)FT_REALLOC( *P
, current
, size
);
477 FT_BASE_DEF( FT_Error
)
478 FT_QRealloc( FT_Memory memory
,
486 (void)FT_QREALLOC( *p
, current
, size
);
492 FT_Free( FT_Memory memory
,
499 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */