8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use core:: ptr:: Unique ;
11
+ use core:: ptr:: { self , Unique } ;
12
12
use core:: mem;
13
13
use core:: slice:: { self , SliceExt } ;
14
+ use core_alloc:: Allocator ;
14
15
use super :: heap;
16
+ use super :: heap:: Allocator as HeapAllocator ;
15
17
use super :: oom;
16
18
use super :: boxed:: Box ;
17
19
use core:: ops:: Drop ;
@@ -46,17 +48,42 @@ use core;
46
48
/// field. This allows zero-sized types to not be special-cased by consumers of
47
49
/// this type.
48
50
#[ unsafe_no_drop_flag]
49
- pub struct RawVec < T > {
51
+ pub struct RawVec < T , A = HeapAllocator > where A : Allocator {
50
52
ptr : Unique < T > ,
51
53
cap : usize ,
54
+ a : A ,
52
55
}
53
56
54
57
impl < T > RawVec < T > {
58
+ /// DOC TODO
59
+ pub fn new ( ) -> Self { RawVec :: new_in ( HeapAllocator ) }
60
+
61
+ /// DOC TODO
62
+ pub fn with_capacity ( cap : usize ) -> Self {
63
+ RawVec :: with_capacity_in ( cap, HeapAllocator )
64
+ }
65
+
66
+ /// DOC TODO
67
+ pub unsafe fn from_raw_parts ( ptr : * mut T , cap : usize ) -> Self {
68
+ RawVec :: from_raw_parts_in ( ptr, cap, HeapAllocator )
69
+ }
70
+
71
+ /// Converts a `Box<[T]>` into a `RawVec<T>`.
72
+ pub fn from_box ( mut slice : Box < [ T ] > ) -> Self {
73
+ unsafe {
74
+ let result = RawVec :: from_raw_parts ( slice. as_mut_ptr ( ) , slice. len ( ) ) ;
75
+ mem:: forget ( slice) ;
76
+ result
77
+ }
78
+ }
79
+ }
80
+
81
+ impl < T , A > RawVec < T , A > where A : Allocator {
55
82
/// Creates the biggest possible RawVec without allocating. If T has positive
56
83
/// size, then this makes a RawVec with capacity 0. If T has 0 size, then it
57
84
/// it makes a RawVec with capacity `usize::MAX`. Useful for implementing
58
85
/// delayed allocation.
59
- pub fn new ( ) -> Self {
86
+ pub fn new_in ( a : A ) -> Self {
60
87
unsafe {
61
88
// !0 is usize::MAX. This branch should be stripped at compile time.
62
89
let cap = if mem:: size_of :: < T > ( ) == 0 {
@@ -69,6 +96,7 @@ impl<T> RawVec<T> {
69
96
RawVec {
70
97
ptr : Unique :: new ( heap:: EMPTY as * mut T ) ,
71
98
cap : cap,
99
+ a : a,
72
100
}
73
101
}
74
102
}
@@ -87,7 +115,7 @@ impl<T> RawVec<T> {
87
115
/// # Aborts
88
116
///
89
117
/// Aborts on OOM
90
- pub fn with_capacity ( cap : usize ) -> Self {
118
+ pub fn with_capacity_in ( cap : usize , a : A ) -> Self {
91
119
unsafe {
92
120
let elem_size = mem:: size_of :: < T > ( ) ;
93
121
@@ -109,6 +137,7 @@ impl<T> RawVec<T> {
109
137
RawVec {
110
138
ptr : Unique :: new ( ptr as * mut _ ) ,
111
139
cap : cap,
140
+ a : a,
112
141
}
113
142
}
114
143
}
@@ -120,24 +149,16 @@ impl<T> RawVec<T> {
120
149
/// The ptr must be allocated, and with the given capacity. The
121
150
/// capacity cannot exceed `isize::MAX` (only a concern on 32-bit systems).
122
151
/// If the ptr and capacity come from a RawVec, then this is guaranteed.
123
- pub unsafe fn from_raw_parts ( ptr : * mut T , cap : usize ) -> Self {
152
+ pub unsafe fn from_raw_parts_in ( ptr : * mut T , cap : usize , a : A ) -> Self {
124
153
RawVec {
125
154
ptr : Unique :: new ( ptr) ,
126
155
cap : cap,
127
- }
128
- }
129
-
130
- /// Converts a `Box<[T]>` into a `RawVec<T>`.
131
- pub fn from_box ( mut slice : Box < [ T ] > ) -> Self {
132
- unsafe {
133
- let result = RawVec :: from_raw_parts ( slice. as_mut_ptr ( ) , slice. len ( ) ) ;
134
- mem:: forget ( slice) ;
135
- result
156
+ a : a,
136
157
}
137
158
}
138
159
}
139
160
140
- impl < T > RawVec < T > {
161
+ impl < T , A > RawVec < T , A > where A : Allocator {
141
162
/// Gets a raw pointer to the start of the allocation. Note that this is
142
163
/// heap::EMPTY if `cap = 0` or T is zero-sized. In the former case, you must
143
164
/// be careful.
@@ -417,7 +438,8 @@ impl<T> RawVec<T> {
417
438
assert ! ( self . cap >= amount, "Tried to shrink to a larger capacity" ) ;
418
439
419
440
if amount == 0 {
420
- mem:: replace ( self , RawVec :: new ( ) ) ;
441
+ let a: A = unsafe { ptr:: read ( & self . a ) } ;
442
+ mem:: replace ( self , RawVec :: new_in ( a) ) ;
421
443
} else if self . cap != amount {
422
444
unsafe {
423
445
// Overflow check is unnecessary as the vector is already at
@@ -459,7 +481,7 @@ impl<T> RawVec<T> {
459
481
}
460
482
}
461
483
462
- impl < T > Drop for RawVec < T > {
484
+ impl < T , A > Drop for RawVec < T , A > where A : Allocator {
463
485
#[ unsafe_destructor_blind_to_params]
464
486
/// Frees the memory owned by the RawVec *without* trying to Drop its contents.
465
487
fn drop ( & mut self ) {
0 commit comments