Index: /trunk/ccl/lisp-kernel/ppc-exceptions.c
===================================================================
--- /trunk/ccl/lisp-kernel/ppc-exceptions.c	(revision 6512)
+++ /trunk/ccl/lisp-kernel/ppc-exceptions.c	(revision 6513)
@@ -577,7 +577,14 @@
   if (newlimit > (natural) (a->high)) {
     if (extend) {
-      if (! resize_dynamic_heap(a->active, (newlimit-oldlimit)+lisp_heap_gc_threshold)) {
-        return false;
-      }
+      natural extend_by = lisp_heap_gc_threshold;
+      do {
+        if (resize_dynamic_heap(a->active, (newlimit-oldlimit)+extend_by)) {
+          break;
+        }
+        extend_by = align_to_power_of_2(extend_by>>1, log2_allocation_quantum);
+        if (extend_by < 4<<20) {
+          return false;
+        }
+      } while (1);
     } else {
       return false;
@@ -868,5 +875,5 @@
 
 OSStatus
-handle_protection_violation(ExceptionInformation *xp, siginfo_t *info, TCR *tcr)
+handle_protection_violation(ExceptionInformation *xp, siginfo_t *info, TCR *tcr, int old_valence)
 {
   BytePtr addr;
@@ -884,4 +891,5 @@
   if (addr && (addr == tcr->safe_ref_address)) {
     adjust_exception_pc(xp,4);
+
     xpGPR(xp,imm0) = 0;
     return 0;
@@ -902,5 +910,7 @@
     }
   }
-  callback_for_trap(nrs_CMAIN.vcell, xp, (pc)xpPC(xp), SIGBUS, (natural)addr, is_write_fault(xp,info));
+  if (old_valence == TCR_STATE_LISP) {
+    callback_for_trap(nrs_CMAIN.vcell, xp, (pc)xpPC(xp), SIGBUS, (natural)addr, is_write_fault(xp,info));
+  }
   return -1;
 }
@@ -1207,5 +1217,6 @@
                        ExceptionInformation *xp, 
                        TCR *tcr, 
-                       siginfo_t *info)
+                       siginfo_t *info,
+                       int old_valence)
 {
   unsigned oldMQ;
@@ -1225,5 +1236,5 @@
   } else if ((xnum == SIGSEGV) ||
 	     (xnum == SIGBUS)) {
-    status = handle_protection_violation(xp, info, tcr);
+    status = handle_protection_violation(xp, info, tcr, old_valence);
   } else if (xnum == SIGFPE) {
     status = handle_sigfpe(xp, tcr);
@@ -1729,8 +1740,7 @@
 
 void
-signal_handler(int signum, siginfo_t *info, ExceptionInformation  *context, TCR *tcr)
+signal_handler(int signum, siginfo_t *info, ExceptionInformation  *context, TCR *tcr, int old_valence)
 {
   xframe_list xframe_link;
-  int old_valence;
 
 #ifdef DARWIN
@@ -1754,5 +1764,5 @@
   
   wait_for_exception_lock_in_handler(tcr, context, &xframe_link);
-  if ((noErr != PMCL_exception_handler(signum, context, tcr, info))) {
+  if ((noErr != PMCL_exception_handler(signum, context, tcr, info, old_valence))) {
     char msg[512];
     snprintf(msg, sizeof(msg), "Unhandled exception %d at 0x%lx, context->regs at #x%lx", signum, xpPC(context), (natural)xpGPRvector(context));
@@ -2050,5 +2060,5 @@
           fprintf(stderr, "[0x%x acquired exception lock for interrupt]\n",tcr);
 #endif
-	  PMCL_exception_handler(signum, context, tcr, info);
+	  PMCL_exception_handler(signum, context, tcr, info, old_valence);
           if (disp) {
             xpGPR(context,allocptr) -= disp;
@@ -2229,9 +2239,12 @@
 #ifdef DARWIN
 
+
 #define TCR_FROM_EXCEPTION_PORT(p) ((TCR *)((natural)p))
 #define TCR_TO_EXCEPTION_PORT(tcr) ((mach_port_t)((natural)(tcr)))
 
+
+#if USE_MACH_EXCEPTION_LOCK
 pthread_mutex_t _mach_exception_lock, *mach_exception_lock;
-
+#endif
 
 #define LISP_EXCEPTIONS_HANDLED_MASK \
@@ -2500,5 +2513,5 @@
   mach_msg_type_number_t thread_state_count;
   ExceptionInformation *pseudosigcontext;
-  int i, j;
+  int i, j, old_valence = tcr->valence;
   kern_return_t result;
   natural stackp;
@@ -2526,4 +2539,5 @@
   ts.__r4 = (natural)pseudosigcontext;
   ts.__r5 = (natural)tcr;
+  ts.__r6 = (natural)old_valence;
   ts.__lr = (natural)pseudo_sigreturn;
 
@@ -2549,7 +2563,7 @@
 
 void
-pseudo_signal_handler(int signum, ExceptionInformation *context, TCR *tcr)
-{
-  signal_handler(signum, NULL, context, tcr);
+pseudo_signal_handler(int signum, ExceptionInformation *context, TCR *tcr, int old_valence)
+{
+  signal_handler(signum, NULL, context, tcr, old_valence);
 } 
 
@@ -2660,5 +2674,11 @@
 #endif
 
-  if (pthread_mutex_trylock(mach_exception_lock) == 0) {
+  if (
+#if USE_MACH_EXCEPTION_LOCK
+    pthread_mutex_trylock(mach_exception_lock) == 0
+#else
+    1
+#endif
+    ) {
     if (tcr->flags & (1<<TCR_FLAG_BIT_PENDING_EXCEPTION)) {
       CLR_TCR_FLAG(tcr,TCR_FLAG_BIT_PENDING_EXCEPTION);
@@ -2718,8 +2738,10 @@
       }
     }
+#if USE_MACH_EXCEPTION_LOCK
 #ifdef DEBUG_MACH_EXCEPTIONS
     fprintf(stderr, "releasing Mach exception lock in exception thread\n");
 #endif
     pthread_mutex_unlock(mach_exception_lock);
+#endif
   } else {
     SET_TCR_FLAG(tcr,TCR_FLAG_BIT_PENDING_EXCEPTION);
@@ -2813,7 +2835,8 @@
   kern_return_t kret;  
   if (__exception_port_set == MACH_PORT_NULL) {
+#if USE_MACH_EXCEPTION_LOCK
     mach_exception_lock = &_mach_exception_lock;
     pthread_mutex_init(mach_exception_lock, NULL);
-
+#endif
     kret = mach_port_allocate(mach_task_self(),
 			      MACH_PORT_RIGHT_PORT_SET,
@@ -3090,5 +3113,7 @@
 
   LOCK(lisp_global(TCR_AREA_LOCK), current);
+#if USE_MACH_EXCEPTION_LOCK
   pthread_mutex_lock(mach_exception_lock);
+#endif
 
   if (suspend_mach_thread(mach_thread)) {
@@ -3118,5 +3143,7 @@
     
   }
+#if USE_MACH_EXCEPTION_LOCK
   pthread_mutex_unlock(mach_exception_lock);
+#endif
   UNLOCK(lisp_global(TCR_AREA_LOCK), current);
   return 0;
