Skip to content

Commit b4d8eaa

Browse files
committed
Add status return to array list modification functions
1 parent 78ff2f0 commit b4d8eaa

4 files changed

Lines changed: 61 additions & 35 deletions

File tree

libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) {
4646
//Then adding an element should fail
4747
EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addInt(list, 10));
4848
EXPECT_EQ(10, celix_arrayList_size(list));
49+
50+
celix_arrayList_destroy(list);
4951
}

libs/utils/include/celix_array_list.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,81 +244,90 @@ size_t celix_arrayList_getSize(const celix_array_list_t *list, int index);
244244
*
245245
* @param map The array list.
246246
* @param value The pointer value to add to the array list.
247+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
247248
*/
248249
CELIX_UTILS_EXPORT
249-
void celix_arrayList_add(celix_array_list_t *list, void* value);
250+
celix_status_t celix_arrayList_add(celix_array_list_t *list, void* value);
250251

251252
/**
252253
* @brief add pointer entry to the back of the array list.
253254
*
254255
* @param map The array list.
255256
* @param value The int value to add to the array list.
257+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
256258
*/
257259
CELIX_UTILS_EXPORT
258-
void celix_arrayList_addInt(celix_array_list_t *list, int value);
260+
celix_status_t celix_arrayList_addInt(celix_array_list_t *list, int value);
259261

260262
/**
261263
* @brief add pointer entry to the back of the array list.
262264
*
263265
* @param map The array list.
264266
* @param value The long value to add to the array list.
267+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
265268
*/
266269
CELIX_UTILS_EXPORT
267-
void celix_arrayList_addLong(celix_array_list_t *list, long value);
270+
celix_status_t celix_arrayList_addLong(celix_array_list_t *list, long value);
268271

269272
/**
270273
* @brief add pointer entry to the back of the array list.
271274
*
272275
* @param map The array list.
273276
* @param value The unsigned int value to add to the array list.
277+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
274278
*/
275279
CELIX_UTILS_EXPORT
276-
void celix_arrayList_addUInt(celix_array_list_t *list, unsigned int value);
280+
celix_status_t celix_arrayList_addUInt(celix_array_list_t *list, unsigned int value);
277281

278282
/**
279283
* @brief add pointer entry to the back of the array list.
280284
*
281285
* @param map The array list.
282286
* @param value The unsigned long value to add to the array list.
287+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
283288
*/
284289
CELIX_UTILS_EXPORT
285-
void celix_arrayList_addULong(celix_array_list_t *list, unsigned long value);
290+
celix_status_t celix_arrayList_addULong(celix_array_list_t *list, unsigned long value);
286291

287292
/**
288293
* @brief add pointer entry to the back of the array list.
289294
*
290295
* @param map The array list.
291296
* @param value The float value to add to the array list.
297+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
292298
*/
293299
CELIX_UTILS_EXPORT
294-
void celix_arrayList_addFloat(celix_array_list_t *list, float value);
300+
celix_status_t celix_arrayList_addFloat(celix_array_list_t *list, float value);
295301

296302
/**
297303
* @brief add pointer entry to the back of the array list.
298304
*
299305
* @param map The array list.
300306
* @param value The double value to add to the array list.
307+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
301308
*/
302309
CELIX_UTILS_EXPORT
303-
void celix_arrayList_addDouble(celix_array_list_t *list, double value);
310+
celix_status_t celix_arrayList_addDouble(celix_array_list_t *list, double value);
304311

305312
/**
306313
* @brief add pointer entry to the back of the array list.
307314
*
308315
* @param map The array list.
309316
* @param value The bool value to add to the array list.
317+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
310318
*/
311319
CELIX_UTILS_EXPORT
312-
void celix_arrayList_addBool(celix_array_list_t *list, bool value);
320+
celix_status_t celix_arrayList_addBool(celix_array_list_t *list, bool value);
313321

314322
/**
315323
* @brief add pointer entry to the back of the array list.
316324
*
317325
* @param map The array list.
318326
* @param value The size_t value to add to the array list.
327+
* @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory.
319328
*/
320329
CELIX_UTILS_EXPORT
321-
void celix_arrayList_addSize(celix_array_list_t *list, size_t value);
330+
celix_status_t celix_arrayList_addSize(celix_array_list_t *list, size_t value);
322331

323332
/**
324333
* @brief Returns the index of the provided entry, if found.

libs/utils/include_deprecated/array_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CELIX_UTILS_DEPRECATED_EXPORT void arrayList_destroy(celix_array_list_t *list);
4747

4848
CELIX_UTILS_DEPRECATED_EXPORT void arrayList_trimToSize(celix_array_list_t *list);
4949

50-
CELIX_UTILS_DEPRECATED_EXPORT void arrayList_ensureCapacity(celix_array_list_t *list, int capacity);
50+
CELIX_UTILS_DEPRECATED_EXPORT celix_status_t arrayList_ensureCapacity(celix_array_list_t *list, int capacity);
5151

5252
CELIX_UTILS_DEPRECATED_EXPORT unsigned int arrayList_size(celix_array_list_t *list);
5353

libs/utils/src/array_list.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ void arrayList_trimToSize(array_list_pt list) {
9090
}
9191
}
9292

93-
void arrayList_ensureCapacity(array_list_pt list, int capacity) {
93+
celix_status_t arrayList_ensureCapacity(array_list_pt list, int capacity) {
94+
celix_status_t status = CELIX_SUCCESS;
9495
celix_array_list_entry_t *newList;
9596
list->modCount++;
9697
size_t oldCapacity = list->capacity;
@@ -100,9 +101,13 @@ void arrayList_ensureCapacity(array_list_pt list, int capacity) {
100101
newCapacity = capacity;
101102
}
102103
newList = realloc(list->elementData, sizeof(celix_array_list_entry_t) * newCapacity);
103-
list->capacity = newCapacity;
104-
list->elementData = newList;
104+
if (newList != NULL) {
105+
list->capacity = newCapacity;
106+
list->elementData = newList;
107+
}
108+
status = newList == NULL ? CELIX_ENOMEM : CELIX_SUCCESS;
105109
}
110+
return status;
106111
}
107112

108113
unsigned int arrayList_size(array_list_pt list) {
@@ -280,7 +285,7 @@ array_list_pt arrayList_clone(array_list_pt list) {
280285
// arrayList_ensureCapacity(new, list->size);
281286
// memcpy(new->elementData, list->elementData, list->size);
282287
// new->size = list->size;
283-
288+
284289
for (i = 0; i < arrayList_size(list); i++) {
285290
arrayList_add(new, arrayList_get(list, i));
286291
}
@@ -420,65 +425,75 @@ double celix_arrayList_getDouble(const celix_array_list_t *list, int index) { re
420425
bool celix_arrayList_getBool(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).boolVal; }
421426
size_t celix_arrayList_getSize(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).sizeVal; }
422427

423-
static void arrayList_addEntry(celix_array_list_t *list, celix_array_list_entry_t entry) {
424-
arrayList_ensureCapacity(list, (int)list->size + 1);
425-
list->elementData[list->size++] = entry;
428+
static celix_status_t celix_arrayList_addEntry(celix_array_list_t *list, celix_array_list_entry_t entry) {
429+
celix_status_t status = arrayList_ensureCapacity(list, (int)list->size + 1);
430+
if (status == CELIX_SUCCESS) {
431+
list->elementData[list->size++] = entry;
432+
}
433+
return status;
426434
}
427435

428-
void celix_arrayList_add(celix_array_list_t *list, void * element) {
436+
celix_status_t celix_arrayList_add(celix_array_list_t *list, void * element) {
429437
celix_array_list_entry_t entry;
430438
memset(&entry, 0, sizeof(entry));
431439
entry.voidPtrVal = element;
432-
arrayList_addEntry(list, entry);
440+
return celix_arrayList_addEntry(list, entry);
433441
}
434442

435-
void celix_arrayList_addInt(celix_array_list_t *list, int val) {
443+
celix_status_t celix_arrayList_addInt(celix_array_list_t *list, int val) {
436444
celix_array_list_entry_t entry;
437445
memset(&entry, 0, sizeof(entry));
438446
entry.intVal = val;
439-
arrayList_addEntry(list, entry);
447+
return celix_arrayList_addEntry(list, entry);
440448
}
441-
void celix_arrayList_addLong(celix_array_list_t *list, long val) {
449+
450+
celix_status_t celix_arrayList_addLong(celix_array_list_t *list, long val) {
442451
celix_array_list_entry_t entry;
443452
memset(&entry, 0, sizeof(entry));
444453
entry.longVal = val;
445-
arrayList_addEntry(list, entry);
454+
return celix_arrayList_addEntry(list, entry);
446455
}
447-
void celix_arrayList_addUInt(celix_array_list_t *list, unsigned int val) {
456+
457+
celix_status_t celix_arrayList_addUInt(celix_array_list_t *list, unsigned int val) {
448458
celix_array_list_entry_t entry;
449459
memset(&entry, 0, sizeof(entry));
450460
entry.uintVal = val;
451-
arrayList_addEntry(list, entry);
461+
return celix_arrayList_addEntry(list, entry);
452462
}
453-
void celix_arrayList_addULong(celix_array_list_t *list, unsigned long val) {
463+
464+
celix_status_t celix_arrayList_addULong(celix_array_list_t *list, unsigned long val) {
454465
celix_array_list_entry_t entry;
455466
memset(&entry, 0, sizeof(entry));
456467
entry.ulongVal = val;
457-
arrayList_addEntry(list, entry);
468+
return celix_arrayList_addEntry(list, entry);
458469
}
459-
void celix_arrayList_addDouble(celix_array_list_t *list, double val) {
470+
471+
celix_status_t celix_arrayList_addDouble(celix_array_list_t *list, double val) {
460472
celix_array_list_entry_t entry;
461473
memset(&entry, 0, sizeof(entry));
462474
entry.doubleVal = val;
463-
arrayList_addEntry(list, entry);
475+
return celix_arrayList_addEntry(list, entry);
464476
}
465-
void celix_arrayList_addFloat(celix_array_list_t *list, float val) {
477+
478+
celix_status_t celix_arrayList_addFloat(celix_array_list_t *list, float val) {
466479
celix_array_list_entry_t entry;
467480
memset(&entry, 0, sizeof(entry));
468481
entry.floatVal = val;
469-
arrayList_addEntry(list, entry);
482+
return celix_arrayList_addEntry(list, entry);
470483
}
471-
void celix_arrayList_addBool(celix_array_list_t *list, bool val) {
484+
485+
celix_status_t celix_arrayList_addBool(celix_array_list_t *list, bool val) {
472486
celix_array_list_entry_t entry;
473487
memset(&entry, 0, sizeof(entry));
474488
entry.boolVal = val;
475-
arrayList_addEntry(list, entry);
489+
return celix_arrayList_addEntry(list, entry);
476490
}
477-
void celix_arrayList_addSize(celix_array_list_t *list, size_t val) {
491+
492+
celix_status_t celix_arrayList_addSize(celix_array_list_t *list, size_t val) {
478493
celix_array_list_entry_t entry;
479494
memset(&entry, 0, sizeof(entry));
480495
entry.sizeVal = val;
481-
arrayList_addEntry(list, entry);
496+
return celix_arrayList_addEntry(list, entry);
482497
}
483498

484499
int celix_arrayList_indexOf(celix_array_list_t *list, celix_array_list_entry_t entry) {

0 commit comments

Comments
 (0)