From 5a2cdd237fff6521b3220d82283284686f6542a9 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 16 Apr 2019 22:09:35 +0200 Subject: [PATCH] replaces dlist by slist --- CMakeLists.txt | 4 +- lib/webfuse/adapter/impl/filesystem.c | 2 - lib/webfuse/adapter/impl/filesystem.h | 4 +- lib/webfuse/adapter/impl/session.c | 48 +++++----- lib/webfuse/adapter/impl/session.h | 10 +- lib/webfuse/adapter/impl/session_manager.c | 73 +++++++-------- lib/webfuse/adapter/impl/session_manager.h | 4 +- lib/webfuse/core/dlist.c | 101 --------------------- lib/webfuse/core/dlist.h | 65 ------------- lib/webfuse/core/slist.c | 77 ++++++++++++++++ lib/webfuse/core/slist.h | 45 +++++++++ test/test_dlist.cc | 49 ---------- test/test_slist.cc | 35 +++++++ 13 files changed, 231 insertions(+), 286 deletions(-) delete mode 100644 lib/webfuse/core/dlist.c delete mode 100644 lib/webfuse/core/dlist.h create mode 100644 lib/webfuse/core/slist.c create mode 100644 lib/webfuse/core/slist.h delete mode 100644 test/test_dlist.cc create mode 100644 test/test_slist.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b8b5f8..774eca6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,7 @@ set(EXTRA_CFLAGS # libwebfuse-core add_library(webfuse-core STATIC - lib/webfuse/core/dlist.c + lib/webfuse/core/slist.c lib/webfuse/core/message.c lib/webfuse/core/message_queue.c lib/webfuse/core/status.c @@ -279,7 +279,6 @@ add_executable(alltests test/msleep.cc test/mock_authenticator.cc test/test_container_of.cc - test/test_dlist.cc test/test_response_parser.cc test/test_server.cc test/test_timepoint.cc @@ -289,6 +288,7 @@ add_executable(alltests test/test_authenticator.cc test/test_authenticators.cc test/test_string.cc + test/test_slist.cc ) target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core ${EXTRA_LIBS} ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES}) diff --git a/lib/webfuse/adapter/impl/filesystem.c b/lib/webfuse/adapter/impl/filesystem.c index 22c827a..3cfcca7 100644 --- a/lib/webfuse/adapter/impl/filesystem.c +++ b/lib/webfuse/adapter/impl/filesystem.c @@ -118,8 +118,6 @@ static bool wf_impl_filesystem_init( char const * name) { bool result = false; - wf_dlist_item_init(&filesystem->item); - char * argv[] = {"", NULL}; filesystem->args.argc = 1; diff --git a/lib/webfuse/adapter/impl/filesystem.h b/lib/webfuse/adapter/impl/filesystem.h index 1fcd9c4..648dfbd 100644 --- a/lib/webfuse/adapter/impl/filesystem.h +++ b/lib/webfuse/adapter/impl/filesystem.h @@ -7,7 +7,7 @@ #include "webfuse/adapter/impl/fuse_wrapper.h" #include "webfuse/adapter/impl/operations.h" -#include "webfuse/core/dlist.h" +#include "webfuse/core/slist.h" #ifdef __cplusplus extern "C" @@ -19,7 +19,7 @@ struct lws; struct wf_impl_filesystem { - struct wf_dlist_item item; + struct wf_slist_item item; struct fuse_args args; struct fuse_session * session; struct fuse_buf buffer; diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index 8111a9e..7551f14 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -48,8 +48,7 @@ struct wf_impl_session * wf_impl_session_create( struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session)); if (NULL != session) { - wf_dlist_item_init(&session->item); - wf_dlist_init(&session->filesystems); + wf_slist_init(&session->filesystems); session->mount_point = strdup(mount_point); session->wsi = wsi; @@ -63,19 +62,24 @@ struct wf_impl_session * wf_impl_session_create( return session; } -static void wf_impl_session_cleanup_filesystem( - struct wf_dlist_item * item, - void * WF_UNUSED_PARAM(user_data)) +static void wf_impl_session_dispose_filesystems( + struct wf_slist * filesystems) { - struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(item, struct wf_impl_filesystem, item); - wf_impl_filesystem_dispose(filesystem); - + struct wf_slist_item * item = filesystems->first; + while (NULL != item) + { + struct wf_slist_item * next = item->next; + struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(item, struct wf_impl_filesystem, item); + wf_impl_filesystem_dispose(filesystem); + + item = next; + } } void wf_impl_session_dispose( struct wf_impl_session * session) { - wf_dlist_cleanup(&session->filesystems, &wf_impl_session_cleanup_filesystem, NULL); + wf_impl_session_dispose_filesystems(&session->filesystems); wf_impl_jsonrpc_proxy_cleanup(&session->rpc); wf_message_queue_cleanup(&session->queue); @@ -101,7 +105,7 @@ bool wf_impl_session_add_filesystem( char const * name) { struct wf_impl_filesystem * filesystem = wf_impl_filesystem_create(session, name); - wf_dlist_prepend(&session->filesystems, &filesystem->item); + wf_slist_append(&session->filesystems, &filesystem->item); return (NULL != filesystem); } @@ -149,30 +153,30 @@ static struct wf_impl_filesystem * wf_impl_session_get_filesystem( struct wf_impl_session * session, struct lws * wsi) { - struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(session->filesystems.first, struct wf_impl_filesystem, item); - while (NULL != filesystem) + struct wf_impl_filesystem * result = NULL; + + struct wf_slist_item * item = session->filesystems.first; + while (NULL != item) { + struct wf_slist_item * next = item->next; + struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(session->filesystems.first, struct wf_impl_filesystem, item); if (wsi == filesystem->wsi) { + result = filesystem; break; } - else - { - filesystem = WF_CONTAINER_OF(filesystem->item.next, struct wf_impl_filesystem, item); - } + + item = next; } - return filesystem; + return result; } bool wf_impl_session_contains_wsi( - struct wf_dlist_item * item, - void * user_data) + struct wf_impl_session * session, + struct lws * wsi) { - struct lws * wsi = user_data; - struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); - bool const result = (NULL != wsi) && ((wsi == session->wsi) || (NULL != wf_impl_session_get_filesystem(session, wsi))); return result; } diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index c0c3f96..8e37be4 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -13,7 +13,7 @@ using std::size_t; #include "webfuse/adapter/impl/jsonrpc/proxy.h" #include "webfuse/adapter/impl/jsonrpc/server.h" #include "webfuse/adapter/impl/filesystem.h" -#include "webfuse/core/dlist.h" +#include "webfuse/core/slist.h" #ifdef __cplusplus extern "C" @@ -28,7 +28,7 @@ struct wf_impl_timeout_manager; struct wf_impl_session { - struct wf_dlist_item item; + struct wf_slist_item item; char * mount_point; struct lws * wsi; bool is_authenticated; @@ -36,7 +36,7 @@ struct wf_impl_session struct wf_impl_authenticators * authenticators; struct wf_impl_jsonrpc_server * server; struct wf_impl_jsonrpc_proxy rpc; - struct wf_dlist filesystems; + struct wf_slist filesystems; }; extern struct wf_impl_session * wf_impl_session_create( @@ -66,8 +66,8 @@ extern void wf_impl_session_onwritable( struct wf_impl_session * session); extern bool wf_impl_session_contains_wsi( - struct wf_dlist_item * item, - void * user_data); + struct wf_impl_session * session, + struct lws * wsi); extern void wf_impl_session_process_filesystem_request( struct wf_impl_session * session, diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index 09a3a21..e93b6c7 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -6,25 +6,21 @@ void wf_impl_session_manager_init( struct wf_impl_session_manager * manager) { - wf_dlist_init(&manager->sessions); + wf_slist_init(&manager->sessions); } -static void wf_impl_session_manager_cleanup_session( - struct wf_dlist_item * item, - void * user_data) -{ - (void) user_data; - struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); - - wf_impl_session_dispose(session); -} - - - void wf_impl_session_manager_cleanup( struct wf_impl_session_manager * manager) { - wf_dlist_cleanup(&manager->sessions, &wf_impl_session_manager_cleanup_session, NULL); + struct wf_slist_item * item = manager->sessions.first; + while (NULL != item) + { + struct wf_slist_item * next = item->next; + struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); + wf_impl_session_dispose(session); + + item = next; + } } struct wf_impl_session * wf_impl_session_manager_add( @@ -39,7 +35,7 @@ struct wf_impl_session * wf_impl_session_manager_add( wsi, authenticators, timeout_manager, server, mount_point); if (NULL != session) { - wf_dlist_prepend(&manager->sessions, &session->item); + wf_slist_append(&manager->sessions, &session->item); } return session; @@ -50,37 +46,42 @@ struct wf_impl_session * wf_impl_session_manager_get( struct lws * wsi) { struct wf_impl_session * session = NULL; - struct wf_dlist_item * item = wf_dlist_find_first( - &manager->sessions, &wf_impl_session_contains_wsi, wsi); - if (NULL != item) + + struct wf_slist_item * item = manager->sessions.first; + while (NULL != item) { - session = WF_CONTAINER_OF(item, struct wf_impl_session, item); + struct wf_slist_item * next = item->next; + struct wf_impl_session * current = WF_CONTAINER_OF(item, struct wf_impl_session, item); + + if (wf_impl_session_contains_wsi(current, wsi)) { + session = current; + break; + } + + item = next; } return session; } -static bool wf_impl_session_manager_remove_predicate( - struct wf_dlist_item * item, - void * user_data) -{ - struct lws * wsi = user_data; - struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); - - return (wsi == session->wsi); -} - void wf_impl_session_manager_remove( struct wf_impl_session_manager * manager, struct lws * wsi) { - struct wf_impl_session * session = NULL; - struct wf_dlist_item * item = wf_dlist_find_first( - &manager->sessions, &wf_impl_session_manager_remove_predicate, wsi); - if (NULL != item) + struct wf_slist_item * item = manager->sessions.first; + struct wf_slist_item * prev = NULL; + while (NULL != item) { - wf_dlist_remove(&manager->sessions, item); - session = WF_CONTAINER_OF(item, struct wf_impl_session, item); - wf_impl_session_dispose(session); + struct wf_slist_item * next = item->next; + struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); + if (wsi == session->wsi) + { + wf_slist_remove_after(&manager->sessions, prev); + wf_impl_session_dispose(session); + break; + } + + prev = item; + item = next; } } diff --git a/lib/webfuse/adapter/impl/session_manager.h b/lib/webfuse/adapter/impl/session_manager.h index 10ac789..bfe4aa3 100644 --- a/lib/webfuse/adapter/impl/session_manager.h +++ b/lib/webfuse/adapter/impl/session_manager.h @@ -7,7 +7,7 @@ #include "webfuse/adapter/impl/session.h" #include "webfuse/adapter/impl/fuse_wrapper.h" -#include "webfuse/core/dlist.h" +#include "webfuse/core/slist.h" #ifdef __cplusplus extern "C" @@ -20,7 +20,7 @@ struct wf_impl_jsonrpc_server; struct wf_impl_session_manager { - struct wf_dlist sessions; + struct wf_slist sessions; }; extern void wf_impl_session_manager_init( diff --git a/lib/webfuse/core/dlist.c b/lib/webfuse/core/dlist.c deleted file mode 100644 index edfd547..0000000 --- a/lib/webfuse/core/dlist.c +++ /dev/null @@ -1,101 +0,0 @@ -#include "webfuse/core/dlist.h" -#include "webfuse/core/util.h" -#include - -void wf_dlist_item_init( - struct wf_dlist_item * item) -{ - item->next = NULL; - item->prev = NULL; -} - -void wf_dlist_item_cleanup( - struct wf_dlist_item * item) -{ - item->next = NULL; - item->prev = NULL; -} - -void wf_dlist_init( - struct wf_dlist * list) -{ - list->first = NULL; -} - -static void wf_dlist_item_cleanup_default( - struct wf_dlist_item * WF_UNUSED_PARAM(item), - void * WF_UNUSED_PARAM(user_data)) -{ - // empty -} - -void wf_dlist_cleanup( - struct wf_dlist * list, - wf_dlist_item_cleanup_fn * cleanup, - void * user_data) -{ - wf_dlist_item_cleanup_fn * effective_cleanup = (NULL != cleanup) ? cleanup : &wf_dlist_item_cleanup_default; - struct wf_dlist_item * item = list->first; - - while (NULL != item) - { - struct wf_dlist_item * next = item->next; - effective_cleanup(item, user_data); - item = next; - } - - list->first = NULL; -} - -void wf_dlist_prepend( - struct wf_dlist * list, - struct wf_dlist_item * item) -{ - if (NULL != list->first) - { - list->first->prev = item; - } - - item->prev = NULL; - item->next = list->first; - list->first = item; -} - -void wf_dlist_remove( - struct wf_dlist * list, - struct wf_dlist_item * item) -{ - if (NULL != item->prev) - { - item->prev->next = item->next; - } - - if (NULL != item->next) - { - item->next->prev = item->prev; - } - - if (list->first == item) - { - list->first = item->next; - } -} - -struct wf_dlist_item * wf_dlist_find_first( - struct wf_dlist * list, - wf_dlist_item_predicate_fn * predicate, - void * user_data) -{ - struct wf_dlist_item * item = list->first; - - while (NULL != item) - { - if (predicate(item, user_data)) - { - break; - } - item = item->next; - } - - return item; -} diff --git a/lib/webfuse/core/dlist.h b/lib/webfuse/core/dlist.h deleted file mode 100644 index 864d882..0000000 --- a/lib/webfuse/core/dlist.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef WF_DLIST_H -#define WF_DLIST_H - -#ifndef __cplusplus -#include -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_dlist_item -{ - struct wf_dlist_item * prev; - struct wf_dlist_item * next; -}; - -struct wf_dlist -{ - struct wf_dlist_item * first; -}; - - -typedef void wf_dlist_item_cleanup_fn( - struct wf_dlist_item * item, - void * user_data); - -typedef bool wf_dlist_item_predicate_fn( - struct wf_dlist_item * item, - void * user_data); - -extern void wf_dlist_item_init( - struct wf_dlist_item * item); - -extern void wf_dlist_item_cleanup( - struct wf_dlist_item * item); - -extern void wf_dlist_init( - struct wf_dlist * list); - -extern void wf_dlist_cleanup( - struct wf_dlist * list, - wf_dlist_item_cleanup_fn * cleanup, - void * user_data); - -extern void wf_dlist_prepend( - struct wf_dlist * list, - struct wf_dlist_item * item); - -extern void wf_dlist_remove( - struct wf_dlist * list, - struct wf_dlist_item * item); - -extern struct wf_dlist_item * wf_dlist_find_first( - struct wf_dlist * list, - wf_dlist_item_predicate_fn * predicate, - void * user_data); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/webfuse/core/slist.c b/lib/webfuse/core/slist.c new file mode 100644 index 0000000..fdc1724 --- /dev/null +++ b/lib/webfuse/core/slist.c @@ -0,0 +1,77 @@ +#include "webfuse/core/slist.h" +#include + +void wf_slist_init( + struct wf_slist * list) +{ + list->first = NULL; + list->last = NULL; +} + +bool wf_slist_empty( + struct wf_slist * list) +{ + return (NULL == list->first); +} + +void wf_slist_append( + struct wf_slist * list, + struct wf_slist_item * item) +{ + item->next = NULL; + + if (NULL != list->last) + { + list->last->next = item; + list->last = item; + } + else + { + list->first = item; + list->last = item; + } +} + +struct wf_slist_item * wf_slist_remove_first( + struct wf_slist * list) +{ + struct wf_slist_item * const result = list->first; + if (NULL != result) + { + list->first = list->first->next; + if (NULL == list->first) + { + list->last = NULL; + } + } + + return result; +} + +struct wf_slist_item * wf_slist_remove_after( + struct wf_slist * list, + struct wf_slist_item * prev) +{ + struct wf_slist_item * result = NULL; + + if (NULL != prev) + { + result = prev->next; + if ((NULL != result) && (NULL != result->next)) + { + prev->next = result->next; + } + else + { + list->last = prev; + prev->next = NULL; + } + + } + else + { + result = wf_slist_remove_first(list); + } + + return result; +} diff --git a/lib/webfuse/core/slist.h b/lib/webfuse/core/slist.h new file mode 100644 index 0000000..5073ff3 --- /dev/null +++ b/lib/webfuse/core/slist.h @@ -0,0 +1,45 @@ +#ifndef WF_SLIST_H +#define WF_SLIST_H + +#ifndef __cplusplus +#include +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_slist_item +{ + struct wf_slist_item * next; +}; + +struct wf_slist +{ + struct wf_slist_item * first; + struct wf_slist_item * last; +}; + +extern void wf_slist_init( + struct wf_slist * list); + +extern bool wf_slist_empty( + struct wf_slist * list); + +extern void wf_slist_append( + struct wf_slist * list, + struct wf_slist_item * item); + +extern struct wf_slist_item * wf_slist_remove_first( + struct wf_slist * list); + +extern struct wf_slist_item * wf_slist_remove_after( + struct wf_slist * list, + struct wf_slist_item * prev); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/test/test_dlist.cc b/test/test_dlist.cc deleted file mode 100644 index 031be1d..0000000 --- a/test/test_dlist.cc +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include "webfuse/core/dlist.h" -#include "webfuse/core/container_of.h" - -namespace -{ - struct Item - { - wf_dlist_item item; - char value; - }; -} - -TEST(DList, prepend) -{ - struct wf_dlist list; - wf_dlist_init(&list); - ASSERT_EQ(nullptr, list.first); - - Item a; - a.value = 'a'; - wf_dlist_prepend(&list, &a.item); - ASSERT_EQ(&a.item, list.first); - ASSERT_EQ(nullptr, a.item.next); - ASSERT_EQ(nullptr, a.item.prev); - - Item b; - b.value = 'b'; - wf_dlist_prepend(&list, &b.item); - ASSERT_EQ(&b.item, list.first); - ASSERT_EQ(&a.item, b.item.next); - ASSERT_EQ(nullptr, b.item.prev); - ASSERT_EQ(nullptr, a.item.next); - ASSERT_EQ(&b.item, a.item.prev); - - Item c; - c.value = 'c'; - wf_dlist_prepend(&list, &c.item); - ASSERT_EQ(&c.item, list.first); - ASSERT_EQ(&b.item, c.item.next); - ASSERT_EQ(nullptr, c.item.prev); - ASSERT_EQ(&a.item, b.item.next); - ASSERT_EQ(&c.item, b.item.prev); - ASSERT_EQ(nullptr, a.item.next); - ASSERT_EQ(&b.item, a.item.prev); - - wf_dlist_cleanup(&list, nullptr, nullptr); - ASSERT_EQ(nullptr, list.first); -} \ No newline at end of file diff --git a/test/test_slist.cc b/test/test_slist.cc new file mode 100644 index 0000000..c6995fa --- /dev/null +++ b/test/test_slist.cc @@ -0,0 +1,35 @@ +#include +#include "webfuse/core/slist.h" + +TEST(wf_slist_remove_after, RemoveFirst) +{ + struct wf_slist list; + struct wf_slist_item item[10]; + + wf_slist_init(&list); + wf_slist_append(&list, &item[0]); + + wf_slist_item * removed = wf_slist_remove_after(&list, NULL); + ASSERT_TRUE(wf_slist_empty(&list)); + ASSERT_EQ(nullptr, list.first); + ASSERT_EQ(nullptr, list.last); + ASSERT_EQ(&item[0], removed); +} + +TEST(wf_slist_remove_after, RemoveLast) +{ + struct wf_slist list; + struct wf_slist_item item[10]; + + wf_slist_init(&list); + wf_slist_append(&list, &item[0]); + wf_slist_append(&list, &item[1]); + wf_slist_append(&list, &item[2]); + + wf_slist_item * removed = wf_slist_remove_after(&list, &item[1]); + ASSERT_FALSE(wf_slist_empty(&list)); + ASSERT_EQ(&item[0], list.first); + ASSERT_EQ(&item[1], list.last); + ASSERT_EQ(nullptr, item[1].next); + ASSERT_EQ(&item[2], removed); +}