Skip to content

Commit

Permalink
fixed svec implementation;
Browse files Browse the repository at this point in the history
fixed z_client_test checking string length;
minor readability fixes;
  • Loading branch information
DenisBiryukov91 committed Jun 25, 2024
1 parent ea30ca1 commit 5f8e54f
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st
}
// Convert bytes to string
size_t len = _z_bytes_len(bytes);
*s->_val = _z_string_preallocate(_z_bytes_len(bytes));
*s->_val = _z_string_preallocate(len);
if (s->_val->len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
_z_bytes_to_buf(bytes, (uint8_t *)s->_val->val, len);
return _Z_RES_OK;
Expand Down
3 changes: 1 addition & 2 deletions src/collections/arc_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ int8_t _z_arc_slice_move(_z_arc_slice_t* dst, _z_arc_slice_t* src) {

int8_t _z_arc_slice_drop(_z_arc_slice_t* s) {
_z_slice_rc_drop(&s->slice);
s->len = 0;
s->start = 0;
*s = _z_arc_slice_empty();
return _Z_RES_OK;
}
4 changes: 2 additions & 2 deletions src/collections/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _z_bytes_t _z_bytes_null(void) {

int8_t _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) {
_z_arc_slice_svec_copy(&dst->_slices, &src->_slices);
if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&dst->_slices)) {
if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&src->_slices)) {
return _Z_RES_OK;
} else {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
Expand Down Expand Up @@ -93,7 +93,7 @@ size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len) {
size_t s_len = _z_arc_slice_len(s);
size_t len_to_copy = remaining >= s_len ? s_len : remaining;
memcpy(start, _z_arc_slice_data(s), len_to_copy);
start += s_len;
start += len_to_copy;
remaining -= len_to_copy;
}

Expand Down
2 changes: 1 addition & 1 deletion src/collections/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void __z_svec_move_inner(void *dst, void *src, z_element_move_f move, size_t num
void _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size) {
dst->_capacity = 0;
dst->_len = 0;
dst->_val = z_malloc(sizeof(void *) * src->_capacity);
dst->_val = z_malloc(element_size * src->_capacity);
if (dst->_val != NULL) {
dst->_capacity = src->_capacity;
dst->_len = src->_len;
Expand Down
8 changes: 4 additions & 4 deletions src/net/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id,
reply.data.replier_id = id;
// Create sample
_z_sample_t sample = _z_sample_null();
sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy
sample.encoding = encoding; // FIXME: call z_encoding_move or copy
_z_bytes_copy(&sample.payload, &payload); // FIXME: call z_bytes_move or copy
sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy
sample.encoding = encoding; // FIXME: call z_encoding_move or copy
_z_bytes_copy(&sample.payload, &payload);
sample.kind = kind;
sample.timestamp = _z_timestamp_duplicate(timestamp);
_z_bytes_copy(&sample.attachment, &attachment); // FIXME: call z_bytes_move or copy
_z_bytes_copy(&sample.attachment, &attachment);

// Create sample rc from value
reply.data.sample = _z_sample_rc_new_from_val(sample);
Expand Down
12 changes: 3 additions & 9 deletions src/protocol/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,17 @@ int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) {
}

int8_t _z_slice_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;

ret |= _z_zsize_decode(&bs->len, zbf);
ret |= _z_slice_val_decode_na(bs, zbf);

return ret;
_Z_RETURN_IF_ERR(_z_zsize_decode(&bs->len, zbf));
return _z_slice_val_decode_na(bs, zbf);
}

int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_val_decode_na(bs, zbf); }

int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_na(bs, zbf); }

int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) {
int8_t ret = _Z_RES_OK;
_z_slice_t s;
ret = _z_slice_decode(&s, zbf);
if (ret != _Z_RES_OK) return ret;
_Z_RETURN_IF_ERR(_z_slice_decode(&s, zbf));
return _z_bytes_from_slice(bs, s);
}

Expand Down
4 changes: 1 addition & 3 deletions src/protocol/codec/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) {
}
case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload
_z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val);
_Z_RETURN_IF_ERR(_z_encoding_decode(&msg->_ext_value.encoding, &zbf));
_Z_RETURN_IF_ERR(
_z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)));
ret = _z_value_decode(&msg->_ext_value, &zbf);
break;
}
case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment
Expand Down
2 changes: 1 addition & 1 deletion tests/z_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) {
z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
assert(z_string_len(z_loan(value)) == strlen(res) + 1);
assert(z_string_len(z_loan(value)) == strlen(res));
assert(strncmp(res, z_string_data(z_loan(value)), strlen(res)) == 0);
assert(_z_str_eq(z_loan(k_str)->val, res) == true);

Expand Down

0 comments on commit 5f8e54f

Please sign in to comment.