vrshoot

view libs/ft2static/freetype/internal/ftmemory.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /***************************************************************************/
2 /* */
3 /* ftmemory.h */
4 /* */
5 /* The FreeType memory management macros (specification). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2004, 2005, 2006, 2007, 2010 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 /***************************************************************************/
19 #ifndef __FTMEMORY_H__
20 #define __FTMEMORY_H__
23 #include <ft2build.h>
24 #include FT_CONFIG_CONFIG_H
25 #include FT_TYPES_H
28 FT_BEGIN_HEADER
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 )
47 /*************************************************************************/
48 /*************************************************************************/
49 /*************************************************************************/
50 /**** ****/
51 /**** ****/
52 /**** M E M O R Y ****/
53 /**** ****/
54 /**** ****/
55 /*************************************************************************/
56 /*************************************************************************/
57 /*************************************************************************/
60 /*
61 * C++ refuses to handle statements like p = (void*)anything, with `p' a
62 * typed pointer. Since we don't have a `typeof' operator in standard
63 * C++, we have to use a template to emulate it.
64 */
66 #ifdef __cplusplus
68 extern "C++"
69 template <typename T> inline T*
70 cplusplus_typeof( T*,
71 void *v )
72 {
73 return static_cast <T*> ( v );
74 }
76 #define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
78 #else
80 #define FT_ASSIGNP( p, val ) (p) = (val)
82 #endif
86 #ifdef FT_DEBUG_MEMORY
88 FT_BASE( const char* ) _ft_debug_file;
89 FT_BASE( long ) _ft_debug_lineno;
91 #define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
92 _ft_debug_lineno = __LINE__, \
93 (exp) )
95 #define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
96 _ft_debug_lineno = __LINE__, \
97 FT_ASSIGNP( p, exp ) )
99 #else /* !FT_DEBUG_MEMORY */
101 #define FT_DEBUG_INNER( exp ) (exp)
102 #define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
104 #endif /* !FT_DEBUG_MEMORY */
107 /*
108 * The allocation functions return a pointer, and the error code
109 * is written to through the `p_error' parameter. See below for
110 * for documentation.
111 */
113 FT_BASE( FT_Pointer )
114 ft_mem_alloc( FT_Memory memory,
115 FT_Long size,
116 FT_Error *p_error );
118 FT_BASE( FT_Pointer )
119 ft_mem_qalloc( FT_Memory memory,
120 FT_Long size,
121 FT_Error *p_error );
123 FT_BASE( FT_Pointer )
124 ft_mem_realloc( FT_Memory memory,
125 FT_Long item_size,
126 FT_Long cur_count,
127 FT_Long new_count,
128 void* block,
129 FT_Error *p_error );
131 FT_BASE( FT_Pointer )
132 ft_mem_qrealloc( FT_Memory memory,
133 FT_Long item_size,
134 FT_Long cur_count,
135 FT_Long new_count,
136 void* block,
137 FT_Error *p_error );
139 FT_BASE( void )
140 ft_mem_free( FT_Memory memory,
141 const void* P );
144 #define FT_MEM_ALLOC( ptr, size ) \
145 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, (size), &error ) )
147 #define FT_MEM_FREE( ptr ) \
148 FT_BEGIN_STMNT \
149 ft_mem_free( memory, (ptr) ); \
150 (ptr) = NULL; \
151 FT_END_STMNT
153 #define FT_MEM_NEW( ptr ) \
154 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
156 #define FT_MEM_REALLOC( ptr, cursz, newsz ) \
157 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, 1, \
158 (cursz), (newsz), \
159 (ptr), &error ) )
161 #define FT_MEM_QALLOC( ptr, size ) \
162 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, (size), &error ) )
164 #define FT_MEM_QNEW( ptr ) \
165 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
167 #define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
168 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, 1, \
169 (cursz), (newsz), \
170 (ptr), &error ) )
172 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
173 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
174 (cursz), (newsz), \
175 (ptr), &error ) )
177 #define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
178 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (item_size), \
179 0, (count), \
180 NULL, &error ) )
182 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
183 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, (itmsz), \
184 (oldcnt), (newcnt), \
185 (ptr), &error ) )
187 #define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
188 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (item_size), \
189 0, (count), \
190 NULL, &error ) )
192 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz) \
193 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, (itmsz), \
194 (oldcnt), (newcnt), \
195 (ptr), &error ) )
198 #define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
201 #define FT_MEM_SET( dest, byte, count ) ft_memset( dest, byte, count )
203 #define FT_MEM_COPY( dest, source, count ) ft_memcpy( dest, source, count )
205 #define FT_MEM_MOVE( dest, source, count ) ft_memmove( dest, source, count )
208 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
210 #define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
213 #define FT_ARRAY_ZERO( dest, count ) \
214 FT_MEM_ZERO( dest, (count) * sizeof ( *(dest) ) )
216 #define FT_ARRAY_COPY( dest, source, count ) \
217 FT_MEM_COPY( dest, source, (count) * sizeof ( *(dest) ) )
219 #define FT_ARRAY_MOVE( dest, source, count ) \
220 FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
223 /*
224 * Return the maximum number of addressable elements in an array.
225 * We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
226 * any problems.
227 */
228 #define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
230 #define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
233 /*************************************************************************/
234 /* */
235 /* The following functions macros expect that their pointer argument is */
236 /* _typed_ in order to automatically compute array element sizes. */
237 /* */
239 #define FT_MEM_NEW_ARRAY( ptr, count ) \
240 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
241 0, (count), \
242 NULL, &error ) )
244 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
245 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, sizeof ( *(ptr) ), \
246 (cursz), (newsz), \
247 (ptr), &error ) )
249 #define FT_MEM_QNEW_ARRAY( ptr, count ) \
250 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
251 0, (count), \
252 NULL, &error ) )
254 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
255 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, sizeof ( *(ptr) ), \
256 (cursz), (newsz), \
257 (ptr), &error ) )
260 #define FT_ALLOC( ptr, size ) \
261 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
263 #define FT_REALLOC( ptr, cursz, newsz ) \
264 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
266 #define FT_ALLOC_MULT( ptr, count, item_size ) \
267 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
269 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
270 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
271 newcnt, itmsz ) )
273 #define FT_QALLOC( ptr, size ) \
274 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
276 #define FT_QREALLOC( ptr, cursz, newsz ) \
277 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
279 #define FT_QALLOC_MULT( ptr, count, item_size ) \
280 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
282 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
283 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
284 newcnt, itmsz ) )
286 #define FT_FREE( ptr ) FT_MEM_FREE( ptr )
288 #define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
290 #define FT_NEW_ARRAY( ptr, count ) \
291 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
293 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
294 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
296 #define FT_QNEW( ptr ) \
297 FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
299 #define FT_QNEW_ARRAY( ptr, count ) \
300 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
302 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
303 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
306 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
308 FT_BASE( FT_Error )
309 FT_Alloc( FT_Memory memory,
310 FT_Long size,
311 void* *P );
313 FT_BASE( FT_Error )
314 FT_QAlloc( FT_Memory memory,
315 FT_Long size,
316 void* *p );
318 FT_BASE( FT_Error )
319 FT_Realloc( FT_Memory memory,
320 FT_Long current,
321 FT_Long size,
322 void* *P );
324 FT_BASE( FT_Error )
325 FT_QRealloc( FT_Memory memory,
326 FT_Long current,
327 FT_Long size,
328 void* *p );
330 FT_BASE( void )
331 FT_Free( FT_Memory memory,
332 void* *P );
334 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
337 FT_BASE( FT_Pointer )
338 ft_mem_strdup( FT_Memory memory,
339 const char* str,
340 FT_Error *p_error );
342 FT_BASE( FT_Pointer )
343 ft_mem_dup( FT_Memory memory,
344 const void* address,
345 FT_ULong size,
346 FT_Error *p_error );
348 #define FT_MEM_STRDUP( dst, str ) \
349 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
351 #define FT_STRDUP( dst, str ) \
352 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
354 #define FT_MEM_DUP( dst, address, size ) \
355 (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
357 #define FT_DUP( dst, address, size ) \
358 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
361 /* Return >= 1 if a truncation occurs. */
362 /* Return 0 if the source string fits the buffer. */
363 /* This is *not* the same as strlcpy(). */
364 FT_BASE( FT_Int )
365 ft_mem_strcpyn( char* dst,
366 const char* src,
367 FT_ULong size );
369 #define FT_STRCPYN( dst, src, size ) \
370 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
372 /* */
375 FT_END_HEADER
377 #endif /* __FTMEMORY_H__ */
380 /* END */