This source file includes following definitions.
- opal_value_array_init
- opal_value_array_reserve
- opal_value_array_get_size
- opal_value_array_get_item
- opal_value_array_set_item
- opal_value_array_append_item
- opal_value_array_remove_item
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #ifndef OPAL_VALUE_ARRAY_H
20 #define OPAL_VALUE_ARRAY_H
21
22 #include "opal_config.h"
23
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 #include <strings.h>
27 #endif
28
29 #include "opal/class/opal_object.h"
30 #if OPAL_ENABLE_DEBUG
31 #include "opal/util/output.h"
32 #endif
33 #include "opal/constants.h"
34
35 BEGIN_C_DECLS
36
37
38
39
40
41 struct opal_value_array_t
42 {
43 opal_object_t super;
44 unsigned char* array_items;
45 size_t array_item_sizeof;
46 size_t array_size;
47 size_t array_alloc_size;
48 };
49 typedef struct opal_value_array_t opal_value_array_t;
50
51 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_value_array_t);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 static inline int opal_value_array_init(opal_value_array_t *array, size_t item_sizeof)
67 {
68 array->array_item_sizeof = item_sizeof;
69 array->array_alloc_size = 1;
70 array->array_size = 0;
71 array->array_items = (unsigned char*)realloc(array->array_items, item_sizeof * array->array_alloc_size);
72 return (NULL != array->array_items) ? OPAL_SUCCESS : OPAL_ERR_OUT_OF_RESOURCE;
73 }
74
75
76
77
78
79
80
81
82
83
84 static inline int opal_value_array_reserve(opal_value_array_t* array, size_t size)
85 {
86 if(size > array->array_alloc_size) {
87 array->array_items = (unsigned char*)realloc(array->array_items, array->array_item_sizeof * size);
88 if(NULL == array->array_items) {
89 array->array_size = 0;
90 array->array_alloc_size = 0;
91 return OPAL_ERR_OUT_OF_RESOURCE;
92 }
93 array->array_alloc_size = size;
94 }
95 return OPAL_SUCCESS;
96 }
97
98
99
100
101
102
103
104
105
106
107 static inline size_t opal_value_array_get_size(opal_value_array_t* array)
108 {
109 return array->array_size;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 OPAL_DECLSPEC int opal_value_array_set_size(opal_value_array_t* array, size_t size);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 #define OPAL_VALUE_ARRAY_GET_ITEM(array, item_type, item_index) \
146 ((item_type*)((array)->array_items))[item_index]
147
148
149
150
151
152
153
154
155
156
157
158
159
160 static inline void* opal_value_array_get_item(opal_value_array_t *array, size_t item_index)
161 {
162 if(item_index >= array->array_size && opal_value_array_set_size(array, item_index+1) != OPAL_SUCCESS)
163 return NULL;
164 return array->array_items + (item_index * array->array_item_sizeof);
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 #define OPAL_VALUE_ARRAY_SET_ITEM(array, item_type, item_index, item_value) \
184 (((item_type*)((array)->array_items))[item_index] = item_value)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 static inline int opal_value_array_set_item(opal_value_array_t *array, size_t item_index, const void* item)
201 {
202 int rc;
203 if(item_index >= array->array_size &&
204 (rc = opal_value_array_set_size(array, item_index+1)) != OPAL_SUCCESS)
205 return rc;
206 memcpy(array->array_items + (item_index * array->array_item_sizeof), item, array->array_item_sizeof);
207 return OPAL_SUCCESS;
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 static inline int opal_value_array_append_item(opal_value_array_t *array, const void *item)
226 {
227 return opal_value_array_set_item(array, array->array_size, item);
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 static inline int opal_value_array_remove_item(opal_value_array_t *array, size_t item_index)
244 {
245 #if OPAL_ENABLE_DEBUG
246 if (item_index >= array->array_size) {
247 opal_output(0, "opal_value_array_remove_item: invalid index %lu\n", (unsigned long)item_index);
248 return OPAL_ERR_BAD_PARAM;
249 }
250 #endif
251 memmove(array->array_items+(array->array_item_sizeof * item_index),
252 array->array_items+(array->array_item_sizeof * (item_index+1)),
253 array->array_item_sizeof * (array->array_size - item_index - 1));
254 array->array_size--;
255 return OPAL_SUCCESS;
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 #define OPAL_VALUE_ARRAY_GET_BASE(array, item_type) \
275 ((item_type*) ((array)->array_items))
276
277 END_C_DECLS
278
279 #endif
280