Skip to content

Commit 99ad808

Browse files
committed
Split unsafe block into small parts in list_llama_ggml_backend_devices
1 parent 7d12f30 commit 99ad808

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

llama-cpp-2/src/lib.rs

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -398,56 +398,44 @@ pub struct LlamaBackendDevice {
398398
pub fn list_llama_ggml_backend_devices() -> Vec<LlamaBackendDevice> {
399399
let mut devices = Vec::new();
400400
for i in 0..unsafe { llama_cpp_sys_2::ggml_backend_dev_count() } {
401-
unsafe {
402-
let dev = llama_cpp_sys_2::ggml_backend_dev_get(i);
403-
let mut props = std::mem::zeroed();
404-
llama_cpp_sys_2::ggml_backend_dev_get_props(dev, &raw mut props);
405-
let name = props.name;
406-
let name = if name.is_null() {
407-
String::new()
408-
} else {
409-
std::ffi::CStr::from_ptr(name).to_string_lossy().to_string()
410-
};
411-
let description = props.description;
412-
let description = if description.is_null() {
413-
String::new()
414-
} else {
415-
std::ffi::CStr::from_ptr(description)
416-
.to_string_lossy()
417-
.to_string()
418-
};
419-
let backend = llama_cpp_sys_2::ggml_backend_dev_backend_reg(dev);
420-
let backend_name = llama_cpp_sys_2::ggml_backend_reg_name(backend);
421-
let backend = if backend_name.is_null() {
401+
fn cstr_to_string(ptr: *const i8) -> String {
402+
if ptr.is_null() {
422403
String::new()
423404
} else {
424-
std::ffi::CStr::from_ptr(backend_name)
405+
unsafe { std::ffi::CStr::from_ptr(ptr) }
425406
.to_string_lossy()
426407
.to_string()
427-
};
428-
let memory_total = props.memory_total;
429-
let memory_free = props.memory_free;
430-
let device_type = match props.type_ {
431-
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_CPU => LlamaBackendDeviceType::Cpu,
432-
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_ACCEL => {
433-
LlamaBackendDeviceType::Accelerator
434-
}
435-
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_GPU => LlamaBackendDeviceType::Gpu,
436-
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_IGPU => {
437-
LlamaBackendDeviceType::IntegratedGpu
438-
}
439-
_ => LlamaBackendDeviceType::Unknown,
440-
};
441-
devices.push(LlamaBackendDevice {
442-
index: i,
443-
name,
444-
description,
445-
backend,
446-
memory_total,
447-
memory_free,
448-
device_type,
449-
});
408+
}
450409
}
410+
let dev = unsafe { llama_cpp_sys_2::ggml_backend_dev_get(i) };
411+
let props = unsafe {
412+
let mut props = std::mem::zeroed();
413+
llama_cpp_sys_2::ggml_backend_dev_get_props(dev, &raw mut props);
414+
props
415+
};
416+
let name = cstr_to_string(props.name);
417+
let description = cstr_to_string(props.description);
418+
let backend = unsafe { llama_cpp_sys_2::ggml_backend_dev_backend_reg(dev) };
419+
let backend_name = unsafe { llama_cpp_sys_2::ggml_backend_reg_name(backend) };
420+
let backend = cstr_to_string(backend_name);
421+
let memory_total = props.memory_total;
422+
let memory_free = props.memory_free;
423+
let device_type = match props.type_ {
424+
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_CPU => LlamaBackendDeviceType::Cpu,
425+
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_ACCEL => LlamaBackendDeviceType::Accelerator,
426+
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_GPU => LlamaBackendDeviceType::Gpu,
427+
llama_cpp_sys_2::GGML_BACKEND_DEVICE_TYPE_IGPU => LlamaBackendDeviceType::IntegratedGpu,
428+
_ => LlamaBackendDeviceType::Unknown,
429+
};
430+
devices.push(LlamaBackendDevice {
431+
index: i,
432+
name,
433+
description,
434+
backend,
435+
memory_total,
436+
memory_free,
437+
device_type,
438+
});
451439
}
452440
devices
453441
}

0 commit comments

Comments
 (0)