Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4caac5

Browse files
alexcrichtonSimonSapin
authored andcommittedApr 12, 2018
Update to most recent version of dlmalloc
Inline the definition of `GlobalAlloc` for `dlmalloc` on wasm and don't rely on usage of unstable features in `dlmalloc` itself.
1 parent 5e5a0c2 commit a4caac5

File tree

4 files changed

+20
-46
lines changed

4 files changed

+20
-46
lines changed
 

‎src/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/liballoc_system/lib.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -495,69 +495,45 @@ mod platform {
495495
mod platform {
496496
extern crate dlmalloc;
497497

498-
use core::alloc::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
498+
use core::alloc::{Alloc, AllocErr, Layout};
499499
use System;
500-
use self::dlmalloc::GlobalDlmalloc;
500+
501+
// No need for synchronization here as wasm is currently single-threaded
502+
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
503+
504+
fn to_result(ptr: *mut u8) -> Result<*mut u8, AllocErr> {
505+
if !ptr.is_null() {
506+
Ok(ptr)
507+
} else {
508+
Err(AllocErr::Unsupported { details: "" })
509+
}
510+
}
501511

502512
#[unstable(feature = "allocator_api", issue = "32838")]
503513
unsafe impl<'a> Alloc for &'a System {
504514
#[inline]
505515
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
506-
GlobalDlmalloc.alloc(layout)
516+
to_result(DLMALLOC.malloc(layout.size(), layout.align()))
507517
}
508518

509519
#[inline]
510-
unsafe fn alloc_zeroed(&mut self, layout: Layout)
511-
-> Result<*mut u8, AllocErr>
512-
{
513-
GlobalDlmalloc.alloc_zeroed(layout)
520+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
521+
to_result(DLMALLOC.calloc(layout.size(), layout.align()))
514522
}
515523

516524
#[inline]
517525
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
518-
GlobalDlmalloc.dealloc(ptr, layout)
526+
DLMALLOC.free(ptr, layout.size(), layout.align())
519527
}
520528

521529
#[inline]
522530
unsafe fn realloc(&mut self,
523531
ptr: *mut u8,
524532
old_layout: Layout,
525533
new_layout: Layout) -> Result<*mut u8, AllocErr> {
526-
GlobalDlmalloc.realloc(ptr, old_layout, new_layout)
527-
}
528-
529-
#[inline]
530-
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
531-
GlobalDlmalloc.usable_size(layout)
532-
}
533-
534-
#[inline]
535-
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
536-
GlobalDlmalloc.alloc_excess(layout)
537-
}
538-
539-
#[inline]
540-
unsafe fn realloc_excess(&mut self,
541-
ptr: *mut u8,
542-
layout: Layout,
543-
new_layout: Layout) -> Result<Excess, AllocErr> {
544-
GlobalDlmalloc.realloc_excess(ptr, layout, new_layout)
545-
}
546-
547-
#[inline]
548-
unsafe fn grow_in_place(&mut self,
549-
ptr: *mut u8,
550-
layout: Layout,
551-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
552-
GlobalDlmalloc.grow_in_place(ptr, layout, new_layout)
553-
}
554-
555-
#[inline]
556-
unsafe fn shrink_in_place(&mut self,
557-
ptr: *mut u8,
558-
layout: Layout,
559-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
560-
GlobalDlmalloc.shrink_in_place(ptr, layout, new_layout)
534+
to_result(DLMALLOC.realloc(
535+
ptr, old_layout.size(), old_layout.align(), new_layout.size(),
536+
))
561537
}
562538
}
563539
}

‎src/rustc/dlmalloc_shim/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ doc = false
1212
[dependencies]
1313
core = { path = "../../libcore" }
1414
compiler_builtins = { path = "../../rustc/compiler_builtins_shim" }
15-
alloc = { path = "../../liballoc" }

0 commit comments

Comments
 (0)
Please sign in to comment.