Skip to content

Commit

Permalink
Add docs for AlignType and preallocation, and update preallocate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Aug 8, 2019
1 parent b99610f commit 8477d9f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/context.rs
Expand Up @@ -7,9 +7,15 @@ use Secp256k1;
#[cfg(feature = "std")]
pub use self::std_only::*;

/// A type that represents the type with the biggest alignment we can get in rust.
/// Trying to match what `malloc` does in C, this should be aligned enough to contain pointers too.
/// This type can have different size/alignment depending on the architecture.
#[cfg(any(target_pointer_width = "32", target_pointer_width = "16", target_pointer_width = "8"))]
pub type AlignType = u64;

/// A type that represents the type with the biggest alignment we can get in rust.
/// Trying to match what `malloc` does in C, this should be aligned enough to contain pointers too.
/// This type can have different size/alignment depending on the architecture.
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "16", target_pointer_width = "8")))]
pub type AlignType = usize;

Expand Down Expand Up @@ -202,7 +208,18 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
pub fn preallocated_new(buf: &'buf mut [AlignType]) -> Result<Secp256k1<AllPreallocated<'buf>>, Error> {
Secp256k1::preallocated_gen_new(buf)
}
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
///
/// Notice that the memory returned is in [AlignedType](type.AlignType.html)
///
/// ## Examples
/// ```rust
/// # use secp256k1::*;
/// let buf_size = Secp256k1::preallocate_size();
/// let mut buf = vec![0; buf_size];
/// let secp = Secp256k1::preallocated_new(&mut buf).unwrap();
///
/// ```
pub fn preallocate_size() -> usize {
Self::preallocate_size_gen()
}
Expand All @@ -214,7 +231,18 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
Secp256k1::preallocated_gen_new(buf)
}

/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
///
/// Notice that the memory returned is in [AlignedType](type.AlignType.html)
///
/// ## Examples
/// ```rust
/// # use secp256k1::*;
/// let buf_size = Secp256k1::preallocate_signing_size();
/// let mut buf = vec![0; buf_size];
/// let secp = Secp256k1::preallocated_signing_only(&mut buf).unwrap();
///
/// ```
#[inline]
pub fn preallocate_signing_size() -> usize {
Self::preallocate_size_gen()
Expand All @@ -227,7 +255,18 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
Secp256k1::preallocated_gen_new(buf)
}

/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
///
/// Notice that the memory returned is in [AlignedType](type.AlignType.html)
///
/// ## Examples
/// ```rust
/// # use secp256k1::*;
/// let buf_size = Secp256k1::preallocate_verification_size();
/// let mut buf = vec![0; buf_size];
/// let secp = Secp256k1::preallocated_verification_only(&mut buf).unwrap();
///
/// ```
#[inline]
pub fn preallocate_verification_size() -> usize {
Self::preallocate_size_gen()
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -573,6 +573,9 @@ impl<C: Context> Secp256k1<C> {
}

/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
///
/// Notice that the memory returned is in [AlignedType](type.AlignType.html)
///
pub fn preallocate_size_gen() -> usize {
assert!(mem::align_of::<AlignType>() >= mem::align_of::<u8>());
assert!(mem::align_of::<AlignType>() >= mem::align_of::<usize>());
Expand Down

0 comments on commit 8477d9f

Please sign in to comment.