From 0f36124ce5d9709b095361133fe0db4c3dc5a0d5 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 12 Jan 2024 15:12:01 +0100 Subject: [PATCH 01/34] Fix c99 compilation (#317) * fix: build tests only in c11 * fix: remove c11 macros from examples * feat: add a way to force c99 standard * ci: add a c99 compilation test --- .github/workflows/build-check.yaml | 12 ++++++++++++ CMakeLists.txt | 2 +- GNUmakefile | 8 ++++++++ examples/unix/c99/z_get.c | 5 +++-- examples/unix/c99/z_info.c | 2 +- examples/unix/c99/z_pub.c | 2 +- examples/unix/c99/z_pub_st.c | 2 +- examples/unix/c99/z_pull.c | 3 ++- examples/unix/c99/z_put.c | 2 +- examples/unix/c99/z_queryable.c | 5 +++-- examples/unix/c99/z_scout.c | 2 +- examples/unix/c99/z_sub.c | 5 +++-- examples/unix/c99/z_sub_st.c | 2 +- 13 files changed, 38 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index 5ba583b28..68f5d0862 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -42,6 +42,18 @@ jobs: - name: Run clang-format dry-run run: find include/ src/ tests/ examples/ -iname "*.ino" -o -iname "*.h" -o -iname "*.c" | xargs clang-format -n -Werror + c99_build: + name: Check c99 compilation + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build with C99 + run: | + sudo apt install -y ninja-build + FORCE_C99=ON CMAKE_GENERATOR=Ninja make + modular_build: name: Modular build on ubuntu-latest runs-on: ubuntu-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index c1e7bcf2c..e504c60e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,7 +304,7 @@ if(UNIX OR MSVC) target_link_libraries(z_keyexpr_canonizer ${Libname}) endif() - if(BUILD_TESTING) + if(BUILD_TESTING AND CMAKE_C_STANDARD MATCHES "11") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) diff --git a/GNUmakefile b/GNUmakefile index e8299b14c..eac55f32a 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -37,6 +37,10 @@ BUILD_INTEGRATION?=OFF # Accepted values: ON, OFF BUILD_TOOLS?=OFF +# Force the use of c99 standard. +# Accepted values: ON, OFF +FORCE_C99?=OFF + # Debug level. This sets the ZENOH_DEBUG variable. # Accepted values: # 0: NONE @@ -72,6 +76,10 @@ CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAK -DZ_FEATURE_PUBLICATION=$(Z_FEATURE_PUBLICATION) -DZ_FEATURE_SUBSCRIPTION=$(Z_FEATURE_SUBSCRIPTION) -DZ_FEATURE_QUERY=$(Z_FEATURE_QUERY) -DZ_FEATURE_QUERYABLE=$(Z_FEATURE_QUERYABLE)\ -DZ_FEATURE_RAWETH_TRANSPORT=$(Z_FEATURE_RAWETH_TRANSPORT) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. +ifeq ($(FORCE_C99), ON) + CMAKE_OPT += -DCMAKE_C_STANDARD=99 +endif + all: make $(BUILD_DIR)/Makefile: diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index e45bfe0fe..29f844879 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); @@ -105,7 +105,8 @@ int main(int argc, char **argv) { char c = '\0'; while (1) { fflush(stdin); - scanf("%c", &c); + int ret = scanf("%c", &c); + (void)ret; // Clear unused result warning if (c == 'q') { break; } diff --git a/examples/unix/c99/z_info.c b/examples/unix/c99/z_info.c index 901587bd8..60606c73a 100644 --- a/examples/unix/c99/z_info.c +++ b/examples/unix/c99/z_info.c @@ -62,7 +62,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index a03b0cc2c..f617c7741 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pub_st.c b/examples/unix/c99/z_pub_st.c index f62a1bd78..1bbaef106 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index b809dadff..10c16d462 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -83,7 +83,8 @@ int main(int argc, char **argv) { char c = '\0'; while (1) { fflush(stdin); - scanf("%c", &c); + int ret = scanf("%c", &c); + (void)ret; // Clear unused result warning if (c == 'q') { break; } diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 528db3785..82c259426 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index d7d543789..a7c2d28af 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -74,7 +74,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); @@ -108,7 +108,8 @@ int main(int argc, char **argv) { char c = '\0'; while (c != 'q') { fflush(stdin); - scanf("%c", &c); + int ret = scanf("%c", &c); + (void)ret; // Clear unused result warning } z_undeclare_queryable(z_queryable_move(&qable)); diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index 5b40a408e..e9453bf96 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -67,7 +67,7 @@ void callback(z_owned_hello_t *hello, void *context) { fprinthello(stdout, z_hello_loan(hello)); fprintf(stdout, "\n"); (*(int *)context)++; - z_drop(hello); + z_hello_drop(hello); } void drop(void *context) { diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index cc738e908..88c144889 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); @@ -96,7 +96,8 @@ int main(int argc, char **argv) { char c = '\0'; while (c != 'q') { fflush(stdin); - scanf("%c", &c); + int ret = scanf("%c", &c); + (void)ret; // Clear unused result warning } z_undeclare_subscriber(z_subscriber_move(&sub)); diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index ceda1495f..326f5fcbc 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) { zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } printf("Opening session...\n"); From 59a40a093e131fbf1c31c78221fdfce520e2e215 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 09:53:44 +0100 Subject: [PATCH 02/34] fix: set correct session pointer type --- include/zenoh-pico/net/query.h | 7 +++++-- include/zenoh-pico/net/session.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 0854907b7..96e579ef6 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -19,6 +19,9 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/protocol/core.h" +// Forward type declaration to avoid cyclical include +typedef struct _z_session_t _z_session_t; + /** * The query to be answered by a queryable. */ @@ -26,7 +29,7 @@ typedef struct { _z_value_t _value; _z_keyexpr_t _key; uint32_t _request_id; - void *_zn; // FIXME: _z_session_t *zn; + _z_session_t *_zn; char *_parameters; _Bool _anyke; } z_query_t; @@ -36,7 +39,7 @@ typedef struct { */ typedef struct { uint32_t _entity_id; - void *_zn; // FIXME: _z_session_t *zn; + _z_session_t *_zn; } _z_queryable_t; #if Z_FEATURE_QUERYABLE == 1 diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 111920337..5545ad5a0 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -27,7 +27,7 @@ /** * A zenoh-net session. */ -typedef struct { +typedef struct _z_session_t { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_t _mutex_inner; #endif // Z_FEATURE_MULTI_THREAD == 1 From ecbc147a02e59f06eacdec9fa50322e748e6307f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 10:40:17 +0100 Subject: [PATCH 03/34] refactor: rename sptr to rc --- .../collections/{pointer.h => refcount.h} | 130 +++++++++--------- include/zenoh-pico/protocol/core.h | 2 +- include/zenoh-pico/session/queryable.h | 6 +- include/zenoh-pico/session/session.h | 14 +- include/zenoh-pico/session/subscription.h | 6 +- src/api/api.c | 2 +- src/net/primitives.c | 10 +- src/session/queryable.c | 30 ++-- src/session/subscription.c | 32 ++--- 9 files changed, 116 insertions(+), 116 deletions(-) rename include/zenoh-pico/collections/{pointer.h => refcount.h} (72%) diff --git a/include/zenoh-pico/collections/pointer.h b/include/zenoh-pico/collections/refcount.h similarity index 72% rename from include/zenoh-pico/collections/pointer.h rename to include/zenoh-pico/collections/refcount.h index aa8e6c782..936f0f362 100644 --- a/include/zenoh-pico/collections/pointer.h +++ b/include/zenoh-pico/collections/refcount.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_COLLECTIONS_POINTER_H -#define ZENOH_PICO_COLLECTIONS_POINTER_H +#ifndef ZENOH_PICO_COLLECTIONS_REFCOUNT_H +#define ZENOH_PICO_COLLECTIONS_REFCOUNT_H #include #include @@ -41,13 +41,13 @@ #endif // __cplusplus /*------------------ Internal Array Macros ------------------*/ -#define _Z_POINTER_DEFINE(name, type) \ +#define _Z_REFCOUNT_DEFINE(name, type) \ typedef struct { \ type##_t *ptr; \ _z_atomic(unsigned int) * _cnt; \ - } name##_sptr_t; \ - static inline name##_sptr_t name##_sptr_new(type##_t val) { \ - name##_sptr_t p; \ + } name##_rc_t; \ + static inline name##_rc_t name##_rc_new(type##_t val) { \ + name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int) *)); \ @@ -60,15 +60,15 @@ } \ return p; \ } \ - static inline name##_sptr_t name##_sptr_clone(name##_sptr_t *p) { \ - name##_sptr_t c; \ + static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ + name##_rc_t c; \ c._cnt = p->_cnt; \ c.ptr = p->ptr; \ _z_atomic_fetch_add_explicit(p->_cnt, 1, _z_memory_order_relaxed); \ return c; \ } \ - static inline name##_sptr_t *name##_sptr_clone_as_ptr(name##_sptr_t *p) { \ - name##_sptr_t *c = (name##_sptr_t *)zp_malloc(sizeof(name##_sptr_t)); \ + static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ + name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ if (c != NULL) { \ c->_cnt = p->_cnt; \ c->ptr = p->ptr; \ @@ -76,10 +76,10 @@ } \ return c; \ } \ - static inline _Bool name##_sptr_eq(const name##_sptr_t *left, const name##_sptr_t *right) { \ + static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ return (left->ptr == right->ptr); \ } \ - static inline _Bool name##_sptr_drop(name##_sptr_t *p) { \ + static inline _Bool name##_rc_drop(name##_rc_t *p) { \ _Bool dropped = false; \ if (p->_cnt != NULL) { \ unsigned int c = _z_atomic_fetch_sub_explicit(p->_cnt, 1, _z_memory_order_release); \ @@ -97,59 +97,59 @@ } #else /*------------------ Internal Array Macros ------------------*/ -#define _Z_POINTER_DEFINE(name, type) \ - typedef struct { \ - type##_t *ptr; \ - volatile uint8_t *_cnt; \ - } name##_sptr_t; \ - static inline name##_sptr_t name##_sptr_new(type##_t val) { \ - name##_sptr_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (uint8_t *)zp_malloc(sizeof(uint8_t)); \ - if (p._cnt != NULL) { \ - *p.ptr = val; \ - *p._cnt = 1; \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_sptr_t name##_sptr_clone(name##_sptr_t *p) { \ - name##_sptr_t c; \ - c._cnt = p->_cnt; \ - c.ptr = p->ptr; \ - *p->_cnt = *p->_cnt + (uint8_t)1; \ - return c; \ - } \ - static inline name##_sptr_t *name##_sptr_clone_as_ptr(name##_sptr_t *p) { \ - name##_sptr_t *c = (name##_sptr_t *)zp_malloc(sizeof(name##_sptr_t)); \ - if (c != NULL) { \ - c->_cnt = p->_cnt; \ - c->ptr = p->ptr; \ - *p->_cnt = *p->_cnt + (uint8_t)1; \ - } \ - return c; \ - } \ - static inline _Bool name##_sptr_eq(const name##_sptr_t *left, const name##_sptr_t *right) { \ - return (left->ptr == right->ptr); \ - } \ - static inline _Bool name##_sptr_drop(name##_sptr_t *p) { \ - _Bool dropped = true; \ - if (p->_cnt != NULL) { \ - *p->_cnt = *p->_cnt - 1; \ - dropped = *p->_cnt == 0; \ - if (dropped == true) { \ - if (p->ptr != NULL) { \ - type##_clear(p->ptr); \ - zp_free(p->ptr); \ - zp_free((void *)p->_cnt); \ - } \ - } \ - } \ - return dropped; \ +#define _Z_REFCOUNT_DEFINE(name, type) \ + typedef struct { \ + type##_t *ptr; \ + volatile uint8_t *_cnt; \ + } name##_rc_t; \ + static inline name##_rc_t name##_rc_new(type##_t val) { \ + name##_rc_t p; \ + p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + if (p.ptr != NULL) { \ + p._cnt = (uint8_t *)zp_malloc(sizeof(uint8_t)); \ + if (p._cnt != NULL) { \ + *p.ptr = val; \ + *p._cnt = 1; \ + } else { \ + zp_free(p.ptr); \ + } \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ + name##_rc_t c; \ + c._cnt = p->_cnt; \ + c.ptr = p->ptr; \ + *p->_cnt = *p->_cnt + (uint8_t)1; \ + return c; \ + } \ + static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ + name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ + if (c != NULL) { \ + c->_cnt = p->_cnt; \ + c->ptr = p->ptr; \ + *p->_cnt = *p->_cnt + (uint8_t)1; \ + } \ + return c; \ + } \ + static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ + return (left->ptr == right->ptr); \ + } \ + static inline _Bool name##_rc_drop(name##_rc_t *p) { \ + _Bool dropped = true; \ + if (p->_cnt != NULL) { \ + *p->_cnt = *p->_cnt - 1; \ + dropped = *p->_cnt == 0; \ + if (dropped == true) { \ + if (p->ptr != NULL) { \ + type##_clear(p->ptr); \ + zp_free(p->ptr); \ + zp_free((void *)p->_cnt); \ + } \ + } \ + } \ + return dropped; \ } #endif // ZENOH_C_STANDARD != 99 -#endif /* ZENOH_PICO_COLLECTIONS_POINTER_H */ +#endif /* ZENOH_PICO_COLLECTIONS_REFCOUNT_H */ diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index a60a12dc6..bb7285854 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -287,7 +287,7 @@ typedef struct { /** * Informations to be passed to :c:func:`_z_declare_subscriber` to configure the created - * :c:type:`_z_subscription_sptr_t`. + * :c:type:`_z_subscription_rc_t`. * * Members: * _z_period_t *period: The subscription period. diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index 95e8cb08a..a4e856b95 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -24,12 +24,12 @@ #define _Z_QUERYABLE_DISTANCE_DEFAULT 0 /*------------------ Queryable ------------------*/ -_z_questionable_sptr_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id); +_z_questionable_rc_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id); _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key); -_z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q); +_z_questionable_rc_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q); int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid); -void _z_unregister_questionable(_z_session_t *zn, _z_questionable_sptr_t *q); +void _z_unregister_questionable(_z_session_t *zn, _z_questionable_rc_t *q); void _z_flush_questionables(_z_session_t *zn); #endif diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 8c11826a6..adbb59085 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -20,7 +20,7 @@ #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/list.h" -#include "zenoh-pico/collections/pointer.h" +#include "zenoh-pico/collections/refcount.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/query.h" @@ -101,10 +101,10 @@ typedef struct { _Bool _z_subscription_eq(const _z_subscription_t *one, const _z_subscription_t *two); void _z_subscription_clear(_z_subscription_t *sub); -_Z_POINTER_DEFINE(_z_subscription, _z_subscription) +_Z_REFCOUNT_DEFINE(_z_subscription, _z_subscription) _Z_ELEM_DEFINE(_z_subscriber, _z_subscription_t, _z_noop_size, _z_subscription_clear, _z_noop_copy) -_Z_ELEM_DEFINE(_z_subscription_sptr, _z_subscription_sptr_t, _z_noop_size, _z_subscription_sptr_drop, _z_noop_copy) -_Z_LIST_DEFINE(_z_subscription_sptr, _z_subscription_sptr_t) +_Z_ELEM_DEFINE(_z_subscription_sptr, _z_subscription_rc_t, _z_noop_size, _z_subscription_rc_drop, _z_noop_copy) +_Z_LIST_DEFINE(_z_subscription_sptr, _z_subscription_rc_t) typedef struct { _z_keyexpr_t _key; @@ -128,10 +128,10 @@ typedef struct { _Bool _z_questionable_eq(const _z_questionable_t *one, const _z_questionable_t *two); void _z_questionable_clear(_z_questionable_t *res); -_Z_POINTER_DEFINE(_z_questionable, _z_questionable) +_Z_REFCOUNT_DEFINE(_z_questionable, _z_questionable) _Z_ELEM_DEFINE(_z_questionable, _z_questionable_t, _z_noop_size, _z_questionable_clear, _z_noop_copy) -_Z_ELEM_DEFINE(_z_questionable_sptr, _z_questionable_sptr_t, _z_noop_size, _z_questionable_sptr_drop, _z_noop_copy) -_Z_LIST_DEFINE(_z_questionable_sptr, _z_questionable_sptr_t) +_Z_ELEM_DEFINE(_z_questionable_sptr, _z_questionable_rc_t, _z_noop_size, _z_questionable_rc_drop, _z_noop_copy) +_Z_LIST_DEFINE(_z_questionable_sptr, _z_questionable_rc_t) typedef struct { _z_reply_t _reply; diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index b6fcb2caa..53bde0216 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -19,11 +19,11 @@ #if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscription ------------------*/ -_z_subscription_sptr_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); +_z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); _z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); -_z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); +_z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, _z_zint_t payload_len #if Z_FEATURE_ATTACHMENT == 1 @@ -38,7 +38,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co z_attachment_t att #endif ); -void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_sptr_t *sub); +void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub); void _z_flush_subscriptions(_z_session_t *zn); /*------------------ Pull ------------------*/ diff --git a/src/api/api.c b/src/api/api.c index 432fd0855..530f90883 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1056,7 +1056,7 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { if (sub._val != NULL) { _z_subscription_sptr_list_t *tail = sub._val->_zn->_local_subscriptions; while (tail != NULL && !z_keyexpr_check(&ret)) { - _z_subscription_sptr_t *head = _z_subscription_sptr_list_head(tail); + _z_subscription_rc_t *head = _z_subscription_sptr_list_head(tail); if (head->ptr->_id == lookup) { _z_keyexpr_t key = _z_keyexpr_duplicate(head->ptr->_key); ret = (z_owned_keyexpr_t){._value = zp_malloc(sizeof(_z_keyexpr_t))}; diff --git a/src/net/primitives.c b/src/net/primitives.c index 13d51e0a5..b1eccb23d 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -202,7 +202,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ ret->_zn = zn; ret->_entity_id = s._id; - _z_subscription_sptr_t *sp_s = _z_register_subscription( + _z_subscription_rc_t *sp_s = _z_register_subscription( zn, _Z_RESOURCE_IS_LOCAL, &s); // This a pointer to the entry stored at session-level. // Do not drop it by the end of this function. if (sp_s != NULL) { @@ -229,7 +229,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t ret = _Z_ERR_GENERIC; if (sub != NULL) { - _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); @@ -256,7 +256,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { int8_t ret = _Z_RES_OK; - _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { _z_zint_t pull_id = _z_get_pull_id(sub->_zn); _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); @@ -288,7 +288,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo ret->_zn = zn; ret->_entity_id = q._id; - _z_questionable_sptr_t *sp_q = + _z_questionable_rc_t *sp_q = _z_register_questionable(zn, &q); // This a pointer to the entry stored at session-level. // Do not drop it by the end of this function. if (sp_q != NULL) { @@ -316,7 +316,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { int8_t ret = _Z_RES_OK; if (qle != NULL) { - _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_entity_id); + _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn, qle->_entity_id); if (q != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); diff --git a/src/session/queryable.c b/src/session/queryable.c index e2550c593..66685fdc0 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -35,12 +35,12 @@ void _z_questionable_clear(_z_questionable_t *qle) { } /*------------------ Queryable ------------------*/ -_z_questionable_sptr_t *__z_get_questionable_by_id(_z_questionable_sptr_list_t *qles, const _z_zint_t id) { - _z_questionable_sptr_t *ret = NULL; +_z_questionable_rc_t *__z_get_questionable_by_id(_z_questionable_sptr_list_t *qles, const _z_zint_t id) { + _z_questionable_rc_t *ret = NULL; _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { - _z_questionable_sptr_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); if (id == qle->ptr->_id) { ret = qle; break; @@ -57,10 +57,10 @@ _z_questionable_sptr_list_t *__z_get_questionable_by_key(_z_questionable_sptr_li _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { - _z_questionable_sptr_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); if (_z_keyexpr_intersects(qle->ptr->_key._suffix, strlen(qle->ptr->_key._suffix), key._suffix, strlen(key._suffix)) == true) { - ret = _z_questionable_sptr_list_push(ret, _z_questionable_sptr_clone_as_ptr(qle)); + ret = _z_questionable_sptr_list_push(ret, _z_questionable_rc_clone_as_ptr(qle)); } xs = _z_questionable_sptr_list_tail(xs); @@ -74,7 +74,7 @@ _z_questionable_sptr_list_t *__z_get_questionable_by_key(_z_questionable_sptr_li * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_questionable_sptr_t *__unsafe_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { +_z_questionable_rc_t *__unsafe_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { _z_questionable_sptr_list_t *qles = zn->_local_questionable; return __z_get_questionable_by_id(qles, id); } @@ -89,12 +89,12 @@ _z_questionable_sptr_list_t *__unsafe_z_get_questionable_by_key(_z_session_t *zn return __z_get_questionable_by_key(qles, key); } -_z_questionable_sptr_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { +_z_questionable_rc_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_questionable_sptr_t *qle = __unsafe_z_get_questionable_by_id(zn, id); + _z_questionable_rc_t *qle = __unsafe_z_get_questionable_by_id(zn, id); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -118,17 +118,17 @@ _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const return qles; } -_z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q) { +_z_questionable_rc_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q) { _Z_DEBUG(">>> Allocating queryable for (%ju:%s)", (uintmax_t)q->_key._id, q->_key._suffix); - _z_questionable_sptr_t *ret = NULL; + _z_questionable_rc_t *ret = NULL; #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - ret = (_z_questionable_sptr_t *)zp_malloc(sizeof(_z_questionable_sptr_t)); + ret = (_z_questionable_rc_t *)zp_malloc(sizeof(_z_questionable_rc_t)); if (ret != NULL) { - *ret = _z_questionable_sptr_new(*q); + *ret = _z_questionable_rc_new(*q); zn->_local_questionable = _z_questionable_sptr_list_push(zn->_local_questionable, ret); } @@ -172,7 +172,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { - _z_questionable_sptr_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); qle->ptr->_callback(&q, qle->ptr->_arg); xs = _z_questionable_sptr_list_tail(xs); } @@ -201,13 +201,13 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons return ret; } -void _z_unregister_questionable(_z_session_t *zn, _z_questionable_sptr_t *qle) { +void _z_unregister_questionable(_z_session_t *zn, _z_questionable_rc_t *qle) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 zn->_local_questionable = - _z_questionable_sptr_list_drop_filter(zn->_local_questionable, _z_questionable_sptr_eq, qle); + _z_questionable_sptr_list_drop_filter(zn->_local_questionable, _z_questionable_rc_eq, qle); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); diff --git a/src/session/subscription.c b/src/session/subscription.c index 435f2d682..c0eec6697 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -39,12 +39,12 @@ void _z_subscription_clear(_z_subscription_t *sub) { /*------------------ Pull ------------------*/ _z_zint_t _z_get_pull_id(_z_session_t *zn) { return zn->_pull_id++; } -_z_subscription_sptr_t *__z_get_subscription_by_id(_z_subscription_sptr_list_t *subs, const _z_zint_t id) { - _z_subscription_sptr_t *ret = NULL; +_z_subscription_rc_t *__z_get_subscription_by_id(_z_subscription_sptr_list_t *subs, const _z_zint_t id) { + _z_subscription_rc_t *ret = NULL; _z_subscription_sptr_list_t *xs = subs; while (xs != NULL) { - _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); if (id == sub->ptr->_id) { ret = sub; break; @@ -61,10 +61,10 @@ _z_subscription_sptr_list_t *__z_get_subscriptions_by_key(_z_subscription_sptr_l _z_subscription_sptr_list_t *xs = subs; while (xs != NULL) { - _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); if (_z_keyexpr_intersects(sub->ptr->_key._suffix, strlen(sub->ptr->_key._suffix), key._suffix, strlen(key._suffix)) == true) { - ret = _z_subscription_sptr_list_push(ret, _z_subscription_sptr_clone_as_ptr(sub)); + ret = _z_subscription_sptr_list_push(ret, _z_subscription_rc_clone_as_ptr(sub)); } xs = _z_subscription_sptr_list_tail(xs); @@ -78,7 +78,7 @@ _z_subscription_sptr_list_t *__z_get_subscriptions_by_key(_z_subscription_sptr_l * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_subscription_sptr_t *__unsafe_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { +_z_subscription_rc_t *__unsafe_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { _z_subscription_sptr_list_t *subs = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_subscriptions : zn->_remote_subscriptions; return __z_get_subscription_by_id(subs, id); @@ -96,12 +96,12 @@ _z_subscription_sptr_list_t *__unsafe_z_get_subscriptions_by_key(_z_session_t *z return __z_get_subscriptions_by_key(subs, key); } -_z_subscription_sptr_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { +_z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_subscription_sptr_t *sub = __unsafe_z_get_subscription_by_id(zn, is_local, id); + _z_subscription_rc_t *sub = __unsafe_z_get_subscription_by_id(zn, is_local, id); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -124,9 +124,9 @@ _z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8 return subs; } -_z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *s) { +_z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *s) { _Z_DEBUG(">>> Allocating sub decl for (%ju:%s)", (uintmax_t)s->_key._id, s->_key._suffix); - _z_subscription_sptr_t *ret = NULL; + _z_subscription_rc_t *ret = NULL; #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); @@ -134,9 +134,9 @@ _z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_lo _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, s->_key); if (subs == NULL) { // A subscription for this name does not yet exists - ret = (_z_subscription_sptr_t *)zp_malloc(sizeof(_z_subscription_sptr_t)); + ret = (_z_subscription_rc_t *)zp_malloc(sizeof(_z_subscription_rc_t)); if (ret != NULL) { - *ret = _z_subscription_sptr_new(*s); + *ret = _z_subscription_rc_new(*s); if (is_local == _Z_RESOURCE_IS_LOCAL) { zn->_local_subscriptions = _z_subscription_sptr_list_push(zn->_local_subscriptions, ret); } else { @@ -206,7 +206,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_subscription_sptr_list_t *xs = subs; _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_sptr_list_len(xs)); while (xs != NULL) { - _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); sub->ptr->_callback(&s, sub->ptr->_arg); xs = _z_subscription_sptr_list_tail(xs); } @@ -223,17 +223,17 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co return ret; } -void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_sptr_t *sub) { +void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 if (is_local == _Z_RESOURCE_IS_LOCAL) { zn->_local_subscriptions = - _z_subscription_sptr_list_drop_filter(zn->_local_subscriptions, _z_subscription_sptr_eq, sub); + _z_subscription_sptr_list_drop_filter(zn->_local_subscriptions, _z_subscription_rc_eq, sub); } else { zn->_remote_subscriptions = - _z_subscription_sptr_list_drop_filter(zn->_remote_subscriptions, _z_subscription_sptr_eq, sub); + _z_subscription_sptr_list_drop_filter(zn->_remote_subscriptions, _z_subscription_rc_eq, sub); } #if Z_FEATURE_MULTI_THREAD == 1 From e1de7d465b0ae0ee2857287f898e35c99f6b39af Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 11:11:13 +0100 Subject: [PATCH 04/34] feat: add compiler based definition --- CMakeLists.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 04a5ecfee..c01b7efa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,7 @@ string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) set(CHECK_THREADS "ON") +# System definition if(CMAKE_SYSTEM_NAME MATCHES "Linux") add_definition(ZENOH_LINUX) elseif(POSIX_COMPATIBLE) @@ -101,6 +102,19 @@ else() return() endif() +# Compiler definition +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") + add_definition(ZENOH_COMPILER_CLANG) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") + add_definition(ZENOH_COMPILER_GCC) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_C_COMPILER_ID STREQUAL "Intel") + add_definition(ZENOH_COMPILER_INTEL) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "MSVC") + add_definition(ZENOH_COMPILER_MSVC) +else() + add_definition(ZENOH_COMPILER_OTHER) +endif() + add_definition(ZENOH_DEBUG=${ZENOH_DEBUG}) if(FRAG_MAX_SIZE) From 790d6666e8e1159c12ed2edeb2860dda4805ad33 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 11:28:54 +0100 Subject: [PATCH 05/34] fix: allocate int instead of int pointer --- include/zenoh-pico/collections/refcount.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 936f0f362..657ecb5a0 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -50,7 +50,7 @@ name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ - p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int) *)); \ + p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int))); \ if (p._cnt != NULL) { \ *p.ptr = val; \ _z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \ From 42f81366bc18aa8e1f11129572c837c10638155c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 11:31:44 +0100 Subject: [PATCH 06/34] fix: align implementation counter size --- include/zenoh-pico/collections/refcount.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 657ecb5a0..346a8d8e5 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -100,13 +100,13 @@ #define _Z_REFCOUNT_DEFINE(name, type) \ typedef struct { \ type##_t *ptr; \ - volatile uint8_t *_cnt; \ + volatile unsigned int *_cnt; \ } name##_rc_t; \ static inline name##_rc_t name##_rc_new(type##_t val) { \ name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ - p._cnt = (uint8_t *)zp_malloc(sizeof(uint8_t)); \ + p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ if (p._cnt != NULL) { \ *p.ptr = val; \ *p._cnt = 1; \ @@ -120,7 +120,7 @@ name##_rc_t c; \ c._cnt = p->_cnt; \ c.ptr = p->ptr; \ - *p->_cnt = *p->_cnt + (uint8_t)1; \ + *p->_cnt = *p->_cnt + 1; \ return c; \ } \ static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ @@ -128,7 +128,7 @@ if (c != NULL) { \ c->_cnt = p->_cnt; \ c->ptr = p->ptr; \ - *p->_cnt = *p->_cnt + (uint8_t)1; \ + *p->_cnt = *p->_cnt + 1; \ } \ return c; \ } \ From 3281c9096935e8610427229d682de39a3b592904 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 11:34:46 +0100 Subject: [PATCH 07/34] fix: require single thread config to use single thread implem --- include/zenoh-pico/collections/refcount.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 346a8d8e5..c40a414aa 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -18,6 +18,7 @@ #include #include +#if Z_FEATURE_MULTI_THREAD == 1 #if ZENOH_C_STANDARD != 99 #ifndef __cplusplus @@ -95,7 +96,10 @@ } \ return dropped; \ } -#else +#else // ZENOH_C_STANDARD == 99 +#error "Multi-thread refcount in C99 only exists for GCC, use GCC or C11 or deactivate multi-thread" +#endif // ZENOH_C_STANDARD != 99 +#else // Z_FEATURE_MULTI_THREAD == 0 /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ typedef struct { \ @@ -150,6 +154,6 @@ } \ return dropped; \ } -#endif // ZENOH_C_STANDARD != 99 +#endif // Z_FEATURE_MULTI_THREAD == 1 #endif /* ZENOH_PICO_COLLECTIONS_REFCOUNT_H */ From 0d8472490e966f1a6ae88318656c632c80c79106 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 11:35:04 +0100 Subject: [PATCH 08/34] feat: add c99 gcc rc implementation --- include/zenoh-pico/collections/refcount.h | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index c40a414aa..cb1d99897 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -97,7 +97,66 @@ return dropped; \ } #else // ZENOH_C_STANDARD == 99 +#ifdef ZENOH_COMPILER_GCC +/*------------------ Internal Array Macros ------------------*/ +#define _Z_REFCOUNT_DEFINE(name, type) \ + typedef struct { \ + type##_t *ptr; \ + unsigned int *_cnt; \ + } name##_rc_t; \ + static inline name##_rc_t name##_rc_new(type##_t val) { \ + name##_rc_t p; \ + p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + if (p.ptr != NULL) { \ + p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ + if (p._cnt != NULL) { \ + *p.ptr = val; \ + __sync_fetch_and_and(p._cnt, 0); \ + __sync_fetch_and_add(p._cnt, 1); \ + } else { \ + zp_free(p.ptr); \ + } \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ + name##_rc_t c; \ + c._cnt = p->_cnt; \ + c.ptr = p->ptr; \ + __sync_fetch_and_add(p->_cnt, 1); \ + return c; \ + } \ + static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ + name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ + if (c != NULL) { \ + c->_cnt = p->_cnt; \ + c->ptr = p->ptr; \ + __sync_fetch_and_add(p->_cnt, 1); \ + } \ + return c; \ + } \ + static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ + return (left->ptr == right->ptr); \ + } \ + static inline _Bool name##_rc_drop(name##_rc_t *p) { \ + _Bool dropped = false; \ + if (p->_cnt != NULL) { \ + unsigned int c = __sync_fetch_and_sub(p->_cnt, 1); \ + dropped = c == 1; \ + if (dropped == true) { \ + __sync_synchronize(); \ + if (p->ptr != NULL) { \ + type##_clear(p->ptr); \ + zp_free(p->ptr); \ + zp_free((void *)p->_cnt); \ + } \ + } \ + } \ + return dropped; \ + } +#else // !ZENOH_COMPILER_GCC #error "Multi-thread refcount in C99 only exists for GCC, use GCC or C11 or deactivate multi-thread" +#endif // ZENOH_COMPILER_GCC #endif // ZENOH_C_STANDARD != 99 #else // Z_FEATURE_MULTI_THREAD == 0 /*------------------ Internal Array Macros ------------------*/ From fa0bcfb491a23b95fa6d6dc78d1dd693f090b0bf Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 15:38:48 +0100 Subject: [PATCH 09/34] refactor: rename derived sptr types to rc --- include/zenoh-pico/net/session.h | 6 +-- include/zenoh-pico/session/queryable.h | 2 +- include/zenoh-pico/session/session.h | 8 ++-- include/zenoh-pico/session/subscription.h | 2 +- src/api/api.c | 6 +-- src/session/queryable.c | 46 +++++++++---------- src/session/subscription.c | 56 +++++++++++------------ 7 files changed, 63 insertions(+), 63 deletions(-) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 5545ad5a0..e995c3b9b 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -51,13 +51,13 @@ typedef struct _z_session_t { // Session subscriptions #if Z_FEATURE_SUBSCRIPTION == 1 - _z_subscription_sptr_list_t *_local_subscriptions; - _z_subscription_sptr_list_t *_remote_subscriptions; + _z_subscription_rc_list_t *_local_subscriptions; + _z_subscription_rc_list_t *_remote_subscriptions; #endif // Session queryables #if Z_FEATURE_QUERYABLE == 1 - _z_questionable_sptr_list_t *_local_questionable; + _z_questionable_rc_list_t *_local_questionable; #endif #if Z_FEATURE_QUERY == 1 _z_pending_query_list_t *_pending_queries; diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index a4e856b95..d756278b3 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -25,7 +25,7 @@ /*------------------ Queryable ------------------*/ _z_questionable_rc_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id); -_z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key); +_z_questionable_rc_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key); _z_questionable_rc_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q); int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index adbb59085..91cbb267b 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -103,8 +103,8 @@ void _z_subscription_clear(_z_subscription_t *sub); _Z_REFCOUNT_DEFINE(_z_subscription, _z_subscription) _Z_ELEM_DEFINE(_z_subscriber, _z_subscription_t, _z_noop_size, _z_subscription_clear, _z_noop_copy) -_Z_ELEM_DEFINE(_z_subscription_sptr, _z_subscription_rc_t, _z_noop_size, _z_subscription_rc_drop, _z_noop_copy) -_Z_LIST_DEFINE(_z_subscription_sptr, _z_subscription_rc_t) +_Z_ELEM_DEFINE(_z_subscription_rc, _z_subscription_rc_t, _z_noop_size, _z_subscription_rc_drop, _z_noop_copy) +_Z_LIST_DEFINE(_z_subscription_rc, _z_subscription_rc_t) typedef struct { _z_keyexpr_t _key; @@ -130,8 +130,8 @@ void _z_questionable_clear(_z_questionable_t *res); _Z_REFCOUNT_DEFINE(_z_questionable, _z_questionable) _Z_ELEM_DEFINE(_z_questionable, _z_questionable_t, _z_noop_size, _z_questionable_clear, _z_noop_copy) -_Z_ELEM_DEFINE(_z_questionable_sptr, _z_questionable_rc_t, _z_noop_size, _z_questionable_rc_drop, _z_noop_copy) -_Z_LIST_DEFINE(_z_questionable_sptr, _z_questionable_rc_t) +_Z_ELEM_DEFINE(_z_questionable_rc, _z_questionable_rc_t, _z_noop_size, _z_questionable_rc_drop, _z_noop_copy) +_Z_LIST_DEFINE(_z_questionable_rc, _z_questionable_rc_t) typedef struct { _z_reply_t _reply; diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 53bde0216..3c5c6a3ed 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -20,7 +20,7 @@ #if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscription ------------------*/ _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); -_z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, +_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); diff --git a/src/api/api.c b/src/api/api.c index 530f90883..ee4898591 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1054,9 +1054,9 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { z_owned_keyexpr_t ret = z_keyexpr_null(); uint32_t lookup = sub._val->_entity_id; if (sub._val != NULL) { - _z_subscription_sptr_list_t *tail = sub._val->_zn->_local_subscriptions; + _z_subscription_rc_list_t *tail = sub._val->_zn->_local_subscriptions; while (tail != NULL && !z_keyexpr_check(&ret)) { - _z_subscription_rc_t *head = _z_subscription_sptr_list_head(tail); + _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); if (head->ptr->_id == lookup) { _z_keyexpr_t key = _z_keyexpr_duplicate(head->ptr->_key); ret = (z_owned_keyexpr_t){._value = zp_malloc(sizeof(_z_keyexpr_t))}; @@ -1066,7 +1066,7 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { _z_keyexpr_clear(&key); } } - tail = _z_subscription_sptr_list_tail(tail); + tail = _z_subscription_rc_list_tail(tail); } } return ret; diff --git a/src/session/queryable.c b/src/session/queryable.c index 66685fdc0..5910b7de1 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -35,35 +35,35 @@ void _z_questionable_clear(_z_questionable_t *qle) { } /*------------------ Queryable ------------------*/ -_z_questionable_rc_t *__z_get_questionable_by_id(_z_questionable_sptr_list_t *qles, const _z_zint_t id) { +_z_questionable_rc_t *__z_get_questionable_by_id(_z_questionable_rc_list_t *qles, const _z_zint_t id) { _z_questionable_rc_t *ret = NULL; - _z_questionable_sptr_list_t *xs = qles; + _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { - _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); if (id == qle->ptr->_id) { ret = qle; break; } - xs = _z_questionable_sptr_list_tail(xs); + xs = _z_questionable_rc_list_tail(xs); } return ret; } -_z_questionable_sptr_list_t *__z_get_questionable_by_key(_z_questionable_sptr_list_t *qles, const _z_keyexpr_t key) { - _z_questionable_sptr_list_t *ret = NULL; +_z_questionable_rc_list_t *__z_get_questionable_by_key(_z_questionable_rc_list_t *qles, const _z_keyexpr_t key) { + _z_questionable_rc_list_t *ret = NULL; - _z_questionable_sptr_list_t *xs = qles; + _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { - _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); if (_z_keyexpr_intersects(qle->ptr->_key._suffix, strlen(qle->ptr->_key._suffix), key._suffix, strlen(key._suffix)) == true) { - ret = _z_questionable_sptr_list_push(ret, _z_questionable_rc_clone_as_ptr(qle)); + ret = _z_questionable_rc_list_push(ret, _z_questionable_rc_clone_as_ptr(qle)); } - xs = _z_questionable_sptr_list_tail(xs); + xs = _z_questionable_rc_list_tail(xs); } return ret; @@ -75,7 +75,7 @@ _z_questionable_sptr_list_t *__z_get_questionable_by_key(_z_questionable_sptr_li * - zn->_mutex_inner */ _z_questionable_rc_t *__unsafe_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { - _z_questionable_sptr_list_t *qles = zn->_local_questionable; + _z_questionable_rc_list_t *qles = zn->_local_questionable; return __z_get_questionable_by_id(qles, id); } @@ -84,8 +84,8 @@ _z_questionable_rc_t *__unsafe_z_get_questionable_by_id(_z_session_t *zn, const * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_questionable_sptr_list_t *__unsafe_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key) { - _z_questionable_sptr_list_t *qles = zn->_local_questionable; +_z_questionable_rc_list_t *__unsafe_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key) { + _z_questionable_rc_list_t *qles = zn->_local_questionable; return __z_get_questionable_by_key(qles, key); } @@ -103,13 +103,13 @@ _z_questionable_rc_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_ return qle; } -_z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { +_z_questionable_rc_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); - _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); + _z_questionable_rc_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -129,7 +129,7 @@ _z_questionable_rc_t *_z_register_questionable(_z_session_t *zn, _z_questionable ret = (_z_questionable_rc_t *)zp_malloc(sizeof(_z_questionable_rc_t)); if (ret != NULL) { *ret = _z_questionable_rc_new(*q); - zn->_local_questionable = _z_questionable_sptr_list_push(zn->_local_questionable, ret); + zn->_local_questionable = _z_questionable_rc_list_push(zn->_local_questionable, ret); } #if Z_FEATURE_MULTI_THREAD == 1 @@ -148,7 +148,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &q_key); if (key._suffix != NULL) { - _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); + _z_questionable_rc_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -170,15 +170,15 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons q._value.encoding = query->_ext_value.encoding; q._value.payload = query->_ext_value.payload; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; - _z_questionable_sptr_list_t *xs = qles; + _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { - _z_questionable_rc_t *qle = _z_questionable_sptr_list_head(xs); + _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); qle->ptr->_callback(&q, qle->ptr->_arg); - xs = _z_questionable_sptr_list_tail(xs); + xs = _z_questionable_rc_list_tail(xs); } _z_keyexpr_clear(&key); - _z_questionable_sptr_list_free(&qles); + _z_questionable_rc_list_free(&qles); #if defined(__STDC_NO_VLA__) || ((__STDC_VERSION__ < 201000L) && (defined(_WIN32) || defined(WIN32))) zp_free(params); #endif @@ -207,7 +207,7 @@ void _z_unregister_questionable(_z_session_t *zn, _z_questionable_rc_t *qle) { #endif // Z_FEATURE_MULTI_THREAD == 1 zn->_local_questionable = - _z_questionable_sptr_list_drop_filter(zn->_local_questionable, _z_questionable_rc_eq, qle); + _z_questionable_rc_list_drop_filter(zn->_local_questionable, _z_questionable_rc_eq, qle); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -219,7 +219,7 @@ void _z_flush_questionables(_z_session_t *zn) { zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_questionable_sptr_list_free(&zn->_local_questionable); + _z_questionable_rc_list_free(&zn->_local_questionable); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); diff --git a/src/session/subscription.c b/src/session/subscription.c index c0eec6697..5124160c3 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -39,35 +39,35 @@ void _z_subscription_clear(_z_subscription_t *sub) { /*------------------ Pull ------------------*/ _z_zint_t _z_get_pull_id(_z_session_t *zn) { return zn->_pull_id++; } -_z_subscription_rc_t *__z_get_subscription_by_id(_z_subscription_sptr_list_t *subs, const _z_zint_t id) { +_z_subscription_rc_t *__z_get_subscription_by_id(_z_subscription_rc_list_t *subs, const _z_zint_t id) { _z_subscription_rc_t *ret = NULL; - _z_subscription_sptr_list_t *xs = subs; + _z_subscription_rc_list_t *xs = subs; while (xs != NULL) { - _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); if (id == sub->ptr->_id) { ret = sub; break; } - xs = _z_subscription_sptr_list_tail(xs); + xs = _z_subscription_rc_list_tail(xs); } return ret; } -_z_subscription_sptr_list_t *__z_get_subscriptions_by_key(_z_subscription_sptr_list_t *subs, const _z_keyexpr_t key) { - _z_subscription_sptr_list_t *ret = NULL; +_z_subscription_rc_list_t *__z_get_subscriptions_by_key(_z_subscription_rc_list_t *subs, const _z_keyexpr_t key) { + _z_subscription_rc_list_t *ret = NULL; - _z_subscription_sptr_list_t *xs = subs; + _z_subscription_rc_list_t *xs = subs; while (xs != NULL) { - _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); if (_z_keyexpr_intersects(sub->ptr->_key._suffix, strlen(sub->ptr->_key._suffix), key._suffix, strlen(key._suffix)) == true) { - ret = _z_subscription_sptr_list_push(ret, _z_subscription_rc_clone_as_ptr(sub)); + ret = _z_subscription_rc_list_push(ret, _z_subscription_rc_clone_as_ptr(sub)); } - xs = _z_subscription_sptr_list_tail(xs); + xs = _z_subscription_rc_list_tail(xs); } return ret; @@ -79,7 +79,7 @@ _z_subscription_sptr_list_t *__z_get_subscriptions_by_key(_z_subscription_sptr_l * - zn->_mutex_inner */ _z_subscription_rc_t *__unsafe_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { - _z_subscription_sptr_list_t *subs = + _z_subscription_rc_list_t *subs = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_subscriptions : zn->_remote_subscriptions; return __z_get_subscription_by_id(subs, id); } @@ -89,9 +89,9 @@ _z_subscription_rc_t *__unsafe_z_get_subscription_by_id(_z_session_t *zn, uint8_ * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_subscription_sptr_list_t *__unsafe_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, +_z_subscription_rc_list_t *__unsafe_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t key) { - _z_subscription_sptr_list_t *subs = + _z_subscription_rc_list_t *subs = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_subscriptions : zn->_remote_subscriptions; return __z_get_subscriptions_by_key(subs, key); } @@ -110,12 +110,12 @@ _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_loc return sub; } -_z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *key) { +_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *key) { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, *key); + _z_subscription_rc_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, *key); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -132,15 +132,15 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, s->_key); + _z_subscription_rc_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, s->_key); if (subs == NULL) { // A subscription for this name does not yet exists ret = (_z_subscription_rc_t *)zp_malloc(sizeof(_z_subscription_rc_t)); if (ret != NULL) { *ret = _z_subscription_rc_new(*s); if (is_local == _Z_RESOURCE_IS_LOCAL) { - zn->_local_subscriptions = _z_subscription_sptr_list_push(zn->_local_subscriptions, ret); + zn->_local_subscriptions = _z_subscription_rc_list_push(zn->_local_subscriptions, ret); } else { - zn->_remote_subscriptions = _z_subscription_sptr_list_push(zn->_remote_subscriptions, ret); + zn->_remote_subscriptions = _z_subscription_rc_list_push(zn->_remote_subscriptions, ret); } } } @@ -187,7 +187,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &keyexpr); _Z_DEBUG("Triggering subs for %d - %s", key._id, key._suffix); if (key._suffix != NULL) { - _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_LOCAL, key); + _z_subscription_rc_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_LOCAL, key); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -203,16 +203,16 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co #if Z_FEATURE_ATTACHMENT == 1 s.attachment = att; #endif - _z_subscription_sptr_list_t *xs = subs; - _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_sptr_list_len(xs)); + _z_subscription_rc_list_t *xs = subs; + _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_rc_list_len(xs)); while (xs != NULL) { - _z_subscription_rc_t *sub = _z_subscription_sptr_list_head(xs); + _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); sub->ptr->_callback(&s, sub->ptr->_arg); - xs = _z_subscription_sptr_list_tail(xs); + xs = _z_subscription_rc_list_tail(xs); } _z_keyexpr_clear(&key); - _z_subscription_sptr_list_free(&subs); + _z_subscription_rc_list_free(&subs); } else { #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); @@ -230,10 +230,10 @@ void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscript if (is_local == _Z_RESOURCE_IS_LOCAL) { zn->_local_subscriptions = - _z_subscription_sptr_list_drop_filter(zn->_local_subscriptions, _z_subscription_rc_eq, sub); + _z_subscription_rc_list_drop_filter(zn->_local_subscriptions, _z_subscription_rc_eq, sub); } else { zn->_remote_subscriptions = - _z_subscription_sptr_list_drop_filter(zn->_remote_subscriptions, _z_subscription_rc_eq, sub); + _z_subscription_rc_list_drop_filter(zn->_remote_subscriptions, _z_subscription_rc_eq, sub); } #if Z_FEATURE_MULTI_THREAD == 1 @@ -246,8 +246,8 @@ void _z_flush_subscriptions(_z_session_t *zn) { zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - _z_subscription_sptr_list_free(&zn->_local_subscriptions); - _z_subscription_sptr_list_free(&zn->_remote_subscriptions); + _z_subscription_rc_list_free(&zn->_local_subscriptions); + _z_subscription_rc_list_free(&zn->_remote_subscriptions); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); From 5320e0af87958dc2048d3e70b6c031fe388f61ba Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 16:15:03 +0100 Subject: [PATCH 10/34] feat: name anon struct for forward declaration --- include/zenoh-pico/collections/refcount.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index cb1d99897..2215837ac 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -43,7 +43,7 @@ /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct { \ + typedef struct name##_rc_t { \ type##_t *ptr; \ _z_atomic(unsigned int) * _cnt; \ } name##_rc_t; \ @@ -100,7 +100,7 @@ #ifdef ZENOH_COMPILER_GCC /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct { \ + typedef struct name##_rc_t { \ type##_t *ptr; \ unsigned int *_cnt; \ } name##_rc_t; \ @@ -161,7 +161,7 @@ #else // Z_FEATURE_MULTI_THREAD == 0 /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct { \ + typedef struct name##_rc_t { \ type##_t *ptr; \ volatile unsigned int *_cnt; \ } name##_rc_t; \ From 06a084684a97b547302c5e447ec34ee98807186c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 16:15:57 +0100 Subject: [PATCH 11/34] feat: add _z_session refcounter type --- include/zenoh-pico/net/session.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index e995c3b9b..cc0831553 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -64,6 +64,10 @@ typedef struct _z_session_t { #endif } _z_session_t; +extern void _z_session_clear(_z_session_t *zn); // Forward type declaration to avoid cyclical include + +_Z_REFCOUNT_DEFINE(_z_session, _z_session) + /** * Open a zenoh-net session * From 4dc768ba5dd42b886d651c65ff3afba5cae26152 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 12 Jan 2024 16:23:34 +0100 Subject: [PATCH 12/34] chore: clang-format --- include/zenoh-pico/net/session.h | 2 +- include/zenoh-pico/session/subscription.h | 3 +-- src/session/queryable.c | 3 +-- src/session/subscription.c | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index cc0831553..117c49c3f 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -64,7 +64,7 @@ typedef struct _z_session_t { #endif } _z_session_t; -extern void _z_session_clear(_z_session_t *zn); // Forward type declaration to avoid cyclical include +extern void _z_session_clear(_z_session_t *zn); // Forward type declaration to avoid cyclical include _Z_REFCOUNT_DEFINE(_z_session, _z_session) diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 3c5c6a3ed..f13ffe05f 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -20,8 +20,7 @@ #if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscription ------------------*/ _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); -_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, - const _z_keyexpr_t *keyexpr); +_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, diff --git a/src/session/queryable.c b/src/session/queryable.c index 5910b7de1..d3b699830 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -206,8 +206,7 @@ void _z_unregister_questionable(_z_session_t *zn, _z_questionable_rc_t *qle) { zp_mutex_lock(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 - zn->_local_questionable = - _z_questionable_rc_list_drop_filter(zn->_local_questionable, _z_questionable_rc_eq, qle); + zn->_local_questionable = _z_questionable_rc_list_drop_filter(zn->_local_questionable, _z_questionable_rc_eq, qle); #if Z_FEATURE_MULTI_THREAD == 1 zp_mutex_unlock(&zn->_mutex_inner); diff --git a/src/session/subscription.c b/src/session/subscription.c index 5124160c3..f54685ff2 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -90,7 +90,7 @@ _z_subscription_rc_t *__unsafe_z_get_subscription_by_id(_z_session_t *zn, uint8_ * - zn->_mutex_inner */ _z_subscription_rc_list_t *__unsafe_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, - const _z_keyexpr_t key) { + const _z_keyexpr_t key) { _z_subscription_rc_list_t *subs = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_subscriptions : zn->_remote_subscriptions; return __z_get_subscriptions_by_key(subs, key); From 76e17c4b15c3576ef26e569367c63e0fa5cd933f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 15 Jan 2024 09:16:16 +0100 Subject: [PATCH 13/34] chore: update gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 11d4de96f..ae356975d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # CMake build +crossbuilds # vscode .vscode @@ -52,6 +53,7 @@ cmake-build-release *.su *.idb *.pdb +*.sarif # Kernel Module Compile Results *.mod* From 5f5c22842a2d5188e335b7dc844ef022d908ed2a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 15 Jan 2024 09:31:46 +0100 Subject: [PATCH 14/34] build: add compiler id log --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c01b7efa9..69ac6dc24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ else() endif() # Compiler definition +message("Compilers in use: ${CMAKE_C_COMPILER_ID}, ${CMAKE_CXX_COMPILER_ID}") if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") add_definition(ZENOH_COMPILER_CLANG) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") From 104fff55e0c337632deeabcc52b7a08c78e062de Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 15 Jan 2024 10:52:42 +0100 Subject: [PATCH 15/34] build: add compiler symbol on platformio builds --- .github/workflows/arduino_esp32.yaml | 2 +- .github/workflows/mbed.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/arduino_esp32.yaml b/.github/workflows/arduino_esp32.yaml index bd347aec6..ce4125da5 100644 --- a/.github/workflows/arduino_esp32.yaml +++ b/.github/workflows/arduino_esp32.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ARDUINO_BASE cd $ARDUINO_BASE - platformio init -b esp32thing_plus --project-option="build_flags=-DZ_FEATURE_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3" + platformio init -b esp32thing_plus --project-option="build_flags=-DZ_FEATURE_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" cd $ARDUINO_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/.github/workflows/mbed.yaml b/.github/workflows/mbed.yaml index 60c74ecb7..d3c799cd3 100644 --- a/.github/workflows/mbed.yaml +++ b/.github/workflows/mbed.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $MBED_BASE cd $MBED_BASE - pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_FEATURE_LINK_SERIAL=1 -DZENOH_DEBUG=3" + pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_FEATURE_LINK_SERIAL=1 -DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" cd $MBED_BASE/lib ln -s $ZENOH_PICO_BASE From dc67d9775add7c31646c32941c6193f3ddf04698 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 15 Jan 2024 16:40:09 +0100 Subject: [PATCH 16/34] feat: remove superfluous function --- include/zenoh-pico/session/utils.h | 3 +-- src/api/api.c | 12 +++++++----- src/session/utils.c | 11 ----------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 39cae73d3..39491ba4d 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -27,9 +27,8 @@ _z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t id, const char *loc const _Bool exit_on_first); int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid); -int8_t _z_session_close(_z_session_t *zn, uint8_t reason); void _z_session_clear(_z_session_t *zn); -void _z_session_free(_z_session_t **zn); +int8_t _z_session_close(_z_session_t *zn, uint8_t reason); int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *z_msg, uint16_t local_peer_id); int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, diff --git a/src/api/api.c b/src/api/api.c index ee4898591..378c15923 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -544,12 +544,14 @@ z_owned_session_t z_open(z_owned_config_t *config) { } int8_t z_close(z_owned_session_t *zs) { - int8_t ret = _Z_RES_OK; - + if ((zs == NULL) || (zs->_value == NULL)) { + return _Z_RES_OK; + } _z_close(zs->_value); - _z_session_free(&zs->_value); - - return ret; + _z_session_clear(zs->_value); + zp_free(zs->_value); + zs->_value = NULL; + return _Z_RES_OK; } int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { diff --git a/src/session/utils.c b/src/session/utils.c index ea6fc34dc..f90ca20ff 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -121,17 +121,6 @@ void _z_session_clear(_z_session_t *zn) { #endif // Z_FEATURE_MULTI_THREAD == 1 } -void _z_session_free(_z_session_t **zn) { - _z_session_t *ptr = *zn; - - if (ptr != NULL) { - _z_session_clear(ptr); - - zp_free(ptr); - *zn = NULL; - } -} - int8_t _z_session_close(_z_session_t *zn, uint8_t reason) { int8_t ret = _Z_ERR_GENERIC; From f04c5a51111628e8d12662325cfd5094dc3f0bf2 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 15 Jan 2024 17:12:41 +0100 Subject: [PATCH 17/34] feat: go through indirect session pointer --- include/zenoh-pico/api/types.h | 11 ++- src/api/api.c | 126 +++++++++++++++++++-------------- tests/z_client_test.c | 4 +- tests/z_peer_multicast_test.c | 5 +- 4 files changed, 88 insertions(+), 58 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index be3726d04..7360928c8 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -129,9 +129,16 @@ _OWNED_TYPE_PTR(_z_scouting_config_t, scouting_config) * Represents a Zenoh session. */ typedef struct { - _z_session_t *_val; + _z_session_t *ptr; +} _z_indir_session_t; + +typedef struct { + _z_indir_session_t _val; } z_session_t; -_OWNED_TYPE_PTR(_z_session_t, session) + +typedef struct { + _z_indir_session_t _value; +} z_owned_session_t; /** * Represents a Zenoh (push) Subscriber entity. diff --git a/src/api/api.c b/src/api/api.c index 378c15923..75ebcd27b 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -89,7 +89,7 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_str_t ret = {._value = NULL}; - _z_keyexpr_t ekey = _z_get_expanded_key_from_key(zs._val, &keyexpr); + _z_keyexpr_t ekey = _z_get_expanded_key_from_key(zs._val.ptr, &keyexpr); ret._value = (char *)ekey._suffix; // ekey will be out of scope so // - suffix can be safely casted as non-const // - suffix does not need to be copied @@ -417,14 +417,21 @@ OWNED_FUNCTIONS_PTR_COMMON(z_scouting_config_t, z_owned_scouting_config_t, scout OWNED_FUNCTIONS_PTR_CLONE(z_scouting_config_t, z_owned_scouting_config_t, scouting_config, _z_owner_noop_copy) OWNED_FUNCTIONS_PTR_DROP(z_scouting_config_t, z_owned_scouting_config_t, scouting_config, _z_scouting_config_free) -OWNED_FUNCTIONS_PTR_COMMON(z_session_t, z_owned_session_t, session) -OWNED_FUNCTIONS_PTR_CLONE(z_session_t, z_owned_session_t, session, _z_owner_noop_copy) -void z_session_drop(z_owned_session_t *val) { z_close(val); } - OWNED_FUNCTIONS_PTR_INTERNAL(z_keyexpr_t, z_owned_keyexpr_t, keyexpr, _z_keyexpr_free, _z_keyexpr_copy) OWNED_FUNCTIONS_PTR_INTERNAL(z_hello_t, z_owned_hello_t, hello, _z_hello_free, _z_owner_noop_copy) OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_str_array_free, _z_owner_noop_copy) +_Bool z_session_check(const z_owned_session_t *val) { return val->_value.ptr != NULL; } +z_session_t z_session_loan(const z_owned_session_t *val) { return (z_session_t){._val = val->_value}; } +z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.ptr = NULL}}; } +z_owned_session_t *z_session_move(z_owned_session_t *val) { return val; } +z_owned_session_t z_session_clone(z_owned_session_t *val) { + z_owned_session_t ret; + ret._value = val->_value; // ret._value = _z_session_rc_clone(&val->_value); + return ret; +} +void z_session_drop(z_owned_session_t *val) { z_close(val); } + #define OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \ _Bool z_##name##_check(const ownedtype *val) { return val->call != NULL; } \ ownedtype *z_##name##_move(ownedtype *val) { return val; } \ @@ -528,38 +535,52 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb } z_owned_session_t z_open(z_owned_config_t *config) { - z_owned_session_t zs = {._value = (_z_session_t *)zp_malloc(sizeof(_z_session_t))}; - memset(zs._value, 0, sizeof(_z_session_t)); - - if (zs._value != NULL) { - if (_z_open(zs._value, config->_value) != _Z_RES_OK) { - zp_free(zs._value); - zs._value = NULL; - } + z_owned_session_t zs = {._value = {.ptr = NULL}}; - z_config_drop(config); + _z_session_t *tmp_sess = (_z_session_t *)zp_malloc(sizeof(_z_session_t)); + if (tmp_sess == NULL) { + return zs; } + memset(tmp_sess, 0, sizeof(_z_session_t)); + // Open session + if (_z_open(tmp_sess, config->_value) != _Z_RES_OK) { + return zs; + } + // Create rc + // _z_session_rc_t zsrc = _z_session_rc_new(tmp_sess); + // if (zsrc.ptr == NULL) { + // _z_close(&tmp_sess); + // _z_session_clear(&tmp_sess); + // return zs; + // } + // // Store rc in session + // zs._value = zsrc; + + zs._value.ptr = tmp_sess; + + z_config_drop(config); return zs; } int8_t z_close(z_owned_session_t *zs) { - if ((zs == NULL) || (zs->_value == NULL)) { + if ((zs == NULL) || (zs->_value.ptr == NULL)) { return _Z_RES_OK; } - _z_close(zs->_value); - _z_session_clear(zs->_value); - zp_free(zs->_value); - zs->_value = NULL; + _z_close(zs->_value.ptr); + // _z_session_rc_drop(&zs->_value); + _z_session_clear(zs->_value.ptr); + zp_free(zs->_value.ptr); + zs->_value.ptr = NULL; return _Z_RES_OK; } int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val->_tp._type) { + switch (zs._val.ptr->_tp._type) { case _Z_TRANSPORT_MULTICAST_TYPE: case _Z_TRANSPORT_RAWETH_TYPE: - _zp_multicast_fetch_zid(&zs._val->_tp, callback); + _zp_multicast_fetch_zid(&zs._val.ptr->_tp, callback); break; default: break; @@ -576,9 +597,9 @@ int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val->_tp._type) { + switch (zs._val.ptr->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: - _zp_unicast_fetch_zid(&zs._val->_tp, callback); + _zp_unicast_fetch_zid(&zs._val.ptr->_tp, callback); break; default: break; @@ -593,7 +614,7 @@ int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) return 0; } -z_id_t z_info_zid(const z_session_t zs) { return zs._val->_local_zid; } +z_id_t z_info_zid(const z_session_t zs) { return zs._val.ptr->_local_zid; } #if Z_FEATURE_PUBLICATION == 1 OWNED_FUNCTIONS_PTR_COMMON(z_publisher_t, z_owned_publisher_t, publisher) @@ -627,7 +648,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint opt.attachment = options->attachment; #endif } - ret = _z_write(zs._val, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, + ret = _z_write(zs._val.ptr, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -636,7 +657,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint ); // Trigger local subscriptions - _z_trigger_local_subscriptions(zs._val, keyexpr, payload, payload_len + _z_trigger_local_subscriptions(zs._val.ptr, keyexpr, payload, payload_len); #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -654,7 +675,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(zs._val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, + ret = _z_write(zs._val.ptr, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -675,10 +696,10 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); + if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); key = _z_rid_with_suffix(id, NULL); } } @@ -689,7 +710,8 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con opt.priority = options->priority; } - return (z_owned_publisher_t){._value = _z_declare_publisher(zs._val, key, opt.congestion_control, opt.priority)}; + return (z_owned_publisher_t){._value = + _z_declare_publisher(zs._val.ptr, key, opt.congestion_control, opt.priority)}; } int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { @@ -822,7 +844,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne wrapped_ctx->ctx = ctx; } - ret = _z_query(zs._val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, + ret = _z_query(zs._val.ptr, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, wrapped_ctx, callback->drop, ctx #if Z_FEATURE_ATTACHMENT == 1 @@ -868,10 +890,10 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); + if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); key = _z_rid_with_suffix(id, NULL); } } @@ -882,7 +904,7 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o } return (z_owned_queryable_t){ - ._value = _z_declare_queryable(zs._val, key, opt.complete, callback->call, callback->drop, ctx)}; + ._value = _z_declare_queryable(zs._val.ptr, key, opt.complete, callback->call, callback->drop, ctx)}; } int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { @@ -928,7 +950,7 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { key._value = (z_keyexpr_t *)zp_malloc(sizeof(z_keyexpr_t)); if (key._value != NULL) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); *key._value = _z_rid_with_suffix(id, NULL); } @@ -938,7 +960,7 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr) { int8_t ret = _Z_RES_OK; - ret = _z_undeclare_resource(zs._val, keyexpr->_value->_id); + ret = _z_undeclare_resource(zs._val.ptr, keyexpr->_value->_id); z_keyexpr_drop(keyexpr); return ret; @@ -971,8 +993,8 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); + if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); if (r == NULL) { char *wild = strpbrk(keyexpr._suffix, "*$"); _Bool do_keydecl = true; @@ -990,7 +1012,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z } } if (do_keydecl) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); key = _z_rid_with_suffix(id, wild); } } @@ -1000,7 +1022,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z if (options != NULL) { subinfo.reliability = options->reliability; } - _z_subscriber_t *sub = _z_declare_subscriber(zs._val, key, subinfo, callback->call, callback->drop, ctx); + _z_subscriber_t *sub = _z_declare_subscriber(zs._val.ptr, key, subinfo, callback->call, callback->drop, ctx); if (suffix != NULL) { zp_free(suffix); } @@ -1017,9 +1039,9 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t callback->context = NULL; z_keyexpr_t key = keyexpr; - _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); key = _z_rid_with_suffix(id, NULL); } @@ -1029,7 +1051,7 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t } return (z_owned_pull_subscriber_t){ - ._value = _z_declare_subscriber(zs._val, key, subinfo, callback->call, callback->drop, ctx)}; + ._value = _z_declare_subscriber(zs._val.ptr, key, subinfo, callback->call, callback->drop, ctx)}; } int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { @@ -1093,7 +1115,7 @@ int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_read_task(zs._val, opt.task_attributes); + return _zp_start_read_task(zs._val.ptr, opt.task_attributes); #else (void)(zs); return -1; @@ -1102,7 +1124,7 @@ int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) int8_t zp_stop_read_task(z_session_t zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_read_task(zs._val); + return _zp_stop_read_task(zs._val.ptr); #else (void)(zs); return -1; @@ -1126,7 +1148,7 @@ int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *option if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_lease_task(zs._val, opt.task_attributes); + return _zp_start_lease_task(zs._val.ptr, opt.task_attributes); #else (void)(zs); return -1; @@ -1135,7 +1157,7 @@ int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *option int8_t zp_stop_lease_task(z_session_t zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_lease_task(zs._val); + return _zp_stop_lease_task(zs._val.ptr); #else (void)(zs); return -1; @@ -1146,7 +1168,7 @@ zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){.__ int8_t zp_read(z_session_t zs, const zp_read_options_t *options) { (void)(options); - return _zp_read(zs._val); + return _zp_read(zs._val.ptr); } zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { @@ -1155,14 +1177,14 @@ zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options) { (void)(options); - return _zp_send_keep_alive(zs._val); + return _zp_send_keep_alive(zs._val.ptr); } zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){.__dummy = 0}; } int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { (void)(options); - return _zp_send_join(zs._val); + return _zp_send_join(zs._val.ptr); } #if Z_FEATURE_ATTACHMENT == 1 void _z_bytes_pair_clear(struct _z_bytes_pair_t *this_) { diff --git a/tests/z_client_test.c b/tests/z_client_test.c index b2975f960..cafaee6d2 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -122,7 +122,7 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); - z_string_t zid1 = format_id(&z_loan(s1)._val->_local_zid); + z_string_t zid1 = format_id(&z_loan(s1)._val.ptr->_local_zid); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -137,7 +137,7 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - z_string_t zid2 = format_id(&z_loan(s2)._val->_local_zid); + z_string_t zid2 = format_id(&z_loan(s2)._val.ptr->_local_zid); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 17bdb27e2..5c8015694 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -73,7 +73,8 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); - _z_bytes_t id_as_bytes = _z_bytes_wrap(z_loan(s1)._val->_local_zid.id, _z_id_len(z_loan(s1)._val->_local_zid)); + _z_bytes_t id_as_bytes = + _z_bytes_wrap(z_loan(s1)._val.ptr->_local_zid.id, _z_id_len(z_loan(s1)._val.ptr->_local_zid)); z_string_t zid1 = _z_string_from_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -90,7 +91,7 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - id_as_bytes = _z_bytes_wrap(z_loan(s2)._val->_local_zid.id, _z_id_len(z_loan(s2)._val->_local_zid)); + id_as_bytes = _z_bytes_wrap(z_loan(s2)._val.ptr->_local_zid.id, _z_id_len(z_loan(s2)._val.ptr->_local_zid)); z_string_t zid2 = _z_string_from_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); From 9161fce28c37970b2783a2023e5e4f8123dd4c8a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 16 Jan 2024 15:55:50 +0100 Subject: [PATCH 18/34] fix: use correct type type for session pointers --- include/zenoh-pico/transport/transport.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 7b8d44836..e708b7e82 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -53,6 +53,9 @@ _Z_LIST_DEFINE(_z_transport_peer_entry, _z_transport_peer_entry_t) _z_transport_peer_entry_list_t *_z_transport_peer_entry_list_insert(_z_transport_peer_entry_list_t *root, _z_transport_peer_entry_t *entry); +// Forward type declaration to avoid cyclical include +typedef struct _z_session_t _z_session_t; + // Forward declaration to be used in _zp_f_send_tmsg* typedef struct _z_transport_multicast_t _z_transport_multicast_t; // Send function prototype @@ -60,6 +63,7 @@ typedef int8_t (*_zp_f_send_tmsg)(_z_transport_multicast_t *self, const _z_trans typedef struct { // Session associated to the transport + _z_session_t *_session; #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes @@ -85,8 +89,6 @@ typedef struct { _z_zint_t _sn_rx_best_effort; volatile _z_zint_t _lease; - void *_session; - #if Z_FEATURE_MULTI_THREAD == 1 zp_task_t *_read_task; zp_task_t *_lease_task; @@ -100,7 +102,7 @@ typedef struct { typedef struct _z_transport_multicast_t { // Session associated to the transport - void *_session; + _z_session_t *_session; #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes From b119691f0baec7a44dfb067931351fa04870c1cb Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 16 Jan 2024 16:13:27 +0100 Subject: [PATCH 19/34] feat: use session ref counter in z_session --- include/zenoh-pico/api/types.h | 8 ++------ src/api/api.c | 35 +++++++++++++--------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 7360928c8..7d776251d 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -129,15 +129,11 @@ _OWNED_TYPE_PTR(_z_scouting_config_t, scouting_config) * Represents a Zenoh session. */ typedef struct { - _z_session_t *ptr; -} _z_indir_session_t; - -typedef struct { - _z_indir_session_t _val; + _z_session_rc_t _val; } z_session_t; typedef struct { - _z_indir_session_t _value; + _z_session_rc_t _value; } z_owned_session_t; /** diff --git a/src/api/api.c b/src/api/api.c index 75ebcd27b..383a8ff98 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -423,11 +423,11 @@ OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_s _Bool z_session_check(const z_owned_session_t *val) { return val->_value.ptr != NULL; } z_session_t z_session_loan(const z_owned_session_t *val) { return (z_session_t){._val = val->_value}; } -z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.ptr = NULL}}; } +z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.ptr = NULL, ._cnt = 0}}; } z_owned_session_t *z_session_move(z_owned_session_t *val) { return val; } z_owned_session_t z_session_clone(z_owned_session_t *val) { z_owned_session_t ret; - ret._value = val->_value; // ret._value = _z_session_rc_clone(&val->_value); + ret._value = _z_session_rc_clone(&val->_value); return ret; } void z_session_drop(z_owned_session_t *val) { z_close(val); } @@ -535,29 +535,22 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb } z_owned_session_t z_open(z_owned_config_t *config) { - z_owned_session_t zs = {._value = {.ptr = NULL}}; + z_owned_session_t zs = {._value = {.ptr = NULL, ._cnt = 0}}; + _z_session_t tmp_sess; + memset(&tmp_sess, 0, sizeof(_z_session_t)); - _z_session_t *tmp_sess = (_z_session_t *)zp_malloc(sizeof(_z_session_t)); - if (tmp_sess == NULL) { + // Create rc + _z_session_rc_t zsrc = _z_session_rc_new(tmp_sess); + if (zsrc.ptr == NULL) { return zs; } - memset(tmp_sess, 0, sizeof(_z_session_t)); - // Open session - if (_z_open(tmp_sess, config->_value) != _Z_RES_OK) { + if (_z_open(zsrc.ptr, config->_value) != _Z_RES_OK) { + _z_session_rc_drop(&zsrc); return zs; } - // Create rc - // _z_session_rc_t zsrc = _z_session_rc_new(tmp_sess); - // if (zsrc.ptr == NULL) { - // _z_close(&tmp_sess); - // _z_session_clear(&tmp_sess); - // return zs; - // } - // // Store rc in session - // zs._value = zsrc; - - zs._value.ptr = tmp_sess; + // Store rc in session + zs._value = zsrc; z_config_drop(config); return zs; @@ -568,9 +561,7 @@ int8_t z_close(z_owned_session_t *zs) { return _Z_RES_OK; } _z_close(zs->_value.ptr); - // _z_session_rc_drop(&zs->_value); - _z_session_clear(zs->_value.ptr); - zp_free(zs->_value.ptr); + _z_session_rc_drop(&zs->_value); zs->_value.ptr = NULL; return _Z_RES_OK; } From 9e293bd46d0a8d901c9389e516b115876b84a371 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 16 Jan 2024 17:01:31 +0100 Subject: [PATCH 20/34] fix: prevent segfault when clearing empty session --- src/link/link.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/link/link.c b/src/link/link.c index 3ccc27697..674da4bf1 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -115,8 +115,12 @@ int8_t _z_listen_link(_z_link_t *zl, const char *locator) { } void _z_link_clear(_z_link_t *l) { - l->_close_f(l); - l->_free_f(l); + if (l->_close_f != NULL) { + l->_close_f(l); + } + if (l->_free_f != NULL) { + l->_free_f(l); + } _z_endpoint_clear(&l->_endpoint); } From 253073e74a80d6ee7c09005436cf0735c0a04528 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 16 Jan 2024 17:01:57 +0100 Subject: [PATCH 21/34] fix: drop owned config all the time --- src/api/api.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/api.c b/src/api/api.c index 383a8ff98..03f856be9 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -542,16 +542,17 @@ z_owned_session_t z_open(z_owned_config_t *config) { // Create rc _z_session_rc_t zsrc = _z_session_rc_new(tmp_sess); if (zsrc.ptr == NULL) { + z_config_drop(config); return zs; } // Open session if (_z_open(zsrc.ptr, config->_value) != _Z_RES_OK) { _z_session_rc_drop(&zsrc); + z_config_drop(config); return zs; } // Store rc in session zs._value = zsrc; - z_config_drop(config); return zs; } From 974c87eded42fe24f02599781e8ec2346346f26f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 09:35:03 +0100 Subject: [PATCH 22/34] feat: add new_from_val rc function --- include/zenoh-pico/collections/refcount.h | 49 +++++++++++++++++++++-- src/api/api.c | 4 +- src/session/queryable.c | 2 +- src/session/subscription.c | 2 +- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 2215837ac..2343f18dd 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -47,7 +47,21 @@ type##_t *ptr; \ _z_atomic(unsigned int) * _cnt; \ } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(type##_t val) { \ + static inline name##_rc_t name##_rc_new(void) { \ + name##_rc_t p; \ + p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + if (p.ptr != NULL) { \ + p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int))); \ + if (p._cnt != NULL) { \ + memset(p.ptr, 0, sizeof(type##_t)); \ + _z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \ + } else { \ + zp_free(p.ptr); \ + } \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ @@ -104,7 +118,22 @@ type##_t *ptr; \ unsigned int *_cnt; \ } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(type##_t val) { \ + static inline name##_rc_t name##_rc_new(void) { \ + name##_rc_t p; \ + p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + if (p.ptr != NULL) { \ + p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ + if (p._cnt != NULL) { \ + memset(p.ptr, 0, sizeof(type##_t)); \ + __sync_fetch_and_and(p._cnt, 0); \ + __sync_fetch_and_add(p._cnt, 1); \ + } else { \ + zp_free(p.ptr); \ + } \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ @@ -165,7 +194,21 @@ type##_t *ptr; \ volatile unsigned int *_cnt; \ } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(type##_t val) { \ + static inline name##_rc_t name##_rc_new(void) { \ + name##_rc_t p; \ + p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + if (p.ptr != NULL) { \ + p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ + if (p._cnt != NULL) { \ + memset(p.ptr, 0, sizeof(type##_t)); \ + *p._cnt = 1; \ + } else { \ + zp_free(p.ptr); \ + } \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ name##_rc_t p; \ p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ diff --git a/src/api/api.c b/src/api/api.c index 03f856be9..97431a6f7 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -536,11 +536,9 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb z_owned_session_t z_open(z_owned_config_t *config) { z_owned_session_t zs = {._value = {.ptr = NULL, ._cnt = 0}}; - _z_session_t tmp_sess; - memset(&tmp_sess, 0, sizeof(_z_session_t)); // Create rc - _z_session_rc_t zsrc = _z_session_rc_new(tmp_sess); + _z_session_rc_t zsrc = _z_session_rc_new(); if (zsrc.ptr == NULL) { z_config_drop(config); return zs; diff --git a/src/session/queryable.c b/src/session/queryable.c index d3b699830..1fd24ddec 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -128,7 +128,7 @@ _z_questionable_rc_t *_z_register_questionable(_z_session_t *zn, _z_questionable ret = (_z_questionable_rc_t *)zp_malloc(sizeof(_z_questionable_rc_t)); if (ret != NULL) { - *ret = _z_questionable_rc_new(*q); + *ret = _z_questionable_rc_new_from_val(*q); zn->_local_questionable = _z_questionable_rc_list_push(zn->_local_questionable, ret); } diff --git a/src/session/subscription.c b/src/session/subscription.c index f54685ff2..c761beb7c 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -136,7 +136,7 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca if (subs == NULL) { // A subscription for this name does not yet exists ret = (_z_subscription_rc_t *)zp_malloc(sizeof(_z_subscription_rc_t)); if (ret != NULL) { - *ret = _z_subscription_rc_new(*s); + *ret = _z_subscription_rc_new_from_val(*s); if (is_local == _Z_RESOURCE_IS_LOCAL) { zn->_local_subscriptions = _z_subscription_rc_list_push(zn->_local_subscriptions, ret); } else { From 0a834ff035da598f78e1ef27ea2d45af1d695683 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 10:30:19 +0100 Subject: [PATCH 23/34] feat: switch publisher sesssion ptr to rc --- include/zenoh-pico/net/primitives.h | 4 ++-- include/zenoh-pico/net/publish.h | 2 +- src/api/api.c | 9 ++++----- src/net/primitives.c | 13 ++++++------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index c1cf2a0da..0e096e2c9 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -86,8 +86,8 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid); * Returns: * The created :c:type:`_z_publisher_t` or null if the declaration failed. */ -_z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, - z_priority_t priority); +_z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, + z_congestion_control_t congestion_control, z_priority_t priority); /** * Undeclare a :c:type:`_z_publisher_t`. diff --git a/include/zenoh-pico/net/publish.h b/include/zenoh-pico/net/publish.h index 15933e035..75ebad8d1 100644 --- a/include/zenoh-pico/net/publish.h +++ b/include/zenoh-pico/net/publish.h @@ -24,7 +24,7 @@ typedef struct { _z_keyexpr_t _key; _z_zint_t _id; - _z_session_t *_zn; + _z_session_rc_t _zn; z_congestion_control_t _congestion_control; z_priority_t _priority; } _z_publisher_t; diff --git a/src/api/api.c b/src/api/api.c index 97431a6f7..c5109345b 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -700,8 +700,7 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con opt.priority = options->priority; } - return (z_owned_publisher_t){._value = - _z_declare_publisher(zs._val.ptr, key, opt.congestion_control, opt.priority)}; + return (z_owned_publisher_t){._value = _z_declare_publisher(&zs._val, key, opt.congestion_control, opt.priority)}; } int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { @@ -738,7 +737,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l #endif } - ret = _z_write(pub._val->_zn, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, + ret = _z_write(pub._val->_zn.ptr, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, pub._val->_congestion_control, pub._val->_priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -747,7 +746,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l ); // Trigger local subscriptions - _z_trigger_local_subscriptions(pub._val->_zn, pub._val->_key, payload, len + _z_trigger_local_subscriptions(pub._val->_zn.ptr, pub._val->_key, payload, len); #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -759,7 +758,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(pub._val->_zn, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, + return _z_write(pub._val->_zn.ptr, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, pub._val->_congestion_control, pub._val->_priority #if Z_FEATURE_ATTACHMENT == 1 , diff --git a/src/net/primitives.c b/src/net/primitives.c index b1eccb23d..1d74115ba 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -98,17 +98,16 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { #if Z_FEATURE_PUBLICATION == 1 /*------------------ Publisher Declaration ------------------*/ -_z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, - z_priority_t priority) { +_z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, + z_congestion_control_t congestion_control, z_priority_t priority) { _z_publisher_t *ret = (_z_publisher_t *)zp_malloc(sizeof(_z_publisher_t)); if (ret != NULL) { - ret->_zn = zn; + ret->_zn = _z_session_rc_clone(zn); ret->_key = _z_keyexpr_duplicate(keyexpr); - ret->_id = _z_get_entity_id(zn); + ret->_id = _z_get_entity_id(zn->ptr); ret->_congestion_control = congestion_control; ret->_priority = priority; } - return ret; } @@ -117,11 +116,11 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { if (pub != NULL) { // Build the declare message to send on the wire - _z_undeclare_resource(pub->_zn, pub->_key._id); + _z_undeclare_resource(pub->_zn.ptr, pub->_key._id); + _z_session_rc_drop(&pub->_zn); } else { ret = _Z_ERR_ENTITY_UNKNOWN; } - return ret; } From 50c5e796cc9f34f456568d46b3e6dc3a05d3d37a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 10:43:19 +0100 Subject: [PATCH 24/34] feat: switch subscriber session ptr to rc --- include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/net/subscribe.h | 2 +- src/api/api.c | 6 +++--- src/net/primitives.c | 31 +++++++++++++++-------------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 0e096e2c9..8c3f0aff5 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -143,7 +143,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay * Returns: * The created :c:type:`_z_subscriber_t` or null if the declaration failed. */ -_z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, +_z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg); /** diff --git a/include/zenoh-pico/net/subscribe.h b/include/zenoh-pico/net/subscribe.h index d6c3ba67a..e2aafe540 100644 --- a/include/zenoh-pico/net/subscribe.h +++ b/include/zenoh-pico/net/subscribe.h @@ -25,7 +25,7 @@ */ typedef struct { uint32_t _entity_id; - _z_session_t *_zn; + _z_session_rc_t _zn; } _z_subscriber_t; typedef _z_subscriber_t _z_pull_subscriber_t; diff --git a/src/api/api.c b/src/api/api.c index c5109345b..3e90faec8 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1011,7 +1011,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z if (options != NULL) { subinfo.reliability = options->reliability; } - _z_subscriber_t *sub = _z_declare_subscriber(zs._val.ptr, key, subinfo, callback->call, callback->drop, ctx); + _z_subscriber_t *sub = _z_declare_subscriber(&zs._val, key, subinfo, callback->call, callback->drop, ctx); if (suffix != NULL) { zp_free(suffix); } @@ -1040,7 +1040,7 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t } return (z_owned_pull_subscriber_t){ - ._value = _z_declare_subscriber(zs._val.ptr, key, subinfo, callback->call, callback->drop, ctx)}; + ._value = _z_declare_subscriber(&zs._val, key, subinfo, callback->call, callback->drop, ctx)}; } int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { @@ -1067,7 +1067,7 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { z_owned_keyexpr_t ret = z_keyexpr_null(); uint32_t lookup = sub._val->_entity_id; if (sub._val != NULL) { - _z_subscription_rc_list_t *tail = sub._val->_zn->_local_subscriptions; + _z_subscription_rc_list_t *tail = sub._val->_zn.ptr->_local_subscriptions; while (tail != NULL && !z_keyexpr_check(&ret)) { _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); if (head->ptr->_id == lookup) { diff --git a/src/net/primitives.c b/src/net/primitives.c index 1d74115ba..e5bc1df5e 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -185,12 +185,12 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay #if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscriber Declaration ------------------*/ -_z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, +_z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_subscription_t s; - s._id = _z_get_entity_id(zn); + s._id = _z_get_entity_id(zn->ptr); s._key_id = keyexpr._id; - s._key = _z_get_expanded_key_from_key(zn, &keyexpr); + s._key = _z_get_expanded_key_from_key(zn->ptr, &keyexpr); s._info = sub_info; s._callback = callback; s._dropper = dropper; @@ -198,19 +198,19 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ _z_subscriber_t *ret = (_z_subscriber_t *)zp_malloc(sizeof(_z_subscriber_t)); if (ret != NULL) { - ret->_zn = zn; + ret->_zn = _z_session_rc_clone(zn); ret->_entity_id = s._id; _z_subscription_rc_t *sp_s = _z_register_subscription( - zn, _Z_RESOURCE_IS_LOCAL, &s); // This a pointer to the entry stored at session-level. - // Do not drop it by the end of this function. + zn->ptr, _Z_RESOURCE_IS_LOCAL, &s); // This a pointer to the entry stored at session-level. + // Do not drop it by the end of this function. if (sp_s != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_decl_subscriber( &keyexpr, s._id, sub_info.reliability == Z_RELIABILITY_RELIABLE, sub_info.mode == Z_SUBMODE_PULL); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - _z_unregister_subscription(zn, _Z_RESOURCE_IS_LOCAL, sp_s); + if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_unregister_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, sp_s); _z_subscriber_free(&ret); } _z_n_msg_clear(&n_msg); @@ -228,15 +228,16 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t ret = _Z_ERR_GENERIC; if (sub != NULL) { - _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(sub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + if (_z_send_n_msg(sub->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local subscription state can be removed - _z_undeclare_resource(sub->_zn, s->ptr->_key_id); - _z_unregister_subscription(sub->_zn, _Z_RESOURCE_IS_LOCAL, s); + _z_undeclare_resource(sub->_zn.ptr, s->ptr->_key_id); + _z_unregister_subscription(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, s); + _z_session_rc_drop(&sub->_zn); } else { ret = _Z_ERR_ENTITY_UNKNOWN; } @@ -255,11 +256,11 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { int8_t ret = _Z_RES_OK; - _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { - _z_zint_t pull_id = _z_get_pull_id(sub->_zn); + _z_zint_t pull_id = _z_get_pull_id(sub->_zn.ptr); _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); - if (_z_send_n_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + if (_z_send_n_msg(sub->_zn.ptr, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } } else { From 1a37eecae36a1a4e5a3139d98f91cef8ce10bd5f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 11:26:16 +0100 Subject: [PATCH 25/34] fix: remove unnecessary dependency --- include/zenoh-pico/net/query.h | 2 +- include/zenoh-pico/session/session.h | 2 +- src/net/query.c | 2 +- src/session/queryable.c | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 96e579ef6..dc39e06d2 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -25,7 +25,7 @@ typedef struct _z_session_t _z_session_t; /** * The query to be answered by a queryable. */ -typedef struct { +typedef struct z_query_t { _z_value_t _value; _z_keyexpr_t _key; uint32_t _request_id; diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 91cbb267b..7a2272253 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -23,7 +23,6 @@ #include "zenoh-pico/collections/refcount.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" -#include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/transport/manager.h" @@ -111,6 +110,7 @@ typedef struct { uint32_t _id; } _z_publication_t; +typedef struct z_query_t z_query_t; // Forward type declaration to avoid cyclical include /** * The callback signature of the functions handling query messages. */ diff --git a/src/net/query.c b/src/net/query.c index 04a728266..1f56e612d 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -11,7 +11,7 @@ // Contributors: // ZettaScale Zenoh Team, -#include "zenoh-pico/session/query.h" +#include "zenoh-pico/net/query.h" #if Z_FEATURE_QUERYABLE == 1 void _z_queryable_clear(_z_queryable_t *qbl) { diff --git a/src/session/queryable.c b/src/session/queryable.c index 1fd24ddec..82823d099 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" From fe05e713453785b6a887588923ebcd15b3440863 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 11:29:35 +0100 Subject: [PATCH 26/34] fix: add necessary dependency --- include/zenoh-pico/net/query.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index dc39e06d2..9cb2ba498 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -17,11 +17,9 @@ #include #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/net/session.h" #include "zenoh-pico/protocol/core.h" -// Forward type declaration to avoid cyclical include -typedef struct _z_session_t _z_session_t; - /** * The query to be answered by a queryable. */ From de195796c86509debabc90e044ddebcaf833746a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 11:59:47 +0100 Subject: [PATCH 27/34] feat: switch queryable session ptr to rc --- include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/net/query.h | 2 +- src/api/api.c | 2 +- src/net/primitives.c | 24 ++++++++++++------------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 8c3f0aff5..a4b76c5b1 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -184,7 +184,7 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub); * Returns: * The created :c:type:`_z_queryable_t` or null if the declaration failed. */ -_z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bool complete, +_z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, _z_questionable_handler_t callback, _z_drop_handler_t dropper, void *arg); /** diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 9cb2ba498..8c6a2d701 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -37,7 +37,7 @@ typedef struct z_query_t { */ typedef struct { uint32_t _entity_id; - _z_session_t *_zn; + _z_session_rc_t _zn; } _z_queryable_t; #if Z_FEATURE_QUERYABLE == 1 diff --git a/src/api/api.c b/src/api/api.c index 3e90faec8..c3e22c9c4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -893,7 +893,7 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o } return (z_owned_queryable_t){ - ._value = _z_declare_queryable(zs._val.ptr, key, opt.complete, callback->call, callback->drop, ctx)}; + ._value = _z_declare_queryable(&zs._val, key, opt.complete, callback->call, callback->drop, ctx)}; } int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { diff --git a/src/net/primitives.c b/src/net/primitives.c index e5bc1df5e..dd8cf6200 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -273,11 +273,11 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { #if Z_FEATURE_QUERYABLE == 1 /*------------------ Queryable Declaration ------------------*/ -_z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bool complete, +_z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, _z_questionable_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_questionable_t q; - q._id = _z_get_entity_id(zn); - q._key = _z_get_expanded_key_from_key(zn, &keyexpr); + q._id = _z_get_entity_id(zn->ptr); + q._key = _z_get_expanded_key_from_key(zn->ptr, &keyexpr); q._complete = complete; q._callback = callback; q._dropper = dropper; @@ -285,20 +285,20 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo _z_queryable_t *ret = (_z_queryable_t *)zp_malloc(sizeof(_z_queryable_t)); if (ret != NULL) { - ret->_zn = zn; + ret->_zn = _z_session_rc_clone(zn); ret->_entity_id = q._id; _z_questionable_rc_t *sp_q = - _z_register_questionable(zn, &q); // This a pointer to the entry stored at session-level. - // Do not drop it by the end of this function. + _z_register_questionable(zn->ptr, &q); // This a pointer to the entry stored at session-level. + // Do not drop it by the end of this function. if (sp_q != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_decl_queryable(&keyexpr, q._id, q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; - _z_unregister_questionable(zn, sp_q); + _z_unregister_questionable(zn->ptr, sp_q); _z_queryable_free(&ret); } _z_n_msg_clear(&n_msg); @@ -316,22 +316,22 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { int8_t ret = _Z_RES_OK; if (qle != NULL) { - _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn, qle->_entity_id); + _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn.ptr, qle->_entity_id); if (q != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(qle->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + if (_z_send_n_msg(qle->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local queryable state can be removed - _z_unregister_questionable(qle->_zn, q); + _z_unregister_questionable(qle->_zn.ptr, q); } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } _z_n_msg_clear(&n_msg); - } else { ret = _Z_ERR_ENTITY_UNKNOWN; } + _z_session_rc_drop(&qle->_zn); } else { ret = _Z_ERR_ENTITY_UNKNOWN; } From f72df5bc35ccd64226149a58048f8be6b5840e59 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 16:12:00 +0100 Subject: [PATCH 28/34] refactor: flatten queryable primitives --- src/net/primitives.c | 85 ++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/src/net/primitives.c b/src/net/primitives.c index dd8cf6200..149f8eef9 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -283,60 +283,53 @@ _z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, q._dropper = dropper; q._arg = arg; + // Allocate queryable _z_queryable_t *ret = (_z_queryable_t *)zp_malloc(sizeof(_z_queryable_t)); - if (ret != NULL) { - ret->_zn = _z_session_rc_clone(zn); - ret->_entity_id = q._id; - - _z_questionable_rc_t *sp_q = - _z_register_questionable(zn->ptr, &q); // This a pointer to the entry stored at session-level. - // Do not drop it by the end of this function. - if (sp_q != NULL) { - // Build the declare message to send on the wire - _z_declaration_t declaration = - _z_make_decl_queryable(&keyexpr, q._id, q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - // ret = _Z_ERR_TRANSPORT_TX_FAILED; - _z_unregister_questionable(zn->ptr, sp_q); - _z_queryable_free(&ret); - } - _z_n_msg_clear(&n_msg); - } else { - _z_queryable_free(&ret); - } - } else { + if (ret == NULL) { _z_questionable_clear(&q); + return NULL; } - + // Create questionable entry, stored at session-level, do not drop it by the end of this function. + _z_questionable_rc_t *sp_q = _z_register_questionable(zn->ptr, &q); + if (sp_q == NULL) { + _z_queryable_free(&ret); + return NULL; + } + // Build the declare message to send on the wire + _z_declaration_t declaration = _z_make_decl_queryable(&keyexpr, q._id, q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_unregister_questionable(zn->ptr, sp_q); + _z_queryable_free(&ret); + return NULL; + } + _z_n_msg_clear(&n_msg); + // Fill queryable + ret->_entity_id = q._id; + ret->_zn = _z_session_rc_clone(zn); return ret; } int8_t _z_undeclare_queryable(_z_queryable_t *qle) { - int8_t ret = _Z_RES_OK; - - if (qle != NULL) { - _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn.ptr, qle->_entity_id); - if (q != NULL) { - // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(qle->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - // Only if message is successfully send, local queryable state can be removed - _z_unregister_questionable(qle->_zn.ptr, q); - } else { - ret = _Z_ERR_TRANSPORT_TX_FAILED; - } - _z_n_msg_clear(&n_msg); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; - } - _z_session_rc_drop(&qle->_zn); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; + if (qle == NULL) { + return _Z_ERR_ENTITY_UNKNOWN; } - - return ret; + // Find questionable entry + _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn.ptr, qle->_entity_id); + if (q == NULL) { + return _Z_ERR_ENTITY_UNKNOWN; + } + // Build the declare message to send on the wire + _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(qle->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + return _Z_ERR_TRANSPORT_TX_FAILED; + } + _z_n_msg_clear(&n_msg); + // Only if message is successfully send, local queryable state can be removed + _z_unregister_questionable(qle->_zn.ptr, q); + _z_session_rc_drop(&qle->_zn); + return _Z_RES_OK; } int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const _z_value_t payload) { From 8232fc2cbecbcc428c567b79c0d8e626e470b224 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 16:24:35 +0100 Subject: [PATCH 29/34] refactor: flatten subscriber primitives --- src/net/primitives.c | 87 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/src/net/primitives.c b/src/net/primitives.c index 149f8eef9..3d4fa39a2 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -196,60 +196,55 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr s._dropper = dropper; s._arg = arg; + // Allocate subscriber _z_subscriber_t *ret = (_z_subscriber_t *)zp_malloc(sizeof(_z_subscriber_t)); - if (ret != NULL) { - ret->_zn = _z_session_rc_clone(zn); - ret->_entity_id = s._id; - - _z_subscription_rc_t *sp_s = _z_register_subscription( - zn->ptr, _Z_RESOURCE_IS_LOCAL, &s); // This a pointer to the entry stored at session-level. - // Do not drop it by the end of this function. - if (sp_s != NULL) { - // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_decl_subscriber( - &keyexpr, s._id, sub_info.reliability == Z_RELIABILITY_RELIABLE, sub_info.mode == Z_SUBMODE_PULL); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - _z_unregister_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, sp_s); - _z_subscriber_free(&ret); - } - _z_n_msg_clear(&n_msg); - } else { - _z_subscriber_free(&ret); - } - } else { + if (ret == NULL) { _z_subscription_clear(&s); + return NULL; } - + // Register subscription, stored at session-level, do not drop it by the end of this function. + _z_subscription_rc_t *sp_s = _z_register_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, &s); + if (sp_s == NULL) { + _z_subscriber_free(&ret); + return NULL; + } + // Build the declare message to send on the wire + _z_declaration_t declaration = _z_make_decl_subscriber( + &keyexpr, s._id, sub_info.reliability == Z_RELIABILITY_RELIABLE, sub_info.mode == Z_SUBMODE_PULL); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_unregister_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, sp_s); + _z_subscriber_free(&ret); + return NULL; + } + _z_n_msg_clear(&n_msg); + // Fill subscriber + ret->_entity_id = s._id; + ret->_zn = _z_session_rc_clone(zn); return ret; } int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { - int8_t ret = _Z_ERR_GENERIC; - - if (sub != NULL) { - _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); - if (s != NULL) { - // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(sub->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - // Only if message is successfully send, local subscription state can be removed - _z_undeclare_resource(sub->_zn.ptr, s->ptr->_key_id); - _z_unregister_subscription(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, s); - _z_session_rc_drop(&sub->_zn); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; - } - _z_n_msg_clear(&n_msg); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; - } - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; + if (sub == NULL) { + return _Z_ERR_ENTITY_UNKNOWN; } - - return ret; + // Find subscription entry + _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + if (s == NULL) { + return _Z_ERR_ENTITY_UNKNOWN; + } + // Build the declare message to send on the wire + _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(sub->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + return _Z_ERR_TRANSPORT_TX_FAILED; + } + _z_n_msg_clear(&n_msg); + // Only if message is successfully send, local subscription state can be removed + _z_undeclare_resource(sub->_zn.ptr, s->ptr->_key_id); + _z_unregister_subscription(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, s); + _z_session_rc_drop(&sub->_zn); + return _Z_RES_OK; } /*------------------ Pull ------------------*/ From 34378c16bd9557fe31791e014dcbdb14105afc32 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 17 Jan 2024 16:28:31 +0100 Subject: [PATCH 30/34] refactor: flatten publisher primitives --- src/net/primitives.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/net/primitives.c b/src/net/primitives.c index 3d4fa39a2..b765bebef 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -100,28 +100,28 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { /*------------------ Publisher Declaration ------------------*/ _z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, z_priority_t priority) { + // Allocate publisher _z_publisher_t *ret = (_z_publisher_t *)zp_malloc(sizeof(_z_publisher_t)); - if (ret != NULL) { - ret->_zn = _z_session_rc_clone(zn); - ret->_key = _z_keyexpr_duplicate(keyexpr); - ret->_id = _z_get_entity_id(zn->ptr); - ret->_congestion_control = congestion_control; - ret->_priority = priority; + if (ret == NULL) { + return NULL; } + // Fill publisher + ret->_key = _z_keyexpr_duplicate(keyexpr); + ret->_id = _z_get_entity_id(zn->ptr); + ret->_congestion_control = congestion_control; + ret->_priority = priority; + ret->_zn = _z_session_rc_clone(zn); return ret; } int8_t _z_undeclare_publisher(_z_publisher_t *pub) { - int8_t ret = _Z_RES_OK; - - if (pub != NULL) { - // Build the declare message to send on the wire - _z_undeclare_resource(pub->_zn.ptr, pub->_key._id); - _z_session_rc_drop(&pub->_zn); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; + if (pub == NULL) { + return _Z_ERR_ENTITY_UNKNOWN; } - return ret; + // Clear publisher + _z_undeclare_resource(pub->_zn.ptr, pub->_key._id); + _z_session_rc_drop(&pub->_zn); + return _Z_RES_OK; } /*------------------ Write ------------------*/ From 6b6f20edae13e1c1e74c38167e03af81304e31b9 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 18 Jan 2024 15:41:32 +0100 Subject: [PATCH 31/34] feat: change rc to use a single inner pointer --- include/zenoh-pico/collections/refcount.h | 137 ++++++++++------------ src/api/api.c | 88 +++++++------- src/net/primitives.c | 50 ++++---- src/session/queryable.c | 6 +- src/session/subscription.c | 6 +- 5 files changed, 140 insertions(+), 147 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 2343f18dd..59e987f0e 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -42,81 +42,71 @@ #endif // __cplusplus /*------------------ Internal Array Macros ------------------*/ -#define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct name##_rc_t { \ - type##_t *ptr; \ - _z_atomic(unsigned int) * _cnt; \ - } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(void) { \ - name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int))); \ - if (p._cnt != NULL) { \ - memset(p.ptr, 0, sizeof(type##_t)); \ - _z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ - name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (_z_atomic(unsigned int) *)zp_malloc(sizeof(_z_atomic(unsigned int))); \ - if (p._cnt != NULL) { \ - *p.ptr = val; \ - _z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ - name##_rc_t c; \ - c._cnt = p->_cnt; \ - c.ptr = p->ptr; \ - _z_atomic_fetch_add_explicit(p->_cnt, 1, _z_memory_order_relaxed); \ - return c; \ - } \ - static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ - name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ - if (c != NULL) { \ - c->_cnt = p->_cnt; \ - c->ptr = p->ptr; \ - _z_atomic_fetch_add_explicit(p->_cnt, 1, _z_memory_order_relaxed); \ - } \ - return c; \ - } \ - static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ - return (left->ptr == right->ptr); \ - } \ - static inline _Bool name##_rc_drop(name##_rc_t *p) { \ - _Bool dropped = false; \ - if (p->_cnt != NULL) { \ - unsigned int c = _z_atomic_fetch_sub_explicit(p->_cnt, 1, _z_memory_order_release); \ - dropped = c == 1; \ - if (dropped == true) { \ - atomic_thread_fence(_z_memory_order_acquire); \ - if (p->ptr != NULL) { \ - type##_clear(p->ptr); \ - zp_free(p->ptr); \ - zp_free((void *)p->_cnt); \ - } \ - } \ - } \ - return dropped; \ +#define _Z_REFCOUNT_DEFINE(name, type) \ + typedef struct name##_inner_rc_t { \ + type##_t val; \ + _z_atomic(unsigned int) _cnt; \ + } name##_inner_rc_t; \ + typedef struct name##_rc_t { \ + name##_inner_rc_t *in; \ + } name##_rc_t; \ + static inline name##_rc_t name##_rc_new(void) { \ + name##_rc_t p; \ + p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ + if (p.in != NULL) { \ + memset(&p.in->val, 0, sizeof(type##_t)); \ + _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ + name##_rc_t p; \ + p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ + if (p.in != NULL) { \ + p.in->val = val; \ + _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); \ + } \ + return p; \ + } \ + static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ + name##_rc_t c; \ + c.in = p->in; \ + _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); \ + return c; \ + } \ + static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ + name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ + if (c != NULL) { \ + c->in = p->in; \ + _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); \ + } \ + return c; \ + } \ + static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ + return (left->in == right->in); \ + } \ + static inline _Bool name##_rc_drop(name##_rc_t *p) { \ + if ((p == NULL) || (p->in == NULL)) { \ + return false; \ + } \ + if (_z_atomic_fetch_sub_explicit(&p->in->_cnt, 1, _z_memory_order_release) > 1) { \ + return false; \ + } \ + atomic_thread_fence(_z_memory_order_acquire); \ + type##_clear(&p->in->val); \ + zp_free(p->in); \ + return true; \ } #else // ZENOH_C_STANDARD == 99 #ifdef ZENOH_COMPILER_GCC /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ + typedef struct name##_inner_rc_t { \ + type##_t val; \ + unsigned int _cnt; \ + } name##_inner_rc_t; \ typedef struct name##_rc_t { \ - type##_t *ptr; \ - unsigned int *_cnt; \ + name##_inner_rc_t *in; \ } name##_rc_t; \ static inline name##_rc_t name##_rc_new(void) { \ name##_rc_t p; \ @@ -190,13 +180,16 @@ #else // Z_FEATURE_MULTI_THREAD == 0 /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ + typedef struct name##_inner_rc_t { \ + type##_t val; \ + unsigned int _cnt; \ + } name##_inner_rc_t; \ typedef struct name##_rc_t { \ - type##_t *ptr; \ - volatile unsigned int *_cnt; \ + name##_inner_rc_t *in; \ } name##_rc_t; \ static inline name##_rc_t name##_rc_new(void) { \ name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ + p.in = (type##_t *)zp_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ if (p._cnt != NULL) { \ diff --git a/src/api/api.c b/src/api/api.c index c3e22c9c4..5250c3439 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -89,7 +89,7 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_str_t ret = {._value = NULL}; - _z_keyexpr_t ekey = _z_get_expanded_key_from_key(zs._val.ptr, &keyexpr); + _z_keyexpr_t ekey = _z_get_expanded_key_from_key(&zs._val.in->val, &keyexpr); ret._value = (char *)ekey._suffix; // ekey will be out of scope so // - suffix can be safely casted as non-const // - suffix does not need to be copied @@ -421,9 +421,9 @@ OWNED_FUNCTIONS_PTR_INTERNAL(z_keyexpr_t, z_owned_keyexpr_t, keyexpr, _z_keyexpr OWNED_FUNCTIONS_PTR_INTERNAL(z_hello_t, z_owned_hello_t, hello, _z_hello_free, _z_owner_noop_copy) OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_str_array_free, _z_owner_noop_copy) -_Bool z_session_check(const z_owned_session_t *val) { return val->_value.ptr != NULL; } +_Bool z_session_check(const z_owned_session_t *val) { return val->_value.in != NULL; } z_session_t z_session_loan(const z_owned_session_t *val) { return (z_session_t){._val = val->_value}; } -z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.ptr = NULL, ._cnt = 0}}; } +z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.in = NULL}}; } z_owned_session_t *z_session_move(z_owned_session_t *val) { return val; } z_owned_session_t z_session_clone(z_owned_session_t *val) { z_owned_session_t ret; @@ -535,16 +535,16 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb } z_owned_session_t z_open(z_owned_config_t *config) { - z_owned_session_t zs = {._value = {.ptr = NULL, ._cnt = 0}}; + z_owned_session_t zs = {._value = {.in = NULL}}; // Create rc _z_session_rc_t zsrc = _z_session_rc_new(); - if (zsrc.ptr == NULL) { + if (zsrc.in == NULL) { z_config_drop(config); return zs; } // Open session - if (_z_open(zsrc.ptr, config->_value) != _Z_RES_OK) { + if (_z_open(&zsrc.in->val, config->_value) != _Z_RES_OK) { _z_session_rc_drop(&zsrc); z_config_drop(config); return zs; @@ -556,21 +556,21 @@ z_owned_session_t z_open(z_owned_config_t *config) { } int8_t z_close(z_owned_session_t *zs) { - if ((zs == NULL) || (zs->_value.ptr == NULL)) { + if ((zs == NULL) || (zs->_value.in == NULL)) { return _Z_RES_OK; } - _z_close(zs->_value.ptr); + _z_close(&zs->_value.in->val); _z_session_rc_drop(&zs->_value); - zs->_value.ptr = NULL; + zs->_value.in = NULL; return _Z_RES_OK; } int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val.ptr->_tp._type) { + switch (zs._val.in->val._tp._type) { case _Z_TRANSPORT_MULTICAST_TYPE: case _Z_TRANSPORT_RAWETH_TYPE: - _zp_multicast_fetch_zid(&zs._val.ptr->_tp, callback); + _zp_multicast_fetch_zid(&zs._val.in->val._tp, callback); break; default: break; @@ -587,9 +587,9 @@ int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val.ptr->_tp._type) { + switch (zs._val.in->val._tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: - _zp_unicast_fetch_zid(&zs._val.ptr->_tp, callback); + _zp_unicast_fetch_zid(&zs._val.in->val._tp, callback); break; default: break; @@ -604,7 +604,7 @@ int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) return 0; } -z_id_t z_info_zid(const z_session_t zs) { return zs._val.ptr->_local_zid; } +z_id_t z_info_zid(const z_session_t zs) { return zs._val.in->val._local_zid; } #if Z_FEATURE_PUBLICATION == 1 OWNED_FUNCTIONS_PTR_COMMON(z_publisher_t, z_owned_publisher_t, publisher) @@ -638,7 +638,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint opt.attachment = options->attachment; #endif } - ret = _z_write(zs._val.ptr, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, + ret = _z_write(&zs._val.in->val, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -647,7 +647,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint ); // Trigger local subscriptions - _z_trigger_local_subscriptions(zs._val.ptr, keyexpr, payload, payload_len); + _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len); #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -665,7 +665,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(zs._val.ptr, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, + ret = _z_write(&zs._val.in->val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -686,10 +686,10 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); + if (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); + uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); key = _z_rid_with_suffix(id, NULL); } } @@ -737,7 +737,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l #endif } - ret = _z_write(pub._val->_zn.ptr, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, + ret = _z_write(&pub._val->_zn.in->val, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, pub._val->_congestion_control, pub._val->_priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -746,7 +746,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l ); // Trigger local subscriptions - _z_trigger_local_subscriptions(pub._val->_zn.ptr, pub._val->_key, payload, len); + _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len); #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -758,7 +758,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(pub._val->_zn.ptr, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, + return _z_write(&pub._val->_zn.in->val, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, pub._val->_congestion_control, pub._val->_priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -833,7 +833,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne wrapped_ctx->ctx = ctx; } - ret = _z_query(zs._val.ptr, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, + ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, wrapped_ctx, callback->drop, ctx #if Z_FEATURE_ATTACHMENT == 1 @@ -879,10 +879,10 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); + if (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); + uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); key = _z_rid_with_suffix(id, NULL); } } @@ -939,7 +939,7 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { key._value = (z_keyexpr_t *)zp_malloc(sizeof(z_keyexpr_t)); if (key._value != NULL) { - uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); + uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); *key._value = _z_rid_with_suffix(id, NULL); } @@ -949,7 +949,7 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr) { int8_t ret = _Z_RES_OK; - ret = _z_undeclare_resource(zs._val.ptr, keyexpr->_value->_id); + ret = _z_undeclare_resource(&zs._val.in->val, keyexpr->_value->_id); z_keyexpr_drop(keyexpr); return ret; @@ -982,8 +982,8 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic // resource declarations are only performed on unicast transports. - if (zs._val.ptr->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); + if (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); if (r == NULL) { char *wild = strpbrk(keyexpr._suffix, "*$"); _Bool do_keydecl = true; @@ -1001,7 +1001,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z } } if (do_keydecl) { - uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); + uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); key = _z_rid_with_suffix(id, wild); } } @@ -1028,9 +1028,9 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t callback->context = NULL; z_keyexpr_t key = keyexpr; - _z_resource_t *r = _z_get_resource_by_key(zs._val.ptr, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val.ptr, keyexpr); + uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); key = _z_rid_with_suffix(id, NULL); } @@ -1067,11 +1067,11 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { z_owned_keyexpr_t ret = z_keyexpr_null(); uint32_t lookup = sub._val->_entity_id; if (sub._val != NULL) { - _z_subscription_rc_list_t *tail = sub._val->_zn.ptr->_local_subscriptions; + _z_subscription_rc_list_t *tail = sub._val->_zn.in->val._local_subscriptions; while (tail != NULL && !z_keyexpr_check(&ret)) { _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); - if (head->ptr->_id == lookup) { - _z_keyexpr_t key = _z_keyexpr_duplicate(head->ptr->_key); + if (head->in->val._id == lookup) { + _z_keyexpr_t key = _z_keyexpr_duplicate(head->in->val._key); ret = (z_owned_keyexpr_t){._value = zp_malloc(sizeof(_z_keyexpr_t))}; if (ret._value != NULL) { *ret._value = key; @@ -1104,7 +1104,7 @@ int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_read_task(zs._val.ptr, opt.task_attributes); + return _zp_start_read_task(&zs._val.in->val, opt.task_attributes); #else (void)(zs); return -1; @@ -1113,7 +1113,7 @@ int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) int8_t zp_stop_read_task(z_session_t zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_read_task(zs._val.ptr); + return _zp_stop_read_task(&zs._val.in->val); #else (void)(zs); return -1; @@ -1137,7 +1137,7 @@ int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *option if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_lease_task(zs._val.ptr, opt.task_attributes); + return _zp_start_lease_task(&zs._val.in->val, opt.task_attributes); #else (void)(zs); return -1; @@ -1146,7 +1146,7 @@ int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *option int8_t zp_stop_lease_task(z_session_t zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_lease_task(zs._val.ptr); + return _zp_stop_lease_task(&zs._val.in->val); #else (void)(zs); return -1; @@ -1157,7 +1157,7 @@ zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){.__ int8_t zp_read(z_session_t zs, const zp_read_options_t *options) { (void)(options); - return _zp_read(zs._val.ptr); + return _zp_read(&zs._val.in->val); } zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { @@ -1166,14 +1166,14 @@ zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options) { (void)(options); - return _zp_send_keep_alive(zs._val.ptr); + return _zp_send_keep_alive(&zs._val.in->val); } zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){.__dummy = 0}; } int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { (void)(options); - return _zp_send_join(zs._val.ptr); + return _zp_send_join(&zs._val.in->val); } #if Z_FEATURE_ATTACHMENT == 1 void _z_bytes_pair_clear(struct _z_bytes_pair_t *this_) { diff --git a/src/net/primitives.c b/src/net/primitives.c index b765bebef..c8ecc6560 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -107,7 +107,7 @@ _z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, } // Fill publisher ret->_key = _z_keyexpr_duplicate(keyexpr); - ret->_id = _z_get_entity_id(zn->ptr); + ret->_id = _z_get_entity_id(&zn->in->val); ret->_congestion_control = congestion_control; ret->_priority = priority; ret->_zn = _z_session_rc_clone(zn); @@ -119,7 +119,7 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { return _Z_ERR_ENTITY_UNKNOWN; } // Clear publisher - _z_undeclare_resource(pub->_zn.ptr, pub->_key._id); + _z_undeclare_resource(&pub->_zn.in->val, pub->_key._id); _z_session_rc_drop(&pub->_zn); return _Z_RES_OK; } @@ -188,9 +188,9 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay _z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_subscription_t s; - s._id = _z_get_entity_id(zn->ptr); + s._id = _z_get_entity_id(&zn->in->val); s._key_id = keyexpr._id; - s._key = _z_get_expanded_key_from_key(zn->ptr, &keyexpr); + s._key = _z_get_expanded_key_from_key(&zn->in->val, &keyexpr); s._info = sub_info; s._callback = callback; s._dropper = dropper; @@ -203,7 +203,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr return NULL; } // Register subscription, stored at session-level, do not drop it by the end of this function. - _z_subscription_rc_t *sp_s = _z_register_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, &s); + _z_subscription_rc_t *sp_s = _z_register_subscription(&zn->in->val, _Z_RESOURCE_IS_LOCAL, &s); if (sp_s == NULL) { _z_subscriber_free(&ret); return NULL; @@ -212,8 +212,8 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr _z_declaration_t declaration = _z_make_decl_subscriber( &keyexpr, s._id, sub_info.reliability == Z_RELIABILITY_RELIABLE, sub_info.mode == Z_SUBMODE_PULL); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - _z_unregister_subscription(zn->ptr, _Z_RESOURCE_IS_LOCAL, sp_s); + if (_z_send_n_msg(&zn->in->val, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_unregister_subscription(&zn->in->val, _Z_RESOURCE_IS_LOCAL, sp_s); _z_subscriber_free(&ret); return NULL; } @@ -229,20 +229,20 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { return _Z_ERR_ENTITY_UNKNOWN; } // Find subscription entry - _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(&sub->_zn.in->val, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s == NULL) { return _Z_ERR_ENTITY_UNKNOWN; } // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); + _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->in->val._key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(sub->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + if (_z_send_n_msg(&sub->_zn.in->val, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { return _Z_ERR_TRANSPORT_TX_FAILED; } _z_n_msg_clear(&n_msg); // Only if message is successfully send, local subscription state can be removed - _z_undeclare_resource(sub->_zn.ptr, s->ptr->_key_id); - _z_unregister_subscription(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, s); + _z_undeclare_resource(&sub->_zn.in->val, s->in->val._key_id); + _z_unregister_subscription(&sub->_zn.in->val, _Z_RESOURCE_IS_LOCAL, s); _z_session_rc_drop(&sub->_zn); return _Z_RES_OK; } @@ -251,11 +251,11 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { int8_t ret = _Z_RES_OK; - _z_subscription_rc_t *s = _z_get_subscription_by_id(sub->_zn.ptr, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); + _z_subscription_rc_t *s = _z_get_subscription_by_id(&sub->_zn.in->val, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { - _z_zint_t pull_id = _z_get_pull_id(sub->_zn.ptr); - _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); - if (_z_send_n_msg(sub->_zn.ptr, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_zint_t pull_id = _z_get_pull_id(&sub->_zn.in->val); + _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->in->val._key), pull_id); + if (_z_send_n_msg(&sub->_zn.in->val, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } } else { @@ -271,8 +271,8 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { _z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, _z_questionable_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_questionable_t q; - q._id = _z_get_entity_id(zn->ptr); - q._key = _z_get_expanded_key_from_key(zn->ptr, &keyexpr); + q._id = _z_get_entity_id(&zn->in->val); + q._key = _z_get_expanded_key_from_key(&zn->in->val, &keyexpr); q._complete = complete; q._callback = callback; q._dropper = dropper; @@ -285,7 +285,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, return NULL; } // Create questionable entry, stored at session-level, do not drop it by the end of this function. - _z_questionable_rc_t *sp_q = _z_register_questionable(zn->ptr, &q); + _z_questionable_rc_t *sp_q = _z_register_questionable(&zn->in->val, &q); if (sp_q == NULL) { _z_queryable_free(&ret); return NULL; @@ -293,8 +293,8 @@ _z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_decl_queryable(&keyexpr, q._id, q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn->ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - _z_unregister_questionable(zn->ptr, sp_q); + if (_z_send_n_msg(&zn->in->val, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_unregister_questionable(&zn->in->val, sp_q); _z_queryable_free(&ret); return NULL; } @@ -310,19 +310,19 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { return _Z_ERR_ENTITY_UNKNOWN; } // Find questionable entry - _z_questionable_rc_t *q = _z_get_questionable_by_id(qle->_zn.ptr, qle->_entity_id); + _z_questionable_rc_t *q = _z_get_questionable_by_id(&qle->_zn.in->val, qle->_entity_id); if (q == NULL) { return _Z_ERR_ENTITY_UNKNOWN; } // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); + _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->in->val._key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(qle->_zn.ptr, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + if (_z_send_n_msg(&qle->_zn.in->val, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { return _Z_ERR_TRANSPORT_TX_FAILED; } _z_n_msg_clear(&n_msg); // Only if message is successfully send, local queryable state can be removed - _z_unregister_questionable(qle->_zn.ptr, q); + _z_unregister_questionable(&qle->_zn.in->val, q); _z_session_rc_drop(&qle->_zn); return _Z_RES_OK; } diff --git a/src/session/queryable.c b/src/session/queryable.c index 82823d099..a73615c61 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -42,7 +42,7 @@ _z_questionable_rc_t *__z_get_questionable_by_id(_z_questionable_rc_list_t *qles _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); - if (id == qle->ptr->_id) { + if (id == qle->in->val._id) { ret = qle; break; } @@ -59,7 +59,7 @@ _z_questionable_rc_list_t *__z_get_questionable_by_key(_z_questionable_rc_list_t _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); - if (_z_keyexpr_intersects(qle->ptr->_key._suffix, strlen(qle->ptr->_key._suffix), key._suffix, + if (_z_keyexpr_intersects(qle->in->val._key._suffix, strlen(qle->in->val._key._suffix), key._suffix, strlen(key._suffix)) == true) { ret = _z_questionable_rc_list_push(ret, _z_questionable_rc_clone_as_ptr(qle)); } @@ -174,7 +174,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons _z_questionable_rc_list_t *xs = qles; while (xs != NULL) { _z_questionable_rc_t *qle = _z_questionable_rc_list_head(xs); - qle->ptr->_callback(&q, qle->ptr->_arg); + qle->in->val._callback(&q, qle->in->val._arg); xs = _z_questionable_rc_list_tail(xs); } diff --git a/src/session/subscription.c b/src/session/subscription.c index c761beb7c..0cd1050d9 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -45,7 +45,7 @@ _z_subscription_rc_t *__z_get_subscription_by_id(_z_subscription_rc_list_t *subs _z_subscription_rc_list_t *xs = subs; while (xs != NULL) { _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); - if (id == sub->ptr->_id) { + if (id == sub->in->val._id) { ret = sub; break; } @@ -62,7 +62,7 @@ _z_subscription_rc_list_t *__z_get_subscriptions_by_key(_z_subscription_rc_list_ _z_subscription_rc_list_t *xs = subs; while (xs != NULL) { _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); - if (_z_keyexpr_intersects(sub->ptr->_key._suffix, strlen(sub->ptr->_key._suffix), key._suffix, + if (_z_keyexpr_intersects(sub->in->val._key._suffix, strlen(sub->in->val._key._suffix), key._suffix, strlen(key._suffix)) == true) { ret = _z_subscription_rc_list_push(ret, _z_subscription_rc_clone_as_ptr(sub)); } @@ -207,7 +207,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_rc_list_len(xs)); while (xs != NULL) { _z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs); - sub->ptr->_callback(&s, sub->ptr->_arg); + sub->in->val._callback(&s, sub->in->val._arg); xs = _z_subscription_rc_list_tail(xs); } From a48cdd3e51505862b2d0ae83902f9da7ac5b8b3d Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 18 Jan 2024 16:06:54 +0100 Subject: [PATCH 32/34] feat: mutualize rc code between implems --- include/zenoh-pico/collections/refcount.h | 209 +++++----------------- 1 file changed, 49 insertions(+), 160 deletions(-) diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 59e987f0e..c348d16b7 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -41,11 +41,53 @@ #define _z_memory_order_relaxed std::memory_order_relaxed #endif // __cplusplus +// c11 atomic variant +#define _ZP_RC_CNT_TYPE _z_atomic(unsigned int) +#define _ZP_RC_OP_INIT_CNT _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); +#define _ZP_RC_OP_INCR_CNT _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); +#define _ZP_RC_OP_DECR_AND_CMP _z_atomic_fetch_sub_explicit(&p->in->_cnt, 1, _z_memory_order_release) > 1 +#define _ZP_RC_OP_SYNC atomic_thread_fence(_z_memory_order_acquire); + +#else // ZENOH_C_STANDARD == 99 +#ifdef ZENOH_COMPILER_GCC + +// c99 gcc sync builtin variant +#define _ZP_RC_CNT_TYPE unsigned int +#define _ZP_RC_OP_INIT_CNT \ + __sync_fetch_and_and(&p.in->_cnt, 0); \ + __sync_fetch_and_add(&p.in->_cnt, 1); +#define _ZP_RC_OP_INCR_CNT __sync_fetch_and_add(&p->in->_cnt, 1); +#define _ZP_RC_OP_DECR_AND_CMP __sync_fetch_and_sub(&p->in->_cnt, 1) > 1 +#define _ZP_RC_OP_SYNC __sync_synchronize(); + +#else // !ZENOH_COMPILER_GCC + +// None variant +#error "Multi-thread refcount in C99 only exists for GCC, use GCC or C11 or deactivate multi-thread" +#define _ZP_RC_CNT_TYPE unsigned int +#define _ZP_RC_OP_INIT_CNT +#define _ZP_RC_OP_INCR_CNT +#define _ZP_RC_OP_DECR_AND_CMP +#define _ZP_RC_OP_SYNC + +#endif // ZENOH_COMPILER_GCC +#endif // ZENOH_C_STANDARD != 99 +#else // Z_FEATURE_MULTI_THREAD == 0 + +// Single thread variant +#define _ZP_RC_CNT_TYPE unsigned int +#define _ZP_RC_OP_INIT_CNT p.in->_cnt = 1; +#define _ZP_RC_OP_INCR_CNT p->in->_cnt += 1; +#define _ZP_RC_OP_DECR_AND_CMP p->in->_cnt-- > 1 +#define _ZP_RC_OP_SYNC + +#endif // Z_FEATURE_MULTI_THREAD == 1 + /*------------------ Internal Array Macros ------------------*/ #define _Z_REFCOUNT_DEFINE(name, type) \ typedef struct name##_inner_rc_t { \ type##_t val; \ - _z_atomic(unsigned int) _cnt; \ + _ZP_RC_CNT_TYPE _cnt; \ } name##_inner_rc_t; \ typedef struct name##_rc_t { \ name##_inner_rc_t *in; \ @@ -55,7 +97,7 @@ p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ if (p.in != NULL) { \ memset(&p.in->val, 0, sizeof(type##_t)); \ - _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); \ + _ZP_RC_OP_INIT_CNT \ } \ return p; \ } \ @@ -64,21 +106,21 @@ p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ if (p.in != NULL) { \ p.in->val = val; \ - _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); \ + _ZP_RC_OP_INIT_CNT \ } \ return p; \ } \ static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ name##_rc_t c; \ c.in = p->in; \ - _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); \ + _ZP_RC_OP_INCR_CNT \ return c; \ } \ static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ if (c != NULL) { \ c->in = p->in; \ - _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); \ + _ZP_RC_OP_INCR_CNT \ } \ return c; \ } \ @@ -89,166 +131,13 @@ if ((p == NULL) || (p->in == NULL)) { \ return false; \ } \ - if (_z_atomic_fetch_sub_explicit(&p->in->_cnt, 1, _z_memory_order_release) > 1) { \ + if (_ZP_RC_OP_DECR_AND_CMP) { \ return false; \ } \ - atomic_thread_fence(_z_memory_order_acquire); \ + _ZP_RC_OP_SYNC \ type##_clear(&p->in->val); \ zp_free(p->in); \ return true; \ } -#else // ZENOH_C_STANDARD == 99 -#ifdef ZENOH_COMPILER_GCC -/*------------------ Internal Array Macros ------------------*/ -#define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct name##_inner_rc_t { \ - type##_t val; \ - unsigned int _cnt; \ - } name##_inner_rc_t; \ - typedef struct name##_rc_t { \ - name##_inner_rc_t *in; \ - } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(void) { \ - name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ - if (p._cnt != NULL) { \ - memset(p.ptr, 0, sizeof(type##_t)); \ - __sync_fetch_and_and(p._cnt, 0); \ - __sync_fetch_and_add(p._cnt, 1); \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ - name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ - if (p._cnt != NULL) { \ - *p.ptr = val; \ - __sync_fetch_and_and(p._cnt, 0); \ - __sync_fetch_and_add(p._cnt, 1); \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ - name##_rc_t c; \ - c._cnt = p->_cnt; \ - c.ptr = p->ptr; \ - __sync_fetch_and_add(p->_cnt, 1); \ - return c; \ - } \ - static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ - name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ - if (c != NULL) { \ - c->_cnt = p->_cnt; \ - c->ptr = p->ptr; \ - __sync_fetch_and_add(p->_cnt, 1); \ - } \ - return c; \ - } \ - static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ - return (left->ptr == right->ptr); \ - } \ - static inline _Bool name##_rc_drop(name##_rc_t *p) { \ - _Bool dropped = false; \ - if (p->_cnt != NULL) { \ - unsigned int c = __sync_fetch_and_sub(p->_cnt, 1); \ - dropped = c == 1; \ - if (dropped == true) { \ - __sync_synchronize(); \ - if (p->ptr != NULL) { \ - type##_clear(p->ptr); \ - zp_free(p->ptr); \ - zp_free((void *)p->_cnt); \ - } \ - } \ - } \ - return dropped; \ - } -#else // !ZENOH_COMPILER_GCC -#error "Multi-thread refcount in C99 only exists for GCC, use GCC or C11 or deactivate multi-thread" -#endif // ZENOH_COMPILER_GCC -#endif // ZENOH_C_STANDARD != 99 -#else // Z_FEATURE_MULTI_THREAD == 0 -/*------------------ Internal Array Macros ------------------*/ -#define _Z_REFCOUNT_DEFINE(name, type) \ - typedef struct name##_inner_rc_t { \ - type##_t val; \ - unsigned int _cnt; \ - } name##_inner_rc_t; \ - typedef struct name##_rc_t { \ - name##_inner_rc_t *in; \ - } name##_rc_t; \ - static inline name##_rc_t name##_rc_new(void) { \ - name##_rc_t p; \ - p.in = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ - if (p._cnt != NULL) { \ - memset(p.ptr, 0, sizeof(type##_t)); \ - *p._cnt = 1; \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ - name##_rc_t p; \ - p.ptr = (type##_t *)zp_malloc(sizeof(type##_t)); \ - if (p.ptr != NULL) { \ - p._cnt = (unsigned int *)zp_malloc(sizeof(unsigned int)); \ - if (p._cnt != NULL) { \ - *p.ptr = val; \ - *p._cnt = 1; \ - } else { \ - zp_free(p.ptr); \ - } \ - } \ - return p; \ - } \ - static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ - name##_rc_t c; \ - c._cnt = p->_cnt; \ - c.ptr = p->ptr; \ - *p->_cnt = *p->_cnt + 1; \ - return c; \ - } \ - static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ - name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ - if (c != NULL) { \ - c->_cnt = p->_cnt; \ - c->ptr = p->ptr; \ - *p->_cnt = *p->_cnt + 1; \ - } \ - return c; \ - } \ - static inline _Bool name##_rc_eq(const name##_rc_t *left, const name##_rc_t *right) { \ - return (left->ptr == right->ptr); \ - } \ - static inline _Bool name##_rc_drop(name##_rc_t *p) { \ - _Bool dropped = true; \ - if (p->_cnt != NULL) { \ - *p->_cnt = *p->_cnt - 1; \ - dropped = *p->_cnt == 0; \ - if (dropped == true) { \ - if (p->ptr != NULL) { \ - type##_clear(p->ptr); \ - zp_free(p->ptr); \ - zp_free((void *)p->_cnt); \ - } \ - } \ - } \ - return dropped; \ - } -#endif // Z_FEATURE_MULTI_THREAD == 1 #endif /* ZENOH_PICO_COLLECTIONS_REFCOUNT_H */ From d4fb72b60b46730c5e1aade474483318c57a2082 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 18 Jan 2024 16:07:28 +0100 Subject: [PATCH 33/34] chore: clang-format --- src/api/api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 5250c3439..6ab55c3b4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -665,7 +665,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(&zs._val.in->val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, + ret = _z_write(&zs._val.in->val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -833,7 +833,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne wrapped_ctx->ctx = ctx; } - ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, + ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, wrapped_ctx, callback->drop, ctx #if Z_FEATURE_ATTACHMENT == 1 From 7e9441a6045016e59956f33cfc39299fb164ae16 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 18 Jan 2024 16:34:46 +0100 Subject: [PATCH 34/34] fix: compilation issues --- src/api/api.c | 9 ++++----- tests/z_client_test.c | 4 ++-- tests/z_peer_multicast_test.c | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 6ab55c3b4..9c3187894 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -647,7 +647,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint ); // Trigger local subscriptions - _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len); + _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -666,7 +666,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o opt.priority = options->priority; } ret = _z_write(&zs._val.in->val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, - opt.priority + opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -746,7 +746,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l ); // Trigger local subscriptions - _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len); + _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -834,8 +834,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne } ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - wrapped_ctx, callback->drop, ctx - + __z_reply_handler, wrapped_ctx, callback->drop, ctx #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() diff --git a/tests/z_client_test.c b/tests/z_client_test.c index cafaee6d2..bbb73bd30 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -122,7 +122,7 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); - z_string_t zid1 = format_id(&z_loan(s1)._val.ptr->_local_zid); + z_string_t zid1 = format_id(&z_loan(s1)._val.in->val._local_zid); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -137,7 +137,7 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - z_string_t zid2 = format_id(&z_loan(s2)._val.ptr->_local_zid); + z_string_t zid2 = format_id(&z_loan(s2)._val.in->val._local_zid); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 5c8015694..f0448ad41 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -74,7 +74,7 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); _z_bytes_t id_as_bytes = - _z_bytes_wrap(z_loan(s1)._val.ptr->_local_zid.id, _z_id_len(z_loan(s1)._val.ptr->_local_zid)); + _z_bytes_wrap(z_loan(s1)._val.in->val._local_zid.id, _z_id_len(z_loan(s1)._val.in->val._local_zid)); z_string_t zid1 = _z_string_from_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -91,7 +91,7 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - id_as_bytes = _z_bytes_wrap(z_loan(s2)._val.ptr->_local_zid.id, _z_id_len(z_loan(s2)._val.ptr->_local_zid)); + id_as_bytes = _z_bytes_wrap(z_loan(s2)._val.in->val._local_zid.id, _z_id_len(z_loan(s2)._val.in->val._local_zid)); z_string_t zid2 = _z_string_from_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2);