Skip to content

Commit

Permalink
make all references to z_buffer_t private
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Apr 8, 2024
1 parent f1cdeda commit e2df814
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 73 deletions.
70 changes: 16 additions & 54 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down
22 changes: 11 additions & 11 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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, \
Expand Down Expand Up @@ -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, \
Expand All @@ -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, \
Expand All @@ -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, \
Expand Down Expand Up @@ -142,7 +142,7 @@ template<> struct zenoh_loan_type<z_owned_pull_subscriber_t>{ typedef z_pull_sub
template<> struct zenoh_loan_type<z_owned_encoding_t>{ typedef z_encoding_t type; };
template<> struct zenoh_loan_type<z_owned_hello_t>{ typedef z_hello_t type; };
template<> struct zenoh_loan_type<z_owned_str_t>{ typedef const char* type; };
template<> struct zenoh_loan_type<z_owned_buffer_t>{ typedef z_buffer_t type; };
template<> struct zenoh_loan_type<zc_owned_payload>{ typedef zc_payload_t type; };
template<> struct zenoh_loan_type<ze_owned_querying_subscriber_t>{ typedef ze_querying_subscriber_t type; };

template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); }
Expand All @@ -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<class T> struct zenoh_drop_type { typedef T type; };
Expand All @@ -174,7 +174,7 @@ template<> struct zenoh_drop_type<z_owned_reply_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_hello_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_query_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_str_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_buffer_t> { typedef void type; };
template<> struct zenoh_drop_type<zc_owned_payload> { typedef void type; };
template<> struct zenoh_drop_type<zc_owned_payload_t> { typedef void type; };
template<> struct zenoh_drop_type<zc_owned_shmbuf_t> { typedef void type; };
template<> struct zenoh_drop_type<zc_owned_shm_manager_t> { typedef void type; };
Expand Down Expand Up @@ -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); }
Expand Down Expand Up @@ -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(); }
Expand Down Expand Up @@ -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); }
Expand Down
16 changes: 8 additions & 8 deletions src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ impl From<ZBuf> 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::<ZBuf>.transmute()
}

/// Decrements the buffer's reference counter, destroying it if applicable.
///
/// `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()
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl From<z_buffer_t> 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(),
Expand All @@ -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(),
Expand All @@ -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()),
Expand All @@ -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)
Expand Down

0 comments on commit e2df814

Please sign in to comment.