Skip to content

Commit 728a875

Browse files
committed
SImplify example (remove id)
1 parent 55912ce commit 728a875

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

src/en/07_ffi.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -690,59 +690,51 @@ impl Drop for XtraResource {
690690

691691
pub mod c_api {
692692
use super::XtraResource;
693-
use std::panic::catch_unwind;
693+
use std::panic::{catch_unwind, AssertUnwindSafe};
694694
use std::sync::atomic::{AtomicU32, Ordering};
695695

696696
const INVALID_TAG: u32 = 0;
697697
const VALID_TAG: u32 = 0xDEAD_BEEF;
698698
const ERR_TAG: u32 = 0xDEAF_CAFE;
699699

700-
static COUNTER: AtomicU32 = AtomicU32::new(0);
701-
702700
pub struct CXtraResource {
703701
tag: AtomicU32, // to detect accidental reuse
704-
id: AtomicU32, // to quasi-identify the object
705702
inner: XtraResource,
706703
}
707704

708705
#[no_mangle]
709706
pub unsafe extern "C" fn xtra_with(cb: unsafe extern "C" fn(*mut CXtraResource) -> ()) {
710-
let inner = if let Ok(res) = catch_unwind(XtraResource::new) {
707+
let inner = if let Ok(res) = catch_unwind(AssertUnwindSafe(XtraResource::new)) {
711708
res
712709
} else {
713710
# println!("cannot allocate resource");
714711
return;
715712
};
716-
let id = COUNTER.fetch_add(1, Ordering::SeqCst);
717713
let tag = VALID_TAG;
718714

719-
// Use heap memory and do not provide pointer to stack to C code!
720-
let mut boxed = Box::new(CXtraResource {
715+
let mut wrapped = CXtraResource {
721716
tag: AtomicU32::new(tag),
722-
id: AtomicU32::new(id),
723717
inner
724-
});
718+
};
725719

726-
# println!("running the callback on {:p}", boxed);
727-
cb(boxed.as_mut() as *mut CXtraResource);
720+
# println!("running the callback on {:p}", &wrapped);
721+
cb(&mut wrapped as *mut CXtraResource);
728722

729-
let new_id = boxed.id.load(Ordering::SeqCst);
730723
// prevent accidental reuse
731-
let new_tag = boxed.tag.swap(INVALID_TAG, Ordering::SeqCst);
732-
733-
if new_id == id && (new_tag == VALID_TAG || new_tag == ERR_TAG) {
734-
# println!("freeing {:p}", boxed);
735-
// implicit boxed drop
724+
let new_tag = wrapped.tag.swap(INVALID_TAG, Ordering::SeqCst);
725+
if new_tag == VALID_TAG || new_tag == ERR_TAG {
726+
# println!("freeing {:p}", &wrapped);
727+
// implicit drop of wrappped
736728
} else {
737-
# println!("forgetting {:p}", boxed);
729+
# println!("forgetting {:p}", &wrapped);
738730
// (...) error handling (should be fatal)
739-
std::mem::forget(boxed); // boxed is corrupted it should not be
731+
std::mem::forget(wrapped);
740732
}
741733
}
742734

743735
#[no_mangle]
744736
pub unsafe extern "C" fn xtra_dosthg(cxtra: *mut CXtraResource, arg: u32) {
745-
let do_it = || {
737+
let do_it = move || {
746738
if let Some(cxtra) = cxtra.as_mut() {
747739
if cxtra.tag.load(Ordering::SeqCst) == VALID_TAG {
748740
# println!("doing something with {:p}", cxtra);

src/fr/07_ffi.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -748,59 +748,51 @@ impl Drop for XtraResource {
748748

749749
pub 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

Comments
 (0)