Skip to content

Commit

Permalink
feat: Rustを1.84.0に上げ、その新機能を利用する (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Jan 11, 2025
1 parent 20178ac commit fda1e71
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ rev = "de226a26e8e18edbdb1d6f986afe37bbbf35fbf4"
version = "0.0.0"
edition = "2021"
publish = false
rust-version = "1.81.0"
rust-version = "1.84.0"
license = "MIT"

# min-sized-rustを元にrelease buildのサイズが小さくなるようにした
Expand Down
8 changes: 3 additions & 5 deletions crates/voicevox_core/src/infer/runtimes/onnxruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,11 @@ pub(crate) mod blocking {
/// # .filename(test_util::ONNXRUNTIME_DYLIB_PATH)
/// # .exec()?;
/// # }
/// use std::ptr;
///
/// let ort1 = voicevox_core::blocking::Onnxruntime::load_once().exec()?;
/// let ort2 = another_lib::nonblocking::Onnxruntime::get().expect("`ort1`と同一のはず");
/// assert_eq!(ptr_addr(ort1), ptr_addr(ort2));
///
/// fn ptr_addr(obj: &impl Sized) -> usize {
/// &raw const *obj as _
/// }
/// assert!(ptr::addr_eq(ort1, ort2));
/// # Ok(())
/// # }
/// ```
Expand Down
23 changes: 17 additions & 6 deletions crates/voicevox_core_c_api/src/drop_check.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::{
collections::BTreeSet,
ffi::{c_char, CStr, CString},
num::NonZero,
ptr::NonNull,
sync::Mutex,
};

use easy_ext::ext;

/// dropして良い`*mut c_char`を把握し、チェックする。
///
/// `Mutex`による内部可変性を持ち、すべての操作は共有参照から行うことができる。
Expand All @@ -26,8 +30,8 @@ pub(crate) static C_STRING_DROP_CHECKER: CStringDropChecker = CStringDropChecker
pub(crate) struct CStringDropChecker(Mutex<Inner>);

struct Inner {
owned_str_addrs: BTreeSet<usize>,
static_str_addrs: BTreeSet<usize>,
owned_str_addrs: BTreeSet<NonZero<usize>>,
static_str_addrs: BTreeSet<NonZero<usize>>,
}

impl CStringDropChecker {
Expand All @@ -46,8 +50,8 @@ impl CStringDropChecker {
owned_str_addrs, ..
} = &mut *self.0.lock().unwrap();

let ptr = s.as_ptr();
let duplicated = !owned_str_addrs.insert(ptr as usize);
let ptr = s.as_non_null_ptr();
let duplicated = !owned_str_addrs.insert(ptr.addr());
if duplicated {
panic!(
"別の{ptr:p}が管理下にあります。原因としては以前に別の文字列が{ptr:p}として存在\
Expand All @@ -69,7 +73,7 @@ impl CStringDropChecker {
static_str_addrs, ..
} = &mut *self.0.lock().unwrap();

static_str_addrs.insert(s.as_ptr() as usize);
static_str_addrs.insert(s.as_non_null_ptr().addr());
s
}

Expand All @@ -85,7 +89,7 @@ impl CStringDropChecker {
..
} = &mut *self.0.lock().unwrap();

let addr = ptr as usize;
let addr = NonZero::new(ptr.addr()).expect("ヌルポインタは解放できません");
if !owned_str_addrs.remove(&addr) {
if static_str_addrs.contains(&addr) {
panic!(
Expand All @@ -103,6 +107,13 @@ impl CStringDropChecker {
}
}

#[ext]
impl CStr {
fn as_non_null_ptr(&self) -> NonNull<c_char> {
NonNull::new(self.as_ptr() as *mut c_char).expect("comes from a `CStr`")
}
}

#[cfg(test)]
mod tests {
use std::ffi::{c_char, CStr};
Expand Down
6 changes: 3 additions & 3 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_synthesis(
style_id: VoicevoxStyleId,
options: VoicevoxSynthesisOptions,
output_wav_length: NonNull<usize>,
output_wav: NonNull<*mut u8>,
output_wav: NonNull<NonNull<u8>>,
) -> VoicevoxResultCode {
init_logger_once();
into_result_code_with_error((|| {
Expand Down Expand Up @@ -1137,7 +1137,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_tts_from_kana(
style_id: VoicevoxStyleId,
options: VoicevoxTtsOptions,
output_wav_length: NonNull<usize>,
output_wav: NonNull<*mut u8>,
output_wav: NonNull<NonNull<u8>>,
) -> VoicevoxResultCode {
init_logger_once();
into_result_code_with_error((|| {
Expand Down Expand Up @@ -1182,7 +1182,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_tts(
style_id: VoicevoxStyleId,
options: VoicevoxTtsOptions,
output_wav_length: NonNull<usize>,
output_wav: NonNull<*mut u8>,
output_wav: NonNull<NonNull<u8>>,
) -> VoicevoxResultCode {
init_logger_once();
into_result_code_with_error((|| {
Expand Down
17 changes: 5 additions & 12 deletions crates/voicevox_core_c_api/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ pub(crate) trait CApiObject: Default + Debug + 'static {
let i = Self::heads().push(Default::default());
NonNull::from(&Self::heads()[i])
};
Self::lock_known_addrs().insert(this.addr_without_provenance());
Self::lock_known_addrs().insert(this.addr());
let body = parking_lot::RwLock::new(body.into()).into();
Self::lock_bodies().insert(this.addr_without_provenance(), body);
Self::lock_bodies().insert(this.addr(), body);
this
}
}
Expand All @@ -86,7 +86,7 @@ impl<T: CApiObject> *const T {
let this = self.validate();

let body = T::lock_bodies()
.get(&this.addr_without_provenance())
.get(&this.addr())
.unwrap_or_else(|| this.panic_for_deleted())
.read_arc();

Expand All @@ -103,7 +103,7 @@ impl<T: CApiObject> *const T {
let this = self.validate();

let body = T::lock_bodies()
.remove(&this.addr_without_provenance())
.remove(&this.addr())
.unwrap_or_else(|| this.panic_for_deleted());

drop(
Expand All @@ -125,7 +125,7 @@ impl<T: CApiObject> *const T {
impl<T: CApiObject> *const T {
fn validate(self) -> NonNull<T> {
let this = NonNull::new(self as *mut T).expect("the argument must not be null");
if !T::lock_known_addrs().contains(&this.addr_without_provenance()) {
if !T::lock_known_addrs().contains(&this.addr()) {
panic!("{self:018p} does not seem to be valid object");
}
this
Expand All @@ -147,13 +147,6 @@ impl<T: CApiObject> NonNull<T> {
}
}

#[ext]
impl<T> NonNull<T> {
fn addr_without_provenance(self) -> NonZero<usize> {
NonZero::new(self.as_ptr() as _).expect("this is from `NonNull`")
}
}

#[ext]
impl<T: CApiObject> T {
fn lock_known_addrs() -> impl DerefMut<Target = HashSet<NonZero<usize>>> {
Expand Down
22 changes: 12 additions & 10 deletions crates/voicevox_core_c_api/src/slice_owner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::UnsafeCell, collections::BTreeMap, ptr::NonNull, sync::Mutex};
use std::{cell::UnsafeCell, collections::BTreeMap, num::NonZeroUsize, ptr::NonNull, sync::Mutex};

/// Cの世界に貸し出す`[u8]`の所有者(owner)。
///
Expand All @@ -16,7 +16,7 @@ use std::{cell::UnsafeCell, collections::BTreeMap, ptr::NonNull, sync::Mutex};
pub(crate) static U8_SLICE_OWNER: SliceOwner<u8> = SliceOwner::new();

pub(crate) struct SliceOwner<T> {
slices: Mutex<BTreeMap<usize, UnsafeCell<Box<[T]>>>>,
slices: Mutex<BTreeMap<NonZeroUsize, UnsafeCell<Box<[T]>>>>,
}

impl<T> SliceOwner<T> {
Expand All @@ -37,16 +37,16 @@ impl<T> SliceOwner<T> {
pub(crate) unsafe fn own_and_lend(
&self,
slice: impl Into<Box<[T]>>,
out_ptr: NonNull<*mut T>,
out_ptr: NonNull<NonNull<T>>,
out_len: NonNull<usize>,
) {
let mut slices = self.slices.lock().unwrap();

let slice = slice.into();
let ptr = slice.as_ptr() as *mut T;
let ptr = NonNull::new(slice.as_ptr() as *mut T).expect("comes from a slice");
let len = slice.len();

let duplicated = slices.insert(ptr as usize, slice.into()).is_some();
let duplicated = slices.insert(ptr.addr(), slice.into()).is_some();
if duplicated {
panic!(
"別の{ptr:p}が管理下にあります。原因としては以前に別の配列が{ptr:p}として存在\
Expand All @@ -67,10 +67,12 @@ impl<T> SliceOwner<T> {
pub(crate) fn drop_for(&self, ptr: *mut T) {
let mut slices = self.slices.lock().unwrap();

slices.remove(&(ptr as usize)).expect(
"解放しようとしたポインタはvoicevox_coreの管理下にありません。\
誤ったポインタであるか、二重解放になっていることが考えられます",
);
NonNull::new(ptr)
.and_then(|ptr| slices.remove(&ptr.addr()))
.expect(
"解放しようとしたポインタはvoicevox_coreの管理下にありません。\
誤ったポインタであるか、二重解放になっていることが考えられます",
);
}
}

Expand Down Expand Up @@ -108,7 +110,7 @@ mod tests {
(ptr.assume_init(), len.assume_init())
};
assert_eq!(expected_len, len);
owner.drop_for(ptr);
owner.drop_for(ptr.as_ptr());
}

fn vec<T: Clone>(initial_cap: usize, elems: &[T]) -> Vec<T> {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.83.0
1.84.0

0 comments on commit fda1e71

Please sign in to comment.