-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d627f5
commit af093e9
Showing
6 changed files
with
106 additions
and
79 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 +1,5 @@ | ||
[package] | ||
name = "munchausen" | ||
name = "fiber_ffi" | ||
version = "0.12.0" | ||
authors = [] | ||
|
||
|
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,4 +1,4 @@ | ||
module Munchausen { | ||
header "munchausen.h" | ||
module FiberFFI { | ||
header "fiber_ffi.h" | ||
export * | ||
} |
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,59 +1,73 @@ | ||
use std::ffi; | ||
|
||
type Callback = unsafe extern "C" fn(data: *mut u8, len: usize) -> ffi::c_int; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn test(cb: Callback) { | ||
let mut vec = vec![1, 2, 3, 4]; | ||
vec.shrink_to_fit(); | ||
assert!(vec.len() == vec.capacity()); | ||
macro_rules! multiconst { | ||
($typename:ident, [$($(#[$attr:meta])* $rawname:ident = $value:expr;)*]) => { | ||
$( | ||
$(#[$attr])* | ||
pub const $rawname: $typename = $value; | ||
)* | ||
} | ||
} | ||
|
||
let res = unsafe { cb(vec.as_mut_ptr(), vec.len() as usize) }; | ||
#[allow(non_camel_case_types)] | ||
pub type fx_handle_t = u32; | ||
#[allow(non_camel_case_types)] | ||
pub type fx_status_t = i32; | ||
#[allow(non_camel_case_types)] | ||
pub type fx_obj_type_t = u32; | ||
#[allow(non_camel_case_types)] | ||
pub type fx_rights_t = u32; | ||
|
||
println!("{:?}", cb); | ||
println!("{}", res); | ||
#[repr(C)] | ||
#[allow(non_camel_case_types)] | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
pub struct fx_handle_info_t { | ||
pub handle: fx_handle_t, | ||
pub ty: fx_obj_type_t, | ||
pub rights: fx_rights_t, | ||
pub unused: u32, | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_munchausen_numbers() -> *mut [i32; 4] { | ||
// Pre-caching the power for all of the digits; 0⁰ is initially in the cache array. | ||
let mut cache = [0; 10]; | ||
let mut index = 0; | ||
let mut munchausen_num: [i32; 4] = [0; 4]; | ||
let munchausen_num_ptr: *mut [i32; 4] = &mut munchausen_num; | ||
|
||
for n in 1..=9 { | ||
cache[n] = (n as i32).pow(n as u32); | ||
} | ||
multiconst!(fx_status_t, [ | ||
FX_OK = 0; | ||
]); | ||
|
||
// Searching for Munchausen numbers iterating through a long range containing all of them. | ||
for n in 0..500000000 { | ||
if is_munchausen_number(n, &cache) { | ||
munchausen_num[index] = n; | ||
index += 1; | ||
} | ||
} | ||
multiconst!(fx_obj_type_t, [ | ||
FX_OBJ_TYPE_NONE = 0; | ||
FX_OBJ_TYPE_PROCESS = 1; | ||
FX_OBJ_TYPE_CHANNEL = 4; | ||
FX_OBJ_TYPE_EVENT = 5; | ||
FX_OBJ_TYPE_PORT = 6; | ||
FX_OBJ_TYPE_JOB = 17; | ||
]); | ||
|
||
munchausen_num_ptr | ||
#[no_mangle] | ||
pub extern "C" fn fx_port_create(options: u32, out: *mut fx_handle_t) -> fx_status_t { | ||
FX_OK | ||
} | ||
|
||
fn is_munchausen_number(number: i32, cache: &[i32; 10]) -> bool { | ||
let mut current_number = number; | ||
let mut sum = 0; | ||
|
||
// The calculation details: Do until we go through all of the digits. | ||
while current_number > 0 { | ||
// Take the last digit of a number. | ||
let digit = current_number % 10; | ||
// Add the cached power of the digit to the overall sum. | ||
sum += cache[digit as usize]; | ||
|
||
if sum > number { | ||
return false; | ||
} | ||
// "Cut" the last digit | ||
current_number /= 10; | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn fx_channel_read( | ||
handle: fx_handle_t, | ||
options: u32, | ||
bytes: *mut u8, | ||
handles: *mut fx_handle_t, | ||
num_bytes: u32, | ||
num_handles: u32, | ||
actual_bytes: *mut u32, | ||
actual_handles: *mut u32, | ||
) -> fx_status_t { | ||
FX_OK | ||
} | ||
|
||
number == sum | ||
#[no_mangle] | ||
pub extern "C" fn fx_channel_read_etc( | ||
handle: fx_handle_t, | ||
options: u32, | ||
bytes: *mut u8, | ||
handles: *mut fx_handle_info_t, | ||
num_bytes: u32, | ||
num_handles: u32, | ||
actual_bytes: *mut u32, | ||
actual_handles: *mut u32, | ||
) -> fx_status_t { | ||
FX_OK | ||
} |