Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/array.h @ 3283:21f30709ead8 HEAD
Added ARRAY_DEFINE_EXTERN().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 08 Apr 2005 18:22:28 +0300 |
parents | a2943c050571 |
children | ed782bb26632 |
rev | line source |
---|---|
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1 #ifndef __ARRAY_H |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 #define __ARRAY_H |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "buffer.h" |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 /* Array is a buffer accessible using fixed size elements. If DEBUG is |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 enabled, it also provides compile time type safety: |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 If DEBUG is enabled, an extra variable is defined along with the array |
3194
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
10 itself. This is used to cast array_idx() return value correctly, so |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 compiler gives a warning if it's assigned into variable with a different |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 type. |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 Example usage: |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 struct foo { |
3194
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
17 array_t ARRAY_DEFINE(bars, struct bar); |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 ... |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 }; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 |
3193
d0adece8a6a0
Updated example code, it was using older API..
Timo Sirainen <tss@iki.fi>
parents:
3190
diff
changeset
|
21 ARRAY_CREATE(&foo->bars, default_pool, struct bar, 10); |
d0adece8a6a0
Updated example code, it was using older API..
Timo Sirainen <tss@iki.fi>
parents:
3190
diff
changeset
|
22 ARRAY_CREATE(&foo->bars, default_pool, struct baz, 10); // compiler warning |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 |
3193
d0adece8a6a0
Updated example code, it was using older API..
Timo Sirainen <tss@iki.fi>
parents:
3190
diff
changeset
|
24 struct bar *bar = array_idx(&foo->bars, 5); |
d0adece8a6a0
Updated example code, it was using older API..
Timo Sirainen <tss@iki.fi>
parents:
3190
diff
changeset
|
25 struct baz *baz = array_idx(&foo->bars, 5); // compiler warning |
3194
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
26 |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
27 When passing array_t as a parameter to function, or when it's otherwise |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
28 accessed in a way that the extra variable cannot be accessed, the code |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
29 won't compile. For situations like those, there's a ARRAY_SET_TYPE() macro. |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
30 |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
31 Example: |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
32 |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
33 void do_foo(array_t *bars) { |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
34 ARRAY_SET_TYPE(bars, struct foo); |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
35 struct foo *foo = array_idx(bars, 0); |
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
36 } |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 */ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 #if defined (DEBUG) && defined (__GNUC__) |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
39 # define ARRAY_TYPE_CHECKS |
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
40 #endif |
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
41 |
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
42 #ifdef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 # define ARRAY_DEFINE(name, array_type) name; array_type *name ## __ ## type |
3283 | 44 # define ARRAY_DEFINE_EXTERN(name, array_type) \ |
45 name; extern array_type *name ## __ ## type | |
3253
f5a1c3f7fd72
Added ARRAY_DEFINE_PTR() which needs to be used instead of ARRAY_DEFINE()
Timo Sirainen <tss@iki.fi>
parents:
3214
diff
changeset
|
46 # define ARRAY_DEFINE_PTR(name, array_type) \ |
f5a1c3f7fd72
Added ARRAY_DEFINE_PTR() which needs to be used instead of ARRAY_DEFINE()
Timo Sirainen <tss@iki.fi>
parents:
3214
diff
changeset
|
47 name; array_type **name ## __ ## type |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
48 # define ARRAY_CREATE(array, pool, array_type, init_count) STMT_START { \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 array_type *_array_tmp = *(array ## __ ## type); _array_tmp = NULL; \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 array_create(array, pool, sizeof(array_type), init_count); \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 } STMT_END |
3194
d5935326e06f
Renamed ARRAY_ARG_SET_TYPE() to ARRAY_SET_TYPE() and updated comments.
Timo Sirainen <tss@iki.fi>
parents:
3193
diff
changeset
|
52 # define ARRAY_SET_TYPE(array, array_type) \ |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 array_type **array ## __ ## type = NULL |
3208
b5fd010a4454
Added ARRAY_INIT for initializing array_t in structs.
Timo Sirainen <tss@iki.fi>
parents:
3196
diff
changeset
|
54 # define ARRAY_INIT { 0, 0 }, 0 |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 # define ARRAY_DEFINE(name, array_type) name |
3283 | 57 # define ARRAY_DEFINE_EXTERN(name, array_type) name |
3253
f5a1c3f7fd72
Added ARRAY_DEFINE_PTR() which needs to be used instead of ARRAY_DEFINE()
Timo Sirainen <tss@iki.fi>
parents:
3214
diff
changeset
|
58 # define ARRAY_DEFINE_PTR(name, array_type) name |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 # define ARRAY_CREATE(array, pool, array_type, init_count) \ |
3214
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
60 array_create(array, pool, sizeof(array_type), init_count) |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
61 /* The reason we do this for non-ARRAY_TYPE_CHECKS as well is because if we |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
62 left this empty, some compilers wouldn't like an extra ";" character at |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
63 the beginning of the function (eg. gcc 2.95). |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
64 |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
65 However just declaring the variable gives "unused variable" warning. |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
66 I couldn't think of anything better, so we just use gcc-specific |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
67 unused-attribute to get rid of that with gcc. */ |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
68 # define ARRAY_SET_TYPE(array, array_type) \ |
e3f6748c59ba
ARRAY_SET_TYPE() in non-DEBUG-mode was giving errors with older gccs if it
Timo Sirainen <tss@iki.fi>
parents:
3208
diff
changeset
|
69 array_type **array ## __ ## type __attr_unused__ = NULL |
3208
b5fd010a4454
Added ARRAY_INIT for initializing array_t in structs.
Timo Sirainen <tss@iki.fi>
parents:
3196
diff
changeset
|
70 # define ARRAY_INIT { 0, 0 } |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
72 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 struct array { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 buffer_t *buffer; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 size_t element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 }; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 array_create_from_buffer(array_t *array, buffer_t *buffer, size_t element_size) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
80 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 array->buffer = buffer; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 array->element_size = element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 array_create(array_t *array, pool_t pool, |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
87 size_t element_size, unsigned int init_count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 buffer_t *buffer; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 buffer = buffer_create_dynamic(pool, init_count * element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 array_create_from_buffer(array, buffer, element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
95 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 array_free(array_t *array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
97 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
98 buffer_free(array->buffer); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
99 array->buffer = NULL; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
100 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
101 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
102 static inline int |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 array_is_created(const array_t *array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
104 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
105 return array->buffer != NULL; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
106 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
107 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
108 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
109 array_clear(array_t *array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
110 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
111 buffer_set_used_size(array->buffer, 0); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
112 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
114 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
115 _array_append(array_t *array, const void *data, unsigned int count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
116 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 buffer_append(array->buffer, data, count * array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
118 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
119 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
120 # define array_append _array_append |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 # define array_append(array, data, count) STMT_START { \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
123 typeof(*(array ## __ ## type)) _array_tmp = data; \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
124 _array_append(array, _array_tmp, count); \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
125 } STMT_END |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
126 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
127 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
128 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
129 array_append_array(array_t *dest_array, const array_t *src_array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
130 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
131 i_assert(dest_array->element_size == src_array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
132 buffer_append_buf(dest_array->buffer, src_array->buffer, 0, (size_t)-1); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
133 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
134 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
135 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
136 _array_insert(array_t *array, unsigned int idx, |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
137 const void *data, unsigned int count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
138 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
139 buffer_insert(array->buffer, idx * array->element_size, |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
140 data, count * array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
142 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
143 # define array_insert _array_insert |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
144 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
145 # define array_insert(array, idx, data, count) STMT_START { \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
146 typeof(*(array ## __ ## type)) _array_tmp = data; \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
147 _array_insert(array, idx, _array_tmp, count); \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
148 } STMT_END |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
151 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 array_delete(array_t *array, unsigned int idx, unsigned int count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
153 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
154 buffer_delete(array->buffer, idx * array->element_size, |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
155 count * array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
156 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
157 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
158 static inline const void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
159 _array_get(const array_t *array, unsigned int *count_r) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
160 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
161 if (count_r != NULL) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
162 *count_r = array->buffer->used / array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
163 return array->buffer->data; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
164 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
165 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
166 # define array_get _array_get |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
167 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
168 # define array_get(array, count) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
169 (const typeof(*(array ## __ ## type)))_array_get(array, count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
170 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
171 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
172 static inline const void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
173 _array_idx(const array_t *array, unsigned int idx) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
174 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
175 i_assert(idx * array->element_size < array->buffer->used); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
176 return CONST_PTR_OFFSET(array->buffer->data, idx * array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
177 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
178 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
179 # define array_idx _array_idx |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
180 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
181 # define array_idx(array, idx) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
182 (const typeof(*(array ## __ ## type)))_array_idx(array, idx) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
183 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
184 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
185 static inline void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
186 _array_get_modifyable(array_t *array, unsigned int *count_r) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
187 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
188 if (count_r != NULL) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
189 *count_r = array->buffer->used / array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
190 return buffer_get_modifyable_data(array->buffer, NULL); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
191 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
192 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
193 # define array_get_modifyable _array_get_modifyable |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
194 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
195 # define array_get_modifyable(array, count) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
196 (typeof(*(array ## __ ## type))) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
197 _array_get_modifyable(array, count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
198 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
199 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
200 static inline void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
201 _array_modifyable_idx(array_t *array, unsigned int idx) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
202 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
203 size_t pos; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
204 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
205 pos = idx * array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
206 if (pos >= array->buffer->used) { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
207 /* index doesn't exist yet, initialize with zero */ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
208 buffer_append_zero(array->buffer, pos + array->element_size - |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
209 array->buffer->used); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
210 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
211 return buffer_get_space_unsafe(array->buffer, pos, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
212 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
213 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
214 # define array_modifyable_idx _array_modifyable_idx |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
215 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
216 # define array_modifyable_idx(array, count) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
217 (typeof(*(array ## __ ## type))) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
218 _array_modifyable_idx(array, count) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
219 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
220 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
221 static inline void |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
222 _array_idx_set(array_t *array, unsigned int idx, const void *data) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
223 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
224 size_t pos; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
225 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
226 pos = idx * array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
227 if (pos > array->buffer->used) { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
228 /* index doesn't exist yet, initialize with zero */ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
229 buffer_append_zero(array->buffer, pos - array->buffer->used); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
230 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
231 buffer_write(array->buffer, pos, data, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
232 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
233 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
234 # define array_idx_set _array_idx_set |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
235 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
236 # define array_idx_set(array, idx, data) STMT_START { \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
237 typeof(*(array ## __ ## type)) _array_tmp = data; \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
238 _array_idx_set(array, idx, _array_tmp); \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
239 } STMT_END |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
240 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
241 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
242 static inline void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
243 _array_modifyable_append(array_t *array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
244 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
245 void *data; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
246 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
247 data = buffer_append_space_unsafe(array->buffer, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
248 memset(data, 0, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
249 return data; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
250 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
251 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
252 # define array_modifyable_append _array_modifyable_append |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
253 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
254 # define array_modifyable_append(array) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
255 (typeof(*(array ## __ ## type))) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
256 _array_modifyable_append(array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
257 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
258 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
259 static inline void * |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
260 _array_modifyable_insert(array_t *array, unsigned int idx) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
261 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
262 void *data; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
263 size_t pos; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
264 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
265 pos = idx * array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
266 buffer_copy(array->buffer, pos + array->element_size, |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
267 array->buffer, pos, (size_t)-1); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
268 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
269 data = buffer_get_space_unsafe(array->buffer, pos, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
270 memset(data, 0, array->element_size); |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
271 return data; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
272 } |
3196
b426b1d58296
#ifdefs were wrong, we need both __GNUC__ and DEBUG to do type checking.
Timo Sirainen <tss@iki.fi>
parents:
3194
diff
changeset
|
273 #ifndef ARRAY_TYPE_CHECKS |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
274 # define array_modifyable_insert _array_modifyable_insert |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
275 #else |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
276 # define array_modifyable_insert(array, idx) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
277 (typeof(*(array ## __ ## type))) \ |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
278 _array_modifyable_insert(array, idx) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
279 #endif |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
280 |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
281 static inline unsigned int |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
282 array_count(const array_t *array) |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
283 { |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
284 return array->buffer->used / array->element_size; |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
285 } |
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
286 |
3254
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
287 static inline int |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
288 array_cmp(const array_t *array1, const array_t *array2) |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
289 { |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
290 if (!array_is_created(array1) || array1->buffer->used == 0) |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
291 return !array_is_created(array2) || array2->buffer->used == 0; |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
292 |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
293 if (!array_is_created(array2)) |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
294 return FALSE; |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
295 |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
296 return buffer_cmp(array1->buffer, array2->buffer); |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
297 } |
a2943c050571
Keywords are now stored in X-Keywords headers in mbox. Did several related
Timo Sirainen <tss@iki.fi>
parents:
3253
diff
changeset
|
298 |
3190
ba17b6e45193
Added dynamic array implementation. When compiling with gcc and DEBUG
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
299 #endif |