Skip to content

Commit 1e9741a

Browse files
committed
[Stdlib] Fix Linux detection for malloc_size. Silence const cast warnings. Fix Linux implementation in Random.cpp to actually compile.
1 parent 4113697 commit 1e9741a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,31 @@ static inline int _swift_stdlib_memcmp(const void *s1, const void *s2,
8181
return memcmp(s1, s2, n);
8282
}
8383

84+
// Casting helper. This code needs to work when included from C or C++.
85+
// Casting away const with a C-style cast warns in C++. Use a const_cast
86+
// there.
87+
#ifdef __cplusplus
88+
#define CONST_CAST(type, value) const_cast<type>(value)
89+
#else
90+
#define CONST_CAST(type, value) (type)value
91+
#endif
92+
8493
// Non-standard extensions
8594
#if defined(__APPLE__)
8695
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
8796
extern __swift_size_t malloc_size(const void *);
8897
return malloc_size(ptr);
8998
}
90-
#elif defined(__GNU_LIBRARY__) || defined(__CYGWIN__) || defined(__ANDROID__) \
99+
#elif defined(__linux__) || defined(__CYGWIN__) || defined(__ANDROID__) \
91100
|| defined(__HAIKU__) || defined(__FreeBSD__)
92101
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
93102
extern __swift_size_t malloc_usable_size(void *ptr);
94-
return malloc_usable_size((void *)ptr);
103+
return malloc_usable_size(CONST_CAST(void *, ptr));
95104
}
96105
#elif defined(_WIN32)
97106
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
98107
extern __swift_size_t _msize(void *ptr);
99-
return _msize(const_cast<void *>(ptr));
108+
return _msize(CONST_CAST(void *, ptr));
100109
}
101110
#else
102111
#error No malloc_size analog known for this platform/libc.

stdlib/public/stubs/Random.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#pragma comment(lib, "Bcrypt.lib")
2424
#else
2525
#include <errno.h>
26+
#include <fcntl.h>
2627
#include <unistd.h>
2728
#endif
2829

@@ -36,6 +37,7 @@
3637

3738
#include <stdlib.h>
3839

40+
#include "swift/Runtime/Debug.h"
3941
#include "swift/Runtime/Mutex.h"
4042
#include "../SwiftShims/Random.h"
4143

@@ -91,12 +93,12 @@ void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
9193

9294
if (actual_nbytes == -1) {
9395
static const int fd =
94-
WHILE_EINTR(_swift_stdlib_open("/dev/urandom", O_RDONLY | O_CLOEXEC, 0));
96+
WHILE_EINTR(open("/dev/urandom", O_RDONLY | O_CLOEXEC, 0));
9597

9698
if (fd != -1) {
9799
static StaticMutex mutex;
98100
mutex.withLock([&] {
99-
actual_nbytes = WHILE_EINTR(_swift_stdlib_read(fd, buf, nbytes));
101+
actual_nbytes = WHILE_EINTR(read(fd, buf, nbytes));
100102
});
101103
}
102104
}

0 commit comments

Comments
 (0)