diff --git a/docs/api.rst b/docs/api.rst index 7274678db..3b77179b0 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -285,7 +285,6 @@ Macros Primitives ~~~~~~~~~~ -.. autocfunction:: primitives.h::z_view_string_empty .. autocfunction:: primitives.h::z_view_string_from_str .. autocfunction:: primitives.h::z_view_keyexpr_from_str .. autocfunction:: primitives.h::z_view_keyexpr_from_str_unchecked diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 450466857..2cbe6dbe7 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -118,7 +118,7 @@ void loop() { z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { z_bytes_from_static_str(&payload, VALUE); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 166f89a5d..a43a100a9 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -165,7 +165,7 @@ void app_main() { z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { z_bytes_from_static_str(&payload, VALUE); - opts.payload = &payload; + opts.payload = z_move(payload); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 003c52529..ed0bb6416 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -77,7 +77,7 @@ void app_main(void) { z_view_keyexpr_t ke; if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) { printf("%s is not a valid key expression", KEYEXPR); - return -1; + return; } while (1) { @@ -89,7 +89,7 @@ void app_main(void) { z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { z_bytes_from_static_str(&payload, VALUE); - opts.payload = &payload; + opts.payload = z_move(payload); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index a69889a0b..a685ac365 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -79,7 +79,7 @@ void app_main(void) { z_view_keyexpr_t ke; if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) { printf("%s is not a valid key expression", KEYEXPR); - return -1; + return; } printf("Creating Queryable on '%s'...\n", KEYEXPR); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index bf802fb14..b9bfbc880 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -86,7 +86,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { z_bytes_from_static_str(&payload, VALUE); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 9fd5830c7..115aed1ba 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -130,7 +130,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (value != NULL) { z_bytes_from_static_str(&payload, value); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } z_owned_closure_reply_t callback; diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index adfddbe80..6309b898f 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -68,9 +68,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); - z_bytes_drop(&first); - z_bytes_drop(&second); - z_bytes_drop(&kv); + z_bytes_drop(z_bytes_move(&first)); + z_bytes_drop(z_bytes_move(&second)); + z_bytes_drop(z_bytes_move(&kv)); kvp->current_idx++; } } @@ -85,8 +85,8 @@ void print_attachment(kv_pairs_rx_t *kvp) { void drop_attachment(kv_pairs_rx_t *kvp) { for (size_t i = 0; i < kvp->current_idx; i++) { - z_string_drop(&kvp->data[i].key); - z_string_drop(&kvp->data[i].value); + z_string_drop(z_string_move(&kvp->data[i].key)); + z_string_drop(z_string_move(&kvp->data[i].value)); } z_free(kvp->data); } @@ -206,7 +206,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (value != NULL) { z_bytes_from_static_str(&payload, value); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } // Add attachment value diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index a05ece655..441b56040 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -94,7 +94,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (value != NULL) { z_bytes_from_static_str(&payload, value); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } z_owned_closure_reply_t closure; z_owned_ring_handler_reply_t handler; diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 44806c397..7bcc970ff 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) { } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; - z_drop(pub); + z_drop(z_move(*pub)); // A note on lifetimes: // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still @@ -59,7 +59,7 @@ int main(int argc, char** argv) { z_view_keyexpr_t ping; z_view_keyexpr_from_str_unchecked(&ping, "test/ping"); z_owned_closure_sample_t respond; - z_closure(&respond, callback, drop, (void*)z_move(pub)); + z_closure(&respond, callback, drop, (void*)(&pub)); z_owned_subscriber_t sub; if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index 7c190d485..52bd4a962 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -80,9 +80,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); - z_bytes_drop(&first); - z_bytes_drop(&second); - z_bytes_drop(&kv); + z_bytes_drop(z_bytes_move(&first)); + z_bytes_drop(z_bytes_move(&second)); + z_bytes_drop(z_bytes_move(&kv)); kvp->current_idx++; } } @@ -97,8 +97,8 @@ void print_attachment(kv_pairs_rx_t *kvp) { void drop_attachment(kv_pairs_rx_t *kvp) { for (size_t i = 0; i < kvp->current_idx; i++) { - z_string_drop(&kvp->data[i].key); - z_string_drop(&kvp->data[i].value); + z_string_drop(z_string_move(&kvp->data[i].key)); + z_string_drop(z_string_move(&kvp->data[i].value)); } z_free(kvp->data); } diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index 6d7c5a8f3..598c1a7f1 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -46,9 +46,9 @@ void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); - z_bytes_drop(&first); - z_bytes_drop(&second); - z_bytes_drop(&kv); + z_bytes_drop(z_bytes_move(&first)); + z_bytes_drop(z_bytes_move(&second)); + z_bytes_drop(z_bytes_move(&kv)); kvp->current_idx++; } } @@ -63,8 +63,8 @@ void print_attachment(kv_pairs_t *kvp) { void drop_attachment(kv_pairs_t *kvp) { for (size_t i = 0; i < kvp->current_idx; i++) { - z_string_drop(&kvp->data[i].key); - z_string_drop(&kvp->data[i].value); + z_string_drop(z_string_move(&kvp->data[i].key)); + z_string_drop(z_string_move(&kvp->data[i].value)); } z_free(kvp->data); } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 9ff5b613e..0e3ae3c33 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -124,7 +124,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (value != NULL) { z_bytes_from_static_str(&payload, value); - opts.payload = &payload; + opts.payload = z_bytes_move(&payload); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 49da3f8a4..6cf66d7ab 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -26,7 +26,7 @@ void callback(const z_loaned_sample_t* sample, void* context) { void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; - z_undeclare_publisher(pub); + z_undeclare_publisher(z_publisher_move(pub)); // A note on lifetimes: // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still @@ -63,7 +63,7 @@ int main(int argc, char** argv) { z_view_keyexpr_t ping; z_view_keyexpr_from_str_unchecked(&ping, "test/ping"); z_owned_closure_sample_t respond; - z_closure_sample(&respond, callback, drop, (void*)z_publisher_move(&pub)); + z_closure_sample(&respond, callback, drop, (void*)(&pub)); z_owned_subscriber_t sub; if (z_declare_subscriber(&sub, z_session_loan(&session), z_view_keyexpr_loan(&ping), z_closure_sample_move(&respond), NULL) < 0) { diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 3101743ad..04864edc7 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -88,7 +88,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (value != NULL) { z_bytes_from_static_str(&payload, value); - opts.payload = &payload; + opts.payload = z_move(payload); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 44806c397..7bcc970ff 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) { } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; - z_drop(pub); + z_drop(z_move(*pub)); // A note on lifetimes: // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still @@ -59,7 +59,7 @@ int main(int argc, char** argv) { z_view_keyexpr_t ping; z_view_keyexpr_from_str_unchecked(&ping, "test/ping"); z_owned_closure_sample_t respond; - z_closure(&respond, callback, drop, (void*)z_move(pub)); + z_closure(&respond, callback, drop, (void*)(&pub)); z_owned_subscriber_t sub; if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index c082fce96..124024f37 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -83,7 +83,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { z_bytes_from_static_str(&payload, VALUE); - opts.payload = &payload; + opts.payload = z_move(payload); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index 489ba4605..57447acbc 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -31,16 +31,15 @@ extern "C" { #define _Z_CHANNEL_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \ collection_type, collection_new_f, collection_free_f, collection_push_f, \ collection_pull_f, collection_try_pull_f, collection_close_f, elem_owned_type, \ - elem_loaned_type, elem_clone_f, elem_drop_f, elem_null_f) \ + elem_loaned_type, elem_clone_f, elem_move_f, elem_drop_f, elem_null_f) \ typedef struct { \ collection_type *collection; \ } handler_type; \ \ _Z_OWNED_TYPE_VALUE(handler_type, handler_name) \ - _Z_LOANED_TYPE(handler_type, handler_name) \ \ static inline void _z_##handler_name##_elem_free(void **elem) { \ - elem_drop_f((elem_owned_type *)*elem); \ + elem_drop_f(elem_move_f((elem_owned_type *)*elem)); \ z_free(*elem); \ *elem = NULL; \ } \ @@ -140,20 +139,25 @@ extern "C" { /* elem_owned_type */ z_owned_##item_name##_t, \ /* elem_loaned_type */ z_loaned_##item_name##_t, \ /* elem_clone_f */ z_##item_name##_clone, \ + /* elem_move_f */ z_##item_name##_move, \ /* elem_drop_f */ z_##item_name##_drop, \ /* elem_null */ z_##item_name##_null) -#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \ - typedef struct { \ - uint8_t _foo; \ - } z_owned_##kind_name##_handler_##item_name##_t; \ - typedef struct { \ - uint8_t _foo; \ - } z_loaned_##kind_name##_handler_##item_name##_t; \ - void *z_##kind_name##_handler_##item_name##_loan(void); \ - void *z_##kind_name##_handler_##item_name##_move(void); \ - void *z_##kind_name##_handler_##item_name##_drop(void); \ - void *z_##kind_name##_handler_##item_name##_recv(void); \ +#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \ + typedef struct { \ + uint8_t _foo; \ + } z_owned_##kind_name##_handler_##item_name##_t; \ + typedef struct { \ + uint8_t _foo; \ + } z_loaned_##kind_name##_handler_##item_name##_t; \ + typedef struct { \ + z_owned_##kind_name##_handler_##item_name##_t *_ptr; \ + } z_moved_##kind_name##_handler_##item_name##_t; \ + void *z_##kind_name##_handler_##item_name##_loan(void); \ + void *z_##kind_name##_handler_##item_name##_move(void); \ + void *z_##kind_name##_handler_##item_name##_drop(void); \ + void *z_##kind_name##_handler_##item_name##_recv(void); \ + void *z_##kind_name##_handler_##item_name##_take(void); \ void *z_##kind_name##_handler_##item_name##_try_recv(void); // This macro defines: diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 31a93057e..43a20c729 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -96,35 +96,36 @@ * x: The instance to drop. */ #define z_drop(x) _Generic((x), \ - z_owned_keyexpr_t * : z_keyexpr_drop, \ - z_owned_config_t * : z_config_drop, \ - z_owned_session_t * : z_session_drop, \ - z_owned_subscriber_t * : z_subscriber_drop, \ - z_owned_publisher_t * : z_publisher_drop, \ - z_owned_queryable_t * : z_queryable_drop, \ - z_owned_reply_t * : z_reply_drop, \ - z_owned_hello_t * : z_hello_drop, \ - z_owned_string_t * : z_string_drop, \ - z_owned_string_array_t * : z_string_array_drop, \ - z_owned_sample_t * : z_sample_drop, \ - z_owned_query_t * : z_query_drop, \ - z_owned_encoding_t * : z_encoding_drop, \ - z_owned_slice_t *: z_slice_drop, \ - z_owned_bytes_t *: z_bytes_drop, \ - z_owned_closure_sample_t * : z_closure_sample_drop, \ - z_owned_closure_query_t * : z_closure_query_drop, \ - z_owned_closure_reply_t * : z_closure_reply_drop, \ - z_owned_closure_hello_t * : z_closure_hello_drop, \ - z_owned_closure_zid_t * : z_closure_zid_drop, \ - z_owned_mutex_t *: z_mutex_drop, \ - z_owned_condvar_t *: z_condvar_drop, \ - z_owned_fifo_handler_query_t * : z_fifo_handler_query_drop, \ - z_owned_fifo_handler_reply_t * : z_fifo_handler_reply_drop, \ - z_owned_fifo_handler_sample_t * : z_fifo_handler_sample_drop, \ - z_owned_ring_handler_query_t * : z_ring_handler_query_drop, \ - z_owned_ring_handler_reply_t * : z_ring_handler_reply_drop, \ - z_owned_ring_handler_sample_t * : z_ring_handler_sample_drop, \ - z_owned_reply_err_t * : z_reply_err_drop \ + z_moved_keyexpr_t : z_keyexpr_drop, \ + z_moved_config_t : z_config_drop, \ + z_moved_session_t : z_session_drop, \ + z_moved_subscriber_t : z_subscriber_drop, \ + z_moved_publisher_t : z_publisher_drop, \ + z_moved_queryable_t : z_queryable_drop, \ + z_moved_reply_t : z_reply_drop, \ + z_moved_hello_t : z_hello_drop, \ + z_moved_string_t : z_string_drop, \ + z_moved_string_array_t : z_string_array_drop, \ + z_moved_sample_t : z_sample_drop, \ + z_moved_query_t : z_query_drop, \ + z_moved_encoding_t : z_encoding_drop, \ + z_moved_slice_t : z_slice_drop, \ + z_moved_bytes_t : z_bytes_drop, \ + z_moved_closure_sample_t : z_closure_sample_drop, \ + z_moved_closure_query_t : z_closure_query_drop, \ + z_moved_closure_reply_t : z_closure_reply_drop, \ + z_moved_closure_hello_t : z_closure_hello_drop, \ + z_moved_closure_zid_t : z_closure_zid_drop, \ + z_moved_task_t : z_task_join, \ + z_moved_mutex_t : z_mutex_drop, \ + z_moved_condvar_t : z_condvar_drop, \ + z_moved_fifo_handler_query_t : z_fifo_handler_query_drop, \ + z_moved_fifo_handler_reply_t : z_fifo_handler_reply_drop, \ + z_moved_fifo_handler_sample_t : z_fifo_handler_sample_drop, \ + z_moved_ring_handler_query_t : z_ring_handler_query_drop, \ + z_moved_ring_handler_reply_t : z_ring_handler_reply_drop, \ + z_moved_ring_handler_sample_t : z_ring_handler_sample_drop, \ + z_moved_reply_err_t : z_reply_err_drop \ )(x) /** @@ -139,7 +140,6 @@ #define z_check(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_check, \ - z_view_keyexpr_t : z_view_keyexpr_check, \ z_owned_reply_err_t : z_reply_err_check, \ z_owned_config_t : z_config_check, \ z_owned_session_t : z_session_check, \ @@ -149,8 +149,12 @@ z_owned_reply_t : z_reply_check, \ z_owned_hello_t : z_hello_check, \ z_owned_string_t : z_string_check, \ - z_view_string_t : z_view_string_check, \ z_owned_string_array_t : z_string_array_check, \ + z_owned_closure_sample_t : z_closure_sample_check, \ + z_owned_closure_query_t : z_closure_query_check, \ + z_owned_closure_reply_t : z_closure_reply_check, \ + z_owned_closure_hello_t : z_closure_hello_check, \ + z_owned_closure_zid_t : z_closure_zid_check, \ z_owned_slice_t : z_slice_check, \ z_owned_bytes_t : z_bytes_check, \ z_owned_sample_t : z_sample_check, \ @@ -234,6 +238,49 @@ z_owned_reply_err_t : z_reply_err_move \ )(&x) +/** + * Defines a generic function for extracting the ``z_owned_X_t`` type from ``z_moved_X_t`` + * + * Parameters: + * x: The pointer to destinaton ``z_owned_X_t`` + * src: The source ``z_moved_X_t`` + * + * Returns: + * Returns the instance associated with `x`. + */ +#define z_take(this_, x) \ + _Generic((this_), \ + z_owned_bytes_t *: z_bytes_take, \ + z_owned_closure_hello_t *: z_closure_hello_take, \ + z_owned_closure_query_t *: z_closure_query_take, \ + z_owned_closure_reply_t *: z_closure_reply_take, \ + z_owned_closure_sample_t *: z_closure_sample_take, \ + z_owned_closure_zid_t * : z_closure_zid_take, \ + z_owned_condvar_t *: z_condvar_take, \ + z_owned_config_t *: z_config_take, \ + z_owned_encoding_t *: z_encoding_take, \ + z_owned_fifo_handler_query_t *: z_fifo_handler_query_take, \ + z_owned_fifo_handler_reply_t *: z_fifo_handler_reply_take, \ + z_owned_fifo_handler_sample_t *: z_fifo_handler_sample_take,\ + z_owned_hello_t *: z_hello_take, \ + z_owned_keyexpr_t *: z_keyexpr_take, \ + z_owned_mutex_t *: z_mutex_take, \ + z_owned_publisher_t *: z_publisher_take, \ + z_owned_query_t *: z_query_take, \ + z_owned_queryable_t *: z_queryable_take, \ + z_owned_reply_t *: z_reply_take, \ + z_owned_reply_err_t *: z_reply_err_take, \ + z_owned_ring_handler_query_t *: z_ring_handler_query_take, \ + z_owned_ring_handler_reply_t *: z_ring_handler_reply_take, \ + z_owned_ring_handler_sample_t *: z_ring_handler_sample_take,\ + z_owned_sample_t *: z_sample_take, \ + z_owned_session_t *: z_session_take, \ + z_owned_slice_t *: z_slice_take, \ + z_owned_string_array_t *: z_string_array_take, \ + z_owned_string_t *: z_string_take, \ + z_owned_subscriber_t *: z_subscriber_take \ + )(this_, x) + /** * Defines a generic function for cloning any of the ``z_owned_X_t`` types. * @@ -264,8 +311,8 @@ /** * Defines a generic function for making null object of any of the ``z_owned_X_t`` types. * - * Returns: - * Returns the uninitialized instance of `x`. + * Parameters: + * x: The instance to nullify. */ #define z_null(x) _Generic((x), \ z_owned_session_t * : z_session_null, \ @@ -278,6 +325,7 @@ z_owned_reply_t * : z_reply_null, \ z_owned_hello_t * : z_hello_null, \ z_owned_string_t * : z_string_null, \ + z_owned_string_array_t * : z_string_array_null, \ z_owned_slice_t *: z_slice_null, \ z_owned_bytes_t *: z_bytes_null, \ z_owned_closure_sample_t * : z_closure_sample_null, \ @@ -287,7 +335,10 @@ z_owned_closure_zid_t * : z_closure_zid_null, \ z_owned_sample_t * : z_sample_null, \ z_owned_encoding_t * : z_encoding_null, \ - z_owned_reply_err_t * : z_reply_err_null \ + z_owned_reply_err_t * : z_reply_err_null, \ + z_owned_task_t * : z_task_null, \ + z_owned_mutex_t * : z_mutex_null, \ + z_owned_condvar_t *: z_condvar_null \ )(x) // clang-format on @@ -434,7 +485,6 @@ inline void z_null(z_owned_fifo_handler_sample_t* v) { return z_fifo_handler_sam 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); } inline bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); } -inline bool z_check(const z_view_keyexpr_t& v) { return z_view_keyexpr_check(&v); } inline bool z_check(const z_owned_config_t& v) { return z_config_check(&v); } inline bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } inline bool z_check(const z_owned_queryable_t& v) { return z_queryable_check(&v); } @@ -442,7 +492,6 @@ inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); } inline bool z_check(const z_owned_query_t& v) { return z_query_check(&v); } inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); } inline bool z_check(const z_owned_string_t& v) { return z_string_check(&v); } -inline bool z_check(const z_view_string_t& v) { return z_view_string_check(&v); } inline bool z_check(const z_owned_sample_t& v) { return z_sample_check(&v); } inline bool z_check(const z_owned_bytes_t& v) { return z_bytes_check(&v); } inline bool z_check(const z_owned_encoding_t& v) { return z_encoding_check(&v); } diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 59092d69c..3281e4f2b 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -27,19 +27,28 @@ // // !!! FOR INTERNAL USAGE ONLY !!! +#define _Z_MOVED_TYPE(name) \ + typedef struct { \ + z_owned_##name##_t *_ptr; \ + } z_moved_##name##_t; + +#define _Z_LOANED_TYPE(type, name) typedef type z_loaned_##name##_t; + // For value types #define _Z_OWNED_TYPE_VALUE(type, name) \ typedef struct { \ type _val; \ - } z_owned_##name##_t; + } z_owned_##name##_t; \ + _Z_LOANED_TYPE(type, name) \ + _Z_MOVED_TYPE(name) // For refcounted types #define _Z_OWNED_TYPE_RC(type, name) \ typedef struct { \ type _rc; \ - } z_owned_##name##_t; - -#define _Z_LOANED_TYPE(type, name) typedef type z_loaned_##name##_t; + } z_owned_##name##_t; \ + _Z_LOANED_TYPE(type, name) \ + _Z_MOVED_TYPE(name) #define _Z_VIEW_TYPE(type, name) \ typedef struct { \ @@ -50,50 +59,88 @@ _Bool z_##name##_check(const z_owned_##name##_t *obj); \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); \ + z_moved_##name##_t z_##name##_move(z_owned_##name##_t *obj); \ + void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src); \ int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src); \ - void z_##name##_drop(z_owned_##name##_t *obj); \ + void z_##name##_drop(z_moved_##name##_t obj); \ void z_##name##_null(z_owned_##name##_t *obj); #define _Z_OWNED_FUNCTIONS_NO_COPY_DEF(name) \ _Bool z_##name##_check(const z_owned_##name##_t *obj); \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); \ - void z_##name##_drop(z_owned_##name##_t *obj); \ + z_moved_##name##_t z_##name##_move(z_owned_##name##_t *obj); \ + void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src); \ + void z_##name##_drop(z_moved_##name##_t obj); \ void z_##name##_null(z_owned_##name##_t *obj); #define _Z_OWNED_FUNCTIONS_SYSTEM_DEF(name) \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); + z_moved_##name##_t z_##name##_move(z_owned_##name##_t *obj); \ + void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src); \ + void z_##name##_null(z_owned_##name##_t *obj); #define _Z_VIEW_FUNCTIONS_DEF(name) \ - _Bool z_view_##name##_check(const z_view_##name##_t *obj); \ + _Bool z_view_##name##_is_empty(const z_view_##name##_t *obj); \ const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *name); \ z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *name); \ - void z_view_##name##_null(z_view_##name##_t *name); + void z_view_##name##_empty(z_view_##name##_t *name); + +#define _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ + z_moved_##name##_t z_##name##_move(z_owned_##name##_t *obj) { return (z_moved_##name##_t){obj}; } \ + void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src) { \ + *obj = *src._ptr; \ + z_##name##_null(src._ptr); \ + } + +#define _Z_OWNED_FUNCTIONS_PTR_IMPL(type, name, f_copy, f_free) \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj->_val != NULL; } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return obj->_val; } \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return obj->_val; } \ + void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = NULL; } \ + int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + int8_t ret = _Z_RES_OK; \ + obj->_val = (type *)z_malloc(sizeof(type)); \ + if (obj->_val != NULL) { \ + f_copy(obj->_val, src); \ + } else { \ + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \ + } \ + return ret; \ + } \ + void z_##name##_drop(z_moved_##name##_t obj) { \ + if ((obj._ptr != NULL) && (obj._ptr->_val != NULL)) { \ + f_free(&obj._ptr->_val); \ + } \ + } #define _Z_OWNED_FUNCTIONS_VALUE_IMPL(type, name, f_check, f_null, f_copy, f_drop) \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ _Bool z_##name##_check(const z_owned_##name##_t *obj) { return f_check((&obj->_val)); } \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return &obj->_val; } \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return &obj->_val; } \ void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = f_null(); } \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } \ int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ return f_copy((&obj->_val), src); \ } \ - void z_##name##_drop(z_owned_##name##_t *obj) { \ - if (obj != NULL) f_drop((&obj->_val)); \ + void z_##name##_drop(z_moved_##name##_t obj) { \ + if (obj._ptr != NULL) f_drop((&obj._ptr->_val)); \ } -#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_INNER(type, name, f_check, f_null, f_drop, attribute) \ - attribute _Bool z_##name##_check(const z_owned_##name##_t *obj) { return f_check((&obj->_val)); } \ - attribute const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return &obj->_val; } \ - attribute z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return &obj->_val; } \ - attribute void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = f_null(); } \ - attribute z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } \ - attribute void z_##name##_drop(z_owned_##name##_t *obj) { \ - if (obj != NULL) f_drop((&obj->_val)); \ + +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_INNER(type, name, f_check, f_null, f_drop, attribute) \ + attribute _Bool z_##name##_check(const z_owned_##name##_t *obj) { return f_check((&obj->_val)); } \ + attribute const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return &obj->_val; } \ + attribute z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return &obj->_val; } \ + attribute void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = f_null(); } \ + attribute z_moved_##name##_t z_##name##_move(z_owned_##name##_t *obj) { return (z_moved_##name##_t){obj}; } \ + attribute void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src) { \ + *obj = *src._ptr; \ + z_##name##_null(src._ptr); \ + } \ + attribute void z_##name##_drop(z_moved_##name##_t obj) { \ + if (obj._ptr != NULL) f_drop((&obj._ptr->_val)); \ } #define _ZP_NOTHING @@ -105,11 +152,11 @@ _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_INNER(type, name, f_check, f_null, f_drop, static inline) #define _Z_OWNED_FUNCTIONS_RC_IMPL(name) \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ _Bool z_##name##_check(const z_owned_##name##_t *val) { return !_Z_RC_IS_NULL(&val->_rc); } \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val) { return &val->_rc; } \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *val) { return &val->_rc; } \ void z_##name##_null(z_owned_##name##_t *val) { val->_rc = _z_##name##_rc_null(); } \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ int8_t ret = _Z_RES_OK; \ obj->_rc = _z_##name##_rc_clone((z_loaned_##name##_t *)src); \ @@ -118,39 +165,43 @@ } \ return ret; \ } \ - void z_##name##_drop(z_owned_##name##_t *val) { \ - if (!_Z_RC_IS_NULL(&val->_rc)) { \ - _z_##name##_rc_drop(&val->_rc); \ + void z_##name##_drop(z_moved_##name##_t val) { \ + if (!_Z_RC_IS_NULL(&val._ptr->_rc)) { \ + _z_##name##_rc_drop(&val._ptr->_rc); \ } \ } #define _Z_OWNED_FUNCTIONS_SYSTEM_IMPL(type, name) \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj != NULL; } \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return &obj->_val; } \ z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return &obj->_val; } \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } + void z_##name##_null(z_owned_##name##_t *obj) { (void)obj; } -#define _Z_VIEW_FUNCTIONS_IMPL(type, name, f_check) \ - _Bool z_view_##name##_check(const z_view_##name##_t *obj) { return f_check((&obj->_val)); } \ +#define _Z_VIEW_FUNCTIONS_IMPL(type, name, f_check, f_null) \ + _Bool z_view_##name##_is_empty(const z_view_##name##_t *obj) { return !f_check((&obj->_val)); } \ const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *obj) { return &obj->_val; } \ - z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; } + z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; } \ + void z_view_##name##_empty(z_view_##name##_t *obj) { obj->_val = f_null(); } #define _Z_OWNED_FUNCTIONS_CLOSURE_DEF(name) \ _Bool z_##name##_check(const z_owned_##name##_t *val); \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val); \ - void z_##name##_drop(z_owned_##name##_t *val); \ + z_moved_##name##_t z_##name##_move(z_owned_##name##_t *val); \ + void z_##name##_take(z_owned_##name##_t *obj, z_moved_##name##_t src); \ + void z_##name##_drop(z_moved_##name##_t val); \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val); \ void z_##name##_null(z_owned_##name##_t *name); #define _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(name, f_call, f_drop) \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ _Bool z_##name##_check(const z_owned_##name##_t *val) { return val->_val.call != NULL; } \ - z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ - void z_##name##_drop(z_owned_##name##_t *val) { \ - if (val->_val.drop != NULL) { \ - (val->_val.drop)(val->_val.context); \ - val->_val.drop = NULL; \ + void z_##name##_drop(z_moved_##name##_t val) { \ + if (val._ptr->_val.drop != NULL) { \ + (val._ptr->_val.drop)(val._ptr->_val.context); \ + val._ptr->_val.drop = NULL; \ } \ - val->_val.call = NULL; \ - val->_val.context = NULL; \ + val._ptr->_val.call = NULL; \ + val._ptr->_val.context = NULL; \ } \ void z_##name##_null(z_owned_##name##_t *val) { \ val->_val.call = NULL; \ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index d89ebd905..1843fcef6 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -32,18 +32,6 @@ extern "C" { #endif -/********* Data Types Handlers *********/ -/** - * Builds an empty :c:type:`z_view_string_t`. - * - * Parameters: - * str: Pointer to an uninitialized :c:type:`z_view_string_t`. - * - * Return: - * ``0`` if creation successful, ``negative value`` otherwise. - */ -int8_t z_view_string_empty(z_view_string_t *str); - /** * Builds a :c:type:`z_view_string_t` by wrapping a ``const char *`` string. * @@ -799,7 +787,7 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const z_loaned_slice * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_owned_slice_t *slice); +int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_moved_slice_t slice); /** * Encodes data into a :c:type:`z_owned_bytes_t`. @@ -866,7 +854,7 @@ int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const z_loaned_stri * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_owned_string_t *s); +int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_moved_string_t s); /** * Encodes a null-terminated string into a :c:type:`z_owned_bytes_t`. @@ -924,13 +912,13 @@ int8_t z_bytes_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_ * * Parameters: * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded pair. - * first: Pointer to the first `z_owned_bytes` to encode. - * second: Pointer to the second `z_owned_bytes` to encode. + * first: Moved first `z_owned_bytes` to encode. + * second: Moved second `z_owned_bytes` to encode. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); +int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_moved_bytes_t first, z_moved_bytes_t second); /** * Parameters: @@ -1081,7 +1069,7 @@ int8_t z_bytes_writer_write_all(z_bytes_writer_t *writer, const uint8_t *src, si * Return: * 0 in case of success, negative error code otherwise */ -int8_t z_bytes_writer_append(z_bytes_writer_t *writer, z_owned_bytes_t *bytes); +int8_t z_bytes_writer_append(z_bytes_writer_t *writer, z_moved_bytes_t bytes); /** * Appends bytes, with boundaries information. It would allow to read the same piece of data using @@ -1094,7 +1082,7 @@ int8_t z_bytes_writer_append(z_bytes_writer_t *writer, z_owned_bytes_t *bytes); * Return: * 0 in case of success, negative error code otherwise */ -int8_t z_bytes_writer_append_bounded(z_bytes_writer_t *writer, z_owned_bytes_t *bytes); +int8_t z_bytes_writer_append_bounded(z_bytes_writer_t *writer, z_moved_bytes_t bytes); /** * Create timestamp. @@ -1482,37 +1470,37 @@ int8_t z_whatami_to_view_string(z_whatami_t whatami, z_view_string_t *str_out); * Scouts for other Zenoh entities like routers and/or peers. * * Parameters: - * config: Pointer to a moved :c:type:`z_owned_config_t` to configure the scouting with. - * callback: Pointer to a moved :c:type:`z_owned_closure_hello_t` callback. + * config: Moved :c:type:`z_owned_config_t` to configure the scouting with. + * callback: Moved :c:type:`z_owned_closure_hello_t` callback. * options: Pointer to a :c:type:`z_scout_options_t` to configure the operation. * * Return: * ``0`` if scouting successfully triggered, ``negative value`` otherwise. */ -int8_t z_scout(z_owned_config_t *config, z_owned_closure_hello_t *callback, const z_scout_options_t *options); +int8_t z_scout(z_moved_config_t config, z_moved_closure_hello_t callback, const z_scout_options_t *options); /** * Opens a Zenoh session. * * Parameters: * zs: Pointer to an uninitialized :c:type:`z_owned_session_t` to store the session info. - * config: Pointer to a moved :c:type:`z_owned_config_t` to configure the session with. + * config: Moved :c:type:`z_owned_config_t` to configure the session with. * * Return: * ``0`` if open successful, ``negative value`` otherwise. */ -int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config); +int8_t z_open(z_owned_session_t *zs, z_moved_config_t config); /** * Closes a Zenoh session. * * Parameters: - * zs: Pointer to a moved :c:type:`z_owned_session_t` to close. + * zs: Moved :c:type:`z_owned_session_t` to close. * * Return: * ``0`` if close successful, ``negative value`` otherwise. */ -int8_t z_close(z_owned_session_t *zs); +int8_t z_close(z_moved_session_t zs); /** * Fetches Zenoh IDs of all connected peers. @@ -1522,12 +1510,12 @@ int8_t z_close(z_owned_session_t *zs); * * Parameters: * zs: Pointer to :c:type:`z_loaned_session_t` to fetch peer id from. - * callback: Pointer to a moved :c:type:`z_owned_closure_zid_t` callback. + * callback: Moved :c:type:`z_owned_closure_zid_t` callback. * * Return: * ``0`` if operation successfully triggered, ``negative value`` otherwise. */ -int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback); +int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t callback); /** * Fetches Zenoh IDs of all connected routers. @@ -1537,12 +1525,12 @@ int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *cal * * Parameters: * zs: Pointer to a :c:type:`z_loaned_session_t` to fetch router id from. - * callback: Pointer to a moved :c:type:`z_owned_closure_zid_t` callback. + * callback: Moved :c:type:`z_owned_closure_zid_t` callback. * * Return: * ``0`` if operation successfully triggered, ``negative value`` otherwise. */ -int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback); +int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t callback); /** * Gets the local Zenoh ID associated to a given Zenoh session. @@ -1691,13 +1679,13 @@ void z_delete_options_default(z_delete_options_t *options); * Parameters: * zs: Pointer to a :c:type:`z_loaned_session_t` to put the data through. * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to put the data for. - * payload: Pointer to a moved :c:type:`z_owned_bytes_t` containing the data to put. + * payload: Moved :c:type:`z_owned_bytes_t` containing the data to put. * options: Pointer to a :c:type:`z_put_options_t` to configure the operation. * * Return: * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_moved_bytes_t payload, const z_put_options_t *options); /** @@ -1742,12 +1730,12 @@ int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *z * Undeclares a publisher. * * Parameters: - * pub: Pointer to a moved :c:type:`z_owned_publisher_t` to undeclare. + * pub: Moved :c:type:`z_owned_publisher_t` to undeclare. * * Return: * ``0`` if undeclare successful, ``negative value`` otherwise. */ -int8_t z_undeclare_publisher(z_owned_publisher_t *pub); +int8_t z_undeclare_publisher(z_moved_publisher_t pub); z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher); @@ -1772,13 +1760,13 @@ void z_publisher_delete_options_default(z_publisher_delete_options_t *options); * * Parameters: * pub: Pointer to a :c:type:`z_loaned_publisher_t` from where to put the data. - * payload: Pointer to a moved :c:type:`z_owned_bytes_t` containing the data to put. + * payload: Moved :c:type:`z_owned_bytes_t` containing the data to put. * options: Pointer to a :c:type:`z_publisher_put_options_t` to configure the operation. * * Return: * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_moved_bytes_t payload, const z_publisher_put_options_t *options); /** @@ -1810,14 +1798,14 @@ void z_get_options_default(z_get_options_t *options); * zs: Pointer to a :c:type:`z_loaned_session_t` to send the query through. * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to send the query for. * parameters: Pointer to the parameters as a null-terminated string. - * callback: Pointer to a :c:type:`z_owned_closure_reply_t` callback. + * callback: Moved :c:type:`z_owned_closure_reply_t` callback. * options: Pointer to a :c:type:`z_get_options_t` to configure the operation. * * Return: * ``0`` if put operation successful, ``negative value`` otherwise. */ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const char *parameters, - z_owned_closure_reply_t *callback, z_get_options_t *options); + z_moved_closure_reply_t callback, z_get_options_t *options); /** * Checks if queryable answered with an OK, which allows this value to be treated as a sample. * @@ -1890,19 +1878,19 @@ void z_queryable_options_default(z_queryable_options_t *options); * ``0`` if declare operation successful, ``negative value`` otherwise. */ int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_session_t *zs, - const z_loaned_keyexpr_t *keyexpr, z_owned_closure_query_t *callback, + const z_loaned_keyexpr_t *keyexpr, z_moved_closure_query_t callback, const z_queryable_options_t *options); /** * Undeclares a queryable. * * Parameters: - * queryable: Pointer to a :c:type:`z_owned_queryable_t` to undeclare. + * queryable: Moved :c:type:`z_owned_queryable_t` to undeclare. * * Return: * ``0`` if undeclare operation successful, ``negative value`` otherwise. */ -int8_t z_undeclare_queryable(z_owned_queryable_t *queryable); +int8_t z_undeclare_queryable(z_moved_queryable_t queryable); /** * Builds a :c:type:`z_query_reply_options_t` with default values. @@ -1929,7 +1917,7 @@ void z_query_reply_options_default(z_query_reply_options_t *options); * Return: * ``0`` if reply operation successful, ``negative value`` otherwise. */ -int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_moved_bytes_t payload, const z_query_reply_options_t *options); /** @@ -1977,13 +1965,13 @@ void z_query_reply_err_options_default(z_query_reply_err_options_t *options); * * Parameters: * query: Pointer to a :c:type:`z_loaned_query_t` to reply. - * payload: Pointer to the reply error data. + * payload: Moved reply error data payload. * options: Pointer to a :c:type:`z_query_reply_err_options_t` to configure the reply error. * * Return: * ``0`` if reply operation successful, ``negative value`` otherwise. */ -int8_t z_query_reply_err(const z_loaned_query_t *query, z_owned_bytes_t *payload, +int8_t z_query_reply_err(const z_loaned_query_t *query, z_moved_bytes_t payload, const z_query_reply_err_options_t *options); #endif @@ -2060,13 +2048,13 @@ int8_t z_declare_keyexpr(z_owned_keyexpr_t *declared_keyexpr, const z_loaned_ses * Undeclares a keyexpr. * * Parameters: - * keyexpr: Pointer to a moved :c:type:`z_owned_keyexpr_t` to undeclare. + * keyexpr: Moved :c:type:`z_owned_keyexpr_t` to undeclare. * zs: Pointer to a :c:type:`z_loaned_session_t` to undeclare the data through. * * Return: * ``0`` if undeclare successful, ``negative value`` otherwise. */ -int8_t z_undeclare_keyexpr(z_owned_keyexpr_t *keyexpr, const z_loaned_session_t *zs); +int8_t z_undeclare_keyexpr(z_moved_keyexpr_t keyexpr, const z_loaned_session_t *zs); #if Z_FEATURE_SUBSCRIPTION == 1 /** @@ -2091,18 +2079,18 @@ void z_subscriber_options_default(z_subscriber_options_t *options); * ``0`` if declare successful, ``negative value`` otherwise. */ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, - z_owned_closure_sample_t *callback, const z_subscriber_options_t *options); + z_moved_closure_sample_t callback, const z_subscriber_options_t *options); /** * Undeclares the subscriber. * * Parameters: - * sub: Pointer to a :c:type:`z_owned_subscriber_t` to undeclare. + * sub: Moved :c:type:`z_owned_subscriber_t` to undeclare. * * Return: * ``0`` if undeclare successful, ``negative value`` otherwise. */ -int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub); +int8_t z_undeclare_subscriber(z_moved_subscriber_t sub); /** * Copies the keyexpr of a subscriber diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 267b33923..68cd9e8f4 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -64,13 +64,11 @@ typedef _z_timestamp_t z_timestamp_t; * uint8_t *start: A pointer to the bytes array. */ _Z_OWNED_TYPE_VALUE(_z_slice_t, slice) -_Z_LOANED_TYPE(_z_slice_t, slice) /** * Represents a container for slices. */ _Z_OWNED_TYPE_VALUE(_z_bytes_t, bytes) -_Z_LOANED_TYPE(_z_bytes_t, bytes) /** * Represents a writer for serialized data. @@ -95,7 +93,6 @@ typedef _z_bytes_iterator_t z_bytes_reader_t; * const char *val: A pointer to the string. */ _Z_OWNED_TYPE_VALUE(_z_string_t, string) -_Z_LOANED_TYPE(_z_string_t, string) _Z_VIEW_TYPE(_z_string_t, string) /** @@ -107,7 +104,6 @@ _Z_VIEW_TYPE(_z_string_t, string) * - :c:func:`z_keyexpr_as_view_string` */ _Z_OWNED_TYPE_VALUE(_z_keyexpr_t, keyexpr) -_Z_LOANED_TYPE(_z_keyexpr_t, keyexpr) _Z_VIEW_TYPE(_z_keyexpr_t, keyexpr) /** @@ -121,13 +117,11 @@ _Z_VIEW_TYPE(_z_keyexpr_t, keyexpr) * - :c:func:`zp_config_insert` */ _Z_OWNED_TYPE_VALUE(_z_config_t, config) -_Z_LOANED_TYPE(_z_config_t, config) /** * Represents a Zenoh Session. */ _Z_OWNED_TYPE_RC(_z_session_rc_t, session) -_Z_LOANED_TYPE(_z_session_rc_t, session) /** * Represents a Zenoh Subscriber entity. @@ -138,7 +132,6 @@ _Z_LOANED_TYPE(_z_session_rc_t, session) * - :c:func:`z_undeclare_subscriber` */ _Z_OWNED_TYPE_VALUE(_z_subscriber_t, subscriber) -_Z_LOANED_TYPE(_z_subscriber_t, subscriber) /** * Represents a Zenoh Publisher entity. @@ -151,7 +144,6 @@ _Z_LOANED_TYPE(_z_subscriber_t, subscriber) * - :c:func:`z_publisher_delete` */ _Z_OWNED_TYPE_VALUE(_z_publisher_t, publisher) -_Z_LOANED_TYPE(_z_publisher_t, publisher) /** * Represents a Zenoh Queryable entity. @@ -162,14 +154,12 @@ _Z_LOANED_TYPE(_z_publisher_t, publisher) * - :c:func:`z_undeclare_queryable` */ _Z_OWNED_TYPE_VALUE(_z_queryable_t, queryable) -_Z_LOANED_TYPE(_z_queryable_t, queryable) /** * Represents a Zenoh Query entity, received by Zenoh Queryable entities. * */ _Z_OWNED_TYPE_RC(_z_query_rc_t, query) -_Z_LOANED_TYPE(_z_query_rc_t, query) /** * Represents the encoding of a payload, in a MIME-like format. @@ -179,7 +169,6 @@ _Z_LOANED_TYPE(_z_query_rc_t, query) * z_loaned_slice_t* suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. */ _Z_OWNED_TYPE_VALUE(_z_encoding_t, encoding) -_Z_LOANED_TYPE(_z_encoding_t, encoding) /** * Represents a Zenoh reply error. @@ -189,7 +178,6 @@ _Z_LOANED_TYPE(_z_encoding_t, encoding) * z_loaned_bytes_t* payload: The payload of this zenoh reply error. */ _Z_OWNED_TYPE_VALUE(_z_value_t, reply_err) -_Z_LOANED_TYPE(_z_value_t, reply_err) /** * Represents the configuration used to configure a subscriber upon declaration :c:func:`z_declare_subscriber`. @@ -222,7 +210,7 @@ typedef struct { * _Bool is_express: If true, Zenoh will not wait to batch this operation with others to reduce the bandwidth. */ typedef struct { - z_owned_encoding_t *encoding; + z_moved_encoding_t encoding; z_congestion_control_t congestion_control; z_priority_t priority; _Bool is_express; @@ -242,20 +230,20 @@ typedef struct { * Represents the configuration used to configure a query reply sent via :c:func:`z_query_reply. * * Members: - * z_owned_encoding_t *encoding: The encoding of the payload. + * z_moved_encoding_t encoding: The encoding of the payload. * z_congestion_control_t congestion_control: The congestion control to apply when routing this message. * z_priority_t priority: The priority of this message when routed. * z_timestamp_t *timestamp: The API level timestamp (e.g. of the data when it was created). * _Bool is_express: If true, Zenoh will not wait to batch this operation with others to reduce the bandwidth. - * z_owned_bytes_t *attachment: An optional attachment to the response. + * z_moved_bytes_t attachment: An optional attachment to the response. */ typedef struct { - z_owned_encoding_t *encoding; + z_moved_encoding_t encoding; z_congestion_control_t congestion_control; z_priority_t priority; z_timestamp_t *timestamp; _Bool is_express; - z_owned_bytes_t *attachment; + z_moved_bytes_t attachment; } z_query_reply_options_t; /** @@ -266,44 +254,44 @@ typedef struct { * z_priority_t priority: The priority of this message when routed. * z_timestamp_t *timestamp: The API level timestamp (e.g. of the data when it was created). * _Bool is_express: If true, Zenoh will not wait to batch this operation with others to reduce the bandwidth. - * z_owned_bytes_t *attachment: An optional attachment to the response. + * z_moved_bytes_t attachment: An optional attachment to the response. */ typedef struct { z_congestion_control_t congestion_control; z_priority_t priority; z_timestamp_t *timestamp; _Bool is_express; - z_owned_bytes_t *attachment; + z_moved_bytes_t attachment; } z_query_reply_del_options_t; /** * Represents the configuration used to configure a query reply error sent via :c:func:`z_query_reply_err. * * Members: - * z_owned_encoding_t *encoding: The encoding of the payload. + * z_moved_encoding_t encoding: The encoding of the payload. */ typedef struct { - z_owned_encoding_t *encoding; + z_moved_encoding_t encoding; } z_query_reply_err_options_t; /** * Represents the configuration used to configure a put operation sent via via :c:func:`z_put`. * * Members: - * z_owned_encoding_t *encoding: The encoding of the payload. + * z_moved_encoding_t encoding: The encoding of the payload. * z_congestion_control_t congestion_control: The congestion control to apply when routing this message. * z_priority_t priority: The priority of this message when routed. * z_timestamp_t *timestamp: The API level timestamp (e.g. of the data when it was created). * _Bool is_express: If true, Zenoh will not wait to batch this operation with others to reduce the bandwidth. - * z_owned_bytes_t *attachment: An optional attachment to the publication. + * z_moved_bytes_t attachment: An optional attachment to the publication. */ typedef struct { - z_owned_encoding_t *encoding; + z_moved_encoding_t encoding; z_congestion_control_t congestion_control; z_priority_t priority; z_timestamp_t *timestamp; _Bool is_express; - z_owned_bytes_t *attachment; + z_moved_bytes_t attachment; } z_put_options_t; @@ -328,14 +316,14 @@ typedef struct { * sent via :c:func:`z_publisher_put`. * * Members: - * z_owned_encoding_t *encoding: The encoding of the payload. + * z_moved_encoding_t encoding: The encoding of the payload. * z_timestamp_t *timestamp: The API level timestamp (e.g. of the data when it was created). - * z_owned_bytes_t *attachment: An optional attachment to the publication. + * z_moved_bytes_t attachment: An optional attachment to the publication. */ typedef struct { - z_owned_encoding_t *encoding; + z_moved_encoding_t encoding; z_timestamp_t *timestamp; - z_owned_bytes_t *attachment; + z_moved_bytes_t attachment; } z_publisher_put_options_t; /** @@ -353,25 +341,25 @@ typedef struct { * Represents the configuration used to configure a get operation sent via :c:func:`z_get`. * * Members: - * z_owned_bytes_t payload: The payload to include in the query. - * z_owned_encoding_t *encoding: Payload encoding. + * z_moved_bytes_t payload: The payload to include in the query. + * z_moved_encoding_t encoding: Payload encoding. * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. * z_congestion_control_t congestion_control: The congestion control to apply when routing the query. * z_priority_t priority: The priority of the query. * _Bool is_express: If true, Zenoh will not wait to batch this operation with others to reduce the bandwidth. * z_query_target_t target: The queryables that should be targeted by this get. - * z_owned_bytes_t *attachment: An optional attachment to the query. + * z_moved_bytes_t attachment: An optional attachment to the query. */ typedef struct { - z_owned_bytes_t *payload; - z_owned_encoding_t *encoding; + z_moved_bytes_t payload; + z_moved_encoding_t encoding; z_query_consolidation_t consolidation; z_congestion_control_t congestion_control; z_priority_t priority; _Bool is_express; z_query_target_t target; uint32_t timeout_ms; - z_owned_bytes_t *attachment; + z_moved_bytes_t attachment; } z_get_options_t; /** @@ -466,7 +454,6 @@ static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } * */ _Z_OWNED_TYPE_VALUE(_z_sample_t, sample) -_Z_LOANED_TYPE(_z_sample_t, sample) /** * Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. @@ -477,13 +464,11 @@ _Z_LOANED_TYPE(_z_sample_t, sample) * z_loaned_string_array_t locators: The locators of the scouted entity. */ _Z_OWNED_TYPE_VALUE(_z_hello_t, hello) -_Z_LOANED_TYPE(_z_hello_t, hello) /** * Represents the reply to a query. */ _Z_OWNED_TYPE_VALUE(_z_reply_t, reply) -_Z_LOANED_TYPE(_z_reply_t, reply) /** * Represents an array of non null-terminated string. @@ -495,7 +480,6 @@ _Z_LOANED_TYPE(_z_reply_t, reply) * - :c:func:`z_str_array_array_is_empty` */ _Z_OWNED_TYPE_VALUE(_z_string_svec_t, string_array) -_Z_LOANED_TYPE(_z_string_svec_t, string_array) _Z_VIEW_TYPE(_z_string_svec_t, string_array) const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k); @@ -521,7 +505,6 @@ typedef struct { * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. */ _Z_OWNED_TYPE_VALUE(_z_closure_sample_t, closure_sample) -_Z_LOANED_TYPE(_z_closure_sample_t, closure_sample) void z_closure_sample_call(const z_loaned_closure_sample_t *closure, const z_loaned_sample_t *sample); @@ -545,7 +528,6 @@ typedef struct { * void *context: a pointer to an arbitrary state. */ _Z_OWNED_TYPE_VALUE(_z_closure_query_t, closure_query) -_Z_LOANED_TYPE(_z_closure_query_t, closure_query) void z_closure_query_call(const z_loaned_closure_query_t *closure, const z_loaned_query_t *query); @@ -569,7 +551,6 @@ typedef struct { * void *context: a pointer to an arbitrary state. */ _Z_OWNED_TYPE_VALUE(_z_closure_reply_t, closure_reply) -_Z_LOANED_TYPE(_z_closure_reply_t, closure_reply) void z_closure_reply_call(const z_loaned_closure_reply_t *closure, const z_loaned_reply_t *reply); @@ -593,7 +574,6 @@ typedef struct { * void *context: a pointer to an arbitrary state. */ _Z_OWNED_TYPE_VALUE(_z_closure_hello_t, closure_hello) -_Z_LOANED_TYPE(_z_closure_hello_t, closure_hello) void z_closure_hello_call(const z_loaned_closure_hello_t *closure, const z_loaned_hello_t *hello); @@ -616,7 +596,6 @@ typedef struct { * void *context: a pointer to an arbitrary state. */ _Z_OWNED_TYPE_VALUE(_z_closure_zid_t, closure_zid) -_Z_LOANED_TYPE(_z_closure_zid_t, closure_zid) void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id); diff --git a/include/zenoh-pico/system/platform-common.h b/include/zenoh-pico/system/platform-common.h index a14ec0ea1..a96600bb4 100644 --- a/include/zenoh-pico/system/platform-common.h +++ b/include/zenoh-pico/system/platform-common.h @@ -90,7 +90,6 @@ int8_t _z_task_cancel(_z_task_t *task); void _z_task_free(_z_task_t **task); _Z_OWNED_TYPE_VALUE(_z_task_t, task) -_Z_LOANED_TYPE(_z_task_t, task) _Z_OWNED_FUNCTIONS_SYSTEM_DEF(task) int8_t z_task_init(z_owned_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); @@ -105,11 +104,10 @@ int8_t _z_mutex_try_lock(_z_mutex_t *m); int8_t _z_mutex_unlock(_z_mutex_t *m); _Z_OWNED_TYPE_VALUE(_z_mutex_t, mutex) -_Z_LOANED_TYPE(_z_mutex_t, mutex) _Z_OWNED_FUNCTIONS_SYSTEM_DEF(mutex) int8_t z_mutex_init(z_owned_mutex_t *m); -int8_t z_mutex_drop(z_owned_mutex_t *m); +int8_t z_mutex_drop(z_moved_mutex_t m); int8_t z_mutex_lock(z_loaned_mutex_t *m); int8_t z_mutex_try_lock(z_loaned_mutex_t *m); @@ -124,11 +122,10 @@ int8_t _z_condvar_signal_all(_z_condvar_t *cv); int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m); _Z_OWNED_TYPE_VALUE(_z_condvar_t, condvar) -_Z_LOANED_TYPE(_z_condvar_t, condvar) _Z_OWNED_FUNCTIONS_SYSTEM_DEF(condvar) int8_t z_condvar_init(z_owned_condvar_t *cv); -int8_t z_condvar_drop(z_owned_condvar_t *cv); +int8_t z_condvar_drop(z_moved_condvar_t cv); int8_t z_condvar_signal(z_loaned_condvar_t *cv); int8_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m); diff --git a/src/api/api.c b/src/api/api.c index f1d2d83b2..6dad9a2f2 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -47,11 +47,6 @@ /********* Data Types Handlers *********/ -int8_t z_view_string_empty(z_view_string_t *str) { - str->_val = _z_string_null(); - return _Z_RES_OK; -} - int8_t z_view_string_from_str(z_view_string_t *str, const char *value) { str->_val = _z_string_from_str((char *)value); return _Z_RES_OK; @@ -218,8 +213,9 @@ int8_t z_config_peer(z_owned_config_t *config, const char *locator) { } _Z_RETURN_IF_ERR(z_config_default(config)); _Z_CLEAN_RETURN_IF_ERR(zp_config_insert(&config->_val, Z_CONFIG_MODE_KEY, Z_CONFIG_MODE_PEER), - z_config_drop(config)); - _Z_CLEAN_RETURN_IF_ERR(zp_config_insert(&config->_val, Z_CONFIG_CONNECT_KEY, locator), z_config_drop(config)); + z_config_drop(z_config_move(config))); + _Z_CLEAN_RETURN_IF_ERR(zp_config_insert(&config->_val, Z_CONFIG_CONNECT_KEY, locator), + z_config_drop(z_config_move(config))); return _Z_RES_OK; } @@ -549,9 +545,9 @@ int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { return _z_bytes_from_double(&bytes->_val, val); } -int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_owned_slice_t *slice) { +int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_moved_slice_t slice) { z_bytes_empty(bytes); - _z_slice_t s = _z_slice_steal(&slice->_val); + _z_slice_t s = _z_slice_steal(&slice._ptr->_val); _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_slice(&bytes->_val, s), _z_slice_clear(&s)); return _Z_RES_OK; } @@ -581,11 +577,11 @@ int8_t z_bytes_serialize_from_buf(z_owned_bytes_t *bytes, const uint8_t *data, s return z_bytes_from_slice(bytes, z_slice_move(&s)); } -int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_owned_string_t *s) { +int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_moved_string_t s) { // TODO, verify that string is a valid UTF-8 ? z_owned_slice_t slice; - size_t str_len = _z_string_len(&s->_val); - slice._val = _z_slice_steal(&s->_val._slice); + size_t str_len = _z_string_len(&s._ptr->_val); + slice._val = _z_slice_steal(&s._ptr->_val._slice); slice._val.len = str_len; return z_bytes_from_slice(bytes, z_slice_move(&slice)); } @@ -621,14 +617,15 @@ int8_t z_bytes_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_ z_owned_bytes_t data; _z_bytes_iterator_writer_t iter_writer = _z_bytes_get_iterator_writer(&bytes->_val, 0); while (iterator_body(&data, context)) { - _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&iter_writer, &data._val), z_bytes_drop(bytes)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&iter_writer, &data._val), + z_bytes_drop(z_bytes_move(bytes))); } return _Z_RES_OK; } -int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { +int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_moved_bytes_t first, z_moved_bytes_t second) { z_bytes_empty(bytes); - return _z_bytes_from_pair(&bytes->_val, &first->_val, &second->_val); + return _z_bytes_from_pair(&bytes->_val, &first._ptr->_val, &second._ptr->_val); } void z_bytes_empty(z_owned_bytes_t *bytes) { bytes->_val = _z_bytes_null(); } @@ -658,7 +655,7 @@ z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { z_bytes_empty(bytes); if (_z_bytes_iterator_next(iter, &bytes->_val) != _Z_RES_OK) { - z_bytes_drop(bytes); + z_bytes_drop(z_bytes_move(bytes)); return false; } return true; @@ -672,12 +669,12 @@ int8_t z_bytes_writer_write_all(z_bytes_writer_t *writer, const uint8_t *src, si return _z_bytes_writer_write_all(&writer->writer, src, len); } -int8_t z_bytes_writer_append(z_bytes_writer_t *writer, z_owned_bytes_t *bytes) { - return _z_bytes_writer_append(&writer->writer, &bytes->_val); +int8_t z_bytes_writer_append(z_bytes_writer_t *writer, z_moved_bytes_t bytes) { + return _z_bytes_writer_append(&writer->writer, &bytes._ptr->_val); } -int8_t z_bytes_writer_append_bounded(z_bytes_writer_t *writer, z_owned_bytes_t *bytes) { - return _z_bytes_iterator_writer_write(writer, &bytes->_val); +int8_t z_bytes_writer_append_bounded(z_bytes_writer_t *writer, z_moved_bytes_t bytes) { + return _z_bytes_iterator_writer_write(writer, &bytes._ptr->_val); } int8_t z_timestamp_new(z_timestamp_t *ts, const z_loaned_session_t *zs) { @@ -772,8 +769,8 @@ _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_value_t, reply_err, _z_value_check, _z_value_nu _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_check, _z_keyexpr_null, _z_keyexpr_copy, _z_keyexpr_clear) -_Z_VIEW_FUNCTIONS_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_check) -_Z_VIEW_FUNCTIONS_IMPL(_z_string_t, string, _z_string_check) +_Z_VIEW_FUNCTIONS_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_check, _z_keyexpr_null) +_Z_VIEW_FUNCTIONS_IMPL(_z_string_t, string, _z_string_check, _z_string_null) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_hello_t, hello, _z_hello_check, _z_hello_null, _z_hello_copy, _z_hello_clear) @@ -813,7 +810,6 @@ int8_t _z_string_array_copy(_z_string_svec_t *dst, const _z_string_svec_t *src) _z_string_svec_t _z_string_array_null(void) { return _z_string_svec_make(0); } _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_string_svec_t, string_array, _z_string_array_check, _z_string_array_null, _z_string_array_copy, _z_string_svec_clear) -_Z_VIEW_FUNCTIONS_IMPL(_z_string_vec_t, string_array, _z_string_array_check) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_slice_t, slice, _z_slice_check, _z_slice_empty, _z_slice_copy, _z_slice_clear) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_bytes_t, bytes, _z_bytes_check, _z_bytes_null, _z_bytes_copy, _z_bytes_drop) @@ -855,32 +851,32 @@ void __z_hello_handler(_z_hello_t *hello, __z_hello_handler_wrapper_t *wrapped_c wrapped_ctx->user_call(hello, wrapped_ctx->ctx); } -int8_t z_scout(z_owned_config_t *config, z_owned_closure_hello_t *callback, const z_scout_options_t *options) { +int8_t z_scout(z_moved_config_t config, z_moved_closure_hello_t callback, const z_scout_options_t *options) { int8_t ret = _Z_RES_OK; - void *ctx = callback->_val.context; - callback->_val.context = NULL; + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; // TODO[API-NET]: When API and NET are a single layer, there is no wrap the user callback and args // to enclose the z_reply_t into a z_owned_reply_t. __z_hello_handler_wrapper_t *wrapped_ctx = (__z_hello_handler_wrapper_t *)z_malloc(sizeof(__z_hello_handler_wrapper_t)); if (wrapped_ctx != NULL) { - wrapped_ctx->user_call = callback->_val.call; + wrapped_ctx->user_call = callback._ptr->_val.call; wrapped_ctx->ctx = ctx; z_what_t what; if (options != NULL) { what = options->what; } else { - char *opt_as_str = _z_config_get(&config->_val, Z_CONFIG_SCOUTING_WHAT_KEY); + char *opt_as_str = _z_config_get(&config._ptr->_val, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_SCOUTING_WHAT_DEFAULT; } what = strtol(opt_as_str, NULL, 10); } - char *opt_as_str = _z_config_get(&config->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY); + char *opt_as_str = _z_config_get(&config._ptr->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; } @@ -890,7 +886,7 @@ int8_t z_scout(z_owned_config_t *config, z_owned_closure_hello_t *callback, cons if (options != NULL) { timeout = options->timeout_ms; } else { - opt_as_str = _z_config_get(&config->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY); + opt_as_str = _z_config_get(&config._ptr->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } @@ -898,24 +894,24 @@ int8_t z_scout(z_owned_config_t *config, z_owned_closure_hello_t *callback, cons } _z_id_t zid = _z_id_empty(); - char *zid_str = _z_config_get(&config->_val, Z_CONFIG_SESSION_ZID_KEY); + char *zid_str = _z_config_get(&config._ptr->_val, Z_CONFIG_SESSION_ZID_KEY); if (zid_str != NULL) { _z_uuid_to_bytes(zid.id, zid_str); } - _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->_val.drop, ctx); + _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback._ptr->_val.drop, ctx); z_free(wrapped_ctx); z_config_drop(config); } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - z_closure_hello_null(callback); + z_closure_hello_null(callback._ptr); return ret; } -int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config) { +int8_t z_open(z_owned_session_t *zs, z_moved_config_t config) { z_session_null(zs); _z_session_t *s = z_malloc(sizeof(_z_session_t)); if (s == NULL) { @@ -932,7 +928,7 @@ int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config) { } zs->_rc = zsrc; // Open session - int8_t ret = _z_open(&zs->_rc, &config->_val); + int8_t ret = _z_open(&zs->_rc, &config._ptr->_val); if (ret != _Z_RES_OK) { _z_session_rc_decr(&zs->_rc); z_session_null(zs); @@ -944,52 +940,52 @@ int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config) { return _Z_RES_OK; } -int8_t z_close(z_owned_session_t *zs) { - if (zs == NULL || !z_session_check(zs)) { +int8_t z_close(z_moved_session_t zs) { + if (zs._ptr == NULL || !z_session_check(zs._ptr)) { return _Z_RES_OK; } z_session_drop(zs); return _Z_RES_OK; } -int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback) { +int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t callback) { // Call transport function switch (_Z_RC_IN_VAL(zs)->_tp._type) { case _Z_TRANSPORT_MULTICAST_TYPE: case _Z_TRANSPORT_RAWETH_TYPE: - _zp_multicast_fetch_zid(&(_Z_RC_IN_VAL(zs)->_tp), &callback->_val); + _zp_multicast_fetch_zid(&(_Z_RC_IN_VAL(zs)->_tp), &callback._ptr->_val); break; default: break; } // Note and clear context - void *ctx = callback->_val.context; - callback->_val.context = NULL; + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; // Drop if needed - if (callback->_val.drop != NULL) { - callback->_val.drop(ctx); + if (callback._ptr->_val.drop != NULL) { + callback._ptr->_val.drop(ctx); } - z_closure_zid_null(callback); + z_closure_zid_null(callback._ptr); return 0; } -int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback) { +int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t callback) { // Call transport function switch (_Z_RC_IN_VAL(zs)->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: - _zp_unicast_fetch_zid(&(_Z_RC_IN_VAL(zs)->_tp), &callback->_val); + _zp_unicast_fetch_zid(&(_Z_RC_IN_VAL(zs)->_tp), &callback._ptr->_val); break; default: break; } // Note and clear context - void *ctx = callback->_val.context; - callback->_val.context = NULL; + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; // Drop if needed - if (callback->_val.drop != NULL) { - callback->_val.drop(ctx); + if (callback._ptr->_val.drop != NULL) { + callback._ptr->_val.drop(ctx); } - z_closure_zid_null(callback); + z_closure_zid_null(callback._ptr); return 0; } @@ -1059,10 +1055,10 @@ _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_publisher_t, publisher, _z_publisher_ch void z_put_options_default(z_put_options_t *options) { options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; - options->encoding = NULL; + options->encoding = (z_moved_encoding_t){NULL}; options->is_express = false; options->timestamp = NULL; - options->attachment = NULL; + options->attachment = (z_moved_bytes_t){NULL}; } void z_delete_options_default(z_delete_options_t *options) { @@ -1072,7 +1068,7 @@ void z_delete_options_default(z_delete_options_t *options) { options->priority = Z_PRIORITY_DEFAULT; } -int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_moved_bytes_t payload, const z_put_options_t *options) { int8_t ret = 0; @@ -1088,16 +1084,17 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_ } _z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true); - ret = _z_write(_Z_RC_IN_VAL(zs), keyexpr_aliased, _z_bytes_from_owned_bytes(payload), - opt.encoding == NULL ? NULL : &opt.encoding->_val, Z_SAMPLE_KIND_PUT, opt.congestion_control, - opt.priority, opt.is_express, opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment)); + ret = + _z_write(_Z_RC_IN_VAL(zs), keyexpr_aliased, _z_bytes_from_owned_bytes(payload._ptr), + opt.encoding._ptr == NULL ? NULL : &opt.encoding._ptr->_val, Z_SAMPLE_KIND_PUT, opt.congestion_control, + opt.priority, opt.is_express, opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment._ptr)); // Trigger local subscriptions _z_trigger_local_subscriptions( - _Z_RC_IN_VAL(zs), keyexpr_aliased, _z_bytes_from_owned_bytes(payload), - opt.encoding == NULL ? NULL : &opt.encoding->_val, + _Z_RC_IN_VAL(zs), keyexpr_aliased, _z_bytes_from_owned_bytes(payload._ptr), + opt.encoding._ptr == NULL ? NULL : &opt.encoding._ptr->_val, _z_n_qos_make(opt.is_express, opt.congestion_control == Z_CONGESTION_CONTROL_BLOCK, opt.priority), - opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment)); + opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment._ptr)); // Clean-up z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); @@ -1123,7 +1120,7 @@ int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, } void z_publisher_options_default(z_publisher_options_t *options) { - options->encoding = NULL; + options->encoding = (z_moved_encoding_t){NULL}; options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; options->is_express = false; @@ -1152,7 +1149,7 @@ int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *z opt = *options; } // Set publisher - _z_publisher_t int_pub = _z_declare_publisher(zs, key, opt.encoding == NULL ? NULL : &opt.encoding->_val, + _z_publisher_t int_pub = _z_declare_publisher(zs, key, opt.encoding._ptr == NULL ? NULL : &opt.encoding._ptr->_val, opt.congestion_control, opt.priority, opt.is_express); // Create write filter int8_t res = _z_write_filter_create(&int_pub); @@ -1166,17 +1163,17 @@ int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *z return _Z_RES_OK; } -int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { return _z_undeclare_and_clear_publisher(&pub->_val); } +int8_t z_undeclare_publisher(z_moved_publisher_t pub) { return _z_undeclare_and_clear_publisher(&pub._ptr->_val); } void z_publisher_put_options_default(z_publisher_put_options_t *options) { - options->encoding = NULL; - options->attachment = NULL; + options->encoding = (z_moved_encoding_t){NULL}; + options->attachment = (z_moved_bytes_t){NULL}; options->timestamp = NULL; } void z_publisher_delete_options_default(z_publisher_delete_options_t *options) { options->timestamp = NULL; } -int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_moved_bytes_t payload, const z_publisher_put_options_t *options) { int8_t ret = 0; // Build options @@ -1188,24 +1185,24 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload opt.attachment = options->attachment; } _z_encoding_t encoding; - if (opt.encoding == NULL) { + if (opt.encoding._ptr == NULL) { _Z_RETURN_IF_ERR(_z_encoding_copy(&encoding, &pub->_encoding)); } else { - encoding = _z_encoding_steal(&opt.encoding->_val); + encoding = _z_encoding_steal(&opt.encoding._ptr->_val); } // Check if write filter is active before writing if (!_z_write_filter_active(pub)) { // Write value - ret = _z_write(_Z_RC_IN_VAL(&pub->_zn), pub->_key, _z_bytes_from_owned_bytes(payload), &encoding, + ret = _z_write(_Z_RC_IN_VAL(&pub->_zn), pub->_key, _z_bytes_from_owned_bytes(payload._ptr), &encoding, Z_SAMPLE_KIND_PUT, pub->_congestion_control, pub->_priority, pub->_is_express, opt.timestamp, - _z_bytes_from_owned_bytes(opt.attachment)); + _z_bytes_from_owned_bytes(opt.attachment._ptr)); } // Trigger local subscriptions _z_trigger_local_subscriptions( - _Z_RC_IN_VAL(&pub->_zn), pub->_key, _z_bytes_from_owned_bytes(payload), &encoding, + _Z_RC_IN_VAL(&pub->_zn), pub->_key, _z_bytes_from_owned_bytes(payload._ptr), &encoding, _z_n_qos_make(pub->_is_express, pub->_congestion_control == Z_CONGESTION_CONTROL_BLOCK, pub->_priority), - opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment)); + opt.timestamp, _z_bytes_from_owned_bytes(opt.attachment._ptr)); // Clean-up _z_encoding_clear(&encoding); z_bytes_drop(opt.attachment); @@ -1248,18 +1245,18 @@ void z_get_options_default(z_get_options_t *options) { options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; options->is_express = false; - options->encoding = NULL; - options->payload = NULL; - options->attachment = NULL; + options->encoding = (z_moved_encoding_t){NULL}; + options->payload = (z_moved_bytes_t){NULL}; + options->attachment = (z_moved_bytes_t){NULL}; options->timeout_ms = Z_GET_TIMEOUT_DEFAULT; } int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const char *parameters, - z_owned_closure_reply_t *callback, z_get_options_t *options) { + z_moved_closure_reply_t callback, z_get_options_t *options) { int8_t ret = _Z_RES_OK; - void *ctx = callback->_val.context; - callback->_val.context = NULL; + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; _z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true); @@ -1269,7 +1266,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation = options->consolidation; opt.target = options->target; opt.encoding = options->encoding; - opt.payload = z_bytes_move(options->payload); + opt.payload = options->payload; opt.attachment = options->attachment; opt.congestion_control = options->congestion_control; opt.priority = options->priority; @@ -1285,19 +1282,18 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co } } // Set value - _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), - .encoding = _z_encoding_from_owned(opt.encoding)}; + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload._ptr), + .encoding = _z_encoding_from_owned(opt.encoding._ptr)}; - ret = _z_query(_Z_RC_IN_VAL(zs), keyexpr_aliased, parameters, opt.target, opt.consolidation.mode, value, - callback->_val.call, callback->_val.drop, ctx, opt.timeout_ms, - _z_bytes_from_owned_bytes(opt.attachment), opt.congestion_control, opt.priority, opt.is_express); - if (opt.payload != NULL) { - z_bytes_drop(opt.payload); - } + ret = + _z_query(_Z_RC_IN_VAL(zs), keyexpr_aliased, parameters, opt.target, opt.consolidation.mode, value, + callback._ptr->_val.call, callback._ptr->_val.drop, ctx, opt.timeout_ms, + _z_bytes_from_owned_bytes(opt.attachment._ptr), opt.congestion_control, opt.priority, opt.is_express); // Clean-up + z_bytes_drop(opt.payload); z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); - z_closure_reply_null(callback); + z_closure_reply_null(callback._ptr); // call and drop passed to _z_query, so we nullify the closure here return ret; } @@ -1335,10 +1331,10 @@ _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_queryable_t, queryable, _z_queryable_ch void z_queryable_options_default(z_queryable_options_t *options) { options->complete = _Z_QUERYABLE_COMPLETE_DEFAULT; } int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_session_t *zs, - const z_loaned_keyexpr_t *keyexpr, z_owned_closure_query_t *callback, + const z_loaned_keyexpr_t *keyexpr, z_moved_closure_query_t callback, const z_queryable_options_t *options) { - void *ctx = callback->_val.context; - callback->_val.context = NULL; + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; _z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true); _z_keyexpr_t key = keyexpr_aliased; @@ -1360,26 +1356,27 @@ int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_sessio opt.complete = options->complete; } - queryable->_val = _z_declare_queryable(zs, key, opt.complete, callback->_val.call, callback->_val.drop, ctx); + queryable->_val = + _z_declare_queryable(zs, key, opt.complete, callback._ptr->_val.call, callback._ptr->_val.drop, ctx); - z_closure_query_null(callback); + z_closure_query_null(callback._ptr); return _Z_RES_OK; } -int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { - return _z_undeclare_and_clear_queryable(&queryable->_val); +int8_t z_undeclare_queryable(z_moved_queryable_t queryable) { + return _z_undeclare_and_clear_queryable(&queryable._ptr->_val); } void z_query_reply_options_default(z_query_reply_options_t *options) { - options->encoding = NULL; + options->encoding = (z_moved_encoding_t){NULL}; options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; options->timestamp = NULL; options->is_express = false; - options->attachment = NULL; + options->attachment = (z_moved_bytes_t){NULL}; } -int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_moved_bytes_t payload, const z_query_reply_options_t *options) { // Try upgrading session weak to rc _z_session_rc_t sess_rc = _z_session_weak_upgrade(&_Z_RC_IN_VAL(query)->_zn); @@ -1395,15 +1392,13 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke opts = *options; } // Set value - _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload), - .encoding = _z_encoding_from_owned(opts.encoding)}; + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload._ptr), + .encoding = _z_encoding_from_owned(opts.encoding._ptr)}; int8_t ret = _z_send_reply(_Z_RC_IN_VAL(query), &sess_rc, keyexpr_aliased, value, Z_SAMPLE_KIND_PUT, opts.congestion_control, - opts.priority, opts.is_express, opts.timestamp, _z_bytes_from_owned_bytes(opts.attachment)); - if (payload != NULL) { - z_bytes_drop(payload); - } + opts.priority, opts.is_express, opts.timestamp, _z_bytes_from_owned_bytes(opts.attachment._ptr)); + z_bytes_drop(payload); // Clean-up _z_session_rc_drop(&sess_rc); z_encoding_drop(opts.encoding); @@ -1416,7 +1411,7 @@ void z_query_reply_del_options_default(z_query_reply_del_options_t *options) { options->priority = Z_PRIORITY_DEFAULT; options->timestamp = NULL; options->is_express = false; - options->attachment = NULL; + options->attachment = (z_moved_bytes_t){NULL}; } int8_t z_query_reply_del(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, @@ -1438,16 +1433,18 @@ int8_t z_query_reply_del(const z_loaned_query_t *query, const z_loaned_keyexpr_t int8_t ret = _z_send_reply(_Z_RC_IN_VAL(query), &sess_rc, keyexpr_aliased, value, Z_SAMPLE_KIND_DELETE, opts.congestion_control, opts.priority, opts.is_express, opts.timestamp, - _z_bytes_from_owned_bytes(opts.attachment)); + _z_bytes_from_owned_bytes(opts.attachment._ptr)); // Clean-up _z_session_rc_drop(&sess_rc); z_bytes_drop(opts.attachment); return ret; } -void z_query_reply_err_options_default(z_query_reply_err_options_t *options) { options->encoding = NULL; } +void z_query_reply_err_options_default(z_query_reply_err_options_t *options) { + options->encoding = (z_moved_encoding_t){NULL}; +} -int8_t z_query_reply_err(const z_loaned_query_t *query, z_owned_bytes_t *payload, +int8_t z_query_reply_err(const z_loaned_query_t *query, z_moved_bytes_t payload, const z_query_reply_err_options_t *options) { // Try upgrading session weak to rc _z_session_rc_t sess_rc = _z_session_weak_upgrade(&_Z_RC_IN_VAL(query)->_zn); @@ -1461,13 +1458,11 @@ int8_t z_query_reply_err(const z_loaned_query_t *query, z_owned_bytes_t *payload opts = *options; } // Set value - _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload), - .encoding = _z_encoding_from_owned(opts.encoding)}; + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload._ptr), + .encoding = _z_encoding_from_owned(opts.encoding._ptr)}; int8_t ret = _z_send_reply_err(_Z_RC_IN_VAL(query), &sess_rc, value); - if (payload != NULL) { - z_bytes_drop(payload); - } + z_bytes_drop(payload); // Clean-up z_encoding_drop(opts.encoding); return ret; @@ -1527,10 +1522,10 @@ int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, c return _Z_RES_OK; } -int8_t z_undeclare_keyexpr(z_owned_keyexpr_t *keyexpr, const z_loaned_session_t *zs) { +int8_t z_undeclare_keyexpr(z_moved_keyexpr_t keyexpr, const z_loaned_session_t *zs) { int8_t ret = _Z_RES_OK; - ret = _z_undeclare_resource(_Z_RC_IN_VAL(zs), keyexpr->_val._id); + ret = _z_undeclare_resource(_Z_RC_IN_VAL(zs), keyexpr._ptr->_val._id); z_keyexpr_drop(keyexpr); return ret; @@ -1552,9 +1547,9 @@ _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_subscriber_t, subscriber, _z_subscriber void z_subscriber_options_default(z_subscriber_options_t *options) { options->reliability = Z_RELIABILITY_DEFAULT; } int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, - z_owned_closure_sample_t *callback, const z_subscriber_options_t *options) { - void *ctx = callback->_val.context; - callback->_val.context = NULL; + z_moved_closure_sample_t callback, const z_subscriber_options_t *options) { + void *ctx = callback._ptr->_val.context; + callback._ptr->_val.context = NULL; char *suffix = NULL; _z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true); @@ -1594,11 +1589,12 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t if (options != NULL) { subinfo.reliability = options->reliability; } - _z_subscriber_t int_sub = _z_declare_subscriber(zs, key, subinfo, callback->_val.call, callback->_val.drop, ctx); + _z_subscriber_t int_sub = + _z_declare_subscriber(zs, key, subinfo, callback._ptr->_val.call, callback._ptr->_val.drop, ctx); if (suffix != NULL) { z_free(suffix); } - z_closure_sample_null(callback); + z_closure_sample_null(callback._ptr); sub->_val = int_sub; if (!_z_subscriber_check(&sub->_val)) { @@ -1608,7 +1604,7 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t } } -int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { return _z_undeclare_and_clear_subscriber(&sub->_val); } +int8_t z_undeclare_subscriber(z_moved_subscriber_t sub) { return _z_undeclare_and_clear_subscriber(&sub._ptr->_val); } int8_t z_subscriber_keyexpr(z_owned_keyexpr_t *keyexpr, z_loaned_subscriber_t *sub) { // Init keyexpr diff --git a/src/system/platform-common.c b/src/system/platform-common.c index 53e7593de..259a19284 100644 --- a/src/system/platform-common.c +++ b/src/system/platform-common.c @@ -38,7 +38,7 @@ int8_t z_task_join(z_owned_task_t *task) { _Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_mutex_t, mutex) int8_t z_mutex_init(z_owned_mutex_t *m) { return _z_mutex_init(&m->_val); } -int8_t z_mutex_drop(z_owned_mutex_t *m) { return _z_mutex_drop(&m->_val); } +int8_t z_mutex_drop(z_moved_mutex_t m) { return _z_mutex_drop(&m._ptr->_val); } int8_t z_mutex_lock(z_loaned_mutex_t *m) { return _z_mutex_lock(m); } int8_t z_mutex_try_lock(z_loaned_mutex_t *m) { return _z_mutex_try_lock(m); } @@ -48,7 +48,7 @@ int8_t z_mutex_unlock(z_loaned_mutex_t *m) { return _z_mutex_unlock(m); } _Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_condvar_t, condvar) int8_t z_condvar_init(z_owned_condvar_t *cv) { return _z_condvar_init(&cv->_val); } -int8_t z_condvar_drop(z_owned_condvar_t *cv) { return _z_condvar_drop(&cv->_val); } +int8_t z_condvar_drop(z_moved_condvar_t cv) { return _z_condvar_drop(&cv._ptr->_val); } int8_t z_condvar_signal(z_loaned_condvar_t *cv) { return _z_condvar_signal(cv); } int8_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m) { return _z_condvar_wait(cv, m); } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index d5e1d0cca..4372884e1 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -68,7 +68,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_view_string_t k_str; z_keyexpr_as_view_string(query_ke, &k_str); (void)arg; - assert(z_check(k_str)); + assert(!z_view_string_is_empty(&k_str)); z_view_string_t pred; z_query_parameters(query, &pred); @@ -97,7 +97,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { z_view_string_t k_str; z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); (void)arg; - assert(z_check(k_str)); + assert(!z_view_string_is_empty(&k_str)); } else { const z_loaned_reply_err_t *_ret_zerr = z_reply_err(reply); (void)(_ret_zerr); @@ -112,7 +112,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { z_view_string_t k_str; z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); (void)arg; - assert(z_check(k_str)); + assert(!z_view_string_is_empty(&k_str)); const char *encoding_expected = z_sample_kind(sample) == Z_SAMPLE_KIND_PUT ? "zenoh/bytes;test_encoding" : "zenoh/bytes"; z_owned_string_t encoding; diff --git a/tests/z_api_bytes_test.c b/tests/z_api_bytes_test.c index 7c8dd08ad..66101494c 100644 --- a/tests/z_api_bytes_test.c +++ b/tests/z_api_bytes_test.c @@ -206,7 +206,7 @@ void test_slice(void) { z_slice_copy_from_buf(&s, data, 10); z_bytes_serialize_from_slice(&payload2, z_slice_loan(&s)); assert(z_slice_check(&s)); - z_slice_drop(&s); + z_slice_drop(z_slice_move(&s)); assert(z_check_and_drop_payload(&payload2, data, 10)); z_owned_bytes_t payload3; diff --git a/tests/z_api_config_test.c b/tests/z_api_config_test.c index d0b22754b..10012f5e5 100644 --- a/tests/z_api_config_test.c +++ b/tests/z_api_config_test.c @@ -26,9 +26,9 @@ void config_client(void) { z_owned_config_t config; assert(0 == z_config_client(&config, locator)); assert(z_config_check(&config)); - strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_MODE_KEY), Z_CONFIG_MODE_CLIENT); - strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_CONNECT_KEY), locator); - z_config_drop(&config); + assert(strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_MODE_KEY), Z_CONFIG_MODE_CLIENT) == 0); + assert(strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_CONNECT_KEY), locator) == 0); + z_config_drop(z_config_move(&config)); } void config_peer(void) { @@ -36,9 +36,9 @@ void config_peer(void) { z_owned_config_t config; assert(0 == z_config_peer(&config, locator)); assert(z_config_check(&config)); - strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_MODE_KEY), Z_CONFIG_MODE_PEER); - strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_CONNECT_KEY), locator); - z_config_drop(&config); + assert(strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_MODE_KEY), Z_CONFIG_MODE_PEER) == 0); + assert(strcmp(zp_config_get(z_config_loan(&config), Z_CONFIG_CONNECT_KEY), locator) == 0); + z_config_drop(z_config_move(&config)); assert(0 != z_config_peer(&config, NULL)); assert(!z_config_check(&config)); } diff --git a/tests/z_api_null_drop_test.c b/tests/z_api_null_drop_test.c index bb3bf6bc2..3c6f7f6b9 100644 --- a/tests/z_api_null_drop_test.c +++ b/tests/z_api_null_drop_test.c @@ -20,150 +20,64 @@ #include "zenoh-pico.h" -int main(void) { - // - // Check that all null functions exists - // - z_owned_session_t session_null_1; - z_session_null(&session_null_1); - z_owned_keyexpr_t keyexpr_null_1; - z_keyexpr_null(&keyexpr_null_1); - z_owned_config_t config_null_1; - z_config_null(&config_null_1); - z_owned_hello_t hello_null_1; - z_hello_null(&hello_null_1); - z_owned_closure_sample_t closure_sample_null_1; - z_closure_sample_null(&closure_sample_null_1); - z_owned_closure_query_t closure_query_null_1; - z_closure_query_null(&closure_query_null_1); - z_owned_closure_reply_t closure_reply_null_1; - z_closure_reply_null(&closure_reply_null_1); - z_owned_closure_hello_t closure_hello_null_1; - z_closure_hello_null(&closure_hello_null_1); - z_owned_closure_zid_t closure_zid_null_1; - z_closure_zid_null(&closure_zid_null_1); - z_owned_string_t str_null_1; - z_string_null(&str_null_1); - - // - // Test that they actually make invalid value (where applicable) - // - assert(!z_check(session_null_1)); - assert(!z_check(keyexpr_null_1)); - assert(!z_check(config_null_1)); - assert(!z_check(hello_null_1)); - assert(!z_check(str_null_1)); - - // - // Test that z_null macro defined for all types - // - z_owned_session_t session_null_2; - z_owned_keyexpr_t keyexpr_null_2; - z_owned_config_t config_null_2; - z_owned_hello_t hello_null_2; - z_owned_closure_sample_t closure_sample_null_2; - z_owned_closure_query_t closure_query_null_2; - z_owned_closure_reply_t closure_reply_null_2; - z_owned_closure_hello_t closure_hello_null_2; - z_owned_closure_zid_t closure_zid_null_2; - z_owned_string_t str_null_2; - - z_null(&session_null_2); - z_null(&keyexpr_null_2); - z_null(&config_null_2); - z_null(&hello_null_2); - z_null(&closure_sample_null_2); - z_null(&closure_query_null_2); - z_null(&closure_reply_null_2); - z_null(&closure_hello_null_2); - z_null(&closure_zid_null_2); - z_null(&str_null_2); - -#if Z_FEATURE_PUBLICATION == 1 - z_owned_publisher_t publisher_null_1; - z_publisher_null(&publisher_null_1); - assert(!z_check(publisher_null_1)); - z_owned_publisher_t publisher_null_2; - z_null(&publisher_null_2); - assert(!z_check(publisher_null_2)); -#endif -#if Z_FEATURE_SUBSCRIPTION == 1 - z_owned_subscriber_t subscriber_null_1; - z_subscriber_null(&subscriber_null_1); - assert(!z_check(subscriber_null_1)); - z_owned_subscriber_t subscriber_null_2; - z_null(&subscriber_null_2); - assert(!z_check(subscriber_null_2)); -#endif -#if Z_FEATURE_QUERYABLE == 1 - z_owned_queryable_t queryable_null_1; - z_queryable_null(&queryable_null_1); - assert(!z_check(queryable_null_1)); - z_owned_queryable_t queryable_null_2; - z_null(&queryable_null_2); - assert(!z_check(queryable_null_2)); -#endif -#if Z_FEATURE_QUERY == 1 - z_owned_reply_t reply_null_1; - z_reply_null(&reply_null_1); - assert(!z_check(reply_null_1)); - z_owned_reply_t reply_null_2; - z_null(&reply_null_2); - assert(!z_check(reply_null_2)); -#endif - - // - // Test that null macro works the same as direct call - // - assert(!z_check(session_null_2)); - assert(!z_check(keyexpr_null_2)); - assert(!z_check(config_null_2)); - assert(!z_check(hello_null_2)); - assert(!z_check(str_null_2)); - - // - // Test drop null and double drop it - // - for (int i = 0; i < 2; ++i) { - z_drop(z_move(session_null_1)); - z_drop(z_move(keyexpr_null_1)); - z_drop(z_move(config_null_1)); - z_drop(z_move(hello_null_1)); - z_drop(z_move(closure_sample_null_1)); - z_drop(z_move(closure_query_null_1)); - z_drop(z_move(closure_reply_null_1)); - z_drop(z_move(closure_hello_null_1)); - z_drop(z_move(closure_zid_null_1)); - z_drop(z_move(str_null_1)); - - z_drop(z_move(session_null_2)); - z_drop(z_move(keyexpr_null_2)); - z_drop(z_move(config_null_2)); - z_drop(z_move(hello_null_2)); - z_drop(z_move(closure_sample_null_2)); - z_drop(z_move(closure_query_null_2)); - z_drop(z_move(closure_reply_null_2)); - z_drop(z_move(closure_hello_null_2)); - z_drop(z_move(closure_zid_null_2)); - z_drop(z_move(str_null_2)); +// fill v with invalid values +// set v to null +// check if v it is null +// make sure that drop on null does not crash +// make sure that double drop on null does not crash +// fill v with invalid values again +// +// set v1 to null +// move v to v1 +// make sure that v is null now +#define TEST(name) \ + { \ + z_owned_##name##_t v; \ + memset(&v, -1, sizeof(v)); \ + z_null(&v); \ + assert(!z_check(v)); \ + z_drop(z_move(v)); \ + z_drop(z_move(v)); \ + z_owned_##name##_t v1; \ + z_null(&v1); \ + memset(&v, -1, sizeof(v)); \ + z_take(&v1, z_move(v)); \ + assert(!z_check(v)); \ + } +int main(void) { + TEST(session) + TEST(keyexpr) + TEST(config) + TEST(hello) + TEST(closure_sample) + TEST(closure_query) + TEST(closure_reply) + TEST(closure_hello) + TEST(closure_zid) + TEST(string) + TEST(string_array) + TEST(sample) + TEST(slice) + TEST(bytes) + TEST(encoding) #if Z_FEATURE_PUBLICATION == 1 - z_drop(z_move(publisher_null_1)); - z_drop(z_move(publisher_null_2)); + TEST(publisher) #endif #if Z_FEATURE_SUBSCRIPTION == 1 - z_drop(z_move(subscriber_null_1)); - z_drop(z_move(subscriber_null_2)); + TEST(subscriber) #endif #if Z_FEATURE_QUERYABLE == 1 - z_drop(z_move(queryable_null_1)); - z_drop(z_move(queryable_null_2)); + TEST(query) + TEST(queryable) #endif #if Z_FEATURE_QUERY == 1 - z_drop(z_move(reply_null_1)); - z_drop(z_move(reply_null_2)); + TEST(reply) #endif - } + // Double drop not supported for these types + // TEST(task) + // TEST(mutex) + // TEST(condvar) return 0; } diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 9f46621d8..3ce6abd1b 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -190,7 +190,7 @@ int main(int argc, char **argv) { z_owned_closure_sample_t callback; z_closure(&callback, data_handler, NULL, &idx[i]); z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); - int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(rids2[i]), &callback, NULL); + int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(rids2[i]), z_move(callback), NULL); assert(res == _Z_RES_OK); printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id, z_loan(rids2[i])->_id, ""); @@ -206,7 +206,7 @@ int main(int argc, char **argv) { z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t)); z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, s1_res); - assert(z_declare_queryable(qle, z_loan(s2), z_loan(ke), &callback, NULL) == _Z_RES_OK); + assert(z_declare_queryable(qle, z_loan(s2), z_loan(ke), z_move(callback), NULL) == _Z_RES_OK); printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_val._entity_id, (z_zint_t)0, s1_res); qles2 = _z_list_push(qles2, qle); } @@ -308,7 +308,7 @@ int main(int argc, char **argv) { z_closure(&callback, reply_handler, NULL, &idx[i]); z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, s1_res); - z_get(z_loan(s1), z_loan(ke), "", &callback, NULL); + z_get(z_loan(s1), z_loan(ke), "", z_move(callback), NULL); printf("Queried data from session 1: %zu %s\n", (z_zint_t)0, s1_res); } } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index c43e004ca..7966eb820 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -118,7 +118,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, s1_res); - int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(ke), &callback, NULL); + int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(ke), z_move(callback), NULL); assert(res == _Z_RES_OK); printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id, (z_zint_t)0, s1_res); diff --git a/tests/z_session_test.c b/tests/z_session_test.c index 4b662d0a0..15ca0f6b3 100644 --- a/tests/z_session_test.c +++ b/tests/z_session_test.c @@ -49,5 +49,5 @@ int main(void) { zp_stop_lease_task(z_loan_mut(s)); // Immediately close the session - z_close(&s); + z_close(z_move(s)); } diff --git a/zenohpico.pc b/zenohpico.pc index 797e4f5d8..fd68ae549 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 1.0.20240805dev +Version: 1.0.20240812dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico