1
1
use core:: marker:: PhantomData ;
2
- use core:: mem:: ManuallyDrop ;
3
- use ptr;
4
- use ffi:: { self , CPtr } ;
2
+ use core:: mem:: { self , ManuallyDrop } ;
3
+ use ffi:: { self , CPtr , types:: AlignedType } ;
5
4
use ffi:: types:: { c_uint, c_void} ;
6
5
use Error ;
7
6
use Secp256k1 ;
@@ -50,7 +49,7 @@ pub unsafe trait Context : private::Sealed {
50
49
/// A constant description of the context.
51
50
const DESCRIPTION : & ' static str ;
52
51
/// A function to deallocate the memory when the context is dropped.
53
- unsafe fn deallocate ( ptr : * mut [ u8 ] ) ;
52
+ unsafe fn deallocate ( ptr : * mut u8 , size : usize ) ;
54
53
}
55
54
56
55
/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
@@ -93,6 +92,8 @@ mod std_only {
93
92
impl private:: Sealed for VerifyOnly { }
94
93
95
94
use super :: * ;
95
+ use std:: alloc;
96
+ const ALIGN_TO : usize = mem:: align_of :: < AlignedType > ( ) ;
96
97
97
98
/// Represents the set of capabilities needed for signing.
98
99
pub enum SignOnly { }
@@ -113,26 +114,29 @@ mod std_only {
113
114
const FLAGS : c_uint = ffi:: SECP256K1_START_SIGN ;
114
115
const DESCRIPTION : & ' static str = "signing only" ;
115
116
116
- unsafe fn deallocate ( ptr : * mut [ u8 ] ) {
117
- let _ = Box :: from_raw ( ptr) ;
117
+ unsafe fn deallocate ( ptr : * mut u8 , size : usize ) {
118
+ let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
119
+ alloc:: dealloc ( ptr, layout) ;
118
120
}
119
121
}
120
122
121
123
unsafe impl Context for VerifyOnly {
122
124
const FLAGS : c_uint = ffi:: SECP256K1_START_VERIFY ;
123
125
const DESCRIPTION : & ' static str = "verification only" ;
124
126
125
- unsafe fn deallocate ( ptr : * mut [ u8 ] ) {
126
- let _ = Box :: from_raw ( ptr) ;
127
+ unsafe fn deallocate ( ptr : * mut u8 , size : usize ) {
128
+ let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
129
+ alloc:: dealloc ( ptr, layout) ;
127
130
}
128
131
}
129
132
130
133
unsafe impl Context for All {
131
134
const FLAGS : c_uint = VerifyOnly :: FLAGS | SignOnly :: FLAGS ;
132
135
const DESCRIPTION : & ' static str = "all capabilities" ;
133
136
134
- unsafe fn deallocate ( ptr : * mut [ u8 ] ) {
135
- let _ = Box :: from_raw ( ptr) ;
137
+ unsafe fn deallocate ( ptr : * mut u8 , size : usize ) {
138
+ let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
139
+ alloc:: dealloc ( ptr, layout) ;
136
140
}
137
141
}
138
142
@@ -142,12 +146,13 @@ mod std_only {
142
146
#[ cfg( target_arch = "wasm32" ) ]
143
147
ffi:: types:: sanity_checks_for_wasm ( ) ;
144
148
145
- let buf = vec ! [ 0u8 ; Self :: preallocate_size_gen( ) ] . into_boxed_slice ( ) ;
146
- let ptr = Box :: into_raw ( buf) ;
149
+ let size = unsafe { ffi:: secp256k1_context_preallocated_size ( C :: FLAGS ) } ;
150
+ let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
151
+ let ptr = unsafe { alloc:: alloc ( layout) } ;
147
152
Secp256k1 {
148
153
ctx : unsafe { ffi:: secp256k1_context_preallocated_create ( ptr as * mut c_void , C :: FLAGS ) } ,
149
154
phantom : PhantomData ,
150
- buf : ptr ,
155
+ size ,
151
156
}
152
157
}
153
158
}
@@ -181,12 +186,13 @@ mod std_only {
181
186
182
187
impl < C : Context > Clone for Secp256k1 < C > {
183
188
fn clone ( & self ) -> Secp256k1 < C > {
184
- let clone_size = unsafe { ffi:: secp256k1_context_preallocated_clone_size ( self . ctx ) } ;
185
- let ptr_buf = Box :: into_raw ( vec ! [ 0u8 ; clone_size] . into_boxed_slice ( ) ) ;
189
+ let size = unsafe { ffi:: secp256k1_context_preallocated_clone_size ( self . ctx as _ ) } ;
190
+ let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
191
+ let ptr = unsafe { alloc:: alloc ( layout) } ;
186
192
Secp256k1 {
187
- ctx : unsafe { ffi:: secp256k1_context_preallocated_clone ( self . ctx , ptr_buf as * mut c_void ) } ,
193
+ ctx : unsafe { ffi:: secp256k1_context_preallocated_clone ( self . ctx , ptr as * mut c_void ) } ,
188
194
phantom : PhantomData ,
189
- buf : ptr_buf ,
195
+ size ,
190
196
}
191
197
}
192
198
}
@@ -202,7 +208,7 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
202
208
const FLAGS : c_uint = ffi:: SECP256K1_START_SIGN ;
203
209
const DESCRIPTION : & ' static str = "signing only" ;
204
210
205
- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
211
+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
206
212
// Allocated by the user
207
213
}
208
214
}
@@ -211,7 +217,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
211
217
const FLAGS : c_uint = ffi:: SECP256K1_START_VERIFY ;
212
218
const DESCRIPTION : & ' static str = "verification only" ;
213
219
214
- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
220
+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
215
221
// Allocated by the user
216
222
}
217
223
}
@@ -220,7 +226,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
220
226
const FLAGS : c_uint = SignOnlyPreallocated :: FLAGS | VerifyOnlyPreallocated :: FLAGS ;
221
227
const DESCRIPTION : & ' static str = "all capabilities" ;
222
228
223
- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
229
+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
224
230
// Allocated by the user
225
231
}
226
232
}
@@ -241,7 +247,7 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
241
247
C :: FLAGS )
242
248
} ,
243
249
phantom : PhantomData ,
244
- buf : buf as * mut [ u8 ] ,
250
+ size : 0 , // We don't care about the size because it's the caller responsibility to deallocate.
245
251
} )
246
252
}
247
253
}
@@ -271,7 +277,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
271
277
ManuallyDrop :: new ( Secp256k1 {
272
278
ctx : raw_ctx,
273
279
phantom : PhantomData ,
274
- buf : ptr :: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
280
+ size : 0 , // We don't care about the size because it's the caller responsibility to deallocate.
275
281
} )
276
282
}
277
283
}
@@ -303,7 +309,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
303
309
ManuallyDrop :: new ( Secp256k1 {
304
310
ctx : raw_ctx,
305
311
phantom : PhantomData ,
306
- buf : ptr :: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
312
+ size : 0 , // We don't care about the size because it's the caller responsibility to deallocate.
307
313
} )
308
314
}
309
315
}
@@ -335,7 +341,7 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
335
341
ManuallyDrop :: new ( Secp256k1 {
336
342
ctx : raw_ctx,
337
343
phantom : PhantomData ,
338
- buf : ptr :: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
344
+ size : 0 , // We don't care about the size because it's the caller responsibility to deallocate.
339
345
} )
340
346
}
341
347
}
0 commit comments