ReactOS 0.4.16-dev-2332-g4cba65d
ftutil.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * ftutil.c
4 *
5 * FreeType utility file for memory and list management (body).
6 *
7 * Copyright (C) 2002-2020 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
22#include <freetype/ftlist.h>
23
24
25 /**************************************************************************
26 *
27 * The macro FT_COMPONENT is used in trace mode. It is an implicit
28 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
29 * messages during execution.
30 */
31#undef FT_COMPONENT
32#define FT_COMPONENT memory
33
34
35 /*************************************************************************/
36 /*************************************************************************/
37 /*************************************************************************/
38 /***** *****/
39 /***** *****/
40 /***** M E M O R Y M A N A G E M E N T *****/
41 /***** *****/
42 /***** *****/
43 /*************************************************************************/
44 /*************************************************************************/
45 /*************************************************************************/
46
47
51 FT_Error *p_error )
52 {
55
56 if ( !error && block && size > 0 )
58
59 *p_error = error;
60 return block;
61 }
62
63
67 FT_Error *p_error )
68 {
71
72
73 if ( size > 0 )
74 {
75 block = memory->alloc( memory, size );
76 if ( !block )
77 error = FT_THROW( Out_Of_Memory );
78 }
79 else if ( size < 0 )
80 {
81 /* may help catch/prevent security issues */
82 error = FT_THROW( Invalid_Argument );
83 }
84
85 *p_error = error;
86 return block;
87 }
88
89
92 FT_Long item_size,
93 FT_Long cur_count,
94 FT_Long new_count,
95 void* block,
96 FT_Error *p_error )
97 {
99
100
101 block = ft_mem_qrealloc( memory, item_size,
102 cur_count, new_count, block, &error );
103 if ( !error && block && new_count > cur_count )
104 FT_MEM_ZERO( (char*)block + cur_count * item_size,
105 ( new_count - cur_count ) * item_size );
106
107 *p_error = error;
108 return block;
109 }
110
111
114 FT_Long item_size,
115 FT_Long cur_count,
116 FT_Long new_count,
117 void* block,
118 FT_Error *p_error )
119 {
121
122
123 /* Note that we now accept `item_size == 0' as a valid parameter, in
124 * order to cover very weird cases where an ALLOC_MULT macro would be
125 * called.
126 */
127 if ( cur_count < 0 || new_count < 0 || item_size < 0 )
128 {
129 /* may help catch/prevent nasty security issues */
130 error = FT_THROW( Invalid_Argument );
131 }
132 else if ( new_count == 0 || item_size == 0 )
133 {
135 block = NULL;
136 }
137 else if ( new_count > FT_INT_MAX / item_size )
138 {
139 error = FT_THROW( Array_Too_Large );
140 }
141 else if ( cur_count == 0 )
142 {
143 FT_ASSERT( !block );
144
145 block = memory->alloc( memory, new_count * item_size );
146 if ( block == NULL )
147 error = FT_THROW( Out_Of_Memory );
148 }
149 else
150 {
152 FT_Long cur_size = cur_count * item_size;
153 FT_Long new_size = new_count * item_size;
154
155
156 block2 = memory->realloc( memory, cur_size, new_size, block );
157 if ( !block2 )
158 error = FT_THROW( Out_Of_Memory );
159 else
160 block = block2;
161 }
162
163 *p_error = error;
164 return block;
165 }
166
167
168 FT_BASE_DEF( void )
170 const void *P )
171 {
172 if ( P )
173 memory->free( memory, (void*)P );
174 }
175
176
179 const void* address,
181 FT_Error *p_error )
182 {
185
186
187 if ( !error && address && size > 0 )
188 ft_memcpy( p, address, size );
189
190 *p_error = error;
191 return p;
192 }
193
194
197 const char* str,
198 FT_Error *p_error )
199 {
201 : 0;
202
203
204 return ft_mem_dup( memory, str, len, p_error );
205 }
206
207
210 const char* src,
211 FT_ULong size )
212 {
213 while ( size > 1 && *src != 0 )
214 {
215 *dst++ = *src++;
216 size--;
217 }
218
219 *dst = 0; /* always zero-terminate */
220
221 return *src != 0;
222 }
223
224
225 /*************************************************************************/
226 /*************************************************************************/
227 /*************************************************************************/
228 /***** *****/
229 /***** *****/
230 /***** D O U B L Y L I N K E D L I S T S *****/
231 /***** *****/
232 /***** *****/
233 /*************************************************************************/
234 /*************************************************************************/
235 /*************************************************************************/
236
237#undef FT_COMPONENT
238#define FT_COMPONENT list
239
240 /* documentation is in ftlist.h */
241
244 void* data )
245 {
247
248
249 if ( !list )
250 return NULL;
251
252 cur = list->head;
253 while ( cur )
254 {
255 if ( cur->data == data )
256 return cur;
257
258 cur = cur->next;
259 }
260
261 return NULL;
262 }
263
264
265 /* documentation is in ftlist.h */
266
267 FT_EXPORT_DEF( void )
270 {
272
273
274 if ( !list || !node )
275 return;
276
277 before = list->tail;
278
279 node->next = NULL;
280 node->prev = before;
281
282 if ( before )
283 before->next = node;
284 else
285 list->head = node;
286
287 list->tail = node;
288 }
289
290
291 /* documentation is in ftlist.h */
292
293 FT_EXPORT_DEF( void )
296 {
298
299
300 if ( !list || !node )
301 return;
302
303 after = list->head;
304
305 node->next = after;
306 node->prev = NULL;
307
308 if ( !after )
309 list->tail = node;
310 else
311 after->prev = node;
312
313 list->head = node;
314 }
315
316
317 /* documentation is in ftlist.h */
318
319 FT_EXPORT_DEF( void )
322 {
324
325
326 if ( !list || !node )
327 return;
328
329 before = node->prev;
330 after = node->next;
331
332 if ( before )
333 before->next = after;
334 else
335 list->head = after;
336
337 if ( after )
338 after->prev = before;
339 else
340 list->tail = before;
341 }
342
343
344 /* documentation is in ftlist.h */
345
346 FT_EXPORT_DEF( void )
349 {
351
352
353 if ( !list || !node )
354 return;
355
356 before = node->prev;
357 after = node->next;
358
359 /* check whether we are already on top of the list */
360 if ( !before )
361 return;
362
363 before->next = after;
364
365 if ( after )
366 after->prev = before;
367 else
368 list->tail = before;
369
370 node->prev = NULL;
371 node->next = list->head;
372 list->head->prev = node;
373 list->head = node;
374 }
375
376
377 /* documentation is in ftlist.h */
378
382 void* user )
383 {
386
387
388 if ( !list || !iterator )
389 return FT_THROW( Invalid_Argument );
390
391 cur = list->head;
392
393 while ( cur )
394 {
395 FT_ListNode next = cur->next;
396
397
398 error = iterator( cur, user );
399 if ( error )
400 break;
401
402 cur = next;
403 }
404
405 return error;
406 }
407
408
409 /* documentation is in ftlist.h */
410
411 FT_EXPORT_DEF( void )
415 void* user )
416 {
418
419
420 if ( !list || !memory )
421 return;
422
423 cur = list->head;
424 while ( cur )
425 {
426 FT_ListNode next = cur->next;
427 void* data = cur->data;
428
429
430 if ( destroy )
431 destroy( memory, data, user );
432
433 FT_FREE( cur );
434 cur = next;
435 }
436
437 list->head = NULL;
438 list->tail = NULL;
439 }
440
441
442/* END */
void destroy(_Tp *__pointer)
Definition: _construct.h:278
void user(int argc, const char *argv[])
Definition: cmds.c:1350
Definition: list.h:37
struct list * prev
Definition: list.h:39
#define FT_EXPORT_DEF(x)
#define FT_BASE_DEF(x)
#define NULL
Definition: types.h:112
#define P(row, col)
size_t const new_size
Definition: expand.cpp:66
return FT_Err_Ok
Definition: ftbbox.c:526
#define FT_ASSERT(condition)
Definition: ftdebug.h:241
#define FT_THROW(e)
Definition: ftdebug.h:243
FT_Error(* FT_List_Iterator)(FT_ListNode node, void *user)
Definition: ftlist.h:196
void(* FT_List_Destructor)(FT_Memory memory, void *data, void *user)
Definition: ftlist.h:250
#define FT_FREE(ptr)
Definition: ftmemory.h:337
#define FT_MEM_ZERO(dest, count)
Definition: ftmemory.h:244
#define ft_memcpy
Definition: ftstdlib.h:82
#define ft_strlen
Definition: ftstdlib.h:88
#define FT_INT_MAX
Definition: ftstdlib.h:63
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:64
unsigned long FT_ULong
Definition: fttypes.h:253
int FT_Error
Definition: fttypes.h:299
signed long FT_Long
Definition: fttypes.h:242
signed int FT_Int
Definition: fttypes.h:220
ft_mem_qalloc(FT_Memory memory, FT_Long size, FT_Error *p_error)
Definition: ftutil.c:65
ft_mem_dup(FT_Memory memory, const void *address, FT_ULong size, FT_Error *p_error)
Definition: ftutil.c:178
ft_mem_strdup(FT_Memory memory, const char *str, FT_Error *p_error)
Definition: ftutil.c:196
FT_List_Up(FT_List list, FT_ListNode node)
Definition: ftutil.c:347
ft_mem_alloc(FT_Memory memory, FT_Long size, FT_Error *p_error)
Definition: ftutil.c:49
FT_List_Insert(FT_List list, FT_ListNode node)
Definition: ftutil.c:294
FT_List_Find(FT_List list, void *data)
Definition: ftutil.c:243
ft_mem_free(FT_Memory memory, const void *P)
Definition: ftutil.c:169
ft_mem_strcpyn(char *dst, const char *src, FT_ULong size)
Definition: ftutil.c:209
FT_List_Finalize(FT_List list, FT_List_Destructor destroy, FT_Memory memory, void *user)
Definition: ftutil.c:412
ft_mem_realloc(FT_Memory memory, FT_Long item_size, FT_Long cur_count, FT_Long new_count, void *block, FT_Error *p_error)
Definition: ftutil.c:91
FT_List_Remove(FT_List list, FT_ListNode node)
Definition: ftutil.c:320
FT_List_Iterate(FT_List list, FT_List_Iterator iterator, void *user)
Definition: ftutil.c:380
ft_mem_qrealloc(FT_Memory memory, FT_Long item_size, FT_Long cur_count, FT_Long new_count, void *block, FT_Error *p_error)
Definition: ftutil.c:113
FT_List_Add(FT_List list, FT_ListNode node)
Definition: ftutil.c:268
FxCollectionEntry * cur
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLuint address
Definition: glext.h:9393
GLenum src
Definition: glext.h:6340
GLsizeiptr size
Definition: glext.h:5919
GLenum GLenum dst
Definition: glext.h:6340
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
#define error(str)
Definition: mkdosfs.c:1605
static char memory[1024 *256]
Definition: process.c:122
static const struct metadata_block block2[]
Definition: metadata.c:3639
static unsigned __int64 next
Definition: rand_nt.c:6
const WCHAR * str
__inline int before(__u32 seq1, __u32 seq2)
Definition: tcpcore.h:2390
__inline int after(__u32 seq1, __u32 seq2)
Definition: tcpcore.h:2395
Definition: dlist.c:348
void * next
Definition: dlist.c:360
static unsigned int block
Definition: xmlmemory.c:101
#define const
Definition: zconf.h:233