Skip to content

Commit

Permalink
store value instead of pointer for z_sample and z_reply
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jul 2, 2024
1 parent 27fb58e commit 55c685e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
13 changes: 3 additions & 10 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
Expand All @@ -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)

/**
Expand Down
5 changes: 3 additions & 2 deletions include/zenoh-pico/net/reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/net/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
27 changes: 18 additions & 9 deletions src/net/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions src/net/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 55c685e

Please sign in to comment.