Skip to content

Commit

Permalink
shm provider update
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jun 27, 2024
1 parent d07a435 commit f519ad8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
11 changes: 5 additions & 6 deletions src/shm/protocol_implementations/posix/posix_shm_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use zenoh::shm::{
use crate::{
errors::{z_error_t, Z_EINVAL, Z_OK},
shm::provider::shm_provider::CSHMProvider,
transmute::{Inplace, TransmuteFromHandle, TransmuteUninitPtr},
transmute::TransmuteFromHandle,
transmute2::RustTypeRefUninit,
z_loaned_memory_layout_t, z_owned_shm_provider_t,
};

Expand All @@ -35,7 +36,7 @@ pub type PosixAllocLayout =
/// Creates a new threadsafe SHM Provider
#[no_mangle]
pub extern "C" fn z_posix_shm_provider_new(
this: *mut MaybeUninit<z_owned_shm_provider_t>,
this: &mut MaybeUninit<z_owned_shm_provider_t>,
layout: &z_loaned_memory_layout_t,
) -> z_error_t {
match PosixShmProviderBackend::builder()
Expand All @@ -47,10 +48,8 @@ pub extern "C" fn z_posix_shm_provider_new(
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
Inplace::init(
this.transmute_uninit_ptr(),
Some(CSHMProvider::Posix(provider)),
);
this.as_rust_type_mut_uninit()
.write(Some(CSHMProvider::Posix(provider)));
Z_OK
}
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/shm/provider/alloc_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ decl_c_type!(
#[no_mangle]
pub extern "C" fn z_alloc_layout_new(
this: &mut MaybeUninit<z_owned_alloc_layout_t>,
provider: &z_loaned_shm_provider_t,
provider: &'static z_loaned_shm_provider_t,
size: usize,
alignment: z_alloc_alignment_t,
) -> z_error_t {
Expand Down
6 changes: 3 additions & 3 deletions src/shm/provider/alloc_layout_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::transmute2::{RustTypeRef, RustTypeRefUninit};
use crate::{
context::{zc_threadsafe_context_t, DroppableContext, ThreadsafeContext},
errors::{z_error_t, Z_EINVAL, Z_OK},
transmute::{TransmuteCopy, TransmuteFromHandle},
transmute::TransmuteCopy,
z_owned_buf_alloc_result_t,
};
use crate::{z_loaned_alloc_layout_t, z_loaned_shm_provider_t, z_owned_alloc_layout_t};
Expand All @@ -37,11 +37,11 @@ use super::{

pub(crate) fn alloc_layout_new(
this: &mut MaybeUninit<z_owned_alloc_layout_t>,
provider: &z_loaned_shm_provider_t,
provider: &'static z_loaned_shm_provider_t,
size: usize,
alignment: z_alloc_alignment_t,
) -> z_error_t {
let layout = match provider.transmute_ref() {
let layout = match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
match provider
.alloc(size)
Expand Down
46 changes: 23 additions & 23 deletions src/shm/provider/shm_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use crate::{
common::types::z_protocol_id_t,
protocol_implementations::posix::posix_shm_provider::PosixShmProvider,
},
transmute::{
unwrap_ref_unchecked, Inplace, TransmuteIntoHandle, TransmuteRef, TransmuteUninitPtr,
},
transmute2::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit},
z_loaned_shm_provider_t, z_owned_buf_alloc_result_t, z_owned_shm_mut_t, z_owned_shm_provider_t,
};

Expand All @@ -51,13 +49,15 @@ pub enum CSHMProvider {
DynamicThreadsafe(DynamicShmProviderThreadsafe),
}

decl_transmute_owned!(Option<CSHMProvider>, z_owned_shm_provider_t);
decl_transmute_handle!(CSHMProvider, z_loaned_shm_provider_t);
decl_c_type!(
owned(z_owned_shm_provider_t, Option<CSHMProvider>),
loaned(z_loaned_shm_provider_t, CSHMProvider),
);

/// Creates a new SHM Provider
#[no_mangle]
pub extern "C" fn z_shm_provider_new(
this: *mut MaybeUninit<z_owned_shm_provider_t>,
this: &mut MaybeUninit<z_owned_shm_provider_t>,
id: z_protocol_id_t,
context: zc_context_t,
callbacks: zc_shm_provider_backend_callbacks_t,
Expand All @@ -68,16 +68,14 @@ pub extern "C" fn z_shm_provider_new(
.backend(backend)
.res();

Inplace::init(
this.transmute_uninit_ptr(),
Some(CSHMProvider::Dynamic(provider)),
);
this.as_rust_type_mut_uninit()
.write(Some(CSHMProvider::Dynamic(provider)));
}

/// Creates a new threadsafe SHM Provider
#[no_mangle]
pub extern "C" fn z_shm_provider_threadsafe_new(
this: *mut MaybeUninit<z_owned_shm_provider_t>,
this: &mut MaybeUninit<z_owned_shm_provider_t>,
id: z_protocol_id_t,
context: zc_threadsafe_context_t,
callbacks: zc_shm_provider_backend_callbacks_t,
Expand All @@ -88,36 +86,38 @@ pub extern "C" fn z_shm_provider_threadsafe_new(
.backend(backend)
.res();

Inplace::init(
this.transmute_uninit_ptr(),
Some(CSHMProvider::DynamicThreadsafe(provider)),
);
this.as_rust_type_mut_uninit()
.write(Some(CSHMProvider::DynamicThreadsafe(provider)));
}

/// Constructs SHM Provider in its gravestone value.
#[no_mangle]
pub extern "C" fn z_shm_provider_null(this: *mut MaybeUninit<z_owned_shm_provider_t>) {
Inplace::empty(this.transmute_uninit_ptr());
pub extern "C" fn z_shm_provider_null(this: &mut MaybeUninit<z_owned_shm_provider_t>) {
this.as_rust_type_mut_uninit().write(None);
}

/// Returns ``true`` if `this` is valid.
#[no_mangle]
pub extern "C" fn z_shm_provider_check(this: &z_owned_shm_provider_t) -> bool {
this.transmute_ref().is_some()
this.as_rust_type_ref().is_some()
}

/// Borrows SHM Provider
#[no_mangle]
pub extern "C" fn z_shm_provider_loan(this: &z_owned_shm_provider_t) -> &z_loaned_shm_provider_t {
let this = this.transmute_ref();
let this = unwrap_ref_unchecked(this);
this.transmute_handle()
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_shm_provider_loan(
this: &z_owned_shm_provider_t,
) -> &z_loaned_shm_provider_t {
this.as_rust_type_ref()
.as_ref()
.unwrap_unchecked()
.as_loaned_ctype_ref()
}

/// Deletes SHM Provider
#[no_mangle]
pub extern "C" fn z_shm_provider_drop(this: &mut z_owned_shm_provider_t) {
let _ = this.transmute_mut().take();
*this.as_rust_type_mut() = None;
}

#[no_mangle]
Expand Down
16 changes: 8 additions & 8 deletions src/shm/provider/shm_provider_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use zenoh::shm::{

use crate::context::{Context, DroppableContext, ThreadsafeContext};
use crate::errors::{z_error_t, Z_EINVAL, Z_OK};
use crate::transmute::{TransmuteCopy, TransmuteFromHandle};
use crate::transmute2::RustTypeRefUninit;
use crate::transmute::TransmuteCopy;
use crate::transmute2::{RustTypeRef, RustTypeRefUninit};
use crate::{z_loaned_shm_provider_t, z_owned_buf_alloc_result_t, z_owned_shm_mut_t};

use super::chunk::z_allocated_chunk_t;
Expand All @@ -37,7 +37,7 @@ pub(crate) fn alloc<Policy: AllocPolicy>(
size: usize,
alignment: z_alloc_alignment_t,
) -> z_error_t {
match provider.transmute_ref() {
match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
alloc_impl::<Policy, StaticProtocolID<POSIX_PROTOCOL_ID>, PosixShmProviderBackend>(
out_result, provider, size, alignment,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub(crate) fn alloc_async<Policy: AsyncAllocPolicy>(
*mut MaybeUninit<z_owned_buf_alloc_result_t>,
),
) -> z_error_t {
match provider.transmute_ref() {
match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
alloc_async_impl::<Policy, StaticProtocolID<POSIX_PROTOCOL_ID>, PosixShmProviderBackend>(
out_result,
Expand Down Expand Up @@ -100,7 +100,7 @@ pub(crate) fn alloc_async<Policy: AsyncAllocPolicy>(
}

pub(crate) fn defragment(provider: &z_loaned_shm_provider_t) {
match provider.transmute_ref() {
match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
provider.defragment();
}
Expand All @@ -114,7 +114,7 @@ pub(crate) fn defragment(provider: &z_loaned_shm_provider_t) {
}

pub(crate) fn garbage_collect(provider: &z_loaned_shm_provider_t) {
match provider.transmute_ref() {
match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
provider.garbage_collect();
}
Expand All @@ -128,7 +128,7 @@ pub(crate) fn garbage_collect(provider: &z_loaned_shm_provider_t) {
}

pub(crate) fn available(provider: &z_loaned_shm_provider_t) -> usize {
match provider.transmute_ref() {
match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => provider.available(),
super::shm_provider::CSHMProvider::Dynamic(provider) => provider.available(),
super::shm_provider::CSHMProvider::DynamicThreadsafe(provider) => provider.available(),
Expand All @@ -142,7 +142,7 @@ pub(crate) fn map(
allocated_chunk: z_allocated_chunk_t,
len: usize,
) {
let mapping = match provider.transmute_ref() {
let mapping = match provider.as_rust_type_ref() {
super::shm_provider::CSHMProvider::Posix(provider) => {
provider.map(allocated_chunk.into(), len)
}
Expand Down

0 comments on commit f519ad8

Please sign in to comment.