25
25
//! drop(pool);
26
26
//! ```
27
27
28
- use std:: alloc:: { alloc, dealloc, handle_alloc_error, realloc, Layout } ;
29
28
use std:: fmt;
30
29
use std:: mem;
31
30
use std:: ops:: { Deref , DerefMut } ;
32
- use std:: ptr:: { self , NonNull } ;
31
+ use std:: ptr;
33
32
use std:: sync:: Mutex ;
34
33
35
34
/// A pool of byte slices, that reuses memory.
36
35
#[ derive( Debug ) ]
37
36
pub struct BytePool {
38
- list : Mutex < Vec < RawBlock > > ,
37
+ list : Mutex < Vec < Vec < u8 > > > ,
39
38
}
40
39
41
- pub struct RawBlock {
42
- ptr : NonNull < u8 > ,
43
- layout : Layout ,
44
- }
45
-
46
- unsafe impl Sync for RawBlock { }
47
- unsafe impl Send for RawBlock { }
48
-
49
- #[ cfg( feature = "stable_deref" ) ]
50
- unsafe impl stable_deref_trait:: StableDeref for RawBlock { }
40
+ pub type RawBlock = Vec < u8 > ;
51
41
52
42
pub struct Block < ' a > {
53
- data : mem:: ManuallyDrop < RawBlock > ,
43
+ data : mem:: ManuallyDrop < Vec < u8 > > ,
54
44
pool : & ' a BytePool ,
55
45
}
56
46
@@ -60,12 +50,6 @@ impl fmt::Debug for Block<'_> {
60
50
}
61
51
}
62
52
63
- impl fmt:: Debug for RawBlock {
64
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
65
- write ! ( f, "RawBlock({:?})" , self . deref( ) )
66
- }
67
- }
68
-
69
53
impl Default for BytePool {
70
54
fn default ( ) -> Self {
71
55
BytePool {
@@ -74,13 +58,6 @@ impl Default for BytePool {
74
58
}
75
59
}
76
60
77
- fn layout_for_size ( size : usize ) -> Layout {
78
- let elem_size = mem:: size_of :: < u8 > ( ) ;
79
- let alloc_size = size. checked_mul ( elem_size) . unwrap ( ) ;
80
- let align = mem:: align_of :: < u8 > ( ) ;
81
- Layout :: from_size_align ( alloc_size, align) . unwrap ( )
82
- }
83
-
84
61
impl BytePool {
85
62
/// Constructs a new pool.
86
63
pub fn new ( ) -> Self {
@@ -98,19 +75,19 @@ impl BytePool {
98
75
let start = if end > 4 { end - 4 } else { 0 } ;
99
76
100
77
for i in start..end {
101
- if lock[ i] . layout . size ( ) == size {
78
+ if lock[ i] . len ( ) == size {
102
79
// found one, reuse it
103
80
return Block :: new ( lock. remove ( i) , self ) ;
104
81
}
105
82
}
106
83
drop ( lock) ;
107
84
108
85
// allocate a new block
109
- let data = RawBlock :: alloc ( size) ;
86
+ let data = vec ! [ 0u8 ; size] ;
110
87
Block :: new ( data, self )
111
88
}
112
89
113
- fn push_raw_block ( & self , block : RawBlock ) {
90
+ fn push_raw_block ( & self , block : Vec < u8 > ) {
114
91
self . list . lock ( ) . unwrap ( ) . push ( block) ;
115
92
}
116
93
}
@@ -122,89 +99,34 @@ impl<'a> Drop for Block<'a> {
122
99
}
123
100
}
124
101
125
- impl RawBlock {
126
- fn alloc ( size : usize ) -> Self {
127
- // TODO: consider caching the layout
128
- let layout = layout_for_size ( size) ;
129
- debug_assert ! ( layout. size( ) > 0 ) ;
130
-
131
- let ptr = unsafe { alloc ( layout) } ;
132
- RawBlock {
133
- ptr : NonNull :: new ( ptr) . unwrap_or_else ( || handle_alloc_error ( layout) ) ,
134
- layout,
135
- }
136
- }
137
-
138
- fn grow ( & mut self , new_size : usize ) {
139
- // TODO: use grow_in_place once it stablizies and possibly via a flag.
140
- assert ! ( new_size > 0 ) ;
141
- let new_layout = Layout :: from_size_align ( new_size, self . layout . align ( ) ) . unwrap ( ) ;
142
- let new_ptr = unsafe { realloc ( self . ptr . as_mut ( ) , self . layout , new_layout. size ( ) ) } ;
143
- self . ptr = NonNull :: new ( new_ptr) . unwrap_or_else ( || handle_alloc_error ( self . layout ) ) ;
144
- self . layout = new_layout;
145
- }
146
-
147
- fn shrink ( & mut self , new_size : usize ) {
148
- // TODO: use shrink_in_place once it stablizies and possibly via a flag.
149
- assert ! ( new_size > 0 ) ;
150
- let new_layout = Layout :: from_size_align ( new_size, self . layout . align ( ) ) . unwrap ( ) ;
151
- let new_ptr = unsafe { realloc ( self . ptr . as_mut ( ) , self . layout , new_layout. size ( ) ) } ;
152
- self . ptr = NonNull :: new ( new_ptr) . unwrap_or_else ( || handle_alloc_error ( self . layout ) ) ;
153
- self . layout = new_layout;
154
- }
155
- }
156
-
157
- impl Drop for RawBlock {
158
- fn drop ( & mut self ) {
159
- unsafe {
160
- dealloc ( self . ptr . as_mut ( ) , self . layout ) ;
161
- }
162
- }
163
- }
164
-
165
- impl Deref for RawBlock {
166
- type Target = [ u8 ] ;
167
-
168
- #[ inline]
169
- fn deref ( & self ) -> & Self :: Target {
170
- unsafe { std:: slice:: from_raw_parts ( self . ptr . as_ptr ( ) , self . layout . size ( ) ) }
171
- }
172
- }
173
-
174
- impl DerefMut for RawBlock {
175
- #[ inline]
176
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
177
- unsafe { std:: slice:: from_raw_parts_mut ( self . ptr . as_mut ( ) , self . layout . size ( ) ) }
178
- }
179
- }
180
-
181
102
impl < ' a > Block < ' a > {
182
- fn new ( data : RawBlock , pool : & ' a BytePool ) -> Self {
103
+ fn new ( data : Vec < u8 > , pool : & ' a BytePool ) -> Self {
183
104
Block {
184
105
data : mem:: ManuallyDrop :: new ( data) ,
185
106
pool,
186
107
}
187
108
}
188
109
189
- /// Resizes a block to a new size
110
+ /// Resizes a block to a new size.
190
111
pub fn realloc ( & mut self , new_size : usize ) {
191
112
use std:: cmp:: Ordering :: * ;
192
113
114
+ assert ! ( new_size > 0 ) ;
193
115
match new_size. cmp ( & self . size ( ) ) {
194
- Greater => self . data . grow ( new_size) ,
195
- Less => self . data . shrink ( new_size) ,
116
+ Greater => self . data . resize ( new_size, 0u8 ) ,
117
+ Less => self . data . truncate ( new_size) ,
196
118
Equal => { }
197
119
}
198
120
}
199
121
200
122
/// Returns the amount of bytes this block has.
201
123
pub fn size ( & self ) -> usize {
202
- self . data . layout . size ( )
124
+ self . data . capacity ( )
203
125
}
204
126
}
205
127
206
128
impl < ' a > Deref for Block < ' a > {
207
- type Target = [ u8 ] ;
129
+ type Target = Vec < u8 > ;
208
130
209
131
#[ inline]
210
132
fn deref ( & self ) -> & Self :: Target {
@@ -254,6 +176,9 @@ mod tests {
254
176
let pool = BytePool :: new ( ) ;
255
177
256
178
let mut buf = pool. alloc ( 10 ) ;
179
+
180
+ let _slice: & [ u8 ] = & buf;
181
+
257
182
assert_eq ! ( buf. len( ) , 10 ) ;
258
183
for el in buf. iter_mut ( ) {
259
184
* el = 1 ;
0 commit comments