Move the WordFM (AVL trees of UWord-pairs) implementation from
helgrind/ into the core.  It's just too darn useful to not be in the
core.  There is some overlap in functionality between OSet and WordFM,
but OSet is more space efficient in some circumstances, whereas WordFM
is easier to use and a bit more flexible in some cases.

Also in this new module (m_wordfm) is a type WordBag, build on top of
WordFM.  This provides Multiset of UWords functionality.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8524 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c
index 4b9be47..54d76cd 100644
--- a/helgrind/hg_main.c
+++ b/helgrind/hg_main.c
@@ -49,11 +49,11 @@
 #include "pub_tool_xarray.h"
 #include "pub_tool_stacktrace.h"
 #include "pub_tool_debuginfo.h"  /* VG_(get_data_description) */
+#include "pub_tool_wordfm.h"
 
 #include "helgrind.h"
 
 #define HG_(str) VGAPPEND(vgHelgrind_,str)
-#include "hg_wordfm.h"
 #include "hg_wordset.h"
 
 /*----------------------------------------------------------------*/
@@ -630,12 +630,12 @@
 {
    Thread* thr;
    Word    count;
-   HG_(initIterBag)( bag );
-   while (HG_(nextIterBag)( bag, (Word*)&thr, &count )) {
+   VG_(initIterBag)( bag );
+   while (VG_(nextIterBag)( bag, (Word*)&thr, &count )) {
       if (count < 1) return False;
       if (!is_sane_Thread(thr)) return False;
    }
-   HG_(doneIterBag)( bag );
+   VG_(doneIterBag)( bag );
    return True;
 }
 static Bool is_sane_Lock_BASE ( Lock* lock )
@@ -657,7 +657,7 @@
 
    /* If heldBy is non-NULL, we require it to contain at least one
       thread. */
-   if (HG_(isEmptyBag)(lock->heldBy))
+   if (VG_(isEmptyBag)(lock->heldBy))
       return False;
 
    /* Lock is either r- or w-held. */
@@ -666,7 +666,7 @@
    if (lock->heldW) {
       /* Held in write-mode */
       if ((lock->kind == LK_nonRec || lock->kind == LK_rdwr)
-          && !HG_(isSingletonTotalBag)(lock->heldBy))
+          && !VG_(isSingletonTotalBag)(lock->heldBy))
          return False;
    } else {
       /* Held in read-mode */
@@ -694,7 +694,7 @@
 {
    tl_assert(is_sane_LockN(lk));
    if (lk->heldBy)
-      HG_(deleteBag)( lk->heldBy );
+      VG_(deleteBag)( lk->heldBy );
    VG_(memset)(lk, 0xAA, sizeof(*lk));
    hg_free(lk);
 }
@@ -729,8 +729,8 @@
          tl_assert(lk->heldBy == NULL); /* can't w-lock recursively */
          tl_assert(!lk->heldW);
          lk->heldW  = True;
-         lk->heldBy = HG_(newBag)( hg_zalloc, hg_free );
-         HG_(addToBag)( lk->heldBy, (Word)thr );
+         lk->heldBy = VG_(newBag)( hg_zalloc, hg_free );
+         VG_(addToBag)( lk->heldBy, (Word)thr );
          break;
       case LK_mbRec:
          if (lk->heldBy == NULL)
@@ -738,11 +738,11 @@
          /* 2nd and subsequent locking of a lock by its owner */
          tl_assert(lk->heldW);
          /* assert: lk is only held by one thread .. */
-         tl_assert(HG_(sizeUniqueBag(lk->heldBy)) == 1);
+         tl_assert(VG_(sizeUniqueBag(lk->heldBy)) == 1);
          /* assert: .. and that thread is 'thr'. */
-         tl_assert(HG_(elemBag)(lk->heldBy, (Word)thr)
-                   == HG_(sizeTotalBag)(lk->heldBy));
-         HG_(addToBag)(lk->heldBy, (Word)thr);
+         tl_assert(VG_(elemBag)(lk->heldBy, (Word)thr)
+                   == VG_(sizeTotalBag)(lk->heldBy));
+         VG_(addToBag)(lk->heldBy, (Word)thr);
          break;
       case LK_rdwr:
          tl_assert(lk->heldBy == NULL && !lk->heldW); /* must be unheld */
@@ -780,11 +780,11 @@
    /* end EXPOSITION only */
 
    if (lk->heldBy) {
-      HG_(addToBag)(lk->heldBy, (Word)thr);
+      VG_(addToBag)(lk->heldBy, (Word)thr);
    } else {
       lk->heldW  = False;
-      lk->heldBy = HG_(newBag)( hg_zalloc, hg_free );
-      HG_(addToBag)( lk->heldBy, (Word)thr );
+      lk->heldBy = VG_(newBag)( hg_zalloc, hg_free );
+      VG_(addToBag)( lk->heldBy, (Word)thr );
    }
    tl_assert(!lk->heldW);
    tl_assert(is_sane_LockN(lk));
@@ -803,13 +803,13 @@
    tl_assert(lk->heldBy);
    stats__lockN_releases++;
    /* Remove it from the holder set */
-   b = HG_(delFromBag)(lk->heldBy, (Word)thr);
+   b = VG_(delFromBag)(lk->heldBy, (Word)thr);
    /* thr must actually have been a holder of lk */
    tl_assert(b);
    /* normalise */
    tl_assert(lk->acquired_at);
-   if (HG_(isEmptyBag)(lk->heldBy)) {
-      HG_(deleteBag)(lk->heldBy);
+   if (VG_(isEmptyBag)(lk->heldBy)) {
+      VG_(deleteBag)(lk->heldBy);
       lk->heldBy      = NULL;
       lk->heldW       = False;
       lk->acquired_at = NULL;
@@ -825,8 +825,8 @@
       return;
    }
    /* for each thread that holds this lock do ... */
-   HG_(initIterBag)( lk->heldBy );
-   while (HG_(nextIterBag)( lk->heldBy, (Word*)&thr, NULL )) {
+   VG_(initIterBag)( lk->heldBy );
+   while (VG_(nextIterBag)( lk->heldBy, (Word*)&thr, NULL )) {
       tl_assert(is_sane_Thread(thr));
       tl_assert(HG_(elemWS)( univ_lsets,
                              thr->locksetA, (Word)lk ));
@@ -840,7 +840,7 @@
             = HG_(delFromWS)( univ_lsets, thr->locksetW, (Word)lk );
       }
    }
-   HG_(doneIterBag)( lk->heldBy );
+   VG_(doneIterBag)( lk->heldBy );
 }
 
 /* --------- xxxID functions --------- */
@@ -1107,10 +1107,10 @@
       Thread* thr;
       Word    count;
       VG_(printf)(" { ");
-      HG_(initIterBag)( lk->heldBy );
-      while (HG_(nextIterBag)( lk->heldBy, (Word*)&thr, &count ))
+      VG_(initIterBag)( lk->heldBy );
+      while (VG_(nextIterBag)( lk->heldBy, (Word*)&thr, &count ))
          VG_(printf)("%lu:%p ", count, thr);
-      HG_(doneIterBag)( lk->heldBy );
+      VG_(doneIterBag)( lk->heldBy );
       VG_(printf)("}");
    }
    VG_(printf)("\n");
@@ -1140,14 +1140,14 @@
    void* gla;
    Lock* lk;
    space(d); VG_(printf)("map_locks (%d entries) {\n",
-                         (Int)HG_(sizeFM)( map_locks ));
-   HG_(initIterFM)( map_locks );
-   while (HG_(nextIterFM)( map_locks, (Word*)&gla,
+                         (Int)VG_(sizeFM)( map_locks ));
+   VG_(initIterFM)( map_locks );
+   while (VG_(nextIterFM)( map_locks, (Word*)&gla,
                                       (Word*)&lk )) {
       space(d+3);
       VG_(printf)("guest %p -> Lock %p\n", gla, lk);
    }
-   HG_(doneIterFM)( map_locks );
+   VG_(doneIterFM)( map_locks );
    space(d); VG_(printf)("}\n");
 }
 
@@ -1188,14 +1188,14 @@
    SegmentID segid;
    Segment*  seg;
    space(d); VG_(printf)("map_segments (%d entries) {\n", 
-                         (Int)HG_(sizeFM)( map_segments ));
-   HG_(initIterFM)( map_segments );
-   while (HG_(nextIterFM)( map_segments, (Word*)&segid,
+                         (Int)VG_(sizeFM)( map_segments ));
+   VG_(initIterFM)( map_segments );
+   while (VG_(nextIterFM)( map_segments, (Word*)&segid,
                                          (Word*)&seg )) {
       space(d+3);
       VG_(printf)("segid 0x%x -> Segment %p\n", (UInt)segid, seg);
    }
-   HG_(doneIterFM)( map_segments );
+   VG_(doneIterFM)( map_segments );
    space(d); VG_(printf)("}\n");
 }
 
@@ -1309,12 +1309,12 @@
    Addr    ga;
    SecMap* sm;
    space(d); VG_(printf)("map_shmem_ShR_and_ShM_only {\n");
-   HG_(initIterFM)( map_shmem );
-   while (HG_(nextIterFM)( map_shmem, (Word*)&ga,
+   VG_(initIterFM)( map_shmem );
+   while (VG_(nextIterFM)( map_shmem, (Word*)&ga,
                                       (Word*)&sm )) {
       pp_SecMap_shared( d+3, sm, ga );
    }
-   HG_(doneIterFM) ( map_shmem );
+   VG_(doneIterFM) ( map_shmem );
    space(d); VG_(printf)("}\n");
 }
 
@@ -1380,7 +1380,7 @@
 
    tl_assert(sizeof(Addr) == sizeof(Word));
    tl_assert(map_shmem == NULL);
-   map_shmem = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
+   map_shmem = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
    tl_assert(map_shmem != NULL);
    shmem__invalidate_scache();
 
@@ -1392,18 +1392,18 @@
    tl_assert(sizeof(SegmentID) <= sizeof(Word));
    tl_assert(sizeof(Segment*) == sizeof(Word));
    tl_assert(map_segments == NULL);
-   map_segments = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
+   map_segments = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
    tl_assert(map_segments != NULL);
    hbefore__invalidate_cache();
 
    tl_assert(sizeof(Addr) == sizeof(Word));
    tl_assert(map_locks == NULL);
-   map_locks = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
+   map_locks = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxed Word cmp*/);
    tl_assert(map_locks != NULL);
 
    __bus_lock_Lock = mk_LockN( LK_nonRec, (Addr)&__bus_lock );
    tl_assert(is_sane_LockN(__bus_lock_Lock));
-   HG_(addToFM)( map_locks, (Word)&__bus_lock, (Word)__bus_lock_Lock );
+   VG_(addToFM)( map_locks, (Word)&__bus_lock, (Word)__bus_lock_Lock );
 
    tl_assert(univ_tsets == NULL);
    univ_tsets = HG_(newWordSetU)( hg_zalloc, hg_free, 8/*cacheSize*/ );
@@ -1522,13 +1522,13 @@
    Bool  found;
    Lock* oldlock = NULL;
    tl_assert(is_sane_ThreadId(tid));
-   found = HG_(lookupFM)( map_locks, 
+   found = VG_(lookupFM)( map_locks, 
                           NULL, (Word*)&oldlock, (Word)ga );
    if (!found) {
       Lock* lock = mk_LockN(lkk, ga);
       lock->appeared_at = VG_(record_ExeContext)( tid, 0 );
       tl_assert(is_sane_LockN(lock));
-      HG_(addToFM)( map_locks, (Word)ga, (Word)lock );
+      VG_(addToFM)( map_locks, (Word)ga, (Word)lock );
       tl_assert(oldlock == NULL);
       // mark the relevant secondary map has .mbHasLocks
       shmem__set_mbHasLocks( ga, True );
@@ -1547,7 +1547,7 @@
 {
    Bool  found;
    Lock* lk = NULL;
-   found = HG_(lookupFM)( map_locks, NULL, (Word*)&lk, (Word)ga );
+   found = VG_(lookupFM)( map_locks, NULL, (Word*)&lk, (Word)ga );
    tl_assert(found  ?  lk != NULL  :  lk == NULL);
    if (found) {
       // check the relevant secondary map has .mbHasLocks?
@@ -1560,7 +1560,7 @@
 {
    Addr  ga2 = 0;
    Lock* lk  = NULL;
-   HG_(delFromFM)( map_locks,
+   VG_(delFromFM)( map_locks,
                    (Word*)&ga2, (Word*)&lk, (Word)ga );
    /* delFromFM produces the val which is being deleted, if it is
       found.  So assert it is non-null; that in effect asserts that we
@@ -1584,7 +1584,7 @@
    Bool     found;
    Segment* seg = NULL;
    tl_assert( is_sane_SegmentID(segid) );
-   found = HG_(lookupFM)( map_segments,
+   found = VG_(lookupFM)( map_segments,
                           NULL, (Word*)&seg, (Word)segid );
    tl_assert(found);
    tl_assert(seg != NULL);
@@ -1596,7 +1596,7 @@
    Bool     found;
    Segment* seg = NULL;
    tl_assert( is_sane_SegmentID(segid) );
-   found = HG_(lookupFM)( map_segments,
+   found = VG_(lookupFM)( map_segments,
                           NULL, (Word*)&seg, (Word)segid );
    if (!found) tl_assert(seg == NULL);
    return seg;
@@ -1605,8 +1605,8 @@
 static void map_segments_add ( SegmentID segid, Segment* seg )
 {
    /* This is a bit inefficient.  Oh well. */
-   tl_assert( !HG_(lookupFM)( map_segments, NULL, NULL, segid ));
-   HG_(addToFM)( map_segments, (Word)segid, (Word)seg );
+   tl_assert( !VG_(lookupFM)( map_segments, NULL, NULL, segid ));
+   VG_(addToFM)( map_segments, (Word)segid, (Word)seg );
 }
 
 /*--------------- to do with Vector Timestamps ---------------*/
@@ -2346,7 +2346,7 @@
 {
    SecMap* sm    = NULL;
    Addr    gaKey = shmem__round_to_SecMap_base(ga);
-   if (HG_(lookupFM)( map_shmem,
+   if (VG_(lookupFM)( map_shmem,
                       NULL/*keyP*/, (Word*)&sm, (Word)gaKey )) {
       /* Found; address of SecMap is in sm */
       tl_assert(sm);
@@ -2354,7 +2354,7 @@
       /* create a new one */
       sm = shmem__alloc_SecMap();
       tl_assert(sm);
-      HG_(addToFM)( map_shmem, (Word)gaKey, (Word)sm );
+      VG_(addToFM)( map_shmem, (Word)gaKey, (Word)sm );
    }
    return sm;
 }
@@ -2368,7 +2368,7 @@
 {
    SecMap* sm;
    Addr aKey = shmem__round_to_SecMap_base(a);
-   if (HG_(lookupFM)( map_shmem,
+   if (VG_(lookupFM)( map_shmem,
                       NULL/*keyP*/, (Word*)&sm, (Word)aKey )) {
       /* Found */
       return sm->mbHasLocks;
@@ -2382,14 +2382,14 @@
    SecMap* sm;
    Addr aKey = shmem__round_to_SecMap_base(a);
    tl_assert(b == False || b == True);
-   if (HG_(lookupFM)( map_shmem,
+   if (VG_(lookupFM)( map_shmem,
                       NULL/*keyP*/, (Word*)&sm, (Word)aKey )) {
       /* Found; address of SecMap is in sm */
    } else {
       /* create a new one */
       sm = shmem__alloc_SecMap();
       tl_assert(sm);
-      HG_(addToFM)( map_shmem, (Word)aKey, (Word)sm );
+      VG_(addToFM)( map_shmem, (Word)aKey, (Word)sm );
    }
    sm->mbHasLocks = b;
 }
@@ -2399,14 +2399,14 @@
    SecMap* sm;
    Addr aKey = shmem__round_to_SecMap_base(a);
    tl_assert(b == False || b == True);
-   if (HG_(lookupFM)( map_shmem,
+   if (VG_(lookupFM)( map_shmem,
                       NULL/*keyP*/, (Word*)&sm, (Word)aKey )) {
       /* Found; address of SecMap is in sm */
    } else {
       /* create a new one */
       sm = shmem__alloc_SecMap();
       tl_assert(sm);
-      HG_(addToFM)( map_shmem, (Word)aKey, (Word)sm );
+      VG_(addToFM)( map_shmem, (Word)aKey, (Word)sm );
    }
    sm->mbHasShared = b;
 }
@@ -2496,7 +2496,7 @@
 static Bool thread_is_a_holder_of_Lock ( Thread* thr, Lock* lk )
 {
    if (lk->heldBy)
-      return HG_(elemBag)( lk->heldBy, (Word)thr ) > 0;
+      return VG_(elemBag)( lk->heldBy, (Word)thr ) > 0;
    else
       return False;
 }
@@ -2555,15 +2555,15 @@
    // # entries in admin_locks == # entries in map_locks
    for (i = 0, lk = admin_locks;  lk;  i++, lk = lk->admin)
       ;
-   if (i != HG_(sizeFM)(map_locks)) BAD("1");
+   if (i != VG_(sizeFM)(map_locks)) BAD("1");
    // for each entry (gla, lk) in map_locks
    //      gla == lk->guest_addr
-   HG_(initIterFM)( map_locks );
-   while (HG_(nextIterFM)( map_locks,
+   VG_(initIterFM)( map_locks );
+   while (VG_(nextIterFM)( map_locks,
                            (Word*)&gla, (Word*)&lk )) {
       if (lk->guestaddr != gla) BAD("2");
    }
-   HG_(doneIterFM)( map_locks );
+   VG_(doneIterFM)( map_locks );
    // scan through admin_locks ...
    for (lk = admin_locks; lk; lk = lk->admin) {
       // lock is sane.  Quite comprehensive, also checks that
@@ -2581,8 +2581,8 @@
       if (lk->heldBy) {
          Thread* thr;
          Word    count;
-         HG_(initIterBag)( lk->heldBy );
-         while (HG_(nextIterBag)( lk->heldBy, 
+         VG_(initIterBag)( lk->heldBy );
+         while (VG_(nextIterBag)( lk->heldBy, 
                                   (Word*)&thr, &count )) {
             // is_sane_LockN above ensures these
             tl_assert(count >= 1);
@@ -2597,7 +2597,7 @@
                 && HG_(elemWS)(univ_lsets, thr->locksetW, (Word)lk)) 
                BAD("8");
          }
-         HG_(doneIterBag)( lk->heldBy );
+         VG_(doneIterBag)( lk->heldBy );
       } else {
          /* lock not held by anybody */
          if (lk->heldW) BAD("9"); /* should be False if !heldBy */
@@ -2631,7 +2631,7 @@
    // # entries in admin_segments == # entries in map_segments
    for (i = 0, seg = admin_segments;  seg;  i++, seg = seg->admin)
       ;
-   if (i != HG_(sizeFM)(map_segments)) BAD("1");
+   if (i != VG_(sizeFM)(map_segments)) BAD("1");
    // for seg in Segments {
    for (seg = admin_segments; seg; seg = seg->admin) {
       if (!is_sane_Segment(seg)) BAD("2");
@@ -2671,9 +2671,9 @@
    Word    i, j, ws_size, n_valid_tags;
    UWord*  ws_words;
    Addr*   valid_tags;
-   HG_(initIterFM)( map_shmem );
+   VG_(initIterFM)( map_shmem );
    // for sm in SecMaps {
-   while (HG_(nextIterFM)( map_shmem,
+   while (VG_(nextIterFM)( map_shmem,
                            (Word*)&smga, (Word*)&sm )) {
       SecMapIter itr;
       SVal*      w32p = NULL;
@@ -2737,7 +2737,7 @@
       // GCing of SecMaps is implemented
       //if (allNoAccess && sm->mbHasLocks) BAD("13a");
    }
-   HG_(doneIterFM)( map_shmem );
+   VG_(doneIterFM)( map_shmem );
 
    // check the cache
    valid_tags   = hg_zalloc(N_WAY_NENT * sizeof(Addr));
@@ -2929,8 +2929,8 @@
                       HG_(cardinalityWS)( univ_lsets, lset_old), lk );
    if (lk->appeared_at) {
       if (ga_to_lastlock == NULL)
-         ga_to_lastlock = HG_(newFM)( hg_zalloc, hg_free, NULL );
-      HG_(addToFM)( ga_to_lastlock, ga_of_access, (Word)lk->appeared_at );
+         ga_to_lastlock = VG_(newFM)( hg_zalloc, hg_free, NULL );
+      VG_(addToFM)( ga_to_lastlock, ga_of_access, (Word)lk->appeared_at );
       stats__ga_LL_adds++;
    }
 }
@@ -2944,7 +2944,7 @@
 {
    ExeContext* ec_hint = NULL;
    if (ga_to_lastlock != NULL 
-       && HG_(lookupFM)(ga_to_lastlock, 
+       && VG_(lookupFM)(ga_to_lastlock, 
                         NULL, (Word*)&ec_hint, ga)) {
       tl_assert(ec_hint != NULL);
       return ec_hint;
@@ -4941,8 +4941,8 @@
    locksToDelete = HG_(emptyWS)( univ_lsets );
    
    /* Iterate over all locks in the range firstA .. lastA inclusive. */
-   HG_(initIterAtFM)( map_locks, firstA );
-   while (HG_(nextIterFM)( map_locks, (Word*)&gla, (Word*)&lk )
+   VG_(initIterAtFM)( map_locks, firstA );
+   while (VG_(nextIterFM)( map_locks, (Word*)&gla, (Word*)&lk )
           && gla <= lastA) {
       tl_assert(is_sane_LockN(lk));
       tl_assert(gla >= firstA);
@@ -4956,15 +4956,15 @@
          them all, but that's too much hassle.  So choose one
          arbitrarily. */
       if (lk->heldBy) {
-         tl_assert(!HG_(isEmptyBag)(lk->heldBy));
-         record_error_FreeMemLock( (Thread*)HG_(anyElementOfBag)(lk->heldBy),
+         tl_assert(!VG_(isEmptyBag)(lk->heldBy));
+         record_error_FreeMemLock( (Thread*)VG_(anyElementOfBag)(lk->heldBy),
                                    lk );
          /* remove lock from locksets of all owning threads */
          remove_Lock_from_locksets_of_all_owning_Threads( lk );
          /* Leave lk->heldBy in place; del_Lock below will free it up. */
       }
    }
-   HG_(doneIterFM)( map_locks );
+   VG_(doneIterFM)( map_locks );
 
    /* --- Step 5 --- */
 
@@ -4991,8 +4991,8 @@
      SecMapIter itr;
      SVal*      w32p = NULL;
 
-     HG_(initIterFM)( map_shmem );
-     while (HG_(nextIterFM)( map_shmem,
+     VG_(initIterFM)( map_shmem );
+     while (VG_(nextIterFM)( map_shmem,
                              (Word*)&ga, (Word*)&sm )) {
         tl_assert(sm);
         stats_SMs++;
@@ -5019,7 +5019,7 @@
               *w32p = wnew;
         }
      }
-     HG_(doneIterFM)( map_shmem );
+     VG_(doneIterFM)( map_shmem );
      if (SHOW_EXPENSIVE_STUFF)
         VG_(printf)("shadow_mem_make_NoAccess: %d SMs, %d scanned\n", 
                     stats_SMs, stats_SMs_scanned);
@@ -5144,9 +5144,9 @@
 
    /* So the lock is held in w-mode.  If it's held by some other
       thread, then libpthread must be buggy. */
-   tl_assert(HG_(sizeUniqueBag)(lk->heldBy) == 1); /* from precondition */
+   tl_assert(VG_(sizeUniqueBag)(lk->heldBy) == 1); /* from precondition */
 
-   if (thr != (Thread*)HG_(anyElementOfBag)(lk->heldBy)) {
+   if (thr != (Thread*)VG_(anyElementOfBag)(lk->heldBy)) {
       record_error_Misc( thr, "Bug in libpthread: write lock "
                               "granted on mutex/rwlock which is currently "
                               "wr-held by a different thread");
@@ -5309,14 +5309,14 @@
 
    /* The lock is held.  Is this thread one of the holders?  If not,
       report a bug in the client. */
-   n = HG_(elemBag)( lock->heldBy, (Word)thr );
+   n = VG_(elemBag)( lock->heldBy, (Word)thr );
    tl_assert(n >= 0);
    if (n == 0) {
       /* We are not a current holder of the lock.  This is a bug in
          the guest, and (per POSIX pthread rules) the unlock
          attempt will fail.  So just complain and do nothing
          else. */
-      Thread* realOwner = (Thread*)HG_(anyElementOfBag)( lock->heldBy );
+      Thread* realOwner = (Thread*)VG_(anyElementOfBag)( lock->heldBy );
       tl_assert(is_sane_Thread(realOwner));
       tl_assert(realOwner != thr);
       tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lock ));
@@ -5335,7 +5335,7 @@
 
    if (n > 0) {
       tl_assert(lock->heldBy);
-      tl_assert(n == HG_(elemBag)( lock->heldBy, (Word)thr )); 
+      tl_assert(n == VG_(elemBag)( lock->heldBy, (Word)thr )); 
       /* We still hold the lock.  So either it's a recursive lock 
          or a rwlock which is currently r-held. */
       tl_assert(lock->kind == LK_mbRec
@@ -5348,7 +5348,7 @@
    } else {
       /* We no longer hold the lock. */
       if (lock->heldBy) {
-         tl_assert(0 == HG_(elemBag)( lock->heldBy, (Word)thr ));
+         tl_assert(0 == VG_(elemBag)( lock->heldBy, (Word)thr ));
       }
       /* update this thread's lockset accordingly. */
       thr->locksetA
@@ -5665,8 +5665,8 @@
    shmem__flush_and_invalidate_scache();
 
    stats_SMs = stats_SMs_scanned = stats_reExcls = 0;
-   HG_(initIterFM)( map_shmem );
-   while (HG_(nextIterFM)( map_shmem,
+   VG_(initIterFM)( map_shmem );
+   while (VG_(nextIterFM)( map_shmem,
                            (Word*)&ga, (Word*)&sm )) {
       SecMapIter itr;
       SVal*      w32p = NULL;
@@ -5723,7 +5723,7 @@
          *w32p = wnew;
       }
    }
-   HG_(doneIterFM)( map_shmem );
+   VG_(doneIterFM)( map_shmem );
 
    if (SHOW_EXPENSIVE_STUFF)
       VG_(printf)("evh__post_thread_join: %d SMs, "
@@ -5912,7 +5912,7 @@
          record_error_Misc( thr, "pthread_mutex_destroy of a locked mutex" );
          /* remove lock from locksets of all owning threads */
          remove_Lock_from_locksets_of_all_owning_Threads( lk );
-         HG_(deleteBag)( lk->heldBy );
+         VG_(deleteBag)( lk->heldBy );
          lk->heldBy = NULL;
          lk->heldW = False;
          lk->acquired_at = NULL;
@@ -5952,7 +5952,7 @@
         && (lk->kind == LK_nonRec || lk->kind == LK_rdwr)
         && lk->heldBy
         && lk->heldW
-        && HG_(elemBag)( lk->heldBy, (Word)thr ) > 0 ) {
+        && VG_(elemBag)( lk->heldBy, (Word)thr ) > 0 ) {
       /* uh, it's a non-recursive lock and we already w-hold it, and
          this is a real lock operation (not a speculative "tryLock"
          kind of thing).  Duh.  Deadlock coming up; but at least
@@ -6021,7 +6021,7 @@
 
 static void map_cond_to_Segment_INIT ( void ) {
    if (UNLIKELY(map_cond_to_Segment == NULL)) {
-      map_cond_to_Segment = HG_(newFM)( hg_zalloc, hg_free, NULL );
+      map_cond_to_Segment = VG_(newFM)( hg_zalloc, hg_free, NULL );
       tl_assert(map_cond_to_Segment != NULL);
    }
 }
@@ -6063,7 +6063,7 @@
       new_seg->vts = tick_VTS( new_seg->thr, new_seg->prev->vts );
 
       /* ... and add the binding. */
-      HG_(addToFM)( map_cond_to_Segment, (Word)cond,
+      VG_(addToFM)( map_cond_to_Segment, (Word)cond,
                                          (Word)(new_seg->prev) );
    }
 }
@@ -6110,7 +6110,7 @@
             thr, "pthread_cond_{timed}wait called with un-held mutex");
       } else
       if (lk->heldBy != NULL
-          && HG_(elemBag)( lk->heldBy, (Word)thr ) == 0) {
+          && VG_(elemBag)( lk->heldBy, (Word)thr ) == 0) {
          lk_valid = False;
          record_error_Misc( 
             thr, "pthread_cond_{timed}wait called with mutex "
@@ -6162,7 +6162,7 @@
       /* and find out which thread signalled us; then add a dependency
          edge back to it. */
       signalling_seg = NULL;
-      found = HG_(lookupFM)( map_cond_to_Segment, 
+      found = VG_(lookupFM)( map_cond_to_Segment, 
                              NULL, (Word*)&signalling_seg,
                                    (Word)cond );
       if (found) {
@@ -6233,7 +6233,7 @@
          record_error_Misc( thr, "pthread_rwlock_destroy of a locked mutex" );
          /* remove lock from locksets of all owning threads */
          remove_Lock_from_locksets_of_all_owning_Threads( lk );
-         HG_(deleteBag)( lk->heldBy );
+         VG_(deleteBag)( lk->heldBy );
          lk->heldBy = NULL;
          lk->heldW = False;
          lk->acquired_at = NULL;
@@ -6362,7 +6362,7 @@
 
 static void map_sem_to_Segment_stack_INIT ( void ) {
    if (map_sem_to_Segment_stack == NULL) {
-      map_sem_to_Segment_stack = HG_(newFM)( hg_zalloc, hg_free, NULL );
+      map_sem_to_Segment_stack = VG_(newFM)( hg_zalloc, hg_free, NULL );
       tl_assert(map_sem_to_Segment_stack != NULL);
    }
 }
@@ -6371,14 +6371,14 @@
    XArray* xa;
    tl_assert(seg);
    map_sem_to_Segment_stack_INIT();
-   if (HG_(lookupFM)( map_sem_to_Segment_stack, 
+   if (VG_(lookupFM)( map_sem_to_Segment_stack, 
                       NULL, (Word*)&xa, (Word)sem )) {
       tl_assert(xa);
       VG_(addToXA)( xa, &seg );
    } else {
       xa = VG_(newXA)( hg_zalloc, hg_free, sizeof(Segment*) );
       VG_(addToXA)( xa, &seg );
-      HG_(addToFM)( map_sem_to_Segment_stack, (Word)sem, (Word)xa );
+      VG_(addToFM)( map_sem_to_Segment_stack, (Word)sem, (Word)xa );
    }
 }
 
@@ -6386,7 +6386,7 @@
    XArray*  xa;
    Segment* seg;
    map_sem_to_Segment_stack_INIT();
-   if (HG_(lookupFM)( map_sem_to_Segment_stack, 
+   if (VG_(lookupFM)( map_sem_to_Segment_stack, 
                       NULL, (Word*)&xa, (Word)sem )) {
       /* xa is the stack for this semaphore. */
       Word sz = VG_(sizeXA)( xa );
@@ -6640,10 +6640,10 @@
    Lock* me;
    LAOGLinks* links;
    VG_(printf)("laog (requested by %s) {\n", who);
-   HG_(initIterFM)( laog );
+   VG_(initIterFM)( laog );
    me = NULL;
    links = NULL;
-   while (HG_(nextIterFM)( laog, (Word*)&me,
+   while (VG_(nextIterFM)( laog, (Word*)&me,
                                  (Word*)&links )) {
       tl_assert(me);
       tl_assert(links);
@@ -6657,7 +6657,7 @@
       me = NULL;
       links = NULL;
    }
-   HG_(doneIterFM)( laog );
+   VG_(doneIterFM)( laog );
    VG_(printf)("}\n");
 }
 
@@ -6681,7 +6681,7 @@
    /* Update the out edges for src */
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) {
       WordSetID outs_new;
       tl_assert(links);
       tl_assert(keyW == (Word)src);
@@ -6692,12 +6692,12 @@
       links = hg_zalloc(sizeof(LAOGLinks));
       links->inns = HG_(emptyWS)( univ_laog );
       links->outs = HG_(singletonWS)( univ_laog, (Word)dst );
-      HG_(addToFM)( laog, (Word)src, (Word)links );
+      VG_(addToFM)( laog, (Word)src, (Word)links );
    }
    /* Update the in edges for dst */
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) {
       WordSetID inns_new;
       tl_assert(links);
       tl_assert(keyW == (Word)dst);
@@ -6708,7 +6708,7 @@
       links = hg_zalloc(sizeof(LAOGLinks));
       links->inns = HG_(singletonWS)( univ_laog, (Word)src );
       links->outs = HG_(emptyWS)( univ_laog );
-      HG_(addToFM)( laog, (Word)dst, (Word)links );
+      VG_(addToFM)( laog, (Word)dst, (Word)links );
    }
 
    tl_assert( (presentF && presentR) || (!presentF && !presentR) );
@@ -6727,7 +6727,7 @@
       expo.src_ec = NULL;
       expo.dst_ec = NULL;
       tl_assert(laog_exposition);
-      if (HG_(lookupFM)( laog_exposition, NULL, NULL, (Word)&expo )) {
+      if (VG_(lookupFM)( laog_exposition, NULL, NULL, (Word)&expo )) {
          /* we already have it; do nothing */
       } else {
          LAOGLinkExposition* expo2 = hg_zalloc(sizeof(LAOGLinkExposition));
@@ -6735,7 +6735,7 @@
          expo2->dst_ga = dst->guestaddr;
          expo2->src_ec = src->acquired_at;
          expo2->dst_ec = dst->acquired_at;
-         HG_(addToFM)( laog_exposition, (Word)expo2, (Word)NULL );
+         VG_(addToFM)( laog_exposition, (Word)expo2, (Word)NULL );
       }
    }
 }
@@ -6748,7 +6748,7 @@
    /* Update the out edges for src */
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) {
       tl_assert(links);
       tl_assert(keyW == (Word)src);
       links->outs = HG_(delFromWS)( univ_laog, links->outs, (Word)dst );
@@ -6756,7 +6756,7 @@
    /* Update the in edges for dst */
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) {
       tl_assert(links);
       tl_assert(keyW == (Word)dst);
       links->inns = HG_(delFromWS)( univ_laog, links->inns, (Word)src );
@@ -6769,7 +6769,7 @@
    LAOGLinks* links;
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) {
       tl_assert(links);
       tl_assert(keyW == (Word)lk);
       return links->outs;
@@ -6784,7 +6784,7 @@
    LAOGLinks* links;
    keyW  = 0;
    links = NULL;
-   if (HG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) {
+   if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) {
       tl_assert(links);
       tl_assert(keyW == (Word)lk);
       return links->inns;
@@ -6801,11 +6801,11 @@
    LAOGLinks* links;
    if ( !laog )
       return; /* nothing much we can do */
-   HG_(initIterFM)( laog );
+   VG_(initIterFM)( laog );
    me = NULL;
    links = NULL;
    if (0) VG_(printf)("laog sanity check\n");
-   while (HG_(nextIterFM)( laog, (Word*)&me,
+   while (VG_(nextIterFM)( laog, (Word*)&me,
                                  (Word*)&links )) {
       tl_assert(me);
       tl_assert(links);
@@ -6826,7 +6826,7 @@
       me = NULL;
       links = NULL;
    }
-   HG_(doneIterFM)( laog );
+   VG_(doneIterFM)( laog );
    return;
 
   bad:
@@ -6860,7 +6860,7 @@
 
    ret     = NULL;
    stack   = VG_(newXA)( hg_zalloc, hg_free, sizeof(Lock*) );
-   visited = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
+   visited = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
 
    (void) VG_(addToXA)( stack, &src );
 
@@ -6875,10 +6875,10 @@
 
       if (HG_(elemWS)( univ_lsets, dsts, (Word)here )) { ret = here; break; }
 
-      if (HG_(lookupFM)( visited, NULL, NULL, (Word)here ))
+      if (VG_(lookupFM)( visited, NULL, NULL, (Word)here ))
          continue;
 
-      HG_(addToFM)( visited, (Word)here, 0 );
+      VG_(addToFM)( visited, (Word)here, 0 );
 
       succs = laog__succs( here );
       HG_(getPayloadWS)( &succs_words, &succs_size, univ_laog, succs );
@@ -6886,7 +6886,7 @@
          (void) VG_(addToXA)( stack, &succs_words[i] );
    }
 
-   HG_(deleteFM)( visited, NULL, NULL );
+   VG_(deleteFM)( visited, NULL, NULL );
    VG_(deleteXA)( stack );
    return ret;
 }
@@ -6913,9 +6913,9 @@
       return;
 
    if (!laog)
-      laog = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
+      laog = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
    if (!laog_exposition)
-      laog_exposition = HG_(newFM)( hg_zalloc, hg_free, 
+      laog_exposition = VG_(newFM)( hg_zalloc, hg_free, 
                                     cmp_LAOGLinkExposition );
 
    /* First, the check.  Complain if there is any path in laog from lk
@@ -6939,7 +6939,7 @@
       key.src_ec = NULL;
       key.dst_ec = NULL;
       found = NULL;
-      if (HG_(lookupFM)( laog_exposition,
+      if (VG_(lookupFM)( laog_exposition,
                          (Word*)&found, NULL, (Word)&key )) {
          tl_assert(found != &key);
          tl_assert(found->src_ga == key.src_ga);
@@ -7021,9 +7021,9 @@
    UWord* ws_words;
 
    if (!laog)
-      laog = HG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
+      laog = VG_(newFM)( hg_zalloc, hg_free, NULL/*unboxedcmp*/ );
    if (!laog_exposition)
-      laog_exposition = HG_(newFM)( hg_zalloc, hg_free, 
+      laog_exposition = VG_(newFM)( hg_zalloc, hg_free, 
                                     cmp_LAOGLinkExposition );
 
    HG_(getPayloadWS)( &ws_words, &ws_size, univ_lsets, locksToDelete );
@@ -7499,7 +7499,7 @@
 
 static void map_pthread_t_to_Thread_INIT ( void ) {
    if (UNLIKELY(map_pthread_t_to_Thread == NULL)) {
-      map_pthread_t_to_Thread = HG_(newFM)( hg_zalloc, hg_free, NULL );
+      map_pthread_t_to_Thread = VG_(newFM)( hg_zalloc, hg_free, NULL );
       tl_assert(map_pthread_t_to_Thread != NULL);
    }
 }
@@ -7559,7 +7559,7 @@
          if (0)
          VG_(printf)("XXXX: bind pthread_t %p to Thread* %p\n",
                      (void*)args[1], (void*)my_thr );
-         HG_(addToFM)( map_pthread_t_to_Thread, (Word)args[1], (Word)my_thr );
+         VG_(addToFM)( map_pthread_t_to_Thread, (Word)args[1], (Word)my_thr );
          break;
       }
 
@@ -7582,7 +7582,7 @@
          VG_(printf)("NOTIFY_JOIN_COMPLETE (tid %d): quitter = %p\n", (Int)tid,
                      (void*)args[1]);
          map_pthread_t_to_Thread_INIT();
-         found = HG_(lookupFM)( map_pthread_t_to_Thread, 
+         found = VG_(lookupFM)( map_pthread_t_to_Thread, 
                                 NULL, (Word*)&thr_q, (Word)args[1] );
           /* Can this fail?  It would mean that our pthread_join
              wrapper observed a successful join on args[1] yet that
@@ -7736,10 +7736,10 @@
    if (!str)
       str = "(null)";
    if (!string_table) {
-      string_table = HG_(newFM)( hg_zalloc, hg_free, string_table_cmp );
+      string_table = VG_(newFM)( hg_zalloc, hg_free, string_table_cmp );
       tl_assert(string_table);
    }
-   if (HG_(lookupFM)( string_table,
+   if (VG_(lookupFM)( string_table,
                       NULL, (Word*)&copy, (Word)str )) {
       tl_assert(copy);
       if (0) VG_(printf)("string_table_strdup: %p -> %p\n", str, copy );
@@ -7747,7 +7747,7 @@
    } else {
       copy = VG_(strdup)(str);
       tl_assert(copy);
-      HG_(addToFM)( string_table, (Word)copy, (Word)copy );
+      VG_(addToFM)( string_table, (Word)copy, (Word)copy );
       return copy;
    }
 }
@@ -7771,10 +7771,10 @@
    stats__ga_LockN_to_P_queries++;
    tl_assert( is_sane_LockN(lkn) );
    if (!yaWFM) {
-      yaWFM = HG_(newFM)( hg_zalloc, hg_free, lock_unique_cmp );
+      yaWFM = VG_(newFM)( hg_zalloc, hg_free, lock_unique_cmp );
       tl_assert(yaWFM);
    }
-   if (!HG_(lookupFM)( yaWFM, NULL, (Word*)&lkp, (Word)lkn)) {
+   if (!VG_(lookupFM)( yaWFM, NULL, (Word*)&lkp, (Word)lkn)) {
       lkp = hg_zalloc( sizeof(Lock) );
       *lkp = *lkn;
       lkp->admin = NULL;
@@ -7785,7 +7785,7 @@
       lkp->heldW  = False;
       lkp->heldBy = NULL;
       lkp->acquired_at = NULL;
-      HG_(addToFM)( yaWFM, (Word)lkp, (Word)lkp );
+      VG_(addToFM)( yaWFM, (Word)lkp, (Word)lkp );
    }
    tl_assert( is_sane_LockP(lkp) );
    return lkp;
@@ -8693,19 +8693,19 @@
 
       VG_(printf)("L(ast)L(ock) map: %'8lu inserts (%d map size)\n",
                   stats__ga_LL_adds,
-                  (Int)(ga_to_lastlock ? HG_(sizeFM)( ga_to_lastlock ) : 0) );
+                  (Int)(ga_to_lastlock ? VG_(sizeFM)( ga_to_lastlock ) : 0) );
 
       VG_(printf)("  LockN-to-P map: %'8lu queries (%d map size)\n",
                   stats__ga_LockN_to_P_queries,
-                  (Int)(yaWFM ? HG_(sizeFM)( yaWFM ) : 0) );
+                  (Int)(yaWFM ? VG_(sizeFM)( yaWFM ) : 0) );
 
       VG_(printf)("string table map: %'8lu queries (%d map size)\n",
                   stats__string_table_queries,
-                  (Int)(string_table ? HG_(sizeFM)( string_table ) : 0) );
+                  (Int)(string_table ? VG_(sizeFM)( string_table ) : 0) );
       VG_(printf)("            LAOG: %'8d map size\n",
-                  (Int)(laog ? HG_(sizeFM)( laog ) : 0));
+                  (Int)(laog ? VG_(sizeFM)( laog ) : 0));
       VG_(printf)(" LAOG exposition: %'8d map size\n",
-                  (Int)(laog_exposition ? HG_(sizeFM)( laog_exposition ) : 0));
+                  (Int)(laog_exposition ? VG_(sizeFM)( laog_exposition ) : 0));
       VG_(printf)("           locks: %'8lu acquires, "
                   "%'lu releases\n",
                   stats__lockN_acquires,