@@ -748,59 +748,51 @@ impl Drop for XtraResource {
748748
749749pub mod c_api {
750750 use super :: XtraResource ;
751- use std :: panic :: catch_unwind;
751+ use std :: panic :: { catch_unwind, AssertUnwindSafe } ;
752752 use std :: sync :: atomic :: {AtomicU32 , Ordering };
753753
754754 const INVALID_TAG : u32 = 0 ;
755755 const VALID_TAG : u32 = 0xDEAD_BEEF ;
756756 const ERR_TAG : u32 = 0xDEAF_CAFE ;
757757
758- static COUNTER : AtomicU32 = AtomicU32 :: new (0 );
759-
760758 pub struct CXtraResource {
761759 tag : AtomicU32 , // pour prévenir d'une réutilisation accidentelle
762- id : AtomicU32 , // pour quasi-identifier l'objet
763760 inner : XtraResource ,
764761 }
765762
766763 #[no_mangle]
767764 pub unsafe extern " C" fn xtra_with (cb : unsafe extern " C" fn (* mut CXtraResource ) -> ()) {
768- let inner = if let Ok (res ) = catch_unwind (XtraResource :: new ) {
765+ let inner = if let Ok (res ) = catch_unwind (AssertUnwindSafe ( XtraResource :: new ) ) {
769766 res
770767 } else {
771768# println! (" impossible d'allouer la ressource" );
772769 return ;
773770 };
774- let id = COUNTER . fetch_add (1 , Ordering :: SeqCst );
775771 let tag = VALID_TAG ;
776772
777- // Utilisation de la mémoire du tas pour ne pas fournir de pointeur de
778- // pile au code C!
779- let mut boxed = Box :: new (CXtraResource {
773+ let mut wrapped = CXtraResource {
780774 tag : AtomicU32 :: new (tag ),
781- id : AtomicU32 :: new (id ),
782775 inner
783- }) ;
776+ };
784777
785- # println! (" appel du callback sur {:p}" , boxed );
786- cb (boxed . as_mut () as * mut CXtraResource );
778+ # println! (" appel du callback sur {:p}" , & wrapped );
779+ cb (& mut wrapped as * mut CXtraResource );
787780
788- let new_id = boxed . id. load (Ordering :: SeqCst );
789781 // pour éviter de réutilisation accidentelle
790- let new_tag = boxed . tag. swap (INVALID_TAG , Ordering :: SeqCst );
791- if new_id == id && ( new_tag == VALID_TAG || new_tag == ERR_TAG ) {
792- # println! (" libération de {:p}" , boxed );
782+ let new_tag = wrapped . tag. swap (INVALID_TAG , Ordering :: SeqCst );
783+ if new_tag == VALID_TAG || new_tag == ERR_TAG {
784+ # println! (" libération de {:p}" , & wrapped );
793785 // drop implicite de la `box`
794786 } else {
795- # println! (" oubli de {:p}" , boxed );
787+ # println! (" oubli de {:p}" , & wrapped );
796788 // (...) gestion des erreurs (partie critique)
797- std :: mem :: forget (boxed ); // la boîte est corrompu, ne pas libérer!
789+ std :: mem :: forget (wrapped ); // la boîte est corrompu, ne pas libérer!
798790 }
799791 }
800792
801793 #[no_mangle]
802794 pub unsafe extern " C" fn xtra_dosthg (cxtra : * mut CXtraResource , arg : u32 ) {
803- let do_it = || {
795+ let do_it = move || {
804796 if let Some (cxtra ) = cxtra . as_mut () {
805797 if cxtra . tag. load (Ordering :: SeqCst ) == VALID_TAG {
806798# println! (" fait quelque chose avec {:p}" , cxtra );
0 commit comments