[interop][SwiftToCxx] ensure that buffers for heap allocated Swift values are released after parameter copy is consumed

This commit is contained in:
Alex Lorenz
2023-10-28 14:11:19 -07:00
parent 4e519a8d8b
commit ab948f4897
3 changed files with 47 additions and 0 deletions

View File

@@ -110,6 +110,12 @@ extern "C" void _fatalError_Cxx_move_of_Swift_value_type_not_supported_yet();
SWIFT_INLINE_THUNK void *_Nonnull opaqueAlloc(size_t size,
size_t align) noexcept {
#if defined(SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_alloc) && \
defined(SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_free)
// Allow the user to provide custom allocator for heap-allocated Swift
// value types.
return SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_alloc(size, align);
#else
#if defined(_WIN32)
void *r = _aligned_malloc(size, align);
#else
@@ -120,14 +126,22 @@ SWIFT_INLINE_THUNK void *_Nonnull opaqueAlloc(size_t size,
(void)res;
#endif
return r;
#endif
}
SWIFT_INLINE_THUNK void opaqueFree(void *_Nonnull p) noexcept {
#if defined(SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_alloc) && \
defined(SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_free)
// Allow the user to provide custom allocator for heap-allocated Swift
// value types.
SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_free(p);
#else
#if defined(_WIN32)
_aligned_free(p);
#else
free(p);
#endif
#endif
}
/// Base class for a container for an opaque Swift value, like resilient struct.

View File

@@ -11,6 +11,25 @@
// REQUIRES: executable_test
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
size_t allocCount = 0;
size_t totalAllocs = 0;
void * _Nonnull trackedAlloc(size_t size, size_t align) {
++allocCount;
++totalAllocs;
return malloc(size);
}
void trackedFree(void *_Nonnull p) {
--allocCount;
free(p);
}
#define SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_alloc trackedAlloc
#define SWIFT_CXX_INTEROPERABILITY_OVERRIDE_OPAQUE_STORAGE_free trackedFree
#include "consuming.h"
extern "C" size_t swift_retainCount(void * _Nonnull obj);
@@ -45,5 +64,8 @@ int main() {
assert(getRetainCount(k) == 3);
}
// CHECK-NEXT: destroy AKlass
// verify that all of the opaque buffers are freed.
assert(allocCount == 0);
assert(totalAllocs != 0);
return 0;
}

View File

@@ -0,0 +1,11 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %S/consuming-parameter-in-cxx.swift -typecheck -module-name Init -clang-header-expose-decls=all-public -emit-clang-header-path %t/consuming.h -enable-library-evolution
// RUN: %target-interop-build-clangxx -c %S/consuming-parameter-in-cxx-execution.cpp -I %t -o %t/swift-consume-execution.o
// RUN: %target-interop-build-swift %S/consuming-parameter-in-cxx.swift -o %t/swift-consume-execution-evo -Xlinker %t/swift-consume-execution.o -module-name Init -Xfrontend -entry-point-function-name -Xfrontend swiftMain -enable-library-evolution
// RUN: %target-codesign %t/swift-consume-execution-evo
// RUN: %target-run %t/swift-consume-execution-evo | %FileCheck %S/consuming-parameter-in-cxx-execution.cpp
// REQUIRES: executable_test