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,14 +226,14 @@ 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
}
227
233
228
234
impl < ' buf , C : Context + ' buf > Secp256k1 < C > {
229
235
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
230
- pub fn preallocated_gen_new ( buf : & ' buf mut [ u8 ] ) -> Result < Secp256k1 < C > , Error > {
236
+ pub fn preallocated_gen_new ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < C > , Error > {
231
237
#[ cfg( target_arch = "wasm32" ) ]
232
238
ffi:: types:: sanity_checks_for_wasm ( ) ;
233
239
@@ -241,14 +247,14 @@ 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
}
248
254
249
255
impl < ' buf > Secp256k1 < AllPreallocated < ' buf > > {
250
256
/// Creates a new Secp256k1 context with all capabilities
251
- pub fn preallocated_new ( buf : & ' buf mut [ u8 ] ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , Error > {
257
+ pub fn preallocated_new ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , Error > {
252
258
Secp256k1 :: preallocated_gen_new ( buf)
253
259
}
254
260
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
@@ -271,14 +277,14 @@ 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
}
278
284
279
285
impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
280
286
/// Creates a new Secp256k1 context that can only be used for signing
281
- pub fn preallocated_signing_only ( buf : & ' buf mut [ u8 ] ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , Error > {
287
+ pub fn preallocated_signing_only ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , Error > {
282
288
Secp256k1 :: preallocated_gen_new ( buf)
283
289
}
284
290
@@ -303,14 +309,14 @@ 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
}
310
316
311
317
impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
312
318
/// Creates a new Secp256k1 context that can only be used for verification
313
- pub fn preallocated_verification_only ( buf : & ' buf mut [ u8 ] ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , Error > {
319
+ pub fn preallocated_verification_only ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , Error > {
314
320
Secp256k1 :: preallocated_gen_new ( buf)
315
321
}
316
322
@@ -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