* file.c (rb_find_file_ext): guard load_path from GC.
[ruby-svn.git] / gc.c
blob50e0067ff15450bf38faacb3087eb1e755f74f3f
1 /**********************************************************************
3 gc.c -
5 $Author$
6 created at: Tue Oct 5 09:44:46 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/signal.h"
16 #include "ruby/st.h"
17 #include "ruby/node.h"
18 #include "ruby/re.h"
19 #include "ruby/io.h"
20 #include "ruby/util.h"
21 #include "eval_intern.h"
22 #include "vm_core.h"
23 #include "gc.h"
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
34 #endif
36 #if defined _WIN32 || defined __CYGWIN__
37 #include <windows.h>
38 #endif
40 #ifdef HAVE_VALGRIND_MEMCHECK_H
41 # include <valgrind/memcheck.h>
42 # ifndef VALGRIND_MAKE_MEM_DEFINED
43 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE(p, n)
44 # endif
45 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
46 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE(p, n)
47 # endif
48 #else
49 # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
50 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
51 #endif
53 int rb_io_fptr_finalize(struct rb_io_t*);
55 #define rb_setjmp(env) RUBY_SETJMP(env)
56 #define rb_jmp_buf rb_jmpbuf_t
58 /* Make alloca work the best possible way. */
59 #ifdef __GNUC__
60 # ifndef atarist
61 # ifndef alloca
62 # define alloca __builtin_alloca
63 # endif
64 # endif /* atarist */
65 #else
66 # ifdef HAVE_ALLOCA_H
67 # include <alloca.h>
68 # else
69 # ifdef _AIX
70 #pragma alloca
71 # else
72 # ifndef alloca /* predefined by HP cc +Olibcalls */
73 void *alloca ();
74 # endif
75 # endif /* AIX */
76 # endif /* HAVE_ALLOCA_H */
77 #endif /* __GNUC__ */
79 #ifndef GC_MALLOC_LIMIT
80 #if defined(MSDOS) || defined(__human68k__)
81 #define GC_MALLOC_LIMIT 200000
82 #else
83 #define GC_MALLOC_LIMIT 8000000
84 #endif
85 #endif
87 static VALUE nomem_error;
89 #define MARK_STACK_MAX 1024
91 int ruby_gc_debug_indent = 0;
93 #undef GC_DEBUG
95 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
96 #pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
97 #endif
99 typedef struct RVALUE {
100 union {
101 struct {
102 VALUE flags; /* always 0 for freed obj */
103 struct RVALUE *next;
104 } free;
105 struct RBasic basic;
106 struct RObject object;
107 struct RClass klass;
108 struct RFloat flonum;
109 struct RString string;
110 struct RArray array;
111 struct RRegexp regexp;
112 struct RHash hash;
113 struct RData data;
114 struct RStruct rstruct;
115 struct RBignum bignum;
116 struct RFile file;
117 struct RNode node;
118 struct RMatch match;
119 struct RRational rational;
120 struct RComplex complex;
121 } as;
122 #ifdef GC_DEBUG
123 char *file;
124 int line;
125 #endif
126 } RVALUE;
128 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
129 #pragma pack(pop)
130 #endif
132 struct heaps_slot {
133 void *membase;
134 RVALUE *slot;
135 int limit;
138 #define HEAP_MIN_SLOTS 10000
139 #define FREE_MIN 4096
141 struct gc_list {
142 VALUE *varptr;
143 struct gc_list *next;
146 typedef struct rb_objspace {
147 struct {
148 unsigned long limit;
149 unsigned long increase;
150 } params;
151 struct {
152 long increment;
153 struct heaps_slot *ptr;
154 long length;
155 long used;
156 RVALUE *freelist;
157 RVALUE *range[2];
158 RVALUE *freed;
159 } heap;
160 struct {
161 int dont_gc;
162 int during_gc;
163 } flags;
164 struct {
165 int need_call;
166 st_table *table;
167 RVALUE *deferred;
168 } final;
169 struct {
170 VALUE buffer[MARK_STACK_MAX];
171 VALUE *ptr;
172 int overflow;
173 } markstack;
174 struct gc_list *global_list;
175 unsigned int count;
176 } rb_objspace_t;
178 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
179 #define rb_objspace (*GET_VM()->objspace)
180 #else
181 static rb_objspace_t rb_objspace = {{GC_MALLOC_LIMIT}, {HEAP_MIN_SLOTS}};
182 #endif
183 #define malloc_limit objspace->params.limit
184 #define malloc_increase objspace->params.increase
185 #define heap_slots objspace->heap.slots
186 #define heaps objspace->heap.ptr
187 #define heaps_length objspace->heap.length
188 #define heaps_used objspace->heap.used
189 #define freelist objspace->heap.freelist
190 #define lomem objspace->heap.range[0]
191 #define himem objspace->heap.range[1]
192 #define heaps_inc objspace->heap.increment
193 #define heaps_freed objspace->heap.freed
194 #define dont_gc objspace->flags.dont_gc
195 #define during_gc objspace->flags.during_gc
196 #define need_call_final objspace->final.need_call
197 #define finalizer_table objspace->final.table
198 #define deferred_final_list objspace->final.deferred
199 #define mark_stack objspace->markstack.buffer
200 #define mark_stack_ptr objspace->markstack.ptr
201 #define mark_stack_overflow objspace->markstack.overflow
202 #define global_List objspace->global_list
204 rb_objspace_t *
205 rb_objspace_alloc(void)
207 rb_objspace_t *objspace = malloc(sizeof(rb_objspace_t));
208 memset(objspace, 0, sizeof(*objspace));
209 malloc_limit = GC_MALLOC_LIMIT;
211 return objspace;
214 /* tiny heap size */
215 /* 32KB */
216 /*#define HEAP_SIZE 0x8000 */
217 /* 128KB */
218 /*#define HEAP_SIZE 0x20000 */
219 /* 64KB */
220 /*#define HEAP_SIZE 0x10000 */
221 /* 16KB */
222 #define HEAP_SIZE 0x4000
223 /* 8KB */
224 /*#define HEAP_SIZE 0x2000 */
225 /* 4KB */
226 /*#define HEAP_SIZE 0x1000 */
227 /* 2KB */
228 /*#define HEAP_SIZE 0x800 */
230 #define HEAP_OBJ_LIMIT (HEAP_SIZE / sizeof(struct RVALUE))
231 #define FREE_MIN 4096
233 extern st_table *rb_class_tbl;
234 VALUE *rb_gc_stack_start = 0;
235 #ifdef __ia64
236 VALUE *rb_gc_register_stack_start = 0;
237 #endif
239 int ruby_gc_stress = 0;
242 #ifdef DJGPP
243 /* set stack size (http://www.delorie.com/djgpp/v2faq/faq15_9.html) */
244 unsigned int _stklen = 0x180000; /* 1.5 kB */
245 #endif
247 #if defined(DJGPP) || defined(_WIN32_WCE)
248 size_t rb_gc_stack_maxsize = 65535*sizeof(VALUE);
249 #else
250 size_t rb_gc_stack_maxsize = 655300*sizeof(VALUE);
251 #endif
253 static void run_final(rb_objspace_t *objspace, VALUE obj);
254 static int garbage_collect(rb_objspace_t *objspace);
256 void
257 rb_global_variable(VALUE *var)
259 rb_gc_register_address(var);
262 void
263 rb_memerror(void)
265 rb_thread_t *th = GET_THREAD();
266 if (!nomem_error ||
267 (rb_thread_raised_p(th, RAISED_NOMEMORY) && rb_safe_level() < 4)) {
268 fprintf(stderr, "[FATAL] failed to allocate memory\n");
269 exit(1);
271 rb_thread_raised_set(th, RAISED_NOMEMORY);
272 rb_exc_raise(nomem_error);
276 * call-seq:
277 * GC.stress => true or false
279 * returns current status of GC stress mode.
282 static VALUE
283 gc_stress_get(VALUE self)
285 return ruby_gc_stress ? Qtrue : Qfalse;
289 * call-seq:
290 * GC.stress = bool => bool
292 * updates GC stress mode.
294 * When GC.stress = true, GC is invoked for all GC opportunity:
295 * all memory and object allocation.
297 * Since it makes Ruby very slow, it is only for debugging.
300 static VALUE
301 gc_stress_set(VALUE self, VALUE bool)
303 rb_secure(2);
304 ruby_gc_stress = RTEST(bool);
305 return bool;
308 void *
309 ruby_vm_xmalloc(rb_objspace_t *objspace, size_t size)
311 void *mem;
313 if (size < 0) {
314 rb_raise(rb_eNoMemError, "negative allocation size (or too big)");
316 if (size == 0) size = 1;
317 malloc_increase += size;
319 if (ruby_gc_stress || malloc_increase > malloc_limit) {
320 garbage_collect(objspace);
322 RUBY_CRITICAL(mem = malloc(size));
323 if (!mem) {
324 if (garbage_collect(objspace)) {
325 RUBY_CRITICAL(mem = malloc(size));
327 if (!mem) {
328 rb_memerror();
332 return mem;
335 void *
336 ruby_xmalloc(size_t size)
338 return ruby_vm_xmalloc(&rb_objspace, size);
341 void *
342 ruby_vm_xmalloc2(rb_objspace_t *objspace, size_t n, size_t size)
344 size_t len = size * n;
345 if (n != 0 && size != len / n) {
346 rb_raise(rb_eArgError, "malloc: possible integer overflow");
348 return ruby_vm_xmalloc(objspace, len);
351 void *
352 ruby_xmalloc2(size_t n, size_t size)
354 return ruby_vm_xmalloc2(&rb_objspace, n, size);
357 void *
358 ruby_vm_xcalloc(rb_objspace_t *objspace, size_t n, size_t size)
360 void *mem;
362 mem = ruby_vm_xmalloc2(objspace, n, size);
363 memset(mem, 0, n * size);
365 return mem;
368 void *
369 ruby_xcalloc(size_t n, size_t size)
371 return ruby_vm_xcalloc(&rb_objspace, n, size);
374 void *
375 ruby_vm_xrealloc(rb_objspace_t *objspace, void *ptr, size_t size)
377 void *mem;
379 if (size < 0) {
380 rb_raise(rb_eArgError, "negative re-allocation size");
382 if (!ptr) return ruby_xmalloc(size);
383 if (size == 0) size = 1;
384 malloc_increase += size;
385 if (ruby_gc_stress) garbage_collect(objspace);
386 RUBY_CRITICAL(mem = realloc(ptr, size));
387 if (!mem) {
388 if (garbage_collect(objspace)) {
389 RUBY_CRITICAL(mem = realloc(ptr, size));
391 if (!mem) {
392 rb_memerror();
396 return mem;
399 void *
400 ruby_xrealloc(void *ptr, size_t size)
402 return ruby_vm_xrealloc(&rb_objspace, ptr, size);
405 void *
406 ruby_vm_xrealloc2(rb_objspace_t *objspace, void *ptr, size_t n, size_t size)
408 size_t len = size * n;
409 if (n != 0 && size != len / n) {
410 rb_raise(rb_eArgError, "realloc: possible integer overflow");
412 return ruby_vm_xrealloc(objspace, ptr, len);
415 void *
416 ruby_xrealloc2(void *ptr, size_t n, size_t size)
418 return ruby_vm_xrealloc2(&rb_objspace, ptr, n, size);
421 void
422 ruby_xfree(void *x)
424 if (x)
425 RUBY_CRITICAL(free(x));
430 * call-seq:
431 * GC.enable => true or false
433 * Enables garbage collection, returning <code>true</code> if garbage
434 * collection was previously disabled.
436 * GC.disable #=> false
437 * GC.enable #=> true
438 * GC.enable #=> false
442 VALUE
443 rb_gc_enable(void)
445 rb_objspace_t *objspace = &rb_objspace;
446 int old = dont_gc;
448 dont_gc = Qfalse;
449 return old;
453 * call-seq:
454 * GC.disable => true or false
456 * Disables garbage collection, returning <code>true</code> if garbage
457 * collection was already disabled.
459 * GC.disable #=> false
460 * GC.disable #=> true
464 VALUE
465 rb_gc_disable(void)
467 rb_objspace_t *objspace = &rb_objspace;
468 int old = dont_gc;
470 dont_gc = Qtrue;
471 return old;
474 VALUE rb_mGC;
476 void
477 rb_gc_register_address(VALUE *addr)
479 rb_objspace_t *objspace = &rb_objspace;
480 struct gc_list *tmp;
482 tmp = ALLOC(struct gc_list);
483 tmp->next = global_List;
484 tmp->varptr = addr;
485 global_List = tmp;
488 void
489 rb_register_mark_object(VALUE obj)
491 VALUE ary = GET_THREAD()->vm->mark_object_ary;
492 rb_ary_push(ary, obj);
495 void
496 rb_gc_unregister_address(VALUE *addr)
498 rb_objspace_t *objspace = &rb_objspace;
499 struct gc_list *tmp = global_List;
501 if (tmp->varptr == addr) {
502 global_List = tmp->next;
503 RUBY_CRITICAL(free(tmp));
504 return;
506 while (tmp->next) {
507 if (tmp->next->varptr == addr) {
508 struct gc_list *t = tmp->next;
510 tmp->next = tmp->next->next;
511 RUBY_CRITICAL(free(t));
512 break;
514 tmp = tmp->next;
519 static void
520 allocate_heaps(rb_objspace_t *objspace, int next_heaps_length)
522 struct heaps_slot *p;
523 int length;
525 heaps_length = next_heaps_length;
526 length = heaps_length*sizeof(struct heaps_slot);
527 RUBY_CRITICAL(
528 if (heaps_used > 0) {
529 p = (struct heaps_slot *)realloc(heaps, length);
530 if (p) heaps = p;
532 else {
533 p = heaps = (struct heaps_slot *)malloc(length);
536 if (p == 0) rb_memerror();
539 static void
540 assign_heap_slot(rb_objspace_t *objspace)
542 RVALUE *p, *pend, *membase;
543 long hi, lo, mid;
544 int objs;
546 objs = HEAP_OBJ_LIMIT;
547 RUBY_CRITICAL(p = (RVALUE*)malloc(HEAP_SIZE));
548 if (p == 0)
549 rb_memerror();
551 lo = 0;
552 hi = heaps_used;
553 while (lo < hi) {
554 mid = (lo + hi) / 2;
555 membase = heaps[mid].membase;
556 if (membase < p) {
557 lo = mid + 1;
559 else if (membase > p) {
560 hi = mid;
562 else {
563 rb_bug("same heap slot is allocated: %p at %ld", p, mid);
567 membase = p;
569 if ((VALUE)p % sizeof(RVALUE) != 0) {
570 p = (RVALUE*)((VALUE)p + sizeof(RVALUE) - ((VALUE)p % sizeof(RVALUE)));
571 if ((membase + HEAP_SIZE) < (p + HEAP_SIZE)) {
572 objs--;
577 if (hi < heaps_used) {
578 MEMMOVE(&heaps[hi+1], &heaps[hi], struct heaps_slot, heaps_used - hi);
580 heaps[hi].membase = membase;
581 heaps[hi].slot = p;
582 heaps[hi].limit = objs;
583 pend = p + objs;
584 if (lomem == 0 || lomem > p) lomem = p;
585 if (himem < pend) himem = pend;
586 heaps_used++;
588 while (p < pend) {
589 p->as.free.flags = 0;
590 p->as.free.next = freelist;
591 freelist = p;
592 p++;
596 static void
597 init_heap(rb_objspace_t *objspace)
599 int add, i;
601 add = HEAP_MIN_SLOTS / HEAP_OBJ_LIMIT;
603 if ((heaps_used + add) > heaps_length) {
604 allocate_heaps(objspace, heaps_used + add);
607 for (i = 0; i < add; i++) {
608 assign_heap_slot(objspace);
610 heaps_inc = 0;
614 static void
615 set_heaps_increment(rb_objspace_t *objspace)
617 heaps_inc = heaps_used * 1.8 - heaps_used;
619 if ((heaps_used + heaps_inc) > heaps_length) {
620 allocate_heaps(objspace, heaps_used + heaps_inc);
624 static int
625 heaps_increment(rb_objspace_t *objspace)
627 if (heaps_inc > 0) {
628 assign_heap_slot(objspace);
629 heaps_inc--;
630 return Qtrue;
632 return Qfalse;
635 #define RANY(o) ((RVALUE*)(o))
637 static VALUE
638 rb_newobj_from_heap(rb_objspace_t *objspace)
640 VALUE obj;
642 if (ruby_gc_stress || !freelist) {
643 if (!heaps_increment(objspace) && !garbage_collect(objspace)) {
644 rb_memerror();
648 obj = (VALUE)freelist;
649 freelist = freelist->as.free.next;
651 MEMZERO((void*)obj, RVALUE, 1);
652 #ifdef GC_DEBUG
653 RANY(obj)->file = rb_sourcefile();
654 RANY(obj)->line = rb_sourceline();
655 #endif
657 return obj;
660 #if USE_VALUE_CACHE
661 static VALUE
662 rb_fill_value_cache(rb_thread_t *th)
664 rb_objspace_t *objspace = &rb_objspace;
665 int i;
666 VALUE rv;
668 /* LOCK */
669 for (i=0; i<RUBY_VM_VALUE_CACHE_SIZE; i++) {
670 VALUE v = rb_newobj_from_heap(objspace);
672 th->value_cache[i] = v;
673 RBASIC(v)->flags = FL_MARK;
675 th->value_cache_ptr = &th->value_cache[0];
676 rv = rb_newobj_from_heap(objspace);
677 /* UNLOCK */
678 return rv;
680 #endif
682 VALUE
683 rb_newobj(void)
685 #if USE_VALUE_CACHE
686 rb_thread_t *th = GET_THREAD();
687 VALUE v = *th->value_cache_ptr;
688 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
689 rb_objspace_t *objspace = th->vm->objspace;
690 #else
691 rb_objspace_t *objspace = &rb_objspace;
692 #endif
694 if (v) {
695 RBASIC(v)->flags = 0;
696 th->value_cache_ptr++;
698 else {
699 v = rb_fill_value_cache(th);
702 #if defined(GC_DEBUG)
703 printf("cache index: %d, v: %p, th: %p\n",
704 th->value_cache_ptr - th->value_cache, v, th);
705 #endif
706 return v;
707 #else
708 rb_objspace_t *objspace = &rb_objspace;
709 return rb_newobj_from_heap(objspace);
710 #endif
713 NODE*
714 rb_node_newnode(enum node_type type, VALUE a0, VALUE a1, VALUE a2)
716 NODE *n = (NODE*)rb_newobj();
718 n->flags |= T_NODE;
719 nd_set_type(n, type);
721 n->u1.value = a0;
722 n->u2.value = a1;
723 n->u3.value = a2;
725 return n;
728 VALUE
729 rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
731 NEWOBJ(data, struct RData);
732 if (klass) Check_Type(klass, T_CLASS);
733 OBJSETUP(data, klass, T_DATA);
734 data->data = datap;
735 data->dfree = dfree;
736 data->dmark = dmark;
738 return (VALUE)data;
741 #ifdef __ia64
742 #define SET_STACK_END (SET_MACHINE_STACK_END(&th->machine_stack_end), th->machine_register_stack_end = rb_ia64_bsp())
743 #else
744 #define SET_STACK_END SET_MACHINE_STACK_END(&th->machine_stack_end)
745 #endif
747 #define STACK_START (th->machine_stack_start)
748 #define STACK_END (th->machine_stack_end)
749 #define STACK_LEVEL_MAX (th->machine_stack_maxsize/sizeof(VALUE))
751 #if STACK_GROW_DIRECTION < 0
752 # define STACK_LENGTH (STACK_START - STACK_END)
753 #elif STACK_GROW_DIRECTION > 0
754 # define STACK_LENGTH (STACK_END - STACK_START + 1)
755 #else
756 # define STACK_LENGTH ((STACK_END < STACK_START) ? STACK_START - STACK_END\
757 : STACK_END - STACK_START + 1)
758 #endif
759 #if STACK_GROW_DIRECTION > 0
760 # define STACK_UPPER(x, a, b) a
761 #elif STACK_GROW_DIRECTION < 0
762 # define STACK_UPPER(x, a, b) b
763 #else
764 static int grow_direction;
765 static int
766 stack_grow_direction(VALUE *addr)
768 rb_thread_t *th = GET_THREAD();
769 SET_STACK_END;
771 if (STACK_END > addr) return grow_direction = 1;
772 return grow_direction = -1;
774 # define stack_growup_p(x) ((grow_direction ? grow_direction : stack_grow_direction(x)) > 0)
775 # define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b)
776 #endif
778 #define GC_WATER_MARK 512
781 ruby_stack_length(VALUE **p)
783 rb_thread_t *th = GET_THREAD();
784 SET_STACK_END;
785 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
786 return STACK_LENGTH;
790 ruby_stack_check(void)
792 int ret;
793 rb_thread_t *th = GET_THREAD();
794 SET_STACK_END;
795 ret = STACK_LENGTH > STACK_LEVEL_MAX + GC_WATER_MARK;
796 #ifdef __ia64
797 if (!ret) {
798 ret = (VALUE*)rb_ia64_bsp() - th->machine_register_stack_start >
799 th->machine_register_stack_maxsize/sizeof(VALUE) + GC_WATER_MARK;
801 #endif
802 return ret;
805 static void
806 init_mark_stack(rb_objspace_t *objspace)
808 mark_stack_overflow = 0;
809 mark_stack_ptr = mark_stack;
812 #define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
814 static void gc_mark(rb_objspace_t *objspace, VALUE ptr, int lev);
815 static void gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev);
817 static void
818 gc_mark_all(rb_objspace_t *objspace)
820 RVALUE *p, *pend;
821 int i;
823 init_mark_stack(objspace);
824 for (i = 0; i < heaps_used; i++) {
825 p = heaps[i].slot; pend = p + heaps[i].limit;
826 while (p < pend) {
827 if ((p->as.basic.flags & FL_MARK) &&
828 (p->as.basic.flags != FL_MARK)) {
829 gc_mark_children(objspace, (VALUE)p, 0);
831 p++;
836 static void
837 gc_mark_rest(rb_objspace_t *objspace)
839 VALUE tmp_arry[MARK_STACK_MAX];
840 VALUE *p;
842 p = (mark_stack_ptr - mark_stack) + tmp_arry;
843 MEMCPY(tmp_arry, mark_stack, VALUE, p - tmp_arry);
845 init_mark_stack(objspace);
846 while (p != tmp_arry) {
847 p--;
848 gc_mark_children(objspace, *p, 0);
852 static inline int
853 is_pointer_to_heap(rb_objspace_t *objspace, void *ptr)
855 register RVALUE *p = RANY(ptr);
856 register struct heaps_slot *heap;
857 register long hi, lo, mid;
859 if (p < lomem || p > himem) return Qfalse;
860 if ((VALUE)p % sizeof(RVALUE) != 0) return Qfalse;
862 /* check if p looks like a pointer using bsearch*/
863 lo = 0;
864 hi = heaps_used;
865 while (lo < hi) {
866 mid = (lo + hi) / 2;
867 heap = &heaps[mid];
868 if (heap->slot <= p) {
869 if (p < heap->slot + heap->limit)
870 return Qtrue;
871 lo = mid + 1;
873 else {
874 hi = mid;
877 return Qfalse;
880 static void
881 mark_locations_array(rb_objspace_t *objspace, register VALUE *x, register long n)
883 VALUE v;
884 while (n--) {
885 v = *x;
886 VALGRIND_MAKE_MEM_DEFINED(&v, sizeof(v));
887 if (is_pointer_to_heap(objspace, (void *)v)) {
888 gc_mark(objspace, v, 0);
890 x++;
894 static void
895 gc_mark_locations(rb_objspace_t *objspace, VALUE *start, VALUE *end)
897 long n;
899 if (end <= start) return;
900 n = end - start;
901 mark_locations_array(&rb_objspace, start,n);
904 void
905 rb_gc_mark_locations(VALUE *start, VALUE *end)
907 gc_mark_locations(&rb_objspace, start, end);
910 #define rb_gc_mark_locations(start, end) gc_mark_locations(objspace, start, end)
912 struct mark_tbl_arg {
913 rb_objspace_t *objspace;
914 int lev;
917 static int
918 mark_entry(ID key, VALUE value, st_data_t data)
920 struct mark_tbl_arg *arg = (void*)data;
921 gc_mark(arg->objspace, value, arg->lev);
922 return ST_CONTINUE;
925 static void
926 mark_tbl(rb_objspace_t *objspace, st_table *tbl, int lev)
928 struct mark_tbl_arg arg;
929 if (!tbl) return;
930 arg.objspace = objspace;
931 arg.lev = lev;
932 st_foreach(tbl, mark_entry, (st_data_t)&arg);
935 void
936 rb_mark_tbl(st_table *tbl)
938 mark_tbl(&rb_objspace, tbl, 0);
941 static int
942 mark_key(VALUE key, VALUE value, st_data_t data)
944 struct mark_tbl_arg *arg = (void*)data;
945 gc_mark(arg->objspace, key, arg->lev);
946 return ST_CONTINUE;
949 static void
950 mark_set(rb_objspace_t *objspace, st_table *tbl, int lev)
952 struct mark_tbl_arg arg;
953 if (!tbl) return;
954 arg.objspace = objspace;
955 arg.lev = lev;
956 st_foreach(tbl, mark_key, (st_data_t)&arg);
959 void
960 rb_mark_set(st_table *tbl)
962 mark_set(&rb_objspace, tbl, 0);
965 static int
966 mark_keyvalue(VALUE key, VALUE value, st_data_t data)
968 struct mark_tbl_arg *arg = (void*)data;
969 gc_mark(arg->objspace, key, arg->lev);
970 gc_mark(arg->objspace, value, arg->lev);
971 return ST_CONTINUE;
974 static void
975 mark_hash(rb_objspace_t *objspace, st_table *tbl, int lev)
977 struct mark_tbl_arg arg;
978 if (!tbl) return;
979 arg.objspace = objspace;
980 arg.lev = lev;
981 st_foreach(tbl, mark_keyvalue, (st_data_t)&arg);
984 void
985 rb_mark_hash(st_table *tbl)
987 mark_hash(&rb_objspace, tbl, 0);
990 void
991 rb_gc_mark_maybe(VALUE obj)
993 if (is_pointer_to_heap(&rb_objspace, (void *)obj)) {
994 gc_mark(&rb_objspace, obj, 0);
998 #define GC_LEVEL_MAX 250
1000 static void
1001 gc_mark(rb_objspace_t *objspace, VALUE ptr, int lev)
1003 register RVALUE *obj;
1005 obj = RANY(ptr);
1006 if (rb_special_const_p(ptr)) return; /* special const not marked */
1007 if (obj->as.basic.flags == 0) return; /* free cell */
1008 if (obj->as.basic.flags & FL_MARK) return; /* already marked */
1009 obj->as.basic.flags |= FL_MARK;
1011 if (lev > GC_LEVEL_MAX || (lev == 0 && ruby_stack_check())) {
1012 if (!mark_stack_overflow) {
1013 if (mark_stack_ptr - mark_stack < MARK_STACK_MAX) {
1014 *mark_stack_ptr = ptr;
1015 mark_stack_ptr++;
1017 else {
1018 mark_stack_overflow = 1;
1021 return;
1023 gc_mark_children(objspace, ptr, lev+1);
1026 void
1027 rb_gc_mark(VALUE ptr)
1029 gc_mark(&rb_objspace, ptr, 0);
1032 static void
1033 gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev)
1035 register RVALUE *obj = RANY(ptr);
1037 goto marking; /* skip */
1039 again:
1040 obj = RANY(ptr);
1041 if (rb_special_const_p(ptr)) return; /* special const not marked */
1042 if (obj->as.basic.flags == 0) return; /* free cell */
1043 if (obj->as.basic.flags & FL_MARK) return; /* already marked */
1044 obj->as.basic.flags |= FL_MARK;
1046 marking:
1047 if (FL_TEST(obj, FL_EXIVAR)) {
1048 rb_mark_generic_ivar(ptr);
1051 switch (obj->as.basic.flags & T_MASK) {
1052 case T_NIL:
1053 case T_FIXNUM:
1054 rb_bug("rb_gc_mark() called for broken object");
1055 break;
1057 case T_NODE:
1058 switch (nd_type(obj)) {
1059 case NODE_IF: /* 1,2,3 */
1060 case NODE_FOR:
1061 case NODE_ITER:
1062 case NODE_WHEN:
1063 case NODE_MASGN:
1064 case NODE_RESCUE:
1065 case NODE_RESBODY:
1066 case NODE_CLASS:
1067 case NODE_BLOCK_PASS:
1068 gc_mark(objspace, (VALUE)obj->as.node.u2.node, lev);
1069 /* fall through */
1070 case NODE_BLOCK: /* 1,3 */
1071 case NODE_OPTBLOCK:
1072 case NODE_ARRAY:
1073 case NODE_DSTR:
1074 case NODE_DXSTR:
1075 case NODE_DREGX:
1076 case NODE_DREGX_ONCE:
1077 case NODE_ENSURE:
1078 case NODE_CALL:
1079 case NODE_DEFS:
1080 case NODE_OP_ASGN1:
1081 case NODE_ARGS:
1082 gc_mark(objspace, (VALUE)obj->as.node.u1.node, lev);
1083 /* fall through */
1084 case NODE_SUPER: /* 3 */
1085 case NODE_FCALL:
1086 case NODE_DEFN:
1087 case NODE_ARGS_AUX:
1088 ptr = (VALUE)obj->as.node.u3.node;
1089 goto again;
1091 case NODE_METHOD: /* 1,2 */
1092 case NODE_WHILE:
1093 case NODE_UNTIL:
1094 case NODE_AND:
1095 case NODE_OR:
1096 case NODE_CASE:
1097 case NODE_SCLASS:
1098 case NODE_DOT2:
1099 case NODE_DOT3:
1100 case NODE_FLIP2:
1101 case NODE_FLIP3:
1102 case NODE_MATCH2:
1103 case NODE_MATCH3:
1104 case NODE_OP_ASGN_OR:
1105 case NODE_OP_ASGN_AND:
1106 case NODE_MODULE:
1107 case NODE_ALIAS:
1108 case NODE_VALIAS:
1109 case NODE_ARGSCAT:
1110 gc_mark(objspace, (VALUE)obj->as.node.u1.node, lev);
1111 /* fall through */
1112 case NODE_FBODY: /* 2 */
1113 case NODE_GASGN:
1114 case NODE_LASGN:
1115 case NODE_DASGN:
1116 case NODE_DASGN_CURR:
1117 case NODE_IASGN:
1118 case NODE_IASGN2:
1119 case NODE_CVASGN:
1120 case NODE_COLON3:
1121 case NODE_OPT_N:
1122 case NODE_EVSTR:
1123 case NODE_UNDEF:
1124 case NODE_POSTEXE:
1125 ptr = (VALUE)obj->as.node.u2.node;
1126 goto again;
1128 case NODE_HASH: /* 1 */
1129 case NODE_LIT:
1130 case NODE_STR:
1131 case NODE_XSTR:
1132 case NODE_DEFINED:
1133 case NODE_MATCH:
1134 case NODE_RETURN:
1135 case NODE_BREAK:
1136 case NODE_NEXT:
1137 case NODE_YIELD:
1138 case NODE_COLON2:
1139 case NODE_SPLAT:
1140 case NODE_TO_ARY:
1141 ptr = (VALUE)obj->as.node.u1.node;
1142 goto again;
1144 case NODE_SCOPE: /* 2,3 */
1145 case NODE_CDECL:
1146 case NODE_OPT_ARG:
1147 gc_mark(objspace, (VALUE)obj->as.node.u3.node, lev);
1148 ptr = (VALUE)obj->as.node.u2.node;
1149 goto again;
1151 case NODE_ZARRAY: /* - */
1152 case NODE_ZSUPER:
1153 case NODE_CFUNC:
1154 case NODE_VCALL:
1155 case NODE_GVAR:
1156 case NODE_LVAR:
1157 case NODE_DVAR:
1158 case NODE_IVAR:
1159 case NODE_CVAR:
1160 case NODE_NTH_REF:
1161 case NODE_BACK_REF:
1162 case NODE_REDO:
1163 case NODE_RETRY:
1164 case NODE_SELF:
1165 case NODE_NIL:
1166 case NODE_TRUE:
1167 case NODE_FALSE:
1168 case NODE_ERRINFO:
1169 case NODE_ATTRSET:
1170 case NODE_BLOCK_ARG:
1171 break;
1172 case NODE_ALLOCA:
1173 mark_locations_array(objspace,
1174 (VALUE*)obj->as.node.u1.value,
1175 obj->as.node.u3.cnt);
1176 ptr = (VALUE)obj->as.node.u2.node;
1177 goto again;
1179 default: /* unlisted NODE */
1180 if (is_pointer_to_heap(objspace, obj->as.node.u1.node)) {
1181 gc_mark(objspace, (VALUE)obj->as.node.u1.node, lev);
1183 if (is_pointer_to_heap(objspace, obj->as.node.u2.node)) {
1184 gc_mark(objspace, (VALUE)obj->as.node.u2.node, lev);
1186 if (is_pointer_to_heap(objspace, obj->as.node.u3.node)) {
1187 gc_mark(objspace, (VALUE)obj->as.node.u3.node, lev);
1190 return; /* no need to mark class. */
1193 gc_mark(objspace, obj->as.basic.klass, lev);
1194 switch (obj->as.basic.flags & T_MASK) {
1195 case T_ICLASS:
1196 case T_CLASS:
1197 case T_MODULE:
1198 mark_tbl(objspace, RCLASS_M_TBL(obj), lev);
1199 mark_tbl(objspace, RCLASS_IV_TBL(obj), lev);
1200 ptr = RCLASS_SUPER(obj);
1201 goto again;
1203 case T_ARRAY:
1204 if (FL_TEST(obj, ELTS_SHARED)) {
1205 ptr = obj->as.array.aux.shared;
1206 goto again;
1208 else {
1209 long i, len = RARRAY_LEN(obj);
1210 VALUE *ptr = RARRAY_PTR(obj);
1211 for (i=0; i < len; i++) {
1212 gc_mark(objspace, *ptr++, lev);
1215 break;
1217 case T_HASH:
1218 mark_hash(objspace, obj->as.hash.ntbl, lev);
1219 ptr = obj->as.hash.ifnone;
1220 goto again;
1222 case T_STRING:
1223 #define STR_ASSOC FL_USER3 /* copied from string.c */
1224 if (FL_TEST(obj, RSTRING_NOEMBED) && FL_ANY(obj, ELTS_SHARED|STR_ASSOC)) {
1225 ptr = obj->as.string.as.heap.aux.shared;
1226 goto again;
1228 break;
1230 case T_DATA:
1231 if (obj->as.data.dmark) (*obj->as.data.dmark)(DATA_PTR(obj));
1232 break;
1234 case T_OBJECT:
1236 long i, len = ROBJECT_NUMIV(obj);
1237 VALUE *ptr = ROBJECT_IVPTR(obj);
1238 for (i = 0; i < len; i++) {
1239 gc_mark(objspace, *ptr++, lev);
1242 break;
1244 case T_FILE:
1245 if (obj->as.file.fptr)
1246 gc_mark(objspace, obj->as.file.fptr->tied_io_for_writing, lev);
1247 break;
1249 case T_REGEXP:
1250 case T_FLOAT:
1251 case T_BIGNUM:
1252 break;
1254 case T_MATCH:
1255 gc_mark(objspace, obj->as.match.regexp, lev);
1256 if (obj->as.match.str) {
1257 ptr = obj->as.match.str;
1258 goto again;
1260 break;
1262 case T_RATIONAL:
1263 gc_mark(objspace, obj->as.rational.num, lev);
1264 gc_mark(objspace, obj->as.rational.den, lev);
1265 break;
1267 case T_COMPLEX:
1268 gc_mark(objspace, obj->as.complex.real, lev);
1269 gc_mark(objspace, obj->as.complex.image, lev);
1270 break;
1272 case T_STRUCT:
1274 long len = RSTRUCT_LEN(obj);
1275 VALUE *ptr = RSTRUCT_PTR(obj);
1277 while (len--) {
1278 gc_mark(objspace, *ptr++, lev);
1281 break;
1283 case T_VALUES:
1285 rb_gc_mark(RVALUES(obj)->v1);
1286 rb_gc_mark(RVALUES(obj)->v2);
1287 ptr = RVALUES(obj)->v3;
1288 goto again;
1290 break;
1292 default:
1293 rb_bug("rb_gc_mark(): unknown data type 0x%lx(%p) %s",
1294 obj->as.basic.flags & T_MASK, obj,
1295 is_pointer_to_heap(objspace, obj) ? "corrupted object" : "non object");
1299 static void obj_free(rb_objspace_t *, VALUE);
1301 static void
1302 finalize_list(rb_objspace_t *objspace, RVALUE *p)
1304 while (p) {
1305 RVALUE *tmp = p->as.free.next;
1306 run_final(objspace, (VALUE)p);
1307 if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */
1308 VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
1309 p->as.free.flags = 0;
1310 p->as.free.next = freelist;
1311 freelist = p;
1313 p = tmp;
1317 static void
1318 free_unused_heaps(rb_objspace_t *objspace)
1320 int i, j;
1321 RVALUE *last = 0;
1323 for (i = j = 1; j < heaps_used; i++) {
1324 if (heaps[i].limit == 0) {
1325 if (!last) {
1326 last = heaps[i].membase;
1328 else {
1329 free(heaps[i].membase);
1331 heaps_used--;
1333 else {
1334 if (i != j) {
1335 heaps[j] = heaps[i];
1337 j++;
1340 if (last) {
1341 if (last < heaps_freed) {
1342 free(heaps_freed);
1343 heaps_freed = last;
1345 else {
1346 free(last);
1351 void rb_gc_abort_threads(void);
1353 static void
1354 gc_sweep(rb_objspace_t *objspace)
1356 RVALUE *p, *pend, *final_list;
1357 int freed = 0;
1358 int i;
1359 unsigned long live = 0, free_min = 0, do_heap_free = 0;
1361 do_heap_free = (heaps_used * HEAP_OBJ_LIMIT) * 0.65;
1362 free_min = (heaps_used * HEAP_OBJ_LIMIT) * 0.2;
1363 if (free_min < FREE_MIN) {
1364 do_heap_free = heaps_used * HEAP_OBJ_LIMIT;
1365 free_min = FREE_MIN;
1368 freelist = 0;
1369 final_list = deferred_final_list;
1370 deferred_final_list = 0;
1371 for (i = 0; i < heaps_used; i++) {
1372 int n = 0;
1373 RVALUE *free = freelist;
1374 RVALUE *final = final_list;
1376 p = heaps[i].slot; pend = p + heaps[i].limit;
1377 while (p < pend) {
1378 if (!(p->as.basic.flags & FL_MARK)) {
1379 if (p->as.basic.flags) {
1380 obj_free(objspace, (VALUE)p);
1382 if (need_call_final && FL_TEST(p, FL_FINALIZE)) {
1383 p->as.free.flags = FL_MARK; /* remain marked */
1384 p->as.free.next = final_list;
1385 final_list = p;
1387 else {
1388 VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
1389 p->as.free.flags = 0;
1390 p->as.free.next = freelist;
1391 freelist = p;
1393 n++;
1395 else if (RBASIC(p)->flags == FL_MARK) {
1396 /* objects to be finalized */
1397 /* do nothing remain marked */
1399 else {
1400 RBASIC(p)->flags &= ~FL_MARK;
1401 live++;
1403 p++;
1405 if (n == heaps[i].limit && freed > do_heap_free) {
1406 RVALUE *pp;
1408 heaps[i].limit = 0;
1409 for (pp = final_list; pp != final; pp = pp->as.free.next) {
1410 p->as.free.flags |= FL_SINGLETON; /* freeing page mark */
1412 freelist = free; /* cancel this page from freelist */
1414 else {
1415 freed += n;
1418 if (malloc_increase > malloc_limit) {
1419 malloc_limit += (malloc_increase - malloc_limit) * (double)live / (live + freed);
1420 if (malloc_limit < GC_MALLOC_LIMIT) malloc_limit = GC_MALLOC_LIMIT;
1422 malloc_increase = 0;
1423 if (freed < free_min) {
1424 set_heaps_increment(objspace);
1425 heaps_increment(objspace);
1427 during_gc = 0;
1429 /* clear finalization list */
1430 if (final_list) {
1431 deferred_final_list = final_list;
1432 return;
1434 free_unused_heaps(objspace);
1437 void
1438 rb_gc_force_recycle(VALUE p)
1440 rb_objspace_t *objspace = &rb_objspace;
1441 VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
1442 RANY(p)->as.free.flags = 0;
1443 RANY(p)->as.free.next = freelist;
1444 freelist = RANY(p);
1447 static void
1448 obj_free(rb_objspace_t *objspace, VALUE obj)
1450 switch (RANY(obj)->as.basic.flags & T_MASK) {
1451 case T_NIL:
1452 case T_FIXNUM:
1453 case T_TRUE:
1454 case T_FALSE:
1455 rb_bug("obj_free() called for broken object");
1456 break;
1459 if (FL_TEST(obj, FL_EXIVAR)) {
1460 rb_free_generic_ivar((VALUE)obj);
1463 switch (RANY(obj)->as.basic.flags & T_MASK) {
1464 case T_OBJECT:
1465 if (!(RANY(obj)->as.basic.flags & ROBJECT_EMBED) &&
1466 RANY(obj)->as.object.as.heap.ivptr) {
1467 RUBY_CRITICAL(free(RANY(obj)->as.object.as.heap.ivptr));
1469 break;
1470 case T_MODULE:
1471 case T_CLASS:
1472 rb_clear_cache_by_class((VALUE)obj);
1473 st_free_table(RCLASS_M_TBL(obj));
1474 if (RCLASS_IV_TBL(obj)) {
1475 st_free_table(RCLASS_IV_TBL(obj));
1477 if (RCLASS_IV_INDEX_TBL(obj)) {
1478 st_free_table(RCLASS_IV_INDEX_TBL(obj));
1480 RUBY_CRITICAL(free(RANY(obj)->as.klass.ptr));
1481 break;
1482 case T_STRING:
1483 rb_str_free(obj);
1484 break;
1485 case T_ARRAY:
1486 rb_ary_free(obj);
1487 break;
1488 case T_HASH:
1489 if (RANY(obj)->as.hash.ntbl) {
1490 st_free_table(RANY(obj)->as.hash.ntbl);
1492 break;
1493 case T_REGEXP:
1494 if (RANY(obj)->as.regexp.ptr) {
1495 onig_free(RANY(obj)->as.regexp.ptr);
1497 if (RANY(obj)->as.regexp.str) {
1498 RUBY_CRITICAL(free(RANY(obj)->as.regexp.str));
1500 break;
1501 case T_DATA:
1502 if (DATA_PTR(obj)) {
1503 if ((long)RANY(obj)->as.data.dfree == -1) {
1504 RUBY_CRITICAL(free(DATA_PTR(obj)));
1506 else if (RANY(obj)->as.data.dfree) {
1507 (*RANY(obj)->as.data.dfree)(DATA_PTR(obj));
1510 break;
1511 case T_MATCH:
1512 if (RANY(obj)->as.match.rmatch) {
1513 struct rmatch *rm = RANY(obj)->as.match.rmatch;
1514 onig_region_free(&rm->regs, 0);
1515 if (rm->char_offset)
1516 RUBY_CRITICAL(free(rm->char_offset));
1517 RUBY_CRITICAL(free(rm));
1519 break;
1520 case T_FILE:
1521 if (RANY(obj)->as.file.fptr) {
1522 rb_io_fptr_finalize(RANY(obj)->as.file.fptr);
1524 break;
1525 case T_RATIONAL:
1526 case T_COMPLEX:
1527 break;
1528 case T_ICLASS:
1529 /* iClass shares table with the module */
1530 break;
1532 case T_FLOAT:
1533 break;
1534 case T_VALUES:
1535 break;
1537 case T_BIGNUM:
1538 if (!(RBASIC(obj)->flags & RBIGNUM_EMBED_FLAG) && RBIGNUM_DIGITS(obj)) {
1539 RUBY_CRITICAL(free(RBIGNUM_DIGITS(obj)));
1541 break;
1542 case T_NODE:
1543 switch (nd_type(obj)) {
1544 case NODE_SCOPE:
1545 if (RANY(obj)->as.node.u1.tbl) {
1546 RUBY_CRITICAL(free(RANY(obj)->as.node.u1.tbl));
1548 break;
1549 case NODE_ALLOCA:
1550 RUBY_CRITICAL(free(RANY(obj)->as.node.u1.node));
1551 break;
1553 return; /* no need to free iv_tbl */
1555 case T_STRUCT:
1556 if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
1557 RANY(obj)->as.rstruct.as.heap.ptr) {
1558 RUBY_CRITICAL(free(RANY(obj)->as.rstruct.as.heap.ptr));
1560 break;
1562 default:
1563 rb_bug("gc_sweep(): unknown data type 0x%lx(%p)",
1564 RANY(obj)->as.basic.flags & T_MASK, (void*)obj);
1568 #ifdef __GNUC__
1569 #if defined(__human68k__) || defined(DJGPP)
1570 #undef rb_setjmp
1571 #undef rb_jmp_buf
1572 #if defined(__human68k__)
1573 typedef unsigned long rb_jmp_buf[8];
1574 __asm__ (".even\n\
1575 _rb_setjmp:\n\
1576 move.l 4(sp),a0\n\
1577 movem.l d3-d7/a3-a5,(a0)\n\
1578 moveq.l #0,d0\n\
1579 rts");
1580 #else
1581 #if defined(DJGPP)
1582 typedef unsigned long rb_jmp_buf[6];
1583 __asm__ (".align 4\n\
1584 _rb_setjmp:\n\
1585 pushl %ebp\n\
1586 movl %esp,%ebp\n\
1587 movl 8(%ebp),%ebp\n\
1588 movl %eax,(%ebp)\n\
1589 movl %ebx,4(%ebp)\n\
1590 movl %ecx,8(%ebp)\n\
1591 movl %edx,12(%ebp)\n\
1592 movl %esi,16(%ebp)\n\
1593 movl %edi,20(%ebp)\n\
1594 popl %ebp\n\
1595 xorl %eax,%eax\n\
1596 ret");
1597 #endif
1598 #endif
1599 int rb_setjmp (rb_jmp_buf);
1600 #endif /* __human68k__ or DJGPP */
1601 #endif /* __GNUC__ */
1603 #define GC_NOTIFY 0
1605 void rb_vm_mark(void *ptr);
1607 static void
1608 mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th)
1610 rb_jmp_buf save_regs_gc_mark;
1611 VALUE *stack_start, *stack_end;
1613 SET_STACK_END;
1614 #if STACK_GROW_DIRECTION < 0
1615 stack_start = th->machine_stack_end;
1616 stack_end = th->machine_stack_start;
1617 #elif STACK_GROW_DIRECTION > 0
1618 stack_start = th->machine_stack_start;
1619 stack_end = th->machine_stack_end + 1;
1620 #else
1621 if (th->machine_stack_end < th->machine_stack_start) {
1622 stack_start = th->machine_stack_end;
1623 stack_end = th->machine_stack_start;
1625 else {
1626 stack_start = th->machine_stack_start;
1627 stack_end = th->machine_stack_end + 1;
1629 #endif
1631 FLUSH_REGISTER_WINDOWS;
1632 /* This assumes that all registers are saved into the jmp_buf (and stack) */
1633 rb_setjmp(save_regs_gc_mark);
1634 mark_locations_array(objspace,
1635 (VALUE*)save_regs_gc_mark,
1636 sizeof(save_regs_gc_mark) / sizeof(VALUE));
1638 rb_gc_mark_locations(stack_start, stack_end);
1639 #ifdef __ia64
1640 rb_gc_mark_locations(th->machine_register_stack_start, th->machine_register_stack_end);
1641 #endif
1642 #if defined(__human68k__) || defined(__mc68000__)
1643 mark_locations_array((VALUE*)((char*)STACK_END + 2),
1644 (STACK_START - STACK_END));
1645 #endif
1648 void rb_gc_mark_encodings(void);
1650 static int
1651 garbage_collect(rb_objspace_t *objspace)
1653 struct gc_list *list;
1654 rb_thread_t *th = GET_THREAD();
1656 if (GC_NOTIFY) printf("start garbage_collect()\n");
1658 if (!heaps) {
1659 return Qfalse;
1662 if (dont_gc || during_gc) {
1663 if (!freelist) {
1664 if (!heaps_increment(objspace)) {
1665 set_heaps_increment(objspace);
1666 heaps_increment(objspace);
1669 return Qtrue;
1671 during_gc++;
1672 objspace->count++;
1674 SET_STACK_END;
1676 init_mark_stack(objspace);
1678 th->vm->self ? rb_gc_mark(th->vm->self) : rb_vm_mark(th->vm);
1680 if (finalizer_table) {
1681 mark_tbl(objspace, finalizer_table, 0);
1684 mark_current_machine_context(objspace, th);
1686 rb_gc_mark_threads();
1687 rb_gc_mark_symbols();
1688 rb_gc_mark_encodings();
1690 /* mark protected global variables */
1691 for (list = global_List; list; list = list->next) {
1692 rb_gc_mark_maybe(*list->varptr);
1694 rb_mark_end_proc();
1695 rb_gc_mark_global_tbl();
1697 mark_tbl(objspace, rb_class_tbl, 0);
1698 rb_gc_mark_trap_list();
1700 /* mark generic instance variables for special constants */
1701 rb_mark_generic_ivar_tbl();
1703 rb_gc_mark_parser();
1705 /* gc_mark objects whose marking are not completed*/
1706 while (!MARK_STACK_EMPTY) {
1707 if (mark_stack_overflow) {
1708 gc_mark_all(objspace);
1710 else {
1711 gc_mark_rest(objspace);
1715 gc_sweep(objspace);
1717 if (GC_NOTIFY) printf("end garbage_collect()\n");
1718 return Qtrue;
1722 rb_garbage_collect(void)
1724 return garbage_collect(&rb_objspace);
1727 void
1728 rb_gc_mark_machine_stack(rb_thread_t *th)
1730 rb_objspace_t *objspace = &rb_objspace;
1731 #if STACK_GROW_DIRECTION < 0
1732 rb_gc_mark_locations(th->machine_stack_end, th->machine_stack_start);
1733 #elif STACK_GROW_DIRECTION > 0
1734 rb_gc_mark_locations(th->machine_stack_start, th->machine_stack_end);
1735 #else
1736 if (th->machine_stack_start < th->machine_stack_end) {
1737 rb_gc_mark_locations(th->machine_stack_start, th->machine_stack_end);
1739 else {
1740 rb_gc_mark_locations(th->machine_stack_end, th->machine_stack_start);
1742 #endif
1743 #ifdef __ia64
1744 rb_gc_mark_locations(th->machine_register_stack_start, th->machine_register_stack_end);
1745 #endif
1750 * call-seq:
1751 * GC.start => nil
1752 * gc.garbage_collect => nil
1753 * ObjectSpace.garbage_collect => nil
1755 * Initiates garbage collection, unless manually disabled.
1759 VALUE
1760 rb_gc_start(void)
1762 rb_gc();
1763 return Qnil;
1766 void
1767 ruby_set_stack_size(size_t size)
1769 rb_gc_stack_maxsize = size;
1772 void
1773 Init_stack(VALUE *addr)
1775 ruby_init_stack(addr);
1778 #undef ruby_init_stack
1779 void
1780 ruby_init_stack(VALUE *addr
1781 #ifdef __ia64
1782 , void *bsp
1783 #endif
1786 if (!rb_gc_stack_start ||
1787 STACK_UPPER(&addr,
1788 rb_gc_stack_start > addr,
1789 rb_gc_stack_start < addr)) {
1790 rb_gc_stack_start = addr;
1792 #ifdef __ia64
1793 if (!rb_gc_register_stack_start ||
1794 (VALUE*)bsp < rb_gc_register_stack_start) {
1795 rb_gc_register_stack_start = (VALUE*)bsp;
1797 #endif
1798 #ifdef HAVE_GETRLIMIT
1800 struct rlimit rlim;
1802 if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
1803 unsigned int space = rlim.rlim_cur/5;
1805 if (space > 1024*1024) space = 1024*1024;
1806 rb_gc_stack_maxsize = rlim.rlim_cur - space;
1809 #elif defined _WIN32
1811 MEMORY_BASIC_INFORMATION mi;
1812 DWORD size;
1813 DWORD space;
1815 if (VirtualQuery(&mi, &mi, sizeof(mi))) {
1816 size = (char *)mi.BaseAddress - (char *)mi.AllocationBase;
1817 space = size / 5;
1818 if (space > 1024*1024) space = 1024*1024;
1819 rb_gc_stack_maxsize = size - space;
1822 #endif
1826 * Document-class: ObjectSpace
1828 * The <code>ObjectSpace</code> module contains a number of routines
1829 * that interact with the garbage collection facility and allow you to
1830 * traverse all living objects with an iterator.
1832 * <code>ObjectSpace</code> also provides support for object
1833 * finalizers, procs that will be called when a specific object is
1834 * about to be destroyed by garbage collection.
1836 * include ObjectSpace
1839 * a = "A"
1840 * b = "B"
1841 * c = "C"
1844 * define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
1845 * define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" })
1846 * define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" })
1848 * <em>produces:</em>
1850 * Finalizer three on 537763470
1851 * Finalizer one on 537763480
1852 * Finalizer two on 537763480
1856 void
1857 Init_heap(void)
1859 if (!rb_gc_stack_start) {
1860 Init_stack(0);
1862 init_heap(&rb_objspace);
1865 static VALUE
1866 os_obj_of(rb_objspace_t *objspace, VALUE of)
1868 int i;
1869 int n = 0;
1871 for (i = 0; i < heaps_used; i++) {
1872 RVALUE *p, *pend;
1874 p = heaps[i].slot; pend = p + heaps[i].limit;
1875 for (;p < pend; p++) {
1876 if (p->as.basic.flags) {
1877 switch (BUILTIN_TYPE(p)) {
1878 case T_NONE:
1879 case T_ICLASS:
1880 case T_NODE:
1881 case T_VALUES:
1882 continue;
1883 case T_CLASS:
1884 if (FL_TEST(p, FL_SINGLETON)) continue;
1885 default:
1886 if (!p->as.basic.klass) continue;
1887 if (!of || rb_obj_is_kind_of((VALUE)p, of)) {
1888 rb_yield((VALUE)p);
1889 n++;
1896 return INT2FIX(n);
1900 * call-seq:
1901 * ObjectSpace.each_object([module]) {|obj| ... } => fixnum
1903 * Calls the block once for each living, nonimmediate object in this
1904 * Ruby process. If <i>module</i> is specified, calls the block
1905 * for only those classes or modules that match (or are a subclass of)
1906 * <i>module</i>. Returns the number of objects found. Immediate
1907 * objects (<code>Fixnum</code>s, <code>Symbol</code>s
1908 * <code>true</code>, <code>false</code>, and <code>nil</code>) are
1909 * never returned. In the example below, <code>each_object</code>
1910 * returns both the numbers we defined and several constants defined in
1911 * the <code>Math</code> module.
1913 * a = 102.7
1914 * b = 95 # Won't be returned
1915 * c = 12345678987654321
1916 * count = ObjectSpace.each_object(Numeric) {|x| p x }
1917 * puts "Total count: #{count}"
1919 * <em>produces:</em>
1921 * 12345678987654321
1922 * 102.7
1923 * 2.71828182845905
1924 * 3.14159265358979
1925 * 2.22044604925031e-16
1926 * 1.7976931348623157e+308
1927 * 2.2250738585072e-308
1928 * Total count: 7
1932 static VALUE
1933 os_each_obj(int argc, VALUE *argv, VALUE os)
1935 VALUE of;
1937 rb_secure(4);
1938 if (argc == 0) {
1939 of = 0;
1941 else {
1942 rb_scan_args(argc, argv, "01", &of);
1944 RETURN_ENUMERATOR(os, 1, &of);
1945 return os_obj_of(&rb_objspace, of);
1949 * call-seq:
1950 * ObjectSpace.undefine_finalizer(obj)
1952 * Removes all finalizers for <i>obj</i>.
1956 static VALUE
1957 undefine_final(VALUE os, VALUE obj)
1959 rb_objspace_t *objspace = &rb_objspace;
1960 if (finalizer_table) {
1961 st_delete(finalizer_table, (st_data_t*)&obj, 0);
1963 return obj;
1967 * call-seq:
1968 * ObjectSpace.define_finalizer(obj, aProc=proc())
1970 * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
1971 * was destroyed.
1975 static VALUE
1976 define_final(int argc, VALUE *argv, VALUE os)
1978 rb_objspace_t *objspace = &rb_objspace;
1979 VALUE obj, block, table;
1981 rb_scan_args(argc, argv, "11", &obj, &block);
1982 if (argc == 1) {
1983 block = rb_block_proc();
1985 else if (!rb_respond_to(block, rb_intern("call"))) {
1986 rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
1987 rb_obj_classname(block));
1989 need_call_final = 1;
1990 FL_SET(obj, FL_FINALIZE);
1992 block = rb_ary_new3(2, INT2FIX(rb_safe_level()), block);
1994 if (!finalizer_table) {
1995 finalizer_table = st_init_numtable();
1997 if (st_lookup(finalizer_table, obj, &table)) {
1998 rb_ary_push(table, block);
2000 else {
2001 st_add_direct(finalizer_table, obj, rb_ary_new3(1, block));
2003 return block;
2006 void
2007 rb_gc_copy_finalizer(VALUE dest, VALUE obj)
2009 rb_objspace_t *objspace = &rb_objspace;
2010 VALUE table;
2012 if (!finalizer_table) return;
2013 if (!FL_TEST(obj, FL_FINALIZE)) return;
2014 if (st_lookup(finalizer_table, obj, &table)) {
2015 st_insert(finalizer_table, dest, table);
2017 FL_SET(dest, FL_FINALIZE);
2020 static VALUE
2021 run_single_final(VALUE arg)
2023 VALUE *args = (VALUE *)arg;
2024 rb_eval_cmd(args[0], args[1], (int)args[2]);
2025 return Qnil;
2028 static void
2029 run_final(rb_objspace_t *objspace, VALUE obj)
2031 long i;
2032 int status, critical_save = rb_thread_critical;
2033 VALUE args[3], table, objid;
2035 objid = rb_obj_id(obj); /* make obj into id */
2036 rb_thread_critical = Qtrue;
2037 args[1] = 0;
2038 args[2] = (VALUE)rb_safe_level();
2039 if (finalizer_table && st_delete(finalizer_table, (st_data_t*)&obj, &table)) {
2040 if (!args[1] && RARRAY_LEN(table) > 0) {
2041 args[1] = rb_obj_freeze(rb_ary_new3(1, objid));
2043 for (i=0; i<RARRAY_LEN(table); i++) {
2044 VALUE final = RARRAY_PTR(table)[i];
2045 args[0] = RARRAY_PTR(final)[1];
2046 args[2] = FIX2INT(RARRAY_PTR(final)[0]);
2047 rb_protect(run_single_final, (VALUE)args, &status);
2050 rb_thread_critical = critical_save;
2053 static void
2054 gc_finalize_deferred(rb_objspace_t *objspace)
2056 RVALUE *p = deferred_final_list;
2058 during_gc++;
2059 deferred_final_list = 0;
2060 if (p) {
2061 finalize_list(objspace, p);
2063 free_unused_heaps(objspace);
2064 during_gc = 0;
2067 void
2068 rb_gc_finalize_deferred(void)
2070 gc_finalize_deferred(&rb_objspace);
2073 void
2074 rb_gc_call_finalizer_at_exit(void)
2076 rb_objspace_t *objspace = &rb_objspace;
2077 RVALUE *p, *pend;
2078 int i;
2080 /* finalizers are part of garbage collection */
2081 during_gc++;
2082 /* run finalizers */
2083 if (need_call_final) {
2084 p = deferred_final_list;
2085 deferred_final_list = 0;
2086 finalize_list(objspace, p);
2087 for (i = 0; i < heaps_used; i++) {
2088 p = heaps[i].slot; pend = p + heaps[i].limit;
2089 while (p < pend) {
2090 if (FL_TEST(p, FL_FINALIZE)) {
2091 FL_UNSET(p, FL_FINALIZE);
2092 p->as.basic.klass = 0;
2093 run_final(objspace, (VALUE)p);
2095 p++;
2099 /* run data object's finalizers */
2100 for (i = 0; i < heaps_used; i++) {
2101 p = heaps[i].slot; pend = p + heaps[i].limit;
2102 while (p < pend) {
2103 if (BUILTIN_TYPE(p) == T_DATA &&
2104 DATA_PTR(p) && RANY(p)->as.data.dfree &&
2105 RANY(p)->as.basic.klass != rb_cThread) {
2106 p->as.free.flags = 0;
2107 if ((long)RANY(p)->as.data.dfree == -1) {
2108 RUBY_CRITICAL(free(DATA_PTR(p)));
2110 else if (RANY(p)->as.data.dfree) {
2111 (*RANY(p)->as.data.dfree)(DATA_PTR(p));
2113 VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
2115 else if (BUILTIN_TYPE(p) == T_FILE) {
2116 if (rb_io_fptr_finalize(RANY(p)->as.file.fptr)) {
2117 p->as.free.flags = 0;
2118 VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
2121 p++;
2124 during_gc = 0;
2127 void
2128 rb_gc(void)
2130 rb_objspace_t *objspace = &rb_objspace;
2131 garbage_collect(objspace);
2132 gc_finalize_deferred(objspace);
2136 * call-seq:
2137 * ObjectSpace._id2ref(object_id) -> an_object
2139 * Converts an object id to a reference to the object. May not be
2140 * called on an object id passed as a parameter to a finalizer.
2142 * s = "I am a string" #=> "I am a string"
2143 * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
2144 * r == s #=> true
2148 static VALUE
2149 id2ref(VALUE obj, VALUE objid)
2151 #if SIZEOF_LONG == SIZEOF_VOIDP
2152 #define NUM2PTR(x) NUM2ULONG(x)
2153 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2154 #define NUM2PTR(x) NUM2ULL(x)
2155 #endif
2156 rb_objspace_t *objspace = &rb_objspace;
2157 VALUE ptr;
2158 void *p0;
2160 rb_secure(4);
2161 ptr = NUM2PTR(objid);
2162 p0 = (void *)ptr;
2164 if (ptr == Qtrue) return Qtrue;
2165 if (ptr == Qfalse) return Qfalse;
2166 if (ptr == Qnil) return Qnil;
2167 if (FIXNUM_P(ptr)) return (VALUE)ptr;
2168 ptr = objid ^ FIXNUM_FLAG; /* unset FIXNUM_FLAG */
2170 if ((ptr % sizeof(RVALUE)) == (4 << 2)) {
2171 ID symid = ptr / sizeof(RVALUE);
2172 if (rb_id2name(symid) == 0)
2173 rb_raise(rb_eRangeError, "%p is not symbol id value", p0);
2174 return ID2SYM(symid);
2177 if (!is_pointer_to_heap(objspace, (void *)ptr) ||
2178 BUILTIN_TYPE(ptr) >= T_VALUES || BUILTIN_TYPE(ptr) == T_ICLASS) {
2179 rb_raise(rb_eRangeError, "%p is not id value", p0);
2181 if (BUILTIN_TYPE(ptr) == 0 || RBASIC(ptr)->klass == 0) {
2182 rb_raise(rb_eRangeError, "%p is recycled object", p0);
2184 return (VALUE)ptr;
2188 * Document-method: __id__
2189 * Document-method: object_id
2191 * call-seq:
2192 * obj.__id__ => fixnum
2193 * obj.object_id => fixnum
2195 * Returns an integer identifier for <i>obj</i>. The same number will
2196 * be returned on all calls to <code>id</code> for a given object, and
2197 * no two active objects will share an id.
2198 * <code>Object#object_id</code> is a different concept from the
2199 * <code>:name</code> notation, which returns the symbol id of
2200 * <code>name</code>. Replaces the deprecated <code>Object#id</code>.
2204 * call-seq:
2205 * obj.hash => fixnum
2207 * Generates a <code>Fixnum</code> hash value for this object. This
2208 * function must have the property that <code>a.eql?(b)</code> implies
2209 * <code>a.hash == b.hash</code>. The hash value is used by class
2210 * <code>Hash</code>. Any hash value that exceeds the capacity of a
2211 * <code>Fixnum</code> will be truncated before being used.
2214 VALUE
2215 rb_obj_id(VALUE obj)
2218 * 32-bit VALUE space
2219 * MSB ------------------------ LSB
2220 * false 00000000000000000000000000000000
2221 * true 00000000000000000000000000000010
2222 * nil 00000000000000000000000000000100
2223 * undef 00000000000000000000000000000110
2224 * symbol ssssssssssssssssssssssss00001110
2225 * object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
2226 * fixnum fffffffffffffffffffffffffffffff1
2228 * object_id space
2229 * LSB
2230 * false 00000000000000000000000000000000
2231 * true 00000000000000000000000000000010
2232 * nil 00000000000000000000000000000100
2233 * undef 00000000000000000000000000000110
2234 * symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
2235 * object oooooooooooooooooooooooooooooo0 o...o % A = 0
2236 * fixnum fffffffffffffffffffffffffffffff1 bignum if required
2238 * where A = sizeof(RVALUE)/4
2240 * sizeof(RVALUE) is
2241 * 20 if 32-bit, double is 4-byte aligned
2242 * 24 if 32-bit, double is 8-byte aligned
2243 * 40 if 64-bit
2245 if (TYPE(obj) == T_SYMBOL) {
2246 return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
2248 if (SPECIAL_CONST_P(obj)) {
2249 return LONG2NUM((SIGNED_VALUE)obj);
2251 return (VALUE)((SIGNED_VALUE)obj|FIXNUM_FLAG);
2255 * call-seq:
2256 * ObjectSpace.count_objects([result_hash]) -> hash
2258 * Counts objects for each type.
2260 * It returns a hash as:
2261 * {:TOTAL=>10000, :FREE=>3011, :T_OBJECT=>6, :T_CLASS=>404, ...}
2263 * If the optional argument, result_hash, is given,
2264 * it is overwritten and returned.
2265 * This is intended to avoid probe effect.
2267 * The contents of the returned hash is implementation defined.
2268 * It may be changed in future.
2270 * This method is not expected to work except C Ruby.
2274 static VALUE
2275 count_objects(int argc, VALUE *argv, VALUE os)
2277 rb_objspace_t *objspace = &rb_objspace;
2278 long counts[T_MASK+1];
2279 long freed = 0;
2280 long total = 0;
2281 int i;
2282 VALUE hash;
2284 if (rb_scan_args(argc, argv, "01", &hash) == 1) {
2285 if (TYPE(hash) != T_HASH)
2286 rb_raise(rb_eTypeError, "non-hash given");
2289 for (i = 0; i <= T_MASK; i++) {
2290 counts[i] = 0;
2293 for (i = 0; i < heaps_used; i++) {
2294 RVALUE *p, *pend;
2296 p = heaps[i].slot; pend = p + heaps[i].limit;
2297 for (;p < pend; p++) {
2298 if (p->as.basic.flags) {
2299 counts[BUILTIN_TYPE(p)]++;
2301 else {
2302 freed++;
2305 total += heaps[i].limit;
2308 if (hash == Qnil)
2309 hash = rb_hash_new();
2310 rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), LONG2NUM(total));
2311 rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), LONG2NUM(freed));
2312 for (i = 0; i <= T_MASK; i++) {
2313 VALUE type;
2314 switch (i) {
2315 #define COUNT_TYPE(t) case t: type = ID2SYM(rb_intern(#t)); break;
2316 COUNT_TYPE(T_NONE);
2317 COUNT_TYPE(T_OBJECT);
2318 COUNT_TYPE(T_CLASS);
2319 COUNT_TYPE(T_MODULE);
2320 COUNT_TYPE(T_FLOAT);
2321 COUNT_TYPE(T_STRING);
2322 COUNT_TYPE(T_REGEXP);
2323 COUNT_TYPE(T_ARRAY);
2324 COUNT_TYPE(T_HASH);
2325 COUNT_TYPE(T_STRUCT);
2326 COUNT_TYPE(T_BIGNUM);
2327 COUNT_TYPE(T_FILE);
2328 COUNT_TYPE(T_DATA);
2329 COUNT_TYPE(T_MATCH);
2330 COUNT_TYPE(T_COMPLEX);
2331 COUNT_TYPE(T_RATIONAL);
2332 COUNT_TYPE(T_NIL);
2333 COUNT_TYPE(T_TRUE);
2334 COUNT_TYPE(T_FALSE);
2335 COUNT_TYPE(T_SYMBOL);
2336 COUNT_TYPE(T_FIXNUM);
2337 COUNT_TYPE(T_VALUES);
2338 COUNT_TYPE(T_UNDEF);
2339 COUNT_TYPE(T_NODE);
2340 COUNT_TYPE(T_ICLASS);
2341 #undef COUNT_TYPE
2342 default: type = INT2NUM(i); break;
2344 if (counts[i])
2345 rb_hash_aset(hash, type, LONG2NUM(counts[i]));
2348 return hash;
2352 * call-seq:
2353 * GC.count -> Integer
2355 * Counts objects for each type.
2357 * It returns a number of GC invoke counts.
2361 static VALUE
2362 gc_count(VALUE self)
2364 return UINT2NUM((&rb_objspace)->count);
2368 * The <code>GC</code> module provides an interface to Ruby's mark and
2369 * sweep garbage collection mechanism. Some of the underlying methods
2370 * are also available via the <code>ObjectSpace</code> module.
2373 void
2374 Init_GC(void)
2376 VALUE rb_mObSpace;
2378 rb_mGC = rb_define_module("GC");
2379 rb_define_singleton_method(rb_mGC, "start", rb_gc_start, 0);
2380 rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
2381 rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
2382 rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
2383 rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1);
2384 rb_define_singleton_method(rb_mGC, "count", gc_count, 0);
2385 rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0);
2387 rb_mObSpace = rb_define_module("ObjectSpace");
2388 rb_define_module_function(rb_mObSpace, "each_object", os_each_obj, -1);
2389 rb_define_module_function(rb_mObSpace, "garbage_collect", rb_gc_start, 0);
2391 rb_define_module_function(rb_mObSpace, "define_finalizer", define_final, -1);
2392 rb_define_module_function(rb_mObSpace, "undefine_finalizer", undefine_final, 1);
2394 rb_define_module_function(rb_mObSpace, "_id2ref", id2ref, 1);
2396 rb_global_variable(&nomem_error);
2397 nomem_error = rb_exc_new2(rb_eNoMemError, "failed to allocate memory");
2399 rb_define_method(rb_mKernel, "hash", rb_obj_id, 0);
2400 rb_define_method(rb_mKernel, "__id__", rb_obj_id, 0);
2401 rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
2403 rb_define_module_function(rb_mObSpace, "count_objects", count_objects, -1);