Skip to content

Commit

Permalink
make functions unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
rpelliard committed Mar 18, 2024
1 parent 1e6524e commit 7163d7a
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ where
}

/// Get the maximum mmap window size
pub fn get_mwindow_size() -> Result<libc::size_t, Error> {
///
/// # 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<libc::size_t, Error> {
crate::init();

let mut size = 0;
Expand All @@ -250,7 +254,11 @@ pub fn get_mwindow_size() -> Result<libc::size_t, Error> {
}

/// 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 {
Expand All @@ -264,7 +272,11 @@ pub fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
}

/// Get the maximum memory that will be mapped in total by the library
pub fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {
///
/// # 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<libc::size_t, Error> {
crate::init();

let mut limit = 0;
Expand All @@ -281,7 +293,11 @@ pub fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {

/// 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 {
Expand All @@ -296,7 +312,11 @@ pub fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {

/// Get the maximum number of files that will be mapped at any time by the
/// library.
pub fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {
///
/// # 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<libc::size_t, Error> {
crate::init();

let mut limit = 0;
Expand All @@ -313,7 +333,11 @@ pub fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {

/// 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 {
Expand All @@ -337,19 +361,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);
}
}
}

0 comments on commit 7163d7a

Please sign in to comment.