/gc.c
C | 4421 lines | 3402 code | 557 blank | 462 comment | 498 complexity | 5ce1e16a09ae848f297e43b8822581d3 MD5 | raw file
Possible License(s): 0BSD, Unlicense, GPL-2.0, BSD-3-Clause, AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- /**********************************************************************
- gc.c -
- $Author$
- created at: Tue Oct 5 09:44:46 JST 1993
- Copyright (C) 1993-2007 Yukihiro Matsumoto
- Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
- Copyright (C) 2000 Information-technology Promotion Agency, Japan
- **********************************************************************/
- #include "ruby/ruby.h"
- #include "ruby/st.h"
- #include "ruby/re.h"
- #include "ruby/io.h"
- #include "ruby/thread.h"
- #include "ruby/util.h"
- #include "eval_intern.h"
- #include "vm_core.h"
- #include "internal.h"
- #include "gc.h"
- #include "constant.h"
- #include "atomic.h"
- #include <stdio.h>
- #include <setjmp.h>
- #include <sys/types.h>
- #include <assert.h>
- #ifdef HAVE_SYS_TIME_H
- #include <sys/time.h>
- #endif
- #ifdef HAVE_SYS_RESOURCE_H
- #include <sys/resource.h>
- #endif
- #if defined(__native_client__) && defined(NACL_NEWLIB)
- # include "nacl/resource.h"
- # undef HAVE_POSIX_MEMALIGN
- # undef HAVE_MEMALIGN
- #endif
- #if defined _WIN32 || defined __CYGWIN__
- #include <windows.h>
- #elif defined(HAVE_POSIX_MEMALIGN)
- #elif defined(HAVE_MEMALIGN)
- #include <malloc.h>
- #endif
- #ifdef HAVE_VALGRIND_MEMCHECK_H
- # include <valgrind/memcheck.h>
- # ifndef VALGRIND_MAKE_MEM_DEFINED
- # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
- # endif
- # ifndef VALGRIND_MAKE_MEM_UNDEFINED
- # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
- # endif
- #else
- # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
- # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
- #endif
- #define rb_setjmp(env) RUBY_SETJMP(env)
- #define rb_jmp_buf rb_jmpbuf_t
- /* Make alloca work the best possible way. */
- #ifdef __GNUC__
- # ifndef atarist
- # ifndef alloca
- # define alloca __builtin_alloca
- # endif
- # endif /* atarist */
- #else
- # ifdef HAVE_ALLOCA_H
- # include <alloca.h>
- # else
- # ifdef _AIX
- #pragma alloca
- # else
- # ifndef alloca /* predefined by HP cc +Olibcalls */
- void *alloca ();
- # endif
- # endif /* AIX */
- # endif /* HAVE_ALLOCA_H */
- #endif /* __GNUC__ */
- #ifndef GC_MALLOC_LIMIT
- #define GC_MALLOC_LIMIT 8000000
- #endif
- #define HEAP_MIN_SLOTS 10000
- #define FREE_MIN 4096
- typedef struct {
- unsigned int initial_malloc_limit;
- unsigned int initial_heap_min_slots;
- unsigned int initial_free_min;
- #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
- int gc_stress;
- #endif
- } ruby_gc_params_t;
- static ruby_gc_params_t initial_params = {
- GC_MALLOC_LIMIT,
- HEAP_MIN_SLOTS,
- FREE_MIN,
- #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
- FALSE,
- #endif
- };
- #define nomem_error GET_VM()->special_exceptions[ruby_error_nomemory]
- #ifndef GC_PROFILE_MORE_DETAIL
- #define GC_PROFILE_MORE_DETAIL 0
- #endif
- typedef struct gc_profile_record {
- double gc_time;
- double gc_mark_time;
- double gc_sweep_time;
- double gc_invoke_time;
- size_t heap_use_slots;
- size_t heap_live_objects;
- size_t heap_free_objects;
- size_t heap_total_objects;
- size_t heap_use_size;
- size_t heap_total_size;
- int have_finalize;
- int is_marked;
- size_t allocate_increase;
- size_t allocate_limit;
- } gc_profile_record;
- #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
- #pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
- #endif
- typedef struct RVALUE {
- union {
- struct {
- VALUE flags; /* always 0 for freed obj */
- struct RVALUE *next;
- } free;
- struct RBasic basic;
- struct RObject object;
- struct RClass klass;
- struct RFloat flonum;
- struct RString string;
- struct RArray array;
- struct RRegexp regexp;
- struct RHash hash;
- struct RData data;
- struct RTypedData typeddata;
- struct RStruct rstruct;
- struct RBignum bignum;
- struct RFile file;
- struct RNode node;
- struct RMatch match;
- struct RRational rational;
- struct RComplex complex;
- } as;
- #ifdef GC_DEBUG
- const char *file;
- int line;
- #endif
- } RVALUE;
- #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
- #pragma pack(pop)
- #endif
- struct heaps_slot {
- void *membase;
- RVALUE *slot;
- size_t limit;
- uintptr_t *bits;
- RVALUE *freelist;
- struct heaps_slot *next;
- struct heaps_slot *prev;
- struct heaps_slot *free_next;
- };
- struct heaps_header {
- struct heaps_slot *base;
- uintptr_t *bits;
- };
- struct sorted_heaps_slot {
- RVALUE *start;
- RVALUE *end;
- struct heaps_slot *slot;
- };
- struct heaps_free_bitmap {
- struct heaps_free_bitmap *next;
- };
- struct gc_list {
- VALUE *varptr;
- struct gc_list *next;
- };
- #define STACK_CHUNK_SIZE 500
- typedef struct stack_chunk {
- VALUE data[STACK_CHUNK_SIZE];
- struct stack_chunk *next;
- } stack_chunk_t;
- typedef struct mark_stack {
- stack_chunk_t *chunk;
- stack_chunk_t *cache;
- size_t index;
- size_t limit;
- size_t cache_size;
- size_t unused_cache_size;
- } mark_stack_t;
- #ifndef CALC_EXACT_MALLOC_SIZE
- #define CALC_EXACT_MALLOC_SIZE 0
- #endif
- typedef struct rb_objspace {
- struct {
- size_t limit;
- size_t increase;
- #if CALC_EXACT_MALLOC_SIZE
- size_t allocated_size;
- size_t allocations;
- #endif
- } malloc_params;
- struct {
- size_t increment;
- struct heaps_slot *ptr;
- struct heaps_slot *sweep_slots;
- struct heaps_slot *free_slots;
- struct sorted_heaps_slot *sorted;
- size_t length;
- size_t used;
- struct heaps_free_bitmap *free_bitmap;
- RVALUE *range[2];
- RVALUE *freed;
- size_t live_num;
- size_t free_num;
- size_t free_min;
- size_t final_num;
- size_t do_heap_free;
- } heap;
- struct {
- int dont_gc;
- int dont_lazy_sweep;
- int during_gc;
- rb_atomic_t finalizing;
- } flags;
- struct {
- st_table *table;
- RVALUE *deferred;
- } final;
- mark_stack_t mark_stack;
- struct {
- int run;
- gc_profile_record *record;
- size_t count;
- size_t size;
- double invoke_time;
- } profile;
- struct gc_list *global_list;
- size_t count;
- int gc_stress;
- struct mark_func_data_struct {
- VALUE data;
- void (*mark_func)(struct rb_objspace *objspace, VALUE v);
- } *mark_func_data;
- } rb_objspace_t;
- #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
- #define rb_objspace (*GET_VM()->objspace)
- #define ruby_initial_gc_stress initial_params.gc_stress
- int *ruby_initial_gc_stress_ptr = &ruby_initial_gc_stress;
- #else
- static rb_objspace_t rb_objspace = {{GC_MALLOC_LIMIT}};
- int *ruby_initial_gc_stress_ptr = &rb_objspace.gc_stress;
- #endif
- #define malloc_limit objspace->malloc_params.limit
- #define malloc_increase objspace->malloc_params.increase
- #define heaps objspace->heap.ptr
- #define heaps_length objspace->heap.length
- #define heaps_used objspace->heap.used
- #define lomem objspace->heap.range[0]
- #define himem objspace->heap.range[1]
- #define heaps_inc objspace->heap.increment
- #define heaps_freed objspace->heap.freed
- #define dont_gc objspace->flags.dont_gc
- #define during_gc objspace->flags.during_gc
- #define finalizing objspace->flags.finalizing
- #define finalizer_table objspace->final.table
- #define deferred_final_list objspace->final.deferred
- #define global_List objspace->global_list
- #define ruby_gc_stress objspace->gc_stress
- #define initial_malloc_limit initial_params.initial_malloc_limit
- #define initial_heap_min_slots initial_params.initial_heap_min_slots
- #define initial_free_min initial_params.initial_free_min
- #define is_lazy_sweeping(objspace) ((objspace)->heap.sweep_slots != 0)
- #define nonspecial_obj_id(obj) (VALUE)((SIGNED_VALUE)(obj)|FIXNUM_FLAG)
- #define RANY(o) ((RVALUE*)(o))
- #define has_free_object (objspace->heap.free_slots && objspace->heap.free_slots->freelist)
- #define HEAP_HEADER(p) ((struct heaps_header *)(p))
- #define GET_HEAP_HEADER(x) (HEAP_HEADER((uintptr_t)(x) & ~(HEAP_ALIGN_MASK)))
- #define GET_HEAP_SLOT(x) (GET_HEAP_HEADER(x)->base)
- #define GET_HEAP_BITMAP(x) (GET_HEAP_HEADER(x)->bits)
- #define NUM_IN_SLOT(p) (((uintptr_t)(p) & HEAP_ALIGN_MASK)/sizeof(RVALUE))
- #define BITMAP_INDEX(p) (NUM_IN_SLOT(p) / (sizeof(uintptr_t) * CHAR_BIT))
- #define BITMAP_OFFSET(p) (NUM_IN_SLOT(p) & ((sizeof(uintptr_t) * CHAR_BIT)-1))
- #define MARKED_IN_BITMAP(bits, p) (bits[BITMAP_INDEX(p)] & ((uintptr_t)1 << BITMAP_OFFSET(p)))
- #ifndef HEAP_ALIGN_LOG
- /* default tiny heap size: 16KB */
- #define HEAP_ALIGN_LOG 14
- #endif
- #define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod))
- enum {
- HEAP_ALIGN = (1UL << HEAP_ALIGN_LOG),
- HEAP_ALIGN_MASK = (~(~0UL << HEAP_ALIGN_LOG)),
- REQUIRED_SIZE_BY_MALLOC = (sizeof(size_t) * 5),
- HEAP_SIZE = (HEAP_ALIGN - REQUIRED_SIZE_BY_MALLOC),
- HEAP_OBJ_LIMIT = (unsigned int)((HEAP_SIZE - sizeof(struct heaps_header))/sizeof(struct RVALUE)),
- HEAP_BITMAP_LIMIT = CEILDIV(CEILDIV(HEAP_SIZE, sizeof(struct RVALUE)), sizeof(uintptr_t) * CHAR_BIT)
- };
- int ruby_gc_debug_indent = 0;
- VALUE rb_mGC;
- extern st_table *rb_class_tbl;
- int ruby_disable_gc_stress = 0;
- static void rb_objspace_call_finalizer(rb_objspace_t *objspace);
- static VALUE define_final0(VALUE obj, VALUE block);
- VALUE rb_define_final(VALUE obj, VALUE block);
- VALUE rb_undefine_final(VALUE obj);
- static void run_final(rb_objspace_t *objspace, VALUE obj);
- static void initial_expand_heap(rb_objspace_t *objspace);
- static void negative_size_allocation_error(const char *);
- static void *aligned_malloc(size_t, size_t);
- static void aligned_free(void *);
- static void init_mark_stack(mark_stack_t *stack);
- static VALUE lazy_sweep_enable(void);
- static int garbage_collect(rb_objspace_t *);
- static int gc_lazy_sweep(rb_objspace_t *);
- static void mark_tbl(rb_objspace_t *, st_table *);
- static void rest_sweep(rb_objspace_t *);
- static void gc_mark_stacked_objects(rb_objspace_t *);
- static double getrusage_time(void);
- static inline void gc_prof_timer_start(rb_objspace_t *);
- static inline void gc_prof_timer_stop(rb_objspace_t *, int);
- static inline void gc_prof_mark_timer_start(rb_objspace_t *);
- static inline void gc_prof_mark_timer_stop(rb_objspace_t *);
- static inline void gc_prof_sweep_timer_start(rb_objspace_t *);
- static inline void gc_prof_sweep_timer_stop(rb_objspace_t *);
- static inline void gc_prof_set_malloc_info(rb_objspace_t *);
- static inline void gc_prof_inc_live_num(rb_objspace_t *);
- static inline void gc_prof_dec_live_num(rb_objspace_t *);
- /*
- --------------------------- ObjectSpace -----------------------------
- */
- #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
- rb_objspace_t *
- rb_objspace_alloc(void)
- {
- rb_objspace_t *objspace = malloc(sizeof(rb_objspace_t));
- memset(objspace, 0, sizeof(*objspace));
- malloc_limit = initial_malloc_limit;
- ruby_gc_stress = ruby_initial_gc_stress;
- return objspace;
- }
- #endif
- #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
- static void free_stack_chunks(mark_stack_t *);
- void
- rb_objspace_free(rb_objspace_t *objspace)
- {
- rest_sweep(objspace);
- if (objspace->profile.record) {
- free(objspace->profile.record);
- objspace->profile.record = 0;
- }
- if (global_List) {
- struct gc_list *list, *next;
- for (list = global_List; list; list = next) {
- next = list->next;
- xfree(list);
- }
- }
- if (objspace->heap.free_bitmap) {
- struct heaps_free_bitmap *list, *next;
- for (list = objspace->heap.free_bitmap; list; list = next) {
- next = list->next;
- free(list);
- }
- }
- if (objspace->heap.sorted) {
- size_t i;
- for (i = 0; i < heaps_used; ++i) {
- free(objspace->heap.sorted[i].slot->bits);
- aligned_free(objspace->heap.sorted[i].slot->membase);
- free(objspace->heap.sorted[i].slot);
- }
- free(objspace->heap.sorted);
- heaps_used = 0;
- heaps = 0;
- }
- free_stack_chunks(&objspace->mark_stack);
- free(objspace);
- }
- #endif
- void
- rb_global_variable(VALUE *var)
- {
- rb_gc_register_address(var);
- }
- static void
- allocate_sorted_heaps(rb_objspace_t *objspace, size_t next_heaps_length)
- {
- struct sorted_heaps_slot *p;
- struct heaps_free_bitmap *bits;
- size_t size, add, i;
- size = next_heaps_length*sizeof(struct sorted_heaps_slot);
- add = next_heaps_length - heaps_used;
- if (heaps_used > 0) {
- p = (struct sorted_heaps_slot *)realloc(objspace->heap.sorted, size);
- if (p) objspace->heap.sorted = p;
- }
- else {
- p = objspace->heap.sorted = (struct sorted_heaps_slot *)malloc(size);
- }
- if (p == 0) {
- during_gc = 0;
- rb_memerror();
- }
- for (i = 0; i < add; i++) {
- bits = (struct heaps_free_bitmap *)malloc(HEAP_BITMAP_LIMIT * sizeof(uintptr_t));
- if (bits == 0) {
- during_gc = 0;
- rb_memerror();
- return;
- }
- bits->next = objspace->heap.free_bitmap;
- objspace->heap.free_bitmap = bits;
- }
- }
- static void
- link_free_heap_slot(rb_objspace_t *objspace, struct heaps_slot *slot)
- {
- slot->free_next = objspace->heap.free_slots;
- objspace->heap.free_slots = slot;
- }
- static void
- unlink_free_heap_slot(rb_objspace_t *objspace, struct heaps_slot *slot)
- {
- objspace->heap.free_slots = slot->free_next;
- slot->free_next = NULL;
- }
- static void
- assign_heap_slot(rb_objspace_t *objspace)
- {
- RVALUE *p, *pend, *membase;
- struct heaps_slot *slot;
- size_t hi, lo, mid;
- size_t objs;
- objs = HEAP_OBJ_LIMIT;
- p = (RVALUE*)aligned_malloc(HEAP_ALIGN, HEAP_SIZE);
- if (p == 0) {
- during_gc = 0;
- rb_memerror();
- }
- slot = (struct heaps_slot *)malloc(sizeof(struct heaps_slot));
- if (slot == 0) {
- aligned_free(p);
- during_gc = 0;
- rb_memerror();
- }
- MEMZERO((void*)slot, struct heaps_slot, 1);
- slot->next = heaps;
- if (heaps) heaps->prev = slot;
- heaps = slot;
- membase = p;
- p = (RVALUE*)((VALUE)p + sizeof(struct heaps_header));
- if ((VALUE)p % sizeof(RVALUE) != 0) {
- p = (RVALUE*)((VALUE)p + sizeof(RVALUE) - ((VALUE)p % sizeof(RVALUE)));
- objs = (HEAP_SIZE - (size_t)((VALUE)p - (VALUE)membase))/sizeof(RVALUE);
- }
- lo = 0;
- hi = heaps_used;
- while (lo < hi) {
- register RVALUE *mid_membase;
- mid = (lo + hi) / 2;
- mid_membase = objspace->heap.sorted[mid].slot->membase;
- if (mid_membase < membase) {
- lo = mid + 1;
- }
- else if (mid_membase > membase) {
- hi = mid;
- }
- else {
- rb_bug("same heap slot is allocated: %p at %"PRIuVALUE, (void *)membase, (VALUE)mid);
- }
- }
- if (hi < heaps_used) {
- MEMMOVE(&objspace->heap.sorted[hi+1], &objspace->heap.sorted[hi], struct sorted_heaps_slot, heaps_used - hi);
- }
- objspace->heap.sorted[hi].slot = slot;
- objspace->heap.sorted[hi].start = p;
- objspace->heap.sorted[hi].end = (p + objs);
- heaps->membase = membase;
- heaps->slot = p;
- heaps->limit = objs;
- assert(objspace->heap.free_bitmap != NULL);
- heaps->bits = (uintptr_t *)objspace->heap.free_bitmap;
- objspace->heap.free_bitmap = objspace->heap.free_bitmap->next;
- HEAP_HEADER(membase)->base = heaps;
- HEAP_HEADER(membase)->bits = heaps->bits;
- memset(heaps->bits, 0, HEAP_BITMAP_LIMIT * sizeof(uintptr_t));
- objspace->heap.free_num += objs;
- pend = p + objs;
- if (lomem == 0 || lomem > p) lomem = p;
- if (himem < pend) himem = pend;
- heaps_used++;
- while (p < pend) {
- p->as.free.flags = 0;
- p->as.free.next = heaps->freelist;
- heaps->freelist = p;
- p++;
- }
- link_free_heap_slot(objspace, heaps);
- }
- static void
- add_heap_slots(rb_objspace_t *objspace, size_t add)
- {
- size_t i;
- size_t next_heaps_length;
- next_heaps_length = heaps_used + add;
- if (next_heaps_length > heaps_length) {
- allocate_sorted_heaps(objspace, next_heaps_length);
- heaps_length = next_heaps_length;
- }
- for (i = 0; i < add; i++) {
- assign_heap_slot(objspace);
- }
- heaps_inc = 0;
- }
- static void
- init_heap(rb_objspace_t *objspace)
- {
- add_heap_slots(objspace, HEAP_MIN_SLOTS / HEAP_OBJ_LIMIT);
- init_mark_stack(&objspace->mark_stack);
- #ifdef USE_SIGALTSTACK
- {
- /* altstack of another threads are allocated in another place */
- rb_thread_t *th = GET_THREAD();
- void *tmp = th->altstack;
- th->altstack = malloc(ALT_STACK_SIZE);
- free(tmp); /* free previously allocated area */
- }
- #endif
- objspace->profile.invoke_time = getrusage_time();
- finalizer_table = st_init_numtable();
- }
- static void
- initial_expand_heap(rb_objspace_t *objspace)
- {
- size_t min_size = initial_heap_min_slots / HEAP_OBJ_LIMIT;
- if (min_size > heaps_used) {
- add_heap_slots(objspace, min_size - heaps_used);
- }
- }
- static void
- set_heaps_increment(rb_objspace_t *objspace)
- {
- size_t next_heaps_length = (size_t)(heaps_used * 1.8);
- if (next_heaps_length == heaps_used) {
- next_heaps_length++;
- }
- heaps_inc = next_heaps_length - heaps_used;
- if (next_heaps_length > heaps_length) {
- allocate_sorted_heaps(objspace, next_heaps_length);
- heaps_length = next_heaps_length;
- }
- }
- static int
- heaps_increment(rb_objspace_t *objspace)
- {
- if (heaps_inc > 0) {
- assign_heap_slot(objspace);
- heaps_inc--;
- return TRUE;
- }
- return FALSE;
- }
- VALUE
- rb_newobj(void)
- {
- rb_objspace_t *objspace = &rb_objspace;
- VALUE obj;
- if (UNLIKELY(during_gc)) {
- dont_gc = 1;
- during_gc = 0;
- rb_bug("object allocation during garbage collection phase");
- }
- if (UNLIKELY(ruby_gc_stress && !ruby_disable_gc_stress)) {
- if (!garbage_collect(objspace)) {
- during_gc = 0;
- rb_memerror();
- }
- }
- if (UNLIKELY(!has_free_object)) {
- if (!gc_lazy_sweep(objspace)) {
- during_gc = 0;
- rb_memerror();
- }
- }
- obj = (VALUE)objspace->heap.free_slots->freelist;
- objspace->heap.free_slots->freelist = RANY(obj)->as.free.next;
- if (objspace->heap.free_slots->freelist == NULL) {
- unlink_free_heap_slot(objspace, objspace->heap.free_slots);
- }
- MEMZERO((void*)obj, RVALUE, 1);
- #ifdef GC_DEBUG
- RANY(obj)->file = rb_sourcefile();
- RANY(obj)->line = rb_sourceline();
- #endif
- gc_prof_inc_live_num(objspace);
- return obj;
- }
- NODE*
- rb_node_newnode(enum node_type type, VALUE a0, VALUE a1, VALUE a2)
- {
- NODE *n = (NODE*)rb_newobj();
- n->flags |= T_NODE;
- nd_set_type(n, type);
- n->u1.value = a0;
- n->u2.value = a1;
- n->u3.value = a2;
- return n;
- }
- VALUE
- rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
- {
- NEWOBJ(data, struct RData);
- if (klass) Check_Type(klass, T_CLASS);
- OBJSETUP(data, klass, T_DATA);
- data->data = datap;
- data->dfree = dfree;
- data->dmark = dmark;
- return (VALUE)data;
- }
- VALUE
- rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
- {
- NEWOBJ(data, struct RTypedData);
- if (klass) Check_Type(klass, T_CLASS);
- OBJSETUP(data, klass, T_DATA);
- data->data = datap;
- data->typed_flag = 1;
- data->type = type;
- return (VALUE)data;
- }
- size_t
- rb_objspace_data_type_memsize(VALUE obj)
- {
- if (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj)->function.dsize) {
- return RTYPEDDATA_TYPE(obj)->function.dsize(RTYPEDDATA_DATA(obj));
- }
- else {
- return 0;
- }
- }
- const char *
- rb_objspace_data_type_name(VALUE obj)
- {
- if (RTYPEDDATA_P(obj)) {
- return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
- }
- else {
- return 0;
- }
- }
- static void gc_mark(rb_objspace_t *objspace, VALUE ptr);
- static void gc_mark_children(rb_objspace_t *objspace, VALUE ptr);
- static inline int
- is_pointer_to_heap(rb_objspace_t *objspace, void *ptr)
- {
- register RVALUE *p = RANY(ptr);
- register struct sorted_heaps_slot *heap;
- register size_t hi, lo, mid;
- if (p < lomem || p > himem) return FALSE;
- if ((VALUE)p % sizeof(RVALUE) != 0) return FALSE;
- /* check if p looks like a pointer using bsearch*/
- lo = 0;
- hi = heaps_used;
- while (lo < hi) {
- mid = (lo + hi) / 2;
- heap = &objspace->heap.sorted[mid];
- if (heap->start <= p) {
- if (p < heap->end)
- return TRUE;
- lo = mid + 1;
- }
- else {
- hi = mid;
- }
- }
- return FALSE;
- }
- static int
- free_method_entry_i(ID key, rb_method_entry_t *me, st_data_t data)
- {
- if (!me->mark) {
- rb_free_method_entry(me);
- }
- return ST_CONTINUE;
- }
- void
- rb_free_m_table(st_table *tbl)
- {
- st_foreach(tbl, free_method_entry_i, 0);
- st_free_table(tbl);
- }
- static int
- free_const_entry_i(ID key, rb_const_entry_t *ce, st_data_t data)
- {
- xfree(ce);
- return ST_CONTINUE;
- }
- void
- rb_free_const_table(st_table *tbl)
- {
- st_foreach(tbl, free_const_entry_i, 0);
- st_free_table(tbl);
- }
- static int obj_free(rb_objspace_t *, VALUE);
- static inline struct heaps_slot *
- add_slot_local_freelist(rb_objspace_t *objspace, RVALUE *p)
- {
- struct heaps_slot *slot;
- VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
- p->as.free.flags = 0;
- slot = GET_HEAP_SLOT(p);
- p->as.free.next = slot->freelist;
- slot->freelist = p;
- return slot;
- }
- static void
- unlink_heap_slot(rb_objspace_t *objspace, struct heaps_slot *slot)
- {
- if (slot->prev)
- slot->prev->next = slot->next;
- if (slot->next)
- slot->next->prev = slot->prev;
- if (heaps == slot)
- heaps = slot->next;
- if (objspace->heap.sweep_slots == slot)
- objspace->heap.sweep_slots = slot->next;
- slot->prev = NULL;
- slot->next = NULL;
- }
- static void
- free_unused_heaps(rb_objspace_t *objspace)
- {
- size_t i, j;
- RVALUE *last = 0;
- for (i = j = 1; j < heaps_used; i++) {
- if (objspace->heap.sorted[i].slot->limit == 0) {
- struct heaps_slot* h = objspace->heap.sorted[i].slot;
- ((struct heaps_free_bitmap *)(h->bits))->next =
- objspace->heap.free_bitmap;
- objspace->heap.free_bitmap = (struct heaps_free_bitmap *)h->bits;
- if (!last) {
- last = objspace->heap.sorted[i].slot->membase;
- }
- else {
- aligned_free(objspace->heap.sorted[i].slot->membase);
- }
- free(objspace->heap.sorted[i].slot);
- heaps_used--;
- }
- else {
- if (i != j) {
- objspace->heap.sorted[j] = objspace->heap.sorted[i];
- }
- j++;
- }
- }
- if (last) {
- if (last < heaps_freed) {
- aligned_free(heaps_freed);
- heaps_freed = last;
- }
- else {
- aligned_free(last);
- }
- }
- }
- static inline void
- make_deferred(RVALUE *p)
- {
- p->as.basic.flags = (p->as.basic.flags & ~T_MASK) | T_ZOMBIE;
- }
- static inline void
- make_io_deferred(RVALUE *p)
- {
- rb_io_t *fptr = p->as.file.fptr;
- make_deferred(p);
- p->as.data.dfree = (void (*)(void*))rb_io_fptr_finalize;
- p->as.data.data = fptr;
- }
- static int
- obj_free(rb_objspace_t *objspace, VALUE obj)
- {
- switch (BUILTIN_TYPE(obj)) {
- case T_NIL:
- case T_FIXNUM:
- case T_TRUE:
- case T_FALSE:
- rb_bug("obj_free() called for broken object");
- break;
- }
- if (FL_TEST(obj, FL_EXIVAR)) {
- rb_free_generic_ivar((VALUE)obj);
- FL_UNSET(obj, FL_EXIVAR);
- }
- switch (BUILTIN_TYPE(obj)) {
- case T_OBJECT:
- if (!(RANY(obj)->as.basic.flags & ROBJECT_EMBED) &&
- RANY(obj)->as.object.as.heap.ivptr) {
- xfree(RANY(obj)->as.object.as.heap.ivptr);
- }
- break;
- case T_MODULE:
- case T_CLASS:
- rb_clear_cache_by_class((VALUE)obj);
- if (RCLASS_M_TBL(obj)) {
- rb_free_m_table(RCLASS_M_TBL(obj));
- }
- if (RCLASS_IV_TBL(obj)) {
- st_free_table(RCLASS_IV_TBL(obj));
- }
- if (RCLASS_CONST_TBL(obj)) {
- rb_free_const_table(RCLASS_CONST_TBL(obj));
- }
- if (RCLASS_IV_INDEX_TBL(obj)) {
- st_free_table(RCLASS_IV_INDEX_TBL(obj));
- }
- xfree(RANY(obj)->as.klass.ptr);
- break;
- case T_STRING:
- rb_str_free(obj);
- break;
- case T_ARRAY:
- rb_ary_free(obj);
- break;
- case T_HASH:
- if (RANY(obj)->as.hash.ntbl) {
- st_free_table(RANY(obj)->as.hash.ntbl);
- }
- break;
- case T_REGEXP:
- if (RANY(obj)->as.regexp.ptr) {
- onig_free(RANY(obj)->as.regexp.ptr);
- }
- break;
- case T_DATA:
- if (DATA_PTR(obj)) {
- if (RTYPEDDATA_P(obj)) {
- RDATA(obj)->dfree = RANY(obj)->as.typeddata.type->function.dfree;
- }
- if (RANY(obj)->as.data.dfree == (RUBY_DATA_FUNC)-1) {
- xfree(DATA_PTR(obj));
- }
- else if (RANY(obj)->as.data.dfree) {
- make_deferred(RANY(obj));
- return 1;
- }
- }
- break;
- case T_MATCH:
- if (RANY(obj)->as.match.rmatch) {
- struct rmatch *rm = RANY(obj)->as.match.rmatch;
- onig_region_free(&rm->regs, 0);
- if (rm->char_offset)
- xfree(rm->char_offset);
- xfree(rm);
- }
- break;
- case T_FILE:
- if (RANY(obj)->as.file.fptr) {
- make_io_deferred(RANY(obj));
- return 1;
- }
- break;
- case T_RATIONAL:
- case T_COMPLEX:
- break;
- case T_ICLASS:
- /* iClass shares table with the module */
- xfree(RANY(obj)->as.klass.ptr);
- break;
- case T_FLOAT:
- break;
- case T_BIGNUM:
- if (!(RBASIC(obj)->flags & RBIGNUM_EMBED_FLAG) && RBIGNUM_DIGITS(obj)) {
- xfree(RBIGNUM_DIGITS(obj));
- }
- break;
- case T_NODE:
- switch (nd_type(obj)) {
- case NODE_SCOPE:
- if (RANY(obj)->as.node.u1.tbl) {
- xfree(RANY(obj)->as.node.u1.tbl);
- }
- break;
- case NODE_ARGS:
- if (RANY(obj)->as.node.u3.args) {
- xfree(RANY(obj)->as.node.u3.args);
- }
- break;
- case NODE_ALLOCA:
- xfree(RANY(obj)->as.node.u1.node);
- break;
- }
- break; /* no need to free iv_tbl */
- case T_STRUCT:
- if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
- RANY(obj)->as.rstruct.as.heap.ptr) {
- xfree(RANY(obj)->as.rstruct.as.heap.ptr);
- }
- break;
- default:
- rb_bug("gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
- BUILTIN_TYPE(obj), (void*)obj, RBASIC(obj)->flags);
- }
- return 0;
- }
- void
- Init_heap(void)
- {
- init_heap(&rb_objspace);
- }
- typedef int each_obj_callback(void *, void *, size_t, void *);
- struct each_obj_args {
- each_obj_callback *callback;
- void *data;
- };
- static VALUE
- objspace_each_objects(VALUE arg)
- {
- size_t i;
- RVALUE *membase = 0;
- RVALUE *pstart, *pend;
- rb_objspace_t *objspace = &rb_objspace;
- struct each_obj_args *args = (struct each_obj_args *)arg;
- volatile VALUE v;
- i = 0;
- while (i < heaps_used) {
- while (0 < i && (uintptr_t)membase < (uintptr_t)objspace->heap.sorted[i-1].slot->membase)
- i--;
- while (i < heaps_used && (uintptr_t)objspace->heap.sorted[i].slot->membase <= (uintptr_t)membase)
- i++;
- if (heaps_used <= i)
- break;
- membase = objspace->heap.sorted[i].slot->membase;
- pstart = objspace->heap.sorted[i].slot->slot;
- pend = pstart + objspace->heap.sorted[i].slot->limit;
- for (; pstart != pend; pstart++) {
- if (pstart->as.basic.flags) {
- v = (VALUE)pstart; /* acquire to save this object */
- break;
- }
- }
- if (pstart != pend) {
- if ((*args->callback)(pstart, pend, sizeof(RVALUE), args->data)) {
- break;
- }
- }
- }
- RB_GC_GUARD(v);
- return Qnil;
- }
- /*
- * rb_objspace_each_objects() is special C API to walk through
- * Ruby object space. This C API is too difficult to use it.
- * To be frank, you should not use it. Or you need to read the
- * source code of this function and understand what this function does.
- *
- * 'callback' will be called several times (the number of heap slot,
- * at current implementation) with:
- * vstart: a pointer to the first living object of the heap_slot.
- * vend: a pointer to next to the valid heap_slot area.
- * stride: a distance to next VALUE.
- *
- * If callback() returns non-zero, the iteration will be stopped.
- *
- * This is a sample callback code to iterate liveness objects:
- *
- * int
- * sample_callback(void *vstart, void *vend, int stride, void *data) {
- * VALUE v = (VALUE)vstart;
- * for (; v != (VALUE)vend; v += stride) {
- * if (RBASIC(v)->flags) { // liveness check
- * // do something with live object 'v'
- * }
- * return 0; // continue to iteration
- * }
- *
- * Note: 'vstart' is not a top of heap_slot. This point the first
- * living object to grasp at least one object to avoid GC issue.
- * This means that you can not walk through all Ruby object slot
- * including freed object slot.
- *
- * Note: On this implementation, 'stride' is same as sizeof(RVALUE).
- * However, there are possibilities to pass variable values with
- * 'stride' with some reasons. You must use stride instead of
- * use some constant value in the iteration.
- */
- void
- rb_objspace_each_objects(each_obj_callback *callback, void *data)
- {
- struct each_obj_args args;
- rb_objspace_t *objspace = &rb_objspace;
- rest_sweep(objspace);
- objspace->flags.dont_lazy_sweep = TRUE;
- args.callback = callback;
- args.data = data;
- rb_ensure(objspace_each_objects, (VALUE)&args, lazy_sweep_enable, Qnil);
- }
- struct os_each_struct {
- size_t num;
- VALUE of;
- };
- static int
- internal_object_p(VALUE obj)
- {
- RVALUE *p = (RVALUE *)obj;
- if (p->as.basic.flags) {
- switch (BUILTIN_TYPE(p)) {
- case T_NONE:
- case T_ICLASS:
- case T_NODE:
- case T_ZOMBIE:
- break;
- case T_CLASS:
- if (FL_TEST(p, FL_SINGLETON))
- break;
- default:
- if (!p->as.basic.klass) break;
- return 0;
- }
- }
- return 1;
- }
- static int
- os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
- {
- struct os_each_struct *oes = (struct os_each_struct *)data;
- RVALUE *p = (RVALUE *)vstart, *pend = (RVALUE *)vend;
- for (; p != pend; p++) {
- volatile VALUE v = (VALUE)p;
- if (!internal_object_p(v)) {
- if (!oes->of || rb_obj_is_kind_of(v, oes->of)) {
- rb_yield(v);
- oes->num++;
- }
- }
- }
- return 0;
- }
- static VALUE
- os_obj_of(VALUE of)
- {
- struct os_each_struct oes;
- oes.num = 0;
- oes.of = of;
- rb_objspace_each_objects(os_obj_of_i, &oes);
- return SIZET2NUM(oes.num);
- }
- /*
- * call-seq:
- * ObjectSpace.each_object([module]) {|obj| ... } -> fixnum
- * ObjectSpace.each_object([module]) -> an_enumerator
- *
- * Calls the block once for each living, nonimmediate object in this
- * Ruby process. If <i>module</i> is specified, calls the block
- * for only those classes or modules that match (or are a subclass of)
- * <i>module</i>. Returns the number of objects found. Immediate
- * objects (<code>Fixnum</code>s, <code>Symbol</code>s
- * <code>true</code>, <code>false</code>, and <code>nil</code>) are
- * never returned. In the example below, <code>each_object</code>
- * returns both the numbers we defined and several constants defined in
- * the <code>Math</code> module.
- *
- * If no block is given, an enumerator is returned instead.
- *
- * a = 102.7
- * b = 95 # Won't be returned
- * c = 12345678987654321
- * count = ObjectSpace.each_object(Numeric) {|x| p x }
- * puts "Total count: #{count}"
- *
- * <em>produces:</em>
- *
- * 12345678987654321
- * 102.7
- * 2.71828182845905
- * 3.14159265358979
- * 2.22044604925031e-16
- * 1.7976931348623157e+308
- * 2.2250738585072e-308
- * Total count: 7
- *
- */
- static VALUE
- os_each_obj(int argc, VALUE *argv, VALUE os)
- {
- VALUE of;
- rb_secure(4);
- if (argc == 0) {
- of = 0;
- }
- else {
- rb_scan_args(argc, argv, "01", &of);
- }
- RETURN_ENUMERATOR(os, 1, &of);
- return os_obj_of(of);
- }
- /*
- * call-seq:
- * ObjectSpace.undefine_finalizer(obj)
- *
- * Removes all finalizers for <i>obj</i>.
- *
- */
- static VALUE
- undefine_final(VALUE os, VALUE obj)
- {
- return rb_undefine_final(obj);
- }
- VALUE
- rb_undefine_final(VALUE obj)
- {
- rb_objspace_t *objspace = &rb_objspace;
- st_data_t data = obj;
- rb_check_frozen(obj);
- st_delete(finalizer_table, &data, 0);
- FL_UNSET(obj, FL_FINALIZE);
- return obj;
- }
- /*
- * call-seq:
- * ObjectSpace.define_finalizer(obj, aProc=proc())
- *
- * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
- * was destroyed.
- *
- */
- static VALUE
- define_final(int argc, VALUE *argv, VALUE os)
- {
- VALUE obj, block;
- rb_scan_args(argc, argv, "11", &obj, &block);
- rb_check_frozen(obj);
- if (argc == 1) {
- block = rb_block_proc();
- }
- else if (!rb_respond_to(block, rb_intern("call"))) {
- rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
- rb_obj_classname(block));
- }
- return define_final0(obj, block);
- }
- static VALUE
- define_final0(VALUE obj, VALUE block)
- {
- rb_objspace_t *objspace = &rb_objspace;
- VALUE table;
- st_data_t data;
- if (!FL_ABLE(obj)) {
- rb_raise(rb_eArgError, "cannot define finalizer for %s",
- rb_obj_classname(obj));
- }
- RBASIC(obj)->flags |= FL_FINALIZE;
- block = rb_ary_new3(2, INT2FIX(rb_safe_level()), block);
- OBJ_FREEZE(block);
- if (st_lookup(finalizer_table, obj, &data)) {
- table = (VALUE)data;
- rb_ary_push(table, block);
- }
- else {
- table = rb_ary_new3(1, block);
- RBASIC(table)->klass = 0;
- st_add_direct(finalizer_table, obj, table);
- }
- return block;
- }
- VALUE
- rb_define_final(VALUE obj, VALUE block)
- {
- rb_check_frozen(obj);
- if (!rb_respond_to(block, rb_intern("call"))) {
- rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
- rb_obj_classname(block));
- }
- return define_final0(obj, block);
- }
- void
- rb_gc_copy_finalizer(VALUE dest, VALUE obj)
- {
- rb_objspace_t *objspace = &rb_objspace;
- VALUE table;
- st_data_t data;
- if (!FL_TEST(obj, FL_FINALIZE)) return;
- if (st_lookup(finalizer_table, obj, &data)) {
- table = (VALUE)data;
- st_insert(finalizer_table, dest, table);
- }
- FL_SET(dest, FL_FINALIZE);
- }
- static VALUE
- run_single_final(VALUE arg)
- {
- VALUE *args = (VALUE *)arg;
- rb_eval_cmd(args[0], args[1], (int)args[2]);
- return Qnil;
- }
- static void
- run_finalizer(rb_objspace_t *objspace, VALUE obj, VALUE table)
- {
- long i;
- int status;
- VALUE args[3];
- VALUE objid = nonspecial_obj_id(obj);
- if (RARRAY_LEN(table) > 0) {
- args[1] = rb_obj_freeze(rb_ary_new3(1, objid));
- }
- else {
- args[1] = 0;
- }
- args[2] = (VALUE)rb_safe_level();
- for (i=0; i<RARRAY_LEN(table); i++) {
- VALUE final = RARRAY_PTR(table)[i];
- args[0] = RARRAY_PTR(final)[1];
- args[2] = FIX2INT(RARRAY_PTR(final)[0]);
- status = 0;
- rb_protect(run_single_final, (VALUE)args, &status);
- if (status)
- rb_set_errinfo(Qnil);
- }
- }
- static void
- run_final(rb_objspace_t *objspace, VALUE obj)
- {
- RUBY_DATA_FUNC free_func = 0;
- st_data_t key, table;
- objspace->heap.final_num--;
- RBASIC(obj)->klass = 0;
- if (RTYPEDDATA_P(obj)) {
- free_func = RTYPEDDATA_TYPE(obj)->function.dfree;
- }
- else {
- free_func = RDATA(obj)->dfree;
- }
- if (free_func) {
- (*free_func)(DATA_PTR(obj));
- }
- key = (st_data_t)obj;
- if (st_delete(finalizer_table, &key, &table)) {
- run_finalizer(objspace, obj, (VALUE)table);
- }
- }
- static void
- finalize_list(rb_objspace_t *objspace, RVALUE *p)
- {
- while (p) {
- RVALUE *tmp = p->as.free.next;
- run_final(objspace, (VALUE)p);
- if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */
- add_slot_local_freelist(objspace, p);
- if (!is_lazy_sweeping(objspace)) {
- gc_prof_dec_live_num(objspace);
- }
- }
- else {
- struct heaps_slot *slot = (struct heaps_slot *)(VALUE)RDATA(p)->dmark;
- slot->limit--;
- }
- p = tmp;
- }
- }
- static void
- finalize_deferred(rb_objspace_t *objspace)
- {
- RVALUE *p = deferred_final_list;
- deferred_final_list = 0;
- if (p) {
- finalize_list(objspace, p);
- }
- }
- void
- rb_gc_finalize_deferred(void)
- {
- rb_objspace_t *objspace = &rb_objspace;
- if (ATOMIC_EXCHANGE(finalizing, 1)) return;
- finalize_deferred(objspace);
- ATOMIC_SET(finalizing, 0);
- }
- static int
- chain_finalized_object(st_data_t key, st_data_t val, st_data_t arg)
- {
- RVALUE *p = (RVALUE *)key, **final_list = (RVALUE **)arg;
- if ((p->as.basic.flags & FL_FINALIZE) == FL_FINALIZE &&
- !MARKED_IN_BITMAP(GET_HEAP_BITMAP(p), p)) {
- if (BUILTIN_TYPE(p) != T_ZOMBIE) {
- p->as.free.flags = T_ZOMBIE;
- RDATA(p)->dfree = 0;
- }
- p->as.free.next = *final_list;
- *final_list = p;
- }
- return ST_CONTINUE;
- }
- struct force_finalize_list {
- VALUE obj;
- VALUE table;
- struct force_finalize_list *next;
- };
- static int
- force_chain_object(st_data_t key, st_data_t val, st_data_t arg)
- {
- struct force_finalize_list **prev = (struct force_finalize_list **)arg;
- struct force_finalize_list *curr = ALLOC(struct force_finalize_list);
- curr->obj = key;
- curr->table = val;
- curr->next = *prev;
- *prev = curr;
- return ST_CONTINUE;
- }
- void
- rb_gc_call_finalizer_at_exit(void)
- {
- rb_objspace_call_finalizer(&rb_objspace);
- }
- static void
- rb_objspace_call_finalizer(rb_objspace_t *objspace)
- {
- RVALUE *p, *pend;
- RVALUE *final_list = 0;
- size_t i;
- rest_sweep(objspace);
- if (ATOMIC_EXCHANGE(finalizing, 1)) return;
- /* run finalizers */
- do {
- finalize_deferred(objspace);
- /* mark reachable objects from finalizers */
- /* They might be not referred from any place here */
- mark_tbl(objspace, finalizer_table);
- gc_mark_stacked_objects(objspace);
- st_foreach(finalizer_table, chain_finalized_object,
- (st_data_t)&deferred_final_list);
- } while (deferred_final_list);
- /* force to run finalizer */
- while (finalizer_table->num_entries) {
- struct force_finalize_list *list = 0;
- st_foreach(finalizer_table, force_chain_object, (st_data_t)&list);
- while (list) {
- struct force_finalize_list *curr = list;
- st_data_t obj = (st_data_t)curr->obj;
- run_finalizer(objspace, curr->obj, curr->table);
- st_delete(finalizer_table, &obj, 0);
- list = curr->next;
- xfree(curr);
- }
- }
- /* finalizers are part of garbage collection */
- during_gc++;
- /* run data object's finalizers */
- for (i = 0; i < heaps_used; i++) {
- p = objspace->heap.sorted[i].start; pend = objspace->heap.sorted[i].end;
- while (p < pend) {
- if (BUILTIN_TYPE(p) == T_DATA &&
- DATA_PTR(p) && RANY(p)->as.data.dfree &&
- !rb_obj_is_thread((VALUE)p) && !rb_obj_is_mutex((VALUE)p) &&
- !rb_obj_is_fiber((VALUE)p)) {
- p->as.free.flags = 0;
- if (RTYPEDDATA_P(p)) {
- RDATA(p)->dfree = RANY(p)->as.typeddata.type->function.dfree;
- }
- if (RANY(p)->as.data.dfree == (RUBY_DATA_FUNC)-1) {
- xfree(DATA_PTR(p));
- }
- else if (RANY(p)->as.data.dfree) {
- make_deferred(RANY(p));
- RANY(p)->as.free.next = final_list;
- final_list = p;
- }
- }
- else if (BUILTIN_TYPE(p) == T_FILE) {
- if (RANY(p)->as.file.fptr) {
- make_io_deferred(RANY(p));
- RANY(p)->as.free.next = final_list;
- final_list = p;
- }
- }
- p++;
- }
- }
- during_gc = 0;
- if (final_list) {
- finalize_list(objspace, final_list);
- }
- st_free_table(finalizer_table);
- finalizer_table = 0;
- ATOMIC_SET(finalizing, 0);
- }
- static inline int
- is_id_value(rb_objspace_t *objspace, VALUE ptr)
- {
- if (!is_pointer_to_heap(objspace, (void *)ptr)) return FALSE;
- if (BUILTIN_TYPE(ptr) > T_FIXNUM) return FALSE;
- if (BUILTIN_TYPE(ptr) == T_ICLASS) return FALSE;
- return TRUE;
- }
- static inline int
- is_dead_object(rb_objspace_t *objspace, VALUE ptr)
- {
- struct heaps_slot *slot = objspace->heap.sweep_slots;
- if (!is_lazy_sweeping(objspace) || MARKED_IN_BITMAP(GET_HEAP_BITMAP(ptr), ptr))
- return FALSE;
- while (slot) {
- if ((VALUE)slot->slot <= ptr && ptr < (VALUE)(slot->slot + slot->limit))
- return TRUE;
- slot = slot->next;
- }
- return FALSE;
- }
- static inline int
- is_live_object(rb_objspace_t *objspace, VALUE ptr)
- {
- if (BUILTIN_TYPE(ptr) == 0) return FALSE;
- if (RBASIC(ptr)->klass == 0) return FALSE;
- if (is_dead_object(objspace, ptr)) return FALSE;
- return TRUE;
- }
- /*
- * call-seq:
- * ObjectSpace._id2ref(object_id) -> an_object
- *
- * Converts an object id to a reference to the object. May not be
- * called on an object id passed as a parameter to a finalizer.
- *
- * s = "I am a string" #=> "I am a string"
- * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
- * r == s #=> true
- *
- */
- static VALUE
- id2ref(VALUE obj, VALUE objid)
- {
- #if SIZEOF_LONG == SIZEOF_VOIDP
- #define NUM2PTR(x) NUM2ULONG(x)
- #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
- #define NUM2PTR(x) NUM2ULL(x)
- #endif
- rb_objspace_t *objspace = &rb_objspace;
- VALUE ptr;
- void *p0;
- rb_secure(4);
- ptr = NUM2PTR(objid);
- p0 = (void *)ptr;
- if (ptr == Qtrue) return Qtrue;
- if (ptr == Qfalse) return Qfalse;
- if (ptr == Qnil) return Qnil;
- if (FIXNUM_P(ptr)) return (VALUE)ptr;
- if (FLONUM_P(ptr)) return (VALUE)ptr;
- ptr = objid ^ FIXNUM_FLAG; /* unset FIXNUM_FLAG */
- if ((ptr % sizeof(RVALUE)) == (4 << 2)) {
- ID symid = ptr / sizeof(RVALUE);
- if (rb_id2name(symid) == 0)
- rb_raise(rb_eRangeError, "%p is not symbol id value", p0);
- return ID2SYM(symid);
- }
- if (!is_id_value(objspace, ptr)) {
- rb_raise(rb_eRangeError, "%p is not id value", p0);
- }
- if (!is_live_object(objspace, ptr)) {
- rb_raise(rb_eRangeError, "%p is recycled object", p0);
- }
- return (VALUE)ptr;
- }
- /*
- * Document-method: __id__
- * Document-method: object_id
- *
- * call-seq:
- * obj.__id__ -> fixnum
- * obj.object_id -> fixnum
- *
- * Returns an integer identifier for <i>obj</i>. The same number will
- * be returned on all calls to <code>id</code> for a given object, and
- * no two active objects will share an id.
- * <code>Object#object_id</code> is a different concept from the
- * <code>:name</code> notation, which returns the symbol id of
- * <code>name</code>. Replaces the deprecated <code>Object#id</code>.
- */
- /*
- * call-seq:
- * obj.hash -> fixnum
- *
- * Generates a <code>Fixnum</code> hash value for this object. This
- * function must have the property that <code>a.eql?(b)</code> implies
- * <code>a.hash == b.hash</code>. The hash value is used by class
- * <code>Hash</code>. Any hash value that exceeds the capacity of a
- * <code>Fixnum</code> will be truncated before being used.
- */
- VALUE
- rb_obj_id(VALUE obj)
- {
- /*
- * 32-bit VALUE space
- * MSB ------------------------ LSB
- * false 00000000000000000000000000000000
- * true 00000000000000000000000000000010
- * nil 00000000000000000000000000000100
- * undef 00000000000000000000000000000110
- * symbol ssssssssssssssssssssssss00001110
- * object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
- * fixnum fffffffffffffffffffffffffffffff1
- *
- * object_id space
- * LSB
- * false 00000000000000000000000000000000
- * true 00000000000000000000000000000010
- * nil 00000000000000000000000000000100
- * undef 00000000000000000000000000000110
- * symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
- * object oooooooooooooooooooooooooooooo0 o...o % A = 0
- * fixnum fffffffffffffffffffffffffffffff1 bignum if required
- *
- * where A = sizeof(RVALUE)/4
- *
- * sizeof(RVALUE) is
- * 20 if 32-bit, double is 4-byte aligned
- * 24 if 32-bit, double is 8-byte aligned
- * 40 if 64-bit
- */
- if (SYMBOL_P(obj)) {
- return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
- }
- else if (FLONUM_P(obj)) {
- #if SIZEOF_LONG == SIZEOF_VOIDP
- return LONG2NUM((SIGNED_VALUE)obj);
- #else
- return LL2NUM((SIGNED_VALUE)obj);
- #endif
- }
- else if (SPECIAL_CONST_P(obj)) {
- return LONG2NUM((SIGNED_VALUE)obj);
- }
- return nonspecial_obj_id(obj);
- }
- static int
- set_zero(st_data_t key, st_data_t val, st_data_t arg)
- {
- VALUE k = (VALUE)key;
- VALUE hash = (VALUE)arg;
- rb_hash_aset(hash, k, INT2FIX(0));
- return ST_CONTINUE;
- }
- /*
- * call-seq:
- * ObjectSpace.count_objects([result_hash]) -> hash
- *
- * Counts objects for each type.
- *
- * It returns a hash as:
- * {:TOTAL=>10000, :FREE=>3011, :T_OBJECT=>6, :T_CLASS=>404, ...}
- *
- * If the optional argument, result_hash, is given,
- * it is overwritten and returned.
- * This is intended to avoid probe effect.
- *
- * The contents of the returned hash is implementation defined.
- * It may be changed in future.
- *
- * This method is not expected to work except C Ruby.
- *
- */
- static VALUE
- count_objects(int argc, VALUE *argv, VALUE os)
- {
- rb_objspace_t *objspace = &rb_objspace;
- size_t counts[T_MASK+1];
- size_t freed = 0;
- size_t total = 0;
- size_t i;
- VALUE hash;
- if (rb_scan_args(argc, argv, "01", &hash) == 1) {
- if (!RB_TYPE_P(hash, T_HASH))
- rb_raise(rb_eTypeError, "non-hash given");
- }
- for (i = 0; i <= T_MASK; i++) {
- counts[i] = 0;
- }
- for (i = 0; i < heaps_used; i++) {
- RVALUE *p, *pend;
- p = objspace->heap.sorted[i].start; pend = objspace->heap.sorted[i].end;
- for (;p < pend; p++) {
- if (p->as.basic.flags) {
- counts[BUILTIN_TYPE(p)]++;
- }
- else {
- freed++;
- }
- }
- total += objspace->heap.sorted[i].slot->limit;
- }
- if (hash == Qnil) {
- hash = rb_hash_new();
- }
- else if (!RHASH_EMPTY_P(hash)) {
- st_foreach(RHASH_TBL(hash), set_zero, hash);
- }
- rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(total));
- rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), SIZET2NUM(freed));
- for (i = 0; i <= T_MASK; i++) {
- VALUE type;
- switch (i) {
- #define COUNT_TYPE(t) case (t): type = ID2SYM(rb_intern(#t)); break;
- COUNT_TYPE(T_NONE);
- COUNT_TYPE(T_OBJECT);
- COUNT_TYPE(T_CLASS);
- COUNT_TYPE(T_MODULE);
- COUNT_TYPE(T_FLOAT);
- COUNT_TYPE(T_STRING);
- COUNT_TYPE(T_REGEXP);
- COUNT_TYPE(T_ARRAY);
- COUNT_TYPE(T_HASH);
- COUNT_TYPE(T_STRUCT);
- COUNT_TYPE(T_BIGNUM);
- COUNT_TYPE(T_FILE);
- COUNT_TYPE(T_DATA);
- COUNT_TYPE(T_MATCH);
- COUNT_TYPE(T_COMPLEX);
- COUNT_TYPE(T_RATIONAL);
- COUNT_TYPE(T_NIL);
- COUNT_TYPE(T_TRUE);
- COUNT_TYPE(T_FALSE);
- COUNT_TYPE(T_SYMBOL);
- COUNT_TYPE(T_FIXNUM);
- COUNT_TYPE(T_UNDEF);
- COUNT_TYPE(T_NODE);
- COUNT_TYPE(T_ICLASS);
- COUNT_TYPE(T_ZOMBIE);
- #undef COUNT_TYPE
- default: type = INT2NUM(i); break;
- }
- if (counts[i])
- rb_hash_aset(hash, type, SIZET2NUM(counts[i]));
- }
- return hash;
- }
- /*
- ------------------------ Garbage Collection ------------------------
- */
- /* Sweeping */
- static VALUE
- lazy_sweep_enable(void)
- {
- rb_objspace_t *objspace = &rb_objspace;
- objspace->flags.dont_lazy_sweep = FALSE;
- return Qnil;
- }
- static void
- gc_clear_slot_bits(struct heaps_slot *slot)
- {
- memset(GET_HEAP_BITMAP(slot->slot), 0,
- HEAP_BITMAP_LIMIT * sizeof(uintptr_t));
- }
- static void
- slot_sweep(rb_objspace_t *objspace, struct heaps_slot *sweep_slot)
- {
- size_t free_num = 0, final_num = 0;
- RVALUE *p, *pend;
- RVALUE *final = deferred_final_list;
- int deferred;
- uintptr_t *bits;
- p = sweep_slot->slot; pend = p + sweep_slot->limit;
- bits = GET_HEAP_BITMAP(p);
- while (p < pend) {
- if ((!(MARKED_IN_BITMAP(bits, p))) && BUILTIN_TYPE(p) != T_ZOMBIE) {
- if (p->as.basic.flags) {
- if ((deferred = obj_free(objspace, (VALUE)p)) ||
- (FL_TEST(p, FL_FINALIZE))) {
- if (!deferred) {
- p->as.free.flags = T_ZOMBIE;
- RDATA(p)->dfree = 0;
- }
- p->as.free.next = deferred_final_list;
- deferred_final_list = p;
- assert(BUILTIN_TYPE(p) == T_ZOMBIE);
- final_num++;
- }
- else {
- VALGRIND_MAKE_MEM_UNDEFINED((void*)p, sizeof(RVALUE));
- p->as.free.flags = 0;
- p->as.free.next = sweep_slot->freelist;
- sweep_slot->freelist = p;
- free_num++;
- }
- }
- else {
- free_num++;
- }
- }
- p++;
- }
- gc_clear_slot_bits(sweep_slot);
- if (final_num + free_num == sweep_slot->limit &&
- objspace->heap.free_num > objspace->heap.do_heap_free) {
- RVALUE *pp;
- for (pp = deferred_final_list; pp != final; pp = pp->as.free.next) {
- RDATA(pp)->dmark = (void (*)(void *))(VALUE)sweep_slot;
- pp->as.free.flags |= FL_SINGLETON; /* freeing page mark */
- }
- sweep_slot->limit = final_num;
- unlink_heap_slot(objspace, sweep_slot);
- }
- else {
- if (free_num > 0) {
- link_free_heap_slot(objspace, sweep_slot);
- }
- else {
- sweep_slot->free_next = NULL;
- }
- objspace->heap.free_num += free_num;
- }
- objspace->heap.final_num += final_num;
- if (deferred_final_list && !finalizing) {
- rb_thread_t *th = GET_THREAD();
- if (th) {
- RUBY_VM_SET_FINALIZER_INTERRUPT(th);
- }
- }
- }
- static int
- ready_to_gc(rb_objspace_t *objspace)
- {
- if (dont_gc || during_gc) {
- if (!has_free_object) {
- if (!heaps_increment(objspace)) {
- set_heaps_increment(objspace);
- heaps_increment(objspace);
- }
- }
- return FALSE;
- }
- return TRUE;
- }
- static void
- before_gc_sweep(rb_objspace_t *objspace)
- {
- objspace->heap.do_heap_free = (size_t)((heaps_used * HEAP_OBJ_LIMIT) * 0.65);
- objspace->heap.free_min = (size_t)((heaps_used * HEAP_OBJ_LIMIT) * 0.2);
- if (objspace->heap.free_min < initial_free_min) {
- objspace->heap.do_heap_free = heaps_used * HEAP_OBJ_LIMIT;
- objspace->heap.free_min = initial_free_min;
- }
- objspace->heap.sweep_slots = heaps;
- objspace->heap.free_num = 0;
- objspace->heap.free_slots = NULL;
- /* sweep unlinked method entries */
- if (GET_VM()->unlinked_method_entry_list) {
- rb_sweep_method_entry(GET_VM());
- }
- }
- static void
- after_gc_sweep(rb_objspace_t *objspace)
- {
- size_t inc;
- gc_prof_set_malloc_info(objspace);
- if (objspace->heap.free_num < objspace->heap.free_min) {
- set_heaps_increment(objspace);
- heaps_increment(objspace);
- }
- inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0);
- if (inc > malloc_limit) {
- malloc_limit += (size_t)((inc - malloc_limit) * (double)objspace->heap.live_num / (heaps_used * HEAP_OBJ_LIMIT));
- …
Large files files are truncated, but you can click here to view the full file