Fix up drm_version allocations

master
itycodes 3 weeks ago
parent 7d74a45bd9
commit df77eca00e

@ -13,7 +13,7 @@ pub struct DrmDeviceNode {
pub path: Option<OsString>, pub path: Option<OsString>,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct GemHandle<'a> { pub struct GemHandle<'a> {
pub handle: DrmGemHandle, pub handle: DrmGemHandle,
pub node: &'a DrmDeviceNode, pub node: &'a DrmDeviceNode,
@ -30,7 +30,7 @@ impl GemHandle<'_> {
pub fn from_handle(node: &DrmDeviceNode, handle: DrmGemHandle) -> Result<GemHandle, GemIoctlError> { pub fn from_handle(node: &DrmDeviceNode, handle: DrmGemHandle) -> Result<GemHandle, GemIoctlError> {
// Avoid invoking GemHandle::is_valid() here to prevent the drop() method from trying to drop an invalid handle // Avoid invoking GemHandle::is_valid() here to prevent the drop() method from trying to drop an invalid handle
let is_valid = uapi::i915::gem_is_valid(node.fd.as_raw_fd(), handle); let is_valid = uapi::i915::gem_is_valid(node.fd.as_raw_fd(), &handle);
if is_valid.is_ok() && is_valid.unwrap() { if is_valid.is_ok() && is_valid.unwrap() {
let res = GemHandle { let res = GemHandle {
@ -43,23 +43,23 @@ impl GemHandle<'_> {
} }
} }
pub fn close(&mut self) -> Result<(), i32> { pub fn close(self) -> Result<(), i32> {
return uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle); return uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle.clone());
} }
pub fn has_tiling(&self) -> Result<bool, uapi::i915::GemIoctlError> { pub fn has_tiling(&self) -> Result<bool, uapi::i915::GemIoctlError> {
uapi::i915::gem_has_tiling(self.node.fd.as_raw_fd(), self.handle) uapi::i915::gem_has_tiling(self.node.fd.as_raw_fd(), &self.handle)
} }
pub fn is_valid(&self) -> Result<bool, GemIoctlError> { pub fn is_valid(&self) -> Result<bool, GemIoctlError> {
uapi::i915::gem_is_valid(self.node.fd.as_raw_fd(), self.handle) uapi::i915::gem_is_valid(self.node.fd.as_raw_fd(), &self.handle)
} }
} }
impl Drop for GemHandle<'_> { impl Drop for GemHandle<'_> {
fn drop(&mut self) { fn drop(&mut self) {
// Ignoring the close failing as an invalid Gem handle's drop is a no-op // Ignoring the close failing as an invalid Gem handle's drop is a no-op
let _ = self.close(); _ = uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle.clone());
} }
} }

@ -29,7 +29,7 @@ pub struct EngineInfo {
pub capabilities: u64, pub capabilities: u64,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct DrmGemHandle { pub struct DrmGemHandle {
pub handle: u32, pub handle: u32,
} }
@ -201,7 +201,7 @@ pub enum GemIoctlError {
Unknown(i32), Unknown(i32),
} }
pub fn gem_has_tiling(fd: RawFd, handle: DrmGemHandle) -> Result<bool, GemIoctlError> { pub fn gem_has_tiling(fd: RawFd, handle: &DrmGemHandle) -> Result<bool, GemIoctlError> {
unsafe { unsafe {
let mut tiling = native::drm_i915_gem_get_tiling { let mut tiling = native::drm_i915_gem_get_tiling {
handle: handle.handle, handle: handle.handle,
@ -220,7 +220,7 @@ pub fn gem_has_tiling(fd: RawFd, handle: DrmGemHandle) -> Result<bool, GemIoctlE
} }
} }
pub fn gem_is_valid(fd: RawFd, handle: DrmGemHandle) -> Result<bool, GemIoctlError> { pub fn gem_is_valid(fd: RawFd, handle: &DrmGemHandle) -> Result<bool, GemIoctlError> {
let res = gem_has_tiling(fd, handle); let res = gem_has_tiling(fd, handle);
if res.is_ok() && res.unwrap() { if res.is_ok() && res.unwrap() {
return Ok(true); return Ok(true);

@ -2,6 +2,8 @@
use std::os::fd::RawFd; use std::os::fd::RawFd;
use std::ffi::{CStr, OsString}; use std::ffi::{CStr, OsString};
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::vec;
use libc; use libc;
use super::native; use super::native;
@ -20,36 +22,38 @@ pub struct DrmVersion {
pub fn get_drm_version(fd: RawFd) -> Option<DrmVersion> { pub fn get_drm_version(fd: RawFd) -> Option<DrmVersion> {
unsafe { unsafe {
let name_vec = libc::malloc(128); let mut name_vec = vec![0 as u8; 128];
libc::memset(name_vec, 0, 128); let mut date_vec = vec![0 as u8; 128];
let date_vec = libc::malloc(128); let mut desc_vec = vec![0 as u8; 128];
libc::memset(date_vec, 0, 128);
let desc_vec = libc::malloc(128);
libc::memset(desc_vec, 0, 128);
let mut version = native::drm_version { let mut version = native::drm_version {
version_major: 0, version_major: 0,
version_minor: 0, version_minor: 0,
version_patchlevel: 0, version_patchlevel: 0,
name: name_vec as *mut i8, name: name_vec.as_mut_ptr() as *mut i8,
date: date_vec as *mut i8, date: date_vec.as_mut_ptr() as *mut i8,
desc: desc_vec as *mut i8, desc: desc_vec.as_mut_ptr() as *mut i8,
name_len: 128, name_len: 128,
date_len: 128, date_len: 128,
desc_len: 128, desc_len: 128,
}; };
let res_val = libc::ioctl(fd, native::DRM_IOCTL_VERSION, &mut version) == 0; let res_val = libc::ioctl(fd, native::DRM_IOCTL_VERSION, &mut version) == 0;
let res = if res_val { let res = if res_val {
let name = CStr::from_ptr(version.name).to_str().expect("Failed to convert name"); name_vec.resize(CStr::from_bytes_until_nul(name_vec.as_slice()).unwrap().to_bytes().len(), 0);
let date = CStr::from_ptr(version.date).to_str().expect("Failed to convert date"); date_vec.resize(CStr::from_bytes_until_nul(date_vec.as_slice()).unwrap().to_bytes().len(), 0);
let desc = CStr::from_ptr(version.desc).to_str().expect("Failed to convert desc"); desc_vec.resize(CStr::from_bytes_until_nul(desc_vec.as_slice()).unwrap().to_bytes().len(), 0);
let name = OsString::from_vec(name_vec);
let date = OsString::from_vec(date_vec);
let desc = OsString::from_vec(desc_vec);
let drm_version = DrmVersion { let drm_version = DrmVersion {
version_major: version.version_major, version_major: version.version_major,
version_minor: version.version_minor, version_minor: version.version_minor,
version_patchlevel: version.version_patchlevel, version_patchlevel: version.version_patchlevel,
name: OsString::from(name.to_string()), name: name,
date: OsString::from(date.to_string()), date: date,
desc: OsString::from(desc.to_string()), desc: desc,
}; };
Some(drm_version) Some(drm_version)
} else {None}; } else {None};

@ -63,9 +63,9 @@ fn test_i915_uapi_gem_lifecycle() {
let node = i915::find_node().expect("Failed to find i915 fd"); let node = i915::find_node().expect("Failed to find i915 fd");
let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem"); let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem");
assert!(gem.handle > 0); assert!(gem.handle > 0);
assert!(i915::gem_is_valid(node.fd.as_raw_fd(), gem).unwrap()); assert!(i915::gem_is_valid(node.fd.as_raw_fd(), &gem).unwrap());
i915::close_gem(node.fd.as_raw_fd(), gem).expect("Failed to close gem"); i915::close_gem(node.fd.as_raw_fd(), gem.clone()).expect("Failed to close gem");
assert!(!i915::gem_is_valid(node.fd.as_raw_fd(), gem).unwrap()); assert!(!i915::gem_is_valid(node.fd.as_raw_fd(), &gem).unwrap());
let invalid_fd = File::open("/dev/null").expect("Failed to open /dev/null"); let invalid_fd = File::open("/dev/null").expect("Failed to open /dev/null");
assert!(i915::make_gem(invalid_fd.as_raw_fd(), 4096).is_none()); assert!(i915::make_gem(invalid_fd.as_raw_fd(), 4096).is_none());
} }
@ -75,6 +75,6 @@ fn test_i915_uapi_gem_tiling() {
let node = i915::find_node().expect("Failed to find i915 fd"); let node = i915::find_node().expect("Failed to find i915 fd");
let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem"); let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem");
// TODO figure out which devices this holds for // TODO figure out which devices this holds for
assert!(i915::gem_has_tiling(node.fd.as_raw_fd(), gem).is_ok_and(|e| e == true)); assert!(i915::gem_has_tiling(node.fd.as_raw_fd(), &gem).is_ok_and(|e| e == true));
i915::close_gem(node.fd.as_raw_fd(), gem).expect("Failed to close gem"); i915::close_gem(node.fd.as_raw_fd(), gem).expect("Failed to close gem");
} }
Loading…
Cancel
Save