1 /***************************************************************************/ 2 /* */ 3 /* ftmemory.h */ 4 /* */ 5 /* The FreeType memory management macros (specification). */ 6 /* */ 7 /* Copyright 1996-2018 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg */ 9 /* */ 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. */ 15 /* */ 16 /***************************************************************************/ 17 18 19 #ifndef FTMEMORY_H_ 20 #define FTMEMORY_H_ 21 22 23 #include <ft2build.h> 24 #include FT_CONFIG_CONFIG_H 25 #include FT_TYPES_H 26 27 28 FT_BEGIN_HEADER 29 30 31 /*************************************************************************/ 32 /* */ 33 /* <Macro> */ 34 /* FT_SET_ERROR */ 35 /* */ 36 /* <Description> */ 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. */ 40 /* */ 41 #undef FT_SET_ERROR 42 #define FT_SET_ERROR( expression ) \ 43 ( ( error = (expression) ) != 0 ) 44 45 46 47 /*************************************************************************/ 48 /*************************************************************************/ 49 /*************************************************************************/ 50 /**** ****/ 51 /**** ****/ 52 /**** M E M O R Y ****/ 53 /**** ****/ 54 /**** ****/ 55 /*************************************************************************/ 56 /*************************************************************************/ 57 /*************************************************************************/ 58 59 60 /* 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. 64 */ 65 66 #ifdef __cplusplus 67 68 extern "C++" 69 { 70 template <typename T> inline T* 71 cplusplus_typeof( T*, 72 void *v ) 73 { 74 return static_cast <T*> ( v ); 75 } 76 } 77 78 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) ) 79 80 #else 81 82 #define FT_ASSIGNP( p, val ) (p) = (val) 83 90 FT_BASE( const char* ) _ft_debug_file; 91 FT_BASE( long ) _ft_debug_lineno; 92 93 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \ 94 _ft_debug_lineno = __LINE__, \ 95 (exp) ) 96 97 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \ 98 _ft_debug_lineno = __LINE__, \ 99 FT_ASSIGNP( p, exp ) ) 100 101 #else /* !FT_DEBUG_MEMORY */ 102 103 #define FT_DEBUG_INNER( exp ) (exp) 104 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp ) 105 106 #endif /* !FT_DEBUG_MEMORY */ 107 108 109 /* 110 * The allocation functions return a pointer, and the error code 111 * is written to through the `p_error' parameter. 112 */ 113 114 /* The `q' variants of the functions below (`q' for `quick') don't fill */ 115 /* the allocated or reallocated memory with zero bytes. */ 116 117 FT_BASE( FT_Pointer ) 118 ft_mem_alloc( FT_Memory memory, 119 FT_Long size, 120 FT_Error *p_error ); 121 122 FT_BASE( FT_Pointer ) 123 ft_mem_qalloc( FT_Memory memory, 124 FT_Long size, 125 FT_Error *p_error ); 126 127 FT_BASE( FT_Pointer ) 128 ft_mem_realloc( FT_Memory memory, 129 FT_Long item_size, 130 FT_Long cur_count, 131 FT_Long new_count, 236 237 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) ) 238 239 240 #define FT_ARRAY_ZERO( dest, count ) \ 241 FT_MEM_ZERO( dest, \ 242 (FT_Offset)(count) * sizeof ( *(dest) ) ) 243 244 #define FT_ARRAY_COPY( dest, source, count ) \ 245 FT_MEM_COPY( dest, \ 246 source, \ 247 (FT_Offset)(count) * sizeof ( *(dest) ) ) 248 249 #define FT_ARRAY_MOVE( dest, source, count ) \ 250 FT_MEM_MOVE( dest, \ 251 source, \ 252 (FT_Offset)(count) * sizeof ( *(dest) ) ) 253 254 255 /* 256 * Return the maximum number of addressable elements in an array. 257 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid 258 * any problems. 259 */ 260 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) ) 261 262 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) ) 263 264 265 /*************************************************************************/ 266 /* */ 267 /* The following functions macros expect that their pointer argument is */ 268 /* _typed_ in order to automatically compute array element sizes. */ 269 /* */ 270 271 #define FT_MEM_NEW_ARRAY( ptr, count ) \ 272 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 273 sizeof ( *(ptr) ), \ 274 0, \ 275 (FT_Long)(count), \ 276 NULL, \ 277 &error ) ) 278 279 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \ 280 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 281 sizeof ( *(ptr) ), \ 282 (FT_Long)(cursz), \ 283 (FT_Long)(newsz), \ 284 (ptr), \ 285 &error ) ) 286 287 #define FT_MEM_QNEW_ARRAY( ptr, count ) \ 288 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 289 sizeof ( *(ptr) ), \ | 1 /**************************************************************************** 2 * 3 * ftmemory.h 4 * 5 * The FreeType memory management macros (specification). 6 * 7 * Copyright (C) 1996-2019 by 8 * David Turner, Robert Wilhelm, and Werner Lemberg 9 * 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. 15 * 16 */ 17 18 19 #ifndef FTMEMORY_H_ 20 #define FTMEMORY_H_ 21 22 23 #include <ft2build.h> 24 #include FT_CONFIG_CONFIG_H 25 #include FT_TYPES_H 26 27 28 FT_BEGIN_HEADER 29 30 31 /************************************************************************** 32 * 33 * @macro: 34 * FT_SET_ERROR 35 * 36 * @description: 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. 40 */ 41 #undef FT_SET_ERROR 42 #define FT_SET_ERROR( expression ) \ 43 ( ( error = (expression) ) != 0 ) 44 45 46 47 /*************************************************************************/ 48 /*************************************************************************/ 49 /*************************************************************************/ 50 /**** ****/ 51 /**** ****/ 52 /**** M E M O R Y ****/ 53 /**** ****/ 54 /**** ****/ 55 /*************************************************************************/ 56 /*************************************************************************/ 57 /*************************************************************************/ 58 59 60 /* 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 C++, 63 * we have to use a template to emulate it. 64 */ 65 66 #ifdef __cplusplus 67 68 extern "C++" 69 { 70 template <typename T> inline T* 71 cplusplus_typeof( T*, 72 void *v ) 73 { 74 return static_cast <T*> ( v ); 75 } 76 } 77 78 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) ) 79 80 #else 81 82 #define FT_ASSIGNP( p, val ) (p) = (val) 83 90 FT_BASE( const char* ) _ft_debug_file; 91 FT_BASE( long ) _ft_debug_lineno; 92 93 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \ 94 _ft_debug_lineno = __LINE__, \ 95 (exp) ) 96 97 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \ 98 _ft_debug_lineno = __LINE__, \ 99 FT_ASSIGNP( p, exp ) ) 100 101 #else /* !FT_DEBUG_MEMORY */ 102 103 #define FT_DEBUG_INNER( exp ) (exp) 104 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp ) 105 106 #endif /* !FT_DEBUG_MEMORY */ 107 108 109 /* 110 * The allocation functions return a pointer, and the error code is written 111 * to through the `p_error' parameter. 112 */ 113 114 /* The `q' variants of the functions below (`q' for `quick') don't fill */ 115 /* the allocated or reallocated memory with zero bytes. */ 116 117 FT_BASE( FT_Pointer ) 118 ft_mem_alloc( FT_Memory memory, 119 FT_Long size, 120 FT_Error *p_error ); 121 122 FT_BASE( FT_Pointer ) 123 ft_mem_qalloc( FT_Memory memory, 124 FT_Long size, 125 FT_Error *p_error ); 126 127 FT_BASE( FT_Pointer ) 128 ft_mem_realloc( FT_Memory memory, 129 FT_Long item_size, 130 FT_Long cur_count, 131 FT_Long new_count, 236 237 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) ) 238 239 240 #define FT_ARRAY_ZERO( dest, count ) \ 241 FT_MEM_ZERO( dest, \ 242 (FT_Offset)(count) * sizeof ( *(dest) ) ) 243 244 #define FT_ARRAY_COPY( dest, source, count ) \ 245 FT_MEM_COPY( dest, \ 246 source, \ 247 (FT_Offset)(count) * sizeof ( *(dest) ) ) 248 249 #define FT_ARRAY_MOVE( dest, source, count ) \ 250 FT_MEM_MOVE( dest, \ 251 source, \ 252 (FT_Offset)(count) * sizeof ( *(dest) ) ) 253 254 255 /* 256 * Return the maximum number of addressable elements in an array. We limit 257 * ourselves to INT_MAX, rather than UINT_MAX, to avoid any problems. 258 */ 259 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) ) 260 261 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) ) 262 263 264 /************************************************************************** 265 * 266 * The following functions macros expect that their pointer argument is 267 * _typed_ in order to automatically compute array element sizes. 268 */ 269 270 #define FT_MEM_NEW_ARRAY( ptr, count ) \ 271 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 272 sizeof ( *(ptr) ), \ 273 0, \ 274 (FT_Long)(count), \ 275 NULL, \ 276 &error ) ) 277 278 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \ 279 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 280 sizeof ( *(ptr) ), \ 281 (FT_Long)(cursz), \ 282 (FT_Long)(newsz), \ 283 (ptr), \ 284 &error ) ) 285 286 #define FT_MEM_QNEW_ARRAY( ptr, count ) \ 287 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 288 sizeof ( *(ptr) ), \ |