YSTest: YCommon.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

YCommon.cpp
浏览该文件的文档.
1 /*
2 © 2009-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YCLib/YModules.h"
29 #include YFM_YCLib_Debug
30 #include <cstdlib> // for std::abort, std::system;
31 #include YFM_YCLib_NativeAPI // for ::swiWaitForVBlank, ::sysconf,
32 // _SC_PAGESIZE, _SC_SEM_NSEMS_MAX, _SC_SEM_VALUE_MAX, _SC_SYMLOOP_MAX,
33 // ::fifoSendValue32, ::SYSTEM_INFO, ::GetSystemInfo;
34 #include <limits> // for std::numeric_limits;
35 #include <limits.h> // for _POSIX_SEM_NSEMS_MAX, _POSIX_SYMLOOP_MAX,
36 // _POSIX_SEM_VALUE_MAX;
37 #if YCL_Win32
38 # include YFM_Win32_YCLib_MinGW32
39 # include <stdlib.h> // for ::_wsystem;
40 # include YFM_Win32_YCLib_NLS // for platform_ex::UTF8ToWCS;
41 #endif
42
43 namespace platform
44 {
45
46 void
47 terminate() ynothrow
48 {
49 #if YCL_DS
50 while(true)
51 ::swiWaitForVBlank();
52 #else
53 std::abort();
54 #endif
55 }
56
57 int
58 usystem(const char* cmd)
59 {
60 #if YCL_Win32
61 return ::_wsystem(platform_ex::UTF8ToWCS(cmd).c_str());
62 #else
63 return std::system(cmd);
64 #endif
65 }
66
67
68 size_t
69 FetchLimit(SystemOption opt) ynothrow
70 {
71 // NOTE: For constant expression value that is always less than max value
72 // of %size_t, there is no check needed.
73 // XXX: Values greater than maximum value of %size_t would be truncated.
74 const auto conf_conv([](long n){
75 using res_t = ystdex::common_type_t<long, size_t>;
76 return n >= 0 && res_t(n) < res_t(std::numeric_limits<size_t>::max())
77 ? size_t(n) : size_t(-1);
78 });
79
80 // NOTE: For POSIX-defined minimum values, if the macro is undefined,
81 // use the value specified in POSIX.1-2013 instead. See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html.
82 switch(opt)
83 {
84 case SystemOption::PageSize:
85 // NOTE: Value of 0 indicates the target system does not support paging.
86 // Though POSIX requires 1 as minimum acceptable value, it is intended
87 // here. For platforms without an appropriate page size, it should be
88 // specified here explicitly, rather than relying on the system library
89 // with POSIX compliance. For example, RTEMS: https://devel.rtems.org/ticket/1216.
90 #if YCL_DS
91 return 0;
92 #elif YCL_Win32
93 ::SYSTEM_INFO info;
94 ::GetSystemInfo(&info);
95
96 return size_t(info.dwPageSize);
97 #elif defined(_SC_PAGESIZE)
98 return conf_conv(::sysconf(_SC_PAGESIZE));
99 #else
100 // XXX: %::getpagesize is not used. Fallback to POSIX allowed minimum
101 // value.
102 return 1;
103 #endif
104 case SystemOption::MaxSemaphoreNumber:
105 #if YCL_DS
106 return 0;
107 #elif YCL_Win32
108 return size_t(-1);
109 #elif defined(_SC_SEM_NSEMS_MAX)
110 return conf_conv(::sysconf(_SC_SEM_NSEMS_MAX));
111 #elif defined(_POSIX_SEM_NSEMS_MAX)
112 static_assert(size_t(_POSIX_SEM_NSEMS_MAX) == _POSIX_SEM_NSEMS_MAX,
113 "Invalid value found.");
114
115 return _POSIX_SEM_NSEMS_MAX;
116 #else
117 return 256;
118 #endif
119 case SystemOption::MaxSemaphoreValue:
120 #if YCL_Win32
121 static_assert(size_t(std::numeric_limits<long>::max())
122 == std::numeric_limits<long>::max(), "Invalid value found.");
123
124 return size_t(std::numeric_limits<long>::max());
125 #elif defined(_SC_SEM_VALUE_MAX)
126 return conf_conv(::sysconf(_SC_SEM_VALUE_MAX));
127 #elif defined(_POSIX_SEM_VALUE_MAX)
128 static_assert(size_t(_POSIX_SEM_VALUE_MAX) == _POSIX_SEM_VALUE_MAX,
129 "Invalid value found.");
130
131 return _POSIX_SEM_VALUE_MAX;
132 #else
133 return 256;
134 #endif
135 case SystemOption::MaxSymlinkLoop:
136 #if YCL_DS
137 return 0;
138 #elif defined(_SC_SYMLOOP_MAX)
139 return conf_conv(::sysconf(_SC_SYMLOOP_MAX));
140 #elif defined(_POSIX_SYMLOOP_MAX)
141 static_assert(size_t(_POSIX_SYMLOOP_MAX) == _POSIX_SYMLOOP_MAX,
142 "Invalid value found.");
143
144 return _POSIX_SYMLOOP_MAX;
145 #else
146 // NOTE: Windows has nothing about this concept. Win32 error
147 // %ERROR_CANT_RESOLVE_FILENAME maps to Windows NT error
148 // %STATUS_REPARSE_POINT_NOT_RESOLVED, which seems to be not
149 // necessarily related to a fixed loop limit. To keep POSIX compliant
150 // and better performance of loop detection, minimum allowed value is
151 // used. See also https://cygwin.com/ml/cygwin/2009-03/msg00335.html.
152 // TODO: Make it configurable for Windows?
153 return 8;
154 #endif
155 default:
156 break;
157 }
158 #if !YCL_DS
159 YTraceDe(Descriptions::Warning,
160 "Configuration option '%zu' is not handled.", size_t(opt));
161 errno = EINVAL;
162 #endif
163 return size_t(-1);
164 }
165
166 } // namespace platform;
167
168 namespace platform_ex
169 {
170
171 #if YCL_DS
172 bool
173 AllowSleep(bool b)
174 {
175 // NOTE: Synchronized with ARM7 power management.
176 static bool b_sleep_enabled(true);
177 const bool b_old(b_sleep_enabled);
178
179 if(b != b_sleep_enabled)
180 {
181 b_sleep_enabled = b;
182 ::fifoSendValue32(FIFO_PM,
183 b ? PM_REQ_SLEEP_ENABLE : PM_REQ_SLEEP_DISABLE);
184 }
185 return b_old;
186 }
187 #elif YCL_Win32
188
189 #endif
190
191 }
192
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:313
platform::FetchLimit
YF_API size_t FetchLimit(SystemOption) ynothrow
取限制配置。
Definition: YCommon.cpp:69
platform
默认平台命名空间。
Definition: Container.h:47
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:131
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:549
platform::usystem
YF_API int usystem(const char *)
执行 UTF-8 字符串的环境命令。
Definition: YCommon.cpp:58
platform::SystemOption::MaxSemaphoreValue
可具有的信号量的最大值。
YModules.h
platform::SystemOption::MaxSemaphoreNumber
一个进程中可具有的信号量的最大数量。
platform_ex::Windows::UTF8ToWCS
UTF8ToWCS
Definition: NLS.h:72
platform::Descriptions::Warning
Definition: YCommon.h:167
platform::terminate
YB_NORETURN YF_API void terminate() ynothrow
异常终止函数。
Definition: YCommon.cpp:47
platform_ex
平台扩展命名空间。
Definition: Android.h:45
platform::SystemOption
SystemOption
系统配置选项。
Definition: YCommon.h:282
platform::SystemOption::MaxSymlinkLoop
在路径解析中符号链接可被可靠遍历的最大次数。
platform::SystemOption::PageSize
内存页面字节数。
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:274