1
- // Copyright 2017 Open Source Robotics Foundation, Inc.
1
+ // Copyright 2018-2019 Open Source Robotics Foundation, Inc.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -34,6 +34,19 @@ typedef struct RCUTILS_PUBLIC_TYPE rcutils_array_list_t
34
34
struct rcutils_array_list_impl_t * impl ;
35
35
} rcutils_array_list_t ;
36
36
37
+ /**
38
+ * Validates that an rcutils_array_list_t* points to a valid array list.
39
+ * \param array_list A pointer to an rcutils_array_list_t
40
+ * \return RCUTILS_RET_INVALID_ARGUMENT if array_list is null
41
+ * \return RCUTILS_RET_NOT_INITIALIZED if array_list is not initialized
42
+ */
43
+ #define ARRAY_LIST_VALIDATE_ARRAY_LIST (array_list ) \
44
+ RCUTILS_CHECK_ARGUMENT_FOR_NULL(array_list, RCUTILS_RET_INVALID_ARGUMENT); \
45
+ if (NULL == array_list->impl) { \
46
+ RCUTILS_SET_ERROR_MSG("array_list is not initialized"); \
47
+ return RCUTILS_RET_NOT_INITIALIZED; \
48
+ }
49
+
37
50
/// Return an empty array_list struct.
38
51
/**
39
52
* This function returns an empty and zero initialized array_list struct.
@@ -42,6 +55,14 @@ typedef struct RCUTILS_PUBLIC_TYPE rcutils_array_list_t
42
55
* Every instance of array_list_t has to either be zero_initialized with this
43
56
* function or manually allocated.
44
57
*
58
+ * <hr>
59
+ * Attribute | Adherence
60
+ * ------------------ | -------------
61
+ * Allocates Memory | No
62
+ * Thread-Safe | Yes
63
+ * Uses Atomics | No
64
+ * Lock-Free | Yes
65
+ *
45
66
* Example:
46
67
*
47
68
* ```c
@@ -56,25 +77,36 @@ RCUTILS_PUBLIC
56
77
rcutils_array_list_t
57
78
rcutils_get_zero_initialized_array_list (void );
58
79
59
- /// Initialize an array liast with a given initial capacity.
80
+ /// Initialize an array list with a given initial capacity.
60
81
/**
61
- * This function will initialize a given, zero initialized, string array to
82
+ * This function will initialize a given, zero initialized, array_list to
62
83
* a given size.
63
84
*
64
- * Note that putting a string into the array gives owenship to the array.
85
+ * <hr>
86
+ * Attribute | Adherence
87
+ * ------------------ | -------------
88
+ * Allocates Memory | Yes
89
+ * Thread-Safe | No
90
+ * Uses Atomics | No
91
+ * Lock-Free | Yes
65
92
*
66
93
* Example:
67
94
*
68
95
* ```c
69
96
* rcutils_allocator_t allocator = rcutils_get_default_allocator();
70
97
* rcutils_array_list_t array_list = rcutils_get_zero_initialized_array_list();
71
- * rcutils_ret_t ret = rcutils_array_list_init(&array_list, 2, &allocator);
98
+ * rcutils_ret_t ret = rcutils_array_list_init(&array_list, 2, sizeof(int), &allocator);
72
99
* if (ret != RCUTILS_RET_OK) {
73
100
* // ... error handling
74
101
* }
75
- * array_list.data[0] = rcutils_strdup("Hello", &allocator);
76
- * array_list.data[1] = rcutils_strdup("World", &allocator);
102
+ * int data = 42;
103
+ * int out_data = 0;
104
+ * ret = rcutils_array_list_add(&array_list, &data);
105
+ * data++;
106
+ * ret = rcutils_array_list_get(&array_list, 0, &out_data);
107
+ * assert(42 == out_data);
77
108
* ret = rcutils_array_list_fini(&array_list);
109
+ * ```
78
110
*
79
111
* \param[inout] array_list object to be initialized
80
112
* \param[in] initial_capacity the initial capacity to allocate in the list
@@ -84,7 +116,6 @@ rcutils_get_zero_initialized_array_list(void);
84
116
* \return `RCUTILS_RET_INVALID_ARGUMENT` for invalid arguments, or
85
117
* \return `RCUTILS_RET_BAD_ALLOC` if memory allocation fails, or
86
118
* \return `RCUTILS_RET_ERROR` if an unknown error occurs
87
- * ```
88
119
*/
89
120
RCUTILS_PUBLIC
90
121
rcutils_ret_t
@@ -94,13 +125,20 @@ rcutils_array_list_init(
94
125
size_t data_size ,
95
126
const rcutils_allocator_t * allocator );
96
127
97
- /// Finalize a string array, reclaiming all resources.
128
+ /// Finalize an array list , reclaiming all resources.
98
129
/**
99
- * This function reclaims any memory owned by the string array, including the
100
- * strings it references.
130
+ * This function reclaims any memory owned by the array list.
101
131
*
102
- * The allocator used to initialize the string array is used to deallocate each
103
- * string in the array and the array of strings itself.
132
+ * The allocator used to initialize the array list is used to deallocate each
133
+ * entry in the list and the list itself.
134
+ *
135
+ * <hr>
136
+ * Attribute | Adherence
137
+ * ------------------ | -------------
138
+ * Allocates Memory | No
139
+ * Thread-Safe | No
140
+ * Uses Atomics | No
141
+ * Lock-Free | Yes
104
142
*
105
143
* \param[inout] array_list object to be finalized
106
144
* \return `RCUTILS_RET_OK` if successful, or
@@ -114,10 +152,17 @@ rcutils_array_list_fini(rcutils_array_list_t * array_list);
114
152
115
153
/// Adds an entry to the list
116
154
/**
117
- * This functions adds the provided data to the end of the list. A copy of
155
+ * This function adds the provided data to the end of the list. A shallow copy of
118
156
* the provided data is made to store in the list instead of just storing
119
157
* the pointer to the provided data.
120
158
*
159
+ * <hr>
160
+ * Attribute | Adherence
161
+ * ------------------ | -------------
162
+ * Allocates Memory | Yes
163
+ * Thread-Safe | No
164
+ * Uses Atomics | No
165
+ * Lock-Free | Yes
121
166
*
122
167
* \param[in] array_list to add the data to
123
168
* \param[in] data a pointer to the data to add to the list
@@ -133,16 +178,24 @@ rcutils_array_list_add(rcutils_array_list_t * array_list, const void * data);
133
178
134
179
/// Sets an entry in the list to the provided data
135
180
/**
136
- * This functions sets the provided data at the specified index in the list. A copy of
137
- * the provided data is made to store in the list instead of just storing
138
- * the pointer to the provided data.
181
+ * This function sets the provided data at the specified index in the list.
182
+ * A shallow copy of the provided data is made to store in the list instead
183
+ * of just storing the pointer to the provided data.
139
184
*
185
+ * <hr>
186
+ * Attribute | Adherence
187
+ * ------------------ | -------------
188
+ * Allocates Memory | No
189
+ * Thread-Safe | No
190
+ * Uses Atomics | No
191
+ * Lock-Free | Yes
140
192
*
141
193
* \param[in] array_list to add the data to
142
194
* \param[in] index the position in the list to set the data
143
195
* \param[in] data a pointer to the data that will be set in the list
144
196
* \return `RCUTILS_RET_OK` if successful, or
145
197
* \return `RCUTILS_RET_INVALID_ARGUMENT` for invalid arguments, or
198
+ * \return `RCUTILS_RET_INVALID_ARGUMENT` if index out of bounds, or
146
199
* \return `RCUTILS_RET_BAD_ALLOC` if memory allocation fails, or
147
200
* \return `RCUTILS_RET_ERROR` if an unknown error occurs
148
201
*/
@@ -153,12 +206,21 @@ rcutils_array_list_set(rcutils_array_list_t * array_list, size_t index, const vo
153
206
154
207
/// Removes an entry in the list at the provided index
155
208
/**
156
- * This functions removes data from the list at the specified index.
209
+ * This function removes data from the list at the specified index. The capacity
210
+ * of the list will never decrease when entries are removed.
157
211
*
212
+ * <hr>
213
+ * Attribute | Adherence
214
+ * ------------------ | -------------
215
+ * Allocates Memory | No
216
+ * Thread-Safe | No
217
+ * Uses Atomics | No
218
+ * Lock-Free | Yes
158
219
*
159
220
* \param[in] array_list to add the data to
160
221
* \return `RCUTILS_RET_OK` if successful, or
161
222
* \return `RCUTILS_RET_INVALID_ARGUMENT` for invalid arguments, or
223
+ * \return `RCUTILS_RET_INVALID_ARGUMENT` if index out of bounds, or
162
224
* \return `RCUTILS_RET_BAD_ALLOC` if memory allocation fails, or
163
225
* \return `RCUTILS_RET_ERROR` if an unknown error occurs
164
226
*/
@@ -169,7 +231,15 @@ rcutils_array_list_remove(rcutils_array_list_t * array_list, size_t index);
169
231
170
232
/// Retrieves an entry in the list at the provided index
171
233
/**
172
- * This functions retrieves a copy of the data stored in the list.
234
+ * This function retrieves a copy of the data stored in the list at the provided index.
235
+ *
236
+ * <hr>
237
+ * Attribute | Adherence
238
+ * ------------------ | -------------
239
+ * Allocates Memory | No
240
+ * Thread-Safe | No
241
+ * Uses Atomics | No
242
+ * Lock-Free | Yes
173
243
*
174
244
*
175
245
* \param[in] array_list to add the data to
@@ -187,7 +257,15 @@ rcutils_array_list_get(const rcutils_array_list_t * array_list, size_t index, vo
187
257
188
258
/// Retrieves the size of the provided array_list
189
259
/**
190
- * This functions retrieves the size of the provided array list
260
+ * This function retrieves the number of items in the provided array list
261
+ *
262
+ * <hr>
263
+ * Attribute | Adherence
264
+ * ------------------ | -------------
265
+ * Allocates Memory | No
266
+ * Thread-Safe | No
267
+ * Uses Atomics | No
268
+ * Lock-Free | Yes
191
269
*
192
270
*
193
271
* \param[in] array_list list to get the size of
0 commit comments