@@ -368,19 +368,16 @@ where
368
368
Arc :: into_raw ( Arc :: new ( src) ) as * const _
369
369
}
370
370
371
- /// Convert a const pointer to a [`Castable`] to an optional `Arc` over the underlying rust type.
371
+ /// Given a const pointer to a [`Castable`] representing an `Arc`, clone the `Arc` and return
372
+ /// the corresponding Rust type.
372
373
///
373
- /// Sometimes we create an `Arc`, then call `into_raw` and return the resulting raw pointer
374
- /// to C, e.g. using [`to_arc_const_ptr`]. C can then call a rustls-ffi function multiple times
375
- /// using that same raw pointer. On each call, we need to reconstruct the Arc, but once we reconstruct
376
- /// the Arc, its reference count will be decremented on drop. We need to reference count to stay at 1,
377
- /// because the C code is holding a copy.
374
+ /// The caller still owns its copy of the `Arc`. In other words, the reference count of the
375
+ /// `Arc` will be incremented by 1 by the end of this function.
378
376
///
379
- /// This function turns the raw pointer back into an Arc, clones it to increment the reference count
380
- /// (which will make it 2 in this particular case), and `mem::forget`s the clone. The `mem::forget`
381
- /// prevents the reference count from being decremented when we exit this function, so it will stay
382
- /// at 2 as long as we are in Rust code. Once the caller drops its `Arc`, the reference count will
383
- /// go back down to 1, since the C code's copy remains.
377
+ /// To achieve that, we need to `mem::forget` the `Arc` we get back from `into_raw`, because
378
+ /// `into_raw` _does_ take back ownership. If we called `into_raw` without `mem::forget`, at the
379
+ /// end of the function that Arc would be dropped and the reference count would be decremented,
380
+ /// potentially to 0, causing memory to be freed.
384
381
///
385
382
/// Does nothing, returning `None`, when passed a `NULL` pointer. Can only be used when the
386
383
/// `Castable` has specified a cast type equal to [`OwnershipArc`].
@@ -389,7 +386,7 @@ where
389
386
///
390
387
/// If non-null, `ptr` must be a pointer that resulted from previously calling `Arc::into_raw`,
391
388
/// e.g. from using [`to_arc_const_ptr`].
392
- pub ( crate ) fn to_arc < C > ( ptr : * const C ) -> Option < Arc < C :: RustType > >
389
+ pub ( crate ) fn clone_arc < C > ( ptr : * const C ) -> Option < Arc < C :: RustType > >
393
390
where
394
391
C : Castable < Ownership = OwnershipArc > ,
395
392
{
@@ -566,32 +563,20 @@ macro_rules! try_ref_from_ptr {
566
563
567
564
pub ( crate ) use try_ref_from_ptr;
568
565
569
- /// Convert a const pointer to a [`Castable`] to an optional `Arc` over the underlying
570
- /// [`Castable::RustType`]. See [`to_arc`] for more information.
571
- ///
572
- /// Does nothing, returning `None`, when passed `NULL`. Can only be used with `Castable`'s that
573
- /// specify an ownership type of [`OwnershipArc`].
574
- pub ( crate ) fn try_arc_from < C > ( from : * const C ) -> Option < Arc < C :: RustType > >
575
- where
576
- C : Castable < Ownership = OwnershipArc > ,
577
- {
578
- to_arc ( from)
579
- }
580
-
581
566
/// If the provided pointer to a [`Castable`] is non-null, convert it to a reference to an `Arc` over
582
567
/// the underlying rust type using [`try_arc_from`]. Otherwise, return
583
568
/// [`rustls_result::NullParameter`], or an appropriate default (`false`, `0`, `NULL`) based on the
584
569
/// context. See [`try_arc_from`] for more information.
585
- macro_rules! try_arc_from_ptr {
570
+ macro_rules! try_clone_arc {
586
571
( $var: ident ) => {
587
- match $crate:: try_arc_from ( $var) {
572
+ match $crate:: clone_arc ( $var) {
588
573
Some ( c) => c,
589
574
None => return $crate:: panic:: NullParameterOrDefault :: value( ) ,
590
575
}
591
576
} ;
592
577
}
593
578
594
- pub ( crate ) use try_arc_from_ptr ;
579
+ pub ( crate ) use try_clone_arc ;
595
580
596
581
/// Convert a mutable pointer to a [`Castable`] to an optional `Box` over the underlying
597
582
/// [`Castable::RustType`].
0 commit comments