From bca6208832c53362f80d59ba384d94acc1354a73 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 27 Mar 2024 17:20:29 +0100 Subject: [PATCH] Owned sample draft --- examples/unix/c11/z_pull.c | 6 ++--- examples/unix/c11/z_sub_channel.c | 6 ++--- include/zenoh-pico/api/handlers.h | 36 ++++------------------------- include/zenoh-pico/api/macros.h | 4 ++++ include/zenoh-pico/api/primitives.h | 1 + include/zenoh-pico/api/types.h | 1 + include/zenoh-pico/protocol/core.h | 3 +++ src/api/api.c | 3 ++- 8 files changed, 21 insertions(+), 39 deletions(-) diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 2b194abf8..aa9a240f6 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -83,9 +83,9 @@ int main(int argc, char **argv) { z_owned_sample_t sample = z_sample_null(); while (true) { for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { - z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample.keyexpr)); - printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, - sample.payload.start); + z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample).keyexpr); + printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)z_loan(sample).payload.len, + z_loan(sample).payload.start); z_drop(z_move(keystr)); z_drop(z_move(sample)); } diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index 005fc60e4..0cb0e376f 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -73,9 +73,9 @@ int main(int argc, char **argv) { z_owned_sample_t sample = z_sample_null(); for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { - z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample.keyexpr)); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, - sample.payload.start); + z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample).keyexpr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)z_loan(sample).payload.len, + z_loan(sample).payload.start); z_drop(z_move(keystr)); z_drop(z_move(sample)); sample = z_sample_null(); diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index ffd41c855..e9d895678 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -24,28 +24,7 @@ #include "zenoh-pico/collections/ring.h" #include "zenoh-pico/system/platform.h" -// -- Owned sample -// @TODO: define it via dedicated macros and move it to the appropriate place in types. -typedef struct { - z_owned_keyexpr_t keyexpr; - z_bytes_t payload; - // @TODO: implement the rest of the fields -} z_owned_sample_t; - -static inline z_owned_sample_t z_sample_null(void) { - z_owned_sample_t sample; - sample.keyexpr = z_keyexpr_null(); - sample.payload = z_bytes_null(); - return sample; -} - -static inline z_owned_sample_t *z_sample_move(z_owned_sample_t *sample) { return sample; } -static inline z_owned_sample_t *z_sample_loan(z_owned_sample_t *sample) { return sample; } - -static inline bool z_sample_check(const z_owned_sample_t *sample) { - return z_keyexpr_check(&sample->keyexpr) && z_bytes_check(&sample->payload); -} - +// TODO(sashacmc): move/rename? static inline z_owned_sample_t z_sample_to_owned(const z_sample_t *src) { z_owned_sample_t dst = z_sample_null(); @@ -57,18 +36,11 @@ static inline z_owned_sample_t z_sample_to_owned(const z_sample_t *src) { if (ke == NULL) { return dst; } - *ke = _z_keyexpr_duplicate(src->keyexpr); - dst.keyexpr._value = ke; - dst.payload = _z_bytes_duplicate(&src->payload); - return dst; -} + dst._value->keyexpr = _z_keyexpr_duplicate(src->keyexpr); + dst._value->payload = _z_bytes_duplicate(&src->payload); -static inline void z_sample_drop(z_owned_sample_t *s) { - if (s != NULL) { - z_keyexpr_drop(&s->keyexpr); - _z_bytes_clear(&s->payload); - } + return dst; } _Z_ELEM_DEFINE(_z_owned_sample, z_owned_sample_t, _z_noop_size, z_sample_drop, _z_noop_copy) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 5ad7478b5..8b277bda1 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -246,6 +246,7 @@ template<> struct zenoh_drop_type { typedef void type; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; +template<> struct zenoh_drop_type { typedef void type; }; template<> inline int8_t z_drop(z_owned_session_t* v) { return z_close(v); } template<> inline int8_t z_drop(z_owned_publisher_t* v) { return z_undeclare_publisher(v); } @@ -262,6 +263,7 @@ template<> inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop template<> inline void z_drop(z_owned_closure_reply_t* v) { z_closure_reply_drop(v); } template<> inline void z_drop(z_owned_closure_hello_t* v) { z_closure_hello_drop(v); } template<> inline void z_drop(z_owned_closure_zid_t* v) { z_closure_zid_drop(v); } +template<> inline void z_drop(z_owned_sample_t* v) { z_closure_sample_drop(v); } inline void z_null(z_owned_session_t& v) { v = z_session_null(); } inline void z_null(z_owned_publisher_t& v) { v = z_publisher_null(); } @@ -278,6 +280,7 @@ inline void z_null(z_owned_closure_query_t& v) { v = z_closure_query_null(); } inline void z_null(z_owned_closure_reply_t& v) { v = z_closure_reply_null(); } inline void z_null(z_owned_closure_hello_t& v) { v = z_closure_hello_null(); } inline void z_null(z_owned_closure_zid_t& v) { v = z_closure_zid_null(); } +inline void z_null(z_owned_sample_t& v) { v = z_closure_sample_null(); } inline bool z_check(const z_owned_session_t& v) { return z_session_check(&v); } inline bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); } @@ -291,6 +294,7 @@ inline bool z_check(const z_owned_queryable_t& v) { return z_queryable_check(&v) inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); } inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); } inline bool z_check(const z_owned_str_t& v) { return z_str_check(&v); } +inline bool z_check(const z_owned_str_t& v) { return z_sample_check(&v); } inline void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample) { z_closure_sample_call(&closure, sample); } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index a72878b99..d2dfbade5 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -692,6 +692,7 @@ _OWNED_FUNCTIONS(z_queryable_t, z_owned_queryable_t, queryable) _OWNED_FUNCTIONS(z_hello_t, z_owned_hello_t, hello) _OWNED_FUNCTIONS(z_reply_t, z_owned_reply_t, reply) _OWNED_FUNCTIONS(z_str_array_t, z_owned_str_array_t, str_array) +_OWNED_FUNCTIONS(z_sample_t, z_owned_sample_t, sample) #define _OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \ _Bool z_##name##_check(const ownedtype *val); \ diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 3c9eda0b9..4cb51282d 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -401,6 +401,7 @@ typedef struct { * z_timestamp_t timestamp: The timestamp of this data sample. */ typedef _z_sample_t z_sample_t; +_OWNED_TYPE_PTR(z_sample_t, sample) /** * Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 113566319..c8a27ecd2 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -231,6 +231,9 @@ typedef struct { z_attachment_t attachment; #endif } _z_sample_t; +static inline bool _z_sample_check(const _z_sample_t *sample) { + return _z_keyexpr_check(sample->keyexpr) && _z_bytes_check(sample->payload); +} /** * Represents a Zenoh value. diff --git a/src/api/api.c b/src/api/api.c index 1369c82b1..d1bb3a179 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -422,6 +422,7 @@ OWNED_FUNCTIONS_PTR_DROP(z_scouting_config_t, z_owned_scouting_config_t, scoutin 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) +OWNED_FUNCTIONS_PTR_INTERNAL(z_sample_t, z_owned_sample_t, sample, _z_sample_free, _z_owner_noop_copy) _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}; } @@ -1258,4 +1259,4 @@ z_owned_bytes_map_t z_bytes_map_new(void) { return (z_owned_bytes_map_t){._inner z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = NULL}; } z_bytes_t z_bytes_from_str(const char *str) { return z_bytes_wrap((const uint8_t *)str, strlen(str)); } z_bytes_t z_bytes_null(void) { return (z_bytes_t){.len = 0, ._is_alloc = false, .start = NULL}; } -#endif \ No newline at end of file +#endif