1 /***************************************************************************/
5 /* The FreeType memory management macros (specification). */
7 /* Copyright 1996-2016 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 /***************************************************************************/
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.
70 template <typename T> inline T*
74 return static_cast <T*> ( v );
78 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
82 #define FT_ASSIGNP( p, val ) (p) = (val)
88 #ifdef FT_DEBUG_MEMORY
90 FT_BASE( const char* ) _ft_debug_file;
91 FT_BASE( long ) _ft_debug_lineno;
93 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
94 _ft_debug_lineno = __LINE__, \
97 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
98 _ft_debug_lineno = __LINE__, \
99 FT_ASSIGNP( p, exp ) )
101 #else /* !FT_DEBUG_MEMORY */
103 #define FT_DEBUG_INNER( exp ) (exp)
104 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
106 #endif /* !FT_DEBUG_MEMORY */
110 * The allocation functions return a pointer, and the error code
111 * is written to through the `p_error' parameter. See below for
115 FT_BASE( FT_Pointer )
116 ft_mem_alloc( FT_Memory memory,
120 FT_BASE( FT_Pointer )
121 ft_mem_qalloc( FT_Memory memory,
125 FT_BASE( FT_Pointer )
126 ft_mem_realloc( FT_Memory memory,
133 FT_BASE( FT_Pointer )
134 ft_mem_qrealloc( FT_Memory memory,
142 ft_mem_free( FT_Memory memory,
146 #define FT_MEM_ALLOC( ptr, size ) \
147 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \
151 #define FT_MEM_FREE( ptr ) \
153 ft_mem_free( memory, (ptr) ); \
157 #define FT_MEM_NEW( ptr ) \
158 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
160 #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
161 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
168 #define FT_MEM_QALLOC( ptr, size ) \
169 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \
173 #define FT_MEM_QNEW( ptr ) \
174 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
176 #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
177 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
184 #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
185 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
186 (FT_Long)(item_size), \
192 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
193 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
200 #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
201 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
202 (FT_Long)(item_size), \
208 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
209 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
217 #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
220 #define FT_MEM_SET( dest, byte, count ) \
221 ft_memset( dest, byte, (FT_Offset)(count) )
223 #define FT_MEM_COPY( dest, source, count ) \
224 ft_memcpy( dest, source, (FT_Offset)(count) )
226 #define FT_MEM_MOVE( dest, source, count ) \
227 ft_memmove( dest, source, (FT_Offset)(count) )
230 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
232 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
235 #define FT_ARRAY_ZERO( dest, count ) \
237 (FT_Offset)(count) * sizeof ( *(dest) ) )
239 #define FT_ARRAY_COPY( dest, source, count ) \
242 (FT_Offset)(count) * sizeof ( *(dest) ) )
244 #define FT_ARRAY_MOVE( dest, source, count ) \
247 (FT_Offset)(count) * sizeof ( *(dest) ) )
251 * Return the maximum number of addressable elements in an array.
252 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
255 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
257 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
260 /*************************************************************************/
262 /* The following functions macros expect that their pointer argument is */
263 /* _typed_ in order to automatically compute array element sizes. */
266 #define FT_MEM_NEW_ARRAY( ptr, count ) \
267 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
274 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
275 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
282 #define FT_MEM_QNEW_ARRAY( ptr, count ) \
283 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
290 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
291 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
298 #define FT_ALLOC( ptr, size ) \
299 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
301 #define FT_REALLOC( ptr, cursz, newsz ) \
302 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
304 #define FT_ALLOC_MULT( ptr, count, item_size ) \
305 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
307 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
308 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
311 #define FT_QALLOC( ptr, size ) \
312 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
314 #define FT_QREALLOC( ptr, cursz, newsz ) \
315 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
317 #define FT_QALLOC_MULT( ptr, count, item_size ) \
318 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
320 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
321 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
324 #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
326 #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
328 #define FT_NEW_ARRAY( ptr, count ) \
329 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
331 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
332 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
334 #define FT_QNEW( ptr ) \
335 FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
337 #define FT_QNEW_ARRAY( ptr, count ) \
338 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
340 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
341 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
344 FT_BASE( FT_Pointer )
345 ft_mem_strdup( FT_Memory memory,
349 FT_BASE( FT_Pointer )
350 ft_mem_dup( FT_Memory memory,
356 #define FT_MEM_STRDUP( dst, str ) \
357 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
359 #define FT_STRDUP( dst, str ) \
360 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
362 #define FT_MEM_DUP( dst, address, size ) \
363 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
365 #define FT_DUP( dst, address, size ) \
366 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
369 /* Return >= 1 if a truncation occurs. */
370 /* Return 0 if the source string fits the buffer. */
371 /* This is *not* the same as strlcpy(). */
373 ft_mem_strcpyn( char* dst,
377 #define FT_STRCPYN( dst, src, size ) \
378 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
385 #endif /* FTMEMORY_H_ */