]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.nativemem/src/org/simantics/nativemem/NativeMem.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.nativemem / src / org / simantics / nativemem / NativeMem.java
1 package org.simantics.nativemem;
2
3 import org.simantics.nativemem.internal.Arch;
4 import org.simantics.nativemem.internal.OS;
5 import org.simantics.nativemem.internal.Psapi32;
6 import org.simantics.nativemem.internal.Psapi64;
7
8 import com.sun.jna.platform.win32.Kernel32;
9 import com.sun.jna.platform.win32.WinNT.HANDLE;
10
11
12 public class NativeMem {
13
14     /**
15      * @param out
16      *            the structure to write the result into or <code>null</code> to
17      *            create a new structure
18      * @return the result structure
19      */
20     public static ProcessMemoryCounters getMemoryCounters(ProcessMemoryCounters out) {
21         if (out == null)
22             out = new ProcessMemoryCounters();
23
24         OS os = OS.calculate();
25         Arch arch = Arch.calculate();
26         switch (os) {
27         case WINDOWS: {
28             HANDLE proc = Kernel32.INSTANCE.GetCurrentProcess();
29             switch (arch) {
30             case X86: {
31                 Psapi32.PROCESS_MEMORY_COUNTERS_EX pmem = new Psapi32.PROCESS_MEMORY_COUNTERS_EX();
32                 boolean ok = Psapi32.INSTANCE.GetProcessMemoryInfo(proc, pmem, pmem.size());
33                 if (ok)
34                     pmem.writeTo(out);
35                 return out;
36             }
37
38             case X86_64: {
39                 Psapi64.PROCESS_MEMORY_COUNTERS_EX pmem = new Psapi64.PROCESS_MEMORY_COUNTERS_EX();
40                 boolean ok = Psapi64.INSTANCE.GetProcessMemoryInfo(proc, pmem, pmem.size());
41                 if (ok)
42                     pmem.writeTo(out);
43                 return out;
44             }
45
46             default:
47                 throw new UnsupportedOperationException("Architecture " + arch + " not supported on operating system " + os);
48             }
49         }
50
51         default:
52             throw new UnsupportedOperationException("Operating system " + os + " not supported");
53         }
54     }
55
56 }