1 /**************************************************************************
3 * Copyright 2010 LunarG, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
38 * Grow the size of the array.
41 _eglGrowArray(_EGLArray
*array
)
46 new_size
= array
->MaxSize
;
47 while (new_size
<= array
->Size
)
50 elems
= realloc(array
->Elements
, new_size
* sizeof(array
->Elements
[0]));
52 _eglLog(_EGL_DEBUG
, "failed to grow %s array to %d",
53 array
->Name
, new_size
);
57 array
->Elements
= elems
;
58 array
->MaxSize
= new_size
;
68 _eglCreateArray(const char *name
, EGLint init_size
)
72 array
= calloc(1, sizeof(*array
));
75 array
->MaxSize
= (init_size
> 0) ? init_size
: 1;
76 if (!_eglGrowArray(array
)) {
87 * Destroy an array, optionally free the data.
90 _eglDestroyArray(_EGLArray
*array
, void (*free_cb
)(void *))
94 for (i
= 0; i
< array
->Size
; i
++)
95 free_cb(array
->Elements
[i
]);
97 free(array
->Elements
);
103 * Append a element to an array.
106 _eglAppendArray(_EGLArray
*array
, void *elem
)
108 if (array
->Size
>= array
->MaxSize
&& !_eglGrowArray(array
))
111 array
->Elements
[array
->Size
++] = elem
;
116 * Erase an element from an array.
119 _eglEraseArray(_EGLArray
*array
, EGLint i
, void (*free_cb
)(void *))
122 free_cb(array
->Elements
[i
]);
123 if (i
< array
->Size
- 1) {
124 memmove(&array
->Elements
[i
], &array
->Elements
[i
+ 1],
125 (array
->Size
- i
- 1) * sizeof(array
->Elements
[0]));
132 * Find in an array for the given element.
135 _eglFindArray(_EGLArray
*array
, void *elem
)
142 for (i
= 0; i
< array
->Size
; i
++)
143 if (array
->Elements
[i
] == elem
)
150 * Filter an array and return the number of filtered elements.
153 _eglFilterArray(_EGLArray
*array
, void **data
, EGLint size
,
154 _EGLArrayForEach filter
, void *filter_data
)
162 for (i
= 0; i
< array
->Size
; i
++) {
163 if (filter(array
->Elements
[i
], filter_data
)) {
164 if (data
&& count
< size
)
165 data
[count
] = array
->Elements
[i
];
168 if (data
&& count
>= size
)
177 * Flatten an array by converting array elements into another form and store
181 _eglFlattenArray(_EGLArray
*array
, void *buffer
, EGLint elem_size
, EGLint size
,
182 _EGLArrayForEach flatten
)
191 /* clamp size to 0 */
194 /* do not exceed buffer size */
197 for (i
= 0; i
< count
; i
++)
198 flatten(array
->Elements
[i
],
199 (void *) ((char *) buffer
+ elem_size
* i
));