Skip to content

Commit 7163d7a

Browse files
committed
make functions unsafe
1 parent 1e6524e commit 7163d7a

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/opts.rs

+42-12
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ where
234234
}
235235

236236
/// Get the maximum mmap window size
237-
pub fn get_mwindow_size() -> Result<libc::size_t, Error> {
237+
///
238+
/// # Safety
239+
/// This function is reading a C global without synchronization, so it is not
240+
/// thread safe, and should only be called before any thread is spawned.
241+
pub unsafe fn get_mwindow_size() -> Result<libc::size_t, Error> {
238242
crate::init();
239243

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

252256
/// Set the maximum mmap window size
253-
pub fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
257+
///
258+
/// # Safety
259+
/// This function is modifying a C global without synchronization, so it is not
260+
/// thread safe, and should only be called before any thread is spawned.
261+
pub unsafe fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
254262
crate::init();
255263

256264
unsafe {
@@ -264,7 +272,11 @@ pub fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
264272
}
265273

266274
/// Get the maximum memory that will be mapped in total by the library
267-
pub fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {
275+
///
276+
/// # Safety
277+
/// This function is reading a C global without synchronization, so it is not
278+
/// thread safe, and should only be called before any thread is spawned.
279+
pub unsafe fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {
268280
crate::init();
269281

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

282294
/// Set the maximum amount of memory that can be mapped at any time
283295
/// by the library.
284-
pub fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {
296+
///
297+
/// # Safety
298+
/// This function is modifying a C global without synchronization, so it is not
299+
/// thread safe, and should only be called before any thread is spawned.
300+
pub unsafe fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {
285301
crate::init();
286302

287303
unsafe {
@@ -296,7 +312,11 @@ pub fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {
296312

297313
/// Get the maximum number of files that will be mapped at any time by the
298314
/// library.
299-
pub fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {
315+
///
316+
/// # Safety
317+
/// This function is reading a C global without synchronization, so it is not
318+
/// thread safe, and should only be called before any thread is spawned.
319+
pub unsafe fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {
300320
crate::init();
301321

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

314334
/// Set the maximum number of files that can be mapped at any time
315335
/// by the library. The default (0) is unlimited.
316-
pub fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
336+
///
337+
/// # Safety
338+
/// This function is modifying a C global without synchronization, so it is not
339+
/// thread safe, and should only be called before any thread is spawned.
340+
pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
317341
crate::init();
318342

319343
unsafe {
@@ -337,19 +361,25 @@ mod test {
337361

338362
#[test]
339363
fn mwindow_size() {
340-
assert!(set_mwindow_size(1024).is_ok());
341-
assert!(get_mwindow_size().unwrap() == 1024);
364+
unsafe {
365+
assert!(set_mwindow_size(1024).is_ok());
366+
assert!(get_mwindow_size().unwrap() == 1024);
367+
}
342368
}
343369

344370
#[test]
345371
fn mwindow_mapped_limit() {
346-
assert!(set_mwindow_mapped_limit(1024).is_ok());
347-
assert!(get_mwindow_mapped_limit().unwrap() == 1024);
372+
unsafe {
373+
assert!(set_mwindow_mapped_limit(1024).is_ok());
374+
assert!(get_mwindow_mapped_limit().unwrap() == 1024);
375+
}
348376
}
349377

350378
#[test]
351379
fn mwindow_file_limit() {
352-
assert!(set_mwindow_file_limit(1024).is_ok());
353-
assert!(get_mwindow_file_limit().unwrap() == 1024);
380+
unsafe {
381+
assert!(set_mwindow_file_limit(1024).is_ok());
382+
assert!(get_mwindow_file_limit().unwrap() == 1024);
383+
}
354384
}
355385
}

0 commit comments

Comments
 (0)