Skip to content

Commit 0b7c267

Browse files
authored
(163677167) Switch swift-founation Data allocation to typed malloc (#1567)
1 parent 3b7d7c8 commit 0b7c267

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Sources/FoundationEssentials/Data/Representations/DataStorage.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,33 @@ internal final class __DataStorage : @unchecked Sendable {
3333
@usableFromInline static let maxSize = Int.max >> 1
3434
@usableFromInline static let vmOpsThreshold = Platform.pageSize * 4
3535

36-
#if !FOUNDATION_FRAMEWORK
3736
static func allocate(_ size: Int, _ clear: Bool) -> UnsafeMutableRawPointer? {
37+
#if canImport(Darwin) && _pointerBitWidth(_64) && !NO_TYPED_MALLOC
38+
var typeDesc = malloc_type_descriptor_v0_t()
39+
typeDesc.summary.layout_semantics.contains_generic_data = true
40+
if clear {
41+
return malloc_type_calloc(1, size, typeDesc.type_id);
42+
} else {
43+
return malloc_type_malloc(size, typeDesc.type_id);
44+
}
45+
#else
3846
if clear {
3947
return calloc(1, size)
4048
} else {
4149
return malloc(size)
4250
}
51+
#endif
4352
}
4453

4554
static func reallocate(_ ptr: UnsafeMutableRawPointer, _ newSize: Int) -> UnsafeMutableRawPointer? {
46-
return realloc(ptr, newSize);
55+
#if canImport(Darwin) && _pointerBitWidth(_64) && !NO_TYPED_MALLOC
56+
var typeDesc = malloc_type_descriptor_v0_t()
57+
typeDesc.summary.layout_semantics.contains_generic_data = true
58+
return malloc_type_realloc(ptr, newSize, typeDesc.type_id);
59+
#else
60+
return realloc(ptr, newSize)
61+
#endif
4762
}
48-
#endif // !FOUNDATION_FRAMEWORK
4963

5064
@usableFromInline // This is not @inlinable as it is a non-trivial, non-generic function.
5165
static func move(_ dest_: UnsafeMutableRawPointer, _ source_: UnsafeRawPointer?, _ num_: Int) {

0 commit comments

Comments
 (0)