summaryrefslogtreecommitdiffstats
path: root/src/system_alloc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/system_alloc.rs')
-rw-r--r--src/system_alloc.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/system_alloc.rs b/src/system_alloc.rs
new file mode 100644
index 0000000..ad1afcb
--- /dev/null
+++ b/src/system_alloc.rs
@@ -0,0 +1,21 @@
+use safe_libc as libc;
+
+struct System;
+
+unsafe impl alloc::alloc::GlobalAlloc for System {
+ unsafe fn alloc(&self, layout: alloc::alloc::Layout) -> *mut u8 {
+ libc::memalign(layout.align(), layout.size()).cast()
+ }
+
+ unsafe fn dealloc(&self, ptr: *mut u8, _layout: alloc::alloc::Layout) {
+ libc::free(ptr.cast());
+ }
+}
+
+#[global_allocator]
+static SYSTEM_ALLOC: System = System;
+
+#[alloc_error_handler]
+fn alloc_error(_: core::alloc::Layout) -> ! {
+ panic!("allocation failure");
+}