Skip to content

Commit

Permalink
Switch keyexpr and locator to non null terminated strings (#602)
Browse files Browse the repository at this point in the history
* feat: add _z_string and _z_slice methods

* feat: remove null-terminated from _z_string

* feat: switch locators to _z_string

* feat: switch _z_keyexpr to _z_string

* fix: remove obsolete functions

* tests: update tests without null terminated strings

* feat: add _z_string_pbrk

* fix: use _z_keyexpr_has_suffix

* doc: remove functions from doc

* feat: remove keyexpr_set_owns mechanism

* feat: add z_string_from_substr

* feat: remove obsolete functions

* fix: canonizer

* fix: use z_string_alias
  • Loading branch information
jean-roland authored Aug 23, 2024
1 parent 459d9b7 commit fd99822
Show file tree
Hide file tree
Showing 47 changed files with 962 additions and 1,071 deletions.
5 changes: 0 additions & 5 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,10 @@ Primitives
.. autocfunction:: primitives.h::z_view_keyexpr_from_str_autocanonize
.. autocfunction:: primitives.h::z_keyexpr_as_view_string
.. autocfunction:: primitives.h::z_keyexpr_is_canon
.. autocfunction:: primitives.h::zp_keyexpr_is_canon_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_canonize
.. autocfunction:: primitives.h::zp_keyexpr_canonize_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_includes
.. autocfunction:: primitives.h::zp_keyexpr_includes_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_intersects
.. autocfunction:: primitives.h::zp_keyexpr_intersect_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_equals
.. autocfunction:: primitives.h::zp_keyexpr_equals_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_relation_to
.. autocfunction:: primitives.h::z_keyexpr_concat
.. autocfunction:: primitives.h::z_keyexpr_join
Expand Down
71 changes: 0 additions & 71 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,6 @@ z_keyexpr_intersection_level_t z_keyexpr_relation_to(const z_loaned_keyexpr_t *l
*/
int8_t z_keyexpr_is_canon(const char *start, size_t len);

/**
* Checks if a given keyexpr is valid and in canonical form.
*
* Parameters:
* start: Pointer to the keyexpr in its string representation as a null terminated string.
* len: Number of characters in ``start``.
*
* Return:
* ``0`` if passed string is a valid (and canon) key expression, or a ``negative value`` otherwise.
* Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`.
*/
int8_t zp_keyexpr_is_canon_null_terminated(const char *start);

/**
* Canonizes of a given keyexpr in string representation.
* The canonization is performed over the passed string, possibly shortening it by modifying ``len``.
Expand All @@ -187,20 +174,6 @@ int8_t zp_keyexpr_is_canon_null_terminated(const char *start);
*/
int8_t z_keyexpr_canonize(char *start, size_t *len);

/**
* Canonizes a given keyexpr in string representation.
* The canonization is performed over the passed string, possibly shortening it by modifying ``len``.
*
* Parameters:
* start: Pointer to the keyexpr in its string representation as a null terminated string.
* len: Number of characters in ``start``.
*
* Return:
* ``0`` if canonization successful, or a ``negative value`` otherwise.
* Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`.
*/
int8_t zp_keyexpr_canonize_null_terminated(char *start);

/**
* Checks if a given keyexpr contains another keyexpr in its set.
*
Expand All @@ -214,21 +187,6 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start);
*/
_Bool z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r);

/**
* Checks if a given keyexpr contains another keyexpr in its set.
*
* Parameters:
* l: Pointer to the keyexpr in its string representation as a null terminated string.
* llen: Number of characters in ``l``.
* r: Pointer to the keyexpr in its string representation as a null terminated string.
* rlen: Number of characters in ``r``.
*
* Return:
* ``true`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set
* defined by ``r``. Otherwise, returns ``false``.
*/
_Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r);

/**
* Checks if a given keyexpr intersects with another keyexpr.
*
Expand All @@ -242,21 +200,6 @@ _Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r);
*/
_Bool z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r);

/**
* Checks if a given keyexpr intersects with another keyexpr.
*
* Parameters:
* l: Pointer to the keyexpr in its string representation as a null terminated string.
* llen: Number of characters in ``l``.
* r: Pointer to the keyexpr in its string representation as a null terminated string.
* rlen: Number of characters in ``r``.
*
* Return:
* ``true`` if keyexprs intersect, i.e. there exists at least one key which is contained in both of the
* sets defined by ``l`` and ``r``. Otherwise, returns ``false``.
*/
_Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r);

/**
* Checks if two keyexpr are equal.
*
Expand All @@ -269,20 +212,6 @@ _Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r);
*/
_Bool z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r);

/**
* Checks if two keyexpr as null terminated string are equal.
*
* Parameters:
* l: Pointer to the keyexpr in its string representation as a null terminated string.
* llen: Number of characters in ``l``.
* r: Pointer to the keyexpr in its string representation as a null terminated string.
* rlen: Number of characters in ``r``.
*
* Return:
* ``true`` if both ``l`` and ``r`` are equal. Otherwise, it returns ``false``.
*/
_Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r);

/**
* Builds a new, zenoh-allocated, empty configuration.
* It consists in an empty set of properties for zenoh session configuration.
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ _z_slice_t _z_slice_from_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_from_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc);
_z_slice_t _z_slice_copy_from_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_steal(_z_slice_t *b);
_z_slice_t _z_slice_alias(const _z_slice_t *bs);
int8_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src);
int8_t _z_slice_n_copy(_z_slice_t *dst, const _z_slice_t *src, size_t offset, size_t len);
_z_slice_t _z_slice_duplicate(const _z_slice_t *src);
void _z_slice_move(_z_slice_t *dst, _z_slice_t *src);
void _z_slice_reset(_z_slice_t *bs);
Expand Down
6 changes: 6 additions & 0 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ _Bool _z_string_check(const _z_string_t *value);
_z_string_t _z_string_make(const char *value);
_z_string_t _z_string_n_make(const char *value, size_t len);
_z_string_t _z_string_from_str(const char *value);
_z_string_t _z_string_from_substr(const char *value, size_t len);
_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c);
_z_string_t *_z_string_make_as_ptr(const char *value);
_Bool _z_string_is_empty(const _z_string_t *s);
const char *_z_string_rchr(_z_string_t *str, char filter);
char *_z_string_pbrk(_z_string_t *str, const char *filter);

size_t _z_string_len(const _z_string_t *s);
const char *_z_string_data(const _z_string_t *s);
int8_t _z_string_copy(_z_string_t *dst, const _z_string_t *src);
int8_t _z_string_copy_substring(_z_string_t *dst, const _z_string_t *src, size_t offset, size_t len);
void _z_string_move(_z_string_t *dst, _z_string_t *src);
_z_string_t _z_string_steal(_z_string_t *str);
_z_string_t _z_string_alias(const _z_string_t *str);
void _z_string_move_str(_z_string_t *dst, char *src);
void _z_string_clear(_z_string_t *s);
void _z_string_free(_z_string_t **s);
void _z_string_reset(_z_string_t *s);
_Bool _z_string_equals(const _z_string_t *left, const _z_string_t *right);
_z_string_t _z_string_convert_bytes(const _z_slice_t *bs);
_z_string_t _z_string_preallocate(const size_t len);

Expand Down
6 changes: 3 additions & 3 deletions include/zenoh-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#define INCLUDE_ZENOH_PICO_CONFIG_H

/*--- CMake generated config; pass values to CMake to change the following tokens ---*/
#define Z_FRAG_MAX_SIZE 4096
#define Z_BATCH_UNICAST_SIZE 2048
#define Z_BATCH_MULTICAST_SIZE 2048
#define Z_FRAG_MAX_SIZE 300000
#define Z_BATCH_UNICAST_SIZE 65535
#define Z_BATCH_MULTICAST_SIZE 8096
#define Z_CONFIG_SOCKET_TIMEOUT 100

#define Z_FEATURE_MULTI_THREAD 1
Expand Down
10 changes: 5 additions & 5 deletions include/zenoh-pico/link/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
#define LOCATOR_METADATA_SEPARATOR '?'
typedef struct {
_z_str_intmap_t _metadata;
char *_protocol;
char *_address;
_z_string_t _protocol;
_z_string_t _address;
} _z_locator_t;

_Bool _z_locator_eq(const _z_locator_t *left, const _z_locator_t *right);

void _z_locator_init(_z_locator_t *locator);
_z_string_t _z_locator_to_string(const _z_locator_t *loc);
int8_t _z_locator_from_str(_z_locator_t *lc, const char *s);
int8_t _z_locator_from_string(_z_locator_t *lc, _z_string_t *s);

size_t _z_locator_size(_z_locator_t *lc);
void _z_locator_clear(_z_locator_t *lc);
Expand All @@ -67,8 +67,8 @@ typedef struct {
_z_str_intmap_t _config;
} _z_endpoint_t;

char *_z_endpoint_to_str(const _z_endpoint_t *e);
int8_t _z_endpoint_from_str(_z_endpoint_t *ep, const char *s);
_z_string_t _z_endpoint_to_string(const _z_endpoint_t *e);
int8_t _z_endpoint_from_string(_z_endpoint_t *ep, _z_string_t *s);
void _z_endpoint_clear(_z_endpoint_t *ep);
void _z_endpoint_free(_z_endpoint_t **ep);

Expand Down
4 changes: 2 additions & 2 deletions include/zenoh-pico/link/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ typedef struct _z_link_t {

void _z_link_clear(_z_link_t *zl);
void _z_link_free(_z_link_t **zl);
int8_t _z_open_link(_z_link_t *zl, const char *locator);
int8_t _z_listen_link(_z_link_t *zl, const char *locator);
int8_t _z_open_link(_z_link_t *zl, _z_string_t *locator);
int8_t _z_listen_link(_z_link_t *zl, _z_string_t *locator);

int8_t _z_link_send_wbuf(const _z_link_t *zl, const _z_wbuf_t *wbf);
size_t _z_link_recv_zbuf(const _z_link_t *zl, _z_zbuf_t *zbf, _z_slice_t *addr);
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/net/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* locator: The locator where to scout.
* timeout: The time that should be spent scouting before returning the results.
*/
void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const uint32_t timeout,
void _z_scout(const z_what_t what, const _z_id_t zid, _z_string_t *locator, const uint32_t timeout,
_z_hello_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop);

/*------------------ Declarations ------------------*/
Expand Down
23 changes: 10 additions & 13 deletions include/zenoh-pico/protocol/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ typedef struct {
#define _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE 0x7fff

/**
* A zenoh-net resource key.
* A zenoh key-expression.
*
* Members:
* _z_zint_t: The resource ID.
* char *val: A pointer to the string containing the resource name.
* uint16_t _id: The resource ID of the ke.
* _z_mapping_t _mapping: The resource mapping of the ke.
* _z_string_t _suffix: The string value of the ke.
*/
typedef struct {
uint16_t _id;
_z_mapping_t _mapping;
char *_suffix;
_z_string_t _suffix;
} _z_keyexpr_t;
static inline _Bool _z_keyexpr_owns_suffix(const _z_keyexpr_t *key) { return (key->_mapping._val & 0x8000) != 0; }

static inline uint16_t _z_keyexpr_mapping_id(const _z_keyexpr_t *key) { return key->_mapping._val & 0x7fff; }
static inline _Bool _z_keyexpr_is_local(const _z_keyexpr_t *key) {
return (key->_mapping._val & 0x7fff) == _Z_KEYEXPR_MAPPING_LOCAL;
}
static inline _z_mapping_t _z_keyexpr_mapping(uint16_t id, _Bool owns_suffix) {
static inline _z_mapping_t _z_keyexpr_mapping(uint16_t id) {
assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE);
_z_mapping_t mapping = {(uint16_t)((owns_suffix ? 0x8000 : 0) | id)};
_z_mapping_t mapping = {id};
return mapping;
}
static inline void _z_keyexpr_set_mapping(_z_keyexpr_t *ke, uint16_t id) {
Expand All @@ -118,12 +119,8 @@ static inline void _z_keyexpr_fix_mapping(_z_keyexpr_t *ke, uint16_t id) {
_z_keyexpr_set_mapping(ke, id);
}
}
static inline void _z_keyexpr_set_owns_suffix(_z_keyexpr_t *ke, _Bool owns_suffix) {
ke->_mapping._val &= 0x7fff;
ke->_mapping._val |= owns_suffix ? 0x8000 : 0;
}
static inline _Bool _z_keyexpr_has_suffix(_z_keyexpr_t ke) { return (ke._suffix != NULL) && (ke._suffix[0] != 0); }
static inline _Bool _z_keyexpr_check(const _z_keyexpr_t *ke) { return (ke->_id != 0) || _z_keyexpr_has_suffix(*ke); }
static inline _Bool _z_keyexpr_has_suffix(const _z_keyexpr_t *ke) { return _z_string_check(&ke->_suffix); }
static inline _Bool _z_keyexpr_check(const _z_keyexpr_t *ke) { return (ke->_id != 0) || _z_keyexpr_has_suffix(ke); }

/**
* Create a resource key from a resource name.
Expand Down
11 changes: 7 additions & 4 deletions include/zenoh-pico/protocol/keyexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@

zp_keyexpr_canon_status_t _z_keyexpr_is_canon(const char *start, size_t len);
zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len);
_Bool _z_keyexpr_includes(const char *lstart, const size_t llen, const char *rstart, const size_t rlen);
_Bool _z_keyexpr_intersects(const char *lstart, const size_t llen, const char *rstart, const size_t rlen);
_Bool _z_keyexpr_suffix_includes(const _z_keyexpr_t *left, const _z_keyexpr_t *right);
_Bool _z_keyexpr_suffix_intersects(const _z_keyexpr_t *left, const _z_keyexpr_t *right);
_Bool _z_keyexpr_suffix_equals(const _z_keyexpr_t *left, const _z_keyexpr_t *right);

/*------------------ clone/Copy/Free helpers ------------------*/
_z_keyexpr_t _z_keyexpr_from_string(uint16_t rid, _z_string_t *str);
int8_t _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src);
_z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src);
_z_keyexpr_t _z_keyexpr_to_owned(_z_keyexpr_t src);
_z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src);
/// Returns either keyexpr defined by id + mapping with null suffix if try_declared is true and id is non-zero,
/// or keyexpr defined by its suffix only, with 0 id and no mapping. This is to be used only when forwarding
/// keyexpr in user api to properly separate declard keyexpr from its suffix.
_z_keyexpr_t _z_keyexpr_alias_from_user_defined(_z_keyexpr_t src, _Bool try_declared);
_z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src);
static inline _z_keyexpr_t _z_keyexpr_null(void) {
_z_keyexpr_t keyexpr = {0, {0}, NULL};
_z_keyexpr_t keyexpr = {0, {0}, _z_string_null()};
return keyexpr;
}
_Bool _z_keyexpr_equals(const _z_keyexpr_t *left, const _z_keyexpr_t *right);
void _z_keyexpr_move(_z_keyexpr_t *dst, _z_keyexpr_t *src);
void _z_keyexpr_clear(_z_keyexpr_t *rk);
void _z_keyexpr_free(_z_keyexpr_t **rk);

Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/session/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "zenoh-pico/protocol/core.h"

/*------------------ Session ------------------*/
_z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t id, const char *locator, const uint32_t timeout,
_z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t id, _z_string_t *locator, const uint32_t timeout,
const _Bool exit_on_first);

int8_t _z_session_init(_z_session_rc_t *zsrc, _z_id_t *zid);
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/system/link/raweth.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// Max frame size
#define _ZP_MAX_ETH_FRAME_SIZE 1514

// Endpoing config types
// Endpoint config types
typedef struct {
_z_keyexpr_t _keyexpr;
uint16_t _vlan; // vlan tag (pcp + dei + id), big endian
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/transport/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "zenoh-pico/link/manager.h"
#include "zenoh-pico/transport/transport.h"

int8_t _z_new_transport(_z_transport_t *zt, _z_id_t *bs, char *locator, z_whatami_t mode);
int8_t _z_new_transport(_z_transport_t *zt, _z_id_t *bs, _z_string_t *locator, z_whatami_t mode);
void _z_free_transport(_z_transport_t **zt);

#endif /* INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H */
Loading

0 comments on commit fd99822

Please sign in to comment.