This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/array_list_return_status_on_modification in repository https://gitbox.apache.org/repos/asf/celix.git
The following commit(s) were added to refs/heads/feature/array_list_return_status_on_modification by this push: new b4d8eaac Add status return to array list modification functions b4d8eaac is described below commit b4d8eaac8e40e0141de9f3ecc7cf3b4410f353dc Author: Pepijn Noltes <pepijnnol...@gmail.com> AuthorDate: Tue May 2 13:18:17 2023 +0200 Add status return to array list modification functions --- .../gtest/src/ArrayListErrorInjectionTestSuite.cc | 2 + libs/utils/include/celix_array_list.h | 27 ++++++--- libs/utils/include_deprecated/array_list.h | 2 +- libs/utils/src/array_list.c | 65 +++++++++++++--------- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc index 6e17fbf8..1d3d3a7d 100644 --- a/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc @@ -46,4 +46,6 @@ TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) { //Then adding an element should fail EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addInt(list, 10)); EXPECT_EQ(10, celix_arrayList_size(list)); + + celix_arrayList_destroy(list); } diff --git a/libs/utils/include/celix_array_list.h b/libs/utils/include/celix_array_list.h index b0a37cbe..6686e7e3 100644 --- a/libs/utils/include/celix_array_list.h +++ b/libs/utils/include/celix_array_list.h @@ -244,81 +244,90 @@ size_t celix_arrayList_getSize(const celix_array_list_t *list, int index); * * @param map The array list. * @param value The pointer value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_add(celix_array_list_t *list, void* value); +celix_status_t celix_arrayList_add(celix_array_list_t *list, void* value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The int value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addInt(celix_array_list_t *list, int value); +celix_status_t celix_arrayList_addInt(celix_array_list_t *list, int value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The long value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addLong(celix_array_list_t *list, long value); +celix_status_t celix_arrayList_addLong(celix_array_list_t *list, long value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The unsigned int value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addUInt(celix_array_list_t *list, unsigned int value); +celix_status_t celix_arrayList_addUInt(celix_array_list_t *list, unsigned int value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The unsigned long value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addULong(celix_array_list_t *list, unsigned long value); +celix_status_t celix_arrayList_addULong(celix_array_list_t *list, unsigned long value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The float value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addFloat(celix_array_list_t *list, float value); +celix_status_t celix_arrayList_addFloat(celix_array_list_t *list, float value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The double value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addDouble(celix_array_list_t *list, double value); +celix_status_t celix_arrayList_addDouble(celix_array_list_t *list, double value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The bool value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addBool(celix_array_list_t *list, bool value); +celix_status_t celix_arrayList_addBool(celix_array_list_t *list, bool value); /** * @brief add pointer entry to the back of the array list. * * @param map The array list. * @param value The size_t value to add to the array list. + * @return CELIX_SUCCESS if the value is added, CELIX_ENOMEM if the array list is out of memory. */ CELIX_UTILS_EXPORT -void celix_arrayList_addSize(celix_array_list_t *list, size_t value); +celix_status_t celix_arrayList_addSize(celix_array_list_t *list, size_t value); /** * @brief Returns the index of the provided entry, if found. diff --git a/libs/utils/include_deprecated/array_list.h b/libs/utils/include_deprecated/array_list.h index 1abaf16e..b42a4836 100644 --- a/libs/utils/include_deprecated/array_list.h +++ b/libs/utils/include_deprecated/array_list.h @@ -47,7 +47,7 @@ CELIX_UTILS_DEPRECATED_EXPORT void arrayList_destroy(celix_array_list_t *list); CELIX_UTILS_DEPRECATED_EXPORT void arrayList_trimToSize(celix_array_list_t *list); -CELIX_UTILS_DEPRECATED_EXPORT void arrayList_ensureCapacity(celix_array_list_t *list, int capacity); +CELIX_UTILS_DEPRECATED_EXPORT celix_status_t arrayList_ensureCapacity(celix_array_list_t *list, int capacity); CELIX_UTILS_DEPRECATED_EXPORT unsigned int arrayList_size(celix_array_list_t *list); diff --git a/libs/utils/src/array_list.c b/libs/utils/src/array_list.c index 27c32010..a90b8cce 100644 --- a/libs/utils/src/array_list.c +++ b/libs/utils/src/array_list.c @@ -90,7 +90,8 @@ void arrayList_trimToSize(array_list_pt list) { } } -void arrayList_ensureCapacity(array_list_pt list, int capacity) { +celix_status_t arrayList_ensureCapacity(array_list_pt list, int capacity) { + celix_status_t status = CELIX_SUCCESS; celix_array_list_entry_t *newList; list->modCount++; size_t oldCapacity = list->capacity; @@ -100,9 +101,13 @@ void arrayList_ensureCapacity(array_list_pt list, int capacity) { newCapacity = capacity; } newList = realloc(list->elementData, sizeof(celix_array_list_entry_t) * newCapacity); - list->capacity = newCapacity; - list->elementData = newList; + if (newList != NULL) { + list->capacity = newCapacity; + list->elementData = newList; + } + status = newList == NULL ? CELIX_ENOMEM : CELIX_SUCCESS; } + return status; } unsigned int arrayList_size(array_list_pt list) { @@ -280,7 +285,7 @@ array_list_pt arrayList_clone(array_list_pt list) { // arrayList_ensureCapacity(new, list->size); // memcpy(new->elementData, list->elementData, list->size); // new->size = list->size; - + for (i = 0; i < arrayList_size(list); i++) { arrayList_add(new, arrayList_get(list, i)); } @@ -420,65 +425,75 @@ double celix_arrayList_getDouble(const celix_array_list_t *list, int index) { re bool celix_arrayList_getBool(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).boolVal; } size_t celix_arrayList_getSize(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).sizeVal; } -static void arrayList_addEntry(celix_array_list_t *list, celix_array_list_entry_t entry) { - arrayList_ensureCapacity(list, (int)list->size + 1); - list->elementData[list->size++] = entry; +static celix_status_t celix_arrayList_addEntry(celix_array_list_t *list, celix_array_list_entry_t entry) { + celix_status_t status = arrayList_ensureCapacity(list, (int)list->size + 1); + if (status == CELIX_SUCCESS) { + list->elementData[list->size++] = entry; + } + return status; } -void celix_arrayList_add(celix_array_list_t *list, void * element) { +celix_status_t celix_arrayList_add(celix_array_list_t *list, void * element) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.voidPtrVal = element; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addInt(celix_array_list_t *list, int val) { +celix_status_t celix_arrayList_addInt(celix_array_list_t *list, int val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.intVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addLong(celix_array_list_t *list, long val) { + +celix_status_t celix_arrayList_addLong(celix_array_list_t *list, long val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.longVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addUInt(celix_array_list_t *list, unsigned int val) { + +celix_status_t celix_arrayList_addUInt(celix_array_list_t *list, unsigned int val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.uintVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addULong(celix_array_list_t *list, unsigned long val) { + +celix_status_t celix_arrayList_addULong(celix_array_list_t *list, unsigned long val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.ulongVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addDouble(celix_array_list_t *list, double val) { + +celix_status_t celix_arrayList_addDouble(celix_array_list_t *list, double val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.doubleVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addFloat(celix_array_list_t *list, float val) { + +celix_status_t celix_arrayList_addFloat(celix_array_list_t *list, float val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.floatVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addBool(celix_array_list_t *list, bool val) { + +celix_status_t celix_arrayList_addBool(celix_array_list_t *list, bool val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.boolVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } -void celix_arrayList_addSize(celix_array_list_t *list, size_t val) { + +celix_status_t celix_arrayList_addSize(celix_array_list_t *list, size_t val) { celix_array_list_entry_t entry; memset(&entry, 0, sizeof(entry)); entry.sizeVal = val; - arrayList_addEntry(list, entry); + return celix_arrayList_addEntry(list, entry); } int celix_arrayList_indexOf(celix_array_list_t *list, celix_array_list_entry_t entry) {