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
use std::mem;
use std::slice;
use std::sync::Arc;

use parking_lot::RwLock;

use crate::allocators::bump_allocator::BumpAllocator;
use crate::tls as nando_tls;

pub trait PersistentlyAllocatable {
    fn set_allocator(&mut self, _allocator: Arc<RwLock<BumpAllocator>>) {}
    fn get_allocator(&self) -> Option<Arc<RwLock<BumpAllocator>>> {
        None
    }
}

macro_rules! simple_pa_impl {
    ($t:ty) => {
        impl PersistentlyAllocatable for $t {}
    };
}

simple_pa_impl!(u8);
simple_pa_impl!(u16);
simple_pa_impl!(u32);
simple_pa_impl!(u64);
simple_pa_impl!(u128);
simple_pa_impl!(usize);

simple_pa_impl!(i8);
simple_pa_impl!(i16);
simple_pa_impl!(i32);
simple_pa_impl!(i64);
simple_pa_impl!(i128);
simple_pa_impl!(isize);

simple_pa_impl!(char);

pub trait CopyFrom {
    type From;

    fn from(&mut self, source: Self::From);
}

macro_rules! simple_cf_impl {
    ($from:ty) => {
        impl CopyFrom for $from {
            type From = $from;

            fn from(&mut self, source: Self::From) {
                let ptr = std::ptr::addr_of!(*self);
                let self_bytes = unsafe {
                    slice::from_raw_parts(
                        (self as *const Self) as *const u8,
                        mem::size_of::<Self>(),
                    )
                };
                nando_tls::add_new_pre_image(ptr as *const (), self_bytes);

                *self = source;

                let self_bytes = unsafe {
                    slice::from_raw_parts(
                        (self as *const Self) as *const u8,
                        mem::size_of::<Self>(),
                    )
                };
                nando_tls::add_new_post_image_if_changed(ptr as *const (), self_bytes);
            }
        }
    };
}

simple_cf_impl!(u32);
simple_cf_impl!(u128);
simple_cf_impl!(usize);