diff --git a/examples/z_get_shm.c b/examples/z_get_shm.c index 48ee8587b..c0846f6a4 100644 --- a/examples/z_get_shm.c +++ b/examples/z_get_shm.c @@ -67,7 +67,7 @@ int main(int argc, char** argv) { // Allocate SHM Buffer z_buf_layout_alloc_result_t alloc; z_shm_provider_alloc(&alloc, z_loan(provider), value_len, alignment); - if (!z_check(alloc.buf)) { + if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { printf("Unexpected failure during SHM buffer allocation..."); return -1; } diff --git a/examples/z_ping_shm.c b/examples/z_ping_shm.c index 085a617b0..5457def13 100644 --- a/examples/z_ping_shm.c +++ b/examples/z_ping_shm.c @@ -77,7 +77,7 @@ int main(int argc, char** argv) { // Allocate SHM Buffer z_buf_layout_alloc_result_t alloc; z_shm_provider_alloc(&alloc, z_loan(provider), args.size, alignment); - if (!z_check(alloc.buf)) { + if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { printf("Unexpected failure during SHM buffer allocation..."); return -1; } diff --git a/examples/z_pub_shm.c b/examples/z_pub_shm.c index 461816aca..cd259cceb 100644 --- a/examples/z_pub_shm.c +++ b/examples/z_pub_shm.c @@ -22,11 +22,22 @@ #define DEFAULT_VALUE "Pub from C!" struct args_t { - char* keyexpr; // -k - char* value; // -v + char* keyexpr; // -k + char* value; // -v + bool add_matching_listener; // --add-matching-listener }; struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); +#ifdef UNSTABLE +void matching_status_handler(const zc_matching_status_t* matching_status, void* arg) { + if (matching_status->matching) { + printf("Subscriber matched\n"); + } else { + printf("No Subscribers matched\n"); + } +} +#endif + int main(int argc, char** argv) { z_owned_config_t config; struct args_t args = parse_args(argc, argv, &config); @@ -48,7 +59,7 @@ int main(int argc, char** argv) { } #ifdef UNSTABLE zc_owned_matching_listener_t listener; - if (add_matching_listener) { + if (args.add_matching_listener) { zc_owned_closure_matching_status_t callback; z_closure(&callback, matching_status_handler, NULL, NULL); zc_publisher_matching_listener_declare(&listener, z_loan(pub), z_move(callback)); @@ -76,7 +87,7 @@ int main(int argc, char** argv) { z_buf_layout_alloc_result_t alloc; z_shm_provider_alloc_gc_defrag_blocking(&alloc, z_loan(provider), buf_ok_size, alignment); - if (z_check(alloc.buf)) { + if (alloc.status == ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { { uint8_t* buf = z_shm_mut_data_mut(z_loan_mut(alloc.buf)); sprintf((char*)buf, "[%4d] %s", idx, args.value); @@ -97,7 +108,7 @@ int main(int argc, char** argv) { } #ifdef UNSTABLE - if (add_matching_listener) { + if (args.add_matching_listener) { zc_publisher_matching_listener_undeclare(z_move(listener)); } #endif @@ -138,8 +149,13 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) { if (!value) { value = DEFAULT_VALUE; } + const char* arg = parse_opt(argc, argv, "add-matching-listener", false); + bool add_matching_listener = false; + if (arg) { + add_matching_listener = true; + } parse_zenoh_common_args(argc, argv, config); - const char* arg = check_unknown_opts(argc, argv); + arg = check_unknown_opts(argc, argv); if (arg) { printf("Unknown option %s\n", arg); exit(-1); @@ -151,5 +167,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) { exit(-1); } free(pos_args); - return (struct args_t){.keyexpr = (char*)keyexpr, .value = (char*)value}; + return (struct args_t){ + .keyexpr = (char*)keyexpr, .value = (char*)value, .add_matching_listener = add_matching_listener}; } diff --git a/examples/z_pub_shm_thr.c b/examples/z_pub_shm_thr.c index cc6d413a8..d91a18e46 100644 --- a/examples/z_pub_shm_thr.c +++ b/examples/z_pub_shm_thr.c @@ -66,7 +66,7 @@ int main(int argc, char **argv) { printf("Allocating single SHM buffer\n"); z_buf_layout_alloc_result_t alloc; z_shm_provider_alloc(&alloc, z_loan(provider), len, alignment); - if (!z_check(alloc.buf)) { + if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { printf("Unexpected failure during SHM buffer allocation...\n"); return -1; } diff --git a/examples/z_queryable_shm.c b/examples/z_queryable_shm.c index e59eca281..1e4fab53f 100644 --- a/examples/z_queryable_shm.c +++ b/examples/z_queryable_shm.c @@ -51,7 +51,7 @@ void query_handler(const z_loaned_query_t *query, void *context) { z_alloc_alignment_t alignment = {0}; z_buf_layout_alloc_result_t alloc; z_shm_provider_alloc_gc_defrag_blocking(&alloc, provider, value_len, alignment); - if (z_check(alloc.buf)) { + if (alloc.status == ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { { uint8_t *buf = z_shm_mut_data_mut(z_loan_mut(alloc.buf)); memcpy(buf, value, value_len); diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 2af99424f..6aa2620b8 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -201,6 +201,50 @@ typedef enum z_whatami_t { Z_WHATAMI_PEER = 2, Z_WHATAMI_CLIENT = 4, } z_whatami_t; +/** + * Status of SHM buffer allocation operation + */ +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) +typedef enum zc_buf_alloc_status_t { +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) + /** + * Allocation ok + */ + ZC_BUF_ALLOC_STATUS_OK = 0, +#endif +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) + /** + * Allocation error + */ + ZC_BUF_ALLOC_STATUS_ALLOC_ERROR = 1, +#endif +} zc_buf_alloc_status_t; +#endif +/** + * Status of SHM buffer layouting + allocation operation + */ +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) +typedef enum zc_buf_layout_alloc_status_t { +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) + /** + * Allocation ok + */ + ZC_BUF_LAYOUT_ALLOC_STATUS_OK = 0, +#endif +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) + /** + * Allocation error + */ + ZC_BUF_LAYOUT_ALLOC_STATUS_ALLOC_ERROR = 1, +#endif +#if (defined(SHARED_MEMORY) && defined(UNSTABLE)) + /** + * Layouting error + */ + ZC_BUF_LAYOUT_ALLOC_STATUS_LAYOUT_ERROR = 2, +#endif +} zc_buf_layout_alloc_status_t; +#endif /** * The locality of samples to be received by subscribers or targeted by publishers. */ @@ -268,8 +312,12 @@ typedef enum zc_reply_keyexpr_t { ZC_REPLY_KEYEXPR_MATCHING_QUERY = 1, } zc_reply_keyexpr_t; #endif +/** + * A result of SHM buffer allocation operation + */ #if (defined(SHARED_MEMORY) && defined(UNSTABLE)) typedef struct z_buf_alloc_result_t { + enum zc_buf_alloc_status_t status; z_owned_shm_mut_t buf; enum z_alloc_error_t error; } z_buf_alloc_result_t; @@ -897,10 +945,13 @@ typedef struct zc_shm_client_callbacks_t { bool (*attach_fn)(struct z_shm_segment_t *out_segment, z_segment_id_t segment_id, void *context); } zc_shm_client_callbacks_t; #endif +/** + * A result of SHM buffer layouting + allocation operation + */ #if (defined(SHARED_MEMORY) && defined(UNSTABLE)) typedef struct z_buf_layout_alloc_result_t { + enum zc_buf_layout_alloc_status_t status; z_owned_shm_mut_t buf; - bool error_is_alloc; enum z_alloc_error_t alloc_error; enum z_layout_error_t layout_error; } z_buf_layout_alloc_result_t; diff --git a/splitguide.yaml b/splitguide.yaml index c8cbae9f8..22bd854e5 100644 --- a/splitguide.yaml +++ b/splitguide.yaml @@ -86,6 +86,7 @@ zenoh_opaque.h: - z_owned_alloc_layout_t!#shared-memory#unstable - z_loaned_alloc_layout_t!#shared-memory#unstable - z_buf_alloc_result_t!#shared-memory#unstable + - zc_buf_alloc_status_t!#shared-memory#unstable - z_alloc_alignment_t!#shared-memory#unstable - z_chunk_descriptor_t!#shared-memory#unstable - z_allocated_chunk_t!#shared-memory#unstable @@ -93,6 +94,7 @@ zenoh_opaque.h: - z_shm_segment_t!#shared-memory#unstable - zc_shm_client_callbacks_t!#shared-memory#unstable - z_buf_layout_alloc_result_t!#shared-memory#unstable + - zc_buf_layout_alloc_status_t!#shared-memory#unstable - zc_shm_provider_backend_callbacks_t!#shared-memory#unstable - z_layout_error_t!#shared-memory#unstable - z_alloc_error_t!#shared-memory#unstable diff --git a/src/shm/provider/types.rs b/src/shm/provider/types.rs index ae0b63c4f..a9c6cb9a5 100644 --- a/src/shm/provider/types.rs +++ b/src/shm/provider/types.rs @@ -249,8 +249,20 @@ pub extern "C" fn z_chunk_alloc_result_drop(this_: &mut z_moved_chunk_alloc_resu let _ = this_.take_rust_type(); } +/// Status of SHM buffer allocation operation +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum zc_buf_alloc_status_t { + /// Allocation ok + OK = 0, + /// Allocation error + ALLOC_ERROR = 1, +} + +/// A result of SHM buffer allocation operation #[repr(C)] pub struct z_buf_alloc_result_t { + status: zc_buf_alloc_status_t, buf: z_owned_shm_mut_t, error: z_alloc_error_t, } @@ -262,6 +274,7 @@ impl From for z_buf_alloc_result_t { Ok(val) => { buf.as_rust_type_mut_uninit().write(Some(val)); Self { + status: zc_buf_alloc_status_t::OK, // SAFETY: this is safe because buf is gravestone-initialized above buf: unsafe { buf.assume_init() }, error: z_alloc_error_t::OTHER, @@ -270,6 +283,7 @@ impl From for z_buf_alloc_result_t { Err(error) => { z_internal_shm_mut_null(&mut buf); Self { + status: zc_buf_alloc_status_t::ALLOC_ERROR, // SAFETY: this is safe because buf is gravestone-initialized above buf: unsafe { buf.assume_init() }, error: error.into(), @@ -279,10 +293,23 @@ impl From for z_buf_alloc_result_t { } } +/// Status of SHM buffer layouting + allocation operation +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum zc_buf_layout_alloc_status_t { + /// Allocation ok + OK = 0, + /// Allocation error + ALLOC_ERROR = 1, + /// Layouting error + LAYOUT_ERROR = 2, +} + +/// A result of SHM buffer layouting + allocation operation #[repr(C)] pub struct z_buf_layout_alloc_result_t { + status: zc_buf_layout_alloc_status_t, buf: z_owned_shm_mut_t, - error_is_alloc: bool, alloc_error: z_alloc_error_t, layout_error: z_layout_error_t, } @@ -294,9 +321,9 @@ impl From for z_buf_layout_alloc_result_t { Ok(val) => { buf.as_rust_type_mut_uninit().write(Some(val)); Self { - // SAFETY: this is safe because buf is gravestone-initialized above + status: zc_buf_layout_alloc_status_t::OK, + // SAFETY: this is safe because buf is initialized above buf: unsafe { buf.assume_init() }, - error_is_alloc: false, alloc_error: z_alloc_error_t::OTHER, layout_error: z_layout_error_t::PROVIDER_INCOMPATIBLE_LAYOUT, } @@ -306,18 +333,18 @@ impl From for z_buf_layout_alloc_result_t { match error { zenoh::shm::ZLayoutAllocError::Alloc(alloc) => { Self { + status: zc_buf_layout_alloc_status_t::ALLOC_ERROR, // SAFETY: this is safe because buf is gravestone-initialized above buf: unsafe { buf.assume_init() }, - error_is_alloc: true, alloc_error: alloc.into(), layout_error: z_layout_error_t::PROVIDER_INCOMPATIBLE_LAYOUT, } } zenoh::shm::ZLayoutAllocError::Layout(layout) => { Self { + status: zc_buf_layout_alloc_status_t::LAYOUT_ERROR, // SAFETY: this is safe because buf is gravestone-initialized above buf: unsafe { buf.assume_init() }, - error_is_alloc: false, alloc_error: z_alloc_error_t::OTHER, layout_error: layout.into(), } diff --git a/tests/z_api_null_drop_test.c b/tests/z_api_null_drop_test.c index 97cbe23eb..805f37f6b 100644 --- a/tests/z_api_null_drop_test.c +++ b/tests/z_api_null_drop_test.c @@ -54,7 +54,9 @@ int main(void) { TEST(closure_query) TEST(closure_reply) TEST(closure_hello) +#ifdef UNSTABLE TEST(closure_zid) +#endif TEST(string) TEST(string_array) TEST(sample) diff --git a/tests/z_api_shm_test.c b/tests/z_api_shm_test.c index 4f3267df9..d11b63eac 100644 --- a/tests/z_api_shm_test.c +++ b/tests/z_api_shm_test.c @@ -33,16 +33,16 @@ return -300; \ } -#define ASSERT_CHECK(var) \ - if (!z_check(var)) { \ - assert(false); \ - return -100; \ +#define ASSERT_CHECK(var) \ + if (!z_internal_check(var)) { \ + assert(false); \ + return -100; \ } -#define ASSERT_CHECK_ERR(var) \ - if (z_check(var)) { \ - assert(false); \ - return -200; \ +#define ASSERT_CHECK_ERR(var) \ + if (z_internal_check(var)) { \ + assert(false); \ + return -200; \ } int test_shm_buffer(z_moved_shm_mut_t* mbuf) { @@ -96,7 +96,8 @@ int test_layouted_allocation(const z_loaned_alloc_layout_t* alloc_layout) { z_alloc_error_t shm_error; z_alloc_layout_alloc_gc(&alloc, alloc_layout); - if (z_check(alloc.buf)) { + if (alloc.status == ZC_BUF_ALLOC_STATUS_OK) { + ASSERT_CHECK(alloc.buf); ASSERT_OK(test_shm_buffer(z_move(alloc.buf))); ASSERT_CHECK_ERR(alloc.buf); return Z_OK; @@ -111,7 +112,8 @@ int test_allocation(const z_loaned_shm_provider_t* provider, size_t size, z_allo z_alloc_error_t shm_error; z_shm_provider_alloc_gc(&alloc, provider, size, alignment); - if (z_check(alloc.buf)) { + if (alloc.status == ZC_BUF_LAYOUT_ALLOC_STATUS_OK) { + ASSERT_CHECK(alloc.buf); ASSERT_OK(test_shm_buffer(z_move(alloc.buf))); ASSERT_CHECK_ERR(alloc.buf); return Z_OK; @@ -244,7 +246,7 @@ void layout_for_fn(struct z_owned_memory_layout_t* layout, void* context) { assert(context); assert(layout); - assert(z_check(*layout)); + assert(z_internal_check(*layout)); // check size and alignment size_t size = 0;