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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#[macro_export]
macro_rules! define_infallible_handle_map_deleter {
($HANDLE_MAP_NAME:ident, $destructor_name:ident) => {
#[no_mangle]
pub extern "C" fn $destructor_name(v: u64) {
let mut error = ffi_support::ExternError::success();
let res = ffi_support::abort_on_panic::call_with_result(&mut error, || {
let map: &$crate::ConcurrentHandleMap<_> = &*$HANDLE_MAP_NAME;
map.delete_u64(v)
});
$crate::handlemap_ext::log_if_error(error);
res
}
};
}
#[macro_export]
macro_rules! define_metric {
($metric_type:ident => $metric_map:ident {
$(new -> $new_fn:ident($($new_argname:ident: $new_argtyp:ty),* $(,)*),)?
destroy -> $destroy_fn:ident,
$(
$op:ident -> $op_fn:ident($($op_argname:ident: $op_argtyp:ty),* $(,)*)
),* $(,)*
}) => {
lazy_static::lazy_static! {
pub static ref $metric_map: ffi_support::ConcurrentHandleMap<glean_core::metrics::$metric_type> = ffi_support::ConcurrentHandleMap::new();
}
$crate::define_infallible_handle_map_deleter!($metric_map, $destroy_fn);
$(
#[no_mangle]
pub extern "C" fn $new_fn(
category: ffi_support::FfiStr,
name: ffi_support::FfiStr,
send_in_pings: crate::RawStringArray,
send_in_pings_len: i32,
lifetime: i32,
disabled: u8,
$($new_argname: $new_argtyp),*
) -> u64 {
$metric_map.insert_with_log(|| {
let name = crate::FallibleToString::to_string_fallible(&name)?;
let category = crate::FallibleToString::to_string_fallible(&category)?;
let send_in_pings = crate::from_raw_string_array(send_in_pings, send_in_pings_len)?;
let lifetime = std::convert::TryFrom::try_from(lifetime)?;
$(
let $new_argname = std::convert::TryFrom::try_from($new_argname)?;
)*
Ok(glean_core::metrics::$metric_type::new(glean_core::CommonMetricData {
name,
category,
send_in_pings,
lifetime,
disabled: disabled != 0,
..Default::default()
}, $($new_argname),*))
})
}
)?
$(
#[no_mangle]
pub extern "C" fn $op_fn(glean_handle: u64, metric_id: u64, $($op_argname: $op_argtyp),*) {
crate::HandleMapExtension::call_infallible(&*crate::GLEAN, glean_handle, |glean| {
crate::HandleMapExtension::call_infallible(&*$metric_map, metric_id, |metric| {
metric.$op(glean, $($op_argname),*);
})
})
}
)*
}
}