1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 extern crate dlmalloc;
13 use alloc::{GlobalAlloc, Layout, System};
15 // FIXME: protect this value for concurrent access
16 static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
18 #[stable(feature = "alloc_system_type", since = "1.28.0")]
19 unsafe impl GlobalAlloc for System {
21 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
22 DLMALLOC.malloc(layout.size(), layout.align())
26 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
27 DLMALLOC.calloc(layout.size(), layout.align())
31 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
32 DLMALLOC.free(ptr, layout.size(), layout.align())
36 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
37 DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size)