1 #include <android/utils/vector.h>
2 #include <limits.h>
3
4 extern void
_avector_ensure(void ** items,size_t itemSize,unsigned * pMaxItems,unsigned newCount)5 _avector_ensure( void** items, size_t itemSize, unsigned* pMaxItems, unsigned newCount )
6 {
7 unsigned oldMax = *pMaxItems;
8
9 if (newCount > oldMax) {
10 unsigned newMax = oldMax;
11 unsigned bigMax = UINT_MAX / itemSize;
12
13 if (itemSize == 0) {
14 AASSERT_FAIL("trying to reallocate array of 0-size items (count=%d)\n", newCount);
15 }
16
17 if (newCount > bigMax) {
18 AASSERT_FAIL("trying to reallocate over-sized array of %d-bytes items (%d > %d)\n",
19 itemSize, newCount, bigMax);
20 }
21
22 while (newMax < newCount) {
23 unsigned newMax2 = newMax + (newMax >> 1) + 4;
24 if (newMax2 < newMax || newMax2 > bigMax)
25 newMax2 = bigMax;
26 newMax = newMax2;
27 }
28
29 *items = _android_array_realloc( *items, itemSize, newMax );
30 *pMaxItems = newMax;
31 }
32 }
33
34