diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index ae67cc2..241d344 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -69,7 +69,7 @@ static void wf_impl_session_dispose_filesystems( while (NULL != item) { struct wf_slist_item * next = item->next; - struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(item, struct wf_impl_filesystem, item); + struct wf_impl_filesystem * filesystem = wf_container_of(item, struct wf_impl_filesystem, item); wf_impl_filesystem_dispose(filesystem); item = next; @@ -116,7 +116,7 @@ void wf_impl_session_onwritable( if (!wf_slist_empty(&session->messages)) { struct wf_slist_item * item = wf_slist_remove_first(&session->messages); - struct wf_message * message = WF_CONTAINER_OF(item, struct wf_message, item); + struct wf_message * message = wf_container_of(item, struct wf_message, item); lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); wf_message_dispose(message); @@ -160,7 +160,7 @@ static struct wf_impl_filesystem * wf_impl_session_get_filesystem( 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); + struct wf_impl_filesystem * filesystem = wf_container_of(session->filesystems.first, struct wf_impl_filesystem, item); if (wsi == filesystem->wsi) { result = filesystem; diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index e93b6c7..c092a48 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -16,7 +16,7 @@ void wf_impl_session_manager_cleanup( while (NULL != item) { struct wf_slist_item * next = item->next; - struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); + struct wf_impl_session * session = wf_container_of(item, struct wf_impl_session, item); wf_impl_session_dispose(session); item = next; @@ -51,7 +51,7 @@ struct wf_impl_session * wf_impl_session_manager_get( while (NULL != item) { struct wf_slist_item * next = item->next; - struct wf_impl_session * current = WF_CONTAINER_OF(item, struct wf_impl_session, item); + struct wf_impl_session * current = wf_container_of(item, struct wf_impl_session, item); if (wf_impl_session_contains_wsi(current, wsi)) { session = current; @@ -73,7 +73,7 @@ void wf_impl_session_manager_remove( while (NULL != item) { struct wf_slist_item * next = item->next; - struct wf_impl_session * session = WF_CONTAINER_OF(item, struct wf_impl_session, item); + 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); diff --git a/lib/webfuse/core/container_of.h b/lib/webfuse/core/container_of.h index fa51a26..d22bc34 100644 --- a/lib/webfuse/core/container_of.h +++ b/lib/webfuse/core/container_of.h @@ -7,7 +7,15 @@ #include #endif -#define WF_CONTAINER_OF(pointer, type, member) \ +#ifdef __GNUC__ +#define wf_container_of(pointer, type, member) \ + ({ \ + const typeof( ((type *)0)->member ) * __member = (pointer); \ + (type *)( (char *)__member - offsetof(type, member) ); \ + }) +#else +#define wf_container_of(pointer, type, member) \ (type *) (((char *) pointer) - offsetof(type, member)) +#endif #endif diff --git a/lib/webfuse/core/message_queue.c b/lib/webfuse/core/message_queue.c index 3853f44..b9eb715 100644 --- a/lib/webfuse/core/message_queue.c +++ b/lib/webfuse/core/message_queue.c @@ -9,7 +9,7 @@ void wf_message_queue_cleanup( while (NULL != item) { struct wf_slist_item * next = item->next; - struct wf_message * message = WF_CONTAINER_OF(item, struct wf_message, item); + struct wf_message * message = wf_container_of(item, struct wf_message, item); wf_message_dispose(message); item = next; } diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index a5dd360..788c314 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -101,7 +101,7 @@ static int wfp_impl_client_protocol_callback( if ((wsi == protocol->wsi) && (!wf_slist_empty(&protocol->messages))) { struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages); - struct wf_message * message = WF_CONTAINER_OF(item, struct wf_message, item); + struct wf_message * message = wf_container_of(item, struct wf_message, item); lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); wf_message_dispose(message); diff --git a/test/test_container_of.cc b/test/test_container_of.cc index 91a96ef..900e180 100644 --- a/test/test_container_of.cc +++ b/test/test_container_of.cc @@ -17,7 +17,7 @@ TEST(ContainerOf, FirstMember) MyStruct my_struct = {23, 42}; int * first = &my_struct.first; - ASSERT_EQ(&my_struct, WF_CONTAINER_OF(first, MyStruct, first)); + ASSERT_EQ(&my_struct, wf_container_of(first, MyStruct, first)); } TEST(ContainerOf, SecondMember) @@ -25,5 +25,5 @@ TEST(ContainerOf, SecondMember) MyStruct my_struct = {23, 42}; int * second = &my_struct.second; - ASSERT_EQ(&my_struct, WF_CONTAINER_OF(second, MyStruct, second)); + ASSERT_EQ(&my_struct, wf_container_of(second, MyStruct, second)); } \ No newline at end of file