1 /***************************************************************************/
5 /* The FreeType memory management macros (specification). */
7 /* Copyright 1996-2002, 2004-2007, 2010, 2013 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, with `p' a
62 * typed pointer. Since we don't have a `typeof' operator in standard
63 * C++, we have to use a template to emulate it.
69 template <typename T
> inline T
*
73 return static_cast <T
*> ( v
);
76 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
80 #define FT_ASSIGNP( p, val ) (p) = (val)
86 #ifdef FT_DEBUG_MEMORY
88 FT_BASE( const char* ) _ft_debug_file
;
89 FT_BASE( long ) _ft_debug_lineno
;
91 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
92 _ft_debug_lineno = __LINE__, \
95 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
96 _ft_debug_lineno = __LINE__, \
97 FT_ASSIGNP( p, exp ) )
99 #else /* !FT_DEBUG_MEMORY */
101 #define FT_DEBUG_INNER( exp ) (exp)
102 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
104 #endif /* !FT_DEBUG_MEMORY */
108 * The allocation functions return a pointer, and the error code
109 * is written to through the `p_error' parameter. See below for
113 FT_BASE( FT_Pointer
)
114 ft_mem_alloc( FT_Memory memory
,
118 FT_BASE( FT_Pointer
)
119 ft_mem_qalloc( FT_Memory memory
,
123 FT_BASE( FT_Pointer
)
124 ft_mem_realloc( FT_Memory memory
,
131 FT_BASE( FT_Pointer
)
132 ft_mem_qrealloc( FT_Memory memory
,
140 ft_mem_free( FT_Memory memory
,
144 #define FT_MEM_ALLOC( ptr, size ) \
145 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \
149 #define FT_MEM_FREE( ptr ) \
151 ft_mem_free( memory, (ptr) ); \
155 #define FT_MEM_NEW( ptr ) \
156 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
158 #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
159 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
166 #define FT_MEM_QALLOC( ptr, size ) \
167 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \
171 #define FT_MEM_QNEW( ptr ) \
172 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
174 #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
175 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
182 #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
183 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
184 (FT_Long)(item_size), \
190 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
191 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
198 #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
199 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
200 (FT_Long)(item_size), \
206 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
207 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
215 #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
218 #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
220 #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
222 #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
225 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
227 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
230 #define FT_ARRAY_ZERO( dest, count ) \
231 FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
233 #define FT_ARRAY_COPY( dest, source, count ) \
234 FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
236 #define FT_ARRAY_MOVE( dest, source, count ) \
237 FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
241 * Return the maximum number of addressable elements in an array.
242 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
245 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
247 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
250 /*************************************************************************/
252 /* The following functions macros expect that their pointer argument is */
253 /* _typed_ in order to automatically compute array element sizes. */
256 #define FT_MEM_NEW_ARRAY( ptr, count ) \
257 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
264 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
265 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
272 #define FT_MEM_QNEW_ARRAY( ptr, count ) \
273 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
280 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
281 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
288 #define FT_ALLOC( ptr, size ) \
289 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
291 #define FT_REALLOC( ptr, cursz, newsz ) \
292 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
294 #define FT_ALLOC_MULT( ptr, count, item_size ) \
295 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
297 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
298 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
301 #define FT_QALLOC( ptr, size ) \
302 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
304 #define FT_QREALLOC( ptr, cursz, newsz ) \
305 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
307 #define FT_QALLOC_MULT( ptr, count, item_size ) \
308 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
310 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
311 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
314 #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
316 #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
318 #define FT_NEW_ARRAY( ptr, count ) \
319 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
321 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
322 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
324 #define FT_QNEW( ptr ) \
325 FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
327 #define FT_QNEW_ARRAY( ptr, count ) \
328 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
330 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
331 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
334 FT_BASE( FT_Pointer
)
335 ft_mem_strdup( FT_Memory memory
,
339 FT_BASE( FT_Pointer
)
340 ft_mem_dup( FT_Memory memory
,
346 #define FT_MEM_STRDUP( dst, str ) \
347 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
349 #define FT_STRDUP( dst, str ) \
350 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
352 #define FT_MEM_DUP( dst, address, size ) \
353 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
355 #define FT_DUP( dst, address, size ) \
356 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
359 /* Return >= 1 if a truncation occurs. */
360 /* Return 0 if the source string fits the buffer. */
361 /* This is *not* the same as strlcpy(). */
363 ft_mem_strcpyn( char* dst
,
367 #define FT_STRCPYN( dst, src, size ) \
368 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
375 #endif /* __FTMEMORY_H__ */