Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for mwindow opts #1035

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,111 @@ where
Ok(())
}

/// Get the maximum mmap window size
///
/// # 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;

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
///
/// # 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();

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
///
/// # 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;

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.
///
/// # 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();

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.
///
/// # 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;

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.
///
/// # 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();

try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_MWINDOW_FILE_LIMIT as libc::c_int,
limit
));

Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -241,4 +346,28 @@ mod test {
fn smoke() {
strict_hash_verification(false);
}

#[test]
fn mwindow_size() {
unsafe {
assert!(set_mwindow_size(1024).is_ok());
assert!(get_mwindow_size().unwrap() == 1024);
}
}

#[test]
fn mwindow_mapped_limit() {
unsafe {
assert!(set_mwindow_mapped_limit(1024).is_ok());
assert!(get_mwindow_mapped_limit().unwrap() == 1024);
}
}

#[test]
fn mwindow_file_limit() {
unsafe {
assert!(set_mwindow_file_limit(1024).is_ok());
assert!(get_mwindow_file_limit().unwrap() == 1024);
}
}
}