Skip to content

Commit

Permalink
Adopt SHM for recent API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowhatter committed Aug 22, 2024
1 parent 6e67b83 commit 31d5207
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/z_get_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_ping_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 24 additions & 7 deletions examples/z_pub_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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};
}
2 changes: 1 addition & 1 deletion examples/z_pub_shm_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
53 changes: 52 additions & 1 deletion include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions splitguide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ 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
- zc_shm_segment_callbacks_t!#shared-memory#unstable
- 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
Expand Down
37 changes: 32 additions & 5 deletions src/shm/provider/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -262,6 +274,7 @@ impl From<BufAllocResult> 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,
Expand All @@ -270,6 +283,7 @@ impl From<BufAllocResult> 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(),
Expand All @@ -279,10 +293,23 @@ impl From<BufAllocResult> 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,
}
Expand All @@ -294,9 +321,9 @@ impl From<BufLayoutAllocResult> 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,
}
Expand All @@ -306,18 +333,18 @@ impl From<BufLayoutAllocResult> 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(),
}
Expand Down
2 changes: 2 additions & 0 deletions tests/z_api_null_drop_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions tests/z_api_shm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 31d5207

Please sign in to comment.