@@ -3,20 +3,38 @@ use std::{alloc::Layout, ptr::NonNull};
3
3
use allocator_api2:: alloc:: Global ;
4
4
use scoped_tls:: scoped_thread_local;
5
5
6
- use crate :: Allocator ;
6
+ use crate :: { FastAlloc , MemorySpace } ;
7
7
8
- scoped_thread_local ! ( pub ( crate ) static ALLOC : Allocator ) ;
8
+ scoped_thread_local ! ( pub ( crate ) static ALLOC : MemorySpace ) ;
9
9
10
- #[ derive( Debug , Clone , Copy , Default ) ]
11
- pub struct SwcAlloc ;
10
+ #[ derive( Debug , Clone , Copy ) ]
11
+ pub struct SwcAlloc {
12
+ pub ( crate ) is_arena_mode : bool ,
13
+ }
14
+
15
+ impl Default for FastAlloc {
16
+ fn default ( ) -> Self {
17
+ Self {
18
+ is_arena_mode : ALLOC . is_set ( ) ,
19
+ }
20
+ }
21
+ }
22
+
23
+ impl Default for SwcAlloc {
24
+ fn default ( ) -> Self {
25
+ SwcAlloc {
26
+ is_arena_mode : ALLOC . is_set ( ) ,
27
+ }
28
+ }
29
+ }
12
30
13
31
impl SwcAlloc {
14
32
/// `true` is passed to `f` if the box is allocated with a custom allocator.
15
33
fn with_allocator < T > (
16
34
& self ,
17
35
f : impl FnOnce ( & dyn allocator_api2:: alloc:: Allocator , bool ) -> T ,
18
36
) -> T {
19
- if ALLOC . is_set ( ) {
37
+ if self . is_arena_mode {
20
38
ALLOC . with ( |a| {
21
39
//
22
40
f ( & & * * a as & dyn allocator_api2:: alloc:: Allocator , true )
@@ -27,21 +45,8 @@ impl SwcAlloc {
27
45
}
28
46
}
29
47
30
- /// Set the last bit to 1
31
48
fn mark_ptr_as_arena_mode ( ptr : NonNull < [ u8 ] > ) -> NonNull < [ u8 ] > {
32
- let ( mut raw_ptr, metadata) = ptr_meta:: PtrExt :: to_raw_parts ( ptr. as_ptr ( ) ) ;
33
-
34
- raw_ptr = ( raw_ptr as usize | 1 ) as * mut ( ) ;
35
-
36
- unsafe {
37
- // Safety:
38
- NonNull :: new_unchecked ( ptr_meta:: from_raw_parts_mut ( raw_ptr, metadata) )
39
- }
40
- }
41
-
42
- fn is_ptr_in_arena_mode ( ptr : NonNull < u8 > ) -> bool {
43
- let ptr = ptr. as_ptr ( ) as usize ;
44
- ptr & 1 == 1
49
+ ptr
45
50
}
46
51
47
52
unsafe impl allocator_api2:: alloc:: Allocator for SwcAlloc {
@@ -73,7 +78,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc {
73
78
}
74
79
75
80
unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
76
- if is_ptr_in_arena_mode ( ptr ) {
81
+ if self . is_arena_mode {
77
82
debug_assert ! (
78
83
ALLOC . is_set( ) ,
79
84
"Deallocating a pointer allocated with arena mode with a non-arena mode allocator"
@@ -96,7 +101,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc {
96
101
old_layout : Layout ,
97
102
new_layout : Layout ,
98
103
) -> Result < NonNull < [ u8 ] > , allocator_api2:: alloc:: AllocError > {
99
- if is_ptr_in_arena_mode ( ptr ) {
104
+ if self . is_arena_mode {
100
105
debug_assert ! (
101
106
ALLOC . is_set( ) ,
102
107
"Growing a pointer allocated with arena mode with a non-arena mode allocator"
@@ -114,7 +119,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc {
114
119
old_layout : Layout ,
115
120
new_layout : Layout ,
116
121
) -> Result < NonNull < [ u8 ] > , allocator_api2:: alloc:: AllocError > {
117
- if is_ptr_in_arena_mode ( ptr ) {
122
+ if self . is_arena_mode {
118
123
debug_assert ! (
119
124
ALLOC . is_set( ) ,
120
125
"Growing a pointer allocated with arena mode with a non-arena mode allocator"
@@ -132,7 +137,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc {
132
137
old_layout : Layout ,
133
138
new_layout : Layout ,
134
139
) -> Result < NonNull < [ u8 ] > , allocator_api2:: alloc:: AllocError > {
135
- if is_ptr_in_arena_mode ( ptr ) {
140
+ if self . is_arena_mode {
136
141
debug_assert ! (
137
142
ALLOC . is_set( ) ,
138
143
"Shrinking a pointer allocated with arena mode with a non-arena mode allocator"
0 commit comments