@@ -6,11 +6,18 @@ use defines::{SparseFormat, BinaryOp, RandomEngineType};
66use error:: HANDLE_ERROR ;
77use std:: mem;
88use 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) ]
1216extern {
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: * const T = :: std:: ptr:: null ( ) ;
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+
2652impl 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