Skip to content

Commit

Permalink
feat: svec ops return error instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Nov 2, 2024
1 parent a617600 commit adc2e35
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 38 deletions.
12 changes: 6 additions & 6 deletions include/zenoh-pico/collections/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ typedef struct {
static inline _z_svec_t _z_svec_null(void) { return (_z_svec_t){0}; }
static inline _z_svec_t _z_svec_alias(const _z_svec_t *src) { return *src; }
_z_svec_t _z_svec_make(size_t capacity, size_t element_size);
bool _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size);
z_result_t _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size);
void _z_svec_move(_z_svec_t *dst, _z_svec_t *src);

size_t _z_svec_len(const _z_svec_t *v);
bool _z_svec_is_empty(const _z_svec_t *v);

bool _z_svec_expand(_z_svec_t *v, z_element_move_f move, size_t element_size);
bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f m, size_t element_size);
z_result_t _z_svec_expand(_z_svec_t *v, z_element_move_f move, size_t element_size);
z_result_t _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f m, size_t element_size);
void *_z_svec_get(const _z_svec_t *v, size_t pos, size_t element_size);
void _z_svec_set(_z_svec_t *sv, size_t pos, void *e, z_element_clear_f f, size_t element_size);
void _z_svec_remove(_z_svec_t *sv, size_t pos, z_element_clear_f f, z_element_move_f m, size_t element_size);
Expand All @@ -104,10 +104,10 @@ void _z_svec_release(_z_svec_t *v);
static inline name##_svec_t name##_svec_make(size_t capacity) { return _z_svec_make(capacity, sizeof(type)); } \
static inline size_t name##_svec_len(const name##_svec_t *v) { return _z_svec_len(v); } \
static inline bool name##_svec_is_empty(const name##_svec_t *v) { return _z_svec_is_empty(v); } \
static inline bool name##_svec_expend(name##_svec_t *v) { \
static inline z_result_t name##_svec_expand(name##_svec_t *v) { \
return _z_svec_expand(v, name##_elem_move, sizeof(type)); \
} \
static inline bool name##_svec_append(name##_svec_t *v, const type *e) { \
static inline z_result_t name##_svec_append(name##_svec_t *v, const type *e) { \
return _z_svec_append(v, e, name##_elem_move, sizeof(type)); \
} \
static inline type *name##_svec_get(const name##_svec_t *v, size_t pos) { \
Expand All @@ -119,7 +119,7 @@ void _z_svec_release(_z_svec_t *v);
static inline void name##_svec_remove(name##_svec_t *v, size_t pos) { \
_z_svec_remove(v, pos, name##_elem_clear, name##_elem_move, sizeof(type)); \
} \
static inline bool name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \
static inline z_result_t name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \
return _z_svec_copy(dst, src, name##_elem_copy, sizeof(type)); \
} \
static inline name##_svec_t name##_svec_alias(const name##_svec_t *v) { return _z_svec_alias(v); } \
Expand Down
3 changes: 1 addition & 2 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ z_result_t z_whatami_to_view_string(z_whatami_t whatami, z_view_string_t *str_ou

bool _z_string_array_check(const _z_string_svec_t *val) { return !_z_string_svec_is_empty(val); }
z_result_t _z_string_array_copy(_z_string_svec_t *dst, const _z_string_svec_t *src) {
_z_string_svec_copy(dst, src);
return dst->_len == src->_len ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY;
return _z_string_svec_copy(dst, src);
}
_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)
Expand Down
12 changes: 3 additions & 9 deletions src/collections/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); }

z_result_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(&src->_slices)) {
return _Z_RES_OK;
} else {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
return _z_arc_slice_svec_copy(&dst->_slices, &src->_slices);
}

_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src) {
Expand Down Expand Up @@ -98,7 +93,7 @@ z_result_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) {
*b = _z_bytes_null();
_z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, s.len);
if (_z_arc_slice_len(&arc_s) != s.len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
return _z_arc_slice_svec_append(&b->_slices, &arc_s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY;
return _z_arc_slice_svec_append(&b->_slices, &arc_s);
}

z_result_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len) {
Expand Down Expand Up @@ -130,8 +125,7 @@ z_result_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) {
}

z_result_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) {
_Z_CLEAN_RETURN_IF_ERR(_z_arc_slice_svec_append(&dst->_slices, s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY,
_z_arc_slice_drop(s));
_Z_CLEAN_RETURN_IF_ERR(_z_arc_slice_svec_append(&dst->_slices, s), _z_arc_slice_drop(s));
return _Z_RES_OK;
}

Expand Down
30 changes: 14 additions & 16 deletions src/collections/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ _z_svec_t _z_svec_make(size_t capacity, size_t element_size) {
}

void __z_svec_copy_inner(void *dst, const void *src, z_element_copy_f copy, size_t num_elements, size_t element_size) {
if (copy == NULL) {
if (copy == _z_noop_copy) {
memcpy(dst, src, num_elements * element_size);
} else {
size_t offset = 0;
Expand Down Expand Up @@ -174,16 +174,17 @@ void _z_svec_move(_z_svec_t *dst, _z_svec_t *src) {
*src = _z_svec_null();
}

bool _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size) {
z_result_t _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(element_size * src->_capacity);
if (dst->_val != NULL) {
dst->_capacity = src->_capacity;
dst->_len = src->_len;
__z_svec_copy_inner(dst->_val, src->_val, copy, src->_len, element_size);
if (dst->_val == NULL) {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
return dst->_len == src->_len;
dst->_capacity = src->_capacity;
dst->_len = src->_len;
__z_svec_copy_inner(dst->_val, src->_val, copy, src->_len, element_size);
return _Z_RES_OK;
}

void _z_svec_reset(_z_svec_t *v, z_element_clear_f clear, size_t element_size) {
Expand Down Expand Up @@ -225,33 +226,30 @@ size_t _z_svec_len(const _z_svec_t *v) { return v->_len; }

bool _z_svec_is_empty(const _z_svec_t *v) { return v->_len == 0; }

bool _z_svec_expand(_z_svec_t *v, z_element_move_f move, size_t element_size) {
z_result_t _z_svec_expand(_z_svec_t *v, z_element_move_f move, size_t element_size) {
// Allocate a new vector
size_t _capacity = v->_capacity == 0 ? 1 : (v->_capacity << 1);
void *_val = (void *)z_malloc(_capacity * element_size);
if (_val == NULL) {
return false;
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
__z_svec_move_inner(_val, v->_val, move, v->_len, element_size);
// Free the old data
z_free(v->_val);

// Update the current vector
v->_val = _val;
v->_capacity = _capacity;
return true;
return _Z_RES_OK;
}

bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t element_size) {
z_result_t _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t element_size) {
if (v->_len == v->_capacity) {
if (!_z_svec_expand(v, move, element_size)) {
return false;
}
_Z_RETURN_IF_ERR(_z_svec_expand(v, move, element_size));
}
// Append element
memcpy((uint8_t *)v->_val + v->_len * element_size, e, element_size);
v->_len++;
return true;
return _Z_RES_OK;
}

void *_z_svec_get(const _z_svec_t *v, size_t i, size_t element_size) {
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ z_result_t _z_value_copy(_z_value_t *dst, const _z_value_t *src) {

z_result_t _z_hello_copy(_z_hello_t *dst, const _z_hello_t *src) {
*dst = _z_hello_null();
_Z_RETURN_IF_ERR(_z_string_svec_copy(&dst->_locators, &src->_locators) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY);
_Z_RETURN_IF_ERR(_z_string_svec_copy(&dst->_locators, &src->_locators));
dst->_version = src->_version;
dst->_whatami = src->_whatami;
memcpy(&dst->_zid.id, &src->_zid.id, _Z_ID_LEN);
Expand Down
5 changes: 1 addition & 4 deletions src/session/subscription.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ static z_result_t __unsafe_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t
if (_z_keyexpr_suffix_intersects(&_Z_RC_IN_VAL(sub)->_key, key)) {
_z_subscription_infos_t new_sub_info = {.arg = _Z_RC_IN_VAL(sub)->_arg,
.callback = _Z_RC_IN_VAL(sub)->_callback};
if (!_z_subscription_infos_svec_append(sub_infos, &new_sub_info)) {
_Z_ERROR("Failed to allocate subscription info vector");
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
_Z_RETURN_IF_ERR(_z_subscription_infos_svec_append(sub_infos, &new_sub_info));
}
xs = _z_subscription_rc_list_tail(xs);
}
Expand Down

0 comments on commit adc2e35

Please sign in to comment.