janus_plugin/
refcount.rs

1/// Utilities for working with Janus reference counts.
2use glib_sys;
3use janus_plugin_sys as ffi;
4use std::ffi::CString;
5pub use ffi::janus_refcount as ReferenceCount;
6
7/// Atomically increment the given reference count by 1.
8pub fn increase(refcount: &ReferenceCount) {
9    let field = &refcount.count;
10    unsafe {
11        if ffi::refcount_debug == 1 {
12            let msg = CString::new(format!("[rust:increase] {:p} ({:?})\n", refcount, field + 1)).unwrap();
13            ffi::janus_vprintf(msg.as_ptr());
14        }
15        glib_sys::g_atomic_int_inc(field as *const _ as *mut _);
16    }
17}
18
19/// Atomically decrement the given reference count by 1. If it's 0, call free.
20pub fn decrease(refcount: &ReferenceCount) {
21    let field = &refcount.count;
22    unsafe {
23        if ffi::refcount_debug == 1 {
24            let msg = CString::new(format!("[rust:decrease] {:p} ({:?})\n", refcount, field - 1)).unwrap();
25            ffi::janus_vprintf(msg.as_ptr());
26        }
27        if glib_sys::g_atomic_int_dec_and_test(field as *const _ as *mut _) == 1 {
28            (refcount.free)(refcount);
29        }
30    }
31}