1 /***************************************************************************/
5 /* The FreeType memory management macros (specification). */
7 /* Copyright 1996-2001, 2002 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 /***************************************************************************/
19 #ifndef __FTMEMORY_H__
20 #define __FTMEMORY_H__
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 )
46 /*************************************************************************/
47 /*************************************************************************/
48 /*************************************************************************/
51 /**** M E M O R Y ****/
54 /*************************************************************************/
55 /*************************************************************************/
56 /*************************************************************************/
58 #ifdef FT_DEBUG_MEMORY
61 FT_Alloc_Debug( FT_Memory memory,
64 const char* file_name,
68 FT_Realloc_Debug( FT_Memory memory,
72 const char* file_name,
76 FT_Free_Debug( FT_Memory memory,
78 const char* file_name,
84 /*************************************************************************/
90 /* Allocates a new block of memory. The returned area is always */
91 /* zero-filled; this is a strong convention in many FreeType parts. */
94 /* memory :: A handle to a given `memory object' which handles */
97 /* size :: The size in bytes of the block to allocate. */
100 /* P :: A pointer to the fresh new block. It should be set to */
101 /* NULL if `size' is 0, or in case of error. */
104 /* FreeType error code. 0 means success. */
107 FT_Alloc( FT_Memory memory,
112 /*************************************************************************/
118 /* Reallocates a block of memory pointed to by `*P' to `Size' bytes */
119 /* from the heap, possibly changing `*P'. */
122 /* memory :: A handle to a given `memory object' which handles */
125 /* current :: The current block size in bytes. */
127 /* size :: The new block size in bytes. */
130 /* P :: A pointer to the fresh new block. It should be set to */
131 /* NULL if `size' is 0, or in case of error. */
134 /* FreeType error code. 0 means success. */
137 /* All callers of FT_Realloc() _must_ provide the current block size */
138 /* as well as the new one. */
141 FT_Realloc( FT_Memory memory,
147 /*************************************************************************/
153 /* Releases a given block of memory allocated through FT_Alloc(). */
156 /* memory :: A handle to a given `memory object' which handles */
157 /* memory deallocation */
159 /* P :: This is the _address_ of a _pointer_ which points to the */
160 /* allocated block. It is always set to NULL on exit. */
163 /* FreeType error code. 0 means success. */
166 /* If P or *P are NULL, this function should return successfully. */
167 /* This is a strong convention within all of FreeType and its */
171 FT_Free( FT_Memory memory,
175 #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
177 #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
179 #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
182 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
184 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
187 /*************************************************************************/
189 /* We first define FT_MEM_ALLOC, FT_MEM_REALLOC, and FT_MEM_FREE. All */
190 /* macros use an _implicit_ `memory' parameter to access the current */
191 /* memory allocator. */
194 #ifdef FT_DEBUG_MEMORY
196 #define FT_MEM_ALLOC( _pointer_, _size_ ) \
197 FT_Alloc_Debug( memory, _size_, \
198 (void**)&(_pointer_), __FILE__, __LINE__ )
200 #define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
201 FT_Realloc_Debug( memory, _current_, _size_, \
202 (void**)&(_pointer_), __FILE__, __LINE__ )
204 #define FT_MEM_FREE( _pointer_ ) \
205 FT_Free_Debug( memory, (void**)&(_pointer_), __FILE__, __LINE__ )
208 #else /* !FT_DEBUG_MEMORY */
211 #define FT_MEM_ALLOC( _pointer_, _size_ ) \
212 FT_Alloc( memory, _size_, (void**)&(_pointer_) )
214 #define FT_MEM_FREE( _pointer_ ) \
215 FT_Free( memory, (void**)&(_pointer_) )
217 #define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
218 FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
221 #endif /* !FT_DEBUG_MEMORY */
224 /*************************************************************************/
226 /* The following functions macros expect that their pointer argument is */
227 /* _typed_ in order to automatically compute array element sizes. */
230 #define FT_MEM_NEW( _pointer_ ) \
231 FT_MEM_ALLOC( _pointer_, sizeof ( *(_pointer_) ) )
233 #define FT_MEM_NEW_ARRAY( _pointer_, _count_ ) \
234 FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
236 #define FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
237 FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ), \
238 (_new_) * sizeof ( *(_pointer_) ) )
241 /*************************************************************************/
243 /* the following macros are obsolete but kept for compatibility reasons */
246 #define FT_MEM_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
247 FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( _type_ ) )
249 #define FT_MEM_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
250 FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( _type ), \
251 (_new_) * sizeof ( _type_ ) )
254 /*************************************************************************/
256 /* The following macros are variants of their FT_MEM_XXXX equivalents; */
257 /* they are used to set an _implicit_ `error' variable and return TRUE */
258 /* if an error occured (i.e. if 'error != 0'). */
261 #define FT_ALLOC( _pointer_, _size_ ) \
262 FT_SET_ERROR( FT_MEM_ALLOC( _pointer_, _size_ ) )
264 #define FT_REALLOC( _pointer_, _current_, _size_ ) \
265 FT_SET_ERROR( FT_MEM_REALLOC( _pointer_, _current_, _size_ ) )
267 #define FT_FREE( _pointer_ ) \
268 FT_MEM_FREE( _pointer_ )
270 #define FT_NEW( _pointer_ ) \
271 FT_SET_ERROR( FT_MEM_NEW( _pointer_ ) )
273 #define FT_NEW_ARRAY( _pointer_, _count_ ) \
274 FT_SET_ERROR( FT_MEM_NEW_ARRAY( _pointer_, _count_ ) )
276 #define FT_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
277 FT_SET_ERROR( FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) )
279 #define FT_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
280 FT_SET_ERROR( FT_MEM_ALLOC( _pointer_, \
281 (_count_) * sizeof ( _type_ ) ) )
283 #define FT_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
284 FT_SET_ERROR( FT_MEM_REALLOC( _pointer_, \
285 (_old_) * sizeof ( _type_ ), \
286 (_new_) * sizeof ( _type_ ) ) )
293 #endif /* __FTMEMORY_H__ */