rustc_codegen_ssa/back/
lto.rs

1use std::ffi::CString;
2use std::sync::Arc;
3
4use rustc_data_structures::memmap::Mmap;
5
6use crate::traits::*;
7
8pub struct ThinModule<B: WriteBackendMethods> {
9    pub shared: Arc<ThinShared<B>>,
10    pub idx: usize,
11}
12
13impl<B: WriteBackendMethods> ThinModule<B> {
14    pub fn name(&self) -> &str {
15        self.shared.module_names[self.idx].to_str().unwrap()
16    }
17
18    pub fn cost(&self) -> u64 {
19        // Yes, that's correct, we're using the size of the bytecode as an
20        // indicator for how costly this codegen unit is.
21        self.data().len() as u64
22    }
23
24    pub fn data(&self) -> &[u8] {
25        let a = self.shared.thin_buffers.get(self.idx).map(|b| b.data());
26        a.unwrap_or_else(|| {
27            let len = self.shared.thin_buffers.len();
28            self.shared.serialized_modules[self.idx - len].data()
29        })
30    }
31}
32
33pub struct ThinShared<B: WriteBackendMethods> {
34    pub data: B::ThinData,
35    pub thin_buffers: Vec<B::ThinBuffer>,
36    pub serialized_modules: Vec<SerializedModule<B::ModuleBuffer>>,
37    pub module_names: Vec<CString>,
38}
39
40pub enum SerializedModule<M: ModuleBufferMethods> {
41    Local(M),
42    FromRlib(Vec<u8>),
43    FromUncompressedFile(Mmap),
44}
45
46impl<M: ModuleBufferMethods> SerializedModule<M> {
47    pub fn data(&self) -> &[u8] {
48        match *self {
49            SerializedModule::Local(ref m) => m.data(),
50            SerializedModule::FromRlib(ref m) => m,
51            SerializedModule::FromUncompressedFile(ref m) => m,
52        }
53    }
54}