-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmrubyarena.hpp
executable file
·45 lines (40 loc) · 1 KB
/
mrubyarena.hpp
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
#ifndef __MRUBYARENA_HPP__
#define __MRUBYARENA_HPP__
/*
* This class represents a scope where, when objects are made natively
* using functions such as `mrb_data_object_alloc` or `mrb_str_new`
* they should be owned by the mruby gc. You use this class like this:
*
* Arena arena(mrb);
* {
* // do allocations here
* }
*
* The braces do nothing important. They are just for readability.
*
* This construct is very important because not doing this causes
* what would start looking like memory leaks since all allocations
* made in native code are tracked on a stack called the gc arena,
* and there is no way to deref only specific objects from it.
*
* Read more about this in the mruby docs folder
*/
class Arena
{
mrb_state* mrb;
int arena_idx;
public:
Arena(mrb_state* mrb) : mrb(mrb)
{
arena_idx = mrb_gc_arena_save(mrb);
}
~Arena()
{
mrb_gc_arena_restore(mrb, arena_idx);
}
void protect(mrb_value obj)
{
mrb_gc_protect(mrb, obj);
}
};
#endif // __MRUBYARENA_HPP__