kronos_compute/implementation/
memory.rs

1//! Memory allocation and management
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6
7/// Allocate device memory
8// SAFETY: This function is called from C code. Caller must ensure:
9// 1. device is a valid VkDevice
10// 2. pAllocateInfo points to a valid VkMemoryAllocateInfo structure
11// 3. pAllocator is either null or points to valid allocation callbacks
12// 4. pMemory points to valid memory for writing the memory handle
13#[no_mangle]
14pub unsafe extern "C" fn vkAllocateMemory(
15    device: VkDevice,
16    pAllocateInfo: *const VkMemoryAllocateInfo,
17    pAllocator: *const VkAllocationCallbacks,
18    pMemory: *mut VkDeviceMemory,
19) -> VkResult {
20    if device.is_null() || pAllocateInfo.is_null() || pMemory.is_null() {
21        return VkResult::ErrorInitializationFailed;
22    }
23    
24    // Forward to real driver
25    if let Some(icd) = super::forward::get_icd_if_enabled() {
26        if let Some(allocate_memory) = icd.allocate_memory {
27            return allocate_memory(device, pAllocateInfo, pAllocator, pMemory);
28        }
29    }
30    
31    // No ICD available
32    VkResult::ErrorInitializationFailed
33}
34
35/// Free device memory
36// SAFETY: This function is called from C code. Caller must ensure:
37// 1. device is a valid VkDevice
38// 2. memory is a valid VkDeviceMemory allocated with vkAllocateMemory
39// 3. pAllocator matches the allocator used in vkAllocateMemory (or both are null)
40// 4. The memory is not currently mapped
41#[no_mangle]
42pub unsafe extern "C" fn vkFreeMemory(
43    device: VkDevice,
44    memory: VkDeviceMemory,
45    pAllocator: *const VkAllocationCallbacks,
46) {
47    if device.is_null() || memory.is_null() {
48        return;
49    }
50    
51    // Forward to real driver
52    if let Some(icd) = super::forward::get_icd_if_enabled() {
53        if let Some(free_memory) = icd.free_memory {
54            free_memory(device, memory, pAllocator);
55        }
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    // Forward to real driver
80    if let Some(icd) = super::forward::get_icd_if_enabled() {
81        if let Some(map_memory) = icd.map_memory {
82            return map_memory(device, memory, offset, size, flags, ppData);
83        }
84    }
85    
86    // No ICD available
87    VkResult::ErrorInitializationFailed
88}
89
90/// Unmap memory
91// SAFETY: This function is called from C code. Caller must ensure:
92// 1. device is a valid VkDevice
93// 2. memory is a valid VkDeviceMemory that is currently mapped
94// 3. Any host writes to the mapped memory are complete
95#[no_mangle]
96pub unsafe extern "C" fn vkUnmapMemory(
97    device: VkDevice,
98    memory: VkDeviceMemory,
99) {
100    if device.is_null() || memory.is_null() {
101        return;
102    }
103    
104    // Forward to real driver
105    if let Some(icd) = super::forward::get_icd_if_enabled() {
106        if let Some(unmap_memory) = icd.unmap_memory {
107            unmap_memory(device, memory);
108        }
109    }
110}