From 8af7bff25226d4bc915566fcf07c43e5742f4a7b Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 23 Dec 2023 17:11:54 +0100 Subject: [PATCH 01/18] Remove cpputest config, cpputest usage and linked list Also refactor shell tui history to use array list instead of linked list. --- bundles/shell/shell_tui/gtest/CMakeLists.txt | 1 + .../shell_tui/gtest/src/ShellTuiTestSuite.cc | 36 ++++-- bundles/shell/shell_tui/src/activator.c | 2 +- bundles/shell/shell_tui/src/history.c | 111 +++++++++++------- bundles/shell/shell_tui/src/history.h | 26 ++-- bundles/shell/shell_tui/src/shell_tui.c | 41 ++++--- bundles/shell/shell_tui/src/shell_tui.h | 6 +- cmake/Modules/FindCppUTest.cmake | 69 ----------- libs/framework/include_deprecated/bundle.h | 2 - libs/framework/include_deprecated/module.h | 9 -- libs/framework/src/bundle.c | 38 ++---- libs/framework/src/bundle_archive.c | 1 - libs/framework/src/framework.c | 1 - libs/framework/src/manifest_parser.h | 1 - libs/framework/src/module.c | 16 --- libs/utils/CMakeLists.txt | 39 ------ libs/utils/include_deprecated/linked_list.h | 92 --------------- .../include_deprecated/linked_list_iterator.h | 66 ----------- 18 files changed, 142 insertions(+), 415 deletions(-) delete mode 100644 cmake/Modules/FindCppUTest.cmake delete mode 100644 libs/utils/include_deprecated/linked_list.h delete mode 100644 libs/utils/include_deprecated/linked_list_iterator.h diff --git a/bundles/shell/shell_tui/gtest/CMakeLists.txt b/bundles/shell/shell_tui/gtest/CMakeLists.txt index 76f201cfd..79cf1ea7b 100644 --- a/bundles/shell/shell_tui/gtest/CMakeLists.txt +++ b/bundles/shell/shell_tui/gtest/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(test_shell_tui target_link_libraries(test_shell_tui PRIVATE Celix::framework GTest::gtest GTest::gtest_main) add_celix_bundle_dependencies(test_shell_tui Celix::shell Celix::shell_tui) +target_include_directories(test_shell_tui PRIVATE ../src) target_compile_definitions(test_shell_tui PRIVATE -DSHELL_BUNDLE_LOCATION=\"$\") target_compile_definitions(test_shell_tui PRIVATE -DSHELL_TUI_BUNDLE_LOCATION=\"$\") diff --git a/bundles/shell/shell_tui/gtest/src/ShellTuiTestSuite.cc b/bundles/shell/shell_tui/gtest/src/ShellTuiTestSuite.cc index ca15a4f63..5cde351e0 100644 --- a/bundles/shell/shell_tui/gtest/src/ShellTuiTestSuite.cc +++ b/bundles/shell/shell_tui/gtest/src/ShellTuiTestSuite.cc @@ -22,6 +22,7 @@ #include "celix/FrameworkFactory.h" #include "celix/BundleContext.h" +#include "history.h" class ShellTuiTestSuite : public ::testing::Test { public: @@ -144,41 +145,40 @@ class ShellTuiTestSuite : public ::testing::Test { int outputWriteFd = -1; }; -TEST_F(ShellTuiTestSuite, testStartStop) { +TEST_F(ShellTuiTestSuite, StartStopTest) { //simple start/stop bundles, but should not leak createFrameworkWithShellBundles(); } -TEST_F(ShellTuiTestSuite, testExecuteLb) { +TEST_F(ShellTuiTestSuite, ExecuteLbTest) { testExecuteLb(false); } -TEST_F(ShellTuiTestSuite, testExecuteLbWithAnsiControlEnabled) { +TEST_F(ShellTuiTestSuite, ExecuteLbWithAnsiControlEnabledTest) { testExecuteLb(true); } -TEST_F(ShellTuiTestSuite, testAutoCompleteHelpCommand) { +TEST_F(ShellTuiTestSuite, AutoCompleteHelpCommandTest) { //note incomplete command with a tab -> should complete command to `help` testAutoCompleteForCommand("hel\t", "help"); } -TEST_F(ShellTuiTestSuite, testAutoCompleteCelixLbCommand) { +TEST_F(ShellTuiTestSuite, AutoCompleteCelixLbCommandTest) { //note incomplete command with a tab -> should complete command to `celix::help` testAutoCompleteForCommand("celix::hel\t", "celix::help"); } -TEST_F(ShellTuiTestSuite, testAutoCompleteLbUsageCommand) { +TEST_F(ShellTuiTestSuite, AutoCompleteLbUsageCommandTest) { //note complete help command with a tab -> should print usage testAutoCompleteForCommand("help \t", "Usage:"); } -TEST_F(ShellTuiTestSuite, testAutoCompleteCelixLbUsageCommand) { +TEST_F(ShellTuiTestSuite, AutoCompleteCelixLbUsageCommandTest) { //note complete celix::help command with a tab -> should print usage testAutoCompleteForCommand("celix::help \t", "Usage:"); } - -TEST_F(ShellTuiTestSuite, testShellTuiWithInvalidFD) { +TEST_F(ShellTuiTestSuite, ShellTuiWithInvalidFDTest) { celix::Properties config{ {"SHELL_TUI_USE_ANSI_CONTROL_SEQUENCES", "true"}, {"SHELL_TUI_INPUT_FILE_DESCRIPTOR", "555"}, //note invalid fd @@ -190,15 +190,15 @@ TEST_F(ShellTuiTestSuite, testShellTuiWithInvalidFD) { writeCmd("lb\n"); } -TEST_F(ShellTuiTestSuite, testShellTuiWithoutShell) { +TEST_F(ShellTuiTestSuite, ShellTuiWithoutShellTest) { testExecuteLbWithoutShell(false); } -TEST_F(ShellTuiTestSuite, testShellTuiWithAnsiControlWithoutShell) { +TEST_F(ShellTuiTestSuite, ShellTuiWithAnsiControlWithoutShellTest) { testExecuteLbWithoutShell(true); } -TEST_F(ShellTuiTestSuite, testAnsiControl) { +TEST_F(ShellTuiTestSuite, AnsiControlTest) { celix::Properties config{ {"SHELL_TUI_USE_ANSI_CONTROL_SEQUENCES", "true"} }; @@ -235,4 +235,14 @@ TEST_F(ShellTuiTestSuite, testAnsiControl) { writeCmd(cmd); std::cout << readPipeOutput() << std::endl; -} \ No newline at end of file +} + +TEST_F(ShellTuiTestSuite, WriteMoreThanMaxHistoryLines) { + createFrameworkWithShellBundles(); + + //WRITE more than hist max lines + int maxHist = CELIX_SHELL_TUI_HIST_MAX; + for (int i = 0; i < maxHist + 1; ++i) { + writeCmd("lb\n"); + } +} diff --git a/bundles/shell/shell_tui/src/activator.c b/bundles/shell/shell_tui/src/activator.c index 3ae833d59..fed534405 100644 --- a/bundles/shell/shell_tui/src/activator.c +++ b/bundles/shell/shell_tui/src/activator.c @@ -70,7 +70,7 @@ static celix_status_t celix_shellTuiActivator_start(celix_shell_tui_activator_t* char *term = getenv("TERM"); useCommands = term != NULL; //if TERM exist, default is to use commands useCommands = celix_bundleContext_getPropertyAsBool(ctx, SHELL_TUI_USE_ANSI_CONTROL_SEQUENCES, useCommands); - act->shellTui = shellTui_create(useCommands, inputFd, outputFd, errorFd); + act->shellTui = shellTui_create(ctx, useCommands, inputFd, outputFd, errorFd); { celix_service_tracking_options_t opts = CELIX_EMPTY_SERVICE_TRACKING_OPTIONS; diff --git a/bundles/shell/shell_tui/src/history.c b/bundles/shell/shell_tui/src/history.c index 2c034f5f7..6a0f80510 100644 --- a/bundles/shell/shell_tui/src/history.c +++ b/bundles/shell/shell_tui/src/history.c @@ -17,64 +17,91 @@ * under the License. */ -#include "history.h" #include #include -#include "linked_list.h" -#define HIST_SIZE 32 +#include "celix_array_list.h" +#include "celix_bundle_context.h" +#include "celix_stdlib_cleanup.h" +#include "celix_utils.h" +#include "history.h" -struct history { - linked_list_pt history_lines; - int currentLine; +struct celix_shell_tui_history { + celix_bundle_context_t* ctx; + celix_array_list_t* historyLines; + int currentLine; }; -history_t *historyCreate() { - history_t* hist = calloc(1, sizeof(*hist)); - linkedList_create(&hist->history_lines); - hist->currentLine = -1; - return hist; -} +celix_shell_tui_history_t* celix_shellTuiHistory_create(celix_bundle_context_t* ctx) { + celix_autofree celix_shell_tui_history_t* hist = calloc(1, sizeof(*hist)); + if (hist) { + hist->ctx = ctx; + celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS; + opts.simpleRemovedCallback = free; + hist->historyLines = celix_arrayList_createWithOptions(&opts); + hist->currentLine = -1; + } -void historyDestroy(history_t *hist) { - unsigned int size = linkedList_size(hist->history_lines); - for(unsigned int i = 0; i < size; i++) { - char *line = linkedList_get(hist->history_lines, i); - free(line); - } - linkedList_destroy(hist->history_lines); - free(hist); + if (!hist || !hist->historyLines) { + celix_bundleContext_log(ctx, CELIX_LOG_LEVEL_ERROR, "Error creating history"); + return NULL; + } + + return celix_steal_ptr(hist); } -void history_addLine(history_t *hist, const char *line) { - linkedList_addFirst(hist->history_lines, strdup(line)); - if(linkedList_size(hist->history_lines) == HIST_SIZE) { - char *lastLine = (char*)linkedList_get(hist->history_lines, HIST_SIZE-1); - free(lastLine); - linkedList_removeIndex(hist->history_lines, HIST_SIZE-1); - } +void celix_shellTuiHistory_destroy(celix_shell_tui_history_t* hist) { + if (hist) { + celix_arrayList_destroy(hist->historyLines); + free(hist); + } } -char *historyGetPrevLine(history_t *hist) { - hist->currentLine = (hist->currentLine + 1) % linkedList_size(hist->history_lines); - return (char*)linkedList_get(hist->history_lines, hist->currentLine); +void celix_shellTuiHistory_addLine(celix_shell_tui_history_t* hist, const char* line) { + if (line == NULL || strlen(line) == 0) { + return; //ignore empty lines + } + celix_autofree char* lineCopy = celix_utils_strdup(line); + celix_status_t status = CELIX_SUCCESS; + if (lineCopy) { + status = celix_arrayList_add(hist->historyLines, lineCopy); + if (celix_arrayList_size(hist->historyLines) > CELIX_SHELL_TUI_HIST_MAX) { + celix_arrayList_removeAt(hist->historyLines, 0); + } + } + + if (!lineCopy || status != CELIX_SUCCESS) { + celix_bundleContext_log(hist->ctx, CELIX_LOG_LEVEL_ERROR, "Error adding line to history"); + } else { + hist->currentLine = celix_arrayList_size(hist->historyLines); + celix_steal_ptr(lineCopy); + } } -char *historyGetNextLine(history_t *hist) { - if(linkedList_size(hist->history_lines) > 0) { - if (hist->currentLine <= 0) { - hist->currentLine = linkedList_size(hist->history_lines) - 1; - } else { - hist->currentLine--; - } +const char* celix_shellTuiHistory_getPrevLine(celix_shell_tui_history_t* hist) { + hist->currentLine--; + if (hist->currentLine < 0) { + hist->currentLine = 0; } - return (char*)linkedList_get(hist->history_lines, hist->currentLine); + int size = celix_arrayList_size(hist->historyLines); + if (size > 0) { + return celix_arrayList_get(hist->historyLines, hist->currentLine); + } + return NULL; } -void historyLineReset(history_t *hist) { - hist->currentLine = -1; +const char* celix_shellTuiHistory_getNextLine(celix_shell_tui_history_t* hist) { + hist->currentLine++; + int size = celix_arrayList_size(hist->historyLines); + if (hist->currentLine >= size) { + hist->currentLine = size - 1; + } + if (size > 0) { + return celix_arrayList_get(hist->historyLines, hist->currentLine); + } + return NULL; } -unsigned int historySize(history_t *hist) { - return linkedList_size(hist->history_lines); +void celix_shellTuiHistory_lineReset(celix_shell_tui_history_t* hist) { + hist->currentLine = celix_arrayList_size(hist->historyLines); } diff --git a/bundles/shell/shell_tui/src/history.h b/bundles/shell/shell_tui/src/history.h index c45790be3..22e3f8072 100644 --- a/bundles/shell/shell_tui/src/history.h +++ b/bundles/shell/shell_tui/src/history.h @@ -16,25 +16,21 @@ * specific language governing permissions and limitations * under the License. */ -/** - * history.h - * - * \date Jan 16, 2016 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ #ifndef SHELL_TUI_HISTORY #define SHELL_TUI_HISTORY -typedef struct history history_t; +#include "celix_bundle_context.h" + +#define CELIX_SHELL_TUI_HIST_MAX 32 + +typedef struct celix_shell_tui_history celix_shell_tui_history_t; -history_t *historyCreate(); -void historyDestroy(history_t *hist); -void history_addLine(history_t *hist, const char *line); -char *historyGetPrevLine(history_t *hist); -char *historyGetNextLine(history_t *hist); -void historyLineReset(history_t *hist); -unsigned int historySize(history_t *hist); +celix_shell_tui_history_t* celix_shellTuiHistory_create(celix_bundle_context_t* ctx); +void celix_shellTuiHistory_destroy(celix_shell_tui_history_t* hist); +void celix_shellTuiHistory_addLine(celix_shell_tui_history_t* hist, const char* line); +const char* celix_shellTuiHistory_getPrevLine(celix_shell_tui_history_t* hist); +const char* celix_shellTuiHistory_getNextLine(celix_shell_tui_history_t* hist); +void celix_shellTuiHistory_lineReset(celix_shell_tui_history_t* hist); #endif // SHELL_TUI_HISTORY diff --git a/bundles/shell/shell_tui/src/shell_tui.c b/bundles/shell/shell_tui/src/shell_tui.c index 3f284db75..a370ec93f 100644 --- a/bundles/shell/shell_tui/src/shell_tui.c +++ b/bundles/shell/shell_tui/src/shell_tui.c @@ -51,6 +51,8 @@ const char * const SHELL_NOT_AVAILABLE_MSG = "[Shell TUI] Shell service not available."; struct shell_tui { + celix_bundle_context_t* ctx; + celix_thread_mutex_t mutex; //protects shell celix_shell_t* shell; celix_thread_t thread; @@ -70,7 +72,7 @@ typedef struct shell_context { char buffer[LINE_SIZE+1]; char dline[LINE_SIZE+1]; int pos; - history_t* hist; + celix_shell_tui_history_t* hist; } shell_context_t; struct OriginalSettings { @@ -98,8 +100,9 @@ static void writePrompt(shell_tui_t*); // Unfortunately has to be static, it is not possible to pass user defined data to the handler static struct OriginalSettings originalSettings; -shell_tui_t* shellTui_create(bool useAnsiControlSequences, int inputFd, int outputFd, int errorFd) { +shell_tui_t* shellTui_create(celix_bundle_context_t* ctx, bool useAnsiControlSequences, int inputFd, int outputFd, int errorFd) { shell_tui_t* result = calloc(1, sizeof(*result)); + result->ctx = ctx; result->inputFd = inputFd; result->output = outputFd == STDOUT_FILENO ? stdout : fdopen(outputFd, "a"); if (result->output == NULL) { @@ -183,7 +186,7 @@ static void* shellTui_runnable(void *data) { //setup shell context shell_context_t ctx; memset(&ctx, 0, sizeof(ctx)); - ctx.hist = historyCreate(); + ctx.hist = celix_shellTuiHistory_create(shellTui->ctx); struct termios term_new; if (shellTui->useAnsiControlSequences && shellTui->inputFd == STDIN_FILENO) { @@ -246,7 +249,7 @@ static void* shellTui_runnable(void *data) { } } - historyDestroy(ctx.hist); + celix_shellTuiHistory_destroy(ctx.hist); if (shellTui->useAnsiControlSequences && shellTui->inputFd == STDIN_FILENO) { tcsetattr(STDIN_FILENO, TCSANOW, &originalSettings.term_org); sigaction(SIGINT, &originalSettings.oldSigIntAction, NULL); @@ -303,26 +306,28 @@ static int shellTui_parseInputForControl(shell_tui_t* shellTui, shell_context_t* char* buffer = ctx->buffer; char* in = ctx->in; char* dline = ctx->dline; - history_t* hist = ctx->hist; + celix_shell_tui_history_t* hist = ctx->hist; int pos = ctx->pos; - char* line = NULL; + const char* line = NULL; - int nr_chars = read(shellTui->inputFd, buffer, LINE_SIZE-pos-1); + int nr_chars = (int)read(shellTui->inputFd, buffer, LINE_SIZE-pos-1); for(int bufpos = 0; bufpos < nr_chars; bufpos++) { if (buffer[bufpos] == KEY_ESC1 && buffer[bufpos+1] == KEY_ESC2) { switch (buffer[bufpos+2]) { case KEY_UP: - if(historySize(hist) > 0) { - strncpy(in, historyGetPrevLine(hist), LINE_SIZE); - pos = strlen(in); - writeLine(shellTui, in, pos); + line = celix_shellTuiHistory_getPrevLine(hist); + if (line) { + strncpy(in, line, LINE_SIZE); + pos = (int)strlen(in); + writeLine(shellTui, line, pos); } break; case KEY_DOWN: - if(historySize(hist) > 0) { - strncpy(in, historyGetNextLine(hist), LINE_SIZE); - pos = strlen(in); - writeLine(shellTui, in, pos); + line = celix_shellTuiHistory_getNextLine(hist); + if (line) { + strncpy(in, line, LINE_SIZE); + pos = (int)strlen(in); + writeLine(shellTui, line, pos); } break; case KEY_RIGHT: @@ -385,7 +390,7 @@ static int shellTui_parseInputForControl(shell_tui_t* shellTui, shell_context_t* writeLine(shellTui, in, pos); fprintf(shellTui->output, "\n"); remove_newlines(in); - history_addLine(hist, in); + celix_shellTuiHistory_addLine(hist, in); memset(dline, 0, LINE_SIZE); strncpy(dline, in, LINE_SIZE); @@ -397,7 +402,7 @@ static int shellTui_parseInputForControl(shell_tui_t* shellTui, shell_context_t* if ((strlen(line) == 0)) { continue; } - historyLineReset(hist); + celix_shellTuiHistory_lineReset(hist); celixThreadMutex_lock(&shellTui->mutex); if (shellTui->shell != NULL) { shellTui->shell->executeCommand(shellTui->shell->handle, line, shellTui->output, shellTui->error); @@ -440,7 +445,7 @@ static void writePrompt(shell_tui_t* shellTui) { static void writeLine(shell_tui_t* shellTui, const char* line, int pos) { clearLine(shellTui); - fwrite( PROMPT, 1, strlen(PROMPT), shellTui->output); + fwrite(PROMPT, 1, strlen(PROMPT), shellTui->output); fwrite(line, 1, strlen(line), shellTui->output); cursorLeft(shellTui, strlen(line)-pos); } diff --git a/bundles/shell/shell_tui/src/shell_tui.h b/bundles/shell/shell_tui/src/shell_tui.h index f15e8832b..b78d5aea9 100644 --- a/bundles/shell/shell_tui/src/shell_tui.h +++ b/bundles/shell/shell_tui/src/shell_tui.h @@ -22,8 +22,9 @@ #include -#include "celix_threads.h" +#include "celix_bundle_context.h" #include "celix_shell.h" +#include "celix_threads.h" typedef struct shell_tui shell_tui_t ; @@ -35,7 +36,8 @@ typedef struct shell_tui shell_tui_t ; * @param outputFd The output file descriptor to use. * @param errorFd The error output file descriptor to use. */ -shell_tui_t* shellTui_create(bool useAnsiControlSequences, int inputFd, int outputFd, int errorFd); +shell_tui_t* +shellTui_create(celix_bundle_context_t* ctx, bool useAnsiControlSequences, int inputFd, int outputFd, int errorFd); /** * @brief Start the shell tui and the thread reading the tty and optional extra read file descriptor. diff --git a/cmake/Modules/FindCppUTest.cmake b/cmake/Modules/FindCppUTest.cmake deleted file mode 100644 index b747b7d12..000000000 --- a/cmake/Modules/FindCppUTest.cmake +++ /dev/null @@ -1,69 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -FIND_PATH(CppUTest_INCLUDE_DIR NAMES CppUTest/TestHarness.h - PATHS $ENV{CPPUTEST_DIR} ${CPPUTEST_DIR} /usr /usr/local /opt/local - PATH_SUFFIXES include -) - -FIND_PATH(CppUTest_EXT_INCLUDE_DIR NAMES CppUTestExt/MockSupport.h - PATHS $ENV{CPPUTEST_DIR} ${CPPUTEST_DIR} /usr /usr/local /opt/local - PATH_SUFFIXES include -) - -FIND_LIBRARY(CppUTest_LIBRARY NAMES CppUTest - PATHS $ENV{CPPUTEST_DIR} ${CPPUTEST_DIR} /usr /usr/local /opt/local - PATH_SUFFIXES lib lib64 -) - -FIND_LIBRARY(CppUTest_EXT_LIBRARY NAMES CppUTestExt - PATHS $ENV{CPPUTEST_DIR} ${CPPUTEST_DIR} /usr /usr/local /opt/local - PATH_SUFFIXES lib lib64 -) - -# handle the QUIETLY and REQUIRED arguments and set CURL_FOUND to TRUE if -# all listed variables are TRUE -INCLUDE(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(CppUTest DEFAULT_MSG CppUTest_LIBRARY CppUTest_INCLUDE_DIR) - - -IF(CppUTest_FOUND) - SET(CppUTest_LIBRARIES ${CppUTest_LIBRARY}) - SET(CppUTest_INCLUDE_DIRS ${CppUTest_INCLUDE_DIR}) - if(NOT TARGET CppUTest) - add_library(CppUTest STATIC IMPORTED) - set_target_properties(CppUTest PROPERTIES - IMPORTED_LOCATION "${CppUTest_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${CppUTest_INCLUDE_DIR}" - ) - if (NOT TARGET CppUTest::CppUTest) - add_library(CppUTest::CppUTest ALIAS CppUTest) - endif () - endif() - SET(CppUTest_EXT_LIBRARIES ${CppUTest_EXT_LIBRARY}) - SET(CppUTest_EXT_INCLUDE_DIRS ${CppUTest_EXT_INCLUDE_DIR}) - if(NOT TARGET CppUTestExt) - add_library(CppUTestExt STATIC IMPORTED) - set_target_properties(CppUTestExt PROPERTIES - IMPORTED_LOCATION "${CppUTest_EXT_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${CppUTest_EXT_INCLUDE_DIR}" - ) - if (NOT TARGET CppUTest::CppUTestExt) - add_library(CppUTest::CppUTestExt ALIAS CppUTestExt) - endif () - endif() -ENDIF(CppUTest_FOUND) diff --git a/libs/framework/include_deprecated/bundle.h b/libs/framework/include_deprecated/bundle.h index c1d8cc445..44bf3c491 100644 --- a/libs/framework/include_deprecated/bundle.h +++ b/libs/framework/include_deprecated/bundle.h @@ -82,8 +82,6 @@ CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t bundle_revise(celix_bundle_t *b CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t bundle_addModule(celix_bundle_t *bundle, module_pt module); -CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t bundle_closeModules(const celix_bundle_t *bundle); - // Service Reference Functions CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_array_list_t *getUsingBundles(service_reference_pt reference); diff --git a/libs/framework/include_deprecated/module.h b/libs/framework/include_deprecated/module.h index 3c09f666e..c17b07918 100644 --- a/libs/framework/include_deprecated/module.h +++ b/libs/framework/include_deprecated/module.h @@ -32,7 +32,6 @@ typedef struct module celix_module_t; #include -#include "linked_list.h" #include "manifest.h" #include "version.h" #include "array_list.h" @@ -59,20 +58,12 @@ CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t module_getSymbolicName(module_p CELIX_FRAMEWORK_DEPRECATED_EXPORT char *module_getId(module_pt module); -CELIX_FRAMEWORK_DEPRECATED_EXPORT linked_list_pt module_getWires(module_pt module); - -CELIX_FRAMEWORK_DEPRECATED_EXPORT void module_setWires(module_pt module, linked_list_pt wires); - CELIX_FRAMEWORK_DEPRECATED_EXPORT bool module_isResolved(module_pt module); CELIX_FRAMEWORK_DEPRECATED_EXPORT void module_setResolved(module_pt module); CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_bundle_t *module_getBundle(module_pt module); -CELIX_FRAMEWORK_DEPRECATED_EXPORT linked_list_pt module_getRequirements(module_pt module); - -CELIX_FRAMEWORK_DEPRECATED_EXPORT linked_list_pt module_getCapabilities(module_pt module); - CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_array_list_t *module_getDependentImporters(module_pt module); CELIX_FRAMEWORK_DEPRECATED_EXPORT void module_addDependentImporter(module_pt module, module_pt importer); diff --git a/libs/framework/src/bundle.c b/libs/framework/src/bundle.c index 20a14e7ed..731441b5b 100644 --- a/libs/framework/src/bundle.c +++ b/libs/framework/src/bundle.c @@ -343,36 +343,18 @@ celix_status_t bundle_closeRevisions(const_bundle_pt bundle) { return status; } -celix_status_t bundle_closeModules(const_bundle_pt bundle) { - celix_status_t status = CELIX_SUCCESS; - - unsigned int i = 0; - for (i = 0; i < arrayList_size(bundle->modules); i++) { - module_pt module = (module_pt) arrayList_get(bundle->modules, i); - module_setWires(module, NULL); - } - - return status; -} - celix_status_t bundle_refresh(bundle_pt bundle) { - celix_status_t status; - module_pt module; - - status = bundle_closeModules(bundle); - if (status == CELIX_SUCCESS) { - arrayList_clear(bundle->modules); - status = bundle_createModule(bundle, &module); - if (status == CELIX_SUCCESS) { - status = bundle_addModule(bundle, module); - if (status == CELIX_SUCCESS) { - __atomic_store_n(&bundle->state, CELIX_BUNDLE_STATE_INSTALLED, __ATOMIC_RELEASE); - } - } - } - - framework_logIfError(bundle->framework->logger, status, NULL, "Failed to refresh bundle"); + module_pt module; + arrayList_clear(bundle->modules); + celix_status_t status = bundle_createModule(bundle, &module); + if (status == CELIX_SUCCESS) { + status = bundle_addModule(bundle, module); + if (status == CELIX_SUCCESS) { + __atomic_store_n(&bundle->state, CELIX_BUNDLE_STATE_INSTALLED, __ATOMIC_RELEASE); + } + } + framework_logIfError(bundle->framework->logger, status, NULL, "Failed to refresh bundle"); return status; } diff --git a/libs/framework/src/bundle_archive.c b/libs/framework/src/bundle_archive.c index d5f0059d9..688567345 100644 --- a/libs/framework/src/bundle_archive.c +++ b/libs/framework/src/bundle_archive.c @@ -37,7 +37,6 @@ #include "bundle_archive_private.h" #include "bundle_revision_private.h" #include "framework_private.h" -#include "linked_list_iterator.h" /** * The bundle archive which is used to store the bundle data and can be reused when a framework is restarted. diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index 227eb77ea..bdfd0ee67 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -1998,7 +1998,6 @@ static celix_status_t celix_framework_uninstallBundleEntryImpl(celix_framework_t if (status == CELIX_SUCCESS) { celix_framework_waitForEmptyEventQueue(framework); //to ensure that the uninstall event is triggered and handled - (void)bundle_closeModules(bnd); (void)bundle_destroy(bnd); if(permanent) { celix_bundleArchive_invalidate(archive); diff --git a/libs/framework/src/manifest_parser.h b/libs/framework/src/manifest_parser.h index f19de3567..a41366331 100644 --- a/libs/framework/src/manifest_parser.h +++ b/libs/framework/src/manifest_parser.h @@ -30,7 +30,6 @@ #include "module.h" #include "version.h" #include "manifest.h" -#include "linked_list.h" typedef struct manifestParser * manifest_parser_pt; diff --git a/libs/framework/src/module.c b/libs/framework/src/module.c index 0ff490574..ed68c45af 100644 --- a/libs/framework/src/module.c +++ b/libs/framework/src/module.c @@ -26,7 +26,6 @@ #include "utils.h" #include "module.h" #include "manifest_parser.h" -#include "linked_list_iterator.h" #include "celix_libloader.h" #include "celix_framework.h" #include "celix_constants.h" @@ -187,13 +186,6 @@ char * module_getId(module_pt module) { return module->id; } -linked_list_pt module_getWires(module_pt module) { - return NULL; -} - -void module_setWires(module_pt module, linked_list_pt wires) { -} - bool module_isResolved(module_pt module) { return module->resolved; } @@ -206,14 +198,6 @@ bundle_pt module_getBundle(module_pt module) { return module->bundle; } -linked_list_pt module_getRequirements(module_pt module) { - return NULL; -} - -linked_list_pt module_getCapabilities(module_pt module) { - return NULL; -} - array_list_pt module_getDependentImporters(module_pt module) { return NULL; } diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt index f9b012602..a9b31234b 100644 --- a/libs/utils/CMakeLists.txt +++ b/libs/utils/CMakeLists.txt @@ -33,8 +33,6 @@ if (UTILS) set(UTILS_SRC src/array_list.c src/hash_map.c - src/linked_list.c - src/linked_list_iterator.c src/celix_threads.c src/version.c src/version_range.c @@ -137,43 +135,6 @@ if (UTILS) target_link_libraries(utils_cut PUBLIC ${UTILS_PUBLIC_DEPS} ${UTILS_PRIVATE_DEPS}) add_subdirectory(gtest) - - find_package(CppUTest) - - if (CppUTest_FOUND) - include_directories(SYSTEM PRIVATE ${CppUTest_INCLUDE_DIR}) - include_directories(include) - include_directories(src) - - add_executable(hash_map_test private/test/hash_map_test.cpp) - target_include_directories(hash_map_test PRIVATE include_deprecated) - target_link_libraries(hash_map_test utils_cut CppUTest pthread) - - add_executable(array_list_test private/test/array_list_test.cpp) - target_include_directories(array_list_test PRIVATE include_deprecated) - target_link_libraries(array_list_test utils_cut CppUTest pthread) - - add_executable(linked_list_test private/test/linked_list_test.cpp) - target_include_directories(linked_list_test PRIVATE include_deprecated) - target_link_libraries(linked_list_test utils_cut CppUTest pthread) - - add_executable(ip_utils_test private/test/ip_utils_test.cpp) - target_include_directories(ip_utils_test PRIVATE include_deprecated) - target_link_libraries(ip_utils_test CppUTest utils_cut pthread) - - add_test(NAME run_array_list_test COMMAND array_list_test) - add_test(NAME run_hash_map_test COMMAND hash_map_test) - add_test(NAME run_linked_list_test COMMAND linked_list_test) - add_test(NAME run_ip_utils_test COMMAND ip_utils_test) - - setup_target_for_coverage(hash_map_test) - setup_target_for_coverage(array_list_test) - setup_target_for_coverage(linked_list_test) - setup_target_for_coverage(ip_utils_test) - else () - message(WARNING "Cannot find CppUTest, deprecated cpputest-based unit test will not be added") - endif () #end CppUTest_FOUND - endif () add_subdirectory(benchmark) diff --git a/libs/utils/include_deprecated/linked_list.h b/libs/utils/include_deprecated/linked_list.h deleted file mode 100644 index 5e379153e..000000000 --- a/libs/utils/include_deprecated/linked_list.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list.h - * - * \date Jul 16, 2010 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef LINKED_LIST_H_ -#define LINKED_LIST_H_ - - -#include "celix_errno.h" -#include "celix_utils_export.h" - -#include - -#ifdef __cplusplus -extern "C" { -#endif -typedef struct linked_list_entry *linked_list_entry_pt __attribute__((deprecated("linked_list is deprecated use celix_long_hash_map or celix_array_list instead"))); -typedef struct linked_list *linked_list_pt __attribute__((deprecated("linked_list is deprecated use celix_long_hash_map or celix_array_list instead"))); - -CELIX_UTILS_DEPRECATED_EXPORT celix_status_t linkedList_create(linked_list_pt *list); - -CELIX_UTILS_DEPRECATED_EXPORT celix_status_t linkedList_destroy(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT celix_status_t linkedList_clone(linked_list_pt list, linked_list_pt *clone); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_getFirst(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_getLast(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_removeFirst(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_removeLast(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedList_addFirst(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedList_addLast(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedList_contains(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT int linkedList_size(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedList_isEmpty(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedList_addElement(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedList_removeElement(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedList_clear(linked_list_pt list); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_get(linked_list_pt list, int index); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_set(linked_list_pt list, int index, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedList_addIndex(linked_list_pt list, int index, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_removeIndex(linked_list_pt list, int index); - -CELIX_UTILS_DEPRECATED_EXPORT linked_list_entry_pt linkedList_entry(linked_list_pt list, int index); - -CELIX_UTILS_DEPRECATED_EXPORT int linkedList_indexOf(linked_list_pt list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT linked_list_entry_pt linkedList_addBefore(linked_list_pt list, void *element, linked_list_entry_pt entry); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedList_removeEntry(linked_list_pt list, linked_list_entry_pt entry); - -#ifdef __cplusplus -} -#endif - -#endif /* LINKED_LIST_H_ */ diff --git a/libs/utils/include_deprecated/linked_list_iterator.h b/libs/utils/include_deprecated/linked_list_iterator.h deleted file mode 100644 index 2de1b7242..000000000 --- a/libs/utils/include_deprecated/linked_list_iterator.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list_iterator.h - * - * \date Jul 16, 2010 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef LINKED_LIST_ITERATOR_H_ -#define LINKED_LIST_ITERATOR_H_ - -#include - -#include "linked_list.h" -#include "celix_utils_export.h" - -#ifdef __cplusplus -extern "C" { -#endif -typedef struct linkedListIterator *linked_list_iterator_pt; - -CELIX_UTILS_DEPRECATED_EXPORT linked_list_iterator_pt linkedListIterator_create(linked_list_pt list, unsigned int index); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedListIterator_destroy(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedListIterator_hasNext(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedListIterator_next(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT bool linkedListIterator_hasPrevious(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void *linkedListIterator_previous(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT int linkedListIterator_nextIndex(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT int linkedListIterator_previousIndex(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedListIterator_remove(linked_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedListIterator_set(linked_list_iterator_pt iterator, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void linkedListIterator_add(linked_list_iterator_pt iterator, void *element); - -#ifdef __cplusplus -} -#endif - -#endif /* LINKED_LIST_ITERATOR_H_ */ From 6a4d2a98997f990e93fb02bff98bab0621db5400 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 23 Dec 2023 17:14:13 +0100 Subject: [PATCH 02/18] Remove cpputest from build config --- .github/workflows/coverage.yml | 2 +- .github/workflows/coverity-scan.yml | 1 - .github/workflows/macos.yml | 4 ++-- .github/workflows/ubuntu.yml | 3 +-- conanfile.py | 3 --- 5 files changed, 4 insertions(+), 9 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index e03444193..8ed1777d8 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -55,7 +55,7 @@ jobs: -o celix:enable_testing_on_ci=True -o celix:enable_ccache=True run: | - conan install . celix/ci -pr:b default -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest --require-override=openssl/1.1.1s + conan install . celix/ci -pr:b default -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing --require-override=openssl/1.1.1s - name: Build run: | conan build . -bf build diff --git a/.github/workflows/coverity-scan.yml b/.github/workflows/coverity-scan.yml index 6baceb7b5..375544813 100644 --- a/.github/workflows/coverity-scan.yml +++ b/.github/workflows/coverity-scan.yml @@ -27,7 +27,6 @@ jobs: libffi-dev \ libxml2-dev \ libczmq-dev \ - libcpputest-dev \ rapidjson-dev \ libavahi-compat-libdnssd-dev \ libcivetweb-dev \ diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 435aac818..fb2b53421 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -59,7 +59,7 @@ jobs: -o celix/*:framework_curlinit=False -o celix/*:enable_ccache=True run: | - conan build . -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b default -pr:h default -s:b build_type=Release -s:h build_type=Release -of build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest + conan build . -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b default -pr:h default -s:b build_type=Release -s:h build_type=Release -of build ${CONAN_BUILD_OPTIONS} -b missing - name: Test run: | cd build @@ -75,7 +75,7 @@ jobs: uses: actions/checkout@v3.3.0 - name: Install dependencies run: | - brew install lcov zeromq czmq cpputest jansson rapidjson libzip ccache + brew install lcov zeromq czmq jansson rapidjson libzip ccache - name: Prepare ccache timestamp id: ccache_cache_timestamp run: | diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index e1bd8ef01..6b47515c5 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -80,7 +80,7 @@ jobs: -o celix:framework_curlinit=False -o celix:enable_ccache=True run: | - conan install . celix/ci -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b release -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest --require-override=openssl/1.1.1s + conan install . celix/ci -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b release -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing --require-override=openssl/1.1.1s - name: Build env: CC: ${{ matrix.compiler[0] }} @@ -120,7 +120,6 @@ jobs: libffi-dev \ libxml2-dev \ libczmq-dev \ - libcpputest-dev \ rapidjson-dev \ libavahi-compat-libdnssd-dev \ libcivetweb-dev \ diff --git a/conanfile.py b/conanfile.py index 66a7c8f7f..c82d22c58 100644 --- a/conanfile.py +++ b/conanfile.py @@ -156,7 +156,6 @@ def package_id(self): def build_requirements(self): if self.options.enable_testing: self.test_requires("gtest/1.10.0") - self.test_requires("cpputest/4.0") if self.options.enable_ccache: self.build_requires("ccache/4.7.4") @@ -347,8 +346,6 @@ def configure(self): self.options['openssl'].shared = True if self.options.enable_testing: self.options['gtest'].shared = True - if self.options.enable_address_sanitizer: - self.options["cpputest"].with_leak_detection = False if (self.options.build_rsa_discovery_common or (self.options.build_rsa_remote_service_admin_dfi and self.options.enable_testing)): self.options['libxml2'].shared = True From 577551e933f01595cb62b58adcc8fefe5adf93dd Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 23 Dec 2023 17:17:31 +0100 Subject: [PATCH 03/18] Remove cpputests for array and linked list --- libs/utils/private/test/array_list_test.cpp | 584 -------------- libs/utils/private/test/linked_list_test.cpp | 796 ------------------- 2 files changed, 1380 deletions(-) delete mode 100644 libs/utils/private/test/array_list_test.cpp delete mode 100644 libs/utils/private/test/linked_list_test.cpp diff --git a/libs/utils/private/test/array_list_test.cpp b/libs/utils/private/test/array_list_test.cpp deleted file mode 100644 index da90de5a1..000000000 --- a/libs/utils/private/test/array_list_test.cpp +++ /dev/null @@ -1,584 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * array_list_test.cpp - * - * \date Sep 15, 2015 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include -#include - -#include "CppUTest/TestHarness.h" -#include "CppUTest/TestHarness_c.h" -#include "CppUTest/CommandLineTestRunner.h" - -extern "C" -{ -#include "celix_array_list.h" -#include "array_list.h" -#include "array_list_private.h" -} - -int main(int argc, char** argv) { - MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); - return RUN_ALL_TESTS(argc, argv); -} - -static char* my_strdup(const char* s){ - char *d = (char*) malloc (strlen (s) + 1); - if (d == NULL) return NULL; - strcpy (d,s); - return d; -} - -//----------------------TESTS GROUP DEFINES---------------------- - -TEST_GROUP(array_list) { - array_list_pt list; - - void setup(){ - arrayList_create(&list); - } - void teardown() { - arrayList_destroy(list); - } -}; - -TEST_GROUP(array_list_iterator) { - array_list_pt list; - - void setup(){ - arrayList_create(&list); - } - void teardown() { - arrayList_destroy(list); - } -}; - -//----------------------ARRAY LIST TESTS---------------------- - -TEST(array_list, create) { - CHECK_C(list != NULL); - LONGS_EQUAL(0, list->size); -} - -TEST(array_list, trimToSize) { - char * entry = my_strdup("entry"); - arrayList_clear(list); - - arrayList_add(list, entry); - LONGS_EQUAL(1, list->size); - LONGS_EQUAL(10, list->capacity); - - arrayList_trimToSize(list); - LONGS_EQUAL(1, list->size); - LONGS_EQUAL(1, list->capacity); - - free(entry); -} - -TEST(array_list, ensureCapacity) { - int i; - arrayList_clear(list); - - LONGS_EQUAL(10, list->capacity); - LONGS_EQUAL(0, list->size); - - for (i = 0; i < 100; i++) { - char * entry = my_strdup("entry"); - arrayList_add(list, entry); - } - LONGS_EQUAL(133, list->capacity); - LONGS_EQUAL(100, list->size); - - for (i = 99; i >= 0; i--) { - free(arrayList_remove(list, i)); - } -} - -TEST(array_list, clone) { - int i; - arrayList_clear(list); - - LONGS_EQUAL(10, list->capacity); - LONGS_EQUAL(0, list->size); - - for (i = 0; i < 12; i++) { - bool added; - char entry[11]; - snprintf(entry, sizeof(entry), "|%s|%d|", "entry", i); - added = arrayList_add(list, my_strdup(entry)); - CHECK(added); - } - LONGS_EQUAL(16, list->capacity); - LONGS_EQUAL(12, list->size); - - array_list_pt clone = NULL; - clone = arrayList_clone(list); - - LONGS_EQUAL(16, clone->capacity); - LONGS_EQUAL(12, clone->size); - - unsigned int j; - for (j = 0; j < 12; j++) { - void *entry = NULL; - char entrys[11]; - snprintf(entrys, sizeof(entrys), "|%s|%d|", "entry", j); - - entry = arrayList_get(clone, j); - STRCMP_EQUAL((char *) entry, entrys); - } - - for (i = 11; i >= 0; i--) { - free(arrayList_remove(list, i)); - } - arrayList_destroy(clone); -} - -TEST(array_list, size){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry"); - char * entry3 = my_strdup("entry"); - arrayList_clear(list); - LONGS_EQUAL(0, list->size); - - arrayList_add(list, entry); - LONGS_EQUAL(1, list->size); - - arrayList_add(list, entry2); - LONGS_EQUAL(2, list->size); - - arrayList_add(list, entry3); - LONGS_EQUAL(3, list->size); - - free(entry); - free(entry2); - free(entry3); -} - -TEST(array_list, isEmpty){ - arrayList_clear(list); - LONGS_EQUAL(0, list->size); - CHECK(arrayList_isEmpty(list)); -} - -TEST(array_list, contains){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = NULL; - bool contains; - - arrayList_clear(list); - - arrayList_add(list, entry); - - arrayList_add(list, entry2); - - CHECK(arrayList_contains(list, entry)); - CHECK(arrayList_contains(list, entry2)); - contains = arrayList_contains(list, NULL); - CHECK(!contains); - - arrayList_add(list, entry3); - - CHECK(arrayList_contains(list, entry3)); - - free(entry); - free(entry2); -} - -TEST(array_list, indexOf){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - - arrayList_clear(list); - - arrayList_add(list, entry); - - arrayList_add(list, entry2); - arrayList_add(list, entry2); - arrayList_add(list, entry2); - arrayList_add(list, entry2); - - LONGS_EQUAL(0, arrayList_indexOf(list, entry)); - LONGS_EQUAL(1, arrayList_indexOf(list, entry2)); - LONGS_EQUAL(0, arrayList_lastIndexOf(list, entry)); - LONGS_EQUAL(4, arrayList_lastIndexOf(list, entry2)); - - free(entry); - free(entry2); -} - -TEST(array_list, get){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = NULL; - char * get; - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - get = (char*) arrayList_get(list, 0); - STRCMP_EQUAL(entry, get); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry2, get); - - arrayList_add(list, entry3); - - get = (char*) arrayList_get(list, 2); - CHECK(get == NULL); - - get = (char*) arrayList_get(list, 42); - CHECK(get == NULL); - - free(entry); - free(entry2); -} - -TEST(array_list, set){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - char * get; - char * old; - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry2, get); - - old = (char*) arrayList_set(list, 1, entry3); - STRCMP_EQUAL(entry2, old); - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry3, get); - - free(entry); - free(entry2); - free(entry3); -} - -TEST(array_list, add){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - char * get; - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry2, get); - - arrayList_addIndex(list, 1, entry3); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry3, get); - - get = (char*) arrayList_get(list, 2); - STRCMP_EQUAL(entry2, get); - - free(entry); - free(entry2); - free(entry3); -} - -TEST(array_list, addAll){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - char * get; - array_list_pt toAdd; - bool changed; - - arrayList_clear(list); - - arrayList_create(&toAdd); - arrayList_add(toAdd, entry); - arrayList_add(toAdd, entry2); - - arrayList_add(list, entry3); - - get = (char*) arrayList_get(list, 0); - STRCMP_EQUAL(entry3, get); - - changed = arrayList_addAll(list, toAdd); - CHECK(changed); - LONGS_EQUAL(3, arrayList_size(list)); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry, get); - - get = (char*) arrayList_get(list, 2); - STRCMP_EQUAL(entry2, get); - - free(entry); - free(entry2); - free(entry3); - arrayList_destroy(toAdd); -} - -TEST(array_list, remove){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - char * get; - char * removed; - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry2, get); - - // Remove first entry - removed = (char*) arrayList_remove(list, 0); - STRCMP_EQUAL(entry, removed); - - // Check the new first entry - get = (char*) arrayList_get(list, 0); - STRCMP_EQUAL(entry2, get); - - // Add a new entry - arrayList_add(list, entry3); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry3, get); - - free(entry); - free(entry2); - free(entry3); -} - -TEST(array_list, removeElement){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - char * get; - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - // Remove entry - CHECK(arrayList_removeElement(list, entry)); - - // Check the new first element - get = (char*) arrayList_get(list, 0); - STRCMP_EQUAL(entry2, get); - - // Add a new entry - arrayList_add(list, entry3); - - get = (char*) arrayList_get(list, 1); - STRCMP_EQUAL(entry3, get); - - free(entry); - free(entry2); - free(entry3); -} - -TEST(array_list, clear){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - - arrayList_clear(list); - - arrayList_add(list, entry); - arrayList_add(list, entry2); - - LONGS_EQUAL(2, arrayList_size(list)); - arrayList_clear(list); - LONGS_EQUAL(0, arrayList_size(list)); - - free(entry); - free(entry2); -} - -//----------------------ARRAY LIST ITERATOR TESTS---------------------- - -TEST(array_list_iterator, create){ - array_list_iterator_pt it_list = arrayListIterator_create(list); - CHECK(it_list != NULL); - POINTERS_EQUAL(list, it_list->list); - arrayListIterator_destroy(it_list); -} - -TEST(array_list_iterator, hasNext){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - array_list_iterator_pt it_list; - - arrayList_add(list, entry); - arrayList_add(list, entry2); - arrayList_add(list, entry3); - it_list = arrayListIterator_create(list); - CHECK(arrayListIterator_hasNext(it_list)); - - arrayListIterator_next(it_list); - CHECK(arrayListIterator_hasNext(it_list)); - - arrayListIterator_next(it_list); - CHECK(arrayListIterator_hasNext(it_list)); - - arrayListIterator_next(it_list); - CHECK(!arrayListIterator_hasNext(it_list)); - - free(entry); - free(entry2); - free(entry3); - arrayListIterator_destroy(it_list); -} - -TEST(array_list_iterator, hasPrevious){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - array_list_iterator_pt it_list; - - arrayList_add(list, entry); - arrayList_add(list, entry2); - arrayList_add(list, entry3); - - it_list = arrayListIterator_create(list); - arrayListIterator_next(it_list); - arrayListIterator_next(it_list); - arrayListIterator_next(it_list); - CHECK(arrayListIterator_hasPrevious(it_list)); - - arrayListIterator_previous(it_list); - CHECK(arrayListIterator_hasPrevious(it_list)); - - arrayListIterator_previous(it_list); - CHECK(arrayListIterator_hasPrevious(it_list)); - - arrayListIterator_previous(it_list); - CHECK(!arrayListIterator_hasPrevious(it_list)); - - free(entry); - free(entry2); - free(entry3); - arrayListIterator_destroy(it_list); -} - -TEST(array_list_iterator, next){ - char * entry = my_strdup("entry"); - char * entry2 = my_strdup("entry2"); - char * entry3 = my_strdup("entry3"); - array_list_iterator_pt it_list; - - arrayList_add(list, entry); - arrayList_add(list, entry2); - arrayList_add(list, entry3); - it_list = arrayListIterator_create(list); - STRCMP_EQUAL(entry, (char*) arrayListIterator_next(it_list)); - STRCMP_EQUAL(entry2, (char*) arrayListIterator_next(it_list)); - STRCMP_EQUAL(entry3, (char*) arrayListIterator_next(it_list)); - POINTERS_EQUAL(NULL, arrayListIterator_next(it_list)); - - //mess up the expected and real changecount, code should check and handle - arrayList_add(list, entry); - arrayListIterator_next(it_list); - - free(entry); - free(entry2); - free(entry3); - arrayListIterator_destroy(it_list); -} - -TEST(array_list_iterator, previous){ - char * value = my_strdup("entry"); - char * value2 = my_strdup("entry2"); - char * value3 = my_strdup("entry3"); - array_list_iterator_pt it_list; - - arrayList_add(list, value); - arrayList_add(list, value2); - arrayList_add(list, value3); - it_list = arrayListIterator_create(list); - - arrayListIterator_next(it_list); - arrayListIterator_next(it_list); - arrayListIterator_next(it_list); - STRCMP_EQUAL(value3, (char*) arrayListIterator_previous(it_list)); - STRCMP_EQUAL(value2, (char*) arrayListIterator_previous(it_list)); - STRCMP_EQUAL(value, (char*) arrayListIterator_previous(it_list)); - POINTERS_EQUAL(NULL, arrayListIterator_previous(it_list)); - - //mess up the expected and real changecount, code should check and handle - arrayListIterator_destroy(it_list); - it_list = arrayListIterator_create(list); - arrayList_add(list, value); - arrayListIterator_previous(it_list); - - free(value); - free(value2); - free(value3); - arrayListIterator_destroy(it_list); -} - -TEST(array_list_iterator, remove){ - char * value = my_strdup("entry"); - char * value2 = my_strdup("entry2"); - char * value3 = my_strdup("entry3"); - array_list_iterator_pt it_list; - - arrayList_add(list, value); - arrayList_add(list, value2); - arrayList_add(list, value3); - it_list = arrayListIterator_create(list); - - LONGS_EQUAL(3, list->size); - STRCMP_EQUAL(value, (char*) arrayList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) arrayList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) arrayList_get(list, 2)); - - arrayListIterator_next(it_list); - arrayListIterator_next(it_list); - arrayListIterator_remove(it_list); - LONGS_EQUAL(2, list->size); - STRCMP_EQUAL(value, (char*) arrayList_get(list, 0)); - STRCMP_EQUAL(value3, (char*) arrayList_get(list, 1)); - - //mess up the expected and real changecount, code should check and handle - arrayList_add(list, value); - arrayListIterator_remove(it_list); - - free(value); - free(value2); - free(value3); - arrayListIterator_destroy(it_list); -} - diff --git a/libs/utils/private/test/linked_list_test.cpp b/libs/utils/private/test/linked_list_test.cpp deleted file mode 100644 index 10711db37..000000000 --- a/libs/utils/private/test/linked_list_test.cpp +++ /dev/null @@ -1,796 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list_test.cpp - * - * \date Sep 15, 2015 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include - -#include "CppUTest/TestHarness.h" -#include "CppUTest/TestHarness_c.h" -#include "CppUTest/CommandLineTestRunner.h" - -extern "C" -{ -#include "linked_list.h" -#include "linked_list_private.h" -#include "linked_list_iterator.h" -} - -int main(int argc, char** argv) { - MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); - return RUN_ALL_TESTS(argc, argv); -} - -static char* my_strdup(const char* s){ - char *d = (char*) malloc (strlen (s) + 1); - if (d == NULL) return NULL; - strcpy (d,s); - return d; -} - -//----------------------TESTGROUP DEFINES---------------------- - -TEST_GROUP(linked_list){ - linked_list_pt list; - - void setup(void) { - linkedList_create(&list); - } - - void teardown(void) { - linkedList_destroy(list); - } -}; - -TEST_GROUP(linked_list_iterator){ - linked_list_pt list; - - void setup(void) { - linkedList_create(&list); - } - - void teardown(void) { - linkedList_destroy(list); - } -}; - -//----------------------LINKED LIST TESTS---------------------- - -TEST(linked_list, create){ - CHECK(list != NULL); - LONGS_EQUAL(0, linkedList_size(list)); -} - -TEST(linked_list, add){ - char * value = my_strdup("element"); - - LONGS_EQUAL(0, linkedList_size(list)); - linkedList_addElement(list, value); - LONGS_EQUAL(1, linkedList_size(list)); - - free(value); -} - -TEST(linked_list, addFirst){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - - STRCMP_EQUAL(value, (char*) linkedList_getFirst(list)); - - linkedList_addFirst(list, value3); - - STRCMP_EQUAL(value3, (char*) linkedList_getFirst(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, addLast){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - - STRCMP_EQUAL(value2, (char*) linkedList_getLast(list)); - - linkedList_addLast(list, value3); - - STRCMP_EQUAL(value3, (char*) linkedList_getLast(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, addIndex){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - char * value4 = my_strdup("element4"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - linkedList_addIndex(list, 2, value4); - - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value4, (char*) linkedList_get(list, 2)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 3)); - - free(value); - free(value2); - free(value3); - free(value4); -} - -TEST(linked_list, addBefore){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - char * value4 = my_strdup("element4"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - linkedList_addBefore(list, value4, linkedList_entry(list, 2)); - - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value4, (char*) linkedList_get(list, 2)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 3)); - - free(value); - free(value2); - free(value3); - free(value4); -} - -TEST(linked_list, remove){ - char * value = strdup("element"); - - LONGS_EQUAL(0, linkedList_size(list)); - linkedList_addElement(list, value); - LONGS_EQUAL(1, linkedList_size(list)); - - linkedList_removeElement(list, value); - LONGS_EQUAL(0, linkedList_size(list)); - - free(value); -} - -TEST(linked_list, removeFirst){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value, (char*) linkedList_getFirst(list)); - - linkedList_removeFirst(list); - - STRCMP_EQUAL(value2, (char*) linkedList_getFirst(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, removeLast){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value3, (char*) linkedList_getLast(list)); - - linkedList_removeLast(list); - - STRCMP_EQUAL(value2, (char*) linkedList_getLast(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, removeElement){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - //add 6 elements - linkedList_addElement(list, NULL); - linkedList_addElement(list, value2); - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, NULL); - linkedList_addElement(list, value3); - LONGS_EQUAL(6, list->size); - - linkedList_removeElement(list, NULL); - linkedList_removeElement(list, NULL); - LONGS_EQUAL(4, list->size); - - linkedList_removeElement(list, value2); - LONGS_EQUAL(3, list->size); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, removeIndex){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - - linkedList_removeIndex(list, 1); - - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, removeEntry){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - linkedList_removeEntry(list, list->header); - LONGS_EQUAL(3, list->size); - - linkedList_removeEntry(list, linkedList_entry(list, 0)); - LONGS_EQUAL(2, list->size); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, clone){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_pt list2; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - linkedList_clone(list, &list2); - LONGS_EQUAL(list->size, list2->size); - STRCMP_EQUAL( (char*) linkedList_getFirst(list), (char*) linkedList_getFirst(list2)); - STRCMP_EQUAL( (char*) linkedList_getLast(list), (char*) linkedList_getLast(list2)); - - free(value); - free(value2); - free(value3); - linkedList_destroy(list2); -} - -TEST(linked_list, clear){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - LONGS_EQUAL(3, list->size); - - linkedList_clear(list); - LONGS_EQUAL(0, list->size); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, get){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, getFirst){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value, (char*) linkedList_getFirst(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, getLast){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value3, (char*) linkedList_getLast(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, set){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - linkedList_set(list, 1, value3); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, contains){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - linkedList_addElement(list, value); - CHECK(linkedList_contains(list, value)); - CHECK(!linkedList_contains(list, value2)); - CHECK(!linkedList_contains(list, value3)); - - linkedList_addElement(list, value2); - CHECK(linkedList_contains(list, value)); - CHECK(linkedList_contains(list, value2)); - CHECK(!linkedList_contains(list, value3)); - - linkedList_addElement(list, value3); - CHECK(linkedList_contains(list, value)); - CHECK(linkedList_contains(list, value2)); - CHECK(linkedList_contains(list, value3)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, size){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - LONGS_EQUAL(0, linkedList_size(list)); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - LONGS_EQUAL(3, linkedList_size(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, isEmpty){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - CHECK(linkedList_isEmpty(list)); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - CHECK(!linkedList_isEmpty(list)); - - linkedList_clear(list); - CHECK(linkedList_isEmpty(list)); - - free(value); - free(value2); - free(value3); -} - -TEST(linked_list, entry){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - char * value4 = my_strdup("element4"); - char * value5 = my_strdup("element5"); - - POINTERS_EQUAL(NULL, linkedList_entry(list, 666)); - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - linkedList_addElement(list, value4); - linkedList_addElement(list, value5); - - STRCMP_EQUAL(value2, (char*) linkedList_entry(list, 1)->element); - STRCMP_EQUAL(value4, (char*) linkedList_entry(list, 3)->element); - - free(value); - free(value2); - free(value3); - free(value4); - free(value5); -} - -TEST(linked_list, indexOf){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - - LONGS_EQUAL(-1, linkedList_indexOf(list, value)); - LONGS_EQUAL(-1, linkedList_indexOf(list, value2)); - LONGS_EQUAL(-1, linkedList_indexOf(list, value3)); - LONGS_EQUAL(-1, linkedList_indexOf(list, NULL)); - - linkedList_addElement(list, value); - LONGS_EQUAL(0, linkedList_indexOf(list, value)); - - linkedList_addElement(list, value2); - LONGS_EQUAL(1, linkedList_indexOf(list, value2)); - - linkedList_addElement(list, value3); - LONGS_EQUAL(2, linkedList_indexOf(list, value3)); - - linkedList_addElement(list, NULL); - LONGS_EQUAL(3, linkedList_indexOf(list, NULL)); - - free(value); - free(value2); - free(value3); -} - -//----------------------LINKED LIST ITERATOR TESTS---------------------- - -TEST(linked_list_iterator, create){ - linked_list_iterator_pt it_list = linkedListIterator_create(list,0); - CHECK(it_list != NULL); - linkedListIterator_destroy(it_list); - - it_list = linkedListIterator_create(list,666); - POINTERS_EQUAL(NULL, it_list); -} - -TEST(linked_list_iterator, hasNext){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - it_list = linkedListIterator_create(list, 0); - CHECK(linkedListIterator_hasNext(it_list)); - - linkedListIterator_next(it_list); - CHECK(linkedListIterator_hasNext(it_list)); - - linkedListIterator_next(it_list); - CHECK(linkedListIterator_hasNext(it_list)); - - linkedListIterator_next(it_list); - CHECK(!linkedListIterator_hasNext(it_list)); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, hasPrevious){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - - it_list = linkedListIterator_create(list, 3); - CHECK(linkedListIterator_hasPrevious(it_list)); - - linkedListIterator_previous(it_list); - CHECK(linkedListIterator_hasPrevious(it_list)); - - linkedListIterator_previous(it_list); - CHECK(linkedListIterator_hasPrevious(it_list)); - - linkedListIterator_previous(it_list); - CHECK(!linkedListIterator_hasPrevious(it_list)); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, next){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 0); - STRCMP_EQUAL(value, (char*) linkedListIterator_next(it_list)); - STRCMP_EQUAL(value2, (char*) linkedListIterator_next(it_list)); - STRCMP_EQUAL(value3, (char*) linkedListIterator_next(it_list)); - POINTERS_EQUAL(NULL, linkedListIterator_next(it_list)); - - //mess up the expected and real changecount, code should check and handle - linkedList_addElement(list, value); - linkedListIterator_next(it_list); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, previous){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 3); - STRCMP_EQUAL(value3, (char*) linkedListIterator_previous(it_list)); - STRCMP_EQUAL(value2, (char*) linkedListIterator_previous(it_list)); - STRCMP_EQUAL(value, (char*) linkedListIterator_previous(it_list)); - POINTERS_EQUAL(NULL, linkedListIterator_previous(it_list)); - - //mess up the expected and real changecount, code should check and handle - linkedListIterator_destroy(it_list); - it_list = linkedListIterator_create(list, 3); - linkedList_addElement(list, value); - linkedListIterator_previous(it_list); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, nextIndex){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 0); - LONGS_EQUAL(0, linkedListIterator_nextIndex(it_list)); - linkedListIterator_next(it_list); - LONGS_EQUAL(1, linkedListIterator_nextIndex(it_list)); - linkedListIterator_next(it_list); - LONGS_EQUAL(2, linkedListIterator_nextIndex(it_list)); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, previousIndex){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 3); - LONGS_EQUAL(2, linkedListIterator_previousIndex(it_list)); - linkedListIterator_previous(it_list); - LONGS_EQUAL(1, linkedListIterator_previousIndex(it_list)); - linkedListIterator_previous(it_list); - LONGS_EQUAL(0, linkedListIterator_previousIndex(it_list)); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, set){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 0); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - linkedListIterator_set(it_list, NULL); - linkedListIterator_next(it_list); - linkedListIterator_next(it_list); - linkedListIterator_set(it_list, value3); - linkedListIterator_next(it_list); - linkedListIterator_set(it_list, value2); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 2)); - - //mess up the expected and real changecount, code should check and handle - linkedList_addElement(list, value); - linkedListIterator_set(it_list, value); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, add){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 0); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1)); - - linkedListIterator_next(it_list); - linkedListIterator_add(it_list, value2); - - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - //mess up the expected and real changecount, code should check and handle - linkedList_addElement(list, value); - linkedListIterator_add(it_list, value2); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} - -TEST(linked_list_iterator, remove){ - char * value = my_strdup("element"); - char * value2 = my_strdup("element2"); - char * value3 = my_strdup("element3"); - linked_list_iterator_pt it_list; - - linkedList_addElement(list, value); - linkedList_addElement(list, value2); - linkedList_addElement(list, value3); - it_list = linkedListIterator_create(list, 0); - - LONGS_EQUAL(3, list->size); - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2)); - - linkedListIterator_next(it_list); - linkedListIterator_next(it_list); - linkedListIterator_remove(it_list); - LONGS_EQUAL(2, list->size); - STRCMP_EQUAL(value, (char*) linkedList_get(list, 0)); - STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1)); - - //mess up the expected and real changecount, code should check and handle - linkedList_addElement(list, value); - linkedListIterator_remove(it_list); - - free(value); - free(value2); - free(value3); - linkedListIterator_destroy(it_list); -} From daa0060a4b53968e06602713acb95444ac2f582d Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 23 Dec 2023 19:07:37 +0100 Subject: [PATCH 04/18] Remove deprecated array_list and its usage --- .../http_admin/http_admin/src/http_admin.c | 17 +- .../include/pubsub_websocket_private.h | 4 +- .../private/src/ps_websocket_activator.c | 6 +- .../private/src/pubsub_websocket_example.c | 2 +- .../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 1 - .../pubsub_admin_zmq/src/pubsub_zmq_admin.c | 1 + .../src/pubsub_zmq_topic_receiver.c | 2 + .../src/pubsub_discovery_impl.c | 2 +- .../pubsub/pubsub_utils/src/pubsub_utils.c | 22 +- .../include/endpoint_descriptor_reader.h | 4 +- .../include/endpoint_descriptor_writer.h | 4 +- .../include/endpoint_discovery_poller.h | 2 +- .../src/endpoint_descriptor_reader.c | 33 +- .../src/endpoint_descriptor_writer.c | 6 +- .../src/endpoint_discovery_poller.c | 210 +++++------ .../src/endpoint_discovery_server.c | 23 +- .../gtest/src/rsa_tests.cc | 8 +- .../src/remote_service_admin_dfi.c | 34 +- .../src/remote_service_admin_dfi.h | 6 +- .../rsa_common/src/import_registration_impl.c | 5 +- .../rsa_common/src/import_registration_impl.h | 2 +- .../src/remote_interceptors_handler.c | 31 +- .../src/remote_service_admin_impl.h | 6 +- .../rsa_spi/include/endpoint_description.h | 1 - .../rsa_spi/include/endpoint_listener.h | 1 - .../rsa_spi/include/remote_service_admin.h | 6 +- .../topology_manager/src/scope.c | 60 ++-- .../topology_manager/src/topology_manager.c | 12 +- .../topology_manager/src/topology_manager.h | 4 +- .../tms_tst/disc_mock/disc_mock_activator.c | 10 +- .../tms_tst/disc_mock/disc_mock_service.c | 4 +- .../tms_tst/disc_mock/disc_mock_service.h | 4 +- .../topology_manager/tms_tst/tms_tests.cpp | 30 +- bundles/shell/remote_shell/src/remote_shell.c | 55 +-- bundles/shell/remote_shell/src/remote_shell.h | 2 +- .../CelixBundleContextServicesTestSuite.cc | 6 +- libs/framework/include/celix_api.h | 10 +- .../include_deprecated/bundle_context.h | 1 - .../include_deprecated/bundle_revision.h | 2 +- .../include_deprecated/dm_component.h | 2 +- .../dm_dependency_manager.h | 1 - libs/framework/include_deprecated/module.h | 1 - .../include_deprecated/service_reference.h | 1 - .../include_deprecated/service_registration.h | 1 - .../include_deprecated/service_registry.h | 2 +- .../include_deprecated/service_tracker.h | 1 - libs/framework/src/bundle.c | 30 +- libs/framework/src/bundle_context.c | 16 +- libs/framework/src/bundle_context_private.h | 2 +- libs/framework/src/bundle_private.h | 2 +- libs/framework/src/dm_component_impl.c | 10 +- .../src/dm_dependency_manager_impl.c | 8 +- libs/framework/src/framework.c | 109 +++--- libs/framework/src/framework_private.h | 14 +- libs/framework/src/module.c | 6 +- .../framework/src/registry_callback_private.h | 2 +- libs/framework/src/service_reference.c | 2 +- libs/framework/src/service_registry.c | 226 ++++++------ libs/framework/src/service_tracker.c | 26 +- .../benchmark/src/StringHashmapBenchmark.cc | 2 +- libs/utils/include_deprecated/array_list.h | 97 ----- .../include_deprecated/celix_utils_api.h | 8 +- libs/utils/src/array_list.c | 337 ++---------------- libs/utils/src/array_list_private.h | 14 - 64 files changed, 583 insertions(+), 976 deletions(-) delete mode 100644 libs/utils/include_deprecated/array_list.h diff --git a/bundles/http_admin/http_admin/src/http_admin.c b/bundles/http_admin/http_admin/src/http_admin.c index fb7ac6be0..ee303022c 100755 --- a/bundles/http_admin/http_admin/src/http_admin.c +++ b/bundles/http_admin/http_admin/src/http_admin.c @@ -17,9 +17,10 @@ * under the License. */ -#include -#include +#include #include +#include +#include #include #include "http_admin.h" @@ -355,9 +356,9 @@ static void httpAdmin_updateInfoSvc(http_admin_manager_t *admin) { size_t resources_urls_size; FILE *stream = open_memstream(&resources_urls, &resources_urls_size); - unsigned int size = arrayList_size(admin->aliasList); + unsigned int size = celix_arrayList_size(admin->aliasList); for (unsigned int i = 0; i < size; ++i) { - http_alias_t *alias = arrayList_get(admin->aliasList, i); + http_alias_t *alias = celix_arrayList_get(admin->aliasList, i); bool isLast = (i == size-1); const char *separator = isLast ? "" : ","; fprintf(stream, "%s%s", alias->url, separator); @@ -456,9 +457,9 @@ static void createAliasesSymlink(const char *aliases, const char *admin_root, co * @return true if alias is already in the list, false if not. */ static bool aliasList_containsAlias(celix_array_list_t *alias_list, const char *alias) { - unsigned int size = arrayList_size(alias_list); + unsigned int size = celix_arrayList_size(alias_list); for(unsigned int i = 0; i < size; i++) { - http_alias_t *http_alias = arrayList_get(alias_list, i); + http_alias_t *http_alias = celix_arrayList_get(alias_list, i); if(strcmp(http_alias->alias_path, alias) == 0) { return true; } @@ -492,9 +493,9 @@ void http_admin_stopBundle(void *data, const celix_bundle_t *bundle) { long bundle_id = celix_bundle_getId(bundle); //Remove all aliases which are connected to this bundle - unsigned int size = arrayList_size(admin->aliasList); + unsigned int size = celix_arrayList_size(admin->aliasList); for (unsigned int i = (size - 1); i < size; i--) { - http_alias_t *alias = arrayList_get(admin->aliasList, i); + http_alias_t *alias = celix_arrayList_get(admin->aliasList, i); if(alias->bundle_id == bundle_id) { remove(alias->alias_path); //Delete alias in cache directory free(alias->url); diff --git a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/include/pubsub_websocket_private.h b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/include/pubsub_websocket_private.h index 5a26a684a..f41dc1edf 100644 --- a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/include/pubsub_websocket_private.h +++ b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/include/pubsub_websocket_private.h @@ -36,7 +36,7 @@ #include "pubsub/publisher.h" struct pubsub_sender{ - array_list_t *trackers; + celix_array_list_t *trackers; const char *ident; hash_map_t *tid_map; //service -> tid long bundleId; @@ -73,7 +73,7 @@ struct send_thread_struct { }; typedef struct send_thread_struct send_thread_struct_t; -pubsub_sender_t* publisher_create(array_list_pt trackers, const char* ident,long bundleId); +pubsub_sender_t* publisher_create(celix_array_list_t* trackers, const char* ident,long bundleId); void publisher_start(pubsub_sender_t *client); void publisher_stop(pubsub_sender_t *client); diff --git a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/ps_websocket_activator.c b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/ps_websocket_activator.c index 5cd72d3e8..0e7eeaab2 100644 --- a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/ps_websocket_activator.c +++ b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/ps_websocket_activator.c @@ -49,9 +49,9 @@ static const char * SUB_TOPICS[] = { struct ps_websocketActivator { pubsub_info_t *pubsub; //Publisher vars - array_list_t *trackerList;//List + celix_array_list_t *trackerList;//List //Subscriber vars - array_list_t *registrationList; //List + celix_array_list_t *registrationList; //List pubsub_subscriber_t *subsvc; }; @@ -119,7 +119,7 @@ static int pubsub_start(struct ps_websocketActivator *act, celix_bundle_context_ #endif service_registration_pt reg = NULL; bundleContext_registerService(ctx, PUBSUB_SUBSCRIBER_SERVICE_NAME, subsvc, props, ®); - arrayList_add(act->registrationList,reg); + celix_arrayList_add(act->registrationList,reg); } subscriber_start((pubsub_receiver_t *) act->subsvc->handle); diff --git a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/pubsub_websocket_example.c b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/pubsub_websocket_example.c index 528b0d33f..3f2b7d3a5 100644 --- a/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/pubsub_websocket_example.c +++ b/bundles/pubsub/examples/pubsub/pubsub_websocket/private/src/pubsub_websocket_example.c @@ -110,7 +110,7 @@ static void* send_thread(void* arg) { return NULL; } -pubsub_sender_t* publisher_create(array_list_pt trackers,const char* ident,long bundleId) { +pubsub_sender_t* publisher_create(celix_array_list_t* trackers,const char* ident,long bundleId) { pubsub_sender_t *publisher = malloc(sizeof(*publisher)); publisher->trackers = trackers; diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c index 464e60192..dfd349acd 100644 --- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c +++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #if defined(__APPLE__) #include diff --git a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c index 7b101b80c..112668f2f 100644 --- a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c +++ b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c @@ -38,6 +38,7 @@ #include "pubsub_psa_zmq_constants.h" #include "pubsub_zmq_topic_sender.h" #include "pubsub_zmq_topic_receiver.h" +#include "utils.h" #define L_DEBUG(...) \ celix_logHelper_log(psa->log, CELIX_LOG_LEVEL_DEBUG, __VA_ARGS__) diff --git a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_receiver.c b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_receiver.c index 97debaaea..70fd12288 100644 --- a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_receiver.c +++ b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_receiver.c @@ -22,6 +22,8 @@ #include #include #include +#include "utils.h" + #if !defined(__APPLE__) #include #endif diff --git a/bundles/pubsub/pubsub_discovery/src/pubsub_discovery_impl.c b/bundles/pubsub/pubsub_discovery/src/pubsub_discovery_impl.c index f4730a8a9..d0d1854df 100644 --- a/bundles/pubsub/pubsub_discovery/src/pubsub_discovery_impl.c +++ b/bundles/pubsub/pubsub_discovery/src/pubsub_discovery_impl.c @@ -33,7 +33,7 @@ #include "celix_properties.h" #include "celix_constants.h" #include "celix_threads.h" -#include "array_list.h" +#include "celix_array_list.h" #include "utils.h" #include "celix_errno.h" #include "filter.h" diff --git a/bundles/pubsub/pubsub_utils/src/pubsub_utils.c b/bundles/pubsub/pubsub_utils/src/pubsub_utils.c index a2cc2a261..9fa546a5e 100644 --- a/bundles/pubsub/pubsub_utils/src/pubsub_utils.c +++ b/bundles/pubsub/pubsub_utils/src/pubsub_utils.c @@ -19,24 +19,20 @@ #include #include +#include +#include +#include +#include #include "celix_constants.h" #include "celix_filter.h" #include "filter.h" - #include "pubsub/publisher.h" #include "pubsub_utils.h" #include "bundle_context.h" - -#include "array_list.h" #include "celix_bundle.h" - -#include -#include -#include - -#include #include "celix_utils.h" +#include "celix_array_list.h" #define MAX_KEYBUNDLE_LENGTH 256 @@ -86,14 +82,14 @@ celix_status_t pubsub_getPubSubInfoFromFilter(const char* filterstr, char **scop * Caller is responsible for freeing the object */ char* pubsub_getKeysBundleDir(celix_bundle_context_t *ctx) { - array_list_pt bundles = NULL; + celix_array_list_t* bundles = NULL; bundleContext_getBundles(ctx, &bundles); - uint32_t nrOfBundles = arrayList_size(bundles); + uint32_t nrOfBundles = celix_arrayList_size(bundles); long bundle_id = -1; char* result = NULL; for (int i = 0; i < nrOfBundles; i++) { - celix_bundle_t *b = arrayList_get(bundles, i); + celix_bundle_t *b = celix_arrayList_get(bundles, i); /* Skip bundle 0 (framework bundle) since it has no path nor revisions */ bundle_getBundleId(b, &bundle_id); @@ -119,7 +115,7 @@ char* pubsub_getKeysBundleDir(celix_bundle_context_t *ctx) { free(dir); } - arrayList_destroy(bundles); + celix_arrayList_destroy(bundles); return result; } diff --git a/bundles/remote_services/discovery_common/include/endpoint_descriptor_reader.h b/bundles/remote_services/discovery_common/include/endpoint_descriptor_reader.h index 38ff23f0c..5f297562a 100644 --- a/bundles/remote_services/discovery_common/include/endpoint_descriptor_reader.h +++ b/bundles/remote_services/discovery_common/include/endpoint_descriptor_reader.h @@ -29,14 +29,14 @@ #include "endpoint_discovery_poller.h" #include "celix_errno.h" -#include "array_list.h" +#include "celix_array_list.h" typedef struct endpoint_descriptor_reader endpoint_descriptor_reader_t; celix_status_t endpointDescriptorReader_create(endpoint_discovery_poller_t *poller, endpoint_descriptor_reader_t * *reader); celix_status_t endpointDescriptorReader_destroy(endpoint_descriptor_reader_t * reader); -celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader_t * reader, char *document, array_list_pt *endpoints); +celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader_t * reader, char *document, celix_array_list_t** endpoints); #endif /* ENDPOINT_DESCRIPTOR_READER_H_ */ diff --git a/bundles/remote_services/discovery_common/include/endpoint_descriptor_writer.h b/bundles/remote_services/discovery_common/include/endpoint_descriptor_writer.h index ea98fc30b..5d4fa5189 100644 --- a/bundles/remote_services/discovery_common/include/endpoint_descriptor_writer.h +++ b/bundles/remote_services/discovery_common/include/endpoint_descriptor_writer.h @@ -28,12 +28,12 @@ #define ENDPOINT_DESCRIPTOR_WRITER_H_ #include "celix_errno.h" -#include "array_list.h" +#include "celix_array_list.h" typedef struct endpoint_descriptor_writer endpoint_descriptor_writer_t; celix_status_t endpointDescriptorWriter_create(endpoint_descriptor_writer_t **writer); celix_status_t endpointDescriptorWriter_destroy(endpoint_descriptor_writer_t *writer); -celix_status_t endpointDescriptorWriter_writeDocument(endpoint_descriptor_writer_t *writer, array_list_pt endpoints, char **document); +celix_status_t endpointDescriptorWriter_writeDocument(endpoint_descriptor_writer_t *writer, celix_array_list_t* endpoints, char **document); #endif /* ENDPOINT_DESCRIPTOR_WRITER_H_ */ diff --git a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h index 9cf03291c..a500b714e 100644 --- a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h +++ b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h @@ -55,6 +55,6 @@ celix_status_t endpointDiscoveryPoller_destroy(endpoint_discovery_poller_t *poll celix_status_t endpointDiscoveryPoller_addDiscoveryEndpoint(endpoint_discovery_poller_t *poller, char *url); celix_status_t endpointDiscoveryPoller_removeDiscoveryEndpoint(endpoint_discovery_poller_t *poller, char *url); -celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_poller_t *poller, array_list_pt urls); +celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_poller_t *poller, celix_array_list_t* urls); #endif /* ENDPOINT_DISCOVERY_POLLER_H_ */ diff --git a/bundles/remote_services/discovery_common/src/endpoint_descriptor_reader.c b/bundles/remote_services/discovery_common/src/endpoint_descriptor_reader.c index 1caa0dc2d..cf87fe593 100644 --- a/bundles/remote_services/discovery_common/src/endpoint_descriptor_reader.c +++ b/bundles/remote_services/discovery_common/src/endpoint_descriptor_reader.c @@ -70,13 +70,13 @@ void endpointDescriptorReader_addSingleValuedProperty(celix_properties_t *proper celix_properties_set(properties, (char *) name, (char*) value); } -void endpointDescriptorReader_addMultiValuedProperty(celix_properties_t *properties, const xmlChar* name, array_list_pt values) { +void endpointDescriptorReader_addMultiValuedProperty(celix_properties_t *properties, const xmlChar* name, celix_array_list_t* values) { char *value = calloc(256, sizeof(*value)); if (value) { - unsigned int size = arrayList_size(values); + unsigned int size = celix_arrayList_size(values); unsigned int i; for (i = 0; i < size; i++) { - char* item = (char*) arrayList_get(values, i); + char* item = (char*) celix_arrayList_get(values, i); if (i > 0) { value = strcat(value, ","); } @@ -89,7 +89,7 @@ void endpointDescriptorReader_addMultiValuedProperty(celix_properties_t *propert } } -celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader_t *reader, char *document, array_list_pt *endpoints) { +celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader_t *reader, char *document, celix_array_list_t** endpoints) { celix_status_t status = CELIX_SUCCESS; reader->reader = xmlReaderForMemory(document, (int) strlen(document), NULL, "UTF-8", 0); @@ -109,15 +109,14 @@ celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader xmlChar *valueBuffer = xmlMalloc(256); valueBuffer[0] = '\0'; - array_list_pt propertyValues = NULL; - arrayList_create(&propertyValues); + celix_array_list_t* propertyValues = celix_arrayList_create(); - array_list_pt endpointDescriptions = NULL; + celix_array_list_t* endpointDescriptions = NULL; if (*endpoints) { // use the given arraylist... endpointDescriptions = *endpoints; } else { - arrayList_create(&endpointDescriptions); + endpointDescriptions = celix_arrayList_create(); // return the read endpoints... *endpoints = endpointDescriptions; } @@ -163,7 +162,7 @@ celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader propertyValue = xmlTextReaderGetAttribute(reader->reader, VALUE); xmlChar *vtype = xmlTextReaderGetAttribute(reader->reader, VALUE_TYPE); propertyType = valueTypeFromString((char*) vtype); - arrayList_clear(propertyValues); + celix_arrayList_clear(propertyValues); if (xmlTextReaderIsEmptyElement(reader->reader)) { inProperty = false; @@ -200,7 +199,7 @@ celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader endpoint_description_t *endpointDescription = NULL; // Completely parsed endpoint description, add it to our list of results... if(endpointDescription_create(endpointProperties, &endpointDescription) == CELIX_SUCCESS){ - arrayList_add(endpointDescriptions, endpointDescription); + celix_arrayList_add(endpointDescriptions, endpointDescription); } endpointProperties = celix_properties_create(); @@ -224,10 +223,10 @@ celix_status_t endpointDescriptorReader_parseDocument(endpoint_descriptor_reader xmlFree((void *) propertyName); unsigned int k=0; - for(;kreader); diff --git a/bundles/remote_services/discovery_common/src/endpoint_descriptor_writer.c b/bundles/remote_services/discovery_common/src/endpoint_descriptor_writer.c index 2c8d32217..b816c8498 100644 --- a/bundles/remote_services/discovery_common/src/endpoint_descriptor_writer.c +++ b/bundles/remote_services/discovery_common/src/endpoint_descriptor_writer.c @@ -72,7 +72,7 @@ celix_status_t endpointDescriptorWriter_destroy(endpoint_descriptor_writer_t *wr return CELIX_SUCCESS; } -celix_status_t endpointDescriptorWriter_writeDocument(endpoint_descriptor_writer_t *writer, array_list_pt endpoints, char **document) { +celix_status_t endpointDescriptorWriter_writeDocument(endpoint_descriptor_writer_t *writer, celix_array_list_t* endpoints, char **document) { celix_status_t status = CELIX_SUCCESS; int rc; @@ -85,8 +85,8 @@ celix_status_t endpointDescriptorWriter_writeDocument(endpoint_descriptor_writer status = CELIX_BUNDLE_EXCEPTION; } else { unsigned int i; - for (i = 0; i < arrayList_size(endpoints); i++) { - endpoint_description_t *endpoint = arrayList_get(endpoints, i); + for (i = 0; i < celix_arrayList_size(endpoints); i++) { + endpoint_description_t *endpoint = celix_arrayList_get(endpoints, i); status = endpointDescriptorWriter_writeEndpoint(writer, endpoint); } if (status == CELIX_SUCCESS) { diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c index 5589c846e..c286d43ef 100644 --- a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c +++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c @@ -48,9 +48,10 @@ #define DEFAULT_POLL_TIMEOUT "10" // seconds static void *endpointDiscoveryPoller_performPeriodicPoll(void *data); -celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t *poller, char *url, array_list_pt currentEndpoints); -static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_poller_t *poller, char *url, array_list_pt *updatedEndpoints); -static celix_status_t endpointDiscoveryPoller_endpointDescriptionEquals(const void *endpointPtr, const void *comparePtr, bool *equals); +celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t *poller, char *url, celix_array_list_t* currentEndpoints); +static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_poller_t *poller, char *url, celix_array_list_t** updatedEndpoints); +static bool endpointDiscoveryPoller_endpointDescriptionEquals(celix_array_list_entry_t endpointEntry, + celix_array_list_entry_t compareEntry); /** * Allocates memory and initializes a new endpoint_discovery_poller instance. @@ -162,7 +163,7 @@ celix_status_t endpointDiscoveryPoller_destroy(endpoint_discovery_poller_t *poll } -celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_poller_t *poller, array_list_pt urls) { +celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_poller_t *poller, celix_array_list_t* urls) { celixThreadMutex_lock(&(poller)->pollerLock); hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries); @@ -170,7 +171,7 @@ celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_ while(hashMapIterator_hasNext(iterator)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); char* toAdd = strdup((char*) hashMapEntry_getKey(entry)); - arrayList_add(urls, toAdd); + celix_arrayList_add(urls, toAdd); } hashMapIterator_destroy(iterator); @@ -183,111 +184,118 @@ celix_status_t endpointDiscoveryPoller_getDiscoveryEndpoints(endpoint_discovery_ /** * Adds a new endpoint URL to the list of polled endpoints. */ -celix_status_t endpointDiscoveryPoller_addDiscoveryEndpoint(endpoint_discovery_poller_t *poller, char *url) { - celix_status_t status; +celix_status_t endpointDiscoveryPoller_addDiscoveryEndpoint(endpoint_discovery_poller_t* poller, char* url) { + celix_status_t status; - status = celixThreadMutex_lock(&(poller)->pollerLock); - if (status != CELIX_SUCCESS) { - return CELIX_BUNDLE_EXCEPTION; - } + status = celixThreadMutex_lock(&(poller)->pollerLock); + if (status != CELIX_SUCCESS) { + return CELIX_BUNDLE_EXCEPTION; + } - // Avoid memory leaks when adding an already existing URL... - array_list_pt endpoints = hashMap_get(poller->entries, url); - if (endpoints == NULL) { - status = arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &endpoints); + // Avoid memory leaks when adding an already existing URL... + celix_array_list_t* endpoints = hashMap_get(poller->entries, url); + if (endpoints == NULL) { + endpoints = celix_arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals); - if (status == CELIX_SUCCESS) { + if (endpoints) { celix_logHelper_debug(*poller->loghelper, "ENDPOINT_POLLER: add new discovery endpoint with url %s", url); - hashMap_put(poller->entries, strdup(url), endpoints); - endpointDiscoveryPoller_poll(poller, url, endpoints); - } - } + hashMap_put(poller->entries, strdup(url), endpoints); + endpointDiscoveryPoller_poll(poller, url, endpoints); + } + } - status = celixThreadMutex_unlock(&poller->pollerLock); + status = celixThreadMutex_unlock(&poller->pollerLock); - return status; + return status; } /** * Removes an endpoint URL from the list of polled endpoints. */ -celix_status_t endpointDiscoveryPoller_removeDiscoveryEndpoint(endpoint_discovery_poller_t *poller, char *url) { - celix_status_t status = CELIX_SUCCESS; - - if (celixThreadMutex_lock(&poller->pollerLock) != CELIX_SUCCESS) { - status = CELIX_BUNDLE_EXCEPTION; - } else { - hash_map_entry_pt entry = hashMap_getEntry(poller->entries, url); - - if (entry == NULL) { - celix_logHelper_debug(*poller->loghelper, "ENDPOINT_POLLER: There was no entry found belonging to url %s - maybe already removed?", url); - } else { - char* origKey = hashMapEntry_getKey(entry); +celix_status_t endpointDiscoveryPoller_removeDiscoveryEndpoint(endpoint_discovery_poller_t* poller, char* url) { + celix_status_t status = CELIX_SUCCESS; + + if (celixThreadMutex_lock(&poller->pollerLock) != CELIX_SUCCESS) { + status = CELIX_BUNDLE_EXCEPTION; + } else { + hash_map_entry_pt entry = hashMap_getEntry(poller->entries, url); + + if (entry == NULL) { + celix_logHelper_debug( + *poller->loghelper, + "ENDPOINT_POLLER: There was no entry found belonging to url %s - maybe already removed?", + url); + } else { + char* origKey = hashMapEntry_getKey(entry); celix_logHelper_debug(*poller->loghelper, "ENDPOINT_POLLER: remove discovery endpoint with url %s", url); - array_list_pt entries = hashMap_remove(poller->entries, url); + celix_array_list_t* entries = hashMap_remove(poller->entries, url); - if (entries != NULL) { - for (unsigned int i = arrayList_size(entries); i > 0; i--) { - endpoint_description_t *endpoint = arrayList_get(entries, i - 1); - discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); - arrayList_remove(entries, i - 1); - endpointDescription_destroy(endpoint); - } - arrayList_destroy(entries); - } + if (entries != NULL) { + for (unsigned int i = celix_arrayList_size(entries); i > 0; i--) { + endpoint_description_t* endpoint = celix_arrayList_get(entries, i - 1); + discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); + celix_arrayList_removeAt(entries, i - 1); + endpointDescription_destroy(endpoint); + } + celix_arrayList_destroy(entries); + } - free(origKey); - } - status = celixThreadMutex_unlock(&poller->pollerLock); - } + free(origKey); + } + status = celixThreadMutex_unlock(&poller->pollerLock); + } - return status; + return status; } - - - -celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t *poller, char *url, array_list_pt currentEndpoints) { - celix_status_t status; - array_list_pt updatedEndpoints = NULL; - - // create an arraylist with a custom equality test to ensure we can find endpoints properly... - arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals, &updatedEndpoints); - status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); - - if (status == CELIX_SUCCESS) { - if (updatedEndpoints != NULL) { - for (unsigned int i = arrayList_size(currentEndpoints); i > 0; i--) { - endpoint_description_t *endpoint = arrayList_get(currentEndpoints, i - 1); - - if (!arrayList_contains(updatedEndpoints, endpoint)) { - status = discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); - arrayList_remove(currentEndpoints, i - 1); - endpointDescription_destroy(endpoint); - } - } - - for (int i = arrayList_size(updatedEndpoints); i > 0; i--) { - endpoint_description_t *endpoint = arrayList_remove(updatedEndpoints, 0); - - if (!arrayList_contains(currentEndpoints, endpoint)) { - arrayList_add(currentEndpoints, endpoint); - status = discovery_addDiscoveredEndpoint(poller->discovery, endpoint); - } else { - endpointDescription_destroy(endpoint); - - } - } - } - } - - if (updatedEndpoints != NULL) { - arrayList_destroy(updatedEndpoints); - } - - return status; +celix_status_t +endpointDiscoveryPoller_poll(endpoint_discovery_poller_t* poller, char* url, celix_array_list_t* currentEndpoints) { + celix_status_t status; + celix_array_list_t* updatedEndpoints = NULL; + + // create an arraylist with a custom equality test to ensure we can find endpoints properly... + updatedEndpoints = celix_arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals); + status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); + + if (updatedEndpoints && status == CELIX_SUCCESS) { + if (updatedEndpoints != NULL) { + for (int i = celix_arrayList_size(currentEndpoints); i > 0; i--) { + endpoint_description_t* endpoint = celix_arrayList_get(currentEndpoints, i - 1); + + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = endpoint; + + if (celix_arrayList_indexOf(updatedEndpoints, entry) < 0) { + status = discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); + celix_arrayList_removeAt(currentEndpoints, i - 1); + endpointDescription_destroy(endpoint); + } + } + + for (int i = 0; i < celix_arrayList_size(updatedEndpoints); i++) { + endpoint_description_t* endpoint = celix_arrayList_get(updatedEndpoints, i); + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = endpoint; + if (celix_arrayList_indexOf(currentEndpoints, entry) < 0) { + celix_arrayList_add(currentEndpoints, endpoint); + status = discovery_addDiscoveredEndpoint(poller->discovery, endpoint); + } else { + endpointDescription_destroy(endpoint); + } + } + celix_arrayList_clear(updatedEndpoints); + } + } + + if (updatedEndpoints != NULL) { + celix_arrayList_destroy(updatedEndpoints); + } + + return status; } static void *endpointDiscoveryPoller_performPeriodicPoll(void *data) { @@ -308,7 +316,7 @@ static void *endpointDiscoveryPoller_performPeriodicPoll(void *data) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); char *url = hashMapEntry_getKey(entry); - array_list_pt currentEndpoints = hashMapEntry_getValue(entry); + celix_array_list_t* currentEndpoints = hashMapEntry_getValue(entry); endpointDiscoveryPoller_poll(poller, url, currentEndpoints); } @@ -349,7 +357,7 @@ static size_t endpointDiscoveryPoller_writeMemory(void *contents, size_t size, s return realsize; } -static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_poller_t *poller, char *url, array_list_pt *updatedEndpoints) { +static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_poller_t *poller, char *url, celix_array_list_t** updatedEndpoints) { celix_status_t status = CELIX_SUCCESS; @@ -399,15 +407,9 @@ static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_po return status; } -static celix_status_t endpointDiscoveryPoller_endpointDescriptionEquals(const void *endpointPtr, const void *comparePtr, bool *equals) { - endpoint_description_t *endpoint = (endpoint_description_t *) endpointPtr; - endpoint_description_t *compare = (endpoint_description_t *) comparePtr; - - if (strcmp(endpoint->id, compare->id) == 0) { - *equals = true; - } else { - *equals = false; - } - - return CELIX_SUCCESS; +static bool endpointDiscoveryPoller_endpointDescriptionEquals(celix_array_list_entry_t endpointEntry, + celix_array_list_entry_t compareEntry) { + endpoint_description_t* endpoint = (endpoint_description_t*)endpointEntry.voidPtrVal; + endpoint_description_t* compare = (endpoint_description_t*)compareEntry.voidPtrVal; + return strcmp(endpoint->id, compare->id) == 0; } diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c index a845f826a..4d6043cce 100644 --- a/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c +++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c @@ -306,11 +306,9 @@ static char* format_path(const char* path) { return result; } -static celix_status_t endpointDiscoveryServer_getEndpoints(endpoint_discovery_server_t *server, const char* the_endpoint_id, array_list_pt *endpoints) { - celix_status_t status; - - status = arrayList_create(endpoints); - if (status != CELIX_SUCCESS) { +static celix_status_t endpointDiscoveryServer_getEndpoints(endpoint_discovery_server_t *server, const char* the_endpoint_id, celix_array_list_t** endpoints) { + *endpoints = celix_arrayList_create(); + if (!(*endpoints)) { return CELIX_ENOMEM; } @@ -323,15 +321,15 @@ static celix_status_t endpointDiscoveryServer_getEndpoints(endpoint_discovery_se if (the_endpoint_id == NULL || strcmp(the_endpoint_id, endpoint_id) == 0) { endpoint_description_t *endpoint = hashMapEntry_getValue(entry); - arrayList_add(*endpoints, endpoint); + celix_arrayList_add(*endpoints, endpoint); } } hashMapIterator_destroy(iter); - return status; + return CELIX_SUCCESS; } -static int endpointDiscoveryServer_writeEndpoints(struct mg_connection* conn, array_list_pt endpoints) { +static int endpointDiscoveryServer_writeEndpoints(struct mg_connection* conn, celix_array_list_t* endpoints) { celix_status_t status; int rv = CIVETWEB_REQUEST_NOT_HANDLED; @@ -360,14 +358,14 @@ static int endpointDiscoveryServer_writeEndpoints(struct mg_connection* conn, ar static int endpointDiscoveryServer_returnAllEndpoints(endpoint_discovery_server_t *server, struct mg_connection* conn) { int status = CIVETWEB_REQUEST_NOT_HANDLED; - array_list_pt endpoints = NULL; + celix_array_list_t* endpoints = NULL; if (celixThreadMutex_lock(&server->serverLock) == CELIX_SUCCESS) { endpointDiscoveryServer_getEndpoints(server, NULL, &endpoints); if (endpoints) { status = endpointDiscoveryServer_writeEndpoints(conn, endpoints); - arrayList_destroy(endpoints); + celix_arrayList_destroy(endpoints); } @@ -381,14 +379,13 @@ static int endpointDiscoveryServer_returnAllEndpoints(endpoint_discovery_server_ static int endpointDiscoveryServer_returnEndpoint(endpoint_discovery_server_t *server, struct mg_connection* conn, const char* endpoint_id) { int status = CIVETWEB_REQUEST_NOT_HANDLED; - array_list_pt endpoints = NULL; + celix_array_list_t* endpoints = NULL; if (celixThreadMutex_lock(&server->serverLock) == CELIX_SUCCESS) { endpointDiscoveryServer_getEndpoints(server, endpoint_id, &endpoints); if (endpoints) { status = endpointDiscoveryServer_writeEndpoints(conn, endpoints); - - arrayList_destroy(endpoints); + celix_arrayList_destroy(endpoints); } celixThreadMutex_unlock(&server->serverLock); diff --git a/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc b/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc index 966a4c4dd..c0ebdd601 100644 --- a/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc +++ b/bundles/remote_services/remote_service_admin_dfi/gtest/src/rsa_tests.cc @@ -93,7 +93,7 @@ extern "C" { ASSERT_EQ(1, celix_arrayList_size(svcRegistration)); - rc = rsa->exportRegistration_close(rsa->admin,(export_registration_t *)(arrayList_get(svcRegistration,0))); + rc = rsa->exportRegistration_close(rsa->admin,(export_registration_t *)(celix_arrayList_get(svcRegistration,0))); ASSERT_EQ(CELIX_SUCCESS, rc); celix_arrayList_destroy(svcRegistration); } @@ -155,11 +155,11 @@ extern "C" { } static void testBundles(void) { - array_list_pt bundles = NULL; + celix_array_list_t* bundles = NULL; int rc = bundleContext_getBundles(context, &bundles); ASSERT_EQ(0, rc); - ASSERT_EQ(3, arrayList_size(bundles)); //framework, rsa_dfi & calc + ASSERT_EQ(3, celix_arrayList_size(bundles)); //framework, rsa_dfi & calc /* int size = arrayList_size(bundles); @@ -175,7 +175,7 @@ extern "C" { printf("got bundle with symbolic name '%s'", name); }*/ - arrayList_destroy(bundles); + celix_arrayList_destroy(bundles); } } diff --git a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c index e8c34e3ec..d0ea98911 100644 --- a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c +++ b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c @@ -86,7 +86,7 @@ struct remote_service_admin { celix_thread_t stopExportsThread; celix_thread_mutex_t importedServicesLock; - array_list_pt importedServices; + celix_array_list_t* importedServices; char *port; char *ip; @@ -190,7 +190,7 @@ celix_status_t remoteServiceAdmin_create(celix_bundle_context_t *context, remote } else { (*admin)->context = context; (*admin)->exportedServices = hashMap_create(NULL, NULL, NULL, NULL); - arrayList_create(&(*admin)->importedServices); + (*admin)->importedServices = celix_arrayList_create(); celixThreadRwlock_create(&(*admin)->exportedServicesLock, NULL); celixThreadMutex_create(&(*admin)->importedServicesLock, NULL); @@ -427,9 +427,9 @@ celix_status_t remoteServiceAdmin_stop(remote_service_admin_t *admin) { celixThreadMutex_lock(&admin->importedServicesLock); int i; - int size = arrayList_size(admin->importedServices); + int size = celix_arrayList_size(admin->importedServices); for (i = 0; i < size ; i += 1) { - import_registration_t *import = arrayList_get(admin->importedServices, i); + import_registration_t *import = celix_arrayList_get(admin->importedServices, i); if (import != NULL) { importRegistration_destroy(import); } @@ -443,7 +443,7 @@ celix_status_t remoteServiceAdmin_stop(remote_service_admin_t *admin) { } hashMap_destroy(admin->exportedServices, false, false); - arrayList_destroy(admin->importedServices); + celix_arrayList_destroy(admin->importedServices); celix_logHelper_destroy(admin->loghelper); @@ -577,7 +577,7 @@ static int remoteServiceAdmin_callback(struct mg_connection *conn) { return result; } -celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, array_list_pt *registrationsOut) { +celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, celix_array_list_t** registrationsOut) { celix_status_t status = CELIX_SUCCESS; bool export = false; @@ -607,7 +607,7 @@ celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, c celix_array_list_t *registrations = NULL; if (export) { registrations = celix_arrayList_create(); - array_list_pt references = NULL; + celix_array_list_t* references = NULL; service_reference_pt reference = NULL; char filter[256]; @@ -618,15 +618,15 @@ celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, c celix_logHelper_log(admin->loghelper, CELIX_LOG_LEVEL_DEBUG, "RSA: exportService called for serviceId %s", serviceId); int i; - int size = arrayList_size(references); + int size = celix_arrayList_size(references); for (i = 0; i < size; i += 1) { if (i == 0) { - reference = arrayList_get(references, i); + reference = celix_arrayList_get(references, i); } else { - bundleContext_ungetServiceReference(admin->context, arrayList_get(references, i)); + bundleContext_ungetServiceReference(admin->context, celix_arrayList_get(references, i)); } } - arrayList_destroy(references); + celix_arrayList_destroy(references); if (reference == NULL) { celix_logHelper_log(admin->loghelper, CELIX_LOG_LEVEL_ERROR, "ERROR: expected a reference for service id %s.", @@ -860,12 +860,12 @@ celix_status_t remoteServiceAdmin_destroyEndpointDescription(endpoint_descriptio } -celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, array_list_pt *services) { +celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, celix_array_list_t** services) { celix_status_t status = CELIX_SUCCESS; return status; } -celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, array_list_pt *services) { +celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, celix_array_list_t** services) { celix_status_t status = CELIX_SUCCESS; return status; } @@ -919,7 +919,7 @@ celix_status_t remoteServiceAdmin_importService(remote_service_admin_t *admin, e } celixThreadMutex_lock(&admin->importedServicesLock); - arrayList_add(admin->importedServices, import); + celix_arrayList_add(admin->importedServices, import); celixThreadMutex_unlock(&admin->importedServicesLock); if (status == CELIX_SUCCESS) { @@ -937,12 +937,12 @@ celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_t * celixThreadMutex_lock(&admin->importedServicesLock); int i; - int size = arrayList_size(admin->importedServices); + int size = celix_arrayList_size(admin->importedServices); import_registration_t * current = NULL; for (i = 0; i < size; i += 1) { - current = arrayList_get(admin->importedServices, i); + current = celix_arrayList_get(admin->importedServices, i); if (current == registration) { - arrayList_remove(admin->importedServices, i); + celix_arrayList_removeAt(admin->importedServices, i); importRegistration_destroy(current); break; } diff --git a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.h b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.h index af9694c66..9661b4243 100644 --- a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.h +++ b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.h @@ -34,10 +34,10 @@ celix_status_t remoteServiceAdmin_destroy(remote_service_admin_t **admin); celix_status_t remoteServiceAdmin_stop(remote_service_admin_t *admin); -celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, array_list_pt *registrations); +celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, celix_array_list_t** registrations); celix_status_t remoteServiceAdmin_removeExportedService(remote_service_admin_t *admin, export_registration_t *registration); -celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, array_list_pt *services); -celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, array_list_pt *services); +celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, celix_array_list_t** services); +celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, celix_array_list_t** services); celix_status_t remoteServiceAdmin_importService(remote_service_admin_t *admin, endpoint_description_t *endpoint, import_registration_t **registration); celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_t *admin, import_registration_t *registration); diff --git a/bundles/remote_services/rsa_common/src/import_registration_impl.c b/bundles/remote_services/rsa_common/src/import_registration_impl.c index 95c67db8d..afd4c5b69 100644 --- a/bundles/remote_services/rsa_common/src/import_registration_impl.c +++ b/bundles/remote_services/rsa_common/src/import_registration_impl.c @@ -84,8 +84,7 @@ celix_status_t importRegistrationFactory_create(celix_log_helper_t *helper, char (*registration_factory)->context = context; (*registration_factory)->bundle = NULL; (*registration_factory)->loghelper = helper; - - arrayList_create(&(*registration_factory)->registrations); + (*registration_factory)->registrations = celix_arrayList_create(); } return status; @@ -99,7 +98,7 @@ celix_status_t importRegistrationFactory_destroy(import_registration_factory_t * if (*registration_factory != NULL) { free((*registration_factory)->serviceName); - arrayList_destroy((*registration_factory)->registrations); + celix_arrayList_destroy((*registration_factory)->registrations); serviceTracker_destroy((*registration_factory)->proxyFactoryTracker); free(*registration_factory); diff --git a/bundles/remote_services/rsa_common/src/import_registration_impl.h b/bundles/remote_services/rsa_common/src/import_registration_impl.h index 1a11ccd3e..1740fe467 100644 --- a/bundles/remote_services/rsa_common/src/import_registration_impl.h +++ b/bundles/remote_services/rsa_common/src/import_registration_impl.h @@ -53,7 +53,7 @@ struct import_registration_factory { remote_proxy_factory_service_t *trackedFactory; service_tracker_t *proxyFactoryTracker; celix_bundle_context_t *context; - array_list_pt registrations; + celix_array_list_t* registrations; celix_bundle_t *bundle; }; diff --git a/bundles/remote_services/rsa_common/src/remote_interceptors_handler.c b/bundles/remote_services/rsa_common/src/remote_interceptors_handler.c index 234b40fbf..65c25207c 100644 --- a/bundles/remote_services/rsa_common/src/remote_interceptors_handler.c +++ b/bundles/remote_services/rsa_common/src/remote_interceptors_handler.c @@ -24,7 +24,6 @@ #include "celix_array_list.h" #include "celix_threads.h" #include "celix_utils.h" -#include "array_list.h" #include "remote_interceptors_handler.h" @@ -93,8 +92,8 @@ void remoteInterceptorsHandler_addInterceptor(void *handle, void *svc, const cel celixThreadMutex_lock(&handler->lock); bool exists = false; - for (uint32_t i = 0; i < arrayList_size(handler->interceptors); i++) { - entry_t *entry = arrayList_get(handler->interceptors, i); + for (uint32_t i = 0; i < celix_arrayList_size(handler->interceptors); i++) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i); if (entry->interceptor == svc) { exists = true; } @@ -116,10 +115,10 @@ void remoteInterceptorsHandler_removeInterceptor(void *handle, void *svc, const celixThreadMutex_lock(&handler->lock); - for (uint32_t i = 0; i < arrayList_size(handler->interceptors); i++) { - entry_t *entry = arrayList_get(handler->interceptors, i); + for (int i = 0; i < celix_arrayList_size(handler->interceptors); i++) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i); if (entry->interceptor == svc) { - arrayList_remove(handler->interceptors, i); + celix_arrayList_removeAt(handler->interceptors, i); free(entry); break; } @@ -133,12 +132,12 @@ bool remoteInterceptorHandler_invokePreExportCall(remote_interceptors_handler_t celixThreadMutex_lock(&handler->lock); - if (*metadata == NULL && arrayList_size(handler->interceptors) > 0) { + if (*metadata == NULL && celix_arrayList_size(handler->interceptors) > 0) { *metadata = celix_properties_create(); } - for (uint32_t i = arrayList_size(handler->interceptors); i > 0; i--) { - entry_t *entry = arrayList_get(handler->interceptors, i - 1); + for (uint32_t i = celix_arrayList_size(handler->interceptors); i > 0; i--) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i - 1); cont = entry->interceptor->preExportCall(entry->interceptor->handle, svcProperties, functionName, *metadata); if (!cont) { @@ -154,8 +153,8 @@ bool remoteInterceptorHandler_invokePreExportCall(remote_interceptors_handler_t void remoteInterceptorHandler_invokePostExportCall(remote_interceptors_handler_t *handler, const celix_properties_t *svcProperties, const char *functionName, celix_properties_t *metadata) { celixThreadMutex_lock(&handler->lock); - for (uint32_t i = arrayList_size(handler->interceptors); i > 0; i--) { - entry_t *entry = arrayList_get(handler->interceptors, i - 1); + for (uint32_t i = celix_arrayList_size(handler->interceptors); i > 0; i--) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i - 1); entry->interceptor->postExportCall(entry->interceptor->handle, svcProperties, functionName, metadata); } @@ -168,12 +167,12 @@ bool remoteInterceptorHandler_invokePreProxyCall(remote_interceptors_handler_t * celixThreadMutex_lock(&handler->lock); - if (*metadata == NULL && arrayList_size(handler->interceptors) > 0) { + if (*metadata == NULL && celix_arrayList_size(handler->interceptors) > 0) { *metadata = celix_properties_create(); } - for (uint32_t i = 0; i < arrayList_size(handler->interceptors); i++) { - entry_t *entry = arrayList_get(handler->interceptors, i); + for (uint32_t i = 0; i < celix_arrayList_size(handler->interceptors); i++) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i); cont = entry->interceptor->preProxyCall(entry->interceptor->handle, svcProperties, functionName, *metadata); if (!cont) { @@ -189,8 +188,8 @@ bool remoteInterceptorHandler_invokePreProxyCall(remote_interceptors_handler_t * void remoteInterceptorHandler_invokePostProxyCall(remote_interceptors_handler_t *handler, const celix_properties_t *svcProperties, const char *functionName, celix_properties_t *metadata) { celixThreadMutex_lock(&handler->lock); - for (uint32_t i = 0; i < arrayList_size(handler->interceptors); i++) { - entry_t *entry = arrayList_get(handler->interceptors, i); + for (uint32_t i = 0; i < celix_arrayList_size(handler->interceptors); i++) { + entry_t *entry = celix_arrayList_get(handler->interceptors, i); entry->interceptor->postProxyCall(entry->interceptor->handle, svcProperties, functionName, metadata); } diff --git a/bundles/remote_services/rsa_common/src/remote_service_admin_impl.h b/bundles/remote_services/rsa_common/src/remote_service_admin_impl.h index 00420d5f5..4d3a420a8 100644 --- a/bundles/remote_services/rsa_common/src/remote_service_admin_impl.h +++ b/bundles/remote_services/rsa_common/src/remote_service_admin_impl.h @@ -37,10 +37,10 @@ celix_status_t remoteServiceAdmin_destroy(remote_service_admin_t **admin); celix_status_t remoteServiceAdmin_send(remote_service_admin_t *rsa, endpoint_description_t *endpointDescription, char *methodSignature, char **reply, int* replyStatus); -celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, array_list_pt *registrations); +celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, celix_array_list_t** registrations); celix_status_t remoteServiceAdmin_removeExportedService(remote_service_admin_t *admin, export_registration_t *registration); -celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, array_list_pt *services); -celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, array_list_pt *services); +celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, celix_array_list_t** services); +celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, celix_array_list_t** services); celix_status_t remoteServiceAdmin_importService(remote_service_admin_t *admin, endpoint_description_t *endpoint, import_registration_t **registration); celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_t *admin, import_registration_t *registration); diff --git a/bundles/remote_services/rsa_spi/include/endpoint_description.h b/bundles/remote_services/rsa_spi/include/endpoint_description.h index e9ba63e5d..e94517dc9 100644 --- a/bundles/remote_services/rsa_spi/include/endpoint_description.h +++ b/bundles/remote_services/rsa_spi/include/endpoint_description.h @@ -28,7 +28,6 @@ #define ENDPOINT_DESCRIPTION_H_ #include "properties.h" -#include "array_list.h" #include "celix_cleanup.h" #include diff --git a/bundles/remote_services/rsa_spi/include/endpoint_listener.h b/bundles/remote_services/rsa_spi/include/endpoint_listener.h index 68bad113e..0d44440cc 100644 --- a/bundles/remote_services/rsa_spi/include/endpoint_listener.h +++ b/bundles/remote_services/rsa_spi/include/endpoint_listener.h @@ -27,7 +27,6 @@ #ifndef ENDPOINT_LISTENER_H_ #define ENDPOINT_LISTENER_H_ -#include "array_list.h" #include "properties.h" #include "endpoint_description.h" diff --git a/bundles/remote_services/rsa_spi/include/remote_service_admin.h b/bundles/remote_services/rsa_spi/include/remote_service_admin.h index 346b11939..0bca98d3e 100644 --- a/bundles/remote_services/rsa_spi/include/remote_service_admin.h +++ b/bundles/remote_services/rsa_spi/include/remote_service_admin.h @@ -41,10 +41,10 @@ typedef struct remote_service_admin remote_service_admin_t; struct remote_service_admin_service { remote_service_admin_t *admin; - celix_status_t (*exportService)(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, array_list_pt *registrations); + celix_status_t (*exportService)(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, celix_array_list_t** registrations); celix_status_t (*removeExportedService)(remote_service_admin_t *admin, export_registration_t *registration); - celix_status_t (*getExportedServices)(remote_service_admin_t *admin, array_list_pt *services); - celix_status_t (*getImportedEndpoints)(remote_service_admin_t *admin, array_list_pt *services); + celix_status_t (*getExportedServices)(remote_service_admin_t *admin, celix_array_list_t** services); + celix_status_t (*getImportedEndpoints)(remote_service_admin_t *admin, celix_array_list_t** services); celix_status_t (*importService)(remote_service_admin_t *admin, endpoint_description_t *endpoint, import_registration_t **registration); celix_status_t (*exportReference_getExportedEndpoint)(export_reference_t *reference, endpoint_description_t **endpoint); diff --git a/bundles/remote_services/topology_manager/src/scope.c b/bundles/remote_services/topology_manager/src/scope.c index cd5cf6b97..3c4af2a98 100644 --- a/bundles/remote_services/topology_manager/src/scope.c +++ b/bundles/remote_services/topology_manager/src/scope.c @@ -26,6 +26,8 @@ #include "utils.h" #include "filter.h" +static bool import_equal(celix_array_list_entry_t src, celix_array_list_entry_t dest); + struct scope_item { celix_properties_t *props; }; @@ -36,14 +38,12 @@ struct scope { hash_map_pt exportScopes; // key is filter, value is scope_item (properties set) celix_thread_mutex_t importScopeLock; - array_list_pt importScopes; // list of filters + celix_array_list_t* importScopes; // list of filters celix_status_t (*exportScopeChangedHandler)(void* manager, char *filter); celix_status_t (*importScopeChangedHandler)(void* manager, char *filter); }; -static celix_status_t import_equal(const void *, const void *, bool *equals); - /* * SERVICES */ @@ -120,10 +120,13 @@ celix_status_t tm_addImportScope(void *handle, char *filter) { return CELIX_ILLEGAL_ARGUMENT; // filter not parsable } if (celixThreadMutex_lock(&scope->importScopeLock) == CELIX_SUCCESS) { - int index = arrayList_indexOf(scope->importScopes, new); - filter_pt present = (filter_pt) arrayList_get(scope->importScopes, index); + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = new; + int index = celix_arrayList_indexOf(scope->importScopes, entry); + filter_pt present = (filter_pt) celix_arrayList_get(scope->importScopes, index); if (present == NULL) { - arrayList_add(scope->importScopes, celix_steal_ptr(new)); + celix_arrayList_add(scope->importScopes, celix_steal_ptr(new)); } else { status = CELIX_ILLEGAL_ARGUMENT; } @@ -150,12 +153,15 @@ celix_status_t tm_removeImportScope(void *handle, char *filter) { } if (celixThreadMutex_lock(&scope->importScopeLock) == CELIX_SUCCESS) { - int index = arrayList_indexOf(scope->importScopes, new); - filter_pt present = (filter_pt) arrayList_get(scope->importScopes, index); + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = new; + int index = celix_arrayList_indexOf(scope->importScopes, entry); + filter_pt present = (filter_pt) celix_arrayList_get(scope->importScopes, index); if (present == NULL) status = CELIX_ILLEGAL_ARGUMENT; else { - arrayList_removeElement(scope->importScopes, present); + celix_arrayList_remove(scope->importScopes, present); filter_destroy(present); } celixThreadMutex_unlock(&scope->importScopeLock); @@ -193,7 +199,9 @@ celix_status_t scope_scopeCreate(void *handle, scope_pt *scope) { celixThreadMutex_create(&(*scope)->importScopeLock, NULL); (*scope)->exportScopes = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); - arrayList_createWithEquals(import_equal, &((*scope)->importScopes)); + celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS; + opts.equalsCallback = import_equal; + (*scope)->importScopes = celix_arrayList_createWithOptions(&opts); (*scope)->exportScopeChangedHandler = NULL; return status; @@ -215,14 +223,11 @@ celix_status_t scope_scopeDestroy(scope_pt scope) { } if (celixThreadMutex_lock(&scope->importScopeLock) == CELIX_SUCCESS) { - array_list_iterator_pt imp_iter = arrayListIterator_create(scope->importScopes); - while (arrayListIterator_hasNext(imp_iter)) { - filter_pt element = (filter_pt) arrayListIterator_next(imp_iter); + for (int i = 0; i < celix_arrayList_size(scope->importScopes); i++) { + filter_pt element = (filter_pt) celix_arrayList_get(scope->importScopes, i); filter_destroy(element); - // no need to call arrayList_removeElement(element) because complete list is destroyed } - arrayListIterator_destroy(imp_iter); - arrayList_destroy(scope->importScopes); + celix_arrayList_destroy(scope->importScopes); celixThreadMutex_unlock(&scope->importScopeLock); } @@ -235,29 +240,30 @@ celix_status_t scope_scopeDestroy(scope_pt scope) { /***************************************************************************** * STATIC FUNCTIONS *****************************************************************************/ -static celix_status_t import_equal(const void *src, const void *dest, bool *equals) { +static bool import_equal(celix_array_list_entry_t src, celix_array_list_entry_t dest) { celix_status_t status; - filter_pt src_filter = (filter_pt) src; - filter_pt dest_filter = (filter_pt) dest; - status = filter_match_filter(src_filter, dest_filter, equals); - return status; + filter_pt src_filter = (filter_pt) src.voidPtrVal; + filter_pt dest_filter = (filter_pt) dest.voidPtrVal; + bool result; + status = filter_match_filter(src_filter, dest_filter, &result); + return (status == CELIX_SUCCESS) && result; } bool scope_allowImport(scope_pt scope, endpoint_description_t *endpoint) { bool allowImport = false; - array_list_iterator_pt iter; if (celixThreadMutex_lock(&(scope->importScopeLock)) == CELIX_SUCCESS) { - if (arrayList_size(scope->importScopes) == 0) { + if (celix_arrayList_size(scope->importScopes) == 0) { allowImport = true; } else { - iter = arrayListIterator_create(scope->importScopes); - while ((allowImport == false) && arrayListIterator_hasNext(iter)) { - filter_pt element = (filter_pt) arrayListIterator_next(iter); + for (int i = 0; i < celix_arrayList_size(scope->importScopes); i++) { + filter_pt element = (filter_pt) celix_arrayList_get(scope->importScopes, i); filter_match(element, endpoint->properties, &allowImport); + if (allowImport) { + break; + } } - arrayListIterator_destroy(iter); } celixThreadMutex_unlock(&scope->importScopeLock); } diff --git a/bundles/remote_services/topology_manager/src/topology_manager.c b/bundles/remote_services/topology_manager/src/topology_manager.c index 5c91fefa7..a152e6691 100644 --- a/bundles/remote_services/topology_manager/src/topology_manager.c +++ b/bundles/remote_services/topology_manager/src/topology_manager.c @@ -836,12 +836,12 @@ static celix_status_t topologyManager_extendFilter(topology_manager_pt manager, return status; } -celix_status_t topologyManager_listenerAdded(void *handle, array_list_pt listeners) { +celix_status_t topologyManager_listenerAdded(void *handle, celix_array_list_t* listeners) { celix_status_t status = CELIX_SUCCESS; topology_manager_pt manager = handle; - for (int i = 0; i < arrayList_size(listeners); i++) { - listener_hook_info_pt info = arrayList_get(listeners, i); + for (int i = 0; i < celix_arrayList_size(listeners); i++) { + listener_hook_info_pt info = celix_arrayList_get(listeners, i); celix_bundle_t *bundle = NULL, *self = NULL; bundleContext_getBundle(info->context, &bundle); bundleContext_getBundle(manager->context, &self); @@ -886,12 +886,12 @@ celix_status_t topologyManager_listenerAdded(void *handle, array_list_pt listene return status; } -celix_status_t topologyManager_listenerRemoved(void *handle, array_list_pt listeners) { +celix_status_t topologyManager_listenerRemoved(void *handle, celix_array_list_t* listeners) { celix_status_t status = CELIX_SUCCESS; topology_manager_pt manager = handle; - for (int i = 0; i < arrayList_size(listeners); i++) { - listener_hook_info_pt info = arrayList_get(listeners, i); + for (int i = 0; i < celix_arrayList_size(listeners); i++) { + listener_hook_info_pt info = celix_arrayList_get(listeners, i); celix_bundle_t *bundle = NULL, *self = NULL; bundleContext_getBundle(info->context, &bundle); diff --git a/bundles/remote_services/topology_manager/src/topology_manager.h b/bundles/remote_services/topology_manager/src/topology_manager.h index e186d5761..5c2cd9388 100644 --- a/bundles/remote_services/topology_manager/src/topology_manager.h +++ b/bundles/remote_services/topology_manager/src/topology_manager.h @@ -60,7 +60,7 @@ celix_status_t topologyManager_removeImportedService(void *handle, endpoint_desc celix_status_t topologyManager_addExportedService(void * handle, service_reference_pt reference, void * service); celix_status_t topologyManager_removeExportedService(void * handle, service_reference_pt reference, void * service); -celix_status_t topologyManager_listenerAdded(void *handle, array_list_pt listeners); -celix_status_t topologyManager_listenerRemoved(void *handle, array_list_pt listeners); +celix_status_t topologyManager_listenerAdded(void *handle, celix_array_list_t* listeners); +celix_status_t topologyManager_listenerRemoved(void *handle, celix_array_list_t* listeners); #endif /* TOPOLOGY_MANAGER_H_ */ diff --git a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_activator.c b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_activator.c index bd5328bf4..8c1574e89 100644 --- a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_activator.c +++ b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_activator.c @@ -44,12 +44,12 @@ celix_status_t celix_bundleActivator_create(celix_bundle_context_t *context, voi discMockService_create(act, &act->serv); act->endpointListener = NULL; act->endpointListenerService = NULL; - status = arrayList_create(&act->endpointList); + act->endpointList = celix_arrayList_create(); } else { status = CELIX_ENOMEM; } - if (status == CELIX_SUCCESS) { + if (status == CELIX_SUCCESS && act->endpointList) { *out = act; } else if (act != NULL) { free(act); @@ -127,7 +127,7 @@ celix_status_t celix_bundleActivator_destroy(void * userData, celix_bundle_conte discMockService_destroy(act->serv); free(act->endpointListener); - arrayList_destroy(act->endpointList); + celix_arrayList_destroy(act->endpointList); free(act); } return CELIX_SUCCESS; @@ -138,7 +138,7 @@ celix_status_t discovery_endpointAdded(void *handle, endpoint_description_t *end struct disc_mock_activator *act = handle; printf("%s\n", __func__); - arrayList_add(act->endpointList, endpoint); + celix_arrayList_add(act->endpointList, endpoint); return status; } @@ -147,7 +147,7 @@ celix_status_t discovery_endpointRemoved(void *handle, endpoint_description_t *e celix_status_t status = CELIX_SUCCESS; struct disc_mock_activator *act = handle; printf("%s\n", __func__); - arrayList_removeElement(act->endpointList, endpoint); + celix_arrayList_remove(act->endpointList, endpoint); return status; } diff --git a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.c b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.c index 047d90b3b..b77f20cb0 100644 --- a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.c +++ b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.c @@ -31,7 +31,7 @@ #include "disc_mock_service.h" -celix_status_t test(void *handle, array_list_pt *descrList); +celix_status_t test(void *handle, celix_array_list_t** descrList); celix_status_t discMockService_create(void *handle, disc_mock_service_t **serv) { *serv = calloc(1, sizeof(struct disc_mock_service)); @@ -49,7 +49,7 @@ celix_status_t discMockService_destroy(disc_mock_service_t *serv) { return CELIX_SUCCESS; } -celix_status_t test(void *handle, array_list_pt *descrList) { +celix_status_t test(void *handle, celix_array_list_t** descrList) { struct disc_mock_activator *act = handle; *descrList = act->endpointList; diff --git a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.h b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.h index 2635ae0a5..7d3a0d246 100644 --- a/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.h +++ b/bundles/remote_services/topology_manager/tms_tst/disc_mock/disc_mock_service.h @@ -28,7 +28,7 @@ typedef struct disc_mock_service { void *handle;// disc_mock_activator_t* - celix_status_t (*getEPDescriptors)(void *handle, array_list_pt *descrList); + celix_status_t (*getEPDescriptors)(void *handle, celix_array_list_t** descrList); } disc_mock_service_t; @@ -42,7 +42,7 @@ struct disc_mock_activator { endpoint_listener_t *endpointListener; service_registration_t *endpointListenerService; - array_list_pt endpointList; + celix_array_list_t* endpointList; }; diff --git a/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp b/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp index 2636da9f8..d70bda87b 100644 --- a/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp +++ b/bundles/remote_services/topology_manager/tms_tst/tms_tests.cpp @@ -267,11 +267,11 @@ extern "C" { /// \TEST_CASE_DESC Checks if 3 bundles are installed after the framework setup static void testBundles(void) { printf("Begin: %s\n", __func__); - array_list_pt bundles = NULL; + celix_array_list_t* bundles = NULL; int rc = bundleContext_getBundles(context, &bundles); EXPECT_EQ(0, rc); - EXPECT_EQ(5, arrayList_size(bundles)); //framework, scopeService & calc & rsa + EXPECT_EQ(5, celix_arrayList_size(bundles)); //framework, scopeService & calc & rsa /* int size = arrayList_size(bundles); @@ -287,7 +287,7 @@ extern "C" { printf("got bundle with symbolic name '%s'", name); }*/ - arrayList_destroy(bundles); + celix_arrayList_destroy(bundles); printf("End: %s\n", __func__); } @@ -384,7 +384,7 @@ extern "C" { static void testScope(void) { int nr_exported; int nr_imported; - array_list_pt epList; + celix_array_list_t* epList; printf("\nBegin: %s\n", __func__); scopeInit("scope.json", &nr_exported, &nr_imported); @@ -393,9 +393,9 @@ extern "C" { discMock->getEPDescriptors(discMock->handle, &epList); // We export one service: Calculator, which has DFI bundle info - EXPECT_EQ(1, arrayList_size(epList)); - for (unsigned int i = 0; i < arrayList_size(epList); i++) { - endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i); + EXPECT_EQ(1, celix_arrayList_size(epList)); + for (int i = 0; i < celix_arrayList_size(epList); i++) { + endpoint_description_t *ep = (endpoint_description_t *) celix_arrayList_get(epList, i); celix_properties_t *props = ep->properties; const char* value = celix_properties_get(props, "key2", ""); EXPECT_STREQ("inaetics", value); @@ -421,16 +421,16 @@ extern "C" { static void testScope2(void) { int nr_exported; int nr_imported; - array_list_pt epList; + celix_array_list_t* epList; printf("\nBegin: %s\n", __func__); scopeInit("scope2.json", &nr_exported, &nr_imported); EXPECT_EQ(3, nr_exported); EXPECT_EQ(1, nr_imported); discMock->getEPDescriptors(discMock->handle, &epList); // We export one service: Calculator, which has DFI bundle info - EXPECT_EQ(1, arrayList_size(epList)); - for (unsigned int i = 0; i < arrayList_size(epList); i++) { - endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i); + EXPECT_EQ(1, celix_arrayList_size(epList)); + for (int i = 0; i < celix_arrayList_size(epList); i++) { + endpoint_description_t *ep = (endpoint_description_t *) celix_arrayList_get(epList, i); celix_properties_t *props = ep->properties; const char* value = celix_properties_get(props, "key2", ""); EXPECT_STREQ("inaetics", value); @@ -445,16 +445,16 @@ extern "C" { static void testScope3(void) { int nr_exported; int nr_imported; - array_list_pt epList; + celix_array_list_t* epList; printf("\nBegin: %s\n", __func__); scopeInit("scope3.json", &nr_exported, &nr_imported); EXPECT_EQ(3, nr_exported); EXPECT_EQ(1, nr_imported); discMock->getEPDescriptors(discMock->handle, &epList); // We export one service: Calculator, which has DFI bundle info - EXPECT_EQ(1, arrayList_size(epList)); - for (unsigned int i = 0; i < arrayList_size(epList); i++) { - endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i); + EXPECT_EQ(1, celix_arrayList_size(epList)); + for (int i = 0; i < celix_arrayList_size(epList); i++) { + endpoint_description_t *ep = (endpoint_description_t *) celix_arrayList_get(epList, i); celix_properties_t *props = ep->properties; const char* value = celix_properties_get(props, "key2", ""); EXPECT_STREQ("inaetics", value); diff --git a/bundles/shell/remote_shell/src/remote_shell.c b/bundles/shell/remote_shell/src/remote_shell.c index 86c281b53..cdff97b30 100644 --- a/bundles/shell/remote_shell/src/remote_shell.c +++ b/bundles/shell/remote_shell/src/remote_shell.c @@ -28,11 +28,11 @@ #include #include #include -#include #include #include "celix_log_helper.h" #include "celix_utils.h" +#include "celix_array_list.h" #include "remote_shell.h" @@ -61,24 +61,29 @@ static celix_status_t remoteShell_connection_print(connection_pt connection, cha static celix_status_t remoteShell_connection_execute(connection_pt connection, char *command); static void* remoteShell_connection_run(void *data); -celix_status_t remoteShell_create(shell_mediator_pt mediator, int maximumConnections, remote_shell_pt *instance) { - celix_status_t status = CELIX_SUCCESS; - (*instance) = calloc(1, sizeof(**instance)); - if ((*instance) != NULL) { - (*instance)->mediator = mediator; - (*instance)->maximumConnections = maximumConnections; - (*instance)->connections = NULL; - (*instance)->loghelper = &mediator->loghelper; - - status = celixThreadMutex_create(&(*instance)->mutex, NULL); - - if (status == CELIX_SUCCESS) { - status = arrayList_create(&(*instance)->connections); - } - } else { - status = CELIX_ENOMEM; - } - return status; +celix_status_t remoteShell_create(shell_mediator_pt mediator, int maximumConnections, remote_shell_pt* instance) { + celix_status_t status = CELIX_SUCCESS; + (*instance) = calloc(1, sizeof(**instance)); + if ((*instance) != NULL) { + (*instance)->mediator = mediator; + (*instance)->maximumConnections = maximumConnections; + (*instance)->connections = NULL; + (*instance)->loghelper = &mediator->loghelper; + + status = celixThreadMutex_create(&(*instance)->mutex, NULL); + + if (status == CELIX_SUCCESS) { + (*instance)->connections = celix_arrayList_create(); + if (!(*instance)->connections) { + free(*instance); + (*instance) = NULL; + status = CELIX_ENOMEM; + } + } + } else { + status = CELIX_ENOMEM; + } + return status; } celix_status_t remoteShell_destroy(remote_shell_pt instance) { @@ -87,7 +92,7 @@ celix_status_t remoteShell_destroy(remote_shell_pt instance) { remoteShell_stopConnections(instance); celixThreadMutex_lock(&instance->mutex); - arrayList_destroy(instance->connections); + celix_arrayList_destroy(instance->connections); celixThreadMutex_unlock(&instance->mutex); return status; @@ -106,9 +111,9 @@ celix_status_t remoteShell_addConnection(remote_shell_pt instance, int socket) { celixThreadMutex_lock(&instance->mutex); - if (arrayList_size(instance->connections) < instance->maximumConnections) { + if (celix_arrayList_size(instance->connections) < instance->maximumConnections) { celix_thread_t connectionRunThread = celix_thread_default; - arrayList_add(instance->connections, connection); + celix_arrayList_add(instance->connections, connection); status = celixThread_create(&connectionRunThread, NULL, &remoteShell_connection_run, connection); } else { status = CELIX_BUNDLE_EXCEPTION; @@ -139,10 +144,10 @@ celix_status_t remoteShell_stopConnections(remote_shell_pt instance) { int i = 0; celixThreadMutex_lock(&instance->mutex); - length = arrayList_size(instance->connections); + length = celix_arrayList_size(instance->connections); for (i = 0; i < length; i += 1) { - connection_pt connection = arrayList_get(instance->connections, i); + connection_pt connection = celix_arrayList_get(instance->connections, i); connection->threadRunning = false; } @@ -204,7 +209,7 @@ void *remoteShell_connection_run(void *data) { celix_logHelper_log(*connection->parent->loghelper, CELIX_LOG_LEVEL_INFO, "REMOTE_SHELL: Closing socket"); celixThreadMutex_lock(&connection->parent->mutex); - arrayList_removeElement(connection->parent->connections, connection); + celix_arrayList_remove(connection->parent->connections, connection); celixThreadMutex_unlock(&connection->parent->mutex); fclose(connection->socketStream); diff --git a/bundles/shell/remote_shell/src/remote_shell.h b/bundles/shell/remote_shell/src/remote_shell.h index ee32ca80d..e95969feb 100644 --- a/bundles/shell/remote_shell/src/remote_shell.h +++ b/bundles/shell/remote_shell/src/remote_shell.h @@ -38,7 +38,7 @@ struct remote_shell { celix_thread_mutex_t mutex; int maximumConnections; - array_list_pt connections; + celix_array_list_t* connections; }; typedef struct remote_shell *remote_shell_pt; diff --git a/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc b/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc index 02c8e07e0..c46f48246 100644 --- a/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc +++ b/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc @@ -1289,13 +1289,13 @@ TEST_F(CelixBundleContextServicesTestSuite, FindServicesTest) { foundId = celix_bundleContext_findService(ctx, "example"); ASSERT_EQ(foundId, svcId1); //oldest should have highest ranking - array_list_t *list = celix_bundleContext_findServices(ctx, "non existing service name"); + celix_array_list_t *list = celix_bundleContext_findServices(ctx, "non existing service name"); ASSERT_EQ(0, celix_arrayList_size(list)); - arrayList_destroy(list); + celix_arrayList_destroy(list); list = celix_bundleContext_findServices(ctx, "example"); ASSERT_EQ(4, celix_arrayList_size(list)); - arrayList_destroy(list); + celix_arrayList_destroy(list); celix_bundleContext_unregisterService(ctx, svcId1); celix_bundleContext_unregisterService(ctx, svcId3); diff --git a/libs/framework/include/celix_api.h b/libs/framework/include/celix_api.h index 84f1b31fa..c4d5fcb86 100644 --- a/libs/framework/include/celix_api.h +++ b/libs/framework/include/celix_api.h @@ -20,12 +20,12 @@ #ifndef CELIX_CELIX_API_H_ #define CELIX_CELIX_API_H_ -#include "properties.h" -#include "array_list.h" +#include "celix_properties.h" +#include "celix_array_list.h" -#include "bundle.h" -#include "bundle_context.h" -#include "framework.h" +#include "celix_bundle.h" +#include "celix_bundle_context.h" +#include "celix_framework.h" #include "celix_properties.h" #include "celix_array_list.h" diff --git a/libs/framework/include_deprecated/bundle_context.h b/libs/framework/include_deprecated/bundle_context.h index 1ee6bfe33..3c12de469 100644 --- a/libs/framework/include_deprecated/bundle_context.h +++ b/libs/framework/include_deprecated/bundle_context.h @@ -35,7 +35,6 @@ #include "bundle_listener.h" #include "framework_listener.h" #include "properties.h" -#include "array_list.h" #include "celix_service_listener.h" #include "dm_dependency_manager.h" diff --git a/libs/framework/include_deprecated/bundle_revision.h b/libs/framework/include_deprecated/bundle_revision.h index 6b39ca4f7..372b9f9ef 100644 --- a/libs/framework/include_deprecated/bundle_revision.h +++ b/libs/framework/include_deprecated/bundle_revision.h @@ -27,7 +27,7 @@ #include "celix_errno.h" #include "manifest.h" #include "celix_log.h" -#include "array_list.h" +#include "celix_array_list.h" #include "celix_framework_export.h" #ifdef __cplusplus diff --git a/libs/framework/include_deprecated/dm_component.h b/libs/framework/include_deprecated/dm_component.h index abe1e42ed..b2d27cf54 100644 --- a/libs/framework/include_deprecated/dm_component.h +++ b/libs/framework/include_deprecated/dm_component.h @@ -32,7 +32,7 @@ #include "celix_types.h" #include "celix_errno.h" #include "properties.h" -#include "array_list.h" +#include "celix_array_list.h" #include "celix_dm_info.h" #include "celix_dm_component.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/dm_dependency_manager.h b/libs/framework/include_deprecated/dm_dependency_manager.h index ce322a3f4..cab5030f8 100644 --- a/libs/framework/include_deprecated/dm_dependency_manager.h +++ b/libs/framework/include_deprecated/dm_dependency_manager.h @@ -30,7 +30,6 @@ #include "celix_types.h" #include "celix_errno.h" -#include "array_list.h" #include "celix_dm_info.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/module.h b/libs/framework/include_deprecated/module.h index c17b07918..0aeabb642 100644 --- a/libs/framework/include_deprecated/module.h +++ b/libs/framework/include_deprecated/module.h @@ -34,7 +34,6 @@ typedef struct module celix_module_t; #include "manifest.h" #include "version.h" -#include "array_list.h" #include "bundle.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/service_reference.h b/libs/framework/include_deprecated/service_reference.h index 184c686df..35068cca5 100644 --- a/libs/framework/include_deprecated/service_reference.h +++ b/libs/framework/include_deprecated/service_reference.h @@ -23,7 +23,6 @@ #include #include "celix_types.h" -#include "array_list.h" #include "service_registration.h" #include "bundle.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/service_registration.h b/libs/framework/include_deprecated/service_registration.h index 7bef89bdd..256d7db03 100644 --- a/libs/framework/include_deprecated/service_registration.h +++ b/libs/framework/include_deprecated/service_registration.h @@ -23,7 +23,6 @@ #include #include "celix_types.h" -#include "array_list.h" #include "bundle.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/service_registry.h b/libs/framework/include_deprecated/service_registry.h index 40e568cca..d7ce53dc2 100644 --- a/libs/framework/include_deprecated/service_registry.h +++ b/libs/framework/include_deprecated/service_registry.h @@ -24,7 +24,7 @@ #include "filter.h" #include "service_factory.h" #include "celix_service_event.h" -#include "array_list.h" +#include "celix_array_list.h" #include "service_registration.h" #include "celix_service_factory.h" #include "celix_framework_export.h" diff --git a/libs/framework/include_deprecated/service_tracker.h b/libs/framework/include_deprecated/service_tracker.h index d65834f3c..5f75c8abf 100644 --- a/libs/framework/include_deprecated/service_tracker.h +++ b/libs/framework/include_deprecated/service_tracker.h @@ -21,7 +21,6 @@ #define SERVICE_TRACKER_H_ #include "celix_service_listener.h" -#include "array_list.h" #include "service_tracker_customizer.h" #include "bundle_context.h" #include "celix_bundle_context.h" diff --git a/libs/framework/src/bundle.c b/libs/framework/src/bundle.c index 731441b5b..6a687696a 100644 --- a/libs/framework/src/bundle.c +++ b/libs/framework/src/bundle.c @@ -83,21 +83,19 @@ celix_status_t celix_bundle_createFromArchive(celix_framework_t *framework, bund } celix_status_t bundle_destroy(bundle_pt bundle) { - array_list_iterator_pt iter = arrayListIterator_create(bundle->modules); - while (arrayListIterator_hasNext(iter)) { - module_pt module = arrayListIterator_next(iter); - module_destroy(module); - } - arrayListIterator_destroy(iter); - arrayList_destroy(bundle->modules); + for (int i = 0; i < celix_arrayList_size(bundle->modules); ++i) { + module_pt module = celix_arrayList_get(bundle->modules, i); + module_destroy(module); + } + celix_arrayList_destroy(bundle->modules); - free(bundle->symbolicName); + free(bundle->symbolicName); free(bundle->name); free(bundle->group); free(bundle->description); - free(bundle); + free(bundle); - return CELIX_SUCCESS; + return CELIX_SUCCESS; } celix_status_t bundle_getArchive(const_bundle_pt bundle, bundle_archive_pt *archive) { @@ -113,16 +111,16 @@ celix_status_t bundle_getArchive(const_bundle_pt bundle, bundle_archive_pt *arch celix_status_t bundle_getCurrentModule(const_bundle_pt bundle, module_pt *module) { celix_status_t status = CELIX_SUCCESS; - if (bundle == NULL || arrayList_size(bundle->modules)==0 ) { + if (bundle == NULL || celix_arrayList_size(bundle->modules)==0 ) { status = CELIX_ILLEGAL_ARGUMENT; } else { - *module = arrayList_get(bundle->modules, arrayList_size(bundle->modules) - 1); + *module = celix_arrayList_get(bundle->modules, celix_arrayList_size(bundle->modules) - 1); } return status; } -array_list_pt bundle_getModules(const_bundle_pt bundle) { +celix_array_list_t* bundle_getModules(const_bundle_pt bundle) { return bundle->modules; } @@ -345,7 +343,7 @@ celix_status_t bundle_closeRevisions(const_bundle_pt bundle) { celix_status_t bundle_refresh(bundle_pt bundle) { module_pt module; - arrayList_clear(bundle->modules); + celix_arrayList_clear(bundle->modules); celix_status_t status = bundle_createModule(bundle, &module); if (status == CELIX_SUCCESS) { status = bundle_addModule(bundle, module); @@ -370,7 +368,7 @@ celix_status_t bundle_getBundleId(const bundle_t *bundle, long *bndId) { return status; } -celix_status_t bundle_getRegisteredServices(bundle_pt bundle, array_list_pt *list) { +celix_status_t bundle_getRegisteredServices(bundle_pt bundle, celix_array_list_t** list) { celix_status_t status; status = fw_getBundleRegisteredServices(bundle->framework, bundle, list); @@ -380,7 +378,7 @@ celix_status_t bundle_getRegisteredServices(bundle_pt bundle, array_list_pt *lis return status; } -celix_status_t bundle_getServicesInUse(bundle_pt bundle, array_list_pt *list) { +celix_status_t bundle_getServicesInUse(bundle_pt bundle, celix_array_list_t** list) { celix_status_t status; status = fw_getBundleServicesInUse(bundle->framework, bundle, list); diff --git a/libs/framework/src/bundle_context.c b/libs/framework/src/bundle_context.c index d8b2c95ca..9785b06eb 100644 --- a/libs/framework/src/bundle_context.c +++ b/libs/framework/src/bundle_context.c @@ -178,7 +178,7 @@ celix_status_t bundleContext_registerServiceFactory(bundle_context_pt context, c return fw_registerServiceFactory(context->framework, service_registration, bndId, serviceName, factory, properties); } -celix_status_t bundleContext_getServiceReferences(bundle_context_pt context, const char * serviceName, const char * filter, array_list_pt *service_references) { +celix_status_t bundleContext_getServiceReferences(bundle_context_pt context, const char * serviceName, const char * filter, celix_array_list_t** service_references) { if (context == NULL || service_references == NULL) { return CELIX_ILLEGAL_ARGUMENT; } @@ -187,20 +187,20 @@ celix_status_t bundleContext_getServiceReferences(bundle_context_pt context, con celix_status_t bundleContext_getServiceReference(bundle_context_pt context, const char * serviceName, service_reference_pt *service_reference) { service_reference_pt reference = NULL; - array_list_pt services = NULL; + celix_array_list_t* services = NULL; celix_status_t status = CELIX_SUCCESS; if (serviceName != NULL) { if (bundleContext_getServiceReferences(context, serviceName, NULL, &services) == CELIX_SUCCESS) { - unsigned int size = arrayList_size(services); + unsigned int size = celix_arrayList_size(services); for(unsigned int i = 0; i < size; i++) { if(i == 0) { - reference = arrayList_get(services, 0); + reference = celix_arrayList_get(services, 0); } else { - bundleContext_ungetServiceReference(context, arrayList_get(services, i)); + bundleContext_ungetServiceReference(context, celix_arrayList_get(services, i)); } } - arrayList_destroy(services); + celix_arrayList_destroy(services); *service_reference = reference; } else { status = CELIX_ILLEGAL_ARGUMENT; @@ -251,7 +251,7 @@ celix_status_t bundleContext_ungetService(bundle_context_pt context, service_ref return serviceReference_ungetService(reference, result); } -celix_status_t bundleContext_getBundles(bundle_context_pt context, array_list_pt *bundles) { +celix_status_t bundleContext_getBundles(bundle_context_pt context, celix_array_list_t** bundles) { if (context == NULL || bundles == NULL) { return CELIX_ILLEGAL_ARGUMENT; } @@ -1387,7 +1387,7 @@ static celix_status_t bundleContext_callServicedTrackerTrackerCallback(void *han if (entry != NULL) { size_t size = celix_arrayList_size(listeners); for (unsigned int i = 0; i < size; ++i) { - listener_hook_info_pt info = arrayList_get(listeners, i); + listener_hook_info_pt info = celix_arrayList_get(listeners, i); celix_bundle_t *bnd = NULL; bundleContext_getBundle(info->context, &bnd); diff --git a/libs/framework/src/bundle_context_private.h b/libs/framework/src/bundle_context_private.h index fa2c2c9c3..38482d1d6 100644 --- a/libs/framework/src/bundle_context_private.h +++ b/libs/framework/src/bundle_context_private.h @@ -80,7 +80,7 @@ struct celix_bundle_context { celix_bundle_t *bundle; celix_thread_mutex_t mutex; //protects fields below (NOTE/FIXME also used by bundle.c for listing service tracker usage) - array_list_t *svcRegistrations; //serviceIds + celix_array_list_t *svcRegistrations; //serviceIds celix_dependency_manager_t *mng; long nextTrackerId; hash_map_t *bundleTrackers; //key = trackerId, value = celix_bundle_context_bundle_tracker_entry_t* diff --git a/libs/framework/src/bundle_private.h b/libs/framework/src/bundle_private.h index 1b3471cf1..f061a9015 100644 --- a/libs/framework/src/bundle_private.h +++ b/libs/framework/src/bundle_private.h @@ -37,7 +37,7 @@ struct celix_bundle { bundle_state_e state; void *handle; bundle_archive_pt archive; - array_list_pt modules; + celix_array_list_t* modules; celix_framework_t *framework; }; diff --git a/libs/framework/src/dm_component_impl.c b/libs/framework/src/dm_component_impl.c index 8ba8def34..2d20ff5a3 100644 --- a/libs/framework/src/dm_component_impl.c +++ b/libs/framework/src/dm_component_impl.c @@ -281,7 +281,7 @@ celix_status_t celix_dmComponent_addServiceDependency(celix_dm_component_t *comp celix_dmServiceDependency_setComponent(dep, component); celixThreadMutex_lock(&component->mutex); - arrayList_add(component->dependencies, dep); + celix_arrayList_add(component->dependencies, dep); bool startDep = celix_dmComponent_currentState(component) != CELIX_DM_CMP_STATE_INACTIVE; if (startDep) { celix_dmServiceDependency_enable(dep); @@ -678,7 +678,7 @@ static celix_status_t celix_dmComponent_handleSet(celix_dm_component_t *componen */ static celix_status_t celix_dmComponent_enableDependencies(celix_dm_component_t *component) { for (int i = 0; i < celix_arrayList_size(component->dependencies); i++) { - celix_dm_service_dependency_t *dependency = arrayList_get(component->dependencies, i); + celix_dm_service_dependency_t *dependency = celix_arrayList_get(component->dependencies, i); celix_dmServiceDependency_enable(dependency); } return CELIX_SUCCESS; @@ -689,7 +689,7 @@ static celix_status_t celix_dmComponent_enableDependencies(celix_dm_component_t */ static celix_status_t celix_dmComponent_disableDependencies(celix_dm_component_t *component) { for (int i = 0; i < celix_arrayList_size(component->dependencies); i++) { - celix_dm_service_dependency_t *dependency = arrayList_get(component->dependencies, i); + celix_dm_service_dependency_t *dependency = celix_arrayList_get(component->dependencies, i); celix_dmServiceDependency_disable(dependency); } return CELIX_SUCCESS; @@ -899,7 +899,7 @@ static celix_status_t celix_dmComponent_registerServices(celix_dm_component_t *c celixThreadMutex_lock(&component->mutex); } for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); i++) { - dm_interface_t *interface = arrayList_get(component->providedInterfaces, i); + dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i); if (interface->svcId == -1L) { celix_properties_t *regProps = celix_properties_copy(interface->properties); celix_service_registration_options_t opts = CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS; @@ -934,7 +934,7 @@ static celix_status_t celix_dmComponent_unregisterServices(celix_dm_component_t } celix_array_list_t* ids = NULL; for (int i = 0; i < celix_arrayList_size(component->providedInterfaces); ++i) { - dm_interface_t *interface = arrayList_get(component->providedInterfaces, i); + dm_interface_t *interface = celix_arrayList_get(component->providedInterfaces, i); if (interface->svcId == -1) { continue; } diff --git a/libs/framework/src/dm_dependency_manager_impl.c b/libs/framework/src/dm_dependency_manager_impl.c index 2b5aee9a5..8c5744287 100644 --- a/libs/framework/src/dm_dependency_manager_impl.c +++ b/libs/framework/src/dm_dependency_manager_impl.c @@ -367,13 +367,13 @@ celix_status_t dependencyManager_getInfo(celix_dependency_manager_t *manager, dm celixThreadMutex_lock(&manager->mutex); if (info != NULL) { - arrayList_create(&info->components); - size = arrayList_size(manager->components); + celix_arrayList_create(&info->components); + size = celix_arrayList_size(manager->components); for (i = 0; i < size; i += 1) { - celix_dm_component_t *cmp = arrayList_get(manager->components, i); + celix_dm_component_t *cmp = celix_arrayList_get(manager->components, i); cmpInfo = NULL; component_getComponentInfo(cmp, &cmpInfo); - arrayList_add(info->components, cmpInfo); + celix_arrayList_add(info->components, cmpInfo); } } else { status = CELIX_ENOMEM; diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index bdfd0ee67..ddef1e150 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -348,10 +348,10 @@ celix_status_t framework_destroy(framework_pt framework) { hashMap_destroy(framework->installRequestMap, false, false); if (framework->bundleListeners) { - arrayList_destroy(framework->bundleListeners); + celix_arrayList_destroy(framework->bundleListeners); } if (framework->frameworkListeners) { - arrayList_destroy(framework->frameworkListeners); + celix_arrayList_destroy(framework->frameworkListeners); } assert(celix_arrayList_size(framework->dispatcher.dynamicEventQueue) == 0); @@ -706,55 +706,55 @@ bool celix_framework_isBundleAlreadyInstalled(celix_framework_t* fw, const char* return alreadyExists; } -celix_status_t fw_getDependentBundles(framework_pt framework, bundle_pt exporter, array_list_pt *list) { +celix_status_t fw_getDependentBundles(framework_pt framework, bundle_pt exporter, celix_array_list_t** list) { celix_status_t status = CELIX_SUCCESS; if (*list != NULL || exporter == NULL || framework == NULL) { - return CELIX_ILLEGAL_ARGUMENT; - } - - array_list_pt modules; - unsigned int modIdx = 0; - arrayList_create(list); - - modules = bundle_getModules(exporter); - for (modIdx = 0; modIdx < arrayList_size(modules); modIdx++) { - module_pt module = (module_pt) arrayList_get(modules, modIdx); - array_list_pt dependents = module_getDependents(module); - if(dependents!=NULL){ - unsigned int depIdx = 0; - for (depIdx = 0; depIdx < arrayList_size(dependents); depIdx++) { - module_pt dependent = (module_pt) arrayList_get(dependents, depIdx); - arrayList_add(*list, module_getBundle(dependent)); - } - arrayList_destroy(dependents); - } - } + return CELIX_ILLEGAL_ARGUMENT; + } + + celix_array_list_t* modules; + unsigned int modIdx = 0; + *list = celix_arrayList_create(); + + modules = bundle_getModules(exporter); + for (modIdx = 0; modIdx < celix_arrayList_size(modules); modIdx++) { + module_pt module = (module_pt)celix_arrayList_get(modules, modIdx); + celix_array_list_t* dependents = module_getDependents(module); + if (dependents != NULL) { + unsigned int depIdx = 0; + for (depIdx = 0; depIdx < celix_arrayList_size(dependents); depIdx++) { + module_pt dependent = (module_pt)celix_arrayList_get(dependents, depIdx); + celix_arrayList_add(*list, module_getBundle(dependent)); + } + celix_arrayList_destroy(dependents); + } + } framework_logIfError(framework->logger, status, NULL, "Cannot get dependent bundles"); return status; } -celix_status_t fw_populateDependentGraph(framework_pt framework, bundle_pt exporter, hash_map_pt *map) { +celix_status_t fw_populateDependentGraph(framework_pt framework, bundle_pt exporter, hash_map_pt* map) { celix_status_t status = CELIX_SUCCESS; - if(framework == NULL || exporter == NULL){ - return CELIX_ILLEGAL_ARGUMENT; + if (framework == NULL || exporter == NULL) { + return CELIX_ILLEGAL_ARGUMENT; } - array_list_pt dependents = NULL; + celix_array_list_t* dependents = NULL; if ((status = fw_getDependentBundles(framework, exporter, &dependents)) == CELIX_SUCCESS) { - if(dependents!=NULL){ - unsigned int depIdx = 0; - for (depIdx = 0; depIdx < arrayList_size(dependents); depIdx++) { - if (!hashMap_containsKey(*map, arrayList_get(dependents, depIdx))) { - hashMap_put(*map, arrayList_get(dependents, depIdx), arrayList_get(dependents, depIdx)); - fw_populateDependentGraph(framework, (bundle_pt) arrayList_get(dependents, depIdx), map); - } - } - arrayList_destroy(dependents); - } + if (dependents != NULL) { + unsigned int depIdx = 0; + for (depIdx = 0; depIdx < celix_arrayList_size(dependents); depIdx++) { + if (!hashMap_containsKey(*map, celix_arrayList_get(dependents, depIdx))) { + hashMap_put(*map, celix_arrayList_get(dependents, depIdx), celix_arrayList_get(dependents, depIdx)); + fw_populateDependentGraph(framework, (bundle_pt)celix_arrayList_get(dependents, depIdx), map); + } + } + celix_arrayList_destroy(dependents); + } } framework_logIfError(framework->logger, status, NULL, "Cannot populate dependent graph"); @@ -798,11 +798,11 @@ celix_status_t fw_registerServiceFactory(framework_pt framework, service_registr return status; } -celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *references, bundle_pt bundle, const char * serviceName, const char * sfilter) { +celix_status_t fw_getServiceReferences(framework_pt framework, celix_array_list_t** references, bundle_pt bundle, const char * serviceName, const char * sfilter) { celix_status_t status = CELIX_SUCCESS; celix_autoptr(celix_filter_t) filter = NULL; - unsigned int refIdx = 0; + int refIdx = 0; if (sfilter != NULL) { filter = celix_filter_create(sfilter); @@ -812,8 +812,8 @@ celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *re if (status == CELIX_SUCCESS) { - for (refIdx = 0; (*references != NULL) && refIdx < arrayList_size(*references); refIdx++) { - service_reference_pt ref = (service_reference_pt) arrayList_get(*references, refIdx); + for (refIdx = 0; (*references != NULL) && refIdx < celix_arrayList_size(*references); refIdx++) { + service_reference_pt ref = (service_reference_pt) celix_arrayList_get(*references, refIdx); service_registration_pt reg = NULL; const char* serviceNameObjectClass; properties_pt props = NULL; @@ -823,7 +823,7 @@ celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *re serviceNameObjectClass = properties_get(props, CELIX_FRAMEWORK_SERVICE_NAME); if (!serviceReference_isAssignableTo(ref, bundle, serviceNameObjectClass)) { serviceReference_release(ref, NULL); - arrayList_remove(*references, refIdx); + celix_arrayList_removeAt(*references, refIdx); refIdx--; } } @@ -835,11 +835,11 @@ celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *re return status; } -celix_status_t fw_getBundleRegisteredServices(framework_pt framework, bundle_pt bundle, array_list_pt *services) { +celix_status_t fw_getBundleRegisteredServices(framework_pt framework, bundle_pt bundle, celix_array_list_t** services) { return serviceRegistry_getRegisteredServices(framework->registry, bundle, services); } -celix_status_t fw_getBundleServicesInUse(framework_pt framework, bundle_pt bundle, array_list_pt *services) { +celix_status_t fw_getBundleServicesInUse(framework_pt framework, bundle_pt bundle, celix_array_list_t* *services) { celix_status_t status = CELIX_SUCCESS; status = serviceRegistry_getServicesInUse(framework->registry, bundle, services); return status; @@ -922,7 +922,7 @@ celix_status_t fw_removeBundleListener(framework_pt framework, bundle_pt bundle, fw_bundle_listener_pt bundleListener = NULL; celixThreadMutex_lock(&framework->bundleListenerLock); - for (int i = 0; i < arrayList_size(framework->bundleListeners); i++) { + for (int i = 0; i < celix_arrayList_size(framework->bundleListeners); i++) { bundleListener = celix_arrayList_get(framework->bundleListeners, i); if (bundleListener->listener == listener && bundleListener->bundle == bundle) { celix_arrayList_removeAt(framework->bundleListeners, i); @@ -953,7 +953,7 @@ celix_status_t fw_addFrameworkListener(framework_pt framework, bundle_pt bundle, frameworkListener->bundle = bundle; celixThreadMutex_lock(&framework->frameworkListenersLock); - arrayList_add(framework->frameworkListeners, frameworkListener); + celix_arrayList_add(framework->frameworkListeners, frameworkListener); celixThreadMutex_unlock(&framework->frameworkListenersLock); } @@ -963,14 +963,14 @@ celix_status_t fw_addFrameworkListener(framework_pt framework, bundle_pt bundle, } celix_status_t fw_removeFrameworkListener(framework_pt framework, bundle_pt bundle, framework_listener_pt listener) { - unsigned int i; + int i; fw_framework_listener_pt frameworkListener; celixThreadMutex_lock(&framework->frameworkListenersLock); - for (i = 0; i < arrayList_size(framework->frameworkListeners); i++) { - frameworkListener = (fw_framework_listener_pt) arrayList_get(framework->frameworkListeners, i); + for (i = 0; i < celix_arrayList_size(framework->frameworkListeners); i++) { + frameworkListener = (fw_framework_listener_pt) celix_arrayList_get(framework->frameworkListeners, i); if (frameworkListener->listener == listener && frameworkListener->bundle == bundle) { - arrayList_remove(framework->frameworkListeners, i); + celix_arrayList_removeAt(framework->frameworkListeners, i); free(frameworkListener); @@ -1029,11 +1029,10 @@ static celix_status_t framework_markBundleResolved(framework_pt framework, modul return status; } -array_list_pt framework_getBundles(framework_pt framework) { +celix_array_list_t* framework_getBundles(framework_pt framework) { //FIXME Note that this does not increase the use count of the bundle, which can lead to race conditions. //promote to use the celix_bundleContext_useBundle(s) functions and deprecated this one - array_list_pt bundles = NULL; - arrayList_create(&bundles); + celix_array_list_t* bundles = celix_arrayList_create(); celixThreadMutex_lock(&framework->installedBundles.mutex); int size = celix_arrayList_size(framework->installedBundles.entries); @@ -1233,13 +1232,13 @@ static void fw_handleEventRequest(celix_framework_t *framework, celix_framework_ celix_array_list_t *localListeners = celix_arrayList_create(); celixThreadMutex_lock(&framework->bundleListenerLock); for (int i = 0; i < celix_arrayList_size(framework->bundleListeners); ++i) { - fw_bundle_listener_pt listener = arrayList_get(framework->bundleListeners, i); + fw_bundle_listener_pt listener = celix_arrayList_get(framework->bundleListeners, i); fw_bundleListener_increaseUseCount(listener); celix_arrayList_add(localListeners, listener); } celixThreadMutex_unlock(&framework->bundleListenerLock); for (int i = 0; i < celix_arrayList_size(localListeners); ++i) { - fw_bundle_listener_pt listener = arrayList_get(localListeners, i); + fw_bundle_listener_pt listener = celix_arrayList_get(localListeners, i); bundle_event_t bEvent; memset(&bEvent, 0, sizeof(bEvent)); diff --git a/libs/framework/src/framework_private.h b/libs/framework/src/framework_private.h index a5b299ff0..e903fa722 100644 --- a/libs/framework/src/framework_private.h +++ b/libs/framework/src/framework_private.h @@ -26,7 +26,7 @@ #include "framework.h" #include "manifest.h" #include "hash_map.h" -#include "array_list.h" +#include "celix_array_list.h" #include "celix_errno.h" #include "service_factory.h" #include "bundle_archive.h" @@ -135,10 +135,10 @@ struct celix_framework { long bundleId; //the bundle id of the framework (normally 0) hash_map_pt installRequestMap; - array_list_pt frameworkListeners; + celix_array_list_t* frameworkListeners; celix_thread_mutex_t frameworkListenersLock; - array_list_pt bundleListeners; + celix_array_list_t* bundleListeners; celix_thread_mutex_t bundleListenerLock; long currentBundleId; //atomic @@ -267,9 +267,9 @@ celix_status_t celix_framework_installBundleInternal(celix_framework_t* framewor celix_status_t fw_registerService(framework_pt framework, service_registration_pt * registration, long bundleId, const char* serviceName, const void* svcObj, properties_pt properties); celix_status_t fw_registerServiceFactory(framework_pt framework, service_registration_pt * registration, long bundleId, const char* serviceName, service_factory_pt factory, properties_pt properties); -celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *references, bundle_pt bundle, const char* serviceName, const char* filter); -celix_status_t fw_getBundleRegisteredServices(framework_pt framework, bundle_pt bundle, array_list_pt *services); -celix_status_t fw_getBundleServicesInUse(framework_pt framework, bundle_pt bundle, array_list_pt *services); +celix_status_t fw_getServiceReferences(framework_pt framework, celix_array_list_t** references, bundle_pt bundle, const char* serviceName, const char* filter); +celix_status_t fw_getBundleRegisteredServices(framework_pt framework, bundle_pt bundle, celix_array_list_t** services); +celix_status_t fw_getBundleServicesInUse(framework_pt framework, bundle_pt bundle, celix_array_list_t** services); void fw_addServiceListener(framework_pt framework, bundle_pt bundle, celix_service_listener_t *listener, const char* filter); void fw_removeServiceListener(framework_pt framework, bundle_pt bundle, celix_service_listener_t *listener); @@ -280,7 +280,7 @@ celix_status_t fw_removeBundleListener(framework_pt framework, bundle_pt bundle, celix_status_t fw_addFrameworkListener(framework_pt framework, bundle_pt bundle, framework_listener_pt listener); celix_status_t fw_removeFrameworkListener(framework_pt framework, bundle_pt bundle, framework_listener_pt listener); -array_list_pt framework_getBundles(framework_pt framework) __attribute__((deprecated("not thread safe, use celix_framework_useBundles instead"))); +celix_array_list_t* framework_getBundles(framework_pt framework) __attribute__((deprecated("not thread safe, use celix_framework_useBundles instead"))); long framework_getBundle(framework_pt framework, const char* location); bundle_pt framework_getBundleById(framework_pt framework, long id); diff --git a/libs/framework/src/module.c b/libs/framework/src/module.c index ed68c45af..2d2dcc9c1 100644 --- a/libs/framework/src/module.c +++ b/libs/framework/src/module.c @@ -198,7 +198,7 @@ bundle_pt module_getBundle(module_pt module) { return module->bundle; } -array_list_pt module_getDependentImporters(module_pt module) { +celix_array_list_t* module_getDependentImporters(module_pt module) { return NULL; } @@ -210,7 +210,7 @@ void module_removeDependentImporter(module_pt module, module_pt importer) { //---------------------------------------------------- //TODO add implementation (functions not implemented but already exported) -array_list_pt module_getDependentRequirers(module_pt module) { +celix_array_list_t* module_getDependentRequirers(module_pt module) { return NULL; } @@ -221,7 +221,7 @@ void module_removeDependentRequirer(module_pt module, module_pt requirer) { } //---------------------------------------------------- -array_list_pt module_getDependents(module_pt module) { +celix_array_list_t* module_getDependents(module_pt module) { return NULL; } diff --git a/libs/framework/src/registry_callback_private.h b/libs/framework/src/registry_callback_private.h index 90185217c..3c96dbbd4 100644 --- a/libs/framework/src/registry_callback_private.h +++ b/libs/framework/src/registry_callback_private.h @@ -35,7 +35,7 @@ typedef struct registry_callback_struct { void *handle; - celix_status_t (*getUsingBundles)(void *handle, service_registration_pt reg, array_list_pt *bundles); + celix_status_t (*getUsingBundles)(void *handle, service_registration_pt reg, celix_array_list_t** bundles); celix_status_t (*unregister)(void *handle, bundle_pt bundle, service_registration_pt reg); bool (*tryRemoveServiceReference)(void *handle, service_reference_pt ref); } registry_callback_t; diff --git a/libs/framework/src/service_reference.c b/libs/framework/src/service_reference.c index 21d1a2829..2d6460ec3 100644 --- a/libs/framework/src/service_reference.c +++ b/libs/framework/src/service_reference.c @@ -313,7 +313,7 @@ unsigned int serviceReference_hashCode(const void *referenceP) { } -celix_status_t serviceReference_getUsingBundles(service_reference_pt ref, array_list_pt *out) { +celix_status_t serviceReference_getUsingBundles(service_reference_pt ref, celix_array_list_t** out) { celix_status_t status = CELIX_SUCCESS; registry_callback_t callback; diff --git a/libs/framework/src/service_registry.c b/libs/framework/src/service_registry.c index 0dd6eaca3..e1d45c8b4 100644 --- a/libs/framework/src/service_registry.c +++ b/libs/framework/src/service_registry.c @@ -38,7 +38,7 @@ static bool serviceRegistry_tryRemoveServiceReference(service_registry_pt regist static celix_status_t serviceRegistry_addHooks(service_registry_pt registry, const char* serviceName, const void *serviceObject, service_registration_pt registration); static celix_status_t serviceRegistry_removeHook(service_registry_pt registry, service_registration_pt registration); static void serviceRegistry_logWarningServiceReferenceUsageCount(service_registry_pt registry, bundle_pt bundle, service_reference_pt ref, size_t usageCount, size_t refCount); -static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt registry, service_registration_pt reg, array_list_pt *bundles); +static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt registry, service_registration_pt reg, celix_array_list_t** bundles); static celix_status_t serviceRegistry_getServiceReference_internal(service_registry_pt registry, bundle_pt owner, service_registration_pt registration, service_reference_pt *out); static void celix_serviceRegistry_serviceChanged(celix_service_registry_t *registry, celix_service_event_type_t eventType, service_registration_pt registration); static void serviceRegistry_callHooksForListenerFilter(service_registry_pt registry, celix_bundle_t *owner, const celix_filter_t *filter, bool removed); @@ -94,7 +94,7 @@ void celix_serviceRegistry_destroy(celix_service_registry_t* registry) { celix_decreaseCountServiceListener(entry); celix_waitAndDestroyServiceListener(entry); } - arrayList_destroy(registry->serviceListeners); + celix_arrayList_destroy(registry->serviceListeners); //destroy service registration map size = hashMap_size(registry->serviceRegistrations); @@ -141,23 +141,23 @@ void celix_serviceRegistry_destroy(celix_service_registry_t* registry) { free(registry); } -celix_status_t serviceRegistry_getRegisteredServices(service_registry_pt registry, bundle_pt bundle, array_list_pt *services) { +celix_status_t serviceRegistry_getRegisteredServices(service_registry_pt registry, bundle_pt bundle, celix_array_list_t** services) { celix_status_t status = CELIX_SUCCESS; celixThreadRwlock_writeLock(®istry->lock); - array_list_pt regs = (array_list_pt) hashMap_get(registry->serviceRegistrations, bundle); + celix_array_list_t* regs = (celix_array_list_t*) hashMap_get(registry->serviceRegistrations, bundle); if (regs != NULL) { unsigned int i; - arrayList_create(services); + *services = celix_arrayList_create(); - for (i = 0; i < arrayList_size(regs); i++) { - service_registration_pt reg = arrayList_get(regs, i); + for (i = 0; i < celix_arrayList_size(regs); i++) { + service_registration_pt reg = celix_arrayList_get(regs, i); service_reference_pt reference = NULL; //assert(serviceRegistration_isValid(reg)); status = serviceRegistry_getServiceReference_internal(registry, bundle, reg, &reference); if (status == CELIX_SUCCESS) { - arrayList_add(*services, reference); + celix_arrayList_add(*services, reference); } } } @@ -178,7 +178,7 @@ celix_status_t serviceRegistry_registerServiceFactory(service_registry_pt regist } static celix_status_t serviceRegistry_registerServiceInternal(service_registry_pt registry, bundle_pt bundle, const char* serviceName, const void * serviceObject, properties_pt dictionary, long reservedId, enum celix_service_type svcType, service_registration_pt *registration) { - array_list_pt regs; + celix_array_list_t* regs; long svcId = reservedId > 0 ? reservedId : celix_serviceRegistry_nextSvcId(registry); celix_properties_setLong(dictionary, CELIX_FRAMEWORK_SERVICE_BUNDLE_ID, celix_bundle_getId(bundle)); @@ -201,13 +201,12 @@ static celix_status_t serviceRegistry_registerServiceInternal(service_registry_p } celixThreadRwlock_writeLock(®istry->lock); - regs = (array_list_pt) hashMap_get(registry->serviceRegistrations, bundle); + regs = (celix_array_list_t*) hashMap_get(registry->serviceRegistrations, bundle); if (regs == NULL) { - regs = NULL; - arrayList_create(®s); + regs = celix_arrayList_create(); hashMap_put(registry->serviceRegistrations, bundle, regs); } - arrayList_add(regs, *registration); + celix_arrayList_add(regs, *registration); //update pending register event celix_increasePendingRegisteredEvent(registry, svcId); @@ -226,56 +225,56 @@ static celix_status_t serviceRegistry_registerServiceInternal(service_registry_p return CELIX_SUCCESS; } -static celix_status_t serviceRegistry_unregisterService(service_registry_pt registry, bundle_pt bundle, service_registration_pt registration) { - // array_list_t clients; - celix_array_list_t *regs; +static celix_status_t serviceRegistry_unregisterService(service_registry_pt registry, + bundle_pt bundle, + service_registration_pt registration) { + // array_list_t clients; + celix_array_list_t* regs; - //fprintf(stderr, "REG: Unregistering service registration with pointer %p\n", registration); + // fprintf(stderr, "REG: Unregistering service registration with pointer %p\n", registration); long svcId = serviceRegistration_getServiceId(registration); - const char *svcName = NULL; + const char* svcName = NULL; serviceRegistration_getServiceName(registration, &svcName); - //printf("Unregistering service %li with name %s\n", svcId, svcName); + // printf("Unregistering service %li with name %s\n", svcId, svcName); if (strcmp(OSGI_FRAMEWORK_LISTENER_HOOK_SERVICE_NAME, svcName) == 0) { serviceRegistry_removeHook(registry, registration); } - celixThreadRwlock_writeLock(®istry->lock); - regs = (celix_array_list_t*) hashMap_get(registry->serviceRegistrations, bundle); - if (regs != NULL) { - arrayList_removeElement(regs, registration); - int size = arrayList_size(regs); + celixThreadRwlock_writeLock(®istry->lock); + regs = (celix_array_list_t*)hashMap_get(registry->serviceRegistrations, bundle); + if (regs != NULL) { + celix_arrayList_remove(regs, registration); + int size = celix_arrayList_size(regs); if (size == 0) { celix_arrayList_destroy(regs); hashMap_remove(registry->serviceRegistrations, bundle); } - } - celixThreadRwlock_unlock(®istry->lock); - + } + celixThreadRwlock_unlock(®istry->lock); - //check and wait for pending register events + // check and wait for pending register events celix_waitForPendingRegisteredEvents(registry, svcId); celix_serviceRegistry_serviceChanged(registry, OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING, registration); celixThreadRwlock_readLock(®istry->lock); - //invalidate service references + // invalidate service references hash_map_iterator_pt iter = hashMapIterator_create(registry->serviceReferences); while (hashMapIterator_hasNext(iter)) { hash_map_pt refsMap = hashMapIterator_nextValue(iter); - service_reference_pt ref = refsMap != NULL ? - hashMap_get(refsMap, (void*)registration->serviceId) : NULL; + service_reference_pt ref = refsMap != NULL ? hashMap_get(refsMap, (void*)registration->serviceId) : NULL; if (ref != NULL) { serviceReference_invalidateCache(ref); } } hashMapIterator_destroy(iter); serviceRegistration_invalidate(registration); - celixThreadRwlock_unlock(®istry->lock); + celixThreadRwlock_unlock(®istry->lock); serviceRegistration_release(registration); - return CELIX_SUCCESS; + return CELIX_SUCCESS; } celix_status_t serviceRegistry_getServiceReference(service_registry_pt registry, bundle_pt owner, @@ -324,66 +323,70 @@ static celix_status_t serviceRegistry_getServiceReference_internal(service_regis return status; } -celix_status_t serviceRegistry_getServiceReferences(service_registry_pt registry, bundle_pt owner, const char *serviceName, filter_pt filter, array_list_pt *out) { - celix_status_t status; - hash_map_iterator_pt iterator; - array_list_pt references = NULL; - array_list_pt matchingRegistrations = NULL; +celix_status_t serviceRegistry_getServiceReferences(service_registry_pt registry, + bundle_pt owner, + const char* serviceName, + filter_pt filter, + celix_array_list_t** out) { bool matchResult; + celix_autoptr(celix_array_list_t) references = celix_arrayList_create(); + celix_autoptr(celix_array_list_t) matchingRegistrations = celix_arrayList_create(); - status = arrayList_create(&references); - status = CELIX_DO_IF(status, arrayList_create(&matchingRegistrations)); + if (!references || !matchingRegistrations) { + fw_log(registry->framework->logger, CELIX_LOG_LEVEL_ERROR, "Cannot create service references, out of memory"); + return CELIX_ENOMEM; + } + celix_status_t status = CELIX_SUCCESS; celixThreadRwlock_readLock(®istry->lock); - iterator = hashMapIterator_create(registry->serviceRegistrations); - while (status == CELIX_SUCCESS && hashMapIterator_hasNext(iterator)) { - array_list_pt regs = (array_list_pt) hashMapIterator_nextValue(iterator); - unsigned int regIdx; - for (regIdx = 0; (regs != NULL) && regIdx < arrayList_size(regs); regIdx++) { - service_registration_pt registration = (service_registration_pt) arrayList_get(regs, regIdx); - properties_pt props = NULL; - - status = serviceRegistration_getProperties(registration, &props); - if (status == CELIX_SUCCESS) { - bool matched = false; - matchResult = false; - if (filter != NULL) { - filter_match(filter, props, &matchResult); - } - if ((serviceName == NULL) && ((filter == NULL) || matchResult)) { - matched = true; - } else if (serviceName != NULL) { - const char *className = NULL; - matchResult = false; - serviceRegistration_getServiceName(registration, &className); - if (filter != NULL) { - filter_match(filter, props, &matchResult); - } - if ((strcmp(className, serviceName) == 0) && ((filter == NULL) || matchResult)) { - matched = true; - } - } - if (matched) { + hash_map_iterator_t iterator = hashMapIterator_construct(registry->serviceRegistrations); + while (status == CELIX_SUCCESS && hashMapIterator_hasNext(&iterator)) { + celix_array_list_t* regs = hashMapIterator_nextValue(&iterator); + unsigned int regIdx; + for (regIdx = 0; (regs != NULL) && regIdx < celix_arrayList_size(regs); regIdx++) { + service_registration_pt registration = celix_arrayList_get(regs, regIdx); + properties_pt props = NULL; + + status = serviceRegistration_getProperties(registration, &props); + if (status == CELIX_SUCCESS) { + bool matched = false; + matchResult = false; + if (filter != NULL) { + filter_match(filter, props, &matchResult); + } + if ((serviceName == NULL) && ((filter == NULL) || matchResult)) { + matched = true; + } else if (serviceName != NULL) { + const char* className = NULL; + matchResult = false; + serviceRegistration_getServiceName(registration, &className); + if (filter != NULL) { + filter_match(filter, props, &matchResult); + } + if ((strcmp(className, serviceName) == 0) && ((filter == NULL) || matchResult)) { + matched = true; + } + } + if (matched) { // assert(serviceRegistration_isValid(registration)); serviceRegistration_retain(registration); - arrayList_add(matchingRegistrations, registration); - } - } - } - } + celix_arrayList_add(matchingRegistrations, registration); + } + } + } + } celixThreadRwlock_unlock(®istry->lock); - hashMapIterator_destroy(iterator); if (status == CELIX_SUCCESS) { unsigned int i; - unsigned int size = arrayList_size(matchingRegistrations); + unsigned int size = celix_arrayList_size(matchingRegistrations); for (i = 0; i < size; i += 1) { - service_registration_pt reg = arrayList_get(matchingRegistrations, i); + service_registration_pt reg = celix_arrayList_get(matchingRegistrations, i); service_reference_pt reference = NULL; celix_status_t subStatus = serviceRegistry_getServiceReference(registry, owner, reg, &reference); if (subStatus == CELIX_SUCCESS) { - arrayList_add(references, reference); + celix_arrayList_add(references, reference); } else { status = CELIX_BUNDLE_EXCEPTION; } @@ -391,23 +394,18 @@ celix_status_t serviceRegistry_getServiceReferences(service_registry_pt registry } } - if(matchingRegistrations != NULL){ - arrayList_destroy(matchingRegistrations); - } - if (status == CELIX_SUCCESS) { - *out = references; + *out = celix_steal_ptr(references); } else { - //TODO: test this branch using malloc mock - unsigned int size = arrayList_size(references); - for(unsigned int i = 0; i < size; i++) { - serviceReference_release(arrayList_get(references, i), NULL); + // TODO: test this branch using malloc mock + unsigned int size = celix_arrayList_size(references); + for (unsigned int i = 0; i < size; i++) { + serviceReference_release(celix_arrayList_get(references, i), NULL); } - arrayList_destroy(references); framework_logIfError(registry->framework->logger, status, NULL, "Cannot get service references"); } - return status; + return status; } static bool serviceRegistry_tryRemoveServiceReference(service_registry_pt registry, service_reference_pt reference) { @@ -518,32 +516,30 @@ celix_status_t serviceRegistry_clearReferencesFor(service_registry_pt registry, return status; } +celix_status_t +serviceRegistry_getServicesInUse(service_registry_pt registry, bundle_pt bundle, celix_array_list_t** out) { + celix_array_list_t* result = celix_arrayList_create(); -celix_status_t serviceRegistry_getServicesInUse(service_registry_pt registry, bundle_pt bundle, array_list_pt *out) { - - array_list_pt result = NULL; - arrayList_create(&result); - - //LOCK + // LOCK celixThreadRwlock_readLock(®istry->lock); hash_map_pt refsMap = hashMap_get(registry->serviceReferences, bundle); - if(refsMap) { + if (refsMap) { hash_map_iterator_pt iter = hashMapIterator_create(refsMap); while (hashMapIterator_hasNext(iter)) { service_reference_pt ref = hashMapIterator_nextValue(iter); - arrayList_add(result, ref); + celix_arrayList_add(result, ref); } hashMapIterator_destroy(iter); } - //UNLOCK + // UNLOCK celixThreadRwlock_unlock(®istry->lock); *out = result; - return CELIX_SUCCESS; + return CELIX_SUCCESS; } static celix_status_t serviceRegistry_addHooks(service_registry_pt registry, const char* serviceName, const void* serviceObject, service_registration_pt registration) { @@ -563,7 +559,7 @@ static celix_status_t serviceRegistry_addHooks(service_registry_pt registry, con long svcId = serviceRegistration_getServiceId(registration); entry = celix_createHookEntry(svcId, (celix_listener_hook_service_t*)serviceObject); celix_increaseCountHook(entry); - arrayList_add(registry->listenerHooks, entry); + celix_arrayList_add(registry->listenerHooks, entry); for (int i = 0; i < celix_arrayList_size(registry->serviceListeners); ++i) { celix_service_registry_service_listener_entry_t *listenerEntry = celix_arrayList_get(registry->serviceListeners, i); @@ -572,7 +568,7 @@ static celix_status_t serviceRegistry_addHooks(service_registry_pt registry, con } celixThreadRwlock_unlock(®istry->lock); - for (int i = 0; i < arrayList_size(listeners); ++i) { + for (int i = 0; i < celix_arrayList_size(listeners); ++i) { celix_service_registry_service_listener_entry_t *listenerEntry = celix_arrayList_get(listeners, i); bundle_getContext(listenerEntry->bundle, &info.context); info.filter = celix_filter_getFilterString(listenerEntry->filter); @@ -619,7 +615,7 @@ static celix_status_t serviceRegistry_removeHook(service_registry_pt registry, s celixThreadRwlock_unlock(®istry->lock); if (removedEntry != NULL) { - for (int i = 0; i < arrayList_size(listeners); ++i) { + for (int i = 0; i < celix_arrayList_size(listeners); ++i) { celix_service_registry_service_listener_entry_t *listenerEntry = celix_arrayList_get(listeners, i); bundle_getContext(listenerEntry->bundle, &info.context); info.filter = celix_filter_getFilterString(listenerEntry->filter); @@ -650,9 +646,9 @@ static void serviceRegistry_callHooksForListenerFilter(service_registry_pt regis celix_array_list_t *hookRegistrations = celix_arrayList_create(); celixThreadRwlock_readLock(®istry->lock); - unsigned size = arrayList_size(registry->listenerHooks); + unsigned size = celix_arrayList_size(registry->listenerHooks); for (int i = 0; i < size; ++i) { - celix_service_registry_listener_hook_entry_t* entry = arrayList_get(registry->listenerHooks, i); + celix_service_registry_listener_hook_entry_t* entry = celix_arrayList_get(registry->listenerHooks, i); if (entry != NULL) { celix_increaseCountHook(entry); //increate use count to ensure the hook cannot be removed, untill used celix_arrayList_add(hookRegistrations, entry); @@ -660,7 +656,7 @@ static void serviceRegistry_callHooksForListenerFilter(service_registry_pt regis } celixThreadRwlock_unlock(®istry->lock); - for (int i = 0; i < arrayList_size(hookRegistrations); ++i) { + for (int i = 0; i < celix_arrayList_size(hookRegistrations); ++i) { celix_service_registry_listener_hook_entry_t* entry = celix_arrayList_get(hookRegistrations, i); if (removed) { entry->hook->removed(entry->hook->handle, infos); @@ -675,18 +671,18 @@ static void serviceRegistry_callHooksForListenerFilter(service_registry_pt regis size_t serviceRegistry_nrOfHooks(service_registry_pt registry) { celixThreadRwlock_readLock(®istry->lock); - unsigned size = arrayList_size(registry->listenerHooks); + unsigned size = celix_arrayList_size(registry->listenerHooks); celixThreadRwlock_unlock(®istry->lock); return (size_t) size; } -static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt registry, service_registration_pt registration, array_list_pt *out) { +static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt registry, service_registration_pt registration, celix_array_list_t** out) { celix_status_t status; - array_list_pt bundles = NULL; + celix_array_list_t* bundles = NULL; hash_map_iterator_pt iter; - status = arrayList_create(&bundles); - if (status == CELIX_SUCCESS) { + bundles = celix_arrayList_create(); + if (bundles) { celixThreadRwlock_readLock(®istry->lock); iter = hashMapIterator_create(registry->serviceReferences); while (hashMapIterator_hasNext(iter)) { @@ -694,18 +690,20 @@ static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt regist bundle_pt registrationUser = hashMapEntry_getKey(entry); hash_map_pt regMap = hashMapEntry_getValue(entry); if (hashMap_containsKey(regMap, (void*)registration->serviceId)) { - arrayList_add(bundles, registrationUser); + celix_arrayList_add(bundles, registrationUser); } } hashMapIterator_destroy(iter); celixThreadRwlock_unlock(®istry->lock); + } else { + status = CELIX_ENOMEM; } if (status == CELIX_SUCCESS) { *out = bundles; } else { if (bundles != NULL) { - arrayList_destroy(bundles); + celix_arrayList_destroy(bundles); } } @@ -830,7 +828,7 @@ char* celix_serviceRegistry_createFilterFor(celix_service_registry_t* registry, "Error incorrect version range."); return NULL; } - versionRange = versionRange_createLDAPFilter(range, CELIX_FRAMEWORK_SERVICE_VERSION); + versionRange = celix_versionRange_createLDAPFilter(range, CELIX_FRAMEWORK_SERVICE_VERSION); if (versionRange == NULL) { celix_framework_log(registry->framework->logger, CELIX_LOG_LEVEL_ERROR, __FUNCTION__, __BASE_FILE__, __LINE__, "Error creating LDAP filter."); @@ -996,7 +994,7 @@ celix_status_t celix_serviceRegistry_addServiceListener(celix_service_registry_t //find already registered services hash_map_iterator_t iter = hashMapIterator_construct(registry->serviceRegistrations); while (hashMapIterator_hasNext(&iter)) { - celix_array_list_t *regs = (array_list_pt) hashMapIterator_nextValue(&iter); + celix_array_list_t *regs = (celix_array_list_t*) hashMapIterator_nextValue(&iter); for (int regIdx = 0; (regs != NULL) && regIdx < celix_arrayList_size(regs); ++regIdx) { service_registration_pt registration = celix_arrayList_get(regs, regIdx); properties_pt props = NULL; diff --git a/libs/framework/src/service_tracker.c b/libs/framework/src/service_tracker.c index 18d07cd4e..8d21a9eab 100644 --- a/libs/framework/src/service_tracker.c +++ b/libs/framework/src/service_tracker.c @@ -267,15 +267,14 @@ service_reference_pt serviceTracker_getServiceReference(service_tracker_t* track return result; } -array_list_pt serviceTracker_getServiceReferences(service_tracker_t* tracker) { +celix_array_list_t* serviceTracker_getServiceReferences(service_tracker_t* tracker) { //TODO deprecated warning -> not locked - array_list_pt references = NULL; - arrayList_create(&references); + celix_array_list_t* references = celix_arrayList_create(); celixThreadMutex_lock(&tracker->mutex); for (int i = 0; i < celix_arrayList_size(tracker->trackedServices); i++) { celix_tracked_entry_t *tracked = celix_arrayList_get(tracker->trackedServices, i); - arrayList_add(references, tracked->reference); + celix_arrayList_add(references, tracked->reference); } celixThreadMutex_unlock(&tracker->mutex); @@ -296,15 +295,14 @@ void *serviceTracker_getService(service_tracker_t* tracker) { return service; } -array_list_pt serviceTracker_getServices(service_tracker_t* tracker) { +celix_array_list_t* serviceTracker_getServices(service_tracker_t* tracker) { //TODO deprecated warning -> not locked, also make locked variant - array_list_pt references = NULL; - arrayList_create(&references); + celix_array_list_t* references = celix_arrayList_create(); celixThreadMutex_lock(&tracker->mutex); for (int i = 0; i < celix_arrayList_size(tracker->trackedServices); i++) { celix_tracked_entry_t *tracked = celix_arrayList_get(tracker->trackedServices, i); - arrayList_add(references, tracked->service); + celix_arrayList_add(references, tracked->service); } celixThreadMutex_unlock(&tracker->mutex); @@ -367,7 +365,7 @@ static void serviceTracker_serviceChanged(void *handle, celix_service_event_t *e size_t serviceTracker_nrOfTrackedServices(service_tracker_t *tracker) { celixThreadMutex_lock(&tracker->mutex); - size_t result = (size_t) arrayList_size(tracker->trackedServices); + size_t result = (size_t) celix_arrayList_size(tracker->trackedServices); celixThreadMutex_unlock(&tracker->mutex); return result; } @@ -382,7 +380,7 @@ static celix_status_t serviceTracker_track(service_tracker_t* tracker, service_r celixThreadMutex_lock(&tracker->mutex); for (int i = 0; i < celix_arrayList_size(tracker->trackedServices); i++) { bool equals = false; - celix_tracked_entry_t *visit = (celix_tracked_entry_t*) arrayList_get(tracker->trackedServices, i); + celix_tracked_entry_t *visit = (celix_tracked_entry_t*) celix_arrayList_get(tracker->trackedServices, i); serviceReference_equals(reference, visit->reference, &equals); if (equals) { //NOTE it is possible to get two REGISTERED events, second one can be ignored. @@ -412,7 +410,7 @@ static celix_status_t serviceTracker_track(service_tracker_t* tracker, service_r celix_tracked_entry_t *tracked = tracked_create(reference, service, props, bnd); //use count 1 celixThreadMutex_lock(&tracker->mutex); - arrayList_add(tracker->trackedServices, tracked); + celix_arrayList_add(tracker->trackedServices, tracked); celixThreadCondition_broadcast(&tracker->condTracked); celixThreadMutex_unlock(&tracker->mutex); @@ -521,7 +519,7 @@ static celix_status_t serviceTracker_untrack(service_tracker_t* tracker, service celixThreadMutex_lock(&tracker->mutex); for (int i = 0; i < celix_arrayList_size(tracker->trackedServices); i++) { bool equals; - celix_tracked_entry_t *tracked = (celix_tracked_entry_t*) arrayList_get(tracker->trackedServices, i); + celix_tracked_entry_t *tracked = celix_arrayList_get(tracker->trackedServices, i); serviceReference_equals(reference, tracked->reference, &equals); if (equals) { remove = tracked; @@ -718,7 +716,7 @@ static celix_tracked_entry_t* celix_serviceTracker_findHighestRankingService(ser //precondition tracker->mutex locked celix_tracked_entry_t* highest = NULL; for (int i = 0; i < celix_arrayList_size(tracker->trackedServices); ++i) { - celix_tracked_entry_t* tracked = (celix_tracked_entry_t *) arrayList_get(tracker->trackedServices, i); + celix_tracked_entry_t* tracked = celix_arrayList_get(tracker->trackedServices, i); if (serviceName == NULL || (serviceName != NULL && tracked->serviceName != NULL && celix_utils_stringEquals(tracked->serviceName, serviceName))) { if (highest == NULL) { @@ -795,7 +793,7 @@ size_t celix_serviceTracker_useServices( count = (size_t)size; celix_tracked_entry_t *entries[size]; for (int i = 0; i < size; i++) { - celix_tracked_entry_t *tracked = (celix_tracked_entry_t *) arrayList_get(tracker->trackedServices, i); + celix_tracked_entry_t *tracked = celix_arrayList_get(tracker->trackedServices, i); tracked_retain(tracked); entries[i] = tracked; } diff --git a/libs/utils/benchmark/src/StringHashmapBenchmark.cc b/libs/utils/benchmark/src/StringHashmapBenchmark.cc index 134a77dae..4552dd044 100644 --- a/libs/utils/benchmark/src/StringHashmapBenchmark.cc +++ b/libs/utils/benchmark/src/StringHashmapBenchmark.cc @@ -18,7 +18,6 @@ */ #include -#include #include #include #include @@ -29,6 +28,7 @@ #include "celix_string_hash_map.h" #include "celix_hash_map_internal.h" #include "celix_properties_internal.h" +#include "utils.h" class StringHashmapBenchmark { public: diff --git a/libs/utils/include_deprecated/array_list.h b/libs/utils/include_deprecated/array_list.h deleted file mode 100644 index b42a48360..000000000 --- a/libs/utils/include_deprecated/array_list.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef ARRAY_LIST_H_ -#define ARRAY_LIST_H_ - -#include "exports.h" -#include "celix_errno.h" -#include "stdbool.h" -#include "celix_array_list.h" - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct celix_array_list *array_list_pt __attribute__((deprecated("array_list is deprecated use celix_array_list instead"))); -typedef struct celix_array_list array_list_t __attribute__((deprecated("array_list is deprecated use celix_array_list instead"))); - -typedef struct celix_array_list_iterator *array_list_iterator_pt; -typedef struct celix_array_list_iterator array_list_iterator_t; - -typedef celix_status_t (*array_list_element_equals_pt)(const void *, const void *, bool *equals); - -CELIX_UTILS_DEPRECATED_EXPORT celix_status_t arrayList_create(celix_array_list_t **list); - -CELIX_UTILS_DEPRECATED_EXPORT celix_status_t arrayList_createWithEquals(array_list_element_equals_pt equals, celix_array_list_t **list); - -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 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); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayList_isEmpty(celix_array_list_t *list); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayList_contains(celix_array_list_t *list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT int arrayList_indexOf(celix_array_list_t *list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT int arrayList_lastIndexOf(celix_array_list_t *list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void *arrayList_get(celix_array_list_t *list, unsigned int index); - -CELIX_UTILS_DEPRECATED_EXPORT void *arrayList_set(celix_array_list_t *list, unsigned int index, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayList_add(celix_array_list_t *list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT int arrayList_addIndex(celix_array_list_t *list, unsigned int index, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayList_addAll(celix_array_list_t *list, celix_array_list_t *toAdd); - -CELIX_UTILS_DEPRECATED_EXPORT void *arrayList_remove(celix_array_list_t *list, unsigned int index); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayList_removeElement(celix_array_list_t *list, void *element); - -CELIX_UTILS_DEPRECATED_EXPORT void arrayList_clear(celix_array_list_t *list); - -CELIX_UTILS_DEPRECATED_EXPORT celix_array_list_t *arrayList_clone(celix_array_list_t *list); - -CELIX_UTILS_DEPRECATED_EXPORT array_list_iterator_pt arrayListIterator_create(celix_array_list_t *list); - -CELIX_UTILS_DEPRECATED_EXPORT void arrayListIterator_destroy(array_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayListIterator_hasNext(array_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void *arrayListIterator_next(array_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT bool arrayListIterator_hasPrevious(array_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void *arrayListIterator_previous(array_list_iterator_pt iterator); - -CELIX_UTILS_DEPRECATED_EXPORT void arrayListIterator_remove(array_list_iterator_pt iterator); - -#ifdef __cplusplus -} -#endif -#endif /* ARRAY_LIST_H_ */ diff --git a/libs/utils/include_deprecated/celix_utils_api.h b/libs/utils/include_deprecated/celix_utils_api.h index ef3153066..f82e2f66e 100644 --- a/libs/utils/include_deprecated/celix_utils_api.h +++ b/libs/utils/include_deprecated/celix_utils_api.h @@ -24,13 +24,11 @@ #include "celix_errno.h" #include "celix_threads.h" -#include "array_list.h" +#include "celix_array_list.h" #include "hash_map.h" -#include "properties.h" -#include "utils.h" +#include "celix_properties.h" #include "celix_utils.h" -#include "version.h" -#include "version_range.h" +#include "celix_version.h" #if defined(NO_MEMSTREAM_AVAILABLE) #include "memstream/open_memstream.h" diff --git a/libs/utils/src/array_list.c b/libs/utils/src/array_list.c index f6234827a..5b7a16e28 100644 --- a/libs/utils/src/array_list.c +++ b/libs/utils/src/array_list.c @@ -28,16 +28,10 @@ #include #include -#include "array_list.h" #include "celix_array_list.h" #include "array_list_private.h" #include "celix_build_assert.h" -static celix_status_t arrayList_elementEquals(const void *a, const void *b, bool *equals) { - *equals = (a == b); - return CELIX_SUCCESS; -} - static bool celix_arrayList_defaultEquals(celix_array_list_entry_t a, celix_array_list_entry_t b) { return memcmp(&a, &b, sizeof(a)) == 0; } @@ -45,9 +39,7 @@ static bool celix_arrayList_defaultEquals(celix_array_list_entry_t a, celix_arra static bool celix_arrayList_equalsForElement(celix_array_list_t *list, celix_array_list_entry_t a, celix_array_list_entry_t b) { bool equals = false; if (list != NULL) { - if (list->equalsDeprecated != NULL) { - list->equalsDeprecated(a.voidPtrVal, b.voidPtrVal, &equals); - } else if (list->equals != NULL) { + if (list->equals != NULL) { equals = list->equals(a, b); } } @@ -63,34 +55,7 @@ static void celix_arrayList_callRemovedCallback(celix_array_list_t *list, int in } } -celix_status_t arrayList_create(array_list_pt *list) { - return arrayList_createWithEquals(arrayList_elementEquals, list); -} - -celix_status_t arrayList_createWithEquals(array_list_element_equals_pt equals, array_list_pt *list) { - *list = celix_arrayList_create(); - if (*list != NULL) { - (*list)->equalsDeprecated = equals; - - } - return CELIX_SUCCESS; -} - -void arrayList_destroy(array_list_pt list) { - celix_arrayList_destroy(list); -} - -void arrayList_trimToSize(array_list_pt list) { - list->modCount++; - size_t oldCapacity = list->capacity; - if (list->size < oldCapacity) { - celix_array_list_entry_t * newList = realloc(list->elementData, sizeof(celix_array_list_entry_t) * list->size); - list->capacity = list->size; - list->elementData = newList; - } -} - -celix_status_t arrayList_ensureCapacity(array_list_pt list, int capacity) { +static celix_status_t celix_arrayList_ensureCapacity(celix_array_list_t* list, int capacity) { celix_status_t status = CELIX_SUCCESS; celix_array_list_entry_t *newList; list->modCount++; @@ -110,265 +75,8 @@ celix_status_t arrayList_ensureCapacity(array_list_pt list, int capacity) { return status; } -unsigned int arrayList_size(array_list_pt list) { - return (int)list->size; -} - -bool arrayList_isEmpty(array_list_pt list) { - return list->size == 0; -} - -bool arrayList_contains(array_list_pt list, void * element) { - int index = arrayList_indexOf(list, element); - return index >= 0; -} - -int arrayList_indexOf(array_list_pt list, void * element) { - if (element == NULL) { - unsigned int i = 0; - for (i = 0; i < list->size; i++) { - if (list->elementData[i].voidPtrVal == NULL) { - return i; - } - } - } else { - unsigned int i = 0; - for (i = 0; i < list->size; i++) { - celix_array_list_entry_t entry; - memset(&entry, 0, sizeof(entry)); - entry.voidPtrVal = element; - bool equals = celix_arrayList_equalsForElement(list, entry, list->elementData[i]); - if (equals) { - return i; - } - } - } - return -1; -} - -int arrayList_lastIndexOf(array_list_pt list, void * element) { - if (element == NULL) { - int i = 0; - int size = (int)list->size; - for (i = size - 1; i >= 0; i--) { - if (list->elementData[i].voidPtrVal == NULL) { - return (int)i; - } - } - } else { - int i = 0; - int size = (int)list->size; - for (i = size - 1; i >= 0; i--) { - celix_array_list_entry_t entry; - memset(&entry, 0, sizeof(entry)); - entry.voidPtrVal = element; - bool equals = celix_arrayList_equalsForElement(list, entry, list->elementData[i]); - if (equals) { - return (int)i; - } - } - } - return -1; -} - -void * arrayList_get(array_list_pt list, unsigned int index) { - if (index >= list->size) { - return NULL; - } - - return list->elementData[index].voidPtrVal; -} - -void * arrayList_set(array_list_pt list, unsigned int index, void * element) { - void * oldElement; - if (index >= list->size) { - return NULL; - } - - oldElement = list->elementData[index].voidPtrVal; - memset(&list->elementData[index], 0, sizeof(celix_array_list_entry_t)); - list->elementData[index].voidPtrVal = element; - return oldElement; -} - -bool arrayList_add(array_list_pt list, void * element) { - arrayList_ensureCapacity(list, (int)list->size + 1); - memset(&list->elementData[list->size], 0, sizeof(celix_array_list_entry_t)); - list->elementData[list->size++].voidPtrVal = element; - return true; -} - -int arrayList_addIndex(array_list_pt list, unsigned int index, void * element) { - size_t numMoved; - if (index > list->size) { - return -1; - } - arrayList_ensureCapacity(list, (int)list->size+1); - numMoved = list->size - index; - memmove(list->elementData+(index+1), list->elementData+index, sizeof(celix_array_list_entry_t) * numMoved); - - list->elementData[index].voidPtrVal = element; - list->size++; - return 0; -} - -void * arrayList_remove(array_list_pt list, unsigned int index) { - void * oldElement; - size_t numMoved; - if (index >= list->size) { - return NULL; - } - - list->modCount++; - oldElement = list->elementData[index].voidPtrVal; - numMoved = list->size - index - 1; - memmove(list->elementData+index, list->elementData+index+1, sizeof(celix_array_list_entry_t) * numMoved); - memset(&list->elementData[--list->size], 0, sizeof(celix_array_list_entry_t)); - - return oldElement; -} - -void arrayList_fastRemove(array_list_pt list, unsigned int index) { - size_t numMoved; - list->modCount++; - - numMoved = list->size - index - 1; - memmove(list->elementData+index, list->elementData+index+1, sizeof(celix_array_list_entry_t) * numMoved); - memset(&list->elementData[--list->size], 0, sizeof(celix_array_list_entry_t)); -} - -bool arrayList_removeElement(array_list_pt list, void * element) { - if (element == NULL) { - unsigned int i = 0; - for (i = 0; i < list->size; i++) { - if (list->elementData[i].voidPtrVal == NULL) { - arrayList_fastRemove(list, i); - return true; - } - } - } else { - unsigned int i = 0; - for (i = 0; i < list->size; i++) { - celix_array_list_entry_t entry; - memset(&entry, 0, sizeof(entry)); - entry.voidPtrVal = element; - bool equals = celix_arrayList_equalsForElement(list, entry, list->elementData[i]); - if (equals) { - arrayList_fastRemove(list, i); - return true; - } - } - } - return false; -} - -void arrayList_clear(array_list_pt list) { - celix_arrayList_clear(list); -} - -bool arrayList_addAll(array_list_pt list, array_list_pt toAdd) { - unsigned int i; - unsigned int size = arrayList_size(toAdd); - arrayList_ensureCapacity(list, list->size + size); -// memcpy(list->elementData+list->size, *toAdd->elementData, size); -// list->size += size; - for (i = 0; i < arrayList_size(toAdd); i++) { - arrayList_add(list, arrayList_get(toAdd, i)); - } - return size != 0; -} - -array_list_pt arrayList_clone(array_list_pt list) { - unsigned int i; - array_list_pt new = NULL; - arrayList_create(&new); -// 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)); - } - new->modCount = 0; - return new; -} - -array_list_iterator_pt arrayListIterator_create(array_list_pt list) { - array_list_iterator_pt iterator = (array_list_iterator_pt) malloc(sizeof(*iterator)); - - iterator->lastReturned = -1; - iterator->cursor = 0; - iterator->list = list; - iterator->expectedModificationCount = list->modCount; - - return iterator; -} - -void arrayListIterator_destroy(array_list_iterator_pt iterator) { - iterator->lastReturned = -1; - iterator->cursor = 0; - iterator->expectedModificationCount = 0; - iterator->list = NULL; - free(iterator); -} - -bool arrayListIterator_hasNext(array_list_iterator_pt iterator) { - return iterator->cursor != iterator->list->size; -} - -void * arrayListIterator_next(array_list_iterator_pt iterator) { - void * next; - if (iterator->expectedModificationCount != iterator->list->modCount) { - return NULL; - } - next = arrayList_get(iterator->list, iterator->cursor); - iterator->lastReturned = iterator->cursor++; - return next; -} - -bool arrayListIterator_hasPrevious(array_list_iterator_pt iterator) { - return iterator->cursor != 0; -} - -void * arrayListIterator_previous(array_list_iterator_pt iterator) { - int i; - void * previous; - if (iterator->expectedModificationCount != iterator->list->modCount) { - return NULL; - } - i = iterator->cursor - 1; - previous = arrayList_get(iterator->list, i); - iterator->lastReturned = iterator->cursor = i; - return previous; -} - -void arrayListIterator_remove(array_list_iterator_pt iterator) { - if (iterator->lastReturned == -1) { - return; - } - if (iterator->expectedModificationCount != iterator->list->modCount) { - return; - } - if (arrayList_remove(iterator->list, iterator->lastReturned) != NULL) { - if (iterator->lastReturned < iterator->cursor) { - iterator->cursor--; - } - iterator->lastReturned = -1; - iterator->expectedModificationCount = iterator->list->modCount; - } -} - - - - -/********************************************************************************************************************** - ********************************************************************************************************************** - * Updated API - ********************************************************************************************************************** - **********************************************************************************************************************/ - celix_array_list_t* celix_arrayList_createWithOptions(const celix_array_list_create_options_t* opts) { - array_list_t *list = calloc(1, sizeof(*list)); + celix_array_list_t *list = calloc(1, sizeof(*list)); if (list != NULL) { list->capacity = 10; list->elementData = malloc(sizeof(celix_array_list_entry_t) * list->capacity); @@ -416,17 +124,38 @@ void* celix_arrayList_get(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).voidPtrVal; } -int celix_arrayList_getInt(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).intVal; } -long int celix_arrayList_getLong(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).longVal; } -unsigned int celix_arrayList_getUInt(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).uintVal; } -unsigned long int celix_arrayList_getULong(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).ulongVal; } -float celix_arrayList_getFloat(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).floatVal; } -double celix_arrayList_getDouble(const celix_array_list_t *list, int index) { return arrayList_getEntry(list, index).doubleVal; } -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; } +int celix_arrayList_getInt(const celix_array_list_t* list, int index) { return arrayList_getEntry(list, index).intVal; } + +long int celix_arrayList_getLong(const celix_array_list_t* list, int index) { + return arrayList_getEntry(list, index).longVal; +} + +unsigned int celix_arrayList_getUInt(const celix_array_list_t* list, int index) { + return arrayList_getEntry(list, index).uintVal; +} + +unsigned long int celix_arrayList_getULong(const celix_array_list_t* list, int index) { + return arrayList_getEntry(list, index).ulongVal; +} + +float celix_arrayList_getFloat(const celix_array_list_t* list, int index) { + return arrayList_getEntry(list, index).floatVal; +} + +double celix_arrayList_getDouble(const celix_array_list_t* list, int index) { + return arrayList_getEntry(list, index).doubleVal; +} + +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 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); + celix_status_t status = celix_arrayList_ensureCapacity(list, (int)list->size + 1); if (status == CELIX_SUCCESS) { list->elementData[list->size++] = entry; } diff --git a/libs/utils/src/array_list_private.h b/libs/utils/src/array_list_private.h index 9a7614338..1746202d7 100644 --- a/libs/utils/src/array_list_private.h +++ b/libs/utils/src/array_list_private.h @@ -27,29 +27,15 @@ #ifndef array_list_t_PRIVATE_H_ #define array_list_t_PRIVATE_H_ -#include "array_list.h" - struct celix_array_list { celix_array_list_entry_t* elementData; size_t size; size_t capacity; - unsigned int modCount; - - array_list_element_equals_pt equalsDeprecated; celix_arrayList_equals_fp equals; - void (*simpleRemovedCallback)(void* value); void* removedCallbackData; void (*removedCallback)(void* data, celix_array_list_entry_t entry); }; -struct celix_array_list_iterator { - array_list_pt list; - unsigned int cursor; - int lastReturned; - unsigned int expectedModificationCount; -}; - - #endif /* array_list_t_PRIVATE_H_ */ From 5d97401ec23bf28a3cabf46494277e9ff07b8133 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 30 Dec 2023 15:50:22 +0100 Subject: [PATCH 05/18] Refactor hash_map cpputests to gtests --- libs/utils/gtest/CMakeLists.txt | 1 + .../src/DeprecatedHashmapTestSuite.cc} | 789 ++++++++---------- libs/utils/src/hash_map.c | 1 - libs/utils/src/hash_map_private.h | 19 +- 4 files changed, 375 insertions(+), 435 deletions(-) rename libs/utils/{private/test/hash_map_test.cpp => gtest/src/DeprecatedHashmapTestSuite.cc} (61%) diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt index bfb9a5531..b492009f3 100644 --- a/libs/utils/gtest/CMakeLists.txt +++ b/libs/utils/gtest/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(test_utils src/ThreadsTestSuite.cc src/CelixErrnoTestSuite.cc src/CelixUtilsAutoCleanupTestSuite.cc + src/DeprecatedHashmapTestSuite.cc ) target_link_libraries(test_utils PRIVATE utils_cut Celix::utils GTest::gtest GTest::gtest_main libzip::zip) diff --git a/libs/utils/private/test/hash_map_test.cpp b/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc similarity index 61% rename from libs/utils/private/test/hash_map_test.cpp rename to libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc index e3fb34da5..46fdf5493 100644 --- a/libs/utils/private/test/hash_map_test.cpp +++ b/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc @@ -16,54 +16,44 @@ * specific language governing permissions and limitations * under the License. */ -/** - * hash_map_test.cpp - * - * \date Sep 15, 2015 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ -#include -#include -#include -#include -#include +#include -#include "CppUTest/TestHarness.h" -#include "CppUTest/TestHarness_c.h" -#include "CppUTest/CommandLineTestRunner.h" - -extern "C" -{ #include "hash_map.h" #include "hash_map_private.h" -} - -int main(int argc, char** argv) { - MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); - return RUN_ALL_TESTS(argc, argv); -} - -static char* my_strdup(const char* s){ - if(s==NULL){ - return NULL; - } - - size_t len = strlen(s); - - char *d = (char*) calloc (len + 1,sizeof(char)); - if (d == NULL){ - return NULL; - } - strncpy (d,s,len); - return d; -} +static unsigned int test_hashKeyChar(const void * k); +static unsigned int test_hashValueChar(const void * v); +static int test_equalsKeyChar(const void * k, const void * o); +static int test_equalsValueChar(const void * v, const void * o); + +class DeprecatedHashmapTestSuite : public ::testing::Test { +public: + DeprecatedHashmapTestSuite() { + map = hashMap_create(test_hashKeyChar, test_hashValueChar, test_equalsKeyChar, test_equalsValueChar); + defaultMap = hashMap_create(nullptr, nullptr, nullptr, nullptr); //use default hash and equals functions + } + + ~DeprecatedHashmapTestSuite() override { + hashMap_destroy(map, false, false); + hashMap_destroy(defaultMap, false, false); + } + + DeprecatedHashmapTestSuite(DeprecatedHashmapTestSuite&&) = delete; + DeprecatedHashmapTestSuite(const DeprecatedHashmapTestSuite&) = delete; + DeprecatedHashmapTestSuite& operator=(DeprecatedHashmapTestSuite&&) = delete; + DeprecatedHashmapTestSuite& operator=(const DeprecatedHashmapTestSuite&) = delete; + + hash_map_t* map{}; + hash_map_t* defaultMap{}; + hash_map_iterator_pt it_map{}; + hash_map_values_pt values{}; + hash_map_key_set_pt key_set{}; + hash_map_entry_set_pt entry_set{}; +}; -//Callback functions -unsigned int test_hashKeyChar(const void * k) { +static unsigned int test_hashKeyChar(const void * k) { char * str = (char *) k; unsigned int hash = 1315423911; @@ -78,104 +68,47 @@ unsigned int test_hashKeyChar(const void * k) { return hash; } -unsigned int test_hashValueChar(const void * v) { +static unsigned int test_hashValueChar(const void * v) { (void)(v); return 0; } -int test_equalsKeyChar(const void * k, const void * o) { +static int test_equalsKeyChar(const void * k, const void * o) { return strcmp((char *)k, (char *) o) == 0; } -int test_equalsValueChar(const void * v, const void * o) { +static int test_equalsValueChar(const void * v, const void * o) { return strcmp((char *)v, (char *) o) == 0; } -//Tests group defines - -TEST_GROUP(hash_map){ - hash_map_pt map; - - void setup() { - map = hashMap_create(NULL, NULL, NULL, NULL); - } - void teardown() { - hashMap_destroy(map, false, false); - } -}; - -TEST_GROUP(hash_map_iterator){ - hash_map_pt map; - hash_map_iterator_pt it_map; - - void setup() { - map = hashMap_create(NULL, NULL, NULL, NULL); - } - void teardown() { - hashMap_destroy(map, false, false); - } -}; - -TEST_GROUP(hash_map_values){ - hash_map_pt map; - hash_map_values_pt values; - - void setup() { - map = hashMap_create(NULL, NULL, NULL, NULL); - } - void teardown() { - hashMap_destroy(map, false, false); - } -}; - -TEST_GROUP(hash_map_keySet){ - hash_map_pt map; - hash_map_key_set_pt key_set; - - void setup() { - map = hashMap_create(NULL, NULL, NULL, NULL); - } - void teardown() { - hashMap_destroy(map, false, false); +static char* my_strdup(const char* s){ + if(s==nullptr){ + return nullptr; } -}; -TEST_GROUP(hash_map_entrySet){ - hash_map_pt map; - hash_map_entry_set_pt entry_set; - - void setup() { - map = hashMap_create(NULL, NULL, NULL, NULL); - } - void teardown() { - hashMap_destroy(map, false, false); - } -}; + size_t len = strlen(s); -TEST_GROUP(hash_map_hash) { - hash_map_pt map; + char *d = (char*) calloc (len + 1,sizeof(char)); - void setup(void) { - map = hashMap_create(test_hashKeyChar, test_hashValueChar, test_equalsKeyChar, test_equalsValueChar); + if (d == nullptr){ + return nullptr; } - void teardown() { - hashMap_destroy(map, false, false); - } -}; -//----------------------HASH MAP DATA MANAGEMENT TEST---------------------- + strncpy (d,s,len); + return d; +} -TEST(hash_map, create){ - CHECK(map != NULL); - LONGS_EQUAL(0, map->size); +TEST_F(DeprecatedHashmapTestSuite, create){ + EXPECT_TRUE(defaultMap != nullptr); + EXPECT_EQ(0, defaultMap->size); // This fails on windows due to dllimport providing a proxy for exported functions. - POINTERS_EQUAL(hashMap_equals, map->equalsKey); - POINTERS_EQUAL(hashMap_equals, map->equalsValue); - POINTERS_EQUAL(hashMap_hashCode, map->hashKey); - POINTERS_EQUAL(hashMap_hashCode, map->hashValue); + EXPECT_EQ(hashMap_equals, defaultMap->equalsKey); + EXPECT_EQ(hashMap_equals, defaultMap->equalsValue); + EXPECT_EQ(hashMap_hashCode, defaultMap->hashKey); + EXPECT_EQ(hashMap_hashCode, defaultMap->hashValue); } -TEST(hash_map, size){ +TEST_F(DeprecatedHashmapTestSuite, size){ char* key = my_strdup("key"); char* value = my_strdup("value"); char* key2 = my_strdup("key2"); @@ -183,53 +116,53 @@ TEST(hash_map, size){ char* key3 = my_strdup("key2"); char* value3 = my_strdup("value3"); - LONGS_EQUAL(0, map->size); + EXPECT_EQ(0, defaultMap->size); // Add one entry - hashMap_put(map, key, value); - LONGS_EQUAL(1, map->size); + hashMap_put(defaultMap, key, value); + EXPECT_EQ(1, defaultMap->size); // Add second entry - hashMap_put(map, key2, value2); - LONGS_EQUAL(2, map->size); + hashMap_put(defaultMap, key2, value2); + EXPECT_EQ(2, defaultMap->size); - // Add entry using the same key, this does not overwrite an existing entry - hashMap_put(map, key3, value3); - LONGS_EQUAL(3, map->size); + // Add entry using a different key with the same str value, this does not overwrite an existing entry + hashMap_put(defaultMap, key3, value3); + EXPECT_EQ(3, defaultMap->size); // Clear map - hashMap_clear(map, true, true); - LONGS_EQUAL(0, map->size); + hashMap_clear(defaultMap, true, true); + EXPECT_EQ(0, defaultMap->size); } -TEST(hash_map, isEmpty){ +TEST_F(DeprecatedHashmapTestSuite, isEmpty){ char * key = my_strdup("key"); char * value = my_strdup("value"); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); // Add one entry hashMap_put(map, key, value); - LONGS_EQUAL(1, map->size); - CHECK(!hashMap_isEmpty(map)); + EXPECT_EQ(1, map->size); + EXPECT_TRUE(!hashMap_isEmpty(map)); // Remove entry hashMap_remove(map, key); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); free(key); free(value); } -TEST(hash_map, get){ +TEST_F(DeprecatedHashmapTestSuite, get){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * neKey = my_strdup("notExisting"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * get; @@ -240,34 +173,34 @@ TEST(hash_map, get){ hashMap_put(map, key2, value2); get = (char*) (char*) hashMap_get(map, key); - STRCMP_EQUAL(value, get); + EXPECT_STREQ(value, get); get = (char*) hashMap_get(map, key2); - STRCMP_EQUAL(value2, get); + EXPECT_STREQ(value2, get); get = (char*) hashMap_get(map, neKey); - POINTERS_EQUAL(NULL, get); + EXPECT_EQ(nullptr, get); - get = (char*) hashMap_get(map, NULL); - POINTERS_EQUAL(NULL, get); + get = (char*) hashMap_get(map, nullptr); + EXPECT_EQ(nullptr, get); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - get = (char*) hashMap_get(map, NULL); - STRCMP_EQUAL(value3, get); + get = (char*) hashMap_get(map, nullptr); + EXPECT_STREQ(value3, get); free(neKey); hashMap_clear(map, true, true); } -TEST(hash_map, containsKey){ +TEST_F(DeprecatedHashmapTestSuite, containsKey){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * neKey = my_strdup("notExisting"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); // Add one entry @@ -276,27 +209,27 @@ TEST(hash_map, containsKey){ // Add second entry hashMap_put(map, key2, value2); - CHECK(hashMap_containsKey(map, key)); - CHECK(hashMap_containsKey(map, key2)); - CHECK(!hashMap_containsKey(map, neKey)); - CHECK(!hashMap_containsKey(map, NULL)); + EXPECT_TRUE(hashMap_containsKey(map, key)); + EXPECT_TRUE(hashMap_containsKey(map, key2)); + EXPECT_TRUE(!hashMap_containsKey(map, neKey)); + EXPECT_TRUE(!hashMap_containsKey(map, nullptr)); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - CHECK(hashMap_containsKey(map, key3)); + EXPECT_TRUE(hashMap_containsKey(map, key3)); free(neKey); hashMap_clear(map, true, true); } -TEST(hash_map, getEntry){ +TEST_F(DeprecatedHashmapTestSuite, getEntry){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * neKey = my_strdup("notExisting"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); hash_map_entry_pt entry; @@ -306,110 +239,110 @@ TEST(hash_map, getEntry){ // Add second entry hashMap_put(map, key2, value2); entry = hashMap_getEntry(map, key); - STRCMP_EQUAL(key, (char*) entry->key); - STRCMP_EQUAL(value, (char*)entry->value); + EXPECT_STREQ(key, (char*) entry->key); + EXPECT_STREQ(value, (char*)entry->value); entry = hashMap_getEntry(map, key2); - STRCMP_EQUAL(key2, (char*) entry->key); - STRCMP_EQUAL(value2, (char*) entry->value); + EXPECT_STREQ(key2, (char*) entry->key); + EXPECT_STREQ(value2, (char*) entry->value); entry = hashMap_getEntry(map, neKey); - POINTERS_EQUAL(NULL, entry); + EXPECT_EQ(nullptr, entry); - entry = hashMap_getEntry(map, NULL); - POINTERS_EQUAL(NULL, entry); + entry = hashMap_getEntry(map, nullptr); + EXPECT_EQ(nullptr, entry); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); entry = hashMap_getEntry(map, key3); - CHECK_EQUAL(key3, entry->key); - STRCMP_EQUAL(value3, (char*) entry->value); + EXPECT_EQ(key3, entry->key); + EXPECT_STREQ(value3, (char*) entry->value); free(neKey); hashMap_clear(map, true, true); } -TEST(hash_map, put){ +TEST_F(DeprecatedHashmapTestSuite, put){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * nkey2 = my_strdup("key2"); char * nvalue2 = my_strdup("value3"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * key4 = my_strdup("key4"); - char * value4 = NULL; + char * value4 = nullptr; char * old; char * get; // Add one entry - hashMap_put(map, key, value); + hashMap_put(defaultMap, key, value); // Add second entry - hashMap_put(map, key2, value2); + hashMap_put(defaultMap, key2, value2); - get = (char*) hashMap_get(map, key); - STRCMP_EQUAL(value, get); + get = (char*) hashMap_get(defaultMap, key); + EXPECT_STREQ(value, get); - get = (char*) hashMap_get(map, key2); - STRCMP_EQUAL(value2, get); + get = (char*) hashMap_get(defaultMap, key2); + EXPECT_STREQ(value2, get); // Try to add an entry with the same key, since no explicit hash function is used, // this will not overwrite an existing entry. - old = (char *) hashMap_put(map, nkey2, nvalue2); - POINTERS_EQUAL(NULL, old); + old = (char *) hashMap_put(defaultMap, nkey2, nvalue2); + EXPECT_EQ(nullptr, old); // Retrieving the values will return the correct values - get = (char*) hashMap_get(map, (void*)key2); - STRCMP_EQUAL(value2, get); - get = (char*) hashMap_get(map, nkey2); - STRCMP_EQUAL(nvalue2, get); + get = (char*) hashMap_get(defaultMap, (void*)key2); + EXPECT_STREQ(value2, get); + get = (char*) hashMap_get(defaultMap, nkey2); + EXPECT_STREQ(nvalue2, get); - // Add third entry with NULL key - hashMap_put(map, key3, value3); + // Add third entry with nullptr key + hashMap_put(defaultMap, key3, value3); - get = (char*) hashMap_get(map, key3); - STRCMP_EQUAL(value3, get); + get = (char*) hashMap_get(defaultMap, key3); + EXPECT_STREQ(value3, get); - // Add fourth entry with NULL value - hashMap_put(map, key4, value4); + // Add fourth entry with nullptr value + hashMap_put(defaultMap, key4, value4); - get =(char*)hashMap_get(map, key4); - CHECK_EQUAL(value4, get); + get =(char*)hashMap_get(defaultMap, key4); + EXPECT_EQ(value4, get); - hashMap_clear(map, true, true); + hashMap_clear(defaultMap, true, true); } -TEST(hash_map, resize){ +TEST_F(DeprecatedHashmapTestSuite, resize){ int i; char key[6]; - LONGS_EQUAL(0, map->size); - LONGS_EQUAL(16, map->tablelength); - LONGS_EQUAL(12, map->treshold); + EXPECT_EQ(0, map->size); + EXPECT_EQ(16, map->tablelength); + EXPECT_EQ(12, map->treshold); for (i = 0; i < 12; i++) { snprintf(key, sizeof(key), "key%d", i); hashMap_put(map, my_strdup(key), my_strdup(key)); } - LONGS_EQUAL(12, map->size); - LONGS_EQUAL(16, map->tablelength); - LONGS_EQUAL(12, map->treshold); + EXPECT_EQ(12, map->size); + EXPECT_EQ(16, map->tablelength); + EXPECT_EQ(12, map->treshold); snprintf(key, sizeof(key), "key%d", i); hashMap_put(map, my_strdup(key), my_strdup(key)); - LONGS_EQUAL(13, map->size); - LONGS_EQUAL(32, map->tablelength); - LONGS_EQUAL(24, map->treshold); + EXPECT_EQ(13, map->size); + EXPECT_EQ(32, map->tablelength); + EXPECT_EQ(24, map->treshold); hashMap_clear(map, true, true); } -TEST(hash_map, remove){ +TEST_F(DeprecatedHashmapTestSuite, remove){ char * key = my_strdup("key"); char * value = my_strdup("value"); - char * key2 = NULL; + char * key2 = nullptr; char * value2 = my_strdup("value2"); char * removeKey; @@ -422,20 +355,20 @@ TEST(hash_map, remove){ // Remove non-existing entry for map removeKey = my_strdup("nonexisting"); hashMap_remove(map, removeKey); - LONGS_EQUAL(2, map->size); - CHECK(!hashMap_isEmpty(map)); + EXPECT_EQ(2, map->size); + EXPECT_TRUE(!hashMap_isEmpty(map)); hashMap_remove(map, key); - LONGS_EQUAL(1, map->size); + EXPECT_EQ(1, map->size); hashMap_remove(map, key2); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); // Remove non-existing entry for empty map hashMap_remove(map, removeKey); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); free(key); free(value); @@ -444,10 +377,10 @@ TEST(hash_map, remove){ free(removeKey); } -TEST(hash_map, removeMapping){ +TEST_F(DeprecatedHashmapTestSuite, removeMapping){ char * key = my_strdup("key"); char * value = my_strdup("value"); - char * key2 = NULL; + char * key2 = nullptr; char * value2 = my_strdup("value2"); hash_map_entry_pt entry1; hash_map_entry_pt entry2; @@ -462,21 +395,21 @@ TEST(hash_map, removeMapping){ entry1 = hashMap_getEntry(map, key); entry2 = hashMap_getEntry(map, key2); - CHECK(entry1 != NULL); - CHECK(entry2 != NULL); + EXPECT_TRUE(entry1 != nullptr); + EXPECT_TRUE(entry2 != nullptr); removed = hashMap_removeMapping(map, entry1); - POINTERS_EQUAL(removed, entry1); - LONGS_EQUAL(1, map->size); + EXPECT_EQ(removed, entry1); + EXPECT_EQ(1, map->size); removed = hashMap_removeMapping(map, entry2); - POINTERS_EQUAL(removed, entry2); - LONGS_EQUAL(0, map->size); + EXPECT_EQ(removed, entry2); + EXPECT_EQ(0, map->size); // Remove unexisting entry for empty map - hashMap_removeMapping(map, NULL); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + hashMap_removeMapping(map, nullptr); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); free(key); free(value); @@ -486,15 +419,15 @@ TEST(hash_map, removeMapping){ free(entry2); } -TEST(hash_map, clear){ +TEST_F(DeprecatedHashmapTestSuite, clear){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * key4 = my_strdup("key4"); - char * value4 = NULL; + char * value4 = nullptr; // Add one entry hashMap_put(map, key, value); @@ -502,15 +435,15 @@ TEST(hash_map, clear){ // Add second entry hashMap_put(map, key2, value2); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - // Add fourth entry with NULL value + // Add fourth entry with nullptr value hashMap_put(map, key4, value4); // Clear but leave keys and values intact hashMap_clear(map, false, false); - LONGS_EQUAL(0, map->size); + EXPECT_EQ(0, map->size); // Add one entry hashMap_put(map, key, value); @@ -518,24 +451,24 @@ TEST(hash_map, clear){ // Add second entry hashMap_put(map, key2, value2); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - // Add fourth entry with NULL value + // Add fourth entry with nullptr value hashMap_put(map, key4, value4); // Clear and clean up keys and values hashMap_clear(map, true, true); - LONGS_EQUAL(0, map->size); + EXPECT_EQ(0, map->size); } -TEST(hash_map, containsValue){ +TEST_F(DeprecatedHashmapTestSuite, containsValue){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * neValue = my_strdup("notExisting"); char * key3 = my_strdup("key3"); - char * value3 = NULL; + char * value3 = nullptr; // Add one entry hashMap_put(map, key, value); @@ -543,21 +476,21 @@ TEST(hash_map, containsValue){ // Add second entry hashMap_put(map, key2, value2); - CHECK(hashMap_containsValue(map, value)); - CHECK(hashMap_containsValue(map, value2)); - CHECK(!hashMap_containsValue(map, neValue)); - CHECK(!hashMap_containsValue(map, NULL)); + EXPECT_TRUE(hashMap_containsValue(map, value)); + EXPECT_TRUE(hashMap_containsValue(map, value2)); + EXPECT_TRUE(!hashMap_containsValue(map, neValue)); + EXPECT_TRUE(!hashMap_containsValue(map, nullptr)); - // Add third entry with NULL value + // Add third entry with nullptr value hashMap_put(map, key3, value3); - CHECK(hashMap_containsValue(map, value3)); + EXPECT_TRUE(hashMap_containsValue(map, value3)); free(neValue); hashMap_clear(map, true, true); } -TEST(hash_map, ValuestoArray){ +TEST_F(DeprecatedHashmapTestSuite, ValuestoArray){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -574,21 +507,21 @@ TEST(hash_map, ValuestoArray){ values = hashMapValues_create(map); hashMapValues_toArray(values, (void***)&array, &size); - LONGS_EQUAL(2, size); - CHECK(hashMapValues_contains(values, array[0])); - CHECK(hashMapValues_contains(values, array[1])); + EXPECT_EQ(2, size); + EXPECT_TRUE(hashMapValues_contains(values, array[0])); + EXPECT_TRUE(hashMapValues_contains(values, array[1])); free(array); hashMapValues_destroy(values); hashMap_clear(map, true, true); } -TEST(hash_map, entryGetKey){ +TEST_F(DeprecatedHashmapTestSuite, entryGetKey){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * get; hash_map_entry_pt entry; @@ -600,25 +533,25 @@ TEST(hash_map, entryGetKey){ entry = hashMap_getEntry(map, key); get = (char*) hashMapEntry_getKey(entry); - STRCMP_EQUAL(key, get); + EXPECT_STREQ(key, get); entry = hashMap_getEntry(map, key2); get = (char*) hashMapEntry_getKey(entry); - STRCMP_EQUAL(key2, get); + EXPECT_STREQ(key2, get); entry = hashMap_getEntry(map, key3); get = (char*) hashMapEntry_getKey(entry); - POINTERS_EQUAL(key3, get); + EXPECT_EQ(key3, get); hashMap_clear(map, true, true); } -TEST(hash_map, entryGetValue){ +TEST_F(DeprecatedHashmapTestSuite, entryGetValue){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * get; hash_map_entry_pt entry; @@ -630,32 +563,32 @@ TEST(hash_map, entryGetValue){ entry = hashMap_getEntry(map, key); get = (char*) hashMapEntry_getValue(entry); - STRCMP_EQUAL(value, get); + EXPECT_STREQ(value, get); entry = hashMap_getEntry(map, key2); get = (char*) hashMapEntry_getValue(entry); - STRCMP_EQUAL(value2, get); + EXPECT_STREQ(value2, get); entry = hashMap_getEntry(map, key3); get = (char*) hashMapEntry_getValue(entry); - POINTERS_EQUAL(value3, get); + EXPECT_EQ(value3, get); hashMap_clear(map, true, true); } //----------------------HASH MAP ITERATOR TEST---------------------- -TEST(hash_map_iterator, create){ +TEST_F(DeprecatedHashmapTestSuite, createIteratorTest) { it_map = hashMapIterator_create(map); - CHECK(it_map != NULL); - POINTERS_EQUAL(map, it_map->map); - POINTERS_EQUAL(it_map->current, NULL); - LONGS_EQUAL(map->modificationCount, it_map->expectedModCount); + EXPECT_TRUE(it_map != nullptr); + EXPECT_EQ(map, it_map->map); + EXPECT_EQ(it_map->current, nullptr); + EXPECT_EQ(map->modificationCount, it_map->expectedModCount); hashMapIterator_destroy(it_map); } -TEST(hash_map_iterator, hasNext){ +TEST_F(DeprecatedHashmapTestSuite, hasNext){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -676,23 +609,23 @@ TEST(hash_map_iterator, hasNext){ it_map = hashMapIterator_create(map); //check hasNext - CHECK(hashMapIterator_hasNext(it_map)); + EXPECT_TRUE(hashMapIterator_hasNext(it_map)); hashMapIterator_nextEntry(it_map); - CHECK(hashMapIterator_hasNext(it_map)); + EXPECT_TRUE(hashMapIterator_hasNext(it_map)); hashMapIterator_nextEntry(it_map); - CHECK(hashMapIterator_hasNext(it_map)); + EXPECT_TRUE(hashMapIterator_hasNext(it_map)); - //third entry next points to NULL + //third entry next points to nullptr hashMapIterator_nextEntry(it_map); - CHECK(!hashMapIterator_hasNext(it_map)); + EXPECT_TRUE(!hashMapIterator_hasNext(it_map)); hashMapIterator_destroy(it_map); hashMap_clear(map, true, true); } -TEST(hash_map_iterator, remove){ +TEST_F(DeprecatedHashmapTestSuite, removeIteratorTest) { char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -708,16 +641,16 @@ TEST(hash_map_iterator, remove){ //create it_map from map it_map = hashMapIterator_create(map); - //try to remove current (NULL) + //try to remove current (nullptr) hashMapIterator_remove(it_map); - LONGS_EQUAL(3, map->size); + EXPECT_EQ(3, map->size); //delete the first and second entry hashMapIterator_nextEntry(it_map); hashMapIterator_remove(it_map); hashMapIterator_nextEntry(it_map); hashMapIterator_remove(it_map); - LONGS_EQUAL(1, map->size); + EXPECT_EQ(1, map->size); free(key); free(value); @@ -728,7 +661,7 @@ TEST(hash_map_iterator, remove){ hashMapIterator_destroy(it_map); } -TEST(hash_map_iterator, nextValue){ +TEST_F(DeprecatedHashmapTestSuite, nextValue){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -743,19 +676,19 @@ TEST(hash_map_iterator, nextValue){ it_map = hashMapIterator_create(map); getValue = (char*) hashMapIterator_nextValue(it_map); - CHECK(hashMap_containsValue(map, getValue)); + EXPECT_TRUE(hashMap_containsValue(map, getValue)); getValue = (char*) hashMapIterator_nextValue(it_map); - CHECK(hashMap_containsValue(map, getValue)); + EXPECT_TRUE(hashMap_containsValue(map, getValue)); getValue = (char*) hashMapIterator_nextValue(it_map); - POINTERS_EQUAL(NULL, getValue); + EXPECT_EQ(nullptr, getValue); hashMapIterator_destroy(it_map); hashMap_clear(map, true, true); } -TEST(hash_map_iterator, nextKey){ +TEST_F(DeprecatedHashmapTestSuite, nextKey){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -770,19 +703,19 @@ TEST(hash_map_iterator, nextKey){ it_map = hashMapIterator_create(map); getKey = (char*) hashMapIterator_nextKey(it_map); - CHECK(hashMap_containsKey(map, getKey)); + EXPECT_TRUE(hashMap_containsKey(map, getKey)); getKey = (char*) hashMapIterator_nextKey(it_map); - CHECK(hashMap_containsKey(map, getKey)); + EXPECT_TRUE(hashMap_containsKey(map, getKey)); getKey = (char*) hashMapIterator_nextKey(it_map); - POINTERS_EQUAL(NULL, getKey); + EXPECT_EQ(nullptr, getKey); hashMapIterator_destroy(it_map); hashMap_clear(map, true, true); } -TEST(hash_map_iterator, nextEntry){ +TEST_F(DeprecatedHashmapTestSuite, nextEntry){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -799,27 +732,27 @@ TEST(hash_map_iterator, nextEntry){ get = (hash_map_entry_pt) hashMapIterator_nextEntry(it_map); getValue = (char*) hashMap_get(map, get->key); - STRCMP_EQUAL((char*)get->value, (char*)getValue); + EXPECT_STREQ((char*)get->value, (char*)getValue); get = (hash_map_entry_pt) hashMapIterator_nextEntry(it_map); getValue = (char*) hashMap_get(map, get->key); - STRCMP_EQUAL((char*)get->value, (char*)getValue); + EXPECT_STREQ((char*)get->value, (char*)getValue); get = (hash_map_entry_pt) hashMapIterator_nextEntry(it_map); - POINTERS_EQUAL(NULL, get); + EXPECT_EQ(nullptr, get); hashMapIterator_destroy(it_map); hashMap_clear(map, true, true); } -TEST(hash_map_iterator, valuesIterator){ +TEST_F(DeprecatedHashmapTestSuite, valuesIterator){ hash_map_values_pt values; hash_map_iterator_pt it_val; values = hashMapValues_create(map); it_val = hashMapValues_iterator(values); - POINTERS_EQUAL(map, it_val->map); + EXPECT_EQ(map, it_val->map); hashMapIterator_destroy(it_val); hashMapValues_destroy(values); @@ -827,7 +760,7 @@ TEST(hash_map_iterator, valuesIterator){ //----------------------HASH MAP VALUES TEST---------------------- -TEST(hash_map_values, create){ +TEST_F(DeprecatedHashmapTestSuite, createValues){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -840,14 +773,14 @@ TEST(hash_map_values, create){ //create Values from map values = hashMapValues_create(map); - CHECK(hashMapValues_contains(values, value)); - CHECK(hashMapValues_contains(values, value2)); + EXPECT_TRUE(hashMapValues_contains(values, value)); + EXPECT_TRUE(hashMapValues_contains(values, value2)); hashMap_clear(map, true, true); hashMapValues_destroy(values); } -TEST(hash_map_values, size){ +TEST_F(DeprecatedHashmapTestSuite, sizeValues){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -860,13 +793,13 @@ TEST(hash_map_values, size){ //create Values from map values = hashMapValues_create(map); - LONGS_EQUAL(2, hashMapValues_size(values)); + EXPECT_EQ(2, hashMapValues_size(values)); hashMap_clear(map, true, true); hashMapValues_destroy(values); } -TEST(hash_map_values, remove){ +TEST_F(DeprecatedHashmapTestSuite, removeValues){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -878,16 +811,16 @@ TEST(hash_map_values, remove){ //create Values from map values = hashMapValues_create(map); - CHECK(hashMapValues_contains(values, value)); - CHECK(hashMapValues_contains(values, value2)); + EXPECT_TRUE(hashMapValues_contains(values, value)); + EXPECT_TRUE(hashMapValues_contains(values, value2)); hashMapValues_remove(values, value); - CHECK(!hashMapValues_contains(values, value)); - CHECK(hashMapValues_contains(values, value2)); + EXPECT_TRUE(!hashMapValues_contains(values, value)); + EXPECT_TRUE(hashMapValues_contains(values, value2)); hashMapValues_remove(values, value2); - CHECK(!hashMapValues_contains(values, value)); - CHECK(!hashMapValues_contains(values, value2)); + EXPECT_TRUE(!hashMapValues_contains(values, value)); + EXPECT_TRUE(!hashMapValues_contains(values, value2)); free(key); free(value); @@ -897,7 +830,7 @@ TEST(hash_map_values, remove){ hashMapValues_destroy(values); } -TEST(hash_map_values, clear){ +TEST_F(DeprecatedHashmapTestSuite, clearValues){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -910,15 +843,15 @@ TEST(hash_map_values, clear){ //create Values from map values = hashMapValues_create(map); - CHECK(hashMapValues_contains(values, value)); - CHECK(hashMapValues_contains(values, value2)); - LONGS_EQUAL(2, values->map->size); + EXPECT_TRUE(hashMapValues_contains(values, value)); + EXPECT_TRUE(hashMapValues_contains(values, value2)); + EXPECT_EQ(2, values->map->size); hashMapValues_clear(values); - CHECK(!hashMapValues_contains(values, value)); - CHECK(!hashMapValues_contains(values, value2)); - LONGS_EQUAL(0, values->map->size); + EXPECT_TRUE(!hashMapValues_contains(values, value)); + EXPECT_TRUE(!hashMapValues_contains(values, value2)); + EXPECT_EQ(0, values->map->size); free(key); free(value); @@ -927,7 +860,7 @@ TEST(hash_map_values, clear){ hashMapValues_destroy(values); } -TEST(hash_map_values, isEmpty){ +TEST_F(DeprecatedHashmapTestSuite, isEmptyValues){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -940,11 +873,11 @@ TEST(hash_map_values, isEmpty){ //create Values from map values = hashMapValues_create(map); - CHECK(!hashMapValues_isEmpty(values)); + EXPECT_TRUE(!hashMapValues_isEmpty(values)); hashMapValues_clear(values); - CHECK(hashMapValues_isEmpty(values)); + EXPECT_TRUE(hashMapValues_isEmpty(values)); free(key); free(value); @@ -955,120 +888,120 @@ TEST(hash_map_values, isEmpty){ //----------------------HASH MAP KEYSET TEST---------------------- -TEST(hash_map_keySet, create){ +TEST_F(DeprecatedHashmapTestSuite, createSet){ key_set = hashMapKeySet_create(map); - POINTERS_EQUAL(map, key_set->map); + EXPECT_EQ(map, key_set->map); hashMapKeySet_destroy(key_set); } -TEST(hash_map_keySet, size){ +TEST_F(DeprecatedHashmapTestSuite, sizeSet){ char * key = my_strdup("key"); char * key2 = my_strdup("key2"); key_set = hashMapKeySet_create(map); - LONGS_EQUAL(0, hashMapKeySet_size(key_set)); + EXPECT_EQ(0, hashMapKeySet_size(key_set)); // Add 2 entries - hashMap_put(map, key, NULL); - hashMap_put(map, key2, NULL); + hashMap_put(map, key, nullptr); + hashMap_put(map, key2, nullptr); - LONGS_EQUAL(2, hashMapKeySet_size(key_set)); + EXPECT_EQ(2, hashMapKeySet_size(key_set)); hashMap_clear(map, true, true); hashMapKeySet_destroy(key_set); } -TEST(hash_map_keySet, contains){ +TEST_F(DeprecatedHashmapTestSuite, containsSet){ char * key = my_strdup("key"); char * key2 = my_strdup("key2"); key_set = hashMapKeySet_create(map); - CHECK(!hashMapKeySet_contains(key_set, key)); - CHECK(!hashMapKeySet_contains(key_set, key2)); + EXPECT_TRUE(!hashMapKeySet_contains(key_set, key)); + EXPECT_TRUE(!hashMapKeySet_contains(key_set, key2)); // Add 2 entries - hashMap_put(map, key, NULL); + hashMap_put(map, key, nullptr); - CHECK(hashMapKeySet_contains(key_set, key)); - CHECK(!hashMapKeySet_contains(key_set, key2)); + EXPECT_TRUE(hashMapKeySet_contains(key_set, key)); + EXPECT_TRUE(!hashMapKeySet_contains(key_set, key2)); - hashMap_put(map, key2, NULL); + hashMap_put(map, key2, nullptr); - CHECK(hashMapKeySet_contains(key_set, key)); - CHECK(hashMapKeySet_contains(key_set, key2)); + EXPECT_TRUE(hashMapKeySet_contains(key_set, key)); + EXPECT_TRUE(hashMapKeySet_contains(key_set, key2)); hashMap_clear(map, true, true); hashMapKeySet_destroy(key_set); } -TEST(hash_map_keySet, remove){ +TEST_F(DeprecatedHashmapTestSuite, removeSet){ char * key = my_strdup("key"); char * key2 = my_strdup("key2"); key_set = hashMapKeySet_create(map); // Add 2 entries - hashMap_put(map, key, NULL); - hashMap_put(map, key2, NULL); + hashMap_put(map, key, nullptr); + hashMap_put(map, key2, nullptr); - LONGS_EQUAL(2, key_set->map->size); + EXPECT_EQ(2, key_set->map->size); hashMapKeySet_remove(key_set, key); - LONGS_EQUAL(1, key_set->map->size); + EXPECT_EQ(1, key_set->map->size); hashMapKeySet_remove(key_set, key2); - LONGS_EQUAL(0, key_set->map->size); + EXPECT_EQ(0, key_set->map->size); free(key); free(key2); hashMapKeySet_destroy(key_set); } -TEST(hash_map_keySet, clear){ +TEST_F(DeprecatedHashmapTestSuite, clearSet){ char * key = my_strdup("key"); char * key2 = my_strdup("key2"); key_set = hashMapKeySet_create(map); // Add 2 entries - hashMap_put(map, key, NULL); - hashMap_put(map, key2, NULL); + hashMap_put(map, key, nullptr); + hashMap_put(map, key2, nullptr); - LONGS_EQUAL(2, key_set->map->size); + EXPECT_EQ(2, key_set->map->size); hashMapKeySet_clear(key_set); - LONGS_EQUAL(0, key_set->map->size); + EXPECT_EQ(0, key_set->map->size); free(key); free(key2); hashMapKeySet_destroy(key_set); } -TEST(hash_map_keySet, isEmpty){ +TEST_F(DeprecatedHashmapTestSuite, isEmptySet){ char * key = my_strdup("key"); char * key2 = my_strdup("key2"); key_set = hashMapKeySet_create(map); - CHECK(hashMapKeySet_isEmpty(key_set)); + EXPECT_TRUE(hashMapKeySet_isEmpty(key_set)); // Add 2 entries - hashMap_put(map, key, NULL); - hashMap_put(map, key2, NULL); + hashMap_put(map, key, nullptr); + hashMap_put(map, key2, nullptr); - CHECK(!hashMapKeySet_isEmpty(key_set)); + EXPECT_TRUE(!hashMapKeySet_isEmpty(key_set)); hashMapKeySet_clear(key_set); - CHECK(hashMapKeySet_isEmpty(key_set)); + EXPECT_TRUE(hashMapKeySet_isEmpty(key_set)); free(key); free(key2); @@ -1077,15 +1010,15 @@ TEST(hash_map_keySet, isEmpty){ //----------------------HASH MAP ENTRYSET TEST---------------------- -TEST(hash_map_entrySet, create){ +TEST_F(DeprecatedHashmapTestSuite, createEntrySet){ entry_set = hashMapEntrySet_create(map); - POINTERS_EQUAL(map, entry_set->map); + EXPECT_EQ(map, entry_set->map); hashMapEntrySet_destroy(entry_set); } -TEST(hash_map_entrySet, size){ +TEST_F(DeprecatedHashmapTestSuite, sizeEntrySet){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1094,20 +1027,20 @@ TEST(hash_map_entrySet, size){ entry_set = hashMapEntrySet_create(map); - LONGS_EQUAL(0, hashMapEntrySet_size(entry_set)); + EXPECT_EQ(0, hashMapEntrySet_size(entry_set)); // Add 2 entries hashMap_put(map, key, value); hashMap_put(map, key2, value2); - LONGS_EQUAL(2, hashMapEntrySet_size(entry_set)); + EXPECT_EQ(2, hashMapEntrySet_size(entry_set)); hashMap_clear(map, true, true); hashMapEntrySet_destroy(entry_set); } -TEST(hash_map_entrySet, contains){ +TEST_F(DeprecatedHashmapTestSuite, containsEntrySet){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1119,15 +1052,15 @@ TEST(hash_map_entrySet, contains){ hashMap_put(map, key, value); hashMap_put(map, key2, value2); - CHECK(!hashMapEntrySet_contains(entry_set, NULL)); - CHECK(!hashMapEntrySet_contains(entry_set, hashMap_getEntry(map, key))); - CHECK(!hashMapEntrySet_contains(entry_set, hashMap_getEntry(map, key2))); + EXPECT_TRUE(!hashMapEntrySet_contains(entry_set, nullptr)); + EXPECT_TRUE(!hashMapEntrySet_contains(entry_set, hashMap_getEntry(map, key))); + EXPECT_TRUE(!hashMapEntrySet_contains(entry_set, hashMap_getEntry(map, key2))); hashMap_clear(map, true, true); hashMapEntrySet_destroy(entry_set); } -TEST(hash_map_entrySet, remove){ +TEST_F(DeprecatedHashmapTestSuite, removeEntrySet){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1139,14 +1072,14 @@ TEST(hash_map_entrySet, remove){ hashMap_put(map, key, value); hashMap_put(map, key2, value2); - CHECK(!hashMapEntrySet_remove(entry_set, (hash_map_entry_pt) NULL)); - LONGS_EQUAL(2, entry_set->map->size); + EXPECT_TRUE(!hashMapEntrySet_remove(entry_set, (hash_map_entry_pt) nullptr)); + EXPECT_EQ(2, entry_set->map->size); - CHECK(hashMapEntrySet_remove(entry_set, hashMap_getEntry(map, key))); - LONGS_EQUAL(1, entry_set->map->size); + EXPECT_TRUE(hashMapEntrySet_remove(entry_set, hashMap_getEntry(map, key))); + EXPECT_EQ(1, entry_set->map->size); - CHECK(hashMapEntrySet_remove(entry_set, hashMap_getEntry(map, key2))); - LONGS_EQUAL(0, entry_set->map->size); + EXPECT_TRUE(hashMapEntrySet_remove(entry_set, hashMap_getEntry(map, key2))); + EXPECT_EQ(0, entry_set->map->size); free(key); free(value); @@ -1156,7 +1089,7 @@ TEST(hash_map_entrySet, remove){ hashMapEntrySet_destroy(entry_set); } -TEST(hash_map_entrySet, clear){ +TEST_F(DeprecatedHashmapTestSuite, clearEntrySet){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1168,11 +1101,11 @@ TEST(hash_map_entrySet, clear){ hashMap_put(map, key, value); hashMap_put(map, key2, value2); - LONGS_EQUAL(2, entry_set->map->size); + EXPECT_EQ(2, entry_set->map->size); hashMapEntrySet_clear(entry_set); - LONGS_EQUAL(0, entry_set->map->size); + EXPECT_EQ(0, entry_set->map->size); free(key); free(value); @@ -1181,7 +1114,7 @@ TEST(hash_map_entrySet, clear){ hashMapEntrySet_destroy(entry_set); } -TEST(hash_map_entrySet, isEmpty){ +TEST_F(DeprecatedHashmapTestSuite, isEmptyEntrySet){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1189,17 +1122,17 @@ TEST(hash_map_entrySet, isEmpty){ entry_set = hashMapEntrySet_create(map); - CHECK(hashMapEntrySet_isEmpty(entry_set)); + EXPECT_TRUE(hashMapEntrySet_isEmpty(entry_set)); // Add 2 entries hashMap_put(map, key, value); hashMap_put(map, key2, value2); - CHECK(!hashMapEntrySet_isEmpty(entry_set)); + EXPECT_TRUE(!hashMapEntrySet_isEmpty(entry_set)); hashMapEntrySet_clear(entry_set); - CHECK(hashMapEntrySet_isEmpty(entry_set)); + EXPECT_TRUE(hashMapEntrySet_isEmpty(entry_set)); free(key); free(value); @@ -1210,7 +1143,7 @@ TEST(hash_map_entrySet, isEmpty){ //----------------------HASH MAP HASH TEST---------------------- -TEST(hash_map_hash, get){ +TEST_F(DeprecatedHashmapTestSuite, getTest){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1218,7 +1151,7 @@ TEST(hash_map_hash, get){ char * getKey = my_strdup("key"); char * get; char * neKey = my_strdup("notExisting"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); hashMap_clear(map, false, false); @@ -1231,39 +1164,39 @@ TEST(hash_map_hash, get){ // Get with new created key get = (char*) (char*) hashMap_get(map, getKey); - CHECK_C(get != NULL); - STRCMP_EQUAL(value, get); + EXPECT_TRUE(get != nullptr); + EXPECT_STREQ(value, get); free(getKey); getKey = my_strdup("key2"); get = (char*) hashMap_get(map, getKey); - CHECK_C(get != NULL); - STRCMP_EQUAL(value2, get); + EXPECT_TRUE(get != nullptr); + EXPECT_STREQ(value2, get); get = (char*) hashMap_get(map, neKey); - POINTERS_EQUAL(NULL, get); + EXPECT_EQ(nullptr, get); - get = (char*) hashMap_get(map, NULL); - POINTERS_EQUAL(NULL,get); + get = (char*) hashMap_get(map, nullptr); + EXPECT_EQ(nullptr,get); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - get = (char*) hashMap_get(map, NULL); - STRCMP_EQUAL(get, value3); + get = (char*) hashMap_get(map, nullptr); + EXPECT_STREQ(get, value3); free(getKey); free(neKey); hashMap_clear(map, true, true); } -TEST(hash_map_hash, containsKey) { +TEST_F(DeprecatedHashmapTestSuite, containsKeyTest) { char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * containsKey = my_strdup("key"); - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); hashMap_clear(map, false, false); @@ -1274,33 +1207,33 @@ TEST(hash_map_hash, containsKey) { // Add second entry hashMap_put(map, key2, value2); - CHECK(hashMap_containsKey(map, containsKey)); + EXPECT_TRUE(hashMap_containsKey(map, containsKey)); free(containsKey); containsKey = my_strdup("key2"); - CHECK(hashMap_containsKey(map, containsKey)); + EXPECT_TRUE(hashMap_containsKey(map, containsKey)); free(containsKey); containsKey = my_strdup("notExisting"); - CHECK(!hashMap_containsKey(map, containsKey)); + EXPECT_TRUE(!hashMap_containsKey(map, containsKey)); free(containsKey); - containsKey = NULL; - CHECK(!hashMap_containsKey(map, containsKey)); + containsKey = nullptr; + EXPECT_TRUE(!hashMap_containsKey(map, containsKey)); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - containsKey = NULL; - CHECK(hashMap_containsKey(map, containsKey)); + containsKey = nullptr; + EXPECT_TRUE(hashMap_containsKey(map, containsKey)); hashMap_clear(map, true, true); } -TEST(hash_map_hash, getEntry){ +TEST_F(DeprecatedHashmapTestSuite, getEntryTest){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * getEntryKey; - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); hash_map_entry_pt entry; @@ -1315,39 +1248,39 @@ TEST(hash_map_hash, getEntry){ // Get with new created key getEntryKey = my_strdup("key"); entry = hashMap_getEntry(map, getEntryKey); - CHECK(entry != NULL); - STRCMP_EQUAL(key, (char*) entry->key); - STRCMP_EQUAL(value, (char*) entry->value); + EXPECT_TRUE(entry != nullptr); + EXPECT_STREQ(key, (char*) entry->key); + EXPECT_STREQ(value, (char*) entry->value); free(getEntryKey); getEntryKey = my_strdup("key2"); entry = hashMap_getEntry(map, getEntryKey); - CHECK(entry != NULL); - STRCMP_EQUAL(key2, (char*) entry->key); - STRCMP_EQUAL(value2, (char*) entry->value); + EXPECT_TRUE(entry != nullptr); + EXPECT_STREQ(key2, (char*) entry->key); + EXPECT_STREQ(value2, (char*) entry->value); free(getEntryKey); getEntryKey = my_strdup("notExisting"); entry = hashMap_getEntry(map, getEntryKey); - POINTERS_EQUAL(NULL, entry); + EXPECT_EQ(nullptr, entry); free(getEntryKey); - getEntryKey = NULL; + getEntryKey = nullptr; entry = hashMap_getEntry(map, getEntryKey); - POINTERS_EQUAL(NULL, entry); + EXPECT_EQ(nullptr, entry); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); - getEntryKey = NULL; + getEntryKey = nullptr; entry = hashMap_getEntry(map, getEntryKey); - CHECK_EQUAL(key3, entry->key); - STRCMP_EQUAL(value3, (char*) entry->value); + EXPECT_EQ(key3, entry->key); + EXPECT_STREQ(value3, (char*) entry->value); hashMap_clear(map, true, true); } -TEST(hash_map_hash, put){ +TEST_F(DeprecatedHashmapTestSuite, putTest){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); @@ -1357,10 +1290,10 @@ TEST(hash_map_hash, put){ char * nkey2 = my_strdup("key2"); char * nvalue2 = my_strdup("value3"); char * old; - char * key3 = NULL; + char * key3 = nullptr; char * value3 = my_strdup("value3"); char * key4 = my_strdup("key4"); - char * value4 = NULL; + char * value4 = nullptr; hashMap_clear(map, false, false); @@ -1372,37 +1305,37 @@ TEST(hash_map_hash, put){ // Get with new key get = (char*) hashMap_get(map, getKey); - STRCMP_EQUAL(value, get); + EXPECT_STREQ(value, get); free(getKey); getKey = my_strdup("key2"); get = (char*) hashMap_get(map, getKey); - STRCMP_EQUAL(value2, get); + EXPECT_STREQ(value2, get); // Overwrite existing entry old = (char *) hashMap_put(map, nkey2, nvalue2); - CHECK(old != NULL); - STRCMP_EQUAL(value2, old); + EXPECT_TRUE(old != nullptr); + EXPECT_STREQ(value2, old); free(getKey); getKey = my_strdup("key2"); get = (char*) hashMap_get(map, key2); - STRCMP_EQUAL(nvalue2, get); + EXPECT_STREQ(nvalue2, get); - // Add third entry with NULL key + // Add third entry with nullptr key hashMap_put(map, key3, value3); free(getKey); - getKey = NULL; + getKey = nullptr; get = (char*) hashMap_get(map, key3); - STRCMP_EQUAL(value3, get); + EXPECT_STREQ(value3, get); - // Add fourth entry with NULL value + // Add fourth entry with nullptr value hashMap_put(map, key4, value4); getKey = my_strdup("key4"); get = (char*) hashMap_get(map, key4); - CHECK_EQUAL(value4, get); + EXPECT_EQ(value4, get); free(getKey); free(value2); @@ -1410,10 +1343,10 @@ TEST(hash_map_hash, put){ hashMap_clear(map, true, true); } -TEST(hash_map_hash, remove){ +TEST_F(DeprecatedHashmapTestSuite, removeTest){ char * key = my_strdup("key"); char * value = my_strdup("value"); - char * key2 = NULL; + char * key2 = nullptr; char * value2 = my_strdup("value2"); char * removeKey = my_strdup("nonexisting"); @@ -1427,26 +1360,26 @@ TEST(hash_map_hash, remove){ // Remove non-existing entry for map hashMap_remove(map, removeKey); - LONGS_EQUAL(2, map->size); - CHECK(!hashMap_isEmpty(map)); + EXPECT_EQ(2, map->size); + EXPECT_TRUE(!hashMap_isEmpty(map)); // Remove entry with new key free(removeKey); removeKey = my_strdup("key"); hashMap_remove(map, removeKey); - LONGS_EQUAL(1, map->size); + EXPECT_EQ(1, map->size); free(removeKey); - removeKey = NULL; + removeKey = nullptr; hashMap_remove(map, removeKey); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); // Remove non-existing entry for empty map removeKey = my_strdup("nonexisting"); hashMap_remove(map, removeKey); - LONGS_EQUAL(0, map->size); - CHECK(hashMap_isEmpty(map)); + EXPECT_EQ(0, map->size); + EXPECT_TRUE(hashMap_isEmpty(map)); free(removeKey); free(key); @@ -1455,14 +1388,14 @@ TEST(hash_map_hash, remove){ hashMap_clear(map, true, true); } -TEST(hash_map_hash, containsValue){ +TEST_F(DeprecatedHashmapTestSuite, containsValueTest){ char * key = my_strdup("key"); char * value = my_strdup("value"); char * key2 = my_strdup("key2"); char * value2 = my_strdup("value2"); char * containsValue = my_strdup("value"); char * key3 = my_strdup("key3"); - char * value3 = NULL; + char * value3 = nullptr; hashMap_clear(map, false, false); @@ -1472,22 +1405,22 @@ TEST(hash_map_hash, containsValue){ // Add second entry hashMap_put(map, key2, value2); - CHECK(hashMap_containsValue(map, containsValue)); + EXPECT_TRUE(hashMap_containsValue(map, containsValue)); free(containsValue); containsValue = my_strdup("value2"); - CHECK(hashMap_containsValue(map, containsValue)); + EXPECT_TRUE(hashMap_containsValue(map, containsValue)); free(containsValue); containsValue = my_strdup("notExisting"); - CHECK(!hashMap_containsValue(map, containsValue)); + EXPECT_TRUE(!hashMap_containsValue(map, containsValue)); free(containsValue); - containsValue = NULL; - CHECK(!hashMap_containsValue(map, containsValue)); + containsValue = nullptr; + EXPECT_TRUE(!hashMap_containsValue(map, containsValue)); - // Add third entry with NULL value + // Add third entry with nullptr value hashMap_put(map, key3, value3); - containsValue = NULL; - CHECK(hashMap_containsValue(map, containsValue)); + containsValue = nullptr; + EXPECT_TRUE(hashMap_containsValue(map, containsValue)); hashMap_clear(map, true, true); } diff --git a/libs/utils/src/hash_map.c b/libs/utils/src/hash_map.c index 40bcb0ed9..ed664e752 100644 --- a/libs/utils/src/hash_map.c +++ b/libs/utils/src/hash_map.c @@ -25,7 +25,6 @@ */ #include -#include #include #include #include diff --git a/libs/utils/src/hash_map_private.h b/libs/utils/src/hash_map_private.h index 2cd6819d9..32e1233ac 100644 --- a/libs/utils/src/hash_map_private.h +++ b/libs/utils/src/hash_map_private.h @@ -29,13 +29,17 @@ #include "hash_map.h" -unsigned int hashMap_hashCode(const void* toHash); -int hashMap_equals(const void* toCompare, const void* compare); +#ifdef __cplusplus +extern "C" { +#endif -void hashMap_resize(hash_map_pt map, int newCapacity); -hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key); -hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry); -void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex); +CELIX_UTILS_DEPRECATED_EXPORT unsigned int hashMap_hashCode(const void* toHash); +CELIX_UTILS_DEPRECATED_EXPORT int hashMap_equals(const void* toCompare, const void* compare); + +CELIX_UTILS_DEPRECATED_EXPORT void hashMap_resize(hash_map_pt map, int newCapacity); +CELIX_UTILS_DEPRECATED_EXPORT hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key); +CELIX_UTILS_DEPRECATED_EXPORT hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry); +CELIX_UTILS_DEPRECATED_EXPORT void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex); struct hashMapEntry { void* key; @@ -69,5 +73,8 @@ struct hashMapEntrySet { hash_map_pt map; }; +#ifdef __cplusplus +} +#endif #endif /* HASH_MAP_PRIVATE_H_ */ From d1f7d10abe3ce23006d1007d1a16100b8af1b8c2 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 30 Dec 2023 17:31:22 +0100 Subject: [PATCH 06/18] Refactor ip utils and ip utils tests Also improves error handling --- libs/utils/gtest/CMakeLists.txt | 1 + .../src/IpUtilsErrorInjectionTestSuite.cc | 16 +- libs/utils/gtest/src/IpUtilsTestSuite.cc | 125 ++++++++++++ libs/utils/include/celix_ip_utils.h | 108 ++++++++++ libs/utils/include_deprecated/ip_utils.h | 52 ----- libs/utils/private/test/ip_utils_test.cpp | 108 ---------- libs/utils/src/ip_utils.c | 192 +++++++++++------- 7 files changed, 361 insertions(+), 241 deletions(-) create mode 100644 libs/utils/gtest/src/IpUtilsTestSuite.cc create mode 100644 libs/utils/include/celix_ip_utils.h delete mode 100644 libs/utils/include_deprecated/ip_utils.h delete mode 100644 libs/utils/private/test/ip_utils_test.cpp diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt index b492009f3..44a4aa426 100644 --- a/libs/utils/gtest/CMakeLists.txt +++ b/libs/utils/gtest/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(test_utils src/ThreadsTestSuite.cc src/CelixErrnoTestSuite.cc src/CelixUtilsAutoCleanupTestSuite.cc + src/IpUtilsTestSuite.cc src/DeprecatedHashmapTestSuite.cc ) diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc index 57a4c83cd..93bd5626b 100644 --- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc @@ -17,10 +17,12 @@ under the License. */ -#include #include + +#include + #include "ifaddrs_ei.h" -#include "ip_utils.h" +#include "celix_ip_utils.h" #include "celix_utils_ei.h" class IpUtilsWithErrorInjectionTestSuite : public ::testing::Test { @@ -28,20 +30,20 @@ class IpUtilsWithErrorInjectionTestSuite : public ::testing::Test { IpUtilsWithErrorInjectionTestSuite() = default; ~IpUtilsWithErrorInjectionTestSuite() override { celix_ei_expect_getifaddrs(nullptr, 0, 0); - celix_ei_expect_celix_utils_strdup(nullptr, 0, 0); + celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr); } }; TEST_F(IpUtilsWithErrorInjectionTestSuite, failToGetInterfaceAddresses) { - celix_ei_expect_getifaddrs((void *)&ipUtils_findIpBySubnet, 0, -1); - auto ipAddresses = ipUtils_findIpBySubnet("192.168.1.0/24"); + celix_ei_expect_getifaddrs((void *)&celix_utils_findIpInSubnet, 0, -1); + auto ipAddresses = celix_utils_findIpInSubnet("192.168.1.0/24"); EXPECT_EQ(ipAddresses, nullptr); EXPECT_EQ(errno, EMFILE); } TEST_F(IpUtilsWithErrorInjectionTestSuite, failToDuplicateString) { - celix_ei_expect_celix_utils_strdup((void *) &ipUtils_findIpBySubnet, 0, nullptr); - auto ipAddresses = ipUtils_findIpBySubnet("192.168.1.0/24"); + celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr); + auto ipAddresses = celix_utils_findIpInSubnet("192.168.1.0/24"); EXPECT_EQ(ipAddresses, nullptr); EXPECT_EQ(errno, ENOMEM); } \ No newline at end of file diff --git a/libs/utils/gtest/src/IpUtilsTestSuite.cc b/libs/utils/gtest/src/IpUtilsTestSuite.cc new file mode 100644 index 000000000..5a2d1aa4b --- /dev/null +++ b/libs/utils/gtest/src/IpUtilsTestSuite.cc @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include + +#include "celix_err.h" +#include "celix_ip_utils.h" +#include "celix_stdlib_cleanup.h" + +class IpUtilsTestSuite : public ::testing::Test { +public: + IpUtilsTestSuite() { + celix_err_resetErrors(); + } + ~IpUtilsTestSuite() override { + celix_err_printErrors(stderr, nullptr, nullptr); + } +}; + +TEST_F(IpUtilsTestSuite, ipToUnsignedIntTest) { + const char *ip = "192.168.1.64"; + uint32_t expected = 3232235840; + uint32_t actual = celix_utils_convertIpToUint(ip, nullptr); + EXPECT_EQ(expected, actual); + + bool converted; + actual = celix_utils_convertIpToUint(ip, &converted); + EXPECT_TRUE(converted); + EXPECT_EQ(expected, actual); + + EXPECT_EQ(0, celix_utils_convertIpToUint("a.b.c.d", &converted)); + EXPECT_FALSE(converted); + EXPECT_EQ(1, celix_err_getErrorCount()); + + EXPECT_EQ(0, celix_utils_convertIpToUint("273.168.1.64", &converted)); + EXPECT_FALSE(converted); + EXPECT_EQ(2, celix_err_getErrorCount()); + + EXPECT_EQ(0, celix_utils_convertIpToUint("10.1.1.1.1", &converted)); + EXPECT_FALSE(converted); + EXPECT_EQ(3, celix_err_getErrorCount()); +} + +TEST_F(IpUtilsTestSuite, unsignedIntToIpTest) { + uint32_t ipAsUint = 3232235840; + const char *expected = "192.168.1.64"; + char* ip = celix_utils_convertUintToIp(ipAsUint); + EXPECT_STREQ(expected, ip); + free(ip); + + ip = celix_utils_convertUintToIp(0); + EXPECT_STREQ("0.0.0.0", ip); + free(ip); + + ip = celix_utils_convertUintToIp(UINT32_MAX); + EXPECT_STREQ("255.255.255.255", ip); + free(ip); +} + +TEST_F(IpUtilsTestSuite, prefixToBitmaskTest) { + uint32_t bitmask = celix_utils_ipPrefixLengthToBitmask(27); + EXPECT_EQ(4294967264, bitmask); + + bitmask = celix_utils_ipPrefixLengthToBitmask(0); + EXPECT_EQ(0, bitmask); + + bitmask = celix_utils_ipPrefixLengthToBitmask(32); + EXPECT_EQ(UINT32_MAX, bitmask); + + bitmask = celix_utils_ipPrefixLengthToBitmask(-1); + EXPECT_EQ(0, bitmask); + + bitmask = celix_utils_ipPrefixLengthToBitmask(33); + EXPECT_EQ(0, bitmask); +} + +TEST_F(IpUtilsTestSuite, NetmaskToPrefixTest) { + int prefix = celix_utils_ipNetmaskToPrefixLength("255.255.255.0"); + EXPECT_EQ(24, prefix); + + prefix = celix_utils_ipNetmaskToPrefixLength("0.0.0.0"); + EXPECT_EQ(0, prefix); + + prefix = celix_utils_ipNetmaskToPrefixLength("255.255.255.255"); + EXPECT_EQ(32, prefix); + + prefix = celix_utils_ipNetmaskToPrefixLength("255.0.0.0"); + EXPECT_EQ(8, prefix); + + EXPECT_EQ(-1, celix_utils_ipNetmaskToPrefixLength("a.b.c.d")); + EXPECT_EQ(1, celix_err_getErrorCount()); +} + +TEST_F(IpUtilsTestSuite, FindIpInSubnetWithInvalidInputTest) { + EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1")); // missing subnet + EXPECT_EQ(1, celix_err_getErrorCount()); + + EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/abc")); // invalid subnet + EXPECT_EQ(2, celix_err_getErrorCount()); + + EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/40")); // out of range subnet + EXPECT_EQ(3, celix_err_getErrorCount()); + + EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/-1")); // out of range subnet + EXPECT_EQ(4, celix_err_getErrorCount()); + + EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("a.b.c.d/8")); // invalid ip + EXPECT_EQ(5, celix_err_getErrorCount()); +} diff --git a/libs/utils/include/celix_ip_utils.h b/libs/utils/include/celix_ip_utils.h new file mode 100644 index 000000000..3e487476c --- /dev/null +++ b/libs/utils/include/celix_ip_utils.h @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * @file celix_ip_utils.h + * @brief Utility functions for IP address manipulation. + * + * This header file contains declarations of utility functions for + * converting IP addresses and manipulating IP address data. + */ + +#ifndef CELIX_IP_UTILS_H +#define CELIX_IP_UTILS_H + +#include "celix_utils_export.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Converts an IP address in string format to an unsigned integer. + * + * This function takes an IP address as a string and converts it to + * its corresponding unsigned integer representation. + * + * @param[in] ip The IP address in string format (e.g., "192.168.0.1"). + * @param[out] converted A boolean indicating whether the conversion was successful. Can be NULL. + * @return The IP address as an uint32_t. Returns 0 if the conversion fails. + */ +uint32_t celix_utils_convertIpToUint(const char* ip, bool* converted); + +/** + * @brief Converts an unsigned integer to its corresponding IP address in string format. + * + * This function converts an unsigned integer representing an IP address + * to a string format. + * + * @param[in] ip The IP address as an uint32_t. + * @return The IP address in string format (e.g., "192.168.0.1"). + */ +char* celix_utils_convertUintToIp(uint32_t ip); + +/** + * @brief Converts a subnet prefix length to a bitmask. + * + * This function takes a subnet prefix length and converts it into + * a bitmask in unsigned integer format. + * + * @param[in] prefix The subnet prefix length. + * @return The corresponding bitmask as an unsigned integer. Returns 0 if the prefix is invalid or 0. + */ +uint32_t celix_utils_ipPrefixLengthToBitmask(int prefix); + +/** + * @brief Converts a netmask string to a prefix length. + * + * This function converts a netmask in string format (e.g., "255.255.255.0") + * to its corresponding prefix length. + * + * @param[in] netmask The netmask in string format. + * @return The prefix length or -1 if the conversion fails. + */ +int celix_utils_ipNetmaskToPrefixLength(const char* netmask); + +/** + * @brief Finds an IP address within a given subnet on the host's network interfaces. + * + * This function searches through the network interfaces of the host to find + * an IP address that falls within the specified subnet. It analyzes the IP addresses + * assigned to each network interface and checks if any of them match the given subnet criteria. + * The function returns the first IP address that is within the specified subnet range. + * + * The input parameter is expected to be in CIDR notation. + * CIDR notation is a concise format for specifying IP addresses ranges using IP address and subnet prefix length. + * It takes the form of 'IP_ADDRESS/PREFIX_LENGTH' (e.g., "192.168.0.1/24"). + * + * @param[in] subnetCidrNotation The IP address with subnet prefix. + * @return A string containing an IP address within the specified subnet that is also + * assigned to a network interface on the host, or NULL if no matching IP address is found. The caller is owner of the + * returned string. + */ +char* celix_utils_findIpInSubnet(const char* subnetCidrNotation); + +#ifdef __cplusplus +} +#endif + +#endif /* CELIX_IP_UTILS_H */ diff --git a/libs/utils/include_deprecated/ip_utils.h b/libs/utils/include_deprecated/ip_utils.h deleted file mode 100644 index 75adcb595..000000000 --- a/libs/utils/include_deprecated/ip_utils.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * ip_utils.h - * - * \date Jun 24, 2019 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef IP_UTILS_H_ -#define IP_UTILS_H_ - -#include - -#include "celix_errno.h" -#include "celix_utils_export.h" - -#ifdef __cplusplus -extern "C" { -#endif - -CELIX_UTILS_DEPRECATED_EXPORT unsigned int ipUtils_ipToUnsignedInt(char *ip); - -CELIX_UTILS_DEPRECATED_EXPORT char *ipUtils_unsignedIntToIp(unsigned int ip); - -CELIX_UTILS_DEPRECATED_EXPORT unsigned int ipUtils_prefixToBitmask(unsigned int prefix); - -CELIX_UTILS_DEPRECATED_EXPORT int ipUtils_netmaskToPrefix(const char *netmask); - -CELIX_UTILS_DEPRECATED_EXPORT char *ipUtils_findIpBySubnet(const char *ipWithPrefix); - -#ifdef __cplusplus -} -#endif -#endif /* IP_UTILS_H_ */ diff --git a/libs/utils/private/test/ip_utils_test.cpp b/libs/utils/private/test/ip_utils_test.cpp deleted file mode 100644 index 7b576b3af..000000000 --- a/libs/utils/private/test/ip_utils_test.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * ip_utils_test.cpp - * - * \date Jun 24, 2019 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include -#include -#include - -#include "CppUTest/TestHarness.h" -#include "CppUTest/TestHarness_c.h" -#include "CppUTest/CommandLineTestRunner.h" - -extern "C" -{ -#include "ip_utils.h" -} - -int main(int argc, char** argv) { - MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); - return RUN_ALL_TESTS(argc, argv); -} - -static char* my_strdup(const char* s){ - if(s==NULL){ - return NULL; - } - - size_t len = strlen(s); - - char *d = (char*) calloc (len + 1,sizeof(char)); - - if (d == NULL){ - return NULL; - } - - strncpy (d,s,len); - return d; -} - -TEST_GROUP(ip_utils) { - void setup(void) { - } - - void teardown() { - } -}; - -TEST(ip_utils, ipToUnsignedInt){ - char *ip = my_strdup("192.168.1.64"); - - unsigned int expected = 3232235840; - unsigned int actual = ipUtils_ipToUnsignedInt(ip); - UNSIGNED_LONGS_EQUAL(expected, actual); - - free(ip); -} - -TEST(ip_utils, unsignedIntToIp){ - unsigned int ipAsUint = 3232235840; - - const char *expected = "192.168.1.64"; - char *actual = ipUtils_unsignedIntToIp(ipAsUint); - STRCMP_EQUAL(expected, actual); - free(actual); -} - -TEST(ip_utils, prefixToBitmask){ - unsigned int expected = 4294967264; - unsigned int actual = ipUtils_prefixToBitmask(27); - - UNSIGNED_LONGS_EQUAL(expected, actual); -} - -TEST(ip_utils, netmaskToPrefix){ - char *netmask = my_strdup("255.255.255.0"); - - int expected = 24; - int actual = ipUtils_netmaskToPrefix(netmask); - LONGS_EQUAL(expected, actual); - - free(netmask); - - actual = ipUtils_netmaskToPrefix("a.b.c.d"); - LONGS_EQUAL(-1, actual); -} diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c index d168fa367..833bd2039 100644 --- a/libs/utils/src/ip_utils.c +++ b/libs/utils/src/ip_utils.c @@ -16,110 +16,151 @@ * specific language governing permissions and limitations * under the License. */ -/** - * ip_utils.c - * - * \date Jun 24, 2019 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ -#include "ip_utils.h" +#include "celix_ip_utils.h" + +#include "celix_err.h" +#include "celix_stdlib_cleanup.h" #include "celix_utils.h" +#include "celix_convert_utils.h" +#include #include +#include +#include +#include +#include +#include #include #include -#include #include -#include - -#include #include -#include -#include -#include -unsigned int ipUtils_ipToUnsignedInt(char *ip) { - unsigned int ipAsUint = 0; +uint32_t celix_utils_convertIpToUint(const char* ip, bool* converted) { + if (converted) { + *converted = false; + } + + // copy for strtok_r + celix_autofree char* input = strdup(ip); + if (!input) { + celix_err_push("Failed to duplicate input string for IP conversion"); + return 0; + } - char *partOfIp = NULL, *savePtr = NULL; - char *input = strdup(ip); // Make a copy because otherwise strtok_r manipulates the input string - partOfIp = strtok_r(input, ".\0", &savePtr); ipAsUint += strtoul(partOfIp, NULL, 10) * (unsigned int) pow(256, 3); - partOfIp = strtok_r(NULL, ".\0", &savePtr); ipAsUint += strtoul(partOfIp, NULL, 10) * (unsigned int) pow(256, 2); - partOfIp = strtok_r(NULL, ".\0", &savePtr); ipAsUint += strtoul(partOfIp, NULL, 10) * (unsigned int) pow(256, 1); - partOfIp = strtok_r(NULL, ".\0", &savePtr); ipAsUint += strtoul(partOfIp, NULL, 10) * (unsigned int) pow(256, 0); - free(input); + uint32_t ipAsUint = 0; + char* partOfIp = NULL; + char* savePtr = NULL; + int count = 0; + while ((partOfIp = strtok_r(partOfIp == NULL ? input : NULL, ".\0", &savePtr)) != NULL) { + if (count > 3) { + celix_err_pushf("Failed to convert IP address %s to unsigned int, to many parts", ip); + return 0; + } + bool longConverted = false; + long partAsLong = celix_utils_convertStringToLong(partOfIp, ULONG_MAX, &longConverted); + if (!longConverted) { + celix_err_pushf("Failed to convert IP address %s to unsigned int, part `%s` is not a number", ip, partOfIp); + return 0; + } else if (partAsLong > 255 || partAsLong < 0) { + celix_err_pushf("Failed to convert IP address %s to unsigned int, part `%s` is out of range", ip, partOfIp); + return 0; + } + ipAsUint += (uint32_t)(partAsLong * powl(256, 3 - count++)); + } + if (converted) { + *converted = true; + } return ipAsUint; } -char *ipUtils_unsignedIntToIp(unsigned int ip) { - char *ipStr = calloc(16, sizeof(char)); +char* celix_utils_convertUintToIp(uint32_t ip) { + char* ipStr = calloc(16, sizeof(char)); + if (!ipStr) { + celix_err_push("Failed to allocate memory for IP address string"); + return NULL; + } - int ipPart1 = ip / (int) pow(256, 3); ip -= ipPart1 * (int) pow(256, 3); - int ipPart2 = ip / (int) pow(256, 2); ip -= ipPart2 * (int) pow(256, 2); - int ipPart3 = ip / (int) pow(256, 1); ip -= ipPart3 * (int) pow(256, 1); - int ipPart4 = ip / (int) pow(256, 0); + int64_t ipPart1 = ip / (int64_t)pow(256, 3); + ip -= ipPart1 * (int64_t)pow(256, 3); + int64_t ipPart2 = ip / (int64_t)pow(256, 2); + ip -= ipPart2 * (int64_t)pow(256, 2); + int64_t ipPart3 = ip / (int64_t)pow(256, 1); + ip -= ipPart3 * (int64_t)pow(256, 1); + int64_t ipPart4 = ip / (int64_t)pow(256, 0); - snprintf(ipStr, 16, "%d.%d.%d.%d", ipPart1, ipPart2, ipPart3, ipPart4); + snprintf(ipStr, 16, "%li.%li.%li.%li", ipPart1, ipPart2, ipPart3, ipPart4); return ipStr; } -unsigned int ipUtils_prefixToBitmask(unsigned int prefix) { - return (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF; +uint32_t celix_utils_ipPrefixLengthToBitmask(int prefix) { + if (prefix > 32 || prefix <= 0) { + return 0; + } + return (uint32_t )((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF); } -int ipUtils_netmaskToPrefix(const char *netmask) { +int celix_utils_ipNetmaskToPrefixLength(const char* netmask) { // Convert netmask to in_addr object struct in_addr in; int ret = inet_pton(AF_INET, netmask, &in); if (ret != 1) { + celix_err_pushf("Failed to convert netmask %s to in_addr object", netmask); return -1; } // Now convert the mask to a prefix int prefix = 0; - bool processed_one = false; - unsigned int i = ntohl(in.s_addr); + uint32_t i = ntohl(in.s_addr); while (i > 0) { if (i & 1) { prefix++; - processed_one = true; - } else { - if (processed_one) return -1; } - i >>= 1; } return prefix; } -/** Finds an IP of the available network interfaces of the machine by specifying an CIDR subnet. - * - * @param ipWithPrefix IP with prefix, e.g. 192.168.1.0/24 - * @return ip In case a matching interface could be found, an allocated string containing the IP of the - * interface will be returned, e.g. 192.168.1.16. Memory for the new string can be freed with free(). - * When no matching interface is found NULL will be returned. - */ -char *ipUtils_findIpBySubnet(const char *ipWithPrefix) { - char *ip = NULL; +char* celix_utils_findIpInSubnet(const char* subnetCidrNotation) { + char* ip = NULL; - char *input = celix_utils_strdup(ipWithPrefix); // Make a copy as otherwise strtok_r manipulates the input string - if (input == NULL) { - goto strdup_failed; + //create copy for strtok_r + celix_autofree char* input = celix_utils_strdup(subnetCidrNotation); + if (!input) { + celix_err_push("Failed to duplicate input string for subnet search"); + return NULL; } - char *savePtr; - char *inputIp = strtok_r(input, "/", &savePtr); - char *inputPrefixStr = strtok_r(NULL, "\0", &savePtr); - unsigned int inputPrefix = (unsigned int) strtoul(inputPrefixStr, NULL, 10); + char* savePtr; + char* inputIp = strtok_r(input, "/", &savePtr); + char* inputPrefixStr = strtok_r(NULL, "\0", &savePtr); - unsigned int ipAsUint = ipUtils_ipToUnsignedInt(inputIp); - unsigned int bitmask = ipUtils_prefixToBitmask(inputPrefix); + if (!inputPrefixStr) { + celix_err_pushf("Failed to parse IP address with prefix %s. Missing a '/'", subnetCidrNotation); + return NULL; + } + + bool convertedLong = false; + int inputPrefix = (int)celix_utils_convertStringToLong(inputPrefixStr, INT_MAX, &convertedLong); + if (!convertedLong) { + celix_err_pushf("Failed to parse prefix in IP address with prefix %s", subnetCidrNotation); + return NULL; + } else if (inputPrefix > 32 || inputPrefix < 0) { + celix_err_pushf( + "Failed to parse IP address with prefix %s. Prefix %s is out of range", subnetCidrNotation, inputPrefixStr); + return NULL; + } + + bool converted; + uint32_t ipAsUint = celix_utils_convertIpToUint(inputIp, &converted); + if (!converted) { + return NULL; + } + uint32_t bitmask = celix_utils_ipPrefixLengthToBitmask(inputPrefix); unsigned int ipRangeStart = ipAsUint & bitmask; unsigned int ipRangeStop = ipAsUint | ~bitmask; @@ -127,44 +168,47 @@ char *ipUtils_findIpBySubnet(const char *ipWithPrefix) { // Requested IP range is known now, now loop through network interfaces struct ifaddrs *ifap, *ifa; - if(getifaddrs (&ifap) == -1) { - goto getifaddrs_failed; + if (getifaddrs(&ifap) == -1) { + celix_err_push("Failed to get network interfaces"); + return NULL; } + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) + if (ifa->ifa_addr == NULL) { continue; + } - if (ifa->ifa_addr->sa_family != AF_INET) + if (ifa->ifa_addr->sa_family != AF_INET) { continue; + } // Retrieve IP address for interface char if_addr[NI_MAXHOST]; - int rv = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), - if_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + int rv = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), if_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (rv != 0) { - printf("getnameinfo() failed: %s\n", gai_strerror(rv)); + celix_err_pushf("Failed to get IP address for interface %s: %s", ifa->ifa_name, gai_strerror(rv)); continue; } // Retrieve netmask - struct sockaddr_in *sa = (struct sockaddr_in *) ifa->ifa_netmask; - char *if_netmask = inet_ntoa(sa->sin_addr); + struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_netmask; + char* if_netmask = inet_ntoa(sa->sin_addr); - unsigned int ifIpAsUint = ipUtils_ipToUnsignedInt(if_addr); - int ifPrefix = ipUtils_netmaskToPrefix(if_netmask); + uint32_t ifIpAsUint = celix_utils_convertIpToUint(if_addr, NULL); + int ifPrefix = celix_utils_ipNetmaskToPrefixLength(if_netmask); if (ifPrefix == -1) { break; } if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) { - ip = strndup(if_addr, 1024); + ip = celix_utils_strdup(if_addr); + if (!ip) { + celix_err_push("Failed to duplicate IP address"); + break; + } break; } } - freeifaddrs(ifap); -getifaddrs_failed: - free(input); -strdup_failed: return ip; } From 8d113a3faadae6139cada52f57524fca5fd5eba4 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 30 Dec 2023 17:34:33 +0100 Subject: [PATCH 07/18] Remove cpputest from build config --- .github/workflows/coverity-scan.yml | 1 - .github/workflows/macos.yml | 4 ++-- .github/workflows/ubuntu.yml | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/coverity-scan.yml b/.github/workflows/coverity-scan.yml index c9d303d5e..534733474 100644 --- a/.github/workflows/coverity-scan.yml +++ b/.github/workflows/coverity-scan.yml @@ -26,7 +26,6 @@ jobs: cmake \ libffi-dev \ libxml2-dev \ - libcpputest-dev \ rapidjson-dev \ libavahi-compat-libdnssd-dev \ libcivetweb-dev \ diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 026dfbbc7..08cb8da96 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -59,7 +59,7 @@ jobs: -o celix/*:framework_curlinit=False -o celix/*:enable_ccache=True run: | - conan build . -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b default -pr:h default -s:b build_type=Release -s:h build_type=Release -of build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest + conan build . -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b default -pr:h default -s:b build_type=Release -s:h build_type=Release -of build ${CONAN_BUILD_OPTIONS} -b missing - name: Test run: | cd build @@ -75,7 +75,7 @@ jobs: uses: actions/checkout@v3.3.0 - name: Install dependencies run: | - brew install lcov cpputest jansson rapidjson libzip ccache ninja + brew install lcov jansson rapidjson libzip ccache ninja - name: Prepare ccache timestamp id: ccache_cache_timestamp run: | diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index df0d9f402..df2994f1f 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -80,7 +80,7 @@ jobs: -o celix:framework_curlinit=False -o celix:enable_ccache=True run: | - conan install . celix/ci -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b release -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest --require-override=openssl/1.1.1s + conan install . celix/ci -c tools.cmake.cmaketoolchain:generator=Ninja -pr:b release -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing --require-override=openssl/1.1.1s - name: Build env: CC: ${{ matrix.compiler[0] }} @@ -119,7 +119,6 @@ jobs: cmake \ libffi-dev \ libxml2-dev \ - libcpputest-dev \ rapidjson-dev \ libavahi-compat-libdnssd-dev \ libcivetweb-dev \ From 01b3af14dbf197239b7403e72cbb538e461a4099 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sat, 30 Dec 2023 19:40:17 +0100 Subject: [PATCH 08/18] Improve error handling of IP utils --- CHANGES.md | 3 + .../src/IpUtilsErrorInjectionTestSuite.cc | 58 +++++++++++++++---- libs/utils/gtest/src/IpUtilsTestSuite.cc | 12 ++-- libs/utils/include/celix_ip_utils.h | 15 ++++- libs/utils/src/ip_utils.c | 30 ++++++---- 5 files changed, 88 insertions(+), 30 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1b712a700..fd5270b2b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -53,6 +53,9 @@ limitations under the License. - C Properties are no longer a direct typedef of `hashmap`. - celix_string/long_hashmap put functions now return a celix_status_t instead of bool (value replaced). THe celix_status_t is used to indicate an ENOMEM error. +- linked_list.h is removed and no longer supported. Use celix_array_list.h instead. +- IP utils is refactored and the API is changed and all IP utils functions are now prefixed with `celix_utils_`. +- array_list.h is removed and no longer supported. Use celix_array_list.h instead. ## New Features diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc index 93bd5626b..8bbed74aa 100644 --- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc @@ -21,29 +21,67 @@ #include -#include "ifaddrs_ei.h" #include "celix_ip_utils.h" +#include "celix_err.h" + #include "celix_utils_ei.h" +#include "ifaddrs_ei.h" +#include "malloc_ei.h" class IpUtilsWithErrorInjectionTestSuite : public ::testing::Test { public: - IpUtilsWithErrorInjectionTestSuite() = default; + IpUtilsWithErrorInjectionTestSuite() { + celix_err_resetErrors(); + } + ~IpUtilsWithErrorInjectionTestSuite() override { celix_ei_expect_getifaddrs(nullptr, 0, 0); celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr); + celix_ei_expect_calloc(nullptr, 0, nullptr); + celix_err_resetErrors(); } }; -TEST_F(IpUtilsWithErrorInjectionTestSuite, failToGetInterfaceAddresses) { +TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToGetInterfaceAddressesTest) { celix_ei_expect_getifaddrs((void *)&celix_utils_findIpInSubnet, 0, -1); - auto ipAddresses = celix_utils_findIpInSubnet("192.168.1.0/24"); - EXPECT_EQ(ipAddresses, nullptr); - EXPECT_EQ(errno, EMFILE); + char* ipAddr = nullptr; + auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); + EXPECT_EQ(EMFILE, status); + EXPECT_EQ(ipAddr, nullptr); + EXPECT_EQ(1, celix_err_getErrorCount()); } -TEST_F(IpUtilsWithErrorInjectionTestSuite, failToDuplicateString) { +TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToDuplicateStringTest) { + int errCount = 1; + char* ipAddr = nullptr; + + //first call to celix_utils_strdup fails celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr); - auto ipAddresses = celix_utils_findIpInSubnet("192.168.1.0/24"); - EXPECT_EQ(ipAddresses, nullptr); + auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); + EXPECT_EQ(status, ENOMEM); + EXPECT_EQ(ipAddr, nullptr); + EXPECT_EQ(errCount++, celix_err_getErrorCount()); + + //second call to celix_utils_strdup fails (in ifa -> ifa_next loop) + celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr, 2); + status = celix_utils_findIpInSubnet("127.0.0.1/24", &ipAddr); + EXPECT_EQ(status, ENOMEM); + EXPECT_EQ(ipAddr, nullptr); + EXPECT_EQ(errCount++, celix_err_getErrorCount()); + + celix_ei_expect_celix_utils_strdup((void *) &celix_utils_convertIpToUint, 0, nullptr); + bool converted; + auto ipAsUint = celix_utils_convertIpToUint("192.168.1.0", &converted); + EXPECT_EQ(ipAsUint, 0); + EXPECT_FALSE(converted); + EXPECT_EQ(errno, ENOMEM); + EXPECT_EQ(errCount++, celix_err_getErrorCount()); +} + +TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToCalledTest) { + celix_ei_expect_calloc((void*)celix_utils_convertUintToIp, 0, nullptr); + auto ip = celix_utils_convertUintToIp(3232235840); + EXPECT_EQ(ip, nullptr); EXPECT_EQ(errno, ENOMEM); -} \ No newline at end of file + EXPECT_EQ(1, celix_err_getErrorCount()); +} diff --git a/libs/utils/gtest/src/IpUtilsTestSuite.cc b/libs/utils/gtest/src/IpUtilsTestSuite.cc index 5a2d1aa4b..27b5933ea 100644 --- a/libs/utils/gtest/src/IpUtilsTestSuite.cc +++ b/libs/utils/gtest/src/IpUtilsTestSuite.cc @@ -108,18 +108,20 @@ TEST_F(IpUtilsTestSuite, NetmaskToPrefixTest) { } TEST_F(IpUtilsTestSuite, FindIpInSubnetWithInvalidInputTest) { - EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1")); // missing subnet + char* ipAddr = nullptr; + + EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1", &ipAddr)); // missing subnet EXPECT_EQ(1, celix_err_getErrorCount()); - EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/abc")); // invalid subnet + EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/abc", &ipAddr)); // invalid subnet EXPECT_EQ(2, celix_err_getErrorCount()); - EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/40")); // out of range subnet + EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/40", &ipAddr)); // out of range subnet EXPECT_EQ(3, celix_err_getErrorCount()); - EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("198.168.0.1/-1")); // out of range subnet + EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/-1", &ipAddr)); // out of range subnet EXPECT_EQ(4, celix_err_getErrorCount()); - EXPECT_EQ(nullptr, celix_utils_findIpInSubnet("a.b.c.d/8")); // invalid ip + EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("a.b.c.d/8", &ipAddr)); // invalid ip EXPECT_EQ(5, celix_err_getErrorCount()); } diff --git a/libs/utils/include/celix_ip_utils.h b/libs/utils/include/celix_ip_utils.h index 3e487476c..6ad89f072 100644 --- a/libs/utils/include/celix_ip_utils.h +++ b/libs/utils/include/celix_ip_utils.h @@ -28,10 +28,11 @@ #ifndef CELIX_IP_UTILS_H #define CELIX_IP_UTILS_H +#include "celix_errno.h" #include "celix_utils_export.h" -#include #include +#include #ifdef __cplusplus extern "C" { @@ -43,6 +44,8 @@ extern "C" { * This function takes an IP address as a string and converts it to * its corresponding unsigned integer representation. * + * If the conversation failed, an error message is logged to celix_err. + * * @param[in] ip The IP address in string format (e.g., "192.168.0.1"). * @param[out] converted A boolean indicating whether the conversion was successful. Can be NULL. * @return The IP address as an uint32_t. Returns 0 if the conversion fails. @@ -77,6 +80,8 @@ uint32_t celix_utils_ipPrefixLengthToBitmask(int prefix); * This function converts a netmask in string format (e.g., "255.255.255.0") * to its corresponding prefix length. * + * If the conversation failed, an error message is logged to celix_err. + * * @param[in] netmask The netmask in string format. * @return The prefix length or -1 if the conversion fails. */ @@ -94,12 +99,16 @@ int celix_utils_ipNetmaskToPrefixLength(const char* netmask); * CIDR notation is a concise format for specifying IP addresses ranges using IP address and subnet prefix length. * It takes the form of 'IP_ADDRESS/PREFIX_LENGTH' (e.g., "192.168.0.1/24"). * + * If an error occurred, an error message is logged to celix_err. + * * @param[in] subnetCidrNotation The IP address with subnet prefix. - * @return A string containing an IP address within the specified subnet that is also + * @param[out] foundIp A string containing an IP address within the specified subnet that is also * assigned to a network interface on the host, or NULL if no matching IP address is found. The caller is owner of the * returned string. + * @return CELIX_SUCCESS if the IP address was found, CELIX_ILLEGAL_ARGUMENT if the subnet is invalid, CELIX_ENOMEM if + * an error occurred or a errno value set by getifaddrs. */ -char* celix_utils_findIpInSubnet(const char* subnetCidrNotation); +celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** foundIp); #ifdef __cplusplus } diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c index 833bd2039..316382018 100644 --- a/libs/utils/src/ip_utils.c +++ b/libs/utils/src/ip_utils.c @@ -25,6 +25,7 @@ #include "celix_convert_utils.h" #include +#include #include #include #include @@ -42,7 +43,7 @@ uint32_t celix_utils_convertIpToUint(const char* ip, bool* converted) { } // copy for strtok_r - celix_autofree char* input = strdup(ip); + celix_autofree char* input = celix_utils_strdup(ip); if (!input) { celix_err_push("Failed to duplicate input string for IP conversion"); return 0; @@ -125,14 +126,17 @@ int celix_utils_ipNetmaskToPrefixLength(const char* netmask) { return prefix; } -char* celix_utils_findIpInSubnet(const char* subnetCidrNotation) { - char* ip = NULL; +celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** foundIp) { + assert(subnetCidrNotation); + assert(foundIp); + + *foundIp = NULL; //create copy for strtok_r celix_autofree char* input = celix_utils_strdup(subnetCidrNotation); if (!input) { celix_err_push("Failed to duplicate input string for subnet search"); - return NULL; + return CELIX_ENOMEM; } char* savePtr; @@ -141,24 +145,24 @@ char* celix_utils_findIpInSubnet(const char* subnetCidrNotation) { if (!inputPrefixStr) { celix_err_pushf("Failed to parse IP address with prefix %s. Missing a '/'", subnetCidrNotation); - return NULL; + return CELIX_ILLEGAL_ARGUMENT; } bool convertedLong = false; int inputPrefix = (int)celix_utils_convertStringToLong(inputPrefixStr, INT_MAX, &convertedLong); if (!convertedLong) { celix_err_pushf("Failed to parse prefix in IP address with prefix %s", subnetCidrNotation); - return NULL; + return CELIX_ILLEGAL_ARGUMENT; } else if (inputPrefix > 32 || inputPrefix < 0) { celix_err_pushf( "Failed to parse IP address with prefix %s. Prefix %s is out of range", subnetCidrNotation, inputPrefixStr); - return NULL; + return CELIX_ILLEGAL_ARGUMENT; } bool converted; uint32_t ipAsUint = celix_utils_convertIpToUint(inputIp, &converted); if (!converted) { - return NULL; + return CELIX_ILLEGAL_ARGUMENT; } uint32_t bitmask = celix_utils_ipPrefixLengthToBitmask(inputPrefix); @@ -169,8 +173,9 @@ char* celix_utils_findIpInSubnet(const char* subnetCidrNotation) { struct ifaddrs *ifap, *ifa; if (getifaddrs(&ifap) == -1) { + celix_status_t status = errno; celix_err_push("Failed to get network interfaces"); - return NULL; + return status; } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { @@ -202,13 +207,14 @@ char* celix_utils_findIpInSubnet(const char* subnetCidrNotation) { } if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) { - ip = celix_utils_strdup(if_addr); + char* ip = celix_utils_strdup(if_addr); if (!ip) { celix_err_push("Failed to duplicate IP address"); - break; + return CELIX_ENOMEM; } + *foundIp = ip; break; } } - return ip; + return CELIX_SUCCESS; } From 61c641abe6479472a00ddf2a1a68490cef2a1167 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 00:41:17 +0100 Subject: [PATCH 09/18] Replace array_list with celix_array_list in rsa shm v2 --- .../rsa_shm/src/rsa_shm_impl.c | 8 ++++---- .../rsa_shm/src/rsa_shm_impl.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.c b/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.c index c93ac4955..ba2fd6f31 100755 --- a/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.c +++ b/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.c @@ -380,9 +380,9 @@ celix_status_t rsaShm_exportService(rsa_shm_t *admin, char *serviceId, } // Create export registrations for its interfaces. - size_t interfaceNum = arrayList_size(interfaces); + size_t interfaceNum = celix_arrayList_size(interfaces); for (int iter = 0; iter < interfaceNum; iter++) { - char *interface = arrayList_get(interfaces, iter); + char *interface = celix_arrayList_get(interfaces, iter); celix_autoptr(endpoint_description_t) endpointDescription = NULL; export_registration_t *registration = NULL; int ret = CELIX_SUCCESS; @@ -538,13 +538,13 @@ static celix_status_t rsaShm_createEndpointDescription(rsa_shm_t *admin, } //LCOV_EXCL_START -celix_status_t rsaShm_getExportedServices(rsa_shm_t *admin CELIX_UNUSED, array_list_pt *services CELIX_UNUSED) { +celix_status_t rsaShm_getExportedServices(rsa_shm_t *admin CELIX_UNUSED, celix_array_list_t** services CELIX_UNUSED) { celix_status_t status = CELIX_SUCCESS; //It is stub and will not be called at present. return status; } -celix_status_t rsaShm_getImportedEndpoints(rsa_shm_t *admin CELIX_UNUSED, array_list_pt *services CELIX_UNUSED) { +celix_status_t rsaShm_getImportedEndpoints(rsa_shm_t *admin CELIX_UNUSED, celix_array_list_t** services CELIX_UNUSED) { celix_status_t status = CELIX_SUCCESS; //It is stub and will not be called at present. return status; diff --git a/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.h b/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.h index 4d8e4a39f..72dc471c1 100644 --- a/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.h +++ b/bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/src/rsa_shm_impl.h @@ -23,6 +23,7 @@ #ifdef __cplusplus extern "C" { #endif + #include "rsa_shm_export_registration.h" #include "rsa_shm_import_registration.h" #include "endpoint_description.h" @@ -49,9 +50,9 @@ celix_status_t rsaShm_exportService(rsa_shm_t *admin, char *serviceId, celix_status_t rsaShm_removeExportedService(rsa_shm_t *admin, export_registration_t *registration); -celix_status_t rsaShm_getExportedServices(rsa_shm_t *admin, array_list_pt *services); +celix_status_t rsaShm_getExportedServices(rsa_shm_t *admin, celix_array_list_t** services); -celix_status_t rsaShm_getImportedEndpoints(rsa_shm_t *admin, array_list_pt *services); +celix_status_t rsaShm_getImportedEndpoints(rsa_shm_t *admin, celix_array_list_t** services); celix_status_t rsaShm_importService(rsa_shm_t *admin, endpoint_description_t *endpointDescription, import_registration_t **registration); From a851bf9fb6b8132b596212accf81f05013c819f3 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 00:46:45 +0100 Subject: [PATCH 10/18] Replace array_list with celix_array_list in conan exmpl test --- examples/conan_test_package/my_rsa_activator.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/conan_test_package/my_rsa_activator.c b/examples/conan_test_package/my_rsa_activator.c index e8f817141..8ba78934c 100644 --- a/examples/conan_test_package/my_rsa_activator.c +++ b/examples/conan_test_package/my_rsa_activator.c @@ -34,21 +34,21 @@ typedef struct my_remote_service_admin_activator { long svcIdRsa; } my_remote_service_admin_activator_t; -static celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, array_list_pt *registrations) { +static celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, char *serviceId, celix_properties_t *properties, celix_array_list_t** registrations) { (void)properties; (void)registrations; celix_logHelper_info(admin->loghelper, "%s called: serviceId=%s\n", __FUNCTION__, serviceId); return CELIX_SUCCESS; } -static celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, array_list_pt *services) { +static celix_status_t remoteServiceAdmin_getExportedServices(remote_service_admin_t *admin, celix_array_list_t** services) { celix_status_t status = CELIX_SUCCESS; (void)services; celix_logHelper_info(admin->loghelper, "%s called\n", __FUNCTION__); return status; } -static celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, array_list_pt *services) { +static celix_status_t remoteServiceAdmin_getImportedEndpoints(remote_service_admin_t *admin, celix_array_list_t** services) { celix_status_t status = CELIX_SUCCESS; (void)services; celix_logHelper_info(admin->loghelper, "%s called\n", __FUNCTION__); From f2317ed1dba8cea8d9716e9e0c41fb041e3d719d Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 01:27:10 +0100 Subject: [PATCH 11/18] Separate celix err ei test to prevent unintentionally init of tss --- libs/utils/gtest/CMakeLists.txt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt index 0351ba150..012d2cd32 100644 --- a/libs/utils/gtest/CMakeLists.txt +++ b/libs/utils/gtest/CMakeLists.txt @@ -92,12 +92,24 @@ setup_target_for_coverage(test_utils SCAN_DIR ..) if (EI_TESTS) + #Note testing celix err separated, otherwise celix err tss can already be initialized by another util function + add_executable(test_celix_err_with_ei + src/ErrErrorInjectionTestSuite.cc + ) + target_link_libraries(test_celix_err_with_ei PRIVATE + utils_cut + Celix::malloc_ei + Celix::threads_ei + GTest::gtest GTest::gtest_main + ) + add_test(NAME test_celix_err_with_ei COMMAND test_celix_err_with_ei) + setup_target_for_coverage(test_celix_err_with_ei SCAN_DIR ..) + add_executable(test_utils_with_ei src/FileUtilsErrorInjectionTestSuite.cc src/ConvertUtilsErrorInjectionTestSuite.cc src/IpUtilsErrorInjectionTestSuite.cc src/ArrayListErrorInjectionTestSuite.cc - src/ErrErrorInjectionTestSuite.cc src/PropertiesErrorInjectionTestSuite.cc src/VersionErrorInjectionTestSuite.cc src/HashMapErrorInjectionTestSuite.cc From 4bb89900f7773b63d1069a822942e3020b66db71 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 01:28:11 +0100 Subject: [PATCH 12/18] Fix memleak in celix_utils_findIpInSubnet when calling getifaddrs --- libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc | 10 +++------- libs/utils/src/ip_utils.c | 2 ++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc index 8bbed74aa..9f0dce5bf 100644 --- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc @@ -19,8 +19,6 @@ #include -#include - #include "celix_ip_utils.h" #include "celix_err.h" @@ -56,25 +54,24 @@ TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToDuplicateStringTest) { char* ipAddr = nullptr; //first call to celix_utils_strdup fails - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr); auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); EXPECT_EQ(status, ENOMEM); EXPECT_EQ(ipAddr, nullptr); EXPECT_EQ(errCount++, celix_err_getErrorCount()); //second call to celix_utils_strdup fails (in ifa -> ifa_next loop) - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr, 2); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr, 2); status = celix_utils_findIpInSubnet("127.0.0.1/24", &ipAddr); EXPECT_EQ(status, ENOMEM); EXPECT_EQ(ipAddr, nullptr); EXPECT_EQ(errCount++, celix_err_getErrorCount()); - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_convertIpToUint, 0, nullptr); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_convertIpToUint, 0, nullptr); bool converted; auto ipAsUint = celix_utils_convertIpToUint("192.168.1.0", &converted); EXPECT_EQ(ipAsUint, 0); EXPECT_FALSE(converted); - EXPECT_EQ(errno, ENOMEM); EXPECT_EQ(errCount++, celix_err_getErrorCount()); } @@ -82,6 +79,5 @@ TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToCalledTest) { celix_ei_expect_calloc((void*)celix_utils_convertUintToIp, 0, nullptr); auto ip = celix_utils_convertUintToIp(3232235840); EXPECT_EQ(ip, nullptr); - EXPECT_EQ(errno, ENOMEM); EXPECT_EQ(1, celix_err_getErrorCount()); } diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c index 316382018..12cde0a38 100644 --- a/libs/utils/src/ip_utils.c +++ b/libs/utils/src/ip_utils.c @@ -209,6 +209,7 @@ celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) { char* ip = celix_utils_strdup(if_addr); if (!ip) { + freeifaddrs(ifap); celix_err_push("Failed to duplicate IP address"); return CELIX_ENOMEM; } @@ -216,5 +217,6 @@ celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** break; } } + freeifaddrs(ifap); return CELIX_SUCCESS; } From 1f8dea82bbfddf6941465b27db4fd4cdf2af9139 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 18:10:49 +0100 Subject: [PATCH 13/18] Fix invalid snprintf Also fix invalid array create in dm_dependency_manager_impl.c and add missing status initialize in service_registry.c. --- libs/framework/src/dm_dependency_manager_impl.c | 3 ++- libs/framework/src/service_registry.c | 2 +- libs/utils/src/ip_utils.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libs/framework/src/dm_dependency_manager_impl.c b/libs/framework/src/dm_dependency_manager_impl.c index 8c5744287..d4e40f465 100644 --- a/libs/framework/src/dm_dependency_manager_impl.c +++ b/libs/framework/src/dm_dependency_manager_impl.c @@ -30,6 +30,7 @@ #include "celix_bundle.h" #include "celix_compiler.h" #include "celix_framework.h" +#include "celix_array_list.h" celix_dependency_manager_t* celix_private_dependencyManager_create(celix_bundle_context_t *context) { celix_dependency_manager_t *manager = calloc(1, sizeof(*manager)); @@ -367,7 +368,7 @@ celix_status_t dependencyManager_getInfo(celix_dependency_manager_t *manager, dm celixThreadMutex_lock(&manager->mutex); if (info != NULL) { - celix_arrayList_create(&info->components); + info->components = celix_arrayList_create(); size = celix_arrayList_size(manager->components); for (i = 0; i < size; i += 1) { celix_dm_component_t *cmp = celix_arrayList_get(manager->components, i); diff --git a/libs/framework/src/service_registry.c b/libs/framework/src/service_registry.c index f75da80e8..5872dabea 100644 --- a/libs/framework/src/service_registry.c +++ b/libs/framework/src/service_registry.c @@ -677,7 +677,7 @@ size_t serviceRegistry_nrOfHooks(service_registry_pt registry) { } static celix_status_t serviceRegistry_getUsingBundles(service_registry_pt registry, service_registration_pt registration, celix_array_list_t** out) { - celix_status_t status; + celix_status_t status = CELIX_SUCCESS; celix_array_list_t* bundles = NULL; hash_map_iterator_pt iter; diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c index 12cde0a38..8d5e0df46 100644 --- a/libs/utils/src/ip_utils.c +++ b/libs/utils/src/ip_utils.c @@ -91,7 +91,7 @@ char* celix_utils_convertUintToIp(uint32_t ip) { ip -= ipPart3 * (int64_t)pow(256, 1); int64_t ipPart4 = ip / (int64_t)pow(256, 0); - snprintf(ipStr, 16, "%li.%li.%li.%li", ipPart1, ipPart2, ipPart3, ipPart4); + snprintf(ipStr, 16, "%i.%i.%i.%i", (int)ipPart1, (int)ipPart2, (int)ipPart3, (int)ipPart4); return ipStr; } From 1a67dabc5b87f0c8250ce4351df310873be3b0b9 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 20:15:15 +0100 Subject: [PATCH 14/18] Replace EXPECT_EQ for pointer compare --- libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc b/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc index 46fdf5493..429d43d7e 100644 --- a/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc +++ b/libs/utils/gtest/src/DeprecatedHashmapTestSuite.cc @@ -102,10 +102,10 @@ TEST_F(DeprecatedHashmapTestSuite, create){ EXPECT_TRUE(defaultMap != nullptr); EXPECT_EQ(0, defaultMap->size); // This fails on windows due to dllimport providing a proxy for exported functions. - EXPECT_EQ(hashMap_equals, defaultMap->equalsKey); - EXPECT_EQ(hashMap_equals, defaultMap->equalsValue); - EXPECT_EQ(hashMap_hashCode, defaultMap->hashKey); - EXPECT_EQ(hashMap_hashCode, defaultMap->hashValue); + EXPECT_TRUE(hashMap_equals == defaultMap->equalsKey); + EXPECT_TRUE(hashMap_equals == defaultMap->equalsValue); + EXPECT_TRUE(hashMap_hashCode == defaultMap->hashKey); + EXPECT_TRUE(hashMap_hashCode == defaultMap->hashValue); } TEST_F(DeprecatedHashmapTestSuite, size){ From b4b26a3f7e06d3037e8399ceb09903e6a17b70ab Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Thu, 4 Jan 2024 14:16:12 +0100 Subject: [PATCH 15/18] Remove ip utils --- CHANGES.md | 2 +- libs/utils/CMakeLists.txt | 1 - libs/utils/gtest/CMakeLists.txt | 2 - .../src/IpUtilsErrorInjectionTestSuite.cc | 83 ------- libs/utils/gtest/src/IpUtilsTestSuite.cc | 127 ---------- libs/utils/include/celix_ip_utils.h | 117 --------- libs/utils/src/ip_utils.c | 222 ------------------ 7 files changed, 1 insertion(+), 553 deletions(-) delete mode 100644 libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc delete mode 100644 libs/utils/gtest/src/IpUtilsTestSuite.cc delete mode 100644 libs/utils/include/celix_ip_utils.h delete mode 100644 libs/utils/src/ip_utils.c diff --git a/CHANGES.md b/CHANGES.md index 157978a4b..12ddff48e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -55,7 +55,7 @@ limitations under the License. THe celix_status_t is used to indicate an ENOMEM error. - Embedded bundles are no longer supported. - linked_list.h is removed and no longer supported. Use celix_array_list.h instead. -- IP utils is refactored and the API is changed and all IP utils functions are now prefixed with `celix_utils_`. +- ip_utils.h is removed and no longer supported. - array_list.h is removed and no longer supported. Use celix_array_list.h instead. ## New Features diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt index a9b31234b..0f4dd6418 100644 --- a/libs/utils/CMakeLists.txt +++ b/libs/utils/CMakeLists.txt @@ -38,7 +38,6 @@ if (UTILS) src/version_range.c src/properties.c src/utils.c - src/ip_utils.c src/filter.c src/celix_log_level.c src/celix_log_utils.c diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt index 012d2cd32..e1e22ef7e 100644 --- a/libs/utils/gtest/CMakeLists.txt +++ b/libs/utils/gtest/CMakeLists.txt @@ -34,7 +34,6 @@ add_executable(test_utils src/ThreadsTestSuite.cc src/CelixErrnoTestSuite.cc src/CelixUtilsAutoCleanupTestSuite.cc - src/IpUtilsTestSuite.cc src/DeprecatedHashmapTestSuite.cc ) @@ -108,7 +107,6 @@ if (EI_TESTS) add_executable(test_utils_with_ei src/FileUtilsErrorInjectionTestSuite.cc src/ConvertUtilsErrorInjectionTestSuite.cc - src/IpUtilsErrorInjectionTestSuite.cc src/ArrayListErrorInjectionTestSuite.cc src/PropertiesErrorInjectionTestSuite.cc src/VersionErrorInjectionTestSuite.cc diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc deleted file mode 100644 index 9f0dce5bf..000000000 --- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc +++ /dev/null @@ -1,83 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. - */ - -#include - -#include "celix_ip_utils.h" -#include "celix_err.h" - -#include "celix_utils_ei.h" -#include "ifaddrs_ei.h" -#include "malloc_ei.h" - -class IpUtilsWithErrorInjectionTestSuite : public ::testing::Test { -public: - IpUtilsWithErrorInjectionTestSuite() { - celix_err_resetErrors(); - } - - ~IpUtilsWithErrorInjectionTestSuite() override { - celix_ei_expect_getifaddrs(nullptr, 0, 0); - celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr); - celix_ei_expect_calloc(nullptr, 0, nullptr); - celix_err_resetErrors(); - } -}; - -TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToGetInterfaceAddressesTest) { - celix_ei_expect_getifaddrs((void *)&celix_utils_findIpInSubnet, 0, -1); - char* ipAddr = nullptr; - auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); - EXPECT_EQ(EMFILE, status); - EXPECT_EQ(ipAddr, nullptr); - EXPECT_EQ(1, celix_err_getErrorCount()); -} - -TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToDuplicateStringTest) { - int errCount = 1; - char* ipAddr = nullptr; - - //first call to celix_utils_strdup fails - celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr); - auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); - EXPECT_EQ(status, ENOMEM); - EXPECT_EQ(ipAddr, nullptr); - EXPECT_EQ(errCount++, celix_err_getErrorCount()); - - //second call to celix_utils_strdup fails (in ifa -> ifa_next loop) - celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr, 2); - status = celix_utils_findIpInSubnet("127.0.0.1/24", &ipAddr); - EXPECT_EQ(status, ENOMEM); - EXPECT_EQ(ipAddr, nullptr); - EXPECT_EQ(errCount++, celix_err_getErrorCount()); - - celix_ei_expect_celix_utils_strdup((void*)celix_utils_convertIpToUint, 0, nullptr); - bool converted; - auto ipAsUint = celix_utils_convertIpToUint("192.168.1.0", &converted); - EXPECT_EQ(ipAsUint, 0); - EXPECT_FALSE(converted); - EXPECT_EQ(errCount++, celix_err_getErrorCount()); -} - -TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToCalledTest) { - celix_ei_expect_calloc((void*)celix_utils_convertUintToIp, 0, nullptr); - auto ip = celix_utils_convertUintToIp(3232235840); - EXPECT_EQ(ip, nullptr); - EXPECT_EQ(1, celix_err_getErrorCount()); -} diff --git a/libs/utils/gtest/src/IpUtilsTestSuite.cc b/libs/utils/gtest/src/IpUtilsTestSuite.cc deleted file mode 100644 index 27b5933ea..000000000 --- a/libs/utils/gtest/src/IpUtilsTestSuite.cc +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include - -#include "celix_err.h" -#include "celix_ip_utils.h" -#include "celix_stdlib_cleanup.h" - -class IpUtilsTestSuite : public ::testing::Test { -public: - IpUtilsTestSuite() { - celix_err_resetErrors(); - } - ~IpUtilsTestSuite() override { - celix_err_printErrors(stderr, nullptr, nullptr); - } -}; - -TEST_F(IpUtilsTestSuite, ipToUnsignedIntTest) { - const char *ip = "192.168.1.64"; - uint32_t expected = 3232235840; - uint32_t actual = celix_utils_convertIpToUint(ip, nullptr); - EXPECT_EQ(expected, actual); - - bool converted; - actual = celix_utils_convertIpToUint(ip, &converted); - EXPECT_TRUE(converted); - EXPECT_EQ(expected, actual); - - EXPECT_EQ(0, celix_utils_convertIpToUint("a.b.c.d", &converted)); - EXPECT_FALSE(converted); - EXPECT_EQ(1, celix_err_getErrorCount()); - - EXPECT_EQ(0, celix_utils_convertIpToUint("273.168.1.64", &converted)); - EXPECT_FALSE(converted); - EXPECT_EQ(2, celix_err_getErrorCount()); - - EXPECT_EQ(0, celix_utils_convertIpToUint("10.1.1.1.1", &converted)); - EXPECT_FALSE(converted); - EXPECT_EQ(3, celix_err_getErrorCount()); -} - -TEST_F(IpUtilsTestSuite, unsignedIntToIpTest) { - uint32_t ipAsUint = 3232235840; - const char *expected = "192.168.1.64"; - char* ip = celix_utils_convertUintToIp(ipAsUint); - EXPECT_STREQ(expected, ip); - free(ip); - - ip = celix_utils_convertUintToIp(0); - EXPECT_STREQ("0.0.0.0", ip); - free(ip); - - ip = celix_utils_convertUintToIp(UINT32_MAX); - EXPECT_STREQ("255.255.255.255", ip); - free(ip); -} - -TEST_F(IpUtilsTestSuite, prefixToBitmaskTest) { - uint32_t bitmask = celix_utils_ipPrefixLengthToBitmask(27); - EXPECT_EQ(4294967264, bitmask); - - bitmask = celix_utils_ipPrefixLengthToBitmask(0); - EXPECT_EQ(0, bitmask); - - bitmask = celix_utils_ipPrefixLengthToBitmask(32); - EXPECT_EQ(UINT32_MAX, bitmask); - - bitmask = celix_utils_ipPrefixLengthToBitmask(-1); - EXPECT_EQ(0, bitmask); - - bitmask = celix_utils_ipPrefixLengthToBitmask(33); - EXPECT_EQ(0, bitmask); -} - -TEST_F(IpUtilsTestSuite, NetmaskToPrefixTest) { - int prefix = celix_utils_ipNetmaskToPrefixLength("255.255.255.0"); - EXPECT_EQ(24, prefix); - - prefix = celix_utils_ipNetmaskToPrefixLength("0.0.0.0"); - EXPECT_EQ(0, prefix); - - prefix = celix_utils_ipNetmaskToPrefixLength("255.255.255.255"); - EXPECT_EQ(32, prefix); - - prefix = celix_utils_ipNetmaskToPrefixLength("255.0.0.0"); - EXPECT_EQ(8, prefix); - - EXPECT_EQ(-1, celix_utils_ipNetmaskToPrefixLength("a.b.c.d")); - EXPECT_EQ(1, celix_err_getErrorCount()); -} - -TEST_F(IpUtilsTestSuite, FindIpInSubnetWithInvalidInputTest) { - char* ipAddr = nullptr; - - EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1", &ipAddr)); // missing subnet - EXPECT_EQ(1, celix_err_getErrorCount()); - - EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/abc", &ipAddr)); // invalid subnet - EXPECT_EQ(2, celix_err_getErrorCount()); - - EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/40", &ipAddr)); // out of range subnet - EXPECT_EQ(3, celix_err_getErrorCount()); - - EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("198.168.0.1/-1", &ipAddr)); // out of range subnet - EXPECT_EQ(4, celix_err_getErrorCount()); - - EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_utils_findIpInSubnet("a.b.c.d/8", &ipAddr)); // invalid ip - EXPECT_EQ(5, celix_err_getErrorCount()); -} diff --git a/libs/utils/include/celix_ip_utils.h b/libs/utils/include/celix_ip_utils.h deleted file mode 100644 index 6ad89f072..000000000 --- a/libs/utils/include/celix_ip_utils.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * @file celix_ip_utils.h - * @brief Utility functions for IP address manipulation. - * - * This header file contains declarations of utility functions for - * converting IP addresses and manipulating IP address data. - */ - -#ifndef CELIX_IP_UTILS_H -#define CELIX_IP_UTILS_H - -#include "celix_errno.h" -#include "celix_utils_export.h" - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Converts an IP address in string format to an unsigned integer. - * - * This function takes an IP address as a string and converts it to - * its corresponding unsigned integer representation. - * - * If the conversation failed, an error message is logged to celix_err. - * - * @param[in] ip The IP address in string format (e.g., "192.168.0.1"). - * @param[out] converted A boolean indicating whether the conversion was successful. Can be NULL. - * @return The IP address as an uint32_t. Returns 0 if the conversion fails. - */ -uint32_t celix_utils_convertIpToUint(const char* ip, bool* converted); - -/** - * @brief Converts an unsigned integer to its corresponding IP address in string format. - * - * This function converts an unsigned integer representing an IP address - * to a string format. - * - * @param[in] ip The IP address as an uint32_t. - * @return The IP address in string format (e.g., "192.168.0.1"). - */ -char* celix_utils_convertUintToIp(uint32_t ip); - -/** - * @brief Converts a subnet prefix length to a bitmask. - * - * This function takes a subnet prefix length and converts it into - * a bitmask in unsigned integer format. - * - * @param[in] prefix The subnet prefix length. - * @return The corresponding bitmask as an unsigned integer. Returns 0 if the prefix is invalid or 0. - */ -uint32_t celix_utils_ipPrefixLengthToBitmask(int prefix); - -/** - * @brief Converts a netmask string to a prefix length. - * - * This function converts a netmask in string format (e.g., "255.255.255.0") - * to its corresponding prefix length. - * - * If the conversation failed, an error message is logged to celix_err. - * - * @param[in] netmask The netmask in string format. - * @return The prefix length or -1 if the conversion fails. - */ -int celix_utils_ipNetmaskToPrefixLength(const char* netmask); - -/** - * @brief Finds an IP address within a given subnet on the host's network interfaces. - * - * This function searches through the network interfaces of the host to find - * an IP address that falls within the specified subnet. It analyzes the IP addresses - * assigned to each network interface and checks if any of them match the given subnet criteria. - * The function returns the first IP address that is within the specified subnet range. - * - * The input parameter is expected to be in CIDR notation. - * CIDR notation is a concise format for specifying IP addresses ranges using IP address and subnet prefix length. - * It takes the form of 'IP_ADDRESS/PREFIX_LENGTH' (e.g., "192.168.0.1/24"). - * - * If an error occurred, an error message is logged to celix_err. - * - * @param[in] subnetCidrNotation The IP address with subnet prefix. - * @param[out] foundIp A string containing an IP address within the specified subnet that is also - * assigned to a network interface on the host, or NULL if no matching IP address is found. The caller is owner of the - * returned string. - * @return CELIX_SUCCESS if the IP address was found, CELIX_ILLEGAL_ARGUMENT if the subnet is invalid, CELIX_ENOMEM if - * an error occurred or a errno value set by getifaddrs. - */ -celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** foundIp); - -#ifdef __cplusplus -} -#endif - -#endif /* CELIX_IP_UTILS_H */ diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c deleted file mode 100644 index 8d5e0df46..000000000 --- a/libs/utils/src/ip_utils.c +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "celix_ip_utils.h" - -#include "celix_err.h" -#include "celix_stdlib_cleanup.h" -#include "celix_utils.h" -#include "celix_convert_utils.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -uint32_t celix_utils_convertIpToUint(const char* ip, bool* converted) { - if (converted) { - *converted = false; - } - - // copy for strtok_r - celix_autofree char* input = celix_utils_strdup(ip); - if (!input) { - celix_err_push("Failed to duplicate input string for IP conversion"); - return 0; - } - - uint32_t ipAsUint = 0; - char* partOfIp = NULL; - char* savePtr = NULL; - int count = 0; - while ((partOfIp = strtok_r(partOfIp == NULL ? input : NULL, ".\0", &savePtr)) != NULL) { - if (count > 3) { - celix_err_pushf("Failed to convert IP address %s to unsigned int, to many parts", ip); - return 0; - } - bool longConverted = false; - long partAsLong = celix_utils_convertStringToLong(partOfIp, ULONG_MAX, &longConverted); - if (!longConverted) { - celix_err_pushf("Failed to convert IP address %s to unsigned int, part `%s` is not a number", ip, partOfIp); - return 0; - } else if (partAsLong > 255 || partAsLong < 0) { - celix_err_pushf("Failed to convert IP address %s to unsigned int, part `%s` is out of range", ip, partOfIp); - return 0; - } - ipAsUint += (uint32_t)(partAsLong * powl(256, 3 - count++)); - } - - if (converted) { - *converted = true; - } - return ipAsUint; -} - -char* celix_utils_convertUintToIp(uint32_t ip) { - char* ipStr = calloc(16, sizeof(char)); - if (!ipStr) { - celix_err_push("Failed to allocate memory for IP address string"); - return NULL; - } - - int64_t ipPart1 = ip / (int64_t)pow(256, 3); - ip -= ipPart1 * (int64_t)pow(256, 3); - int64_t ipPart2 = ip / (int64_t)pow(256, 2); - ip -= ipPart2 * (int64_t)pow(256, 2); - int64_t ipPart3 = ip / (int64_t)pow(256, 1); - ip -= ipPart3 * (int64_t)pow(256, 1); - int64_t ipPart4 = ip / (int64_t)pow(256, 0); - - snprintf(ipStr, 16, "%i.%i.%i.%i", (int)ipPart1, (int)ipPart2, (int)ipPart3, (int)ipPart4); - - return ipStr; -} - -uint32_t celix_utils_ipPrefixLengthToBitmask(int prefix) { - if (prefix > 32 || prefix <= 0) { - return 0; - } - return (uint32_t )((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF); -} - -int celix_utils_ipNetmaskToPrefixLength(const char* netmask) { - // Convert netmask to in_addr object - struct in_addr in; - int ret = inet_pton(AF_INET, netmask, &in); - if (ret != 1) { - celix_err_pushf("Failed to convert netmask %s to in_addr object", netmask); - return -1; - } - - // Now convert the mask to a prefix - int prefix = 0; - uint32_t i = ntohl(in.s_addr); - - while (i > 0) { - if (i & 1) { - prefix++; - } - i >>= 1; - } - - return prefix; -} - -celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** foundIp) { - assert(subnetCidrNotation); - assert(foundIp); - - *foundIp = NULL; - - //create copy for strtok_r - celix_autofree char* input = celix_utils_strdup(subnetCidrNotation); - if (!input) { - celix_err_push("Failed to duplicate input string for subnet search"); - return CELIX_ENOMEM; - } - - char* savePtr; - char* inputIp = strtok_r(input, "/", &savePtr); - char* inputPrefixStr = strtok_r(NULL, "\0", &savePtr); - - if (!inputPrefixStr) { - celix_err_pushf("Failed to parse IP address with prefix %s. Missing a '/'", subnetCidrNotation); - return CELIX_ILLEGAL_ARGUMENT; - } - - bool convertedLong = false; - int inputPrefix = (int)celix_utils_convertStringToLong(inputPrefixStr, INT_MAX, &convertedLong); - if (!convertedLong) { - celix_err_pushf("Failed to parse prefix in IP address with prefix %s", subnetCidrNotation); - return CELIX_ILLEGAL_ARGUMENT; - } else if (inputPrefix > 32 || inputPrefix < 0) { - celix_err_pushf( - "Failed to parse IP address with prefix %s. Prefix %s is out of range", subnetCidrNotation, inputPrefixStr); - return CELIX_ILLEGAL_ARGUMENT; - } - - bool converted; - uint32_t ipAsUint = celix_utils_convertIpToUint(inputIp, &converted); - if (!converted) { - return CELIX_ILLEGAL_ARGUMENT; - } - uint32_t bitmask = celix_utils_ipPrefixLengthToBitmask(inputPrefix); - - unsigned int ipRangeStart = ipAsUint & bitmask; - unsigned int ipRangeStop = ipAsUint | ~bitmask; - - // Requested IP range is known now, now loop through network interfaces - struct ifaddrs *ifap, *ifa; - - if (getifaddrs(&ifap) == -1) { - celix_status_t status = errno; - celix_err_push("Failed to get network interfaces"); - return status; - } - - for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) { - continue; - } - - if (ifa->ifa_addr->sa_family != AF_INET) { - continue; - } - - // Retrieve IP address for interface - char if_addr[NI_MAXHOST]; - int rv = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), if_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); - - if (rv != 0) { - celix_err_pushf("Failed to get IP address for interface %s: %s", ifa->ifa_name, gai_strerror(rv)); - continue; - } - - // Retrieve netmask - struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_netmask; - char* if_netmask = inet_ntoa(sa->sin_addr); - - uint32_t ifIpAsUint = celix_utils_convertIpToUint(if_addr, NULL); - int ifPrefix = celix_utils_ipNetmaskToPrefixLength(if_netmask); - if (ifPrefix == -1) { - break; - } - - if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) { - char* ip = celix_utils_strdup(if_addr); - if (!ip) { - freeifaddrs(ifap); - celix_err_push("Failed to duplicate IP address"); - return CELIX_ENOMEM; - } - *foundIp = ip; - break; - } - } - freeifaddrs(ifap); - return CELIX_SUCCESS; -} From 7a9bae954eb145306c2acbff12b8dbdc5261ad1d Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Thu, 4 Jan 2024 14:16:48 +0100 Subject: [PATCH 16/18] Remove unused linked_list source files --- libs/utils/src/linked_list.c | 269 -------------------------- libs/utils/src/linked_list_iterator.c | 154 --------------- libs/utils/src/linked_list_private.h | 44 ----- 3 files changed, 467 deletions(-) delete mode 100644 libs/utils/src/linked_list.c delete mode 100644 libs/utils/src/linked_list_iterator.c delete mode 100644 libs/utils/src/linked_list_private.h diff --git a/libs/utils/src/linked_list.c b/libs/utils/src/linked_list.c deleted file mode 100644 index 3bb0db8fa..000000000 --- a/libs/utils/src/linked_list.c +++ /dev/null @@ -1,269 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list.c - * - * \date Jul 16, 2010 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include - -#include "linked_list.h" -#include "linked_list_private.h" - - -celix_status_t linkedList_create(linked_list_pt *list) { - linked_list_pt linked_list = malloc(sizeof(*linked_list)); - if (linked_list) { - linked_list->header = (linked_list_entry_pt) malloc(sizeof(*(linked_list->header))); - if (linked_list->header) { - linked_list->header->element = NULL; - linked_list->header->next = linked_list->header; - linked_list->header->previous = linked_list->header; - linked_list->size = 0; - linked_list->modificationCount = 0; - - *list = linked_list; - - return CELIX_SUCCESS; - } - } - - return CELIX_ENOMEM; -} - -celix_status_t linkedList_destroy(linked_list_pt list) { - celix_status_t status = CELIX_SUCCESS; - - linked_list_entry_pt current = NULL; - linked_list_entry_pt next = NULL; - - current = list->header->next; - - while (current != list->header) { - next = current->next; - free(current); - current = next; - } - - free(list->header); - free(list); - - return status; -} - -celix_status_t linkedList_clone(linked_list_pt list, linked_list_pt *clone) { - celix_status_t status; - - status = linkedList_create(clone); - if (status == CELIX_SUCCESS) { - struct linked_list_entry *e; - for (e = list->header->next; e != list->header; e = e->next) { - linkedList_addElement(*clone, e->element); - } - } - - return status; -} - -void * linkedList_getFirst(linked_list_pt list) { - if (list->size == 0) { - return NULL; - } - return list->header->next->element; -} - -void * linkedList_getLast(linked_list_pt list) { - if (list->size == 0) { - return NULL; - } - return list->header->previous->element; -} - -void * linkedList_removeFirst(linked_list_pt list) { - return linkedList_removeEntry(list, list->header->next); -} - -void * linkedList_removeLast(linked_list_pt list) { - return linkedList_removeEntry(list, list->header->previous); -} - -void linkedList_addFirst(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header->next); -} - -void linkedList_addLast(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header); -} - -bool linkedList_contains(linked_list_pt list, void * element) { - return linkedList_indexOf(list, element) != -1; -} - -int linkedList_size(linked_list_pt list) { - return list->size; -} - -bool linkedList_isEmpty(linked_list_pt list) { - return linkedList_size(list) == 0; -} - -bool linkedList_addElement(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header); - return true; -} - -bool linkedList_removeElement(linked_list_pt list, void * element) { - if (element == NULL) { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (entry->element == NULL) { - linkedList_removeEntry(list, entry); - return true; - } - } - } else { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (element == entry->element) { - linkedList_removeEntry(list, entry); - return true; - } - } - } - return false; -} - -void linkedList_clear(linked_list_pt list) { - linked_list_entry_pt entry = list->header->next; - while (entry != list->header) { - linked_list_entry_pt next = entry->next; - entry->next = entry->previous = NULL; - // free(entry->element); - entry->element = NULL; - free(entry); - entry = next; - } - list->header->next = list->header->previous = list->header; - list->size = 0; - list->modificationCount++; -} - -void * linkedList_get(linked_list_pt list, int index) { - return linkedList_entry(list, index)->element; -} -void * linkedList_set(linked_list_pt list, int index, void * element) { - linked_list_entry_pt entry = linkedList_entry(list, index); - void * old = entry->element; - entry->element = element; - return old; -} - -void linkedList_addIndex(linked_list_pt list, int index, void * element) { - linkedList_addBefore(list, element, (index == list->size ? list->header : linkedList_entry(list, index))); -} - -void * linkedList_removeIndex(linked_list_pt list, int index) { - return linkedList_removeEntry(list, linkedList_entry(list, index)); -} - -linked_list_entry_pt linkedList_entry(linked_list_pt list, int index) { - linked_list_entry_pt entry; - int i; - if (index < 0 || index >= list->size) { - return NULL; - } - - entry = list->header; - if (index < (list->size >> 1)) { - for (i = 0; i <= index; i++) { - entry = entry->next; - } - } else { - for (i = list->size; i > index; i--) { - entry = entry->previous; - } - } - return entry; -} - -int linkedList_indexOf(linked_list_pt list, void * element) { - int index = 0; - if (element == NULL) { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (entry->element == NULL) { - return index; - } - index++; - } - } else { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (element == entry->element) { - return index; - } - index++; - } - } - return -1; -} - -linked_list_entry_pt linkedList_addBefore(linked_list_pt list, void * element, linked_list_entry_pt entry) { - linked_list_entry_pt new = NULL; - - new = malloc(sizeof(*new)); - if (new != NULL) { - - new->element = element; - new->next = entry; - new->previous = entry->previous; - - new->previous->next = new; - new->next->previous = new; - - list->size++; - list->modificationCount++; - } - - return new; -} - -void * linkedList_removeEntry(linked_list_pt list, linked_list_entry_pt entry) { - void * result; - if (entry == list->header) { - return NULL; - } - - result = entry->element; - - entry->previous->next = entry->next; - entry->next->previous = entry->previous; - - entry->next = entry->previous = NULL; - free(entry); - - list->size--; - list->modificationCount++; - - return result; -} diff --git a/libs/utils/src/linked_list_iterator.c b/libs/utils/src/linked_list_iterator.c deleted file mode 100644 index c42c3d25c..000000000 --- a/libs/utils/src/linked_list_iterator.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list_iterator.c - * - * \date Jul 16, 2010 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#include -#include - -#include "linked_list_iterator.h" -#include "linked_list_private.h" - -struct linkedListIterator { - linked_list_entry_pt lastReturned; - linked_list_entry_pt next; - int nextIndex; - linked_list_pt list; - int expectedModificationCount; -}; - -linked_list_iterator_pt linkedListIterator_create(linked_list_pt list, unsigned int index) { - linked_list_iterator_pt iterator; - if (index > list->size) { - return NULL; - } - iterator = (linked_list_iterator_pt) malloc(sizeof(*iterator)); - iterator->lastReturned = list->header; - iterator->list = list; - iterator->expectedModificationCount = list->modificationCount; - if (index < (list->size >> 1)) { - iterator->next = iterator->list->header->next; - for (iterator->nextIndex = 0; iterator->nextIndex < index; iterator->nextIndex++) { - iterator->next = iterator->next->next; - } - } else { - iterator->next = list->header; - for (iterator->nextIndex = list->size; iterator->nextIndex > index; iterator->nextIndex--) { - iterator->next = iterator->next->previous; - } - } - return iterator; -} - -void linkedListIterator_destroy(linked_list_iterator_pt iterator) { - iterator->expectedModificationCount = 0; - iterator->lastReturned = NULL; - iterator->list = NULL; - iterator->next = NULL; - iterator->nextIndex = 0; - free(iterator); -} - -bool linkedListIterator_hasNext(linked_list_iterator_pt iterator) { - return iterator->nextIndex != iterator->list->size; -} - -void * linkedListIterator_next(linked_list_iterator_pt iterator) { - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return NULL; - } - if (iterator->nextIndex == iterator->list->size) { - return NULL; - } - iterator->lastReturned = iterator->next; - iterator->next = iterator->next->next; - iterator->nextIndex++; - return iterator->lastReturned->element; -} - -bool linkedListIterator_hasPrevious(linked_list_iterator_pt iterator) { - return iterator->nextIndex != 0; -} - -void * linkedListIterator_previous(linked_list_iterator_pt iterator) { - if (iterator->nextIndex == 0) { - return NULL; - } - - iterator->lastReturned = iterator->next = iterator->next->previous; - iterator->nextIndex--; - - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return NULL; - } - - return iterator->lastReturned->element; -} - -int linkedListIterator_nextIndex(linked_list_iterator_pt iterator) { - return iterator->nextIndex; -} - -int linkedListIterator_previousIndex(linked_list_iterator_pt iterator) { - return iterator->nextIndex-1; -} - -void linkedListIterator_remove(linked_list_iterator_pt iterator) { - linked_list_entry_pt lastNext; - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - lastNext = iterator->lastReturned->next; - if (linkedList_removeEntry(iterator->list, iterator->lastReturned) == NULL) { - return; - } - if (iterator->next == iterator->lastReturned) { - iterator->next = lastNext; - } else { - iterator->nextIndex--; - } - iterator->lastReturned = iterator->list->header; - iterator->expectedModificationCount++; -} - -void linkedListIterator_set(linked_list_iterator_pt iterator, void * element) { - if (iterator->lastReturned == iterator->list->header) { - return; - } - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - iterator->lastReturned->element = element; -} - -void linkedListIterator_add(linked_list_iterator_pt iterator, void * element) { - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - iterator->lastReturned = iterator->list->header; - linkedList_addBefore(iterator->list, element, iterator->next); - iterator->nextIndex++; - iterator->expectedModificationCount++; -} - diff --git a/libs/utils/src/linked_list_private.h b/libs/utils/src/linked_list_private.h deleted file mode 100644 index e80026aae..000000000 --- a/libs/utils/src/linked_list_private.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * linked_list_private.h - * - * \date Jul 16, 2010 - * \author Apache Celix Project Team - * \copyright Apache License, Version 2.0 - */ - -#ifndef LINKED_LIST_PRIVATE_H_ -#define LINKED_LIST_PRIVATE_H_ - -#include "linked_list.h" - -struct linked_list_entry { - void * element; - struct linked_list_entry * next; - struct linked_list_entry * previous; -}; - -struct linked_list { - linked_list_entry_pt header; - size_t size; - int modificationCount; -}; - -#endif /* LINKED_LIST_PRIVATE_H_ */ From cd8bfd54d6ca8cd022d98907d3750f7ae6e4e093 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Thu, 4 Jan 2024 14:26:20 +0100 Subject: [PATCH 17/18] Refactor endpointDiscoveryPoller_poll for indent and error handling --- .../include/endpoint_discovery_poller.h | 1 + .../src/endpoint_discovery_poller.c | 103 +++++++++--------- 2 files changed, 50 insertions(+), 54 deletions(-) diff --git a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h index a500b714e..4b9178f36 100644 --- a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h +++ b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h @@ -32,6 +32,7 @@ #include "celix_log_helper.h" #include "celix_threads.h" #include "hash_map.h" +#include "celix_array_list.h" typedef struct endpoint_discovery_poller endpoint_discovery_poller_t; diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c index c286d43ef..edf560fca 100644 --- a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c +++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c @@ -252,89 +252,84 @@ celix_status_t endpointDiscoveryPoller_removeDiscoveryEndpoint(endpoint_discover celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t* poller, char* url, celix_array_list_t* currentEndpoints) { - celix_status_t status; - celix_array_list_t* updatedEndpoints = NULL; - // create an arraylist with a custom equality test to ensure we can find endpoints properly... - updatedEndpoints = celix_arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals); - status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); + celix_array_list_t* updatedEndpoints = celix_arrayList_createWithEquals(endpointDiscoveryPoller_endpointDescriptionEquals); + if (!updatedEndpoints) { + return CELIX_ENOMEM; + } - if (updatedEndpoints && status == CELIX_SUCCESS) { - if (updatedEndpoints != NULL) { - for (int i = celix_arrayList_size(currentEndpoints); i > 0; i--) { - endpoint_description_t* endpoint = celix_arrayList_get(currentEndpoints, i - 1); + celix_status_t status = endpointDiscoveryPoller_getEndpoints(poller, url, &updatedEndpoints); + if (status == CELIX_SUCCESS) { + for (int i = celix_arrayList_size(currentEndpoints); i > 0; i--) { + endpoint_description_t* endpoint = celix_arrayList_get(currentEndpoints, i - 1); - celix_array_list_entry_t entry; - memset(&entry, 0, sizeof(entry)); - entry.voidPtrVal = endpoint; + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = endpoint; - if (celix_arrayList_indexOf(updatedEndpoints, entry) < 0) { - status = discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); - celix_arrayList_removeAt(currentEndpoints, i - 1); - endpointDescription_destroy(endpoint); - } + if (celix_arrayList_indexOf(updatedEndpoints, entry) < 0) { + status = discovery_removeDiscoveredEndpoint(poller->discovery, endpoint); + celix_arrayList_removeAt(currentEndpoints, i - 1); + endpointDescription_destroy(endpoint); } + } - for (int i = 0; i < celix_arrayList_size(updatedEndpoints); i++) { - endpoint_description_t* endpoint = celix_arrayList_get(updatedEndpoints, i); - celix_array_list_entry_t entry; - memset(&entry, 0, sizeof(entry)); - entry.voidPtrVal = endpoint; - if (celix_arrayList_indexOf(currentEndpoints, entry) < 0) { - celix_arrayList_add(currentEndpoints, endpoint); - status = discovery_addDiscoveredEndpoint(poller->discovery, endpoint); - } else { - endpointDescription_destroy(endpoint); - } + for (int i = 0; i < celix_arrayList_size(updatedEndpoints); i++) { + endpoint_description_t* endpoint = celix_arrayList_get(updatedEndpoints, i); + celix_array_list_entry_t entry; + memset(&entry, 0, sizeof(entry)); + entry.voidPtrVal = endpoint; + if (celix_arrayList_indexOf(currentEndpoints, entry) < 0) { + celix_arrayList_add(currentEndpoints, endpoint); + status = discovery_addDiscoveredEndpoint(poller->discovery, endpoint); + } else { + endpointDescription_destroy(endpoint); } - celix_arrayList_clear(updatedEndpoints); } } - if (updatedEndpoints != NULL) { + if (updatedEndpoints) { celix_arrayList_destroy(updatedEndpoints); } return status; } -static void *endpointDiscoveryPoller_performPeriodicPoll(void *data) { - endpoint_discovery_poller_t *poller = (endpoint_discovery_poller_t *) data; +static void* endpointDiscoveryPoller_performPeriodicPoll(void* data) { + endpoint_discovery_poller_t* poller = (endpoint_discovery_poller_t*)data; - useconds_t interval = (useconds_t) (poller->poll_interval * 1000000L); + useconds_t interval = (useconds_t)(poller->poll_interval * 1000000L); - while (poller->running) { - usleep(interval); - celix_status_t status = celixThreadMutex_lock(&poller->pollerLock); + while (poller->running) { + usleep(interval); + celix_status_t status = celixThreadMutex_lock(&poller->pollerLock); - if (status != CELIX_SUCCESS) { + if (status != CELIX_SUCCESS) { celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to obtain lock; retrying..."); - } else { - hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries); + } else { + hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries); - while (hashMapIterator_hasNext(iterator)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); + while (hashMapIterator_hasNext(iterator)) { + hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); - char *url = hashMapEntry_getKey(entry); - celix_array_list_t* currentEndpoints = hashMapEntry_getValue(entry); + char* url = hashMapEntry_getKey(entry); + celix_array_list_t* currentEndpoints = hashMapEntry_getValue(entry); - endpointDiscoveryPoller_poll(poller, url, currentEndpoints); - } + endpointDiscoveryPoller_poll(poller, url, currentEndpoints); + } - hashMapIterator_destroy(iterator); - } + hashMapIterator_destroy(iterator); + } - status = celixThreadMutex_unlock(&poller->pollerLock); - if (status != CELIX_SUCCESS) { + status = celixThreadMutex_unlock(&poller->pollerLock); + if (status != CELIX_SUCCESS) { celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to release lock; retrying..."); - } - } + } + } - return NULL; + return NULL; } - - struct MemoryStruct { char *memory; size_t size; From 36d8c40c5d75b65f718941965ccfff64e3d5bb24 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Thu, 4 Jan 2024 14:26:45 +0100 Subject: [PATCH 18/18] Remove unneeded export macros in hash_map_private.h --- libs/utils/src/hash_map_private.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/utils/src/hash_map_private.h b/libs/utils/src/hash_map_private.h index 32e1233ac..2018404db 100644 --- a/libs/utils/src/hash_map_private.h +++ b/libs/utils/src/hash_map_private.h @@ -33,13 +33,13 @@ extern "C" { #endif -CELIX_UTILS_DEPRECATED_EXPORT unsigned int hashMap_hashCode(const void* toHash); -CELIX_UTILS_DEPRECATED_EXPORT int hashMap_equals(const void* toCompare, const void* compare); +unsigned int hashMap_hashCode(const void* toHash); +int hashMap_equals(const void* toCompare, const void* compare); -CELIX_UTILS_DEPRECATED_EXPORT void hashMap_resize(hash_map_pt map, int newCapacity); -CELIX_UTILS_DEPRECATED_EXPORT hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key); -CELIX_UTILS_DEPRECATED_EXPORT hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry); -CELIX_UTILS_DEPRECATED_EXPORT void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex); +void hashMap_resize(hash_map_pt map, int newCapacity); +hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key); +hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry); +void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex); struct hashMapEntry { void* key;