1010
1111//! Memory allocation APIs
1212
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" ) ]
1914
2015use cmp;
2116use fmt;
2217use mem;
2318use usize;
2419use ptr:: { self , NonNull } ;
2520
21+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
2622#[ cfg( stage0) ]
2723pub type Opaque = u8 ;
2824
2925/// Represents the combination of a starting address and
3026/// a total capacity of the returned block.
27+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
3128#[ derive( Debug ) ]
3229pub struct Excess ( pub NonNull < u8 > , pub usize ) ;
3330
@@ -48,6 +45,7 @@ fn size_align<T>() -> (usize, usize) {
4845/// requests have positive size. A caller to the `Alloc::alloc`
4946/// method must either ensure that conditions like this are met, or
5047/// use specific allocators with looser requirements.)
48+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
5149#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
5250pub struct Layout {
5351 // size of the requested block of memory, measured in bytes.
@@ -78,6 +76,7 @@ impl Layout {
7876 /// * `size`, when rounded up to the nearest multiple of `align`,
7977 /// must not overflow (i.e. the rounded value must be less than
8078 /// `usize::MAX`).
79+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
8180 #[ inline]
8281 pub fn from_size_align ( size : usize , align : usize ) -> Result < Self , LayoutErr > {
8382 if !align. is_power_of_two ( ) {
@@ -114,20 +113,24 @@ impl Layout {
114113 /// This function is unsafe as it does not verify that `align` is
115114 /// a power-of-two nor `size` aligned to `align` fits within the
116115 /// address space (i.e. the `Layout::from_size_align` preconditions).
116+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
117117 #[ inline]
118118 pub unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
119119 Layout { size : size, align : align }
120120 }
121121
122122 /// The minimum size in bytes for a memory block of this layout.
123+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
123124 #[ inline]
124125 pub fn size ( & self ) -> usize { self . size }
125126
126127 /// The minimum byte alignment for a memory block of this layout.
128+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
127129 #[ inline]
128130 pub fn align ( & self ) -> usize { self . align }
129131
130132 /// Constructs a `Layout` suitable for holding a value of type `T`.
133+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
131134 pub fn new < T > ( ) -> Self {
132135 let ( size, align) = size_align :: < T > ( ) ;
133136 // Note that the align is guaranteed by rustc to be a power of two and
@@ -143,6 +146,7 @@ impl Layout {
143146 /// Produces layout describing a record that could be used to
144147 /// allocate backing structure for `T` (which could be a trait
145148 /// or other unsized type like a slice).
149+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
146150 pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
147151 let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
148152 // See rationale in `new` for why this us using an unsafe variant below
@@ -168,6 +172,7 @@ impl Layout {
168172 ///
169173 /// Panics if the combination of `self.size` and the given `align`
170174 /// violates the conditions listed in `from_size_align`.
175+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
171176 #[ inline]
172177 pub fn align_to ( & self , align : usize ) -> Self {
173178 Layout :: from_size_align ( self . size , cmp:: max ( self . align , align) ) . unwrap ( )
@@ -189,6 +194,7 @@ impl Layout {
189194 /// to be less than or equal to the alignment of the starting
190195 /// address for the whole allocated block of memory. One way to
191196 /// satisfy this constraint is to ensure `align <= self.align`.
197+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
192198 #[ inline]
193199 pub fn padding_needed_for ( & self , align : usize ) -> usize {
194200 let len = self . size ( ) ;
@@ -224,6 +230,7 @@ impl Layout {
224230 /// of each element in the array.
225231 ///
226232 /// On arithmetic overflow, returns `None`.
233+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
227234 #[ inline]
228235 pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutErr > {
229236 let padded_size = self . size . checked_add ( self . padding_needed_for ( self . align ) )
@@ -244,6 +251,7 @@ impl Layout {
244251 /// (assuming that the record itself starts at offset 0).
245252 ///
246253 /// On arithmetic overflow, returns `None`.
254+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
247255 pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
248256 let new_align = cmp:: max ( self . align , next. align ) ;
249257 let realigned = Layout :: from_size_align ( self . size , new_align) ?;
@@ -271,6 +279,7 @@ impl Layout {
271279 /// aligned.
272280 ///
273281 /// On arithmetic overflow, returns `None`.
282+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
274283 pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutErr > {
275284 let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutErr { private : ( ) } ) ?;
276285 Layout :: from_size_align ( size, self . align )
@@ -291,6 +300,7 @@ impl Layout {
291300 /// `extend`.)
292301 ///
293302 /// On arithmetic overflow, returns `None`.
303+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
294304 pub fn extend_packed ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
295305 let new_size = self . size ( ) . checked_add ( next. size ( ) )
296306 . ok_or ( LayoutErr { private : ( ) } ) ?;
@@ -301,6 +311,7 @@ impl Layout {
301311 /// Creates a layout describing the record for a `[T; n]`.
302312 ///
303313 /// On arithmetic overflow, returns `None`.
314+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
304315 pub fn array < T > ( n : usize ) -> Result < Self , LayoutErr > {
305316 Layout :: new :: < T > ( )
306317 . repeat ( n)
@@ -314,12 +325,14 @@ impl Layout {
314325/// The parameters given to `Layout::from_size_align`
315326/// or some other `Layout` constructor
316327/// do not satisfy its documented constraints.
328+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
317329#[ derive( Clone , PartialEq , Eq , Debug ) ]
318330pub struct LayoutErr {
319331 private : ( )
320332}
321333
322334// (we need this for downstream impl of trait Error)
335+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
323336impl fmt:: Display for LayoutErr {
324337 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
325338 f. write_str ( "invalid parameters to Layout::from_size_align" )
@@ -330,10 +343,12 @@ impl fmt::Display for LayoutErr {
330343/// that may be due to resource exhaustion or to
331344/// something wrong when combining the given input arguments with this
332345/// allocator.
346+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
333347#[ derive( Clone , PartialEq , Eq , Debug ) ]
334348pub struct AllocErr ;
335349
336350// (we need this for downstream impl of trait Error)
351+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
337352impl fmt:: Display for AllocErr {
338353 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
339354 f. write_str ( "memory allocation failed" )
@@ -344,16 +359,19 @@ impl fmt::Display for AllocErr {
344359/// `shrink_in_place` were unable to reuse the given memory block for
345360/// a requested layout.
346361// FIXME: should this be in libcore or liballoc?
362+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
347363#[ derive( Clone , PartialEq , Eq , Debug ) ]
348364pub struct CannotReallocInPlace ;
349365
366+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
350367impl CannotReallocInPlace {
351368 pub fn description ( & self ) -> & str {
352369 "cannot reallocate allocator's memory in place"
353370 }
354371}
355372
356373// (we need this for downstream impl of trait Error)
374+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
357375impl fmt:: Display for CannotReallocInPlace {
358376 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
359377 write ! ( f, "{}" , self . description( ) )
@@ -435,6 +453,7 @@ impl From<AllocErr> for CollectionAllocErr {
435453/// * `Layout` queries and calculations in general must be correct. Callers of
436454/// this trait are allowed to rely on the contracts defined on each method,
437455/// and implementors must ensure such contracts remain true.
456+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
438457pub unsafe trait GlobalAlloc {
439458 /// Allocate memory as described by the given `layout`.
440459 ///
@@ -650,6 +669,7 @@ pub unsafe trait GlobalAlloc {
650669///
651670/// Note that this list may get tweaked over time as clarifications are made in
652671/// the future.
672+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
653673pub unsafe trait Alloc {
654674
655675 // (Note: some existing allocators have unspecified but well-defined
0 commit comments