11use 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 } ;
54use ffi:: types:: { c_uint, c_void} ;
65use Error ;
76use Secp256k1 ;
@@ -50,7 +49,7 @@ pub unsafe trait Context : private::Sealed {
5049 /// A constant description of the context.
5150 const DESCRIPTION : & ' static str ;
5251 /// 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 ) ;
5453}
5554
5655/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
@@ -93,6 +92,8 @@ mod std_only {
9392 impl private:: Sealed for VerifyOnly { }
9493
9594 use super :: * ;
95+ use std:: alloc;
96+ const ALIGN_TO : usize = mem:: align_of :: < AlignedType > ( ) ;
9697
9798 /// Represents the set of capabilities needed for signing.
9899 pub enum SignOnly { }
@@ -113,26 +114,29 @@ mod std_only {
113114 const FLAGS : c_uint = ffi:: SECP256K1_START_SIGN ;
114115 const DESCRIPTION : & ' static str = "signing only" ;
115116
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) ;
118120 }
119121 }
120122
121123 unsafe impl Context for VerifyOnly {
122124 const FLAGS : c_uint = ffi:: SECP256K1_START_VERIFY ;
123125 const DESCRIPTION : & ' static str = "verification only" ;
124126
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) ;
127130 }
128131 }
129132
130133 unsafe impl Context for All {
131134 const FLAGS : c_uint = VerifyOnly :: FLAGS | SignOnly :: FLAGS ;
132135 const DESCRIPTION : & ' static str = "all capabilities" ;
133136
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) ;
136140 }
137141 }
138142
@@ -142,12 +146,13 @@ mod std_only {
142146 #[ cfg( target_arch = "wasm32" ) ]
143147 ffi:: types:: sanity_checks_for_wasm ( ) ;
144148
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) } ;
147152 Secp256k1 {
148153 ctx : unsafe { ffi:: secp256k1_context_preallocated_create ( ptr as * mut c_void , C :: FLAGS ) } ,
149154 phantom : PhantomData ,
150- buf : ptr ,
155+ size ,
151156 }
152157 }
153158 }
@@ -181,12 +186,13 @@ mod std_only {
181186
182187 impl < C : Context > Clone for Secp256k1 < C > {
183188 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) } ;
186192 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 ) } ,
188194 phantom : PhantomData ,
189- buf : ptr_buf ,
195+ size ,
190196 }
191197 }
192198 }
@@ -202,7 +208,7 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
202208 const FLAGS : c_uint = ffi:: SECP256K1_START_SIGN ;
203209 const DESCRIPTION : & ' static str = "signing only" ;
204210
205- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
211+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
206212 // Allocated by the user
207213 }
208214}
@@ -211,7 +217,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
211217 const FLAGS : c_uint = ffi:: SECP256K1_START_VERIFY ;
212218 const DESCRIPTION : & ' static str = "verification only" ;
213219
214- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
220+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
215221 // Allocated by the user
216222 }
217223}
@@ -220,14 +226,14 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
220226 const FLAGS : c_uint = SignOnlyPreallocated :: FLAGS | VerifyOnlyPreallocated :: FLAGS ;
221227 const DESCRIPTION : & ' static str = "all capabilities" ;
222228
223- unsafe fn deallocate ( _ptr : * mut [ u8 ] ) {
229+ unsafe fn deallocate ( _ptr : * mut u8 , _size : usize ) {
224230 // Allocated by the user
225231 }
226232}
227233
228234impl < ' buf , C : Context + ' buf > Secp256k1 < C > {
229235 /// 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 > {
231237 #[ cfg( target_arch = "wasm32" ) ]
232238 ffi:: types:: sanity_checks_for_wasm ( ) ;
233239
@@ -241,14 +247,14 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
241247 C :: FLAGS )
242248 } ,
243249 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.
245251 } )
246252 }
247253}
248254
249255impl < ' buf > Secp256k1 < AllPreallocated < ' buf > > {
250256 /// 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 > {
252258 Secp256k1 :: preallocated_gen_new ( buf)
253259 }
254260 /// 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>> {
271277 ManuallyDrop :: new ( Secp256k1 {
272278 ctx : raw_ctx,
273279 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.
275281 } )
276282 }
277283}
278284
279285impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
280286 /// 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 > {
282288 Secp256k1 :: preallocated_gen_new ( buf)
283289 }
284290
@@ -303,14 +309,14 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
303309 ManuallyDrop :: new ( Secp256k1 {
304310 ctx : raw_ctx,
305311 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.
307313 } )
308314 }
309315}
310316
311317impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
312318 /// 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 > {
314320 Secp256k1 :: preallocated_gen_new ( buf)
315321 }
316322
@@ -335,7 +341,7 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
335341 ManuallyDrop :: new ( Secp256k1 {
336342 ctx : raw_ctx,
337343 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.
339345 } )
340346 }
341347}
0 commit comments