Skip to content

Commit fd206ab

Browse files
committed
Replace use of boxes with global allocator
1 parent 7b99784 commit fd206ab

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/context.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
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};
54
use ffi::types::{c_uint, c_void};
65
use Error;
76
use 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,7 +226,7 @@ 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
}
@@ -241,7 +247,7 @@ 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
}
@@ -271,7 +277,7 @@ 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
}
@@ -303,7 +309,7 @@ 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
}
@@ -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
}

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl std::error::Error for Error {}
558558
pub struct Secp256k1<C: Context> {
559559
ctx: *mut ffi::Context,
560560
phantom: PhantomData<C>,
561-
buf: *mut [u8],
561+
size: usize,
562562
}
563563

564564
// The underlying secp context does not contain any references to memory it does not own
@@ -607,7 +607,7 @@ impl<C: Context> Drop for Secp256k1<C> {
607607
fn drop(&mut self) {
608608
unsafe {
609609
ffi::secp256k1_context_preallocated_destroy(self.ctx);
610-
C::deallocate(self.buf);
610+
C::deallocate(self.ctx as _, self.size);
611611
}
612612
}
613613
}
@@ -781,10 +781,10 @@ mod tests {
781781
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) };
782782
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) };
783783

784-
let buf: *mut [u8] = &mut [0u8;0] as _;
785-
let full: Secp256k1<AllPreallocated> = Secp256k1{ctx: ctx_full, phantom: PhantomData, buf};
786-
let sign: Secp256k1<SignOnlyPreallocated> = Secp256k1{ctx: ctx_sign, phantom: PhantomData, buf};
787-
let vrfy: Secp256k1<VerifyOnlyPreallocated> = Secp256k1{ctx: ctx_vrfy, phantom: PhantomData, buf};
784+
let size = 0;
785+
let full: Secp256k1<AllPreallocated> = Secp256k1{ctx: ctx_full, phantom: PhantomData, size};
786+
let sign: Secp256k1<SignOnlyPreallocated> = Secp256k1{ctx: ctx_sign, phantom: PhantomData, size};
787+
let vrfy: Secp256k1<VerifyOnlyPreallocated> = Secp256k1{ctx: ctx_vrfy, phantom: PhantomData, size};
788788

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

0 commit comments

Comments
 (0)