You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/system/system_info.hpp
  3. *
  4. * Purpose: Contains the basic_system_info class, which provides information
  5. * regarding the host system, such as number of processors and
  6. * page size.
  7. *
  8. * Created: 7th February 2002
  9. * Updated: 10th August 2009
  10. *
  11. * Home: http://stlsoft.org/
  12. *
  13. * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice, this
  20. * list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  25. * any contributors may be used to endorse or promote products derived from
  26. * this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * ////////////////////////////////////////////////////////////////////// */
  41. /** \file winstl/system/system_info.hpp
  42. *
  43. * \brief [C++ only] Definition of the winstl::system_info class
  44. * template
  45. * (\ref group__library__system "System" Library).
  46. */
  47. #ifndef WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_INFO
  48. #define WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_INFO
  49. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  50. # define WINSTL_VER_WINSTL_SYSTEM_HPP_SYSTEM_INFO_MAJOR 4
  51. # define WINSTL_VER_WINSTL_SYSTEM_HPP_SYSTEM_INFO_MINOR 0
  52. # define WINSTL_VER_WINSTL_SYSTEM_HPP_SYSTEM_INFO_REVISION 2
  53. # define WINSTL_VER_WINSTL_SYSTEM_HPP_SYSTEM_INFO_EDIT 57
  54. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Includes
  57. */
  58. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  59. # include <winstl/winstl.h>
  60. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _WINSTL_NO_NAMESPACE
  65. # if defined(_STLSOFT_NO_NAMESPACE) || \
  66. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  67. /* There is no stlsoft namespace, so must define ::winstl */
  68. namespace winstl
  69. {
  70. # else
  71. /* Define stlsoft::winstl_project */
  72. namespace stlsoft
  73. {
  74. namespace winstl_project
  75. {
  76. # endif /* _STLSOFT_NO_NAMESPACE */
  77. #endif /* !_WINSTL_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Classes
  80. */
  81. /** \brief Provides system information, which provides information regarding
  82. * the host system, such as number of processors and page size
  83. *
  84. * \ingroup group__library__system
  85. *
  86. * This class wraps the GetSystemInfo() API function. Since the information that
  87. * this function provides is constant for any particular active system for its
  88. * lifetime, the function is called only once, as implemented via the
  89. * get_systeminfo_() method.
  90. */
  91. class system_info
  92. {
  93. public:
  94. /// This type
  95. typedef system_info class_type;
  96. // Construction
  97. private:
  98. system_info();
  99. ~system_info() stlsoft_throw_0();
  100. // Operations
  101. public:
  102. /// Returns the number of processes on the host machine
  103. static ws_uint32_t number_of_processors()
  104. {
  105. return get_systeminfo_().dwNumberOfProcessors;
  106. }
  107. /// Returns the page size of the host machine
  108. static ws_size_t page_size()
  109. {
  110. return get_systeminfo_().dwPageSize;
  111. }
  112. /// Returns the allocation granularity of the host machine
  113. static ws_size_t allocation_granularity()
  114. {
  115. return get_systeminfo_().dwAllocationGranularity;
  116. }
  117. /// Provides a non-mutable (const) reference to the \c SYSTEM_INFO instance
  118. static SYSTEM_INFO const& get_systeminfo()
  119. {
  120. return get_systeminfo_();
  121. }
  122. // Implementation
  123. private:
  124. /// Unfortunately, something in this technique scares the Borland compilers (5.5
  125. /// and 5.51) into Internal compiler errors so the s_init variable in
  126. /// get_systeminfo_() is int rather than bool when compiling for borland.
  127. static SYSTEM_INFO const& get_systeminfo_()
  128. {
  129. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  130. _MSC_VER >= 1310
  131. // Safe to suppress these warnings, because race-conditions are benign here
  132. # pragma warning(push)
  133. # pragma warning(disable : 4640)
  134. #endif /* compiler */
  135. static SYSTEM_INFO s_systeminfo;
  136. #ifdef STLSOFT_COMPILER_IS_BORLAND
  137. /* WSCB: Borland has an internal compiler error if use ws_bool_t */
  138. static ws_int_t s_init = (::GetSystemInfo(&s_systeminfo), ws_true_v);
  139. #else /* ? compiler */
  140. static ws_bool_t s_init = (::GetSystemInfo(&s_systeminfo), ws_true_v);
  141. #endif /* compiler */
  142. STLSOFT_SUPPRESS_UNUSED(s_init);
  143. return s_systeminfo;
  144. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  145. _MSC_VER >= 1310
  146. # pragma warning(pop)
  147. #endif /* compiler */
  148. }
  149. };
  150. ////////////////////////////////////////////////////////////////////////////
  151. // Unit-testing
  152. #ifdef STLSOFT_UNITTEST
  153. # include "./unittest/system_info_unittest_.h"
  154. #endif /* STLSOFT_UNITTEST */
  155. /* ////////////////////////////////////////////////////////////////////// */
  156. #ifndef _WINSTL_NO_NAMESPACE
  157. # if defined(_STLSOFT_NO_NAMESPACE) || \
  158. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  159. } // namespace winstl
  160. # else
  161. } // namespace winstl_project
  162. } // namespace stlsoft
  163. # endif /* _STLSOFT_NO_NAMESPACE */
  164. #endif /* !_WINSTL_NO_NAMESPACE */
  165. /* ////////////////////////////////////////////////////////////////////// */
  166. #endif /* WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_INFO */
  167. /* ///////////////////////////// end of file //////////////////////////// */