Skip to content

Commit

Permalink
Remove the impl Alloc for System
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed May 3, 2019
1 parent 1891bfa commit 07a364e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 42 deletions.
13 changes: 4 additions & 9 deletions src/liballoc/tests/heap.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
use std::alloc::{Global, Alloc, Layout, System};
use std::alloc::{GlobalAlloc, Layout, System};

/// Issue #45955.
#[test]
fn alloc_system_overaligned_request() {
check_overalign_requests(System)
}

#[test]
fn std_heap_overaligned_request() {
check_overalign_requests(Global)
}

fn check_overalign_requests<T: Alloc>(mut allocator: T) {
fn check_overalign_requests<T: GlobalAlloc>(allocator: T) {
let size = 8;
let align = 16; // greater than size
let iterations = 100;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
allocator.alloc(Layout::from_size_align(size, align).unwrap())
}).collect();
for &ptr in &pointers {
assert_eq!((ptr.as_ptr() as usize) % align, 0,
assert_eq!((ptr as usize) % align, 0,
"Got a pointer less aligned than requested")
}

Expand Down
28 changes: 0 additions & 28 deletions src/libstd/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, ptr};
use core::ptr::NonNull;

use crate::sys_common::util::dumb_print;

Expand Down Expand Up @@ -133,33 +132,6 @@ pub use alloc_crate::alloc::*;
#[derive(Debug, Default, Copy, Clone)]
pub struct System;

// The Alloc impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`.
#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl Alloc for System {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
}

#[inline]
unsafe fn realloc(&mut self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}
}

static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());

/// Registers a custom allocation error hook, replacing any that was previously registered.
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/allocator/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

extern crate helper;

use std::alloc::{self, Global, Alloc, System, Layout};
use std::alloc::{Global, Alloc, GlobalAlloc, System, Layout};
use std::sync::atomic::{AtomicUsize, Ordering};

static HITS: AtomicUsize = AtomicUsize::new(0);

struct A;

unsafe impl alloc::GlobalAlloc for A {
unsafe impl GlobalAlloc for A {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
HITS.fetch_add(1, Ordering::SeqCst);
System.alloc(layout)
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
drop(s);
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);

let ptr = System.alloc(layout.clone()).unwrap();
let ptr = System.alloc(layout.clone());
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
helper::work_with(&ptr);
System.dealloc(ptr, layout);
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/allocator/xcrate-use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern crate custom;
extern crate helper;

use std::alloc::{Global, Alloc, System, Layout};
use std::alloc::{Global, Alloc, GlobalAlloc, System, Layout};
use std::sync::atomic::{Ordering, AtomicUsize};

#[global_allocator]
Expand All @@ -26,7 +26,7 @@ fn main() {
Global.dealloc(ptr, layout.clone());
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);

let ptr = System.alloc(layout.clone()).unwrap();
let ptr = System.alloc(layout.clone());
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
helper::work_with(&ptr);
System.dealloc(ptr, layout);
Expand Down

0 comments on commit 07a364e

Please sign in to comment.