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;
@@ -24,11 +19,13 @@ use usize;
24
19
use ptr:: { self , NonNull } ;
25
20
use num:: NonZeroUsize ;
26
21
22
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
27
23
#[ cfg( stage0) ]
28
24
pub type Opaque = u8 ;
29
25
30
26
/// Represents the combination of a starting address and
31
27
/// a total capacity of the returned block.
28
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
32
29
#[ derive( Debug ) ]
33
30
pub struct Excess ( pub NonNull < u8 > , pub usize ) ;
34
31
@@ -49,6 +46,7 @@ fn size_align<T>() -> (usize, usize) {
49
46
/// requests have positive size. A caller to the `Alloc::alloc`
50
47
/// method must either ensure that conditions like this are met, or
51
48
/// use specific allocators with looser requirements.)
49
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
52
50
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
53
51
pub struct Layout {
54
52
// size of the requested block of memory, measured in bytes.
@@ -74,6 +72,7 @@ impl Layout {
74
72
/// * `size`, when rounded up to the nearest multiple of `align`,
75
73
/// must not overflow (i.e. the rounded value must be less than
76
74
/// `usize::MAX`).
75
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
77
76
#[ inline]
78
77
pub fn from_size_align ( size : usize , align : usize ) -> Result < Self , LayoutErr > {
79
78
if !align. is_power_of_two ( ) {
@@ -109,20 +108,24 @@ impl Layout {
109
108
///
110
109
/// This function is unsafe as it does not verify the preconditions from
111
110
/// [`Layout::from_size_align`](#method.from_size_align).
111
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
112
112
#[ inline]
113
113
pub unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
114
114
Layout { size_ : size, align_ : NonZeroUsize :: new_unchecked ( align) }
115
115
}
116
116
117
117
/// The minimum size in bytes for a memory block of this layout.
118
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
118
119
#[ inline]
119
120
pub fn size ( & self ) -> usize { self . size_ }
120
121
121
122
/// The minimum byte alignment for a memory block of this layout.
123
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
122
124
#[ inline]
123
125
pub fn align ( & self ) -> usize { self . align_ . get ( ) }
124
126
125
127
/// Constructs a `Layout` suitable for holding a value of type `T`.
128
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
126
129
#[ inline]
127
130
pub fn new < T > ( ) -> Self {
128
131
let ( size, align) = size_align :: < T > ( ) ;
@@ -139,6 +142,7 @@ impl Layout {
139
142
/// Produces layout describing a record that could be used to
140
143
/// allocate backing structure for `T` (which could be a trait
141
144
/// or other unsized type like a slice).
145
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
142
146
#[ inline]
143
147
pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
144
148
let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
@@ -166,6 +170,7 @@ impl Layout {
166
170
/// Panics if the combination of `self.size()` and the given `align`
167
171
/// violates the conditions listed in
168
172
/// [`Layout::from_size_align`](#method.from_size_align).
173
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
169
174
#[ inline]
170
175
pub fn align_to ( & self , align : usize ) -> Self {
171
176
Layout :: from_size_align ( self . size ( ) , cmp:: max ( self . align ( ) , align) ) . unwrap ( )
@@ -187,6 +192,7 @@ impl Layout {
187
192
/// to be less than or equal to the alignment of the starting
188
193
/// address for the whole allocated block of memory. One way to
189
194
/// satisfy this constraint is to ensure `align <= self.align()`.
195
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
190
196
#[ inline]
191
197
pub fn padding_needed_for ( & self , align : usize ) -> usize {
192
198
let len = self . size ( ) ;
@@ -223,6 +229,7 @@ impl Layout {
223
229
/// of each element in the array.
224
230
///
225
231
/// On arithmetic overflow, returns `LayoutErr`.
232
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
226
233
#[ inline]
227
234
pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutErr > {
228
235
let padded_size = self . size ( ) . checked_add ( self . padding_needed_for ( self . align ( ) ) )
@@ -248,6 +255,7 @@ impl Layout {
248
255
/// (assuming that the record itself starts at offset 0).
249
256
///
250
257
/// On arithmetic overflow, returns `LayoutErr`.
258
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
251
259
#[ inline]
252
260
pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
253
261
let new_align = cmp:: max ( self . align ( ) , next. align ( ) ) ;
@@ -274,6 +282,7 @@ impl Layout {
274
282
/// aligned.
275
283
///
276
284
/// On arithmetic overflow, returns `LayoutErr`.
285
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
277
286
#[ inline]
278
287
pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutErr > {
279
288
let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutErr { private : ( ) } ) ?;
@@ -295,6 +304,7 @@ impl Layout {
295
304
/// `extend`.)
296
305
///
297
306
/// On arithmetic overflow, returns `LayoutErr`.
307
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
298
308
#[ inline]
299
309
pub fn extend_packed ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
300
310
let new_size = self . size ( ) . checked_add ( next. size ( ) )
@@ -306,6 +316,7 @@ impl Layout {
306
316
/// Creates a layout describing the record for a `[T; n]`.
307
317
///
308
318
/// On arithmetic overflow, returns `LayoutErr`.
319
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
309
320
#[ inline]
310
321
pub fn array < T > ( n : usize ) -> Result < Self , LayoutErr > {
311
322
Layout :: new :: < T > ( )
@@ -320,12 +331,14 @@ impl Layout {
320
331
/// The parameters given to `Layout::from_size_align`
321
332
/// or some other `Layout` constructor
322
333
/// do not satisfy its documented constraints.
334
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
323
335
#[ derive( Clone , PartialEq , Eq , Debug ) ]
324
336
pub struct LayoutErr {
325
337
private : ( )
326
338
}
327
339
328
340
// (we need this for downstream impl of trait Error)
341
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
329
342
impl fmt:: Display for LayoutErr {
330
343
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
331
344
f. write_str ( "invalid parameters to Layout::from_size_align" )
@@ -336,10 +349,12 @@ impl fmt::Display for LayoutErr {
336
349
/// that may be due to resource exhaustion or to
337
350
/// something wrong when combining the given input arguments with this
338
351
/// allocator.
352
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
339
353
#[ derive( Clone , PartialEq , Eq , Debug ) ]
340
354
pub struct AllocErr ;
341
355
342
356
// (we need this for downstream impl of trait Error)
357
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
343
358
impl fmt:: Display for AllocErr {
344
359
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
345
360
f. write_str ( "memory allocation failed" )
@@ -350,16 +365,19 @@ impl fmt::Display for AllocErr {
350
365
/// `shrink_in_place` were unable to reuse the given memory block for
351
366
/// a requested layout.
352
367
// FIXME: should this be in libcore or liballoc?
368
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
353
369
#[ derive( Clone , PartialEq , Eq , Debug ) ]
354
370
pub struct CannotReallocInPlace ;
355
371
372
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
356
373
impl CannotReallocInPlace {
357
374
pub fn description ( & self ) -> & str {
358
375
"cannot reallocate allocator's memory in place"
359
376
}
360
377
}
361
378
362
379
// (we need this for downstream impl of trait Error)
380
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
363
381
impl fmt:: Display for CannotReallocInPlace {
364
382
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
365
383
write ! ( f, "{}" , self . description( ) )
@@ -449,6 +467,7 @@ impl From<LayoutErr> for CollectionAllocErr {
449
467
/// * `Layout` queries and calculations in general must be correct. Callers of
450
468
/// this trait are allowed to rely on the contracts defined on each method,
451
469
/// and implementors must ensure such contracts remain true.
470
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
452
471
pub unsafe trait GlobalAlloc {
453
472
/// Allocate memory as described by the given `layout`.
454
473
///
@@ -664,6 +683,7 @@ pub unsafe trait GlobalAlloc {
664
683
///
665
684
/// Note that this list may get tweaked over time as clarifications are made in
666
685
/// the future.
686
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
667
687
pub unsafe trait Alloc {
668
688
669
689
// (Note: some existing allocators have unspecified but well-defined
0 commit comments