OSDN Git Service

Introduce and use config/gcc-version.m4.
[pf3gnuchains/gcc-fork.git] / libmudflap / mf-runtime.c
index 8b1cc74..64b1842 100644 (file)
@@ -2,6 +2,8 @@
    Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
    Contributed by Frank Ch. Eigler <fche@redhat.com>
    and Graydon Hoare <graydon@redhat.com>
+   Splay Tree code originally by Mark Mitchell <mark@markmitchell.com>,
+   adapted from libiberty.
 
 This file is part of GCC.
 
@@ -33,7 +35,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 /* These attempt to coax various unix flavours to declare all our
    needed tidbits in the system headers.  */
-#if !defined(__FreeBSD__)
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
 #define _POSIX_SOURCE
 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
 #define _GNU_SOURCE 
@@ -67,10 +69,63 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 #include "mf-runtime.h"
 #include "mf-impl.h"
-#include "splay-tree.h"
 
 
 /* ------------------------------------------------------------------------ */
+/* Splay-tree implementation.  */
+
+typedef uintptr_t mfsplay_tree_key;
+typedef void *mfsplay_tree_value;
+
+/* Forward declaration for a node in the tree.  */
+typedef struct mfsplay_tree_node_s *mfsplay_tree_node;
+
+/* The type of a function used to iterate over the tree.  */
+typedef int (*mfsplay_tree_foreach_fn) (mfsplay_tree_node, void *);
+
+/* The nodes in the splay tree.  */
+struct mfsplay_tree_node_s
+{
+  /* Data.  */
+  mfsplay_tree_key key;
+  mfsplay_tree_value value;
+  /* Children.  */
+  mfsplay_tree_node left;
+  mfsplay_tree_node right;
+  /* XXX: The addition of a parent pointer may eliminate some recursion.  */
+};
+
+/* The splay tree itself.  */
+struct mfsplay_tree_s
+{
+  /* The root of the tree.  */
+  mfsplay_tree_node root;
+
+  /* The last key value for which the tree has been splayed, but not
+     since modified.  */
+  mfsplay_tree_key last_splayed_key;
+  int last_splayed_key_p;
+
+  /* Statistics.  */
+  unsigned num_keys;
+
+  /* Traversal recursion control flags.  */
+  unsigned max_depth;
+  unsigned depth;
+  unsigned rebalance_p;
+};
+typedef struct mfsplay_tree_s *mfsplay_tree;
+
+static mfsplay_tree mfsplay_tree_new (void);
+static mfsplay_tree_node mfsplay_tree_insert (mfsplay_tree, mfsplay_tree_key, mfsplay_tree_value);
+static void mfsplay_tree_remove (mfsplay_tree, mfsplay_tree_key);
+static mfsplay_tree_node mfsplay_tree_lookup (mfsplay_tree, mfsplay_tree_key);
+static mfsplay_tree_node mfsplay_tree_predecessor (mfsplay_tree, mfsplay_tree_key);
+static mfsplay_tree_node mfsplay_tree_successor (mfsplay_tree, mfsplay_tree_key);
+static int mfsplay_tree_foreach (mfsplay_tree, mfsplay_tree_foreach_fn, void *);
+static void mfsplay_tree_rebalance (mfsplay_tree sp);
+
+/* ------------------------------------------------------------------------ */
 /* Utility macros */
 
 #define CTOR  __attribute__ ((constructor))
@@ -105,7 +160,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 /* Required globals.  */
 
 #define LOOKUP_CACHE_MASK_DFL 1023
-#define LOOKUP_CACHE_SIZE_MAX 4096 /* Allows max CACHE_MASK 0x0FFF */
+#define LOOKUP_CACHE_SIZE_MAX 65536 /* Allows max CACHE_MASK 0xFFFF */
 #define LOOKUP_CACHE_SHIFT_DFL 2
 
 struct __mf_cache __mf_lookup_cache [LOOKUP_CACHE_SIZE_MAX];
@@ -136,7 +191,11 @@ pthread_mutex_t __mf_biglock =
    the libmudflap.la (no threading support) can diagnose whether
    the application is linked with -lpthread.  See __mf_usage() below.  */
 #if HAVE_PTHREAD_H
+#ifdef _POSIX_THREADS
 #pragma weak pthread_join
+#else
+#define pthread_join NULL
+#endif
 const void *threads_active_p = (void *) pthread_join;
 #endif
 
@@ -202,7 +261,7 @@ static __mf_object_t *__mf_object_cemetary[__MF_TYPE_MAX_CEM+1][__MF_PERSIST_MAX
 /* ------------------------------------------------------------------------ */
 /* Forward function declarations */
 
-static void __mf_init () CTOR;
+void __mf_init () CTOR;
 static void __mf_sigusr1_respond ();
 static unsigned __mf_find_objects (uintptr_t ptr_low, uintptr_t ptr_high, 
                                    __mf_object_t **objs, unsigned max_objs);
@@ -213,7 +272,7 @@ static unsigned __mf_find_dead_objects (uintptr_t ptr_low, uintptr_t ptr_high,
 static void __mf_adapt_cache ();
 static void __mf_describe_object (__mf_object_t *obj);
 static unsigned __mf_watch_or_not (void *ptr, size_t sz, char flag);
-static splay_tree __mf_object_tree (int type);
+static mfsplay_tree __mf_object_tree (int type);
 static void __mf_link_object (__mf_object_t *node);
 static void __mf_unlink_object (__mf_object_t *node);
 
@@ -233,6 +292,7 @@ __mf_set_default_options ()
   __mf_opts.persistent_count = 100;
   __mf_opts.crumple_zone = 32;
   __mf_opts.backtrace = 4;
+  __mf_opts.timestamps = 1;
   __mf_opts.mudflap_mode = mode_check;
   __mf_opts.violation_mode = viol_nop;
   __mf_opts.heur_std_data = 1;
@@ -250,36 +310,36 @@ static struct option
       set_option,
       read_integer_option,
     } type;
-  int value;
-  int *target;
+  unsigned value;
+  unsigned *target;
 } 
 options [] =
   {
     {"mode-nop", 
      "mudflaps do nothing", 
-     set_option, (int)mode_nop, (int *)&__mf_opts.mudflap_mode},    
+     set_option, (unsigned)mode_nop, (unsigned *)&__mf_opts.mudflap_mode},    
     {"mode-populate", 
      "mudflaps populate object tree", 
-     set_option, (int)mode_populate, (int *)&__mf_opts.mudflap_mode},    
+     set_option, (unsigned)mode_populate, (unsigned *)&__mf_opts.mudflap_mode},    
     {"mode-check", 
      "mudflaps check for memory violations",
-     set_option, (int)mode_check, (int *)&__mf_opts.mudflap_mode},
+     set_option, (unsigned)mode_check, (unsigned *)&__mf_opts.mudflap_mode},
     {"mode-violate", 
      "mudflaps always cause violations (diagnostic)",
-     set_option, (int)mode_violate, (int *)&__mf_opts.mudflap_mode},
+     set_option, (unsigned)mode_violate, (unsigned *)&__mf_opts.mudflap_mode},
    
     {"viol-nop", 
      "violations do not change program execution",
-     set_option, (int)viol_nop, (int *)&__mf_opts.violation_mode},
+     set_option, (unsigned)viol_nop, (unsigned *)&__mf_opts.violation_mode},
     {"viol-abort", 
      "violations cause a call to abort()",
-     set_option, (int)viol_abort, (int *)&__mf_opts.violation_mode},
+     set_option, (unsigned)viol_abort, (unsigned *)&__mf_opts.violation_mode},
     {"viol-segv", 
      "violations are promoted to SIGSEGV signals",
-     set_option, (int)viol_segv, (int *)&__mf_opts.violation_mode},
+     set_option, (unsigned)viol_segv, (unsigned *)&__mf_opts.violation_mode},
     {"viol-gdb", 
      "violations fork a gdb process attached to current program",
-     set_option, (int)viol_gdb, (int *)&__mf_opts.violation_mode},
+     set_option, (unsigned)viol_gdb, (unsigned *)&__mf_opts.violation_mode},
     {"trace-calls", 
      "trace calls to mudflap runtime library",
      set_option, 1, &__mf_opts.trace_mf_calls},
@@ -309,6 +369,12 @@ options [] =
     {"abbreviate", 
      "abbreviate repetitive listings",
      set_option, 1, &__mf_opts.abbreviate},
+    {"timestamps", 
+     "track object lifetime timestamps",
+     set_option, 1, &__mf_opts.timestamps},
+    {"ignore-reads", 
+     "ignore read accesses - assume okay",
+     set_option, 1, &__mf_opts.ignore_reads},
     {"wipe-stack",
      "wipe stack objects at unwind",
      set_option, 1, &__mf_opts.wipe_stack},
@@ -365,7 +431,7 @@ __mf_usage ()
 
   fprintf (stderr, 
            "This is a %s%sGCC \"mudflap\" memory-checked binary.\n"
-           "Mudflap is Copyright (C) 2002-2003 Free Software Foundation, Inc.\n"
+           "Mudflap is Copyright (C) 2002-2004 Free Software Foundation, Inc.\n"
            "\n"
            "The mudflap code can be controlled by an environment variable:\n"
            "\n"
@@ -600,22 +666,26 @@ struct __mf_dynamic_entry __mf_dynamic [] =
 /* ------------------------------------------------------------------------ */
 
 /* Lookup & manage automatic initialization of the five or so splay trees.  */
-static splay_tree
+static mfsplay_tree
 __mf_object_tree (int type)
 {
-  static splay_tree trees [__MF_TYPE_MAX+1];
+  static mfsplay_tree trees [__MF_TYPE_MAX+1];
   assert (type >= 0 && type <= __MF_TYPE_MAX);
   if (UNLIKELY (trees[type] == NULL))
-    trees[type] = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
+    trees[type] = mfsplay_tree_new ();
   return trees[type];
 }
 
 
-void
+/* not static */void
 __mf_init ()
 {
   char *ov = 0;
 
+  /* Return if initialization has already been done. */
+  if (LIKELY (__mf_starting_p == 0))
+    return;
+
   /* This initial bootstrap phase requires that __mf_starting_p = 1. */
 #ifdef PIC
   __mf_resolve_dynamics ();
@@ -658,6 +728,7 @@ __wrap_main (int argc, char* argv[])
 {
   extern char **environ;
   extern int main ();
+  extern int __real_main ();
   static int been_here = 0;
 
   if (__mf_opts.heur_std_data && ! been_here)
@@ -708,6 +779,12 @@ void __mf_fini ()
 {
   TRACE ("__mf_fini\n");
   __mfu_report ();
+
+#ifndef PIC
+/* Since we didn't populate the tree for allocations in constructors
+   before __mf_init, we cannot check destructors after __mf_fini.  */
+  __mf_opts.mudflap_mode = mode_nop;
+#endif
 }
 
 
@@ -744,8 +821,13 @@ void __mfu_check (void *ptr, size_t sz, int type, const char *location)
   switch (__mf_opts.mudflap_mode)
     {
     case mode_nop:
-      entry->low = MINPTR;
-      entry->high = MAXPTR;
+      /* It is tempting to poison the cache here similarly to
+         mode_populate.  However that eliminates a valuable
+         distinction between these two modes.  mode_nop is useful to
+         let a user count & trace every single check / registration
+         call.  mode_populate is useful to let a program run fast
+         while unchecked.
+      */
       judgement = 1;
       break;
 
@@ -835,7 +917,7 @@ void __mfu_check (void *ptr, size_t sz, int type, const char *location)
                   judgement = -1;
               }
 
-            /* We now know that the access spans one or more valid objects.  */
+            /* We now know that the access spans one or more only valid objects.  */
             if (LIKELY (judgement >= 0))
               for (i = 0; i < obj_count; i++)
                 {
@@ -849,12 +931,58 @@ void __mfu_check (void *ptr, size_t sz, int type, const char *location)
                       entry->high = obj->high;
                       judgement = 1;
                     }
+                }
 
-                  /* XXX: Access runs off left or right side of this
-                          object.  That could be okay, if there are
-                          other objects that fill in all the holes. */
+            /* This access runs off the end of one valid object.  That
+                could be okay, if other valid objects fill in all the
+                holes.  We allow this only for HEAP and GUESS type
+                objects.  Accesses to STATIC and STACK variables
+                should not be allowed to span.  */
+            if (UNLIKELY ((judgement == 0) && (obj_count > 1)))
+              {
+                unsigned uncovered = 0;
+                for (i = 0; i < obj_count; i++)
+                  {
+                    __mf_object_t *obj = all_ovr_obj[i];
+                    int j, uncovered_low_p, uncovered_high_p;
+                    uintptr_t ptr_lower, ptr_higher;
+
+                    uncovered_low_p = ptr_low < obj->low;
+                    ptr_lower = CLAMPSUB (obj->low, 1);
+                    uncovered_high_p = ptr_high > obj->high;
+                    ptr_higher = CLAMPADD (obj->high, 1);
+
+                    for (j = 0; j < obj_count; j++)
+                      {
+                        __mf_object_t *obj2 = all_ovr_obj[j];
+
+                        if (i == j) continue;
+
+                        /* Filter out objects that cannot be spanned across.  */
+                        if (obj2->type == __MF_TYPE_STACK 
+                            || obj2->type == __MF_TYPE_STATIC)
+                          continue;
+
+                          /* Consider a side "covered" if obj2 includes
+                             the next byte on that side.  */
+                          if (uncovered_low_p
+                              && (ptr_lower >= obj2->low && ptr_lower <= obj2->high))
+                            uncovered_low_p = 0;
+                          if (uncovered_high_p
+                              && (ptr_high >= obj2->low && ptr_higher <= obj2->high))
+                            uncovered_high_p = 0;
+                      }
+                    
+                    if (uncovered_low_p || uncovered_high_p)
+                      uncovered ++;
+                  }
+
+                /* Success if no overlapping objects are uncovered.  */
+                if (uncovered == 0)
+                  judgement = 1;
                 }
 
+
             if (dealloc_me != NULL)
               CALL_REAL (free, dealloc_me);
 
@@ -909,7 +1037,8 @@ __mf_insert_new_object (uintptr_t low, uintptr_t high, int type,
   new_obj->name = name;
   new_obj->alloc_pc = pc;
 #if HAVE_GETTIMEOFDAY
-  gettimeofday (& new_obj->alloc_time, NULL);
+  if (__mf_opts.timestamps)
+    gettimeofday (& new_obj->alloc_time, NULL);
 #endif
 #if LIBMUDFLAPTH
   new_obj->alloc_thread = pthread_self ();
@@ -1149,7 +1278,8 @@ __mfu_unregister (void *ptr, size_t sz, int type)
             old_obj->deallocated_p = 1;
             old_obj->dealloc_pc = (uintptr_t) __builtin_return_address (0);
 #if HAVE_GETTIMEOFDAY
-            gettimeofday (& old_obj->dealloc_time, NULL);
+            if (__mf_opts.timestamps)
+              gettimeofday (& old_obj->dealloc_time, NULL);
 #endif
 #ifdef LIBMUDFLAPTH
             old_obj->dealloc_thread = pthread_self ();
@@ -1232,7 +1362,7 @@ struct tree_stats
 
 
 static int
-__mf_adapt_cache_fn (splay_tree_node n, void *param)
+__mf_adapt_cache_fn (mfsplay_tree_node n, void *param)
 {
   __mf_object_t *obj = (__mf_object_t *) n->value;
   struct tree_stats *s = (struct tree_stats *) param;
@@ -1289,11 +1419,11 @@ __mf_adapt_cache ()
 
   memset (&s, 0, sizeof (s));
 
-  splay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP), __mf_adapt_cache_fn, (void *) & s);
-  splay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP_I), __mf_adapt_cache_fn, (void *) & s);
-  splay_tree_foreach (__mf_object_tree (__MF_TYPE_STACK), __mf_adapt_cache_fn, (void *) & s);
-  splay_tree_foreach (__mf_object_tree (__MF_TYPE_STATIC), __mf_adapt_cache_fn, (void *) & s);
-  splay_tree_foreach (__mf_object_tree (__MF_TYPE_GUESS), __mf_adapt_cache_fn, (void *) & s);
+  mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP), __mf_adapt_cache_fn, (void *) & s);
+  mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP_I), __mf_adapt_cache_fn, (void *) & s);
+  mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_STACK), __mf_adapt_cache_fn, (void *) & s);
+  mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_STATIC), __mf_adapt_cache_fn, (void *) & s);
+  mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_GUESS), __mf_adapt_cache_fn, (void *) & s);
 
   /* Maybe we're dealing with funny aging/adaptation parameters, or an
      empty tree.  Just leave the cache alone in such cases, rather
@@ -1329,7 +1459,7 @@ __mf_adapt_cache ()
       cache_utilization += 1.0;
   cache_utilization /= (1 + __mf_lc_mask);
 
-  new_mask |= 0x3ff; /* XXX: force a large cache.  */
+  new_mask |= 0xffff; /* XXX: force a large cache.  */
   new_mask &= (LOOKUP_CACHE_SIZE_MAX - 1);
 
   VERBOSE_TRACE ("adapt cache obj=%u/%u sizes=%lu/%.0f/%.0f => "
@@ -1363,11 +1493,11 @@ __mf_find_objects2 (uintptr_t ptr_low, uintptr_t ptr_high,
                     __mf_object_t **objs, unsigned max_objs, int type)
 {
   unsigned count = 0;
-  splay_tree t = __mf_object_tree (type);
-  splay_tree_key k = (splay_tree_key) ptr_low;
+  mfsplay_tree t = __mf_object_tree (type);
+  mfsplay_tree_key k = (mfsplay_tree_key) ptr_low;
   int direction;
 
-  splay_tree_node n = splay_tree_lookup (t, k);
+  mfsplay_tree_node n = mfsplay_tree_lookup (t, k);
   /* An exact match for base address implies a hit.  */
   if (n != NULL)
     {
@@ -1380,13 +1510,13 @@ __mf_find_objects2 (uintptr_t ptr_low, uintptr_t ptr_high,
   for (direction = 0; direction < 2; direction ++)
     {
       /* Reset search origin.  */
-      k = (splay_tree_key) ptr_low;
+      k = (mfsplay_tree_key) ptr_low;
 
       while (1)
         {
           __mf_object_t *obj;
               
-          n = (direction == 0 ? splay_tree_predecessor (t, k) : splay_tree_successor (t, k));
+          n = (direction == 0 ? mfsplay_tree_successor (t, k) : mfsplay_tree_predecessor (t, k));
           if (n == NULL) break;
           obj = (__mf_object_t *) n->value;
               
@@ -1397,7 +1527,7 @@ __mf_find_objects2 (uintptr_t ptr_low, uintptr_t ptr_high,
             objs[count] = (__mf_object_t *) n->value;
           count ++;
 
-          k = (splay_tree_key) obj->low;
+          k = (mfsplay_tree_key) obj->low;
         }
     }
 
@@ -1439,8 +1569,8 @@ __mf_find_objects (uintptr_t ptr_low, uintptr_t ptr_high,
 static void
 __mf_link_object (__mf_object_t *node)
 {
-  splay_tree t = __mf_object_tree (node->type);
-  splay_tree_insert (t, (splay_tree_key) node->low, (splay_tree_value) node);
+  mfsplay_tree t = __mf_object_tree (node->type);
+  mfsplay_tree_insert (t, (mfsplay_tree_key) node->low, (mfsplay_tree_value) node);
 }
 
 /* __mf_unlink_object */
@@ -1448,8 +1578,8 @@ __mf_link_object (__mf_object_t *node)
 static void
 __mf_unlink_object (__mf_object_t *node)
 {
-  splay_tree t = __mf_object_tree (node->type);
-  splay_tree_remove (t, (splay_tree_key) node->low);
+  mfsplay_tree t = __mf_object_tree (node->type);
+  mfsplay_tree_remove (t, (mfsplay_tree_key) node->low);
 }
 
 /* __mf_find_dead_objects */
@@ -1531,7 +1661,8 @@ __mf_describe_object (__mf_object_t *obj)
   if (__mf_opts.abbreviate && obj->description_epoch == epoch)
     {
       fprintf (stderr,
-               "mudflap object %p: name=`%s'\n",
+               "mudflap %sobject %p: name=`%s'\n",
+               (obj->deallocated_p ? "dead " : ""),
                (void *) obj, (obj->name ? obj->name : ""));
       return;
     }
@@ -1539,13 +1670,14 @@ __mf_describe_object (__mf_object_t *obj)
     obj->description_epoch = epoch;
 
   fprintf (stderr,
-           "mudflap object %p: name=`%s'\n"
+           "mudflap %sobject %p: name=`%s'\n"
            "bounds=[%p,%p] size=%lu area=%s check=%ur/%uw liveness=%u%s\n"
            "alloc time=%lu.%06lu pc=%p"
 #ifdef LIBMUDFLAPTH
            " thread=%u"
 #endif
            "\n",
+           (obj->deallocated_p ? "dead " : ""),
            (void *) obj, (obj->name ? obj->name : ""), 
            (void *) obj->low, (void *) obj->high,
            (unsigned long) (obj->high - obj->low + 1),
@@ -1601,7 +1733,7 @@ __mf_describe_object (__mf_object_t *obj)
 
 
 static int
-__mf_report_leaks_fn (splay_tree_node n, void *param)
+__mf_report_leaks_fn (mfsplay_tree_node n, void *param)
 {
   __mf_object_t *node = (__mf_object_t *) n->value;
   unsigned *count = (unsigned *) param;
@@ -1621,9 +1753,9 @@ __mf_report_leaks ()
 {
   unsigned count = 0;
 
-  (void) splay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP),
+  (void) mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP),
                              __mf_report_leaks_fn, & count);
-  (void) splay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP_I),
+  (void) mfsplay_tree_foreach (__mf_object_tree (__MF_TYPE_HEAP_I),
                              __mf_report_leaks_fn, & count);
 
   return count;
@@ -1835,7 +1967,7 @@ __mf_violation (void *ptr, size_t sz, uintptr_t pc,
   {
     unsigned dead_p;
     unsigned num_helpful = 0;
-    struct timeval now;
+    struct timeval now = { 0, 0 };
 #if HAVE_GETTIMEOFDAY
     gettimeofday (& now, NULL);
 #endif
@@ -2140,25 +2272,507 @@ __assert_fail (const char *msg, const char *file, unsigned line, const char *fun
 
 
 
-
-
-/* #include the generic splay tree implementation from libiberty here, to
-   ensure that it uses our memory allocation primitives.  */
+/* Adapted splay tree code, originally from libiberty.  It has been
+   specialized for libmudflap as requested by RMS.  */
 
 static void
-splay_tree_free (void *p)
+mfsplay_tree_free (void *p)
 {
   DECLARE (void, free, void *p);
   CALL_REAL (free, p);
 }
 
 static void *
-splay_tree_xmalloc (size_t s)
+mfsplay_tree_xmalloc (size_t s)
 {
   DECLARE (void *, malloc, size_t s);
   return CALL_REAL (malloc, s);
 }
 
-#define free(z) splay_tree_free(z)
-#define xmalloc(z) splay_tree_xmalloc(z)
-#include "splay-tree.c"
+
+static void mfsplay_tree_splay (mfsplay_tree, mfsplay_tree_key);
+static mfsplay_tree_node mfsplay_tree_splay_helper (mfsplay_tree,
+                                                mfsplay_tree_key,
+                                                mfsplay_tree_node *,
+                                                mfsplay_tree_node *,
+                                                mfsplay_tree_node *);
+
+
+/* Help splay SP around KEY.  PARENT and GRANDPARENT are the parent
+   and grandparent, respectively, of NODE.  */
+
+static mfsplay_tree_node
+mfsplay_tree_splay_helper (mfsplay_tree sp,
+                         mfsplay_tree_key key,
+                         mfsplay_tree_node * node,
+                         mfsplay_tree_node * parent,
+                         mfsplay_tree_node * grandparent)
+{
+  mfsplay_tree_node *next;
+  mfsplay_tree_node n;
+  int comparison;
+
+  n = *node;
+
+  if (!n)
+    return *parent;
+
+  comparison = ((key > n->key) ? 1 : ((key < n->key) ? -1 : 0));
+
+  if (comparison == 0)
+    /* We've found the target.  */
+    next = 0;
+  else if (comparison < 0)
+    /* The target is to the left.  */
+    next = &n->left;
+  else
+    /* The target is to the right.  */
+    next = &n->right;
+
+  if (next)
+    {
+      /* Check whether our recursion depth is too high.  Abort this search,
+         and signal that a rebalance is required to continue.  */
+      if (sp->depth > sp->max_depth)
+        {
+          sp->rebalance_p = 1;
+          return n;
+         }
+
+      /* Continue down the tree.  */
+      sp->depth ++;
+      n = mfsplay_tree_splay_helper (sp, key, next, node, parent);
+      sp->depth --;
+
+      /* The recursive call will change the place to which NODE
+         points.  */
+      if (*node != n || sp->rebalance_p)
+        return n;
+    }
+
+  if (!parent)
+    /* NODE is the root.  We are done.  */
+    return n;
+
+  /* First, handle the case where there is no grandparent (i.e.,
+   *PARENT is the root of the tree.)  */
+  if (!grandparent)
+    {
+      if (n == (*parent)->left)
+        {
+          *node = n->right;
+          n->right = *parent;
+        }
+      else
+        {
+          *node = n->left;
+          n->left = *parent;
+        }
+      *parent = n;
+      return n;
+    }
+
+  /* Next handle the cases where both N and *PARENT are left children,
+     or where both are right children.  */
+  if (n == (*parent)->left && *parent == (*grandparent)->left)
+    {
+      mfsplay_tree_node p = *parent;
+
+      (*grandparent)->left = p->right;
+      p->right = *grandparent;
+      p->left = n->right;
+      n->right = p;
+      *grandparent = n;
+      return n;
+    }
+  else if (n == (*parent)->right && *parent == (*grandparent)->right)
+    {
+      mfsplay_tree_node p = *parent;
+
+      (*grandparent)->right = p->left;
+      p->left = *grandparent;
+      p->right = n->left;
+      n->left = p;
+      *grandparent = n;
+      return n;
+    }
+
+  /* Finally, deal with the case where N is a left child, but *PARENT
+     is a right child, or vice versa.  */
+  if (n == (*parent)->left)
+    {
+      (*parent)->left = n->right;
+      n->right = *parent;
+      (*grandparent)->right = n->left;
+      n->left = *grandparent;
+      *grandparent = n;
+      return n;
+    }
+  else
+    {
+      (*parent)->right = n->left;
+      n->left = *parent;
+      (*grandparent)->left = n->right;
+      n->right = *grandparent;
+      *grandparent = n;
+      return n;
+    }
+}
+
+
+
+static int
+mfsplay_tree_rebalance_helper1 (mfsplay_tree_node n, void *array_ptr)
+{
+  mfsplay_tree_node **p = array_ptr;
+  *(*p) = n;
+  (*p)++;
+  return 0;
+}
+
+
+static mfsplay_tree_node
+mfsplay_tree_rebalance_helper2 (mfsplay_tree_node * array, unsigned low,
+                              unsigned high)
+{
+  unsigned middle = low + (high - low) / 2;
+  mfsplay_tree_node n = array[middle];
+
+  /* Note that since we're producing a balanced binary tree, it is not a problem
+     that this function is recursive.  */
+  if (low + 1 <= middle)
+    n->left = mfsplay_tree_rebalance_helper2 (array, low, middle - 1);
+  else
+    n->left = NULL;
+
+  if (middle + 1 <= high)
+    n->right = mfsplay_tree_rebalance_helper2 (array, middle + 1, high);
+  else
+    n->right = NULL;
+
+  return n;
+}
+
+
+/* Rebalance the entire tree.  Do this by copying all the node
+   pointers into an array, then cleverly re-linking them.  */
+static void
+mfsplay_tree_rebalance (mfsplay_tree sp)
+{
+  mfsplay_tree_node *all_nodes, *all_nodes_1;
+
+  if (sp->num_keys <= 2)
+    return;
+
+  all_nodes = mfsplay_tree_xmalloc (sizeof (mfsplay_tree_node) * sp->num_keys);
+
+  /* Traverse all nodes to copy their addresses into this array.  */
+  all_nodes_1 = all_nodes;
+  mfsplay_tree_foreach (sp, mfsplay_tree_rebalance_helper1,
+                      (void *) &all_nodes_1);
+
+  /* Relink all the nodes.  */
+  sp->root = mfsplay_tree_rebalance_helper2 (all_nodes, 0, sp->num_keys - 1);
+
+  mfsplay_tree_free (all_nodes);
+}
+
+
+/* Splay SP around KEY.  */
+static void
+mfsplay_tree_splay (mfsplay_tree sp, mfsplay_tree_key key)
+{
+  if (sp->root == 0)
+    return;
+
+  /* If we just splayed the tree with the same key, do nothing.  */
+  if (sp->last_splayed_key_p &&
+      (sp->last_splayed_key == key))
+    return;
+
+  /* Compute a maximum recursion depth for a splay tree with NUM nodes.
+     The idea is to limit excessive stack usage if we're facing
+     degenerate access patterns.  Unfortunately such patterns can occur
+     e.g. during static initialization, where many static objects might
+     be registered in increasing address sequence, or during a case where
+     large tree-like heap data structures are allocated quickly. 
+
+     On x86, this corresponds to roughly 200K of stack usage. 
+     XXX: For libmudflapth, this could be a function of __mf_opts.thread_stack.  */
+  sp->max_depth = 2500;
+  sp->rebalance_p = sp->depth = 0;
+
+  mfsplay_tree_splay_helper (sp, key, &sp->root, NULL, NULL);
+  if (sp->rebalance_p)
+    {
+      mfsplay_tree_rebalance (sp);
+
+      sp->rebalance_p = sp->depth = 0;
+      mfsplay_tree_splay_helper (sp, key, &sp->root, NULL, NULL);
+
+      if (sp->rebalance_p)
+        abort ();
+    }
+
+
+  /* Cache this splay key. */
+  sp->last_splayed_key = key;
+  sp->last_splayed_key_p = 1;
+}
+
+
+
+/* Allocate a new splay tree.  */
+static mfsplay_tree
+mfsplay_tree_new ()
+{
+  mfsplay_tree sp = mfsplay_tree_xmalloc (sizeof (struct mfsplay_tree_s));
+  sp->root = NULL;
+  sp->last_splayed_key_p = 0;
+  sp->num_keys = 0;
+
+  return sp;
+}
+
+
+
+/* Insert a new node (associating KEY with DATA) into SP.  If a
+   previous node with the indicated KEY exists, its data is replaced
+   with the new value.  Returns the new node.  */
+static mfsplay_tree_node
+mfsplay_tree_insert (mfsplay_tree sp, mfsplay_tree_key key, mfsplay_tree_value value)
+{
+  int comparison = 0;
+
+  mfsplay_tree_splay (sp, key);
+
+  if (sp->root)
+    comparison = ((sp->root->key > key) ? 1 :
+                  ((sp->root->key < key) ? -1 : 0));
+
+  if (sp->root && comparison == 0)
+    {
+      /* If the root of the tree already has the indicated KEY, just
+         replace the value with VALUE.  */
+      sp->root->value = value;
+    }
+  else
+    {
+      /* Create a new node, and insert it at the root.  */
+      mfsplay_tree_node node;
+
+      node = mfsplay_tree_xmalloc (sizeof (struct mfsplay_tree_node_s));
+      node->key = key;
+      node->value = value;
+      sp->num_keys++;
+      if (!sp->root)
+        node->left = node->right = 0;
+      else if (comparison < 0)
+        {
+          node->left = sp->root;
+          node->right = node->left->right;
+          node->left->right = 0;
+        }
+      else
+        {
+          node->right = sp->root;
+          node->left = node->right->left;
+          node->right->left = 0;
+        }
+
+      sp->root = node;
+      sp->last_splayed_key_p = 0;
+    }
+
+  return sp->root;
+}
+
+/* Remove KEY from SP.  It is not an error if it did not exist.  */
+
+static void
+mfsplay_tree_remove (mfsplay_tree sp, mfsplay_tree_key key)
+{
+  mfsplay_tree_splay (sp, key);
+  sp->last_splayed_key_p = 0;
+  if (sp->root && (sp->root->key == key))
+    {
+      mfsplay_tree_node left, right;
+      left = sp->root->left;
+      right = sp->root->right;
+      /* Delete the root node itself.  */
+      mfsplay_tree_free (sp->root);
+      sp->num_keys--;
+      /* One of the children is now the root.  Doesn't matter much
+         which, so long as we preserve the properties of the tree.  */
+      if (left)
+        {
+          sp->root = left;
+          /* If there was a right child as well, hang it off the 
+             right-most leaf of the left child.  */
+          if (right)
+            {
+              while (left->right)
+                left = left->right;
+              left->right = right;
+            }
+        }
+      else
+        sp->root = right;
+    }
+}
+
+/* Lookup KEY in SP, returning VALUE if present, and NULL 
+   otherwise.  */
+
+static mfsplay_tree_node
+mfsplay_tree_lookup (mfsplay_tree sp, mfsplay_tree_key key)
+{
+  mfsplay_tree_splay (sp, key);
+  if (sp->root && (sp->root->key == key))
+    return sp->root;
+  else
+    return 0;
+}
+
+
+/* Return the immediate predecessor KEY, or NULL if there is no
+   predecessor.  KEY need not be present in the tree.  */
+
+static mfsplay_tree_node
+mfsplay_tree_predecessor (mfsplay_tree sp, mfsplay_tree_key key)
+{
+  int comparison;
+  mfsplay_tree_node node;
+  /* If the tree is empty, there is certainly no predecessor.  */
+  if (!sp->root)
+    return NULL;
+  /* Splay the tree around KEY.  That will leave either the KEY
+     itself, its predecessor, or its successor at the root.  */
+  mfsplay_tree_splay (sp, key);
+  comparison = ((sp->root->key > key) ? 1 :
+                ((sp->root->key < key) ? -1 : 0));
+
+  /* If the predecessor is at the root, just return it.  */
+  if (comparison < 0)
+    return sp->root;
+  /* Otherwise, find the rightmost element of the left subtree.  */
+  node = sp->root->left;
+  if (node)
+    while (node->right)
+      node = node->right;
+  return node;
+}
+
+/* Return the immediate successor KEY, or NULL if there is no
+   successor.  KEY need not be present in the tree.  */
+
+static mfsplay_tree_node
+mfsplay_tree_successor (mfsplay_tree sp, mfsplay_tree_key key)
+{
+  int comparison;
+  mfsplay_tree_node node;
+  /* If the tree is empty, there is certainly no successor.  */
+  if (!sp->root)
+    return NULL;
+  /* Splay the tree around KEY.  That will leave either the KEY
+     itself, its predecessor, or its successor at the root.  */
+  mfsplay_tree_splay (sp, key);
+  comparison = ((sp->root->key > key) ? 1 :
+                ((sp->root->key < key) ? -1 : 0));
+  /* If the successor is at the root, just return it.  */
+  if (comparison > 0)
+    return sp->root;
+  /* Otherwise, find the leftmost element of the right subtree.  */
+  node = sp->root->right;
+  if (node)
+    while (node->left)
+      node = node->left;
+  return node;
+}
+
+/* Call FN, passing it the DATA, for every node in SP, following an
+   in-order traversal.  If FN every returns a non-zero value, the
+   iteration ceases immediately, and the value is returned.
+   Otherwise, this function returns 0.
+   
+   This function simulates recursion using dynamically allocated
+   arrays, since it may be called from mfsplay_tree_rebalance(), which
+   in turn means that the tree is already uncomfortably deep for stack
+   space limits.  */
+static int
+mfsplay_tree_foreach (mfsplay_tree st, mfsplay_tree_foreach_fn fn, void *data)
+{
+  mfsplay_tree_node *stack1;
+  char *stack2;
+  unsigned sp;
+  int val = 0;
+  enum s { s_left, s_here, s_right, s_up };
+
+  if (st->root == NULL) /* => num_keys == 0 */
+    return 0;
+
+  stack1 = mfsplay_tree_xmalloc (sizeof (mfsplay_tree_node) * st->num_keys);
+  stack2 = mfsplay_tree_xmalloc (sizeof (char) * st->num_keys);
+
+  sp = 0;
+  stack1 [sp] = st->root;
+  stack2 [sp] = s_left;
+
+  while (1)
+    {
+      mfsplay_tree_node n;
+      enum s s;
+
+      n = stack1 [sp];
+      s = stack2 [sp];
+
+      /* Handle each of the four possible states separately.  */
+
+      /* 1: We're here to traverse the left subtree (if any).  */
+      if (s == s_left)
+        {
+          stack2 [sp] = s_here;
+          if (n->left != NULL)
+            {
+              sp ++;
+              stack1 [sp] = n->left;
+              stack2 [sp] = s_left;
+            }
+        }
+
+      /* 2: We're here to traverse this node.  */
+      else if (s == s_here)
+        {
+          stack2 [sp] = s_right;
+          val = (*fn) (n, data);
+          if (val) break;
+        }
+
+      /* 3: We're here to traverse the right subtree (if any).  */
+      else if (s == s_right)
+        {
+          stack2 [sp] = s_up;
+          if (n->right != NULL)
+            {
+              sp ++;
+              stack1 [sp] = n->right;
+              stack2 [sp] = s_left;
+            }
+        }
+
+      /* 4: We're here after both subtrees (if any) have been traversed.  */
+      else if (s == s_up)
+        {
+          /* Pop the stack.  */
+          if (sp == 0) break; /* Popping off the root note: we're finished!  */
+          sp --;
+        }
+
+      else
+        abort ();
+    }
+
+  mfsplay_tree_free (stack1);
+  mfsplay_tree_free (stack2);
+  return val;
+}