@@ -42,35 +42,14 @@ pub enum AllocInit {
42
42
}
43
43
44
44
/// Represents a block of allocated memory returned by an allocator.
45
- #[ derive( Debug ) ]
45
+ #[ derive( Debug , Copy , Clone ) ]
46
46
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
47
47
pub struct MemoryBlock {
48
- ptr : NonNull < u8 > ,
49
- size : usize ,
48
+ pub ptr : NonNull < u8 > ,
49
+ pub size : usize ,
50
50
}
51
51
52
52
impl MemoryBlock {
53
- /// Creates a new `MemoryBlock` from the specified `ptr` and `size`.
54
- #[ inline]
55
- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
56
- pub const fn new ( ptr : NonNull < u8 > , size : usize ) -> Self {
57
- Self { ptr, size }
58
- }
59
-
60
- /// Acquires the underlying `NonNull<u8>` pointer.
61
- #[ inline]
62
- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
63
- pub const fn ptr ( & self ) -> NonNull < u8 > {
64
- self . ptr
65
- }
66
-
67
- /// Returns the size of the memory block.
68
- #[ inline]
69
- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
70
- pub const fn size ( & self ) -> usize {
71
- self . size
72
- }
73
-
74
53
/// Initialize the memory block like specified by `init`.
75
54
///
76
55
/// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off].
@@ -98,12 +77,10 @@ impl MemoryBlock {
98
77
#[ inline]
99
78
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
100
79
pub unsafe fn init_offset ( & mut self , init : AllocInit , offset : usize ) {
101
- debug_assert ! ( offset <= self . size( ) , "`offset` must be smaller than or equal to `size()`" ) ;
80
+ debug_assert ! ( offset <= self . size, "`offset` must be smaller than or equal to `size()`" ) ;
102
81
match init {
103
82
AllocInit :: Uninitialized => ( ) ,
104
- AllocInit :: Zeroed => {
105
- self . ptr ( ) . as_ptr ( ) . add ( offset) . write_bytes ( 0 , self . size ( ) - offset)
106
- }
83
+ AllocInit :: Zeroed => self . ptr . as_ptr ( ) . add ( offset) . write_bytes ( 0 , self . size - offset) ,
107
84
}
108
85
}
109
86
}
@@ -246,9 +223,9 @@ pub unsafe trait AllocRef {
246
223
///
247
224
/// * `ptr` must be [*currently allocated*] via this allocator,
248
225
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
249
- // We can't require that `new_size` is strictly greater than `memory.size() ` because of ZSTs.
226
+ // We can't require that `new_size` is strictly greater than `memory.size` because of ZSTs.
250
227
// An alternative would be
251
- // * `new_size must be strictly greater than `memory.size() ` or both are zero
228
+ // * `new_size must be strictly greater than `memory.size` or both are zero
252
229
/// * `new_size` must be greater than or equal to `layout.size()`
253
230
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
254
231
/// (i.e., the rounded value must be less than `usize::MAX`).
@@ -280,19 +257,19 @@ pub unsafe trait AllocRef {
280
257
match placement {
281
258
ReallocPlacement :: InPlace => Err ( AllocErr ) ,
282
259
ReallocPlacement :: MayMove => {
283
- let old_size = layout. size ( ) ;
260
+ let size = layout. size ( ) ;
284
261
debug_assert ! (
285
- new_size >= old_size ,
262
+ new_size >= size ,
286
263
"`new_size` must be greater than or equal to `layout.size()`"
287
264
) ;
288
265
289
- if new_size == old_size {
290
- return Ok ( MemoryBlock :: new ( ptr, old_size ) ) ;
266
+ if new_size == size {
267
+ return Ok ( MemoryBlock { ptr, size } ) ;
291
268
}
292
269
293
270
let new_layout = Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ;
294
271
let new_memory = self . alloc ( new_layout, init) ?;
295
- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr ( ) . as_ptr ( ) , old_size ) ;
272
+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr . as_ptr ( ) , size ) ;
296
273
self . dealloc ( ptr, layout) ;
297
274
Ok ( new_memory)
298
275
}
@@ -324,10 +301,10 @@ pub unsafe trait AllocRef {
324
301
///
325
302
/// * `ptr` must be [*currently allocated*] via this allocator,
326
303
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
327
- // We can't require that `new_size` is strictly smaller than `memory.size() ` because of ZSTs.
304
+ // We can't require that `new_size` is strictly smaller than `memory.size` because of ZSTs.
328
305
// An alternative would be
329
- // * `new_size must be strictly smaller than `memory.size() ` or both are zero
330
- /// * `new_size` must be smaller than or equal to `memory .size()`
306
+ // * `new_size must be strictly smaller than `memory.size` or both are zero
307
+ /// * `new_size` must be smaller than or equal to `layout .size()`
331
308
///
332
309
/// [*currently allocated*]: #currently-allocated-memory
333
310
/// [*fit*]: #memory-fitting
@@ -355,19 +332,19 @@ pub unsafe trait AllocRef {
355
332
match placement {
356
333
ReallocPlacement :: InPlace => Err ( AllocErr ) ,
357
334
ReallocPlacement :: MayMove => {
358
- let old_size = layout. size ( ) ;
335
+ let size = layout. size ( ) ;
359
336
debug_assert ! (
360
- new_size <= old_size ,
337
+ new_size <= size ,
361
338
"`new_size` must be smaller than or equal to `layout.size()`"
362
339
) ;
363
340
364
- if new_size == old_size {
365
- return Ok ( MemoryBlock :: new ( ptr, old_size ) ) ;
341
+ if new_size == size {
342
+ return Ok ( MemoryBlock { ptr, size } ) ;
366
343
}
367
344
368
345
let new_layout = Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ;
369
346
let new_memory = self . alloc ( new_layout, AllocInit :: Uninitialized ) ?;
370
- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr ( ) . as_ptr ( ) , new_size) ;
347
+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr . as_ptr ( ) , new_size) ;
371
348
self . dealloc ( ptr, layout) ;
372
349
Ok ( new_memory)
373
350
}
0 commit comments