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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::mem::size_of;

use gc_info_table::GCInfo;
use header::HeapObjectHeader;
use large_space::PreciseAllocation;
/// Just like C's offsetof.
///
/// The magic number 0x4000 is insignificant. We use it to avoid using NULL, since
/// NULL can cause compiler problems, especially in cases of multiple inheritance.
#[macro_export]
macro_rules! offsetof {
    ($name : ident . $($field: ident).*) => {
        unsafe {
            let uninit = std::mem::transmute::<_,*const $name>(0x4000usize);
            let fref = &(&*uninit).$($field).*;
            let faddr = fref as *const _ as usize;
            faddr - 0x4000
        }
    }
}

macro_rules! as_atomic {
    ($value: expr;$t: ident) => {
        unsafe { core::mem::transmute::<_, &'_ core::sync::atomic::$t>($value as *const _) }
    };
}

macro_rules! logln_if {
    ($cond: expr, $($t:tt)*) => {
        if $cond {
            println!($($t)*);
        }
    };
}
/// rounds the given value `val` up to the nearest multiple
/// of `align`
pub fn align(value: u32, align: u32) -> u32 {
    if align == 0 {
        return value;
    }

    ((value + align - 1) / align) * align
}
macro_rules! log_if {
    ($cond: expr, $($t:tt)*) => {
        if $cond {
            print!($($t)*);
        }
    };
}
pub mod allocation_config;
pub mod allocator;
pub mod block;
pub mod block_allocator;
pub mod gc_info_table;
pub mod gcref;
pub mod global_allocator;
pub mod globals;
pub mod header;
pub mod heap;
pub mod internal;
pub mod large_space;
pub mod marking;
pub mod mmap;
pub mod task_scheduler;
pub mod visitor;


pub struct GCPlatform;

impl GCPlatform {
    /// Initializes global state for GC.
    pub fn initialize() {
        #[cfg(target_family = "wasm")]
        {
            panic!("Invoke GCPlatform::initialize_wasm on WASM!");
        }
        unsafe {
            gc_info_table::GCInfoTable::init(None);
        }
    }

    pub unsafe fn initialize_wasm(
        _gc_info_table_mem: &'static mut [u8; size_of::<GCInfo>() * (1 << 14)],
    ) {
    }
}

/// Configuration for heap constructor.
#[repr(C)]
pub struct Config {
    /// How fast heap threshold should grow
    pub heap_growth_factor: f64,
    /// Heap size. It is heap size only for Immix block space. LargeObjectSpace will allocate until System OOMs
    pub heap_size: usize,
    /// Maximum heap size before first GC
    pub max_heap_size: usize,
    /// Maximum eden heap size before first GC (does not matter atm)
    pub max_eden_size: usize,
    /// Enables verbose printing
    pub verbose: bool,
    /// Enable generational GC (does not work atm)
    pub generational: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            generational: false,
            verbose: false,
            max_eden_size: 64 * 1024,
            max_heap_size: 256 * 1024,
            heap_growth_factor: 1.5,
            heap_size: 1 * 1024 * 1024 * 1024,
        }
    }
}

/// Returns GC allocation size of object. 
pub fn gc_size(ptr: *const HeapObjectHeader) -> usize {
    unsafe {
        let size = (*ptr).get_size();
        if size == 0 {
            (*PreciseAllocation::from_cell(ptr as _)).cell_size()
        } else {
            size
        }
    }
}

pub mod c_api {

    use std::ptr::{null_mut, NonNull};

    use crate::{
        gc_info_table::{GCInfo, GC_TABLE},
        gcref::{UntypedGcRef, WeakGcRef},
        header::HeapObjectHeader,
        heap::Heap,
        internal::gc_info::GCInfoIndex,
        visitor::Visitor,
        Config, GCPlatform,
    };

    #[no_mangle]
    pub extern "C" fn comet_gc_size(ptr: *const HeapObjectHeader) -> usize {
        super::gc_size(ptr)
    }
    #[no_mangle]
    pub extern "C" fn comet_default_config() -> Config {
        Config::default()
    }
    #[no_mangle]
    pub extern "C" fn comet_init() {
        GCPlatform::initialize();
    }
    #[no_mangle]
    pub extern "C" fn comet_heap_create(config: Config) -> *mut Heap {
        Box::into_raw(Heap::new(config))
    }
    /// Free comet heap
    #[no_mangle]
    pub extern "C" fn comet_heap_free(heap: *mut Heap) {
        unsafe {
            Box::from_raw(heap);
        }
    }

    /// Add GC constraint to the Comet Heap. Each constraint is executed when marking starts
    /// to obtain list of root objects.
    #[no_mangle]
    pub extern "C" fn comet_heap_add_constraint(
        heap: *mut Heap,
        data: *mut u8,
        callback: extern "C" fn(*mut u8, *mut Visitor),
    ) {
        unsafe {
            (*heap).add_constraint(move |vis: &mut Visitor| {
                let data = data;
                callback(data, vis as *mut _);
            });
        }
    }

    /// Add core constraints to the heap. This one will setup stack scanning routines.
    #[no_mangle]
    pub extern "C" fn comet_heap_add_core_constraints(heap: *mut Heap) {
        unsafe {
            (*heap).add_core_constraints();
        }
    }

    #[no_mangle]
    pub extern "C" fn comet_heap_collect(heap: *mut Heap) {
        unsafe {
            (*heap).collect_garbage();
        }
    }

    #[no_mangle]
    pub extern "C" fn comet_heap_collect_if_necessary_or_defer(heap: *mut Heap) {
        unsafe {
            (*heap).collect_if_necessary_or_defer();
        }
    }

    #[no_mangle]
    pub extern "C" fn comet_heap_allocate_weak(
        heap: *mut Heap,
        object: *mut HeapObjectHeader,
    ) -> WeakGcRef {
        unsafe {
            (*heap).allocate_weak(UntypedGcRef {
                header: NonNull::new_unchecked(object),
            })
        }
    }

    /// Allocates memory and returns pointer. NULL is returned if no memory is available.
    #[no_mangle]
    pub extern "C" fn comet_heap_allocate(
        heap: *mut Heap,
        size: usize,
        index: GCInfoIndex,
    ) -> *mut HeapObjectHeader {
        unsafe {
            match (*heap).allocate_raw(size, index) {
                Some(mem) => mem.header.as_ptr(),
                None => null_mut(),
            }
        }
    }

    /// Allocates memory and returns pointer. When no memory is left process is aborted.
    #[no_mangle]
    pub extern "C" fn comet_heap_allocate_or_fail(
        heap: *mut Heap,
        size: usize,
        index: GCInfoIndex,
    ) -> *mut HeapObjectHeader {
        unsafe { (*heap).allocate_raw_or_fail(size, index).header.as_ptr() }
    }

    /// Upgrade weak ref. If it is still alive then pointer is returned otherwise NULL is returned.
    #[no_mangle]
    pub extern "C" fn comet_weak_upgrade(weak: WeakGcRef) -> *mut HeapObjectHeader {
        match weak.upgrade() {
            Some(ptr) => ptr.header.as_ptr(),
            None => null_mut(),
        }
    }

    #[no_mangle]
    pub extern "C" fn comet_trace(vis: *mut Visitor, ptr: *mut HeapObjectHeader) {
        if ptr.is_null() {
            return;
        }
        unsafe {
            (*vis).trace_untyped(UntypedGcRef {
                header: NonNull::new_unchecked(ptr),
            })
        }
    }

    #[no_mangle]
    pub extern "C" fn comet_trace_conservatively(
        vis: *mut Visitor,
        from: *const u8,
        to: *const u8,
    ) {
        unsafe { (*vis).trace_conservatively(from, to) }
    }

    #[no_mangle]
    pub extern "C" fn comet_add_gc_info(info: GCInfo) -> GCInfoIndex {
        unsafe { GC_TABLE.add_gc_info(info) }
    }

    #[no_mangle]
    pub extern "C" fn comet_get_gc_info(index: GCInfoIndex) -> *mut GCInfo {
        unsafe { GC_TABLE.get_gc_info_mut(index) as *mut _ }
    }
}