Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
utils_macos.cc
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_MACOS)
7
8#include "platform/utils.h"
10
11#include <errno.h> // NOLINT
12#include <sys/utsname.h> // NOLINT
13
14namespace dart {
15
16char* Utils::StrNDup(const char* s, intptr_t n) {
17// strndup has only been added to Mac OS X in 10.7. We are supplying
18// our own copy here if needed.
19#if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
20 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060
21 intptr_t len = strlen(s);
22 if ((n < 0) || (len < 0)) {
23 return nullptr;
24 }
25 if (n < len) {
26 len = n;
27 }
28 char* result = reinterpret_cast<char*>(malloc(len + 1));
29 result[len] = '\0';
30 return reinterpret_cast<char*>(memmove(result, s, len));
31#else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
32 return strndup(s, n);
33#endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
34}
35
36char* Utils::StrDup(const char* s) {
37 return strdup(s);
38}
39
40intptr_t Utils::StrNLen(const char* s, intptr_t n) {
41// strnlen has only been added to Mac OS X in 10.7. We are supplying
42// our own copy here if needed.
43#if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
44 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060
45 intptr_t len = 0;
46 while ((len <= n) && (*s != '\0')) {
47 s++;
48 len++;
49 }
50 return len;
51#else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
52 return strnlen(s, n);
53#endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
54}
55
56int Utils::SNPrint(char* str, size_t size, const char* format, ...) {
57 va_list args;
59 int retval = VSNPrint(str, size, format, args);
60 va_end(args);
61 return retval;
62}
63
64int Utils::VSNPrint(char* str, size_t size, const char* format, va_list args) {
65 int retval = vsnprintf(str, size, format, args);
66 if (retval < 0) {
67 FATAL("Fatal error in Utils::VSNPrint with format '%s'", format);
68 }
69 return retval;
70}
71
72int Utils::Close(int fildes) {
73 return close(fildes);
74}
75size_t Utils::Read(int filedes, void* buf, size_t nbyte) {
76 return read(filedes, buf, nbyte);
77}
78int Utils::Unlink(const char* path) {
79 return unlink(path);
80}
81
82namespace internal {
83
84// Returns the running system's Darwin major version. Don't call this, it's
85// an implementation detail and its result is meant to be cached by
86// MacOSXMinorVersion.
87int32_t DarwinMajorVersionInternal() {
88 // uname is implemented as a simple series of sysctl system calls to
89 // obtain the relevant data from the kernel. The data is
90 // compiled right into the kernel, so no threads or blocking or other
91 // funny business is necessary.
92
93 struct utsname uname_info;
94 if (uname(&uname_info) != 0) {
95 FATAL("Fatal error in DarwinMajorVersionInternal : invalid return uname");
96 return 0;
97 }
98
99 if (strcmp(uname_info.sysname, "Darwin") != 0) {
100 FATAL(
101 "Fatal error in DarwinMajorVersionInternal : unexpected uname"
102 " sysname '%s'",
103 uname_info.sysname);
104 return 0;
105 }
106
107 int32_t darwin_major_version = 0;
108 char* dot = strchr(uname_info.release, '.');
109 if (dot) {
110 errno = 0;
111 char* end_ptr = nullptr;
112 darwin_major_version = strtol(uname_info.release, &end_ptr, 10);
113 if (errno != 0 || (end_ptr == uname_info.release)) {
114 dot = nullptr;
115 }
116 }
117
118 if (!dot) {
119 FATAL(
120 "Fatal error in DarwinMajorVersionInternal :"
121 " could not parse uname release '%s'",
122 uname_info.release);
123 return 0;
124 }
125
126 return darwin_major_version;
127}
128
129// Returns the running system's Mac OS X version which matches the encoding
130// of MAC_OS_X_VERSION_* defines in AvailabilityMacros.h
131int32_t MacOSXVersionInternal() {
132 const int32_t darwin_major_version = DarwinMajorVersionInternal();
133
134 int32_t major_version;
135 int32_t minor_version;
136
137 if (darwin_major_version < 20) {
138 // For Mac OS X 10.* minor version is off by 4 from Darwin's major
139 // version, e.g. 5.* is v10.1.*, 6.* is v10.2.* and so on.
140 // Pretend that anything below Darwin v5 is just Mac OS X Cheetah (v10.0).
141 major_version = 10;
142 minor_version = Utils::Maximum(0, darwin_major_version - 4);
143 } else {
144 // Starting from Darwin v20 major version increment in lock-step:
145 // Darwin v20 - Mac OS X v11, Darwin v21 - Mac OS X v12, etc
146 major_version = (darwin_major_version - 9);
147 minor_version = 0;
148 }
149
150 // Caveat: MAC_OS_X_VERSION_* is encoded using decimal encoding.
151 // Starting at MAC_OS_X_VERSION_10_10 versions use 2 decimal digits for
152 // minor version and patch number.
153 const int32_t field_multiplier = (darwin_major_version < 14) ? 10 : 100;
154 const int32_t major_multiplier = field_multiplier * field_multiplier;
155 const int32_t minor_multiplier = field_multiplier;
156
157 return major_version * major_multiplier + minor_version * minor_multiplier;
158}
159
160int32_t MacOSXVersion() {
161 static int mac_os_x_version = MacOSXVersionInternal();
162 return mac_os_x_version;
163}
164
165} // namespace internal
166
167namespace {
168int32_t MacOSMinorVersion(int32_t version) {
169 // Caveat: MAC_OS_X_VERSION_* is encoded using decimal encoding.
170 // Starting at MAC_OS_X_VERSION_10_10 versions use 2 decimal digits for
171 // minor version and patch number.
172 const int32_t field_multiplier =
173 (version < MAC_OS_X_VERSION_10_10) ? 10 : 100;
174 return (version / field_multiplier) % field_multiplier;
175}
176
177int32_t MacOSMajorVersion(int32_t version) {
178 // Caveat: MAC_OS_X_VERSION_* is encoded using decimal encoding.
179 // Starting at MAC_OS_X_VERSION_10_10 versions use 2 decimal digits for
180 // minor version and patch number.
181 const int32_t field_multiplier =
182 (version < MAC_OS_X_VERSION_10_10) ? 10 : 100;
183 return version / (field_multiplier * field_multiplier);
184}
185} // namespace
186
188 const int32_t current_version = internal::MacOSXVersion();
189
190 if (current_version >= MAC_OS_X_VERSION_MIN_REQUIRED) {
191 return nullptr;
192 }
193
194 return Utils::SCreate(
195 "Current Mac OS X version %d.%d is lower than minimum supported version "
196 "%d.%d",
197 MacOSMajorVersion(current_version), MacOSMinorVersion(current_version),
198 MacOSMajorVersion(MAC_OS_X_VERSION_MIN_REQUIRED),
199 MacOSMinorVersion(MAC_OS_X_VERSION_MIN_REQUIRED));
200}
201
202} // namespace dart
203
204#endif // defined(DART_HOST_OS_MACOS)
static bool read(SkStream *stream, void *buffer, size_t amount)
static constexpr T Maximum(T x, T y)
Definition utils.h:26
static int SNPrint(char *str, size_t size, const char *format,...) PRINTF_ATTRIBUTE(3
static char * StrDup(const char *s)
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
static size_t Read(int filedes, void *buf, size_t nbyte)
static intptr_t StrNLen(const char *s, intptr_t n)
static int Close(int fildes)
static int Unlink(const char *path)
static char * SCreate(const char *format,...) PRINTF_ATTRIBUTE(1
Definition utils.cc:231
static char * StrNDup(const char *s, intptr_t n)
struct MyStruct s
#define FATAL(error)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
uint32_t uint32_t * format
va_start(args, format)
va_end(args)
int32_t MacOSXVersion()
void * malloc(size_t size)
Definition allocation.cc:19
char * CheckIsAtLeastMinRequiredMacOSVersion()
char * strdup(const char *str1)
SINT T dot(const Vec< N, T > &a, const Vec< N, T > &b)
Definition SkVx.h:964