File tree Expand file tree Collapse file tree 5 files changed +55
-0
lines changed Expand file tree Collapse file tree 5 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -231,6 +231,8 @@ SWIFT_RUNTIME_EXPORT
231
231
size_t swift_retainCount (HeapObject *object);
232
232
SWIFT_RUNTIME_EXPORT
233
233
size_t swift_unownedRetainCount (HeapObject *object);
234
+ SWIFT_RUNTIME_EXPORT
235
+ size_t swift_weakRetainCount (HeapObject *object);
234
236
235
237
// / Is this pointer a non-null unique reference to an object
236
238
// / that uses Swift reference counting?
Original file line number Diff line number Diff line change 13
13
#define SWIFT_STDLIB_SHIMS_HEAPOBJECT_H
14
14
15
15
#include " RefCount.h"
16
+ #include " SwiftStddef.h"
16
17
#include " System.h"
17
18
18
19
#define SWIFT_ABI_HEAP_OBJECT_HEADER_SIZE_64 16
@@ -30,6 +31,7 @@ template <typename Target> struct TargetHeapMetadata;
30
31
using HeapMetadata = TargetHeapMetadata<InProcess>;
31
32
#else
32
33
typedef struct HeapMetadata HeapMetadata;
34
+ typedef struct HeapObject HeapObject;
33
35
#endif
34
36
35
37
// The members of the HeapObject header that are not shared by a
@@ -69,6 +71,15 @@ SWIFT_RUNTIME_STDLIB_INTERFACE
69
71
void _swift_instantiateInertHeapObject (void *address,
70
72
const HeapMetadata *metadata);
71
73
74
+ SWIFT_RUNTIME_STDLIB_INTERFACE
75
+ __swift_size_t swift_retainCount (HeapObject *obj);
76
+
77
+ SWIFT_RUNTIME_STDLIB_INTERFACE
78
+ __swift_size_t swift_unownedRetainCount (HeapObject *obj);
79
+
80
+ SWIFT_RUNTIME_STDLIB_INTERFACE
81
+ __swift_size_t swift_weakRetainCount (HeapObject *obj);
82
+
72
83
#ifdef __cplusplus
73
84
} // extern "C"
74
85
#endif
Original file line number Diff line number Diff line change 10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import SwiftShims
14
+
13
15
@_frozen // FIXME(sil-serialize-all)
14
16
public enum _DebuggerSupport {
15
17
@_frozen // FIXME(sil-serialize-all)
@@ -335,3 +337,11 @@ func _stringForPrintObject(_ value: Any) -> String {
335
337
public
336
338
func _debuggerTestingCheckExpect( _ checked_value: String ,
337
339
_ expected_value: String ) { }
340
+
341
+ // Utilities to get refcount(s) of class objects.
342
+ @_silgen_name ( " swift_retainCount " )
343
+ public func _getRetainCount( _ Value: AnyObject ) -> UInt
344
+ @_silgen_name ( " swift_unownedRetainCount " )
345
+ public func _getUnownedRetainCount( _ Value : AnyObject ) -> UInt
346
+ @_silgen_name ( " swift_weakRetainCount " )
347
+ public func _getWeakRetainCount( _ Value : AnyObject ) -> UInt
Original file line number Diff line number Diff line change @@ -372,6 +372,10 @@ size_t swift::swift_unownedRetainCount(HeapObject *object) {
372
372
return object->refCounts .getUnownedCount ();
373
373
}
374
374
375
+ size_t swift::swift_weakRetainCount (HeapObject *object) {
376
+ return object->refCounts .getWeakCount ();
377
+ }
378
+
375
379
HeapObject *swift::swift_unownedRetain (HeapObject *object) {
376
380
SWIFT_RT_TRACK_INVOCATION (object, swift_unownedRetain);
377
381
if (!isValidPointerForNativeRetain (object))
Original file line number Diff line number Diff line change 1
1
// RUN: %target-run-simple-swift
2
2
// REQUIRES: executable_test
3
+ // REQUIRES: optimized_stdlib
3
4
4
5
import StdlibUnittest
5
6
@@ -99,4 +100,31 @@ StringForPrintObjectTests.test("DontBridgeThisStruct") {
99
100
}
100
101
#endif
101
102
103
+ class RefCountedObj {
104
+ var patatino : Int
105
+ init ( _ p : Int ) {
106
+ self . patatino = p
107
+ }
108
+ public func f( ) -> Int {
109
+ return self . patatino
110
+ }
111
+ }
112
+
113
+ let RefcountTests = TestSuite ( " RefCount " )
114
+ RefcountTests . test ( " Basic " ) {
115
+ var Obj = RefCountedObj ( 47 ) ;
116
+
117
+ // Counters for live objects should be always positive.
118
+ // We try to be a little bit more lax here because optimizations
119
+ // or different stdlib versions might impact the exact value of
120
+ // refcounting, and we're just interested in testing whether the
121
+ // stub works properly.
122
+ expectGT ( _getRetainCount ( Obj) , 0 ) ;
123
+ expectGT ( _getWeakRetainCount ( Obj) , 0 ) ;
124
+ expectGT ( _getUnownedRetainCount ( Obj) , 0 ) ;
125
+
126
+ // Try to keep the object live here.
127
+ let _ = Obj . f ( )
128
+ }
129
+
102
130
runAllTests ( )
You can’t perform that action at this time.
0 commit comments