diff -ruN klic-3.003-2002-02-18e/runtime/gc.c klic-3.003-2002-02-19/runtime/gc.c
--- klic-3.003-2002-02-18e/runtime/gc.c	Mon Feb 18 17:10:57 2002
+++ klic-3.003-2002-02-19/runtime/gc.c	Tue Feb 19 13:16:42 2002
@@ -219,9 +219,8 @@
   if( susp ) copied_susp++;				\
 }while(0)
 
-static q*
-copy_terms(hp, ntop, otop, nsize, osize, sp, max)
-  q* hp;
+static void
+copy_terms(ntop, otop, nsize, osize, sp, max)
   q* ntop;
   q* otop;
   unsigned long nsize, osize;
@@ -229,6 +228,7 @@
   q** max;
 {
   declare_globals;
+  q* hp = heapp();
   while( sp > gcstack ){
     q* addr;
     q obj;
@@ -453,13 +453,12 @@
     }
   }
   gcsp = sp;
-  return hp;
+  set_heapp(hp);
 }
 
 static struct goalrec*
-copy_one_queue(qp, hp, ntop, otop, nsize, osize)
+copy_one_queue(qp, ntop, otop, nsize, osize)
   struct goalrec* qp;
-  q* hp;
   q* ntop;
   q* otop;
   unsigned long nsize, osize;
@@ -482,6 +481,7 @@
   qp = last;
   last = &goal_queue_tail;
   for( ; qp != NULL; qp=next ){
+    q* hp;
     next = qp->next;
 #ifdef SHM
     if( is_shma(qp) ){
@@ -490,12 +490,13 @@
       continue; /* for qp */
     }
 #endif
+    hp = heapp();
     copy_one_goal(qp, gcsp, gcmax, 0);
-    hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+    set_heapp(hp);
+    copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
     qp->next = last;
     last = qp;
   }
-  set_heapp(hp);
   return last;
 }
 
@@ -582,15 +583,14 @@
     allocp = gc_hook_table[k](allocp, ntop, otop, nsize, osize);
   }
 
+  set_heapp(allocp);
   for( ; pq->prio >= 0; pq = pq->next ){
-    pq->q = copy_one_queue(pq->q, allocp, ntop, otop, nsize, osize);
-    allocp = heapp();
+    pq->q = copy_one_queue(pq->q, ntop, otop, nsize, osize);
   }
-  qp = copy_one_queue(qp, allocp, ntop, otop, nsize, osize);
+  qp = copy_one_queue(qp, ntop, otop, nsize, osize);
 
 #ifdef SHM
   {
-    q* hp = heapp();
     TADDRtbl* nptr;
     TADDRtbl* sptr = &ADDRtbl;
     for( sptr=sptr->next; sptr!=&ADDRtbl; sptr=nptr ){
@@ -599,7 +599,7 @@
       case CONS:
       case FUNCTOR:  /* generator hook but anybody reqested */
         push_gc_stack((q*) &sptr->localA, gcsp, gcmax);
-        hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+	copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
         push_shm_stack(&sptr->globalA, shm_sp, shm_gcmax);
         break;
       case ATOMIC: /* genarator object (distributed interface) */
@@ -616,7 +616,7 @@
 
 	    /* patch for debugging of hirata problem */
 	    hirata_bug1 = 1;
-	    hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+	    copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
 	    hirata_bug1 = 0;
 
 	    wk = derefone(top);
@@ -626,7 +626,7 @@
 	    derefone(wk) = 0;
 	  }else{ /* generator */
 	    push_gc_stack(&wk, gcsp, gcmax);
-	    hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+	    copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
 	    sptr->localA = (q*) tag_local(wk);
 	  }
 	  push_shm_stack(&sptr->globalA, shm_sp, shm_gcmax);
@@ -641,7 +641,7 @@
 	  }else if( isint(wqp->next) ){
 	    copy_one_goal(wqp, gcsp, gcmax, 1);
 	    sptr->localA = (q*) wqp;
-	    hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+	    copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
 	    if( isref(sptr->globalA) ){
 	      for(;;){
 		q ww = derefone(sptr->globalA);
@@ -686,7 +686,6 @@
 	}
       }
     }
-    set_heapp(hp);
   }
 #endif /* SHM */
 
@@ -725,7 +724,9 @@
       }else{
 	/* not copied yet */
 	copy_one_goal(sgl->goal, gcsp, gcmax, 0);
-	hp = copy_terms(hp, ntop, otop, nsize, osize, gcsp, gcmax);
+	set_heapp(hp);
+	copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
+	hp = heapp();
 	dead_goal = sgl->goal;
       }
     }
@@ -765,8 +766,9 @@
 {
   declare_globals;
   push_gc_stack(term, gcsp, gcmax);
-  return
-    copy_terms(allocp, ntop, otop, nsize, osize, gcsp, gcmax);
+  set_heapp(allocp);
+  copy_terms(ntop, otop, nsize, osize, gcsp, gcmax);
+  return heapp();
 }
 
 /*
@@ -781,8 +783,8 @@
   q* allocp = heapp();
 
   push_gc_stack(term, sp, gcmax);
-  set_heapp( copy_terms(allocp, new_space_top, old_space_top,
-		     new_space_size, old_space_size,
-		     sp, gcmax) );
+  set_heapp(allocp);
+  copy_terms(new_space_top, old_space_top, new_space_size, old_space_size,
+	     sp, gcmax);
   return *term;
 }
