Skip to content

Commit 5ff64f8

Browse files
authored
Introduce a CMake option to disable MetadataAllocator and use malloc+free instead, use it for freestandning preset (swiftlang#39678)
1 parent 1147d08 commit 5ff64f8

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ option(SWIFT_STDLIB_OS_VERSIONING
123123
"Build stdlib with availability based on OS versions (Darwin only)."
124124
TRUE)
125125

126+
option(SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
127+
"Build stdlib without a custom implementation of MetadataAllocator, relying on malloc+free instead."
128+
FALSE)
129+
126130
option(SWIFT_BUILD_TEST_SUPPORT_MODULES
127131
"Whether to build StdlibUnittest and other test support modules. Defaults to On when SWIFT_BUILD_SDK_OVERLAY is On, or when SWIFT_INCLUDE_TESTS is On."
128132
"${SWIFT_BUILD_TEST_SUPPORT_MODULES_default}")

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ function(_add_target_variant_c_compile_flags)
355355
list(APPEND result "-DSWIFT_RUNTIME_OS_VERSIONING")
356356
endif()
357357

358+
if(SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR)
359+
list(APPEND result "-DSWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR")
360+
endif()
361+
358362
if(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING)
359363
list(APPEND result "-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING")
360364
endif()

stdlib/public/runtime/Metadata.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5822,6 +5822,8 @@ void swift::checkMetadataDependencyCycle(const Metadata *startMetadata,
58225822
/*** Allocator implementation **********************************************/
58235823
/***************************************************************************/
58245824

5825+
#if !SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
5826+
58255827
namespace {
58265828
struct alignas(sizeof(uintptr_t) * 2) PoolRange {
58275829
static constexpr uintptr_t PageSize = 16 * 1024;
@@ -6059,6 +6061,8 @@ void MetadataAllocator::Deallocate(const void *allocation, size_t size,
60596061
std::memory_order_relaxed);
60606062
}
60616063

6064+
#endif
6065+
60626066
void *swift::allocateMetadata(size_t size, size_t alignment) {
60636067
return MetadataAllocator(MetadataTag).Allocate(size, alignment);
60646068
}

stdlib/public/runtime/MetadataCache.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
namespace swift {
2828

29+
#if !SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
30+
2931
class MetadataAllocator : public llvm::AllocatorBase<MetadataAllocator> {
3032
private:
3133
uint16_t Tag;
@@ -51,6 +53,26 @@ class MetadataAllocator : public llvm::AllocatorBase<MetadataAllocator> {
5153
}
5254
};
5355

56+
#else
57+
58+
class MetadataAllocator {
59+
public:
60+
MetadataAllocator(uint16_t tag) {}
61+
void *Allocate(size_t size, size_t alignment) {
62+
if (alignment < sizeof(void*)) alignment = sizeof(void*);
63+
void *ptr = nullptr;
64+
if (posix_memalign(&ptr, alignment, size) != 0) {
65+
return nullptr;
66+
}
67+
return ptr;
68+
}
69+
void Deallocate(const void *ptr, size_t size = 0, size_t Alignment = 0) {
70+
return free(const_cast<void *>(ptr));
71+
}
72+
};
73+
74+
#endif
75+
5476
template <uint16_t StaticTag>
5577
class TaggedMetadataAllocator : public MetadataAllocator {
5678
public:

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,7 @@ extra-cmake-options=
24782478
-DSWIFT_ENABLE_DISPATCH:BOOL=FALSE
24792479
-DSWIFT_IMPLICIT_CONCURRENCY_IMPORT:BOOL=FALSE
24802480
-DSWIFT_STDLIB_ENABLE_PRESPECIALIZATION:BOOL=FALSE
2481+
-DSWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR:BOOL=TRUE
24812482

24822483
[preset: stdlib_S_standalone_minimal_macho_x86_64,build]
24832484
mixin-preset=

0 commit comments

Comments
 (0)