| 1 |
/* |
| 2 |
** gc.c - garbage collector for mruby |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
#ifdef MRB_USE_MALLOC_TRIM |
| 9 |
#include <malloc.h> |
| 10 |
#endif |
| 11 |
#include <mruby.h> |
| 12 |
#include <mruby/array.h> |
| 13 |
#include <mruby/class.h> |
| 14 |
#include <mruby/data.h> |
| 15 |
#include <mruby/istruct.h> |
| 16 |
#include <mruby/hash.h> |
| 17 |
#include <mruby/proc.h> |
| 18 |
#include <mruby/range.h> |
| 19 |
#include <mruby/string.h> |
| 20 |
#include <mruby/variable.h> |
| 21 |
#include <mruby/gc.h> |
| 22 |
#include <mruby/error.h> |
| 23 |
#include <mruby/throw.h> |
| 24 |
#include <mruby/internal.h> |
| 25 |
#include <mruby/presym.h> |
| 26 |
|
| 27 |
#ifdef MRB_GC_STRESS |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
/* |
| 32 |
= Tri-color Incremental Garbage Collection |
| 33 |
|
| 34 |
mruby's GC is Tri-color Incremental GC with Mark & Sweep. |
| 35 |
Algorithm details are omitted. |
| 36 |
Instead, the implementation part is described below. |
| 37 |
|
| 38 |
== Object's Color |
| 39 |
|
| 40 |
Each object can be painted in three colors: |
| 41 |
|
| 42 |
* White - Unmarked. |
| 43 |
* Gray - Marked, But the child objects are unmarked. |
| 44 |
* Black - Marked, the child objects are also marked. |
| 45 |
|
| 46 |
Extra color |
| 47 |
|
| 48 |
* Red - Static (ROM object) no need to be collected. |
| 49 |
- All child objects should be Red as well. |
| 50 |
|
| 51 |
== Two White Types |
| 52 |
|
| 53 |
There are two white color types in a flip-flop fashion: White-A and White-B, |
| 54 |
which respectively represent the Current White color (the newly allocated |
| 55 |
objects in the current GC cycle) and the Sweep Target White color (the |
| 56 |
dead objects to be swept). |
| 57 |
|
| 58 |
A and B will be switched just at the beginning of the next GC cycle. At |
| 59 |
that time, all the dead objects have been swept, while the newly created |
| 60 |
objects in the current GC cycle which finally remains White are now |
| 61 |
regarded as dead objects. Instead of traversing all the White-A objects and |
| 62 |
painting them as White-B, just switch the meaning of White-A and White-B as |
| 63 |
this will be much cheaper. |
| 64 |
|
| 65 |
As a result, the objects we sweep in the current GC cycle are always |
| 66 |
left from the previous GC cycle. This allows us to sweep objects |
| 67 |
incrementally, without the disturbance of the newly created objects. |
| 68 |
|
| 69 |
== Execution Timing |
| 70 |
|
| 71 |
GC Execution Time and Each step interval are decided by live objects count. |
| 72 |
List of Adjustment API: |
| 73 |
|
| 74 |
* gc_interval_ratio_set |
| 75 |
* gc_step_ratio_set |
| 76 |
|
| 77 |
For details, see the comments for each function. |
| 78 |
|
| 79 |
== Write Barrier |
| 80 |
|
| 81 |
mruby implementer and C extension library writer must insert a write |
| 82 |
barrier when updating a reference from a field of an object. |
| 83 |
When updating a reference from a field of object A to object B, |
| 84 |
two different types of write barrier are available: |
| 85 |
|
| 86 |
* mrb_field_write_barrier - target B object for a mark. |
| 87 |
* mrb_write_barrier - target A object for a mark. |
| 88 |
|
| 89 |
== Generational Mode |
| 90 |
|
| 91 |
mruby's GC offers an Generational Mode while re-using the tri-color GC |
| 92 |
infrastructure. It will treat the Black objects as Old objects after each |
| 93 |
sweep phase, instead of painting them White. The key ideas are still the same |
| 94 |
as traditional generational GC: |
| 95 |
|
| 96 |
* Minor GC - just traverse the Young objects (Gray objects) in the mark |
| 97 |
phase, then only sweep the newly created objects, and leave |
| 98 |
the Old objects live. |
| 99 |
|
| 100 |
* Major GC - same as a full regular GC cycle. |
| 101 |
|
| 102 |
The difference from "traditional" generational GC is, that the major GC |
| 103 |
in mruby is triggered incrementally in a tri-color manner. |
| 104 |
|
| 105 |
|
| 106 |
For details, see the comments for each function. |
| 107 |
|
| 108 |
*/ |
| 109 |
|
| 110 |
struct free_obj { |
| 111 |
MRB_OBJECT_HEADER; |
| 112 |
struct RBasic *next; |
| 113 |
}; |
| 114 |
|
| 115 |
struct RVALUE_initializer { |
| 116 |
MRB_OBJECT_HEADER; |
| 117 |
char padding[sizeof(void*) * 4 - sizeof(uint32_t)]; |
| 118 |
}; |
| 119 |
|
| 120 |
typedef struct { |
| 121 |
union { |
| 122 |
struct RVALUE_initializer init; /* must be first member to ensure initialization */ |
| 123 |
struct free_obj free; |
| 124 |
struct RBasic basic; |
| 125 |
struct RObject object; |
| 126 |
struct RClass klass; |
| 127 |
struct RString string; |
| 128 |
struct RArray array; |
| 129 |
struct RHash hash; |
| 130 |
struct RRange range; |
| 131 |
struct RData data; |
| 132 |
struct RIStruct istruct; |
| 133 |
struct RProc proc; |
| 134 |
struct REnv env; |
| 135 |
struct RFiber fiber; |
| 136 |
struct RException exc; |
| 137 |
struct RBreak brk; |
| 138 |
} as; |
| 139 |
} RVALUE; |
| 140 |
|
| 141 |
#ifdef GC_DEBUG |
| 142 |
#define DEBUG(x) (x) |
| 143 |
#else |
| 144 |
#define DEBUG(x) |
| 145 |
#endif |
| 146 |
|
| 147 |
#ifndef MRB_HEAP_PAGE_SIZE |
| 148 |
#define MRB_HEAP_PAGE_SIZE 1024 |
| 149 |
#endif |
| 150 |
|
| 151 |
typedef struct mrb_heap_page { |
| 152 |
struct RBasic *freelist; |
| 153 |
struct mrb_heap_page *next; |
| 154 |
struct mrb_heap_page *free_next; |
| 155 |
mrb_bool old:1; |
| 156 |
/* Flexible array members are not C++ compatible */ |
| 157 |
/* void* objects[]; */ |
| 158 |
} mrb_heap_page; |
| 159 |
|
| 160 |
#define GC_STEP_SIZE 1024 |
| 161 |
|
| 162 |
/* white: 001 or 010, black: 100, gray: 000, red:111 */ |
| 163 |
#define GC_GRAY 0 |
| 164 |
#define GC_WHITE_A 1 |
| 165 |
#define GC_WHITE_B 2 |
| 166 |
#define GC_BLACK 4 |
| 167 |
#define GC_RED MRB_GC_RED |
| 168 |
#define GC_WHITES (GC_WHITE_A | GC_WHITE_B) |
| 169 |
#define GC_COLOR_MASK 7 |
| 170 |
mrb_static_assert(MRB_GC_RED <= GC_COLOR_MASK); |
| 171 |
|
| 172 |
#define paint_gray(o) ((o)->color = GC_GRAY) |
| 173 |
#define paint_black(o) ((o)->color = GC_BLACK) |
| 174 |
#define paint_white(o) ((o)->color = GC_WHITES) |
| 175 |
#define paint_partial_white(s, o) ((o)->color = (s)->current_white_part) |
| 176 |
#define is_gray(o) ((o)->color == GC_GRAY) |
| 177 |
#define is_white(o) ((o)->color & GC_WHITES) |
| 178 |
#define is_black(o) ((o)->color == GC_BLACK) |
| 179 |
#define is_red(o) ((o)->color == GC_RED) |
| 180 |
#define flip_white_part(s) ((s)->current_white_part = other_white_part(s)) |
| 181 |
#define other_white_part(s) ((s)->current_white_part ^ GC_WHITES) |
| 182 |
#define is_dead(s, o) (((o)->color & other_white_part(s) & GC_WHITES) || (o)->tt == MRB_TT_FREE) |
| 183 |
|
| 184 |
/* We have removed `objects[]` from `mrb_heap_page` since it was not C++ |
| 185 |
* compatible. Using array index to get pointer after structure instead. */ |
| 186 |
|
| 187 |
/* #define objects(p) ((RVALUE*)p->objects) */ |
| 188 |
#define objects(p) ((RVALUE*)&p[1]) |
| 189 |
|
| 190 |
mrb_noreturn void mrb_raise_nomemory(mrb_state *mrb); |
| 191 |
|
| 192 |
MRB_API void* |
| 193 |
mrb_realloc_simple(mrb_state *mrb, void *p, size_t len) |
| 194 |
{ |
| 195 |
void *p2; |
| 196 |
|
| 197 |
#if defined(MRB_GC_STRESS) && defined(MRB_DEBUG) |
| 198 |
mrb_full_gc(mrb); |
| 199 |
#endif |
| 200 |
p2 = (mrb->allocf)(mrb, p, len, mrb->allocf_ud); |
| 201 |
if (!p2 && len > 0 && mrb->gc.heaps) { |
| 202 |
mrb_full_gc(mrb); |
| 203 |
p2 = (mrb->allocf)(mrb, p, len, mrb->allocf_ud); |
| 204 |
} |
| 205 |
|
| 206 |
return p2; |
| 207 |
} |
| 208 |
|
| 209 |
MRB_API void* |
| 210 |
mrb_realloc(mrb_state *mrb, void *p, size_t len) |
| 211 |
{ |
| 212 |
void *p2; |
| 213 |
|
| 214 |
p2 = mrb_realloc_simple(mrb, p, len); |
| 215 |
if (len == 0) return p2; |
| 216 |
if (p2 == NULL) { |
| 217 |
mrb->gc.out_of_memory = TRUE; |
| 218 |
mrb_raise_nomemory(mrb); |
| 219 |
} |
| 220 |
else { |
| 221 |
mrb->gc.out_of_memory = FALSE; |
| 222 |
} |
| 223 |
|
| 224 |
return p2; |
| 225 |
} |
| 226 |
|
| 227 |
MRB_API void* |
| 228 |
mrb_malloc(mrb_state *mrb, size_t len) |
| 229 |
{ |
| 230 |
return mrb_realloc(mrb, 0, len); |
| 231 |
} |
| 232 |
|
| 233 |
MRB_API void* |
| 234 |
mrb_malloc_simple(mrb_state *mrb, size_t len) |
| 235 |
{ |
| 236 |
return mrb_realloc_simple(mrb, 0, len); |
| 237 |
} |
| 238 |
|
| 239 |
MRB_API void* |
| 240 |
mrb_calloc(mrb_state *mrb, size_t nelem, size_t len) |
| 241 |
{ |
| 242 |
void *p; |
| 243 |
|
| 244 |
if (nelem > 0 && len > 0 && |
| 245 |
nelem <= SIZE_MAX / len) { |
| 246 |
size_t size; |
| 247 |
size = nelem * len; |
| 248 |
p = mrb_malloc(mrb, size); |
| 249 |
|
| 250 |
memset(p, 0, size); |
| 251 |
} |
| 252 |
else { |
| 253 |
p = NULL; |
| 254 |
} |
| 255 |
|
| 256 |
return p; |
| 257 |
} |
| 258 |
|
| 259 |
MRB_API void |
| 260 |
mrb_free(mrb_state *mrb, void *p) |
| 261 |
{ |
| 262 |
(mrb->allocf)(mrb, p, 0, mrb->allocf_ud); |
| 263 |
} |
| 264 |
|
| 265 |
MRB_API void* |
| 266 |
mrb_alloca(mrb_state *mrb, size_t size) |
| 267 |
{ |
| 268 |
struct RString *s; |
| 269 |
s = MRB_OBJ_ALLOC(mrb, MRB_TT_STRING, NULL); |
| 270 |
return s->as.heap.ptr = (char*)mrb_malloc(mrb, size); |
| 271 |
} |
| 272 |
|
| 273 |
static mrb_bool |
| 274 |
heap_p(mrb_gc *gc, struct RBasic *object) |
| 275 |
{ |
| 276 |
mrb_heap_page* page; |
| 277 |
|
| 278 |
page = gc->heaps; |
| 279 |
while (page) { |
| 280 |
RVALUE *p; |
| 281 |
|
| 282 |
p = objects(page); |
| 283 |
if (&p[0].as.basic <= object && object <= &p[MRB_HEAP_PAGE_SIZE - 1].as.basic) { |
| 284 |
return TRUE; |
| 285 |
} |
| 286 |
page = page->next; |
| 287 |
} |
| 288 |
return FALSE; |
| 289 |
} |
| 290 |
|
| 291 |
MRB_API mrb_bool |
| 292 |
mrb_object_dead_p(mrb_state *mrb, struct RBasic *object) |
| 293 |
{ |
| 294 |
mrb_gc *gc = &mrb->gc; |
| 295 |
if (!heap_p(gc, object)) return TRUE; |
| 296 |
return is_dead(gc, object); |
| 297 |
} |
| 298 |
|
| 299 |
static void |
| 300 |
add_heap(mrb_state *mrb, mrb_gc *gc) |
| 301 |
{ |
| 302 |
mrb_heap_page *page = (mrb_heap_page*)mrb_calloc(mrb, 1, sizeof(mrb_heap_page) + MRB_HEAP_PAGE_SIZE * sizeof(RVALUE)); |
| 303 |
RVALUE *p, *e; |
| 304 |
struct RBasic *prev = NULL; |
| 305 |
|
| 306 |
for (p = objects(page), e=p+MRB_HEAP_PAGE_SIZE; p<e; p++) { |
| 307 |
p->as.free.tt = MRB_TT_FREE; |
| 308 |
p->as.free.next = prev; |
| 309 |
prev = &p->as.basic; |
| 310 |
} |
| 311 |
page->freelist = prev; |
| 312 |
|
| 313 |
page->next = gc->heaps; |
| 314 |
gc->heaps = page; |
| 315 |
|
| 316 |
page->free_next = gc->free_heaps; |
| 317 |
gc->free_heaps = page; |
| 318 |
} |
| 319 |
|
| 320 |
#define DEFAULT_GC_INTERVAL_RATIO 200 |
| 321 |
#define DEFAULT_GC_STEP_RATIO 200 |
| 322 |
#define MAJOR_GC_INC_RATIO 120 |
| 323 |
#define MAJOR_GC_TOOMANY 10000 |
| 324 |
#define is_generational(gc) ((gc)->generational) |
| 325 |
#define is_major_gc(gc) (is_generational(gc) && (gc)->full) |
| 326 |
#define is_minor_gc(gc) (is_generational(gc) && !(gc)->full) |
| 327 |
|
| 328 |
void |
| 329 |
mrb_gc_init(mrb_state *mrb, mrb_gc *gc) |
| 330 |
{ |
| 331 |
#ifndef MRB_GC_FIXED_ARENA |
| 332 |
gc->arena = (struct RBasic**)mrb_malloc(mrb, sizeof(struct RBasic*)*MRB_GC_ARENA_SIZE); |
| 333 |
gc->arena_capa = MRB_GC_ARENA_SIZE; |
| 334 |
#endif |
| 335 |
|
| 336 |
gc->current_white_part = GC_WHITE_A; |
| 337 |
gc->heaps = NULL; |
| 338 |
gc->free_heaps = NULL; |
| 339 |
add_heap(mrb, gc); |
| 340 |
gc->interval_ratio = DEFAULT_GC_INTERVAL_RATIO; |
| 341 |
gc->step_ratio = DEFAULT_GC_STEP_RATIO; |
| 342 |
#ifndef MRB_GC_TURN_OFF_GENERATIONAL |
| 343 |
gc->generational = TRUE; |
| 344 |
gc->full = TRUE; |
| 345 |
#endif |
| 346 |
} |
| 347 |
|
| 348 |
static void obj_free(mrb_state *mrb, struct RBasic *obj, int end); |
| 349 |
|
| 350 |
static void |
| 351 |
free_heap(mrb_state *mrb, mrb_gc *gc) |
| 352 |
{ |
| 353 |
mrb_heap_page *page = gc->heaps; |
| 354 |
mrb_heap_page *tmp; |
| 355 |
RVALUE *p, *e; |
| 356 |
|
| 357 |
while (page) { |
| 358 |
tmp = page; |
| 359 |
page = page->next; |
| 360 |
for (p = objects(tmp), e=p+MRB_HEAP_PAGE_SIZE; p<e; p++) { |
| 361 |
if (p->as.free.tt != MRB_TT_FREE) |
| 362 |
obj_free(mrb, &p->as.basic, TRUE); |
| 363 |
} |
| 364 |
mrb_free(mrb, tmp); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
void |
| 369 |
mrb_gc_destroy(mrb_state *mrb, mrb_gc *gc) |
| 370 |
{ |
| 371 |
free_heap(mrb, gc); |
| 372 |
#ifndef MRB_GC_FIXED_ARENA |
| 373 |
mrb_free(mrb, gc->arena); |
| 374 |
#endif |
| 375 |
} |
| 376 |
|
| 377 |
static void |
| 378 |
gc_protect(mrb_state *mrb, mrb_gc *gc, struct RBasic *p) |
| 379 |
{ |
| 380 |
#ifdef MRB_GC_FIXED_ARENA |
| 381 |
if (gc->arena_idx >= MRB_GC_ARENA_SIZE) { |
| 382 |
/* arena overflow error */ |
| 383 |
gc->arena_idx = MRB_GC_ARENA_SIZE - 4; /* force room in arena */ |
| 384 |
mrb_exc_raise(mrb, mrb_obj_value(mrb->arena_err)); |
| 385 |
} |
| 386 |
#else |
| 387 |
if (gc->arena_idx >= gc->arena_capa) { |
| 388 |
/* extend arena */ |
| 389 |
int newcapa = gc->arena_capa * 3 / 2; |
| 390 |
gc->arena = (struct RBasic**)mrb_realloc(mrb, gc->arena, sizeof(struct RBasic*)*newcapa); |
| 391 |
gc->arena_capa = newcapa; |
| 392 |
} |
| 393 |
#endif |
| 394 |
gc->arena[gc->arena_idx++] = p; |
| 395 |
} |
| 396 |
|
| 397 |
/* mrb_gc_protect() leaves the object in the arena */ |
| 398 |
MRB_API void |
| 399 |
mrb_gc_protect(mrb_state *mrb, mrb_value obj) |
| 400 |
{ |
| 401 |
if (mrb_immediate_p(obj)) return; |
| 402 |
struct RBasic *p = mrb_basic_ptr(obj); |
| 403 |
if (is_red(p)) return; |
| 404 |
gc_protect(mrb, &mrb->gc, p); |
| 405 |
} |
| 406 |
|
| 407 |
#define GC_ROOT_SYM MRB_SYM(_gc_root_) |
| 408 |
|
| 409 |
/* mrb_gc_register() keeps the object from GC. |
| 410 |
|
| 411 |
Register your object when it's exported to C world, |
| 412 |
without reference from Ruby world, e.g. callback |
| 413 |
arguments. Don't forget to remove the object using |
| 414 |
mrb_gc_unregister, otherwise your object will leak. |
| 415 |
*/ |
| 416 |
|
| 417 |
MRB_API void |
| 418 |
mrb_gc_register(mrb_state *mrb, mrb_value obj) |
| 419 |
{ |
| 420 |
mrb_value table; |
| 421 |
|
| 422 |
if (mrb_immediate_p(obj)) return; |
| 423 |
table = mrb_gv_get(mrb, GC_ROOT_SYM); |
| 424 |
if (mrb_nil_p(table) || !mrb_array_p(table)) { |
| 425 |
table = mrb_ary_new(mrb); |
| 426 |
mrb_gv_set(mrb, GC_ROOT_SYM, table); |
| 427 |
} |
| 428 |
mrb_ary_push(mrb, table, obj); |
| 429 |
} |
| 430 |
|
| 431 |
/* mrb_gc_unregister() removes the object from GC root. */ |
| 432 |
MRB_API void |
| 433 |
mrb_gc_unregister(mrb_state *mrb, mrb_value obj) |
| 434 |
{ |
| 435 |
mrb_value table; |
| 436 |
struct RArray *a; |
| 437 |
|
| 438 |
if (mrb_immediate_p(obj)) return; |
| 439 |
table = mrb_gv_get(mrb, GC_ROOT_SYM); |
| 440 |
if (mrb_nil_p(table)) return; |
| 441 |
if (!mrb_array_p(table)) { |
| 442 |
mrb_gv_set(mrb, GC_ROOT_SYM, mrb_nil_value()); |
| 443 |
return; |
| 444 |
} |
| 445 |
a = mrb_ary_ptr(table); |
| 446 |
mrb_ary_modify(mrb, a); |
| 447 |
for (mrb_int i = 0; i < ARY_LEN(a); i++) { |
| 448 |
if (mrb_ptr(ARY_PTR(a)[i]) == mrb_ptr(obj)) { |
| 449 |
mrb_int len = ARY_LEN(a)-1; |
| 450 |
mrb_value *ptr = ARY_PTR(a); |
| 451 |
|
| 452 |
ARY_SET_LEN(a, len); |
| 453 |
memmove(&ptr[i], &ptr[i + 1], (len - i) * sizeof(mrb_value)); |
| 454 |
break; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
MRB_API struct RBasic* |
| 460 |
mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls) |
| 461 |
{ |
| 462 |
static const RVALUE RVALUE_zero = { { { NULL, NULL, MRB_TT_FALSE } } }; |
| 463 |
mrb_gc *gc = &mrb->gc; |
| 464 |
|
| 465 |
if (cls) { |
| 466 |
enum mrb_vtype tt; |
| 467 |
|
| 468 |
switch (cls->tt) { |
| 469 |
case MRB_TT_CLASS: |
| 470 |
case MRB_TT_SCLASS: |
| 471 |
case MRB_TT_MODULE: |
| 472 |
case MRB_TT_ENV: |
| 473 |
break; |
| 474 |
default: |
| 475 |
mrb_raise(mrb, E_TYPE_ERROR, "allocation failure"); |
| 476 |
} |
| 477 |
tt = MRB_INSTANCE_TT(cls); |
| 478 |
if (tt != MRB_TT_FALSE && |
| 479 |
tt != MRB_TT_UNDEF && |
| 480 |
ttype != MRB_TT_SCLASS && |
| 481 |
ttype != MRB_TT_ICLASS && |
| 482 |
ttype != MRB_TT_ENV && |
| 483 |
ttype != MRB_TT_BIGINT && |
| 484 |
ttype != tt) { |
| 485 |
mrb_raisef(mrb, E_TYPE_ERROR, "allocation failure of %C", cls); |
| 486 |
} |
| 487 |
} |
| 488 |
if (ttype <= MRB_TT_FREE) { |
| 489 |
mrb_raisef(mrb, E_TYPE_ERROR, "allocation failure of %C (type %d)", cls, (int)ttype); |
| 490 |
} |
| 491 |
|
| 492 |
#ifdef MRB_GC_STRESS |
| 493 |
mrb_full_gc(mrb); |
| 494 |
#endif |
| 495 |
if (gc->threshold < gc->live) { |
| 496 |
mrb_incremental_gc(mrb); |
| 497 |
} |
| 498 |
if (gc->free_heaps == NULL) { |
| 499 |
add_heap(mrb, gc); |
| 500 |
} |
| 501 |
|
| 502 |
struct RBasic *p = gc->free_heaps->freelist; |
| 503 |
gc->free_heaps->freelist = ((struct free_obj*)p)->next; |
| 504 |
if (gc->free_heaps->freelist == NULL) { |
| 505 |
gc->free_heaps = gc->free_heaps->free_next; |
| 506 |
} |
| 507 |
|
| 508 |
gc->live++; |
| 509 |
gc_protect(mrb, gc, p); |
| 510 |
*(RVALUE*)p = RVALUE_zero; |
| 511 |
p->tt = ttype; |
| 512 |
p->c = cls; |
| 513 |
paint_partial_white(gc, p); |
| 514 |
return p; |
| 515 |
} |
| 516 |
|
| 517 |
static inline void |
| 518 |
add_gray_list(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj) |
| 519 |
{ |
| 520 |
#ifdef MRB_GC_STRESS |
| 521 |
if (obj->tt > MRB_TT_MAXDEFINE) { |
| 522 |
abort(); |
| 523 |
} |
| 524 |
#endif |
| 525 |
paint_gray(obj); |
| 526 |
obj->gcnext = gc->gray_list; |
| 527 |
gc->gray_list = obj; |
| 528 |
} |
| 529 |
|
| 530 |
static void |
| 531 |
mark_context_stack(mrb_state *mrb, struct mrb_context *c) |
| 532 |
{ |
| 533 |
size_t i, e; |
| 534 |
mrb_value nil; |
| 535 |
|
| 536 |
if (c->stbase == NULL) return; |
| 537 |
if (c->ci) { |
| 538 |
e = (c->ci->stack ? c->ci->stack - c->stbase : 0); |
| 539 |
e += mrb_ci_nregs(c->ci); |
| 540 |
} |
| 541 |
else { |
| 542 |
e = 0; |
| 543 |
} |
| 544 |
if (c->stbase + e > c->stend) e = c->stend - c->stbase; |
| 545 |
for (i=0; i<e; i++) { |
| 546 |
mrb_value v = c->stbase[i]; |
| 547 |
|
| 548 |
if (!mrb_immediate_p(v)) { |
| 549 |
mrb_gc_mark(mrb, mrb_basic_ptr(v)); |
| 550 |
} |
| 551 |
} |
| 552 |
e = c->stend - c->stbase; |
| 553 |
nil = mrb_nil_value(); |
| 554 |
for (; i<e; i++) { |
| 555 |
c->stbase[i] = nil; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static void |
| 560 |
mark_context(mrb_state *mrb, struct mrb_context *c) |
| 561 |
{ |
| 562 |
mrb_callinfo *ci; |
| 563 |
|
| 564 |
start: |
| 565 |
if (c->status == MRB_FIBER_TERMINATED) return; |
| 566 |
|
| 567 |
/* mark VM stack */ |
| 568 |
mark_context_stack(mrb, c); |
| 569 |
|
| 570 |
/* mark call stack */ |
| 571 |
if (c->cibase) { |
| 572 |
for (ci = c->cibase; ci <= c->ci; ci++) { |
| 573 |
mrb_gc_mark(mrb, (struct RBasic*)ci->proc); |
| 574 |
mrb_gc_mark(mrb, (struct RBasic*)ci->u.target_class); |
| 575 |
} |
| 576 |
} |
| 577 |
/* mark fibers */ |
| 578 |
mrb_gc_mark(mrb, (struct RBasic*)c->fib); |
| 579 |
if (c->prev) { |
| 580 |
c = c->prev; |
| 581 |
goto start; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
static void |
| 586 |
gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj) |
| 587 |
{ |
| 588 |
mrb_assert(is_gray(obj)); |
| 589 |
paint_black(obj); |
| 590 |
mrb_gc_mark(mrb, (struct RBasic*)obj->c); |
| 591 |
switch (obj->tt) { |
| 592 |
case MRB_TT_ICLASS: |
| 593 |
{ |
| 594 |
struct RClass *c = (struct RClass*)obj; |
| 595 |
if (MRB_FLAG_TEST(c, MRB_FL_CLASS_IS_ORIGIN)) |
| 596 |
mrb_gc_mark_mt(mrb, c); |
| 597 |
mrb_gc_mark(mrb, (struct RBasic*)((struct RClass*)obj)->super); |
| 598 |
} |
| 599 |
break; |
| 600 |
|
| 601 |
case MRB_TT_CLASS: |
| 602 |
case MRB_TT_MODULE: |
| 603 |
case MRB_TT_SCLASS: |
| 604 |
{ |
| 605 |
struct RClass *c = (struct RClass*)obj; |
| 606 |
|
| 607 |
mrb_gc_mark_mt(mrb, c); |
| 608 |
mrb_gc_mark(mrb, (struct RBasic*)c->super); |
| 609 |
} |
| 610 |
/* fall through */ |
| 611 |
|
| 612 |
case MRB_TT_OBJECT: |
| 613 |
case MRB_TT_CDATA: |
| 614 |
mrb_gc_mark_iv(mrb, (struct RObject*)obj); |
| 615 |
break; |
| 616 |
|
| 617 |
case MRB_TT_PROC: |
| 618 |
{ |
| 619 |
struct RProc *p = (struct RProc*)obj; |
| 620 |
|
| 621 |
mrb_gc_mark(mrb, (struct RBasic*)p->upper); |
| 622 |
mrb_gc_mark(mrb, (struct RBasic*)p->e.env); |
| 623 |
} |
| 624 |
break; |
| 625 |
|
| 626 |
case MRB_TT_ENV: |
| 627 |
{ |
| 628 |
struct REnv *e = (struct REnv*)obj; |
| 629 |
|
| 630 |
if (MRB_ENV_ONSTACK_P(e) && e->cxt && e->cxt->fib) { |
| 631 |
mrb_gc_mark(mrb, (struct RBasic*)e->cxt->fib); |
| 632 |
} |
| 633 |
mrb_int len = MRB_ENV_LEN(e); |
| 634 |
for (mrb_int i=0; i<len; i++) { |
| 635 |
mrb_gc_mark_value(mrb, e->stack[i]); |
| 636 |
} |
| 637 |
} |
| 638 |
break; |
| 639 |
|
| 640 |
case MRB_TT_FIBER: |
| 641 |
{ |
| 642 |
struct mrb_context *c = ((struct RFiber*)obj)->cxt; |
| 643 |
|
| 644 |
if (c) mark_context(mrb, c); |
| 645 |
} |
| 646 |
break; |
| 647 |
|
| 648 |
case MRB_TT_STRUCT: |
| 649 |
case MRB_TT_ARRAY: |
| 650 |
{ |
| 651 |
struct RArray *a = (struct RArray*)obj; |
| 652 |
size_t e=ARY_LEN(a); |
| 653 |
mrb_value *p = ARY_PTR(a); |
| 654 |
|
| 655 |
for (size_t i=0; i<e; i++) { |
| 656 |
mrb_gc_mark_value(mrb, p[i]); |
| 657 |
} |
| 658 |
} |
| 659 |
break; |
| 660 |
|
| 661 |
case MRB_TT_HASH: |
| 662 |
mrb_gc_mark_iv(mrb, (struct RObject*)obj); |
| 663 |
mrb_gc_mark_hash(mrb, (struct RHash*)obj); |
| 664 |
break; |
| 665 |
|
| 666 |
case MRB_TT_STRING: |
| 667 |
if (RSTR_FSHARED_P(obj)) { |
| 668 |
struct RString *s = (struct RString*)obj; |
| 669 |
mrb_gc_mark(mrb, (struct RBasic*)s->as.heap.aux.fshared); |
| 670 |
} |
| 671 |
break; |
| 672 |
|
| 673 |
case MRB_TT_RANGE: |
| 674 |
mrb_gc_mark_range(mrb, (struct RRange*)obj); |
| 675 |
break; |
| 676 |
|
| 677 |
case MRB_TT_BREAK: |
| 678 |
{ |
| 679 |
struct RBreak *brk = (struct RBreak*)obj; |
| 680 |
mrb_gc_mark(mrb, (struct RBasic*)mrb_break_proc_get(brk)); |
| 681 |
mrb_gc_mark_value(mrb, mrb_break_value_get(brk)); |
| 682 |
} |
| 683 |
break; |
| 684 |
|
| 685 |
case MRB_TT_EXCEPTION: |
| 686 |
mrb_gc_mark_iv(mrb, (struct RObject*)obj); |
| 687 |
if (((struct RException*)obj)->mesg) { |
| 688 |
mrb_gc_mark(mrb, (struct RBasic*)((struct RException*)obj)->mesg); |
| 689 |
} |
| 690 |
mrb_gc_mark(mrb, (struct RBasic*)((struct RException*)obj)->backtrace); |
| 691 |
break; |
| 692 |
|
| 693 |
default: |
| 694 |
break; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
MRB_API void |
| 699 |
mrb_gc_mark(mrb_state *mrb, struct RBasic *obj) |
| 700 |
{ |
| 701 |
if (obj == 0) return; |
| 702 |
if (!is_white(obj)) return; |
| 703 |
if (is_red(obj)) return; |
| 704 |
mrb_assert((obj)->tt != MRB_TT_FREE); |
| 705 |
add_gray_list(mrb, &mrb->gc, obj); |
| 706 |
} |
| 707 |
|
| 708 |
static void |
| 709 |
obj_free(mrb_state *mrb, struct RBasic *obj, int end) |
| 710 |
{ |
| 711 |
DEBUG(fprintf(stderr, "obj_free(%p,tt=%d)\n",obj,obj->tt)); |
| 712 |
switch (obj->tt) { |
| 713 |
case MRB_TT_OBJECT: |
| 714 |
mrb_gc_free_iv(mrb, (struct RObject*)obj); |
| 715 |
break; |
| 716 |
|
| 717 |
case MRB_TT_EXCEPTION: |
| 718 |
mrb_gc_free_iv(mrb, (struct RObject*)obj); |
| 719 |
break; |
| 720 |
|
| 721 |
case MRB_TT_CLASS: |
| 722 |
case MRB_TT_MODULE: |
| 723 |
case MRB_TT_SCLASS: |
| 724 |
mrb_gc_free_mt(mrb, (struct RClass*)obj); |
| 725 |
mrb_gc_free_iv(mrb, (struct RObject*)obj); |
| 726 |
if (!end) |
| 727 |
mrb_mc_clear_by_class(mrb, (struct RClass*)obj); |
| 728 |
break; |
| 729 |
case MRB_TT_ICLASS: |
| 730 |
if (MRB_FLAG_TEST(obj, MRB_FL_CLASS_IS_ORIGIN)) |
| 731 |
mrb_gc_free_mt(mrb, (struct RClass*)obj); |
| 732 |
if (!end) |
| 733 |
mrb_mc_clear_by_class(mrb, (struct RClass*)obj); |
| 734 |
break; |
| 735 |
case MRB_TT_ENV: |
| 736 |
{ |
| 737 |
struct REnv *e = (struct REnv*)obj; |
| 738 |
|
| 739 |
if (MRB_ENV_ONSTACK_P(e)) { |
| 740 |
/* cannot be freed */ |
| 741 |
e->stack = NULL; |
| 742 |
break; |
| 743 |
} |
| 744 |
mrb_free(mrb, e->stack); |
| 745 |
e->stack = NULL; |
| 746 |
} |
| 747 |
break; |
| 748 |
|
| 749 |
case MRB_TT_FIBER: |
| 750 |
{ |
| 751 |
struct mrb_context *c = ((struct RFiber*)obj)->cxt; |
| 752 |
|
| 753 |
if (c != mrb->root_c) { |
| 754 |
mrb_free_context(mrb, c); |
| 755 |
} |
| 756 |
} |
| 757 |
break; |
| 758 |
|
| 759 |
case MRB_TT_STRUCT: |
| 760 |
case MRB_TT_ARRAY: |
| 761 |
if (ARY_SHARED_P(obj)) |
| 762 |
mrb_ary_decref(mrb, ((struct RArray*)obj)->as.heap.aux.shared); |
| 763 |
else if (!ARY_EMBED_P(obj)) |
| 764 |
mrb_free(mrb, ((struct RArray*)obj)->as.heap.ptr); |
| 765 |
break; |
| 766 |
|
| 767 |
case MRB_TT_HASH: |
| 768 |
mrb_gc_free_iv(mrb, (struct RObject*)obj); |
| 769 |
mrb_gc_free_hash(mrb, (struct RHash*)obj); |
| 770 |
break; |
| 771 |
|
| 772 |
case MRB_TT_STRING: |
| 773 |
mrb_gc_free_str(mrb, (struct RString*)obj); |
| 774 |
break; |
| 775 |
|
| 776 |
case MRB_TT_PROC: |
| 777 |
{ |
| 778 |
struct RProc *p = (struct RProc*)obj; |
| 779 |
|
| 780 |
if (!MRB_PROC_CFUNC_P(p) && p->body.irep) { |
| 781 |
mrb_irep *irep = (mrb_irep*)p->body.irep; |
| 782 |
if (end) { |
| 783 |
mrb_irep_cutref(mrb, irep); |
| 784 |
} |
| 785 |
mrb_irep_decref(mrb, irep); |
| 786 |
} |
| 787 |
} |
| 788 |
break; |
| 789 |
|
| 790 |
case MRB_TT_RANGE: |
| 791 |
mrb_gc_free_range(mrb, ((struct RRange*)obj)); |
| 792 |
break; |
| 793 |
|
| 794 |
case MRB_TT_CDATA: |
| 795 |
{ |
| 796 |
struct RData *d = (struct RData*)obj; |
| 797 |
if (d->type && d->type->dfree) { |
| 798 |
d->type->dfree(mrb, d->data); |
| 799 |
} |
| 800 |
mrb_gc_free_iv(mrb, (struct RObject*)obj); |
| 801 |
} |
| 802 |
break; |
| 803 |
|
| 804 |
#if defined(MRB_USE_RATIONAL) && defined(MRB_INT64) && defined(MRB_32BIT) |
| 805 |
case MRB_TT_RATIONAL: |
| 806 |
{ |
| 807 |
struct RData *o = (struct RData*)obj; |
| 808 |
mrb_free(mrb, o->iv); |
| 809 |
} |
| 810 |
break; |
| 811 |
#endif |
| 812 |
|
| 813 |
#if defined(MRB_USE_COMPLEX) && defined(MRB_32BIT) && !defined(MRB_USE_FLOAT32) |
| 814 |
case MRB_TT_COMPLEX: |
| 815 |
{ |
| 816 |
struct RData *o = (struct RData*)obj; |
| 817 |
mrb_free(mrb, o->iv); |
| 818 |
} |
| 819 |
break; |
| 820 |
#endif |
| 821 |
|
| 822 |
#ifdef MRB_USE_BIGINT |
| 823 |
case MRB_TT_BIGINT: |
| 824 |
mrb_gc_free_bint(mrb, obj); |
| 825 |
break; |
| 826 |
#endif |
| 827 |
|
| 828 |
default: |
| 829 |
break; |
| 830 |
} |
| 831 |
#if defined(MRB_GC_STRESS) && defined(MRB_DEBUG) |
| 832 |
memset(obj, -1, sizeof(RVALUE)); |
| 833 |
paint_white(obj); |
| 834 |
#endif |
| 835 |
obj->tt = MRB_TT_FREE; |
| 836 |
} |
| 837 |
|
| 838 |
static void |
| 839 |
root_scan_phase(mrb_state *mrb, mrb_gc *gc) |
| 840 |
{ |
| 841 |
int i, e; |
| 842 |
|
| 843 |
if (!is_minor_gc(gc)) { |
| 844 |
gc->gray_list = NULL; |
| 845 |
gc->atomic_gray_list = NULL; |
| 846 |
} |
| 847 |
|
| 848 |
mrb_gc_mark_gv(mrb); |
| 849 |
/* mark arena */ |
| 850 |
for (i=0,e=gc->arena_idx; i<e; i++) { |
| 851 |
mrb_gc_mark(mrb, gc->arena[i]); |
| 852 |
} |
| 853 |
/* mark class hierarchy */ |
| 854 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->object_class); |
| 855 |
|
| 856 |
/* mark built-in classes */ |
| 857 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->class_class); |
| 858 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->module_class); |
| 859 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->proc_class); |
| 860 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->string_class); |
| 861 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->array_class); |
| 862 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->hash_class); |
| 863 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->range_class); |
| 864 |
|
| 865 |
#ifndef MRB_NO_FLOAT |
| 866 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->float_class); |
| 867 |
#endif |
| 868 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->integer_class); |
| 869 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->true_class); |
| 870 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->false_class); |
| 871 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->nil_class); |
| 872 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->symbol_class); |
| 873 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->kernel_module); |
| 874 |
|
| 875 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->eException_class); |
| 876 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->eStandardError_class); |
| 877 |
|
| 878 |
/* mark top_self */ |
| 879 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->top_self); |
| 880 |
/* mark exception */ |
| 881 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->exc); |
| 882 |
|
| 883 |
mark_context(mrb, mrb->c); |
| 884 |
if (mrb->root_c != mrb->c) { |
| 885 |
mark_context(mrb, mrb->root_c); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
/* rough estimation of number of GC marks (non recursive) */ |
| 890 |
static size_t |
| 891 |
gc_gray_counts(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj) |
| 892 |
{ |
| 893 |
size_t children = 0; |
| 894 |
|
| 895 |
switch (obj->tt) { |
| 896 |
case MRB_TT_ICLASS: |
| 897 |
children++; |
| 898 |
break; |
| 899 |
|
| 900 |
case MRB_TT_CLASS: |
| 901 |
case MRB_TT_SCLASS: |
| 902 |
case MRB_TT_MODULE: |
| 903 |
{ |
| 904 |
struct RClass *c = (struct RClass*)obj; |
| 905 |
|
| 906 |
children += mrb_gc_mark_iv_size(mrb, (struct RObject*)obj); |
| 907 |
children += mrb_gc_mark_mt_size(mrb, c); |
| 908 |
children++; |
| 909 |
} |
| 910 |
break; |
| 911 |
|
| 912 |
case MRB_TT_OBJECT: |
| 913 |
case MRB_TT_CDATA: |
| 914 |
children += mrb_gc_mark_iv_size(mrb, (struct RObject*)obj); |
| 915 |
break; |
| 916 |
|
| 917 |
case MRB_TT_ENV: |
| 918 |
children += MRB_ENV_LEN(obj); |
| 919 |
break; |
| 920 |
|
| 921 |
case MRB_TT_FIBER: |
| 922 |
{ |
| 923 |
struct mrb_context *c = ((struct RFiber*)obj)->cxt; |
| 924 |
size_t i; |
| 925 |
mrb_callinfo *ci; |
| 926 |
|
| 927 |
if (!c || c->status == MRB_FIBER_TERMINATED) break; |
| 928 |
if (!c->ci) break; |
| 929 |
|
| 930 |
/* mark stack */ |
| 931 |
i = c->ci->stack - c->stbase; |
| 932 |
i += mrb_ci_nregs(c->ci); |
| 933 |
if (c->stbase + i > c->stend) i = c->stend - c->stbase; |
| 934 |
children += i; |
| 935 |
|
| 936 |
/* mark closure */ |
| 937 |
if (c->cibase) { |
| 938 |
for (i=0, ci = c->cibase; ci <= c->ci; i++, ci++) |
| 939 |
; |
| 940 |
} |
| 941 |
children += i; |
| 942 |
} |
| 943 |
break; |
| 944 |
|
| 945 |
case MRB_TT_STRUCT: |
| 946 |
case MRB_TT_ARRAY: |
| 947 |
{ |
| 948 |
struct RArray *a = (struct RArray*)obj; |
| 949 |
children += ARY_LEN(a); |
| 950 |
} |
| 951 |
break; |
| 952 |
|
| 953 |
case MRB_TT_HASH: |
| 954 |
children += mrb_gc_mark_iv_size(mrb, (struct RObject*)obj); |
| 955 |
children += mrb_gc_mark_hash_size(mrb, (struct RHash*)obj); |
| 956 |
break; |
| 957 |
|
| 958 |
case MRB_TT_PROC: |
| 959 |
case MRB_TT_RANGE: |
| 960 |
case MRB_TT_BREAK: |
| 961 |
children+=2; |
| 962 |
break; |
| 963 |
|
| 964 |
case MRB_TT_EXCEPTION: |
| 965 |
children += mrb_gc_mark_iv_size(mrb, (struct RObject*)obj); |
| 966 |
if (((struct RException*)obj)->mesg) { |
| 967 |
children++; |
| 968 |
} |
| 969 |
if (((struct RException*)obj)->backtrace) { |
| 970 |
children++; |
| 971 |
} |
| 972 |
break; |
| 973 |
|
| 974 |
default: |
| 975 |
break; |
| 976 |
} |
| 977 |
return children; |
| 978 |
} |
| 979 |
|
| 980 |
static void |
| 981 |
gc_mark_gray_list(mrb_state *mrb, mrb_gc *gc) { |
| 982 |
while (gc->gray_list) { |
| 983 |
struct RBasic *obj = gc->gray_list; |
| 984 |
gc->gray_list = obj->gcnext; |
| 985 |
obj->gcnext = NULL; |
| 986 |
gc_mark_children(mrb, gc, obj); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
static size_t |
| 991 |
incremental_marking_phase(mrb_state *mrb, mrb_gc *gc, size_t limit) |
| 992 |
{ |
| 993 |
size_t tried_marks = 0; |
| 994 |
|
| 995 |
while (gc->gray_list && tried_marks < limit) { |
| 996 |
struct RBasic *obj = gc->gray_list; |
| 997 |
gc->gray_list = obj->gcnext; |
| 998 |
obj->gcnext = NULL; |
| 999 |
gc_mark_children(mrb, gc, obj); |
| 1000 |
tried_marks += gc_gray_counts(mrb, gc, obj); |
| 1001 |
} |
| 1002 |
|
| 1003 |
return tried_marks; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static void |
| 1007 |
clear_error_object(mrb_state *mrb, struct RObject *obj) |
| 1008 |
{ |
| 1009 |
if (obj == 0) return; |
| 1010 |
if (!is_white(obj)) return; |
| 1011 |
paint_black(obj); |
| 1012 |
mrb_gc_mark(mrb, (struct RBasic*)obj->c); |
| 1013 |
mrb_gc_free_iv(mrb, obj); |
| 1014 |
struct RException *err = (struct RException*)obj; |
| 1015 |
err->iv = NULL; |
| 1016 |
err->mesg = NULL; |
| 1017 |
err->backtrace = NULL; |
| 1018 |
} |
| 1019 |
|
| 1020 |
static void |
| 1021 |
final_marking_phase(mrb_state *mrb, mrb_gc *gc) |
| 1022 |
{ |
| 1023 |
int i, e; |
| 1024 |
|
| 1025 |
/* mark arena */ |
| 1026 |
for (i=0,e=gc->arena_idx; i<e; i++) { |
| 1027 |
mrb_gc_mark(mrb, gc->arena[i]); |
| 1028 |
} |
| 1029 |
mrb_gc_mark_gv(mrb); |
| 1030 |
mark_context(mrb, mrb->c); |
| 1031 |
if (mrb->c != mrb->root_c) { |
| 1032 |
mark_context(mrb, mrb->root_c); |
| 1033 |
} |
| 1034 |
mrb_gc_mark(mrb, (struct RBasic*)mrb->exc); |
| 1035 |
|
| 1036 |
/* mark pre-allocated exception */ |
| 1037 |
clear_error_object(mrb, mrb->nomem_err); |
| 1038 |
clear_error_object(mrb, mrb->stack_err); |
| 1039 |
#ifdef MRB_GC_FIXED_ARENA |
| 1040 |
clear_error_object(mrb, mrb->arena_err); |
| 1041 |
#endif |
| 1042 |
|
| 1043 |
gc_mark_gray_list(mrb, gc); |
| 1044 |
mrb_assert(gc->gray_list == NULL); |
| 1045 |
gc->gray_list = gc->atomic_gray_list; |
| 1046 |
gc->atomic_gray_list = NULL; |
| 1047 |
gc_mark_gray_list(mrb, gc); |
| 1048 |
mrb_assert(gc->gray_list == NULL); |
| 1049 |
} |
| 1050 |
|
| 1051 |
static void |
| 1052 |
prepare_incremental_sweep(mrb_state *mrb, mrb_gc *gc) |
| 1053 |
{ |
| 1054 |
// mrb_assert(gc->atomic_gray_list == NULL); |
| 1055 |
// mrb_assert(gc->gray_list == NULL); |
| 1056 |
gc->state = MRB_GC_STATE_SWEEP; |
| 1057 |
gc->sweeps = NULL; |
| 1058 |
gc->live_after_mark = gc->live; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static size_t |
| 1062 |
incremental_sweep_phase(mrb_state *mrb, mrb_gc *gc, size_t limit) |
| 1063 |
{ |
| 1064 |
mrb_heap_page *prev = gc->sweeps; |
| 1065 |
mrb_heap_page *page = prev ? prev->next : gc->heaps; |
| 1066 |
size_t tried_sweep = 0; |
| 1067 |
|
| 1068 |
while (page && (tried_sweep < limit)) { |
| 1069 |
RVALUE *p = objects(page); |
| 1070 |
RVALUE *e = p + MRB_HEAP_PAGE_SIZE; |
| 1071 |
size_t freed = 0; |
| 1072 |
mrb_bool dead_slot = TRUE; |
| 1073 |
|
| 1074 |
if (is_minor_gc(gc) && page->old) { |
| 1075 |
/* skip a slot which doesn't contain any young object */ |
| 1076 |
p = e; |
| 1077 |
dead_slot = FALSE; |
| 1078 |
} |
| 1079 |
while (p<e) { |
| 1080 |
if (is_dead(gc, &p->as.basic)) { |
| 1081 |
if (p->as.basic.tt != MRB_TT_FREE) { |
| 1082 |
obj_free(mrb, &p->as.basic, FALSE); |
| 1083 |
if (p->as.basic.tt == MRB_TT_FREE) { |
| 1084 |
p->as.free.next = page->freelist; |
| 1085 |
page->freelist = (struct RBasic*)p; |
| 1086 |
freed++; |
| 1087 |
} |
| 1088 |
else { |
| 1089 |
dead_slot = FALSE; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
else { |
| 1094 |
if (!is_generational(gc)) |
| 1095 |
paint_partial_white(gc, &p->as.basic); /* next gc target */ |
| 1096 |
dead_slot = FALSE; |
| 1097 |
} |
| 1098 |
p++; |
| 1099 |
} |
| 1100 |
|
| 1101 |
/* free dead slot */ |
| 1102 |
if (dead_slot) { |
| 1103 |
mrb_heap_page *next = page->next; |
| 1104 |
|
| 1105 |
if (prev) prev->next = next; |
| 1106 |
if (gc->heaps == page) |
| 1107 |
gc->heaps = page->next; |
| 1108 |
|
| 1109 |
mrb_free(mrb, page); |
| 1110 |
page = next; |
| 1111 |
} |
| 1112 |
else { |
| 1113 |
if (page->freelist == NULL && is_minor_gc(gc)) |
| 1114 |
page->old = TRUE; |
| 1115 |
else |
| 1116 |
page->old = FALSE; |
| 1117 |
prev = page; |
| 1118 |
page = page->next; |
| 1119 |
} |
| 1120 |
tried_sweep += MRB_HEAP_PAGE_SIZE; |
| 1121 |
gc->live -= freed; |
| 1122 |
gc->live_after_mark -= freed; |
| 1123 |
} |
| 1124 |
gc->sweeps = prev; |
| 1125 |
|
| 1126 |
/* rebuild free_heaps link */ |
| 1127 |
gc->free_heaps = NULL; |
| 1128 |
for (mrb_heap_page *p = gc->heaps; p; p=p->next) { |
| 1129 |
if (p->freelist) { |
| 1130 |
p->free_next = gc->free_heaps; |
| 1131 |
gc->free_heaps = p; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
return tried_sweep; |
| 1136 |
} |
| 1137 |
|
| 1138 |
static size_t |
| 1139 |
incremental_gc(mrb_state *mrb, mrb_gc *gc, size_t limit) |
| 1140 |
{ |
| 1141 |
switch (gc->state) { |
| 1142 |
case MRB_GC_STATE_ROOT: |
| 1143 |
root_scan_phase(mrb, gc); |
| 1144 |
gc->state = MRB_GC_STATE_MARK; |
| 1145 |
flip_white_part(gc); |
| 1146 |
return 0; |
| 1147 |
case MRB_GC_STATE_MARK: |
| 1148 |
if (gc->gray_list) { |
| 1149 |
return incremental_marking_phase(mrb, gc, limit); |
| 1150 |
} |
| 1151 |
else { |
| 1152 |
final_marking_phase(mrb, gc); |
| 1153 |
prepare_incremental_sweep(mrb, gc); |
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
case MRB_GC_STATE_SWEEP: { |
| 1157 |
size_t tried_sweep = 0; |
| 1158 |
tried_sweep = incremental_sweep_phase(mrb, gc, limit); |
| 1159 |
if (tried_sweep == 0) |
| 1160 |
gc->state = MRB_GC_STATE_ROOT; |
| 1161 |
return tried_sweep; |
| 1162 |
} |
| 1163 |
default: |
| 1164 |
/* unknown state */ |
| 1165 |
mrb_assert(0); |
| 1166 |
return 0; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
static void |
| 1171 |
incremental_gc_finish(mrb_state *mrb, mrb_gc *gc) |
| 1172 |
{ |
| 1173 |
do { |
| 1174 |
incremental_gc(mrb, gc, SIZE_MAX); |
| 1175 |
} while (gc->state != MRB_GC_STATE_ROOT); |
| 1176 |
} |
| 1177 |
|
| 1178 |
static void |
| 1179 |
incremental_gc_step(mrb_state *mrb, mrb_gc *gc) |
| 1180 |
{ |
| 1181 |
size_t limit = 0, result = 0; |
| 1182 |
limit = (GC_STEP_SIZE/100) * gc->step_ratio; |
| 1183 |
while (result < limit) { |
| 1184 |
result += incremental_gc(mrb, gc, limit); |
| 1185 |
if (gc->state == MRB_GC_STATE_ROOT) |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
|
| 1189 |
gc->threshold = gc->live + GC_STEP_SIZE; |
| 1190 |
} |
| 1191 |
|
| 1192 |
static void |
| 1193 |
clear_all_old(mrb_state *mrb, mrb_gc *gc) |
| 1194 |
{ |
| 1195 |
mrb_assert(is_generational(gc)); |
| 1196 |
if (gc->full) { |
| 1197 |
/* finish the half baked GC */ |
| 1198 |
incremental_gc_finish(mrb, gc); |
| 1199 |
} |
| 1200 |
/* Sweep the dead objects, then reset all the live objects |
| 1201 |
* (including all the old objects, of course) to white. */ |
| 1202 |
gc->generational = FALSE; |
| 1203 |
prepare_incremental_sweep(mrb, gc); |
| 1204 |
incremental_gc_finish(mrb, gc); |
| 1205 |
gc->generational = TRUE; |
| 1206 |
/* The gray objects have already been painted as white */ |
| 1207 |
gc->atomic_gray_list = gc->gray_list = NULL; |
| 1208 |
} |
| 1209 |
|
| 1210 |
MRB_API void |
| 1211 |
mrb_incremental_gc(mrb_state *mrb) |
| 1212 |
{ |
| 1213 |
mrb_gc *gc = &mrb->gc; |
| 1214 |
|
| 1215 |
if (gc->disabled || gc->iterating) return; |
| 1216 |
|
| 1217 |
if (is_minor_gc(gc)) { |
| 1218 |
incremental_gc_finish(mrb, gc); |
| 1219 |
} |
| 1220 |
else { |
| 1221 |
incremental_gc_step(mrb, gc); |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (gc->state == MRB_GC_STATE_ROOT) { |
| 1225 |
mrb_assert(gc->live >= gc->live_after_mark); |
| 1226 |
gc->threshold = (gc->live_after_mark/100) * gc->interval_ratio; |
| 1227 |
if (gc->threshold < GC_STEP_SIZE) { |
| 1228 |
gc->threshold = GC_STEP_SIZE; |
| 1229 |
} |
| 1230 |
|
| 1231 |
if (is_major_gc(gc)) { |
| 1232 |
size_t threshold = gc->live_after_mark/100 * MAJOR_GC_INC_RATIO; |
| 1233 |
|
| 1234 |
gc->full = FALSE; |
| 1235 |
if (threshold < MAJOR_GC_TOOMANY) { |
| 1236 |
gc->oldgen_threshold = threshold; |
| 1237 |
} |
| 1238 |
else { |
| 1239 |
/* too many objects allocated during incremental GC, */ |
| 1240 |
/* instead of increasing threshold, invoke full GC. */ |
| 1241 |
mrb_full_gc(mrb); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
else if (is_minor_gc(gc) && gc->live > gc->oldgen_threshold) { |
| 1245 |
clear_all_old(mrb, gc); |
| 1246 |
gc->full = TRUE; |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* Perform a full gc cycle */ |
| 1252 |
MRB_API void |
| 1253 |
mrb_full_gc(mrb_state *mrb) |
| 1254 |
{ |
| 1255 |
mrb_gc *gc = &mrb->gc; |
| 1256 |
|
| 1257 |
if (!mrb->c) return; |
| 1258 |
if (gc->disabled || gc->iterating) return; |
| 1259 |
|
| 1260 |
if (is_generational(gc)) { |
| 1261 |
/* clear all the old objects back to young */ |
| 1262 |
clear_all_old(mrb, gc); |
| 1263 |
gc->full = TRUE; |
| 1264 |
} |
| 1265 |
else if (gc->state != MRB_GC_STATE_ROOT) { |
| 1266 |
/* finish half baked GC cycle */ |
| 1267 |
incremental_gc_finish(mrb, gc); |
| 1268 |
} |
| 1269 |
|
| 1270 |
incremental_gc_finish(mrb, gc); |
| 1271 |
gc->threshold = (gc->live_after_mark/100) * gc->interval_ratio; |
| 1272 |
|
| 1273 |
if (is_generational(gc)) { |
| 1274 |
gc->oldgen_threshold = gc->live_after_mark/100 * MAJOR_GC_INC_RATIO; |
| 1275 |
gc->full = FALSE; |
| 1276 |
} |
| 1277 |
|
| 1278 |
#ifdef MRB_USE_MALLOC_TRIM |
| 1279 |
malloc_trim(0); |
| 1280 |
#endif |
| 1281 |
} |
| 1282 |
|
| 1283 |
MRB_API void |
| 1284 |
mrb_garbage_collect(mrb_state *mrb) |
| 1285 |
{ |
| 1286 |
mrb_full_gc(mrb); |
| 1287 |
} |
| 1288 |
|
| 1289 |
/* |
| 1290 |
* Field write barrier |
| 1291 |
* Paint obj(Black) -> value(White) to obj(Black) -> value(Gray). |
| 1292 |
*/ |
| 1293 |
|
| 1294 |
MRB_API void |
| 1295 |
mrb_field_write_barrier(mrb_state *mrb, struct RBasic *obj, struct RBasic *value) |
| 1296 |
{ |
| 1297 |
mrb_gc *gc = &mrb->gc; |
| 1298 |
|
| 1299 |
if (!value) return; |
| 1300 |
if (!is_black(obj)) return; |
| 1301 |
if (!is_white(value)) return; |
| 1302 |
if (is_red(value)) return; |
| 1303 |
|
| 1304 |
mrb_assert(gc->state == MRB_GC_STATE_MARK || (!is_dead(gc, value) && !is_dead(gc, obj))); |
| 1305 |
mrb_assert(is_generational(gc) || gc->state != MRB_GC_STATE_ROOT); |
| 1306 |
|
| 1307 |
if (is_generational(gc) || gc->state == MRB_GC_STATE_MARK) { |
| 1308 |
add_gray_list(mrb, gc, value); |
| 1309 |
} |
| 1310 |
else { |
| 1311 |
mrb_assert(gc->state == MRB_GC_STATE_SWEEP); |
| 1312 |
paint_partial_white(gc, obj); /* for never write barriers */ |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* |
| 1317 |
* Write barrier |
| 1318 |
* Paint obj(Black) to obj(Gray). |
| 1319 |
* |
| 1320 |
* The object that is painted gray will be traversed atomically in final |
| 1321 |
* mark phase. So you use this write barrier if it's frequency written spot. |
| 1322 |
* e.g. Set element on Array. |
| 1323 |
*/ |
| 1324 |
|
| 1325 |
MRB_API void |
| 1326 |
mrb_write_barrier(mrb_state *mrb, struct RBasic *obj) |
| 1327 |
{ |
| 1328 |
mrb_gc *gc = &mrb->gc; |
| 1329 |
|
| 1330 |
if (!is_black(obj)) return; |
| 1331 |
|
| 1332 |
mrb_assert(!is_dead(gc, obj)); |
| 1333 |
mrb_assert(is_generational(gc) || gc->state != MRB_GC_STATE_ROOT); |
| 1334 |
paint_gray(obj); |
| 1335 |
obj->gcnext = gc->atomic_gray_list; |
| 1336 |
gc->atomic_gray_list = obj; |
| 1337 |
} |
| 1338 |
|
| 1339 |
/* |
| 1340 |
* call-seq: |
| 1341 |
* GC.start -> nil |
| 1342 |
* |
| 1343 |
* Initiates full garbage collection. |
| 1344 |
* |
| 1345 |
*/ |
| 1346 |
|
| 1347 |
static mrb_value |
| 1348 |
gc_start(mrb_state *mrb, mrb_value obj) |
| 1349 |
{ |
| 1350 |
mrb_full_gc(mrb); |
| 1351 |
return mrb_nil_value(); |
| 1352 |
} |
| 1353 |
|
| 1354 |
/* |
| 1355 |
* call-seq: |
| 1356 |
* GC.enable -> true or false |
| 1357 |
* |
| 1358 |
* Enables garbage collection, returning <code>true</code> if garbage |
| 1359 |
* collection was previously disabled. |
| 1360 |
* |
| 1361 |
* GC.disable #=> false |
| 1362 |
* GC.enable #=> true |
| 1363 |
* GC.enable #=> false |
| 1364 |
* |
| 1365 |
*/ |
| 1366 |
|
| 1367 |
static mrb_value |
| 1368 |
gc_enable(mrb_state *mrb, mrb_value obj) |
| 1369 |
{ |
| 1370 |
mrb_bool old = mrb->gc.disabled; |
| 1371 |
|
| 1372 |
mrb->gc.disabled = FALSE; |
| 1373 |
|
| 1374 |
return mrb_bool_value(old); |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* call-seq: |
| 1379 |
* GC.disable -> true or false |
| 1380 |
* |
| 1381 |
* Disables garbage collection, returning <code>true</code> if garbage |
| 1382 |
* collection was already disabled. |
| 1383 |
* |
| 1384 |
* GC.disable #=> false |
| 1385 |
* GC.disable #=> true |
| 1386 |
* |
| 1387 |
*/ |
| 1388 |
|
| 1389 |
static mrb_value |
| 1390 |
gc_disable(mrb_state *mrb, mrb_value obj) |
| 1391 |
{ |
| 1392 |
mrb_bool old = mrb->gc.disabled; |
| 1393 |
|
| 1394 |
mrb->gc.disabled = TRUE; |
| 1395 |
|
| 1396 |
return mrb_bool_value(old); |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* |
| 1400 |
* call-seq: |
| 1401 |
* GC.interval_ratio -> int |
| 1402 |
* |
| 1403 |
* Returns ratio of GC interval. Default value is 200(%). |
| 1404 |
* |
| 1405 |
*/ |
| 1406 |
|
| 1407 |
static mrb_value |
| 1408 |
gc_interval_ratio_get(mrb_state *mrb, mrb_value obj) |
| 1409 |
{ |
| 1410 |
return mrb_int_value(mrb, mrb->gc.interval_ratio); |
| 1411 |
} |
| 1412 |
|
| 1413 |
/* |
| 1414 |
* call-seq: |
| 1415 |
* GC.interval_ratio = int -> nil |
| 1416 |
* |
| 1417 |
* Updates ratio of GC interval. Default value is 200(%). |
| 1418 |
* GC start as soon as after end all step of GC if you set 100(%). |
| 1419 |
* |
| 1420 |
*/ |
| 1421 |
|
| 1422 |
static mrb_value |
| 1423 |
gc_interval_ratio_set(mrb_state *mrb, mrb_value obj) |
| 1424 |
{ |
| 1425 |
mrb_int ratio; |
| 1426 |
|
| 1427 |
mrb_get_args(mrb, "i", &ratio); |
| 1428 |
mrb->gc.interval_ratio = (int)ratio; |
| 1429 |
return mrb_nil_value(); |
| 1430 |
} |
| 1431 |
|
| 1432 |
/* |
| 1433 |
* call-seq: |
| 1434 |
* GC.step_ratio -> int |
| 1435 |
* |
| 1436 |
* Returns step span ratio of Incremental GC. Default value is 200(%). |
| 1437 |
* |
| 1438 |
*/ |
| 1439 |
|
| 1440 |
static mrb_value |
| 1441 |
gc_step_ratio_get(mrb_state *mrb, mrb_value obj) |
| 1442 |
{ |
| 1443 |
return mrb_int_value(mrb, mrb->gc.step_ratio); |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* |
| 1447 |
* call-seq: |
| 1448 |
* GC.step_ratio = int -> nil |
| 1449 |
* |
| 1450 |
* Updates step span ratio of Incremental GC. Default value is 200(%). |
| 1451 |
* 1 step of incrementalGC becomes long if a rate is big. |
| 1452 |
* |
| 1453 |
*/ |
| 1454 |
|
| 1455 |
static mrb_value |
| 1456 |
gc_step_ratio_set(mrb_state *mrb, mrb_value obj) |
| 1457 |
{ |
| 1458 |
mrb_int ratio; |
| 1459 |
|
| 1460 |
mrb_get_args(mrb, "i", &ratio); |
| 1461 |
mrb->gc.step_ratio = (int)ratio; |
| 1462 |
return mrb_nil_value(); |
| 1463 |
} |
| 1464 |
|
| 1465 |
static void |
| 1466 |
change_gen_gc_mode(mrb_state *mrb, mrb_gc *gc, mrb_bool enable) |
| 1467 |
{ |
| 1468 |
if (gc->disabled || gc->iterating) { |
| 1469 |
mrb_raise(mrb, E_RUNTIME_ERROR, "generational mode changed when GC disabled"); |
| 1470 |
return; |
| 1471 |
} |
| 1472 |
if (is_generational(gc) && !enable) { |
| 1473 |
clear_all_old(mrb, gc); |
| 1474 |
mrb_assert(gc->state == MRB_GC_STATE_ROOT); |
| 1475 |
gc->full = FALSE; |
| 1476 |
} |
| 1477 |
else if (!is_generational(gc) && enable) { |
| 1478 |
incremental_gc_finish(mrb, gc); |
| 1479 |
gc->oldgen_threshold = gc->live_after_mark/100 * MAJOR_GC_INC_RATIO; |
| 1480 |
gc->full = FALSE; |
| 1481 |
} |
| 1482 |
gc->generational = enable; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/* |
| 1486 |
* call-seq: |
| 1487 |
* GC.generational_mode -> true or false |
| 1488 |
* |
| 1489 |
* Returns generational or normal gc mode. |
| 1490 |
* |
| 1491 |
*/ |
| 1492 |
|
| 1493 |
static mrb_value |
| 1494 |
gc_generational_mode_get(mrb_state *mrb, mrb_value self) |
| 1495 |
{ |
| 1496 |
return mrb_bool_value(mrb->gc.generational); |
| 1497 |
} |
| 1498 |
|
| 1499 |
/* |
| 1500 |
* call-seq: |
| 1501 |
* GC.generational_mode = true or false -> true or false |
| 1502 |
* |
| 1503 |
* Changes to generational or normal gc mode. |
| 1504 |
* |
| 1505 |
*/ |
| 1506 |
|
| 1507 |
static mrb_value |
| 1508 |
gc_generational_mode_set(mrb_state *mrb, mrb_value self) |
| 1509 |
{ |
| 1510 |
mrb_bool enable; |
| 1511 |
|
| 1512 |
mrb_get_args(mrb, "b", &enable); |
| 1513 |
if (mrb->gc.generational != enable) |
| 1514 |
change_gen_gc_mode(mrb, &mrb->gc, enable); |
| 1515 |
|
| 1516 |
return mrb_bool_value(enable); |
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
static void |
| 1521 |
gc_each_objects(mrb_state *mrb, mrb_gc *gc, mrb_each_object_callback *callback, void *data) |
| 1522 |
{ |
| 1523 |
mrb_heap_page* page; |
| 1524 |
|
| 1525 |
page = gc->heaps; |
| 1526 |
while (page != NULL) { |
| 1527 |
RVALUE *p; |
| 1528 |
|
| 1529 |
p = objects(page); |
| 1530 |
for (int i=0; i < MRB_HEAP_PAGE_SIZE; i++) { |
| 1531 |
if ((*callback)(mrb, &p[i].as.basic, data) == MRB_EACH_OBJ_BREAK) |
| 1532 |
return; |
| 1533 |
} |
| 1534 |
page = page->next; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
void |
| 1539 |
mrb_objspace_each_objects(mrb_state *mrb, mrb_each_object_callback *callback, void *data) |
| 1540 |
{ |
| 1541 |
mrb_bool iterating = mrb->gc.iterating; |
| 1542 |
|
| 1543 |
mrb_full_gc(mrb); |
| 1544 |
mrb->gc.iterating = TRUE; |
| 1545 |
if (iterating) { |
| 1546 |
gc_each_objects(mrb, &mrb->gc, callback, data); |
| 1547 |
} |
| 1548 |
else { |
| 1549 |
struct mrb_jmpbuf *prev_jmp = mrb->jmp; |
| 1550 |
struct mrb_jmpbuf c_jmp; |
| 1551 |
|
| 1552 |
MRB_TRY(&c_jmp) { |
| 1553 |
mrb->jmp = &c_jmp; |
| 1554 |
gc_each_objects(mrb, &mrb->gc, callback, data); |
| 1555 |
mrb->jmp = prev_jmp; |
| 1556 |
mrb->gc.iterating = iterating; |
| 1557 |
} MRB_CATCH(&c_jmp) { |
| 1558 |
mrb->gc.iterating = iterating; |
| 1559 |
mrb->jmp = prev_jmp; |
| 1560 |
MRB_THROW(prev_jmp); |
| 1561 |
} MRB_END_EXC(&c_jmp); |
| 1562 |
} |
| 1563 |
} |
| 1564 |
|
| 1565 |
size_t |
| 1566 |
mrb_objspace_page_slot_size(void) |
| 1567 |
{ |
| 1568 |
return sizeof(RVALUE); |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
void |
| 1573 |
mrb_init_gc(mrb_state *mrb) |
| 1574 |
{ |
| 1575 |
struct RClass *gc; |
| 1576 |
|
| 1577 |
mrb_static_assert_object_size(RVALUE); |
| 1578 |
|
| 1579 |
gc = mrb_define_module(mrb, "GC"); |
| 1580 |
|
| 1581 |
mrb_define_class_method(mrb, gc, "start", gc_start, MRB_ARGS_NONE()); |
| 1582 |
mrb_define_class_method(mrb, gc, "enable", gc_enable, MRB_ARGS_NONE()); |
| 1583 |
mrb_define_class_method(mrb, gc, "disable", gc_disable, MRB_ARGS_NONE()); |
| 1584 |
mrb_define_class_method(mrb, gc, "interval_ratio", gc_interval_ratio_get, MRB_ARGS_NONE()); |
| 1585 |
mrb_define_class_method(mrb, gc, "interval_ratio=", gc_interval_ratio_set, MRB_ARGS_REQ(1)); |
| 1586 |
mrb_define_class_method(mrb, gc, "step_ratio", gc_step_ratio_get, MRB_ARGS_NONE()); |
| 1587 |
mrb_define_class_method(mrb, gc, "step_ratio=", gc_step_ratio_set, MRB_ARGS_REQ(1)); |
| 1588 |
mrb_define_class_method(mrb, gc, "generational_mode=", gc_generational_mode_set, MRB_ARGS_REQ(1)); |
| 1589 |
mrb_define_class_method(mrb, gc, "generational_mode", gc_generational_mode_get, MRB_ARGS_NONE()); |
| 1590 |
} |