@@ -26,11 +26,30 @@ use crate::utils::sync::Mutex;
26
26
27
27
#[ repr( C ) ]
28
28
pub struct SlabHeader {
29
- pub ptr : UnsafeRef < SmallSlab > ,
29
+ /// Reference to the slab pool.
30
+ ptr : UnsafeRef < SmallSlab > ,
31
+ }
32
+
33
+ impl SlabHeader {
34
+ /// Gets the [`SlabHeader`] from an allocated object.
35
+ pub fn from_object < ' a > ( ptr : * const u8 ) -> & ' a Self {
36
+ assert ! ( !ptr. is_null( ) ) ;
37
+
38
+ let ptr = ( ptr as usize & !( 0xfff ) ) as * mut SlabHeader ;
39
+ unsafe { & * ptr }
40
+ }
41
+
42
+ /// Returns the slab pool to which this header belongs to.
43
+ pub fn as_slab < ' a > ( & ' a self ) -> & ' a SmallSlab {
44
+ self . ptr . as_ref ( )
45
+ }
30
46
}
31
47
32
48
const_assert_eq ! ( core:: mem:: size_of:: <SlabHeader >( ) , 8 ) ;
33
49
50
+ unsafe impl Send for SlabHeader { }
51
+ unsafe impl Sync for SlabHeader { }
52
+
34
53
/// For small slabs, the [`BufCtl`]s are stored inline.
35
54
struct BufCtl ( Option < NonNull < BufCtl > > ) ;
36
55
@@ -48,6 +67,9 @@ impl BufCtl {
48
67
49
68
const_assert_eq ! ( core:: mem:: size_of:: <BufCtl >( ) , 8 ) ;
50
69
70
+ unsafe impl Send for BufCtl { }
71
+ unsafe impl Sync for BufCtl { }
72
+
51
73
/// Used for allocations smaller than `1/8` of a page.
52
74
pub struct SmallSlab {
53
75
/// Size of the slab.
@@ -136,6 +158,3 @@ impl SmallSlab {
136
158
self . size
137
159
}
138
160
}
139
-
140
- unsafe impl Send for SmallSlab { }
141
- unsafe impl Sync for SmallSlab { }
0 commit comments