Skip to content

Commit

Permalink
Reimplement _withStackOrHeapBuffer w/o blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov committed Apr 16, 2020
1 parent a865496 commit 5b440b2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CoreFoundation/Base.subproj/CFRuntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static CFRuntimeClass const * __CFRuntimeClassTable[__CFRuntimeClassTableSize] _
[_kCFRuntimeIDCFMessagePort] = &__CFMessagePortClass,
#endif


#if !TARGET_OS_WASI
[_kCFRuntimeIDCFRunLoopMode] = &__CFRunLoopModeClass,
[_kCFRuntimeIDCFRunLoop] = &__CFRunLoopClass,
[_kCFRuntimeIDCFRunLoopSource] = &__CFRunLoopSourceClass,
Expand All @@ -243,6 +243,8 @@ static CFRuntimeClass const * __CFRuntimeClassTable[__CFRuntimeClassTableSize] _
[_kCFRuntimeIDCFSocket] = &__CFSocketClass,
[_kCFRuntimeIDCFReadStream] = &__CFReadStreamClass,
[_kCFRuntimeIDCFWriteStream] = &__CFWriteStreamClass,
#endif

[_kCFRuntimeIDCFAttributedString] = &__CFAttributedStringClass,
[_kCFRuntimeIDCFRunArray] = &__CFRunArrayClass,
[_kCFRuntimeIDCFCharacterSet] = &__CFCharacterSetClass,
Expand Down
4 changes: 2 additions & 2 deletions CoreFoundation/Base.subproj/CoreFoundation_Prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ CF_INLINE uint64_t mach_absolute_time() {
#define malloc_default_zone() (void *)0
#endif // TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WIN32

#if TARGET_OS_LINUX || TARGET_OS_WIN32 || defined(__OpenBSD__)
#if TARGET_OS_LINUX || TARGET_OS_WIN32 || defined(__OpenBSD__) || TARGET_OS_WASI
#define strtod_l(a,b,locale) strtod(a,b)
#define strtoul_l(a,b,c,locale) strtoul(a,b,c)
#define strtol_l(a,b,c,locale) strtol(a,b,c)
Expand All @@ -242,7 +242,7 @@ CF_INLINE int flsl( long mask ) {
}
return idx;
}
#endif // TARGET_OS_LINUX || TARGET_OS_WIN32 || defined(__OpenBSD__)
#endif // TARGET_OS_LINUX || TARGET_OS_WIN32 || defined(__OpenBSD__) || TARGET_OS_WASI

#if TARGET_OS_LINUX

Expand Down
2 changes: 2 additions & 0 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ static inline _Bool _resizeConditionalAllocationBuffer(_ConditionalAllocationBuf
return true;
}

#if !TARGET_OS_WASI
static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
_ConditionalAllocationBuffer buffer;
#if TARGET_OS_MAC
Expand All @@ -485,6 +486,7 @@ static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((n
}
return true;
}
#endif

#pragma mark - Character Set

Expand Down
13 changes: 8 additions & 5 deletions CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ else()
set(FRAMEWORK_LIBRARY_TYPE STATIC)
endif()

set(CF_WASI_UNAVAILABLE_SOURCES )
set(CF_WASI_UNAVAILABLE_PRIVATE_HEADERS )
set(CF_WASI_UNAVAILABLE_PUBLIC_HEADERS )
set(CF_WASI_UNAVAILABLE_SOURCES)
set(CF_WASI_UNAVAILABLE_PRIVATE_HEADERS)
set(CF_WASI_UNAVAILABLE_PUBLIC_HEADERS)

if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL wasm32)
list(APPEND CF_WASI_UNAVAILABLE_PRIVATE_HEADERS
Expand Down Expand Up @@ -190,7 +190,6 @@ add_framework(CoreFoundation
PlugIn.subproj/CFBundle_BinaryTypes.h
PlugIn.subproj/CFBundle_Internal.h
PlugIn.subproj/CFPlugIn_Factory.h
${CF_WASI_UNAVAILABLE_PRIVATE_HEADERS}
# String
String.subproj/CFAttributedStringPriv.h
String.subproj/CFBurstTrie.h
Expand All @@ -214,6 +213,8 @@ add_framework(CoreFoundation
URL.subproj/CFURL.inc.h
URL.subproj/CFURLPriv.h
URL.subproj/CFURLSessionInterface.h

${CF_WASI_UNAVAILABLE_PRIVATE_HEADERS}
PUBLIC_HEADERS
# FIXME: PrivateHeaders referenced by public headers
Base.subproj/CFKnownLocations.h
Expand Down Expand Up @@ -390,7 +391,9 @@ add_framework(CoreFoundation
URL.subproj/CFURLAccess.c
URL.subproj/CFURL.c
URL.subproj/CFURLComponents.c
URL.subproj/CFURLComponents_URIParser.c)
URL.subproj/CFURLComponents_URIParser.c

${CF_WASI_UNAVAILABLE_SOURCES})
target_compile_definitions(CoreFoundation
PRIVATE
$<$<COMPILE_LANGUAGE:ASM>:CF_CHARACTERSET_BITMAP="CharacterSets/CFCharacterSetBitmaps.bitmap">
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/Locale.subproj/CFLocaleKeys.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ CONST_STRING_DECL(kCFCalendarIdentifierEthiopicAmeteMihret, "ethiopic");
CONST_STRING_DECL(kCFCalendarIdentifierEthiopicAmeteAlem, "ethiopic-amete-alem");

// Aliases for other platforms.
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WIN32
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WIN32 || TARGET_OS_WASI

CF_EXPORT CFStringRef const kCFBuddhistCalendar __attribute__((alias ("kCFCalendarIdentifierBuddhist")));
CF_EXPORT CFStringRef const kCFChineseCalendar __attribute__((alias ("kCFCalendarIdentifierChinese")));
Expand Down
2 changes: 1 addition & 1 deletion Sources/BlocksRuntime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#if TARGET_OS_WIN32
#include <Windows.h>
#include <Psapi.h>
#else
#elif !TARGET_OS_WASI
#include <dlfcn.h>
#endif
#if __has_include(<os/assumes.h>)
Expand Down
11 changes: 11 additions & 0 deletions Sources/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ internal func __NSDataIsCompact(_ data: NSData) -> Bool {
internal func __withStackOrHeapBuffer(_ size: Int, _ block: (UnsafeMutablePointer<_ConditionalAllocationBuffer>) -> Void) -> Bool {
return _withStackOrHeapBuffer(size, block)
}
#elseif os(WASI)
// `_withStackOrHeapBuffer` from CoreFoundation depends on BlocksRuntime, which
// is unavailable on WASI
@usableFromInline @discardableResult
internal func __withStackOrHeapBuffer(_ size: Int, _ block: (UnsafeMutablePointer<_ConditionalAllocationBuffer>) -> Void) -> Bool {
let memory = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: 1)
var buffer = _ConditionalAllocationBuffer(memory: memory, capacity: size, onStack: false)
block(&buffer)
memory.deallocate()
return true
}
#else
@inlinable @inline(__always) @discardableResult
internal func __withStackOrHeapBuffer(_ size: Int, _ block: (UnsafeMutablePointer<_ConditionalAllocationBuffer>) -> Void) -> Bool {
Expand Down

0 comments on commit 5b440b2

Please sign in to comment.