@@ -27,19 +27,15 @@ use crate::ptr::{self, NonNull};
2727/// that may be due to resource exhaustion or to
2828/// something wrong when combining the given input arguments with this
2929/// allocator.
30- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
30+ #[ stable ( feature = "min_allocator " , since = "CURRENT_RUSTC_VERSION " ) ]
3131#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
3232pub struct AllocError ;
3333
34- #[ unstable(
35- feature = "allocator_api" ,
36- reason = "the precise API and guarantees it provides may be tweaked." ,
37- issue = "32838"
38- ) ]
34+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
3935impl Error for AllocError { }
4036
4137// (we need this for downstream impl of trait Error)
42- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
38+ #[ stable ( feature = "min_allocator " , since = "CURRENT_RUSTC_VERSION " ) ]
4339impl fmt:: Display for AllocError {
4440 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
4541 f. write_str ( "memory allocation failed" )
@@ -100,15 +96,29 @@ impl fmt::Display for AllocError {
10096/// A memory block which is [*currently allocated*] may be passed to
10197/// any method of the allocator that accepts such an argument.
10298///
99+ /// No [*currently allocated*] memory may be accessed by the allocator except
100+ /// through the pointer passed back in within `grow`, `shrink`, and `deallocate`
101+ /// until that memory has been deallocated.
102+ ///
103+ /// Implementors of `Allocator` which are also [`PartialEq`] must ensure equality
104+ /// guarantees that memory allocated with one can be freed with the other.
105+ ///
106+ /// Behaviour when unwinding out of any of the allocating or deallocating
107+ /// functions is undefined.
108+ ///
103109/// Additionally, any memory block returned by the allocator must
104110/// satisfy the allocation invariants described in `core::ptr`.
105111/// In particular, if a block has base address `p` and size `n`,
106112/// then `p as usize + n <= usize::MAX` must hold.
107113///
108114/// This ensures that pointer arithmetic within the allocation
109115/// (for example, `ptr.add(len)`) cannot overflow the address space.
116+ ///
117+ /// Note that some of the stated safety requirements are still experimental and
118+ /// may be relaxed in the future.
119+ ///
110120/// [*currently allocated*]: #currently-allocated-memory
111- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
121+ #[ stable ( feature = "min_allocator " , since = "CURRENT_RUSTC_VERSION " ) ]
112122#[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
113123pub const unsafe trait Allocator {
114124 /// Attempts to allocate a block of memory.
@@ -129,14 +139,15 @@ pub const unsafe trait Allocator {
129139 /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
130140 /// allocator's size or alignment constraints.
131141 ///
132- /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
142+ /// Implementations are encouraged to return `Err` on memory exhaustion rather than
133143 /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
134144 /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
135145 ///
136146 /// Clients wishing to abort computation in response to an allocation error are encouraged to
137147 /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
138148 ///
139149 /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
150+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
140151 fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > ;
141152
142153 /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized.
@@ -146,14 +157,15 @@ pub const unsafe trait Allocator {
146157 /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
147158 /// allocator's size or alignment constraints.
148159 ///
149- /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
160+ /// Implementations are encouraged to return `Err` on memory exhaustion rather than
150161 /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
151162 /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
152163 ///
153164 /// Clients wishing to abort computation in response to an allocation error are encouraged to
154165 /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
155166 ///
156167 /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
168+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
157169 fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
158170 let ptr = self . allocate ( layout) ?;
159171 // SAFETY: `alloc` returns a valid memory block
@@ -165,11 +177,12 @@ pub const unsafe trait Allocator {
165177 ///
166178 /// # Safety
167179 ///
168- /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, and
169- /// * `layout` must [*fit*] that block of memory.
180+ /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator,
181+ /// * `layout` must [*fit*] that block of memory, and
170182 ///
171183 /// [*currently allocated*]: #currently-allocated-memory
172184 /// [*fit*]: #memory-fitting
185+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
173186 unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) ;
174187
175188 /// Attempts to extend the memory block.
@@ -190,7 +203,7 @@ pub const unsafe trait Allocator {
190203 ///
191204 /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator.
192205 /// * `old_layout` must [*fit*] that block of memory (The `new_layout` argument need not fit it.).
193- /// * `new_layout.size()` must be greater than or equal to `old_layout.size()`.
206+ /// * `new_layout.size()` must be greater than or equal to `old_layout.size()`
194207 ///
195208 /// Note that `new_layout.align()` need not be the same as `old_layout.align()`.
196209 ///
@@ -202,14 +215,15 @@ pub const unsafe trait Allocator {
202215 /// Returns `Err` if the new layout does not meet the allocator's size and alignment
203216 /// constraints of the allocator, or if growing otherwise fails.
204217 ///
205- /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
218+ /// Implementations are encouraged to return `Err` on memory exhaustion rather than
206219 /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
207220 /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
208221 ///
209222 /// Clients wishing to abort computation in response to an allocation error are encouraged to
210223 /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
211224 ///
212225 /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
226+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
213227 unsafe fn grow (
214228 & self ,
215229 ptr : NonNull < u8 > ,
@@ -265,14 +279,15 @@ pub const unsafe trait Allocator {
265279 /// Returns `Err` if the new layout does not meet the allocator's size and alignment
266280 /// constraints of the allocator, or if growing otherwise fails.
267281 ///
268- /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
282+ /// Implementations are encouraged to return `Err` on memory exhaustion rather than
269283 /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
270284 /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
271285 ///
272286 /// Clients wishing to abort computation in response to an allocation error are encouraged to
273287 /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
274288 ///
275289 /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
290+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
276291 unsafe fn grow_zeroed (
277292 & self ,
278293 ptr : NonNull < u8 > ,
@@ -329,14 +344,15 @@ pub const unsafe trait Allocator {
329344 /// Returns `Err` if the new layout does not meet the allocator's size and alignment
330345 /// constraints of the allocator, or if shrinking otherwise fails.
331346 ///
332- /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
347+ /// Implementations are encouraged to return `Err` on memory exhaustion rather than
333348 /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
334349 /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
335350 ///
336351 /// Clients wishing to abort computation in response to an allocation error are encouraged to
337352 /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
338353 ///
339354 /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
355+ #[ stable( feature = "min_allocator" , since = "CURRENT_RUSTC_VERSION" ) ]
340356 unsafe fn shrink (
341357 & self ,
342358 ptr : NonNull < u8 > ,
@@ -366,16 +382,22 @@ pub const unsafe trait Allocator {
366382 /// Creates a "by reference" adapter for this instance of `Allocator`.
367383 ///
368384 /// The returned adapter also implements `Allocator` and will simply borrow this.
385+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
369386 #[ inline( always) ]
370387 fn by_ref ( & self ) -> & Self
371388 where
372389 Self : Sized ,
373390 {
374391 self
375392 }
393+
394+ /// Dummy function. Used to make `Allocator` non-`dyn` compatible for now.
395+ #[ doc( hidden) ]
396+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
397+ fn dummy_no_dyn ( ) { }
376398}
377399
378- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
400+ #[ stable ( feature = "min_allocator " , since = "CURRENT_RUSTC_VERSION " ) ]
379401#[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
380402unsafe impl < A > const Allocator for & A
381403where
@@ -431,7 +453,7 @@ where
431453 }
432454}
433455
434- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
456+ #[ stable ( feature = "min_allocator " , since = "CURRENT_RUSTC_VERSION " ) ]
435457unsafe impl < A > Allocator for & mut A
436458where
437459 A : Allocator + ?Sized ,
0 commit comments