1 | /***************************************************************************/
|
---|
2 | /* */
|
---|
3 | /* ftmemory.h */
|
---|
4 | /* */
|
---|
5 | /* The FreeType memory management macros (specification). */
|
---|
6 | /* */
|
---|
7 | /* Copyright 1996-2001, 2002, 2004, 2005 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 |
|
---|
28 | FT_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 | #ifdef FT_DEBUG_MEMORY
|
---|
60 |
|
---|
61 | FT_BASE( FT_Error )
|
---|
62 | FT_Alloc_Debug( FT_Memory memory,
|
---|
63 | FT_Long size,
|
---|
64 | void* *P,
|
---|
65 | const char* file_name,
|
---|
66 | FT_Long line_no );
|
---|
67 |
|
---|
68 | FT_BASE( FT_Error )
|
---|
69 | FT_QAlloc_Debug( FT_Memory memory,
|
---|
70 | FT_Long size,
|
---|
71 | void* *P,
|
---|
72 | const char* file_name,
|
---|
73 | FT_Long line_no );
|
---|
74 |
|
---|
75 | FT_BASE( FT_Error )
|
---|
76 | FT_Realloc_Debug( FT_Memory memory,
|
---|
77 | FT_Long current,
|
---|
78 | FT_Long size,
|
---|
79 | void* *P,
|
---|
80 | const char* file_name,
|
---|
81 | FT_Long line_no );
|
---|
82 |
|
---|
83 | FT_BASE( FT_Error )
|
---|
84 | FT_QRealloc_Debug( FT_Memory memory,
|
---|
85 | FT_Long current,
|
---|
86 | FT_Long size,
|
---|
87 | void* *P,
|
---|
88 | const char* file_name,
|
---|
89 | FT_Long line_no );
|
---|
90 |
|
---|
91 | FT_BASE( void )
|
---|
92 | FT_Free_Debug( FT_Memory memory,
|
---|
93 | FT_Pointer block,
|
---|
94 | const char* file_name,
|
---|
95 | FT_Long line_no );
|
---|
96 |
|
---|
97 | #endif
|
---|
98 |
|
---|
99 |
|
---|
100 | /*************************************************************************/
|
---|
101 | /* */
|
---|
102 | /* <Function> */
|
---|
103 | /* FT_Alloc */
|
---|
104 | /* */
|
---|
105 | /* <Description> */
|
---|
106 | /* Allocates a new block of memory. The returned area is always */
|
---|
107 | /* zero-filled; this is a strong convention in many FreeType parts. */
|
---|
108 | /* */
|
---|
109 | /* <Input> */
|
---|
110 | /* memory :: A handle to a given `memory object' which handles */
|
---|
111 | /* allocation. */
|
---|
112 | /* */
|
---|
113 | /* size :: The size in bytes of the block to allocate. */
|
---|
114 | /* */
|
---|
115 | /* <Output> */
|
---|
116 | /* P :: A pointer to the fresh new block. It should be set to */
|
---|
117 | /* NULL if `size' is 0, or in case of error. */
|
---|
118 | /* */
|
---|
119 | /* <Return> */
|
---|
120 | /* FreeType error code. 0 means success. */
|
---|
121 | /* */
|
---|
122 | FT_BASE( FT_Error )
|
---|
123 | FT_Alloc( FT_Memory memory,
|
---|
124 | FT_Long size,
|
---|
125 | void* *P );
|
---|
126 |
|
---|
127 |
|
---|
128 | /*************************************************************************/
|
---|
129 | /* */
|
---|
130 | /* <Function> */
|
---|
131 | /* FT_QAlloc */
|
---|
132 | /* */
|
---|
133 | /* <Description> */
|
---|
134 | /* Allocates a new block of memory. The returned area is *not* */
|
---|
135 | /* zero-filled, making allocation quicker. */
|
---|
136 | /* */
|
---|
137 | /* <Input> */
|
---|
138 | /* memory :: A handle to a given `memory object' which handles */
|
---|
139 | /* allocation. */
|
---|
140 | /* */
|
---|
141 | /* size :: The size in bytes of the block to allocate. */
|
---|
142 | /* */
|
---|
143 | /* <Output> */
|
---|
144 | /* P :: A pointer to the fresh new block. It should be set to */
|
---|
145 | /* NULL if `size' is 0, or in case of error. */
|
---|
146 | /* */
|
---|
147 | /* <Return> */
|
---|
148 | /* FreeType error code. 0 means success. */
|
---|
149 | /* */
|
---|
150 | FT_BASE( FT_Error )
|
---|
151 | FT_QAlloc( FT_Memory memory,
|
---|
152 | FT_Long size,
|
---|
153 | void* *p );
|
---|
154 |
|
---|
155 |
|
---|
156 | /*************************************************************************/
|
---|
157 | /* */
|
---|
158 | /* <Function> */
|
---|
159 | /* FT_Realloc */
|
---|
160 | /* */
|
---|
161 | /* <Description> */
|
---|
162 | /* Reallocates a block of memory pointed to by `*P' to `Size' bytes */
|
---|
163 | /* from the heap, possibly changing `*P'. The returned area is */
|
---|
164 | /* zero-filled. */
|
---|
165 | /* */
|
---|
166 | /* <Input> */
|
---|
167 | /* memory :: A handle to a given `memory object' which handles */
|
---|
168 | /* reallocation. */
|
---|
169 | /* */
|
---|
170 | /* current :: The current block size in bytes. */
|
---|
171 | /* */
|
---|
172 | /* size :: The new block size in bytes. */
|
---|
173 | /* */
|
---|
174 | /* <InOut> */
|
---|
175 | /* P :: A pointer to the fresh new block. It should be set to */
|
---|
176 | /* NULL if `size' is 0, or in case of error. */
|
---|
177 | /* */
|
---|
178 | /* <Return> */
|
---|
179 | /* FreeType error code. 0 means success. */
|
---|
180 | /* */
|
---|
181 | /* <Note> */
|
---|
182 | /* All callers of FT_Realloc() _must_ provide the current block size */
|
---|
183 | /* as well as the new one. */
|
---|
184 | /* */
|
---|
185 | FT_BASE( FT_Error )
|
---|
186 | FT_Realloc( FT_Memory memory,
|
---|
187 | FT_Long current,
|
---|
188 | FT_Long size,
|
---|
189 | void* *P );
|
---|
190 |
|
---|
191 |
|
---|
192 | /*************************************************************************/
|
---|
193 | /* */
|
---|
194 | /* <Function> */
|
---|
195 | /* FT_QRealloc */
|
---|
196 | /* */
|
---|
197 | /* <Description> */
|
---|
198 | /* Reallocates a block of memory pointed to by `*P' to `Size' bytes */
|
---|
199 | /* from the heap, possibly changing `*P'. The returned area is *not* */
|
---|
200 | /* zero-filled, making reallocation quicker. */
|
---|
201 | /* */
|
---|
202 | /* <Input> */
|
---|
203 | /* memory :: A handle to a given `memory object' which handles */
|
---|
204 | /* reallocation. */
|
---|
205 | /* */
|
---|
206 | /* current :: The current block size in bytes. */
|
---|
207 | /* */
|
---|
208 | /* size :: The new block size in bytes. */
|
---|
209 | /* */
|
---|
210 | /* <InOut> */
|
---|
211 | /* P :: A pointer to the fresh new block. It should be set to */
|
---|
212 | /* NULL if `size' is 0, or in case of error. */
|
---|
213 | /* */
|
---|
214 | /* <Return> */
|
---|
215 | /* FreeType error code. 0 means success. */
|
---|
216 | /* */
|
---|
217 | /* <Note> */
|
---|
218 | /* All callers of FT_Realloc() _must_ provide the current block size */
|
---|
219 | /* as well as the new one. */
|
---|
220 | /* */
|
---|
221 | FT_BASE( FT_Error )
|
---|
222 | FT_QRealloc( FT_Memory memory,
|
---|
223 | FT_Long current,
|
---|
224 | FT_Long size,
|
---|
225 | void* *p );
|
---|
226 |
|
---|
227 |
|
---|
228 | /*************************************************************************/
|
---|
229 | /* */
|
---|
230 | /* <Function> */
|
---|
231 | /* FT_Free */
|
---|
232 | /* */
|
---|
233 | /* <Description> */
|
---|
234 | /* Releases a given block of memory allocated through FT_Alloc(). */
|
---|
235 | /* */
|
---|
236 | /* <Input> */
|
---|
237 | /* memory :: A handle to a given `memory object' which handles */
|
---|
238 | /* memory deallocation */
|
---|
239 | /* */
|
---|
240 | /* P :: This is the _address_ of a _pointer_ which points to the */
|
---|
241 | /* allocated block. It is always set to NULL on exit. */
|
---|
242 | /* */
|
---|
243 | /* <Note> */
|
---|
244 | /* If P or *P is NULL, this function should return successfully. */
|
---|
245 | /* This is a strong convention within all of FreeType and its */
|
---|
246 | /* drivers. */
|
---|
247 | /* */
|
---|
248 | FT_BASE( void )
|
---|
249 | FT_Free( FT_Memory memory,
|
---|
250 | void* *P );
|
---|
251 |
|
---|
252 |
|
---|
253 | #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
|
---|
254 |
|
---|
255 | #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
|
---|
256 |
|
---|
257 | #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
|
---|
258 |
|
---|
259 |
|
---|
260 | #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
|
---|
261 |
|
---|
262 | #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
|
---|
263 |
|
---|
264 | #define FT_ARRAY_ZERO( dest, count ) \
|
---|
265 | FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
|
---|
266 |
|
---|
267 | #define FT_ARRAY_COPY( dest, source, count ) \
|
---|
268 | FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
|
---|
269 |
|
---|
270 | #define FT_ARRAY_MOVE( dest, source, count ) \
|
---|
271 | FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
|
---|
272 |
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Return the maximum number of adressable elements in an array.
|
---|
276 | * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
|
---|
277 | * any problems.
|
---|
278 | */
|
---|
279 | #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
|
---|
280 |
|
---|
281 | #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
|
---|
282 |
|
---|
283 |
|
---|
284 | /*************************************************************************/
|
---|
285 | /* */
|
---|
286 | /* We first define FT_MEM_ALLOC, FT_MEM_REALLOC, and FT_MEM_FREE. All */
|
---|
287 | /* macros use an _implicit_ `memory' parameter to access the current */
|
---|
288 | /* memory allocator. */
|
---|
289 | /* */
|
---|
290 |
|
---|
291 | #ifdef FT_DEBUG_MEMORY
|
---|
292 |
|
---|
293 | #define FT_MEM_ALLOC( _pointer_, _size_ ) \
|
---|
294 | FT_Alloc_Debug( memory, _size_, \
|
---|
295 | (void**)(void*)&(_pointer_), \
|
---|
296 | __FILE__, __LINE__ )
|
---|
297 |
|
---|
298 | #define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
|
---|
299 | FT_Realloc_Debug( memory, _current_, _size_, \
|
---|
300 | (void**)(void*)&(_pointer_), \
|
---|
301 | __FILE__, __LINE__ )
|
---|
302 |
|
---|
303 | #define FT_MEM_QALLOC( _pointer_, _size_ ) \
|
---|
304 | FT_QAlloc_Debug( memory, _size_, \
|
---|
305 | (void**)(void*)&(_pointer_), \
|
---|
306 | __FILE__, __LINE__ )
|
---|
307 |
|
---|
308 | #define FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) \
|
---|
309 | FT_QRealloc_Debug( memory, _current_, _size_, \
|
---|
310 | (void**)(void*)&(_pointer_), \
|
---|
311 | __FILE__, __LINE__ )
|
---|
312 |
|
---|
313 | #define FT_MEM_FREE( _pointer_ ) \
|
---|
314 | FT_Free_Debug( memory, (void**)(void*)&(_pointer_), \
|
---|
315 | __FILE__, __LINE__ )
|
---|
316 |
|
---|
317 |
|
---|
318 | #else /* !FT_DEBUG_MEMORY */
|
---|
319 |
|
---|
320 |
|
---|
321 | #define FT_MEM_ALLOC( _pointer_, _size_ ) \
|
---|
322 | FT_Alloc( memory, _size_, \
|
---|
323 | (void**)(void*)&(_pointer_) )
|
---|
324 |
|
---|
325 | #define FT_MEM_FREE( _pointer_ ) \
|
---|
326 | FT_Free( memory, \
|
---|
327 | (void**)(void*)&(_pointer_) )
|
---|
328 |
|
---|
329 | #define FT_MEM_REALLOC( _pointer_, _current_, _size_ ) \
|
---|
330 | FT_Realloc( memory, _current_, _size_, \
|
---|
331 | (void**)(void*)&(_pointer_) )
|
---|
332 |
|
---|
333 | #define FT_MEM_QALLOC( _pointer_, _size_ ) \
|
---|
334 | FT_QAlloc( memory, _size_, \
|
---|
335 | (void**)(void*)&(_pointer_) )
|
---|
336 |
|
---|
337 | #define FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) \
|
---|
338 | FT_QRealloc( memory, _current_, _size_, \
|
---|
339 | (void**)(void*)&(_pointer_) )
|
---|
340 |
|
---|
341 | #endif /* !FT_DEBUG_MEMORY */
|
---|
342 |
|
---|
343 |
|
---|
344 | /*************************************************************************/
|
---|
345 | /* */
|
---|
346 | /* The following functions macros expect that their pointer argument is */
|
---|
347 | /* _typed_ in order to automatically compute array element sizes. */
|
---|
348 | /* */
|
---|
349 |
|
---|
350 | #define FT_MEM_NEW( _pointer_ ) \
|
---|
351 | FT_MEM_ALLOC( _pointer_, sizeof ( *(_pointer_) ) )
|
---|
352 |
|
---|
353 | #define FT_MEM_NEW_ARRAY( _pointer_, _count_ ) \
|
---|
354 | FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
|
---|
355 |
|
---|
356 | #define FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
|
---|
357 | FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ), \
|
---|
358 | (_new_) * sizeof ( *(_pointer_) ) )
|
---|
359 |
|
---|
360 | #define FT_MEM_QNEW( _pointer_ ) \
|
---|
361 | FT_MEM_QALLOC( _pointer_, sizeof ( *(_pointer_) ) )
|
---|
362 |
|
---|
363 | #define FT_MEM_QNEW_ARRAY( _pointer_, _count_ ) \
|
---|
364 | FT_MEM_QALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
|
---|
365 |
|
---|
366 | #define FT_MEM_QRENEW_ARRAY( _pointer_, _old_, _new_ ) \
|
---|
367 | FT_MEM_QREALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ), \
|
---|
368 | (_new_) * sizeof ( *(_pointer_) ) )
|
---|
369 |
|
---|
370 |
|
---|
371 | /*************************************************************************/
|
---|
372 | /* */
|
---|
373 | /* the following macros are obsolete but kept for compatibility reasons */
|
---|
374 | /* */
|
---|
375 |
|
---|
376 | #define FT_MEM_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
|
---|
377 | FT_MEM_ALLOC( _pointer_, (_count_) * sizeof ( _type_ ) )
|
---|
378 |
|
---|
379 | #define FT_MEM_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
|
---|
380 | FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( _type ), \
|
---|
381 | (_new_) * sizeof ( _type_ ) )
|
---|
382 |
|
---|
383 |
|
---|
384 | /*************************************************************************/
|
---|
385 | /* */
|
---|
386 | /* The following macros are variants of their FT_MEM_XXXX equivalents; */
|
---|
387 | /* they are used to set an _implicit_ `error' variable and return TRUE */
|
---|
388 | /* if an error occured (i.e. if 'error != 0'). */
|
---|
389 | /* */
|
---|
390 |
|
---|
391 | #define FT_ALLOC( _pointer_, _size_ ) \
|
---|
392 | FT_SET_ERROR( FT_MEM_ALLOC( _pointer_, _size_ ) )
|
---|
393 |
|
---|
394 | #define FT_REALLOC( _pointer_, _current_, _size_ ) \
|
---|
395 | FT_SET_ERROR( FT_MEM_REALLOC( _pointer_, _current_, _size_ ) )
|
---|
396 |
|
---|
397 | #define FT_QALLOC( _pointer_, _size_ ) \
|
---|
398 | FT_SET_ERROR( FT_MEM_QALLOC( _pointer_, _size_ ) )
|
---|
399 |
|
---|
400 | #define FT_QREALLOC( _pointer_, _current_, _size_ ) \
|
---|
401 | FT_SET_ERROR( FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) )
|
---|
402 |
|
---|
403 |
|
---|
404 | #define FT_FREE( _pointer_ ) \
|
---|
405 | FT_MEM_FREE( _pointer_ )
|
---|
406 |
|
---|
407 |
|
---|
408 | #define FT_NEW( _pointer_ ) \
|
---|
409 | FT_ALLOC( _pointer_, sizeof ( *(_pointer_) ) )
|
---|
410 |
|
---|
411 | #define FT_NEW_ARRAY( _pointer_, _count_ ) \
|
---|
412 | FT_ALLOC( _pointer_, sizeof ( *(_pointer_) ) * (_count_) )
|
---|
413 |
|
---|
414 | #define FT_RENEW_ARRAY( _pointer_, _old_, _new_ ) \
|
---|
415 | FT_REALLOC( _pointer_, sizeof ( *(_pointer_) ) * (_old_), \
|
---|
416 | sizeof ( *(_pointer_) ) * (_new_) )
|
---|
417 |
|
---|
418 | #define FT_QNEW( _pointer_ ) \
|
---|
419 | FT_QALLOC( _pointer_, sizeof ( *(_pointer_) ) )
|
---|
420 |
|
---|
421 | #define FT_QNEW_ARRAY( _pointer_, _count_ ) \
|
---|
422 | FT_QALLOC( _pointer_, sizeof ( *(_pointer_) ) * (_count_) )
|
---|
423 |
|
---|
424 | #define FT_QRENEW_ARRAY( _pointer_, _old_, _new_ ) \
|
---|
425 | FT_QREALLOC( _pointer_, sizeof ( *(_pointer_) ) * (_old_), \
|
---|
426 | sizeof ( *(_pointer_) ) * (_new_) )
|
---|
427 |
|
---|
428 |
|
---|
429 | #define FT_ALLOC_ARRAY( _pointer_, _count_, _type_ ) \
|
---|
430 | FT_ALLOC( _pointer_, (_count_) * sizeof ( _type_ ) )
|
---|
431 |
|
---|
432 | #define FT_REALLOC_ARRAY( _pointer_, _old_, _new_, _type_ ) \
|
---|
433 | FT_REALLOC( _pointer_, (_old_) * sizeof ( _type_ ), \
|
---|
434 | (_new_) * sizeof ( _type_ ) )
|
---|
435 |
|
---|
436 |
|
---|
437 | /* */
|
---|
438 |
|
---|
439 |
|
---|
440 | FT_END_HEADER
|
---|
441 |
|
---|
442 | #endif /* __FTMEMORY_H__ */
|
---|
443 |
|
---|
444 |
|
---|
445 | /* END */
|
---|