diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index 2d168225c..bd8f15836 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -79,8 +79,8 @@ void fprinthello(const z_loaned_hello_t *hello) { Serial.println(" }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(z_hello_loan(hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(hello); Serial.println(""); (*(int *)context)++; } diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index e569d81ea..e1ce7b9c7 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -133,8 +133,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_hello_loan(hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; } diff --git a/examples/freertos_plus_tcp/z_scout.c b/examples/freertos_plus_tcp/z_scout.c index 4b62c1301..1d7ba13a3 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -67,8 +67,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_loan(*hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; } diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 9f78c6be4..a8d305a22 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -75,8 +75,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_hello_loan(hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; } diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index a39bd2226..be0be84fa 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -65,11 +65,10 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_loan(*hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; - z_drop(hello); } void drop(void *context) { diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index d94c278f4..d383bc335 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -64,11 +64,10 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_hello_loan(hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; - z_hello_drop(hello); } void drop(void *context) { diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index dfd993637..efffdc993 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -64,8 +64,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_loan(*hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; } diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index 06c8277b5..32c9a5660 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -61,8 +61,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, " }"); } -void callback(z_owned_hello_t *hello, void *context) { - fprinthello(stdout, z_hello_loan(hello)); +void callback(const z_loaned_hello_t *hello, void *context) { + fprinthello(stdout, hello); fprintf(stdout, "\n"); (*(int *)context)++; } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 97b1debf4..bae7ab894 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1011,7 +1011,7 @@ int8_t z_closure_owned_reply(z_owned_closure_owned_reply_t *closure, z_owned_rep * Return: * The hello closure. */ -int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, z_dropper_handler_t drop, +int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_loaned_hello_handler_t call, z_dropper_handler_t drop, void *context); /** diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index b5a0b617e..ac4797f1f 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -588,7 +588,7 @@ typedef struct { void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_owned_reply_t *reply); -typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); +typedef void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *arg); /** * Represents the Zenoh ID callback closure. @@ -597,18 +597,18 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * * Members: * void *context: a pointer to an arbitrary state. - * z_owned_hello_handler_t call: `void (*z_owned_hello_handler_t)(const z_owned_hello_t *hello, void *arg)` is the + * z_loaned_hello_handler_t call: `void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *arg)` is the * callback function. * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ typedef struct { void *context; - z_owned_hello_handler_t call; + z_loaned_hello_handler_t call; z_dropper_handler_t drop; } z_owned_closure_hello_t; -void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello); +void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello); typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); diff --git a/src/api/api.c b/src/api/api.c index 1072c95b9..c439ed142 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -657,7 +657,7 @@ void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_ } } -void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello) { +void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello) { if (closure->call != NULL) { (closure->call)(hello, closure->context); } @@ -781,18 +781,17 @@ OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_query, z_ow OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply, _z_reply_handler_t, z_dropper_handler_t) OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_reply_t, closure_owned_reply, z_owned_reply_handler_t, z_dropper_handler_t) -OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello, z_owned_hello_handler_t, z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello, z_loaned_hello_handler_t, z_dropper_handler_t) OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid, z_id_handler_t, z_dropper_handler_t) /************* Primitives **************/ typedef struct __z_hello_handler_wrapper_t { - z_owned_hello_handler_t user_call; + z_loaned_hello_handler_t user_call; void *ctx; } __z_hello_handler_wrapper_t; void __z_hello_handler(_z_hello_t *hello, __z_hello_handler_wrapper_t *wrapped_ctx) { - z_owned_hello_t ohello = {._val = hello}; - wrapped_ctx->user_call(&ohello, wrapped_ctx->ctx); + wrapped_ctx->user_call(hello, wrapped_ctx->ctx); } int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callback) { diff --git a/src/net/primitives.c b/src/net/primitives.c index c0392e806..35476ccf0 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -44,7 +44,8 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const while (hellos != NULL) { _z_hello_t *hello = NULL; hellos = _z_hello_list_pop(hellos, &hello); - (*callback)(hello, arg_call); // callback takes ownership of hello + (*callback)(hello, arg_call); + _z_hello_free(&hello); } if (dropper != NULL) { diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index c63dce561..8ed72b264 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -53,11 +53,11 @@ void zid_handler(const z_id_t *id, void *arg) { } volatile unsigned int hellos = 0; -void hello_handler(z_owned_hello_t *hello, void *arg) { +void hello_handler(const z_loaned_hello_t *hello, void *arg) { + (void)hello; (void)(arg); printf("%s\n", __func__); hellos++; - z_drop(hello); } volatile unsigned int queries = 0;