From 53b1723cc1ca22ca9e1c5a5b6cbab80ae7cc7f16 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 30 Oct 2024 10:27:00 +0100 Subject: [PATCH] add empty gravestone values for bytes_writer and serializer --- docs/api.rst | 11 ++++ include/zenoh-pico/api/handlers.h | 52 ++++++++--------- include/zenoh-pico/api/olv_macros.h | 88 +++++++++++++++++------------ include/zenoh-pico/api/primitives.h | 8 +-- src/api/api.c | 14 ++--- src/api/serialization.c | 5 +- 6 files changed, 105 insertions(+), 73 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 28e2e9fb7..48b6bf9b7 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -269,6 +269,7 @@ See details at :ref:`owned_types_concept` .. c:function:: void z_bytes_writer_clone(z_owned_bytes_writer_t * dst, const z_loaned_bytes_writer_t * bytes_writer) .. c:function:: const z_loaned_bytes_writer_t * z_bytes_writer_loan(const z_owned_bytes_writer_t * bytes_writer) .. c:function:: z_loaned_bytes_writer_t * z_bytes_writer_loan_mut(z_owned_bytes_writer_t * bytes_writer) +.. c:function:: z_result_t z_bytes_writer_loaned_take(z_owned_bytes_writer_t *dst, z_loaned_bytes_writer_t *src) Encoding -------- @@ -1283,6 +1284,16 @@ Functions .. autocfunction:: serialization.h::ze_serialize_str .. autocfunction:: serialization.h::ze_serialize_substr +Ownership Functions +^^^^^^^^^^^^^^^^^^^ + +See details at :ref:`owned_types_concept` + +.. c:function:: void ze_serializer_drop(ze_moved_serializer_t * serializer) +.. c:function:: void ze_serializer_clone(ze_owned_serializer_t * dst, const ze_loaned_serializer_t * serializer) +.. c:function:: const ze_loaned_serializer_t * ze_serializer_loan(const ze_owned_serializer_t * serializer) +.. c:function:: ze_loaned_serializer_t * ze_serializer_loan_mut(ze_owned_serializer_t * serializer) +.. c:function:: z_result_t ze_serializer_loaned_take(ze_owned_serializer_t *dst, ze_loaned_serializer_t *src) Others ====== diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index c3342302a..973173e9d 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -109,8 +109,8 @@ extern "C" { return h; \ } \ \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_INLINE_IMPL(handler_type, handler_name, _z_##handler_name##_check, \ - _z_##handler_name##_null, _z_##handler_name##_clear) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_INLINE_IMPL(handler_type, handler_name, _z_##handler_name##_check, \ + _z_##handler_name##_null, _z_##handler_name##_clear) \ \ static inline z_result_t handler_new_f_name(callback_type *callback, z_owned_##handler_name##_t *handler, \ size_t capacity) { \ @@ -145,30 +145,30 @@ extern "C" { /* elem_drop_f */ z_##item_name##_drop, \ /* elem_null */ z_internal_##item_name##_null) -#define _Z_CHANNEL_DUMMY_IMPL(handler_type, handler_name, item_name) \ - _Z_OWNED_TYPE_VALUE(handler_type, handler_name) \ - static inline void _z_##handler_name##_clear(handler_type *handler) { _ZP_UNUSED(handler); } \ - static inline bool _z_##handler_name##_check(const handler_type *handler) { \ - _ZP_UNUSED(handler); \ - return false; \ - } \ - static inline handler_type _z_##handler_name##_null(void) { \ - handler_type h = {0}; \ - return h; \ - } \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_INLINE_IMPL(handler_type, handler_name, _z_##handler_name##_check, \ - _z_##handler_name##_null, _z_##handler_name##_clear) \ - static inline z_result_t z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \ - z_owned_##item_name##_t *e) { \ - _ZP_UNUSED(handler); \ - _ZP_UNUSED(e); \ - return Z_CHANNEL_DISCONNECTED; \ - } \ - static inline z_result_t z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, \ - z_owned_##item_name##_t *e) { \ - _ZP_UNUSED(handler); \ - _ZP_UNUSED(e); \ - return Z_CHANNEL_DISCONNECTED; \ +#define _Z_CHANNEL_DUMMY_IMPL(handler_type, handler_name, item_name) \ + _Z_OWNED_TYPE_VALUE(handler_type, handler_name) \ + static inline void _z_##handler_name##_clear(handler_type *handler) { _ZP_UNUSED(handler); } \ + static inline bool _z_##handler_name##_check(const handler_type *handler) { \ + _ZP_UNUSED(handler); \ + return false; \ + } \ + static inline handler_type _z_##handler_name##_null(void) { \ + handler_type h = {0}; \ + return h; \ + } \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_INLINE_IMPL(handler_type, handler_name, _z_##handler_name##_check, \ + _z_##handler_name##_null, _z_##handler_name##_clear) \ + static inline z_result_t z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \ + z_owned_##item_name##_t *e) { \ + _ZP_UNUSED(handler); \ + _ZP_UNUSED(e); \ + return Z_CHANNEL_DISCONNECTED; \ + } \ + static inline z_result_t z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, \ + z_owned_##item_name##_t *e) { \ + _ZP_UNUSED(handler); \ + _ZP_UNUSED(e); \ + return Z_CHANNEL_DISCONNECTED; \ } #define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \ diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 7c61804f2..0435a3687 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -59,7 +59,7 @@ type _val; \ } z_view_##name##_t; -#define _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \ +#define _Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF_PREFIX(prefix, name) \ void prefix##_internal_##name##_null(prefix##_owned_##name##_t *obj); \ bool prefix##_internal_##name##_check(const prefix##_owned_##name##_t *obj); \ const prefix##_loaned_##name##_t *prefix##_##name##_loan(const prefix##_owned_##name##_t *obj); \ @@ -68,11 +68,15 @@ void prefix##_##name##_take(prefix##_owned_##name##_t *obj, prefix##_moved_##name##_t *src); \ void prefix##_##name##_drop(prefix##_moved_##name##_t *obj); -#define _Z_OWNED_FUNCTIONS_DEF_PREFIX(prefix, name) \ - _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \ - z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, const prefix##_loaned_##name##_t *src); \ +#define _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \ + _Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF_PREFIX(prefix, name) \ z_result_t prefix##_##name##_take_loaned(prefix##_owned_##name##_t *dst, prefix##_loaned_##name##_t *src); +#define _Z_OWNED_FUNCTIONS_DEF_PREFIX(prefix, name) \ + _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \ + z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, const prefix##_loaned_##name##_t *src); + +#define _Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF(name) _Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF_PREFIX(z, name) #define _Z_OWNED_FUNCTIONS_NO_COPY_DEF(name) _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(z, name) #define _Z_OWNED_FUNCTIONS_DEF(name) _Z_OWNED_FUNCTIONS_DEF_PREFIX(z, name) @@ -102,49 +106,63 @@ #define _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE_PREFIX_INNER(z, name, _ZP_NOTHING) -#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, attribute) \ - attribute void prefix##_internal_##name##_null(prefix##_owned_##name##_t *obj) { obj->_val = f_null(); } \ - _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE_PREFIX_INNER(prefix, name, attribute) \ - attribute bool prefix##_internal_##name##_check(const prefix##_owned_##name##_t *obj) { \ - return f_check((&obj->_val)); \ - } \ - attribute const prefix##_loaned_##name##_t *prefix##_##name##_loan(const prefix##_owned_##name##_t *obj) { \ - return &obj->_val; \ - } \ - attribute prefix##_loaned_##name##_t *prefix##_##name##_loan_mut(prefix##_owned_##name##_t *obj) { \ - return &obj->_val; \ - } \ - attribute void prefix##_##name##_drop(prefix##_moved_##name##_t *obj) { \ - if (obj != NULL) f_drop((&obj->_this._val)); \ +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, \ + attribute) \ + attribute void prefix##_internal_##name##_null(prefix##_owned_##name##_t *obj) { obj->_val = f_null(); } \ + _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE_PREFIX_INNER(prefix, name, attribute) \ + attribute bool prefix##_internal_##name##_check(const prefix##_owned_##name##_t *obj) { \ + return f_check((&obj->_val)); \ + } \ + attribute const prefix##_loaned_##name##_t *prefix##_##name##_loan(const prefix##_owned_##name##_t *obj) { \ + return &obj->_val; \ + } \ + attribute prefix##_loaned_##name##_t *prefix##_##name##_loan_mut(prefix##_owned_##name##_t *obj) { \ + return &obj->_val; \ + } \ + attribute void prefix##_##name##_drop(prefix##_moved_##name##_t *obj) { \ + if (obj != NULL) f_drop((&obj->_this._val)); \ } -#define _Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_copy, f_move, f_drop, \ - attribute) \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, attribute) \ - attribute z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, \ - const prefix##_loaned_##name##_t *src) { \ - return f_copy((&obj->_val), src); \ - } \ - attribute z_result_t prefix##_##name##_take_loaned(prefix##_owned_##name##_t *obj, \ - prefix##_loaned_##name##_t *src) { \ - f_move((&obj->_val), src); \ - return _Z_RES_OK; \ +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_move, f_drop, \ + attribute) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, attribute) \ + attribute z_result_t prefix##_##name##_take_loaned(prefix##_owned_##name##_t *obj, \ + prefix##_loaned_##name##_t *src) { \ + f_move((&obj->_val), src); \ + return _Z_RES_OK; \ + } + +#define _Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_copy, f_move, f_drop, \ + attribute) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_move, f_drop, attribute) \ + attribute z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, \ + const prefix##_loaned_##name##_t *src) { \ + return f_copy((&obj->_val), src); \ } #define _Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(prefix, type, name, f_check, f_null, f_copy, f_move, f_drop) \ _Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_copy, f_move, f_drop, _ZP_NOTHING) -#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(prefix, type, name, f_check, f_null, f_drop) \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, _ZP_NOTHING) +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX(prefix, type, name, f_check, f_null, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, _ZP_NOTHING) + +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(prefix, type, name, f_check, f_null, f_move, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_move, f_drop, _ZP_NOTHING) -#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(type, name, f_check, f_null, f_drop) \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(z, type, name, f_check, f_null, f_drop) +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL(type, name, f_check, f_null, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX(z, type, name, f_check, f_null, f_drop) + +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(type, name, f_check, f_null, f_move, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(z, type, name, f_check, f_null, f_move, f_drop) #define _Z_OWNED_FUNCTIONS_VALUE_IMPL(type, name, f_check, f_null, f_copy, f_move, f_drop) \ _Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(z, type, name, f_check, f_null, f_copy, f_move, f_drop) -#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_INLINE_IMPL(type, name, f_check, f_null, f_drop) \ - _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_drop, static inline) +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_INLINE_IMPL(type, name, f_check, f_null, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_drop, static inline) + +#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_INLINE_IMPL(type, name, f_check, f_null, f_move, f_drop) \ + _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_move, f_drop, static inline) #define _Z_OWNED_FUNCTIONS_RC_IMPL(name) \ _Z_OWNED_FUNCTIONS_IMPL_MOVE_TAKE(name) \ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 6c10996f1..239fe009d 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1098,10 +1098,10 @@ void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id) _Z_OWNED_FUNCTIONS_DEF(string) _Z_OWNED_FUNCTIONS_DEF(keyexpr) _Z_OWNED_FUNCTIONS_DEF(config) -_Z_OWNED_FUNCTIONS_NO_COPY_DEF(session) -_Z_OWNED_FUNCTIONS_NO_COPY_DEF(subscriber) -_Z_OWNED_FUNCTIONS_NO_COPY_DEF(publisher) -_Z_OWNED_FUNCTIONS_NO_COPY_DEF(queryable) +_Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF(session) +_Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF(subscriber) +_Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF(publisher) +_Z_OWNED_FUNCTIONS_NO_COPY_NO_MOVE_DEF(queryable) _Z_OWNED_FUNCTIONS_DEF(hello) _Z_OWNED_FUNCTIONS_DEF(reply) _Z_OWNED_FUNCTIONS_DEF(string_array) diff --git a/src/api/api.c b/src/api/api.c index 1f2d54d98..7aba4a8f1 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -529,7 +529,7 @@ _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_slice_t, slice, _z_slice_check, _z_slice_empty, _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_bytes_t, bytes, _z_bytes_check, _z_bytes_null, _z_bytes_copy, _z_bytes_move, _z_bytes_drop) _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_bytes_writer_t, bytes_writer, _z_bytes_writer_check, _z_bytes_writer_empty, - _z_bytes_writer_clear) + _z_bytes_writer_move, _z_bytes_writer_clear) #if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 // Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid @@ -790,8 +790,8 @@ void _z_publisher_drop(_z_publisher_t *pub) { _z_publisher_clear(pub); } -_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_publisher_t, publisher, _z_publisher_check, _z_publisher_null, - _z_publisher_drop) +_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL(_z_publisher_t, publisher, _z_publisher_check, _z_publisher_null, + _z_publisher_drop) void z_put_options_default(z_put_options_t *options) { options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; @@ -1127,8 +1127,8 @@ void _z_queryable_drop(_z_queryable_t *queryable) { _z_queryable_clear(queryable); } -_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_queryable_t, queryable, _z_queryable_check, _z_queryable_null, - _z_queryable_drop) +_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL(_z_queryable_t, queryable, _z_queryable_check, _z_queryable_null, + _z_queryable_drop) void z_queryable_options_default(z_queryable_options_t *options) { options->complete = _Z_QUERYABLE_COMPLETE_DEFAULT; } @@ -1346,8 +1346,8 @@ void _z_subscriber_drop(_z_subscriber_t *sub) { _z_subscriber_clear(sub); } -_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_subscriber_t, subscriber, _z_subscriber_check, _z_subscriber_null, - _z_subscriber_drop) +_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_NO_MOVE_IMPL(_z_subscriber_t, subscriber, _z_subscriber_check, _z_subscriber_null, + _z_subscriber_drop) void z_subscriber_options_default(z_subscriber_options_t *options) { options->__dummy = 0; } diff --git a/src/api/serialization.c b/src/api/serialization.c index c6b018593..37bd94982 100644 --- a/src/api/serialization.c +++ b/src/api/serialization.c @@ -25,9 +25,12 @@ _ze_serializer_t _ze_serializer_empty(void) { } void _ze_serializer_clear(_ze_serializer_t *serializer) { _z_bytes_writer_clear(&serializer->_writer); } +void _ze_serializer_move(_ze_serializer_t *dst, _ze_serializer_t *src) { + _z_bytes_writer_move(&dst->_writer, &dst->_writer); +} _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(ze, _ze_serializer_t, serializer, _ze_serializer_check, - _ze_serializer_empty, _ze_serializer_clear) + _ze_serializer_empty, _ze_serializer_move, _ze_serializer_clear) z_result_t ze_serializer_empty(ze_owned_serializer_t *serializer) { serializer->_val._writer = _z_bytes_writer_empty();