1 /**********************************************************************
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"
17 #include "ruby/node.h"
20 #include "ruby/util.h"
21 #include "eval_intern.h"
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_TIME_H
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
36 #if defined _WIN32 || defined __CYGWIN__
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)
45 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
46 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE(p, n)
49 # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
50 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
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. */
62 # define alloca __builtin_alloca
72 # ifndef alloca /* predefined by HP cc +Olibcalls */
76 # endif /* HAVE_ALLOCA_H */
79 #ifndef GC_MALLOC_LIMIT
80 #if defined(MSDOS) || defined(__human68k__)
81 #define GC_MALLOC_LIMIT 200000
83 #define GC_MALLOC_LIMIT 8000000
87 static VALUE nomem_error
;
89 #define MARK_STACK_MAX 1024
91 int ruby_gc_debug_indent
= 0;
95 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
96 #pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
99 typedef struct RVALUE
{
102 VALUE flags
; /* always 0 for freed obj */
106 struct RObject object
;
108 struct RFloat flonum
;
109 struct RString string
;
111 struct RRegexp regexp
;
114 struct RStruct rstruct
;
115 struct RBignum bignum
;
119 struct RRational rational
;
120 struct RComplex
complex;
128 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
138 #define HEAP_MIN_SLOTS 10000
139 #define FREE_MIN 4096
143 struct gc_list
*next
;
146 typedef struct rb_objspace
{
149 unsigned long increase
;
154 struct heaps_slot
*ptr
;
171 VALUE buffer
[MARK_STACK_MAX
];
175 struct gc_list
*global_list
;
179 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
180 #define rb_objspace (*GET_VM()->objspace)
182 static rb_objspace_t rb_objspace
= {{GC_MALLOC_LIMIT
}, {HEAP_MIN_SLOTS
}};
184 #define malloc_limit objspace->params.limit
185 #define malloc_increase objspace->params.increase
186 #define heap_slots objspace->heap.slots
187 #define heaps objspace->heap.ptr
188 #define heaps_length objspace->heap.length
189 #define heaps_used objspace->heap.used
190 #define freelist objspace->heap.freelist
191 #define lomem objspace->heap.range[0]
192 #define himem objspace->heap.range[1]
193 #define objects_delta objspace->heap.delta
194 #define heaps_inc objspace->heap.increment
195 #define heaps_freed objspace->heap.freed
196 #define dont_gc objspace->flags.dont_gc
197 #define during_gc objspace->flags.during_gc
198 #define need_call_final objspace->final.need_call
199 #define finalizer_table objspace->final.table
200 #define deferred_final_list objspace->final.deferred
201 #define mark_stack objspace->markstack.buffer
202 #define mark_stack_ptr objspace->markstack.ptr
203 #define mark_stack_overflow objspace->markstack.overflow
204 #define global_List objspace->global_list
207 rb_objspace_alloc(void)
209 rb_objspace_t
*objspace
= malloc(sizeof(rb_objspace_t
));
210 memset(objspace
, 0, sizeof(*objspace
));
211 malloc_limit
= GC_MALLOC_LIMIT
;
212 objects_delta
= HEAP_MIN_SLOTS
;
219 /*#define HEAP_SIZE 0x8000 */
221 /*#define HEAP_SIZE 0x20000 */
223 /*#define HEAP_SIZE 0x10000 */
225 #define HEAP_SIZE 0x4000
227 /*#define HEAP_SIZE 0x2000 */
229 /*#define HEAP_SIZE 0x1000 */
231 /*#define HEAP_SIZE 0x800 */
233 #define HEAP_OBJ_LIMIT (HEAP_SIZE / sizeof(struct RVALUE))
234 #define FREE_MIN 4096
236 extern st_table
*rb_class_tbl
;
237 VALUE
*rb_gc_stack_start
= 0;
239 VALUE
*rb_gc_register_stack_start
= 0;
242 int ruby_gc_stress
= 0;
246 /* set stack size (http://www.delorie.com/djgpp/v2faq/faq15_9.html) */
247 unsigned int _stklen
= 0x180000; /* 1.5 kB */
250 #if defined(DJGPP) || defined(_WIN32_WCE)
251 size_t rb_gc_stack_maxsize
= 65535*sizeof(VALUE
);
253 size_t rb_gc_stack_maxsize
= 655300*sizeof(VALUE
);
256 static void run_final(rb_objspace_t
*objspace
, VALUE obj
);
257 static int garbage_collect(rb_objspace_t
*objspace
);
260 rb_global_variable(VALUE
*var
)
262 rb_gc_register_address(var
);
268 rb_thread_t
*th
= GET_THREAD();
270 (rb_thread_raised_p(th
, RAISED_NOMEMORY
) && rb_safe_level() < 4)) {
271 fprintf(stderr
, "[FATAL] failed to allocate memory\n");
274 rb_thread_raised_set(th
, RAISED_NOMEMORY
);
275 rb_exc_raise(nomem_error
);
280 * GC.stress => true or false
282 * returns current status of GC stress mode.
286 gc_stress_get(VALUE self
)
288 return ruby_gc_stress
? Qtrue
: Qfalse
;
293 * GC.stress = bool => bool
295 * updates GC stress mode.
297 * When GC.stress = true, GC is invoked for all GC opportunity:
298 * all memory and object allocation.
300 * Since it makes Ruby very slow, it is only for debugging.
304 gc_stress_set(VALUE self
, VALUE
bool)
307 ruby_gc_stress
= RTEST(bool);
312 ruby_vm_xmalloc(rb_objspace_t
*objspace
, size_t size
)
317 rb_raise(rb_eNoMemError
, "negative allocation size (or too big)");
319 if (size
== 0) size
= 1;
320 malloc_increase
+= size
;
322 if (ruby_gc_stress
|| malloc_increase
> malloc_limit
) {
323 garbage_collect(objspace
);
325 RUBY_CRITICAL(mem
= malloc(size
));
327 if (garbage_collect(objspace
)) {
328 RUBY_CRITICAL(mem
= malloc(size
));
339 ruby_xmalloc(size_t size
)
341 return ruby_vm_xmalloc(&rb_objspace
, size
);
345 ruby_vm_xmalloc2(rb_objspace_t
*objspace
, size_t n
, size_t size
)
347 size_t len
= size
* n
;
348 if (n
!= 0 && size
!= len
/ n
) {
349 rb_raise(rb_eArgError
, "malloc: possible integer overflow");
351 return ruby_vm_xmalloc(objspace
, len
);
355 ruby_xmalloc2(size_t n
, size_t size
)
357 return ruby_vm_xmalloc2(&rb_objspace
, n
, size
);
361 ruby_vm_xcalloc(rb_objspace_t
*objspace
, size_t n
, size_t size
)
365 mem
= ruby_vm_xmalloc2(objspace
, n
, size
);
366 memset(mem
, 0, n
* size
);
372 ruby_xcalloc(size_t n
, size_t size
)
374 return ruby_vm_xcalloc(&rb_objspace
, n
, size
);
378 ruby_vm_xrealloc(rb_objspace_t
*objspace
, void *ptr
, size_t size
)
383 rb_raise(rb_eArgError
, "negative re-allocation size");
385 if (!ptr
) return ruby_xmalloc(size
);
386 if (size
== 0) size
= 1;
387 malloc_increase
+= size
;
388 if (ruby_gc_stress
) garbage_collect(objspace
);
389 RUBY_CRITICAL(mem
= realloc(ptr
, size
));
391 if (garbage_collect(objspace
)) {
392 RUBY_CRITICAL(mem
= realloc(ptr
, size
));
403 ruby_xrealloc(void *ptr
, size_t size
)
405 return ruby_vm_xrealloc(&rb_objspace
, ptr
, size
);
409 ruby_vm_xrealloc2(rb_objspace_t
*objspace
, void *ptr
, size_t n
, size_t size
)
411 size_t len
= size
* n
;
412 if (n
!= 0 && size
!= len
/ n
) {
413 rb_raise(rb_eArgError
, "realloc: possible integer overflow");
415 return ruby_vm_xrealloc(objspace
, ptr
, len
);
419 ruby_xrealloc2(void *ptr
, size_t n
, size_t size
)
421 return ruby_vm_xrealloc2(&rb_objspace
, ptr
, n
, size
);
428 RUBY_CRITICAL(free(x
));
434 * GC.enable => true or false
436 * Enables garbage collection, returning <code>true</code> if garbage
437 * collection was previously disabled.
439 * GC.disable #=> false
441 * GC.enable #=> false
448 rb_objspace_t
*objspace
= &rb_objspace
;
457 * GC.disable => true or false
459 * Disables garbage collection, returning <code>true</code> if garbage
460 * collection was already disabled.
462 * GC.disable #=> false
463 * GC.disable #=> true
470 rb_objspace_t
*objspace
= &rb_objspace
;
480 rb_gc_register_address(VALUE
*addr
)
482 rb_objspace_t
*objspace
= &rb_objspace
;
485 tmp
= ALLOC(struct gc_list
);
486 tmp
->next
= global_List
;
492 rb_register_mark_object(VALUE obj
)
494 VALUE ary
= GET_THREAD()->vm
->mark_object_ary
;
495 rb_ary_push(ary
, obj
);
499 rb_gc_unregister_address(VALUE
*addr
)
501 rb_objspace_t
*objspace
= &rb_objspace
;
502 struct gc_list
*tmp
= global_List
;
504 if (tmp
->varptr
== addr
) {
505 global_List
= tmp
->next
;
506 RUBY_CRITICAL(free(tmp
));
510 if (tmp
->next
->varptr
== addr
) {
511 struct gc_list
*t
= tmp
->next
;
513 tmp
->next
= tmp
->next
->next
;
514 RUBY_CRITICAL(free(t
));
523 allocate_heaps(rb_objspace_t
*objspace
)
525 struct heaps_slot
*p
;
528 heaps_length
+= objects_delta
/ HEAP_OBJ_LIMIT
;
529 length
= heaps_length
*sizeof(struct heaps_slot
);
531 if (heaps_used
> 0) {
532 p
= (struct heaps_slot
*)realloc(heaps
, length
);
536 p
= heaps
= (struct heaps_slot
*)malloc(length
);
539 if (p
== 0) rb_memerror();
543 assign_heap_slot(rb_objspace_t
*objspace
)
545 RVALUE
*p
, *pend
, *membase
;
549 objs
= HEAP_OBJ_LIMIT
;
550 RUBY_CRITICAL(p
= (RVALUE
*)malloc(HEAP_SIZE
));
558 membase
= heaps
[mid
].membase
;
562 else if (membase
> p
) {
566 rb_bug("same heap slot is allocated: %p at %ld", p
, mid
);
572 if ((VALUE
)p
% sizeof(RVALUE
) != 0) {
573 p
= (RVALUE
*)((VALUE
)p
+ sizeof(RVALUE
) - ((VALUE
)p
% sizeof(RVALUE
)));
574 if ((membase
+ HEAP_SIZE
) < (p
+ HEAP_SIZE
)) {
580 if (hi
< heaps_used
) {
581 MEMMOVE(&heaps
[hi
+1], &heaps
[hi
], struct heaps_slot
, heaps_used
- hi
);
583 heaps
[hi
].membase
= membase
;
585 heaps
[hi
].limit
= objs
;
587 if (lomem
== 0 || lomem
> p
) lomem
= p
;
588 if (himem
< pend
) himem
= pend
;
592 p
->as
.free
.flags
= 0;
593 p
->as
.free
.next
= freelist
;
600 add_heap(rb_objspace_t
*objspace
)
604 add
= objects_delta
/ HEAP_OBJ_LIMIT
;
605 objects_delta
*= 1.8;
607 if ((heaps_used
+ add
) > heaps_length
) {
608 allocate_heaps(objspace
);
611 for (i
= 0; i
< add
; i
++) {
612 assign_heap_slot(objspace
);
619 set_heaps_increment(rb_objspace_t
*objspace
)
621 heaps_inc
+= objects_delta
/ HEAP_OBJ_LIMIT
;
622 objects_delta
*= 1.8;
624 if ((heaps_used
+ heaps_inc
) > heaps_length
) {
625 allocate_heaps(objspace
);
630 heaps_increment(rb_objspace_t
*objspace
)
633 assign_heap_slot(objspace
);
640 #define RANY(o) ((RVALUE*)(o))
643 rb_newobj_from_heap(rb_objspace_t
*objspace
)
647 if (ruby_gc_stress
|| !freelist
) {
648 if (!heaps_increment(objspace
) && !garbage_collect(objspace
)) {
653 obj
= (VALUE
)freelist
;
654 freelist
= freelist
->as
.free
.next
;
656 MEMZERO((void*)obj
, RVALUE
, 1);
658 RANY(obj
)->file
= rb_sourcefile();
659 RANY(obj
)->line
= rb_sourceline();
667 rb_fill_value_cache(rb_thread_t
*th
)
669 rb_objspace_t
*objspace
= &rb_objspace
;
674 for (i
=0; i
<RUBY_VM_VALUE_CACHE_SIZE
; i
++) {
675 VALUE v
= rb_newobj_from_heap(objspace
);
677 th
->value_cache
[i
] = v
;
678 RBASIC(v
)->flags
= FL_MARK
;
680 th
->value_cache_ptr
= &th
->value_cache
[0];
681 rv
= rb_newobj_from_heap(objspace
);
691 rb_thread_t
*th
= GET_THREAD();
692 VALUE v
= *th
->value_cache_ptr
;
693 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
694 rb_objspace_t
*objspace
= th
->vm
->objspace
;
696 rb_objspace_t
*objspace
= &rb_objspace
;
700 RBASIC(v
)->flags
= 0;
701 th
->value_cache_ptr
++;
704 v
= rb_fill_value_cache(th
);
707 #if defined(GC_DEBUG)
708 printf("cache index: %d, v: %p, th: %p\n",
709 th
->value_cache_ptr
- th
->value_cache
, v
, th
);
713 rb_objspace_t
*objspace
= &rb_objspace
;
714 return rb_newobj_from_heap(objspace
);
719 rb_node_newnode(enum node_type type
, VALUE a0
, VALUE a1
, VALUE a2
)
721 NODE
*n
= (NODE
*)rb_newobj();
724 nd_set_type(n
, type
);
734 rb_data_object_alloc(VALUE klass
, void *datap
, RUBY_DATA_FUNC dmark
, RUBY_DATA_FUNC dfree
)
736 NEWOBJ(data
, struct RData
);
737 if (klass
) Check_Type(klass
, T_CLASS
);
738 OBJSETUP(data
, klass
, T_DATA
);
747 #define SET_STACK_END (SET_MACHINE_STACK_END(&th->machine_stack_end), th->machine_register_stack_end = rb_ia64_bsp())
749 #define SET_STACK_END SET_MACHINE_STACK_END(&th->machine_stack_end)
752 #define STACK_START (th->machine_stack_start)
753 #define STACK_END (th->machine_stack_end)
754 #define STACK_LEVEL_MAX (th->machine_stack_maxsize/sizeof(VALUE))
756 #if STACK_GROW_DIRECTION < 0
757 # define STACK_LENGTH (STACK_START - STACK_END)
758 #elif STACK_GROW_DIRECTION > 0
759 # define STACK_LENGTH (STACK_END - STACK_START + 1)
761 # define STACK_LENGTH ((STACK_END < STACK_START) ? STACK_START - STACK_END\
762 : STACK_END - STACK_START + 1)
764 #if STACK_GROW_DIRECTION > 0
765 # define STACK_UPPER(x, a, b) a
766 #elif STACK_GROW_DIRECTION < 0
767 # define STACK_UPPER(x, a, b) b
769 static int grow_direction
;
771 stack_grow_direction(VALUE
*addr
)
773 rb_thread_t
*th
= GET_THREAD();
776 if (STACK_END
> addr
) return grow_direction
= 1;
777 return grow_direction
= -1;
779 # define stack_growup_p(x) ((grow_direction ? grow_direction : stack_grow_direction(x)) > 0)
780 # define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b)
783 #define GC_WATER_MARK 512
786 ruby_stack_length(VALUE
**p
)
788 rb_thread_t
*th
= GET_THREAD();
790 if (p
) *p
= STACK_UPPER(STACK_END
, STACK_START
, STACK_END
);
795 ruby_stack_check(void)
798 rb_thread_t
*th
= GET_THREAD();
800 ret
= STACK_LENGTH
> STACK_LEVEL_MAX
+ GC_WATER_MARK
;
803 ret
= (VALUE
*)rb_ia64_bsp() - th
->machine_register_stack_start
>
804 th
->machine_register_stack_maxsize
/sizeof(VALUE
) + GC_WATER_MARK
;
811 init_mark_stack(rb_objspace_t
*objspace
)
813 mark_stack_overflow
= 0;
814 mark_stack_ptr
= mark_stack
;
817 #define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
819 static void gc_mark(rb_objspace_t
*objspace
, VALUE ptr
, int lev
);
820 static void gc_mark_children(rb_objspace_t
*objspace
, VALUE ptr
, int lev
);
823 gc_mark_all(rb_objspace_t
*objspace
)
828 init_mark_stack(objspace
);
829 for (i
= 0; i
< heaps_used
; i
++) {
830 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
832 if ((p
->as
.basic
.flags
& FL_MARK
) &&
833 (p
->as
.basic
.flags
!= FL_MARK
)) {
834 gc_mark_children(objspace
, (VALUE
)p
, 0);
842 gc_mark_rest(rb_objspace_t
*objspace
)
844 VALUE tmp_arry
[MARK_STACK_MAX
];
847 p
= (mark_stack_ptr
- mark_stack
) + tmp_arry
;
848 MEMCPY(tmp_arry
, mark_stack
, VALUE
, p
- tmp_arry
);
850 init_mark_stack(objspace
);
851 while (p
!= tmp_arry
) {
853 gc_mark_children(objspace
, *p
, 0);
858 is_pointer_to_heap(rb_objspace_t
*objspace
, void *ptr
)
860 register RVALUE
*p
= RANY(ptr
);
861 register struct heaps_slot
*heap
;
862 register long hi
, lo
, mid
;
864 if (p
< lomem
|| p
> himem
) return Qfalse
;
865 if ((VALUE
)p
% sizeof(RVALUE
) != 0) return Qfalse
;
867 /* check if p looks like a pointer using bsearch*/
873 if (heap
->slot
<= p
) {
874 if (p
< heap
->slot
+ heap
->limit
)
886 mark_locations_array(rb_objspace_t
*objspace
, register VALUE
*x
, register long n
)
891 VALGRIND_MAKE_MEM_DEFINED(&v
, sizeof(v
));
892 if (is_pointer_to_heap(objspace
, (void *)v
)) {
893 gc_mark(objspace
, v
, 0);
900 gc_mark_locations(rb_objspace_t
*objspace
, VALUE
*start
, VALUE
*end
)
904 if (end
<= start
) return;
906 mark_locations_array(&rb_objspace
, start
,n
);
910 rb_gc_mark_locations(VALUE
*start
, VALUE
*end
)
912 gc_mark_locations(&rb_objspace
, start
, end
);
915 #define rb_gc_mark_locations(start, end) gc_mark_locations(objspace, start, end)
917 struct mark_tbl_arg
{
918 rb_objspace_t
*objspace
;
923 mark_entry(ID key
, VALUE value
, st_data_t data
)
925 struct mark_tbl_arg
*arg
= (void*)data
;
926 gc_mark(arg
->objspace
, value
, arg
->lev
);
931 mark_tbl(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
933 struct mark_tbl_arg arg
;
935 arg
.objspace
= objspace
;
937 st_foreach(tbl
, mark_entry
, (st_data_t
)&arg
);
941 rb_mark_tbl(st_table
*tbl
)
943 mark_tbl(&rb_objspace
, tbl
, 0);
947 mark_key(VALUE key
, VALUE value
, st_data_t data
)
949 struct mark_tbl_arg
*arg
= (void*)data
;
950 gc_mark(arg
->objspace
, key
, arg
->lev
);
955 mark_set(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
957 struct mark_tbl_arg arg
;
959 arg
.objspace
= objspace
;
961 st_foreach(tbl
, mark_key
, (st_data_t
)&arg
);
965 rb_mark_set(st_table
*tbl
)
967 mark_set(&rb_objspace
, tbl
, 0);
971 mark_keyvalue(VALUE key
, VALUE value
, st_data_t data
)
973 struct mark_tbl_arg
*arg
= (void*)data
;
974 gc_mark(arg
->objspace
, key
, arg
->lev
);
975 gc_mark(arg
->objspace
, value
, arg
->lev
);
980 mark_hash(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
982 struct mark_tbl_arg arg
;
984 arg
.objspace
= objspace
;
986 st_foreach(tbl
, mark_keyvalue
, (st_data_t
)&arg
);
990 rb_mark_hash(st_table
*tbl
)
992 mark_hash(&rb_objspace
, tbl
, 0);
996 rb_gc_mark_maybe(VALUE obj
)
998 if (is_pointer_to_heap(&rb_objspace
, (void *)obj
)) {
999 gc_mark(&rb_objspace
, obj
, 0);
1003 #define GC_LEVEL_MAX 250
1006 gc_mark(rb_objspace_t
*objspace
, VALUE ptr
, int lev
)
1008 register RVALUE
*obj
;
1011 if (rb_special_const_p(ptr
)) return; /* special const not marked */
1012 if (obj
->as
.basic
.flags
== 0) return; /* free cell */
1013 if (obj
->as
.basic
.flags
& FL_MARK
) return; /* already marked */
1014 obj
->as
.basic
.flags
|= FL_MARK
;
1016 if (lev
> GC_LEVEL_MAX
|| (lev
== 0 && ruby_stack_check())) {
1017 if (!mark_stack_overflow
) {
1018 if (mark_stack_ptr
- mark_stack
< MARK_STACK_MAX
) {
1019 *mark_stack_ptr
= ptr
;
1023 mark_stack_overflow
= 1;
1028 gc_mark_children(objspace
, ptr
, lev
+1);
1032 rb_gc_mark(VALUE ptr
)
1034 gc_mark(&rb_objspace
, ptr
, 0);
1038 gc_mark_children(rb_objspace_t
*objspace
, VALUE ptr
, int lev
)
1040 register RVALUE
*obj
= RANY(ptr
);
1042 goto marking
; /* skip */
1046 if (rb_special_const_p(ptr
)) return; /* special const not marked */
1047 if (obj
->as
.basic
.flags
== 0) return; /* free cell */
1048 if (obj
->as
.basic
.flags
& FL_MARK
) return; /* already marked */
1049 obj
->as
.basic
.flags
|= FL_MARK
;
1052 if (FL_TEST(obj
, FL_EXIVAR
)) {
1053 rb_mark_generic_ivar(ptr
);
1056 switch (obj
->as
.basic
.flags
& T_MASK
) {
1059 rb_bug("rb_gc_mark() called for broken object");
1063 switch (nd_type(obj
)) {
1064 case NODE_IF
: /* 1,2,3 */
1072 case NODE_BLOCK_PASS
:
1073 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u2
.node
, lev
);
1075 case NODE_BLOCK
: /* 1,3 */
1081 case NODE_DREGX_ONCE
:
1087 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u1
.node
, lev
);
1089 case NODE_SUPER
: /* 3 */
1093 ptr
= (VALUE
)obj
->as
.node
.u3
.node
;
1096 case NODE_METHOD
: /* 1,2 */
1109 case NODE_OP_ASGN_OR
:
1110 case NODE_OP_ASGN_AND
:
1115 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u1
.node
, lev
);
1117 case NODE_FBODY
: /* 2 */
1121 case NODE_DASGN_CURR
:
1130 ptr
= (VALUE
)obj
->as
.node
.u2
.node
;
1133 case NODE_HASH
: /* 1 */
1146 ptr
= (VALUE
)obj
->as
.node
.u1
.node
;
1149 case NODE_SCOPE
: /* 2,3 */
1152 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u3
.node
, lev
);
1153 ptr
= (VALUE
)obj
->as
.node
.u2
.node
;
1156 case NODE_ZARRAY
: /* - */
1175 case NODE_BLOCK_ARG
:
1178 mark_locations_array(objspace
,
1179 (VALUE
*)obj
->as
.node
.u1
.value
,
1180 obj
->as
.node
.u3
.cnt
);
1181 ptr
= (VALUE
)obj
->as
.node
.u2
.node
;
1184 default: /* unlisted NODE */
1185 if (is_pointer_to_heap(objspace
, obj
->as
.node
.u1
.node
)) {
1186 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u1
.node
, lev
);
1188 if (is_pointer_to_heap(objspace
, obj
->as
.node
.u2
.node
)) {
1189 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u2
.node
, lev
);
1191 if (is_pointer_to_heap(objspace
, obj
->as
.node
.u3
.node
)) {
1192 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u3
.node
, lev
);
1195 return; /* no need to mark class. */
1198 gc_mark(objspace
, obj
->as
.basic
.klass
, lev
);
1199 switch (obj
->as
.basic
.flags
& T_MASK
) {
1203 mark_tbl(objspace
, RCLASS_M_TBL(obj
), lev
);
1204 mark_tbl(objspace
, RCLASS_IV_TBL(obj
), lev
);
1205 ptr
= RCLASS_SUPER(obj
);
1209 if (FL_TEST(obj
, ELTS_SHARED
)) {
1210 ptr
= obj
->as
.array
.aux
.shared
;
1214 long i
, len
= RARRAY_LEN(obj
);
1215 VALUE
*ptr
= RARRAY_PTR(obj
);
1216 for (i
=0; i
< len
; i
++) {
1217 gc_mark(objspace
, *ptr
++, lev
);
1223 mark_hash(objspace
, obj
->as
.hash
.ntbl
, lev
);
1224 ptr
= obj
->as
.hash
.ifnone
;
1228 #define STR_ASSOC FL_USER3 /* copied from string.c */
1229 if (FL_TEST(obj
, RSTRING_NOEMBED
) && FL_ANY(obj
, ELTS_SHARED
|STR_ASSOC
)) {
1230 ptr
= obj
->as
.string
.as
.heap
.aux
.shared
;
1236 if (obj
->as
.data
.dmark
) (*obj
->as
.data
.dmark
)(DATA_PTR(obj
));
1241 long i
, len
= ROBJECT_NUMIV(obj
);
1242 VALUE
*ptr
= ROBJECT_IVPTR(obj
);
1243 for (i
= 0; i
< len
; i
++) {
1244 gc_mark(objspace
, *ptr
++, lev
);
1250 if (obj
->as
.file
.fptr
)
1251 gc_mark(objspace
, obj
->as
.file
.fptr
->tied_io_for_writing
, lev
);
1260 gc_mark(objspace
, obj
->as
.match
.regexp
, lev
);
1261 if (obj
->as
.match
.str
) {
1262 ptr
= obj
->as
.match
.str
;
1268 gc_mark(objspace
, obj
->as
.rational
.num
, lev
);
1269 gc_mark(objspace
, obj
->as
.rational
.den
, lev
);
1273 gc_mark(objspace
, obj
->as
.complex.real
, lev
);
1274 gc_mark(objspace
, obj
->as
.complex.image
, lev
);
1279 long len
= RSTRUCT_LEN(obj
);
1280 VALUE
*ptr
= RSTRUCT_PTR(obj
);
1283 gc_mark(objspace
, *ptr
++, lev
);
1290 rb_gc_mark(RVALUES(obj
)->v1
);
1291 rb_gc_mark(RVALUES(obj
)->v2
);
1292 ptr
= RVALUES(obj
)->v3
;
1298 rb_bug("rb_gc_mark(): unknown data type 0x%lx(%p) %s",
1299 obj
->as
.basic
.flags
& T_MASK
, obj
,
1300 is_pointer_to_heap(objspace
, obj
) ? "corrupted object" : "non object");
1304 static void obj_free(rb_objspace_t
*, VALUE
);
1307 finalize_list(rb_objspace_t
*objspace
, RVALUE
*p
)
1310 RVALUE
*tmp
= p
->as
.free
.next
;
1311 run_final(objspace
, (VALUE
)p
);
1312 if (!FL_TEST(p
, FL_SINGLETON
)) { /* not freeing page */
1313 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
1314 p
->as
.free
.flags
= 0;
1315 p
->as
.free
.next
= freelist
;
1323 free_unused_heaps(rb_objspace_t
*objspace
)
1328 for (i
= j
= 1; j
< heaps_used
; i
++) {
1329 if (heaps
[i
].limit
== 0) {
1331 last
= heaps
[i
].membase
;
1334 free(heaps
[i
].membase
);
1340 heaps
[j
] = heaps
[i
];
1346 if (last
< heaps_freed
) {
1355 objects_delta
= heaps_used
* HEAP_OBJ_LIMIT
;
1359 void rb_gc_abort_threads(void);
1362 gc_sweep(rb_objspace_t
*objspace
)
1364 RVALUE
*p
, *pend
, *final_list
;
1367 unsigned long live
= 0, free_min
= 0, do_heap_free
= 0;
1369 do_heap_free
= (heaps_used
* HEAP_OBJ_LIMIT
) * 0.65;
1370 free_min
= (heaps_used
* HEAP_OBJ_LIMIT
) * 0.2;
1371 if (free_min
< FREE_MIN
) {
1372 do_heap_free
= heaps_used
* HEAP_OBJ_LIMIT
;
1373 free_min
= FREE_MIN
;
1377 final_list
= deferred_final_list
;
1378 deferred_final_list
= 0;
1379 for (i
= 0; i
< heaps_used
; i
++) {
1381 RVALUE
*free
= freelist
;
1382 RVALUE
*final
= final_list
;
1384 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
1386 if (!(p
->as
.basic
.flags
& FL_MARK
)) {
1387 if (p
->as
.basic
.flags
) {
1388 obj_free(objspace
, (VALUE
)p
);
1390 if (need_call_final
&& FL_TEST(p
, FL_FINALIZE
)) {
1391 p
->as
.free
.flags
= FL_MARK
; /* remain marked */
1392 p
->as
.free
.next
= final_list
;
1396 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
1397 p
->as
.free
.flags
= 0;
1398 p
->as
.free
.next
= freelist
;
1403 else if (RBASIC(p
)->flags
== FL_MARK
) {
1404 /* objects to be finalized */
1405 /* do nothing remain marked */
1408 RBASIC(p
)->flags
&= ~FL_MARK
;
1413 if (n
== heaps
[i
].limit
&& freed
> do_heap_free
) {
1417 for (pp
= final_list
; pp
!= final
; pp
= pp
->as
.free
.next
) {
1418 p
->as
.free
.flags
|= FL_SINGLETON
; /* freeing page mark */
1420 freelist
= free
; /* cancel this page from freelist */
1426 if (malloc_increase
> malloc_limit
) {
1427 malloc_limit
+= (malloc_increase
- malloc_limit
) * (double)live
/ (live
+ freed
);
1428 if (malloc_limit
< GC_MALLOC_LIMIT
) malloc_limit
= GC_MALLOC_LIMIT
;
1430 malloc_increase
= 0;
1431 if (freed
< free_min
) {
1432 set_heaps_increment(objspace
);
1433 heaps_increment(objspace
);
1437 /* clear finalization list */
1439 deferred_final_list
= final_list
;
1442 free_unused_heaps(objspace
);
1446 rb_gc_force_recycle(VALUE p
)
1448 rb_objspace_t
*objspace
= &rb_objspace
;
1449 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
1450 RANY(p
)->as
.free
.flags
= 0;
1451 RANY(p
)->as
.free
.next
= freelist
;
1456 obj_free(rb_objspace_t
*objspace
, VALUE obj
)
1458 switch (RANY(obj
)->as
.basic
.flags
& T_MASK
) {
1463 rb_bug("obj_free() called for broken object");
1467 if (FL_TEST(obj
, FL_EXIVAR
)) {
1468 rb_free_generic_ivar((VALUE
)obj
);
1471 switch (RANY(obj
)->as
.basic
.flags
& T_MASK
) {
1473 if (!(RANY(obj
)->as
.basic
.flags
& ROBJECT_EMBED
) &&
1474 RANY(obj
)->as
.object
.as
.heap
.ivptr
) {
1475 RUBY_CRITICAL(free(RANY(obj
)->as
.object
.as
.heap
.ivptr
));
1480 rb_clear_cache_by_class((VALUE
)obj
);
1481 st_free_table(RCLASS_M_TBL(obj
));
1482 if (RCLASS_IV_TBL(obj
)) {
1483 st_free_table(RCLASS_IV_TBL(obj
));
1485 if (RCLASS_IV_INDEX_TBL(obj
)) {
1486 st_free_table(RCLASS_IV_INDEX_TBL(obj
));
1488 RUBY_CRITICAL(free(RANY(obj
)->as
.klass
.ptr
));
1497 if (RANY(obj
)->as
.hash
.ntbl
) {
1498 st_free_table(RANY(obj
)->as
.hash
.ntbl
);
1502 if (RANY(obj
)->as
.regexp
.ptr
) {
1503 onig_free(RANY(obj
)->as
.regexp
.ptr
);
1505 if (RANY(obj
)->as
.regexp
.str
) {
1506 RUBY_CRITICAL(free(RANY(obj
)->as
.regexp
.str
));
1510 if (DATA_PTR(obj
)) {
1511 if ((long)RANY(obj
)->as
.data
.dfree
== -1) {
1512 RUBY_CRITICAL(free(DATA_PTR(obj
)));
1514 else if (RANY(obj
)->as
.data
.dfree
) {
1515 (*RANY(obj
)->as
.data
.dfree
)(DATA_PTR(obj
));
1520 if (RANY(obj
)->as
.match
.rmatch
) {
1521 struct rmatch
*rm
= RANY(obj
)->as
.match
.rmatch
;
1522 onig_region_free(&rm
->regs
, 0);
1523 if (rm
->char_offset
)
1524 RUBY_CRITICAL(free(rm
->char_offset
));
1525 RUBY_CRITICAL(free(rm
));
1529 if (RANY(obj
)->as
.file
.fptr
) {
1530 rb_io_fptr_finalize(RANY(obj
)->as
.file
.fptr
);
1537 /* iClass shares table with the module */
1546 if (!(RBASIC(obj
)->flags
& RBIGNUM_EMBED_FLAG
) && RBIGNUM_DIGITS(obj
)) {
1547 RUBY_CRITICAL(free(RBIGNUM_DIGITS(obj
)));
1551 switch (nd_type(obj
)) {
1553 if (RANY(obj
)->as
.node
.u1
.tbl
) {
1554 RUBY_CRITICAL(free(RANY(obj
)->as
.node
.u1
.tbl
));
1558 RUBY_CRITICAL(free(RANY(obj
)->as
.node
.u1
.node
));
1561 return; /* no need to free iv_tbl */
1564 if ((RBASIC(obj
)->flags
& RSTRUCT_EMBED_LEN_MASK
) == 0 &&
1565 RANY(obj
)->as
.rstruct
.as
.heap
.ptr
) {
1566 RUBY_CRITICAL(free(RANY(obj
)->as
.rstruct
.as
.heap
.ptr
));
1571 rb_bug("gc_sweep(): unknown data type 0x%lx(%p)",
1572 RANY(obj
)->as
.basic
.flags
& T_MASK
, (void*)obj
);
1577 #if defined(__human68k__) || defined(DJGPP)
1580 #if defined(__human68k__)
1581 typedef unsigned long rb_jmp_buf
[8];
1585 movem.l d3-d7/a3-a5,(a0)\n\
1590 typedef unsigned long rb_jmp_buf
[6];
1591 __asm__ (".align 4\n\
1595 movl 8(%ebp),%ebp\n\
1597 movl %ebx,4(%ebp)\n\
1598 movl %ecx,8(%ebp)\n\
1599 movl %edx,12(%ebp)\n\
1600 movl %esi,16(%ebp)\n\
1601 movl %edi,20(%ebp)\n\
1607 int rb_setjmp (rb_jmp_buf
);
1608 #endif /* __human68k__ or DJGPP */
1609 #endif /* __GNUC__ */
1613 void rb_vm_mark(void *ptr
);
1616 mark_current_machine_context(rb_objspace_t
*objspace
, rb_thread_t
*th
)
1618 rb_jmp_buf save_regs_gc_mark
;
1619 VALUE
*stack_start
, *stack_end
;
1622 #if STACK_GROW_DIRECTION < 0
1623 stack_start
= th
->machine_stack_end
;
1624 stack_end
= th
->machine_stack_start
;
1625 #elif STACK_GROW_DIRECTION > 0
1626 stack_start
= th
->machine_stack_start
;
1627 stack_end
= th
->machine_stack_end
+ 1;
1629 if (th
->machine_stack_end
< th
->machine_stack_start
) {
1630 stack_start
= th
->machine_stack_end
;
1631 stack_end
= th
->machine_stack_start
;
1634 stack_start
= th
->machine_stack_start
;
1635 stack_end
= th
->machine_stack_end
+ 1;
1639 FLUSH_REGISTER_WINDOWS
;
1640 /* This assumes that all registers are saved into the jmp_buf (and stack) */
1641 rb_setjmp(save_regs_gc_mark
);
1642 mark_locations_array(objspace
,
1643 (VALUE
*)save_regs_gc_mark
,
1644 sizeof(save_regs_gc_mark
) / sizeof(VALUE
));
1646 rb_gc_mark_locations(stack_start
, stack_end
);
1648 rb_gc_mark_locations(th
->machine_register_stack_start
, th
->machine_register_stack_end
);
1650 #if defined(__human68k__) || defined(__mc68000__)
1651 mark_locations_array((VALUE
*)((char*)STACK_END
+ 2),
1652 (STACK_START
- STACK_END
));
1656 void rb_gc_mark_encodings(void);
1659 garbage_collect(rb_objspace_t
*objspace
)
1661 struct gc_list
*list
;
1662 rb_thread_t
*th
= GET_THREAD();
1664 if (GC_NOTIFY
) printf("start garbage_collect()\n");
1670 if (dont_gc
|| during_gc
) {
1681 init_mark_stack(objspace
);
1683 th
->vm
->self
? rb_gc_mark(th
->vm
->self
) : rb_vm_mark(th
->vm
);
1685 if (finalizer_table
) {
1686 mark_tbl(objspace
, finalizer_table
, 0);
1689 mark_current_machine_context(objspace
, th
);
1691 rb_gc_mark_threads();
1692 rb_gc_mark_symbols();
1693 rb_gc_mark_encodings();
1695 /* mark protected global variables */
1696 for (list
= global_List
; list
; list
= list
->next
) {
1697 rb_gc_mark_maybe(*list
->varptr
);
1700 rb_gc_mark_global_tbl();
1702 mark_tbl(objspace
, rb_class_tbl
, 0);
1703 rb_gc_mark_trap_list();
1705 /* mark generic instance variables for special constants */
1706 rb_mark_generic_ivar_tbl();
1708 rb_gc_mark_parser();
1710 /* gc_mark objects whose marking are not completed*/
1711 while (!MARK_STACK_EMPTY
) {
1712 if (mark_stack_overflow
) {
1713 gc_mark_all(objspace
);
1716 gc_mark_rest(objspace
);
1722 if (GC_NOTIFY
) printf("end garbage_collect()\n");
1727 rb_garbage_collect(void)
1729 return garbage_collect(&rb_objspace
);
1733 rb_gc_mark_machine_stack(rb_thread_t
*th
)
1735 rb_objspace_t
*objspace
= &rb_objspace
;
1736 #if STACK_GROW_DIRECTION < 0
1737 rb_gc_mark_locations(th
->machine_stack_end
, th
->machine_stack_start
);
1738 #elif STACK_GROW_DIRECTION > 0
1739 rb_gc_mark_locations(th
->machine_stack_start
, th
->machine_stack_end
);
1741 if (th
->machine_stack_start
< th
->machine_stack_end
) {
1742 rb_gc_mark_locations(th
->machine_stack_start
, th
->machine_stack_end
);
1745 rb_gc_mark_locations(th
->machine_stack_end
, th
->machine_stack_start
);
1749 rb_gc_mark_locations(th
->machine_register_stack_start
, th
->machine_register_stack_end
);
1757 * gc.garbage_collect => nil
1758 * ObjectSpace.garbage_collect => nil
1760 * Initiates garbage collection, unless manually disabled.
1772 ruby_set_stack_size(size_t size
)
1774 rb_gc_stack_maxsize
= size
;
1778 Init_stack(VALUE
*addr
)
1780 ruby_init_stack(addr
);
1783 #undef ruby_init_stack
1785 ruby_init_stack(VALUE
*addr
1791 if (!rb_gc_stack_start
||
1793 rb_gc_stack_start
> addr
,
1794 rb_gc_stack_start
< addr
)) {
1795 rb_gc_stack_start
= addr
;
1798 if (!rb_gc_register_stack_start
||
1799 (VALUE
*)bsp
< rb_gc_register_stack_start
) {
1800 rb_gc_register_stack_start
= (VALUE
*)bsp
;
1803 #ifdef HAVE_GETRLIMIT
1807 if (getrlimit(RLIMIT_STACK
, &rlim
) == 0) {
1808 unsigned int space
= rlim
.rlim_cur
/5;
1810 if (space
> 1024*1024) space
= 1024*1024;
1811 rb_gc_stack_maxsize
= rlim
.rlim_cur
- space
;
1814 #elif defined _WIN32
1816 MEMORY_BASIC_INFORMATION mi
;
1820 if (VirtualQuery(&mi
, &mi
, sizeof(mi
))) {
1821 size
= (char *)mi
.BaseAddress
- (char *)mi
.AllocationBase
;
1823 if (space
> 1024*1024) space
= 1024*1024;
1824 rb_gc_stack_maxsize
= size
- space
;
1831 * Document-class: ObjectSpace
1833 * The <code>ObjectSpace</code> module contains a number of routines
1834 * that interact with the garbage collection facility and allow you to
1835 * traverse all living objects with an iterator.
1837 * <code>ObjectSpace</code> also provides support for object
1838 * finalizers, procs that will be called when a specific object is
1839 * about to be destroyed by garbage collection.
1841 * include ObjectSpace
1849 * define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
1850 * define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" })
1851 * define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" })
1853 * <em>produces:</em>
1855 * Finalizer three on 537763470
1856 * Finalizer one on 537763480
1857 * Finalizer two on 537763480
1864 if (!rb_gc_stack_start
) {
1867 add_heap(&rb_objspace
);
1871 os_obj_of(rb_objspace_t
*objspace
, VALUE of
)
1876 for (i
= 0; i
< heaps_used
; i
++) {
1879 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
1880 for (;p
< pend
; p
++) {
1881 if (p
->as
.basic
.flags
) {
1882 switch (BUILTIN_TYPE(p
)) {
1889 if (FL_TEST(p
, FL_SINGLETON
)) continue;
1891 if (!p
->as
.basic
.klass
) continue;
1892 if (!of
|| rb_obj_is_kind_of((VALUE
)p
, of
)) {
1906 * ObjectSpace.each_object([module]) {|obj| ... } => fixnum
1908 * Calls the block once for each living, nonimmediate object in this
1909 * Ruby process. If <i>module</i> is specified, calls the block
1910 * for only those classes or modules that match (or are a subclass of)
1911 * <i>module</i>. Returns the number of objects found. Immediate
1912 * objects (<code>Fixnum</code>s, <code>Symbol</code>s
1913 * <code>true</code>, <code>false</code>, and <code>nil</code>) are
1914 * never returned. In the example below, <code>each_object</code>
1915 * returns both the numbers we defined and several constants defined in
1916 * the <code>Math</code> module.
1919 * b = 95 # Won't be returned
1920 * c = 12345678987654321
1921 * count = ObjectSpace.each_object(Numeric) {|x| p x }
1922 * puts "Total count: #{count}"
1924 * <em>produces:</em>
1930 * 2.22044604925031e-16
1931 * 1.7976931348623157e+308
1932 * 2.2250738585072e-308
1938 os_each_obj(int argc
, VALUE
*argv
, VALUE os
)
1947 rb_scan_args(argc
, argv
, "01", &of
);
1949 RETURN_ENUMERATOR(os
, 1, &of
);
1950 return os_obj_of(&rb_objspace
, of
);
1955 * ObjectSpace.undefine_finalizer(obj)
1957 * Removes all finalizers for <i>obj</i>.
1962 undefine_final(VALUE os
, VALUE obj
)
1964 rb_objspace_t
*objspace
= &rb_objspace
;
1965 if (finalizer_table
) {
1966 st_delete(finalizer_table
, (st_data_t
*)&obj
, 0);
1973 * ObjectSpace.define_finalizer(obj, aProc=proc())
1975 * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
1981 define_final(int argc
, VALUE
*argv
, VALUE os
)
1983 rb_objspace_t
*objspace
= &rb_objspace
;
1984 VALUE obj
, block
, table
;
1986 rb_scan_args(argc
, argv
, "11", &obj
, &block
);
1988 block
= rb_block_proc();
1990 else if (!rb_respond_to(block
, rb_intern("call"))) {
1991 rb_raise(rb_eArgError
, "wrong type argument %s (should be callable)",
1992 rb_obj_classname(block
));
1994 need_call_final
= 1;
1995 FL_SET(obj
, FL_FINALIZE
);
1997 block
= rb_ary_new3(2, INT2FIX(rb_safe_level()), block
);
1999 if (!finalizer_table
) {
2000 finalizer_table
= st_init_numtable();
2002 if (st_lookup(finalizer_table
, obj
, &table
)) {
2003 rb_ary_push(table
, block
);
2006 st_add_direct(finalizer_table
, obj
, rb_ary_new3(1, block
));
2012 rb_gc_copy_finalizer(VALUE dest
, VALUE obj
)
2014 rb_objspace_t
*objspace
= &rb_objspace
;
2017 if (!finalizer_table
) return;
2018 if (!FL_TEST(obj
, FL_FINALIZE
)) return;
2019 if (st_lookup(finalizer_table
, obj
, &table
)) {
2020 st_insert(finalizer_table
, dest
, table
);
2022 FL_SET(dest
, FL_FINALIZE
);
2026 run_single_final(VALUE arg
)
2028 VALUE
*args
= (VALUE
*)arg
;
2029 rb_eval_cmd(args
[0], args
[1], (int)args
[2]);
2034 run_final(rb_objspace_t
*objspace
, VALUE obj
)
2037 int status
, critical_save
= rb_thread_critical
;
2038 VALUE args
[3], table
, objid
;
2040 objid
= rb_obj_id(obj
); /* make obj into id */
2041 rb_thread_critical
= Qtrue
;
2043 args
[2] = (VALUE
)rb_safe_level();
2044 if (finalizer_table
&& st_delete(finalizer_table
, (st_data_t
*)&obj
, &table
)) {
2045 if (!args
[1] && RARRAY_LEN(table
) > 0) {
2046 args
[1] = rb_obj_freeze(rb_ary_new3(1, objid
));
2048 for (i
=0; i
<RARRAY_LEN(table
); i
++) {
2049 VALUE final
= RARRAY_PTR(table
)[i
];
2050 args
[0] = RARRAY_PTR(final
)[1];
2051 args
[2] = FIX2INT(RARRAY_PTR(final
)[0]);
2052 rb_protect(run_single_final
, (VALUE
)args
, &status
);
2055 rb_thread_critical
= critical_save
;
2059 gc_finalize_deferred(rb_objspace_t
*objspace
)
2061 RVALUE
*p
= deferred_final_list
;
2064 deferred_final_list
= 0;
2066 finalize_list(objspace
, p
);
2068 free_unused_heaps(objspace
);
2073 rb_gc_finalize_deferred(void)
2075 gc_finalize_deferred(&rb_objspace
);
2079 rb_gc_call_finalizer_at_exit(void)
2081 rb_objspace_t
*objspace
= &rb_objspace
;
2085 /* finalizers are part of garbage collection */
2087 /* run finalizers */
2088 if (need_call_final
) {
2089 p
= deferred_final_list
;
2090 deferred_final_list
= 0;
2091 finalize_list(objspace
, p
);
2092 for (i
= 0; i
< heaps_used
; i
++) {
2093 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
2095 if (FL_TEST(p
, FL_FINALIZE
)) {
2096 FL_UNSET(p
, FL_FINALIZE
);
2097 p
->as
.basic
.klass
= 0;
2098 run_final(objspace
, (VALUE
)p
);
2104 /* run data object's finalizers */
2105 for (i
= 0; i
< heaps_used
; i
++) {
2106 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
2108 if (BUILTIN_TYPE(p
) == T_DATA
&&
2109 DATA_PTR(p
) && RANY(p
)->as
.data
.dfree
&&
2110 RANY(p
)->as
.basic
.klass
!= rb_cThread
) {
2111 p
->as
.free
.flags
= 0;
2112 if ((long)RANY(p
)->as
.data
.dfree
== -1) {
2113 RUBY_CRITICAL(free(DATA_PTR(p
)));
2115 else if (RANY(p
)->as
.data
.dfree
) {
2116 (*RANY(p
)->as
.data
.dfree
)(DATA_PTR(p
));
2118 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
2120 else if (BUILTIN_TYPE(p
) == T_FILE
) {
2121 if (rb_io_fptr_finalize(RANY(p
)->as
.file
.fptr
)) {
2122 p
->as
.free
.flags
= 0;
2123 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
2135 rb_objspace_t
*objspace
= &rb_objspace
;
2136 garbage_collect(objspace
);
2137 gc_finalize_deferred(objspace
);
2142 * ObjectSpace._id2ref(object_id) -> an_object
2144 * Converts an object id to a reference to the object. May not be
2145 * called on an object id passed as a parameter to a finalizer.
2147 * s = "I am a string" #=> "I am a string"
2148 * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
2154 id2ref(VALUE obj
, VALUE objid
)
2156 #if SIZEOF_LONG == SIZEOF_VOIDP
2157 #define NUM2PTR(x) NUM2ULONG(x)
2158 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2159 #define NUM2PTR(x) NUM2ULL(x)
2161 rb_objspace_t
*objspace
= &rb_objspace
;
2166 ptr
= NUM2PTR(objid
);
2169 if (ptr
== Qtrue
) return Qtrue
;
2170 if (ptr
== Qfalse
) return Qfalse
;
2171 if (ptr
== Qnil
) return Qnil
;
2172 if (FIXNUM_P(ptr
)) return (VALUE
)ptr
;
2173 ptr
= objid
^ FIXNUM_FLAG
; /* unset FIXNUM_FLAG */
2175 if ((ptr
% sizeof(RVALUE
)) == (4 << 2)) {
2176 ID symid
= ptr
/ sizeof(RVALUE
);
2177 if (rb_id2name(symid
) == 0)
2178 rb_raise(rb_eRangeError
, "%p is not symbol id value", p0
);
2179 return ID2SYM(symid
);
2182 if (!is_pointer_to_heap(objspace
, (void *)ptr
) ||
2183 BUILTIN_TYPE(ptr
) >= T_VALUES
|| BUILTIN_TYPE(ptr
) == T_ICLASS
) {
2184 rb_raise(rb_eRangeError
, "%p is not id value", p0
);
2186 if (BUILTIN_TYPE(ptr
) == 0 || RBASIC(ptr
)->klass
== 0) {
2187 rb_raise(rb_eRangeError
, "%p is recycled object", p0
);
2193 * Document-method: __id__
2194 * Document-method: object_id
2197 * obj.__id__ => fixnum
2198 * obj.object_id => fixnum
2200 * Returns an integer identifier for <i>obj</i>. The same number will
2201 * be returned on all calls to <code>id</code> for a given object, and
2202 * no two active objects will share an id.
2203 * <code>Object#object_id</code> is a different concept from the
2204 * <code>:name</code> notation, which returns the symbol id of
2205 * <code>name</code>. Replaces the deprecated <code>Object#id</code>.
2210 * obj.hash => fixnum
2212 * Generates a <code>Fixnum</code> hash value for this object. This
2213 * function must have the property that <code>a.eql?(b)</code> implies
2214 * <code>a.hash == b.hash</code>. The hash value is used by class
2215 * <code>Hash</code>. Any hash value that exceeds the capacity of a
2216 * <code>Fixnum</code> will be truncated before being used.
2220 rb_obj_id(VALUE obj
)
2223 * 32-bit VALUE space
2224 * MSB ------------------------ LSB
2225 * false 00000000000000000000000000000000
2226 * true 00000000000000000000000000000010
2227 * nil 00000000000000000000000000000100
2228 * undef 00000000000000000000000000000110
2229 * symbol ssssssssssssssssssssssss00001110
2230 * object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
2231 * fixnum fffffffffffffffffffffffffffffff1
2235 * false 00000000000000000000000000000000
2236 * true 00000000000000000000000000000010
2237 * nil 00000000000000000000000000000100
2238 * undef 00000000000000000000000000000110
2239 * symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
2240 * object oooooooooooooooooooooooooooooo0 o...o % A = 0
2241 * fixnum fffffffffffffffffffffffffffffff1 bignum if required
2243 * where A = sizeof(RVALUE)/4
2246 * 20 if 32-bit, double is 4-byte aligned
2247 * 24 if 32-bit, double is 8-byte aligned
2250 if (TYPE(obj
) == T_SYMBOL
) {
2251 return (SYM2ID(obj
) * sizeof(RVALUE
) + (4 << 2)) | FIXNUM_FLAG
;
2253 if (SPECIAL_CONST_P(obj
)) {
2254 return LONG2NUM((SIGNED_VALUE
)obj
);
2256 return (VALUE
)((SIGNED_VALUE
)obj
|FIXNUM_FLAG
);
2261 * ObjectSpace.count_objects([result_hash]) -> hash
2263 * Counts objects for each type.
2265 * It returns a hash as:
2266 * {:TOTAL=>10000, :FREE=>3011, :T_OBJECT=>6, :T_CLASS=>404, ...}
2268 * If the optional argument, result_hash, is given,
2269 * it is overwritten and returned.
2270 * This is intended to avoid probe effect.
2272 * The contents of the returned hash is implementation defined.
2273 * It may be changed in future.
2275 * This method is not expected to work except C Ruby.
2280 count_objects(int argc
, VALUE
*argv
, VALUE os
)
2282 rb_objspace_t
*objspace
= &rb_objspace
;
2283 long counts
[T_MASK
+1];
2289 if (rb_scan_args(argc
, argv
, "01", &hash
) == 1) {
2290 if (TYPE(hash
) != T_HASH
)
2291 rb_raise(rb_eTypeError
, "non-hash given");
2294 for (i
= 0; i
<= T_MASK
; i
++) {
2298 for (i
= 0; i
< heaps_used
; i
++) {
2301 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
2302 for (;p
< pend
; p
++) {
2303 if (p
->as
.basic
.flags
) {
2304 counts
[BUILTIN_TYPE(p
)]++;
2310 total
+= heaps
[i
].limit
;
2314 hash
= rb_hash_new();
2315 rb_hash_aset(hash
, ID2SYM(rb_intern("TOTAL")), LONG2NUM(total
));
2316 rb_hash_aset(hash
, ID2SYM(rb_intern("FREE")), LONG2NUM(freed
));
2317 for (i
= 0; i
<= T_MASK
; i
++) {
2320 #define COUNT_TYPE(t) case t: type = ID2SYM(rb_intern(#t)); break;
2322 COUNT_TYPE(T_OBJECT
);
2323 COUNT_TYPE(T_CLASS
);
2324 COUNT_TYPE(T_MODULE
);
2325 COUNT_TYPE(T_FLOAT
);
2326 COUNT_TYPE(T_STRING
);
2327 COUNT_TYPE(T_REGEXP
);
2328 COUNT_TYPE(T_ARRAY
);
2330 COUNT_TYPE(T_STRUCT
);
2331 COUNT_TYPE(T_BIGNUM
);
2334 COUNT_TYPE(T_MATCH
);
2335 COUNT_TYPE(T_COMPLEX
);
2336 COUNT_TYPE(T_RATIONAL
);
2339 COUNT_TYPE(T_FALSE
);
2340 COUNT_TYPE(T_SYMBOL
);
2341 COUNT_TYPE(T_FIXNUM
);
2342 COUNT_TYPE(T_VALUES
);
2343 COUNT_TYPE(T_UNDEF
);
2345 COUNT_TYPE(T_ICLASS
);
2347 default: type
= INT2NUM(i
); break;
2350 rb_hash_aset(hash
, type
, LONG2NUM(counts
[i
]));
2358 * GC.count -> Integer
2360 * Counts objects for each type.
2362 * It returns a number of GC invoke counts.
2367 gc_count(VALUE self
)
2369 return UINT2NUM((&rb_objspace
)->count
);
2373 * The <code>GC</code> module provides an interface to Ruby's mark and
2374 * sweep garbage collection mechanism. Some of the underlying methods
2375 * are also available via the <code>ObjectSpace</code> module.
2383 rb_mGC
= rb_define_module("GC");
2384 rb_define_singleton_method(rb_mGC
, "start", rb_gc_start
, 0);
2385 rb_define_singleton_method(rb_mGC
, "enable", rb_gc_enable
, 0);
2386 rb_define_singleton_method(rb_mGC
, "disable", rb_gc_disable
, 0);
2387 rb_define_singleton_method(rb_mGC
, "stress", gc_stress_get
, 0);
2388 rb_define_singleton_method(rb_mGC
, "stress=", gc_stress_set
, 1);
2389 rb_define_singleton_method(rb_mGC
, "count", gc_count
, 0);
2390 rb_define_method(rb_mGC
, "garbage_collect", rb_gc_start
, 0);
2392 rb_mObSpace
= rb_define_module("ObjectSpace");
2393 rb_define_module_function(rb_mObSpace
, "each_object", os_each_obj
, -1);
2394 rb_define_module_function(rb_mObSpace
, "garbage_collect", rb_gc_start
, 0);
2396 rb_define_module_function(rb_mObSpace
, "define_finalizer", define_final
, -1);
2397 rb_define_module_function(rb_mObSpace
, "undefine_finalizer", undefine_final
, 1);
2399 rb_define_module_function(rb_mObSpace
, "_id2ref", id2ref
, 1);
2401 rb_global_variable(&nomem_error
);
2402 nomem_error
= rb_exc_new2(rb_eNoMemError
, "failed to allocate memory");
2404 rb_define_method(rb_mKernel
, "hash", rb_obj_id
, 0);
2405 rb_define_method(rb_mKernel
, "__id__", rb_obj_id
, 0);
2406 rb_define_method(rb_mKernel
, "object_id", rb_obj_id
, 0);
2408 rb_define_module_function(rb_mObSpace
, "count_objects", count_objects
, -1);