Skip to content

Commit 9cdfb0e

Browse files
authored
Merge pull request #30560 from 3405691582/OpenBSD_StringStorage_AvoidMallocSize
[stdlib] Avoid malloc_size on OpenBSD.
2 parents 23de169 + 7830028 commit 9cdfb0e

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_STDLIB_SHIMS_LIBCSHIMS_H
2020
#define SWIFT_STDLIB_SHIMS_LIBCSHIMS_H
2121

22+
#include "SwiftStdbool.h"
2223
#include "SwiftStdint.h"
2324
#include "SwiftStddef.h"
2425
#include "Visibility.h"
@@ -104,12 +105,14 @@ static inline int _swift_stdlib_memcmp(const void *s1, const void *s2,
104105

105106
// Non-standard extensions
106107
#if defined(__APPLE__)
108+
#define HAS_MALLOC_SIZE 1
107109
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
108110
extern __swift_size_t malloc_size(const void *);
109111
return malloc_size(ptr);
110112
}
111113
#elif defined(__linux__) || defined(__CYGWIN__) || defined(__ANDROID__) \
112114
|| defined(__HAIKU__) || defined(__FreeBSD__) || defined(__wasi__)
115+
#define HAS_MALLOC_SIZE 1
113116
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
114117
#if defined(__ANDROID__)
115118
#if !defined(__ANDROID_API__) || __ANDROID_API__ >= 17
@@ -121,14 +124,23 @@ static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
121124
return malloc_usable_size(CONST_CAST(void *, ptr));
122125
}
123126
#elif defined(_WIN32)
127+
#define HAS_MALLOC_SIZE 1
124128
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
125129
extern __swift_size_t _msize(void *ptr);
126130
return _msize(CONST_CAST(void *, ptr));
127131
}
128132
#else
129-
#error No malloc_size analog known for this platform/libc.
133+
#define HAS_MALLOC_SIZE 0
134+
135+
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
136+
return 0;
137+
}
130138
#endif
131139

140+
static inline __swift_bool _swift_stdlib_has_malloc_size() {
141+
return HAS_MALLOC_SIZE != 0;
142+
}
143+
132144
// Math library functions
133145
static inline SWIFT_ALWAYS_INLINE
134146
float _stdlib_remainderf(float _self, float _other) {

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -268,11 +268,15 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
268268
realMinimumCapacity._builtinWordValue, Element.self)
269269

270270
let storageAddr = UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_storage))
271-
let endAddr = storageAddr + _swift_stdlib_malloc_size(storageAddr)
272-
let realCapacity = endAddr.assumingMemoryBound(to: Element.self) - firstElementAddress
273-
274-
_initStorageHeader(
275-
count: uninitializedCount, capacity: realCapacity)
271+
if let allocSize = _mallocSize(ofAllocation: storageAddr) {
272+
let endAddr = storageAddr + allocSize
273+
let realCapacity = endAddr.assumingMemoryBound(to: Element.self) - firstElementAddress
274+
_initStorageHeader(
275+
count: uninitializedCount, capacity: realCapacity)
276+
} else {
277+
_initStorageHeader(
278+
count: uninitializedCount, capacity: realMinimumCapacity)
279+
}
276280
}
277281
}
278282

stdlib/public/core/Shims.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -14,9 +14,9 @@
1414
///
1515
//===----------------------------------------------------------------------===//
1616

17-
#if _runtime(_ObjC)
1817
import SwiftShims
1918

19+
#if _runtime(_ObjC)
2020
@inlinable
2121
internal func _makeSwiftNSFastEnumerationState()
2222
-> _SwiftNSFastEnumerationState {
@@ -36,3 +36,8 @@ internal var _fastEnumerationStorageMutationsTarget: CUnsignedLong = 0
3636
internal let _fastEnumerationStorageMutationsPtr =
3737
UnsafeMutablePointer<CUnsignedLong>(Builtin.addressof(&_fastEnumerationStorageMutationsTarget))
3838
#endif
39+
40+
@usableFromInline @_alwaysEmitIntoClient
41+
internal func _mallocSize(ofAllocation ptr: UnsafeRawPointer) -> Int? {
42+
return _swift_stdlib_has_malloc_size() ? _swift_stdlib_malloc_size(ptr) : nil
43+
}

stdlib/public/core/StringStorage.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -206,13 +206,17 @@ fileprivate func _allocate<T: AnyObject>(
206206
let totalTailBytes = total - numHeaderBytes
207207

208208
let object = tailAllocator(totalTailBytes)
209-
let mallocSize = _swift_stdlib_malloc_size(
210-
UnsafeRawPointer(Builtin.bridgeToRawPointer(object)))
211-
_internalInvariant(mallocSize % MemoryLayout<Int>.stride == 0)
209+
if let allocSize = _mallocSize(ofAllocation:
210+
UnsafeRawPointer(Builtin.bridgeToRawPointer(object))) {
211+
_internalInvariant(allocSize % MemoryLayout<Int>.stride == 0)
212212

213-
let realNumTailBytes = mallocSize - numHeaderBytes
214-
_internalInvariant(realNumTailBytes >= numTailBytes)
215-
return (object, realNumTailBytes)
213+
let realNumTailBytes = allocSize - numHeaderBytes
214+
_internalInvariant(realNumTailBytes >= numTailBytes)
215+
216+
return (object, realNumTailBytes)
217+
} else {
218+
return (object, totalTailBytes)
219+
}
216220
}
217221

218222
fileprivate func _allocateStringStorage(

0 commit comments

Comments
 (0)