6930181996-02-25Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
6930181996-02-25Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
3741091999-09-25Henrik Grubbström (Grubba) /**/
6930181996-02-25Fredrik Hübinette (Hubbe) #include "global.h"
a29e021996-10-15Fredrik Hübinette (Hubbe) struct callback *gc_evaluator_callback=0;
c94c371996-03-28Fredrik Hübinette (Hubbe) #include "array.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "multiset.h"
c94c371996-03-28Fredrik Hübinette (Hubbe) #include "mapping.h" #include "object.h" #include "program.h"
4a578f1997-01-27Fredrik Hübinette (Hubbe) #include "stralloc.h"
9c6f7d1997-04-15Fredrik Hübinette (Hubbe) #include "stuff.h" #include "error.h"
9aa6fa1997-05-19Fredrik Hübinette (Hubbe) #include "pike_memory.h"
1a11681997-10-06Fredrik Hübinette (Hubbe) #include "pike_macros.h"
1ca3ba1997-10-13Fredrik Hübinette (Hubbe) #include "pike_types.h"
dc296b1997-10-21Fredrik Hübinette (Hubbe) #include "time_stuff.h"
2eeba91999-03-17Fredrik Hübinette (Hubbe) #include "constants.h"
1637c42000-02-01Fredrik Hübinette (Hubbe) #include "interpret.h"
c94c371996-03-28Fredrik Hübinette (Hubbe) 
6930181996-02-25Fredrik Hübinette (Hubbe) #include "gc.h" #include "main.h"
dc296b1997-10-21Fredrik Hübinette (Hubbe) #include <math.h>
6930181996-02-25Fredrik Hübinette (Hubbe) 
3741091999-09-25Henrik Grubbström (Grubba) #include "block_alloc.h"
699bb02000-04-22Martin Stjernholm RCSID("$Id: gc.c,v 1.56 2000/04/22 13:20:39 mast Exp $");
24ddc71998-03-28Henrik Grubbström (Grubba) 
6930181996-02-25Fredrik Hübinette (Hubbe) /* Run garbage collect approximate every time we have * 20 percent of all arrays, objects and programs is * garbage. */ #define GC_CONST 20 #define MIN_ALLOC_THRESHOLD 1000
b95bef1996-03-29Fredrik Hübinette (Hubbe) #define MAX_ALLOC_THRESHOLD 10000000
6930181996-02-25Fredrik Hübinette (Hubbe) #define MULTIPLIER 0.9
c94c371996-03-28Fredrik Hübinette (Hubbe) #define MARKER_CHUNK_SIZE 1023
6930181996-02-25Fredrik Hübinette (Hubbe) 
dc296b1997-10-21Fredrik Hübinette (Hubbe) INT32 num_objects =0; INT32 num_allocs =0;
6930181996-02-25Fredrik Hübinette (Hubbe) INT32 alloc_threshold = MIN_ALLOC_THRESHOLD;
4fa1832000-04-19Martin Stjernholm int Pike_in_gc = 0;
8ea2061998-04-29Fredrik Noring struct pike_queue gc_mark_queue;
4452c12000-02-02Fredrik Hübinette (Hubbe) time_t last_gc;
6930181996-02-25Fredrik Hübinette (Hubbe) 
dc296b1997-10-21Fredrik Hübinette (Hubbe) static double objects_alloced = 0.0; static double objects_freed = 0.0;
6930181996-02-25Fredrik Hübinette (Hubbe) 
4a578f1997-01-27Fredrik Hübinette (Hubbe) struct callback_list gc_callbacks;
424d9c1999-05-02Fredrik Hübinette (Hubbe) struct callback *debug_add_gc_callback(callback_func call,
4a578f1997-01-27Fredrik Hübinette (Hubbe)  void *arg, callback_func free_func) { return add_to_callback(&gc_callbacks, call, arg, free_func); }
c94c371996-03-28Fredrik Hübinette (Hubbe) 
424d9c1999-05-02Fredrik Hübinette (Hubbe) #undef INIT_BLOCK
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
424d9c1999-05-02Fredrik Hübinette (Hubbe) #define INIT_BLOCK(X) (X)->flags=(X)->refs=(X)->xrefs=0 #else #define INIT_BLOCK(X) (X)->flags=(X)->refs=0
05c7cd1997-07-19Fredrik Hübinette (Hubbe) #endif
c94c371996-03-28Fredrik Hübinette (Hubbe) 
424d9c1999-05-02Fredrik Hübinette (Hubbe) PTR_HASH_ALLOC(marker,MARKER_CHUNK_SIZE)
c94c371996-03-28Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
1ca3ba1997-10-13Fredrik Hübinette (Hubbe) 
dc296b1997-10-21Fredrik Hübinette (Hubbe) void dump_gc_info(void) { fprintf(stderr,"Current number of objects: %ld\n",(long)num_objects); fprintf(stderr,"Objects allocated total : %ld\n",(long)num_allocs); fprintf(stderr," threshold for next gc() : %ld\n",(long)alloc_threshold); fprintf(stderr,"Average allocs per gc() : %f\n",objects_alloced); fprintf(stderr,"Average frees per gc() : %f\n",objects_freed); fprintf(stderr,"Second since last gc() : %ld\n", (long)TIME(0) - (long)last_gc); fprintf(stderr,"Projected garbage : %f\n", objects_freed * (double) num_allocs / (double) alloc_threshold);
4fa1832000-04-19Martin Stjernholm  fprintf(stderr,"in_gc : %d\n", Pike_in_gc);
dc296b1997-10-21Fredrik Hübinette (Hubbe) }
1ca3ba1997-10-13Fredrik Hübinette (Hubbe) TYPE_T attempt_to_identify(void *something) { struct array *a; struct object *o; struct program *p;
62971d1998-01-19Fredrik Hübinette (Hubbe)  struct mapping *m; struct multiset *mu;
1ca3ba1997-10-13Fredrik Hübinette (Hubbe)  a=&empty_array; do { if(a==(struct array *)something) return T_ARRAY; a=a->next; }while(a!=&empty_array); for(o=first_object;o;o=o->next) if(o==(struct object *)something) return T_OBJECT; for(p=first_program;p;p=p->next) if(p==(struct program *)something) return T_PROGRAM;
62971d1998-01-19Fredrik Hübinette (Hubbe)  for(m=first_mapping;m;m=m->next) if(m==(struct mapping *)something) return T_MAPPING; for(mu=first_multiset;mu;mu=mu->next) if(mu==(struct multiset *)something) return T_MULTISET; if(safe_debug_findstring((struct pike_string *)something)) return T_STRING;
1ca3ba1997-10-13Fredrik Hübinette (Hubbe)  return T_UNKNOWN; }
b8a6e71996-09-25Fredrik Hübinette (Hubbe) static void *check_for =0;
1ca3ba1997-10-13Fredrik Hübinette (Hubbe) static char *found_where="";
f6d0171997-10-15Fredrik Hübinette (Hubbe) static void *found_in=0;
4694111997-11-07Fredrik Hübinette (Hubbe) static int found_in_type=0;
f6d0171997-10-15Fredrik Hübinette (Hubbe) void *gc_svalue_location=0;
b8a6e71996-09-25Fredrik Hübinette (Hubbe) 
3568101997-10-16Fredrik Hübinette (Hubbe) void describe_location(void *memblock, TYPE_T type, void *location) {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  struct program *p;
4694111997-11-07Fredrik Hübinette (Hubbe)  if(!location) return;
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) /* fprintf(stderr,"**Location of (short) svalue: %p\n",location); */
62971d1998-01-19Fredrik Hübinette (Hubbe) 
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  if(type==T_UNKNOWN) type=attempt_to_identify(memblock); fprintf(stderr," <from %s %p offset %ld>\n", get_name_of_type(type), memblock, ((long)location - (long)memblock));
62971d1998-01-19Fredrik Hübinette (Hubbe)  switch(type)
3568101997-10-16Fredrik Hübinette (Hubbe)  {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  case T_UNKNOWN: for(p=first_program;p;p=p->next) { if(memblock == (void *)p->program) { fprintf(stderr," **In memory block for program at %p\n",p); memblock=p; type=T_PROGRAM; goto describe_program_location; } } break;
0b69441998-01-19Fredrik Hübinette (Hubbe)  case T_PROGRAM:
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  describe_program_location:
0b69441998-01-19Fredrik Hübinette (Hubbe)  {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  long e;
0b69441998-01-19Fredrik Hübinette (Hubbe)  struct program *p=(struct program *)memblock; char *ptr=(char *)location;
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  if(location == (void *)&p->prev) fprintf(stderr," **In p->prev\n"); if(location == (void *)&p->next) fprintf(stderr," **In p->next\n"); if(ptr >= (char *)p->inherits && ptr<(char*)(p->inherits+p->num_inherits)) { e=((long)ptr - (long)(p->inherits)) / sizeof(struct inherit); fprintf(stderr," **In p->inherits[%ld] (%s)\n", e, p->inherits[e].name ? p->inherits[e].name->str : "no name"); return; } if(ptr >= (char *)p->constants && ptr<(char*)(p->constants+p->num_constants)) { e=((long)ptr - (long)(p->constants)) / sizeof(struct program_constant); fprintf(stderr," **In p->constants[%ld] (%s)\n", e, p->constants[e].name ? p->constants[e].name->str : "no name"); return; }
0b69441998-01-19Fredrik Hübinette (Hubbe)  if(ptr >= (char *)p->identifiers && ptr<(char*)(p->identifiers+p->num_identifiers))
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  { e=((long)ptr - (long)(p->identifiers)) / sizeof(struct identifier); fprintf(stderr," **In p->identifiers[%ld] (%s)\n", e, p->identifiers[e].name ? p->constants[e].name->str : "no name"); return; } #define FOO(NTYP,TYP,NAME) \ if(location == (void *)&p->NAME) fprintf(stderr," **In p->" #NAME "\n"); \ if(ptr >= (char *)p->NAME && ptr<(char*)(p->NAME+p->PIKE_CONCAT(num_,NAME))) \ fprintf(stderr," **In p->" #NAME "[%ld]\n",((long)ptr - (long)(p->NAME)) / sizeof(TYP)); #include "program_areas.h"
0b69441998-01-19Fredrik Hübinette (Hubbe)  return; }
62971d1998-01-19Fredrik Hübinette (Hubbe)  case T_OBJECT:
3568101997-10-16Fredrik Hübinette (Hubbe)  {
62971d1998-01-19Fredrik Hübinette (Hubbe)  struct object *o=(struct object *)memblock;
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  struct program *p; if(location == (void *)&o->parent) fprintf(stderr," **In o->parent\n"); if(location == (void *)&o->prog) fprintf(stderr," **In o->prog\n"); if(location == (void *)&o->next) fprintf(stderr," **In o->next\n"); if(location == (void *)&o->prev) fprintf(stderr," **In o->prev\n"); p=o->prog; if(!o->prog) { p=id_to_program(o->program_id); if(p) fprintf(stderr," **(We are lucky, found program for destructed object)\n"); } if(p)
3568101997-10-16Fredrik Hübinette (Hubbe)  {
62971d1998-01-19Fredrik Hübinette (Hubbe)  INT32 e,d;
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  for(e=0;e<(INT32)p->num_inherits;e++)
3568101997-10-16Fredrik Hübinette (Hubbe)  {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  struct inherit tmp=p->inherits[e];
62971d1998-01-19Fredrik Hübinette (Hubbe)  char *base=o->storage + tmp.storage_offset; for(d=0;d<(INT32)tmp.prog->num_identifiers;d++)
3568101997-10-16Fredrik Hübinette (Hubbe)  {
62971d1998-01-19Fredrik Hübinette (Hubbe)  struct identifier *id=tmp.prog->identifiers+d; if(!IDENTIFIER_IS_VARIABLE(id->identifier_flags)) continue; if(location == (void *)(base + id->func.offset)) {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  fprintf(stderr," **In variable %s\n",id->name->str);
62971d1998-01-19Fredrik Hübinette (Hubbe)  }
3568101997-10-16Fredrik Hübinette (Hubbe)  }
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  if((char *)location >= base && (char *)location <= base + ( tmp.prog->storage_needed - tmp.prog->inherits[0].storage_offset )) { fprintf(stderr," **In storage for inherit %d",e); if(tmp.name) fprintf(stderr," (%s)",tmp.name->str); fprintf(stderr,"\n"); }
3568101997-10-16Fredrik Hübinette (Hubbe)  } }
62971d1998-01-19Fredrik Hübinette (Hubbe)  return;
3568101997-10-16Fredrik Hübinette (Hubbe)  }
62971d1998-01-19Fredrik Hübinette (Hubbe)  case T_ARRAY: { struct array *a=(struct array *)memblock; struct svalue *s=(struct svalue *)location;
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  fprintf(stderr," **In index %ld\n",(long)(s-ITEM(a)));
62971d1998-01-19Fredrik Hübinette (Hubbe)  return; }
3568101997-10-16Fredrik Hübinette (Hubbe)  } }
b8a6e71996-09-25Fredrik Hübinette (Hubbe) static void gdb_gc_stop_here(void *a) {
4694111997-11-07Fredrik Hübinette (Hubbe)  fprintf(stderr,"***One ref found%s.\n",found_where);
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  describe_something(found_in, found_in_type, 0);
3568101997-10-16Fredrik Hübinette (Hubbe)  describe_location(found_in, found_in_type, gc_svalue_location);
b8a6e71996-09-25Fredrik Hübinette (Hubbe) }
f6d0171997-10-15Fredrik Hübinette (Hubbe) 
4694111997-11-07Fredrik Hübinette (Hubbe) void debug_gc_xmark_svalues(struct svalue *s, int num, char *fromwhere) { found_in=(void *)fromwhere; found_in_type=-1; gc_xmark_svalues(s,num); found_in_type=T_UNKNOWN; found_in=0; }
f6d0171997-10-15Fredrik Hübinette (Hubbe) TYPE_FIELD debug_gc_check_svalues(struct svalue *s, int num, TYPE_T t, void *data) { TYPE_FIELD ret; found_in=data; found_in_type=t; ret=gc_check_svalues(s,num); found_in_type=T_UNKNOWN;
4694111997-11-07Fredrik Hübinette (Hubbe)  found_in=0;
f6d0171997-10-15Fredrik Hübinette (Hubbe)  return ret; } void debug_gc_check_short_svalue(union anything *u, TYPE_T type, TYPE_T t, void *data) { found_in=data; found_in_type=t; gc_check_short_svalue(u,type); found_in_type=T_UNKNOWN;
4694111997-11-07Fredrik Hübinette (Hubbe)  found_in=0;
f6d0171997-10-15Fredrik Hübinette (Hubbe) }
0b69441998-01-19Fredrik Hübinette (Hubbe)  int debug_gc_check(void *x, TYPE_T t, void *data) { int ret; found_in=data; found_in_type=t; ret=gc_check(x); found_in_type=T_UNKNOWN; found_in=0; return ret; }
699bb02000-04-22Martin Stjernholm int debug_gc_check_nongarbed(void *x, TYPE_T t, void *data) { int ret; found_in=data; found_in_type=t; ret=gc_check_nongarbed(x); found_in_type=T_UNKNOWN; found_in=0; return ret; }
25479a2000-03-07Fredrik Hübinette (Hubbe) void low_describe_something(void *a, int t, int dm)
f6d0171997-10-15Fredrik Hübinette (Hubbe) { struct program *p=(struct program *)a; switch(t) {
2eeba91999-03-17Fredrik Hübinette (Hubbe)  case T_FUNCTION: if(attempt_to_identify(a) != T_OBJECT) { fprintf(stderr,"**Builtin function!\n"); break; }
f6d0171997-10-15Fredrik Hübinette (Hubbe)  case T_OBJECT: p=((struct object *)a)->prog;
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Parent identifier: %d\n",((struct object *)a)->parent_identifier);
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Program id: %ld\n",((struct object *)a)->program_id);
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  if( ((struct object *)a)->parent) { fprintf(stderr,"**Describing object's parent:\n"); describe_something( ((struct object *)a)->parent, t, 1); }else{ fprintf(stderr,"**There is no parent (any longer?)\n"); }
f6d0171997-10-15Fredrik Hübinette (Hubbe)  if(!p) { fprintf(stderr,"**The object is destructed.\n");
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  p=id_to_program(((struct object *)a)->program_id); if(!p) break;
f6d0171997-10-15Fredrik Hübinette (Hubbe)  } fprintf(stderr,"**Attempting to describe program object was instantiated from:\n"); case T_PROGRAM:
3568101997-10-16Fredrik Hübinette (Hubbe)  { char *tmp; INT32 line,pos;
2eeba91999-03-17Fredrik Hübinette (Hubbe)  int foo=0;
3568101997-10-16Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Program id: %ld\n",(long)(p->id));
05590d1998-04-23Fredrik Hübinette (Hubbe)  if(p->flags & PROGRAM_HAS_C_METHODS)
f6d0171997-10-15Fredrik Hübinette (Hubbe)  { fprintf(stderr,"**The program was written in C.\n"); }
2eeba91999-03-17Fredrik Hübinette (Hubbe)  for(pos=0;pos<100;pos++)
f6d0171997-10-15Fredrik Hübinette (Hubbe)  {
3568101997-10-16Fredrik Hübinette (Hubbe)  tmp=get_line(p->program+pos, p, &line); if(tmp && line) { fprintf(stderr,"**Location: %s:%ld\n",tmp,(long)line);
2eeba91999-03-17Fredrik Hübinette (Hubbe)  foo=1;
3568101997-10-16Fredrik Hübinette (Hubbe)  break; }
2eeba91999-03-17Fredrik Hübinette (Hubbe)  if(pos+1>=(long)p->num_program) break; } #if 0 if(!foo && p->num_linenumbers>1 && EXTRACT_UCHAR(p->linenumbers)=='\177') { fprintf(stderr,"**From file: %s\n",p->linenumbers+1); foo=1; } #endif if(!foo) { int e; #if 0 fprintf(stderr,"**identifiers:\n"); for(e=0;e<p->num_identifiers;e++) fprintf(stderr,"**** %s\n",p->identifiers[e].name->str); #else fprintf(stderr,"**identifiers:\n"); for(e=0;e<p->num_identifier_references;e++) fprintf(stderr,"**** %s\n",ID_FROM_INT(p,e)->name->str); #endif fprintf(stderr,"**num inherits: %d\n",p->num_inherits);
f6d0171997-10-15Fredrik Hübinette (Hubbe)  }
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) #define FOO(NUMTYPE,TYPE,NAME) \ fprintf(stderr,"* " #NAME " %p[%d]\n",p->NAME,p->PIKE_CONCAT(num_,NAME)); #include "program_areas.h"
2eeba91999-03-17Fredrik Hübinette (Hubbe) 
f6d0171997-10-15Fredrik Hübinette (Hubbe)  break;
3568101997-10-16Fredrik Hübinette (Hubbe)  }
f6d0171997-10-15Fredrik Hübinette (Hubbe)  case T_ARRAY: fprintf(stderr,"**Describing array:\n"); debug_dump_array((struct array *)a); break;
62971d1998-01-19Fredrik Hübinette (Hubbe) 
61e9a01998-01-25Fredrik Hübinette (Hubbe)  case T_MAPPING:
1d152d2000-03-07Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Describing mapping:\n"); debug_dump_mapping((struct mapping *)a);
9126d42000-03-07Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Describing mapping data block:\n"); describe_something( ((struct mapping *)a)->data, -2, dm);
61e9a01998-01-25Fredrik Hübinette (Hubbe)  break;
62971d1998-01-19Fredrik Hübinette (Hubbe)  case T_STRING: { struct pike_string *s=(struct pike_string *)a; fprintf(stderr,"**String length is %d:\n",s->len); if(s->len>77) { fprintf(stderr,"** \"%60s ...\"\n",s->str); }else{ fprintf(stderr,"** \"%s\"\n",s->str); } break; }
f6d0171997-10-15Fredrik Hübinette (Hubbe)  }
25479a2000-03-07Fredrik Hübinette (Hubbe) } void describe_something(void *a, int t, int dm) { struct program *p=(struct program *)a; if(!a) return; if(t==-1) { fprintf(stderr,"**Location description: %s\n",(char *)a); return; }
3845452000-03-08Henrik Grubbström (Grubba) #ifdef DEBUG_MALLOC if (((int)a) == 0x55555555) { fprintf(stderr,"**Location: %p Type: %s Zapped pointer\n",a, get_name_of_type(t)); } else #endif /* DEBUG_MALLOC */ if (((int)a) & 3) { fprintf(stderr,"**Location: %p Type: %s Missaligned address\n",a, get_name_of_type(t)); } else { fprintf(stderr,"**Location: %p Type: %s Refs: %d\n",a, get_name_of_type(t), *(INT32 *)a); }
25479a2000-03-07Fredrik Hübinette (Hubbe)  low_describe_something(a,t,dm); #ifdef DEBUG_MALLOC if(dm) debug_malloc_dump_references(a); #endif
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  fprintf(stderr,"*******************\n"); } void describe(void *x) { describe_something(x, attempt_to_identify(x),1);
f6d0171997-10-15Fredrik Hübinette (Hubbe) }
c72a4e1998-12-15Fredrik Hübinette (Hubbe) void debug_describe_svalue(struct svalue *s) { fprintf(stderr,"Svalue at %p is:\n",s); switch(s->type) { case T_INT: fprintf(stderr," %ld\n",(long)s->u.integer); break; case T_FLOAT: fprintf(stderr," %f\n",s->u.float_number); break;
2eeba91999-03-17Fredrik Hübinette (Hubbe)  case T_FUNCTION: if(s->subtype == FUNCTION_BUILTIN) { fprintf(stderr," Builtin function: %s\n",s->u.efun->name->str); }else{ if(!s->u.object->prog) {
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  struct program *p=id_to_program(s->u.object->program_id); if(p) { fprintf(stderr," Function (destructed) name: %s\n",ID_FROM_INT(p,s->subtype)->name->str); }else{ fprintf(stderr," Function in destructed object.\n"); }
2eeba91999-03-17Fredrik Hübinette (Hubbe)  }else{ fprintf(stderr," Function name: %s\n",ID_FROM_INT(s->u.object->prog,s->subtype)->name->str); } }
c72a4e1998-12-15Fredrik Hübinette (Hubbe)  } describe_something(s->u.refs,s->type,1); }
b8a6e71996-09-25Fredrik Hübinette (Hubbe) #endif
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) INT32 real_gc_check(void *a)
c94c371996-03-28Fredrik Hübinette (Hubbe) {
235bf32000-04-14Henrik Grubbström (Grubba)  struct marker *m = get_marker(a);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b8a6e71996-09-25Fredrik Hübinette (Hubbe)  if(check_for) { if(check_for == a) { gdb_gc_stop_here(a); }
4a578f1997-01-27Fredrik Hübinette (Hubbe)  return 0;
b8a6e71996-09-25Fredrik Hübinette (Hubbe)  }
699bb02000-04-22Martin Stjernholm  if (m->flags & GC_NONGARBED) fatal("Using gc_check() and gc_check_nongarbed() on the same object.\n");
b8a6e71996-09-25Fredrik Hübinette (Hubbe) #endif
235bf32000-04-14Henrik Grubbström (Grubba)  m->flags |= GC_CHECKED; return add_ref(m);
c94c371996-03-28Fredrik Hübinette (Hubbe) }
699bb02000-04-22Martin Stjernholm INT32 real_gc_check_nongarbed(void *a) { struct marker *m = get_marker(a); #ifdef PIKE_DEBUG if (m->flags & GC_CHECKED) fatal("Using gc_check_nongarbed() and gc_check() on the same object.\n"); #endif if (m->flags & GC_NONGARBED) return 1; else { m->flags |= GC_NONGARBED; return 0; } }
b51e6d1998-02-18Fredrik Hübinette (Hubbe) static void init_gc(void) {
424d9c1999-05-02Fredrik Hübinette (Hubbe) #if 0
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  INT32 tmp3; /* init hash , hashsize will be a prime between num_objects/8 and * num_objects/4, this will assure that no re-hashing is needed. */ tmp3=my_log2(num_objects); if(!d_flag) tmp3-=2; if(tmp3<0) tmp3=0; if(tmp3>=(long)NELEM(hashprimes)) tmp3=NELEM(hashprimes)-1; hashsize=hashprimes[tmp3]; hash=(struct marker **)xalloc(sizeof(struct marker **)*hashsize); MEMSET((char *)hash,0,sizeof(struct marker **)*hashsize); markers_left_in_chunk=0;
424d9c1999-05-02Fredrik Hübinette (Hubbe) #else
1637c42000-02-01Fredrik Hübinette (Hubbe) /* init_marker_hash(num_objects*8); */
424d9c1999-05-02Fredrik Hübinette (Hubbe)  init_marker_hash(); #endif
b51e6d1998-02-18Fredrik Hübinette (Hubbe) } static void exit_gc(void) {
424d9c1999-05-02Fredrik Hübinette (Hubbe) #if 0
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  struct marker_chunk *m; /* Free hash table */ free((char *)hash); while((m=chunk)) { chunk=m->next; free((char *)m); }
424d9c1999-05-02Fredrik Hübinette (Hubbe) #else #ifdef DO_PIKE_CLEANUP int e=0; struct marker *h; for(e=0;e<marker_hash_table_size;e++) while(marker_hash_table[e]) remove_marker(marker_hash_table[e]->data); #endif exit_marker_hash(); #endif
b51e6d1998-02-18Fredrik Hübinette (Hubbe) }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b51e6d1998-02-18Fredrik Hübinette (Hubbe) void locate_references(void *a) {
4fa1832000-04-19Martin Stjernholm  if(!Pike_in_gc)
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  init_gc(); fprintf(stderr,"**Looking for references:\n"); check_for=a;
25d21c1998-02-24Per Hedbor 
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  found_where=" in an array"; gc_check_all_arrays(); found_where=" in a multiset"; gc_check_all_multisets(); found_where=" in a mapping"; gc_check_all_mappings(); found_where=" in a program"; gc_check_all_programs(); found_where=" in an object"; gc_check_all_objects(); found_where=" in a module"; call_callback(& gc_callbacks, (void *)0); found_where=""; check_for=0;
4fa1832000-04-19Martin Stjernholm  if(!Pike_in_gc)
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  exit_gc(); }
25d21c1998-02-24Per Hedbor #endif
b51e6d1998-02-18Fredrik Hübinette (Hubbe) 
1637c42000-02-01Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG int debug_gc_is_referenced(void *a)
c94c371996-03-28Fredrik Hübinette (Hubbe) { struct marker *m;
424d9c1999-05-02Fredrik Hübinette (Hubbe)  m=get_marker(a);
1637c42000-02-01Fredrik Hübinette (Hubbe) 
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  if(m->refs + m->xrefs > *(INT32 *)a || (!(m->refs < *(INT32 *)a) && m->xrefs) )
b8a6e71996-09-25Fredrik Hübinette (Hubbe)  {
1422411997-10-13Fredrik Hübinette (Hubbe)  INT32 refs=m->refs; INT32 xrefs=m->xrefs;
1ca3ba1997-10-13Fredrik Hübinette (Hubbe)  TYPE_T t=attempt_to_identify(a);
f6d0171997-10-15Fredrik Hübinette (Hubbe)  fprintf(stderr,"**Something has %ld references, while gc() found %ld + %ld external.\n",(long)*(INT32 *)a,(long)refs,(long)xrefs);
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  describe_something(a, t, 1);
1ca3ba1997-10-13Fredrik Hübinette (Hubbe) 
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  locate_references(a);
1422411997-10-13Fredrik Hübinette (Hubbe)  fatal("Ref counts are wrong (has %d, found %d + %d external)\n", *(INT32 *)a, refs, xrefs);
b8a6e71996-09-25Fredrik Hübinette (Hubbe)  }
1637c42000-02-01Fredrik Hübinette (Hubbe) 
c94c371996-03-28Fredrik Hübinette (Hubbe)  return m->refs < *(INT32 *)a; }
1637c42000-02-01Fredrik Hübinette (Hubbe) #endif
c94c371996-03-28Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
05c7cd1997-07-19Fredrik Hübinette (Hubbe) int gc_external_mark(void *a) { struct marker *m; if(check_for) { if(a==check_for) {
1ca3ba1997-10-13Fredrik Hübinette (Hubbe)  char *tmp=found_where;
199f9e1997-10-22Fredrik Hübinette (Hubbe)  found_where=" externally";
05c7cd1997-07-19Fredrik Hübinette (Hubbe)  gdb_gc_stop_here(a);
1ca3ba1997-10-13Fredrik Hübinette (Hubbe)  found_where=tmp;
05c7cd1997-07-19Fredrik Hübinette (Hubbe)  return 1; } return 0; }
424d9c1999-05-02Fredrik Hübinette (Hubbe)  m=get_marker(a);
05c7cd1997-07-19Fredrik Hübinette (Hubbe)  m->xrefs++; m->flags|=GC_XREFERENCED; gc_is_referenced(a); return 0; } #endif
c94c371996-03-28Fredrik Hübinette (Hubbe) int gc_mark(void *a) { struct marker *m;
424d9c1999-05-02Fredrik Hübinette (Hubbe)  m=get_marker(a);
c94c371996-03-28Fredrik Hübinette (Hubbe)  if(m->flags & GC_REFERENCED) { return 0; }else{ m->flags |= GC_REFERENCED; return 1; } }
1637c42000-02-01Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG int debug_gc_do_free(void *a)
c94c371996-03-28Fredrik Hübinette (Hubbe) { struct marker *m;
424d9c1999-05-02Fredrik Hübinette (Hubbe)  m=get_marker(a);
1637c42000-02-01Fredrik Hübinette (Hubbe) 
235bf32000-04-14Henrik Grubbström (Grubba)  if( ((m->flags & (GC_REFERENCED|GC_CHECKED)) == GC_CHECKED) && (m->flags & GC_XREFERENCED))
8fb1e11998-04-05Fredrik Hübinette (Hubbe)  { INT32 refs=m->refs; INT32 xrefs=m->xrefs; TYPE_T t=attempt_to_identify(a); fprintf(stderr,"**gc_is_referenced failed, object has %ld references, while gc() found %ld + %ld external.\n",(long)*(INT32 *)a,(long)refs,(long)xrefs); describe_something(a, t, 1); locate_references(a); fatal("GC failed object (has %d, found %d + %d external)\n", *(INT32 *)a, refs, xrefs); }
1637c42000-02-01Fredrik Hübinette (Hubbe) 
235bf32000-04-14Henrik Grubbström (Grubba)  return (m->flags & (GC_REFERENCED|GC_CHECKED)) == GC_CHECKED;
c94c371996-03-28Fredrik Hübinette (Hubbe) }
1637c42000-02-01Fredrik Hübinette (Hubbe) #endif
c94c371996-03-28Fredrik Hübinette (Hubbe) 
be478c1997-08-30Henrik Grubbström (Grubba) void do_gc(void)
6930181996-02-25Fredrik Hübinette (Hubbe) { double tmp;
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  INT32 tmp2;
dc296b1997-10-21Fredrik Hübinette (Hubbe)  double multiplier;
6930181996-02-25Fredrik Hübinette (Hubbe) 
4fa1832000-04-19Martin Stjernholm  if(Pike_in_gc) return;
699bb02000-04-22Martin Stjernholm  init_gc();
4fa1832000-04-19Martin Stjernholm  Pike_in_gc=1; /* Make sure there will be no callback to this while we're in the gc. */ destruct_objects_to_destruct();
890e5b1996-11-21Fredrik Hübinette (Hubbe) 
e78abd1996-11-21Fredrik Hübinette (Hubbe)  if(gc_evaluator_callback) { remove_callback(gc_evaluator_callback); gc_evaluator_callback=0; }
890e5b1996-11-21Fredrik Hübinette (Hubbe) 
6930181996-02-25Fredrik Hübinette (Hubbe)  tmp2=num_objects;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
6930181996-02-25Fredrik Hübinette (Hubbe)  if(t_flag) fprintf(stderr,"Garbage collecting ... ");
06983f1996-09-22Fredrik Hübinette (Hubbe)  if(num_objects < 0) fatal("Panic, less than zero objects!\n");
dc296b1997-10-21Fredrik Hübinette (Hubbe) 
6930181996-02-25Fredrik Hübinette (Hubbe) #endif
4452c12000-02-02Fredrik Hübinette (Hubbe)  last_gc=TIME(0);
dc296b1997-10-21Fredrik Hübinette (Hubbe)  multiplier=pow(MULTIPLIER, (double) num_allocs / (double) alloc_threshold); objects_alloced*=multiplier;
6930181996-02-25Fredrik Hübinette (Hubbe)  objects_alloced += (double) num_allocs;
dc296b1997-10-21Fredrik Hübinette (Hubbe)  objects_freed*=multiplier;
6930181996-02-25Fredrik Hübinette (Hubbe)  objects_freed += (double) num_objects;
c94c371996-03-28Fredrik Hübinette (Hubbe) 
a991451997-07-08Fredrik Hübinette (Hubbe)  /* First we count internal references */
6930181996-02-25Fredrik Hübinette (Hubbe)  gc_check_all_arrays();
06983f1996-09-22Fredrik Hübinette (Hubbe)  gc_check_all_multisets();
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_check_all_mappings();
6930181996-02-25Fredrik Hübinette (Hubbe)  gc_check_all_programs(); gc_check_all_objects();
4a578f1997-01-27Fredrik Hübinette (Hubbe)  call_callback(& gc_callbacks, (void *)0);
c94c371996-03-28Fredrik Hübinette (Hubbe) 
a991451997-07-08Fredrik Hübinette (Hubbe)  /* Next we mark anything with external references */
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_mark_all_arrays();
991e5a1998-04-28Fredrik Hübinette (Hubbe)  run_queue(&gc_mark_queue);
06983f1996-09-22Fredrik Hübinette (Hubbe)  gc_mark_all_multisets();
991e5a1998-04-28Fredrik Hübinette (Hubbe)  run_queue(&gc_mark_queue);
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_mark_all_mappings();
991e5a1998-04-28Fredrik Hübinette (Hubbe)  run_queue(&gc_mark_queue);
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_mark_all_programs();
991e5a1998-04-28Fredrik Hübinette (Hubbe)  run_queue(&gc_mark_queue);
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_mark_all_objects();
991e5a1998-04-28Fredrik Hübinette (Hubbe)  run_queue(&gc_mark_queue);
c94c371996-03-28Fredrik Hübinette (Hubbe) 
4a578f1997-01-27Fredrik Hübinette (Hubbe)  if(d_flag) gc_mark_all_strings();
a991451997-07-08Fredrik Hübinette (Hubbe)  /* Now we free the unused stuff */
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_free_all_unreferenced_arrays();
06983f1996-09-22Fredrik Hübinette (Hubbe)  gc_free_all_unreferenced_multisets();
c94c371996-03-28Fredrik Hübinette (Hubbe)  gc_free_all_unreferenced_mappings(); gc_free_all_unreferenced_programs(); gc_free_all_unreferenced_objects();
b51e6d1998-02-18Fredrik Hübinette (Hubbe)  exit_gc();
699bb02000-04-22Martin Stjernholm  Pike_in_gc=0;
6930181996-02-25Fredrik Hübinette (Hubbe) 
699bb02000-04-22Martin Stjernholm  destruct_objects_to_destruct();
6930181996-02-25Fredrik Hübinette (Hubbe)  objects_freed -= (double) num_objects; tmp=(double)num_objects; tmp=tmp * GC_CONST/100.0 * (objects_alloced+1.0) / (objects_freed+1.0); if((int)tmp < alloc_threshold + num_allocs) { alloc_threshold=(int)tmp; }else{ alloc_threshold+=num_allocs; } if(alloc_threshold < MIN_ALLOC_THRESHOLD) alloc_threshold = MIN_ALLOC_THRESHOLD;
b95bef1996-03-29Fredrik Hübinette (Hubbe)  if(alloc_threshold > MAX_ALLOC_THRESHOLD) alloc_threshold = MAX_ALLOC_THRESHOLD;
6930181996-02-25Fredrik Hübinette (Hubbe)  num_allocs=0;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
6930181996-02-25Fredrik Hübinette (Hubbe)  if(t_flag) fprintf(stderr,"done (freed %ld of %ld objects).\n", (long)(tmp2-num_objects),(long)tmp2); #endif
a29e021996-10-15Fredrik Hübinette (Hubbe) 
bf45771996-12-05Fredrik Hübinette (Hubbe) #ifdef ALWAYS_GC
890e5b1996-11-21Fredrik Hübinette (Hubbe)  ADD_GC_CALLBACK(); #else
bf45771996-12-05Fredrik Hübinette (Hubbe)  if(d_flag > 3) ADD_GC_CALLBACK();
a29e021996-10-15Fredrik Hübinette (Hubbe) #endif
6930181996-02-25Fredrik Hübinette (Hubbe) }
1637c42000-02-01Fredrik Hübinette (Hubbe) void f__gc_status(INT32 args) { pop_n_elems(args); push_constant_text("num_objects"); push_int(num_objects); push_constant_text("num_allocs"); push_int(num_allocs); push_constant_text("alloc_threshold"); push_int(alloc_threshold); push_constant_text("objects_alloced"); push_int(objects_alloced); push_constant_text("objects_freed"); push_int(objects_freed); push_constant_text("last_gc"); push_int(last_gc); push_constant_text("projected_garbage"); push_float(objects_freed * (double) num_allocs / (double) alloc_threshold); f_aggregate_mapping(14); }