blob: 9029d05ee1cc87cae576b0a076f3d133a3b6f408 [file] [log] [blame]
David 'Digit' Turner4c0f7452010-11-17 17:55:17 +01001#include <android/utils/vector.h>
2#include <limits.h>
3
4extern void
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