-
Notifications
You must be signed in to change notification settings - Fork 32
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
c ffi #39
base: master
Are you sure you want to change the base?
c ffi #39
Changes from 1 commit
6eed4dd
8b9eac9
e41b482
5adc9a9
ab9d7b9
ff7d4c9
259fb4e
7355479
160c2b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||
use libc::{c_char,size_t}; | ||||||||||||
use std::ffi::CStr; | ||||||||||||
use std::vec; | ||||||||||||
|
||||||||||||
use super::Trie; | ||||||||||||
use super::trie_common::TrieCommon; | ||||||||||||
|
||||||||||||
ffi_fn! { | ||||||||||||
fn radix_trie_create()-> *const Trie<Vec<u8>, usize>{ | ||||||||||||
let trie = Trie::<Vec<u8>, usize>::new(); | ||||||||||||
return Box::into_raw(Box::new(trie)); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
ffi_fn! { | ||||||||||||
fn radix_trie_free(trie_ptr: *const Trie<Vec<u8>, usize>){ | ||||||||||||
unsafe { Box::from_raw(trie_ptr as *mut Trie<Vec<u8>, usize>); } | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
ffi_fn! { | ||||||||||||
fn radix_trie_insert(trie_ptr:*const Trie<Vec<u8>, usize>, key_ptr:*const c_char, value:usize){ | ||||||||||||
let mut trie = unsafe { &mut *(trie_ptr as *mut Trie<Vec<u8>, usize>) }; | ||||||||||||
let key = unsafe { CStr::from_ptr(key_ptr) }.to_bytes().to_vec(); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could optimise this (avoid a double allocation), by implementing Lines 85 to 89 in c34a167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I did this on purpose to tolerant unexpected string pointer deallocation from foreign languages, especially managed ones whose memory is not being handled explicitly, does that make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree that we want that behaviour, but the current code copies the string twice. The impl TrieKey for CStr {
fn encode_bytes(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
} Maybe the compiler is smart enough to optimise the double-copy away, but it doesn't hurt to give it some help (and |
||||||||||||
trie.insert(key, value); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
ffi_fn! { | ||||||||||||
fn radix_trie_len(trie_ptr:*const Trie<Vec<u8>, usize>)->usize{ | ||||||||||||
let trie = unsafe { &*trie_ptr }; | ||||||||||||
return trie.len(); | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ extern crate quickcheck; | |
#[cfg(test)] | ||
extern crate rand; | ||
|
||
extern crate libc; | ||
|
||
pub use nibble_vec::NibbleVec; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate import (delete this line) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
pub use keys::TrieKey; | ||
pub use trie_common::TrieCommon; | ||
|
@@ -33,6 +35,9 @@ mod test; | |
#[cfg(test)] | ||
mod qc_test; | ||
|
||
mod c_ffi; | ||
pub use c_ffi::*; | ||
|
||
const BRANCH_FACTOR: usize = 16; | ||
|
||
/// Data-structure for storing and querying string-like keys and associated values. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small thing, but it would be good if this was an optional dependency behind a feature flag, see: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What feature name would you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ffi
orc-bindings
orc_bindings
would probably be good