Line |
Branch |
Exec |
Source |
1 |
|
|
#ifndef __MRUBYARENA_HPP__ |
2 |
|
|
#define __MRUBYARENA_HPP__ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* This class represents a scope where, when objects are made natively |
6 |
|
|
* using functions such as `mrb_data_object_alloc` or `mrb_str_new` |
7 |
|
|
* they should be owned by the mruby gc. You use this class like this: |
8 |
|
|
* |
9 |
|
|
* Arena arena(mrb); |
10 |
|
|
* { |
11 |
|
|
* // do allocations here |
12 |
|
|
* } |
13 |
|
|
* |
14 |
|
|
* The braces do nothing important. They are just for readability. |
15 |
|
|
* |
16 |
|
|
* This construct is very important because not doing this causes |
17 |
|
|
* what would start looking like memory leaks since all allocations |
18 |
|
|
* made in native code are tracked on a stack called the gc arena, |
19 |
|
|
* and there is no way to deref only specific objects from it. |
20 |
|
|
* |
21 |
|
|
* Read more about this in the mruby docs folder |
22 |
|
|
*/ |
23 |
|
|
class Arena |
24 |
|
|
{ |
25 |
|
|
mrb_state* mrb; |
26 |
|
|
int arena_idx; |
27 |
|
|
|
28 |
|
|
public: |
29 |
|
37 |
Arena(mrb_state* mrb) : mrb(mrb) |
30 |
|
|
{ |
31 |
|
37 |
arena_idx = mrb_gc_arena_save(mrb); |
32 |
|
37 |
} |
33 |
|
|
|
34 |
|
37 |
~Arena() |
35 |
|
|
{ |
36 |
|
37 |
mrb_gc_arena_restore(mrb, arena_idx); |
37 |
|
37 |
} |
38 |
|
|
|
39 |
|
|
void protect(mrb_value obj) |
40 |
|
|
{ |
41 |
|
|
mrb_gc_protect(mrb, obj); |
42 |
|
|
} |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
#endif // __MRUBYARENA_HPP__ |
46 |
|
|
|