Index: /trunk/src/VBox/Runtime/common/dbg/dbgas.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/dbg/dbgas.cpp	(revision 22112)
+++ /trunk/src/VBox/Runtime/common/dbg/dbgas.cpp	(revision 22113)
@@ -68,4 +68,6 @@
     /** Pointer to the next module with an identical name. */
     PRTDBGASMOD         pNextName;
+    /** The index into RTDBGASINT::papModules. */
+    uint32_t            iOrdinal;
 } RTDBGASMOD;
 
@@ -112,5 +114,5 @@
     /** Pointer to the module table.
      * The valid array length is given by cModules. */
-    PRTDBGASMOD         paModules;
+    PRTDBGASMOD        *papModules;
     /** AVL tree translating module handles to module entries. */
     AVLPVTREE           ModTree;
@@ -209,5 +211,5 @@
     pDbgAs->hLock       = NIL_RTSEMRW;
     pDbgAs->cModules    = 0;
-    pDbgAs->paModules   = NULL;
+    pDbgAs->papModules  = NULL;
     pDbgAs->ModTree     = NULL;
     pDbgAs->MapTree     = NULL;
@@ -332,9 +334,18 @@
     while (i-- > 0)
     {
-        RTDbgModRelease((RTDBGMOD)pDbgAs->paModules[i].Core.Key);
-        pDbgAs->paModules[i].Core.Key = NIL_RTDBGMOD;
-    }
-    RTMemFree(pDbgAs->paModules);
-    pDbgAs->paModules = NULL;
+        PRTDBGASMOD pMod = pDbgAs->papModules[i];
+        AssertPtr(pMod);
+        if (VALID_PTR(pMod))
+        {
+            Assert(pMod->iOrdinal == i);
+            RTDbgModRelease((RTDBGMOD)pMod->Core.Key);
+            pMod->Core.Key = NIL_RTDBGMOD;
+            pMod->iOrdinal = UINT32_MAX;
+            RTMemFree(pMod);
+        }
+        pDbgAs->papModules[i] = NULL;
+    }
+    RTMemFree(pDbgAs->papModules);
+    pDbgAs->papModules = NULL;
 
     RTMemFree(pDbgAs);
@@ -521,29 +532,25 @@
         if (!(pDbgAs->cModules % 32))
         {
-            void *pvNew = RTMemRealloc(pDbgAs->paModules, sizeof(pDbgAs->paModules[0]) * (pDbgAs->cModules + 32));
+            void *pvNew = RTMemRealloc(pDbgAs->papModules, sizeof(pDbgAs->papModules[0]) * (pDbgAs->cModules + 32));
             if (!pvNew)
                 return VERR_NO_MEMORY;
-            pDbgAs->paModules = (PRTDBGASMOD)pvNew;
-
-            /** @todo this rebuilding of the tree gets a bit silly... */
-            pDbgAs->ModTree = NULL;
-            for (uint32_t iMod = 0; iMod < pDbgAs->cModules; iMod++)
-            {
-                bool fRc = RTAvlPVInsert(&pDbgAs->ModTree, &pDbgAs->paModules[iMod].Core);
-                Assert(fRc); NOREF(fRc);
-            }
+            pDbgAs->papModules = (PRTDBGASMOD *)pvNew;
         }
-        pMod = &pDbgAs->paModules[pDbgAs->cModules];
-        pDbgAs->cModules++;
-
+        pMod = (PRTDBGASMOD)RTMemAlloc(sizeof(*pMod));
+        if (!pMod)
+            return VERR_NO_MEMORY;
         pMod->Core.Key  = hDbgMod;
         pMod->pMapHead  = NULL;
         pMod->pNextName = NULL;
-        if (!RTAvlPVInsert(&pDbgAs->ModTree, &pMod->Core))
+        if (RT_UNLIKELY(!RTAvlPVInsert(&pDbgAs->ModTree, &pMod->Core)))
         {
             AssertFailed();
             pDbgAs->cModules--;
+            RTMemFree(pMod);
             return VERR_INTERNAL_ERROR;
         }
+        pMod->iOrdinal = pDbgAs->cModules;
+        pDbgAs->papModules[pDbgAs->cModules] = pMod;
+        pDbgAs->cModules++;
         RTDbgModRetain(hDbgMod);
 
@@ -558,7 +565,8 @@
             if (!pName)
             {
+                RTDbgModRelease(hDbgMod);
                 pDbgAs->cModules--;
                 RTAvlPVRemove(&pDbgAs->ModTree, hDbgMod);
-                RTDbgModRelease(hDbgMod);
+                RTMemFree(pMod);
                 return VERR_NO_MEMORY;
             }
@@ -759,15 +767,19 @@
      * Remove it from the module table by replacing it by the last entry.
      */
-    uint32_t iMod = pMod - &pDbgAs->paModules[0];
-    Assert(iMod < pDbgAs->cModules);
     pDbgAs->cModules--;
-    if (iMod <= pDbgAs->cModules)
-    {
-        pNode = RTAvlPVRemove(&pDbgAs->ModTree, pDbgAs->paModules[pDbgAs->cModules].Core.Key);
-        Assert(pNode);
-        pDbgAs->paModules[iMod] = pDbgAs->paModules[pDbgAs->cModules];
-        bool fRc = RTAvlPVInsert(&pDbgAs->ModTree, &pMod->Core);
-        Assert(fRc); NOREF(fRc);
-    }
+    uint32_t iMod = pMod->iOrdinal;
+    Assert(iMod <= pDbgAs->cModules);
+    if (iMod != pDbgAs->cModules)
+    {
+        PRTDBGASMOD pTailMod = pDbgAs->papModules[pDbgAs->cModules];
+        pTailMod->iOrdinal = iMod;
+        pDbgAs->papModules[iMod] = pTailMod;
+    }
+    pMod->iOrdinal = UINT32_MAX;
+
+    /*
+     * Free it.
+     */
+    RTMemFree(pMod);
 }
 
@@ -789,5 +801,5 @@
     /* unlink */
     PRTDBGASMOD pMod = pMap->pMod;
-    if (pMod->pMapHead)
+    if (pMod->pMapHead == pMap)
         pMod->pMapHead = pMap->pNext;
     else
@@ -942,5 +954,5 @@
      * Get, retain and return it.
      */
-    RTDBGMOD hMod = (RTDBGMOD)pDbgAs->paModules[iModule].Core.Key;
+    RTDBGMOD hMod = (RTDBGMOD)pDbgAs->papModules[iModule]->Core.Key;
     RTDbgModRetain(hMod);
 
@@ -1321,11 +1333,11 @@
 
     uint32_t iMod = *pcModules = pDbgAs->cModules;
-    PRTDBGMOD paModules = (PRTDBGMOD)RTMemTmpAlloc(sizeof(paModules[0]) * RT_MAX(iMod, 1));
-    if (paModules)
+    PRTDBGMOD pahModules = (PRTDBGMOD)RTMemTmpAlloc(sizeof(pahModules[0]) * RT_MAX(iMod, 1));
+    if (pahModules)
     {
         while (iMod-- > 0)
         {
-            RTDBGMOD hMod = (RTDBGMOD)pDbgAs->paModules[iMod].Core.Key;
-            paModules[iMod] = hMod;
+            RTDBGMOD hMod = (RTDBGMOD)pDbgAs->papModules[iMod]->Core.Key;
+            pahModules[iMod] = hMod;
             RTDbgModRetain(hMod);
         }
@@ -1333,5 +1345,5 @@
 
     RTDBGAS_UNLOCK_READ(pDbgAs);
-    return paModules;
+    return pahModules;
 }
 
@@ -1448,6 +1460,6 @@
      */
     uint32_t cModules;
-    PRTDBGMOD paModules = rtDbgAsSnapshotModuleTable(pDbgAs, &cModules);
-    if (!paModules)
+    PRTDBGMOD pahModules = rtDbgAsSnapshotModuleTable(pDbgAs, &cModules);
+    if (!pahModules)
         return VERR_NO_TMP_MEMORY;
 
@@ -1455,24 +1467,24 @@
     {
         if (    cchModPat == 0
-            ||  RTStrSimplePatternNMatch(pachModPat, cchModPat, RTDbgModName(paModules[i]), RTSTR_MAX))
+            ||  RTStrSimplePatternNMatch(pachModPat, cchModPat, RTDbgModName(pahModules[i]), RTSTR_MAX))
         {
-            int rc = RTDbgModSymbolByName(paModules[i], pszSymbol, pSymbol);
+            int rc = RTDbgModSymbolByName(pahModules[i], pszSymbol, pSymbol);
             if (RT_SUCCESS(rc))
             {
-                if (rtDbgAsFindMappingAndAdjustSymbolValue(pDbgAs, paModules[i], pSymbol))
+                if (rtDbgAsFindMappingAndAdjustSymbolValue(pDbgAs, pahModules[i], pSymbol))
                 {
                     if (phMod)
-                        RTDbgModRetain(*phMod = paModules[i]);
+                        RTDbgModRetain(*phMod = pahModules[i]);
                     for (; i < cModules; i++)
-                        RTDbgModRelease(paModules[i]);
-                    RTMemTmpFree(paModules);
+                        RTDbgModRelease(pahModules[i]);
+                    RTMemTmpFree(pahModules);
                     return rc;
                 }
             }
         }
-        RTDbgModRelease(paModules[i]);
-    }
-
-    RTMemTmpFree(paModules);
+        RTDbgModRelease(pahModules[i]);
+    }
+
+    RTMemTmpFree(pahModules);
     return VERR_SYMBOL_NOT_FOUND;
 }
@@ -1523,6 +1535,6 @@
      */
     uint32_t cModules;
-    PRTDBGMOD paModules = rtDbgAsSnapshotModuleTable(pDbgAs, &cModules);
-    if (!paModules)
+    PRTDBGMOD pahModules = rtDbgAsSnapshotModuleTable(pDbgAs, &cModules);
+    if (!pahModules)
         return VERR_NO_TMP_MEMORY;
 
@@ -1530,24 +1542,24 @@
     {
         if (    cchModPat == 0
-            ||  RTStrSimplePatternNMatch(pachModPat, cchModPat, RTDbgModName(paModules[i]), RTSTR_MAX))
+            ||  RTStrSimplePatternNMatch(pachModPat, cchModPat, RTDbgModName(pahModules[i]), RTSTR_MAX))
         {
-            int rc = RTDbgModSymbolByNameA(paModules[i], pszSymbol, ppSymbol);
+            int rc = RTDbgModSymbolByNameA(pahModules[i], pszSymbol, ppSymbol);
             if (RT_SUCCESS(rc))
             {
-                if (rtDbgAsFindMappingAndAdjustSymbolValue(pDbgAs, paModules[i], *ppSymbol))
+                if (rtDbgAsFindMappingAndAdjustSymbolValue(pDbgAs, pahModules[i], *ppSymbol))
                 {
                     if (phMod)
-                        RTDbgModRetain(*phMod = paModules[i]);
+                        RTDbgModRetain(*phMod = pahModules[i]);
                     for (; i < cModules; i++)
-                        RTDbgModRelease(paModules[i]);
-                    RTMemTmpFree(paModules);
+                        RTDbgModRelease(pahModules[i]);
+                    RTMemTmpFree(pahModules);
                     return rc;
                 }
             }
         }
-        RTDbgModRelease(paModules[i]);
-    }
-
-    RTMemTmpFree(paModules);
+        RTDbgModRelease(pahModules[i]);
+    }
+
+    RTMemTmpFree(pahModules);
     return VERR_SYMBOL_NOT_FOUND;
 }
