Skip to content

Commit 9508028

Browse files
committed
Pinned memory allocation and free functions
1 parent bbf08a1 commit 9508028

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/core/device.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::defines::AfError;
22
use super::error::HANDLE_ERROR;
3-
use super::util::free_host;
3+
use super::util::{dim_t, free_host, void_ptr};
44

55
use libc::{c_char, c_int, size_t};
66
use std::borrow::Cow;
@@ -33,6 +33,9 @@ extern "C" {
3333
fn af_get_mem_step_size(step_bytes: *mut size_t) -> c_int;
3434
fn af_device_gc() -> c_int;
3535
fn af_sync(device: c_int) -> c_int;
36+
37+
fn af_alloc_pinned(non_pagable_ptr: *mut void_ptr, bytes: dim_t) -> c_int;
38+
fn af_free_pinned(non_pagable_ptr: void_ptr) -> c_int;
3639
}
3740

3841
/// Get ArrayFire Version Number
@@ -314,3 +317,17 @@ pub fn sync(device: i32) {
314317
HANDLE_ERROR(AfError::from(err_val));
315318
}
316319
}
320+
321+
/// Allocate non-pageable memory on HOST memory
322+
pub unsafe fn alloc_pinned(bytes: usize) -> void_ptr {
323+
let mut out: void_ptr = std::ptr::null_mut();
324+
let err_val = af_alloc_pinned(&mut out as *mut void_ptr, bytes as dim_t);
325+
HANDLE_ERROR(AfError::from(err_val));
326+
out
327+
}
328+
329+
/// Free the pointer returned by [alloc_pinned](./fn.alloc_pinned.html)
330+
pub unsafe fn free_pinned(ptr: void_ptr) {
331+
let err_val = af_free_pinned(ptr);
332+
HANDLE_ERROR(AfError::from(err_val));
333+
}

0 commit comments

Comments
 (0)