Skip to content

Commit 9d7ab41

Browse files
authored
Merge pull request #663 from chewing/refactor-mut-static-reference
refactor(capi): use RwLock to replace mut static reference
2 parents 2a356df + 7bf5652 commit 9d7ab41

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

capi/data/mini.dat

5 Bytes
Binary file not shown.

capi/src/io.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
mem,
66
ptr::{null, null_mut},
77
slice, str,
8-
sync::OnceLock,
8+
sync::RwLock,
99
};
1010

1111
use chewing::{
@@ -46,25 +46,19 @@ enum Owned {
4646
CUShortSlice(usize),
4747
}
4848

49-
static mut OWNED: OnceLock<BTreeMap<*mut c_void, Owned>> = OnceLock::new();
49+
static OWNED: RwLock<BTreeMap<usize, Owned>> = RwLock::new(BTreeMap::new());
5050

5151
fn owned_into_raw<T>(owned: Owned, ptr: *mut T) -> *mut T {
52-
unsafe {
53-
if OWNED.get().is_none() {
54-
let _ = OWNED.set(BTreeMap::new());
55-
}
56-
}
57-
let void_ptr: *mut c_void = ptr.cast();
58-
match unsafe { OWNED.get_mut() } {
59-
Some(map) => {
60-
map.insert(void_ptr, owned);
52+
match OWNED.write() {
53+
Ok(mut map) => {
54+
map.insert(ptr as usize, owned);
6155
ptr
6256
}
63-
None => null_mut(),
57+
Err(_) => null_mut(),
6458
}
6559
}
6660

67-
static mut EMPTY_STRING_BUFFER: [u8; 1] = [0; 1];
61+
static EMPTY_STRING_BUFFER: [u8; 1] = [0; 1];
6862

6963
fn copy_cstr(buf: &mut [u8], buffer: &str) -> *const c_char {
7064
let n = min(buf.len(), buffer.len());
@@ -73,8 +67,8 @@ fn copy_cstr(buf: &mut [u8], buffer: &str) -> *const c_char {
7367
buf.as_ptr().cast()
7468
}
7569

76-
fn global_empty_cstr() -> *mut c_char {
77-
unsafe { EMPTY_STRING_BUFFER.as_mut_ptr().cast() }
70+
fn global_empty_cstr() -> *const c_char {
71+
EMPTY_STRING_BUFFER.as_ptr().cast()
7872
}
7973

8074
unsafe fn slice_from_ptr_with_nul<'a>(ptr: *const c_char) -> Option<&'a [c_char]> {
@@ -110,11 +104,6 @@ pub unsafe extern "C" fn chewing_new2(
110104
logger: Option<unsafe extern "C" fn(data: *mut c_void, level: c_int, fmt: *const c_char, ...)>,
111105
loggerdata: *mut c_void,
112106
) -> *mut ChewingContext {
113-
unsafe {
114-
if OWNED.get().is_none() {
115-
let _ = OWNED.set(BTreeMap::new());
116-
}
117-
}
118107
LOGGER.init();
119108
let _ = log::set_logger(&LOGGER);
120109
log::set_max_level(log::LevelFilter::Trace);
@@ -220,11 +209,6 @@ pub unsafe extern "C" fn chewing_new2(
220209
#[no_mangle]
221210
pub unsafe extern "C" fn chewing_delete(ctx: *mut ChewingContext) {
222211
if !ctx.is_null() {
223-
unsafe {
224-
if OWNED.get().is_none() {
225-
let _ = OWNED.take();
226-
}
227-
}
228212
LOGGER.set(None);
229213
info!("Destroying context {ctx:?}");
230214
drop(unsafe { Box::from_raw(ctx) })
@@ -237,8 +221,8 @@ pub unsafe extern "C" fn chewing_delete(ctx: *mut ChewingContext) {
237221
#[no_mangle]
238222
pub unsafe extern "C" fn chewing_free(ptr: *mut c_void) {
239223
if !ptr.is_null() {
240-
if let Some(map) = unsafe { OWNED.get() } {
241-
if let Some(owned) = map.get(&ptr) {
224+
if let Ok(map) = OWNED.write() {
225+
if let Some(owned) = map.get(&(ptr as usize)) {
242226
match owned {
243227
Owned::CString => drop(unsafe { CString::from_raw(ptr.cast()) }),
244228
Owned::CUShortSlice(len) => {
@@ -437,7 +421,7 @@ pub unsafe extern "C" fn chewing_config_set_int(
437421
options.esc_clear_all_buffer = value > 0;
438422
}
439423
"chewing.auto_commit_threshold" => {
440-
if value < 0 || value > 39 {
424+
if !(0..=39).contains(&value) {
441425
return ERROR;
442426
}
443427
options.auto_commit_threshold = value as usize;

0 commit comments

Comments
 (0)