Index: /trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletion.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletion.cpp	(revision 39013)
+++ /trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletion.cpp	(revision 39014)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2008 Oracle Corporation
+ * Copyright (C) 2006-2011 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -42,4 +42,8 @@
 #include "PDMAsyncCompletionInternal.h"
 
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
 /**
  * Async I/O type.
@@ -142,5 +146,10 @@
 typedef PPDMACBWMGR *PPPDMACBWMGR;
 
+
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
 static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask);
+
 
 /**
@@ -151,13 +160,10 @@
  * @param   ppTemplate    Where to store the template handle.
  */
-static int pdmR3AsyncCompletionTemplateCreate(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE enmType)
+static int pdmR3AsyncCompletionTemplateCreate(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
+                                              PDMASYNCCOMPLETIONTEMPLATETYPE enmType)
 {
     PUVM pUVM = pVM->pUVM;
 
-    if (ppTemplate == NULL)
-    {
-        AssertMsgFailed(("ppTemplate is NULL\n"));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
 
     PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
@@ -208,15 +214,6 @@
      */
     VM_ASSERT_EMT(pVM);
-    if (!pfnCompleted)
-    {
-        AssertMsgFailed(("No completion callback!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
-
-    if (!ppTemplate)
-    {
-        AssertMsgFailed(("Template pointer is NULL!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
 
     /*
@@ -259,15 +256,6 @@
      * Validate input.
      */
-    if (!pfnCompleted)
-    {
-        AssertMsgFailed(("No completion callback!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
-
-    if (!ppTemplate)
-    {
-        AssertMsgFailed(("Template pointer is NULL!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
 
     /*
@@ -311,15 +299,6 @@
      */
     VM_ASSERT_EMT(pVM);
-    if (!pfnCompleted)
-    {
-        AssertMsgFailed(("No completion callback!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
-
-    if (!ppTemplate)
-    {
-        AssertMsgFailed(("Template pointer is NULL!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
 
     /*
@@ -362,15 +341,6 @@
      */
     VM_ASSERT_EMT(pVM);
-    if (!pfnCompleted)
-    {
-        AssertMsgFailed(("No completion callback!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
-
-    if (!ppTemplate)
-    {
-        AssertMsgFailed(("Template pointer is NULL!\n"));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
 
     /*
@@ -599,9 +569,7 @@
     PPDMACBWMGR pBwMgr = NULL;
 
-    if (VALID_PTR(pcszId))
-    {
-        int rc;
-        rc = RTCritSectEnter(&pEpClass->CritSect);
-        AssertRC(rc);
+    if (RT_VALID_PTR(pcszId))
+    {
+        int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
 
         pBwMgr = pEpClass->pBwMgrsHead;
@@ -610,6 +578,5 @@
             pBwMgr = pBwMgr->pNext;
 
-        rc = RTCritSectLeave(&pEpClass->CritSect);
-        AssertRC(rc);
+        rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
     }
 
@@ -620,23 +587,16 @@
 {
     PPDMASYNCCOMPLETIONEPCLASS pEpClass = pBwMgr->pEpClass;
-    int rc;
-
-    rc = RTCritSectEnter(&pEpClass->CritSect);
-    AssertRC(rc);
+    int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
 
     pBwMgr->pNext = pEpClass->pBwMgrsHead;
     pEpClass->pBwMgrsHead = pBwMgr;
 
-    rc = RTCritSectLeave(&pEpClass->CritSect);
-    AssertRC(rc);
+    rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
 }
 
 static void pdmacBwMgrUnlink(PPDMACBWMGR pBwMgr)
 {
-    int rc;
     PPDMASYNCCOMPLETIONEPCLASS pEpClass = pBwMgr->pEpClass;
-
-    rc = RTCritSectEnter(&pEpClass->CritSect);
-    AssertRC(rc);
+    int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
 
     if (pBwMgr == pEpClass->pBwMgrsHead)
@@ -653,6 +613,5 @@
     }
 
-    rc = RTCritSectLeave(&pEpClass->CritSect);
-    AssertRC(rc);
+    rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
 }
 
@@ -660,7 +619,4 @@
                                            uint32_t cbTransferPerSecStart, uint32_t cbTransferPerSecStep)
 {
-    int rc = VINF_SUCCESS;
-    PPDMACBWMGR pBwMgr;
-
     LogFlowFunc(("pEpClass=%#p pcszBwMgr=%#p{%s} cbTransferPerSecMax=%u cbTransferPerSecStart=%u cbTransferPerSecStep=%u\n",
                  pEpClass, pcszBwMgr, cbTransferPerSecMax, cbTransferPerSecStart, cbTransferPerSecStep));
@@ -670,5 +626,6 @@
     AssertReturn(*pcszBwMgr != '\0', VERR_INVALID_PARAMETER);
 
-    pBwMgr = pdmacBwMgrFindById(pEpClass, pcszBwMgr);
+    int         rc;
+    PPDMACBWMGR pBwMgr = pdmacBwMgrFindById(pEpClass, pcszBwMgr);
     if (!pBwMgr)
     {
@@ -722,5 +679,5 @@
 bool pdmacEpIsTransferAllowed(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint32_t cbTransfer, RTMSINTERVAL *pmsWhenNext)
 {
-    bool fAllowed = true;
+    bool        fAllowed = true;
     PPDMACBWMGR pBwMgr = ASMAtomicReadPtrT(&pEndpoint->pBwMgr, PPDMACBWMGR);
 
@@ -765,5 +722,4 @@
 
     LogFlowFunc(("fAllowed=%RTbool\n", fAllowed));
-
     return fAllowed;
 }
@@ -780,23 +736,19 @@
         {
             case PDMASYNCCOMPLETIONTEMPLATETYPE_DEV:
-            {
                 pTemplate->u.Dev.pfnCompleted(pTemplate->u.Dev.pDevIns, pTask->pvUser, rc);
                 break;
-            }
+
             case PDMASYNCCOMPLETIONTEMPLATETYPE_DRV:
-            {
                 pTemplate->u.Drv.pfnCompleted(pTemplate->u.Drv.pDrvIns, pTemplate->u.Drv.pvTemplateUser, pTask->pvUser, rc);
                 break;
-            }
+
             case PDMASYNCCOMPLETIONTEMPLATETYPE_USB:
-            {
                 pTemplate->u.Usb.pfnCompleted(pTemplate->u.Usb.pUsbIns, pTask->pvUser, rc);
                 break;
-            }
+
             case PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL:
-            {
                 pTemplate->u.Int.pfnCompleted(pTemplate->pVM, pTask->pvUser, pTemplate->u.Int.pvUser, rc);
                 break;
-            }
+
             default:
                 AssertMsgFailed(("Unknown template type!\n"));
@@ -817,11 +769,8 @@
 int pdmR3AsyncCompletionEpClassInit(PVM pVM, PCPDMASYNCCOMPLETIONEPCLASSOPS pEpClassOps, PCFGMNODE pCfgHandle)
 {
-    int rc = VINF_SUCCESS;
-
     /* Validate input. */
-    if (   !pEpClassOps
-        || (pEpClassOps->u32Version != PDMAC_EPCLASS_OPS_VERSION)
-        || (pEpClassOps->u32VersionEnd != PDMAC_EPCLASS_OPS_VERSION))
-        AssertMsgFailedReturn(("Invalid endpoint class data\n"), VERR_VERSION_MISMATCH);
+    AssertPtrReturn(pEpClassOps, VERR_INVALID_POINTER);
+    AssertReturn(pEpClassOps->u32Version == PDMAC_EPCLASS_OPS_VERSION, VERR_VERSION_MISMATCH);
+    AssertReturn(pEpClassOps->u32VersionEnd == PDMAC_EPCLASS_OPS_VERSION, VERR_VERSION_MISMATCH);
 
     LogFlowFunc((": pVM=%p pEpClassOps=%p{%s}\n", pVM, pEpClassOps, pEpClassOps->pcszName));
@@ -830,7 +779,7 @@
     PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = NULL;
 
-    rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
-                          pEpClassOps->cbEndpointClassGlobal,
-                          (void **)&pEndpointClass);
+    int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
+                              pEpClassOps->cbEndpointClassGlobal,
+                              (void **)&pEndpointClass);
     if (RT_SUCCESS(rc))
     {
@@ -922,5 +871,4 @@
 static void pdmR3AsyncCompletionEpClassTerminate(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass)
 {
-    int rc = VINF_SUCCESS;
     PVM pVM = pEndpointClass->pVM;
 
@@ -957,7 +905,4 @@
 int pdmR3AsyncCompletionInit(PVM pVM)
 {
-    int  rc   = VINF_SUCCESS;
-    PUVM pUVM = pVM->pUVM;
-
     LogFlowFunc((": pVM=%p\n", pVM));
 
@@ -967,8 +912,6 @@
     PCFGMNODE pCfgAsyncCompletion = CFGMR3GetChild(CFGMR3GetChild(pCfgRoot, "PDM"), "AsyncCompletion");
 
-    rc = pdmR3AsyncCompletionEpClassInit(pVM, &g_PDMAsyncCompletionEndpointClassFile, pCfgAsyncCompletion);
-
+    int rc = pdmR3AsyncCompletionEpClassInit(pVM, &g_PDMAsyncCompletionEndpointClassFile, pCfgAsyncCompletion);
     LogFlowFunc((": pVM=%p rc=%Rrc\n", pVM, rc));
-
     return rc;
 }
@@ -1007,7 +950,7 @@
         if (pUVM->pdm.s.apAsyncCompletionEndpointClass[i])
         {
-            PPDMASYNCCOMPLETIONEPCLASS pEpClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[i];
+            PPDMASYNCCOMPLETIONEPCLASS  pEpClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[i];
+            PPDMACBWMGR                 pBwMgr   = pEpClass->pBwMgrsHead;
             PPDMASYNCCOMPLETIONENDPOINT pEp;
-            PPDMACBWMGR pBwMgr = pEpClass->pBwMgrsHead;
 
             if (pBwMgr)
@@ -1061,8 +1004,5 @@
 {
     PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
-    PPDMASYNCCOMPLETIONTASK pTask = NULL;
-
-    pTask = (PPDMASYNCCOMPLETIONTASK)RTMemCacheAlloc(pEndpointClass->hMemCacheTasks);
-
+    PPDMASYNCCOMPLETIONTASK    pTask = (PPDMASYNCCOMPLETIONTASK)RTMemCacheAlloc(pEndpointClass->hMemCacheTasks);
     if (RT_LIKELY(pTask))
     {
@@ -1092,55 +1032,22 @@
 {
     PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
-    uint64_t tsRun  = RTTimeNanoTS() - pTask->tsNsStart;
-
-    if (RT_UNLIKELY(tsRun >= (uint64_t)10*1000*1000*1000))
-    {
-        LogRel(("AsyncCompletion: Task %#p completed after %llu seconds\n", pTask, tsRun / ((uint64_t)1000*1000*1000)));
-    }
+    uint64_t cNsRun = RTTimeNanoTS() - pTask->tsNsStart;
+
+    if (RT_UNLIKELY(cNsRun >= RT_NS_10SEC))
+        LogRel(("AsyncCompletion: Task %#p completed after %llu seconds\n", pTask, cNsRun / RT_NS_1SEC));
 
 #ifdef VBOX_WITH_STATISTICS
-    uint64_t iStatIdx;
-
-    if (tsRun < 1000)
-    {
-        /* Update nanoseconds statistics */
-        iStatIdx = tsRun / 100;
-        STAM_COUNTER_INC(&pEndpoint->StatTaskRunTimesNs[iStatIdx]);
-    }
+    PSTAMCOUNTER pStatCounter;
+    if (cNsRun < RT_NS_1US)
+        pStatCounter = &pEndpoint->StatTaskRunTimesNs[cNsRun / (RT_NS_1US / 10)];
+    else if (cNsRun < RT_NS_1MS)
+        pStatCounter = &pEndpoint->StatTaskRunTimesUs[cNsRun / (RT_NS_1MS / 10)];
+    else if (cNsRun < RT_NS_1SEC)
+        pStatCounter = &pEndpoint->StatTaskRunTimesMs[cNsRun / (RT_NS_1SEC / 10)];
+    else if (cNsRun < RT_NS_1SEC_64*100)
+        pStatCounter = &pEndpoint->StatTaskRunTimesSec[cNsRun / (RT_NS_1SEC_64*100 / 10)];
     else
-    {
-        tsRun /= 1000;
-
-        if (tsRun < 1000)
-        {
-            /* Update microsecnds statistics */
-            iStatIdx = tsRun / 100;
-            STAM_COUNTER_INC(&pEndpoint->StatTaskRunTimesMicroSec[iStatIdx]);
-        }
-        else
-        {
-            tsRun /= 1000;
-
-            if (tsRun < 1000)
-            {
-                /* Update milliseconds statistics */
-                iStatIdx = tsRun / 100;
-                STAM_COUNTER_INC(&pEndpoint->StatTaskRunTimesMs[iStatIdx]);
-            }
-            else
-            {
-                tsRun /= 1000;
-
-                if (tsRun < 1000)
-                {
-                    /* Update seconds statistics */
-                    iStatIdx = tsRun / 10;
-                    STAM_COUNTER_INC(&pEndpoint->StatTaskRunTimesSec[iStatIdx]);
-                }
-                else
-                    STAM_COUNTER_INC(&pEndpoint->StatTaskRunOver100Sec);
-            }
-        }
-    }
+        pStatCounter = &pEndpoint->StatTaskRunOver100Sec;
+    STAM_COUNTER_INC(pStatCounter);
 
     STAM_COUNTER_INC(&pEndpoint->StatIoOpsCompleted);
@@ -1148,5 +1055,4 @@
     uint64_t tsMsCur = RTTimeMilliTS();
     uint64_t tsInterval = tsMsCur - pEndpoint->tsIntervalStartMs;
-
     if (tsInterval >= 1000)
     {
@@ -1155,5 +1061,5 @@
         pEndpoint->cIoOpsCompleted = 0;
     }
-#endif
+#endif /* VBOX_WITH_STATISTICS */
 
     RTMemCacheFree(pEndpointClass->hMemCacheTasks, pTask);
@@ -1180,13 +1086,11 @@
                                                    PPDMASYNCCOMPLETIONTEMPLATE pTemplate)
 {
-    int rc = VINF_SUCCESS;
-
     LogFlowFunc((": ppEndpoint=%p pszFilename=%p{%s} fFlags=%u pTemplate=%p\n",
                  ppEndpoint, pszFilename, pszFilename, fFlags, pTemplate));
 
     /* Sanity checks. */
-    AssertReturn(VALID_PTR(ppEndpoint),  VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(pszFilename), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(pTemplate),   VERR_INVALID_POINTER);
+    AssertPtrReturn(ppEndpoint,  VERR_INVALID_POINTER);
+    AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
+    AssertPtrReturn(pTemplate,   VERR_INVALID_POINTER);
 
     /* Check that the flags are valid. */
@@ -1203,55 +1107,77 @@
     /* Search for a already opened endpoint for this file. */
     pEndpoint = pdmR3AsyncCompletionFindEndpointWithUri(pEndpointClass, pszFilename);
-
-    if(!pEndpoint)
-    {
-        rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
+    if (pEndpoint)
+    {
+        /* Endpoint found. */
+        pEndpoint->cUsers++;
+
+        *ppEndpoint = pEndpoint;
+        return VINF_SUCCESS;
+    }
+
+    /* Create an endpoint. */
+    int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
                               pEndpointClass->pEndpointOps->cbEndpoint,
                               (void **)&pEndpoint);
-        if (RT_SUCCESS(rc))
+    if (RT_SUCCESS(rc))
+    {
+
+        /* Initialize common parts. */
+        pEndpoint->pNext             = NULL;
+        pEndpoint->pPrev             = NULL;
+        pEndpoint->pEpClass          = pEndpointClass;
+        pEndpoint->pTemplate         = pTemplate;
+        pEndpoint->pszUri            = RTStrDup(pszFilename);
+        pEndpoint->cUsers            = 1;
+        pEndpoint->pBwMgr            = NULL;
+
+        if (   pEndpoint->pszUri
+            && RT_SUCCESS(rc))
         {
-
-            /* Initialize common parts. */
-            pEndpoint->pNext             = NULL;
-            pEndpoint->pPrev             = NULL;
-            pEndpoint->pEpClass          = pEndpointClass;
-            pEndpoint->pTemplate         = pTemplate;
-            pEndpoint->pszUri            = RTStrDup(pszFilename);
-            pEndpoint->cUsers            = 1;
-            pEndpoint->pBwMgr            = NULL;
-
-            if (   pEndpoint->pszUri
-                && RT_SUCCESS(rc))
+            /* Call the initializer for the endpoint. */
+            rc = pEndpointClass->pEndpointOps->pfnEpInitialize(pEndpoint, pszFilename, fFlags);
+            if (RT_SUCCESS(rc))
             {
-                /* Call the initializer for the endpoint. */
-                rc = pEndpointClass->pEndpointOps->pfnEpInitialize(pEndpoint, pszFilename, fFlags);
+                /* Link it into the list of endpoints. */
+                rc = RTCritSectEnter(&pEndpointClass->CritSect);
+                AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
+
+                pEndpoint->pNext = pEndpointClass->pEndpointsHead;
+                if (pEndpointClass->pEndpointsHead)
+                    pEndpointClass->pEndpointsHead->pPrev = pEndpoint;
+
+                pEndpointClass->pEndpointsHead = pEndpoint;
+                pEndpointClass->cEndpoints++;
+
+                rc = RTCritSectLeave(&pEndpointClass->CritSect);
+                AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
+
+                /* Reference the template. */
+                ASMAtomicIncU32(&pTemplate->cUsed);
+
+#ifdef VBOX_WITH_STATISTICS
+                /* Init the statistics part */
+                for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs); i++)
+                {
+                    rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesNs[i], STAMTYPE_COUNTER,
+                                         STAMVISIBILITY_USED,
+                                         STAMUNIT_OCCURENCES,
+                                         "Nanosecond resolution runtime statistics",
+                                         "/PDM/AsyncCompletion/File/%s/TaskRun1Ns-%u-%u",
+                                         RTPathFilename(pEndpoint->pszUri),
+                                         i*100, i*100+100-1);
+                    if (RT_FAILURE(rc))
+                        break;
+                }
+
                 if (RT_SUCCESS(rc))
                 {
-                    /* Link it into the list of endpoints. */
-                    rc = RTCritSectEnter(&pEndpointClass->CritSect);
-                    AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
-
-                    pEndpoint->pNext = pEndpointClass->pEndpointsHead;
-                    if (pEndpointClass->pEndpointsHead)
-                        pEndpointClass->pEndpointsHead->pPrev = pEndpoint;
-
-                    pEndpointClass->pEndpointsHead = pEndpoint;
-                    pEndpointClass->cEndpoints++;
-
-                    rc = RTCritSectLeave(&pEndpointClass->CritSect);
-                    AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
-
-                    /* Reference the template. */
-                    ASMAtomicIncU32(&pTemplate->cUsed);
-
-#ifdef VBOX_WITH_STATISTICS
-                    /* Init the statistics part */
-                    for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs); i++)
+                    for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesUs); i++)
                     {
-                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesNs[i], STAMTYPE_COUNTER,
+                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesUs[i], STAMTYPE_COUNTER,
                                              STAMVISIBILITY_USED,
                                              STAMUNIT_OCCURENCES,
-                                             "Nanosecond resolution runtime statistics",
-                                             "/PDM/AsyncCompletion/File/%s/TaskRun1Ns-%u-%u",
+                                             "Microsecond resolution runtime statistics",
+                                             "/PDM/AsyncCompletion/File/%s/TaskRun2MicroSec-%u-%u",
                                              RTPathFilename(pEndpoint->pszUri),
                                              i*100, i*100+100-1);
@@ -1259,117 +1185,94 @@
                             break;
                     }
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMicroSec); i++)
-                        {
-                            rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesMicroSec[i], STAMTYPE_COUNTER,
-                                                 STAMVISIBILITY_USED,
-                                                 STAMUNIT_OCCURENCES,
-                                                 "Microsecond resolution runtime statistics",
-                                                 "/PDM/AsyncCompletion/File/%s/TaskRun2MicroSec-%u-%u",
-                                                 RTPathFilename(pEndpoint->pszUri),
-                                                 i*100, i*100+100-1);
-                            if (RT_FAILURE(rc))
-                                break;
-                        }
-                    }
-
-                   if (RT_SUCCESS(rc))
+                }
+
+               if (RT_SUCCESS(rc))
+               {
+                   for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
                    {
-                       for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
-                       {
-                           rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesMs[i], STAMTYPE_COUNTER,
-                                                STAMVISIBILITY_USED,
-                                                STAMUNIT_OCCURENCES,
-                                                "Milliseconds resolution runtime statistics",
-                                                "/PDM/AsyncCompletion/File/%s/TaskRun3Ms-%u-%u",
-                                                RTPathFilename(pEndpoint->pszUri),
-                                                i*100, i*100+100-1);
-                           if (RT_FAILURE(rc))
-                               break;
-                       }
+                       rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesMs[i], STAMTYPE_COUNTER,
+                                            STAMVISIBILITY_USED,
+                                            STAMUNIT_OCCURENCES,
+                                            "Milliseconds resolution runtime statistics",
+                                            "/PDM/AsyncCompletion/File/%s/TaskRun3Ms-%u-%u",
+                                            RTPathFilename(pEndpoint->pszUri),
+                                            i*100, i*100+100-1);
+                       if (RT_FAILURE(rc))
+                           break;
                    }
-
-                   if (RT_SUCCESS(rc))
+               }
+
+               if (RT_SUCCESS(rc))
+               {
+                   for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
                    {
-                       for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
-                       {
-                            rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesSec[i], STAMTYPE_COUNTER,
-                                                 STAMVISIBILITY_USED,
-                                                 STAMUNIT_OCCURENCES,
-                                                 "Second resolution runtime statistics",
-                                                 "/PDM/AsyncCompletion/File/%s/TaskRun4Sec-%u-%u",
-                                                 RTPathFilename(pEndpoint->pszUri),
-                                                 i*10, i*10+10-1);
-                            if (RT_FAILURE(rc))
-                                break;
-                        }
-                    }
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunOver100Sec, STAMTYPE_COUNTER,
+                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesSec[i], STAMTYPE_COUNTER,
                                              STAMVISIBILITY_USED,
                                              STAMUNIT_OCCURENCES,
-                                             "Tasks which ran more than 100sec",
-                                             "/PDM/AsyncCompletion/File/%s/TaskRunSecGreater100Sec",
-                                             RTPathFilename(pEndpoint->pszUri));
+                                             "Second resolution runtime statistics",
+                                             "/PDM/AsyncCompletion/File/%s/TaskRun4Sec-%u-%u",
+                                             RTPathFilename(pEndpoint->pszUri),
+                                             i*10, i*10+10-1);
+                        if (RT_FAILURE(rc))
+                            break;
                     }
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsPerSec, STAMTYPE_COUNTER,
-                                             STAMVISIBILITY_ALWAYS,
-                                             STAMUNIT_OCCURENCES,
-                                             "Processed I/O operations per second",
-                                             "/PDM/AsyncCompletion/File/%s/IoOpsPerSec",
-                                             RTPathFilename(pEndpoint->pszUri));
-                    }
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsStarted, STAMTYPE_COUNTER,
-                                             STAMVISIBILITY_ALWAYS,
-                                             STAMUNIT_OCCURENCES,
-                                             "Started I/O operations for this endpoint",
-                                             "/PDM/AsyncCompletion/File/%s/IoOpsStarted",
-                                             RTPathFilename(pEndpoint->pszUri));
-                    }
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsCompleted, STAMTYPE_COUNTER,
-                                             STAMVISIBILITY_ALWAYS,
-                                             STAMUNIT_OCCURENCES,
-                                             "Completed I/O operations for this endpoint",
-                                             "/PDM/AsyncCompletion/File/%s/IoOpsCompleted",
-                                             RTPathFilename(pEndpoint->pszUri));
-                    }
-
-                    pEndpoint->tsIntervalStartMs = RTTimeMilliTS();
+                }
+
+                if (RT_SUCCESS(rc))
+                {
+                    rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunOver100Sec, STAMTYPE_COUNTER,
+                                         STAMVISIBILITY_USED,
+                                         STAMUNIT_OCCURENCES,
+                                         "Tasks which ran more than 100sec",
+                                         "/PDM/AsyncCompletion/File/%s/TaskRunSecGreater100Sec",
+                                         RTPathFilename(pEndpoint->pszUri));
+                }
+
+                if (RT_SUCCESS(rc))
+                {
+                    rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsPerSec, STAMTYPE_COUNTER,
+                                         STAMVISIBILITY_ALWAYS,
+                                         STAMUNIT_OCCURENCES,
+                                         "Processed I/O operations per second",
+                                         "/PDM/AsyncCompletion/File/%s/IoOpsPerSec",
+                                         RTPathFilename(pEndpoint->pszUri));
+                }
+
+                if (RT_SUCCESS(rc))
+                {
+                    rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsStarted, STAMTYPE_COUNTER,
+                                         STAMVISIBILITY_ALWAYS,
+                                         STAMUNIT_OCCURENCES,
+                                         "Started I/O operations for this endpoint",
+                                         "/PDM/AsyncCompletion/File/%s/IoOpsStarted",
+                                         RTPathFilename(pEndpoint->pszUri));
+                }
+
+                if (RT_SUCCESS(rc))
+                {
+                    rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsCompleted, STAMTYPE_COUNTER,
+                                         STAMVISIBILITY_ALWAYS,
+                                         STAMUNIT_OCCURENCES,
+                                         "Completed I/O operations for this endpoint",
+                                         "/PDM/AsyncCompletion/File/%s/IoOpsCompleted",
+                                         RTPathFilename(pEndpoint->pszUri));
+                }
+                /** @todo why bother maintaing rc when it's just ignored /
+                          logged and not returned? */
+
+                pEndpoint->tsIntervalStartMs = RTTimeMilliTS();
 #endif
 
-                    *ppEndpoint = pEndpoint;
-
-                    LogFlowFunc((": Created endpoint for %s: rc=%Rrc\n", pszFilename, rc));
-                    return VINF_SUCCESS;
-                }
-                RTStrFree(pEndpoint->pszUri);
+                *ppEndpoint = pEndpoint;
+
+                LogFlowFunc((": Created endpoint for %s: rc=%Rrc\n", pszFilename, rc));
+                return VINF_SUCCESS;
             }
-            MMR3HeapFree(pEndpoint);
+            RTStrFree(pEndpoint->pszUri);
         }
-    }
-    else
-    {
-        /* Endpoint found. */
-        pEndpoint->cUsers++;
-
-        *ppEndpoint = pEndpoint;
-        return VINF_SUCCESS;
+        MMR3HeapFree(pEndpoint);
     }
 
     LogFlowFunc((": Creation of endpoint for %s failed: rc=%Rrc\n", pszFilename, rc));
-
     return rc;
 }
@@ -1418,6 +1321,6 @@
         for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs); i++)
             STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesNs[i]);
-        for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMicroSec); i++)
-            STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesMicroSec[i]);
+        for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesUs); i++)
+            STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesUs[i]);
         for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
             STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesMs[i]);
@@ -1441,9 +1344,7 @@
                                           PPPDMASYNCCOMPLETIONTASK ppTask)
 {
-    int rc = VINF_SUCCESS;
-
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(paSegments), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(ppTask), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
+    AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
     AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
     AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
@@ -1456,10 +1357,8 @@
         return VERR_NO_MEMORY;
 
-    rc = pEndpoint->pEpClass->pEndpointOps->pfnEpRead(pTask, pEndpoint, off,
-                                                      paSegments, cSegments, cbRead);
+    int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpRead(pTask, pEndpoint, off,
+                                                          paSegments, cSegments, cbRead);
     if (RT_SUCCESS(rc))
-    {
         *ppTask = pTask;
-    }
     else
         pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
@@ -1473,9 +1372,7 @@
                                            PPPDMASYNCCOMPLETIONTASK ppTask)
 {
-    int rc = VINF_SUCCESS;
-
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(paSegments), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(ppTask), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
+    AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
     AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
     AssertReturn(cbWrite > 0, VERR_INVALID_PARAMETER);
@@ -1488,6 +1385,6 @@
         return VERR_NO_MEMORY;
 
-    rc = pEndpoint->pEpClass->pEndpointOps->pfnEpWrite(pTask, pEndpoint, off,
-                                                       paSegments, cSegments, cbWrite);
+    int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpWrite(pTask, pEndpoint, off,
+                                                           paSegments, cSegments, cbWrite);
     if (RT_SUCCESS(rc))
     {
@@ -1504,8 +1401,6 @@
                                            PPPDMASYNCCOMPLETIONTASK ppTask)
 {
-    int rc = VINF_SUCCESS;
-
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(ppTask), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
+    AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
 
     PPDMASYNCCOMPLETIONTASK pTask;
@@ -1515,9 +1410,7 @@
         return VERR_NO_MEMORY;
 
-    rc = pEndpoint->pEpClass->pEndpointOps->pfnEpFlush(pTask, pEndpoint);
+    int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpFlush(pTask, pEndpoint);
     if (RT_SUCCESS(rc))
-    {
         *ppTask = pTask;
-    }
     else
         pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
@@ -1529,11 +1422,10 @@
                                              uint64_t *pcbSize)
 {
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
-    AssertReturn(VALID_PTR(pcbSize), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
+    AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
 
     if (pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize)
         return pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize(pEndpoint, pcbSize);
-    else
-        return VERR_NOT_SUPPORTED;
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -1541,10 +1433,9 @@
                                              uint64_t cbSize)
 {
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
 
     if (pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize)
         return pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize(pEndpoint, cbSize);
-    else
-        return VERR_NOT_SUPPORTED;
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -1552,13 +1443,12 @@
                                               const char *pcszBwMgr)
 {
-    int rc = VINF_SUCCESS;
-    AssertReturn(VALID_PTR(pEndpoint), VERR_INVALID_POINTER);
+    AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
     PPDMACBWMGR pBwMgrOld = NULL;
     PPDMACBWMGR pBwMgrNew = NULL;
 
+    int rc = VINF_SUCCESS;
     if (pcszBwMgr)
     {
         pBwMgrNew = pdmacBwMgrFindById(pEndpoint->pEpClass, pcszBwMgr);
-
         if (pBwMgrNew)
             pdmacBwMgrRef(pBwMgrNew);
@@ -1570,5 +1460,4 @@
     {
         pBwMgrOld = ASMAtomicXchgPtrT(&pEndpoint->pBwMgr, pBwMgrNew, PPDMACBWMGR);
-
         if (pBwMgrOld)
             pdmacBwMgrUnref(pBwMgrOld);
@@ -1585,12 +1474,10 @@
 VMMR3DECL(int) PDMR3AsyncCompletionBwMgrSetMaxForFile(PVM pVM, const char *pcszBwMgr, uint32_t cbMaxNew)
 {
-    int rc = VINF_SUCCESS;
     AssertPtrReturn(pVM, VERR_INVALID_POINTER);
     AssertPtrReturn(pcszBwMgr, VERR_INVALID_POINTER);
-    PPDMACBWMGR pBwMgr;
-
-    PPDMASYNCCOMPLETIONEPCLASS pEpClass = pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
-
-    pBwMgr = pdmacBwMgrFindById(pEpClass, pcszBwMgr);
+
+    int                         rc       = VINF_SUCCESS;
+    PPDMASYNCCOMPLETIONEPCLASS  pEpClass = pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
+    PPDMACBWMGR                 pBwMgr   = pdmacBwMgrFindById(pEpClass, pcszBwMgr);
     if (pBwMgr)
     {
Index: /trunk/src/VBox/VMM/include/PDMAsyncCompletionInternal.h
===================================================================
--- /trunk/src/VBox/VMM/include/PDMAsyncCompletionInternal.h	(revision 39013)
+++ /trunk/src/VBox/VMM/include/PDMAsyncCompletionInternal.h	(revision 39014)
@@ -211,5 +211,5 @@
     uint32_t                                    u32Alignment;
     STAMCOUNTER                                 StatTaskRunTimesNs[10];
-    STAMCOUNTER                                 StatTaskRunTimesMicroSec[10];
+    STAMCOUNTER                                 StatTaskRunTimesUs[10];
     STAMCOUNTER                                 StatTaskRunTimesMs[10];
     STAMCOUNTER                                 StatTaskRunTimesSec[10];
