]> git.proxmox.com Git - rustc.git/blob - src/dlmalloc/src/lib.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / dlmalloc / src / lib.rs
1 #![feature(allocator_api, alloc)]
2 #![cfg_attr(target_arch = "wasm32", feature(link_llvm_intrinsics))]
3 #![no_std]
4
5 extern crate alloc;
6
7 use alloc::heap::{Alloc, Layout, AllocErr};
8 use core::cmp;
9 use core::ptr;
10
11 pub use self::global::GlobalDlmalloc;
12
13 mod global;
14 mod dlmalloc;
15
16 pub struct Dlmalloc(dlmalloc::Dlmalloc);
17
18 #[cfg(target_arch = "wasm32")]
19 #[path = "wasm.rs"]
20 mod sys;
21
22 #[cfg(target_os = "macos")]
23 #[path = "macos.rs"]
24 mod sys;
25
26 #[cfg(target_os = "linux")]
27 #[path = "linux.rs"]
28 mod sys;
29
30 impl Dlmalloc {
31 pub fn new() -> Dlmalloc {
32 Dlmalloc(dlmalloc::Dlmalloc::new())
33 }
34 }
35
36 unsafe impl Alloc for Dlmalloc {
37 #[inline]
38 unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
39 let ptr = if layout.align() <= self.0.malloc_alignment() {
40 self.0.malloc(layout.size())
41 } else {
42 self.0.memalign(layout.align(), layout.size())
43 };
44 if ptr.is_null() {
45 Err(AllocErr::Exhausted { request: layout })
46 } else {
47 Ok(ptr)
48 }
49 }
50
51 #[inline]
52 unsafe fn alloc_zeroed(&mut self, layout: Layout)
53 -> Result<*mut u8, AllocErr>
54 {
55 let size = layout.size();
56 let ptr = self.alloc(layout)?;
57 if self.0.calloc_must_clear(ptr) {
58 ptr::write_bytes(ptr, 0, size);
59 }
60 Ok(ptr)
61 }
62
63 #[inline]
64 unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
65 drop(layout);
66 self.0.free(ptr)
67 }
68
69 #[inline]
70 unsafe fn realloc(&mut self,
71 ptr: *mut u8,
72 old_layout: Layout,
73 new_layout: Layout) -> Result<*mut u8, AllocErr> {
74 if old_layout.align() != new_layout.align() {
75 return Err(AllocErr::Unsupported {
76 details: "cannot change alignment on `realloc`",
77 })
78 }
79
80 if new_layout.align() <= self.0.malloc_alignment() {
81 let ptr = self.0.realloc(ptr, new_layout.size());
82 if !ptr.is_null() {
83 Ok(ptr as *mut u8)
84 } else {
85 Err(AllocErr::Exhausted { request: new_layout })
86 }
87 } else {
88 let res = self.alloc(new_layout.clone());
89 if let Ok(new_ptr) = res {
90 let size = cmp::min(old_layout.size(), new_layout.size());
91 ptr::copy_nonoverlapping(ptr, new_ptr, size);
92 self.dealloc(ptr, old_layout);
93 }
94 res
95 }
96 }
97
98 // fn oom(&mut self, err: AllocErr) -> ! {
99 // System.oom(err)
100 // }
101
102 // #[inline]
103 // fn usable_size(&self, layout: &Layout) -> (usize, usize) {
104 // (&self).usable_size(layout)
105 // }
106 //
107 // #[inline]
108 // unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
109 // (&*self).alloc_excess(layout)
110 // }
111 //
112 // #[inline]
113 // unsafe fn realloc_excess(&mut self,
114 // ptr: *mut u8,
115 // layout: Layout,
116 // new_layout: Layout) -> Result<Excess, AllocErr> {
117 // (&*self).realloc_excess(ptr, layout, new_layout)
118 // }
119 //
120 // #[inline]
121 // unsafe fn grow_in_place(&mut self,
122 // ptr: *mut u8,
123 // layout: Layout,
124 // new_layout: Layout) -> Result<(), CannotReallocInPlace> {
125 // (&*self).grow_in_place(ptr, layout, new_layout)
126 // }
127 //
128 // #[inline]
129 // unsafe fn shrink_in_place(&mut self,
130 // ptr: *mut u8,
131 // layout: Layout,
132 // new_layout: Layout) -> Result<(), CannotReallocInPlace> {
133 // (&*self).shrink_in_place(ptr, layout, new_layout)
134 // }
135 }
136
137 // unsafe impl<'a> Alloc for &'a Dlmalloc {
138 // #[inline]
139 // unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
140 // panic!()
141 // }
142 //
143 // // #[inline]
144 // // unsafe fn alloc_zeroed(&mut self, layout: Layout)
145 // // -> Result<*mut u8, AllocErr>
146 // // {
147 // // panic!()
148 // // }
149 //
150 // #[inline]
151 // unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
152 // panic!()
153 // }
154 //
155 // // #[inline]
156 // // unsafe fn realloc(&mut self,
157 // // ptr: *mut u8,
158 // // old_layout: Layout,
159 // // new_layout: Layout) -> Result<*mut u8, AllocErr> {
160 // // panic!()
161 // // }
162 //
163 // fn oom(&mut self, err: AllocErr) -> ! {
164 // System.oom(err)
165 // }
166 // }