1 /***************************************************************************/
5 /* The FreeType memory management macros (specification). */
7 /* Copyright 1996-2001, 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 /***************************************************************************/
19 #ifndef __FTMEMORY_H__
20 #define __FTMEMORY_H__
24 #include FT_CONFIG_CONFIG_H
31 /*************************************************************************/
37 /* This macro is used to set an implicit `error' variable to a given */
38 /* expression's value (usually a function call), and convert it to a */
39 /* boolean which is set whenever the value is != 0. */
42 #define FT_SET_ERROR( expression ) \
43 ( ( error = (expression) ) != 0 )
47 /*************************************************************************/
48 /*************************************************************************/
49 /*************************************************************************/
52 /**** M E M O R Y ****/
55 /*************************************************************************/
56 /*************************************************************************/
57 /*************************************************************************/
61 * C++ refuses to handle statements like p = (void*)anything; where `p'
62 * is a typed pointer. Since we don't have a `typeof' operator in
63 * standard C++, we have to use ugly casts.
67 #define FT_ASSIGNP( p, val ) *((void**)&(p)) = (val)
69 #define FT_ASSIGNP( p, val ) (p) = (val)
74 #ifdef FT_DEBUG_MEMORY
76 FT_BASE( const char* ) _ft_debug_file
;
77 FT_BASE( long ) _ft_debug_lineno
;
79 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
80 _ft_debug_lineno = __LINE__, \
83 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
84 _ft_debug_lineno = __LINE__, \
85 FT_ASSIGNP( p, exp ) )
87 #else /* !FT_DEBUG_MEMORY */
89 #define FT_DEBUG_INNER( exp ) (exp)
90 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
92 #endif /* !FT_DEBUG_MEMORY */
96 * The allocation functions return a pointer, and the error code
97 * is written to through the `p_error' parameter. See below for
101 FT_BASE( FT_Pointer
)
102 ft_mem_alloc( FT_Memory memory
,
106 FT_BASE( FT_Pointer
)
107 ft_mem_qalloc( FT_Memory memory
,
111 FT_BASE( FT_Pointer
)
112 ft_mem_realloc( FT_Memory memory
,
119 FT_BASE( FT_Pointer
)
120 ft_mem_qrealloc( FT_Memory memory
,
128 ft_mem_free( FT_Memory memory
,
132 #define FT_MEM_ALLOC( ptr, size ) \
133 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, (size), &error ) )
135 #define FT_MEM_FREE( ptr ) \
137 ft_mem_free( memory, (ptr) ); \
141 #define FT_MEM_NEW( ptr ) \
142 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
144 #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
145 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, 1, \
149 #define FT_MEM_QALLOC( ptr, size ) \
150 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, (size), &error ) )
152 #define FT_MEM_QNEW( ptr ) \
153 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
155 #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
156 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, 1, \
160 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
161 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
165 #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
166 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (item_size), \
170 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
171 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (itmsz), \
172 (oldcnt), (newcnt), \
175 #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
176 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (item_size), \
180 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
181 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (itmsz), \
182 (oldcnt), (newcnt), \
186 #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
189 #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
191 #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
193 #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
196 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
198 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
201 #define FT_ARRAY_ZERO( dest, count ) \
202 FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
204 #define FT_ARRAY_COPY( dest, source, count ) \
205 FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
207 #define FT_ARRAY_MOVE( dest, source, count ) \
208 FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
212 * Return the maximum number of addressable elements in an array.
213 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
216 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
218 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
221 /*************************************************************************/
223 /* The following functions macros expect that their pointer argument is */
224 /* _typed_ in order to automatically compute array element sizes. */
227 #define FT_MEM_NEW_ARRAY( ptr, count ) \
228 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
232 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
233 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
237 #define FT_MEM_QNEW_ARRAY( ptr, count ) \
238 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
242 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
243 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
248 #define FT_ALLOC( ptr, size ) \
249 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
251 #define FT_REALLOC( ptr, cursz, newsz ) \
252 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
254 #define FT_ALLOC_MULT( ptr, count, item_size ) \
255 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
257 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
258 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
261 #define FT_QALLOC( ptr, size ) \
262 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
264 #define FT_QREALLOC( ptr, cursz, newsz ) \
265 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
267 #define FT_QALLOC_MULT( ptr, count, item_size ) \
268 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
270 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
271 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
274 #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
276 #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
278 #define FT_NEW_ARRAY( ptr, count ) \
279 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
281 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
282 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
284 #define FT_QNEW( ptr ) \
285 FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
287 #define FT_QNEW_ARRAY( ptr, count ) \
288 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
290 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
291 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
294 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
297 FT_Alloc( FT_Memory memory
,
302 FT_QAlloc( FT_Memory memory
,
307 FT_Realloc( FT_Memory memory
,
313 FT_QRealloc( FT_Memory memory
,
319 FT_Free( FT_Memory memory
,
322 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
325 FT_BASE( FT_Pointer
)
326 ft_mem_strdup( FT_Memory memory
,
330 FT_BASE( FT_Pointer
)
331 ft_mem_dup( FT_Memory memory
,
336 #define FT_MEM_STRDUP( dst, str ) \
337 (dst) = ft_mem_strdup( memory, (const char*)(str), &error )
339 #define FT_STRDUP( dst, str ) \
340 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
342 #define FT_MEM_DUP( dst, address, size ) \
343 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
345 #define FT_DUP( dst, address, size ) \
346 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
349 /* Return >= 1 if a truncation occurs. */
350 /* Return 0 if the source string fits the buffer. */
351 /* This is *not* the same as strlcpy(). */
353 ft_mem_strcpyn( char* dst
,
357 #define FT_STRCPYN( dst, src, size ) \
358 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
365 #endif /* __FTMEMORY_H__ */