Skip to content

Commit

Permalink
- more additions to the API
Browse files Browse the repository at this point in the history
- C Provider and Client API tests
  • Loading branch information
yellowhatter committed May 25, 2024
1 parent 11a546e commit 0dece15
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/shm/provider/alloc_layout_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zenoh::prelude::*;
use zenoh::shm::AllocPolicy;

use crate::{
errors::{z_error_t, Z_EINVAL, Z_OK}, transmute::{Inplace, TransmuteFromHandle, TransmuteUninitPtr}, z_owned_buf_alloc_result_t
errors::{z_error_t, Z_EINVAL, Z_OK}, transmute::{Inplace, TransmuteCopy, TransmuteFromHandle, TransmuteUninitPtr}, z_owned_buf_alloc_result_t
};

use super::{alloc_layout::{z_loaned_alloc_layout_t, z_owned_alloc_layout_t, CSHMLayout}, shared_memory_provider::z_loaned_shared_memory_provider_t, types::z_alloc_alignment_t};
Expand All @@ -33,7 +33,7 @@ pub(crate) fn alloc_layout_new(
super::shared_memory_provider::CSHMProvider::Posix(provider) => {
match provider
.alloc(size)
.with_alignment(alignment.into())
.with_alignment(alignment.transmute_copy())
.into_layout()
{
Ok(layout) => CSHMLayout::Posix(layout),
Expand All @@ -46,7 +46,7 @@ pub(crate) fn alloc_layout_new(
super::shared_memory_provider::CSHMProvider::Dynamic(provider) => {
match provider
.alloc(size)
.with_alignment(alignment.into())
.with_alignment(alignment.transmute_copy())
.into_layout()
{
Ok(layout) => CSHMLayout::Dynamic(layout),
Expand All @@ -59,7 +59,7 @@ pub(crate) fn alloc_layout_new(
super::shared_memory_provider::CSHMProvider::DynamicThreadsafe(provider) => {
match provider
.alloc(size)
.with_alignment(alignment.into())
.with_alignment(alignment.transmute_copy())
.into_layout()
{
Ok(layout) => CSHMLayout::DynamicThreadsafe(layout),
Expand Down
9 changes: 8 additions & 1 deletion src/shm/provider/shared_memory_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use super::{
shared_memory_provider_backend::{
zc_shared_memory_provider_backend_callbacks_t, DynamicSharedMemoryProviderBackend,
},
shared_memory_provider_impl::{alloc, alloc_async, defragment, garbage_collect, map},
shared_memory_provider_impl::{alloc, alloc_async, available, defragment, garbage_collect, map},
types::z_alloc_alignment_t,
};

Expand Down Expand Up @@ -248,6 +248,13 @@ pub extern "C" fn z_shared_memory_provider_garbage_collect(
garbage_collect(provider);
}

#[no_mangle]
pub extern "C" fn z_shared_memory_provider_available(
provider: &z_loaned_shared_memory_provider_t,
) -> usize {
available(provider)
}

#[no_mangle]
pub extern "C" fn z_shared_memory_provider_map(
out_result: *mut MaybeUninit<z_owned_shm_mut_t>,
Expand Down
1 change: 0 additions & 1 deletion src/shm/provider/shared_memory_provider_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub struct zc_shared_memory_provider_backend_callbacks_t {
defragment_fn: unsafe extern "C" fn(*mut c_void) -> usize,
available_fn: unsafe extern "C" fn(*mut c_void) -> usize,
layout_for_fn: unsafe extern "C" fn(*mut c_void, *mut z_owned_memory_layout_t),
drop_fn: unsafe extern "C" fn(*mut c_void),
}

#[derive(Debug)]
Expand Down
18 changes: 16 additions & 2 deletions src/shm/provider/shared_memory_provider_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh::shm::{AllocPolicy, AsyncAllocPolicy, SharedMemoryProvider};

use crate::context::{Context, DroppableContext, ThreadsafeContext};
use crate::errors::{z_error_t, Z_EINVAL, Z_OK};
use crate::transmute::{Inplace, TransmuteFromHandle, TransmuteUninitPtr};
use crate::transmute::{Inplace, TransmuteCopy, TransmuteFromHandle, TransmuteUninitPtr};
use crate::{z_owned_buf_alloc_result_t, z_owned_shm_mut_t};

use super::chunk::z_allocated_chunk_t;
Expand Down Expand Up @@ -132,6 +132,20 @@ pub(crate) fn garbage_collect(provider: &z_loaned_shared_memory_provider_t) {
}
}

pub(crate) fn available(provider: &z_loaned_shared_memory_provider_t) -> usize {
match provider.transmute_ref() {
super::shared_memory_provider::CSHMProvider::Posix(provider) => {
provider.available()
}
super::shared_memory_provider::CSHMProvider::Dynamic(provider) => {
provider.available()
}
super::shared_memory_provider::CSHMProvider::DynamicThreadsafe(provider) => {
provider.available()
}
}
}

#[no_mangle]
pub(crate) fn map(
out_result: *mut MaybeUninit<z_owned_shm_mut_t>,
Expand Down Expand Up @@ -172,7 +186,7 @@ fn alloc_impl<
) -> z_error_t {
let result = provider
.alloc(size)
.with_alignment(alignment.into())
.with_alignment(alignment.transmute_copy())
.with_policy::<Policy>()
.wait();

Expand Down
23 changes: 16 additions & 7 deletions src/shm/provider/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_util::core::zerror;
use crate::{
errors::{z_error_t, Z_EINVAL, Z_OK},
transmute::{
unwrap_ref_unchecked, Inplace, TransmuteIntoHandle, TransmuteRef, TransmuteUninitPtr,
unwrap_ref_unchecked, Inplace, TransmuteCopy, TransmuteFromHandle, TransmuteIntoHandle, TransmuteRef, TransmuteUninitPtr
},
z_loaned_buf_alloc_result_t, z_loaned_chunk_alloc_result_t, z_loaned_memory_layout_t,
z_owned_buf_alloc_result_t, z_owned_chunk_alloc_result_t, z_owned_memory_layout_t,
Expand Down Expand Up @@ -66,15 +66,12 @@ impl From<z_alloc_error_t> for ZAllocError {

// An AllocAlignment.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct z_alloc_alignment_t {
pub pow: u8,
pow: u8,
}

impl From<z_alloc_alignment_t> for AllocAlignment {
fn from(value: z_alloc_alignment_t) -> Self {
Self::new(value.pow)
}
}
decl_transmute_copy!(AllocAlignment, z_alloc_alignment_t);

decl_transmute_owned!(Option<MemoryLayout>, z_owned_memory_layout_t);
decl_transmute_handle!(MemoryLayout, z_loaned_memory_layout_t);
Expand Down Expand Up @@ -126,6 +123,18 @@ pub extern "C" fn z_memory_layout_drop(this: &mut z_owned_memory_layout_t) {
let _ = this.transmute_mut().take();
}

/// Deletes Memory Layout
#[no_mangle]
pub extern "C" fn z_memory_layout_get_data(
out_size: &mut MaybeUninit<usize>,
out_alignment: &mut MaybeUninit<z_alloc_alignment_t>,
this: &z_loaned_memory_layout_t,
) {
let layout = this.transmute_ref();
out_size.write(layout.size());
out_alignment.write(layout.alignment().transmute_copy() );
}

decl_transmute_owned!(Option<ChunkAllocResult>, z_owned_chunk_alloc_result_t);

decl_transmute_handle!(ChunkAllocResult, z_loaned_chunk_alloc_result_t);
Expand Down
Loading

0 comments on commit 0dece15

Please sign in to comment.