Index: /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp
===================================================================
--- /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp	(revision 71628)
+++ /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp	(revision 71629)
@@ -1,6 +1,6 @@
 /* $Id$ */
 /** @file
- *
- * VBox Host Guest Shared Memory Interface (HGSMI).
+ * VBox Host Guest Shared Memory Interface (HGSMI), host part.
+ *
  * Host part:
  *  - virtual hardware IO handlers;
@@ -86,37 +86,35 @@
 
 #ifdef VBOXHGSMI_STATE_DEBUG
-#define VBOXHGSMI_STATE_START_MAGIC 0x12345678
-#define VBOXHGSMI_STATE_STOP_MAGIC  0x87654321
-#define VBOXHGSMI_STATE_FIFOSTART_MAGIC 0x9abcdef1
-#define VBOXHGSMI_STATE_FIFOSTOP_MAGIC 0x1fedcba9
-
-#define VBOXHGSMI_SAVE_START(_pSSM)     do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_START_MAGIC);     AssertRC(rc2); }while(0)
-#define VBOXHGSMI_SAVE_STOP(_pSSM)      do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_STOP_MAGIC);      AssertRC(rc2); }while(0)
-#define VBOXHGSMI_SAVE_FIFOSTART(_pSSM) do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_FIFOSTART_MAGIC); AssertRC(rc2); }while(0)
-#define VBOXHGSMI_SAVE_FIFOSTOP(_pSSM)  do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_FIFOSTOP_MAGIC);  AssertRC(rc2); }while(0)
-
-#define VBOXHGSMI_LOAD_CHECK(_pSSM, _v) \
-    do{ \
+# define VBOXHGSMI_STATE_START_MAGIC     UINT32_C(0x12345678)
+# define VBOXHGSMI_STATE_STOP_MAGIC      UINT32_C(0x87654321)
+# define VBOXHGSMI_STATE_FIFOSTART_MAGIC UINT32_C(0x9abcdef1)
+# define VBOXHGSMI_STATE_FIFOSTOP_MAGIC  UINT32_C(0x1fedcba9)
+
+# define VBOXHGSMI_SAVE_START(_pSSM)     do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_START_MAGIC);     AssertRC(rc2); }while(0)
+# define VBOXHGSMI_SAVE_STOP(_pSSM)      do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_STOP_MAGIC);      AssertRC(rc2); }while(0)
+# define VBOXHGSMI_SAVE_FIFOSTART(_pSSM) do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_FIFOSTART_MAGIC); AssertRC(rc2); }while(0)
+# define VBOXHGSMI_SAVE_FIFOSTOP(_pSSM)  do{ int rc2 = SSMR3PutU32(_pSSM, VBOXHGSMI_STATE_FIFOSTOP_MAGIC);  AssertRC(rc2); }while(0)
+
+# define VBOXHGSMI_LOAD_CHECK(_pSSM, _v) \
+    do { \
         uint32_t u32; \
         int rc2 = SSMR3GetU32(_pSSM, &u32); AssertRC(rc2); \
         Assert(u32 == (_v)); \
-    }while(0)
-
-#define VBOXHGSMI_LOAD_START(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_START_MAGIC)
-#define VBOXHGSMI_LOAD_FIFOSTART(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_FIFOSTART_MAGIC)
-#define VBOXHGSMI_LOAD_FIFOSTOP(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_FIFOSTOP_MAGIC)
-#define VBOXHGSMI_LOAD_STOP(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_STOP_MAGIC)
-#else
-#define VBOXHGSMI_SAVE_START(_pSSM) do{ }while(0)
-#define VBOXHGSMI_SAVE_STOP(_pSSM) do{ }while(0)
-#define VBOXHGSMI_SAVE_FIFOSTART(_pSSM) do{ }while(0)
-#define VBOXHGSMI_SAVE_FIFOSTOP(_pSSM) do{ }while(0)
-
-
-#define VBOXHGSMI_LOAD_START(_pSSM) do{ }while(0)
-#define VBOXHGSMI_LOAD_FIFOSTART(_pSSM) do{ }while(0)
-#define VBOXHGSMI_LOAD_FIFOSTOP(_pSSM) do{ }while(0)
-#define VBOXHGSMI_LOAD_STOP(_pSSM) do{ }while(0)
-
+    } while(0)
+
+# define VBOXHGSMI_LOAD_START(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_START_MAGIC)
+# define VBOXHGSMI_LOAD_FIFOSTART(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_FIFOSTART_MAGIC)
+# define VBOXHGSMI_LOAD_FIFOSTOP(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_FIFOSTOP_MAGIC)
+# define VBOXHGSMI_LOAD_STOP(_pSSM) VBOXHGSMI_LOAD_CHECK(_pSSM, VBOXHGSMI_STATE_STOP_MAGIC)
+#else   /* !VBOXHGSMI_STATE_DEBUG */
+# define VBOXHGSMI_SAVE_START(a_pSSM)       do { } while(0)
+# define VBOXHGSMI_SAVE_STOP(a_pSSM)        do { } while(0)
+# define VBOXHGSMI_SAVE_FIFOSTART(a_pSSM)   do { } while(0)
+# define VBOXHGSMI_SAVE_FIFOSTOP(a_pSSM)    do { } while(0)
+
+# define VBOXHGSMI_LOAD_START(a_pSSM)       do { } while(0)
+# define VBOXHGSMI_LOAD_FIFOSTART(a_pSSM)   do { } while(0)
+# define VBOXHGSMI_LOAD_FIFOSTOP(a_pSSM)    do { } while(0)
+# define VBOXHGSMI_LOAD_STOP(a_pSSM)        do { } while(0)
 #endif
 
@@ -125,32 +123,34 @@
  */
 #ifdef HGSMI_STRICT
-#define HGSMI_STRICT_ASSERT_FAILED() AssertFailed()
-#define HGSMI_STRICT_ASSERT(expr) Assert(expr)
+# define HGSMI_STRICT_ASSERT_FAILED()   AssertFailed()
+# define HGSMI_STRICT_ASSERT(expr)      Assert(expr)
 #else
-#define HGSMI_STRICT_ASSERT_FAILED() do {} while (0)
-#define HGSMI_STRICT_ASSERT(expr) do {} while (0)
-#endif /* !HGSMI_STRICT */
-
-
-/* Host heap types. */
-#define HGSMI_HEAP_TYPE_NULL    0 /* Heap not initialized. */
-#define HGSMI_HEAP_TYPE_POINTER 1 /* Deprecated, used only for old saved states. RTHEAPSIMPLE. */
-#define HGSMI_HEAP_TYPE_OFFSET  2 /* Deprecated, used only for old saved states. RTHEAPOFFSET. */
-#define HGSMI_HEAP_TYPE_MA      3 /* Memory allocator. */
+# define HGSMI_STRICT_ASSERT_FAILED()   do {} while (0)
+# define HGSMI_STRICT_ASSERT(expr)      do {} while (0)
+#endif
+
+
+/** @name Host heap types.
+ *  @{ */
+#define HGSMI_HEAP_TYPE_NULL    0 /**< Heap not initialized. */
+#define HGSMI_HEAP_TYPE_POINTER 1 /**< Deprecated, used only for old saved states. RTHEAPSIMPLE. */
+#define HGSMI_HEAP_TYPE_OFFSET  2 /**< Deprecated, used only for old saved states. RTHEAPOFFSET. */
+#define HGSMI_HEAP_TYPE_MA      3 /**< Memory allocator. */
+/** @} */
 
 typedef struct HGSMIHOSTHEAP
 {
-    uint32_t u32HeapType;   /* HGSMI_HEAP_TYPE_* */
-    int32_t volatile cRefs; /* How many blocks allocated. */
-    HGSMIAREA area;         /* Host heap location. */
+    uint32_t u32HeapType;   /**< HGSMI_HEAP_TYPE_* */
+    int32_t volatile cRefs; /**< How many blocks allocated. */
+    HGSMIAREA area;         /**< Host heap location. */
     union
     {
-        HGSMIMADATA ma;     /* Memory allocator for the default host heap implementation. */
-        struct              /* Legacy heap implementations. For old saved states. */
+        HGSMIMADATA ma;     /**< Memory allocator for the default host heap implementation. */
+        struct              /**< Legacy heap implementations. For old saved states. */
         {
             union
             {
-                RTHEAPSIMPLE hPtr;  /* Pointer based heap. */
-                RTHEAPOFFSET hOff;  /* Offset based heap. */
+                RTHEAPSIMPLE hPtr;  /**< Pointer based heap. */
+                RTHEAPOFFSET hOff;  /**< Offset based heap. */
             } u;
         } legacy;
@@ -160,29 +160,29 @@
 typedef struct HGSMIINSTANCE
 {
-    PVM pVM;                           /* The VM. */
-
-    const char *pszName;               /* A name for the instance. Mostyl used in the log. */
-
-    RTCRITSECT   instanceCritSect;     /* For updating the instance data: FIFO's, channels. */
-
-    HGSMIAREA area; /* The shared memory description. */
-    HGSMIHOSTHEAP hostHeap;            /* Host heap instance. */
-    RTCRITSECT    hostHeapCritSect;    /* Heap serialization lock. */
-
-    RTLISTANCHOR hostFIFO;             /* Pending host buffers. */
-    RTLISTANCHOR hostFIFORead;         /* Host buffers read by the guest. */
-    RTLISTANCHOR hostFIFOProcessed;    /* Processed by the guest. */
-    RTLISTANCHOR hostFIFOFree;         /* Buffers for reuse. */
+    PVM pVM;                           /**< The VM. */
+
+    const char *pszName;               /**< A name for the instance. Mostyl used in the log. */
+
+    RTCRITSECT   instanceCritSect;     /**< For updating the instance data: FIFO's, channels. */
+
+    HGSMIAREA     area;                /**< The shared memory description. */
+    HGSMIHOSTHEAP hostHeap;            /**< Host heap instance. */
+    RTCRITSECT    hostHeapCritSect;    /**< Heap serialization lock. */
+
+    RTLISTANCHOR hostFIFO;             /**< Pending host buffers. */
+    RTLISTANCHOR hostFIFORead;         /**< Host buffers read by the guest. */
+    RTLISTANCHOR hostFIFOProcessed;    /**< Processed by the guest. */
+    RTLISTANCHOR hostFIFOFree;         /**< Buffers for reuse. */
 #ifdef VBOX_WITH_WDDM
-    RTLISTANCHOR guestCmdCompleted;    /* list of completed guest commands to be returned to the guest*/
+    RTLISTANCHOR guestCmdCompleted;    /**< list of completed guest commands to be returned to the guest*/
 #endif
-    RTCRITSECT hostFIFOCritSect;       /* FIFO serialization lock. */
-
-    PFNHGSMINOTIFYGUEST pfnNotifyGuest; /* Guest notification callback. */
-    void *pvNotifyGuest;                /* Guest notification callback context. */
+    RTCRITSECT hostFIFOCritSect;       /**< FIFO serialization lock. */
+
+    PFNHGSMINOTIFYGUEST pfnNotifyGuest; /**< Guest notification callback. */
+    void *pvNotifyGuest;                /**< Guest notification callback context. */
 
     volatile HGSMIHOSTFLAGS *pHGFlags;
 
-    HGSMICHANNELINFO channelInfo;      /* Channel handlers indexed by the channel id.
+    HGSMICHANNELINFO channelInfo;      /**< Channel handlers indexed by the channel id.
                                         * The array is accessed under the instance lock.
                                         */
@@ -197,9 +197,9 @@
     RTLISTNODE nodeEntry;
 
-    HGSMIINSTANCE *pIns;               /* Backlink to the HGSMI instance. */
-
-    volatile uint32_t fl;              /* Status flags of the entry. */
-
-    HGSMIOFFSET offBuffer;             /* Offset of the HGSMI buffer header in the HGSMI host heap:
+    HGSMIINSTANCE *pIns;               /**< Backlink to the HGSMI instance. */
+
+    volatile uint32_t fl;              /**< Status flags of the entry. */
+
+    HGSMIOFFSET offBuffer;             /**< Offset of the HGSMI buffer header in the HGSMI host heap:
                                         * [pIns->hostHeap.area.offBase .. offLast]. */
 } HGSMIHOSTFIFOENTRY;
@@ -213,5 +213,5 @@
 #define HGSMI_F_HOST_FIFO_CANCELED  0x0020
 
-static DECLCALLBACK(void) hgsmiHostCommandFreeCallback (void *pvCallback);
+static DECLCALLBACK(void) hgsmiHostCommandFreeCallback(void *pvCallback);
 
 #ifdef VBOX_WITH_WDDM
@@ -220,9 +220,9 @@
 {
     RTLISTNODE nodeEntry;
-    HGSMIOFFSET offBuffer; /* Offset of the guest command buffer. */
+    HGSMIOFFSET offBuffer; /**< Offset of the guest command buffer. */
 } HGSMIGUESTCOMPLENTRY;
 
 
-static void hgsmiGuestCompletionFIFOFree (HGSMIINSTANCE *pIns, HGSMIGUESTCOMPLENTRY *pEntry)
+static void hgsmiGuestCompletionFIFOFree(HGSMIINSTANCE *pIns, HGSMIGUESTCOMPLENTRY *pEntry)
 {
     NOREF (pIns);
@@ -230,46 +230,42 @@
 }
 
-static int hgsmiGuestCompletionFIFOAlloc (HGSMIINSTANCE *pIns, HGSMIGUESTCOMPLENTRY **ppEntry)
-{
-    int rc = VINF_SUCCESS;
-
-    NOREF (pIns);
-
-    HGSMIGUESTCOMPLENTRY *pEntry = (HGSMIGUESTCOMPLENTRY *)RTMemAllocZ (sizeof (HGSMIGUESTCOMPLENTRY));
-
+static int hgsmiGuestCompletionFIFOAlloc(HGSMIINSTANCE *pIns, HGSMIGUESTCOMPLENTRY **ppEntry)
+{
+    HGSMIGUESTCOMPLENTRY *pEntry = (HGSMIGUESTCOMPLENTRY *)RTMemAllocZ(sizeof(HGSMIGUESTCOMPLENTRY));
     if (pEntry)
+    {
         *ppEntry = pEntry;
-    else
-        rc = VERR_NO_MEMORY;
-
-    return rc;
-}
-
-#endif
-
-static int hgsmiLock (HGSMIINSTANCE *pIns)
-{
-    int rc = RTCritSectEnter (&pIns->instanceCritSect);
-    AssertRC (rc);
-    return rc;
-}
-
-static void hgsmiUnlock (HGSMIINSTANCE *pIns)
-{
-    int rc = RTCritSectLeave (&pIns->instanceCritSect);
-    AssertRC (rc);
-}
-
-static int hgsmiFIFOLock (HGSMIINSTANCE *pIns)
-{
-    int rc = RTCritSectEnter (&pIns->hostFIFOCritSect);
-    AssertRC (rc);
-    return rc;
-}
-
-static void hgsmiFIFOUnlock (HGSMIINSTANCE *pIns)
-{
-    int rc = RTCritSectLeave (&pIns->hostFIFOCritSect);
-    AssertRC (rc);
+        return VINF_SUCCESS;
+    }
+    NOREF(pIns);
+    return VERR_NO_MEMORY;
+}
+
+#endif /* VBOX_WITH_WDDM */
+
+static int hgsmiLock(HGSMIINSTANCE *pIns)
+{
+    int rc = RTCritSectEnter(&pIns->instanceCritSect);
+    AssertRC(rc);
+    return rc;
+}
+
+static void hgsmiUnlock(HGSMIINSTANCE *pIns)
+{
+    int rc = RTCritSectLeave(&pIns->instanceCritSect);
+    AssertRC(rc);
+}
+
+static int hgsmiFIFOLock(HGSMIINSTANCE *pIns)
+{
+    int rc = RTCritSectEnter(&pIns->hostFIFOCritSect);
+    AssertRC(rc);
+    return rc;
+}
+
+static void hgsmiFIFOUnlock(HGSMIINSTANCE *pIns)
+{
+    int rc = RTCritSectLeave(&pIns->hostFIFOCritSect);
+    AssertRC(rc);
 }
 
@@ -282,8 +278,7 @@
  * @thread EMT
  */
-void HGSMIGuestWrite (PHGSMIINSTANCE pIns,
-                      HGSMIOFFSET offBuffer)
-{
-    HGSMIBufferProcess (&pIns->area, &pIns->channelInfo, offBuffer);
+void HGSMIGuestWrite(PHGSMIINSTANCE pIns, HGSMIOFFSET offBuffer)
+{
+    HGSMIBufferProcess(&pIns->area, &pIns->channelInfo, offBuffer);
 }
 
@@ -325,5 +320,5 @@
 
 /* Called from HGSMI_IO_GUEST read handler. */
-HGSMIOFFSET HGSMIGuestRead (PHGSMIINSTANCE pIns)
+HGSMIOFFSET HGSMIGuestRead(PHGSMIINSTANCE pIns)
 {
     LogFlowFunc(("pIns %p\n", pIns));
@@ -346,7 +341,5 @@
 }
 
-static bool hgsmiProcessHostCmdCompletion(HGSMIINSTANCE *pIns,
-                                          HGSMIOFFSET offBuffer,
-                                          bool bCompleteFirst)
+static bool hgsmiProcessHostCmdCompletion(HGSMIINSTANCE *pIns, HGSMIOFFSET offBuffer, bool fCompleteFirst)
 {
     VM_ASSERT_EMT(pIns->pVM);
@@ -362,5 +355,5 @@
         {
             Assert(pIter->fl == (HGSMI_F_HOST_FIFO_ALLOCATED | HGSMI_F_HOST_FIFO_READ));
-            if (bCompleteFirst || pIter->offBuffer == offBuffer)
+            if (fCompleteFirst || pIter->offBuffer == offBuffer)
             {
                 pEntry = pIter;
@@ -371,5 +364,5 @@
         LogFlowFunc(("read list entry: %p.\n", pEntry));
 
-        Assert(pEntry || bCompleteFirst);
+        Assert(pEntry || fCompleteFirst);
 
         if (pEntry)
@@ -385,10 +378,9 @@
 
             hgsmiHostCommandFreeCallback(pEntry);
-
             return true;
         }
 
         hgsmiFIFOUnlock(pIns);
-        if (!bCompleteFirst)
+        if (!fCompleteFirst)
             LogRel(("HGSMI[%s]: ignored invalid write to the host FIFO: 0x%08X!!!\n", pIns->pszName, offBuffer));
     }
@@ -396,21 +388,26 @@
 }
 
-/* The guest has finished processing of a buffer previously submitted by the host.
+/**
+ * The guest has finished processing of a buffer previously submitted by the
+ * host.
+ *
  * Called from HGSMI_IO_HOST write handler.
  * @thread EMT
  */
-void HGSMIHostWrite (HGSMIINSTANCE *pIns,
-                     HGSMIOFFSET offBuffer)
+void HGSMIHostWrite(HGSMIINSTANCE *pIns, HGSMIOFFSET offBuffer)
 {
     LogFlowFunc(("pIns %p offBuffer 0x%x\n", pIns, offBuffer));
 
-    hgsmiProcessHostCmdCompletion (pIns, offBuffer, false);
-}
-
-/* The guest reads a new host buffer to be processed.
+    hgsmiProcessHostCmdCompletion(pIns, offBuffer, false);
+}
+
+/**
+ * The guest reads a new host buffer to be processed.
+ *
  * Called from the HGSMI_IO_HOST read handler.
+ *
  * @thread EMT
  */
-HGSMIOFFSET HGSMIHostRead (HGSMIINSTANCE *pIns)
+HGSMIOFFSET HGSMIHostRead(HGSMIINSTANCE *pIns)
 {
     LogFlowFunc(("pIns %p\n", pIns));
@@ -460,11 +457,9 @@
 
 
-/* Tells the guest that a new buffer to be processed is available from the host. */
-static void hgsmiNotifyGuest (HGSMIINSTANCE *pIns)
+/** Tells the guest that a new buffer to be processed is available from the host. */
+static void hgsmiNotifyGuest(HGSMIINSTANCE *pIns)
 {
     if (pIns->pfnNotifyGuest)
-    {
-        pIns->pfnNotifyGuest (pIns->pvNotifyGuest);
-    }
+        pIns->pfnNotifyGuest(pIns->pvNotifyGuest);
 }
 
@@ -477,5 +472,5 @@
 uint32_t HGSMIGetHostGuestFlags(HGSMIINSTANCE *pIns)
 {
-    return pIns->pHGFlags? ASMAtomicReadU32(&pIns->pHGFlags->u32HostFlags) : 0;
+    return pIns->pHGFlags ? ASMAtomicReadU32(&pIns->pHGFlags->u32HostFlags) : 0;
 }
 
@@ -483,6 +478,7 @@
 {
     AssertPtrReturnVoid(pIns->pHGFlags);
-    ASMAtomicAndU32(&pIns->pHGFlags->u32HostFlags, (~flags));
-}
+    ASMAtomicAndU32(&pIns->pHGFlags->u32HostFlags, ~flags);
+}
+
 
 /*
@@ -492,8 +488,9 @@
  *
  */
+
 static int hgsmiHostHeapLock(HGSMIINSTANCE *pIns)
 {
     int rc = RTCritSectEnter(&pIns->hostHeapCritSect);
-    AssertRC (rc);
+    AssertRC(rc);
     return rc;
 }
@@ -502,5 +499,5 @@
 {
     int rc = RTCritSectLeave(&pIns->hostHeapCritSect);
-    AssertRC (rc);
+    AssertRC(rc);
 }
 
@@ -570,15 +567,9 @@
     HGSMIOFFSET offHeapHandle;
     if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_POINTER)
-    {
         offHeapHandle = (HGSMIOFFSET)((uintptr_t)pHeap->u.legacy.u.hPtr - (uintptr_t)pHeap->area.pu8Base);
-    }
     else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
-    {
         offHeapHandle = (HGSMIOFFSET)((uintptr_t)pHeap->u.legacy.u.hOff - (uintptr_t)pHeap->area.pu8Base);
-    }
     else
-    {
         offHeapHandle = HGSMIOFFSET_VOID;
-    }
     return offHeapHandle;
 }
@@ -596,7 +587,5 @@
     {
         if (u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
-        {
             pHeap->u.legacy.u.hOff = (RTHEAPOFFSET)((uint8_t *)pvBase + offHeapHandle);
-        }
         else if (u32HeapType == HGSMI_HEAP_TYPE_POINTER)
         {
@@ -611,11 +600,7 @@
 
         if (RT_SUCCESS(rc))
-        {
             pHeap->u32HeapType = u32HeapType;
-        }
         else
-        {
             HGSMIAreaClear(&pHeap->area);
-        }
     }
 
@@ -636,13 +621,8 @@
     {
         rc = HGSMIMAInit(&pHeap->u.ma, &pHeap->area, paDescriptors, cBlocks, cbMaxBlock, pEnv);
-
         if (RT_SUCCESS(rc))
-        {
             pHeap->u32HeapType = HGSMI_HEAP_TYPE_MA;
-        }
         else
-        {
             HGSMIAreaClear(&pHeap->area);
-        }
     }
 
@@ -659,39 +639,23 @@
 {
     if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_MA)
-    {
         HGSMIMAUninit(&pHeap->u.ma);
-    }
     hgsmiHostHeapSetupUninitialized(pHeap);
 }
 
-static int hgsmiHostFIFOAlloc (HGSMIINSTANCE *pIns, HGSMIHOSTFIFOENTRY **ppEntry)
-{
-    int rc = VINF_SUCCESS;
-
-    NOREF (pIns);
-
-    HGSMIHOSTFIFOENTRY *pEntry = (HGSMIHOSTFIFOENTRY *)RTMemAllocZ (sizeof (HGSMIHOSTFIFOENTRY));
-
+static int hgsmiHostFIFOAlloc(HGSMIHOSTFIFOENTRY **ppEntry)
+{
+    HGSMIHOSTFIFOENTRY *pEntry = (HGSMIHOSTFIFOENTRY *)RTMemAllocZ(sizeof(HGSMIHOSTFIFOENTRY));
     if (pEntry)
     {
         pEntry->fl = HGSMI_F_HOST_FIFO_ALLOCATED;
-    }
-    else
-    {
-        rc = VERR_NO_MEMORY;
-    }
-
-    if (RT_SUCCESS (rc))
-    {
         *ppEntry = pEntry;
-    }
-
-    return rc;
-}
-
-static void hgsmiHostFIFOFree (HGSMIINSTANCE *pIns, HGSMIHOSTFIFOENTRY *pEntry)
-{
-    NOREF (pIns);
-    RTMemFree (pEntry);
+        return VINF_SUCCESS;
+    }
+    return VERR_NO_MEMORY;
+}
+
+static void hgsmiHostFIFOFree(HGSMIHOSTFIFOENTRY *pEntry)
+{
+    RTMemFree(pEntry);
 }
 
@@ -701,13 +665,13 @@
 
     HGSMIINSTANCE *pIns = pEntry->pIns;
-    int rc = hgsmiFIFOLock (pIns);
+    int rc = hgsmiFIFOLock(pIns);
     if (RT_SUCCESS(rc))
     {
         RTListNodeRemove(&pEntry->nodeEntry);
-        hgsmiFIFOUnlock (pIns);
+        hgsmiFIFOUnlock(pIns);
 
         void RT_UNTRUSTED_VOLATILE_GUEST *pvData = HGSMIBufferDataFromOffset(&pIns->hostHeap.area, pEntry->offBuffer);
 
-        rc = hgsmiHostHeapLock (pIns);
+        rc = hgsmiHostHeapLock(pIns);
         if (RT_SUCCESS(rc))
         {
@@ -718,5 +682,5 @@
         }
 
-        hgsmiHostFIFOFree (pIns, pEntry);
+        hgsmiHostFIFOFree(pEntry);
     }
 
@@ -747,16 +711,12 @@
 
         if (pEntry)
-        {
             RTListNodeRemove(&pEntry->nodeEntry);
-        }
         else
-        {
             AssertLogRelMsgFailed(("HGSMI[%s]: the host frees unprocessed FIFO entry: 0x%08X\n",
                                    pIns->pszName, offBuffer));
-        }
-
-        hgsmiFIFOUnlock (pIns);
-
-        rc = hgsmiHostHeapLock (pIns);
+
+        hgsmiFIFOUnlock(pIns);
+
+        rc = hgsmiHostHeapLock(pIns);
         if (RT_SUCCESS(rc))
         {
@@ -770,12 +730,12 @@
         {
             /* Deallocate the entry. */
-            hgsmiHostFIFOFree(pIns, pEntry);
-        }
-    }
-
-    return rc;
-}
-
-static DECLCALLBACK(void) hgsmiHostCommandFreeCallback (void *pvCallback)
+            hgsmiHostFIFOFree(pEntry);
+        }
+    }
+
+    return rc;
+}
+
+static DECLCALLBACK(void) hgsmiHostCommandFreeCallback(void *pvCallback)
 {
     /* Guest has processed the command. */
@@ -785,15 +745,13 @@
 
     /* This is a simple callback, just signal the event. */
-    hgsmiHostCommandFreeByEntry (pEntry);
-}
-
-static int hgsmiHostCommandWrite(HGSMIINSTANCE *pIns,
-                                 HGSMIOFFSET offBuffer)
+    hgsmiHostCommandFreeByEntry(pEntry);
+}
+
+static int hgsmiHostCommandWrite(HGSMIINSTANCE *pIns, HGSMIOFFSET offBuffer)
 {
     AssertPtrReturn(pIns->pHGFlags, VERR_WRONG_ORDER);
 
     HGSMIHOSTFIFOENTRY *pEntry;
-    int rc = hgsmiHostFIFOAlloc(pIns, &pEntry);
-
+    int rc = hgsmiHostFIFOAlloc(&pEntry);
     if (RT_SUCCESS(rc))
     {
@@ -813,7 +771,5 @@
         }
         else
-        {
-            hgsmiHostFIFOFree(pIns, pEntry);
-        }
+            hgsmiHostFIFOFree(pEntry);
     }
 
@@ -874,7 +830,5 @@
 
         if (pvData)
-        {
             *ppvData = pvData;
-        }
         else
         {
@@ -978,5 +932,4 @@
     RT_UNTRUSTED_VALIDATED_FENCE();
 
-
     /*
      * Lock the heap and do the job.
@@ -1026,7 +979,5 @@
             rc = SSMR3PutU32(pSSM, pIter->offBuffer);
             if (RT_FAILURE(rc))
-            {
                 break;
-            }
         }
     }
@@ -1055,7 +1006,5 @@
             rc = SSMR3PutU32(pSSM, pIter->offBuffer);
             if (RT_FAILURE(rc))
-            {
                 break;
-            }
         }
     }
@@ -1066,19 +1015,19 @@
 }
 
-static int hgsmiHostLoadFifoEntryLocked (PHGSMIINSTANCE pIns, HGSMIHOSTFIFOENTRY **ppEntry, PSSMHANDLE pSSM)
+static int hgsmiHostLoadFifoEntryLocked(PHGSMIINSTANCE pIns, HGSMIHOSTFIFOENTRY **ppEntry, PSSMHANDLE pSSM)
 {
     HGSMIHOSTFIFOENTRY *pEntry;
-    int rc = hgsmiHostFIFOAlloc (pIns, &pEntry);  AssertRC(rc);
-    if (RT_SUCCESS (rc))
+    int rc = hgsmiHostFIFOAlloc(&pEntry);  AssertRC(rc);
+    if (RT_SUCCESS(rc))
     {
         uint32_t u32;
         pEntry->pIns = pIns;
-        rc = SSMR3GetU32 (pSSM, &u32); AssertRC(rc);
+        rc = SSMR3GetU32(pSSM, &u32); AssertRC(rc);
         pEntry->fl = u32;
-        rc = SSMR3GetU32 (pSSM, &pEntry->offBuffer); AssertRC(rc);
-        if (RT_SUCCESS (rc))
+        rc = SSMR3GetU32(pSSM, &pEntry->offBuffer); AssertRC(rc);
+        if (RT_SUCCESS(rc))
             *ppEntry = pEntry;
         else
-            hgsmiHostFIFOFree (pIns, pEntry);
+            hgsmiHostFIFOFree(pEntry);
     }
 
@@ -1113,12 +1062,12 @@
 {
     HGSMIGUESTCOMPLENTRY *pEntry;
-    int rc = hgsmiGuestCompletionFIFOAlloc (pIns, &pEntry); AssertRC(rc);
+    int rc = hgsmiGuestCompletionFIFOAlloc(pIns, &pEntry); AssertRC(rc);
     if (RT_SUCCESS (rc))
     {
-        rc = SSMR3GetU32 (pSSM, &pEntry->offBuffer); AssertRC(rc);
-        if (RT_SUCCESS (rc))
+        rc = SSMR3GetU32(pSSM, &pEntry->offBuffer); AssertRC(rc);
+        if (RT_SUCCESS(rc))
             *ppEntry = pEntry;
         else
-            hgsmiGuestCompletionFIFOFree (pIns, pEntry);
+            hgsmiGuestCompletionFIFOFree(pIns, pEntry);
     }
     return rc;
@@ -1158,5 +1107,5 @@
                 AssertRCBreak(rc);
 
-                hgsmiHostFIFOFree(pIns, pEntry);
+                hgsmiHostFIFOFree(pEntry);
             }
         }
@@ -1198,33 +1147,22 @@
                 uint32_t i;
                 for (i = 0; i < *pcBlocks; ++i)
-                {
                     SSMR3GetU32(pSSM, &paDescriptors[i]);
-                }
             }
             else
-            {
                 rc = VERR_NO_MEMORY;
-            }
         }
 
         if (RT_SUCCESS(rc))
-        {
             rc = SSMR3GetU32(pSSM, pcbMaxBlock);
-        }
-
         if (RT_SUCCESS(rc))
-        {
             *ppaDescriptors = paDescriptors;
-        }
         else
-        {
             RTMemFree(paDescriptors);
-        }
-    }
-
-    return rc;
-}
-
-int HGSMIHostSaveStateExec (PHGSMIINSTANCE pIns, PSSMHANDLE pSSM)
+    }
+
+    return rc;
+}
+
+int HGSMIHostSaveStateExec(PHGSMIINSTANCE pIns, PSSMHANDLE pSSM)
 {
     VBOXHGSMI_SAVE_START(pSSM);
@@ -1234,37 +1172,32 @@
     SSMR3PutU32(pSSM, pIns->hostHeap.u32HeapType);
 
-    HGSMIOFFSET off = pIns->pHGFlags ? HGSMIPointerToOffset(&pIns->area, (const HGSMIBUFFERHEADER *)pIns->pHGFlags) : HGSMIOFFSET_VOID;
-    SSMR3PutU32 (pSSM, off);
-
-    off = pIns->hostHeap.u32HeapType == HGSMI_HEAP_TYPE_MA?
-              0:
-              hgsmiHostHeapHandleLocationOffset(&pIns->hostHeap);
+    HGSMIOFFSET off = pIns->pHGFlags ? HGSMIPointerToOffset(&pIns->area, (const HGSMIBUFFERHEADER *)pIns->pHGFlags)
+                                     : HGSMIOFFSET_VOID;
+    SSMR3PutU32(pSSM, off);
+
+    off = pIns->hostHeap.u32HeapType == HGSMI_HEAP_TYPE_MA ? 0 : hgsmiHostHeapHandleLocationOffset(&pIns->hostHeap);
     rc = SSMR3PutU32 (pSSM, off);
     if (off != HGSMIOFFSET_VOID)
     {
-        SSMR3PutU32 (pSSM, hgsmiHostHeapOffset(&pIns->hostHeap));
-        SSMR3PutU32 (pSSM, hgsmiHostHeapSize(&pIns->hostHeap));
+        SSMR3PutU32(pSSM, hgsmiHostHeapOffset(&pIns->hostHeap));
+        SSMR3PutU32(pSSM, hgsmiHostHeapSize(&pIns->hostHeap));
         /* need save mem pointer to calculate offset on restore */
-        SSMR3PutU64 (pSSM, (uint64_t)(uintptr_t)pIns->area.pu8Base);
+        SSMR3PutU64(pSSM, (uint64_t)(uintptr_t)pIns->area.pu8Base);
         rc = hgsmiFIFOLock (pIns);
         if (RT_SUCCESS(rc))
         {
-            rc = hgsmiHostSaveFifoLocked (&pIns->hostFIFO, pSSM); AssertRC(rc);
-            rc = hgsmiHostSaveFifoLocked (&pIns->hostFIFORead, pSSM); AssertRC(rc);
-            rc = hgsmiHostSaveFifoLocked (&pIns->hostFIFOProcessed, pSSM); AssertRC(rc);
+            rc = hgsmiHostSaveFifoLocked(&pIns->hostFIFO, pSSM); AssertRC(rc);
+            rc = hgsmiHostSaveFifoLocked(&pIns->hostFIFORead, pSSM); AssertRC(rc);
+            rc = hgsmiHostSaveFifoLocked(&pIns->hostFIFOProcessed, pSSM); AssertRC(rc);
 #ifdef VBOX_WITH_WDDM
-            rc = hgsmiHostSaveGuestCmdCompletedFifoLocked (&pIns->guestCmdCompleted, pSSM); AssertRC(rc);
+            rc = hgsmiHostSaveGuestCmdCompletedFifoLocked(&pIns->guestCmdCompleted, pSSM); AssertRC(rc);
 #endif
 
-            hgsmiFIFOUnlock (pIns);
+            hgsmiFIFOUnlock(pIns);
         }
 
         if (RT_SUCCESS(rc))
-        {
             if (pIns->hostHeap.u32HeapType == HGSMI_HEAP_TYPE_MA)
-            {
                 rc = hgsmiHostSaveMA(pSSM, &pIns->hostHeap.u.ma);
-            }
-        }
     }
 
@@ -1274,5 +1207,5 @@
 }
 
-int HGSMIHostLoadStateExec (PHGSMIINSTANCE pIns, PSSMHANDLE pSSM, uint32_t u32Version)
+int HGSMIHostLoadStateExec(PHGSMIINSTANCE pIns, PSSMHANDLE pSSM, uint32_t u32Version)
 {
     if (u32Version < VGA_SAVEDSTATE_VERSION_HGSMI)
@@ -1282,7 +1215,5 @@
 
     int rc;
-    HGSMIOFFSET off;
     uint32_t u32HeapType = HGSMI_HEAP_TYPE_NULL;
-
     if (u32Version >= VGA_SAVEDSTATE_VERSION_HGSMIMA)
     {
@@ -1291,7 +1222,8 @@
     }
 
+    HGSMIOFFSET off;
     rc = SSMR3GetU32(pSSM, &off);
     AssertLogRelRCReturn(rc, rc);
-    pIns->pHGFlags = (off != HGSMIOFFSET_VOID) ? (HGSMIHOSTFLAGS*)HGSMIOffsetToPointer (&pIns->area, off) : NULL;
+    pIns->pHGFlags = off != HGSMIOFFSET_VOID ? (HGSMIHOSTFLAGS *)HGSMIOffsetToPointer(&pIns->area, off) : NULL;
 
     rc = SSMR3GetU32(pSSM, &off);
@@ -1301,9 +1233,6 @@
         /* There is a saved heap. */
         if (u32HeapType == HGSMI_HEAP_TYPE_NULL)
-        {
-            u32HeapType = u32Version > VGA_SAVEDSTATE_VERSION_HOST_HEAP?
-                              HGSMI_HEAP_TYPE_OFFSET:
-                              HGSMI_HEAP_TYPE_POINTER;
-        }
+            u32HeapType = u32Version > VGA_SAVEDSTATE_VERSION_HOST_HEAP
+                        ? HGSMI_HEAP_TYPE_OFFSET : HGSMI_HEAP_TYPE_POINTER;
 
         HGSMIOFFSET offHeap;
@@ -1317,18 +1246,18 @@
         if (RT_SUCCESS(rc))
         {
-            rc = hgsmiFIFOLock (pIns);
+            rc = hgsmiFIFOLock(pIns);
             if (RT_SUCCESS(rc))
             {
-                rc = hgsmiHostLoadFifoLocked (pIns, &pIns->hostFIFO, pSSM);
+                rc = hgsmiHostLoadFifoLocked(pIns, &pIns->hostFIFO, pSSM);
                 if (RT_SUCCESS(rc))
-                    rc = hgsmiHostLoadFifoLocked (pIns, &pIns->hostFIFORead, pSSM);
+                    rc = hgsmiHostLoadFifoLocked(pIns, &pIns->hostFIFORead, pSSM);
                 if (RT_SUCCESS(rc))
-                    rc = hgsmiHostLoadFifoLocked (pIns, &pIns->hostFIFOProcessed, pSSM);
+                    rc = hgsmiHostLoadFifoLocked(pIns, &pIns->hostFIFOProcessed, pSSM);
 #ifdef VBOX_WITH_WDDM
                 if (RT_SUCCESS(rc) && u32Version > VGA_SAVEDSTATE_VERSION_PRE_WDDM)
-                    rc = hgsmiHostLoadGuestCmdCompletedFifoLocked (pIns, &pIns->guestCmdCompleted, pSSM, u32Version);
+                    rc = hgsmiHostLoadGuestCmdCompletedFifoLocked(pIns, &pIns->guestCmdCompleted, pSSM, u32Version);
 #endif
 
-                hgsmiFIFOUnlock (pIns);
+                hgsmiFIFOUnlock(pIns);
             }
         }
@@ -1359,6 +1288,6 @@
                      || u32HeapType == HGSMI_HEAP_TYPE_POINTER)
             {
-                rc = hgsmiHostHeapLock (pIns);
-                if (RT_SUCCESS (rc))
+                rc = hgsmiHostHeapLock(pIns);
+                if (RT_SUCCESS(rc))
                 {
                     Assert(!pIns->hostHeap.cRefs);
@@ -1373,5 +1302,5 @@
                                                offHeap);
 
-                    hgsmiHostHeapUnlock (pIns);
+                    hgsmiHostHeapUnlock(pIns);
                 }
             }
@@ -1390,8 +1319,6 @@
 /* Register a new HGSMI channel by a predefined index.
  */
-int HGSMIHostChannelRegister(PHGSMIINSTANCE pIns,
-                             uint8_t u8Channel,
-                             PFNHGSMICHANNELHANDLER pfnChannelHandler,
-                             void *pvChannelHandler)
+int HGSMIHostChannelRegister(PHGSMIINSTANCE pIns, uint8_t u8Channel,
+                             PFNHGSMICHANNELHANDLER pfnChannelHandler, void *pvChannelHandler)
 {
     LogFlowFunc(("pIns %p, u8Channel %x, pfnChannelHandler %p, pvChannelHandler %p\n",
@@ -1412,5 +1339,4 @@
 
     LogFlowFunc(("leave rc = %Rrc\n", rc));
-
     return rc;
 }
@@ -1483,14 +1409,10 @@
 void RT_UNTRUSTED_VOLATILE_GUEST *HGSMIOffsetToPointerHost(PHGSMIINSTANCE pIns, HGSMIOFFSET offBuffer)
 {
-    const HGSMIAREA *pArea = &pIns->area;
-
-    if (   offBuffer < pArea->offBase
-        || offBuffer > pArea->offLast)
-    {
-        LogFunc(("offset 0x%x is outside the area [0x%x;0x%x]!!!\n", offBuffer, pArea->offBase, pArea->offLast));
-        return NULL;
-    }
-
-    return HGSMIOffsetToPointer(pArea, offBuffer);
+    const HGSMIAREA  *pArea   = &pIns->area;
+    HGSMIOFFSET const offArea = offBuffer - pArea->offBase;
+    ASSERT_GUEST_MSG_RETURN(offArea < pArea->cbArea,
+                            ("offBuffer=%#x; area %#x LB %#x\n", offBuffer, pArea->offBase, pArea->cbArea),
+                            NULL);
+    return &pArea->pu8Base[offArea];
 }
 
@@ -1498,11 +1420,10 @@
 HGSMIOFFSET HGSMIPointerToOffsetHost(PHGSMIINSTANCE pIns, const void RT_UNTRUSTED_VOLATILE_GUEST *pv)
 {
-    const HGSMIAREA *pArea = &pIns->area;
-    uintptr_t off = (uintptr_t)pv - (uintptr_t)pArea->pu8Base;
-    if (off < pArea->cbArea)
-        return pArea->offBase + (HGSMIOFFSET)off;
-
-    LogFunc(("pointer %p is outside the area %p LB %#x!!!\n", pv, pArea->pu8Base, pArea->cbArea));
-    return HGSMIOFFSET_VOID;
+    const HGSMIAREA *pArea   = &pIns->area;
+    uintptr_t const  offArea = (uintptr_t)pv - (uintptr_t)pArea->pu8Base;
+    ASSERT_GUEST_MSG_RETURN(offArea < pArea->cbArea,
+                            ("pv=%p; area %#x LB %#x\n", pv, pArea->offBase, pArea->cbArea),
+                            HGSMIOFFSET_VOID);
+    return pArea->offBase + (HGSMIOFFSET)offArea;
 }
 
@@ -1542,13 +1463,13 @@
 
 
-void *HGSMIContext (PHGSMIINSTANCE pIns)
+void *HGSMIContext(PHGSMIINSTANCE pIns)
 {
     uint8_t *p = (uint8_t *)pIns;
-    return p + sizeof (HGSMIINSTANCE);
+    return p + sizeof(HGSMIINSTANCE);
 }
 
 /* The guest submitted a buffer. */
-static DECLCALLBACK(int) hgsmiChannelHandler (void *pvHandler, uint16_t u16ChannelInfo,
-                                              RT_UNTRUSTED_VOLATILE_GUEST void *pvBuffer, HGSMISIZE cbBuffer)
+static DECLCALLBACK(int) hgsmiChannelHandler(void *pvHandler, uint16_t u16ChannelInfo,
+                                             RT_UNTRUSTED_VOLATILE_GUEST void *pvBuffer, HGSMISIZE cbBuffer)
 {
     int rc = VINF_SUCCESS;
@@ -1563,19 +1484,17 @@
         case HGSMI_CC_HOST_FLAGS_LOCATION:
         {
-            if (cbBuffer < sizeof (HGSMIBUFFERLOCATION))
-            {
-                rc = VERR_INVALID_PARAMETER;
-                break;
-            }
-
-            HGSMIBUFFERLOCATION *pLoc = (HGSMIBUFFERLOCATION *)pvBuffer;
-            if (pLoc->cbLocation != sizeof(HGSMIHOSTFLAGS))
-            {
-                rc = VERR_INVALID_PARAMETER;
-                break;
-            }
-
-            pIns->pHGFlags = (HGSMIHOSTFLAGS*)HGSMIOffsetToPointer (&pIns->area, pLoc->offLocation);
-        } break;
+            ASSERT_GUEST_RETURN(cbBuffer >= sizeof(HGSMIBUFFERLOCATION), VERR_INVALID_PARAMETER);
+            HGSMIBUFFERLOCATION RT_UNTRUSTED_VOLATILE_GUEST *pLoc = (HGSMIBUFFERLOCATION RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
+            HGSMIBUFFERLOCATION LocSafe;
+            LocSafe.cbLocation  = pLoc->cbLocation;
+            LocSafe.offLocation = pLoc->offLocation;
+            RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
+
+            ASSERT_GUEST_RETURN(LocSafe.cbLocation == sizeof(HGSMIHOSTFLAGS), VERR_INVALID_PARAMETER);
+            RT_UNTRUSTED_VALIDATED_FENCE();
+
+            pIns->pHGFlags = (HGSMIHOSTFLAGS RT_UNTRUSTED_VOLATILE_GUEST *)HGSMIOffsetToPointer(&pIns->area, pLoc->offLocation);
+            break;
+        }
 
         default:
@@ -1588,13 +1507,13 @@
 }
 
-int HGSMICreate (PHGSMIINSTANCE *ppIns,
-                 PVM             pVM,
-                 const char     *pszName,
-                 HGSMIOFFSET     offBase,
-                 uint8_t        *pu8MemBase,
-                 HGSMISIZE       cbMem,
-                 PFNHGSMINOTIFYGUEST pfnNotifyGuest,
-                 void           *pvNotifyGuest,
-                 size_t         cbContext)
+int HGSMICreate(PHGSMIINSTANCE *ppIns,
+                PVM             pVM,
+                const char     *pszName,
+                HGSMIOFFSET     offBase,
+                uint8_t        *pu8MemBase,
+                HGSMISIZE       cbMem,
+                PFNHGSMINOTIFYGUEST pfnNotifyGuest,
+                void           *pvNotifyGuest,
+                size_t          cbContext)
 {
     LogFlowFunc(("ppIns = %p, pVM = %p, pszName = [%s], offBase = 0x%08X, pu8MemBase = %p, cbMem = 0x%08X, "
@@ -1615,67 +1534,42 @@
     AssertPtrReturn(pu8MemBase, VERR_INVALID_PARAMETER);
 
-    int rc = VINF_SUCCESS;
-
-    PHGSMIINSTANCE pIns = (PHGSMIINSTANCE)RTMemAllocZ (sizeof (HGSMIINSTANCE) + cbContext);
-
-    if (!pIns)
-    {
+    int rc;
+    PHGSMIINSTANCE pIns = (PHGSMIINSTANCE)RTMemAllocZ(sizeof (HGSMIINSTANCE) + cbContext);
+    if (pIns)
+    {
+        rc = HGSMIAreaInitialize (&pIns->area, pu8MemBase, cbMem, offBase);
+        if (RT_SUCCESS (rc))
+            rc = RTCritSectInit (&pIns->instanceCritSect);
+        if (RT_SUCCESS (rc))
+            rc = RTCritSectInit (&pIns->hostHeapCritSect);
+        if (RT_SUCCESS (rc))
+            rc = RTCritSectInit (&pIns->hostFIFOCritSect);
+        if (RT_SUCCESS (rc))
+        {
+            pIns->pVM            = pVM;
+            pIns->pszName        = VALID_PTR(pszName) ? pszName : "";
+
+            hgsmiHostHeapSetupUninitialized(&pIns->hostHeap);
+
+            pIns->pfnNotifyGuest = pfnNotifyGuest;
+            pIns->pvNotifyGuest  = pvNotifyGuest;
+
+            RTListInit(&pIns->hostFIFO);
+            RTListInit(&pIns->hostFIFORead);
+            RTListInit(&pIns->hostFIFOProcessed);
+            RTListInit(&pIns->hostFIFOFree);
+            RTListInit(&pIns->guestCmdCompleted);
+
+            rc = HGSMIHostChannelRegister(pIns, HGSMI_CH_HGSMI, hgsmiChannelHandler, pIns);
+        }
+        if (RT_SUCCESS (rc))
+            *ppIns = pIns;
+        else
+            HGSMIDestroy(pIns);
+    }
+    else
         rc = VERR_NO_MEMORY;
-    }
-
-    if (RT_SUCCESS (rc))
-    {
-        rc = HGSMIAreaInitialize (&pIns->area, pu8MemBase, cbMem, offBase);
-    }
-
-    if (RT_SUCCESS (rc))
-    {
-        rc = RTCritSectInit (&pIns->instanceCritSect);
-    }
-
-    if (RT_SUCCESS (rc))
-    {
-        rc = RTCritSectInit (&pIns->hostHeapCritSect);
-    }
-
-    if (RT_SUCCESS (rc))
-    {
-        rc = RTCritSectInit (&pIns->hostFIFOCritSect);
-    }
-
-    if (RT_SUCCESS (rc))
-    {
-        pIns->pVM            = pVM;
-
-        pIns->pszName        = VALID_PTR(pszName)? pszName: "";
-
-        hgsmiHostHeapSetupUninitialized(&pIns->hostHeap);
-
-        pIns->pfnNotifyGuest = pfnNotifyGuest;
-        pIns->pvNotifyGuest  = pvNotifyGuest;
-
-        RTListInit(&pIns->hostFIFO);
-        RTListInit(&pIns->hostFIFORead);
-        RTListInit(&pIns->hostFIFOProcessed);
-        RTListInit(&pIns->hostFIFOFree);
-        RTListInit(&pIns->guestCmdCompleted);
-    }
-
-    rc = HGSMIHostChannelRegister (pIns,
-                                   HGSMI_CH_HGSMI,
-                                   hgsmiChannelHandler,
-                                   pIns);
-
-    if (RT_SUCCESS (rc))
-    {
-        *ppIns = pIns;
-    }
-    else
-    {
-        HGSMIDestroy (pIns);
-    }
 
     LogFlowFunc(("leave rc = %Rrc, pIns = %p\n", rc, pIns));
-
     return rc;
 }
@@ -1707,5 +1601,5 @@
 }
 
-void HGSMIDestroy (PHGSMIINSTANCE pIns)
+void HGSMIDestroy(PHGSMIINSTANCE pIns)
 {
     LogFlowFunc(("pIns = %p\n", pIns));
@@ -1714,23 +1608,13 @@
     {
         hgsmiHostHeapDestroy(&pIns->hostHeap);
-
-        if (RTCritSectIsInitialized (&pIns->hostHeapCritSect))
-        {
-            RTCritSectDelete (&pIns->hostHeapCritSect);
-        }
-
-        if (RTCritSectIsInitialized (&pIns->instanceCritSect))
-        {
-            RTCritSectDelete (&pIns->instanceCritSect);
-        }
-
-        if (RTCritSectIsInitialized (&pIns->hostFIFOCritSect))
-        {
-            RTCritSectDelete (&pIns->hostFIFOCritSect);
-        }
-
-        memset (pIns, 0, sizeof (HGSMIINSTANCE));
-
-        RTMemFree (pIns);
+        if (RTCritSectIsInitialized(&pIns->hostHeapCritSect))
+            RTCritSectDelete(&pIns->hostHeapCritSect);
+        if (RTCritSectIsInitialized(&pIns->instanceCritSect))
+            RTCritSectDelete(&pIns->instanceCritSect);
+        if (RTCritSectIsInitialized(&pIns->hostFIFOCritSect))
+            RTCritSectDelete(&pIns->hostFIFOCritSect);
+
+        memset(pIns, 0, sizeof (HGSMIINSTANCE));
+        RTMemFree(pIns);
     }
 
@@ -1740,12 +1624,12 @@
 #ifdef VBOX_WITH_WDDM
 
-static int hgsmiGuestCommandComplete (HGSMIINSTANCE *pIns, HGSMIOFFSET offMem)
+static int hgsmiGuestCommandComplete(HGSMIINSTANCE *pIns, HGSMIOFFSET offMem)
 {
     HGSMIGUESTCOMPLENTRY *pEntry = NULL;
 
     AssertPtrReturn(pIns->pHGFlags, VERR_WRONG_ORDER);
-    int rc = hgsmiGuestCompletionFIFOAlloc (pIns, &pEntry);
+    int rc = hgsmiGuestCompletionFIFOAlloc(pIns, &pEntry);
     AssertRC(rc);
-    if (RT_SUCCESS (rc))
+    if (RT_SUCCESS(rc))
     {
         pEntry->offBuffer = offMem;
@@ -1753,5 +1637,5 @@
         rc = hgsmiFIFOLock(pIns);
         AssertRC(rc);
-        if (RT_SUCCESS (rc))
+        if (RT_SUCCESS(rc))
         {
             RTListAppend(&pIns->guestCmdCompleted, &pEntry->nodeEntry);
@@ -1761,7 +1645,5 @@
         }
         else
-        {
             hgsmiGuestCompletionFIFOFree(pIns, pEntry);
-        }
     }
 
@@ -1771,7 +1653,10 @@
 int hgsmiCompleteGuestCommand(PHGSMIINSTANCE pIns, HGSMIOFFSET offBuffer, bool fDoIrq)
 {
-    int rc = hgsmiGuestCommandComplete (pIns, offBuffer);
+    int rc = hgsmiGuestCommandComplete(pIns, offBuffer);
     if (RT_SUCCESS (rc))
     {
+#ifdef DEBUG_misha
+        Assert(fDoIrq);
+#endif
         if (fDoIrq)
         {
@@ -1779,10 +1664,4 @@
             hgsmiNotifyGuest (pIns);
         }
-#ifdef DEBUG_misha
-        else
-        {
-            Assert(0);
-        }
-#endif
     }
     return rc;
@@ -1792,25 +1671,16 @@
 {
     LogFlowFunc(("pIns = %p, pvMem = %p\n", pIns, pvMem));
-
-    int rc = VINF_SUCCESS;
 
     HGSMIBUFFERHEADER RT_UNTRUSTED_VOLATILE_GUEST *pHeader   = HGSMIBufferHeaderFromData(pvMem);
     HGSMIOFFSET                                    offBuffer = HGSMIPointerToOffset(&pIns->area, pHeader);
-
-    Assert(offBuffer != HGSMIOFFSET_VOID);
-    if (offBuffer != HGSMIOFFSET_VOID)
-    {
-        rc = hgsmiCompleteGuestCommand(pIns, offBuffer, fDoIrq);
-        AssertRC (rc);
-    }
-    else
-    {
-        LogRel(("invalid cmd offset \n"));
-        rc = VERR_INVALID_PARAMETER;
-    }
+    ASSERT_GUEST_RETURN(offBuffer != HGSMIOFFSET_VOID, VERR_INVALID_PARAMETER);
+
+    int rc = hgsmiCompleteGuestCommand(pIns, offBuffer, fDoIrq);
+    AssertRC(rc);
 
     LogFlowFunc(("rc = %Rrc\n", rc));
-
-    return rc;
-}
-#endif
+    return rc;
+}
+
+#endif /* VBOX_WITH_WDDM */
+
