newlib_alloc/
lib.rs

1//! Global allocator for Rust no_std projects on newlib targets.
2
3#![no_std]
4#![warn(missing_docs)]
5
6extern crate core;
7
8use core::alloc::{GlobalAlloc, Layout};
9use libc::{free, memalign};
10
11/// Global allocator for Rust no_std projects on newlib targets.
12pub struct Alloc;
13
14unsafe impl GlobalAlloc for Alloc {
15    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
16        memalign(layout.align(), layout.size()) as *mut _
17    }
18
19    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
20        free(ptr as *mut _);
21    }
22}