From 8d18374343cfb63e67e5dfb207aea6545ebb4966 Mon Sep 17 00:00:00 2001 From: Ronan Pelliard Date: Mon, 11 Mar 2024 16:56:25 +0100 Subject: [PATCH 1/3] Add bindings for mwindow opts --- src/opts.rs | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/opts.rs b/src/opts.rs index c709fd0c22..6c023399c9 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -233,6 +233,99 @@ where Ok(()) } +/// Get the maximum mmap window size +pub 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 + )); + } + + Ok(size) +} + +/// Set the maximum mmap window size +pub fn set_mwindow_size(size: usize) -> Result<(), Error> { + crate::init(); + + unsafe { + 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 { + 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 + )); + } + + 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: usize) -> Result<(), Error> { + crate::init(); + + unsafe { + 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 { + 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 + )); + } + + 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: usize) -> Result<(), Error> { + crate::init(); + + unsafe { + 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::*; @@ -241,4 +334,22 @@ mod test { fn smoke() { strict_hash_verification(false); } + + #[test] + fn mwindow_size() { + 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); + } + + #[test] + fn mwindow_file_limit() { + assert!(set_mwindow_file_limit(1024).is_ok()); + assert!(get_mwindow_file_limit().unwrap() == 1024); + } } From 1e6524ec0615ef94a775c83532baf21e4b045b66 Mon Sep 17 00:00:00 2001 From: Ronan Pelliard Date: Wed, 13 Mar 2024 20:34:47 +0100 Subject: [PATCH 2/3] use libc::size_t as param type --- src/opts.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/opts.rs b/src/opts.rs index 6c023399c9..acac9a5083 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -234,7 +234,7 @@ where } /// Get the maximum mmap window size -pub fn get_mwindow_size() -> Result { +pub fn get_mwindow_size() -> Result { crate::init(); let mut size = 0; @@ -250,7 +250,7 @@ pub fn get_mwindow_size() -> Result { } /// Set the maximum mmap window size -pub fn set_mwindow_size(size: usize) -> Result<(), Error> { +pub fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> { crate::init(); unsafe { @@ -264,7 +264,7 @@ pub fn set_mwindow_size(size: usize) -> Result<(), Error> { } /// Get the maximum memory that will be mapped in total by the library -pub fn get_mwindow_mapped_limit() -> Result { +pub fn get_mwindow_mapped_limit() -> Result { crate::init(); let mut limit = 0; @@ -281,7 +281,7 @@ pub fn get_mwindow_mapped_limit() -> Result { /// Set the maximum amount of memory that can be mapped at any time /// by the library. -pub fn set_mwindow_mapped_limit(limit: usize) -> Result<(), Error> { +pub fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> { crate::init(); unsafe { @@ -296,7 +296,7 @@ pub fn set_mwindow_mapped_limit(limit: usize) -> 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 { +pub fn get_mwindow_file_limit() -> Result { crate::init(); let mut limit = 0; @@ -313,7 +313,7 @@ pub fn get_mwindow_file_limit() -> Result { /// 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: usize) -> Result<(), Error> { +pub fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> { crate::init(); unsafe { From 68880b2644f635ae3e73dfd88a56ca66971e463d Mon Sep 17 00:00:00 2001 From: Ronan Pelliard Date: Mon, 18 Mar 2024 13:51:17 +0100 Subject: [PATCH 3/3] make functions unsafe --- src/opts.rs | 114 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 48 deletions(-) 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); + } } }