MemoryManagement.cs
1 using System; 2 3 namespace Ryujinx.Memory 4 { 5 public static class MemoryManagement 6 { 7 public static IntPtr Allocate(ulong size, bool forJit) 8 { 9 if (OperatingSystem.IsWindows()) 10 { 11 return MemoryManagementWindows.Allocate((IntPtr)size); 12 } 13 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 14 { 15 return MemoryManagementUnix.Allocate(size, forJit); 16 } 17 else 18 { 19 throw new PlatformNotSupportedException(); 20 } 21 } 22 23 public static IntPtr Reserve(ulong size, bool forJit, bool viewCompatible) 24 { 25 if (OperatingSystem.IsWindows()) 26 { 27 return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible); 28 } 29 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 30 { 31 return MemoryManagementUnix.Reserve(size, forJit); 32 } 33 else 34 { 35 throw new PlatformNotSupportedException(); 36 } 37 } 38 39 public static void Commit(IntPtr address, ulong size, bool forJit) 40 { 41 if (OperatingSystem.IsWindows()) 42 { 43 MemoryManagementWindows.Commit(address, (IntPtr)size); 44 } 45 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 46 { 47 MemoryManagementUnix.Commit(address, size, forJit); 48 } 49 else 50 { 51 throw new PlatformNotSupportedException(); 52 } 53 } 54 55 public static void Decommit(IntPtr address, ulong size) 56 { 57 if (OperatingSystem.IsWindows()) 58 { 59 MemoryManagementWindows.Decommit(address, (IntPtr)size); 60 } 61 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 62 { 63 MemoryManagementUnix.Decommit(address, size); 64 } 65 else 66 { 67 throw new PlatformNotSupportedException(); 68 } 69 } 70 71 public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size, MemoryBlock owner) 72 { 73 if (OperatingSystem.IsWindows()) 74 { 75 MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (IntPtr)size, owner); 76 } 77 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 78 { 79 MemoryManagementUnix.MapView(sharedMemory, srcOffset, address, size); 80 } 81 else 82 { 83 throw new PlatformNotSupportedException(); 84 } 85 } 86 87 public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, MemoryBlock owner) 88 { 89 if (OperatingSystem.IsWindows()) 90 { 91 MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size, owner); 92 } 93 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 94 { 95 MemoryManagementUnix.UnmapView(address, size); 96 } 97 else 98 { 99 throw new PlatformNotSupportedException(); 100 } 101 } 102 103 public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool throwOnFail) 104 { 105 bool result; 106 107 if (OperatingSystem.IsWindows()) 108 { 109 result = MemoryManagementWindows.Reprotect(address, (IntPtr)size, permission, forView); 110 } 111 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 112 { 113 result = MemoryManagementUnix.Reprotect(address, size, permission); 114 } 115 else 116 { 117 throw new PlatformNotSupportedException(); 118 } 119 120 if (!result && throwOnFail) 121 { 122 throw new MemoryProtectionException(permission); 123 } 124 } 125 126 public static bool Free(IntPtr address, ulong size) 127 { 128 if (OperatingSystem.IsWindows()) 129 { 130 return MemoryManagementWindows.Free(address, (IntPtr)size); 131 } 132 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 133 { 134 return MemoryManagementUnix.Free(address); 135 } 136 else 137 { 138 throw new PlatformNotSupportedException(); 139 } 140 } 141 142 public static IntPtr CreateSharedMemory(ulong size, bool reserve) 143 { 144 if (OperatingSystem.IsWindows()) 145 { 146 return MemoryManagementWindows.CreateSharedMemory((IntPtr)size, reserve); 147 } 148 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 149 { 150 return MemoryManagementUnix.CreateSharedMemory(size, reserve); 151 } 152 else 153 { 154 throw new PlatformNotSupportedException(); 155 } 156 } 157 158 public static void DestroySharedMemory(IntPtr handle) 159 { 160 if (OperatingSystem.IsWindows()) 161 { 162 MemoryManagementWindows.DestroySharedMemory(handle); 163 } 164 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 165 { 166 MemoryManagementUnix.DestroySharedMemory(handle); 167 } 168 else 169 { 170 throw new PlatformNotSupportedException(); 171 } 172 } 173 174 public static IntPtr MapSharedMemory(IntPtr handle, ulong size) 175 { 176 if (OperatingSystem.IsWindows()) 177 { 178 return MemoryManagementWindows.MapSharedMemory(handle); 179 } 180 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 181 { 182 return MemoryManagementUnix.MapSharedMemory(handle, size); 183 } 184 else 185 { 186 throw new PlatformNotSupportedException(); 187 } 188 } 189 190 public static void UnmapSharedMemory(IntPtr address, ulong size) 191 { 192 if (OperatingSystem.IsWindows()) 193 { 194 MemoryManagementWindows.UnmapSharedMemory(address); 195 } 196 else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) 197 { 198 MemoryManagementUnix.UnmapSharedMemory(address, size); 199 } 200 else 201 { 202 throw new PlatformNotSupportedException(); 203 } 204 } 205 } 206 }