1 /****************************************************************************
5 * The FreeType memory management macros (specification).
7 * Copyright (C) 1996-2023 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.
24 #include FT_CONFIG_CONFIG_H
25 #include <freetype/fttypes.h>
27 #include "compiler-macros.h"
32 /**************************************************************************
38 * This macro is used to set an implicit 'error' variable to a given
39 * expression's value (usually a function call), and convert it to a
40 * boolean which is set whenever the value is != 0.
43 #define FT_SET_ERROR( expression ) \
44 ( ( error = (expression) ) != 0 )
48 /*************************************************************************/
49 /*************************************************************************/
50 /*************************************************************************/
53 /**** M E M O R Y ****/
56 /*************************************************************************/
57 /*************************************************************************/
58 /*************************************************************************/
61 /* The calculation `NULL + n' is undefined in C. Even if the resulting */
62 /* pointer doesn't get dereferenced, this causes warnings with */
65 /* We thus provide a macro that should be used if `base' can be NULL. */
66 #define FT_OFFSET( base, count ) ( (base) ? (base) + (count) : NULL )
70 * C++ refuses to handle statements like p = (void*)anything, with `p' a
71 * typed pointer. Since we don't have a `typeof' operator in standard C++,
72 * we have to use a template to emulate it.
79 template <typename T> inline T*
83 return static_cast <T*> ( v );
87 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
91 #define FT_ASSIGNP( p, val ) (p) = (val)
97 #ifdef FT_DEBUG_MEMORY
99 FT_BASE( const char* ) ft_debug_file_;
100 FT_BASE( long ) ft_debug_lineno_;
102 #define FT_DEBUG_INNER( exp ) ( ft_debug_file_ = __FILE__, \
103 ft_debug_lineno_ = __LINE__, \
106 #define FT_ASSIGNP_INNER( p, exp ) ( ft_debug_file_ = __FILE__, \
107 ft_debug_lineno_ = __LINE__, \
108 FT_ASSIGNP( p, exp ) )
110 #else /* !FT_DEBUG_MEMORY */
112 #define FT_DEBUG_INNER( exp ) (exp)
113 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
115 #endif /* !FT_DEBUG_MEMORY */
119 * The allocation functions return a pointer, and the error code is written
120 * to through the `p_error' parameter.
123 /* The `q' variants of the functions below (`q' for `quick') don't fill */
124 /* the allocated or reallocated memory with zero bytes. */
126 FT_BASE( FT_Pointer )
127 ft_mem_alloc( FT_Memory memory,
131 FT_BASE( FT_Pointer )
132 ft_mem_qalloc( FT_Memory memory,
136 FT_BASE( FT_Pointer )
137 ft_mem_realloc( FT_Memory memory,
144 FT_BASE( FT_Pointer )
145 ft_mem_qrealloc( FT_Memory memory,
153 ft_mem_free( FT_Memory memory,
157 /* The `Q' variants of the macros below (`Q' for `quick') don't fill */
158 /* the allocated or reallocated memory with zero bytes. */
160 #define FT_MEM_ALLOC( ptr, size ) \
161 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \
165 #define FT_MEM_FREE( ptr ) \
167 FT_DEBUG_INNER( ft_mem_free( memory, (ptr) ) ); \
171 #define FT_MEM_NEW( ptr ) \
172 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
174 #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
175 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
182 #define FT_MEM_QALLOC( ptr, size ) \
183 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \
187 #define FT_MEM_QNEW( ptr ) \
188 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
190 #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
191 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
198 #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
199 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
200 (FT_Long)(item_size), \
206 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
207 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
214 #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
215 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
216 (FT_Long)(item_size), \
222 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
223 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
231 #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
234 #define FT_MEM_SET( dest, byte, count ) \
235 ft_memset( dest, byte, (FT_Offset)(count) )
237 #define FT_MEM_COPY( dest, source, count ) \
238 ft_memcpy( dest, source, (FT_Offset)(count) )
240 #define FT_MEM_MOVE( dest, source, count ) \
241 ft_memmove( dest, source, (FT_Offset)(count) )
244 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
246 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
249 #define FT_ARRAY_ZERO( dest, count ) \
251 (FT_Offset)(count) * sizeof ( *(dest) ) )
253 #define FT_ARRAY_COPY( dest, source, count ) \
256 (FT_Offset)(count) * sizeof ( *(dest) ) )
258 #define FT_ARRAY_MOVE( dest, source, count ) \
261 (FT_Offset)(count) * sizeof ( *(dest) ) )
265 * Return the maximum number of addressable elements in an array. We limit
266 * ourselves to INT_MAX, rather than UINT_MAX, to avoid any problems.
268 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
270 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
273 /**************************************************************************
275 * The following functions macros expect that their pointer argument is
276 * _typed_ in order to automatically compute array element sizes.
279 #define FT_MEM_NEW_ARRAY( ptr, count ) \
280 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
287 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
288 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
295 #define FT_MEM_QNEW_ARRAY( ptr, count ) \
296 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
303 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
304 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
311 #define FT_ALLOC( ptr, size ) \
312 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
314 #define FT_REALLOC( ptr, cursz, newsz ) \
315 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
317 #define FT_ALLOC_MULT( ptr, count, item_size ) \
318 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
320 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
321 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
324 #define FT_QALLOC( ptr, size ) \
325 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
327 #define FT_QREALLOC( ptr, cursz, newsz ) \
328 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
330 #define FT_QALLOC_MULT( ptr, count, item_size ) \
331 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
333 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
334 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
337 #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
339 #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
341 #define FT_NEW_ARRAY( ptr, count ) \
342 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
344 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
345 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
347 #define FT_QNEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
349 #define FT_QNEW_ARRAY( ptr, count ) \
350 FT_MEM_SET_ERROR( FT_MEM_QNEW_ARRAY( ptr, count ) )
352 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
353 FT_MEM_SET_ERROR( FT_MEM_QRENEW_ARRAY( ptr, curcnt, newcnt ) )
356 FT_BASE( FT_Pointer )
357 ft_mem_strdup( FT_Memory memory,
361 FT_BASE( FT_Pointer )
362 ft_mem_dup( FT_Memory memory,
368 #define FT_MEM_STRDUP( dst, str ) \
369 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
371 #define FT_STRDUP( dst, str ) \
372 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
374 #define FT_MEM_DUP( dst, address, size ) \
375 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
377 #define FT_DUP( dst, address, size ) \
378 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
381 /* Return >= 1 if a truncation occurs. */
382 /* Return 0 if the source string fits the buffer. */
383 /* This is *not* the same as strlcpy(). */
385 ft_mem_strcpyn( char* dst,
389 #define FT_STRCPYN( dst, src, size ) \
390 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
395 #endif /* FTMEMORY_H_ */