blob: e5910555faae9319ebd5ca77af06d2c8f21626bf [file] [log] [blame]
David 'Digit' Turner4c0f7452010-11-17 17:55:17 +01001/* Copyright (C) 2009 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12#ifndef _ANDROID_UTILS_VECTOR_H
13#define _ANDROID_UTILS_VECTOR_H
14
15#include "android/utils/system.h"
16#include "android/utils/assert.h"
17
18#define AVECTOR_DECL(ctype,name) \
19 ctype* name; \
20 unsigned num_##name; \
21 unsigned max_##name \
22
23#define AVECTOR_SIZE(obj,name) \
24 (obj)->num_##name
25
26#define AVECTOR_INIT(obj,name) \
27 do { \
28 (obj)->name = NULL; \
29 (obj)->num_##name = 0; \
30 (obj)->max_##name = 0; \
31 } while (0)
32
33#define AVECTOR_INIT_ALLOC(obj,name,count) \
34 do { \
35 AARRAY_NEW0( (obj)->name, (count) ); \
36 (obj)->num_##name = 0; \
37 (obj)->max_##name = (count); \
38 } while (0)
39
40#define AVECTOR_DONE(obj,name) \
41 do { \
42 AFREE((obj)->name); \
43 (obj)->num_##name = 0; \
44 (obj)->max_##name = 0; \
45 } while (0)
46
47#define AVECTOR_AT(obj,name,index) \
48 (&(obj)->name[(index)])
49
50#define AVECTOR_REALLOC(obj,name,newMax) \
51 do { \
52 AARRAY_RENEW((obj)->name,newMax); \
53 (obj)->max_##name = (newMax); \
54 } while(0)
55
56#define AVECTOR_ENSURE(obj,name,newCount) \
57 do { \
58 unsigned _newCount = (newCount); \
59 if (_newCount > (obj)->max_##name) \
60 AASSERT_LOC(); \
61 _avector_ensure( (void**)&(obj)->name, sizeof((obj)->name[0]), \
62 &(obj)->max_##name, _newCount ); \
63 } while (0);
64
65extern void _avector_ensure( void** items, size_t itemSize,
66 unsigned* pMaxItems, unsigned newCount );
67
68#define AVECTOR_FOREACH(obj,name,itemptr,statement) \
69 do { \
70 unsigned __vector_nn = 0; \
71 unsigned __vector_max = (obj)->num_##name; \
72 for ( ; __vector_nn < __vector_max; __vector_nn++ ) { \
73 itemptr = &(obj)->name[__vector_nn]; \
74 statement; \
75 } \
76 } while (0);
77
78/* */
79
80#endif /* _ANDROID_UTILS_VECTOR_H */