1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// Utilities for working with Janus reference counts.
use glib_sys;
use janus_plugin_sys as ffi;
use std::ffi::CString;
pub use ffi::janus_refcount as ReferenceCount;

/// Atomically increment the given reference count by 1.
pub fn increase(refcount: &ReferenceCount) {
    let field = &refcount.count;
    unsafe {
        if ffi::refcount_debug == 1 {
            let msg = CString::new(format!("[rust:increase] {:p} ({:?})\n", refcount, field + 1)).unwrap();
            ffi::janus_vprintf(msg.as_ptr());
        }
        glib_sys::g_atomic_int_inc(field as *const _ as *mut _);
    }
}

/// Atomically decrement the given reference count by 1. If it's 0, call free.
pub fn decrease(refcount: &ReferenceCount) {
    let field = &refcount.count;
    unsafe {
        if ffi::refcount_debug == 1 {
            let msg = CString::new(format!("[rust:decrease] {:p} ({:?})\n", refcount, field - 1)).unwrap();
            ffi::janus_vprintf(msg.as_ptr());
        }
        if glib_sys::g_atomic_int_dec_and_test(field as *const _ as *mut _) == 1 {
            (refcount.free)(refcount);
        }
    }
}