unity_native_plugin/
memory_manager.rs

1use crate::define_unity_interface;
2use crate::interface::UnityInterface;
3use std::ffi::{c_void, CStr};
4use std::ptr::null_mut;
5use unity_native_plugin_sys::*;
6
7
8define_unity_interface!(
9    UnityMemoryManager,
10    IUnityMemoryManager,
11    0xBAF9E57C61A811EC_u64,
12    0xC5A7CC7861A811EC_u64
13);
14
15pub struct UnityAllocator {
16    allocator: *mut unity_native_plugin_sys::UnityAllocator,
17    memory_manager: UnityMemoryManager,
18}
19
20impl Drop for UnityAllocator {
21    fn drop(&mut self) {
22        unsafe {
23            self.memory_manager.destroy_allocator(self.allocator);
24        }
25    }
26}
27
28impl UnityAllocator {
29    pub unsafe fn allocate(&self,
30                           size: usize,
31                           align: usize,
32                           file: &CStr,
33                           line: i32) -> *mut c_void {
34        unsafe {
35            self.memory_manager.allocate(self.allocator, size, align, file, line)
36        }
37    }
38
39    pub unsafe fn deallocate(&self,
40                             ptr: *mut c_void,
41                             file: &CStr,
42                             line: i32) {
43        unsafe {
44            self.memory_manager.deallocate(self.allocator, ptr, file, line)
45        }
46    }
47
48    pub unsafe fn reallocate(&self,
49                             ptr: *mut c_void,
50                             size: usize,
51                             align: usize,
52                             file: &CStr,
53                             line: i32) -> *mut c_void {
54        unsafe {
55            self.memory_manager.reallocate(self.allocator, ptr, size, align, file, line)
56        }
57    }
58}
59
60impl UnityMemoryManager {
61    pub unsafe fn create_allocator(&self, area_name: &CStr, object_name: &CStr) -> Option<UnityAllocator> {
62        unsafe {
63            let allocator = self.interface().CreateAllocator.expect("CreateAllocator")(area_name.as_ptr(), object_name.as_ptr());
64            if allocator != null_mut() {
65                Some(UnityAllocator { allocator: allocator, memory_manager: self.clone() })
66            } else {
67                None
68            }
69        }
70    }
71
72    pub(crate) unsafe fn destroy_allocator(&self, allocator: *mut unity_native_plugin_sys::UnityAllocator) {
73        unsafe {
74            self.interface().DestroyAllocator.expect("DestroyAllocator")(allocator)
75        }
76    }
77
78    pub(crate) unsafe fn allocate(&self,
79                                  allocator: *mut unity_native_plugin_sys::UnityAllocator,
80                                  size: usize,
81                                  align: usize,
82                                  file: &CStr,
83                                  line: i32) -> *mut c_void {
84        unsafe {
85            self.interface().Allocate.expect("Allocate")(allocator, size, align, file.as_ptr(), line)
86        }
87    }
88
89    pub(crate) unsafe fn deallocate(&self,
90                                    allocator: *mut unity_native_plugin_sys::UnityAllocator,
91                                    ptr: *mut c_void,
92                                    file: &CStr,
93                                    line: i32) {
94        unsafe {
95            self.interface().Deallocate.expect("Deallocate")(allocator, ptr, file.as_ptr(), line)
96        }
97    }
98
99    pub(crate) unsafe fn reallocate(&self,
100                                    allocator: *mut unity_native_plugin_sys::UnityAllocator,
101                                    ptr: *mut c_void,
102                                    size: usize,
103                                    align: usize,
104                                    file: &CStr,
105                                    line: i32) -> *mut c_void {
106        unsafe {
107            self.interface().Reallocate.expect("Reallocate")(allocator, ptr, size, align, file.as_ptr(), line)
108        }
109    }
110}