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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![cfg(windows)]
extern crate winapi;
use std::sync::atomic::{AtomicUsize, Ordering};
#[repr(transparent)]
pub struct VTable<T> {
pub ptr: *const T,
}
impl<T> VTable<T> {
pub fn new(ptr: &'static T) -> Self {
VTable { ptr }
}
}
impl<T> std::fmt::Debug for VTable<T> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_tuple("VTable").field(&self.ptr).finish()
}
}
pub unsafe trait BuildVTable<T: 'static> {
const VTBL: T;
fn static_vtable() -> VTable<T>;
}
#[derive(Debug)]
pub struct Refcount {
count: AtomicUsize,
}
impl Default for Refcount {
fn default() -> Self {
Refcount {
count: AtomicUsize::new(1),
}
}
}
impl Refcount {
#[inline]
pub unsafe fn add_ref(&self) -> u32 {
self.count.fetch_add(1, Ordering::Acquire) as u32 + 1
}
#[inline]
pub unsafe fn release(&self) -> u32 {
self.count.fetch_sub(1, Ordering::Release) as u32 - 1
}
}