From 35681031c20c89421bfc4cb5d2ab7d99dba122c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Thu, 16 Oct 1997 19:31:41 -0700
Subject: [PATCH] garbage collector bug fixed

Rev: src/gc.c:1.18
Rev: src/object.c:1.27
Rev: src/program.c:1.45
---
 src/gc.c      | 60 +++++++++++++++++++++++++++++++++++++++++++++------
 src/object.c  | 36 +++++++++++++++++--------------
 src/program.c | 55 ++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 127 insertions(+), 24 deletions(-)

diff --git a/src/gc.c b/src/gc.c
index 4d2b0b416f..99ab80fc4c 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -149,11 +149,49 @@ static void *found_in=0;
 static TYPE_T found_in_type=0;
 void *gc_svalue_location=0;
 
+void describe_location(void *memblock, TYPE_T type, void *location)
+{
+  fprintf(stderr,"**Location of (short) svalue: %p\n",location);
+  if(type==T_OBJECT)
+  {
+    struct object *o=(struct object *)memblock;
+    if(o->prog)
+    {
+      INT32 e,d;
+      for(e=0;e<o->prog->num_inherits;e++)
+      {
+	struct inherit tmp=o->prog->inherits[e];
+	char *base=o->storage + tmp.storage_offset;
+
+	for(d=0;d<tmp.prog->num_identifiers;d++)
+	{
+	  struct identifier *id=tmp.prog->identifiers+d;
+	  if(!IDENTIFIER_IS_VARIABLE(id->identifier_flags)) continue;
+
+	  if(location == (void *)(base + id->func.offset))
+	  {
+	    fprintf(stderr,"**In variable %s\n",id->name->str);
+	  }
+	}
+      }
+    }
+    return;
+  }
+
+  if(type == T_ARRAY)
+  {
+    struct array *a=(struct array *)memblock;
+    struct svalue *s=(struct svalue *)location;
+    fprintf(stderr,"**In index %ld\n",(long)(s-ITEM(a)));
+    return;
+  }
+}
+
 static void gdb_gc_stop_here(void *a)
 {
   fprintf(stderr,"**One ref found%s.\n",found_where);
-  fprintf(stderr,"**Location of (short) svalue: %p\n",gc_svalue_location);
   describe_something(found_in, found_in_type);
+  describe_location(found_in, found_in_type, gc_svalue_location);
 }
 
 TYPE_FIELD debug_gc_check_svalues(struct svalue *s, int num, TYPE_T t, void *data)
@@ -194,18 +232,28 @@ void describe_something(void *a, TYPE_T t)
       fprintf(stderr,"**Attempting to describe program object was instantiated from:\n");
       
     case T_PROGRAM:
+    {
+      char *tmp;
+      INT32 line,pos;
+
+      fprintf(stderr,"**Program id: %ld\n",(long)(p->id));
       if(!p->num_linenumbers)
       {
 	fprintf(stderr,"**The program was written in C.\n");
 	break;
       }
-      if(p->linenumbers[0]==127)
+
+      for(pos=0;pos<(long)p->program_size && pos<100;pos++)
       {
-	fprintf(stderr,"**The program may have been compiled from %s.\n",p->linenumbers+1);
-	break;
+	tmp=get_line(p->program+pos, p, &line);
+	if(tmp && line)
+	{
+	  fprintf(stderr,"**Location: %s:%ld\n",tmp,(long)line);
+	  break;
+	}
       }
-      fprintf(stderr,"**No information available about this program.\n");
       break;
+    }
       
     case T_ARRAY:
       fprintf(stderr,"**Describing array:\n");
@@ -243,7 +291,7 @@ int gc_is_referenced(void *a)
     TYPE_T t=attempt_to_identify(a);
 
     fprintf(stderr,"**Something has %ld references, while gc() found %ld + %ld external.\n",(long)*(INT32 *)a,(long)refs,(long)xrefs);
-    describe_something(t, a);
+    describe_something(a, t);
 
     fprintf(stderr,"**Looking for references:\n");
     check_for=a;
diff --git a/src/object.c b/src/object.c
index 6bca7aa956..6c24f45ed6 100644
--- a/src/object.c
+++ b/src/object.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: object.c,v 1.26 1997/10/16 06:34:26 hubbe Exp $");
+RCSID("$Id: object.c,v 1.27 1997/10/17 02:31:40 hubbe Exp $");
 #include "object.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
@@ -670,7 +670,7 @@ int object_equal_p(struct object *a, struct object *b, struct processing *p)
   if(a->prog)
   {
     int e;
-    for(e=0;e<(int)a->prog->num_identifiers;e++)
+    for(e=0;e<(int)a->prog->num_identifier_references;e++)
     {
       struct identifier *i;
       i=ID_FROM_INT(a->prog, e);
@@ -836,22 +836,26 @@ void gc_check_all_objects(void)
   {
     if(o->prog)
     {
-      INT32 e;
-
-      for(e=0;e<(int)o->prog->num_identifier_indexes;e++)
+      INT32 e,d;
+      
+      for(e=0;e<(int)o->prog->num_inherits;e++)
       {
-	struct identifier *i;
-	
-	i=ID_FROM_INT(o->prog, e);
-	
-	if(!IDENTIFIER_IS_VARIABLE(i->identifier_flags)) continue;
-	
-	if(i->run_time_type == T_MIXED)
+	struct inherit in=o->prog->inherits[e];
+	char *base=o->storage + in.storage_offset;
+	for(d=0;d<in.prog->num_identifiers;d++)
 	{
-	  debug_gc_check_svalues((struct svalue *)LOW_GET_GLOBAL(o,e,i),1, T_OBJECT, o);
-	}else{
-	  debug_gc_check_short_svalue((union anything *)LOW_GET_GLOBAL(o,e,i),
-				      i->run_time_type, T_OBJECT,o);
+	  struct identifier *i=in.prog->identifiers+d;
+
+	  if(!IDENTIFIER_IS_VARIABLE(i->identifier_flags)) continue;
+	
+	  if(i->run_time_type == T_MIXED)
+	  {
+	    debug_gc_check_svalues((struct svalue *)(base+i->func.offset),1, T_OBJECT, o);
+	  }else{
+	    debug_gc_check_short_svalue((union anything *)
+					(base+i->func.offset),
+					i->run_time_type, T_OBJECT,o);
+	  }
 	}
       }
     }
diff --git a/src/program.c b/src/program.c
index 56c2b827a3..2ccd50f67a 100644
--- a/src/program.c
+++ b/src/program.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: program.c,v 1.44 1997/10/16 06:34:27 hubbe Exp $");
+RCSID("$Id: program.c,v 1.45 1997/10/17 02:31:41 hubbe Exp $");
 #include "program.h"
 #include "object.h"
 #include "dynamic_buffer.h"
@@ -534,11 +534,12 @@ void check_program(struct program *p)
   {
     if(p->identifier_references[e].inherit_offset > p->num_inherits)
       fatal("Inherit offset is wrong!\n");
-
+    
     if(p->identifier_references[e].identifier_offset >
        p->inherits[p->identifier_references[e].inherit_offset].prog->num_identifiers)
       fatal("Identifier offset is wrong!\n");
   }
+    
 
   for(e=0;e<(int)p->num_identifier_indexes;e++)
   {
@@ -548,8 +549,56 @@ void check_program(struct program *p)
 
   for(e=0;e<(int)p->num_inherits;e++)
   {
+    struct program *tmp_prog=p->inherits[e].prog;
+    INT32 d;
+
     if(p->inherits[e].storage_offset < 0)
       fatal("Inherit->storage_offset is wrong.\n");
+
+    for(d=0;d<(int)tmp_prog->num_identifiers;d++)
+    {
+      struct identifier *id=tmp_prog->identifiers+d;
+
+      if(IDENTIFIER_IS_VARIABLE(id->identifier_flags))
+      {
+	INT32 offset,size,e2;
+	offset=p->inherits[e].storage_offset + id->func.offset;
+	size=id->run_time_type == T_MIXED ? sizeof(struct svalue) : sizeof(union anything);
+
+	if(offset < 0 || offset+size > p->storage_needed)
+	  fatal("Variable located outside allocated space.\n");
+
+
+	for(e2=0;e2<(int)p->num_inherits;e2++)
+	{
+	  struct program *tmp_prog2=p->inherits[e2].prog;
+	  INT32 d2;
+
+	  for(d2=0;d2<(int)tmp_prog2->num_identifiers;d2++)
+	  {
+	    struct identifier *id2=tmp_prog2->identifiers+d2;
+
+	    if(e==e2 && d==d2) continue;
+
+	    if(IDENTIFIER_IS_VARIABLE(id2->identifier_flags))
+	    {
+	      INT32 offset2,size2;
+
+	      offset2=p->inherits[e2].storage_offset + id2->func.offset;
+	      size2=id2->run_time_type == T_MIXED ? sizeof(struct svalue) : sizeof(union anything);
+	      if( (offset > offset2) ?
+		  (offset2+size2 > offset) : 
+		  (offset+size > offset2))
+	      {
+		fatal("Variable %s (%ld+%ld) and %s (%ld+%ld) overlap.\n",
+		      id->name->str, (long)offset, (long)size,
+		      id2->name->str, (long)offset2, (long)size2);
+	      }
+	    }
+	  }
+	}
+      }
+    }
   }
 }
 #endif
@@ -1736,6 +1785,7 @@ void gc_check_all_programs(void)
   {
     debug_gc_check_svalues(p->constants, p->num_constants, T_PROGRAM, p);
 
+#ifdef DEBUG
     if(d_flag)
     {
       int e;
@@ -1748,6 +1798,7 @@ void gc_check_all_programs(void)
 	gc_check(p->identifiers[e].type);
       }
     }
+#endif
   }
 }
 
-- 
GitLab