-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`Mmap` will be auto-released out of scope.
- Loading branch information
Huo Linhe
committed
Mar 7, 2016
1 parent
66ad1cd
commit a470b94
Showing
3 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,61 @@ | ||
use std::path::Path; | ||
use std::mem; | ||
use libc::c_void; | ||
use super::UnQlite; | ||
use ffi::{unqlite_util_random_num, unqlite_util_random_string}; | ||
use ffi::{unqlite_util_load_mmaped_file, unqlite_util_random_num, unqlite_util_random_string, | ||
unqlite_util_release_mmaped_file}; | ||
|
||
impl <'util> UnQlite { | ||
impl<'util> UnQlite { | ||
/// Generate random string using the UnQLite PRNG. | ||
/// | ||
/// It will generate a english alphabet based string of length buf_size (last argument). | ||
fn random_string(&self, buf_size: u32) -> Vec<u8> { | ||
pub fn random_string(&self, buf_size: u32) -> Vec<u8> { | ||
unsafe { | ||
let mut vec: Vec<u8> = Vec::with_capacity(buf_size as usize); | ||
error_or!(unqlite_util_random_string(self.db, vec.as_mut_ptr() as *mut i8, buf_size)).unwrap(); | ||
error_or!(unqlite_util_random_string(self.db, vec.as_mut_ptr() as *mut i8, buf_size)) | ||
.unwrap(); | ||
vec | ||
} | ||
} | ||
|
||
/// Generate random number using the UnQLite PRNG. | ||
/// | ||
/// It will return a 32-bit unsigned integer between 0 and 0xFFFFFFFF. | ||
fn random_num(&self) -> u32 { | ||
pub fn random_num(&self) -> u32 { | ||
unsafe { unqlite_util_random_num(self.db) } | ||
} | ||
|
||
/// Memory-mapped file | ||
pub fn load_mmaped_file<P: AsRef<Path>>(path: P) -> ::Result<Mmap> { | ||
unsafe { | ||
let path = path.as_ref(); | ||
let mut ptr: *mut c_void = mem::uninitialized(); | ||
let mut size: i64 = mem::uninitialized(); | ||
error_or!(unqlite_util_load_mmaped_file(path.as_os_str() | ||
.to_cstring() | ||
.unwrap() | ||
.as_ptr(), | ||
&mut ptr, | ||
&mut size)) | ||
.map(|_| { | ||
Mmap { | ||
ptr: ptr, | ||
size: size, | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
pub struct Mmap { | ||
pub ptr: *mut c_void, | ||
pub size: i64, | ||
} | ||
|
||
impl Drop for Mmap { | ||
fn drop(&mut self) { | ||
unsafe { | ||
unqlite_util_random_num(self.db) | ||
error_or!(unqlite_util_release_mmaped_file(self.ptr, self.size)).unwrap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![feature(convert)] | ||
|
||
extern crate unqlite_sys as ffi; | ||
extern crate libc; | ||
|
||
|