Hardware/Opcode.cs
changeset 239 91800d0f54de
parent 238 bddc6e01840a
child 344 3145aadca3d2
     1.1 --- a/Hardware/Opcode.cs	Wed Nov 03 22:07:46 2010 +0000
     1.2 +++ b/Hardware/Opcode.cs	Thu Nov 04 21:46:14 2010 +0000
     1.3 @@ -37,7 +37,7 @@
     1.4  
     1.5  using System;
     1.6  using System.Runtime.InteropServices;
     1.7 -using Mono.Unix.Native;
     1.8 +using System.Reflection;
     1.9  
    1.10  namespace OpenHardwareMonitor.Hardware {
    1.11    internal static class Opcode {
    1.12 @@ -64,11 +64,28 @@
    1.13        }
    1.14        
    1.15        size = (ulong)(rdtscCode.Length + cpuidCode.Length);
    1.16 -      
    1.17 -      if ((p == 4) || (p == 128)) { // Unix
    1.18 -        codeBuffer = Syscall.mmap(IntPtr.Zero, size, 
    1.19 -          MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC, 
    1.20 -          MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_PRIVATE, -1, 0);
    1.21 +
    1.22 +      if ((p == 4) || (p == 128)) { // Unix   
    1.23 +        Assembly assembly = 
    1.24 +          Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
    1.25 +          "PublicKeyToken=0738eb9f132ed756");
    1.26 +
    1.27 +        Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
    1.28 +        MethodInfo mmap = syscall.GetMethod("mmap");
    1.29 +
    1.30 +        Type mmapProts = assembly.GetType("Mono.Unix.Native.MmapProts");
    1.31 +        object mmapProtsParam = Enum.ToObject(mmapProts,
    1.32 +          (int)mmapProts.GetField("PROT_READ").GetValue(null) |
    1.33 +          (int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
    1.34 +          (int)mmapProts.GetField("PROT_EXEC").GetValue(null));
    1.35 +
    1.36 +        Type mmapFlags = assembly.GetType("Mono.Unix.Native.MmapFlags");
    1.37 +        object mmapFlagsParam = Enum.ToObject(mmapFlags,
    1.38 +          (int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
    1.39 +          (int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));
    1.40 +        
    1.41 +        codeBuffer = (IntPtr)mmap.Invoke(null, new object[] { IntPtr.Zero, 
    1.42 +          size, mmapProtsParam, mmapFlagsParam, -1, 0 });        
    1.43        } else { // Windows
    1.44          codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
    1.45            (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE, 
    1.46 @@ -93,7 +110,14 @@
    1.47        
    1.48        int p = (int)Environment.OSVersion.Platform;
    1.49        if ((p == 4) || (p == 128)) { // Unix
    1.50 -        Syscall.munmap(codeBuffer, size);
    1.51 +        Assembly assembly =
    1.52 +          Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
    1.53 +          "PublicKeyToken=0738eb9f132ed756");
    1.54 +
    1.55 +        Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
    1.56 +        MethodInfo munmap = syscall.GetMethod("munmap");
    1.57 +        munmap.Invoke(null, new object[] { codeBuffer, size });
    1.58 +
    1.59        } else { // Windows
    1.60          NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero, 
    1.61            FreeType.RELEASE);        
    1.62 @@ -246,7 +270,7 @@
    1.63      }
    1.64  
    1.65      [Flags]
    1.66 -    enum FreeType {
    1.67 +    public enum FreeType {
    1.68        DECOMMIT = 0x4000,
    1.69        RELEASE = 0x8000
    1.70      }