1 // Windows/System.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../Common/MyWindows.h"
6 
7 #include "../Common/Defs.h"
8 
9 #include "System.h"
10 
11 namespace NWindows {
12 namespace NSystem {
13 
CountAffinity(DWORD_PTR mask)14 UInt32 CountAffinity(DWORD_PTR mask)
15 {
16   UInt32 num = 0;
17   for (unsigned i = 0; i < sizeof(mask) * 8; i++)
18     num += (UInt32)((mask >> i) & 1);
19   return num;
20 }
21 
22 #ifdef _WIN32
23 
Get()24 BOOL CProcessAffinity::Get()
25 {
26   #ifndef UNDER_CE
27   return GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
28   #else
29   return FALSE;
30   #endif
31 }
32 
33 
GetNumberOfProcessors()34 UInt32 GetNumberOfProcessors()
35 {
36   // We need to know how many threads we can use.
37   // By default the process is assigned to one group.
38   // So we get the number of logical processors (threads)
39   // assigned to current process in the current group.
40   // Group size can be smaller than total number logical processors, for exammple, 2x36
41 
42   CProcessAffinity pa;
43 
44   if (pa.Get() && pa.processAffinityMask != 0)
45     return pa.GetNumProcessThreads();
46 
47   SYSTEM_INFO systemInfo;
48   GetSystemInfo(&systemInfo);
49   // the number of logical processors in the current group
50   return (UInt32)systemInfo.dwNumberOfProcessors;
51 }
52 
53 #else
54 
GetNumberOfProcessors()55 UInt32 GetNumberOfProcessors()
56 {
57   return 1;
58 }
59 
60 #endif
61 
62 
63 #ifdef _WIN32
64 
65 #ifndef UNDER_CE
66 
67 #if !defined(_WIN64) && defined(__GNUC__)
68 
69 typedef struct _MY_MEMORYSTATUSEX {
70   DWORD dwLength;
71   DWORD dwMemoryLoad;
72   DWORDLONG ullTotalPhys;
73   DWORDLONG ullAvailPhys;
74   DWORDLONG ullTotalPageFile;
75   DWORDLONG ullAvailPageFile;
76   DWORDLONG ullTotalVirtual;
77   DWORDLONG ullAvailVirtual;
78   DWORDLONG ullAvailExtendedVirtual;
79 } MY_MEMORYSTATUSEX, *MY_LPMEMORYSTATUSEX;
80 
81 #else
82 
83 #define MY_MEMORYSTATUSEX MEMORYSTATUSEX
84 #define MY_LPMEMORYSTATUSEX LPMEMORYSTATUSEX
85 
86 #endif
87 
88 typedef BOOL (WINAPI *GlobalMemoryStatusExP)(MY_LPMEMORYSTATUSEX lpBuffer);
89 
90 #endif
91 
92 #endif
93 
94 
GetRamSize(UInt64 & size)95 bool GetRamSize(UInt64 &size)
96 {
97   size = (UInt64)(sizeof(size_t)) << 29;
98 
99   #ifdef _WIN32
100 
101   #ifndef UNDER_CE
102     MY_MEMORYSTATUSEX stat;
103     stat.dwLength = sizeof(stat);
104   #endif
105 
106   #ifdef _WIN64
107 
108     if (!::GlobalMemoryStatusEx(&stat))
109       return false;
110     size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
111     return true;
112 
113   #else
114 
115     #ifndef UNDER_CE
116       GlobalMemoryStatusExP globalMemoryStatusEx = (GlobalMemoryStatusExP)
117           ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GlobalMemoryStatusEx");
118       if (globalMemoryStatusEx && globalMemoryStatusEx(&stat))
119       {
120         size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
121         return true;
122       }
123     #endif
124 
125     {
126       MEMORYSTATUS stat2;
127       stat2.dwLength = sizeof(stat2);
128       ::GlobalMemoryStatus(&stat2);
129       size = MyMin(stat2.dwTotalVirtual, stat2.dwTotalPhys);
130       return true;
131     }
132 
133   #endif
134 
135   #else
136 
137   return false;
138 
139   #endif
140 }
141 
142 }}
143