kronos_compute/implementation/
memory.rs

1//! REAL Kronos memory implementation - NO ICD forwarding!
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use std::ptr;
7use std::sync::atomic::{AtomicU64, Ordering};
8use std::sync::Mutex;
9use std::collections::HashMap;
10
11// Memory handle counter
12static MEMORY_COUNTER: AtomicU64 = AtomicU64::new(1);
13
14// Registry of active memory allocations
15lazy_static::lazy_static! {
16    static ref MEMORY_ALLOCS: Mutex<HashMap<u64, MemoryData>> = Mutex::new(HashMap::new());
17}
18
19struct MemoryData {
20    device: VkDevice,
21    size: VkDeviceSize,
22    memory_type_index: u32,
23    data: Vec<u8>,
24    mapped: bool,
25}
26
27/// Allocate device memory - REAL implementation
28#[no_mangle]
29pub unsafe extern "C" fn vkAllocateMemory(
30    device: VkDevice,
31    pAllocateInfo: *const VkMemoryAllocateInfo,
32    _pAllocator: *const VkAllocationCallbacks,
33    pMemory: *mut VkDeviceMemory,
34) -> VkResult {
35    log::info!("=== KRONOS vkAllocateMemory called (Pure Rust) ===");
36    
37    if device.is_null() || pAllocateInfo.is_null() || pMemory.is_null() {
38        return VkResult::ErrorInitializationFailed;
39    }
40    
41    let alloc_info = &*pAllocateInfo;
42    
43    // Validate allocation size
44    if alloc_info.allocationSize == 0 {
45        return VkResult::ErrorInitializationFailed;
46    }
47    
48    // Create memory handle
49    let handle = MEMORY_COUNTER.fetch_add(1, Ordering::SeqCst);
50    
51    // Allocate actual memory
52    let data = vec![0u8; alloc_info.allocationSize as usize];
53    
54    // Store memory data
55    let memory_data = MemoryData {
56        device,
57        size: alloc_info.allocationSize,
58        memory_type_index: alloc_info.memoryTypeIndex,
59        data,
60        mapped: false,
61    };
62    
63    MEMORY_ALLOCS.lock().unwrap().insert(handle, memory_data);
64    
65    *pMemory = VkDeviceMemory::from_raw(handle);
66    
67    log::info!("Allocated {} bytes of memory as handle {:?}", alloc_info.allocationSize, handle);
68    
69    VkResult::Success
70}
71
72/// Free device memory
73#[no_mangle]
74pub unsafe extern "C" fn vkFreeMemory(
75    device: VkDevice,
76    memory: VkDeviceMemory,
77    _pAllocator: *const VkAllocationCallbacks,
78) {
79    if device.is_null() || memory.is_null() {
80        return;
81    }
82    
83    let handle = memory.as_raw();
84    MEMORY_ALLOCS.lock().unwrap().remove(&handle);
85    
86    log::info!("Freed memory {:?}", handle);
87}
88
89/// Map device memory
90#[no_mangle]
91pub unsafe extern "C" fn vkMapMemory(
92    device: VkDevice,
93    memory: VkDeviceMemory,
94    offset: VkDeviceSize,
95    size: VkDeviceSize,
96    _flags: VkMemoryMapFlags,
97    ppData: *mut *mut std::ffi::c_void,
98) -> VkResult {
99    if device.is_null() || memory.is_null() || ppData.is_null() {
100        return VkResult::ErrorInitializationFailed;
101    }
102    
103    let handle = memory.as_raw();
104    if let Some(memory_data) = MEMORY_ALLOCS.lock().unwrap().get_mut(&handle) {
105        // Validate offset and size
106        let map_size = if size == VK_WHOLE_SIZE {
107            memory_data.size - offset
108        } else {
109            size
110        };
111        
112        if offset + map_size > memory_data.size {
113            return VkResult::ErrorMemoryMapFailed;
114        }
115        
116        // Return pointer to our data
117        let ptr = memory_data.data.as_mut_ptr().add(offset as usize);
118        *ppData = ptr as *mut std::ffi::c_void;
119        
120        memory_data.mapped = true;
121        
122        log::info!("Mapped memory {:?} at offset {} size {}", handle, offset, map_size);
123        
124        VkResult::Success
125    } else {
126        VkResult::ErrorMemoryMapFailed
127    }
128}
129
130/// Unmap device memory
131#[no_mangle]
132pub unsafe extern "C" fn vkUnmapMemory(
133    device: VkDevice,
134    memory: VkDeviceMemory,
135) {
136    if device.is_null() || memory.is_null() {
137        return;
138    }
139    
140    let handle = memory.as_raw();
141    if let Some(memory_data) = MEMORY_ALLOCS.lock().unwrap().get_mut(&handle) {
142        memory_data.mapped = false;
143        log::info!("Unmapped memory {:?}", handle);
144    }
145}