Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
use mpz::*;
use std::{mem, slice, str};
use libc::{c_char, strlen};

type AllocFunc = extern "C" fn(usize) -> *mut c_char;
type ReallocFunc = extern "C" fn(*mut c_char, usize, usize) -> *mut c_char;
type FreeFunc = extern "C" fn(*mut c_char, usize);

#[link(name = "gmp")]
extern "C" {
pub fn __gmpz_fdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
pub fn __gmpz_cdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
pub fn __gmp_get_memory_functions(alloc: *mut AllocFunc, relloc: *mut ReallocFunc, free: *mut FreeFunc);
}

pub struct GString(*mut u8, usize);

impl GString {
pub unsafe fn from_raw(raw: *mut c_char) -> GString {
GString(mem::transmute(raw), strlen(raw) as usize + 1)
}

pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
let bytes: &[u8] = unsafe { slice::from_raw_parts(self.0, self.1) };
str::from_utf8(&bytes[..bytes.len() - 1])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CStr::from_ptr is more direct and avoids the separate strlen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no matter what, the length needs to be known in order to call the free func, so it's either strlen or CStr::to_bytes().len(). i think strlen communicates more clearly how the length param to free_func is computed, whereas the alternate method relies on knowing that CStr implicitly calls strlen behind the scenes.

}
}

impl Drop for GString {
fn drop(&mut self) {
use std::ptr::null_mut;
unsafe {
let mut free_func: FreeFunc = mem::uninitialized();
__gmp_get_memory_functions(null_mut(), null_mut(), mem::transmute(&mut free_func));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transmute is unnecessary because &mut FreeFunc converts implicitly to *mut FreeFunc.

free_func(mem::transmute(self.0), self.1);
}
}
}
30 changes: 22 additions & 8 deletions src/mpf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libc::{c_double, c_int, c_long, c_ulong, c_void,c_char};
use std::mem::uninitialized;
use std::mem::{transmute, uninitialized};
use std::cmp;
use std::cmp::Ordering::{self, Greater, Less, Equal};
use std::ops::{Div, DivAssign, Mul, MulAssign, Add, AddAssign, Sub, SubAssign, Neg};
Expand All @@ -10,6 +10,7 @@ use super::mpz::{Mpz, mpz_srcptr};
use super::mpq::{Mpq, mpq_srcptr};
use super::sign::Sign;
use num_traits::{Zero, One};
use ffi::GString;

type mp_exp_t = c_long;

Expand Down Expand Up @@ -37,7 +38,7 @@ extern "C" {

fn __gmpf_set_str(rop: mpf_ptr, str: *const c_char, base: c_int);
fn __gmpf_set_si(rop: mpf_ptr, op: c_long);
fn __gmpf_get_str(str: *const c_char, expptr: *const mp_exp_t, base: i32, n_digits: i32, op: mpf_ptr) -> *mut c_char;
fn __gmpf_get_str(str: *mut c_char, expptr: *const mp_exp_t, base: i32, n_digits: i32, op: mpf_srcptr) -> *mut c_char;

fn __gmpf_cmp(op1: mpf_srcptr, op2: mpf_srcptr) -> c_int;
fn __gmpf_cmp_d(op1: mpf_srcptr, op2: c_double) -> c_int;
Expand Down Expand Up @@ -119,13 +120,26 @@ impl Mpf {
}
}

pub fn get_str(&mut self, n_digits: i32, base: i32, exp: &mut c_long) -> String{
let c_str = CString::new("").unwrap();
let out;
unsafe{
out = CString::from_raw(__gmpf_get_str(c_str.into_raw(), exp, base, n_digits, &mut self.mpf));
pub fn get_str(&self, n_digits: i32, base: i32, exp: &mut c_long) -> String {
use std::ptr::null_mut;
if n_digits == 0 {
// Maximal significant digits requested. Let GMP compute the length
// and allocate the space required.
unsafe {
let p = __gmpf_get_str(null_mut(), exp, base, n_digits, &self.mpf);
// De-allocates the GMP string on drop.
GString::from_raw(p).to_str().unwrap().to_string()
}
} else {
// From mpf/get_str.c.
let bytes = unsafe {
let mut bytes: Vec<u8> = uninitialized();
Copy link

@andersk andersk Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a Vec with uninitialized data, not an uninitialized Vec! I think Vec::with_capacity is the right thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, thanks for catching this.

let c_str: *mut c_char = transmute(bytes.as_mut_ptr());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a worrying transmute—you can just bytes.as_mut_ptr() as *mut c_char.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure that as_mut_ptr will return a * mut u8.

__gmpf_get_str(c_str, exp, base, n_digits, &self.mpf);
bytes
};
String::from_utf8(bytes).unwrap()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That includes the trailing NUL, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. from mpf/get_str.c:

  if (dbuf == 0)
    {
      /* We didn't get a string from the user.  Allocate one (and return
	 a pointer to it) with space for `-' and terminating null.  */
      alloc_size = n_digits + 2;
      dbuf = (char *) (*__gmp_allocate_func) (n_digits + 2);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No no, my point here was that String::from_utf8 includes the trailing NUL in its return value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i realized what you meant a few minutes after hitting send. this branch should be tested anyway, and it shows up in the test.

}
out.to_str().unwrap().to_string()
}

pub fn abs(&self) -> Mpf {
Expand Down