Skip to content

Commit 4308159

Browse files
committed
Run rustfmt
1 parent c3660c9 commit 4308159

File tree

8 files changed

+199
-245
lines changed

8 files changed

+199
-245
lines changed

src/dlmalloc.rs

Lines changed: 144 additions & 150 deletions
Large diffs are not rendered by default.

src/global.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use core::alloc::{Layout, GlobalAlloc};
1+
#[cfg(feature = "allocator-api")]
2+
use core::alloc::{Alloc, AllocErr};
3+
use core::alloc::{GlobalAlloc, Layout};
24
use core::ops::{Deref, DerefMut};
35
#[cfg(feature = "allocator-api")]
46
use core::ptr::NonNull;
5-
#[cfg(feature = "allocator-api")]
6-
use core::alloc::{AllocErr, Alloc};
77

88
use Dlmalloc;
99

@@ -30,29 +30,15 @@ unsafe impl GlobalAlloc for GlobalDlmalloc {
3030
}
3131

3232
#[inline]
33-
unsafe fn realloc(
34-
&self,
35-
ptr: *mut u8,
36-
layout: Layout,
37-
new_size: usize
38-
) -> *mut u8 {
39-
<Dlmalloc>::realloc(
40-
&mut get(),
41-
ptr,
42-
layout.size(),
43-
layout.align(),
44-
new_size,
45-
)
33+
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
34+
<Dlmalloc>::realloc(&mut get(), ptr, layout.size(), layout.align(), new_size)
4635
}
4736
}
4837

4938
#[cfg(feature = "allocator-api")]
5039
unsafe impl Alloc for GlobalDlmalloc {
5140
#[inline]
52-
unsafe fn alloc(
53-
&mut self,
54-
layout: Layout
55-
) -> Result<NonNull<u8>, AllocErr> {
41+
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
5642
get().alloc(layout)
5743
}
5844

@@ -66,16 +52,13 @@ unsafe impl Alloc for GlobalDlmalloc {
6652
&mut self,
6753
ptr: NonNull<u8>,
6854
layout: Layout,
69-
new_size: usize
55+
new_size: usize,
7056
) -> Result<NonNull<u8>, AllocErr> {
7157
Alloc::realloc(&mut *get(), ptr, layout, new_size)
7258
}
7359

7460
#[inline]
75-
unsafe fn alloc_zeroed(
76-
&mut self,
77-
layout: Layout
78-
) -> Result<NonNull<u8>, AllocErr> {
61+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
7962
get().alloc_zeroed(layout)
8063
}
8164
}

src/lib.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
#![deny(missing_docs)]
1919

2020
#[cfg(feature = "allocator-api")]
21-
use core::alloc::{Alloc, Layout, AllocErr};
21+
use core::alloc::{Alloc, AllocErr, Layout};
2222
use core::cmp;
2323
use core::ptr;
2424

2525
#[cfg(all(feature = "global", not(test)))]
2626
pub use self::global::GlobalDlmalloc;
2727

28+
mod dlmalloc;
2829
#[cfg(all(feature = "global", not(test)))]
2930
mod global;
30-
mod dlmalloc;
3131

3232
/// An allocator instance
3333
///
@@ -109,11 +109,13 @@ impl Dlmalloc {
109109
/// Safety and contracts are largely governed by the `GlobalAlloc::realloc`
110110
/// method contracts.
111111
#[inline]
112-
pub unsafe fn realloc(&mut self,
113-
ptr: *mut u8,
114-
old_size: usize,
115-
old_align: usize,
116-
new_size: usize) -> *mut u8 {
112+
pub unsafe fn realloc(
113+
&mut self,
114+
ptr: *mut u8,
115+
old_size: usize,
116+
old_align: usize,
117+
new_size: usize,
118+
) -> *mut u8 {
117119
if old_align <= self.0.malloc_alignment() {
118120
self.0.realloc(ptr, new_size)
119121
} else {
@@ -131,10 +133,7 @@ impl Dlmalloc {
131133
#[cfg(feature = "allocator-api")]
132134
unsafe impl Alloc for Dlmalloc {
133135
#[inline]
134-
unsafe fn alloc(
135-
&mut self,
136-
layout: Layout
137-
) -> Result<ptr::NonNull<u8>, AllocErr> {
136+
unsafe fn alloc(&mut self, layout: Layout) -> Result<ptr::NonNull<u8>, AllocErr> {
138137
let ptr = <Dlmalloc>::malloc(self, layout.size(), layout.align());
139138
ptr::NonNull::new(ptr).ok_or(AllocErr)
140139
}
@@ -149,23 +148,14 @@ unsafe impl Alloc for Dlmalloc {
149148
&mut self,
150149
ptr: ptr::NonNull<u8>,
151150
layout: Layout,
152-
new_size: usize
151+
new_size: usize,
153152
) -> Result<ptr::NonNull<u8>, AllocErr> {
154-
let ptr = <Dlmalloc>::realloc(
155-
self,
156-
ptr.as_ptr(),
157-
layout.size(),
158-
layout.align(),
159-
new_size,
160-
);
153+
let ptr = <Dlmalloc>::realloc(self, ptr.as_ptr(), layout.size(), layout.align(), new_size);
161154
ptr::NonNull::new(ptr).ok_or(AllocErr)
162155
}
163156

164157
#[inline]
165-
unsafe fn alloc_zeroed(
166-
&mut self,
167-
layout: Layout
168-
) -> Result<ptr::NonNull<u8>, AllocErr> {
158+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<ptr::NonNull<u8>, AllocErr> {
169159
let ptr = <Dlmalloc>::calloc(self, layout.size(), layout.align());
170160
ptr::NonNull::new(ptr).ok_or(AllocErr)
171161
}

src/linux.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ extern crate libc;
33
use core::ptr;
44

55
pub unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) {
6-
let addr = libc::mmap(0 as *mut _,
7-
size,
8-
libc::PROT_WRITE | libc::PROT_READ,
9-
libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
10-
-1,
11-
0);
6+
let addr = libc::mmap(
7+
0 as *mut _,
8+
size,
9+
libc::PROT_WRITE | libc::PROT_READ,
10+
libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
11+
-1,
12+
0,
13+
);
1214
if addr == libc::MAP_FAILED {
1315
(ptr::null_mut(), 0, 0)
1416
} else {
1517
(addr as *mut u8, size, 0)
1618
}
1719
}
1820

19-
pub unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool)
20-
-> *mut u8
21-
{
21+
pub unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 {
2222
let flags = if can_move { libc::MREMAP_MAYMOVE } else { 0 };
2323
let ptr = libc::mremap(ptr as *mut _, oldsize, newsize, flags);
2424
if ptr == libc::MAP_FAILED {
@@ -31,7 +31,7 @@ pub unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool
3131
pub unsafe fn free_part(ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
3232
let rc = libc::mremap(ptr as *mut _, oldsize, newsize, 0);
3333
if rc != libc::MAP_FAILED {
34-
return true
34+
return true;
3535
}
3636
libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0
3737
}
@@ -49,16 +49,12 @@ static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
4949

5050
#[cfg(feature = "global")]
5151
pub fn acquire_global_lock() {
52-
unsafe {
53-
assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0)
54-
}
52+
unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) }
5553
}
5654

5755
#[cfg(feature = "global")]
5856
pub fn release_global_lock() {
59-
unsafe {
60-
assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0)
61-
}
57+
unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) }
6258
}
6359

6460
pub fn allocates_zeros() -> bool {

src/macos.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ extern crate libc;
33
use core::ptr;
44

55
pub unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) {
6-
let addr = libc::mmap(0 as *mut _,
7-
size,
8-
libc::PROT_WRITE | libc::PROT_READ,
9-
libc::MAP_ANON | libc::MAP_PRIVATE,
10-
-1,
11-
0);
6+
let addr = libc::mmap(
7+
0 as *mut _,
8+
size,
9+
libc::PROT_WRITE | libc::PROT_READ,
10+
libc::MAP_ANON | libc::MAP_PRIVATE,
11+
-1,
12+
0,
13+
);
1214
if addr == libc::MAP_FAILED {
1315
(ptr::null_mut(), 0, 0)
1416
} else {
1517
(addr as *mut u8, size, 0)
1618
}
1719
}
1820

19-
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool)
20-
-> *mut u8
21-
{
21+
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
2222
ptr::null_mut()
2323
}
2424

@@ -39,16 +39,12 @@ static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
3939

4040
#[cfg(feature = "global")]
4141
pub fn acquire_global_lock() {
42-
unsafe {
43-
assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0)
44-
}
42+
unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) }
4543
}
4644

4745
#[cfg(feature = "global")]
4846
pub fn release_global_lock() {
49-
unsafe {
50-
assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0)
51-
}
47+
unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) }
5248
}
5349

5450
pub fn allocates_zeros() -> bool {

src/sgx.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use core::ptr;
2-
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
2+
use core::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
33

44
// Do not remove inline: will result in relocation failure
55
#[inline(always)]
66
unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
7-
(image_base()+offset) as *mut T
7+
(image_base() + offset) as *mut T
88
}
99

1010
// Do not remove inline: will result in relocation failure
@@ -13,12 +13,12 @@ unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
1313
#[inline(always)]
1414
fn image_base() -> u64 {
1515
let base;
16-
unsafe{asm!("lea IMAGE_BASE(%rip),$0":"=r"(base))};
16+
unsafe { asm!("lea IMAGE_BASE(%rip),$0":"=r"(base)) };
1717
base
1818
}
1919

2020
pub unsafe fn alloc(_size: usize) -> (*mut u8, usize, u32) {
21-
extern {
21+
extern "C" {
2222
static HEAP_BASE: u64;
2323
static HEAP_SIZE: usize;
2424
}
@@ -31,9 +31,7 @@ pub unsafe fn alloc(_size: usize) -> (*mut u8, usize, u32) {
3131
}
3232
}
3333

34-
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool)
35-
-> *mut u8
36-
{
34+
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
3735
ptr::null_mut()
3836
}
3937

src/wasm.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use core::ptr;
21
use core::arch::wasm32;
2+
use core::ptr;
33

44
pub unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) {
55
let pages = size / page_size();
@@ -10,9 +10,7 @@ pub unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) {
1010
((prev * page_size()) as *mut u8, pages * page_size(), 0)
1111
}
1212

13-
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool)
14-
-> *mut u8
15-
{
13+
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
1614
// TODO: I think this can be implemented near the end?
1715
ptr::null_mut()
1816
}

tests/smoke.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
extern crate dlmalloc;
22
extern crate rand;
33

4-
use std::cmp;
54
use dlmalloc::Dlmalloc;
65
use rand::Rng;
6+
use std::cmp;
77

88
#[test]
99
fn smoke() {
@@ -32,13 +32,12 @@ fn stress() {
3232
unsafe {
3333
for _ in 0..max {
3434
let free =
35-
ptrs.len() > 0 &&
36-
((ptrs.len() < 10_000 && rng.gen_weighted_bool(3)) || rng.gen());
35+
ptrs.len() > 0 && ((ptrs.len() < 10_000 && rng.gen_weighted_bool(3)) || rng.gen());
3736
if free {
3837
let idx = rng.gen_range(0, ptrs.len());
3938
let (ptr, size, align) = ptrs.swap_remove(idx);
4039
a.free(ptr, size, align);
41-
continue
40+
continue;
4241
}
4342

4443
if ptrs.len() > 0 && rng.gen_weighted_bool(100) {
@@ -49,7 +48,7 @@ fn stress() {
4948
} else if size > 10 {
5049
rng.gen_range(size / 2, size)
5150
} else {
52-
continue
51+
continue;
5352
};
5453
let mut tmp = Vec::new();
5554
for i in 0..cmp::min(size, new_size) {

0 commit comments

Comments
 (0)