1/***************************************************************************/ 2/* */ 3/* ftmemory.h */ 4/* */ 5/* The FreeType memory management macros (specification). */ 6/* */ 7/* Copyright 1996-2015 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 28FT_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 template <typename T> inline T* 70 cplusplus_typeof( T*, 71 void *v ) 72 { 73 return static_cast <T*> ( v ); 74 } 75 76#define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) ) 77 78#else 79 80#define FT_ASSIGNP( p, val ) (p) = (val) 81 82#endif 83 84 85 86#ifdef FT_DEBUG_MEMORY 87 88 FT_BASE( const char* ) _ft_debug_file; 89 FT_BASE( long ) _ft_debug_lineno; 90 91#define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \ 92 _ft_debug_lineno = __LINE__, \ 93 (exp) ) 94 95#define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \ 96 _ft_debug_lineno = __LINE__, \ 97 FT_ASSIGNP( p, exp ) ) 98 99#else /* !FT_DEBUG_MEMORY */ 100 101#define FT_DEBUG_INNER( exp ) (exp) 102#define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp ) 103 104#endif /* !FT_DEBUG_MEMORY */ 105 106 107 /* 108 * The allocation functions return a pointer, and the error code 109 * is written to through the `p_error' parameter. See below for 110 * for documentation. 111 */ 112 113 FT_BASE( FT_Pointer ) 114 ft_mem_alloc( FT_Memory memory, 115 FT_Long size, 116 FT_Error *p_error ); 117 118 FT_BASE( FT_Pointer ) 119 ft_mem_qalloc( FT_Memory memory, 120 FT_Long size, 121 FT_Error *p_error ); 122 123 FT_BASE( FT_Pointer ) 124 ft_mem_realloc( FT_Memory memory, 125 FT_Long item_size, 126 FT_Long cur_count, 127 FT_Long new_count, 128 void* block, 129 FT_Error *p_error ); 130 131 FT_BASE( FT_Pointer ) 132 ft_mem_qrealloc( FT_Memory memory, 133 FT_Long item_size, 134 FT_Long cur_count, 135 FT_Long new_count, 136 void* block, 137 FT_Error *p_error ); 138 139 FT_BASE( void ) 140 ft_mem_free( FT_Memory memory, 141 const void* P ); 142 143 144#define FT_MEM_ALLOC( ptr, size ) \ 145 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \ 146 (FT_Long)(size), \ 147 &error ) ) 148 149#define FT_MEM_FREE( ptr ) \ 150 FT_BEGIN_STMNT \ 151 ft_mem_free( memory, (ptr) ); \ 152 (ptr) = NULL; \ 153 FT_END_STMNT 154 155#define FT_MEM_NEW( ptr ) \ 156 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) ) 157 158#define FT_MEM_REALLOC( ptr, cursz, newsz ) \ 159 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 160 1, \ 161 (FT_Long)(cursz), \ 162 (FT_Long)(newsz), \ 163 (ptr), \ 164 &error ) ) 165 166#define FT_MEM_QALLOC( ptr, size ) \ 167 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \ 168 (FT_Long)(size), \ 169 &error ) ) 170 171#define FT_MEM_QNEW( ptr ) \ 172 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) ) 173 174#define FT_MEM_QREALLOC( ptr, cursz, newsz ) \ 175 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 176 1, \ 177 (FT_Long)(cursz), \ 178 (FT_Long)(newsz), \ 179 (ptr), \ 180 &error ) ) 181 182#define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \ 183 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 184 (FT_Long)(item_size), \ 185 0, \ 186 (FT_Long)(count), \ 187 NULL, \ 188 &error ) ) 189 190#define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \ 191 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 192 (FT_Long)(itmsz), \ 193 (FT_Long)(oldcnt), \ 194 (FT_Long)(newcnt), \ 195 (ptr), \ 196 &error ) ) 197 198#define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \ 199 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 200 (FT_Long)(item_size), \ 201 0, \ 202 (FT_Long)(count), \ 203 NULL, \ 204 &error ) ) 205 206#define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \ 207 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 208 (FT_Long)(itmsz), \ 209 (FT_Long)(oldcnt), \ 210 (FT_Long)(newcnt), \ 211 (ptr), \ 212 &error ) ) 213 214 215#define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 ) 216 217 218#define FT_MEM_SET( dest, byte, count ) \ 219 ft_memset( dest, byte, (FT_Offset)(count) ) 220 221#define FT_MEM_COPY( dest, source, count ) \ 222 ft_memcpy( dest, source, (FT_Offset)(count) ) 223 224#define FT_MEM_MOVE( dest, source, count ) \ 225 ft_memmove( dest, source, (FT_Offset)(count) ) 226 227 228#define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count ) 229 230#define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) ) 231 232 233#define FT_ARRAY_ZERO( dest, count ) \ 234 FT_MEM_ZERO( dest, \ 235 (FT_Offset)(count) * sizeof ( *(dest) ) ) 236 237#define FT_ARRAY_COPY( dest, source, count ) \ 238 FT_MEM_COPY( dest, \ 239 source, \ 240 (FT_Offset)(count) * sizeof ( *(dest) ) ) 241 242#define FT_ARRAY_MOVE( dest, source, count ) \ 243 FT_MEM_MOVE( dest, \ 244 source, \ 245 (FT_Offset)(count) * sizeof ( *(dest) ) ) 246 247 248 /* 249 * Return the maximum number of addressable elements in an array. 250 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid 251 * any problems. 252 */ 253#define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) ) 254 255#define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) ) 256 257 258 /*************************************************************************/ 259 /* */ 260 /* The following functions macros expect that their pointer argument is */ 261 /* _typed_ in order to automatically compute array element sizes. */ 262 /* */ 263 264#define FT_MEM_NEW_ARRAY( ptr, count ) \ 265 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 266 sizeof ( *(ptr) ), \ 267 0, \ 268 (FT_Long)(count), \ 269 NULL, \ 270 &error ) ) 271 272#define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \ 273 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \ 274 sizeof ( *(ptr) ), \ 275 (FT_Long)(cursz), \ 276 (FT_Long)(newsz), \ 277 (ptr), \ 278 &error ) ) 279 280#define FT_MEM_QNEW_ARRAY( ptr, count ) \ 281 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 282 sizeof ( *(ptr) ), \ 283 0, \ 284 (FT_Long)(count), \ 285 NULL, \ 286 &error ) ) 287 288#define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \ 289 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \ 290 sizeof ( *(ptr) ), \ 291 (FT_Long)(cursz), \ 292 (FT_Long)(newsz), \ 293 (ptr), \ 294 &error ) ) 295 296#define FT_ALLOC( ptr, size ) \ 297 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) ) 298 299#define FT_REALLOC( ptr, cursz, newsz ) \ 300 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) ) 301 302#define FT_ALLOC_MULT( ptr, count, item_size ) \ 303 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) ) 304 305#define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \ 306 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \ 307 newcnt, itmsz ) ) 308 309#define FT_QALLOC( ptr, size ) \ 310 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) ) 311 312#define FT_QREALLOC( ptr, cursz, newsz ) \ 313 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) ) 314 315#define FT_QALLOC_MULT( ptr, count, item_size ) \ 316 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) ) 317 318#define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \ 319 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \ 320 newcnt, itmsz ) ) 321 322#define FT_FREE( ptr ) FT_MEM_FREE( ptr ) 323 324#define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) ) 325 326#define FT_NEW_ARRAY( ptr, count ) \ 327 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) ) 328 329#define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \ 330 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) ) 331 332#define FT_QNEW( ptr ) \ 333 FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) ) 334 335#define FT_QNEW_ARRAY( ptr, count ) \ 336 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) ) 337 338#define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \ 339 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) ) 340 341 342 FT_BASE( FT_Pointer ) 343 ft_mem_strdup( FT_Memory memory, 344 const char* str, 345 FT_Error *p_error ); 346 347 FT_BASE( FT_Pointer ) 348 ft_mem_dup( FT_Memory memory, 349 const void* address, 350 FT_ULong size, 351 FT_Error *p_error ); 352 353 354#define FT_MEM_STRDUP( dst, str ) \ 355 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error ) 356 357#define FT_STRDUP( dst, str ) \ 358 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) ) 359 360#define FT_MEM_DUP( dst, address, size ) \ 361 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error ) 362 363#define FT_DUP( dst, address, size ) \ 364 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) ) 365 366 367 /* Return >= 1 if a truncation occurs. */ 368 /* Return 0 if the source string fits the buffer. */ 369 /* This is *not* the same as strlcpy(). */ 370 FT_BASE( FT_Int ) 371 ft_mem_strcpyn( char* dst, 372 const char* src, 373 FT_ULong size ); 374 375#define FT_STRCPYN( dst, src, size ) \ 376 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) ) 377 378 /* */ 379 380 381FT_END_HEADER 382 383#endif /* __FTMEMORY_H__ */ 384 385 386/* END */ 387