Skip to content

Commit 1d80171

Browse files
committed
Added wrappers around af_alloc_host and af_free_host in the util module.
1 parent 12d2231 commit 1d80171

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/device/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ extern crate libc;
33
use defines::{AfError, DType};
44
use error::HANDLE_ERROR;
55
use self::libc::{c_int, size_t, c_char, c_void};
6-
use std::ffi::CString;
6+
use std::ffi::{CStr, CString};
7+
use util::free_host;
78

89
extern {
910
fn af_get_version(major: *mut c_int, minor: *mut c_int, patch: *mut c_int) -> c_int;
@@ -21,9 +22,6 @@ extern {
2122
fn af_get_mem_step_size(step_bytes: *mut size_t) -> c_int;
2223
fn af_device_gc() -> c_int;
2324
fn af_sync(device: c_int) -> c_int;
24-
25-
fn af_alloc_host(elements: size_t, _type: DType) -> *mut c_void;
26-
fn af_free_host(ptr: *mut c_void) -> c_int;
2725
}
2826

2927
/// Get ArrayFire Version Number
@@ -72,17 +70,15 @@ pub fn info() {
7270
/// [0] GeForce GT 750M, 2048 MB, CUDA Compute 3.0
7371
/// ```
7472
pub fn info_string(verbose: bool) -> String {
75-
let mut tmp: *mut c_char = 0 as *mut c_char;
73+
let result: String;
7674
unsafe {
75+
let mut tmp: *mut c_char = 0 as *mut c_char;
7776
let err_val = af_info_string(&mut tmp, verbose);
7877
HANDLE_ERROR(AfError::from(err_val));
79-
80-
let result = (*CString::from_raw(tmp)).to_str().unwrap().to_owned();
81-
82-
let err_val = af_free_host(tmp as *mut c_void);
83-
HANDLE_ERROR(AfError::from(err_val));
84-
result
78+
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
79+
free_host(tmp);
8580
}
81+
result
8682
}
8783

8884
/// Initialize ArrayFire library

src/util.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ use defines::{SparseFormat, BinaryOp, RandomEngineType};
66
use error::HANDLE_ERROR;
77
use std::mem;
88
use self::num::Complex;
9-
use self::libc::{uint8_t, c_int, size_t};
9+
use self::libc::{uint8_t, c_int, size_t, c_void};
10+
11+
// This is private in array
12+
// use array::DimT;
13+
type DimT = self::libc::c_longlong;
1014

1115
#[allow(dead_code)]
1216
extern {
1317
fn af_get_size_of(size: *mut size_t, aftype: uint8_t) -> c_int;
18+
19+
fn af_alloc_host(ptr: *mut *const c_void, bytes: DimT) -> c_int;
20+
fn af_free_host(ptr: *mut c_void) -> c_int;
1421
}
1522

1623
/// Get size, in bytes, of the arrayfire native type
@@ -23,6 +30,25 @@ pub fn get_size(value: DType) -> usize {
2330
}
2431
}
2532

33+
/// Allocates space using Arrayfire allocator in host memory
34+
pub fn alloc_host<T>(elements: usize, _type: DType) -> *const T {
35+
let ptr = 0 as *const T;
36+
let bytes = (elements * get_size(_type)) as DimT;
37+
unsafe {
38+
let err_val = af_alloc_host(&mut (ptr as *const c_void), bytes);
39+
HANDLE_ERROR(AfError::from(err_val));
40+
}
41+
ptr
42+
}
43+
44+
/// Frees memory allocated by Arrayfire allocator in host memory
45+
pub fn free_host<T>(ptr: *mut T) {
46+
unsafe {
47+
let err_val = af_free_host(ptr as *mut c_void);
48+
HANDLE_ERROR(AfError::from(err_val));
49+
}
50+
}
51+
2652
impl From<i32> for AfError {
2753
fn from(t: i32) -> AfError {
2854
assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);

0 commit comments

Comments
 (0)