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
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Handles the internals of trace sharing.

use crate::CLoc;

// Macro defined in `crate::mem`.
new! {
    mod mem for Vec<super::CLoc>, uid: Trace
}

pub use mem::{AsRead, AsWrite, Trace};

/// Registers a list of locations and returns its UID.
pub fn add(trace: Vec<CLoc>) -> Trace {
    let mut mem = mem::write();
    mem.get_uid(trace)
}

/// Registers some lists of locations and returns its UID.
pub fn add_all(capa: usize, mut get_loc: impl FnMut() -> Option<Vec<CLoc>>) -> Vec<Trace> {
    let mut mem = mem::write();
    let mut res = Vec::with_capacity(capa);
    while let Some(locs) = get_loc() {
        res.push(mem.get_uid(locs))
    }
    res
}

/// Retrieves a list of locations from its UID.
pub fn get(uid: Trace) -> std::sync::Arc<Vec<CLoc>> {
    let mem = mem::read();
    mem.get_elm(uid)
}