c_no_std binding to show use in C freestanding environments. (#238)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-05-17 21:31:11 -04:00
committed by GitHub
parent 5422bde391
commit 67f65c0561
12 changed files with 202 additions and 13 deletions
+7 -2
View File
@@ -5,13 +5,18 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "staticlib"]
[dependencies]
anyhow = "1.0"
regorus = { path = "../.." }
regorus = { path = "../..", default-features = false }
serde_json = "1.0.113"
[features]
default = ["std"]
std = ["regorus/std"]
custom_allocator = []
[build-dependencies]
cbindgen = "0.26.0"
csbindgen = "1.9.0"
-1
View File
@@ -85,4 +85,3 @@ namespace RegorusFFI
}
+1 -4
View File
@@ -137,9 +137,6 @@ bitflags = false
############## Options for How Your Rust library Should Be Parsed ##############
[parse]
@@ -154,5 +151,5 @@ extra_bindings = []
[parse.expand]
crates = []
all_features = false
default_features = true
default_features = false
features = []
+4
View File
@@ -90,6 +90,10 @@ RegorusResult regorus_engine_set_input_from_json_file(RegorusEngine *engine, con
/// * `query`: Rego expression to be evaluate.
RegorusResult regorus_engine_eval_query(RegorusEngine *engine, const char *query);
extern uint8_t *regorus_aligned_alloc(uintptr_t alignment, uintptr_t size);
extern void regorus_free(uint8_t *ptr);
} // extern "C"
#endif // REGORUS_FFI_HPP
+4
View File
@@ -124,4 +124,8 @@ struct RegorusResult regorus_engine_set_input_from_json_file(struct RegorusEngin
*/
struct RegorusResult regorus_engine_eval_query(struct RegorusEngine *engine, const char *query);
extern uint8_t *regorus_aligned_alloc(uintptr_t alignment, uintptr_t size);
extern void regorus_free(uint8_t *ptr);
#endif /* REGORUS_H */
+32 -1
View File
@@ -127,7 +127,6 @@ pub extern "C" fn regorus_engine_drop(engine: *mut RegorusEngine) {
///
/// * `path`: A filename to be associated with the policy.
/// * `rego`: Rego policy.
#[no_mangle]
pub extern "C" fn regorus_engine_add_policy(
engine: *mut RegorusEngine,
@@ -141,6 +140,7 @@ pub extern "C" fn regorus_engine_add_policy(
}())
}
#[cfg(feature = "std")]
#[no_mangle]
pub extern "C" fn regorus_engine_add_policy_from_file(
engine: *mut RegorusEngine,
@@ -169,6 +169,7 @@ pub extern "C" fn regorus_engine_add_data_json(
}())
}
#[cfg(feature = "std")]
#[no_mangle]
pub extern "C" fn regorus_engine_add_data_from_json_file(
engine: *mut RegorusEngine,
@@ -209,6 +210,7 @@ pub extern "C" fn regorus_engine_set_input_json(
}())
}
#[cfg(feature = "std")]
#[no_mangle]
pub extern "C" fn regorus_engine_set_input_from_json_file(
engine: *mut RegorusEngine,
@@ -246,3 +248,32 @@ pub extern "C" fn regorus_engine_eval_query(
Err(e) => to_regorus_result(Err(e)),
}
}
#[cfg(feature = "custom_allocator")]
extern "C" {
fn regorus_aligned_alloc(alignment: usize, size: usize) -> *mut u8;
fn regorus_free(ptr: *mut u8);
}
#[cfg(feature = "custom_allocator")]
mod allocator {
use std::alloc::{GlobalAlloc, Layout};
struct RegorusAllocator {}
unsafe impl GlobalAlloc for RegorusAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let size = layout.size();
let align = layout.align();
crate::regorus_aligned_alloc(align, size)
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
crate::regorus_free(ptr)
}
}
#[global_allocator]
static ALLOCATOR: RegorusAllocator = RegorusAllocator {};
}