@@ -65,6 +65,20 @@ open class NativeTypeWrapper: Hashable {
65
65
66
66
}
67
67
68
+ open class NativeTraitWrapper : NativeTypeWrapper {
69
+
70
+ public func activate( ) -> Self {
71
+ Bindings . cacheInstance ( instance: self )
72
+ return self
73
+ }
74
+
75
+ public func activateOnce( ) -> Self {
76
+ Bindings . cacheInstance ( instance: self )
77
+ return self
78
+ }
79
+
80
+ }
81
+
68
82
public class Bindings {
69
83
70
84
internal static var minimumPrintSeverity : PrintSeverity = . WARNING
@@ -6010,7 +6024,7 @@ public class Bindings {
6010
6024
}
6011
6025
6012
6026
return
6013
- Result_PublicKeyErrorZ ( pointer: recover_pk ( msgWrapper. cOpaqueStruct!, Bindings . new_LDKStr ( string: sig) ) )
6027
+ Result_PublicKeyErrorZ ( pointer: recover_pk ( msgWrapper. cOpaqueStruct!, Bindings . new_LDKStr ( string: sig, chars_is_owned : true ) ) )
6014
6028
6015
6029
6016
6030
}
@@ -6023,7 +6037,7 @@ public class Bindings {
6023
6037
}
6024
6038
6025
6039
return
6026
- verify ( msgWrapper. cOpaqueStruct!, Bindings . new_LDKStr ( string: sig) , Bindings . new_LDKPublicKey ( array: pk) )
6040
+ verify ( msgWrapper. cOpaqueStruct!, Bindings . new_LDKStr ( string: sig, chars_is_owned : true ) , Bindings . new_LDKPublicKey ( array: pk) )
6027
6041
6028
6042
6029
6043
}
@@ -6053,7 +6067,7 @@ public class Bindings {
6053
6067
serWrapper. noOpRetain ( )
6054
6068
}
6055
6069
6056
- return withUnsafePointer ( to: arg. cOpaqueStruct!) { ( argPointer: UnsafePointer < LDKKeysInterface > ) in
6070
+ return withUnsafePointer ( to: arg. activateOnce ( ) . cOpaqueStruct!) { ( argPointer: UnsafePointer < LDKKeysInterface > ) in
6057
6071
6058
6072
Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ( pointer: C2Tuple_BlockHashChannelMonitorZ_read ( serWrapper. cOpaqueStruct!, argPointer) )
6059
6073
@@ -6230,10 +6244,10 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
6230
6244
6231
6245
return withUnsafePointer ( to: route_params. cOpaqueStruct!) { ( route_paramsPointer: UnsafePointer < LDKRouteParameters > ) in
6232
6246
withUnsafePointer ( to: network. cOpaqueStruct!) { ( networkPointer: UnsafePointer < LDKNetworkGraph > ) in
6233
- withUnsafePointer ( to: scorer. cOpaqueStruct!) { ( scorerPointer: UnsafePointer < LDKScore > ) in
6247
+ withUnsafePointer ( to: scorer. activateOnce ( ) . cOpaqueStruct!) { ( scorerPointer: UnsafePointer < LDKScore > ) in
6234
6248
withUnsafePointer ( to: Bindings . array_to_tuple32 ( array: random_seed_bytes) ) { ( random_seed_bytesPointer: UnsafePointer < ( UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ) > ) in
6235
6249
6236
- Result_RouteLightningErrorZ ( pointer: find_route ( Bindings . new_LDKPublicKey ( array: our_node_pubkey) , route_paramsPointer, networkPointer, first_hopsPointer, logger. cOpaqueStruct!, scorerPointer, random_seed_bytesPointer) )
6250
+ Result_RouteLightningErrorZ ( pointer: find_route ( Bindings . new_LDKPublicKey ( array: our_node_pubkey) , route_paramsPointer, networkPointer, first_hopsPointer, logger. activate ( ) . cOpaqueStruct!, scorerPointer, random_seed_bytesPointer) )
6237
6251
6238
6252
}
6239
6253
}
@@ -6255,7 +6269,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6255
6269
}
6256
6270
6257
6271
return
6258
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_phantom_invoice ( amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description) , Bindings . new_LDKThirtyTwoBytes ( array: payment_hash) , Bindings . new_LDKThirtyTwoBytes ( array: payment_secret) , phantom_route_hintsWrapper. dangle ( ) . cOpaqueStruct!, keys_manager. cOpaqueStruct!, network) )
6272
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_phantom_invoice ( amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description, chars_is_owned : true ) , Bindings . new_LDKThirtyTwoBytes ( array: payment_hash) , Bindings . new_LDKThirtyTwoBytes ( array: payment_secret) , phantom_route_hintsWrapper. dangle ( ) . cOpaqueStruct!, keys_manager. activate ( ) . cOpaqueStruct!, network) )
6259
6273
6260
6274
6261
6275
}
@@ -6273,7 +6287,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6273
6287
}
6274
6288
6275
6289
return
6276
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_phantom_invoice_with_description_hash ( amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKThirtyTwoBytes ( array: payment_hash) , Bindings . new_LDKThirtyTwoBytes ( array: payment_secret) , phantom_route_hintsWrapper. dangle ( ) . cOpaqueStruct!, keys_manager. cOpaqueStruct!, network) )
6290
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_phantom_invoice_with_description_hash ( amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKThirtyTwoBytes ( array: payment_hash) , Bindings . new_LDKThirtyTwoBytes ( array: payment_secret) , phantom_route_hintsWrapper. dangle ( ) . cOpaqueStruct!, keys_manager. activate ( ) . cOpaqueStruct!, network) )
6277
6291
6278
6292
6279
6293
}
@@ -6282,7 +6296,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6282
6296
6283
6297
return withUnsafePointer ( to: channelmanager. cOpaqueStruct!) { ( channelmanagerPointer: UnsafePointer < LDKChannelManager > ) in
6284
6298
6285
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager ( channelmanagerPointer, keys_manager. cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description) ) )
6299
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager ( channelmanagerPointer, keys_manager. activate ( ) . cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description, chars_is_owned : true ) ) )
6286
6300
6287
6301
}
6288
6302
@@ -6292,7 +6306,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6292
6306
6293
6307
return withUnsafePointer ( to: channelmanager. cOpaqueStruct!) { ( channelmanagerPointer: UnsafePointer < LDKChannelManager > ) in
6294
6308
6295
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_with_description_hash ( channelmanagerPointer, keys_manager. cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!) )
6309
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_with_description_hash ( channelmanagerPointer, keys_manager. activate ( ) . cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!) )
6296
6310
6297
6311
}
6298
6312
@@ -6302,7 +6316,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6302
6316
6303
6317
return withUnsafePointer ( to: channelmanager. cOpaqueStruct!) { ( channelmanagerPointer: UnsafePointer < LDKChannelManager > ) in
6304
6318
6305
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch ( channelmanagerPointer, keys_manager. cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!, duration_since_epoch) )
6319
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch ( channelmanagerPointer, keys_manager. activate ( ) . cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, description_hash. danglingClone ( ) . cOpaqueStruct!, duration_since_epoch) )
6306
6320
6307
6321
}
6308
6322
@@ -6312,40 +6326,61 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6312
6326
6313
6327
return withUnsafePointer ( to: channelmanager. cOpaqueStruct!) { ( channelmanagerPointer: UnsafePointer < LDKChannelManager > ) in
6314
6328
6315
- Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_and_duration_since_epoch ( channelmanagerPointer, keys_manager. cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description) , duration_since_epoch) )
6329
+ Result_InvoiceSignOrCreationErrorZ ( pointer: create_invoice_from_channelmanager_and_duration_since_epoch ( channelmanagerPointer, keys_manager. activate ( ) . cOpaqueStruct!, network, amt_msat. danglingClone ( ) . cOpaqueStruct!, Bindings . new_LDKStr ( string: description, chars_is_owned : true ) , duration_since_epoch) )
6316
6330
6317
6331
}
6318
6332
6319
6333
}
6320
6334
6321
6335
/* STATIC_METHODS_END */
6322
6336
6323
- static var nativelyExposedInstances = [ String: NativeTypeWrapper] ( )
6337
+ static var nativelyExposedInstances = [ UInt: NativeTraitWrapper] ( )
6338
+ static var nativelyExposedInstanceReferenceCounter = [ UInt: Int] ( )
6339
+
6340
+ public class func cacheInstance( instance: NativeTraitWrapper , countIdempotently: Bool = false ) {
6341
+ let key = instance. globalInstanceNumber
6342
+ let referenceCount = ( Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0 ) + 1
6343
+ if ( !countIdempotently || referenceCount == 1 ) {
6344
+ // if we count non-idempotently, always update the counter
6345
+ // otherwise, only update the counter the first time
6346
+ Self . nativelyExposedInstanceReferenceCounter [ key] = referenceCount
6347
+ }
6348
+ if referenceCount == 1 {
6349
+ print ( " Caching global instance \( key) . Cached instance count: \( nativelyExposedInstanceReferenceCounter. count) " )
6350
+ Self . nativelyExposedInstances [ key] = instance
6351
+ }
6352
+ }
6324
6353
6325
- public class func instanceToPointer( instance: NativeTypeWrapper ) -> UnsafeMutableRawPointer {
6326
- let pointer = Unmanaged . passUnretained ( instance) . toOpaque ( )
6327
- instance. pointerDebugDescription = pointer. debugDescription
6328
- Self . nativelyExposedInstances [ pointer. debugDescription] = instance
6354
+ public class func instanceToPointer( instance: NativeTraitWrapper ) -> UnsafeMutableRawPointer {
6355
+ let key = instance. globalInstanceNumber
6356
+ let pointer = UnsafeMutableRawPointer ( bitPattern: key) !
6357
+ // don't automatically cache the trait instance
6358
+ // Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
6329
6359
return pointer
6330
6360
}
6331
6361
6332
- public class func pointerToInstance< T: NativeTypeWrapper > ( pointer: UnsafeRawPointer , sourceMarker: String ? ) -> T {
6333
-
6334
- let callStack = Thread . callStackSymbols
6335
- let caller = sourceMarker ?? callStack [ 1 ]
6336
- // print("Retrieving instance from pointer for caller: \(caller)")
6337
- // let value = Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
6338
- let value = Self . nativelyExposedInstances [ pointer. debugDescription] as! T
6339
- // print("Instance retrieved for caller: \(caller)")
6362
+ public class func pointerToInstance< T: NativeTraitWrapper > ( pointer: UnsafeRawPointer , sourceMarker: String ? ) -> T {
6363
+ let key = UInt ( bitPattern: pointer)
6364
+ let referenceCount = Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0
6365
+ if referenceCount < 1 {
6366
+ print ( " Bad lookup: non-positive reference count for instance \( key) : \( referenceCount) ! " , severity: . ERROR)
6367
+ }
6368
+ let value = Self . nativelyExposedInstances [ key] as! T
6340
6369
return value
6341
6370
}
6342
6371
6343
- public class func removeInstancePointer( instance: NativeTypeWrapper ) -> Bool {
6344
- guard let debugDescription = instance. pointerDebugDescription else {
6345
- return false
6372
+ public class func removeInstancePointer( instance: NativeTraitWrapper ) -> Bool {
6373
+ let key = instance. globalInstanceNumber
6374
+ let referenceCount = ( Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0 ) - 1
6375
+ Self . nativelyExposedInstanceReferenceCounter [ key] = referenceCount
6376
+ if referenceCount == 0 {
6377
+ print ( " Uncaching global instance \( key) " )
6378
+ // TODO: fix counting
6379
+ Self . nativelyExposedInstances. removeValue ( forKey: key)
6380
+ instance. pointerDebugDescription = nil
6381
+ } else if referenceCount < 0 {
6382
+ print ( " Bad uncache: negative reference count ( \( referenceCount) ) for instance \( key) ! " , severity: . ERROR)
6346
6383
}
6347
- Self . nativelyExposedInstances. removeValue ( forKey: debugDescription)
6348
- instance. pointerDebugDescription = nil
6349
6384
return true
6350
6385
}
6351
6386
@@ -6429,7 +6464,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6429
6464
}
6430
6465
/* RUST_TO_SWIFT_END */
6431
6466
6432
- public class func LDKStr_to_string( nativeType: LDKStr ) -> String {
6467
+ public class func LDKStr_to_string( nativeType: LDKStr , deallocate : Bool = true ) -> String {
6433
6468
var array = [ UInt8] ( )
6434
6469
for index in 0 ..< Int ( nativeType. len) {
6435
6470
let currentEntry = nativeType. chars [ index]
@@ -6438,6 +6473,9 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6438
6473
}
6439
6474
let data = Data ( bytes: array)
6440
6475
let string = String ( data: data, encoding: . utf8) !
6476
+ if deallocate && nativeType. len > 0 {
6477
+ Str_free ( nativeType)
6478
+ }
6441
6479
return string
6442
6480
}
6443
6481
@@ -6471,7 +6509,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6471
6509
let nativeKeysManager = keysManager. cOpaqueStruct!
6472
6510
let amount = Option_u64Z ( value: amountMsat)
6473
6511
let nativeAmount = amount. cOpaqueStruct!
6474
- let nativeDescription = Self . new_LDKStr ( string: description)
6512
+ let nativeDescription = Self . new_LDKStr ( string: description, chars_is_owned : true )
6475
6513
return withUnsafePointer ( to: channelManager. cOpaqueStruct!) { ( pointer: UnsafePointer < LDKChannelManager > ) -> Result_InvoiceSignOrCreationErrorZ in
6476
6514
let nativeResult = create_invoice_from_channelmanager ( pointer, nativeKeysManager, network, nativeAmount, nativeDescription)
6477
6515
return Result_InvoiceSignOrCreationErrorZ ( pointer: nativeResult)
@@ -6488,14 +6526,14 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6488
6526
}
6489
6527
}
6490
6528
*/
6491
-
6529
+
6492
6530
public class func get_ldk_swift_bindings_version( ) -> String {
6493
- return " fe0ea5b41ca6eb7ef88a4d2fbd7dc1f647c89112 "
6531
+ return " 90b85905ad0889a5368bb8eee79bfbb4d0ff444e "
6494
6532
}
6495
6533
6496
6534
}
6497
6535
6498
- public class InstanceCrashSimulator : NativeTypeWrapper {
6536
+ public class InstanceCrashSimulator : NativeTraitWrapper {
6499
6537
6500
6538
public init ( ) {
6501
6539
super. init ( conflictAvoidingVariableName: 0 )
0 commit comments