unity_native_plugin/
memory_manager.rs1use crate::interface::UnityInterface;
2use std::ffi::{CStr, c_void};
3
4define_unity_interface!(
5 UnityMemoryManager,
6 unity_native_plugin_sys::IUnityMemoryManager,
7 0xBAF9E57C61A811EC_u64,
8 0xC5A7CC7861A811EC_u64
9);
10
11pub trait UnityMemoryManagerInterface {
12 unsafe fn create_allocator(
16 &self,
17 area_name: &CStr,
18 object_name: &CStr,
19 ) -> Option<UnityAllocator>;
20}
21
22pub use UnityMemoryManagerInterface as IUnityMemoryManager;
23
24pub struct UnityAllocator {
25 allocator: *mut unity_native_plugin_sys::UnityAllocator,
26 memory_manager: UnityMemoryManager,
27}
28
29impl Drop for UnityAllocator {
30 fn drop(&mut self) {
31 unsafe {
32 self.memory_manager.destroy_allocator(self.allocator);
33 }
34 }
35}
36
37impl UnityAllocator {
38 pub unsafe fn allocate(
39 &self,
40 size: usize,
41 align: usize,
42 file: &CStr,
43 line: i32,
44 ) -> *mut c_void {
45 unsafe {
46 self.memory_manager
47 .allocate(self.allocator, size, align, file, line)
48 }
49 }
50
51 pub unsafe fn deallocate(&self, ptr: *mut c_void, file: &CStr, line: i32) {
52 unsafe {
53 self.memory_manager
54 .deallocate(self.allocator, ptr, file, line)
55 }
56 }
57
58 pub unsafe fn reallocate(
59 &self,
60 ptr: *mut c_void,
61 size: usize,
62 align: usize,
63 file: &CStr,
64 line: i32,
65 ) -> *mut c_void {
66 unsafe {
67 self.memory_manager
68 .reallocate(self.allocator, ptr, size, align, file, line)
69 }
70 }
71}
72
73impl UnityMemoryManagerInterface for UnityMemoryManager {
74 unsafe fn create_allocator(
75 &self,
76 area_name: &CStr,
77 object_name: &CStr,
78 ) -> Option<UnityAllocator> {
79 unsafe {
80 let allocator = self.interface().CreateAllocator.expect("CreateAllocator")(
81 area_name.as_ptr(),
82 object_name.as_ptr(),
83 );
84 if !allocator.is_null() {
85 Some(UnityAllocator {
86 allocator,
87 memory_manager: *self,
88 })
89 } else {
90 None
91 }
92 }
93 }
94}
95
96impl UnityMemoryManager {
97 pub(crate) unsafe fn destroy_allocator(
98 &self,
99 allocator: *mut unity_native_plugin_sys::UnityAllocator,
100 ) {
101 unsafe { self.interface().DestroyAllocator.expect("DestroyAllocator")(allocator) }
102 }
103
104 pub(crate) unsafe fn allocate(
105 &self,
106 allocator: *mut unity_native_plugin_sys::UnityAllocator,
107 size: usize,
108 align: usize,
109 file: &CStr,
110 line: i32,
111 ) -> *mut c_void {
112 unsafe {
113 self.interface().Allocate.expect("Allocate")(
114 allocator,
115 size,
116 align,
117 file.as_ptr(),
118 line,
119 )
120 }
121 }
122
123 pub(crate) unsafe fn deallocate(
124 &self,
125 allocator: *mut unity_native_plugin_sys::UnityAllocator,
126 ptr: *mut c_void,
127 file: &CStr,
128 line: i32,
129 ) {
130 unsafe {
131 self.interface().Deallocate.expect("Deallocate")(allocator, ptr, file.as_ptr(), line)
132 }
133 }
134
135 pub(crate) unsafe fn reallocate(
136 &self,
137 allocator: *mut unity_native_plugin_sys::UnityAllocator,
138 ptr: *mut c_void,
139 size: usize,
140 align: usize,
141 file: &CStr,
142 line: i32,
143 ) -> *mut c_void {
144 unsafe {
145 self.interface().Reallocate.expect("Reallocate")(
146 allocator,
147 ptr,
148 size,
149 align,
150 file.as_ptr(),
151 line,
152 )
153 }
154 }
155}