-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand.rs
74 lines (67 loc) · 2.14 KB
/
expand.rs
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
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
Expr, Ident, Result, Token,
};
pub(crate) struct BtfMapArgs {
pub(crate) name: Ident,
pub(crate) map_type: Expr,
pub(crate) key_type: Expr,
pub(crate) value_type: Expr,
pub(crate) max_entries: Expr,
pub(crate) map_flags: Expr,
}
impl Parse for BtfMapArgs {
fn parse(input: ParseStream) -> Result<Self> {
let name = input.parse()?;
input.parse::<Token![,]>()?;
let map_type = input.parse()?;
input.parse::<Token![,]>()?;
let key_type = input.parse()?;
input.parse::<Token![,]>()?;
let value_type = input.parse()?;
input.parse::<Token![,]>()?;
let max_entries = input.parse()?;
input.parse::<Token![,]>()?;
let map_flags = input.parse()?;
Ok(Self {
name,
map_type,
key_type,
value_type,
max_entries,
map_flags,
})
}
}
pub(crate) fn btf_map(args: BtfMapArgs) -> Result<TokenStream> {
let BtfMapArgs {
name,
map_type,
key_type,
value_type,
max_entries,
map_flags,
} = args;
let struct_name = Ident::new(&format!("_ty_{}", name), Span::call_site());
let name_str = format!("{}", name);
Ok(quote! {
pub struct #struct_name {
pub r#type: *const [i32; #map_type as usize],
pub key: *const #key_type,
pub value: *const #value_type,
pub max_entries: *const [i32; #max_entries as usize],
pub map_flags: *const [i32; #map_flags as usize],
}
#[link_section = ".maps"]
#[export_name = #name_str]
pub static mut #name: #struct_name = #struct_name {
r#type: &[0i32; #map_type as usize] as *const [i32; #map_type as usize],
key: ::core::ptr::null(),
value: ::core::ptr::null(),
max_entries: &[0i32; #max_entries as usize] as *const [i32; #max_entries as usize],
map_flags: &[0i32; #map_flags as usize] as *const [i32; #map_flags as usize],
};
})
}