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
284
285
286
287
288
289
290
291
292
293
//! Ruby's garbage collector.

use crate::{
    prelude::*,
    ruby::{self, VALUE},
};

/// Starts the garbage collector.
#[inline]
pub fn start() {
    unsafe { ruby::rb_gc_start() };
}

/// Tells the garbage collector how much memory is being used by an external
/// library. This may trigger the GC to resize or free emory blocks.
#[inline]
pub fn adjust_mem_usage(diff: isize) {
    unsafe { ruby::rb_gc_adjust_memory_usage(diff) };
}

/// Returns the number of times the GC has ran.
#[inline]
pub fn count() -> usize {
    unsafe { ruby::rb_gc_count() }
}

/// Disables the garbage collector, returning whether it was already previously
/// disabled.
#[inline]
pub fn disable() -> bool {
    unsafe { ruby::rb_gc_disable() != 0 }
}

/// Enables the garbage collector, returning whether it was already previously
/// enabled.
#[inline]
pub fn enable() -> bool {
    unsafe { ruby::rb_gc_enable() != 0 }
}

/// Calls `f` while the garbage collector is disabled.
#[inline]
pub fn disabled<F, O>(f: F) -> O
    where F: FnOnce() -> O
{
    disable();
    let output = f();
    enable();
    output
}

/// Forces `obj` to be garbage-collected.
///
/// # Safety
///
/// The caller must ensure that `obj` does not have ownership over any
/// currently-referenced memory.
#[inline]
pub unsafe fn force_recycle(obj: impl Object) {
    ruby::rb_gc_force_recycle(obj.raw());
}

// Only safely usable with `Symbol` and `Hash`
#[inline]
unsafe fn _stat_unchecked(key: AnyObject) -> usize {
    ruby::rb_gc_stat(key.raw())
}

// monomorphization
unsafe fn _stat(key: AnyObject) -> Result<usize> {
    crate::protected_no_panic(|| _stat_unchecked(key))
}

/// Returns the status information for `key`, or an exception if one is raised.
///
/// # Examples
///
/// The number of available heap slots can be retrieved as such:
///
/// ```
/// # rosy::vm::init().unwrap();
/// let slots = rosy::gc::stat("heap_available_slots").unwrap();
/// assert_ne!(slots, 0);
/// ```
#[inline]
pub fn stat(key: impl GcInfoKey) -> Result<usize> {
    key.stat_gc()
}

/// Returns the status information for `key`.
///
/// # Safety
///
/// An exception may be raised if `key` is unknown.
#[inline]
pub unsafe fn stat_unchecked(key: impl GcInfoKey) -> usize {
    key.stat_gc_unchecked()
}

// Only safely usable with `Symbol` and `Hash`
#[inline]
unsafe fn _latest_info_unchecked(key: AnyObject) -> AnyObject {
    AnyObject::from_raw(ruby::rb_gc_latest_gc_info(key.raw()))
}

// monomorphization
unsafe fn _latest_info(key: AnyObject) -> Result<AnyObject> {
    crate::protected_no_panic(|| _latest_info_unchecked(key))
}

/// Returns the latest information regarding `key` with respect to the garbage
/// collector.
#[inline]
pub fn latest_info(key: impl GcInfoKey) -> Result<AnyObject> {
    key.latest_gc_info()
}

/// Returns the latest information regarding `key` with respect to the garbage
/// collector.
///
/// # Safety
///
/// An exception may be raised if `key` is unknown.
#[inline]
pub unsafe fn latest_info_unchecked(key: impl GcInfoKey) -> AnyObject {
    key.latest_gc_info_unchecked()
}

/// Marks the object for Ruby to avoid garbage collecting it.
#[inline]
pub fn mark(obj: impl Object) {
    unsafe { ruby::rb_gc_mark(obj.raw()) };
}

/// Iterates over `objs`, `mark`ing each one.
#[inline]
pub fn mark_iter<I, O>(objs: I)
where
    I: IntoIterator<Item = O>,
    O: Object,
{
    objs.into_iter().for_each(mark);
}

/// Marks the object for Ruby to avoid garbage collecting it.
// TODO: Figure out what the difference is between this and `mark`
#[inline]
pub fn mark_maybe(obj: impl Object) {
    unsafe { ruby::rb_gc_mark_maybe(obj.raw()) };
}

/// Registers the object address with the garbage collector and tells it to
/// avoid collecting it.
#[inline]
pub fn register_mark(obj: impl Object) {
    unsafe { ruby::rb_gc_register_mark_object(obj.raw()) };
}

/// Registers `address` with the garbage collector.
#[inline]
pub fn register(address: &impl Object) {
    let address = address as *const _ as *const VALUE as *mut VALUE;
    unsafe { ruby::rb_gc_register_address(address) };
}

/// Unregisters `address` with the garbage collector.
#[inline]
pub fn unregister(address: &impl Object) {
    let address = address as *const _ as *const VALUE as *mut VALUE;
    unsafe { ruby::rb_gc_unregister_address(address) };
}

/// A key that can be used to look up what the latest information is about the
/// garbage collector.
pub trait GcInfoKey: Sized {
    /// Returns the status information for `self` with respect to the garbage
    /// collector, or an exception if one is raised.
    #[inline]
    fn stat_gc(self) -> Result<usize>;

    /// Returns the status information for `self` with respect to the garbage
    /// collector.
    ///
    /// # Safety
    ///
    /// If the key is not available, an exception is thrown that should be
    /// caught and handled correctly.
    unsafe fn stat_gc_unchecked(self) -> usize;

    /// Returns the latest information regarding `self` with respect to the
    /// garbage collector.
    ///
    /// If an exception is raised, it is returned as a `Result::Err`.
    #[inline]
    fn latest_gc_info(self) -> Result<AnyObject>;

    /// Returns the latest information regarding `self` with respect to the
    /// garbage collector.
    ///
    /// # Safety
    ///
    /// If the key is not available, an exception is thrown that should be
    /// caught and handled correctly.
    unsafe fn latest_gc_info_unchecked(self) -> AnyObject;
}

impl GcInfoKey for Hash {
    #[inline]
    fn stat_gc(self) -> Result<usize> {
        unsafe { _stat(self.into()) }
    }

    #[inline]
    unsafe fn stat_gc_unchecked(self) -> usize {
        _stat_unchecked(self.into())
    }

    #[inline]
    fn latest_gc_info(self) -> Result<AnyObject> {
        unsafe { _latest_info(self.into()) }
    }

    #[inline]
    unsafe fn latest_gc_info_unchecked(self) -> AnyObject {
        _latest_info_unchecked(self.into())
    }
}

impl GcInfoKey for Symbol {
    #[inline]
    fn stat_gc(self) -> Result<usize> {
        unsafe { _stat(self.into()) }
    }

    #[inline]
    unsafe fn stat_gc_unchecked(self) -> usize {
        _stat_unchecked(self.into())
    }

    #[inline]
    fn latest_gc_info(self) -> Result<AnyObject> {
        unsafe { _latest_info(self.into()) }
    }

    #[inline]
    unsafe fn latest_gc_info_unchecked(self) -> AnyObject {
        _latest_info_unchecked(self.into())
    }
}

impl GcInfoKey for &str {
    #[inline]
    fn stat_gc(self) -> Result<usize> {
        Symbol::from(self).stat_gc()
    }

    #[inline]
    unsafe fn stat_gc_unchecked(self) -> usize {
        Symbol::from(self).stat_gc_unchecked()
    }

    #[inline]
    fn latest_gc_info(self) -> Result<AnyObject> {
        Symbol::from(self).latest_gc_info()
    }

    #[inline]
    unsafe fn latest_gc_info_unchecked(self) -> AnyObject {
        Symbol::from(self).latest_gc_info_unchecked()
    }
}

impl GcInfoKey for String {
    #[inline]
    fn stat_gc(self) -> Result<usize> {
        Symbol::from(self).stat_gc()
    }

    #[inline]
    unsafe fn stat_gc_unchecked(self) -> usize {
        Symbol::from(self).stat_gc_unchecked()
    }

    #[inline]
    fn latest_gc_info(self) -> Result<AnyObject> {
        Symbol::from(self).latest_gc_info()
    }

    #[inline]
    unsafe fn latest_gc_info_unchecked(self) -> AnyObject {
        Symbol::from(self).latest_gc_info_unchecked()
    }
}