Skip to content

Commit e6ecc9a

Browse files
committed
Modified for some reviews
1 parent 198441b commit e6ecc9a

File tree

10 files changed

+78
-47
lines changed

10 files changed

+78
-47
lines changed

include/swift/Runtime/MutexWin32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ struct ReadWriteLockPlatformHelper {
9292
};
9393
}
9494

95-
#endif
95+
#endif

stdlib/public/runtime/Errors.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(_MSC_VER)
18+
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
19+
#else
20+
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
21+
#endif
22+
1723
#include <stdio.h>
1824
#include <stdlib.h>
1925
#include <string.h>
@@ -23,9 +29,6 @@
2329
#else
2430
#include <unistd.h>
2531
#endif
26-
#if !defined(_MSC_VER)
27-
#include <pthread.h>
28-
#endif
2932
#include <stdarg.h>
3033
#include "swift/Runtime/Debug.h"
3134
#include "swift/Runtime/Mutex.h"
@@ -35,7 +38,7 @@
3538
#if !defined(_MSC_VER)
3639
#include <cxxabi.h>
3740
#endif
38-
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
41+
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
3942

4043
// execinfo.h is not available on Android. Checks in this file ensure that
4144
// fatalError behaves as expected, but without stack traces.
@@ -57,7 +60,7 @@ enum: uint32_t {
5760

5861
using namespace swift;
5962

60-
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
63+
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
6164

6265
static bool getSymbolNameAddr(llvm::StringRef libraryName, Dl_info dlinfo,
6366
std::string &symbolName, uintptr_t &addrOut) {
@@ -203,7 +206,7 @@ reportNow(uint32_t flags, const char *message)
203206
#ifdef __APPLE__
204207
asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s", message);
205208
#endif
206-
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
209+
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
207210
if (flags & FatalErrorFlags::ReportBacktrace) {
208211
fputs("Current stack trace:\n", stderr);
209212
constexpr unsigned maxSupportedStackDepth = 128;
@@ -225,6 +228,26 @@ void swift::swift_reportError(uint32_t flags,
225228
reportOnCrash(flags, message);
226229
}
227230

231+
static int swift_vasprintf(char **strp, const char *fmt, va_list ap) {
232+
#if defined(_MSC_VER)
233+
int len = _vscprintf(fmt, ap);
234+
if (len < 0)
235+
return -1;
236+
char *buffer = reinterpret_cast<char *>(malloc(len + 1));
237+
if (!buffer)
238+
return -1;
239+
int result = vsprintf(*strp, fmt, ap);
240+
if (result < 0) {
241+
free(buffer);
242+
return -1;
243+
}
244+
*strp = buffer;
245+
return result;
246+
#else
247+
return vasprintf(strp, fmt, ap);
248+
#endif
249+
}
250+
228251
// Report a fatal error to system console, stderr, and crash logs, then abort.
229252
LLVM_ATTRIBUTE_NORETURN
230253
void
@@ -234,13 +257,7 @@ swift::fatalError(uint32_t flags, const char *format, ...)
234257
va_start(args, format);
235258

236259
char *log;
237-
#if defined(_MSC_VER)
238-
int len = _vscprintf(format, args) + 1;
239-
log = reinterpret_cast<char *>(malloc(len));
240-
vsprintf(log, format, args);
241-
#else
242-
vasprintf(&log, format, args);
243-
#endif
260+
swift_vasprintf(&log, format, args);
244261

245262
swift_reportError(flags, log);
246263
abort();

stdlib/public/runtime/HeapObject.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
#include <cstring>
2929
#include <cstdio>
3030
#include <cstdlib>
31-
#if !defined(_MSC_VER)
32-
#include <unistd.h>
33-
#endif
3431
#include "../SwiftShims/RuntimeShims.h"
3532
#if SWIFT_OBJC_INTEROP
3633
# include <objc/NSObject.h>

stdlib/public/runtime/Metadata.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,22 @@
6262
using namespace swift;
6363
using namespace metadataimpl;
6464

65+
static uintptr_t swift_pageSize() {
66+
#if defined(__APPLE__)
67+
return vm_page_size;
68+
#elif defined(_MSC_VER)
69+
SYSTEM_INFO SystemInfo;
70+
GetSystemInfo(&SystemInfo);
71+
return SystemInfo.dwPageSize;
72+
#else
73+
return sysconf(_SC_PAGESIZE);
74+
#endif
75+
}
76+
6577
// allocate memory up to a nearby page boundary
66-
static void *swift_allocPage(size_t size) {
78+
static void *swift_allocateMetadataRoundingToPage(size_t size) {
79+
const uintptr_t PageSizeMask = SWIFT_LAZY_CONSTANT(swift_pageSize()) - 1;
80+
size = (size + PageSizeMask) & ~PageSizeMask;
6781
#if defined(_MSC_VER)
6882
auto mem = VirtualAlloc(
6983
nullptr, size, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
@@ -76,30 +90,27 @@ static void *swift_allocPage(size_t size) {
7690
return mem;
7791
}
7892

79-
// free memory allocated by swift_allocPage()
80-
// On success, swift_freePage() returns 0, on failure -1
81-
static int swift_freePage(void *addr, size_t size) {
93+
// free memory allocated by swift_allocateMetadataRoundingToPage()
94+
static void swift_freeMetadata(void *addr, size_t size) {
8295
#if defined(_MSC_VER)
83-
return VirtualFree(addr, 0, MEM_RELEASE) == 0 ? -1 : 0;
96+
// On success, VirtualFree() returns nonzero, on failure 0
97+
int result = VirtualFree(addr, 0, MEM_RELEASE);
98+
if (result == 0)
99+
fatalError(/* flags = */ 0, "swift_freePage: VirtualFree() failed");
84100
#else
85-
return munmap(addr, size);
101+
// On success, munmap() returns 0, on failure -1
102+
int result = munmap(addr, size);
103+
if (result != 0)
104+
fatalError(/* flags = */ 0, "swift_freePage: munmap() failed");
86105
#endif
87106
}
88107

89108
void *MetadataAllocator::alloc(size_t size) {
90-
#if defined(__APPLE__)
91-
const uintptr_t PageSizeMask = vm_page_mask;
92-
#elif defined(_MSC_VER)
93-
SYSTEM_INFO SystemInfo;
94-
GetSystemInfo(&SystemInfo);
95-
const uintptr_t PageSizeMask = SystemInfo.dwPageSize - 1;
96-
#else
97-
static const uintptr_t PageSizeMask = sysconf(_SC_PAGESIZE) - 1;
98-
#endif
109+
const uintptr_t PageSize = SWIFT_LAZY_CONSTANT(swift_pageSize());
99110
// If the requested size is a page or larger, map page(s) for it
100111
// specifically.
101-
if (LLVM_UNLIKELY(size > PageSizeMask)) {
102-
void *mem = swift_allocPage((size + PageSizeMask) & ~PageSizeMask);
112+
if (LLVM_UNLIKELY(size >= PageSize)) {
113+
void *mem = swift_allocateMetadataRoundingToPage(size);
103114
if (!mem)
104115
crash("unable to allocate memory for metadata cache");
105116
return mem;
@@ -112,10 +123,11 @@ void *MetadataAllocator::alloc(size_t size) {
112123

113124
// If we wrap over the end of the page, allocate a new page.
114125
void *allocation = nullptr;
126+
const uintptr_t PageSizeMask = PageSize - 1;
115127
if (LLVM_UNLIKELY(((uintptr_t)next & ~PageSizeMask)
116128
!= (((uintptr_t)end & ~PageSizeMask)))) {
117129
// Allocate a new page if we haven't already.
118-
allocation = swift_allocPage(PageSizeMask + 1);
130+
allocation = swift_allocateMetadataRoundingToPage(PageSize);
119131

120132
if (!allocation)
121133
crash("unable to allocate memory for metadata cache");
@@ -135,7 +147,7 @@ void *MetadataAllocator::alloc(size_t size) {
135147
// This potentially causes us to perform multiple mmaps under contention,
136148
// but it keeps the fast path pristine.
137149
if (allocation) {
138-
swift_freePage(allocation, PageSizeMask + 1);
150+
swift_freeMetadata(allocation, PageSize);
139151
}
140152
}
141153
}

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@
3434
#include <link.h>
3535
#endif
3636

37-
#if defined(_MSC_VER)
38-
#include <windows.h>
39-
#else
40-
#include <dlfcn.h>
41-
#endif
42-
4337
using namespace swift;
4438
using namespace Demangle;
4539

stdlib/public/runtime/Once.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void swift::swift_once(swift_once_t *predicate, void (*fn)(void *)) {
4949
// process (the token is a word that is atomically incremented from 0 to
5050
// 1 to 2 during initialization) to work. We should implement our own version
5151
// that we can rely on to continue to work that way.
52+
// The MSVC port also relies on this, because the std::call_once on MSVC
53+
// follows the compatible init process.
5254
// For more information, see rdar://problem/18499385
5355
std::call_once(*predicate, [fn]() { fn(nullptr); });
5456
#endif

stdlib/public/runtime/Reflection.mm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <cstring>
2525
#include <new>
2626
#include <string>
27-
#if !defined(_MSC_VER)
28-
#include <dlfcn.h>
29-
#endif
3027

3128
#if SWIFT_OBJC_INTEROP
3229
#include "swift/Runtime/ObjCBridge.h"

stdlib/public/runtime/SwiftObject.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "../SwiftShims/RuntimeShims.h"
3535
#include "Private.h"
3636
#include "swift/Runtime/Debug.h"
37-
#if !defined(_MSC_VER)
37+
#if SWIFT_OBJC_INTEROP
3838
#include <dlfcn.h>
3939
#endif
4040
#include <stdio.h>

stdlib/public/stubs/LibcShims.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,28 @@ int swift::_swift_stdlib_memcmp(const void *s1, const void *s2,
5757

5858
__swift_ssize_t
5959
swift::_swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
60+
#if defined(_MSC_VER)
61+
return _read(fd, buf, nbyte);
62+
#else
6063
return read(fd, buf, nbyte);
64+
#endif
6165
}
6266

6367
__swift_ssize_t
6468
swift::_swift_stdlib_write(int fd, const void *buf, __swift_size_t nbyte) {
69+
#if defined(_MSC_VER)
70+
return _write(fd, buf, nbyte);
71+
#else
6572
return write(fd, buf, nbyte);
73+
#endif
6674
}
6775

6876
int swift::_swift_stdlib_close(int fd) {
77+
#if defined(_MSC_VER)
78+
return _close(fd);
79+
#else
6980
return close(fd);
81+
#endif
7082
}
7183

7284
#if defined(__APPLE__)

stdlib/public/stubs/Stubs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ ssize_t swift::swift_stdlib_readLine_stdin(char **LinePtr) {
265265
if (LinePtr == nullptr)
266266
return -1;
267267

268-
int Capacity = 0;
268+
ssize_t Capacity = 0;
269269
ssize_t Pos = 0;
270270
char *ReadBuf = nullptr;
271271

0 commit comments

Comments
 (0)