ftmemory.h

Go to the documentation of this file.
00001 /***************************************************************************/
00002 /*                                                                         */
00003 /*  ftmemory.h                                                             */
00004 /*                                                                         */
00005 /*    The FreeType memory management macros (specification).               */
00006 /*                                                                         */
00007 /*  Copyright 1996-2001, 2002, 2004, 2005, 2006, 2007 by                   */
00008 /*  David Turner, Robert Wilhelm, and Werner Lemberg                       */
00009 /*                                                                         */
00010 /*  This file is part of the FreeType project, and may only be used,       */
00011 /*  modified, and distributed under the terms of the FreeType project      */
00012 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
00013 /*  this file you indicate that you have read the license and              */
00014 /*  understand and accept it fully.                                        */
00015 /*                                                                         */
00016 /***************************************************************************/
00017 
00018 
00019 #ifndef __FTMEMORY_H__
00020 #define __FTMEMORY_H__
00021 
00022 
00023 #include <ft2build.h>
00024 #include FT_CONFIG_CONFIG_H
00025 #include FT_TYPES_H
00026 
00027 
00028 FT_BEGIN_HEADER
00029 
00030 
00031   /*************************************************************************/
00032   /*                                                                       */
00033   /* <Macro>                                                               */
00034   /*    FT_SET_ERROR                                                       */
00035   /*                                                                       */
00036   /* <Description>                                                         */
00037   /*    This macro is used to set an implicit `error' variable to a given  */
00038   /*    expression's value (usually a function call), and convert it to a  */
00039   /*    boolean which is set whenever the value is != 0.                   */
00040   /*                                                                       */
00041 #undef  FT_SET_ERROR
00042 #define FT_SET_ERROR( expression ) \
00043           ( ( error = (expression) ) != 0 )
00044 
00045 
00046 
00047   /*************************************************************************/
00048   /*************************************************************************/
00049   /*************************************************************************/
00050   /****                                                                 ****/
00051   /****                                                                 ****/
00052   /****                           M E M O R Y                           ****/
00053   /****                                                                 ****/
00054   /****                                                                 ****/
00055   /*************************************************************************/
00056   /*************************************************************************/
00057   /*************************************************************************/
00058 
00059 
00060   /*
00061    *  C++ refuses to handle statements like p = (void*)anything; where `p'
00062    *  is a typed pointer.  Since we don't have a `typeof' operator in
00063    *  standard C++, we have to use ugly casts.
00064    */
00065 
00066 #ifdef __cplusplus
00067 #define FT_ASSIGNP( p, val )  *((void**)&(p)) = (val)
00068 #else
00069 #define FT_ASSIGNP( p, val )  (p) = (val)
00070 #endif
00071 
00072 
00073 
00074 #ifdef FT_DEBUG_MEMORY
00075 
00076   FT_BASE( const char* )  _ft_debug_file;
00077   FT_BASE( long )         _ft_debug_lineno;
00078 
00079 #define FT_DEBUG_INNER( exp )  ( _ft_debug_file   = __FILE__, \
00080                                  _ft_debug_lineno = __LINE__, \
00081                                  (exp) )
00082 
00083 #define FT_ASSIGNP_INNER( p, exp )  ( _ft_debug_file   = __FILE__, \
00084                                       _ft_debug_lineno = __LINE__, \
00085                                       FT_ASSIGNP( p, exp ) )
00086 
00087 #else /* !FT_DEBUG_MEMORY */
00088 
00089 #define FT_DEBUG_INNER( exp )       (exp)
00090 #define FT_ASSIGNP_INNER( p, exp )  FT_ASSIGNP( p, exp )
00091 
00092 #endif /* !FT_DEBUG_MEMORY */
00093 
00094 
00095   /*
00096    *  The allocation functions return a pointer, and the error code
00097    *  is written to through the `p_error' parameter.  See below for
00098    *  for documentation.
00099    */
00100 
00101   FT_BASE( FT_Pointer )
00102   ft_mem_alloc( FT_Memory  memory,
00103                 FT_Long    size,
00104                 FT_Error  *p_error );
00105 
00106   FT_BASE( FT_Pointer )
00107   ft_mem_qalloc( FT_Memory  memory,
00108                  FT_Long    size,
00109                  FT_Error  *p_error );
00110 
00111   FT_BASE( FT_Pointer )
00112   ft_mem_realloc( FT_Memory  memory,
00113                   FT_Long    item_size,
00114                   FT_Long    cur_count,
00115                   FT_Long    new_count,
00116                   void*      block,
00117                   FT_Error  *p_error );
00118 
00119   FT_BASE( FT_Pointer )
00120   ft_mem_qrealloc( FT_Memory  memory,
00121                    FT_Long    item_size,
00122                    FT_Long    cur_count,
00123                    FT_Long    new_count,
00124                    void*      block,
00125                    FT_Error  *p_error );
00126 
00127   FT_BASE( void )
00128   ft_mem_free( FT_Memory    memory,
00129                const void*  P );
00130 
00131 
00132 #define FT_MEM_ALLOC( ptr, size )                                         \
00133           FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, (size), &error ) )
00134 
00135 #define FT_MEM_FREE( ptr )                \
00136           FT_BEGIN_STMNT                  \
00137             ft_mem_free( memory, (ptr) ); \
00138             (ptr) = NULL;                 \
00139           FT_END_STMNT
00140 
00141 #define FT_MEM_NEW( ptr )                        \
00142           FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
00143 
00144 #define FT_MEM_REALLOC( ptr, cursz, newsz )                        \
00145           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, 1,        \
00146                                                  (cursz), (newsz), \
00147                                                  (ptr), &error ) )
00148 
00149 #define FT_MEM_QALLOC( ptr, size )                                         \
00150           FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, (size), &error ) )
00151 
00152 #define FT_MEM_QNEW( ptr )                        \
00153           FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
00154 
00155 #define FT_MEM_QREALLOC( ptr, cursz, newsz )                         \
00156           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, 1,        \
00157                                                   (cursz), (newsz), \
00158                                                   (ptr), &error ) )
00159 
00160 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz )                             \
00161           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
00162                                                   (cursz), (newsz),          \
00163                                                   (ptr), &error ) )
00164 
00165 #define FT_MEM_ALLOC_MULT( ptr, count, item_size )                    \
00166           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (item_size), \
00167                                                  0, (count),          \
00168                                                  NULL, &error ) )
00169 
00170 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz )            \
00171           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (itmsz),    \
00172                                                  (oldcnt), (newcnt), \
00173                                                  (ptr), &error ) )
00174 
00175 #define FT_MEM_QALLOC_MULT( ptr, count, item_size )                    \
00176           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (item_size), \
00177                                                   0, (count),          \
00178                                                   NULL, &error ) )
00179 
00180 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz)             \
00181           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (itmsz),    \
00182                                                   (oldcnt), (newcnt), \
00183                                                   (ptr), &error ) )
00184 
00185 
00186 #define FT_MEM_SET_ERROR( cond )  ( (cond), error != 0 )
00187 
00188 
00189 #define FT_MEM_SET( dest, byte, count )     ft_memset( dest, byte, count )
00190 
00191 #define FT_MEM_COPY( dest, source, count )  ft_memcpy( dest, source, count )
00192 
00193 #define FT_MEM_MOVE( dest, source, count )  ft_memmove( dest, source, count )
00194 
00195 
00196 #define FT_MEM_ZERO( dest, count )  FT_MEM_SET( dest, 0, count )
00197 
00198 #define FT_ZERO( p )                FT_MEM_ZERO( p, sizeof ( *(p) ) )
00199 
00200 
00201 #define FT_ARRAY_ZERO( dest, count )                        \
00202           FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
00203 
00204 #define FT_ARRAY_COPY( dest, source, count )                        \
00205           FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
00206 
00207 #define FT_ARRAY_MOVE( dest, source, count )                        \
00208           FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
00209 
00210 
00211   /*
00212    *  Return the maximum number of addressable elements in an array.
00213    *  We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
00214    *  any problems.
00215    */
00216 #define FT_ARRAY_MAX( ptr )           ( FT_INT_MAX / sizeof ( *(ptr) ) )
00217 
00218 #define FT_ARRAY_CHECK( ptr, count )  ( (count) <= FT_ARRAY_MAX( ptr ) )
00219 
00220 
00221   /*************************************************************************/
00222   /*                                                                       */
00223   /* The following functions macros expect that their pointer argument is  */
00224   /* _typed_ in order to automatically compute array element sizes.        */
00225   /*                                                                       */
00226 
00227 #define FT_MEM_NEW_ARRAY( ptr, count )                                      \
00228           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
00229                                                  0, (count),                \
00230                                                  NULL, &error ) )
00231 
00232 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz )                             \
00233           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
00234                                                  (cursz), (newsz),          \
00235                                                  (ptr), &error ) )
00236 
00237 #define FT_MEM_QNEW_ARRAY( ptr, count )                                      \
00238           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
00239                                                   0, (count),                \
00240                                                   NULL, &error ) )
00241 
00242 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz )                             \
00243           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
00244                                                   (cursz), (newsz),          \
00245                                                   (ptr), &error ) )
00246 
00247 
00248 #define FT_ALLOC( ptr, size )                           \
00249           FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
00250 
00251 #define FT_REALLOC( ptr, cursz, newsz )                           \
00252           FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
00253 
00254 #define FT_ALLOC_MULT( ptr, count, item_size )                           \
00255           FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
00256 
00257 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz )              \
00258           FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt,      \
00259                                                  newcnt, itmsz ) )
00260 
00261 #define FT_QALLOC( ptr, size )                           \
00262           FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
00263 
00264 #define FT_QREALLOC( ptr, cursz, newsz )                           \
00265           FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
00266 
00267 #define FT_QALLOC_MULT( ptr, count, item_size )                           \
00268           FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
00269 
00270 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz )              \
00271           FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt,      \
00272                                                   newcnt, itmsz ) )
00273 
00274 #define FT_FREE( ptr )  FT_MEM_FREE( ptr )
00275 
00276 #define FT_NEW( ptr )  FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
00277 
00278 #define FT_NEW_ARRAY( ptr, count )                           \
00279           FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
00280 
00281 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt )                           \
00282           FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
00283 
00284 #define FT_QNEW( ptr )                           \
00285           FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
00286 
00287 #define FT_QNEW_ARRAY( ptr, count )                          \
00288           FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
00289 
00290 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt )                          \
00291           FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
00292 
00293 
00294 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
00295 
00296   FT_BASE( FT_Error )
00297   FT_Alloc( FT_Memory  memory,
00298             FT_Long    size,
00299             void*     *P );
00300 
00301   FT_BASE( FT_Error )
00302   FT_QAlloc( FT_Memory  memory,
00303              FT_Long    size,
00304              void*     *p );
00305 
00306   FT_BASE( FT_Error )
00307   FT_Realloc( FT_Memory  memory,
00308               FT_Long    current,
00309               FT_Long    size,
00310               void*     *P );
00311 
00312   FT_BASE( FT_Error )
00313   FT_QRealloc( FT_Memory  memory,
00314                FT_Long    current,
00315                FT_Long    size,
00316                void*     *p );
00317 
00318   FT_BASE( void )
00319   FT_Free( FT_Memory  memory,
00320            void*     *P );
00321 
00322 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
00323 
00324 
00325   FT_BASE( FT_Pointer )
00326   ft_mem_strdup( FT_Memory    memory,
00327                  const char*  str,
00328                  FT_Error    *p_error );
00329 
00330   FT_BASE( FT_Pointer )
00331   ft_mem_dup( FT_Memory    memory,
00332               const void*  address,
00333               FT_ULong     size,
00334               FT_Error    *p_error );
00335 
00336 #define FT_MEM_STRDUP( dst, str )                                            \
00337           (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
00338 
00339 #define FT_STRDUP( dst, str )                           \
00340           FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
00341 
00342 #define FT_MEM_DUP( dst, address, size )                                    \
00343           (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
00344 
00345 #define FT_DUP( dst, address, size )                           \
00346           FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
00347 
00348 
00349   /* Return >= 1 if a truncation occurs.            */
00350   /* Return 0 if the source string fits the buffer. */
00351   /* This is *not* the same as strlcpy().           */
00352   FT_BASE( FT_Int )
00353   ft_mem_strcpyn( char*        dst,
00354                   const char*  src,
00355                   FT_ULong     size );
00356 
00357 #define FT_STRCPYN( dst, src, size )                                         \
00358           ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
00359 
00360  /* */
00361 
00362 
00363 FT_END_HEADER
00364 
00365 #endif /* __FTMEMORY_H__ */
00366 
00367 
00368 /* END */

Generated on Tue Jul 5 14:13:38 2011 for ROOT_528-00b_version by  doxygen 1.5.1