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
;
153 struct heaps_slot
*ptr
;
170 VALUE buffer
[MARK_STACK_MAX
];
174 struct gc_list
*global_list
;
178 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
179 #define rb_objspace (*GET_VM()->objspace)
181 static rb_objspace_t rb_objspace
= {{GC_MALLOC_LIMIT
}, {HEAP_MIN_SLOTS
}};
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
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
;
216 /*#define HEAP_SIZE 0x8000 */
218 /*#define HEAP_SIZE 0x20000 */
220 /*#define HEAP_SIZE 0x10000 */
222 #define HEAP_SIZE 0x4000
224 /*#define HEAP_SIZE 0x2000 */
226 /*#define HEAP_SIZE 0x1000 */
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;
236 VALUE
*rb_gc_register_stack_start
= 0;
239 int ruby_gc_stress
= 0;
243 /* set stack size (http://www.delorie.com/djgpp/v2faq/faq15_9.html) */
244 unsigned int _stklen
= 0x180000; /* 1.5 kB */
247 #if defined(DJGPP) || defined(_WIN32_WCE)
248 size_t rb_gc_stack_maxsize
= 65535*sizeof(VALUE
);
250 size_t rb_gc_stack_maxsize
= 655300*sizeof(VALUE
);
253 static void run_final(rb_objspace_t
*objspace
, VALUE obj
);
254 static int garbage_collect(rb_objspace_t
*objspace
);
257 rb_global_variable(VALUE
*var
)
259 rb_gc_register_address(var
);
265 rb_thread_t
*th
= GET_THREAD();
267 (rb_thread_raised_p(th
, RAISED_NOMEMORY
) && rb_safe_level() < 4)) {
268 fprintf(stderr
, "[FATAL] failed to allocate memory\n");
271 rb_thread_raised_set(th
, RAISED_NOMEMORY
);
272 rb_exc_raise(nomem_error
);
277 * GC.stress => true or false
279 * returns current status of GC stress mode.
283 gc_stress_get(VALUE self
)
285 return ruby_gc_stress
? Qtrue
: Qfalse
;
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.
301 gc_stress_set(VALUE self
, VALUE
bool)
304 ruby_gc_stress
= RTEST(bool);
309 ruby_vm_xmalloc(rb_objspace_t
*objspace
, size_t size
)
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
));
324 if (garbage_collect(objspace
)) {
325 RUBY_CRITICAL(mem
= malloc(size
));
336 ruby_xmalloc(size_t size
)
338 return ruby_vm_xmalloc(&rb_objspace
, size
);
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
);
352 ruby_xmalloc2(size_t n
, size_t size
)
354 return ruby_vm_xmalloc2(&rb_objspace
, n
, size
);
358 ruby_vm_xcalloc(rb_objspace_t
*objspace
, size_t n
, size_t size
)
362 mem
= ruby_vm_xmalloc2(objspace
, n
, size
);
363 memset(mem
, 0, n
* size
);
369 ruby_xcalloc(size_t n
, size_t size
)
371 return ruby_vm_xcalloc(&rb_objspace
, n
, size
);
375 ruby_vm_xrealloc(rb_objspace_t
*objspace
, void *ptr
, size_t size
)
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
));
388 if (garbage_collect(objspace
)) {
389 RUBY_CRITICAL(mem
= realloc(ptr
, size
));
400 ruby_xrealloc(void *ptr
, size_t size
)
402 return ruby_vm_xrealloc(&rb_objspace
, ptr
, size
);
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
);
416 ruby_xrealloc2(void *ptr
, size_t n
, size_t size
)
418 return ruby_vm_xrealloc2(&rb_objspace
, ptr
, n
, size
);
425 RUBY_CRITICAL(free(x
));
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
438 * GC.enable #=> false
445 rb_objspace_t
*objspace
= &rb_objspace
;
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
467 rb_objspace_t
*objspace
= &rb_objspace
;
477 rb_gc_register_address(VALUE
*addr
)
479 rb_objspace_t
*objspace
= &rb_objspace
;
482 tmp
= ALLOC(struct gc_list
);
483 tmp
->next
= global_List
;
489 rb_register_mark_object(VALUE obj
)
491 VALUE ary
= GET_THREAD()->vm
->mark_object_ary
;
492 rb_ary_push(ary
, obj
);
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
));
507 if (tmp
->next
->varptr
== addr
) {
508 struct gc_list
*t
= tmp
->next
;
510 tmp
->next
= tmp
->next
->next
;
511 RUBY_CRITICAL(free(t
));
520 allocate_heaps(rb_objspace_t
*objspace
, size_t next_heaps_length
)
522 struct heaps_slot
*p
;
525 size
= next_heaps_length
*sizeof(struct heaps_slot
);
527 if (heaps_used
> 0) {
528 p
= (struct heaps_slot
*)realloc(heaps
, size
);
532 p
= heaps
= (struct heaps_slot
*)malloc(size
);
535 if (p
== 0) rb_memerror();
536 heaps_length
= next_heaps_length
;
540 assign_heap_slot(rb_objspace_t
*objspace
)
542 RVALUE
*p
, *pend
, *membase
;
546 objs
= HEAP_OBJ_LIMIT
;
547 RUBY_CRITICAL(p
= (RVALUE
*)malloc(HEAP_SIZE
));
552 if ((VALUE
)p
% sizeof(RVALUE
) != 0) {
553 p
= (RVALUE
*)((VALUE
)p
+ sizeof(RVALUE
) - ((VALUE
)p
% sizeof(RVALUE
)));
554 if ((HEAP_SIZE
- HEAP_OBJ_LIMIT
* sizeof(RVALUE
)) < ((char*)p
- (char*)membase
)) {
563 register RVALUE
*mid_membase
;
565 mid_membase
= heaps
[mid
].membase
;
566 if (mid_membase
< membase
) {
569 else if (mid_membase
> membase
) {
573 rb_bug("same heap slot is allocated: %p at %ld", membase
, mid
);
576 if (hi
< heaps_used
) {
577 MEMMOVE(&heaps
[hi
+1], &heaps
[hi
], struct heaps_slot
, heaps_used
- hi
);
579 heaps
[hi
].membase
= membase
;
581 heaps
[hi
].limit
= objs
;
583 if (lomem
== 0 || lomem
> p
) lomem
= p
;
584 if (himem
< pend
) himem
= pend
;
588 p
->as
.free
.flags
= 0;
589 p
->as
.free
.next
= freelist
;
596 init_heap(rb_objspace_t
*objspace
)
600 add
= HEAP_MIN_SLOTS
/ HEAP_OBJ_LIMIT
;
602 if ((heaps_used
+ add
) > heaps_length
) {
603 allocate_heaps(objspace
, heaps_used
+ add
);
606 for (i
= 0; i
< add
; i
++) {
607 assign_heap_slot(objspace
);
614 set_heaps_increment(rb_objspace_t
*objspace
)
616 size_t next_heaps_length
= heaps_used
* 1.8;
617 heaps_inc
= next_heaps_length
- heaps_used
;
619 if (next_heaps_length
> heaps_length
) {
620 allocate_heaps(objspace
, next_heaps_length
);
625 heaps_increment(rb_objspace_t
*objspace
)
628 assign_heap_slot(objspace
);
635 #define RANY(o) ((RVALUE*)(o))
638 rb_newobj_from_heap(rb_objspace_t
*objspace
)
642 if (ruby_gc_stress
|| !freelist
) {
643 if (!heaps_increment(objspace
) && !garbage_collect(objspace
)) {
648 obj
= (VALUE
)freelist
;
649 freelist
= freelist
->as
.free
.next
;
651 MEMZERO((void*)obj
, RVALUE
, 1);
653 RANY(obj
)->file
= rb_sourcefile();
654 RANY(obj
)->line
= rb_sourceline();
662 rb_fill_value_cache(rb_thread_t
*th
)
664 rb_objspace_t
*objspace
= &rb_objspace
;
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
);
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
;
691 rb_objspace_t
*objspace
= &rb_objspace
;
695 RBASIC(v
)->flags
= 0;
696 th
->value_cache_ptr
++;
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
);
708 rb_objspace_t
*objspace
= &rb_objspace
;
709 return rb_newobj_from_heap(objspace
);
714 rb_node_newnode(enum node_type type
, VALUE a0
, VALUE a1
, VALUE a2
)
716 NODE
*n
= (NODE
*)rb_newobj();
719 nd_set_type(n
, type
);
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
);
742 #define SET_STACK_END (SET_MACHINE_STACK_END(&th->machine_stack_end), th->machine_register_stack_end = rb_ia64_bsp())
744 #define SET_STACK_END SET_MACHINE_STACK_END(&th->machine_stack_end)
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)
756 # define STACK_LENGTH ((STACK_END < STACK_START) ? STACK_START - STACK_END\
757 : STACK_END - STACK_START + 1)
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
764 static int grow_direction
;
766 stack_grow_direction(VALUE
*addr
)
768 rb_thread_t
*th
= GET_THREAD();
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)
778 #define GC_WATER_MARK 512
781 ruby_stack_length(VALUE
**p
)
783 rb_thread_t
*th
= GET_THREAD();
785 if (p
) *p
= STACK_UPPER(STACK_END
, STACK_START
, STACK_END
);
790 ruby_stack_check(void)
793 rb_thread_t
*th
= GET_THREAD();
795 ret
= STACK_LENGTH
> STACK_LEVEL_MAX
+ GC_WATER_MARK
;
798 ret
= (VALUE
*)rb_ia64_bsp() - th
->machine_register_stack_start
>
799 th
->machine_register_stack_maxsize
/sizeof(VALUE
) + GC_WATER_MARK
;
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
);
818 gc_mark_all(rb_objspace_t
*objspace
)
823 init_mark_stack(objspace
);
824 for (i
= 0; i
< heaps_used
; i
++) {
825 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
827 if ((p
->as
.basic
.flags
& FL_MARK
) &&
828 (p
->as
.basic
.flags
!= FL_MARK
)) {
829 gc_mark_children(objspace
, (VALUE
)p
, 0);
837 gc_mark_rest(rb_objspace_t
*objspace
)
839 VALUE tmp_arry
[MARK_STACK_MAX
];
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
) {
848 gc_mark_children(objspace
, *p
, 0);
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 size_t 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*/
868 if (heap
->slot
<= p
) {
869 if (p
< heap
->slot
+ heap
->limit
)
881 mark_locations_array(rb_objspace_t
*objspace
, register VALUE
*x
, register long n
)
886 VALGRIND_MAKE_MEM_DEFINED(&v
, sizeof(v
));
887 if (is_pointer_to_heap(objspace
, (void *)v
)) {
888 gc_mark(objspace
, v
, 0);
895 gc_mark_locations(rb_objspace_t
*objspace
, VALUE
*start
, VALUE
*end
)
899 if (end
<= start
) return;
901 mark_locations_array(&rb_objspace
, start
,n
);
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
;
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
);
926 mark_tbl(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
928 struct mark_tbl_arg arg
;
930 arg
.objspace
= objspace
;
932 st_foreach(tbl
, mark_entry
, (st_data_t
)&arg
);
936 rb_mark_tbl(st_table
*tbl
)
938 mark_tbl(&rb_objspace
, tbl
, 0);
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
);
950 mark_set(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
952 struct mark_tbl_arg arg
;
954 arg
.objspace
= objspace
;
956 st_foreach(tbl
, mark_key
, (st_data_t
)&arg
);
960 rb_mark_set(st_table
*tbl
)
962 mark_set(&rb_objspace
, tbl
, 0);
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
);
975 mark_hash(rb_objspace_t
*objspace
, st_table
*tbl
, int lev
)
977 struct mark_tbl_arg arg
;
979 arg
.objspace
= objspace
;
981 st_foreach(tbl
, mark_keyvalue
, (st_data_t
)&arg
);
985 rb_mark_hash(st_table
*tbl
)
987 mark_hash(&rb_objspace
, tbl
, 0);
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
1001 gc_mark(rb_objspace_t
*objspace
, VALUE ptr
, int lev
)
1003 register RVALUE
*obj
;
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
;
1018 mark_stack_overflow
= 1;
1023 gc_mark_children(objspace
, ptr
, lev
+1);
1027 rb_gc_mark(VALUE ptr
)
1029 gc_mark(&rb_objspace
, ptr
, 0);
1033 gc_mark_children(rb_objspace_t
*objspace
, VALUE ptr
, int lev
)
1035 register RVALUE
*obj
= RANY(ptr
);
1037 goto marking
; /* skip */
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
;
1047 if (FL_TEST(obj
, FL_EXIVAR
)) {
1048 rb_mark_generic_ivar(ptr
);
1051 switch (obj
->as
.basic
.flags
& T_MASK
) {
1054 rb_bug("rb_gc_mark() called for broken object");
1058 switch (nd_type(obj
)) {
1059 case NODE_IF
: /* 1,2,3 */
1067 case NODE_BLOCK_PASS
:
1068 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u2
.node
, lev
);
1070 case NODE_BLOCK
: /* 1,3 */
1076 case NODE_DREGX_ONCE
:
1082 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u1
.node
, lev
);
1084 case NODE_SUPER
: /* 3 */
1088 ptr
= (VALUE
)obj
->as
.node
.u3
.node
;
1091 case NODE_METHOD
: /* 1,2 */
1104 case NODE_OP_ASGN_OR
:
1105 case NODE_OP_ASGN_AND
:
1110 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u1
.node
, lev
);
1112 case NODE_FBODY
: /* 2 */
1116 case NODE_DASGN_CURR
:
1125 ptr
= (VALUE
)obj
->as
.node
.u2
.node
;
1128 case NODE_HASH
: /* 1 */
1141 ptr
= (VALUE
)obj
->as
.node
.u1
.node
;
1144 case NODE_SCOPE
: /* 2,3 */
1147 gc_mark(objspace
, (VALUE
)obj
->as
.node
.u3
.node
, lev
);
1148 ptr
= (VALUE
)obj
->as
.node
.u2
.node
;
1151 case NODE_ZARRAY
: /* - */
1170 case NODE_BLOCK_ARG
:
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
;
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
) {
1198 mark_tbl(objspace
, RCLASS_M_TBL(obj
), lev
);
1199 mark_tbl(objspace
, RCLASS_IV_TBL(obj
), lev
);
1200 ptr
= RCLASS_SUPER(obj
);
1204 if (FL_TEST(obj
, ELTS_SHARED
)) {
1205 ptr
= obj
->as
.array
.aux
.shared
;
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
);
1218 mark_hash(objspace
, obj
->as
.hash
.ntbl
, lev
);
1219 ptr
= obj
->as
.hash
.ifnone
;
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
;
1231 if (obj
->as
.data
.dmark
) (*obj
->as
.data
.dmark
)(DATA_PTR(obj
));
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
);
1245 if (obj
->as
.file
.fptr
)
1246 gc_mark(objspace
, obj
->as
.file
.fptr
->tied_io_for_writing
, lev
);
1255 gc_mark(objspace
, obj
->as
.match
.regexp
, lev
);
1256 if (obj
->as
.match
.str
) {
1257 ptr
= obj
->as
.match
.str
;
1263 gc_mark(objspace
, obj
->as
.rational
.num
, lev
);
1264 gc_mark(objspace
, obj
->as
.rational
.den
, lev
);
1268 gc_mark(objspace
, obj
->as
.complex.real
, lev
);
1269 gc_mark(objspace
, obj
->as
.complex.image
, lev
);
1274 long len
= RSTRUCT_LEN(obj
);
1275 VALUE
*ptr
= RSTRUCT_PTR(obj
);
1278 gc_mark(objspace
, *ptr
++, lev
);
1285 rb_gc_mark(RVALUES(obj
)->v1
);
1286 rb_gc_mark(RVALUES(obj
)->v2
);
1287 ptr
= RVALUES(obj
)->v3
;
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
);
1302 finalize_list(rb_objspace_t
*objspace
, RVALUE
*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
;
1318 free_unused_heaps(rb_objspace_t
*objspace
)
1323 for (i
= j
= 1; j
< heaps_used
; i
++) {
1324 if (heaps
[i
].limit
== 0) {
1326 last
= heaps
[i
].membase
;
1329 free(heaps
[i
].membase
);
1335 heaps
[j
] = heaps
[i
];
1341 if (last
< heaps_freed
) {
1351 void rb_gc_abort_threads(void);
1354 gc_sweep(rb_objspace_t
*objspace
)
1356 RVALUE
*p
, *pend
, *final_list
;
1359 size_t 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
;
1369 final_list
= deferred_final_list
;
1370 deferred_final_list
= 0;
1371 for (i
= 0; i
< heaps_used
; i
++) {
1373 RVALUE
*free
= freelist
;
1374 RVALUE
*final
= final_list
;
1376 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
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
;
1388 VALGRIND_MAKE_MEM_UNDEFINED((void*)p
, sizeof(RVALUE
));
1389 p
->as
.free
.flags
= 0;
1390 p
->as
.free
.next
= freelist
;
1395 else if (RBASIC(p
)->flags
== FL_MARK
) {
1396 /* objects to be finalized */
1397 /* do nothing remain marked */
1400 RBASIC(p
)->flags
&= ~FL_MARK
;
1405 if (n
== heaps
[i
].limit
&& freed
> do_heap_free
) {
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 */
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
);
1429 /* clear finalization list */
1431 deferred_final_list
= final_list
;
1434 free_unused_heaps(objspace
);
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
;
1448 obj_free(rb_objspace_t
*objspace
, VALUE obj
)
1450 switch (RANY(obj
)->as
.basic
.flags
& T_MASK
) {
1455 rb_bug("obj_free() called for broken object");
1459 if (FL_TEST(obj
, FL_EXIVAR
)) {
1460 rb_free_generic_ivar((VALUE
)obj
);
1463 switch (RANY(obj
)->as
.basic
.flags
& T_MASK
) {
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
));
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
));
1489 if (RANY(obj
)->as
.hash
.ntbl
) {
1490 st_free_table(RANY(obj
)->as
.hash
.ntbl
);
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
));
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
));
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
));
1521 if (RANY(obj
)->as
.file
.fptr
) {
1522 rb_io_fptr_finalize(RANY(obj
)->as
.file
.fptr
);
1529 /* iClass shares table with the module */
1538 if (!(RBASIC(obj
)->flags
& RBIGNUM_EMBED_FLAG
) && RBIGNUM_DIGITS(obj
)) {
1539 RUBY_CRITICAL(free(RBIGNUM_DIGITS(obj
)));
1543 switch (nd_type(obj
)) {
1545 if (RANY(obj
)->as
.node
.u1
.tbl
) {
1546 RUBY_CRITICAL(free(RANY(obj
)->as
.node
.u1
.tbl
));
1550 RUBY_CRITICAL(free(RANY(obj
)->as
.node
.u1
.node
));
1553 return; /* no need to free iv_tbl */
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
));
1563 rb_bug("gc_sweep(): unknown data type 0x%lx(%p)",
1564 RANY(obj
)->as
.basic
.flags
& T_MASK
, (void*)obj
);
1569 #if defined(__human68k__) || defined(DJGPP)
1572 #if defined(__human68k__)
1573 typedef unsigned long rb_jmp_buf
[8];
1577 movem.l d3-d7/a3-a5,(a0)\n\
1582 typedef unsigned long rb_jmp_buf
[6];
1583 __asm__ (".align 4\n\
1587 movl 8(%ebp),%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\
1599 int rb_setjmp (rb_jmp_buf
);
1600 #endif /* __human68k__ or DJGPP */
1601 #endif /* __GNUC__ */
1605 void rb_vm_mark(void *ptr
);
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
;
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;
1621 if (th
->machine_stack_end
< th
->machine_stack_start
) {
1622 stack_start
= th
->machine_stack_end
;
1623 stack_end
= th
->machine_stack_start
;
1626 stack_start
= th
->machine_stack_start
;
1627 stack_end
= th
->machine_stack_end
+ 1;
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
);
1640 rb_gc_mark_locations(th
->machine_register_stack_start
, th
->machine_register_stack_end
);
1642 #if defined(__human68k__) || defined(__mc68000__)
1643 mark_locations_array((VALUE
*)((char*)STACK_END
+ 2),
1644 (STACK_START
- STACK_END
));
1648 void rb_gc_mark_encodings(void);
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");
1662 if (dont_gc
|| during_gc
) {
1664 if (!heaps_increment(objspace
)) {
1665 set_heaps_increment(objspace
);
1666 heaps_increment(objspace
);
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
);
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
);
1711 gc_mark_rest(objspace
);
1717 if (GC_NOTIFY
) printf("end garbage_collect()\n");
1722 rb_garbage_collect(void)
1724 return garbage_collect(&rb_objspace
);
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
);
1736 if (th
->machine_stack_start
< th
->machine_stack_end
) {
1737 rb_gc_mark_locations(th
->machine_stack_start
, th
->machine_stack_end
);
1740 rb_gc_mark_locations(th
->machine_stack_end
, th
->machine_stack_start
);
1744 rb_gc_mark_locations(th
->machine_register_stack_start
, th
->machine_register_stack_end
);
1752 * gc.garbage_collect => nil
1753 * ObjectSpace.garbage_collect => nil
1755 * Initiates garbage collection, unless manually disabled.
1767 ruby_set_stack_size(size_t size
)
1769 rb_gc_stack_maxsize
= size
;
1773 Init_stack(VALUE
*addr
)
1775 ruby_init_stack(addr
);
1778 #undef ruby_init_stack
1780 ruby_init_stack(VALUE
*addr
1786 if (!rb_gc_stack_start
||
1788 rb_gc_stack_start
> addr
,
1789 rb_gc_stack_start
< addr
)) {
1790 rb_gc_stack_start
= addr
;
1793 if (!rb_gc_register_stack_start
||
1794 (VALUE
*)bsp
< rb_gc_register_stack_start
) {
1795 rb_gc_register_stack_start
= (VALUE
*)bsp
;
1798 #ifdef HAVE_GETRLIMIT
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
;
1815 if (VirtualQuery(&mi
, &mi
, sizeof(mi
))) {
1816 size
= (char *)mi
.BaseAddress
- (char *)mi
.AllocationBase
;
1818 if (space
> 1024*1024) space
= 1024*1024;
1819 rb_gc_stack_maxsize
= size
- space
;
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
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
1859 if (!rb_gc_stack_start
) {
1862 init_heap(&rb_objspace
);
1866 os_obj_of(rb_objspace_t
*objspace
, VALUE of
)
1871 for (i
= 0; i
< heaps_used
; i
++) {
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
)) {
1884 if (FL_TEST(p
, FL_SINGLETON
)) continue;
1886 if (!p
->as
.basic
.klass
) continue;
1887 if (!of
|| rb_obj_is_kind_of((VALUE
)p
, of
)) {
1896 return SIZET2NUM(n
);
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.
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>
1925 * 2.22044604925031e-16
1926 * 1.7976931348623157e+308
1927 * 2.2250738585072e-308
1933 os_each_obj(int argc
, VALUE
*argv
, VALUE os
)
1942 rb_scan_args(argc
, argv
, "01", &of
);
1944 RETURN_ENUMERATOR(os
, 1, &of
);
1945 return os_obj_of(&rb_objspace
, of
);
1950 * ObjectSpace.undefine_finalizer(obj)
1952 * Removes all finalizers for <i>obj</i>.
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);
1968 * ObjectSpace.define_finalizer(obj, aProc=proc())
1970 * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
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
);
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
);
2001 st_add_direct(finalizer_table
, obj
, rb_ary_new3(1, block
));
2007 rb_gc_copy_finalizer(VALUE dest
, VALUE obj
)
2009 rb_objspace_t
*objspace
= &rb_objspace
;
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
);
2021 run_single_final(VALUE arg
)
2023 VALUE
*args
= (VALUE
*)arg
;
2024 rb_eval_cmd(args
[0], args
[1], (int)args
[2]);
2029 run_final(rb_objspace_t
*objspace
, VALUE obj
)
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
;
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
;
2054 gc_finalize_deferred(rb_objspace_t
*objspace
)
2056 RVALUE
*p
= deferred_final_list
;
2059 deferred_final_list
= 0;
2061 finalize_list(objspace
, p
);
2063 free_unused_heaps(objspace
);
2068 rb_gc_finalize_deferred(void)
2070 gc_finalize_deferred(&rb_objspace
);
2074 rb_gc_call_finalizer_at_exit(void)
2076 rb_objspace_t
*objspace
= &rb_objspace
;
2080 /* finalizers are part of garbage collection */
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
;
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
);
2099 /* run data object's finalizers */
2100 for (i
= 0; i
< heaps_used
; i
++) {
2101 p
= heaps
[i
].slot
; pend
= p
+ heaps
[i
].limit
;
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
));
2130 rb_objspace_t
*objspace
= &rb_objspace
;
2131 garbage_collect(objspace
);
2132 gc_finalize_deferred(objspace
);
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"
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)
2156 rb_objspace_t
*objspace
= &rb_objspace
;
2161 ptr
= NUM2PTR(objid
);
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
);
2188 * Document-method: __id__
2189 * Document-method: object_id
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>.
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.
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
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
2241 * 20 if 32-bit, double is 4-byte aligned
2242 * 24 if 32-bit, double is 8-byte aligned
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
);
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.
2275 count_objects(int argc
, VALUE
*argv
, VALUE os
)
2277 rb_objspace_t
*objspace
= &rb_objspace
;
2278 size_t counts
[T_MASK
+1];
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
++) {
2293 for (i
= 0; i
< heaps_used
; i
++) {
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
)]++;
2305 total
+= heaps
[i
].limit
;
2309 hash
= rb_hash_new();
2310 rb_hash_aset(hash
, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(total
));
2311 rb_hash_aset(hash
, ID2SYM(rb_intern("FREE")), SIZET2NUM(freed
));
2312 for (i
= 0; i
<= T_MASK
; i
++) {
2315 #define COUNT_TYPE(t) case t: type = ID2SYM(rb_intern(#t)); break;
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
);
2325 COUNT_TYPE(T_STRUCT
);
2326 COUNT_TYPE(T_BIGNUM
);
2329 COUNT_TYPE(T_MATCH
);
2330 COUNT_TYPE(T_COMPLEX
);
2331 COUNT_TYPE(T_RATIONAL
);
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
);
2340 COUNT_TYPE(T_ICLASS
);
2342 default: type
= INT2NUM(i
); break;
2345 rb_hash_aset(hash
, type
, SIZET2NUM(counts
[i
]));
2353 * GC.count -> Integer
2355 * The number of times GC occured.
2357 * It returns the number of times GC occured since the process started.
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.
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);