diff --git a/Cargo.toml b/Cargo.toml index d0dc090b6..da45726c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,26 +33,26 @@ license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/eclipse-iceoryx/iceoryx2" rust-version = "1.72.1" -version = "0.0.2" +version = "0.0.5" [workspace.dependencies] -iceoryx2-bb-threadsafe = { version = "0.0.2", path = "iceoryx2-bb/threadsafe/" } -iceoryx2-bb-lock-free = { version = "0.0.2", path = "iceoryx2-bb/lock-free/" } -iceoryx2-bb-container = { version = "0.0.2", path = "iceoryx2-bb/container/" } -iceoryx2-bb-elementary = { version = "0.0.2", path = "iceoryx2-bb/elementary/" } -iceoryx2-bb-log = { version = "0.0.2", path = "iceoryx2-bb/log/" } -iceoryx2-bb-memory = { version = "0.0.2", path = "iceoryx2-bb/memory/" } -iceoryx2-bb-posix = { version = "0.0.2", path = "iceoryx2-bb/posix/" } -iceoryx2-bb-system-types = { version = "0.0.2", path = "iceoryx2-bb/system-types/" } -iceoryx2-bb-testing = { version = "0.0.2", path = "iceoryx2-bb/testing/" } +iceoryx2-bb-threadsafe = { version = "0.0.5", path = "iceoryx2-bb/threadsafe/" } +iceoryx2-bb-lock-free = { version = "0.0.5", path = "iceoryx2-bb/lock-free/" } +iceoryx2-bb-container = { version = "0.0.5", path = "iceoryx2-bb/container/" } +iceoryx2-bb-elementary = { version = "0.0.5", path = "iceoryx2-bb/elementary/" } +iceoryx2-bb-log = { version = "0.0.5", path = "iceoryx2-bb/log/" } +iceoryx2-bb-memory = { version = "0.0.5", path = "iceoryx2-bb/memory/" } +iceoryx2-bb-posix = { version = "0.0.5", path = "iceoryx2-bb/posix/" } +iceoryx2-bb-system-types = { version = "0.0.5", path = "iceoryx2-bb/system-types/" } +iceoryx2-bb-testing = { version = "0.0.5", path = "iceoryx2-bb/testing/" } -iceoryx2-pal-concurrency-sync = { version = "0.0.2", path = "iceoryx2-pal/concurrency-sync/" } -iceoryx2-pal-posix = { version = "0.0.2", path = "iceoryx2-pal/posix/" } -iceoryx2-pal-configuration = { version = "0.0.2", path = "iceoryx2-pal/configuration/" } +iceoryx2-pal-concurrency-sync = { version = "0.0.5", path = "iceoryx2-pal/concurrency-sync/" } +iceoryx2-pal-posix = { version = "0.0.5", path = "iceoryx2-pal/posix/" } +iceoryx2-pal-configuration = { version = "0.0.5", path = "iceoryx2-pal/configuration/" } -iceoryx2-cal = { version = "0.0.2", path = "iceoryx2-cal" } +iceoryx2-cal = { version = "0.0.5", path = "iceoryx2-cal" } -iceoryx2 = { version = "0.0.2", path = "iceoryx2/" } +iceoryx2 = { version = "0.0.5", path = "iceoryx2/" } bindgen = { version = "0.65.1" } bitflags = { version = "1.3.2" } diff --git a/iceoryx2-bb/container/Cargo.toml b/iceoryx2-bb/container/Cargo.toml index 13ceeda1f..d6e7fd1b7 100644 --- a/iceoryx2-bb/container/Cargo.toml +++ b/iceoryx2-bb/container/Cargo.toml @@ -17,7 +17,5 @@ iceoryx2-bb-elementary = { workspace = true } iceoryx2-bb-log = { workspace = true } [dev-dependencies] -pin-init = { workspace = true } generic-tests = { workspace = true } -iceoryx2-bb-memory = { workspace = true } iceoryx2-bb-testing = { workspace = true } diff --git a/iceoryx2-bb/container/src/queue.rs b/iceoryx2-bb/container/src/queue.rs index bb91a8017..a3c6cd25a 100644 --- a/iceoryx2-bb/container/src/queue.rs +++ b/iceoryx2-bb/container/src/queue.rs @@ -46,7 +46,7 @@ //! //! ``` //! use iceoryx2_bb_container::queue::RelocatableQueue; -//! use iceoryx2_bb_memory::bump_allocator::BumpAllocator; +//! use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; //! use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; //! use std::ptr::NonNull; //! @@ -54,9 +54,7 @@ //! const MEM_SIZE: usize = RelocatableQueue::::const_memory_size(QUEUE_CAPACITY); //! let mut memory = [0u8; MEM_SIZE]; //! -//! let bump_allocator = BumpAllocator::new( -//! unsafe { NonNull::new_unchecked(memory.as_mut_ptr() as *mut u8) }, -//! MEM_SIZE); +//! let bump_allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); //! //! let queue = unsafe { RelocatableQueue::::new_uninit(QUEUE_CAPACITY) }; //! unsafe { queue.init(&bump_allocator).expect("queue init failed") }; diff --git a/iceoryx2-bb/container/src/vec.rs b/iceoryx2-bb/container/src/vec.rs index faca0907e..132f21518 100644 --- a/iceoryx2-bb/container/src/vec.rs +++ b/iceoryx2-bb/container/src/vec.rs @@ -47,7 +47,7 @@ //! //! ``` //! use iceoryx2_bb_container::vec::Vec; -//! use iceoryx2_bb_memory::bump_allocator::BumpAllocator; +//! use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; //! use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; //! use std::ptr::NonNull; //! @@ -55,9 +55,7 @@ //! const MEM_SIZE: usize = Vec::::const_memory_size(VEC_CAPACITY); //! let mut memory = [0u8; MEM_SIZE]; //! -//! let bump_allocator = BumpAllocator::new( -//! unsafe { NonNull::new_unchecked(memory.as_mut_ptr() as *mut u8) }, -//! MEM_SIZE); +//! let bump_allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); //! //! let vec = unsafe { Vec::::new_uninit(VEC_CAPACITY) }; //! unsafe { vec.init(&bump_allocator).expect("vec init failed") }; diff --git a/iceoryx2-bb/container/tests/queue_tests.rs b/iceoryx2-bb/container/tests/queue_tests.rs index a5f0676a7..d88be789b 100644 --- a/iceoryx2-bb/container/tests/queue_tests.rs +++ b/iceoryx2-bb/container/tests/queue_tests.rs @@ -11,24 +11,19 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use iceoryx2_bb_container::queue::*; -use iceoryx2_bb_memory::{bump_allocator::BumpAllocator, memory::Memory}; +use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; use iceoryx2_bb_testing::assert_that; -use pin_init::init_stack; const SUT_CAPACITY: usize = 128; type Sut = FixedSizeQueue; #[test] fn queue_push_pop_works_with_uninitialized_memory() { - init_stack!( - memory = - Memory::<{ Queue::::const_memory_size(129_usize) }, BumpAllocator>::new_filled( - 0xff, - ) - ); - let memory = memory.unwrap(); + let mut memory = [0u8; 1024]; + let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); + let mut sut = unsafe { RelocatableQueue::::new_uninit(SUT_CAPACITY) }; - unsafe { assert_that!(sut.init(memory.allocator()), is_ok) }; + unsafe { assert_that!(sut.init(&allocator), is_ok) }; for i in 0..sut.capacity() { let element = i * 2 + 3; diff --git a/iceoryx2-bb/container/tests/vec_tests.rs b/iceoryx2-bb/container/tests/vec_tests.rs index 68b7f7297..d2c3d2d4a 100644 --- a/iceoryx2-bb/container/tests/vec_tests.rs +++ b/iceoryx2-bb/container/tests/vec_tests.rs @@ -11,10 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use iceoryx2_bb_container::vec::*; +use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; -use iceoryx2_bb_memory::{bump_allocator::BumpAllocator, memory::Memory}; use iceoryx2_bb_testing::assert_that; -use pin_init::init_stack; const SUT_CAPACITY: usize = 128; type Sut = FixedSizeVec; @@ -70,15 +69,10 @@ fn fixed_size_vec_push_pop_works() { #[test] fn vec_push_pop_works_with_uninitialized_memory() { - init_stack!( - memory = - Memory::<{ Vec::::const_memory_size(129_usize) }, BumpAllocator>::new_filled( - 0xff, - ) - ); - let memory = memory.unwrap(); + let mut memory = [0u8; 1024]; + let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); let mut sut = unsafe { Vec::::new_uninit(SUT_CAPACITY) }; - unsafe { assert_that!(sut.init(memory.allocator()), is_ok) }; + unsafe { assert_that!(sut.init(&allocator), is_ok) }; for i in 0..sut.capacity() { let element = i * 2 + 3; diff --git a/iceoryx2-bb/elementary/src/bump_allocator.rs b/iceoryx2-bb/elementary/src/bump_allocator.rs new file mode 100644 index 000000000..da2796af0 --- /dev/null +++ b/iceoryx2-bb/elementary/src/bump_allocator.rs @@ -0,0 +1,56 @@ +// Copyright (c) 2023 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use crate::{allocator::BaseAllocator, math::align}; +use std::sync::atomic::{AtomicUsize, Ordering}; + +/// Simple BumpAllocator for testing purposes. Do not use this in production. If you are looking +/// for a production ready BumpAllocator use the one from iceoryx2_bb_memory::bump_allocator +#[doc(hidden)] +pub struct BumpAllocator { + start: usize, + pos: AtomicUsize, +} + +impl BumpAllocator { + pub fn new(start: usize) -> Self { + Self { + start, + pos: AtomicUsize::new(start), + } + } +} + +impl BaseAllocator for BumpAllocator { + fn allocate( + &self, + layout: std::alloc::Layout, + ) -> Result, crate::allocator::AllocationError> { + let mem = align(self.pos.load(Ordering::Relaxed), layout.align()); + self.pos.store(mem + layout.size(), Ordering::Relaxed); + + unsafe { + Ok(std::ptr::NonNull::new_unchecked( + std::slice::from_raw_parts_mut(mem as *mut u8, layout.size()), + )) + } + } + + unsafe fn deallocate( + &self, + _ptr: std::ptr::NonNull, + _layout: std::alloc::Layout, + ) -> Result<(), crate::allocator::DeallocationError> { + self.pos.store(self.start, Ordering::Relaxed); + Ok(()) + } +} diff --git a/iceoryx2-bb/elementary/src/lib.rs b/iceoryx2-bb/elementary/src/lib.rs index b26854294..972622e10 100644 --- a/iceoryx2-bb/elementary/src/lib.rs +++ b/iceoryx2-bb/elementary/src/lib.rs @@ -15,6 +15,7 @@ #[macro_use] pub mod enum_gen; pub mod allocator; +pub mod bump_allocator; pub mod lazy_singleton; pub mod math; pub mod owning_pointer; diff --git a/iceoryx2-bb/lock-free/Cargo.toml b/iceoryx2-bb/lock-free/Cargo.toml index 24b5358bd..6d102c3ef 100644 --- a/iceoryx2-bb/lock-free/Cargo.toml +++ b/iceoryx2-bb/lock-free/Cargo.toml @@ -18,7 +18,6 @@ tiny-fn = { workspace = true } [dev-dependencies] iceoryx2-bb-posix = { workspace = true } -iceoryx2-bb-memory = { workspace = true } iceoryx2-bb-testing = { workspace = true } pin-init = { workspace = true } generic-tests = { workspace = true } diff --git a/iceoryx2-bb/lock-free/src/mpmc/unique_index_set.rs b/iceoryx2-bb/lock-free/src/mpmc/unique_index_set.rs index f32edb756..3be091dcf 100644 --- a/iceoryx2-bb/lock-free/src/mpmc/unique_index_set.rs +++ b/iceoryx2-bb/lock-free/src/mpmc/unique_index_set.rs @@ -19,15 +19,16 @@ //! ## Runtime fixed size UniqueIndexSet //! //! ``` -//! use iceoryx2_bb_memory::heap_allocator::*; +//! use iceoryx2_bb_elementary::bump_allocator::*; //! use iceoryx2_bb_lock_free::mpmc::unique_index_set::*; //! use iceoryx2_bb_elementary::relocatable_container::*; //! //! const CAPACITY: usize = 128; -//! let heap_allocator = HeapAllocator::new(); +//! let mut memory = [0u8; UniqueIndexSet::const_memory_size(CAPACITY)]; +//! let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); //! //! let index_set = unsafe { UniqueIndexSet::new_uninit(CAPACITY) }; -//! unsafe { index_set.init(&heap_allocator) }.expect("failed to allocate enough memory"); +//! unsafe { index_set.init(&allocator) }.expect("failed to allocate enough memory"); //! //! let new_index = match unsafe { index_set.acquire() } { //! None => panic!("Out of indices"), @@ -147,15 +148,16 @@ impl Drop for UniqueIndex<'_> { /// /// ## With a custom allocator /// ``` -/// use iceoryx2_bb_memory::heap_allocator::*; +/// use iceoryx2_bb_elementary::bump_allocator::*; /// use iceoryx2_bb_lock_free::mpmc::unique_index_set::*; /// use iceoryx2_bb_elementary::relocatable_container::*; /// /// const CAPACITY: usize = 128; -/// let heap_allocator = HeapAllocator::new(); +/// let mut memory = [0u8; UniqueIndexSet::const_memory_size(CAPACITY)]; +/// let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); /// /// let index_set = unsafe { UniqueIndexSet::new_uninit(CAPACITY) }; -/// unsafe { index_set.init(&heap_allocator) }.expect("failed to allocate enough memory"); +/// unsafe { index_set.init(&allocator) }.expect("failed to allocate enough memory"); /// /// let new_index = match unsafe { index_set.acquire() } { /// None => panic!("Out of indices"), diff --git a/iceoryx2-bb/lock-free/tests/mpmc_container_tests.rs b/iceoryx2-bb/lock-free/tests/mpmc_container_tests.rs index e49aad03e..1fc2f75ff 100644 --- a/iceoryx2-bb/lock-free/tests/mpmc_container_tests.rs +++ b/iceoryx2-bb/lock-free/tests/mpmc_container_tests.rs @@ -44,14 +44,12 @@ unsafe impl Send for TestType {} #[generic_tests::define] mod mpmc_container { + use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; use iceoryx2_bb_lock_free::mpmc::container::*; use iceoryx2_bb_lock_free::mpmc::unique_index_set::*; - use iceoryx2_bb_memory::bump_allocator::BumpAllocator; - use iceoryx2_bb_memory::memory::Memory; use iceoryx2_bb_posix::system_configuration::SystemInfo; use iceoryx2_bb_testing::assert_that; - use pin_init::init_stack; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; @@ -117,15 +115,10 @@ mod mpmc_container { >() { // TestType is the largest test type so it is safe to acquire this memory for every test // case - hack required since `T` cannot be used in const operations - init_stack!( - memory = Memory::< - { Container::::const_memory_size(129_usize) }, - BumpAllocator, - >::new_filled(0xff,) - ); - let memory = memory.unwrap(); + let mut memory = [0u8; Container::::const_memory_size(129_usize)]; + let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); let sut = unsafe { Container::::new_uninit(CAPACITY) }; - unsafe { assert_that!(sut.init(memory.allocator()), is_ok) }; + unsafe { assert_that!(sut.init(&allocator), is_ok) }; let mut stored_indices = vec![]; for i in 0..CAPACITY - 1 { diff --git a/iceoryx2-bb/lock-free/tests/mpmc_unique_index_set_tests.rs b/iceoryx2-bb/lock-free/tests/mpmc_unique_index_set_tests.rs index 3ba3f7e0b..a57fb0993 100644 --- a/iceoryx2-bb/lock-free/tests/mpmc_unique_index_set_tests.rs +++ b/iceoryx2-bb/lock-free/tests/mpmc_unique_index_set_tests.rs @@ -10,13 +10,11 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT +use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; use iceoryx2_bb_lock_free::mpmc::unique_index_set::*; -use iceoryx2_bb_memory::bump_allocator::BumpAllocator; -use iceoryx2_bb_memory::memory::Memory; use iceoryx2_bb_posix::system_configuration::SystemInfo; use iceoryx2_bb_testing::assert_that; -use pin_init::init_stack; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Barrier, Mutex}; use std::thread; @@ -101,13 +99,10 @@ fn mpmc_unique_index_set_borrowed_indices_works() { #[test] fn mpmc_unique_index_set_acquire_and_release_works_with_uninitialized_memory() { - init_stack!( - memory = - Memory::<{ UniqueIndexSet::const_memory_size(128) }, BumpAllocator>::new_filled(0xff,) - ); - let memory = memory.unwrap(); + let mut memory = [0u8; UniqueIndexSet::const_memory_size(128)]; + let allocator = BumpAllocator::new(memory.as_mut_ptr() as usize); let sut = unsafe { UniqueIndexSet::new_uninit(CAPACITY) }; - unsafe { assert_that!(sut.init(memory.allocator()), is_ok) }; + unsafe { assert_that!(sut.init(&allocator), is_ok) }; let mut ids = vec![]; diff --git a/iceoryx2-pal/posix/build.rs b/iceoryx2-pal/posix/build.rs index f661a0dac..5791262ff 100644 --- a/iceoryx2-pal/posix/build.rs +++ b/iceoryx2-pal/posix/build.rs @@ -17,18 +17,30 @@ use std::env; use std::path::PathBuf; fn main() { - #[cfg(target_os = "linux")] - println!("cargo:rustc-link-lib=acl"); #[cfg(any(target_os = "linux", target_os = "freebsd"))] println!("cargo:rustc-link-lib=pthread"); - println!("cargo:rerun-if-changed=src/c/posix.h"); - - let bindings = bindgen::Builder::default() - .header("src/c/posix.h") - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - .generate() - .expect("Unable to generate bindings"); + if std::env::var("DOCS_RS").is_ok() { + println!("cargo:rerun-if-changed=src/c/posix_docs_rs.h"); + } else { + #[cfg(target_os = "linux")] + println!("cargo:rustc-link-lib=acl"); + println!("cargo:rerun-if-changed=src/c/posix.h"); + } + + let bindings = if std::env::var("DOCS_RS").is_ok() { + bindgen::Builder::default() + .header("src/c/posix_docs_rs.h") + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .generate() + .expect("Unable to generate bindings") + } else { + bindgen::Builder::default() + .header("src/c/posix.h") + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .generate() + .expect("Unable to generate bindings") + }; let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings diff --git a/iceoryx2-pal/posix/src/c/posix_docs_rs.h b/iceoryx2-pal/posix/src/c/posix_docs_rs.h new file mode 100644 index 000000000..a7696b7ba --- /dev/null +++ b/iceoryx2-pal/posix/src/c/posix_docs_rs.h @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/////////////////////////////// +// stub libacl.h implementation +/////////////////////////////// + +typedef int acl_tag_t; +typedef unsigned int acl_perm_t; +typedef int acl_type_t; +typedef int acl_t; +typedef int acl_entry_t; +typedef int acl_permset_t; + +#define ACL_EXECUTE 0x01 +#define ACL_WRITE 0x02 +#define ACL_READ 0x04 + +#define ACL_UNDEFINED_TAG 0 +#define ACL_USER_OBJ 1 +#define ACL_USER 2 +#define ACL_GROUP_OBJ 3 +#define ACL_GROUP 4 +#define ACL_MASK 5 +#define ACL_OTHER 6 + +#define ACL_FIRST_ENTRY 7 +#define ACL_NEXT_ENTRY 8 + +int acl_get_perm(acl_permset_t, acl_perm_t) { return 0; } +acl_t acl_init(int) { return 0; } +int acl_free(void *) { return 0; } +int acl_valid(acl_t) { return 0; } +int acl_create_entry(acl_t *, acl_entry_t *) { return 0; } +int acl_get_entry(acl_t, int, acl_entry_t *) { return 0; } +int acl_add_perm(acl_permset_t, acl_perm_t) { return 0; } +int acl_clear_perms(acl_permset_t) { return 0; } +int acl_get_permset(acl_entry_t, acl_permset_t *) { return 0; } +int acl_set_permset(acl_entry_t, acl_permset_t) { return 0; } +void *acl_get_qualifier(acl_entry_t) { return NULL; } +int acl_set_qualifier(acl_entry_t, const void *) { return 0; } +int acl_get_tag_type(acl_entry_t, acl_tag_t *) { return 0; } +int acl_set_tag_type(acl_entry_t, acl_tag_t) { return 0; } +acl_t acl_get_fd(int) { return 0; } +int acl_set_fd(int, acl_t) { return 0; } +char *acl_to_text(acl_t, ssize_t *) { return NULL; } +acl_t acl_from_text(const char *) { return 0; }