1 /***************************************************************************/
5 /* The FreeType memory management macros (specification). */
7 /* Copyright 1996-2001 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 /*************************************************************************/
59 /*************************************************************************/
65 /* Allocates a new block of memory. The returned area is always */
66 /* zero-filled; this is a strong convention in many FreeType parts. */
69 /* memory :: A handle to a given `memory object' which handles */
72 /* size :: The size in bytes of the block to allocate. */
75 /* P :: A pointer to the fresh new block. It should be set to */
76 /* NULL if `size' is 0, or in case of error. */
79 /* FreeType error code. 0 means success. */
82 FT_Alloc( FT_Memory memory,
87 /*************************************************************************/
93 /* Reallocates a block of memory pointed to by `*P' to `Size' bytes */
94 /* from the heap, possibly changing `*P'. */
97 /* memory :: A handle to a given `memory object' which handles */
100 /* current :: The current block size in bytes. */
102 /* size :: The new block size in bytes. */
105 /* P :: A pointer to the fresh new block. It should be set to */
106 /* NULL if `size' is 0, or in case of error. */
109 /* FreeType error code. 0 means success. */
112 /* All callers of FT_Realloc() _must_ provide the current block size */
113 /* as well as the new one. */
116 FT_Realloc( FT_Memory memory,
122 /*************************************************************************/
128 /* Releases a given block of memory allocated through FT_Alloc(). */
131 /* memory :: A handle to a given `memory object' which handles */
132 /* memory deallocation */
134 /* P :: This is the _address_ of a _pointer_ which points to the */
135 /* allocated block. It is always set to NULL on exit. */
138 /* FreeType error code. 0 means success. */
141 /* If P or *P are NULL, this function should return successfully. */
142 /* This is a strong convention within all of FreeType and its */
146 FT_Free( FT_Memory memory,
151 /* This `#include' is needed by the MEM_xxx() macros; it should be */
152 /* available on all platforms we know of. */
155 #define MEM_Set( dest, byte, count ) memset( dest, byte, count )
157 #define MEM_Copy( dest, source, count ) memcpy( dest, source, count )
159 #define MEM_Move( dest, source, count ) memmove( dest, source, count )
162 /*************************************************************************/
164 /* We now support closures to produce completely reentrant code. This */
165 /* means the allocation functions now takes an additional argument */
166 /* (`memory'). It is a handle to a given memory object, responsible for */
167 /* all low-level operations, including memory management and */
168 /* synchronisation. */
170 /* In order to keep our code readable and use the same macros in the */
171 /* font drivers and the rest of the library, MEM_Alloc(), ALLOC(), and */
172 /* ALLOC_ARRAY() now use an implicit variable, `memory'. It must be */
173 /* defined at all locations where a memory operation is queried. */
175 #define MEM_Alloc( _pointer_, _size_ ) \
176 FT_Alloc( memory, _size_, (void**)&(_pointer_) )
178 #define MEM_Alloc_Array( _pointer_, _count_, _type_ ) \
179 FT_Alloc( memory, (_count_)*sizeof ( _type_ ), \
180 (void**)&(_pointer_) )
182 #define MEM_Realloc( _pointer_, _current_, _size_ ) \
183 FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
185 #define MEM_Realloc_Array( _pointer_, _current_, _new_, _type_ ) \
186 FT_Realloc( memory, (_current_)*sizeof ( _type_ ), \
187 (_new_)*sizeof ( _type_ ), (void**)&(_pointer_) )
189 #define ALLOC( _pointer_, _size_ ) \
190 FT_SET_ERROR( MEM_Alloc( _pointer_, _size_ ) )
192 #define REALLOC( _pointer_, _current_, _size_ ) \
193 FT_SET_ERROR( MEM_Realloc( _pointer_, _current_, _size_ ) )
195 #define ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
196 FT_SET_ERROR( MEM_Alloc( _pointer_, \
197 (_count_)*sizeof ( _type_ ) ) )
199 #define REALLOC_ARRAY( _pointer_, _current_, _count_, _type_ ) \
200 FT_SET_ERROR( MEM_Realloc( _pointer_, \
201 (_current_)*sizeof ( _type_ ), \
202 (_count_)*sizeof ( _type_ ) ) )
204 #define FREE( _pointer_ ) FT_Free( memory, (void**)&(_pointer_) )
209 #endif /* __FTMEMORY_H__ */