sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
ftmemory.h
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * ftmemory.h
4 *
5 * The FreeType memory management macros (specification).
6 *
7 * Copyright (C) 1996-2025 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
23#ifndef FTMEMORY_H_
24#define FTMEMORY_H_
25
26
27#include <ft2build.h>
28#include FT_CONFIG_CONFIG_H
29#include <freetype/fttypes.h>
30
31#include "compiler-macros.h"
32
33FT_BEGIN_HEADER
34
35
36 /**************************************************************************
37 *
38 * @macro:
39 * FT_SET_ERROR
40 *
41 * @description:
42 * This macro is used to set an implicit 'error' variable to a given
43 * expression's value (usually a function call), and convert it to a
44 * boolean which is set whenever the value is != 0.
45 */
46#undef FT_SET_ERROR
47#define FT_SET_ERROR( expression ) \
48 ( ( error = (expression) ) != 0 )
49
50
51
52 /*************************************************************************/
53 /*************************************************************************/
54 /*************************************************************************/
55 /**** ****/
56 /**** ****/
57 /**** M E M O R Y ****/
58 /**** ****/
59 /**** ****/
60 /*************************************************************************/
61 /*************************************************************************/
62 /*************************************************************************/
63
64
65 /* The calculation `NULL + n' is undefined in C. Even if the resulting */
66 /* pointer doesn't get dereferenced, this causes warnings with */
67 /* sanitizers. */
68 /* */
69 /* We thus provide a macro that should be used if `base' can be NULL. */
70#define FT_OFFSET( base, count ) ( (base) ? (base) + (count) : NULL )
71
72
73 /*
74 * C++ refuses to handle statements like p = (void*)anything, with `p' a
75 * typed pointer. Since we don't have a `typeof' operator in standard C++,
76 * we have to use a template to emulate it.
77 */
78
79#ifdef __cplusplus
80
81extern "C++"
82{
83 template <typename T> inline T*
84 cplusplus_typeof( T*,
85 void *v )
86 {
87 return static_cast <T*> ( v );
88 }
89}
90
91#define FT_ASSIGNP( p, val ) (p) = cplusplus_typeof( (p), (val) )
92
93#else
94
95#define FT_ASSIGNP( p, val ) (p) = (val)
96
97#endif
98
99
100
101#ifdef FT_DEBUG_MEMORY
102
103 FT_BASE( const char* ) ft_debug_file_;
104 FT_BASE( long ) ft_debug_lineno_;
105
106#define FT_DEBUG_INNER( exp ) ( ft_debug_file_ = __FILE__, \
107 ft_debug_lineno_ = __LINE__, \
108 (exp) )
109
110#define FT_ASSIGNP_INNER( p, exp ) ( ft_debug_file_ = __FILE__, \
111 ft_debug_lineno_ = __LINE__, \
112 FT_ASSIGNP( p, exp ) )
113
114#else /* !FT_DEBUG_MEMORY */
115
116#define FT_DEBUG_INNER( exp ) (exp)
117#define FT_ASSIGNP_INNER( p, exp ) FT_ASSIGNP( p, exp )
118
119#endif /* !FT_DEBUG_MEMORY */
120
121
122 /*
123 * The allocation functions return a pointer, and the error code is written
124 * to through the `p_error' parameter.
125 */
126
127 /* The `q' variants of the functions below (`q' for `quick') don't fill */
128 /* the allocated or reallocated memory with zero bytes. */
129
130 FT_BASE( FT_Pointer )
131 ft_mem_alloc( FT_Memory memory,
132 FT_Long size,
133 FT_Error *p_error );
134
135 FT_BASE( FT_Pointer )
136 ft_mem_qalloc( FT_Memory memory,
137 FT_Long size,
138 FT_Error *p_error );
139
140 FT_BASE( FT_Pointer )
141 ft_mem_realloc( FT_Memory memory,
142 FT_Long item_size,
143 FT_Long cur_count,
144 FT_Long new_count,
145 void* block,
146 FT_Error *p_error );
147
148 FT_BASE( FT_Pointer )
149 ft_mem_qrealloc( FT_Memory memory,
150 FT_Long item_size,
151 FT_Long cur_count,
152 FT_Long new_count,
153 void* block,
154 FT_Error *p_error );
155
156 FT_BASE( void )
157 ft_mem_free( FT_Memory memory,
158 const void* P );
159
160
161 /* The `Q' variants of the macros below (`Q' for `quick') don't fill */
162 /* the allocated or reallocated memory with zero bytes. */
163
164#define FT_MEM_ALLOC( ptr, size ) \
165 FT_ASSIGNP_INNER( ptr, ft_mem_alloc( memory, \
166 (FT_Long)(size), \
167 &error ) )
168
169#define FT_MEM_FREE( ptr ) \
170 FT_BEGIN_STMNT \
171 FT_DEBUG_INNER( ft_mem_free( memory, (ptr) ) ); \
172 (ptr) = NULL; \
173 FT_END_STMNT
174
175#define FT_MEM_NEW( ptr ) \
176 FT_MEM_ALLOC( ptr, sizeof ( *(ptr) ) )
177
178#define FT_MEM_REALLOC( ptr, cursz, newsz ) \
179 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
180 1, \
181 (FT_Long)(cursz), \
182 (FT_Long)(newsz), \
183 (ptr), \
184 &error ) )
185
186#define FT_MEM_QALLOC( ptr, size ) \
187 FT_ASSIGNP_INNER( ptr, ft_mem_qalloc( memory, \
188 (FT_Long)(size), \
189 &error ) )
190
191#define FT_MEM_QNEW( ptr ) \
192 FT_MEM_QALLOC( ptr, sizeof ( *(ptr) ) )
193
194#define FT_MEM_QREALLOC( ptr, cursz, newsz ) \
195 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
196 1, \
197 (FT_Long)(cursz), \
198 (FT_Long)(newsz), \
199 (ptr), \
200 &error ) )
201
202#define FT_MEM_ALLOC_MULT( ptr, count, item_size ) \
203 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
204 (FT_Long)(item_size), \
205 0, \
206 (FT_Long)(count), \
207 NULL, \
208 &error ) )
209
210#define FT_MEM_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
211 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
212 (FT_Long)(itmsz), \
213 (FT_Long)(oldcnt), \
214 (FT_Long)(newcnt), \
215 (ptr), \
216 &error ) )
217
218#define FT_MEM_QALLOC_MULT( ptr, count, item_size ) \
219 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
220 (FT_Long)(item_size), \
221 0, \
222 (FT_Long)(count), \
223 NULL, \
224 &error ) )
225
226#define FT_MEM_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
227 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
228 (FT_Long)(itmsz), \
229 (FT_Long)(oldcnt), \
230 (FT_Long)(newcnt), \
231 (ptr), \
232 &error ) )
233
234
235#define FT_MEM_SET_ERROR( cond ) ( (cond), error != 0 )
236
237
238#define FT_MEM_SET( dest, byte, count ) \
239 ft_memset( dest, byte, (FT_Offset)(count) )
240
241#define FT_MEM_COPY( dest, source, count ) \
242 ft_memcpy( dest, source, (FT_Offset)(count) )
243
244#define FT_MEM_MOVE( dest, source, count ) \
245 ft_memmove( dest, source, (FT_Offset)(count) )
246
247
248#define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
249
250#define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
251
252
253#define FT_ARRAY_ZERO( dest, count ) \
254 FT_MEM_ZERO( dest, \
255 (FT_Offset)(count) * sizeof ( *(dest) ) )
256
257#define FT_ARRAY_COPY( dest, source, count ) \
258 FT_MEM_COPY( dest, \
259 source, \
260 (FT_Offset)(count) * sizeof ( *(dest) ) )
261
262#define FT_ARRAY_MOVE( dest, source, count ) \
263 FT_MEM_MOVE( dest, \
264 source, \
265 (FT_Offset)(count) * sizeof ( *(dest) ) )
266
267
268 /*
269 * Return the maximum number of addressable elements in an array. We limit
270 * ourselves to INT_MAX, rather than UINT_MAX, to avoid any problems.
271 */
272#define FT_ARRAY_MAX( ptr ) ( FT_INT_MAX / sizeof ( *(ptr) ) )
273
274#define FT_ARRAY_CHECK( ptr, count ) ( (count) <= FT_ARRAY_MAX( ptr ) )
275
276
277 /**************************************************************************
278 *
279 * The following functions macros expect that their pointer argument is
280 * _typed_ in order to automatically compute array element sizes.
281 */
282
283#define FT_MEM_NEW_ARRAY( ptr, count ) \
284 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
285 sizeof ( *(ptr) ), \
286 0, \
287 (FT_Long)(count), \
288 NULL, \
289 &error ) )
290
291#define FT_MEM_RENEW_ARRAY( ptr, cursz, newsz ) \
292 FT_ASSIGNP_INNER( ptr, ft_mem_realloc( memory, \
293 sizeof ( *(ptr) ), \
294 (FT_Long)(cursz), \
295 (FT_Long)(newsz), \
296 (ptr), \
297 &error ) )
298
299#define FT_MEM_QNEW_ARRAY( ptr, count ) \
300 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
301 sizeof ( *(ptr) ), \
302 0, \
303 (FT_Long)(count), \
304 NULL, \
305 &error ) )
306
307#define FT_MEM_QRENEW_ARRAY( ptr, cursz, newsz ) \
308 FT_ASSIGNP_INNER( ptr, ft_mem_qrealloc( memory, \
309 sizeof ( *(ptr) ), \
310 (FT_Long)(cursz), \
311 (FT_Long)(newsz), \
312 (ptr), \
313 &error ) )
314
315#define FT_ALLOC( ptr, size ) \
316 FT_MEM_SET_ERROR( FT_MEM_ALLOC( ptr, size ) )
317
318#define FT_REALLOC( ptr, cursz, newsz ) \
319 FT_MEM_SET_ERROR( FT_MEM_REALLOC( ptr, cursz, newsz ) )
320
321#define FT_ALLOC_MULT( ptr, count, item_size ) \
322 FT_MEM_SET_ERROR( FT_MEM_ALLOC_MULT( ptr, count, item_size ) )
323
324#define FT_REALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
325 FT_MEM_SET_ERROR( FT_MEM_REALLOC_MULT( ptr, oldcnt, \
326 newcnt, itmsz ) )
327
328#define FT_QALLOC( ptr, size ) \
329 FT_MEM_SET_ERROR( FT_MEM_QALLOC( ptr, size ) )
330
331#define FT_QREALLOC( ptr, cursz, newsz ) \
332 FT_MEM_SET_ERROR( FT_MEM_QREALLOC( ptr, cursz, newsz ) )
333
334#define FT_QALLOC_MULT( ptr, count, item_size ) \
335 FT_MEM_SET_ERROR( FT_MEM_QALLOC_MULT( ptr, count, item_size ) )
336
337#define FT_QREALLOC_MULT( ptr, oldcnt, newcnt, itmsz ) \
338 FT_MEM_SET_ERROR( FT_MEM_QREALLOC_MULT( ptr, oldcnt, \
339 newcnt, itmsz ) )
340
341#define FT_FREE( ptr ) FT_MEM_FREE( ptr )
342
343#define FT_NEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_NEW( ptr ) )
344
345#define FT_NEW_ARRAY( ptr, count ) \
346 FT_MEM_SET_ERROR( FT_MEM_NEW_ARRAY( ptr, count ) )
347
348#define FT_RENEW_ARRAY( ptr, curcnt, newcnt ) \
349 FT_MEM_SET_ERROR( FT_MEM_RENEW_ARRAY( ptr, curcnt, newcnt ) )
350
351#define FT_QNEW( ptr ) FT_MEM_SET_ERROR( FT_MEM_QNEW( ptr ) )
352
353#define FT_QNEW_ARRAY( ptr, count ) \
354 FT_MEM_SET_ERROR( FT_MEM_QNEW_ARRAY( ptr, count ) )
355
356#define FT_QRENEW_ARRAY( ptr, curcnt, newcnt ) \
357 FT_MEM_SET_ERROR( FT_MEM_QRENEW_ARRAY( ptr, curcnt, newcnt ) )
358
359
360 FT_BASE( FT_Pointer )
361 ft_mem_strdup( FT_Memory memory,
362 const char* str,
363 FT_Error *p_error );
364
365 FT_BASE( FT_Pointer )
366 ft_mem_dup( FT_Memory memory,
367 const void* address,
368 FT_ULong size,
369 FT_Error *p_error );
370
371
372#define FT_MEM_STRDUP( dst, str ) \
373 (dst) = (char*)ft_mem_strdup( memory, (const char*)(str), &error )
374
375#define FT_STRDUP( dst, str ) \
376 FT_MEM_SET_ERROR( FT_MEM_STRDUP( dst, str ) )
377
378#define FT_MEM_DUP( dst, address, size ) \
379 FT_ASSIGNP_INNER( dst, ft_mem_dup( memory, \
380 (address), \
381 (FT_ULong)(size), \
382 &error ) )
383
384#define FT_DUP( dst, address, size ) \
385 FT_MEM_SET_ERROR( FT_MEM_DUP( dst, address, size ) )
386
387
388 /* Return >= 1 if a truncation occurs. */
389 /* Return 0 if the source string fits the buffer. */
390 /* This is *not* the same as strlcpy(). */
391 FT_BASE( FT_Int )
392 ft_mem_strcpyn( char* dst,
393 const char* src,
394 FT_ULong size );
395
396#define FT_STRCPYN( dst, src, size ) \
397 ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
398
399
400FT_END_HEADER
401
402#endif /* FTMEMORY_H_ */
403
404
405/* END */