Skip to content

Commit 1289cff

Browse files
committed
Update to nightly-2024-03-01
1 parent c4e5db8 commit 1289cff

21 files changed

+76
-92
lines changed

buddy/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#![allow(incomplete_features)]
22
#![feature(type_alias_impl_trait)]
33
#![feature(thread_local)]
4-
#![feature(core_intrinsics)]
54

65
extern crate mallockit;
76

8-
use core::alloc::Layout;
97
use mallockit::{
108
space::*,
119
space::{freelist_space::*, large_object_space::*},
1210
util::*,
1311
Mutator, Plan,
1412
};
15-
use std::intrinsics::likely;
1613

1714
const FREELIST_SPACE: SpaceId = SpaceId::DEFAULT;
1815
const LARGE_OBJECT_SPACE: SpaceId = SpaceId::LARGE_OBJECT_SPACE;
@@ -35,7 +32,7 @@ impl Plan for Buddy {
3532

3633
fn get_layout(ptr: Address) -> Layout {
3734
debug_assert!(FREELIST_SPACE.contains(ptr) || LARGE_OBJECT_SPACE.contains(ptr));
38-
if likely(FREELIST_SPACE.contains(ptr)) {
35+
if FREELIST_SPACE.contains(ptr) {
3936
FreeListSpace::get_layout(ptr)
4037
} else {
4138
Self::get().large_object_space.get_layout::<Size4K>(ptr)
@@ -66,7 +63,7 @@ impl Mutator for BuddyMutator {
6663
const NEW: Self = Self::new();
6764

6865
fn alloc(&mut self, layout: Layout) -> Option<Address> {
69-
if likely(FreeListSpace::can_allocate(layout)) {
66+
if FreeListSpace::can_allocate(layout) {
7067
mallockit::stat::track_allocation(layout, false);
7168
self.freelist.alloc(layout)
7269
} else {
@@ -77,7 +74,7 @@ impl Mutator for BuddyMutator {
7774

7875
fn dealloc(&mut self, ptr: Address) {
7976
debug_assert!(FREELIST_SPACE.contains(ptr) || LARGE_OBJECT_SPACE.contains(ptr));
80-
if likely(FREELIST_SPACE.contains(ptr)) {
77+
if FREELIST_SPACE.contains(ptr) {
8178
mallockit::stat::track_deallocation(false);
8279
self.freelist.dealloc(ptr)
8380
} else {

bump/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
extern crate mallockit;
66

7-
use core::alloc::Layout;
87
use mallockit::{space::immortal_space::*, space::*, util::*, Mutator, Plan};
98

109
const IMMORTAL_SPACE: SpaceId = SpaceId::DEFAULT;

hoard/src/hoard_space.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::intrinsics::likely;
2-
31
use super::{page_resource::BlockPageResource, Allocator, Space, SpaceId};
42
use crate::{pool::Pool, super_block::SuperBlock};
53
use mallockit::util::{discrete_tlab::DiscreteTLAB, size_class::SizeClass, *};
@@ -103,7 +101,7 @@ impl HoardAllocator {
103101
impl Allocator for HoardAllocator {
104102
fn alloc(&mut self, layout: Layout) -> Option<Address> {
105103
let size_class = SizeClass::from_layout(layout);
106-
if likely(layout.size() <= Self::LARGEST_SMALL_OBJECT) {
104+
if layout.size() <= Self::LARGEST_SMALL_OBJECT {
107105
if let Some(cell) = self.tlab.pop(size_class) {
108106
return Some(cell);
109107
}
@@ -114,10 +112,9 @@ impl Allocator for HoardAllocator {
114112
fn dealloc(&mut self, cell: Address) {
115113
let block = SuperBlock::containing(cell);
116114
let size = block.size_class.bytes();
117-
if likely(
118-
size <= Self::LARGEST_SMALL_OBJECT
119-
&& size + self.tlab.free_bytes() <= Self::LOCAL_HEAP_THRESHOLD,
120-
) {
115+
if size <= Self::LARGEST_SMALL_OBJECT
116+
&& size + self.tlab.free_bytes() <= Self::LOCAL_HEAP_THRESHOLD
117+
{
121118
self.tlab.push(block.size_class, cell);
122119
} else {
123120
self.local.free_cell(cell, &self.space);

hoard/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#![allow(incomplete_features)]
22
#![feature(thread_local)]
3-
#![feature(core_intrinsics)]
43
#![feature(const_mut_refs)]
54
#![feature(const_trait_impl)]
65
#![feature(step_trait)]
7-
#![feature(const_likely)]
86

97
#[allow(unused)]
108
#[macro_use]
@@ -14,10 +12,8 @@ mod hoard_space;
1412
mod pool;
1513
mod super_block;
1614

17-
use core::alloc::Layout;
1815
use hoard_space::*;
1916
use mallockit::{space::large_object_space::*, space::*, util::*, Mutator, Plan};
20-
use std::intrinsics::likely;
2117

2218
const HOARD_SPACE: SpaceId = SpaceId::DEFAULT;
2319
const LARGE_OBJECT_SPACE: SpaceId = SpaceId::LARGE_OBJECT_SPACE;
@@ -40,7 +36,7 @@ impl Plan for Hoard {
4036

4137
fn get_layout(ptr: Address) -> Layout {
4238
debug_assert!(HOARD_SPACE.contains(ptr) || LARGE_OBJECT_SPACE.contains(ptr));
43-
if likely(HOARD_SPACE.contains(ptr)) {
39+
if HOARD_SPACE.contains(ptr) {
4440
HoardSpace::get_layout(ptr)
4541
} else {
4642
Self::get().large_object_space.get_layout::<Size4K>(ptr)
@@ -68,7 +64,7 @@ impl Mutator for HoardMutator {
6864
const NEW: Self = Self::new();
6965

7066
fn alloc(&mut self, layout: Layout) -> Option<Address> {
71-
if likely(HoardSpace::can_allocate(layout)) {
67+
if HoardSpace::can_allocate(layout) {
7268
mallockit::stat::track_allocation(layout, false);
7369
self.hoard.alloc(layout)
7470
} else {
@@ -79,7 +75,7 @@ impl Mutator for HoardMutator {
7975

8076
fn dealloc(&mut self, ptr: Address) {
8177
debug_assert!(HOARD_SPACE.contains(ptr) || LARGE_OBJECT_SPACE.contains(ptr));
82-
if likely(HOARD_SPACE.contains(ptr)) {
78+
if HOARD_SPACE.contains(ptr) {
8379
mallockit::stat::track_deallocation(false);
8480
self.hoard.dealloc(ptr)
8581
} else {

hoard/src/pool.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use crate::{hoard_space::HoardSpace, super_block::SuperBlock};
22
use array_const_fn_init::array_const_fn_init;
33
use mallockit::util::{size_class::SizeClass, Address, Lazy, Local};
44
use spin::{relax::Yield, MutexGuard};
5-
use std::{
6-
intrinsics::likely,
7-
sync::atomic::{AtomicUsize, Ordering},
8-
};
5+
use std::sync::atomic::{AtomicUsize, Ordering};
96

107
type Mutex<T> = spin::mutex::Mutex<T, Yield>;
118

@@ -134,12 +131,12 @@ impl BlockList {
134131

135132
#[cold]
136133
fn move_to_front_slow(&mut self, mut block: SuperBlock, alloc: bool) {
137-
if likely(Some(block) == self.cache) {
134+
if Some(block) == self.cache {
138135
return;
139136
}
140137
let group = Self::group(block, alloc);
141138
let block_group = block.group as usize;
142-
if likely(Some(block) == self.groups[group] || group == block_group) {
139+
if Some(block) == self.groups[group] || group == block_group {
143140
return;
144141
}
145142
if self.groups[block_group] == Some(block) {
@@ -161,7 +158,7 @@ impl BlockList {
161158
}
162159

163160
fn move_to_front(&mut self, block: SuperBlock, alloc: bool) {
164-
if likely(Some(block) == self.cache) {
161+
if Some(block) == self.cache {
165162
return;
166163
}
167164
self.move_to_front_slow(block, alloc)

hoard/src/super_block.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
intrinsics::{likely, unlikely},
3-
num::NonZeroUsize,
4-
};
1+
use std::num::NonZeroUsize;
52

63
use mallockit::util::{aligned_block::AlignedBlockConfig, size_class::SizeClass};
74

@@ -64,17 +61,17 @@ impl SuperBlock {
6461
}
6562

6663
pub fn alloc_cell(mut self) -> Option<Address> {
67-
let cell = if unlikely(self.head_cell.is_zero()) {
68-
if likely(self.bump_cursor < Self::BYTES as u32) {
64+
let cell = if !self.head_cell.is_zero() {
65+
self.head_cell
66+
} else {
67+
if self.bump_cursor < Self::BYTES as u32 {
6968
let cell = self.start() + (self.bump_cursor as usize);
7069
self.bump_cursor = self.bump_cursor + self.size_class.bytes() as u32;
7170
self.used_bytes += self.size_class.bytes() as u32;
7271
return Some(cell);
7372
} else {
7473
return None;
7574
}
76-
} else {
77-
self.head_cell
7875
};
7976
self.head_cell = unsafe { cell.load::<Address>() };
8077
self.used_bytes += self.size_class.bytes() as u32;

mallockit/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![allow(incomplete_features)]
2+
#![allow(internal_features)]
23
#![feature(core_intrinsics)]
34
#![feature(const_trait_impl)]
45
#![feature(const_mut_refs)]
56
#![feature(const_ptr_is_null)]
67
#![feature(type_ascription)]
78
#![feature(step_trait)]
8-
#![feature(const_likely)]
99
#![feature(thread_local)]
1010
#![feature(allocator_api)]
1111
#![feature(never_type)]
@@ -21,6 +21,7 @@
2121
#![feature(type_alias_impl_trait)]
2222
#![feature(specialization)]
2323
#![feature(const_for)]
24+
#![feature(effects)]
2425

2526
extern crate mallockit_proc_macro;
2627

mallockit/src/log.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use libc;
21
use spin::Mutex;
32
use std::fmt;
43
use std::fmt::Write;
5-
use std::intrinsics::unlikely;
64

75
#[doc(hidden)]
86
#[cold]
@@ -76,7 +74,7 @@ impl Log {
7674
fn put_char(&mut self, c: u8) {
7775
self.buffer[self.cursor] = c;
7876
self.cursor += 1;
79-
if unlikely(self.cursor >= self.buffer.len()) {
77+
if self.cursor >= self.buffer.len() {
8078
self.flush();
8179
}
8280
}

mallockit/src/malloc.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::util::Address;
44
use crate::util::Lazy;
55
use crate::Mutator;
66
use core::{alloc::Layout, ptr};
7-
use std::intrinsics::unlikely;
7+
use std::marker::PhantomData;
88

99
pub trait GetMutatorType {
1010
type Mutator: Mutator;
1111
}
1212

13-
pub struct MallocAPI<P: Plan>(&'static Lazy<P>);
13+
pub struct MallocAPI<P: Plan>(PhantomData<P>);
1414

1515
impl<P: Plan> GetMutatorType for MallocAPI<P> {
1616
type Mutator = P::Mutator;
@@ -31,7 +31,7 @@ impl<P: Plan> MallocAPI<P> {
3131
pub const PAGE_SIZE: usize = 4096;
3232

3333
pub const fn new(plan: &'static Lazy<P>) -> Self {
34-
Self(plan)
34+
Self(PhantomData)
3535
}
3636

3737
pub const fn new_mutator() -> P::Mutator {
@@ -58,7 +58,7 @@ impl<P: Plan> MallocAPI<P> {
5858
pub unsafe fn malloc_size(&self, ptr: Address) -> usize {
5959
let ptr = Address::from(ptr);
6060
#[cfg(target_os = "macos")]
61-
if unlikely(Self::zero_spaceid(ptr.into())) {
61+
if Self::zero_spaceid(ptr.into()) {
6262
return crate::util::macos_malloc_zone::external_memory_size(ptr);
6363
}
6464
P::get_layout(ptr).size()
@@ -85,11 +85,11 @@ impl<P: Plan> MallocAPI<P> {
8585
}
8686

8787
pub unsafe fn free(&self, ptr: *mut u8) {
88-
if unlikely(ptr.is_null()) {
88+
if ptr.is_null() {
8989
return;
9090
}
9191
#[cfg(target_os = "macos")]
92-
if unlikely(Self::zero_spaceid(ptr.into())) {
92+
if Self::zero_spaceid(ptr.into()) {
9393
return;
9494
}
9595
self.mutator().dealloc(ptr.into());
@@ -105,14 +105,14 @@ impl<P: Plan> MallocAPI<P> {
105105
if ptr.is_null() {
106106
return self.alloc_or_enomem(new_size, Self::MIN_ALIGNMENT);
107107
}
108-
if unlikely(free_if_new_size_is_zero && new_size == 0) {
108+
if free_if_new_size_is_zero && new_size == 0 {
109109
self.free(ptr);
110110
return ptr::null_mut();
111111
}
112112
let new_size = Self::align_up(new_size, Self::MIN_ALIGNMENT);
113113

114114
#[cfg(target_os = "macos")]
115-
if unlikely(Self::zero_spaceid(ptr.into())) {
115+
if Self::zero_spaceid(ptr.into()) {
116116
let ptr = Address::from(ptr);
117117
let old_size = crate::util::macos_malloc_zone::external_memory_size(ptr);
118118
let new_layout =
@@ -152,7 +152,7 @@ impl<P: Plan> MallocAPI<P> {
152152
mut alignment: usize,
153153
size: usize,
154154
) -> i32 {
155-
if unlikely(!alignment.is_power_of_two()) {
155+
if !alignment.is_power_of_two() {
156156
return libc::EINVAL;
157157
}
158158
alignment = std::cmp::max(alignment, Self::MIN_ALIGNMENT);
@@ -168,7 +168,7 @@ impl<P: Plan> MallocAPI<P> {
168168
pub unsafe fn memalign(&self, alignment: usize, size: usize) -> *mut u8 {
169169
let mut result = ptr::null_mut();
170170
let errno = self.posix_memalign(&mut result, alignment, size);
171-
if unlikely(result.is_null()) {
171+
if result.is_null() {
172172
Self::set_error(errno)
173173
}
174174
result
@@ -181,11 +181,10 @@ impl<P: Plan> MallocAPI<P> {
181181
einval_if_size_is_not_aligned: bool,
182182
einval_if_size_is_zero: bool,
183183
) -> *mut u8 {
184-
if unlikely(
185-
!alignment.is_power_of_two()
186-
|| (einval_if_size_is_not_aligned && (size & (alignment - 1)) != 0)
187-
|| (einval_if_size_is_zero && size == 0),
188-
) {
184+
if !alignment.is_power_of_two()
185+
|| (einval_if_size_is_not_aligned && (size & (alignment - 1)) != 0)
186+
|| (einval_if_size_is_zero && size == 0)
187+
{
189188
Self::set_error(libc::EINVAL);
190189
return ptr::null_mut();
191190
}

mallockit/src/mutator.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::alloc::Layout;
2-
use std::intrinsics::unlikely;
32
use std::ptr;
43

54
use crate::plan::Plan;
@@ -33,7 +32,7 @@ pub trait Mutator: Sized + 'static + TLS {
3332

3433
fn realloc(&mut self, ptr: Address, new_size: usize) -> Option<Address> {
3534
let layout = Self::Plan::get_layout(ptr);
36-
if unlikely(layout.size() >= new_size) {
35+
if layout.size() >= new_size {
3736
return Some(ptr);
3837
}
3938
let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
@@ -64,7 +63,7 @@ impl InternalTLS {
6463

6564
#[cfg(not(target_os = "macos"))]
6665
pub fn current() -> &'static mut Self {
67-
unsafe { &mut INTERNAL_TLS }
66+
unsafe { &mut *INTERNAL_TLS.get() }
6867
}
6968

7069
#[cfg(target_os = "macos")]
@@ -76,7 +75,8 @@ impl InternalTLS {
7675

7776
#[cfg(not(target_os = "macos"))]
7877
#[thread_local]
79-
static mut INTERNAL_TLS: InternalTLS = InternalTLS::NEW;
78+
static mut INTERNAL_TLS: std::cell::UnsafeCell<InternalTLS> =
79+
std::cell::UnsafeCell::new(InternalTLS::NEW);
8080

8181
pub trait TLS: Sized {
8282
const NEW: Self;
@@ -127,7 +127,7 @@ mod macos_tls {
127127
#[allow(unused)]
128128
pub(super) fn get_internal_tls() -> *mut InternalTLS {
129129
let mut tls = _get_tls::<InternalTLS>();
130-
if unlikely(tls.is_null()) {
130+
if tls.is_null() {
131131
unsafe {
132132
mallockit_initialize_macos_tls();
133133
}
@@ -140,7 +140,7 @@ mod macos_tls {
140140
#[allow(unused)]
141141
pub(super) fn get_tls<T: TLS>() -> *mut T {
142142
let mut tls = _get_tls::<(InternalTLS, T)>();
143-
if unlikely(tls.is_null()) {
143+
if tls.is_null() {
144144
tls = init_tls::<T>();
145145
}
146146
unsafe { &mut (*tls).1 }

0 commit comments

Comments
 (0)