1
/***************************************************************************/
5
/* The FreeType memory management macros (specification). */
7
/* Copyright 1996-2001, 2002, 2004 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_QAlloc_Debug( FT_Memory memory,
71
const char* file_name,
75
FT_Realloc_Debug( FT_Memory memory,
79
const char* file_name,
83
FT_QRealloc_Debug( FT_Memory memory,
87
const char* file_name,
91
FT_Free_Debug( FT_Memory memory,
93
const char* file_name,
99
/*************************************************************************/
105
/* Allocates a new block of memory. The returned area is always */
106
/* zero-filled; this is a strong convention in many FreeType parts. */
109
/* memory :: A handle to a given `memory object' which handles */
112
/* size :: The size in bytes of the block to allocate. */
115
/* P :: A pointer to the fresh new block. It should be set to */
116
/* NULL if `size' is 0, or in case of error. */
119
/* FreeType error code. 0 means success. */
122
FT_Alloc( FT_Memory memory,
128
FT_QAlloc( FT_Memory memory,
133
/*************************************************************************/
139
/* Reallocates a block of memory pointed to by `*P' to `Size' bytes */
140
/* from the heap, possibly changing `*P'. */
143
/* memory :: A handle to a given `memory object' which handles */
146
/* current :: The current block size in bytes. */
148
/* size :: The new block size in bytes. */
151
/* P :: A pointer to the fresh new block. It should be set to */
152
/* NULL if `size' is 0, or in case of error. */
155
/* FreeType error code. 0 means success. */
158
/* All callers of FT_Realloc() _must_ provide the current block size */
159
/* as well as the new one. */
162
FT_Realloc( FT_Memory memory,
169
FT_QRealloc( FT_Memory memory,
175
/*************************************************************************/
181
/* Releases a given block of memory allocated through FT_Alloc(). */
184
/* memory :: A handle to a given `memory object' which handles */
185
/* memory deallocation */
187
/* P :: This is the _address_ of a _pointer_ which points to the */
188
/* allocated block. It is always set to NULL on exit. */
191
/* If P or *P is NULL, this function should return successfully. */
192
/* This is a strong convention within all of FreeType and its */
196
FT_Free( FT_Memory memory,
200
#define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
202
#define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
204
#define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
207
#define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
209
#define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
211
#define FT_ARRAY_COPY( dest, source, count ) \
212
FT_MEM_COPY( dest, source, (count) * sizeof( *(dest) ) )
214
#define FT_ARRAY_MOVE( dest, source, count ) \
215
FT_MEM_MOVE( dest, source, (count) * sizeof( *(dest) ) )
218
/*************************************************************************/
220
/* We first define FT_MEM_ALLOC, FT_MEM_REALLOC, and FT_MEM_FREE. All */
221
/* macros use an _implicit_ `memory' parameter to access the current */
222
/* memory allocator. */
225
#ifdef FT_DEBUG_MEMORY
227
#define FT_MEM_ALLOC( _pointer_, _size_ ) \
228
FT_Alloc_Debug( memory, _size_, \
229
(void**)&(_pointer_), __FILE__, __LINE__ )
231
#define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
232
FT_Realloc_Debug( memory, _current_, _size_, \
233
(void**)&(_pointer_), __FILE__, __LINE__ )
235
#define FT_MEM_QALLOC( _pointer_, _size_ ) \
236
FT_QAlloc_Debug( memory, _size_, \
237
(void**)&(_pointer_), __FILE__, __LINE__ )
239
#define FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) \
240
FT_QRealloc_Debug( memory, _current_, _size_, \
241
(void**)&(_pointer_), __FILE__, __LINE__ )
243
#define FT_MEM_FREE( _pointer_ ) \
244
FT_Free_Debug( memory, (void**)&(_pointer_), __FILE__, __LINE__ )
247
#else /* !FT_DEBUG_MEMORY */
250
#define FT_MEM_ALLOC( _pointer_, _size_ ) \
251
FT_Alloc( memory, _size_, (void**)&(_pointer_) )
253
#define FT_MEM_FREE( _pointer_ ) \
254
FT_Free( memory, (void**)&(_pointer_) )
256
#define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
257
FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
259
#define FT_MEM_QALLOC( _pointer_, _size_ ) \
260
FT_QAlloc( memory, _size_, (void**)&(_pointer_) )
262
#define FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) \
263
FT_QRealloc( memory, _current_, _size_, (void**)&(_pointer_) )
265
#endif /* !FT_DEBUG_MEMORY */
268
/*************************************************************************/
270
/* The following functions macros expect that their pointer argument is */
271
/* _typed_ in order to automatically compute array element sizes. */
274
#define FT_MEM_NEW( _pointer_ ) \
275
FT_MEM_ALLOC( _pointer_, sizeof ( *(_pointer_) ) )
277
#define FT_MEM_NEW_ARRAY( _pointer_, _count_ ) \
278
FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
280
#define FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
281
FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ), \
282
(_new_) * sizeof ( *(_pointer_) ) )
284
#define FT_MEM_QNEW( _pointer_ ) \
285
FT_MEM_QALLOC( _pointer_, sizeof ( *(_pointer_) ) )
287
#define FT_MEM_QNEW_ARRAY( _pointer_, _count_ ) \
288
FT_MEM_QALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
290
#define FT_MEM_QRENEW_ARRAY( _pointer_, _old_, _new_ ) \
291
FT_MEM_QREALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ), \
292
(_new_) * sizeof ( *(_pointer_) ) )
295
/*************************************************************************/
297
/* the following macros are obsolete but kept for compatibility reasons */
300
#define FT_MEM_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
301
FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( _type_ ) )
303
#define FT_MEM_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
304
FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( _type ), \
305
(_new_) * sizeof ( _type_ ) )
308
/*************************************************************************/
310
/* The following macros are variants of their FT_MEM_XXXX equivalents; */
311
/* they are used to set an _implicit_ `error' variable and return TRUE */
312
/* if an error occured (i.e. if 'error != 0'). */
315
#define FT_ALLOC( _pointer_, _size_ ) \
316
FT_SET_ERROR( FT_MEM_ALLOC( _pointer_, _size_ ) )
318
#define FT_REALLOC( _pointer_, _current_, _size_ ) \
319
FT_SET_ERROR( FT_MEM_REALLOC( _pointer_, _current_, _size_ ) )
321
#define FT_FREE( _pointer_ ) \
322
FT_MEM_FREE( _pointer_ )
324
#define FT_QALLOC( _pointer_, _size_ ) \
325
FT_SET_ERROR( FT_MEM_QALLOC( _pointer_, _size_ ) )
327
#define FT_QREALLOC( _pointer_, _current_, _size_ ) \
328
FT_SET_ERROR( FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) )
331
#define FT_NEW( _pointer_ ) \
332
FT_SET_ERROR( FT_MEM_NEW( _pointer_ ) )
334
#define FT_NEW_ARRAY( _pointer_, _count_ ) \
335
FT_SET_ERROR( FT_MEM_NEW_ARRAY( _pointer_, _count_ ) )
337
#define FT_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
338
FT_SET_ERROR( FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) )
340
#define FT_QNEW( _pointer_ ) \
341
FT_SET_ERROR( FT_MEM_QNEW( _pointer_ ) )
343
#define FT_QNEW_ARRAY( _pointer_, _count_ ) \
344
FT_SET_ERROR( FT_MEM_QNEW_ARRAY( _pointer_, _count_ ) )
346
#define FT_QRENEW_ARRAY( _pointer_, _old_, _new_ ) \
347
FT_SET_ERROR( FT_MEM_QRENEW_ARRAY( _pointer_, _old_, _new_ ) )
350
#define FT_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
351
FT_SET_ERROR( FT_MEM_ALLOC( _pointer_, \
352
(_count_) * sizeof ( _type_ ) ) )
354
#define FT_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
355
FT_SET_ERROR( FT_MEM_REALLOC( _pointer_, \
356
(_old_) * sizeof ( _type_ ), \
357
(_new_) * sizeof ( _type_ ) ) )
364
#endif /* __FTMEMORY_H__ */