10
10
11
11
//! Memory allocation APIs
12
12
13
- #![ unstable( feature = "allocator_api" ,
14
- reason = "the precise API and guarantees it provides may be tweaked \
15
- slightly, especially to possibly take into account the \
16
- types being stored to make room for a future \
17
- tracing garbage collector",
18
- issue = "32838" ) ]
13
+ #![ stable( feature = "alloc_module" , since = "1.28.0" ) ]
19
14
20
15
use cmp;
21
16
use fmt;
22
17
use mem;
23
18
use usize;
24
19
use ptr:: { self , NonNull } ;
25
20
21
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
26
22
#[ cfg( stage0) ]
27
23
pub type Opaque = u8 ;
28
24
29
25
/// Represents the combination of a starting address and
30
26
/// a total capacity of the returned block.
27
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
31
28
#[ derive( Debug ) ]
32
29
pub struct Excess ( pub NonNull < u8 > , pub usize ) ;
33
30
@@ -48,6 +45,7 @@ fn size_align<T>() -> (usize, usize) {
48
45
/// requests have positive size. A caller to the `Alloc::alloc`
49
46
/// method must either ensure that conditions like this are met, or
50
47
/// use specific allocators with looser requirements.)
48
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
51
49
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
52
50
pub struct Layout {
53
51
// size of the requested block of memory, measured in bytes.
@@ -78,6 +76,7 @@ impl Layout {
78
76
/// * `size`, when rounded up to the nearest multiple of `align`,
79
77
/// must not overflow (i.e. the rounded value must be less than
80
78
/// `usize::MAX`).
79
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
81
80
#[ inline]
82
81
pub fn from_size_align ( size : usize , align : usize ) -> Result < Self , LayoutErr > {
83
82
if !align. is_power_of_two ( ) {
@@ -114,20 +113,24 @@ impl Layout {
114
113
/// This function is unsafe as it does not verify that `align` is
115
114
/// a power-of-two nor `size` aligned to `align` fits within the
116
115
/// address space (i.e. the `Layout::from_size_align` preconditions).
116
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
117
117
#[ inline]
118
118
pub unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
119
119
Layout { size : size, align : align }
120
120
}
121
121
122
122
/// The minimum size in bytes for a memory block of this layout.
123
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
123
124
#[ inline]
124
125
pub fn size ( & self ) -> usize { self . size }
125
126
126
127
/// The minimum byte alignment for a memory block of this layout.
128
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
127
129
#[ inline]
128
130
pub fn align ( & self ) -> usize { self . align }
129
131
130
132
/// Constructs a `Layout` suitable for holding a value of type `T`.
133
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
131
134
pub fn new < T > ( ) -> Self {
132
135
let ( size, align) = size_align :: < T > ( ) ;
133
136
// Note that the align is guaranteed by rustc to be a power of two and
@@ -143,6 +146,7 @@ impl Layout {
143
146
/// Produces layout describing a record that could be used to
144
147
/// allocate backing structure for `T` (which could be a trait
145
148
/// or other unsized type like a slice).
149
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
146
150
pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
147
151
let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
148
152
// See rationale in `new` for why this us using an unsafe variant below
@@ -168,6 +172,7 @@ impl Layout {
168
172
///
169
173
/// Panics if the combination of `self.size` and the given `align`
170
174
/// violates the conditions listed in `from_size_align`.
175
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
171
176
#[ inline]
172
177
pub fn align_to ( & self , align : usize ) -> Self {
173
178
Layout :: from_size_align ( self . size , cmp:: max ( self . align , align) ) . unwrap ( )
@@ -189,6 +194,7 @@ impl Layout {
189
194
/// to be less than or equal to the alignment of the starting
190
195
/// address for the whole allocated block of memory. One way to
191
196
/// satisfy this constraint is to ensure `align <= self.align`.
197
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
192
198
#[ inline]
193
199
pub fn padding_needed_for ( & self , align : usize ) -> usize {
194
200
let len = self . size ( ) ;
@@ -224,6 +230,7 @@ impl Layout {
224
230
/// of each element in the array.
225
231
///
226
232
/// On arithmetic overflow, returns `None`.
233
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
227
234
#[ inline]
228
235
pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutErr > {
229
236
let padded_size = self . size . checked_add ( self . padding_needed_for ( self . align ) )
@@ -244,6 +251,7 @@ impl Layout {
244
251
/// (assuming that the record itself starts at offset 0).
245
252
///
246
253
/// On arithmetic overflow, returns `None`.
254
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
247
255
pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
248
256
let new_align = cmp:: max ( self . align , next. align ) ;
249
257
let realigned = Layout :: from_size_align ( self . size , new_align) ?;
@@ -271,6 +279,7 @@ impl Layout {
271
279
/// aligned.
272
280
///
273
281
/// On arithmetic overflow, returns `None`.
282
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
274
283
pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutErr > {
275
284
let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutErr { private : ( ) } ) ?;
276
285
Layout :: from_size_align ( size, self . align )
@@ -291,6 +300,7 @@ impl Layout {
291
300
/// `extend`.)
292
301
///
293
302
/// On arithmetic overflow, returns `None`.
303
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
294
304
pub fn extend_packed ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
295
305
let new_size = self . size ( ) . checked_add ( next. size ( ) )
296
306
. ok_or ( LayoutErr { private : ( ) } ) ?;
@@ -301,6 +311,7 @@ impl Layout {
301
311
/// Creates a layout describing the record for a `[T; n]`.
302
312
///
303
313
/// On arithmetic overflow, returns `None`.
314
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
304
315
pub fn array < T > ( n : usize ) -> Result < Self , LayoutErr > {
305
316
Layout :: new :: < T > ( )
306
317
. repeat ( n)
@@ -314,12 +325,14 @@ impl Layout {
314
325
/// The parameters given to `Layout::from_size_align`
315
326
/// or some other `Layout` constructor
316
327
/// do not satisfy its documented constraints.
328
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
317
329
#[ derive( Clone , PartialEq , Eq , Debug ) ]
318
330
pub struct LayoutErr {
319
331
private : ( )
320
332
}
321
333
322
334
// (we need this for downstream impl of trait Error)
335
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
323
336
impl fmt:: Display for LayoutErr {
324
337
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
325
338
f. write_str ( "invalid parameters to Layout::from_size_align" )
@@ -330,10 +343,12 @@ impl fmt::Display for LayoutErr {
330
343
/// that may be due to resource exhaustion or to
331
344
/// something wrong when combining the given input arguments with this
332
345
/// allocator.
346
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
333
347
#[ derive( Clone , PartialEq , Eq , Debug ) ]
334
348
pub struct AllocErr ;
335
349
336
350
// (we need this for downstream impl of trait Error)
351
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
337
352
impl fmt:: Display for AllocErr {
338
353
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
339
354
f. write_str ( "memory allocation failed" )
@@ -344,16 +359,19 @@ impl fmt::Display for AllocErr {
344
359
/// `shrink_in_place` were unable to reuse the given memory block for
345
360
/// a requested layout.
346
361
// FIXME: should this be in libcore or liballoc?
362
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
347
363
#[ derive( Clone , PartialEq , Eq , Debug ) ]
348
364
pub struct CannotReallocInPlace ;
349
365
366
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
350
367
impl CannotReallocInPlace {
351
368
pub fn description ( & self ) -> & str {
352
369
"cannot reallocate allocator's memory in place"
353
370
}
354
371
}
355
372
356
373
// (we need this for downstream impl of trait Error)
374
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
357
375
impl fmt:: Display for CannotReallocInPlace {
358
376
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
359
377
write ! ( f, "{}" , self . description( ) )
@@ -435,6 +453,7 @@ impl From<AllocErr> for CollectionAllocErr {
435
453
/// * `Layout` queries and calculations in general must be correct. Callers of
436
454
/// this trait are allowed to rely on the contracts defined on each method,
437
455
/// and implementors must ensure such contracts remain true.
456
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
438
457
pub unsafe trait GlobalAlloc {
439
458
/// Allocate memory as described by the given `layout`.
440
459
///
@@ -650,6 +669,7 @@ pub unsafe trait GlobalAlloc {
650
669
///
651
670
/// Note that this list may get tweaked over time as clarifications are made in
652
671
/// the future.
672
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
653
673
pub unsafe trait Alloc {
654
674
655
675
// (Note: some existing allocators have unspecified but well-defined
0 commit comments