1
- #[ cfg( rayon_hash_unstable) ] pub use std:: alloc:: oom;
2
- #[ cfg( not( rayon_hash_unstable) ) ] pub use std:: process:: abort as oom;
1
+ // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2
+ // file at the top-level directory of this distribution and at
3
+ // http://rust-lang.org/COPYRIGHT.
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8
+ // option. This file may not be copied, modified, or distributed
9
+ // except according to those terms.
10
+
11
+ //! Memory allocation APIs (forked from `libcore/alloc.rs`)
12
+
13
+ // #![stable(feature = "alloc_module", since = "1.28.0")]
14
+
15
+ use std:: cmp;
16
+ use std:: mem;
17
+ use std:: usize;
18
+ use std:: alloc:: { self , LayoutErr } ;
19
+
20
+ #[ inline]
21
+ fn layout_err ( ) -> LayoutErr {
22
+ alloc:: Layout :: from_size_align ( usize:: MAX , usize:: MAX ) . unwrap_err ( )
23
+ }
24
+
25
+ /// Layout of a block of memory.
26
+ ///
27
+ /// An instance of `Layout` describes a particular layout of memory.
28
+ /// You build a `Layout` up as an input to give to an allocator.
29
+ ///
30
+ /// All layouts have an associated non-negative size and a
31
+ /// power-of-two alignment.
32
+ ///
33
+ /// (Note however that layouts are *not* required to have positive
34
+ /// size, even though many allocators require that all memory
35
+ /// requests have positive size. A caller to the `Alloc::alloc`
36
+ /// method must either ensure that conditions like this are met, or
37
+ /// use specific allocators with looser requirements.)
38
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
39
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
40
+ pub ( crate ) struct Layout {
41
+ inner : alloc:: Layout ,
42
+ }
43
+
44
+ impl Layout {
45
+ /// Constructs a `Layout` from a given `size` and `align`,
46
+ /// or returns `LayoutErr` if either of the following conditions
47
+ /// are not met:
48
+ ///
49
+ /// * `align` must be a power of two,
50
+ ///
51
+ /// * `size`, when rounded up to the nearest multiple of `align`,
52
+ /// must not overflow (i.e. the rounded value must be less than
53
+ /// `usize::MAX`).
54
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
55
+ #[ inline]
56
+ pub ( crate ) fn from_size_align ( size : usize , align : usize ) -> Result < Self , LayoutErr > {
57
+ alloc:: Layout :: from_size_align ( size, align) . map ( Layout :: from)
58
+ }
59
+
60
+ /// Creates a layout, bypassing all checks.
61
+ ///
62
+ /// # Safety
63
+ ///
64
+ /// This function is unsafe as it does not verify the preconditions from
65
+ /// [`Layout::from_size_align`](#method.from_size_align).
66
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
67
+ #[ inline]
68
+ pub ( crate ) unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
69
+ Layout :: from ( alloc:: Layout :: from_size_align_unchecked ( size, align) )
70
+ }
71
+
72
+ /// The minimum size in bytes for a memory block of this layout.
73
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
74
+ #[ inline]
75
+ pub ( crate ) fn size ( & self ) -> usize { self . inner . size ( ) }
76
+
77
+ /// The minimum byte alignment for a memory block of this layout.
78
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
79
+ #[ inline]
80
+ pub ( crate ) fn align ( & self ) -> usize { self . inner . align ( ) }
81
+
82
+ /// Constructs a `Layout` suitable for holding a value of type `T`.
83
+ // #[stable(feature = "alloc_layout", since = "1.28.0")]
84
+ #[ inline]
85
+ pub ( crate ) fn new < T > ( ) -> Self {
86
+ Layout :: from ( alloc:: Layout :: new :: < T > ( ) )
87
+ }
88
+
89
+ /// Returns the amount of padding we must insert after `self`
90
+ /// to ensure that the following address will satisfy `align`
91
+ /// (measured in bytes).
92
+ ///
93
+ /// E.g. if `self.size()` is 9, then `self.padding_needed_for(4)`
94
+ /// returns 3, because that is the minimum number of bytes of
95
+ /// padding required to get a 4-aligned address (assuming that the
96
+ /// corresponding memory block starts at a 4-aligned address).
97
+ ///
98
+ /// The return value of this function has no meaning if `align` is
99
+ /// not a power-of-two.
100
+ ///
101
+ /// Note that the utility of the returned value requires `align`
102
+ /// to be less than or equal to the alignment of the starting
103
+ /// address for the whole allocated block of memory. One way to
104
+ /// satisfy this constraint is to ensure `align <= self.align()`.
105
+ // #[unstable(feature = "allocator_api", issue = "32838")]
106
+ #[ inline]
107
+ pub ( crate ) fn padding_needed_for ( & self , align : usize ) -> usize {
108
+ let len = self . size ( ) ;
109
+
110
+ // Rounded up value is:
111
+ // len_rounded_up = (len + align - 1) & !(align - 1);
112
+ // and then we return the padding difference: `len_rounded_up - len`.
113
+ //
114
+ // We use modular arithmetic throughout:
115
+ //
116
+ // 1. align is guaranteed to be > 0, so align - 1 is always
117
+ // valid.
118
+ //
119
+ // 2. `len + align - 1` can overflow by at most `align - 1`,
120
+ // so the &-mask wth `!(align - 1)` will ensure that in the
121
+ // case of overflow, `len_rounded_up` will itself be 0.
122
+ // Thus the returned padding, when added to `len`, yields 0,
123
+ // which trivially satisfies the alignment `align`.
124
+ //
125
+ // (Of course, attempts to allocate blocks of memory whose
126
+ // size and padding overflow in the above manner should cause
127
+ // the allocator to yield an error anyway.)
128
+
129
+ let len_rounded_up = len. wrapping_add ( align) . wrapping_sub ( 1 )
130
+ & !align. wrapping_sub ( 1 ) ;
131
+ return len_rounded_up. wrapping_sub ( len) ;
132
+ }
133
+
134
+ /// Creates a layout describing the record for `n` instances of
135
+ /// `self`, with a suitable amount of padding between each to
136
+ /// ensure that each instance is given its requested size and
137
+ /// alignment. On success, returns `(k, offs)` where `k` is the
138
+ /// layout of the array and `offs` is the distance between the start
139
+ /// of each element in the array.
140
+ ///
141
+ /// On arithmetic overflow, returns `LayoutErr`.
142
+ // #[unstable(feature = "allocator_api", issue = "32838")]
143
+ #[ inline]
144
+ pub ( crate ) fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutErr > {
145
+ let padded_size = self . size ( ) . checked_add ( self . padding_needed_for ( self . align ( ) ) )
146
+ . ok_or ( layout_err ( ) ) ?;
147
+ let alloc_size = padded_size. checked_mul ( n)
148
+ . ok_or ( layout_err ( ) ) ?;
149
+
150
+ unsafe {
151
+ // self.align is already known to be valid and alloc_size has been
152
+ // padded already.
153
+ Ok ( ( Layout :: from_size_align_unchecked ( alloc_size, self . align ( ) ) , padded_size) )
154
+ }
155
+ }
156
+
157
+ /// Creates a layout describing the record for `self` followed by
158
+ /// `next`, including any necessary padding to ensure that `next`
159
+ /// will be properly aligned. Note that the result layout will
160
+ /// satisfy the alignment properties of both `self` and `next`.
161
+ ///
162
+ /// Returns `Some((k, offset))`, where `k` is layout of the concatenated
163
+ /// record and `offset` is the relative location, in bytes, of the
164
+ /// start of the `next` embedded within the concatenated record
165
+ /// (assuming that the record itself starts at offset 0).
166
+ ///
167
+ /// On arithmetic overflow, returns `LayoutErr`.
168
+ // #[unstable(feature = "allocator_api", issue = "32838")]
169
+ #[ inline]
170
+ pub ( crate ) fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutErr > {
171
+ let new_align = cmp:: max ( self . align ( ) , next. align ( ) ) ;
172
+ let pad = self . padding_needed_for ( next. align ( ) ) ;
173
+
174
+ let offset = self . size ( ) . checked_add ( pad)
175
+ . ok_or ( layout_err ( ) ) ?;
176
+ let new_size = offset. checked_add ( next. size ( ) )
177
+ . ok_or ( layout_err ( ) ) ?;
178
+
179
+ let layout = Layout :: from_size_align ( new_size, new_align) ?;
180
+ Ok ( ( layout, offset) )
181
+ }
182
+
183
+ /// Creates a layout describing the record for a `[T; n]`.
184
+ ///
185
+ /// On arithmetic overflow, returns `LayoutErr`.
186
+ // #[unstable(feature = "allocator_api", issue = "32838")]
187
+ #[ inline]
188
+ pub ( crate ) fn array < T > ( n : usize ) -> Result < Self , LayoutErr > {
189
+ Layout :: new :: < T > ( )
190
+ . repeat ( n)
191
+ . map ( |( k, offs) | {
192
+ debug_assert ! ( offs == mem:: size_of:: <T >( ) ) ;
193
+ k
194
+ } )
195
+ }
196
+ }
197
+
198
+ impl From < alloc:: Layout > for Layout {
199
+ #[ inline]
200
+ fn from ( inner : alloc:: Layout ) -> Layout {
201
+ Layout { inner }
202
+ }
203
+ }
204
+
205
+ impl From < Layout > for alloc:: Layout {
206
+ #[ inline]
207
+ fn from ( layout : Layout ) -> alloc:: Layout {
208
+ layout. inner
209
+ }
210
+ }
3
211
4
212
/// Augments `AllocErr` with a CapacityOverflow variant.
213
+ // FIXME: should this be in libcore or liballoc?
5
214
#[ derive( Clone , PartialEq , Eq , Debug ) ]
6
215
// #[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
7
216
pub enum CollectionAllocErr {
@@ -11,3 +220,11 @@ pub enum CollectionAllocErr {
11
220
/// Error due to the allocator (see the `AllocErr` type's docs).
12
221
AllocErr ,
13
222
}
223
+
224
+ // #[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
225
+ impl From < LayoutErr > for CollectionAllocErr {
226
+ #[ inline]
227
+ fn from ( _: LayoutErr ) -> Self {
228
+ CollectionAllocErr :: CapacityOverflow
229
+ }
230
+ }
0 commit comments