diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 41129a653..4326d856f 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -203,22 +203,6 @@ typedef struct z_attachment_t { const void *data; z_attachment_iter_driver_t iteration_driver; } z_attachment_t; -/** - * A split buffer that owns all of its data. - * - * To minimize copies and reallocations, Zenoh may provide you data in split buffers. - */ -typedef struct ALIGN(8) z_owned_buffer_t { - uint8_t _0[40]; -} z_owned_buffer_t; -/** - * A loan of a `z_owned_buffer_t`. - * - * As it is a split buffer, it may contain more than one slice. It's number of slices is returned by `z_buffer_slice_count`. - */ -typedef struct z_buffer_t { - struct z_owned_buffer_t *_inner; -} z_buffer_t; typedef struct z_owned_bytes_t { uint8_t *start; size_t len; @@ -644,6 +628,14 @@ typedef struct z_owned_encoding_t { typedef struct z_query_consolidation_t { enum z_consolidation_mode_t mode; } z_query_consolidation_t; +/** + * A split buffer that owns all of its data. + * + * To minimize copies and reallocations, Zenoh may provide you data in split buffers. + */ +typedef struct ALIGN(8) z_owned_buffer_t { + uint8_t _0[40]; +} z_owned_buffer_t; /** * An owned payload, backed by a reference counted owner. * @@ -788,6 +780,14 @@ typedef struct z_query_reply_options_t { struct z_encoding_t encoding; struct z_attachment_t attachment; } z_query_reply_options_t; +/** + * A loan of a `z_owned_buffer_t`. + * + * As it is a split buffer, it may contain more than one slice. It's number of slices is returned by `z_buffer_slice_count`. + */ +typedef struct z_buffer_t { + struct z_owned_buffer_t *_inner; +} z_buffer_t; typedef struct z_buffer_t zc_payload_t; /** * A zenoh value. @@ -1073,44 +1073,6 @@ ZENOHC_API size_t z_attachment_len(struct z_attachment_t this_); * Returns the gravestone value for `z_attachment_t`. */ ZENOHC_API struct z_attachment_t z_attachment_null(void); -/** - * Returns `true` if the buffer is in a valid state. - */ -ZENOHC_API bool z_buffer_check(const struct z_owned_buffer_t *buffer); -/** - * Increments the buffer's reference count, returning an owned version of the buffer. - */ -ZENOHC_API struct z_owned_buffer_t z_buffer_clone(struct z_buffer_t buffer); -/** - * Decrements the buffer's reference counter, destroying it if applicable. - * - * `buffer` will be reset to `z_buffer_null`, preventing UB on double-frees. - */ -ZENOHC_API void z_buffer_drop(struct z_owned_buffer_t *buffer); -/** - * Returns total number bytes in the buffer. - */ -ZENOHC_API size_t z_buffer_len(struct z_buffer_t buffer); -/** - * Loans the buffer, allowing you to call functions that only need a loan of it. - */ -ZENOHC_API struct z_buffer_t z_buffer_loan(const struct z_owned_buffer_t *buffer); -/** - * The gravestone value for `z_owned_buffer_t`. - */ -ZENOHC_API struct z_owned_buffer_t z_buffer_null(void); -/** - * Returns the `index`th slice of the buffer, aliasing it. - * - * Out of bounds accesses will return `z_bytes_empty`. - */ -ZENOHC_API struct z_bytes_t z_buffer_slice_at(struct z_buffer_t buffer, size_t index); -/** - * Returns the number of slices in the buffer. - * - * If the return value is 0 or 1, then the buffer's data is contiguous in memory. - */ -ZENOHC_API size_t z_buffer_slice_count(struct z_buffer_t buffer); /** * Returns ``true`` if `b` is initialized. */ diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index bb50476a5..815b2df85 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -14,7 +14,7 @@ z_owned_hello_t : z_hello_loan, \ z_owned_str_t : z_str_loan, \ z_owned_query_t : z_query_loan, \ - z_owned_buffer_t: z_buffer_loan, \ + zc_owned_payload_t : zc_payload_loan, \ ze_owned_querying_subscriber_t : ze_querying_subscriber_loan \ )(&x) @@ -43,7 +43,7 @@ z_owned_reply_channel_t * : z_reply_channel_drop, \ z_owned_query_channel_t * : z_query_channel_drop, \ z_owned_bytes_map_t * : z_bytes_map_drop, \ - z_owned_buffer_t * : z_buffer_drop, \ + zc_owned_payload_t * : zc_payload_drop, \ zc_owned_shmbuf_t * : zc_shmbuf_drop, \ zc_owned_shm_manager_t * : zc_shm_manager_drop, \ zc_owned_liveliness_token_t * : zc_liveliness_undeclare_token, \ @@ -74,7 +74,7 @@ z_owned_reply_channel_closure_t * : z_reply_channel_closure_null, \ z_owned_reply_channel_t * : z_reply_channel_null, \ z_owned_bytes_map_t * : z_bytes_map_null, \ - z_owned_buffer_t * : z_buffer_null, \ + zc_owned_payload_t * : zc_payload_null, \ z_attachment_t * : z_attachment_null, \ zc_owned_shmbuf_t * : zc_shmbuf_null, \ zc_owned_shm_manager_t * : zc_shm_manager_null, \ @@ -89,7 +89,7 @@ z_keyexpr_t : z_keyexpr_is_initialized, \ z_owned_config_t : z_config_check, \ z_owned_scouting_config_t : z_scouting_config_check, \ - z_owned_bytes_t : z_bytes_check, \ + z_owned_bytes_t : z_bytes_check, \ z_owned_subscriber_t : z_subscriber_check, \ z_owned_pull_subscriber_t : z_pull_subscriber_check, \ z_owned_queryable_t : z_queryable_check, \ @@ -99,7 +99,7 @@ z_owned_query_t : z_query_check, \ z_owned_str_t : z_str_check, \ z_owned_bytes_map_t : z_bytes_map_check, \ - z_owned_buffer_t : z_buffer_check, \ + zc_owned_payload_t: zc_payload_check, \ z_attachment_t : z_attachment_check, \ zc_owned_shmbuf_t : zc_shmbuf_check, \ zc_owned_shm_manager_t : zc_shm_manager_check, \ @@ -142,7 +142,7 @@ template<> struct zenoh_loan_type{ typedef z_pull_sub template<> struct zenoh_loan_type{ typedef z_encoding_t type; }; template<> struct zenoh_loan_type{ typedef z_hello_t type; }; template<> struct zenoh_loan_type{ typedef const char* type; }; -template<> struct zenoh_loan_type{ typedef z_buffer_t type; }; +template<> struct zenoh_loan_type{ typedef zc_payload_t type; }; template<> struct zenoh_loan_type{ typedef ze_querying_subscriber_t type; }; template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } @@ -155,7 +155,7 @@ template<> inline z_encoding_t z_loan(const z_owned_encoding_t& x) { return z_en template<> inline z_hello_t z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } template<> inline z_query_t z_loan(const z_owned_query_t& x) { return z_query_loan(&x); } template<> inline const char* z_loan(const z_owned_str_t& x) { return z_str_loan(&x); } -template<> inline z_buffer_t z_loan(const z_owned_buffer_t& x) { return z_buffer_loan(&x); } +template<> inline zc_payload_t z_loan(const zc_owned_payload& x) { return zc_payload_loan(&x); } template<> inline ze_querying_subscriber_t z_loan(const ze_owned_querying_subscriber_t& x) { return ze_querying_subscriber_loan(&x); } template struct zenoh_drop_type { typedef T type; }; @@ -174,7 +174,7 @@ template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; +template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; @@ -204,7 +204,7 @@ template<> inline void z_drop(z_owned_reply_t* v) { z_reply_drop(v); } template<> inline void z_drop(z_owned_hello_t* v) { z_hello_drop(v); } template<> inline void z_drop(z_owned_query_t* v) { z_query_drop(v); } template<> inline void z_drop(z_owned_str_t* v) { z_str_drop(v); } -template<> inline void z_drop(z_owned_buffer_t* v) { z_buffer_drop(v); } +template<> inline void z_drop(zc_owned_payload* v) { zc_payload_drop(v); } template<> inline void z_drop(zc_owned_payload_t* v) { zc_payload_drop(v); } template<> inline void z_drop(zc_owned_shmbuf_t* v) { zc_shmbuf_drop(v); } template<> inline void z_drop(zc_owned_shm_manager_t* v) { zc_shm_manager_drop(v); } @@ -234,7 +234,7 @@ inline void z_null(z_owned_reply_t& v) { v = z_reply_null(); } inline void z_null(z_owned_hello_t& v) { v = z_hello_null(); } inline void z_null(z_owned_query_t& v) { v = z_query_null(); } inline void z_null(z_owned_str_t& v) { v = z_str_null(); } -inline void z_null(z_owned_buffer_t& v) { v = z_buffer_null(); } +inline void z_null(zc_owned_payload& v) { v = zc_payload_null(); } inline void z_null(zc_owned_payload_t& v) { v = zc_payload_null(); } inline void z_null(zc_owned_shmbuf_t& v) { v = zc_shmbuf_null(); } inline void z_null(zc_owned_shm_manager_t& v) { v = zc_shm_manager_null(); } @@ -269,7 +269,7 @@ inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); } inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); } inline bool z_check(const z_owned_query_t& v) { return z_query_check(&v); } inline bool z_check(const z_owned_str_t& v) { return z_str_check(&v); } -inline bool z_check(const z_owned_buffer_t& v) { return z_buffer_check(&v); } +inline bool z_check(const zc_owned_payload& v) { return zc_payload_check(&v); } inline bool z_check(const z_owned_bytes_map_t& v) { return z_bytes_map_check(&v); } inline bool z_check(const z_attachment_t& v) { return z_attachment_check(&v); } inline bool z_check(const zc_owned_liveliness_token_t& v) { return zc_liveliness_token_check(&v); } diff --git a/src/payload.rs b/src/payload.rs index f7a737ef5..a1745ba36 100644 --- a/src/payload.rs +++ b/src/payload.rs @@ -28,7 +28,7 @@ impl From for z_owned_buffer_t { /// The gravestone value for `z_owned_buffer_t`. #[no_mangle] -pub extern "C" fn z_buffer_null() -> z_owned_buffer_t { +extern "C" fn z_buffer_null() -> z_owned_buffer_t { None::.transmute() } @@ -36,19 +36,19 @@ pub extern "C" fn z_buffer_null() -> z_owned_buffer_t { /// /// `buffer` will be reset to `z_buffer_null`, preventing UB on double-frees. #[no_mangle] -pub extern "C" fn z_buffer_drop(buffer: &mut z_owned_buffer_t) { +extern "C" fn z_buffer_drop(buffer: &mut z_owned_buffer_t) { core::mem::drop(buffer.take()) } /// Returns `true` if the buffer is in a valid state. #[no_mangle] -pub extern "C" fn z_buffer_check(buffer: &z_owned_buffer_t) -> bool { +extern "C" fn z_buffer_check(buffer: &z_owned_buffer_t) -> bool { buffer.is_some() } /// Loans the buffer, allowing you to call functions that only need a loan of it. #[no_mangle] -pub extern "C" fn z_buffer_loan(buffer: &z_owned_buffer_t) -> z_buffer_t { +extern "C" fn z_buffer_loan(buffer: &z_owned_buffer_t) -> z_buffer_t { buffer.as_ref().into() } @@ -78,7 +78,7 @@ impl From for Option<&'static ZBuf> { /// Increments the buffer's reference count, returning an owned version of the buffer. #[no_mangle] -pub extern "C" fn z_buffer_clone(buffer: z_buffer_t) -> z_owned_buffer_t { +extern "C" fn z_buffer_clone(buffer: z_buffer_t) -> z_owned_buffer_t { match buffer._inner { Some(b) => unsafe { b.as_ref().deref().clone().transmute() }, None => ZBuf::empty().into(), @@ -89,7 +89,7 @@ pub extern "C" fn z_buffer_clone(buffer: z_buffer_t) -> z_owned_buffer_t { /// /// If the return value is 0 or 1, then the buffer's data is contiguous in memory. #[no_mangle] -pub extern "C" fn z_buffer_slice_count(buffer: z_buffer_t) -> usize { +extern "C" fn z_buffer_slice_count(buffer: z_buffer_t) -> usize { match buffer.into() { None => 0, Some(buf) => ZBuf::slices(buf).len(), @@ -98,7 +98,7 @@ pub extern "C" fn z_buffer_slice_count(buffer: z_buffer_t) -> usize { /// Returns total number bytes in the buffer. #[no_mangle] -pub extern "C" fn z_buffer_len(buffer: z_buffer_t) -> usize { +extern "C" fn z_buffer_len(buffer: z_buffer_t) -> usize { match buffer.into() { None => 0, Some(buf) => ZBuf::slices(buf).fold(0, |acc, s| acc + s.len()), @@ -109,7 +109,7 @@ pub extern "C" fn z_buffer_len(buffer: z_buffer_t) -> usize { /// /// Out of bounds accesses will return `z_bytes_empty`. #[no_mangle] -pub extern "C" fn z_buffer_slice_at(buffer: z_buffer_t, index: usize) -> z_bytes_t { +extern "C" fn z_buffer_slice_at(buffer: z_buffer_t, index: usize) -> z_bytes_t { match buffer.into() { None => z_bytes_empty(), Some(buf) => ZBuf::slices(buf)