From 55c685eea41f18d8961387f2321c79f874715327 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 2 Jul 2024 16:49:27 +0200 Subject: [PATCH] store value instead of pointer for z_sample and z_reply --- include/zenoh-pico/api/types.h | 13 +++---------- include/zenoh-pico/net/reply.h | 5 +++-- include/zenoh-pico/net/sample.h | 2 +- src/api/api.c | 4 ++-- src/net/reply.c | 27 ++++++++++++++++++--------- src/net/sample.c | 18 +++++++++++------- 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index a2ed9349f..71afc6d70 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -395,15 +395,8 @@ static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } * * A sample is the value associated to a given key-expression at a given point in time. * - * Members: - * z_keyexpr_t keyexpr: The keyexpr of this data sample. - * z_loaned_bytes_t* payload: The value of this data sample. - * z_loaned_encoding_t encoding: The encoding of the value of this data sample. - * z_sample_kind_t kind: The kind of this data sample (PUT or DELETE). - * z_timestamp_t timestamp: The timestamp of this data sample. - * z_qos_t qos: Quality of service settings used to deliver this sample. - */ -_Z_OWNED_TYPE_PTR(_z_sample_t, sample) + */ +_Z_OWNED_TYPE_VALUE(_z_sample_t, sample) _Z_LOANED_TYPE(_z_sample_t, sample) /** @@ -420,7 +413,7 @@ _Z_LOANED_TYPE(_z_hello_t, hello) /** * Represents the reply to a query. */ -_Z_OWNED_TYPE_PTR(_z_reply_t, reply) +_Z_OWNED_TYPE_VALUE(_z_reply_t, reply) _Z_LOANED_TYPE(_z_reply_t, reply) /** diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index dca38a666..989038471 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -37,7 +37,7 @@ typedef struct _z_reply_data_t { } _z_reply_data_t; void _z_reply_data_clear(_z_reply_data_t *rd); -void _z_reply_data_copy(_z_reply_data_t *dst, const _z_reply_data_t *src); +int8_t _z_reply_data_copy(_z_reply_data_t *dst, const _z_reply_data_t *src); _z_reply_t _z_reply_move(_z_reply_t *src_reply); _Z_ELEM_DEFINE(_z_reply_data, _z_reply_data_t, _z_noop_size, _z_reply_data_clear, _z_noop_copy) @@ -58,9 +58,10 @@ typedef struct _z_reply_t { } _z_reply_t; _z_reply_t _z_reply_null(void); +_Bool _z_reply_check(const _z_reply_t *reply); void _z_reply_clear(_z_reply_t *src); void _z_reply_free(_z_reply_t **hello); -void _z_reply_copy(_z_reply_t *dst, const _z_reply_t *src); +int8_t _z_reply_copy(_z_reply_t *dst, const _z_reply_t *src); _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, const _z_bytes_t attachment); diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index 5dee423d7..c735ea701 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -51,7 +51,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src); */ void _z_sample_free(_z_sample_t **sample); -void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); +int8_t _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); _z_sample_t _z_sample_create(_z_keyexpr_t *key, const _z_bytes_t payload, _z_timestamp_t timestamp, diff --git a/src/api/api.c b/src/api/api.c index aec73a739..4ace5b3d2 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -667,7 +667,7 @@ static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) } #endif -_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_sample_t, sample, _z_sample_copy, _z_sample_free) +_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_sample_t, sample, _z_sample_check, _z_sample_null, _z_sample_copy, _z_sample_clear) _Z_OWNED_FUNCTIONS_RC_IMPL(session) _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_sample, _z_data_handler_t, z_dropper_handler_t) @@ -988,7 +988,7 @@ z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher) { #endif #if Z_FEATURE_QUERY == 1 -_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_reply_t, reply, _z_reply_copy, _z_reply_free) +_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_reply_t, reply, _z_reply_check, _z_reply_null, _z_reply_copy, _z_reply_clear) void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); diff --git a/src/net/reply.c b/src/net/reply.c index 06d63128e..ce04f8a5e 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -17,15 +17,20 @@ #include "zenoh-pico/session/utils.h" #include "zenoh-pico/utils/logging.h" +_z_reply_data_t _z_reply_data_null(void) { + return (_z_reply_data_t){ + .replier_id = {.id = {0}}, + .sample = _z_sample_null(), + }; +} + _z_reply_t _z_reply_null(void) { - _z_reply_t r = {._tag = Z_REPLY_TAG_DATA, - .data = { - .replier_id = {.id = {0}}, - .sample = _z_sample_null(), - }}; + _z_reply_t r = {._tag = Z_REPLY_TAG_DATA, .data = _z_reply_data_null()}; return r; } +_Bool _z_reply_check(const _z_reply_t *reply) { return _z_sample_check(&reply->data.sample); } + #if Z_FEATURE_QUERY == 1 void _z_reply_data_clear(_z_reply_data_t *reply_data) { _z_sample_clear(&reply_data->sample); @@ -42,9 +47,11 @@ void _z_reply_data_free(_z_reply_data_t **reply_data) { } } -void _z_reply_data_copy(_z_reply_data_t *dst, const _z_reply_data_t *src) { +int8_t _z_reply_data_copy(_z_reply_data_t *dst, const _z_reply_data_t *src) { + *dst = _z_reply_data_null(); + _Z_RETURN_IF_ERR(_z_sample_copy(&dst->sample, &src->sample)); dst->replier_id = src->replier_id; - _z_sample_copy(&dst->sample, &src->sample); + return _Z_RES_OK; } _z_reply_t _z_reply_move(_z_reply_t *src_reply) { @@ -66,9 +73,11 @@ void _z_reply_free(_z_reply_t **reply) { } } -void _z_reply_copy(_z_reply_t *dst, const _z_reply_t *src) { - _z_reply_data_copy(&dst->data, &src->data); +int8_t _z_reply_copy(_z_reply_t *dst, const _z_reply_t *src) { + *dst = _z_reply_null(); + _Z_RETURN_IF_ERR(_z_reply_data_copy(&dst->data, &src->data)); dst->_tag = src->_tag; + return _Z_RES_OK; } _Bool _z_pending_reply_eq(const _z_pending_reply_t *one, const _z_pending_reply_t *two) { diff --git a/src/net/sample.c b/src/net/sample.c index 6c27e64f8..ec056f0a1 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -30,7 +30,8 @@ _z_sample_t _z_sample_null(void) { } _Bool _z_sample_check(const _z_sample_t *sample) { - return _z_keyexpr_check(&sample->keyexpr) && _z_bytes_check(&sample->payload); + return _z_keyexpr_check(&sample->keyexpr) || _z_bytes_check(&sample->payload) || + _z_bytes_check(&sample->attachment) || _z_encoding_check(&sample->encoding); } void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { @@ -40,6 +41,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { _z_bytes_move(&dst->payload, &src->payload); _z_encoding_move(&dst->encoding, &src->encoding); + _z_bytes_move(&dst->attachment, &src->attachment); dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move dst->timestamp.id = src->timestamp.id; // FIXME: call the z_timestamp_move @@ -62,13 +64,15 @@ void _z_sample_free(_z_sample_t **sample) { } } -void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src) { - dst->keyexpr = _z_keyexpr_duplicate(src->keyexpr); - _z_bytes_copy(&dst->payload, &src->payload); - dst->timestamp = _z_timestamp_duplicate(&src->timestamp); - _z_encoding_copy(&dst->encoding, &src->encoding); +int8_t _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src) { + *dst = _z_sample_null(); + _Z_RETURN_IF_ERR(_z_keyexpr_copy(&dst->keyexpr, &src->keyexpr)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_copy(&dst->payload, &src->payload), _z_sample_clear(dst)); + _Z_CLEAN_RETURN_IF_ERR(_z_encoding_copy(&dst->encoding, &src->encoding), _z_sample_clear(dst)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_copy(&dst->attachment, &src->attachment), _z_sample_clear(dst)); dst->kind = src->kind; - _z_bytes_copy(&dst->attachment, &src->attachment); + dst->timestamp = _z_timestamp_duplicate(&src->timestamp); + return _Z_RES_OK; } _z_sample_t _z_sample_duplicate(const _z_sample_t *src) {