Imported Upstream version 2.5.5
[platform/upstream/freetype2.git] / include / freetype / internal / ftmemory.h
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftmemory.h                                                             */
4 /*                                                                         */
5 /*    The FreeType memory management macros (specification).               */
6 /*                                                                         */
7 /*  Copyright 1996-2016 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
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    */
65
66 #ifdef __cplusplus
67
68 extern "C++"
69 {
70   template <typename T> inline T*
71   cplusplus_typeof(        T*,
72                     void  *v )
73   {
74     return static_cast <T*> ( v );
75   }
76 }
77
78 #define FT_ASSIGNP( p, val )  (p) = cplusplus_typeof( (p), (val) )
79
80 #else
81
82 #define FT_ASSIGNP( p, val )  (p) = (val)
83
84 #endif
85
86
87
88 #ifdef FT_DEBUG_MEMORY
89
90   FT_BASE( const char* )  _ft_debug_file;
91   FT_BASE( long )         _ft_debug_lineno;
92
93 #define FT_DEBUG_INNER( exp )  ( _ft_debug_file   = __FILE__, \
94                                  _ft_debug_lineno = __LINE__, \
95                                  (exp) )
96
97 #define FT_ASSIGNP_INNER( p, exp )  ( _ft_debug_file   = __FILE__, \
98                                       _ft_debug_lineno = __LINE__, \
99                                       FT_ASSIGNP( p, exp ) )
100
101 #else /* !FT_DEBUG_MEMORY */
102
103 #define FT_DEBUG_INNER( exp )       (exp)
104 #define FT_ASSIGNP_INNER( p, exp )  FT_ASSIGNP( p, exp )
105
106 #endif /* !FT_DEBUG_MEMORY */
107
108
109   /*
110    *  The allocation functions return a pointer, and the error code
111    *  is written to through the `p_error' parameter.  See below for
112    *  for documentation.
113    */
114
115   FT_BASE( FT_Pointer )
116   ft_mem_alloc( FT_Memory  memory,
117                 FT_Long    size,
118                 FT_Error  *p_error );
119
120   FT_BASE( FT_Pointer )
121   ft_mem_qalloc( FT_Memory  memory,
122                  FT_Long    size,
123                  FT_Error  *p_error );
124
125   FT_BASE( FT_Pointer )
126   ft_mem_realloc( FT_Memory  memory,
127                   FT_Long    item_size,
128                   FT_Long    cur_count,
129                   FT_Long    new_count,
130                   void*      block,
131                   FT_Error  *p_error );
132
133   FT_BASE( FT_Pointer )
134   ft_mem_qrealloc( FT_Memory  memory,
135                    FT_Long    item_size,
136                    FT_Long    cur_count,
137                    FT_Long    new_count,
138                    void*      block,
139                    FT_Error  *p_error );
140
141   FT_BASE( void )
142   ft_mem_free( FT_Memory    memory,
143                const void*  P );
144
145
146 #define FT_MEM_ALLOC( ptr, size )                               \
147           FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory,          \
148                                                (FT_Long)(size), \
149                                                &error ) )
150
151 #define FT_MEM_FREE( ptr )                \
152           FT_BEGIN_STMNT                  \
153             ft_mem_free( memory, (ptr) ); \
154             (ptr) = NULL;                 \
155           FT_END_STMNT
156
157 #define FT_MEM_NEW( ptr )                        \
158           FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
159
160 #define FT_MEM_REALLOC( ptr, cursz, newsz )                        \
161           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory,           \
162                                                  1,                \
163                                                  (FT_Long)(cursz), \
164                                                  (FT_Long)(newsz), \
165                                                  (ptr),            \
166                                                  &error ) )
167
168 #define FT_MEM_QALLOC( ptr, size )                               \
169           FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory,          \
170                                                 (FT_Long)(size), \
171                                                 &error ) )
172
173 #define FT_MEM_QNEW( ptr )                        \
174           FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
175
176 #define FT_MEM_QREALLOC( ptr, cursz, newsz )                        \
177           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory,           \
178                                                   1,                \
179                                                   (FT_Long)(cursz), \
180                                                   (FT_Long)(newsz), \
181                                                   (ptr),            \
182                                                   &error ) )
183
184 #define FT_MEM_ALLOC_MULT( ptr, count, item_size )                     \
185           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory,               \
186                                                  (FT_Long)(item_size), \
187                                                  0,                    \
188                                                  (FT_Long)(count),     \
189                                                  NULL,                 \
190                                                  &error ) )
191
192 #define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz )           \
193           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory,            \
194                                                  (FT_Long)(itmsz),  \
195                                                  (FT_Long)(oldcnt), \
196                                                  (FT_Long)(newcnt), \
197                                                  (ptr),             \
198                                                  &error ) )
199
200 #define FT_MEM_QALLOC_MULT( ptr, count, item_size )                     \
201           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory,               \
202                                                   (FT_Long)(item_size), \
203                                                   0,                    \
204                                                   (FT_Long)(count),     \
205                                                   NULL,                 \
206                                                   &error ) )
207
208 #define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz)            \
209           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory,            \
210                                                   (FT_Long)(itmsz),  \
211                                                   (FT_Long)(oldcnt), \
212                                                   (FT_Long)(newcnt), \
213                                                   (ptr),             \
214                                                   &error ) )
215
216
217 #define FT_MEM_SET_ERROR( cond )  ( (cond), error != 0 )
218
219
220 #define FT_MEM_SET( dest, byte, count )               \
221           ft_memset( dest, byte, (FT_Offset)(count) )
222
223 #define FT_MEM_COPY( dest, source, count )              \
224           ft_memcpy( dest, source, (FT_Offset)(count) )
225
226 #define FT_MEM_MOVE( dest, source, count )               \
227           ft_memmove( dest, source, (FT_Offset)(count) )
228
229
230 #define FT_MEM_ZERO( dest, count )  FT_MEM_SET( dest, 0, count )
231
232 #define FT_ZERO( p )                FT_MEM_ZERO( p, sizeof ( *(p) ) )
233
234
235 #define FT_ARRAY_ZERO( dest, count )                             \
236           FT_MEM_ZERO( dest,                                     \
237                        (FT_Offset)(count) * sizeof ( *(dest) ) )
238
239 #define FT_ARRAY_COPY( dest, source, count )                     \
240           FT_MEM_COPY( dest,                                     \
241                        source,                                   \
242                        (FT_Offset)(count) * sizeof ( *(dest) ) )
243
244 #define FT_ARRAY_MOVE( dest, source, count )                     \
245           FT_MEM_MOVE( dest,                                     \
246                        source,                                   \
247                        (FT_Offset)(count) * sizeof ( *(dest) ) )
248
249
250   /*
251    *  Return the maximum number of addressable elements in an array.
252    *  We limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
253    *  any problems.
254    */
255 #define FT_ARRAY_MAX( ptr )           ( FT_INT_MAX / sizeof ( *(ptr) ) )
256
257 #define FT_ARRAY_CHECK( ptr, count )  ( (count) <= FT_ARRAY_MAX( ptr ) )
258
259
260   /*************************************************************************/
261   /*                                                                       */
262   /* The following functions macros expect that their pointer argument is  */
263   /* _typed_ in order to automatically compute array element sizes.        */
264   /*                                                                       */
265
266 #define FT_MEM_NEW_ARRAY( ptr, count )                              \
267           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory,            \
268                                                  sizeof ( *(ptr) ), \
269                                                  0,                 \
270                                                  (FT_Long)(count),  \
271                                                  NULL,              \
272                                                  &error ) )
273
274 #define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz )                     \
275           FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory,            \
276                                                  sizeof ( *(ptr) ), \
277                                                  (FT_Long)(cursz),  \
278                                                  (FT_Long)(newsz),  \
279                                                  (ptr),             \
280                                                  &error ) )
281
282 #define FT_MEM_QNEW_ARRAY( ptr, count )                              \
283           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory,            \
284                                                   sizeof ( *(ptr) ), \
285                                                   0,                 \
286                                                   (FT_Long)(count),  \
287                                                   NULL,              \
288                                                   &error ) )
289
290 #define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz )                     \
291           FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory,            \
292                                                   sizeof ( *(ptr) ), \
293                                                   (FT_Long)(cursz),  \
294                                                   (FT_Long)(newsz),  \
295                                                   (ptr),             \
296                                                   &error ) )
297
298 #define FT_ALLOC( ptr, size )                           \
299           FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
300
301 #define FT_REALLOC( ptr, cursz, newsz )                           \
302           FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
303
304 #define FT_ALLOC_MULT( ptr, count, item_size )                           \
305           FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
306
307 #define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz )              \
308           FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt,      \
309                                                  newcnt, itmsz ) )
310
311 #define FT_QALLOC( ptr, size )                           \
312           FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
313
314 #define FT_QREALLOC( ptr, cursz, newsz )                           \
315           FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
316
317 #define FT_QALLOC_MULT( ptr, count, item_size )                           \
318           FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
319
320 #define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz )              \
321           FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt,      \
322                                                   newcnt, itmsz ) )
323
324 #define FT_FREE( ptr )  FT_MEM_FREE( ptr )
325
326 #define FT_NEW( ptr )  FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
327
328 #define FT_NEW_ARRAY( ptr, count )                           \
329           FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
330
331 #define FT_RENEW_ARRAY( ptr, curcnt, newcnt )                           \
332           FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
333
334 #define FT_QNEW( ptr )                           \
335           FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
336
337 #define FT_QNEW_ARRAY( ptr, count )                          \
338           FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
339
340 #define FT_QRENEW_ARRAY( ptr, curcnt, newcnt )                          \
341           FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
342
343
344   FT_BASE( FT_Pointer )
345   ft_mem_strdup( FT_Memory    memory,
346                  const char*  str,
347                  FT_Error    *p_error );
348
349   FT_BASE( FT_Pointer )
350   ft_mem_dup( FT_Memory    memory,
351               const void*  address,
352               FT_ULong     size,
353               FT_Error    *p_error );
354
355
356 #define FT_MEM_STRDUP( dst, str )                                            \
357           (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
358
359 #define FT_STRDUP( dst, str )                           \
360           FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
361
362 #define FT_MEM_DUP( dst, address, size )                                    \
363           (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
364
365 #define FT_DUP( dst, address, size )                           \
366           FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
367
368
369   /* Return >= 1 if a truncation occurs.            */
370   /* Return 0 if the source string fits the buffer. */
371   /* This is *not* the same as strlcpy().           */
372   FT_BASE( FT_Int )
373   ft_mem_strcpyn( char*        dst,
374                   const char*  src,
375                   FT_ULong     size );
376
377 #define FT_STRCPYN( dst, src, size )                                         \
378           ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
379
380  /* */
381
382
383 FT_END_HEADER
384
385 #endif /* FTMEMORY_H_ */
386
387
388 /* END */