Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / ogdf / basic / System.h
blobbd8db4670aaacc5ad6130bf8a22708094ed7077b
1 /*
2 * $Revision: 2523 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Decalration of System class which provides unified
11 * access to system information.
13 * \author Carsten Gutwenger
15 * \par License:
16 * This file is part of the Open Graph Drawing Framework (OGDF).
18 * \par
19 * Copyright (C)<br>
20 * See README.txt in the root directory of the OGDF installation for details.
22 * \par
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * Version 2 or 3 as published by the Free Software Foundation;
26 * see the file LICENSE.txt included in the packaging of this file
27 * for details.
29 * \par
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * \par
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
41 * \see http://www.gnu.org/copyleft/gpl.html
42 ***************************************************************/
45 #ifdef _MSC_VER
46 #pragma once
47 #endif
49 #ifndef OGDF_SYSTEM_H
50 #define OGDF_SYSTEM_H
53 #include <ogdf/basic/basic.h>
54 #if defined(OGDF_SYSTEM_OSX)
55 #include <stdlib.h>
56 #elif defined(OGDF_SYSTEM_UNIX) || defined(__MINGW32__)
57 #include <malloc.h>
58 #endif
60 // detect processor architecture we're compiling for
62 // OGDF_ARCH_X86 Intel / AMD x86 32-bit processors
63 // OGDF_ARCH_X64 Intel / AMD x86 64-bit processors
64 // OGDF_ARCH_IA64 Intel Itanium
65 // OGDF_ARCH_PPC PowerPC
66 // OGDF_ARCH_SPARC SUN SPARC
67 // OGDF_ARCH_SPARC_V9 SUN SPARC V9
69 #if defined(_M_X64) || defined(__x86_64__)
70 #define OGDF_ARCH_X64
71 #elif defined(_M_IX86) || defined(__i386__)
72 #define OGDF_ARCH_X86
73 #elif defined(_M_IA64) || defined(__ia64__)
74 #define OGDF_ARCH_IA64
75 #elif defined(_M_MPPC) || defined(_M_PPC) || defined(__powerpc__)
76 #define OGDF_ARCH_PPC
77 #elif defined(__sparc__)
78 #define OGDF_ARCH_SPARC
79 #elif defined(__sparc_v9__)
80 #define OGDF_ARCH_SPARC_V9
81 #endif
83 // use SSE2 always if x64-platform or compiler option is set
84 #ifndef OGDF_USE_SSE2
85 #if defined(OGDF_ARCH_X64)
86 #define OGDF_USE_SSE2
87 #elif defined(_MSC_VER)
88 #if _M_IX86_FP >= 2
89 #define OGDF_USE_SSE2
90 #endif
91 #endif
92 #endif
94 // macros to check for using special cpu features
96 // OGDF_CHECK_SSE2 returns true if SSE2 can be used
98 #ifdef OGDF_USE_SSE2
99 #define OGDF_CHECK_SSE2 true
100 #elif defined(OGDF_ARCH_X86)
101 #define OGDF_CHECK_SSE2 ogdf::System::cpuSupports(ogdf::cpufSSE2)
102 #else
103 #define OGDF_USE_SSE2 false
104 #endif
106 // work-around for MinGW-w64
107 #ifdef __MINGW64__
108 #ifndef _aligned_free
109 #define _aligned_free(a) __mingw_aligned_free(a)
110 #endif
111 #ifndef _aligned_malloc
112 #define _aligned_malloc(a,b) __mingw_aligned_malloc(a,b)
113 #endif
114 #endif
117 namespace ogdf {
119 //! Special features supported by a x86/x64 CPU.
121 * This enumeration is used to specify spcial additional features that
122 * are supported by the CPU, in particular extended instruction sets
123 * such as SSE.
125 enum CPUFeature {
126 cpufMMX, //!< Intel MMX Technology
127 cpufSSE, //!< Streaming SIMD Extensions (SSE)
128 cpufSSE2, //!< Streaming SIMD Extensions 2 (SSE2)
129 cpufSSE3, //!< Streaming SIMD Extensions 3 (SSE3)
130 cpufSSSE3, //!< Supplemental Streaming SIMD Extensions 3 (SSSE3)
131 cpufSSE4_1, //!< Streaming SIMD Extensions 4.1 (SSE4.1)
132 cpufSSE4_2, //!< Streaming SIMD Extensions 4.2 (SSE4.2)
133 cpufVMX, //!< Virtual Machine Extensions
134 cpufSMX, //!< Safer Mode Extensions
135 cpufEST, //!< Enhanced Intel SpeedStep Technology
136 cpufMONITOR //!< Processor supports MONITOR/MWAIT instructions
139 //! Bit mask for CPU features.
140 enum CPUFeatureMask {
141 cpufmMMX = 1 << cpufMMX, //!< Intel MMX Technology
142 cpufmSSE = 1 << cpufSSE, //!< Streaming SIMD Extensions (SSE)
143 cpufmSSE2 = 1 << cpufSSE2, //!< Streaming SIMD Extensions 2 (SSE2)
144 cpufmSSE3 = 1 << cpufSSE3, //!< Streaming SIMD Extensions 3 (SSE3)
145 cpufmSSSE3 = 1 << cpufSSSE3, //!< Supplemental Streaming SIMD Extensions 3 (SSSE3)
146 cpufmSSE4_1 = 1 << cpufSSE4_1, //!< Streaming SIMD Extensions 4.1 (SSE4.1)
147 cpufmSSE4_2 = 1 << cpufSSE4_2, //!< Streaming SIMD Extensions 4.2 (SSE4.2)
148 cpufmVMX = 1 << cpufVMX, //!< Virtual Machine Extensions
149 cpufmSMX = 1 << cpufSMX, //!< Safer Mode Extensions
150 cpufmEST = 1 << cpufEST, //!< Enhanced Intel SpeedStep Technology
151 cpufmMONITOR = 1 << cpufMONITOR //!< Processor supports MONITOR/MWAIT instructions
155 //! %System specific functionality.
157 * The class System encapsulates system specific functions
158 * providing unified access across different operating systems.
159 * The provided functionality includes:
160 * - Access to file system functionality (listing directories etc.).
161 * - Query memory usage.
162 * - Access to high-perfomance counter under Windows and Cygwin.
163 * - Query CPU specific information.
165 class OGDF_EXPORT System {
167 //friend class ::OgdfInitialization;
169 public:
171 * @name Memory usage
172 * These methods allow to query the amount of physical memory, as well as the
173 * current memory usage by both the process and OGDF's internal memory manager.
175 //@{
177 static void *alignedMemoryAlloc16(size_t size) {
178 size_t alignment = 16;
179 #ifdef OGDF_SYSTEM_WINDOWS
180 return _aligned_malloc(size,alignment);
181 #elif defined(OGDF_SYSTEM_OSX)
182 // malloc returns 16 byte aligned memory on OS X.
183 return malloc(size);
184 #else
185 return memalign(alignment,size);
186 #endif
189 static void alignedMemoryFree(void *p) {
190 #ifdef OGDF_SYSTEM_WINDOWS
191 _aligned_free(p);
192 #else
193 free(p);
194 #endif
197 //! Returns the page size of virtual memory (in bytes).
198 static int pageSize() { return s_pageSize; }
200 //! Returns the total size of physical memory (in bytes).
201 static long long physicalMemory();
203 //! Returns the size of available (free) physical memory (in bytes).
204 static long long availablePhysicalMemory();
206 //! Returns the amount of memory (in bytes) allocated by the process.
207 static size_t memoryUsedByProcess();
209 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
210 //! Returns the maximal amount of memory (in bytes) used by the process (Windows/Cygwin only).
211 static size_t peakMemoryUsedByProcess();
212 #endif
214 //! Returns the amount of memory (in bytes) allocated by OGDF's memory manager.
216 * The memory manager allocates blocks of a fixed size from the system (via malloc())
217 * and makes it available in its free lists (for allocating small pieces of memory.
218 * The returned value is the total amount of memory allocated from the system;
219 * the amount of memory currently allocated from the user is
220 * memoryAllocatedByMemoryManager() - memoryInFreelistOfMemoryManager().
222 * Keep in mind that the memory manager never releases memory to the system before
223 * its destruction.
225 static size_t memoryAllocatedByMemoryManager();
227 //! Returns the amount of memory (in bytes) contained in the global free list of OGDF's memory manager.
228 static size_t memoryInGlobalFreeListOfMemoryManager();
230 //! Returns the amount of memory (in bytes) contained in the thread's free list of OGDF's memory manager.
231 static size_t memoryInThreadFreeListOfMemoryManager();
233 //! Returns the amount of memory (in bytes) allocated on the heap (e.g., with malloc).
235 * This refers to dynamically allocated memory, e.g., memory allocated with malloc()
236 * or new.
238 static size_t memoryAllocatedByMalloc();
240 //! Returns the amount of memory (in bytes) contained in free chunks on the heap.
242 * This refers to memory that has been deallocated with free() or delete, but has not
243 * yet been returned to the operating system.
245 static size_t memoryInFreelistOfMalloc();
247 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
248 //@}
250 * @name Measuring time
251 * These methods provide various ways to measure time. The high-performance
252 * counter (Windows and Cygwin only) can be used to measure real time
253 * periods with a better resolution than the standard system time function.
255 //@{
257 //! Returns the current value of the high-performance counter in \a counter.
258 static void getHPCounter(LARGE_INTEGER &counter);
260 //! Returns the elapsed time (in seconds) between \a startCounter and \a endCounter.
261 static double elapsedSeconds(
262 const LARGE_INTEGER &startCounter,
263 const LARGE_INTEGER &endCounter);
264 #endif
266 //! Returns the elapsed time (in milliseconds) between \a t and now.
268 * The functions sets \a t to to the current time. Usually, you first call
269 * usedRealTime(t) to query the start time \a t, and determine the elapsed time
270 * after performing some computation by calling usedRealTime(t) again; this time
271 * the return value gives you the elapsed time in milliseconds.
273 static __int64 usedRealTime(__int64 &t);
276 //@}
278 * @name Processor information
279 * These methods allow to query information about the current processor such as
280 * supported instruction sets (e.g., SSE extensions), cache size, and number of
281 * installed processors.
283 //@{
285 //! Returns the bit vector describing the CPU features supported on current system.
286 static int cpuFeatures() { return s_cpuFeatures; }
288 //! Returns true if the CPU supports \a feature.
289 static bool cpuSupports(CPUFeature feature) {
290 return (s_cpuFeatures & (1 << feature)) != 0;
293 //! Returns the L2-cache size (in KBytes).
294 static int cacheSizeKBytes() { return s_cacheSize; }
296 //! Returns the number of bytes in a cache line.
297 static int cacheLineBytes() { return s_cacheLine; }
299 //! Returns the number of processors (cores) available on the current system.
300 static int numberOfProcessors() { return s_numberOfProcessors; }
302 //@}
304 private:
305 static unsigned int s_cpuFeatures; //!< Supported CPU features.
306 static int s_cacheSize; //!< Cache size in KBytes.
307 static int s_cacheLine; //!< Bytes in a cache line.
308 static int s_numberOfProcessors; //!< Number of processors (cores) available.
309 static int s_pageSize; //!< The page size of virtual memory.
311 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
312 static LARGE_INTEGER s_HPCounterFrequency; //!< Frequency of high-performance counter.
313 #endif
315 public:
316 //! Static initilization routine (automatically called).
317 static void init();
321 } // end namespace ogdf
324 #endif