diff --git a/src/opts.rs b/src/opts.rs index acac9a5083..88f4eb74b7 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -234,94 +234,106 @@ where } /// Get the maximum mmap window size -pub fn get_mwindow_size() -> Result { +/// +/// # Safety +/// This function is reading a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn get_mwindow_size() -> Result { crate::init(); let mut size = 0; - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_GET_MWINDOW_SIZE as libc::c_int, - &mut size - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_GET_MWINDOW_SIZE as libc::c_int, + &mut size + )); Ok(size) } /// Set the maximum mmap window size -pub fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> { +/// +/// # Safety +/// This function is modifying a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> { crate::init(); - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_SET_MWINDOW_SIZE as libc::c_int, - size - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_SET_MWINDOW_SIZE as libc::c_int, + size + )); Ok(()) } /// Get the maximum memory that will be mapped in total by the library -pub fn get_mwindow_mapped_limit() -> Result { +/// +/// # Safety +/// This function is reading a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn get_mwindow_mapped_limit() -> Result { crate::init(); let mut limit = 0; - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_GET_MWINDOW_MAPPED_LIMIT as libc::c_int, - &mut limit - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_GET_MWINDOW_MAPPED_LIMIT as libc::c_int, + &mut limit + )); Ok(limit) } /// Set the maximum amount of memory that can be mapped at any time /// by the library. -pub fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> { +/// +/// # Safety +/// This function is modifying a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> { crate::init(); - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_SET_MWINDOW_MAPPED_LIMIT as libc::c_int, - limit - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_SET_MWINDOW_MAPPED_LIMIT as libc::c_int, + limit + )); Ok(()) } /// Get the maximum number of files that will be mapped at any time by the /// library. -pub fn get_mwindow_file_limit() -> Result { +/// +/// # Safety +/// This function is reading a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn get_mwindow_file_limit() -> Result { crate::init(); let mut limit = 0; - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_GET_MWINDOW_FILE_LIMIT as libc::c_int, - &mut limit - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_GET_MWINDOW_FILE_LIMIT as libc::c_int, + &mut limit + )); Ok(limit) } /// Set the maximum number of files that can be mapped at any time /// by the library. The default (0) is unlimited. -pub fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> { +/// +/// # Safety +/// This function is modifying a C global without synchronization, so it is not +/// thread safe, and should only be called before any thread is spawned. +pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> { crate::init(); - unsafe { - try_call!(raw::git_libgit2_opts( - raw::GIT_OPT_SET_MWINDOW_FILE_LIMIT as libc::c_int, - limit - )); - } + try_call!(raw::git_libgit2_opts( + raw::GIT_OPT_SET_MWINDOW_FILE_LIMIT as libc::c_int, + limit + )); Ok(()) } @@ -337,19 +349,25 @@ mod test { #[test] fn mwindow_size() { - assert!(set_mwindow_size(1024).is_ok()); - assert!(get_mwindow_size().unwrap() == 1024); + unsafe { + assert!(set_mwindow_size(1024).is_ok()); + assert!(get_mwindow_size().unwrap() == 1024); + } } #[test] fn mwindow_mapped_limit() { - assert!(set_mwindow_mapped_limit(1024).is_ok()); - assert!(get_mwindow_mapped_limit().unwrap() == 1024); + unsafe { + assert!(set_mwindow_mapped_limit(1024).is_ok()); + assert!(get_mwindow_mapped_limit().unwrap() == 1024); + } } #[test] fn mwindow_file_limit() { - assert!(set_mwindow_file_limit(1024).is_ok()); - assert!(get_mwindow_file_limit().unwrap() == 1024); + unsafe { + assert!(set_mwindow_file_limit(1024).is_ok()); + assert!(get_mwindow_file_limit().unwrap() == 1024); + } } }