2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
gc.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Oct 5 09:44:46 JST 1993
|
|
|
|
|
2003-01-16 02:34:03 -05:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-19 23:59:39 -05:00
|
|
|
#include "rubysig.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
#include "st.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "re.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <setjmp.h>
|
2001-11-19 09:42:45 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-11-19 00:03:03 -05:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-01 06:57:32 -04:00
|
|
|
#ifdef __ia64__
|
|
|
|
#include <ucontext.h>
|
2003-08-02 02:45:57 -04:00
|
|
|
#pragma weak __libc_ia64_register_backing_store_base
|
2003-08-01 06:57:32 -04:00
|
|
|
extern unsigned long __libc_ia64_register_backing_store_base;
|
|
|
|
#endif
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void re_free_registers _((struct re_registers*));
|
|
|
|
void rb_io_fptr_finalize _((struct OpenFile*));
|
|
|
|
|
2002-08-12 03:39:12 -04:00
|
|
|
#if !defined(setjmp) && defined(HAVE__SETJMP)
|
1998-01-16 07:13:05 -05:00
|
|
|
#define setjmp(env) _setjmp(env)
|
|
|
|
#endif
|
|
|
|
|
2001-02-11 02:12:02 -05:00
|
|
|
/* Make alloca work the best possible way. */
|
2002-10-25 14:39:30 -04:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# ifndef atarist
|
|
|
|
# ifndef alloca
|
|
|
|
# define alloca __builtin_alloca
|
|
|
|
# endif
|
|
|
|
# endif /* atarist */
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALLOCA_H
|
2001-02-11 02:12:02 -05:00
|
|
|
# include <alloca.h>
|
2001-03-26 03:57:16 -05:00
|
|
|
# else
|
|
|
|
# ifdef _AIX
|
2002-10-25 14:39:30 -04:00
|
|
|
#pragma alloca
|
2001-03-26 03:57:16 -05:00
|
|
|
# else
|
|
|
|
# ifndef alloca /* predefined by HP cc +Olibcalls */
|
|
|
|
void *alloca ();
|
|
|
|
# endif
|
2002-10-25 14:39:30 -04:00
|
|
|
# endif /* AIX */
|
|
|
|
# endif /* HAVE_ALLOCA_H */
|
|
|
|
#endif /* __GNUC__ */
|
2001-02-11 02:12:02 -05:00
|
|
|
|
2002-10-10 11:26:58 -04:00
|
|
|
#ifndef GC_MALLOC_LIMIT
|
|
|
|
#if defined(MSDOS) || defined(__human68k__)
|
|
|
|
#define GC_MALLOC_LIMIT 200000
|
|
|
|
#else
|
|
|
|
#define GC_MALLOC_LIMIT 8000000
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-10-10 13:11:18 -04:00
|
|
|
static unsigned long malloc_increase = 0;
|
2002-10-10 11:26:58 -04:00
|
|
|
static unsigned long malloc_limit = GC_MALLOC_LIMIT;
|
1998-01-16 07:13:05 -05:00
|
|
|
static void run_final();
|
2002-04-24 00:54:16 -04:00
|
|
|
static VALUE nomem_error;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
void
|
|
|
|
rb_memerror()
|
1999-12-14 01:50:43 -05:00
|
|
|
{
|
2000-12-28 21:47:07 -05:00
|
|
|
static int recurse = 0;
|
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
if (recurse > 0 && rb_safe_level() < 4) {
|
|
|
|
fprintf(stderr, "[FATAL] failed to allocate memory\n");
|
|
|
|
exit(1);
|
1999-12-14 01:50:43 -05:00
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
recurse++;
|
|
|
|
rb_exc_raise(nomem_error);
|
1999-12-14 01:50:43 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void *
|
2000-05-15 22:46:57 -04:00
|
|
|
ruby_xmalloc(size)
|
2000-11-27 04:23:38 -05:00
|
|
|
long size;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
void *mem;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (size < 0) {
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_raise(rb_eNoMemError, "negative allocation size (or too big)");
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (size == 0) size = 1;
|
2002-10-10 13:11:18 -04:00
|
|
|
malloc_increase += size;
|
2000-09-25 13:51:29 -04:00
|
|
|
|
2002-10-10 13:11:18 -04:00
|
|
|
if (malloc_increase > malloc_limit) {
|
2002-10-10 11:26:58 -04:00
|
|
|
rb_gc();
|
|
|
|
}
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(mem = malloc(size));
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!mem) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc();
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(mem = malloc(size));
|
1999-12-14 01:50:43 -05:00
|
|
|
if (!mem) {
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_memerror();
|
1999-12-14 01:50:43 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2000-05-15 22:46:57 -04:00
|
|
|
ruby_xcalloc(n, size)
|
2000-11-27 04:23:38 -05:00
|
|
|
long n, size;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
mem = xmalloc(n * size);
|
|
|
|
memset(mem, 0, n * size);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2000-05-15 22:46:57 -04:00
|
|
|
ruby_xrealloc(ptr, size)
|
1998-01-16 07:13:05 -05:00
|
|
|
void *ptr;
|
2000-11-27 04:23:38 -05:00
|
|
|
long size;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
void *mem;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (size < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative re-allocation size");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!ptr) return xmalloc(size);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (size == 0) size = 1;
|
2002-10-10 13:11:18 -04:00
|
|
|
malloc_increase += size;
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(mem = realloc(ptr, size));
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!mem) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc();
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(mem = realloc(ptr, size));
|
2001-07-20 11:20:25 -04:00
|
|
|
if (!mem) {
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_memerror();
|
2001-07-20 11:20:25 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
void
|
2000-05-15 22:46:57 -04:00
|
|
|
ruby_xfree(x)
|
1999-08-13 01:45:20 -04:00
|
|
|
void *x;
|
|
|
|
{
|
2000-11-14 02:10:31 -05:00
|
|
|
if (x)
|
|
|
|
RUBY_CRITICAL(free(x));
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
extern int ruby_in_compile;
|
1998-01-16 07:13:05 -05:00
|
|
|
static int dont_gc;
|
1999-01-19 23:59:39 -05:00
|
|
|
static int during_gc;
|
|
|
|
static int need_call_final = 0;
|
2000-07-15 09:37:03 -04:00
|
|
|
static st_table *finalizer_table = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-01-29 00:10:42 -05:00
|
|
|
VALUE
|
|
|
|
rb_gc_enable()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
int old = dont_gc;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
dont_gc = Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2001-01-29 00:10:42 -05:00
|
|
|
VALUE
|
|
|
|
rb_gc_disable()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
int old = dont_gc;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
dont_gc = Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_mGC;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static struct gc_list {
|
|
|
|
VALUE *varptr;
|
|
|
|
struct gc_list *next;
|
2001-10-31 01:53:22 -05:00
|
|
|
} *global_List = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_gc_register_address(addr)
|
|
|
|
VALUE *addr;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
struct gc_list *tmp;
|
|
|
|
|
|
|
|
tmp = ALLOC(struct gc_list);
|
2001-10-31 01:53:22 -05:00
|
|
|
tmp->next = global_List;
|
2000-02-08 03:54:01 -05:00
|
|
|
tmp->varptr = addr;
|
2001-10-31 01:53:22 -05:00
|
|
|
global_List = tmp;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
void
|
|
|
|
rb_gc_unregister_address(addr)
|
|
|
|
VALUE *addr;
|
|
|
|
{
|
2001-10-31 01:53:22 -05:00
|
|
|
struct gc_list *tmp = global_List;
|
2000-02-08 03:54:01 -05:00
|
|
|
|
|
|
|
if (tmp->varptr == addr) {
|
2001-10-31 01:53:22 -05:00
|
|
|
global_List = tmp->next;
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(tmp));
|
2000-02-08 03:54:01 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (tmp->next) {
|
|
|
|
if (tmp->next->varptr == addr) {
|
|
|
|
struct gc_list *t = tmp->next;
|
|
|
|
|
|
|
|
tmp->next = tmp->next->next;
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(t));
|
2000-02-08 03:54:01 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_global_variable(var)
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
rb_gc_register_address(var);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
typedef struct RVALUE {
|
|
|
|
union {
|
|
|
|
struct {
|
2001-02-02 06:38:20 -05:00
|
|
|
unsigned long flags; /* always 0 for freed obj */
|
1998-01-16 07:13:05 -05:00
|
|
|
struct RVALUE *next;
|
|
|
|
} free;
|
|
|
|
struct RBasic basic;
|
|
|
|
struct RObject object;
|
1999-01-19 23:59:39 -05:00
|
|
|
struct RClass klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
struct RFloat flonum;
|
|
|
|
struct RString string;
|
|
|
|
struct RArray array;
|
|
|
|
struct RRegexp regexp;
|
|
|
|
struct RHash hash;
|
|
|
|
struct RData data;
|
|
|
|
struct RStruct rstruct;
|
|
|
|
struct RBignum bignum;
|
|
|
|
struct RFile file;
|
|
|
|
struct RNode node;
|
|
|
|
struct RMatch match;
|
|
|
|
struct RVarmap varmap;
|
|
|
|
struct SCOPE scope;
|
|
|
|
} as;
|
|
|
|
} RVALUE;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static RVALUE *freelist = 0;
|
2000-11-20 03:26:48 -05:00
|
|
|
static RVALUE *deferred_final_list = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#define HEAPS_INCREMENT 10
|
|
|
|
static RVALUE **heaps;
|
|
|
|
static int heaps_length = 0;
|
|
|
|
static int heaps_used = 0;
|
|
|
|
|
2001-07-02 04:46:28 -04:00
|
|
|
#define HEAP_MIN_SLOTS 10000
|
|
|
|
static int *heaps_limits;
|
|
|
|
static int heap_slots = HEAP_MIN_SLOTS;
|
|
|
|
|
2000-06-28 04:31:35 -04:00
|
|
|
#define FREE_MIN 4096
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static RVALUE *himem, *lomem;
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_heap()
|
|
|
|
{
|
|
|
|
RVALUE *p, *pend;
|
|
|
|
|
|
|
|
if (heaps_used == heaps_length) {
|
|
|
|
/* Realloc heaps */
|
|
|
|
heaps_length += HEAPS_INCREMENT;
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(heaps = (heaps_used>0)?
|
|
|
|
(RVALUE**)realloc(heaps, heaps_length*sizeof(RVALUE*)):
|
|
|
|
(RVALUE**)malloc(heaps_length*sizeof(RVALUE*)));
|
2002-04-24 00:54:16 -04:00
|
|
|
if (heaps == 0) rb_memerror();
|
2001-07-02 04:46:28 -04:00
|
|
|
RUBY_CRITICAL(heaps_limits = (heaps_used>0)?
|
|
|
|
(int*)realloc(heaps_limits, heaps_length*sizeof(int)):
|
|
|
|
(int*)malloc(heaps_length*sizeof(int)));
|
2002-04-24 00:54:16 -04:00
|
|
|
if (heaps_limits == 0) rb_memerror();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-07-02 04:46:28 -04:00
|
|
|
for (;;) {
|
|
|
|
RUBY_CRITICAL(p = heaps[heaps_used] = (RVALUE*)malloc(sizeof(RVALUE)*heap_slots));
|
|
|
|
heaps_limits[heaps_used] = heap_slots;
|
|
|
|
if (p == 0) {
|
|
|
|
if (heap_slots == HEAP_MIN_SLOTS) {
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_memerror();
|
2001-07-02 04:46:28 -04:00
|
|
|
}
|
|
|
|
heap_slots = HEAP_MIN_SLOTS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pend = p + heap_slots;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (lomem == 0 || lomem > p) lomem = p;
|
|
|
|
if (himem < pend) himem = pend;
|
2001-07-02 04:46:28 -04:00
|
|
|
heaps_used++;
|
2001-11-02 02:20:58 -05:00
|
|
|
heap_slots *= 1.8;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (p < pend) {
|
2001-02-02 06:38:20 -05:00
|
|
|
p->as.free.flags = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
p->as.free.next = freelist;
|
|
|
|
freelist = p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
#define RANY(o) ((RVALUE*)(o))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_newobj()
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj;
|
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
if (!freelist) rb_gc();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
obj = (VALUE)freelist;
|
|
|
|
freelist = freelist->as.free.next;
|
2000-08-09 00:32:24 -04:00
|
|
|
MEMZERO((void*)obj, RVALUE, 1);
|
2000-08-07 01:05:04 -04:00
|
|
|
return obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_data_object_alloc(klass, datap, dmark, dfree)
|
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
void *datap;
|
2000-06-26 11:16:03 -04:00
|
|
|
RUBY_DATA_FUNC dmark;
|
|
|
|
RUBY_DATA_FUNC dfree;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
NEWOBJ(data, struct RData);
|
2003-08-13 22:28:14 -04:00
|
|
|
Check_Type(klass, T_CLASS);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(data, klass, T_DATA);
|
1998-01-16 07:13:05 -05:00
|
|
|
data->data = datap;
|
|
|
|
data->dfree = dfree;
|
|
|
|
data->dmark = dmark;
|
|
|
|
|
|
|
|
return (VALUE)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern st_table *rb_class_tbl;
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE *rb_gc_stack_start = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-04-25 05:45:43 -04:00
|
|
|
#if defined(DJGPP) || defined(_WIN32_WCE)
|
2001-11-19 00:03:03 -05:00
|
|
|
static unsigned int STACK_LEVEL_MAX = 65535;
|
|
|
|
#else
|
|
|
|
#ifdef __human68k__
|
|
|
|
extern unsigned int _stacksize;
|
|
|
|
# define STACK_LEVEL_MAX (_stacksize - 4096)
|
|
|
|
# undef HAVE_GETRLIMIT
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
static unsigned int STACK_LEVEL_MAX = 655300;
|
|
|
|
#else
|
|
|
|
# define STACK_LEVEL_MAX 655300
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef C_ALLOCA
|
|
|
|
# define SET_STACK_END VALUE stack_end; alloca(0);
|
|
|
|
# define STACK_END (&stack_end)
|
|
|
|
#else
|
2003-08-01 06:57:32 -04:00
|
|
|
# if defined(__GNUC__) && defined(USE_BUILTIN_FRAME_ADDRESS) && !defined(__ia64__)
|
2002-04-25 09:57:01 -04:00
|
|
|
# define SET_STACK_END VALUE *stack_end = __builtin_frame_address(0)
|
2001-11-19 00:03:03 -05:00
|
|
|
# else
|
2002-04-25 09:57:01 -04:00
|
|
|
# define SET_STACK_END VALUE *stack_end = alloca(1)
|
2001-11-19 00:03:03 -05:00
|
|
|
# endif
|
|
|
|
# define STACK_END (stack_end)
|
|
|
|
#endif
|
2003-01-04 13:48:24 -05:00
|
|
|
#if defined(sparc) || defined(__sparc__)
|
2001-11-19 00:03:03 -05:00
|
|
|
# define STACK_LENGTH (rb_gc_stack_start - STACK_END + 0x80)
|
2003-06-27 23:29:00 -04:00
|
|
|
#elif STACK_GROW_DIRECTION < 0
|
|
|
|
# define STACK_LENGTH (rb_gc_stack_start - STACK_END)
|
|
|
|
#elif STACK_GROW_DIRECTION > 0
|
2003-08-01 10:57:48 -04:00
|
|
|
# define STACK_LENGTH (STACK_END - rb_gc_stack_start + 1)
|
2001-11-19 00:03:03 -05:00
|
|
|
#else
|
|
|
|
# define STACK_LENGTH ((STACK_END < rb_gc_stack_start) ? rb_gc_stack_start - STACK_END\
|
2003-08-01 10:57:48 -04:00
|
|
|
: STACK_END - rb_gc_stack_start + 1)
|
2001-11-19 00:03:03 -05:00
|
|
|
#endif
|
2003-06-27 23:29:00 -04:00
|
|
|
#if STACK_GROW_DIRECTION > 0
|
|
|
|
# define STACK_UPPER(x, a, b) a
|
|
|
|
#elif STACK_GROW_DIRECTION < 0
|
|
|
|
# define STACK_UPPER(x, a, b) b
|
|
|
|
#else
|
|
|
|
static int
|
|
|
|
stack_growup_p(addr)
|
|
|
|
VALUE *addr;
|
|
|
|
{
|
|
|
|
SET_STACK_END;
|
|
|
|
|
|
|
|
if (STACK_END > addr) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
# define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b)
|
|
|
|
#endif
|
2001-11-19 00:03:03 -05:00
|
|
|
|
2003-04-09 02:44:34 -04:00
|
|
|
#define GC_WARTER_MARK 512
|
|
|
|
|
2001-11-19 00:03:03 -05:00
|
|
|
#define CHECK_STACK(ret) do {\
|
|
|
|
SET_STACK_END;\
|
2003-04-09 02:44:34 -04:00
|
|
|
(ret) = (STACK_LENGTH > STACK_LEVEL_MAX + GC_WARTER_MARK);\
|
2002-04-25 09:57:01 -04:00
|
|
|
} while (0)
|
2001-11-19 00:03:03 -05:00
|
|
|
|
|
|
|
int
|
|
|
|
ruby_stack_length(p)
|
|
|
|
VALUE **p;
|
|
|
|
{
|
|
|
|
SET_STACK_END;
|
2003-06-27 23:29:00 -04:00
|
|
|
if (p) *p = STACK_UPPER(STACK_END, rb_gc_stack_start, STACK_END);
|
2001-11-19 00:03:03 -05:00
|
|
|
return STACK_LENGTH;
|
|
|
|
}
|
|
|
|
|
2001-11-21 10:42:12 -05:00
|
|
|
int
|
2001-11-19 00:03:03 -05:00
|
|
|
ruby_stack_check()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CHECK_STACK(ret);
|
2001-11-21 10:42:12 -05:00
|
|
|
return ret;
|
2001-11-19 00:03:03 -05:00
|
|
|
}
|
2001-11-13 03:19:52 -05:00
|
|
|
|
|
|
|
#define MARK_STACK_MAX 1024
|
|
|
|
static VALUE mark_stack[MARK_STACK_MAX];
|
|
|
|
static VALUE *mark_stack_ptr;
|
|
|
|
static int mark_stack_overflow;
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_mark_stack()
|
|
|
|
{
|
|
|
|
mark_stack_overflow = 0;
|
|
|
|
mark_stack_ptr = mark_stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
|
|
|
|
|
2002-03-07 06:19:37 -05:00
|
|
|
static st_table *source_filenames;
|
|
|
|
|
|
|
|
char *
|
|
|
|
rb_source_filename(f)
|
|
|
|
const char *f;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
|
* st.h, st.c: Introduce new conventional typedef's, st_data_t,
st_compare_func_t, st_hash_func_t and st_each_func_t.
* st.h, st.c: Do explicit function declarations and do not rely on
implicit declarations. On such platforms as IA64, int argument
values are NOT automatically promoted to long (64bit) values, so
explicit declarations are mandatory for those functions that
take long values or pointers. This fixes miniruby's coredump on
FreeBSD/IA64.
* class.c, eval.c, gc.c, hash.c, marshal.c, parse.y, variable.c:
Add proper casts to avoid warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-01-06 10:55:43 -05:00
|
|
|
if (!st_lookup(source_filenames, (st_data_t)f, (st_data_t *)&name)) {
|
2002-03-07 06:19:37 -05:00
|
|
|
long len = strlen(f) + 1;
|
|
|
|
char *ptr = name = ALLOC_N(char, len + 1);
|
|
|
|
*ptr++ = 0;
|
|
|
|
MEMCPY(ptr, f, char, len);
|
* st.h, st.c: Introduce new conventional typedef's, st_data_t,
st_compare_func_t, st_hash_func_t and st_each_func_t.
* st.h, st.c: Do explicit function declarations and do not rely on
implicit declarations. On such platforms as IA64, int argument
values are NOT automatically promoted to long (64bit) values, so
explicit declarations are mandatory for those functions that
take long values or pointers. This fixes miniruby's coredump on
FreeBSD/IA64.
* class.c, eval.c, gc.c, hash.c, marshal.c, parse.y, variable.c:
Add proper casts to avoid warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-01-06 10:55:43 -05:00
|
|
|
st_add_direct(source_filenames, (st_data_t)ptr, (st_data_t)name);
|
2002-03-07 06:19:37 -05:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return name + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_source_filename(f)
|
|
|
|
char *f;
|
|
|
|
{
|
|
|
|
if (f) {
|
|
|
|
f[-1] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* st.h, st.c: Introduce new conventional typedef's, st_data_t,
st_compare_func_t, st_hash_func_t and st_each_func_t.
* st.h, st.c: Do explicit function declarations and do not rely on
implicit declarations. On such platforms as IA64, int argument
values are NOT automatically promoted to long (64bit) values, so
explicit declarations are mandatory for those functions that
take long values or pointers. This fixes miniruby's coredump on
FreeBSD/IA64.
* class.c, eval.c, gc.c, hash.c, marshal.c, parse.y, variable.c:
Add proper casts to avoid warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-01-06 10:55:43 -05:00
|
|
|
static int
|
2002-03-07 06:19:37 -05:00
|
|
|
sweep_source_filename(key, value)
|
|
|
|
char *key, *value;
|
|
|
|
{
|
|
|
|
if (*value) {
|
|
|
|
*value = 0;
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
free(value);
|
|
|
|
return ST_DELETE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-09 04:27:01 -04:00
|
|
|
static void rb_gc_mark_children _((VALUE ptr));
|
2003-04-09 02:44:34 -04:00
|
|
|
|
2001-11-13 03:19:52 -05:00
|
|
|
static void
|
|
|
|
gc_mark_all()
|
|
|
|
{
|
|
|
|
RVALUE *p, *pend;
|
|
|
|
int i;
|
2001-11-27 05:00:35 -05:00
|
|
|
|
|
|
|
init_mark_stack();
|
|
|
|
for (i = 0; i < heaps_used; i++) {
|
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
|
|
|
while (p < pend) {
|
|
|
|
if ((p->as.basic.flags & FL_MARK) &&
|
|
|
|
(p->as.basic.flags != FL_MARK)) {
|
2003-04-09 04:27:01 -04:00
|
|
|
rb_gc_mark_children((VALUE)p);
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
2001-11-27 05:00:35 -05:00
|
|
|
p++;
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-09 04:27:01 -04:00
|
|
|
static void
|
|
|
|
gc_mark_rest()
|
|
|
|
{
|
|
|
|
VALUE tmp_arry[MARK_STACK_MAX];
|
|
|
|
VALUE *p;
|
|
|
|
|
|
|
|
p = (mark_stack_ptr - mark_stack) + tmp_arry;
|
|
|
|
MEMCPY(tmp_arry, mark_stack, VALUE, MARK_STACK_MAX);
|
|
|
|
|
|
|
|
init_mark_stack();
|
|
|
|
while(p != tmp_arry){
|
|
|
|
p--;
|
|
|
|
rb_gc_mark_children(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-10 05:07:31 -05:00
|
|
|
static inline int
|
2000-05-09 00:53:16 -04:00
|
|
|
is_pointer_to_heap(ptr)
|
1998-01-16 07:19:22 -05:00
|
|
|
void *ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
register RVALUE *p = RANY(ptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
register RVALUE *heap_org;
|
|
|
|
register long i;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (p < lomem || p > himem) return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* check if p looks like a pointer */
|
|
|
|
for (i=0; i < heaps_used; i++) {
|
|
|
|
heap_org = heaps[i];
|
2001-07-02 04:46:28 -04:00
|
|
|
if (heap_org <= p && p < heap_org + heaps_limits[i] &&
|
|
|
|
((((char*)p)-((char*)heap_org))%sizeof(RVALUE)) == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_locations_array(x, n)
|
1999-01-19 23:59:39 -05:00
|
|
|
register VALUE *x;
|
|
|
|
register long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
while (n--) {
|
2002-05-14 01:59:35 -04:00
|
|
|
if (is_pointer_to_heap((void *)*x)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark(*x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_locations(start, end)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *start, *end;
|
|
|
|
{
|
|
|
|
VALUE *tmp;
|
|
|
|
long n;
|
|
|
|
|
2003-06-27 23:29:00 -04:00
|
|
|
n = end - start;
|
1998-01-16 07:13:05 -05:00
|
|
|
mark_locations_array(start,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mark_entry(key, value)
|
|
|
|
ID key;
|
|
|
|
VALUE value;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark(value);
|
1998-01-16 07:13:05 -05:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
rb_mark_tbl(tbl)
|
1998-01-16 07:13:05 -05:00
|
|
|
st_table *tbl;
|
|
|
|
{
|
|
|
|
if (!tbl) return;
|
|
|
|
st_foreach(tbl, mark_entry, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-02-27 03:04:32 -05:00
|
|
|
mark_keyvalue(key, value)
|
1999-12-14 01:50:43 -05:00
|
|
|
VALUE key;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE value;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark(key);
|
|
|
|
rb_gc_mark(value);
|
1998-01-16 07:13:05 -05:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
rb_mark_hash(tbl)
|
1998-01-16 07:13:05 -05:00
|
|
|
st_table *tbl;
|
|
|
|
{
|
|
|
|
if (!tbl) return;
|
2003-02-27 03:04:32 -05:00
|
|
|
st_foreach(tbl, mark_keyvalue, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_maybe(obj)
|
2001-03-18 22:20:24 -05:00
|
|
|
VALUE obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-05-14 01:59:35 -04:00
|
|
|
if (is_pointer_to_heap((void *)obj)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark(obj);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 03:19:52 -05:00
|
|
|
void
|
|
|
|
rb_gc_mark(ptr)
|
2001-03-18 22:20:24 -05:00
|
|
|
VALUE ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-11-27 05:00:35 -05:00
|
|
|
int ret;
|
2003-02-20 19:28:04 -05:00
|
|
|
register RVALUE *obj;
|
2001-11-27 05:00:35 -05:00
|
|
|
|
2003-04-09 04:27:01 -04:00
|
|
|
obj = RANY(ptr);
|
|
|
|
if (rb_special_const_p(ptr)) return; /* special const not marked */
|
|
|
|
if (obj->as.basic.flags == 0) return; /* free cell */
|
|
|
|
if (obj->as.basic.flags & FL_MARK) return; /* already marked */
|
|
|
|
obj->as.basic.flags |= FL_MARK;
|
|
|
|
|
2001-11-27 05:00:35 -05:00
|
|
|
CHECK_STACK(ret);
|
|
|
|
if (ret) {
|
|
|
|
if (!mark_stack_overflow) {
|
2001-11-13 03:19:52 -05:00
|
|
|
if (mark_stack_ptr - mark_stack < MARK_STACK_MAX) {
|
|
|
|
*mark_stack_ptr = ptr;
|
2001-11-27 05:00:35 -05:00
|
|
|
mark_stack_ptr++;
|
|
|
|
}
|
|
|
|
else {
|
2001-11-13 03:19:52 -05:00
|
|
|
mark_stack_overflow = 1;
|
|
|
|
}
|
|
|
|
}
|
2003-02-20 19:28:04 -05:00
|
|
|
return;
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
2003-04-09 04:27:01 -04:00
|
|
|
rb_gc_mark_children(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_gc_mark_children(ptr)
|
|
|
|
VALUE ptr;
|
|
|
|
{
|
|
|
|
register RVALUE *obj = RANY(ptr);
|
|
|
|
|
|
|
|
goto marking; /* skip */
|
2001-11-13 03:19:52 -05:00
|
|
|
|
2003-02-20 19:28:04 -05:00
|
|
|
again:
|
|
|
|
obj = RANY(ptr);
|
|
|
|
if (rb_special_const_p(ptr)) return; /* special const not marked */
|
|
|
|
if (obj->as.basic.flags == 0) return; /* free cell */
|
|
|
|
if (obj->as.basic.flags & FL_MARK) return; /* already marked */
|
|
|
|
obj->as.basic.flags |= FL_MARK;
|
|
|
|
|
2003-04-09 04:27:01 -04:00
|
|
|
marking:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FL_TEST(obj, FL_EXIVAR)) {
|
2003-02-20 19:28:04 -05:00
|
|
|
rb_mark_generic_ivar(ptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (obj->as.basic.flags & T_MASK) {
|
|
|
|
case T_NIL:
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("rb_gc_mark() called for broken object");
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NODE:
|
2002-03-07 06:19:37 -05:00
|
|
|
mark_source_filename(obj->as.node.nd_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (nd_type(obj)) {
|
|
|
|
case NODE_IF: /* 1,2,3 */
|
|
|
|
case NODE_FOR:
|
|
|
|
case NODE_ITER:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_CREF:
|
|
|
|
case NODE_WHEN:
|
|
|
|
case NODE_MASGN:
|
|
|
|
case NODE_RESCUE:
|
|
|
|
case NODE_RESBODY:
|
2003-02-19 04:27:49 -05:00
|
|
|
case NODE_CLASS:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u2.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
/* fall through */
|
|
|
|
case NODE_BLOCK: /* 1,3 */
|
|
|
|
case NODE_ARRAY:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DREGX_ONCE:
|
|
|
|
case NODE_FBODY:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_ENSURE:
|
1998-01-16 07:13:05 -05:00
|
|
|
case NODE_CALL:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_DEFS:
|
|
|
|
case NODE_OP_ASGN1:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u1.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
/* fall through */
|
|
|
|
case NODE_SUPER: /* 3 */
|
|
|
|
case NODE_FCALL:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_DEFN:
|
1998-01-16 07:13:05 -05:00
|
|
|
case NODE_NEWLINE:
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.node.u3.node;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case NODE_WHILE: /* 1,2 */
|
|
|
|
case NODE_UNTIL:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_AND:
|
|
|
|
case NODE_OR:
|
|
|
|
case NODE_CASE:
|
|
|
|
case NODE_SCLASS:
|
|
|
|
case NODE_DOT2:
|
|
|
|
case NODE_DOT3:
|
|
|
|
case NODE_FLIP2:
|
|
|
|
case NODE_FLIP3:
|
1998-01-16 07:19:22 -05:00
|
|
|
case NODE_MATCH2:
|
|
|
|
case NODE_MATCH3:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_OP_ASGN_OR:
|
|
|
|
case NODE_OP_ASGN_AND:
|
2003-02-19 04:27:49 -05:00
|
|
|
case NODE_MODULE:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u1.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
/* fall through */
|
|
|
|
case NODE_METHOD: /* 2 */
|
|
|
|
case NODE_NOT:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_GASGN:
|
|
|
|
case NODE_LASGN:
|
|
|
|
case NODE_DASGN:
|
2000-01-31 22:12:21 -05:00
|
|
|
case NODE_DASGN_CURR:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_IASGN:
|
2000-08-28 05:53:42 -04:00
|
|
|
case NODE_CVDECL:
|
2000-11-21 09:26:25 -05:00
|
|
|
case NODE_CVASGN:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_COLON3:
|
|
|
|
case NODE_OPT_N:
|
2002-07-26 02:12:39 -04:00
|
|
|
case NODE_EVSTR:
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.node.u2.node;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case NODE_HASH: /* 1 */
|
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DEFINED:
|
1998-01-16 07:19:22 -05:00
|
|
|
case NODE_MATCH:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_RETURN:
|
2001-07-02 04:46:28 -04:00
|
|
|
case NODE_BREAK:
|
|
|
|
case NODE_NEXT:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_YIELD:
|
|
|
|
case NODE_COLON2:
|
1999-08-13 01:45:20 -04:00
|
|
|
case NODE_ARGS:
|
2003-03-26 02:01:14 -05:00
|
|
|
case NODE_SPLAT:
|
|
|
|
case NODE_SVALUE:
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.node.u1.node;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case NODE_SCOPE: /* 2,3 */
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_BLOCK_PASS:
|
2003-02-19 22:35:44 -05:00
|
|
|
case NODE_CDECL:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u3.node);
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.node.u2.node;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case NODE_ZARRAY: /* - */
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_ZSUPER:
|
1998-01-16 07:13:05 -05:00
|
|
|
case NODE_CFUNC:
|
|
|
|
case NODE_VCALL:
|
|
|
|
case NODE_GVAR:
|
|
|
|
case NODE_LVAR:
|
|
|
|
case NODE_DVAR:
|
|
|
|
case NODE_IVAR:
|
|
|
|
case NODE_CVAR:
|
|
|
|
case NODE_NTH_REF:
|
|
|
|
case NODE_BACK_REF:
|
|
|
|
case NODE_ALIAS:
|
|
|
|
case NODE_VALIAS:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_REDO:
|
|
|
|
case NODE_RETRY:
|
1998-01-16 07:13:05 -05:00
|
|
|
case NODE_UNDEF:
|
|
|
|
case NODE_SELF:
|
|
|
|
case NODE_NIL:
|
1999-01-19 23:59:39 -05:00
|
|
|
case NODE_TRUE:
|
|
|
|
case NODE_FALSE:
|
|
|
|
case NODE_ATTRSET:
|
|
|
|
case NODE_BLOCK_ARG:
|
1998-01-16 07:19:22 -05:00
|
|
|
case NODE_POSTEXE:
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef C_ALLOCA
|
|
|
|
case NODE_ALLOCA:
|
|
|
|
mark_locations_array((VALUE*)obj->as.node.u1.value,
|
|
|
|
obj->as.node.u3.cnt);
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.node.u2.node;
|
|
|
|
goto again;
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-20 19:28:04 -05:00
|
|
|
default: /* unlisted NODE */
|
2000-05-09 00:53:16 -04:00
|
|
|
if (is_pointer_to_heap(obj->as.node.u1.node)) {
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u1.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-09 00:53:16 -04:00
|
|
|
if (is_pointer_to_heap(obj->as.node.u2.node)) {
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u2.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-09 00:53:16 -04:00
|
|
|
if (is_pointer_to_heap(obj->as.node.u3.node)) {
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark((VALUE)obj->as.node.u3.node);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return; /* no need to mark class. */
|
|
|
|
}
|
|
|
|
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark(obj->as.basic.klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (obj->as.basic.flags & T_MASK) {
|
|
|
|
case T_ICLASS:
|
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_mark_tbl(obj->as.klass.m_tbl);
|
|
|
|
rb_mark_tbl(obj->as.klass.iv_tbl);
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = obj->as.klass.super;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_ARRAY:
|
2002-09-04 05:23:51 -04:00
|
|
|
if (FL_TEST(obj, ELTS_SHARED)) {
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = obj->as.array.aux.shared;
|
|
|
|
goto again;
|
2002-09-04 05:23:51 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-08-28 04:05:23 -04:00
|
|
|
long i, len = obj->as.array.len;
|
2001-11-13 03:19:52 -05:00
|
|
|
VALUE *ptr = obj->as.array.ptr;
|
2001-10-31 01:53:22 -05:00
|
|
|
|
2002-09-04 05:23:51 -04:00
|
|
|
for (i=0; i < len; i++) {
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark(*ptr++);
|
2002-09-04 05:23:51 -04:00
|
|
|
}
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_HASH:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_mark_hash(obj->as.hash.tbl);
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = obj->as.hash.ifnone;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_STRING:
|
2001-12-10 22:48:08 -05:00
|
|
|
#define STR_ASSOC FL_USER3 /* copied from string.c */
|
2001-12-10 02:18:16 -05:00
|
|
|
if (FL_TEST(obj, ELTS_SHARED|STR_ASSOC)) {
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = obj->as.string.aux.shared;
|
|
|
|
goto again;
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DATA:
|
|
|
|
if (obj->as.data.dmark) (*obj->as.data.dmark)(DATA_PTR(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OBJECT:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_mark_tbl(obj->as.object.iv_tbl);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FILE:
|
|
|
|
case T_REGEXP:
|
|
|
|
case T_FLOAT:
|
|
|
|
case T_BIGNUM:
|
2001-01-23 03:08:59 -05:00
|
|
|
case T_BLKTAG:
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MATCH:
|
|
|
|
if (obj->as.match.str) {
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = obj->as.match.str;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_VARMAP:
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark(obj->as.varmap.val);
|
2003-02-20 19:28:04 -05:00
|
|
|
ptr = (VALUE)obj->as.varmap.next;
|
|
|
|
goto again;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_SCOPE:
|
2001-02-02 06:38:20 -05:00
|
|
|
if (obj->as.scope.local_vars && (obj->as.scope.flags & SCOPE_MALLOC)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
int n = obj->as.scope.local_tbl[0]+1;
|
|
|
|
VALUE *vars = &obj->as.scope.local_vars[-1];
|
|
|
|
|
2001-11-13 03:19:52 -05:00
|
|
|
while (n--) {
|
2003-02-20 19:28:04 -05:00
|
|
|
rb_gc_mark(*vars++);
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRUCT:
|
2001-11-13 03:19:52 -05:00
|
|
|
{
|
2003-02-20 19:28:04 -05:00
|
|
|
long len = obj->as.rstruct.len;
|
2001-11-13 03:19:52 -05:00
|
|
|
VALUE *ptr = obj->as.rstruct.ptr;
|
2001-10-31 01:53:22 -05:00
|
|
|
|
2003-02-20 19:28:04 -05:00
|
|
|
while (len--) {
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_gc_mark(*ptr++);
|
2003-02-20 19:28:04 -05:00
|
|
|
}
|
2001-11-13 03:19:52 -05:00
|
|
|
}
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
default:
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_bug("rb_gc_mark(): unknown data type 0x%lx(0x%lx) %s",
|
1999-01-19 23:59:39 -05:00
|
|
|
obj->as.basic.flags & T_MASK, obj,
|
2002-01-23 02:30:43 -05:00
|
|
|
is_pointer_to_heap(obj) ? "corrupted object" : "non object");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static void obj_free _((VALUE));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
gc_sweep()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
RVALUE *p, *pend, *final_list;
|
1998-01-16 07:13:05 -05:00
|
|
|
int freed = 0;
|
2002-10-02 02:02:17 -04:00
|
|
|
int i, j;
|
2003-01-16 02:38:40 -05:00
|
|
|
unsigned long live = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-09-02 08:19:30 -04:00
|
|
|
if (ruby_in_compile && ruby_parser_stack_on_heap()) {
|
|
|
|
/* should not reclaim nodes during compilation
|
|
|
|
if yacc's semantic stack is not allocated on machine stack */
|
2002-10-02 02:02:17 -04:00
|
|
|
for (i = 0; i < heaps_used; i++) {
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
2000-11-20 03:26:48 -05:00
|
|
|
while (p < pend) {
|
2001-11-13 03:19:52 -05:00
|
|
|
if (!(p->as.basic.flags&FL_MARK) && BUILTIN_TYPE(p) == T_NODE)
|
2001-03-18 22:20:24 -05:00
|
|
|
rb_gc_mark((VALUE)p);
|
2000-11-20 03:26:48 -05:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-07 06:19:37 -05:00
|
|
|
mark_source_filename(ruby_sourcefile);
|
|
|
|
st_foreach(source_filenames, sweep_source_filename, 0);
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
freelist = 0;
|
2000-11-20 03:26:48 -05:00
|
|
|
final_list = deferred_final_list;
|
|
|
|
deferred_final_list = 0;
|
2002-10-02 02:02:17 -04:00
|
|
|
for (i = 0; i < heaps_used; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
int n = 0;
|
2002-10-02 02:02:17 -04:00
|
|
|
RVALUE *free = freelist;
|
|
|
|
RVALUE *final = final_list;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (!(p->as.basic.flags & FL_MARK)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (p->as.basic.flags) {
|
|
|
|
obj_free((VALUE)p);
|
|
|
|
}
|
2000-08-07 02:11:34 -04:00
|
|
|
if (need_call_final && FL_TEST(p, FL_FINALIZE)) {
|
2001-02-02 06:38:20 -05:00
|
|
|
p->as.free.flags = FL_MARK; /* remain marked */
|
1999-01-19 23:59:39 -05:00
|
|
|
p->as.free.next = final_list;
|
|
|
|
final_list = p;
|
|
|
|
}
|
|
|
|
else {
|
2001-02-02 06:38:20 -05:00
|
|
|
p->as.free.flags = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
p->as.free.next = freelist;
|
|
|
|
freelist = p;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
n++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (RBASIC(p)->flags == FL_MARK) {
|
|
|
|
/* objects to be finalized */
|
|
|
|
/* do notning remain marked */
|
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:13:05 -05:00
|
|
|
RBASIC(p)->flags &= ~FL_MARK;
|
2002-12-29 09:51:22 -05:00
|
|
|
live++;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
2002-10-02 02:02:17 -04:00
|
|
|
if (n == heaps_limits[i] && freed + n > FREE_MIN) {
|
|
|
|
RVALUE *pp;
|
|
|
|
|
|
|
|
heaps_limits[i] = 0;
|
|
|
|
for (pp = final_list; pp != final; pp = pp->as.free.next) {
|
|
|
|
p->as.free.flags |= FL_SINGLETON; /* freeing page mark */
|
|
|
|
}
|
|
|
|
freelist = free; /* cancel this page from freelist */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
freed += n;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2003-02-20 19:28:04 -05:00
|
|
|
malloc_limit += (malloc_increase - malloc_limit) * (double)live / (live + freed);
|
2002-12-29 09:51:22 -05:00
|
|
|
if (malloc_limit < GC_MALLOC_LIMIT) malloc_limit = GC_MALLOC_LIMIT;
|
2002-10-10 13:11:18 -04:00
|
|
|
malloc_increase = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (freed < FREE_MIN) {
|
|
|
|
add_heap();
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
during_gc = 0;
|
|
|
|
|
|
|
|
/* clear finalization list */
|
2001-01-23 04:55:10 -05:00
|
|
|
if (final_list) {
|
1999-01-19 23:59:39 -05:00
|
|
|
RVALUE *tmp;
|
|
|
|
|
2000-11-20 03:26:48 -05:00
|
|
|
if (rb_prohibit_interrupt || ruby_in_compile) {
|
|
|
|
deferred_final_list = final_list;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for (p = final_list; p; p = tmp) {
|
|
|
|
tmp = p->as.free.next;
|
|
|
|
run_final((VALUE)p);
|
2002-10-02 02:02:17 -04:00
|
|
|
if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */
|
|
|
|
p->as.free.flags = 0;
|
|
|
|
p->as.free.next = freelist;
|
|
|
|
freelist = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-17 03:27:00 -04:00
|
|
|
for (i = j = 1; j < heaps_used; i++) {
|
2002-10-02 02:02:17 -04:00
|
|
|
if (heaps_limits[i] == 0) {
|
|
|
|
free(heaps[i]);
|
|
|
|
heaps_used--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (i != j) {
|
|
|
|
heaps[j] = heaps[i];
|
2002-10-09 02:12:54 -04:00
|
|
|
heaps_limits[j] = heaps_limits[i];
|
2002-10-02 02:02:17 -04:00
|
|
|
}
|
|
|
|
j++;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_force_recycle(p)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE p;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-02-02 06:38:20 -05:00
|
|
|
RANY(p)->as.free.flags = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
RANY(p)->as.free.next = freelist;
|
|
|
|
freelist = RANY(p);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
obj_free(obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
switch (RANY(obj)->as.basic.flags & T_MASK) {
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_NIL:
|
|
|
|
case T_FIXNUM:
|
|
|
|
case T_TRUE:
|
|
|
|
case T_FALSE:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("obj_free() called for broken object");
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FL_TEST(obj, FL_EXIVAR)) {
|
|
|
|
rb_free_generic_ivar((VALUE)obj);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
switch (RANY(obj)->as.basic.flags & T_MASK) {
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_OBJECT:
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RANY(obj)->as.object.iv_tbl) {
|
|
|
|
st_free_table(RANY(obj)->as.object.iv_tbl);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_MODULE:
|
|
|
|
case T_CLASS:
|
2003-07-07 04:28:19 -04:00
|
|
|
rb_clear_cache_by_class((VALUE)obj);
|
1999-01-19 23:59:39 -05:00
|
|
|
st_free_table(RANY(obj)->as.klass.m_tbl);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RANY(obj)->as.object.iv_tbl) {
|
|
|
|
st_free_table(RANY(obj)->as.object.iv_tbl);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_STRING:
|
2001-12-10 02:18:16 -05:00
|
|
|
if (RANY(obj)->as.string.ptr && !FL_TEST(obj, ELTS_SHARED)) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.string.ptr));
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
2001-12-10 02:18:16 -05:00
|
|
|
if (RANY(obj)->as.array.ptr && !FL_TEST(obj, ELTS_SHARED)) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.array.ptr));
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_HASH:
|
2000-09-26 23:43:15 -04:00
|
|
|
if (RANY(obj)->as.hash.tbl) {
|
1999-01-19 23:59:39 -05:00
|
|
|
st_free_table(RANY(obj)->as.hash.tbl);
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_REGEXP:
|
2000-09-26 23:43:15 -04:00
|
|
|
if (RANY(obj)->as.regexp.ptr) {
|
|
|
|
re_free_pattern(RANY(obj)->as.regexp.ptr);
|
|
|
|
}
|
|
|
|
if (RANY(obj)->as.regexp.str) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.regexp.str));
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_DATA:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (DATA_PTR(obj)) {
|
|
|
|
if ((long)RANY(obj)->as.data.dfree == -1) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(DATA_PTR(obj)));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else if (RANY(obj)->as.data.dfree) {
|
|
|
|
(*RANY(obj)->as.data.dfree)(DATA_PTR(obj));
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_MATCH:
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RANY(obj)->as.match.regs) {
|
1999-01-19 23:59:39 -05:00
|
|
|
re_free_registers(RANY(obj)->as.match.regs);
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.match.regs));
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_FILE:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RANY(obj)->as.file.fptr) {
|
|
|
|
rb_io_fptr_finalize(RANY(obj)->as.file.fptr);
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.file.fptr));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_ICLASS:
|
|
|
|
/* iClass shares table with the module */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
case T_VARMAP:
|
2001-01-23 03:08:59 -05:00
|
|
|
case T_BLKTAG:
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
2000-09-26 23:43:15 -04:00
|
|
|
if (RANY(obj)->as.bignum.digits) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.bignum.digits));
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_NODE:
|
1999-01-19 23:59:39 -05:00
|
|
|
switch (nd_type(obj)) {
|
|
|
|
case NODE_SCOPE:
|
|
|
|
if (RANY(obj)->as.node.u1.tbl) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.node.u1.tbl));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef C_ALLOCA
|
|
|
|
case NODE_ALLOCA:
|
2001-03-26 03:57:16 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.node.u1.node));
|
1999-01-19 23:59:39 -05:00
|
|
|
break;
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return; /* no need to free iv_tbl */
|
|
|
|
|
|
|
|
case T_SCOPE:
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RANY(obj)->as.scope.local_vars &&
|
2001-02-02 06:38:20 -05:00
|
|
|
RANY(obj)->as.scope.flags != SCOPE_ALLOCA) {
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE *vars = RANY(obj)->as.scope.local_vars-1;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (vars[0] == 0)
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.scope.local_tbl));
|
2001-02-02 06:38:20 -05:00
|
|
|
if (RANY(obj)->as.scope.flags & SCOPE_MALLOC)
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(vars));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRUCT:
|
2000-09-26 23:43:15 -04:00
|
|
|
if (RANY(obj)->as.rstruct.ptr) {
|
2000-11-14 02:10:31 -05:00
|
|
|
RUBY_CRITICAL(free(RANY(obj)->as.rstruct.ptr));
|
2000-09-26 23:43:15 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_bug("gc_sweep(): unknown data type 0x%lx(%ld)", obj,
|
1999-01-19 23:59:39 -05:00
|
|
|
RANY(obj)->as.basic.flags & T_MASK);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_frame(frame)
|
1998-01-16 07:13:05 -05:00
|
|
|
struct FRAME *frame;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
mark_locations_array(frame->argv, frame->argc);
|
2002-12-31 06:44:29 -05:00
|
|
|
rb_gc_mark((VALUE)frame->node);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#if defined(__human68k__) || defined(DJGPP)
|
|
|
|
#if defined(__human68k__)
|
|
|
|
typedef unsigned long rb_jmp_buf[8];
|
2001-06-19 11:42:00 -04:00
|
|
|
__asm__ (".even\n\
|
|
|
|
_rb_setjmp:\n\
|
|
|
|
move.l 4(sp),a0\n\
|
|
|
|
movem.l d3-d7/a3-a5,(a0)\n\
|
|
|
|
moveq.l #0,d0\n\
|
1998-01-16 07:13:05 -05:00
|
|
|
rts");
|
2000-01-04 23:41:21 -05:00
|
|
|
#ifdef setjmp
|
|
|
|
#undef setjmp
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
#if defined(DJGPP)
|
|
|
|
typedef unsigned long rb_jmp_buf[6];
|
2001-06-19 11:42:00 -04:00
|
|
|
__asm__ (".align 4\n\
|
|
|
|
_rb_setjmp:\n\
|
|
|
|
pushl %ebp\n\
|
|
|
|
movl %esp,%ebp\n\
|
|
|
|
movl 8(%ebp),%ebp\n\
|
|
|
|
movl %eax,(%ebp)\n\
|
|
|
|
movl %ebx,4(%ebp)\n\
|
|
|
|
movl %ecx,8(%ebp)\n\
|
|
|
|
movl %edx,12(%ebp)\n\
|
|
|
|
movl %esi,16(%ebp)\n\
|
|
|
|
movl %edi,20(%ebp)\n\
|
|
|
|
popl %ebp\n\
|
|
|
|
xorl %eax,%eax\n\
|
1998-01-16 07:13:05 -05:00
|
|
|
ret");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
int rb_setjmp (rb_jmp_buf);
|
|
|
|
#define jmp_buf rb_jmp_buf
|
|
|
|
#define setjmp rb_setjmp
|
|
|
|
#endif /* __human68k__ or DJGPP */
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
struct gc_list *list;
|
1999-08-24 04:21:56 -04:00
|
|
|
struct FRAME * volatile frame; /* gcc 2.7.2.3 -O2 bug?? */
|
1998-01-16 07:13:05 -05:00
|
|
|
jmp_buf save_regs_gc_mark;
|
2002-05-14 02:22:31 -04:00
|
|
|
SET_STACK_END;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-11-20 03:26:48 -05:00
|
|
|
if (dont_gc || during_gc) {
|
2002-09-06 04:59:41 -04:00
|
|
|
if (!freelist) {
|
2000-08-02 00:54:21 -04:00
|
|
|
add_heap();
|
|
|
|
}
|
2000-07-27 05:49:34 -04:00
|
|
|
return;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (during_gc) return;
|
|
|
|
during_gc++;
|
|
|
|
|
2001-11-13 03:19:52 -05:00
|
|
|
init_mark_stack();
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
/* mark frame stack */
|
1999-01-19 23:59:39 -05:00
|
|
|
for (frame = ruby_frame; frame; frame = frame->prev) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_gc_mark_frame(frame);
|
|
|
|
if (frame->tmp) {
|
|
|
|
struct FRAME *tmp = frame->tmp;
|
|
|
|
while (tmp) {
|
|
|
|
rb_gc_mark_frame(tmp);
|
|
|
|
tmp = tmp->prev;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-03-18 22:20:24 -05:00
|
|
|
rb_gc_mark((VALUE)ruby_scope);
|
|
|
|
rb_gc_mark((VALUE)ruby_dyna_vars);
|
2000-07-15 09:37:03 -04:00
|
|
|
if (finalizer_table) {
|
|
|
|
rb_mark_tbl(finalizer_table);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2003-08-01 06:57:32 -04:00
|
|
|
/* This assumes that all registers are saved into the jmp_buf (and stack) */
|
1998-01-16 07:13:05 -05:00
|
|
|
setjmp(save_regs_gc_mark);
|
1999-08-24 04:21:56 -04:00
|
|
|
mark_locations_array((VALUE*)save_regs_gc_mark, sizeof(save_regs_gc_mark) / sizeof(VALUE *));
|
2003-06-27 23:29:00 -04:00
|
|
|
#if STACK_GROW_DIRECTION < 0
|
|
|
|
rb_gc_mark_locations((VALUE*)STACK_END, rb_gc_stack_start);
|
|
|
|
#elif STACK_GROW_DIRECTION > 0
|
2003-08-01 10:57:48 -04:00
|
|
|
rb_gc_mark_locations(rb_gc_stack_start, (VALUE*)STACK_END + 1);
|
2003-06-27 23:29:00 -04:00
|
|
|
#else
|
|
|
|
if ((VALUE*)STACK_END < rb_gc_stack_start)
|
|
|
|
rb_gc_mark_locations((VALUE*)STACK_END, rb_gc_stack_start);
|
|
|
|
else
|
2003-08-01 10:57:48 -04:00
|
|
|
rb_gc_mark_locations(rb_gc_stack_start, (VALUE*)STACK_END + 1);
|
2003-06-27 23:29:00 -04:00
|
|
|
#endif
|
2003-08-01 06:57:32 -04:00
|
|
|
#ifdef __ia64__
|
|
|
|
/* mark backing store (flushed register window on the stack) */
|
|
|
|
/* the basic idea from guile GC code */
|
|
|
|
{
|
|
|
|
ucontext_t ctx;
|
|
|
|
VALUE *top, *bot;
|
|
|
|
getcontext(&ctx);
|
2003-08-01 07:01:04 -04:00
|
|
|
mark_locations_array((VALUE*)&ctx.uc_mcontext,
|
2003-08-01 06:57:32 -04:00
|
|
|
((size_t)(sizeof(VALUE)-1 + sizeof ctx.uc_mcontext)/sizeof(VALUE)));
|
|
|
|
bot = (VALUE*)__libc_ia64_register_backing_store_base;
|
2003-08-06 15:21:24 -04:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
top = (VALUE*)ctx.uc_mcontext.mc_special.bspstore;
|
|
|
|
#else
|
2003-08-01 06:57:32 -04:00
|
|
|
top = (VALUE*)ctx.uc_mcontext.sc_ar_bsp;
|
2003-08-06 15:21:24 -04:00
|
|
|
#endif
|
2003-08-01 06:57:32 -04:00
|
|
|
rb_gc_mark_locations(bot, top);
|
|
|
|
}
|
|
|
|
#endif
|
2003-05-08 05:45:52 -04:00
|
|
|
#if defined(__human68k__) || defined(__mc68000__)
|
2003-06-27 23:29:00 -04:00
|
|
|
rb_gc_mark_locations((VALUE*)((char*)STACK_END + 2),
|
|
|
|
(VALUE*)((char*)rb_gc_stack_start + 2));
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_threads();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* mark protected global variables */
|
2001-10-31 01:53:22 -05:00
|
|
|
for (list = global_List; list; list = list->next) {
|
2003-04-21 04:17:18 -04:00
|
|
|
rb_gc_mark_maybe(*list->varptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-11-26 04:07:26 -05:00
|
|
|
rb_mark_end_proc();
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_global_tbl();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mark_tbl(rb_class_tbl);
|
|
|
|
rb_gc_mark_trap_list();
|
|
|
|
|
|
|
|
/* mark generic instance variables for special constants */
|
|
|
|
rb_mark_generic_ivar_tbl();
|
2002-09-05 16:00:52 -04:00
|
|
|
|
|
|
|
rb_gc_mark_parser();
|
2001-11-13 03:19:52 -05:00
|
|
|
|
|
|
|
/* gc_mark objects whose marking are not completed*/
|
|
|
|
while (!MARK_STACK_EMPTY){
|
|
|
|
if (mark_stack_overflow){
|
|
|
|
gc_mark_all();
|
2001-11-27 05:00:35 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-11-13 03:19:52 -05:00
|
|
|
gc_mark_rest();
|
2001-10-31 01:53:22 -05:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
gc_sweep();
|
|
|
|
}
|
|
|
|
|
2001-01-29 00:10:42 -05:00
|
|
|
VALUE
|
|
|
|
rb_gc_start()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc();
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-13 01:45:20 -04:00
|
|
|
Init_stack(addr)
|
|
|
|
VALUE *addr;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-10-10 03:03:36 -04:00
|
|
|
#if defined(__human68k__)
|
1998-01-16 07:13:05 -05:00
|
|
|
extern void *_SEND;
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_gc_stack_start = _SEND;
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
2002-10-03 07:24:24 -04:00
|
|
|
if (!addr) addr = (VALUE *)&addr;
|
2003-02-27 03:04:32 -05:00
|
|
|
if (rb_gc_stack_start) {
|
2003-06-27 23:29:00 -04:00
|
|
|
if (STACK_UPPER(&addr,
|
2003-08-01 10:57:48 -04:00
|
|
|
rb_gc_stack_start > addr,
|
2003-06-27 23:29:00 -04:00
|
|
|
rb_gc_stack_start < ++addr))
|
|
|
|
rb_gc_stack_start = addr;
|
2003-02-27 03:04:32 -05:00
|
|
|
return;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_gc_stack_start = addr;
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
2001-11-19 00:03:03 -05:00
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
|
|
|
|
double space = (double)rlim.rlim_cur*0.2;
|
|
|
|
|
|
|
|
if (space > 1024*1024) space = 1024*1024;
|
|
|
|
STACK_LEVEL_MAX = (rlim.rlim_cur - space) / sizeof(VALUE);
|
|
|
|
}
|
|
|
|
}
|
2003-07-31 12:34:07 -04:00
|
|
|
#ifdef __ia64__
|
|
|
|
/* ruby crashes on IA64 if compiled with optimizer on */
|
|
|
|
/* when if STACK_LEVEL_MAX is greater than this magic number */
|
|
|
|
/* I know this is a kludge. I suspect optimizer bug */
|
2003-08-01 06:57:32 -04:00
|
|
|
#define IA64_MAGIC_STACK_LIMIT 49152
|
2003-07-31 12:34:07 -04:00
|
|
|
if (STACK_LEVEL_MAX > IA64_MAGIC_STACK_LIMIT)
|
|
|
|
STACK_LEVEL_MAX = IA64_MAGIC_STACK_LIMIT;
|
|
|
|
#endif
|
2001-11-19 00:03:03 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
Init_heap()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!rb_gc_stack_start) {
|
|
|
|
Init_stack(0);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
add_heap();
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
os_live_obj()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < heaps_used; i++) {
|
|
|
|
RVALUE *p, *pend;
|
|
|
|
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
for (;p < pend; p++) {
|
|
|
|
if (p->as.basic.flags) {
|
|
|
|
switch (TYPE(p)) {
|
|
|
|
case T_ICLASS:
|
|
|
|
case T_VARMAP:
|
|
|
|
case T_SCOPE:
|
|
|
|
case T_NODE:
|
|
|
|
continue;
|
|
|
|
case T_CLASS:
|
|
|
|
if (FL_TEST(p, FL_SINGLETON)) continue;
|
|
|
|
default:
|
2001-01-23 03:08:59 -05:00
|
|
|
if (!p->as.basic.klass) continue;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_yield((VALUE)p);
|
1998-01-16 07:13:05 -05:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
os_obj_of(of)
|
|
|
|
VALUE of;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < heaps_used; i++) {
|
|
|
|
RVALUE *p, *pend;
|
|
|
|
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
for (;p < pend; p++) {
|
|
|
|
if (p->as.basic.flags) {
|
|
|
|
switch (TYPE(p)) {
|
|
|
|
case T_ICLASS:
|
|
|
|
case T_VARMAP:
|
|
|
|
case T_SCOPE:
|
|
|
|
case T_NODE:
|
|
|
|
continue;
|
|
|
|
case T_CLASS:
|
|
|
|
if (FL_TEST(p, FL_SINGLETON)) continue;
|
|
|
|
default:
|
2001-01-23 03:08:59 -05:00
|
|
|
if (!p->as.basic.klass) continue;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_obj_is_kind_of((VALUE)p, of)) {
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_yield((VALUE)p);
|
1998-01-16 07:13:05 -05:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
os_each_obj(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE of;
|
|
|
|
|
2003-03-26 02:01:14 -05:00
|
|
|
rb_secure(4);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (rb_scan_args(argc, argv, "01", &of) == 0) {
|
|
|
|
return os_live_obj();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return os_obj_of(of);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE finalizers;
|
|
|
|
|
|
|
|
static VALUE
|
2003-06-06 05:44:22 -04:00
|
|
|
add_final(os, block)
|
|
|
|
VALUE os, block;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-15 09:37:03 -04:00
|
|
|
rb_warn("ObjectSpace::add_finalizer is deprecated; use define_finalizer");
|
2003-06-07 11:34:31 -04:00
|
|
|
if (!rb_respond_to(block, rb_intern("call"))) {
|
|
|
|
rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_obj_classname(block));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_ary_push(finalizers, block);
|
|
|
|
return block;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-06-06 05:44:22 -04:00
|
|
|
rm_final(os, block)
|
|
|
|
VALUE os, block;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-15 09:37:03 -04:00
|
|
|
rb_warn("ObjectSpace::remove_finalizer is deprecated; use undefine_finalizer");
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_ary_delete(finalizers, block);
|
|
|
|
return block;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
finals()
|
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
rb_warn("ObjectSpace::finalizers is deprecated");
|
1998-01-16 07:13:05 -05:00
|
|
|
return finalizers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
call_final(os, obj)
|
|
|
|
VALUE os, obj;
|
|
|
|
{
|
2002-08-29 05:08:18 -04:00
|
|
|
rb_warn("ObjectSpace::call_finalizer is deprecated; use define_finalizer");
|
1998-01-16 07:13:05 -05:00
|
|
|
need_call_final = 1;
|
|
|
|
FL_SET(obj, FL_FINALIZE);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2000-07-15 09:37:03 -04:00
|
|
|
static VALUE
|
|
|
|
undefine_final(os, obj)
|
|
|
|
VALUE os, obj;
|
|
|
|
{
|
|
|
|
if (finalizer_table) {
|
2003-07-25 01:36:55 -04:00
|
|
|
st_delete(finalizer_table, (st_data_t*)&obj, 0);
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
define_final(argc, argv, os)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE os;
|
|
|
|
{
|
2003-06-06 05:44:22 -04:00
|
|
|
VALUE obj, block, table;
|
2000-07-15 09:37:03 -04:00
|
|
|
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_scan_args(argc, argv, "11", &obj, &block);
|
2000-07-15 09:37:03 -04:00
|
|
|
if (argc == 1) {
|
2003-06-16 03:14:50 -04:00
|
|
|
block = rb_block_proc();
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
2003-06-07 11:34:31 -04:00
|
|
|
else if (!rb_respond_to(block, rb_intern("call"))) {
|
|
|
|
rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_obj_classname(block));
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
|
|
|
need_call_final = 1;
|
|
|
|
FL_SET(obj, FL_FINALIZE);
|
|
|
|
|
|
|
|
if (!finalizer_table) {
|
|
|
|
finalizer_table = st_init_numtable();
|
|
|
|
}
|
|
|
|
if (st_lookup(finalizer_table, obj, &table)) {
|
2003-06-06 05:44:22 -04:00
|
|
|
rb_ary_push(table, block);
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
|
|
|
else {
|
2003-06-06 05:44:22 -04:00
|
|
|
st_add_direct(finalizer_table, obj, rb_ary_new3(1, block));
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
2003-06-06 05:44:22 -04:00
|
|
|
return block;
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
|
|
|
|
2002-12-04 02:39:32 -05:00
|
|
|
void
|
|
|
|
rb_gc_copy_finalizer(dest, obj)
|
|
|
|
VALUE dest, obj;
|
|
|
|
{
|
|
|
|
VALUE table;
|
|
|
|
|
|
|
|
if (!finalizer_table) return;
|
|
|
|
if (!FL_TEST(obj, FL_FINALIZE)) return;
|
|
|
|
if (FL_TEST(dest, FL_FINALIZE)) {
|
|
|
|
rb_warn("copy_finalizer: descarding old finalizers");
|
|
|
|
}
|
|
|
|
if (st_lookup(finalizer_table, obj, &table)) {
|
|
|
|
st_insert(finalizer_table, dest, table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
run_single_final(args)
|
|
|
|
VALUE *args;
|
|
|
|
{
|
2001-11-13 03:19:52 -05:00
|
|
|
rb_eval_cmd(args[0], args[1], 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
|
|
|
run_final(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-08-21 11:47:54 -04:00
|
|
|
long i;
|
2003-07-24 03:50:36 -04:00
|
|
|
int status, critical_save = rb_thread_critical;
|
2000-07-17 05:38:10 -04:00
|
|
|
VALUE args[2], table;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-07-24 03:50:36 -04:00
|
|
|
rb_thread_critical = Qtrue;
|
2000-07-17 05:38:10 -04:00
|
|
|
args[1] = rb_ary_new3(1, rb_obj_id(obj)); /* make obj into id */
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<RARRAY(finalizers)->len; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
args[0] = RARRAY(finalizers)->ptr[i];
|
2001-05-07 05:26:29 -04:00
|
|
|
rb_protect((VALUE(*)_((VALUE)))run_single_final, (VALUE)args, &status);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2003-07-25 01:36:55 -04:00
|
|
|
if (finalizer_table && st_delete(finalizer_table, (st_data_t*)&obj, &table)) {
|
2000-07-15 09:37:03 -04:00
|
|
|
for (i=0; i<RARRAY(table)->len; i++) {
|
|
|
|
args[0] = RARRAY(table)->ptr[i];
|
2001-05-07 05:26:29 -04:00
|
|
|
rb_protect((VALUE(*)_((VALUE)))run_single_final, (VALUE)args, &status);
|
2000-07-15 09:37:03 -04:00
|
|
|
}
|
|
|
|
}
|
2003-07-24 03:50:36 -04:00
|
|
|
rb_thread_critical = critical_save;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_call_finalizer_at_exit()
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
|
|
|
RVALUE *p, *pend;
|
|
|
|
int i;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
/* run finalizers */
|
2001-01-23 04:55:10 -05:00
|
|
|
if (need_call_final) {
|
|
|
|
if (deferred_final_list) {
|
|
|
|
p = deferred_final_list;
|
|
|
|
while (p) {
|
|
|
|
RVALUE *tmp = p;
|
|
|
|
p = p->as.free.next;
|
|
|
|
run_final((VALUE)tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < heaps_used; i++) {
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
2001-01-23 04:55:10 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (FL_TEST(p, FL_FINALIZE)) {
|
|
|
|
FL_UNSET(p, FL_FINALIZE);
|
|
|
|
p->as.basic.klass = 0;
|
|
|
|
run_final((VALUE)p);
|
|
|
|
}
|
|
|
|
p++;
|
2000-04-10 01:48:43 -04:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
}
|
2003-08-01 10:57:48 -04:00
|
|
|
/* run data object's finalizers */
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i = 0; i < heaps_used; i++) {
|
2001-07-02 04:46:28 -04:00
|
|
|
p = heaps[i]; pend = p + heaps_limits[i];
|
1998-01-16 07:19:22 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (BUILTIN_TYPE(p) == T_DATA &&
|
1999-08-13 01:45:20 -04:00
|
|
|
DATA_PTR(p) && RANY(p)->as.data.dfree) {
|
2001-02-02 06:38:20 -05:00
|
|
|
p->as.free.flags = 0;
|
2003-03-23 05:55:39 -05:00
|
|
|
if ((long)RANY(p)->as.data.dfree == -1) {
|
|
|
|
RUBY_CRITICAL(free(DATA_PTR(p)));
|
|
|
|
}
|
|
|
|
else if (RANY(p)->as.data.dfree) {
|
|
|
|
(*RANY(p)->as.data.dfree)(DATA_PTR(p));
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
else if (BUILTIN_TYPE(p) == T_FILE) {
|
2001-02-02 06:38:20 -05:00
|
|
|
p->as.free.flags = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_io_fptr_finalize(RANY(p)->as.file.fptr);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
id2ref(obj, id)
|
|
|
|
VALUE obj, id;
|
|
|
|
{
|
2001-01-09 02:26:21 -05:00
|
|
|
unsigned long ptr, p0;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_secure(4);
|
2001-05-02 00:22:21 -04:00
|
|
|
p0 = ptr = NUM2ULONG(id);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ptr == Qtrue) return Qtrue;
|
|
|
|
if (ptr == Qfalse) return Qfalse;
|
|
|
|
if (ptr == Qnil) return Qnil;
|
2001-03-20 22:41:45 -05:00
|
|
|
if (FIXNUM_P(ptr)) return (VALUE)ptr;
|
|
|
|
if (SYMBOL_P(ptr) && rb_id2name(SYM2ID((VALUE)ptr)) != 0) {
|
|
|
|
return (VALUE)ptr;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
ptr = id ^ FIXNUM_FLAG; /* unset FIXNUM_FLAG */
|
2002-05-14 01:59:35 -04:00
|
|
|
if (!is_pointer_to_heap((void *)ptr)) {
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eRangeError, "0x%lx is not id value", p0);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (BUILTIN_TYPE(ptr) == 0) {
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eRangeError, "0x%lx is recycled object", p0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return (VALUE)ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_GC()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_mObSpace;
|
|
|
|
|
|
|
|
rb_mGC = rb_define_module("GC");
|
2001-01-29 00:10:42 -05:00
|
|
|
rb_define_singleton_method(rb_mGC, "start", rb_gc_start, 0);
|
|
|
|
rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
|
|
|
|
rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
|
|
|
|
rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_mObSpace = rb_define_module("ObjectSpace");
|
|
|
|
rb_define_module_function(rb_mObSpace, "each_object", os_each_obj, -1);
|
2001-01-29 00:10:42 -05:00
|
|
|
rb_define_module_function(rb_mObSpace, "garbage_collect", rb_gc_start, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_module_function(rb_mObSpace, "add_finalizer", add_final, 1);
|
|
|
|
rb_define_module_function(rb_mObSpace, "remove_finalizer", rm_final, 1);
|
|
|
|
rb_define_module_function(rb_mObSpace, "finalizers", finals, 0);
|
|
|
|
rb_define_module_function(rb_mObSpace, "call_finalizer", call_final, 1);
|
2000-07-15 09:37:03 -04:00
|
|
|
|
|
|
|
rb_define_module_function(rb_mObSpace, "define_finalizer", define_final, -1);
|
|
|
|
rb_define_module_function(rb_mObSpace, "undefine_finalizer", undefine_final, 1);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_module_function(rb_mObSpace, "_id2ref", id2ref, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_gc_register_address(&rb_mObSpace);
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_global_variable(&finalizers);
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_gc_unregister_address(&rb_mObSpace);
|
1999-01-19 23:59:39 -05:00
|
|
|
finalizers = rb_ary_new();
|
2002-03-07 06:19:37 -05:00
|
|
|
|
|
|
|
source_filenames = st_init_strtable();
|
2002-04-24 00:54:16 -04:00
|
|
|
|
2002-04-24 01:07:07 -04:00
|
|
|
nomem_error = rb_exc_new2(rb_eNoMemError, "failed to allocate memory");
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_global_variable(&nomem_error);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|