kronos_compute/implementation/
memory.rs

1//! Memory allocation and management
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use crate::implementation::icd_loader;
7
8/// Allocate device memory
9// SAFETY: This function is called from C code. Caller must ensure:
10// 1. device is a valid VkDevice
11// 2. pAllocateInfo points to a valid VkMemoryAllocateInfo structure
12// 3. pAllocator is either null or points to valid allocation callbacks
13// 4. pMemory points to valid memory for writing the memory handle
14#[no_mangle]
15pub unsafe extern "C" fn vkAllocateMemory(
16    device: VkDevice,
17    pAllocateInfo: *const VkMemoryAllocateInfo,
18    pAllocator: *const VkAllocationCallbacks,
19    pMemory: *mut VkDeviceMemory,
20) -> VkResult {
21    if device.is_null() || pAllocateInfo.is_null() || pMemory.is_null() {
22        return VkResult::ErrorInitializationFailed;
23    }
24    
25    if let Some(icd) = icd_loader::icd_for_device(device) {
26        if let Some(f) = icd.allocate_memory { return f(device, pAllocateInfo, pAllocator, pMemory); }
27    }
28    if let Some(icd) = super::forward::get_icd_if_enabled() {
29        if let Some(allocate_memory) = icd.allocate_memory { return allocate_memory(device, pAllocateInfo, pAllocator, pMemory); }
30    }
31    VkResult::ErrorInitializationFailed
32}
33
34/// Free device memory
35// SAFETY: This function is called from C code. Caller must ensure:
36// 1. device is a valid VkDevice
37// 2. memory is a valid VkDeviceMemory allocated with vkAllocateMemory
38// 3. pAllocator matches the allocator used in vkAllocateMemory (or both are null)
39// 4. The memory is not currently mapped
40#[no_mangle]
41pub unsafe extern "C" fn vkFreeMemory(
42    device: VkDevice,
43    memory: VkDeviceMemory,
44    pAllocator: *const VkAllocationCallbacks,
45) {
46    if device.is_null() || memory.is_null() {
47        return;
48    }
49    
50    if let Some(icd) = icd_loader::icd_for_device(device) {
51        if let Some(f) = icd.free_memory { f(device, memory, pAllocator); }
52        return;
53    }
54    if let Some(icd) = super::forward::get_icd_if_enabled() {
55        if let Some(free_memory) = icd.free_memory { free_memory(device, memory, pAllocator); }
56    }
57}
58
59/// Map memory for CPU access
60// SAFETY: This function is called from C code. Caller must ensure:
61// 1. device is a valid VkDevice
62// 2. memory is a valid VkDeviceMemory with the VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
63// 3. offset and size are within the allocated memory range
64// 4. ppData points to valid memory for writing the mapped pointer
65// 5. The memory is not already mapped
66#[no_mangle]
67pub unsafe extern "C" fn vkMapMemory(
68    device: VkDevice,
69    memory: VkDeviceMemory,
70    offset: VkDeviceSize,
71    size: VkDeviceSize,
72    flags: VkMemoryMapFlags,
73    ppData: *mut *mut libc::c_void,
74) -> VkResult {
75    if device.is_null() || memory.is_null() || ppData.is_null() {
76        return VkResult::ErrorInitializationFailed;
77    }
78    
79    if let Some(icd) = icd_loader::icd_for_device(device) {
80        if let Some(f) = icd.map_memory { return f(device, memory, offset, size, flags, ppData); }
81    }
82    if let Some(icd) = super::forward::get_icd_if_enabled() {
83        if let Some(map_memory) = icd.map_memory { return map_memory(device, memory, offset, size, flags, ppData); }
84    }
85    VkResult::ErrorInitializationFailed
86}
87
88/// Unmap memory
89// SAFETY: This function is called from C code. Caller must ensure:
90// 1. device is a valid VkDevice
91// 2. memory is a valid VkDeviceMemory that is currently mapped
92// 3. Any host writes to the mapped memory are complete
93#[no_mangle]
94pub unsafe extern "C" fn vkUnmapMemory(
95    device: VkDevice,
96    memory: VkDeviceMemory,
97) {
98    if device.is_null() || memory.is_null() {
99        return;
100    }
101    
102    if let Some(icd) = icd_loader::icd_for_device(device) {
103        if let Some(f) = icd.unmap_memory { f(device, memory); }
104        return;
105    }
106    if let Some(icd) = super::forward::get_icd_if_enabled() {
107        if let Some(unmap_memory) = icd.unmap_memory { unmap_memory(device, memory); }
108    }
109}