1
/***************************************************************************/
5
/* Adobe's code for Array Stacks (body). */
7
/* Copyright 2007-2013 Adobe Systems Incorporated. */
9
/* This software, and all works of authorship, whether in source or */
10
/* object code form as indicated by the copyright notice(s) included */
11
/* herein (collectively, the "Work") is made available, and may only be */
12
/* used, modified, and distributed under the FreeType Project License, */
13
/* LICENSE.TXT. Additionally, subject to the terms and conditions of the */
14
/* FreeType Project License, each contributor to the Work hereby grants */
15
/* to any individual or legal entity exercising permissions granted by */
16
/* the FreeType Project License and this section (hereafter, "You" or */
17
/* "Your") a perpetual, worldwide, non-exclusive, no-charge, */
18
/* royalty-free, irrevocable (except as stated in this section) patent */
19
/* license to make, have made, use, offer to sell, sell, import, and */
20
/* otherwise transfer the Work, where such license applies only to those */
21
/* patent claims licensable by such contributor that are necessarily */
22
/* infringed by their contribution(s) alone or by combination of their */
23
/* contribution(s) with the Work to which such contribution(s) was */
24
/* submitted. If You institute patent litigation against any entity */
25
/* (including a cross-claim or counterclaim in a lawsuit) alleging that */
26
/* the Work or a contribution incorporated within the Work constitutes */
27
/* direct or contributory patent infringement, then any patent licenses */
28
/* granted to You under this License for that Work shall terminate as of */
29
/* the date such litigation is filed. */
31
/* By using, modifying, or distributing the Work you indicate that you */
32
/* have read and understood the terms and conditions of the */
33
/* FreeType Project License as well as those provided in this section, */
34
/* and you accept them fully. */
36
/***************************************************************************/
40
#include FT_INTERNAL_DEBUG_H
49
* CF2_ArrStack uses an error pointer, to enable shared errors.
50
* Shared errors are necessary when multiple objects allow the program
51
* to continue after detecting errors. Only the first error should be
56
cf2_arrstack_init( CF2_ArrStack arrstack,
61
FT_ASSERT( arrstack != NULL );
63
/* initialize the structure */
64
arrstack->memory = memory;
65
arrstack->error = error;
66
arrstack->sizeItem = sizeItem;
67
arrstack->allocated = 0;
68
arrstack->chunk = 10; /* chunks of 10 items */
70
arrstack->totalSize = 0;
76
cf2_arrstack_finalize( CF2_ArrStack arrstack )
78
FT_Memory memory = arrstack->memory; /* for FT_FREE */
81
FT_ASSERT( arrstack != NULL );
83
arrstack->allocated = 0;
85
arrstack->totalSize = 0;
87
/* free the data buffer */
88
FT_FREE( arrstack->ptr );
92
/* allocate or reallocate the buffer size; */
93
/* return false on memory error */
95
cf2_arrstack_setNumElements( CF2_ArrStack arrstack,
98
FT_ASSERT( arrstack != NULL );
101
FT_Error error = FT_Err_Ok; /* for FT_REALLOC */
102
FT_Memory memory = arrstack->memory; /* for FT_REALLOC */
104
size_t newSize = numElements * arrstack->sizeItem;
107
if ( numElements > FT_LONG_MAX / arrstack->sizeItem )
111
FT_ASSERT( newSize > 0 ); /* avoid realloc with zero size */
113
if ( !FT_REALLOC( arrstack->ptr, arrstack->totalSize, newSize ) )
115
arrstack->allocated = numElements;
116
arrstack->totalSize = newSize;
118
if ( arrstack->count > numElements )
120
/* we truncated the list! */
121
CF2_SET_ERROR( arrstack->error, Stack_Overflow );
122
arrstack->count = numElements;
126
return TRUE; /* success */
131
/* if there's not already an error, store this one */
132
CF2_SET_ERROR( arrstack->error, Out_Of_Memory );
138
/* set the count, ensuring allocation is sufficient */
140
cf2_arrstack_setCount( CF2_ArrStack arrstack,
143
FT_ASSERT( arrstack != NULL );
145
if ( numElements > arrstack->allocated )
147
/* expand the allocation first */
148
if ( !cf2_arrstack_setNumElements( arrstack, numElements ) )
152
arrstack->count = numElements;
156
/* clear the count */
158
cf2_arrstack_clear( CF2_ArrStack arrstack )
160
FT_ASSERT( arrstack != NULL );
166
/* current number of items */
167
FT_LOCAL_DEF( size_t )
168
cf2_arrstack_size( const CF2_ArrStack arrstack )
170
FT_ASSERT( arrstack != NULL );
172
return arrstack->count;
176
FT_LOCAL_DEF( void* )
177
cf2_arrstack_getBuffer( const CF2_ArrStack arrstack )
179
FT_ASSERT( arrstack != NULL );
181
return arrstack->ptr;
185
/* return pointer to the given element */
186
FT_LOCAL_DEF( void* )
187
cf2_arrstack_getPointer( const CF2_ArrStack arrstack,
193
FT_ASSERT( arrstack != NULL );
195
if ( idx >= arrstack->count )
198
CF2_SET_ERROR( arrstack->error, Stack_Overflow );
199
idx = 0; /* choose safe default */
202
newPtr = (FT_Byte*)arrstack->ptr + idx * arrstack->sizeItem;
208
/* push (append) an element at the end of the list; */
209
/* return false on memory error */
210
/* TODO: should there be a length param for extra checking? */
212
cf2_arrstack_push( CF2_ArrStack arrstack,
215
FT_ASSERT( arrstack != NULL );
217
if ( arrstack->count == arrstack->allocated )
219
/* grow the buffer by one chunk */
220
if ( !cf2_arrstack_setNumElements(
221
arrstack, arrstack->allocated + arrstack->chunk ) )
223
/* on error, ignore the push */
228
FT_ASSERT( ptr != NULL );
231
size_t offset = arrstack->count * arrstack->sizeItem;
232
void* newPtr = (FT_Byte*)arrstack->ptr + offset;
235
FT_MEM_COPY( newPtr, ptr, arrstack->sizeItem );
236
arrstack->count += 1;