1 #ifndef MBA_VARRAY_H
2 #define MBA_VARRAY_H
3 
4 /* varray - a variable sized array
5  */
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #ifndef LIBMBA_API
12 #ifdef WIN32
13 # ifdef LIBMBA_EXPORTS
14 #  define LIBMBA_API  __declspec(dllexport)
15 # else /* LIBMBA_EXPORTS */
16 #  define LIBMBA_API  __declspec(dllimport)
17 # endif /* LIBMBA_EXPORTS */
18 #else /* WIN32 */
19 # define LIBMBA_API extern
20 #endif /* WIN32 */
21 #endif /* LIBMBA_API */
22 
23 #include <stddef.h>
24 #include <mba/allocator.h>
25 #include <mba/iterator.h>
26 
27 /*
28 0  1      32768
29 1  2      65536
30 2  4      131072
31 3  8      262144
32 4  16     524288
33 5  32     1048576  2^5 is default VARRAY_INIT_SIZE
34 6  64     2097152
35 7  128    4194304
36 8  256    8388608
37 9  512
38 10 1024
39 11 2048
40 12 4096
41 13 8192
42 14 16384
43 15 32768
44 16 65536
45 17 131072
46 18 262144
47 19 524288
48 20 1048576
49 21 2097152
50 22 4194304
51 23 8388608
52 */
53 
54 #ifndef VARRAY_INIT_SIZE
55 #define VARRAY_INIT_SIZE 5
56 #endif
57 
58 struct varray {
59 	size_t size;                                          /* element size */
60 	ptrdiff_t al;  /* relative offset of this object to allocator of bins */
61 	ref_t bins[16];                                 /* 0 to 2^20 elements */
62 };
63 
64 LIBMBA_API int varray_init(struct varray *va, size_t membsize, struct allocator *al);
65 LIBMBA_API int varray_reinit(struct varray *va, struct allocator *al);
66 LIBMBA_API int varray_deinit(struct varray *va);
67 LIBMBA_API struct varray *varray_new(size_t membsize, struct allocator *al);
68 LIBMBA_API int varray_del(void *va);
69 LIBMBA_API int varray_release(struct varray *va, unsigned int from);
70 LIBMBA_API void *varray_get(struct varray *va, unsigned int idx);
71 LIBMBA_API int varray_index(struct varray *va, void *elem);
72 LIBMBA_API void varray_iterate(void *va, iter_t *iter);
73 LIBMBA_API void *varray_next(void *va, iter_t *iter);
74 
75 #ifdef __cplusplus
76 }
77 #endif
78 
79 #endif /* MBA_VARRAY_H */
80