Remove size field

The `Secp256k1` `size` field is a cached value that we get using
`ffi::secp256k1_context_preallocated_size` or
`ffi::secp256k1_context_preallocated_clone_size`, both of which just
return the result of `sizeof(rustsecp256k1_v0_6_1_context)`. Instead of
caching this value we can just call
`ffi::secp256k1_context_preallocated_clone_size` in the `Drop`
implementation.
This commit is contained in:
Tobin C. Harding 2022-11-29 14:17:23 +11:00
parent 4864a33a62
commit 129ba3cd58
2 changed files with 8 additions and 25 deletions

View File

@ -201,7 +201,6 @@ mod alloc_only {
ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS) ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS)
}, },
phantom: PhantomData, phantom: PhantomData,
size,
}; };
#[cfg(all( #[cfg(all(
@ -259,7 +258,6 @@ mod alloc_only {
ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void) ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)
}, },
phantom: PhantomData, phantom: PhantomData,
size,
} }
} }
} }
@ -315,7 +313,6 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
) )
}, },
phantom: PhantomData, phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
}) })
} }
} }
@ -344,11 +341,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
pub unsafe fn from_raw_all( pub unsafe fn from_raw_all(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }
@ -378,11 +371,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
pub unsafe fn from_raw_signing_only( pub unsafe fn from_raw_signing_only(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }
@ -412,10 +401,6 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
pub unsafe fn from_raw_verification_only( pub unsafe fn from_raw_verification_only(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }

View File

@ -373,7 +373,6 @@ impl std::error::Error for Error {
pub struct Secp256k1<C: Context> { pub struct Secp256k1<C: Context> {
ctx: *mut ffi::Context, ctx: *mut ffi::Context,
phantom: PhantomData<C>, phantom: PhantomData<C>,
size: usize,
} }
// The underlying secp context does not contain any references to memory it does not own. // The underlying secp context does not contain any references to memory it does not own.
@ -390,8 +389,9 @@ impl<C: Context> Eq for Secp256k1<C> {}
impl<C: Context> Drop for Secp256k1<C> { impl<C: Context> Drop for Secp256k1<C> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let size = ffi::secp256k1_context_preallocated_clone_size(self.ctx);
ffi::secp256k1_context_preallocated_destroy(self.ctx); ffi::secp256k1_context_preallocated_destroy(self.ctx);
C::deallocate(self.ctx as _, self.size); C::deallocate(self.ctx as _, size);
} }
} }
} }
@ -558,13 +558,11 @@ mod tests {
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) }; let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) };
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) }; let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) };
let size = 0; let full: Secp256k1<AllPreallocated> = Secp256k1 { ctx: ctx_full, phantom: PhantomData };
let full: Secp256k1<AllPreallocated> =
Secp256k1 { ctx: ctx_full, phantom: PhantomData, size };
let sign: Secp256k1<SignOnlyPreallocated> = let sign: Secp256k1<SignOnlyPreallocated> =
Secp256k1 { ctx: ctx_sign, phantom: PhantomData, size }; Secp256k1 { ctx: ctx_sign, phantom: PhantomData };
let vrfy: Secp256k1<VerifyOnlyPreallocated> = let vrfy: Secp256k1<VerifyOnlyPreallocated> =
Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData, size }; Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData };
let (sk, pk) = full.generate_keypair(&mut rand::thread_rng()); let (sk, pk) = full.generate_keypair(&mut rand::thread_rng());
let msg = Message::from_slice(&[2u8; 32]).unwrap(); let msg = Message::from_slice(&[2u8; 32]).unwrap();