Hugin trunk 0.1
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2* Copyright (C) 2007-2008 Anael Orlinski
3*
4* This file is part of Panomatic.
5*
6* Panomatic is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation; either version 2 of the License, or
9* (at your option) any later version.
10*
11* Panomatic is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with Panomatic; if not, write to the Free Software
18* <http://www.gnu.org/licenses/>.
19*/
20
21#include "Utils.h"
22#if defined(HW_NCPU) || defined(__APPLE__)
23#include <sys/sysctl.h>
24#endif
25#ifdef _WIN32
26#include <windows.h>
27#include <algorithm>
28#elif defined __APPLE__
29#include <CoreServices/CoreServices.h> //for gestalt
30#else
31#include <unistd.h>
32#endif
33
34#ifdef _WIN32
35unsigned long long utils::getTotalMemory()
36{
37 MEMORYSTATUSEX status;
38 status.dwLength = sizeof(status);
39 GlobalMemoryStatusEx(&status);
40#ifndef _WIN64
41 // when compiled as 32 bit version, we can only use about 2 GB
42 // even if we have more memory available on a 64 bit system
43 return std::min<unsigned long long>(status.ullTotalPhys, 1500*1024*1024);
44#else
45 return status.ullTotalPhys;
46#endif
47};
48#elif defined __APPLE__
49unsigned long long utils::getTotalMemory()
50{
53 {
54 unsigned long long _ramSize = ramSize;
55 return _ramSize * 1024 * 1024;
56 }
57 else
58 {
59 // if query was not successful return 1 GB,
60 // return 0 would result in crash in calling function
61 return 1024*1024*1024;
62 }
63};
64#else
65unsigned long long utils::getTotalMemory()
66{
69 return pages * page_size;
70}
71#endif
72
73#if defined _WIN32
74unsigned int utils::GetPhysicalCPUCount()
75{
76 // on Windows use GetLogicalProcessorInformationEx to get physical core count
78 DWORD length = 0;
79 while (1)
80 {
82 {
83 break;
84 };
86 {
87 return 1;
88 };
91 {
93 return 1;
94 }
96 };
97
98 unsigned int cpuCount = 0;
100 (void*)procInfo < (void*)((uintptr_t)procInfoTotal + length);
102 {
103 if (procInfo->Relationship == RelationProcessorCore)
104 {
105 ++cpuCount;
106 };
107 };
109 return cpuCount > 0 ? cpuCount : 1;
110}
111#elif defined(__APPLE__) || defined(__FreeBSD__)
112#include <sys/sysctl.h>
113
114unsigned int utils::GetPhysicalCPUCount()
115{
117 size_t num_cores_len = sizeof(num_cores);
118 sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
119 return num_cores;
120}
121#else
122// for Linux, read from /proc/cpuinfo
123#include <cstring>
124#include <stdio.h>
125
127{
128 char str[256];
129 int cpuCount = 0;
130 FILE* fp;
131 if ((fp = fopen("/proc/cpuinfo", "r")))
132 {
133 while (fgets(str, sizeof(str), fp))
134 {
135 if (memcmp(str, "core id", 7) == 0)
136 {
137 ++cpuCount;
138 };
139 };
140 fclose(fp);
141 };
142 return cpuCount > 0 ? cpuCount : 1;
143}
144#endif
unsigned long long getTotalMemory()
returns the total memory in byte
Definition Utils.cpp:65
unsigned int GetPhysicalCPUCount()
return the number of physical cpu cores
Definition Utils.cpp:126
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41