Skip to content

Commit

Permalink
works on stable rust
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangpengHao committed Oct 7, 2024
1 parent 2263792 commit 5a1d660
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
Expand All @@ -37,7 +37,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
Expand All @@ -55,7 +55,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rust-src
- run: sudo apt install libnuma-dev llvm-dev
Expand All @@ -76,7 +76,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rust-src
- run: sudo apt install libnuma-dev
Expand All @@ -93,7 +93,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rust-src
- run: sudo apt install libnuma-dev
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
- uses: actions-rs/toolchain@v1
name: Setup toolchain
with:
toolchain: nightly
toolchain: stable
override: true
components: rust-src, llvm-tools-preview
- run: sudo apt install libnuma-dev
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "congee"
version = "0.2.18"
version = "0.3.0"
edition = "2021"
description = "A Rust implementation of ART-OLC concurrent adaptive radix tree."
keywords = ["ART", "adaptive-radix-tree", "concurrent"]
Expand All @@ -14,13 +14,13 @@ license = "MIT"
[dependencies]
crossbeam-epoch = "0.9.18"
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0.205", features = ["derive"], optional = true }
serde = { version = "1.0.210", features = ["derive"], optional = true }

[dev-dependencies]
rand = "0.8.5"
shumai = "0.2.15"
serde = "1.0.205"
serde_json = "1.0.122"
serde = "1.0.210"
serde_json = "1.0.128"
flurry = "0.5.1"
mimalloc = { version = "0.1.43", default-features = false }
selfsimilar = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl BaseNode {
let ptr = allocator
.allocate_zeroed(layout)
.map_err(|_e| ArtError::Oom)?;
let ptr = ptr.as_non_null_ptr().as_ptr() as *mut BaseNode;
let ptr = ptr.as_ptr() as *mut BaseNode;
let node = BaseNode::new(N::get_type(), prefix);
unsafe {
std::ptr::write(ptr, node);
Expand Down
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#![allow(clippy::comparison_chain)]
#![allow(clippy::len_without_is_empty)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![feature(slice_ptr_get)]
#![feature(allocator_api)]

mod base_node;
mod error;
Expand All @@ -25,8 +23,6 @@ mod stats;
#[cfg(test)]
mod tests;

use std::alloc::AllocError;
use std::alloc::Allocator;
use std::marker::PhantomData;

use error::OOMError;
Expand All @@ -45,8 +41,28 @@ pub struct DefaultAllocator {}
unsafe impl Send for DefaultAllocator {}
unsafe impl Sync for DefaultAllocator {}

unsafe impl Allocator for DefaultAllocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, AllocError> {
/// We should use the `Allocator` trait in the std, but it is not stable yet.
/// https://github.com/rust-lang/rust/issues/32838
pub trait Allocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, OOMError>;
fn allocate_zeroed(
&self,
layout: std::alloc::Layout,
) -> Result<std::ptr::NonNull<[u8]>, OOMError> {
let ptr = self.allocate(layout)?;
unsafe {
std::ptr::write_bytes(ptr.as_ptr() as *mut u8, 0, layout.size());
}
Ok(ptr)
}
/// # Safety
/// The caller must ensure that the pointer is valid and that the layout is correct.
/// The pointer must allocated by this allocator.
unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout);
}

impl Allocator for DefaultAllocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, OOMError> {
let ptr = unsafe { std::alloc::alloc(layout) };
let ptr_slice = std::ptr::slice_from_raw_parts_mut(ptr, layout.size());
Ok(std::ptr::NonNull::new(ptr_slice).unwrap())
Expand Down
17 changes: 7 additions & 10 deletions src/tests/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{
alloc::AllocError,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};

use crate::{node_256::Node256, node_4::Node4, Allocator, Art};
use crate::{error::OOMError, node_256::Node256, node_4::Node4, Allocator, Art};

struct SmallAllocatorInner {
max_size: AtomicUsize,
Expand All @@ -23,8 +20,8 @@ impl SmallAllocator {
}
}

unsafe impl Allocator for SmallAllocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, AllocError> {
impl Allocator for SmallAllocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, OOMError> {
let current_size = self.0.max_size.load(Ordering::Relaxed);
if current_size >= layout.size() {
self.0
Expand All @@ -34,7 +31,7 @@ unsafe impl Allocator for SmallAllocator {
let ptr_slice = std::ptr::slice_from_raw_parts_mut(ptr, layout.size());
Ok(std::ptr::NonNull::new(ptr_slice).unwrap())
} else {
return Err(AllocError);
Err(OOMError::new())
}
}

Expand Down

0 comments on commit 5a1d660

Please sign in to comment.