diff -ruN klic-3.003-2002-02-24/include/klic/gc_macro.h klic-3.003-2002-02-24a/include/klic/gc_macro.h
--- klic-3.003-2002-02-24/include/klic/gc_macro.h	Sun Feb 24 13:16:21 2002
+++ klic-3.003-2002-02-24a/include/klic/gc_macro.h	Sun Feb 24 14:31:09 2002
@@ -65,9 +65,11 @@
   rest_of_stream = newvar; \
   argv[0] = GC_TERM; \
   argv[1] = newvar; \
+  assert(g_allocp >= heapp() + 1); \
   tmp = GC_wakeup_g_new(2, argv, g_allocp); \
   g_allocp = heapp(); \
   GC_KL1_UNIFY((var), tmp); \
+  assert(g_allocp == heapp() + 4); \
   return g_allocp; \
 }while(0)
 
@@ -102,26 +104,27 @@
 #define GC_TRY_TO_ALLOC(new, type, size, gc_request) \
 do{ \
   q res; \
-  set_heapp(g_allocp); \
+  assert(g_allocp==heapp()); \
   G_HEAPALLOC_WITH_CHECK((new), (size), type, res); \
-  assert(res==GENERIC_GCREQUEST || \
-        (res==GENERIC_SUCCEEDED && g_allocp >= heapp()+(size)) ); \
+  assert((res==GENERIC_GCREQUEST && g_allocp == heapp()) || \
+         (res==GENERIC_SUCCEEDED && g_allocp == heapp()+(size)) ); \
   if (GC_GCREQUEST == res) goto gc_request; \
 }while(0)
 
 
 #define GC_UNIFY(x,y) \
-  (set_heapp(g_allocp), do_shallow_unify((x), (y)), g_allocp = heapp())
+  (assert(g_allocp==heapp()), do_shallow_unify((x), (y)), g_allocp = heapp())
 
 #define GC_UNIFY_VALUE(x, y)					\
 do{								\
+  assert(g_allocp == heapp()); \
   if (!isref(x) || derefone(x) != (x)) {			\
-    set_heapp(g_allocp); \
     do_shallow_unify_value((x), (y));	\
     g_allocp = heapp(); \
   } else {							\
     derefone(x) = (y);						\
   }								\
+  assert(g_allocp == heapp()); \
 }while(0)
 
 /**************************************************************************/
@@ -170,10 +173,10 @@
 #define GCSET_NEWOBJ_FOR_NEW(newgobj,size) \
 do{ \
   q res; \
-  set_heapp(g_allocp); \
+  assert(g_allocp == heapp()); \
   G_HEAPALLOC_WITH_CHECK((newgobj), (size), (GC_OBJ_TYPE*), res); \
-  assert(res==GENERIC_GCREQUEST || \
-        (res==GENERIC_SUCCEEDED && g_allocp >= heapp()+(size)) ); \
+  assert((res==GENERIC_GCREQUEST && g_allocp == heapp()) || \
+         (res==GENERIC_SUCCEEDED && g_allocp == heapp()+(size)) ); \
   if(GC_GCREQUEST == res){ \
     q var; \
     struct goalrec *goal; \
@@ -235,8 +238,8 @@
 
 #define GC_RETURN_FROM_GC(newgobj) \
 do{ \
+  assert(g_allocp == heapp()); \
   gcsp  = g_sp; \
-  set_heapp(g_allocp); \
   return (q *) (newgobj); \
 }while(0)
 
@@ -316,6 +319,7 @@
   gp->args[0] = (x); \
   gp->args[1] = (y); \
   resume_same_prio(gp); \
+  assert(g_allocp >= heapp() + 4); \
 }while(0)
 
 #endif /* _KLIC_GC_MACRO_H_ */
diff -ruN klic-3.003-2002-02-24/runtime/ge_readhook.c klic-3.003-2002-02-24a/runtime/ge_readhook.c
--- klic-3.003-2002-02-24/runtime/ge_readhook.c	Sun Feb 24 13:20:34 2002
+++ klic-3.003-2002-02-24a/runtime/ge_readhook.c	Sun Feb 24 14:25:21 2002
@@ -79,6 +79,7 @@
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->to_read_hook, newself->to_read_hook);
   assert(g_allocp >= heapp());
 
+  set_heapp(g_allocp);
   GC_RETURN_FROM_GC(newself);
 }
 
@@ -96,7 +97,9 @@
   GC_STD_DECL_FOR_NEW;
   GC_OBJ_TYPE* new_read_hook;
 
+  set_heapp(g_allocp);
   GCSET_NEWOBJ_FOR_NEW(new_read_hook, GC_OBJ_SIZE(new_read_hook));
+  assert(g_allocp == heapp() + GC_OBJ_SIZE(new_read_hook));
   new_read_hook->node  = (long) intval(GC_ARGV[0]);
   new_read_hook->index = (long) intval(GC_ARGV[1]);
   new_read_hook->wec   = (long) intval(GC_ARGV[2]);
diff -ruN klic-3.003-2002-02-24/runtime/ge_replyhook.c klic-3.003-2002-02-24a/runtime/ge_replyhook.c
--- klic-3.003-2002-02-24/runtime/ge_replyhook.c	Mon Jan 21 20:55:25 2002
+++ klic-3.003-2002-02-24a/runtime/ge_replyhook.c	Sun Feb 24 14:25:13 2002
@@ -52,6 +52,7 @@
   newself->index = GC_SELF->index;
   newself->wec = GC_SELF->wec;
 
+  set_heapp(g_allocp);
   GC_RETURN_FROM_GC(newself);
 }
 
@@ -71,7 +72,9 @@
   GC_OBJ_TYPE* new_reply_hook;
   q var;
 
+  set_heapp(g_allocp);
   GCSET_NEWOBJ_FOR_NEW(new_reply_hook, GC_OBJ_SIZE(new_reply_hook));
+  assert(g_allocp == heapp() + GC_OBJ_SIZE(new_reply_hook));
   new_reply_hook->node = intval(GC_ARGV[0]);
   new_reply_hook->index  = intval(GC_ARGV[1]);
   new_reply_hook->wec    = intval(GC_ARGV[2]);
diff -ruN klic-3.003-2002-02-24/runtime/gio.c klic-3.003-2002-02-24a/runtime/gio.c
--- klic-3.003-2002-02-24/runtime/gio.c	Sun Feb 24 13:21:03 2002
+++ klic-3.003-2002-02-24a/runtime/gio.c	Sun Feb 24 14:31:32 2002
@@ -113,7 +113,9 @@
 	  c = klic_getc(GC_SELF->infile);
 	}
 	if( c=='\n' ){ GC_SELF->linecount++; }
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(c));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -167,12 +169,15 @@
 	g_allocp = heapp();
 	free(buf);
 	GC_UNIFY_VALUE(G_ARG(message, 1), string);
+	assert(g_allocp == heapp());
 	break;
       }
 
       case functor_linecount_1: {
 	CheckInput();
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(GC_SELF->linecount));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -213,7 +218,9 @@
 	  written = klic_fwrite(generic_string_body(str),
 				1, size, GC_SELF->outfile);
 	}
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 1), G_MAKEINT(written));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -260,7 +267,9 @@
 	    iseof = feof(GC_SELF->outfile);
 	  }
 	}
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(iseof));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -279,7 +288,9 @@
 	    (GC_SELF->infile == NULL ? GC_SELF->outfile : GC_SELF->infile);
 	  result = fseek(file, offset, whence);
 	}
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 2), G_MAKEINT(result));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -296,13 +307,17 @@
 	    (GC_SELF->infile == NULL ? GC_SELF->outfile : GC_SELF->infile);
 	  result = ftell(file);
 	}
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(result));
+	assert(g_allocp == heapp());
 	break;
       }
 
       case functor_fflush_1: {
 	long result = do_flush(GC_SELF);
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(result));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -331,12 +346,16 @@
 	    }
 	  }
 	}
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(result));
+	assert(g_allocp == heapp());
 	break;
       }
 
       case functor_sync_1: {
+	set_heapp(g_allocp);
 	GC_UNIFY_VALUE(G_ARG(message, 0), G_MAKEINT(0));
+	assert(g_allocp == heapp());
 	break;
       }
 
@@ -398,6 +417,7 @@
   G_MAKE_VAR(newvar);
   assert(g_allocp >= heapp() + 1);
   GC_KL1_UNIFY(GC_TERM, newvar);
+  assert(g_allocp >= heapp() + 4);
   GC_RETURN_WITH_HOOK(newvar);
 
  suspend:
@@ -420,6 +440,7 @@
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->inname, newself->inname);
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->outname, newself->outname);
   assert(g_allocp >= heapp());
+  set_heapp(g_allocp);
   GC_RETURN_FROM_GC(newself);
 }
 
@@ -477,7 +498,9 @@
   outpath = GC_ARGV[3];
   GC_DEREF_FOR_NEW(outpath);
 
+  set_heapp(g_allocp);
   GCSET_NEWOBJ_FOR_NEW(newobj, GC_OBJ_SIZE(newobj));
+  assert(g_allocp == heapp() + GC_OBJ_SIZE(newobj));
   if( infile == NILATOM ){
     newobj->infile = NULL;
   }else{
diff -ruN klic-3.003-2002-02-24/runtime/gmerge.c klic-3.003-2002-02-24a/runtime/gmerge.c
--- klic-3.003-2002-02-24/runtime/gmerge.c	Sun Feb 24 13:21:14 2002
+++ klic-3.003-2002-02-24a/runtime/gmerge.c	Sun Feb 24 14:31:55 2002
@@ -55,10 +55,14 @@
   list0:
     {
       struct cons *newout;
+      set_heapp(g_allocp);
       GC_TRY_TO_ALLOC(newout,(struct cons *),2,gc_request);
+      assert(g_allocp == heapp() + 2);
       newout->car = G_CAR_OF(GC_TERM);
       GCSET_VAR(newout->cdr);
+      set_heapp(g_allocp);
       GC_UNIFY(GC_SELF->outstream,G_MAKECONS(newout));
+      assert(g_allocp == heapp());
       GC_SELF->outstream = newout->cdr;
       GC_TERM = G_CDR_OF(GC_TERM);
       goto top;
@@ -66,7 +70,9 @@
   atomic0:
     if (GC_TERM == NILATOM) {
       if (--(GC_SELF->count) == 0) {
+	set_heapp(g_allocp);
 	GC_UNIFY(GC_SELF->outstream,NILATOM);
+	assert(g_allocp == heapp());
       }
       GC_TERMINATE;
     } else goto invalid_data;
@@ -83,21 +89,27 @@
       }
       GC_SELF->count += size - 1;
       if (GC_SELF->count == 0) {
+	set_heapp(g_allocp);
 	GC_UNIFY(GC_SELF->outstream,NILATOM);
+	assert(g_allocp == heapp());
       } else {
 	q argv[2];
 	q hook_var;
 	for (i=0; i<size; i++) {
 	  argv[0] = G_MAKEINT(i);
 	  GD_CALL_GMETHOD(GC_TERM,element_2,argv);
+	  set_heapp(g_allocp);
 	  GC_TRY_TO_ALLOC(hook_var,(q),1,gc_request2);
+	  assert(g_allocp == heapp() + 1);
 	  derefone(hook_var) =
 	    GC_MAKE_HOOK_VAR((struct consumer_object*) GC_SELF);
 	  GC_KL1_UNIFY(argv[1],hook_var);
+	  assert(g_allocp >= heapp() + 4);
 	}
       }
       GC_TERMINATE;
     gc_request2:
+      assert(g_allocp == heapp());
       GC_SELF->count -= size-i-1;
       G_MAKE_VAR(newvar);
       assert(g_allocp >= heapp() + 1);
@@ -110,9 +122,11 @@
   debug_printf("### %k ###\n", GC_TERM);
   GC_FAIL("Invalid data unified with merger");
  gc_request:
+  assert(g_allocp == heapp());
   G_MAKE_VAR(newvar);
   assert(g_allocp >= heapp() + 1);
   GC_KL1_UNIFY(GC_TERM,newvar);
+  assert(g_allocp >= heapp() + 4);
   GC_RETURN_WITH_HOOK(newvar);
 }
 
@@ -125,6 +139,7 @@
   newself->count = GC_SELF->count;
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->outstream,newself->outstream);
   assert(g_allocp >= heapp());
+  set_heapp(g_allocp);
   GC_RETURN_FROM_GC(newself);
 }
 
@@ -152,7 +167,9 @@
 
   if (GC_ARGC != 1) GC_ERROR_IN_NEW("Arity mismatch");
 
+  set_heapp(g_allocp);
   GCSET_NEWOBJ_FOR_NEW(newmerger,GC_OBJ_SIZE(newmerger));
+  assert(g_allocp == heapp() + GC_OBJ_SIZE(newmerger));
   newmerger->count = 1;
   newmerger->outstream = GC_ARGV[0];
   var = GC_MAKE_HOOK_VAR((struct consumer_object*) newmerger);
diff -ruN klic-3.003-2002-02-24/runtime/shm_obj.c klic-3.003-2002-02-24a/runtime/shm_obj.c
--- klic-3.003-2002-02-24/runtime/shm_obj.c	Sat Feb 16 12:04:51 2002
+++ klic-3.003-2002-02-24a/runtime/shm_obj.c	Sun Feb 24 14:29:34 2002
@@ -62,20 +62,6 @@
   (lrec) = temp; \
 }
 
-/* Resume a goal with the same priority as current */
-
-#define resume_same_prio(gp) {        \
-  declare_globals;                    \
-  if( resumed_goals == 0 ){           \
-    resumed_goals = gp;               \
-    gp->next = gp;                    \
-  }else{                            \
-    gp->next = resumed_goals->next;   \
-    resumed_goals->next = gp;         \
-  }                                   \
-  heaplimit = 0;                      \
-}
-
 
 static module shm_resume(struct global_variables* glbl,
   struct goalrec* qp, q* allocp, const struct predicate* toppred);
diff -ruN klic-3.003-2002-02-24/runtime/wakeup.c klic-3.003-2002-02-24a/runtime/wakeup.c
--- klic-3.003-2002-02-24/runtime/wakeup.c	Sun Feb 24 13:21:43 2002
+++ klic-3.003-2002-02-24a/runtime/wakeup.c	Sun Feb 24 14:32:07 2002
@@ -28,6 +28,7 @@
   G_STD_DECL;
 
   GC_KL1_UNIFY(GC_SELF->x,GC_SELF->y);
+  assert(g_allocp >= heapp() + 4);
   GC_TERMINATE;
 }
 
@@ -40,6 +41,7 @@
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->x,newself->x);
   G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->y,newself->y);
   assert(g_allocp >= heapp());
+  set_heapp(g_allocp);
   GC_RETURN_FROM_GC(newself);
 }
 
@@ -67,7 +69,9 @@
 
   if (GC_ARGC != 2) GC_FAIL("arity mismatch in creation of a merger.");
 
+  set_heapp(g_allocp);
   GCSET_NEWOBJ_FOR_NEW(wakeup,GC_OBJ_SIZE(wakeup));
+  assert(g_allocp == heapp() + GC_OBJ_SIZE(wakeup));
   wakeup->x = GC_ARGV[0];
   wakeup->y = GC_ARGV[1];
   var = GC_MAKE_HOOK_VAR((struct consumer_object*) wakeup);
