Skip to content

Commit

Permalink
Move protection (#575)
Browse files Browse the repository at this point in the history
* owned type added, test compile fail fix

* owned types added, compilation passed

* arduino example fix

* test build fix

* test build fixes

* test build fix

* examples buld fix

* examples build fix

* examble build fix

* example build fix

* test build fix

* moved struct in dummy macro

* take implemented, test for take added

* build errors fixed, take macro added

* compilation fix

* test fail fix

* z_view_xxx_check/null replaced by is_empty/empty to separate them from _check/_null for owned structs

* no z_check for view anymore

* compile fix

* query under feature
  • Loading branch information
milyin authored Aug 12, 2024
1 parent 48887a9 commit 939c469
Show file tree
Hide file tree
Showing 33 changed files with 472 additions and 495 deletions.
1 change: 0 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ Macros
Primitives
~~~~~~~~~~

.. autocfunction:: primitives.h::z_view_string_empty
.. autocfunction:: primitives.h::z_view_string_from_str
.. autocfunction:: primitives.h::z_view_keyexpr_from_str
.. autocfunction:: primitives.h::z_view_keyexpr_from_str_unchecked
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void loop() {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void app_main() {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
4 changes: 2 additions & 2 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void app_main(void) {
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
return;
}

while (1) {
Expand All @@ -89,7 +89,7 @@ void app_main(void) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void app_main(void) {
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
return;
}

printf("Creating Queryable on '%s'...\n", KEYEXPR);
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}

z_owned_closure_reply_t callback;
Expand Down
12 changes: 6 additions & 6 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -85,8 +85,8 @@ void print_attachment(kv_pairs_rx_t *kvp) {

void drop_attachment(kv_pairs_rx_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}

// Add attachment value
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t closure;
z_owned_ring_handler_reply_t handler;
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c11/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_drop(pub);
z_drop(z_move(*pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure(&respond, callback, drop, (void*)z_move(pub));
z_closure(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_queryable_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -97,8 +97,8 @@ void print_attachment(kv_pairs_rx_t *kvp) {

void drop_attachment(kv_pairs_rx_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -63,8 +63,8 @@ void print_attachment(kv_pairs_t *kvp) {

void drop_attachment(kv_pairs_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c99/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {

void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_undeclare_publisher(pub);
z_undeclare_publisher(z_publisher_move(pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -63,7 +63,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure_sample(&respond, callback, drop, (void*)z_publisher_move(&pub));
z_closure_sample(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&session), z_view_keyexpr_loan(&ping),
z_closure_sample_move(&respond), NULL) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
4 changes: 2 additions & 2 deletions examples/windows/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_drop(pub);
z_drop(z_move(*pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure(&respond, callback, drop, (void*)z_move(pub));
z_closure(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
32 changes: 18 additions & 14 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ extern "C" {
#define _Z_CHANNEL_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \
collection_type, collection_new_f, collection_free_f, collection_push_f, \
collection_pull_f, collection_try_pull_f, collection_close_f, elem_owned_type, \
elem_loaned_type, elem_clone_f, elem_drop_f, elem_null_f) \
elem_loaned_type, elem_clone_f, elem_move_f, elem_drop_f, elem_null_f) \
typedef struct { \
collection_type *collection; \
} handler_type; \
\
_Z_OWNED_TYPE_VALUE(handler_type, handler_name) \
_Z_LOANED_TYPE(handler_type, handler_name) \
\
static inline void _z_##handler_name##_elem_free(void **elem) { \
elem_drop_f((elem_owned_type *)*elem); \
elem_drop_f(elem_move_f((elem_owned_type *)*elem)); \
z_free(*elem); \
*elem = NULL; \
} \
Expand Down Expand Up @@ -140,20 +139,25 @@ extern "C" {
/* elem_owned_type */ z_owned_##item_name##_t, \
/* elem_loaned_type */ z_loaned_##item_name##_t, \
/* elem_clone_f */ z_##item_name##_clone, \
/* elem_move_f */ z_##item_name##_move, \
/* elem_drop_f */ z_##item_name##_drop, \
/* elem_null */ z_##item_name##_null)

#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \
typedef struct { \
uint8_t _foo; \
} z_owned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
uint8_t _foo; \
} z_loaned_##kind_name##_handler_##item_name##_t; \
void *z_##kind_name##_handler_##item_name##_loan(void); \
void *z_##kind_name##_handler_##item_name##_move(void); \
void *z_##kind_name##_handler_##item_name##_drop(void); \
void *z_##kind_name##_handler_##item_name##_recv(void); \
#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \
typedef struct { \
uint8_t _foo; \
} z_owned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
uint8_t _foo; \
} z_loaned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
z_owned_##kind_name##_handler_##item_name##_t *_ptr; \
} z_moved_##kind_name##_handler_##item_name##_t; \
void *z_##kind_name##_handler_##item_name##_loan(void); \
void *z_##kind_name##_handler_##item_name##_move(void); \
void *z_##kind_name##_handler_##item_name##_drop(void); \
void *z_##kind_name##_handler_##item_name##_recv(void); \
void *z_##kind_name##_handler_##item_name##_take(void); \
void *z_##kind_name##_handler_##item_name##_try_recv(void);

// This macro defines:
Expand Down
Loading

0 comments on commit 939c469

Please sign in to comment.