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
use rb_sys::{rb_gc_adjust_memory_usage, ssize_t};
use std::alloc::{GlobalAlloc, Layout, System};
pub struct RbAllocator;
unsafe impl GlobalAlloc for RbAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ret = System.alloc(layout);
if !ret.is_null() {
rb_gc_adjust_memory_usage(layout.size() as ssize_t)
}
ret
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ret = System.alloc_zeroed(layout);
if !ret.is_null() {
rb_gc_adjust_memory_usage(layout.size() as ssize_t)
}
ret
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout);
rb_gc_adjust_memory_usage(0 - (layout.size() as ssize_t));
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let ret = System.realloc(ptr, layout, new_size);
if !ret.is_null() {
rb_gc_adjust_memory_usage((new_size as isize - layout.size() as isize) as ssize_t);
}
ret
}
}
#[macro_export]
macro_rules! ruby_global_allocator {
() => {
use $crate::RbAllocator;
#[global_allocator]
static RUBY_GLOBAL_ALLOCATOR: RbAllocator = RbAllocator;
};
}