Skip to content

Commit 3760ef6

Browse files
committed
Merge #541: Remove size field
129ba3c Remove size field (Tobin C. Harding) Pull request description: 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. Fix #537 ACKs for top commit: apoelstra: ACK 129ba3c Tree-SHA512: 3fce7863065e4b485fd2d1fdbbfe7002fa6188f1a703d89fdda570a1a32471d298e2e33fb8c5951a56a79facb5d2b427d58e473b5cb1d68eb02ffed728392b97
2 parents 8ab0bbc + 129ba3c commit 3760ef6

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

src/context.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ mod alloc_only {
212212
ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS)
213213
},
214214
phantom: PhantomData,
215-
size,
216215
};
217216

218217
#[cfg(all(
@@ -273,7 +272,6 @@ mod alloc_only {
273272
ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)
274273
},
275274
phantom: PhantomData,
276-
size,
277275
}
278276
}
279277
}
@@ -329,7 +327,6 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
329327
)
330328
},
331329
phantom: PhantomData,
332-
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
333330
})
334331
}
335332
}
@@ -358,11 +355,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
358355
pub unsafe fn from_raw_all(
359356
raw_ctx: *mut ffi::Context,
360357
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
361-
ManuallyDrop::new(Secp256k1 {
362-
ctx: raw_ctx,
363-
phantom: PhantomData,
364-
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
365-
})
358+
ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
366359
}
367360
}
368361

@@ -393,11 +386,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
393386
pub unsafe fn from_raw_signing_only(
394387
raw_ctx: *mut ffi::Context,
395388
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
396-
ManuallyDrop::new(Secp256k1 {
397-
ctx: raw_ctx,
398-
phantom: PhantomData,
399-
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
400-
})
389+
ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
401390
}
402391
}
403392

@@ -428,10 +417,6 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
428417
pub unsafe fn from_raw_verification_only(
429418
raw_ctx: *mut ffi::Context,
430419
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {
431-
ManuallyDrop::new(Secp256k1 {
432-
ctx: raw_ctx,
433-
phantom: PhantomData,
434-
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
435-
})
420+
ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
436421
}
437422
}

src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ impl std::error::Error for Error {
371371
pub struct Secp256k1<C: Context> {
372372
ctx: *mut ffi::Context,
373373
phantom: PhantomData<C>,
374-
size: usize,
375374
}
376375

377376
// The underlying secp context does not contain any references to memory it does not own.
@@ -388,8 +387,9 @@ impl<C: Context> Eq for Secp256k1<C> {}
388387
impl<C: Context> Drop for Secp256k1<C> {
389388
fn drop(&mut self) {
390389
unsafe {
390+
let size = ffi::secp256k1_context_preallocated_clone_size(self.ctx);
391391
ffi::secp256k1_context_preallocated_destroy(self.ctx);
392-
C::deallocate(self.ctx as _, self.size);
392+
C::deallocate(self.ctx as _, size);
393393
}
394394
}
395395
}
@@ -556,13 +556,11 @@ mod tests {
556556
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) };
557557
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) };
558558

559-
let size = 0;
560-
let full: Secp256k1<AllPreallocated> =
561-
Secp256k1 { ctx: ctx_full, phantom: PhantomData, size };
559+
let full: Secp256k1<AllPreallocated> = Secp256k1 { ctx: ctx_full, phantom: PhantomData };
562560
let sign: Secp256k1<SignOnlyPreallocated> =
563-
Secp256k1 { ctx: ctx_sign, phantom: PhantomData, size };
561+
Secp256k1 { ctx: ctx_sign, phantom: PhantomData };
564562
let vrfy: Secp256k1<VerifyOnlyPreallocated> =
565-
Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData, size };
563+
Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData };
566564

567565
let (sk, pk) = full.generate_keypair(&mut rand::thread_rng());
568566
let msg = Message::from_slice(&[2u8; 32]).unwrap();

0 commit comments

Comments
 (0)