Skip to content

Commit ba7081a

Browse files
committed
Make AllocErr a zero-size unit struct
1 parent a4caac5 commit ba7081a

File tree

7 files changed

+51
-135
lines changed

7 files changed

+51
-135
lines changed

src/liballoc/alloc.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
issue = "32838")]
1717

1818
use core::intrinsics::{min_align_of_val, size_of_val};
19-
use core::mem::{self, ManuallyDrop};
19+
use core::mem;
2020
use core::usize;
2121

2222
#[doc(inline)]
@@ -86,12 +86,12 @@ pub const Heap: Global = Global;
8686
unsafe impl Alloc for Global {
8787
#[inline]
8888
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
89-
let mut err = ManuallyDrop::new(mem::uninitialized::<AllocErr>());
89+
let mut err = AllocErr;
9090
let ptr = __rust_alloc(layout.size(),
9191
layout.align(),
92-
&mut *err as *mut AllocErr as *mut u8);
92+
&mut err as *mut AllocErr as *mut u8);
9393
if ptr.is_null() {
94-
Err(ManuallyDrop::into_inner(err))
94+
Err(AllocErr)
9595
} else {
9696
Ok(ptr)
9797
}
@@ -129,15 +129,15 @@ unsafe impl Alloc for Global {
129129
new_layout: Layout)
130130
-> Result<*mut u8, AllocErr>
131131
{
132-
let mut err = ManuallyDrop::new(mem::uninitialized::<AllocErr>());
132+
let mut err = AllocErr;
133133
let ptr = __rust_realloc(ptr,
134134
layout.size(),
135135
layout.align(),
136136
new_layout.size(),
137137
new_layout.align(),
138-
&mut *err as *mut AllocErr as *mut u8);
138+
&mut err as *mut AllocErr as *mut u8);
139139
if ptr.is_null() {
140-
Err(ManuallyDrop::into_inner(err))
140+
Err(AllocErr)
141141
} else {
142142
mem::forget(err);
143143
Ok(ptr)
@@ -146,27 +146,27 @@ unsafe impl Alloc for Global {
146146

147147
#[inline]
148148
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
149-
let mut err = ManuallyDrop::new(mem::uninitialized::<AllocErr>());
149+
let mut err = AllocErr;
150150
let ptr = __rust_alloc_zeroed(layout.size(),
151151
layout.align(),
152-
&mut *err as *mut AllocErr as *mut u8);
152+
&mut err as *mut AllocErr as *mut u8);
153153
if ptr.is_null() {
154-
Err(ManuallyDrop::into_inner(err))
154+
Err(AllocErr)
155155
} else {
156156
Ok(ptr)
157157
}
158158
}
159159

160160
#[inline]
161161
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
162-
let mut err = ManuallyDrop::new(mem::uninitialized::<AllocErr>());
162+
let mut err = AllocErr;
163163
let mut size = 0;
164164
let ptr = __rust_alloc_excess(layout.size(),
165165
layout.align(),
166166
&mut size,
167-
&mut *err as *mut AllocErr as *mut u8);
167+
&mut err as *mut AllocErr as *mut u8);
168168
if ptr.is_null() {
169-
Err(ManuallyDrop::into_inner(err))
169+
Err(AllocErr)
170170
} else {
171171
Ok(Excess(ptr, size))
172172
}
@@ -177,17 +177,17 @@ unsafe impl Alloc for Global {
177177
ptr: *mut u8,
178178
layout: Layout,
179179
new_layout: Layout) -> Result<Excess, AllocErr> {
180-
let mut err = ManuallyDrop::new(mem::uninitialized::<AllocErr>());
180+
let mut err = AllocErr;
181181
let mut size = 0;
182182
let ptr = __rust_realloc_excess(ptr,
183183
layout.size(),
184184
layout.align(),
185185
new_layout.size(),
186186
new_layout.align(),
187187
&mut size,
188-
&mut *err as *mut AllocErr as *mut u8);
188+
&mut err as *mut AllocErr as *mut u8);
189189
if ptr.is_null() {
190-
Err(ManuallyDrop::into_inner(err))
190+
Err(AllocErr)
191191
} else {
192192
Ok(Excess(ptr, size))
193193
}

src/liballoc/raw_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ mod tests {
760760
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
761761
let size = layout.size();
762762
if size > self.fuel {
763-
return Err(AllocErr::Unsupported { details: "fuel exhausted" });
763+
return Err(AllocErr);
764764
}
765765
match Global.alloc(layout) {
766766
ok @ Ok(_) => { self.fuel -= size; ok }

src/liballoc_jemalloc/lib.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ extern crate libc;
3030
pub use contents::*;
3131
#[cfg(not(dummy_jemalloc))]
3232
mod contents {
33-
use core::ptr;
34-
3533
use core::alloc::{Alloc, AllocErr, Layout};
3634
use alloc_system::System;
3735
use libc::{c_int, c_void, size_t};
@@ -106,14 +104,9 @@ mod contents {
106104
#[rustc_std_internal_symbol]
107105
pub unsafe extern fn __rde_alloc(size: usize,
108106
align: usize,
109-
err: *mut u8) -> *mut u8 {
107+
_err: *mut u8) -> *mut u8 {
110108
let flags = align_to_flags(align, size);
111109
let ptr = mallocx(size as size_t, flags) as *mut u8;
112-
if ptr.is_null() {
113-
let layout = Layout::from_size_align_unchecked(size, align);
114-
ptr::write(err as *mut AllocErr,
115-
AllocErr::Exhausted { request: layout });
116-
}
117110
ptr
118111
}
119112

@@ -155,39 +148,27 @@ mod contents {
155148
old_align: usize,
156149
new_size: usize,
157150
new_align: usize,
158-
err: *mut u8) -> *mut u8 {
151+
_err: *mut u8) -> *mut u8 {
159152
if new_align != old_align {
160-
ptr::write(err as *mut AllocErr,
161-
AllocErr::Unsupported { details: "can't change alignments" });
162153
return 0 as *mut u8
163154
}
164155

165156
let flags = align_to_flags(new_align, new_size);
166157
let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8;
167-
if ptr.is_null() {
168-
let layout = Layout::from_size_align_unchecked(new_size, new_align);
169-
ptr::write(err as *mut AllocErr,
170-
AllocErr::Exhausted { request: layout });
171-
}
172158
ptr
173159
}
174160

175161
#[no_mangle]
176162
#[rustc_std_internal_symbol]
177163
pub unsafe extern fn __rde_alloc_zeroed(size: usize,
178164
align: usize,
179-
err: *mut u8) -> *mut u8 {
165+
_err: *mut u8) -> *mut u8 {
180166
let ptr = if align <= MIN_ALIGN && align <= size {
181167
calloc(size as size_t, 1) as *mut u8
182168
} else {
183169
let flags = align_to_flags(align, size) | MALLOCX_ZERO;
184170
mallocx(size as size_t, flags) as *mut u8
185171
};
186-
if ptr.is_null() {
187-
let layout = Layout::from_size_align_unchecked(size, align);
188-
ptr::write(err as *mut AllocErr,
189-
AllocErr::Exhausted { request: layout });
190-
}
191172
ptr
192173
}
193174

src/liballoc_system/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,15 @@ mod platform {
133133
#[cfg(target_os = "macos")]
134134
{
135135
if layout.align() > (1 << 31) {
136-
return Err(AllocErr::Unsupported {
137-
details: "requested alignment too large"
138-
})
136+
return Err(AllocErr)
139137
}
140138
}
141139
aligned_malloc(&layout)
142140
};
143141
if !ptr.is_null() {
144142
Ok(ptr)
145143
} else {
146-
Err(AllocErr::Exhausted { request: layout })
144+
Err(AllocErr)
147145
}
148146
}
149147

@@ -156,7 +154,7 @@ mod platform {
156154
if !ptr.is_null() {
157155
Ok(ptr)
158156
} else {
159-
Err(AllocErr::Exhausted { request: layout })
157+
Err(AllocErr)
160158
}
161159
} else {
162160
let ret = self.alloc(layout.clone());
@@ -178,17 +176,15 @@ mod platform {
178176
old_layout: Layout,
179177
new_layout: Layout) -> Result<*mut u8, AllocErr> {
180178
if old_layout.align() != new_layout.align() {
181-
return Err(AllocErr::Unsupported {
182-
details: "cannot change alignment on `realloc`",
183-
})
179+
return Err(AllocErr)
184180
}
185181

186182
if new_layout.align() <= MIN_ALIGN && new_layout.align() <= new_layout.size(){
187183
let ptr = libc::realloc(ptr as *mut libc::c_void, new_layout.size());
188184
if !ptr.is_null() {
189185
Ok(ptr as *mut u8)
190186
} else {
191-
Err(AllocErr::Exhausted { request: new_layout })
187+
Err(AllocErr)
192188
}
193189
} else {
194190
let res = self.alloc(new_layout.clone());
@@ -342,7 +338,7 @@ mod platform {
342338
}
343339
};
344340
if ptr.is_null() {
345-
Err(AllocErr::Exhausted { request: layout })
341+
Err(AllocErr)
346342
} else {
347343
Ok(ptr as *mut u8)
348344
}
@@ -382,9 +378,7 @@ mod platform {
382378
old_layout: Layout,
383379
new_layout: Layout) -> Result<*mut u8, AllocErr> {
384380
if old_layout.align() != new_layout.align() {
385-
return Err(AllocErr::Unsupported {
386-
details: "cannot change alignment on `realloc`",
387-
})
381+
return Err(AllocErr)
388382
}
389383

390384
if new_layout.align() <= MIN_ALIGN {
@@ -395,7 +389,7 @@ mod platform {
395389
if !ptr.is_null() {
396390
Ok(ptr as *mut u8)
397391
} else {
398-
Err(AllocErr::Exhausted { request: new_layout })
392+
Err(AllocErr)
399393
}
400394
} else {
401395
let res = self.alloc(new_layout.clone());
@@ -505,7 +499,7 @@ mod platform {
505499
if !ptr.is_null() {
506500
Ok(ptr)
507501
} else {
508-
Err(AllocErr::Unsupported { details: "" })
502+
Err(AllocErr)
509503
}
510504
}
511505

src/libcore/alloc.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -320,50 +320,12 @@ impl Layout {
320320
/// something wrong when combining the given input arguments with this
321321
/// allocator.
322322
#[derive(Clone, PartialEq, Eq, Debug)]
323-
pub enum AllocErr {
324-
/// Error due to hitting some resource limit or otherwise running
325-
/// out of memory. This condition strongly implies that *some*
326-
/// series of deallocations would allow a subsequent reissuing of
327-
/// the original allocation request to succeed.
328-
Exhausted { request: Layout },
329-
330-
/// Error due to allocator being fundamentally incapable of
331-
/// satisfying the original request. This condition implies that
332-
/// such an allocation request will never succeed on the given
333-
/// allocator, regardless of environment, memory pressure, or
334-
/// other contextual conditions.
335-
///
336-
/// For example, an allocator that does not support requests for
337-
/// large memory blocks might return this error variant.
338-
Unsupported { details: &'static str },
339-
}
340-
341-
impl AllocErr {
342-
#[inline]
343-
pub fn invalid_input(details: &'static str) -> Self {
344-
AllocErr::Unsupported { details: details }
345-
}
346-
#[inline]
347-
pub fn is_memory_exhausted(&self) -> bool {
348-
if let AllocErr::Exhausted { .. } = *self { true } else { false }
349-
}
350-
#[inline]
351-
pub fn is_request_unsupported(&self) -> bool {
352-
if let AllocErr::Unsupported { .. } = *self { true } else { false }
353-
}
354-
#[inline]
355-
pub fn description(&self) -> &str {
356-
match *self {
357-
AllocErr::Exhausted { .. } => "allocator memory exhausted",
358-
AllocErr::Unsupported { .. } => "unsupported allocator request",
359-
}
360-
}
361-
}
323+
pub struct AllocErr;
362324

363325
// (we need this for downstream impl of trait Error)
364326
impl fmt::Display for AllocErr {
365327
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
366-
write!(f, "{}", self.description())
328+
f.write_str("memory allocation failed")
367329
}
368330
}
369331

@@ -592,12 +554,8 @@ pub unsafe trait Alloc {
592554
/// aborting.
593555
///
594556
/// `oom` is meant to be used by clients unable to cope with an
595-
/// unsatisfied allocation request (signaled by an error such as
596-
/// `AllocErr::Exhausted`), and wish to abandon computation rather
597-
/// than attempt to recover locally. Such clients should pass the
598-
/// signaling error value back into `oom`, where the allocator
599-
/// may incorporate that error value into its diagnostic report
600-
/// before aborting.
557+
/// unsatisfied allocation request, and wish to abandon
558+
/// computation rather than attempt to recover locally.
601559
///
602560
/// Implementations of the `oom` method are discouraged from
603561
/// infinitely regressing in nested calls to `oom`. In
@@ -963,7 +921,7 @@ pub unsafe trait Alloc {
963921
if k.size() > 0 {
964922
unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
965923
} else {
966-
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
924+
Err(AllocErr)
967925
}
968926
}
969927

@@ -1036,7 +994,7 @@ pub unsafe trait Alloc {
1036994
})
1037995
}
1038996
}
1039-
_ => Err(AllocErr::invalid_input("invalid layout for alloc_array")),
997+
_ => Err(AllocErr),
1040998
}
1041999
}
10421000

@@ -1084,7 +1042,7 @@ pub unsafe trait Alloc {
10841042
.map(|p| NonNull::new_unchecked(p as *mut T))
10851043
}
10861044
_ => {
1087-
Err(AllocErr::invalid_input("invalid layout for realloc_array"))
1045+
Err(AllocErr)
10881046
}
10891047
}
10901048
}
@@ -1118,7 +1076,7 @@ pub unsafe trait Alloc {
11181076
Ok(self.dealloc(raw_ptr, k.clone()))
11191077
}
11201078
_ => {
1121-
Err(AllocErr::invalid_input("invalid layout for dealloc_array"))
1079+
Err(AllocErr)
11221080
}
11231081
}
11241082
}

0 commit comments

Comments
 (0)