File tree Expand file tree Collapse file tree 5 files changed +46
-0
lines changed Expand file tree Collapse file tree 5 files changed +46
-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)
@@ -345,3 +347,11 @@ func _stringForPrintObject(_ value: Any) -> String {
345
347
public
346
348
func _debuggerTestingCheckExpect( _ checked_value: String ,
347
349
_ expected_value: String ) { }
350
+
351
+ // Utilities to get refcount(s) of class objects.
352
+ @_silgen_name ( " swift_retainCount " )
353
+ public func _getRetainCount( _ Value: AnyObject ) -> UInt
354
+ @_silgen_name ( " swift_unownedRetainCount " )
355
+ public func _getUnownedRetainCount( _ Value : AnyObject ) -> UInt
356
+ @_silgen_name ( " swift_weakRetainCount " )
357
+ 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 @@ -99,4 +99,23 @@ StringForPrintObjectTests.test("DontBridgeThisStruct") {
99
99
}
100
100
#endif
101
101
102
+ class RefCountedObj {
103
+ var patatino : Int
104
+ init ( _ p : Int ) {
105
+ self . patatino = p
106
+ }
107
+ public func f( ) -> Int {
108
+ return self . patatino
109
+ }
110
+ }
111
+
112
+ let RefcountTests = TestSuite ( " RefCount " )
113
+ RefcountTests . test ( " Basic " ) {
114
+ var Obj = RefCountedObj ( 47 ) ;
115
+ expectEqual ( _getRetainCount ( Obj) , 2 ) ;
116
+ expectEqual ( _getWeakRetainCount ( Obj) , 1 ) ;
117
+ expectEqual ( _getUnownedRetainCount ( Obj) , 1 ) ;
118
+ let _ = Obj . f ( ) // try to keep the object live here.
119
+ }
120
+
102
121
runAllTests ( )
You can’t perform that action at this time.
0 commit comments