Index: /trunk/include/VBox/HGSMI/HGSMI.h
===================================================================
--- /trunk/include/VBox/HGSMI/HGSMI.h	(revision 50481)
+++ /trunk/include/VBox/HGSMI/HGSMI.h	(revision 50482)
@@ -35,4 +35,5 @@
 #include <VBox/HGSMI/HGSMIDefs.h>
 #include <VBox/HGSMI/HGSMIChannels.h>
+#include <VBox/HGSMI/HGSMIMemAlloc.h>
 
 /*
@@ -71,15 +72,22 @@
  */
 
+/* Heap types. */
+#define HGSMI_HEAP_TYPE_NULL    0 /* Heap not initialized. */
+#define HGSMI_HEAP_TYPE_POINTER 1 /* RTHEAPSIMPLE. Obsolete. */
+#define HGSMI_HEAP_TYPE_OFFSET  2 /* RTHEAPOFFSET. Obsolete. */
+#define HGSMI_HEAP_TYPE_MA      3 /* Memory allocator. */
+
 #pragma pack(1)
-typedef struct _HGSMIHEAP
+typedef struct HGSMIHEAP
 {
     union
     {
-        RTHEAPSIMPLE  hPtr;         /**< Pointer based heap. */
-        RTHEAPOFFSET  hOff;         /**< Offset based heap. */
+        HGSMIMADATA   ma;           /* Memory Allocator */
+        RTHEAPSIMPLE  hPtr;         /* Pointer based heap. */
+        RTHEAPOFFSET  hOff;         /* Offset based heap. */
     } u;
-    HGSMIAREA     area;             /**< Description. */
-    int           cRefs;            /**< Number of heap allocations. */
-    bool          fOffsetBased;     /**< Set if offset based. */
+    HGSMIAREA     area;             /* Description. */
+    int           cRefs;            /* Number of heap allocations. */
+    uint32_t      u32HeapType;      /* HGSMI_HEAP_TYPE_* */
 } HGSMIHEAP;
 #pragma pack()
@@ -145,19 +153,19 @@
 }
 
-DECLINLINE(HGSMIOFFSET) HGSMIPointerToOffset (const HGSMIAREA *pArea,
-                                              const HGSMIBUFFERHEADER *pHeader)
-{
-    return pArea->offBase + (HGSMIOFFSET)((uint8_t *)pHeader - pArea->pu8Base);
-}
-
-DECLINLINE(HGSMIBUFFERHEADER *) HGSMIOffsetToPointer (const HGSMIAREA *pArea,
-                                                      HGSMIOFFSET offBuffer)
-{
-    return (HGSMIBUFFERHEADER *)(pArea->pu8Base + (offBuffer - pArea->offBase));
+DECLINLINE(HGSMIOFFSET) HGSMIPointerToOffset(const HGSMIAREA *pArea,
+                                             const void *pv)
+{
+    return pArea->offBase + (HGSMIOFFSET)((uint8_t *)pv - pArea->pu8Base);
+}
+
+DECLINLINE(void *) HGSMIOffsetToPointer(const HGSMIAREA *pArea,
+                                        HGSMIOFFSET offBuffer)
+{
+    return pArea->pu8Base + (offBuffer - pArea->offBase);
 }
 
 DECLINLINE(uint8_t *) HGSMIBufferDataFromOffset (const HGSMIAREA *pArea, HGSMIOFFSET offBuffer)
 {
-    HGSMIBUFFERHEADER *pHeader = HGSMIOffsetToPointer (pArea, offBuffer);
+    HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer(pArea, offBuffer);
     Assert(pHeader);
     if(pHeader)
@@ -168,5 +176,5 @@
 DECLINLINE(uint8_t *) HGSMIBufferDataAndChInfoFromOffset (const HGSMIAREA *pArea, HGSMIOFFSET offBuffer, uint16_t * pChInfo)
 {
-    HGSMIBUFFERHEADER *pHeader = HGSMIOffsetToPointer (pArea, offBuffer);
+    HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer (pArea, offBuffer);
     Assert(pHeader);
     if(pHeader)
@@ -191,7 +199,12 @@
 void HGSMIAreaClear (HGSMIAREA *pArea);
 
-DECLINLINE(bool) HGSMIAreaContainsOffset(HGSMIAREA *pArea, HGSMIOFFSET offSet)
-{
-    return pArea->offBase <= offSet && pArea->offBase + pArea->cbArea > offSet;
+DECLINLINE(bool) HGSMIAreaContainsOffset(const HGSMIAREA *pArea, HGSMIOFFSET off)
+{
+    return off >= pArea->offBase && off - pArea->offBase < pArea->cbArea;
+}
+
+DECLINLINE(bool) HGSMIAreaContainsPointer(const HGSMIAREA *pArea, const void *pv)
+{
+    return (uintptr_t)pv >= (uintptr_t)pArea->pu8Base && (uintptr_t)pv - (uintptr_t)pArea->pu8Base < pArea->cbArea;
 }
 
@@ -203,10 +216,12 @@
 
 int HGSMIHeapSetup (HGSMIHEAP *pHeap,
+                    uint32_t u32HeapType,
                     void *pvBase,
                     HGSMISIZE cbArea,
                     HGSMIOFFSET offBase,
-                    bool fOffsetBased);
+                    const HGSMIENV *pEnv);
 
 int HGSMIHeapRelocate (HGSMIHEAP *pHeap,
+                       uint32_t u32HeapType,
                        void *pvBase,
                        uint32_t offHeapHandle,
@@ -214,8 +229,7 @@
                        HGSMISIZE cbArea,
                        HGSMIOFFSET offBase,
-                       bool fOffsetBased);
-
-void HGSMIHeapSetupUnitialized (HGSMIHEAP *pHeap);
-bool HGSMIHeapIsItialized (HGSMIHEAP *pHeap);
+                       const HGSMIENV *pEnv);
+
+void HGSMIHeapSetupUninitialized (HGSMIHEAP *pHeap);
 
 void HGSMIHeapDestroy (HGSMIHEAP *pHeap);
Index: /trunk/include/VBox/HGSMI/HGSMIDefs.h
===================================================================
--- /trunk/include/VBox/HGSMI/HGSMIDefs.h	(revision 50481)
+++ /trunk/include/VBox/HGSMI/HGSMIDefs.h	(revision 50482)
@@ -109,3 +109,15 @@
 #define HGSMI_NUMBER_OF_CHANNELS 0x100
 
+typedef struct HGSMIENV
+{
+    /* Environment context pointer. */
+    void *pvEnv;
+
+    /* Allocate system memory. */
+    DECLCALLBACKMEMBER(void *, pfnAlloc)(void *pvEnv, HGSMISIZE cb);
+
+    /* Free system memory. */
+    DECLCALLBACKMEMBER(void, pfnFree)(void *pvEnv, void *pv);
+} HGSMIENV;
+
 #endif /* !___VBox_HGSMI_HGSMIDefs_h */
Index: /trunk/include/VBox/VBoxVideoGuest.h
===================================================================
--- /trunk/include/VBox/VBoxVideoGuest.h	(revision 50481)
+++ /trunk/include/VBox/VBoxVideoGuest.h	(revision 50482)
@@ -226,5 +226,6 @@
                                             void *pvGuestHeapMemory,
                                             uint32_t cbGuestHeapMemory,
-                                            uint32_t offVRAMGuestHeapMemory);
+                                            uint32_t offVRAMGuestHeapMemory,
+                                            const HGSMIENV *pEnv);
 RTDECL(void)     VBoxHGSMIGetHostAreaMapping(PHGSMIGUESTCOMMANDCONTEXT pCtx,
                                              uint32_t cbVRAM,
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/xpdm/VBoxDispVBVA.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/xpdm/VBoxDispVBVA.cpp	(revision 50481)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/xpdm/VBoxDispVBVA.cpp	(revision 50482)
@@ -300,4 +300,23 @@
 #endif /* VBOX_VBVA_ADJUST_RECT */
 
+static DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
+{
+    NOREF(pvEnv);
+    return EngAllocMem(0, cb, 0);
+}
+
+static DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
+{
+    NOREF(pvEnv);
+    EngFreeMem(pv);
+}
+
+static HGSMIENV g_hgsmiEnvDisp =
+{
+    NULL,
+    hgsmiEnvAlloc,
+    hgsmiEnvFree
+};
+
 int VBoxDispVBVAInit(PVBOXDISPDEV pDev)
 {
@@ -406,8 +425,9 @@
 
         rc = HGSMIHeapSetup(&pDev->hgsmi.ctx.heapCtx,
+                            HGSMI_HEAP_TYPE_POINTER,
                             (uint8_t *)pDev->memInfo.VideoRamBase+pDev->layout.offDisplayInfo+sizeof(HGSMIHOSTFLAGS),
                             pDev->layout.cbDisplayInfo-sizeof(HGSMIHOSTFLAGS),
                             info.areaDisplay.offBase+pDev->layout.offDisplayInfo+sizeof(HGSMIHOSTFLAGS),
-                            false /*fOffsetBased*/);
+                            &g_hgsmiEnvDisp);
 
         if (RT_SUCCESS(rc))
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPHGSMI.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPHGSMI.cpp	(revision 50481)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPHGSMI.cpp	(revision 50482)
@@ -20,4 +20,24 @@
 #include "VBoxMPCommon.h"
 #include <VBox/VMMDev.h>
+#include <iprt/alloc.h>
+
+static DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
+{
+    NOREF(pvEnv);
+    return RTMemAlloc(cb);
+}
+
+static DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
+{
+    NOREF(pvEnv);
+    RTMemFree(pv);
+}
+
+static HGSMIENV g_hgsmiEnvMP =
+{
+    NULL,
+    hgsmiEnvAlloc,
+    hgsmiEnvFree
+};
 
 /**
@@ -68,5 +88,6 @@
                                             cbGuestHeapMemory,
                                               offVRAMBaseMapping
-                                            + offGuestHeapMemory);
+                                            + offGuestHeapMemory,
+                                            &g_hgsmiEnvMP);
 
             if (RT_FAILURE(rc))
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.cpp	(revision 50481)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.cpp	(revision 50482)
@@ -186,8 +186,9 @@
 }
 
-int VBoxSHGSMIInit(PVBOXSHGSMI pHeap, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase, bool fOffsetBased)
+int VBoxSHGSMIInit(PVBOXSHGSMI pHeap, uint32_t u32HeapType, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase,
+                   const HGSMIENV *pEnv)
 {
     KeInitializeSpinLock(&pHeap->HeapLock);
-    return HGSMIHeapSetup(&pHeap->Heap, pvBase, cbArea, offBase, fOffsetBased);
+    return HGSMIHeapSetup(&pHeap->Heap, u32HeapType, pvBase, cbArea, offBase, pEnv);
 }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.h	(revision 50481)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPShgsmi.h	(revision 50482)
@@ -61,5 +61,5 @@
 }
 
-int VBoxSHGSMIInit(PVBOXSHGSMI pHeap, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase, bool fOffsetBased);
+int VBoxSHGSMIInit(PVBOXSHGSMI pHeap, uint32_t u32HeapType, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase, const HGSMIENV *pEnv);
 void VBoxSHGSMITerm(PVBOXSHGSMI pHeap);
 void* VBoxSHGSMIHeapAlloc(PVBOXSHGSMI pHeap, HGSMISIZE cbData, uint8_t u8Channel, uint16_t u16ChannelInfo);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp	(revision 50481)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp	(revision 50482)
@@ -1451,4 +1451,23 @@
 #endif
 
+static DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
+{
+    NOREF(pvEnv);
+    return RTMemAlloc(cb);
+}
+
+static DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
+{
+    NOREF(pvEnv);
+    RTMemFree(pv);
+}
+
+static HGSMIENV g_hgsmiEnvVdma =
+{
+    NULL,
+    hgsmiEnvAlloc,
+    hgsmiEnvFree
+};
+
 /* create a DMACommand buffer */
 int vboxVdmaCreate(PVBOXMP_DEVEXT pDevExt, VBOXVDMAINFO *pInfo
@@ -1486,8 +1505,9 @@
         /* Setup a HGSMI heap within the adapter information area. */
         rc = VBoxSHGSMIInit(&pInfo->CmdHeap,
+                             HGSMI_HEAP_TYPE_POINTER,
                              pvBuffer,
                              cbBuffer,
                              offBuffer,
-                             false /*fOffsetBased*/);
+                             &g_hgsmiEnvVdma);
         Assert(RT_SUCCESS(rc));
         if(RT_SUCCESS(rc))
Index: /trunk/src/VBox/Additions/common/VBoxVideo/HGSMIBase.cpp
===================================================================
--- /trunk/src/VBox/Additions/common/VBoxVideo/HGSMIBase.cpp	(revision 50481)
+++ /trunk/src/VBox/Additions/common/VBoxVideo/HGSMIBase.cpp	(revision 50482)
@@ -320,16 +320,15 @@
                                        void *pvGuestHeapMemory,
                                        uint32_t cbGuestHeapMemory,
-                                       uint32_t offVRAMGuestHeapMemory)
+                                       uint32_t offVRAMGuestHeapMemory,
+                                       const HGSMIENV *pEnv)
 {
     /** @todo should we be using a fixed ISA port value here? */
     pCtx->port = (RTIOPORT)VGA_PORT_HGSMI_GUEST;
 #ifdef VBOX_WDDM_MINIPORT
-    return VBoxSHGSMIInit(&pCtx->heapCtx, pvGuestHeapMemory,
-                          cbGuestHeapMemory, offVRAMGuestHeapMemory,
-                          false /*fOffsetBased*/);
+    return VBoxSHGSMIInit(&pCtx->heapCtx, HGSMI_HEAP_TYPE_POINTER, pvGuestHeapMemory,
+                          cbGuestHeapMemory, offVRAMGuestHeapMemory, pEnv);
 #else
-    return HGSMIHeapSetup(&pCtx->heapCtx, pvGuestHeapMemory,
-                          cbGuestHeapMemory, offVRAMGuestHeapMemory,
-                          false /*fOffsetBased*/);
+    return HGSMIHeapSetup(&pCtx->heapCtx, HGSMI_HEAP_TYPE_POINTER, pvGuestHeapMemory,
+                          cbGuestHeapMemory, offVRAMGuestHeapMemory, pEnv);
 #endif
 }
Index: /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp
===================================================================
--- /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp	(revision 50481)
+++ /trunk/src/VBox/Devices/Graphics/HGSMI/HGSMIHost.cpp	(revision 50482)
@@ -1113,4 +1113,23 @@
 }
 
+static DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
+{
+    NOREF(pvEnv);
+    return RTMemAlloc(cb);
+}
+
+static DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
+{
+    NOREF(pvEnv);
+    RTMemFree(pv);
+}
+
+static HGSMIENV g_hgsmiEnv =
+{
+    NULL,
+    hgsmiEnvAlloc,
+    hgsmiEnvFree
+};
+
 int HGSMISetupHostHeap (PHGSMIINSTANCE pIns,
                         HGSMIOFFSET    offHeap,
@@ -1144,8 +1163,9 @@
             {
                 rc = HGSMIHeapSetup (&pIns->hostHeap,
+                                     HGSMI_HEAP_TYPE_OFFSET,
                                      pIns->area.pu8Base+offHeap,
                                      cbHeap,
                                      offHeap,
-                                     true /*fOffsetBased*/);
+                                     &g_hgsmiEnv);
             }
 
@@ -1376,4 +1396,7 @@
 
             rc = HGSMIHeapRelocate(&pIns->hostHeap,
+                                   u32Version > VGA_SAVEDSTATE_VERSION_HOST_HEAP?
+                                       HGSMI_HEAP_TYPE_OFFSET:
+                                       HGSMI_HEAP_TYPE_POINTER,
                                    pIns->area.pu8Base+offHeap,
                                    off,
@@ -1381,5 +1404,5 @@
                                    cbHeap,
                                    offHeap,
-                                   u32Version > VGA_SAVEDSTATE_VERSION_HOST_HEAP);
+                                   &g_hgsmiEnv);
 
             hgsmiHostHeapUnlock (pIns);
@@ -1681,5 +1704,5 @@
         pIns->pszName        = VALID_PTR(pszName)? pszName: "";
 
-        HGSMIHeapSetupUnitialized (&pIns->hostHeap);
+        HGSMIHeapSetupUninitialized(&pIns->hostHeap);
 
         pIns->pfnNotifyGuest = pfnNotifyGuest;
@@ -1725,5 +1748,5 @@
 #endif
 
-    HGSMIHeapSetupUnitialized (&pIns->hostHeap);
+    HGSMIHeapSetupUninitialized(&pIns->hostHeap);
 
     return flags;
Index: /trunk/src/VBox/GuestHost/HGSMI/HGSMICommon.cpp
===================================================================
--- /trunk/src/VBox/GuestHost/HGSMI/HGSMICommon.cpp	(revision 50481)
+++ /trunk/src/VBox/GuestHost/HGSMI/HGSMICommon.cpp	(revision 50482)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2012 Oracle Corporation
+ * Copyright (C) 2006-2014 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -189,21 +189,12 @@
 }
 
-void HGSMIHeapSetupUnitialized (HGSMIHEAP *pHeap)
-{
-    pHeap->u.hPtr = NIL_RTHEAPSIMPLE;
-    pHeap->cRefs = 0;
-    pHeap->area.cbArea = 0;
-    pHeap->area.offBase = HGSMIOFFSET_VOID;
-    pHeap->area.offLast = HGSMIOFFSET_VOID;
-    pHeap->area.pu8Base = 0;
-    pHeap->fOffsetBased = false;
-}
-
-bool HGSMIHeapIsItialized (HGSMIHEAP *pHeap)
-{
-    return pHeap->u.hPtr != NIL_RTHEAPSIMPLE;
+void HGSMIHeapSetupUninitialized(HGSMIHEAP *pHeap)
+{
+    RT_ZERO(*pHeap);
+    pHeap->u32HeapType = HGSMI_HEAP_TYPE_NULL;
 }
 
 int HGSMIHeapRelocate (HGSMIHEAP *pHeap,
+                       uint32_t u32HeapType,
                        void *pvBase,
                        uint32_t offHeapHandle,
@@ -211,6 +202,5 @@
                        HGSMISIZE cbArea,
                        HGSMIOFFSET offBase,
-                       bool fOffsetBased
-                       )
+                       const HGSMIENV *pEnv)
 {
     if (   !pHeap
@@ -224,15 +214,27 @@
     if (RT_SUCCESS (rc))
     {
-        if (fOffsetBased)
+        if (u32HeapType == HGSMI_HEAP_TYPE_MA)
+        {
+            /* @todo rc = HGSMIMAInit(&pHeap->u.ma, &pHeap->area, NULL, 0, 0, pEnv); */
+            rc = VERR_NOT_IMPLEMENTED;
+        }
+        else if (u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
+        {
             pHeap->u.hOff = (RTHEAPOFFSET)((uint8_t *)pvBase + offHeapHandle);
-        else
+        }
+        else if (u32HeapType == HGSMI_HEAP_TYPE_POINTER)
         {
             pHeap->u.hPtr = (RTHEAPSIMPLE)((uint8_t *)pvBase + offHeapHandle);
             rc = RTHeapSimpleRelocate (pHeap->u.hPtr, offDelta); AssertRC(rc);
         }
-        if (RT_SUCCESS (rc))
+        else
+        {
+            rc = VERR_NOT_SUPPORTED;
+        }
+
+        if (RT_SUCCESS(rc))
         {
             pHeap->cRefs = 0;
-            pHeap->fOffsetBased = fOffsetBased;
+            pHeap->u32HeapType = u32HeapType;
         }
         else
@@ -246,8 +248,9 @@
 
 int HGSMIHeapSetup (HGSMIHEAP *pHeap,
+                    uint32_t u32HeapType,
                     void *pvBase,
                     HGSMISIZE cbArea,
                     HGSMIOFFSET offBase,
-                    bool fOffsetBased)
+                    const HGSMIENV *pEnv)
 {
     if (   !pHeap
@@ -261,13 +264,25 @@
     if (RT_SUCCESS (rc))
     {
-        if (!fOffsetBased)
+        if (u32HeapType == HGSMI_HEAP_TYPE_MA)
+        {
+            rc = HGSMIMAInit(&pHeap->u.ma, &pHeap->area, NULL, 0, 0, pEnv);
+        }
+        else if (u32HeapType == HGSMI_HEAP_TYPE_POINTER)
+        {
             rc = RTHeapSimpleInit (&pHeap->u.hPtr, pvBase, cbArea);
+        }
+        else if (u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
+        {
+            rc = RTHeapOffsetInit (&pHeap->u.hOff, pvBase, cbArea);
+        }
         else
-            rc = RTHeapOffsetInit (&pHeap->u.hOff, pvBase, cbArea);
+        {
+            rc = VERR_NOT_SUPPORTED;
+        }
 
         if (RT_SUCCESS (rc))
         {
             pHeap->cRefs = 0;
-            pHeap->fOffsetBased = fOffsetBased;
+            pHeap->u32HeapType = u32HeapType;
         }
         else
@@ -284,8 +299,10 @@
     if (pHeap)
     {
+        if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_MA)
+        {
+            HGSMIMAUninit(&pHeap->u.ma);
+        }
         Assert(!pHeap->cRefs);
-        pHeap->u.hPtr = NIL_RTHEAPSIMPLE;
-        HGSMIAreaClear (&pHeap->area);
-        pHeap->cRefs = 0;
+        HGSMIHeapSetupUninitialized(pHeap);
     }
 }
@@ -296,10 +313,5 @@
                       uint16_t u16ChannelInfo)
 {
-    if (pHeap->u.hPtr == NIL_RTHEAPSIMPLE)
-    {
-        return NULL;
-    }
-
-    size_t cbAlloc = HGSMIBufferRequiredSize (cbData);
+    HGSMISIZE cbAlloc = HGSMIBufferRequiredSize (cbData);
 
     HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIHeapBufferAlloc (pHeap, cbAlloc);
@@ -326,5 +338,5 @@
 {
     if (   pvData
-        && pHeap->u.hPtr != NIL_RTHEAPSIMPLE)
+        && pHeap->u32HeapType != HGSMI_HEAP_TYPE_NULL)
     {
         HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData);
@@ -336,24 +348,41 @@
 void* HGSMIHeapBufferAlloc (HGSMIHEAP *pHeap, HGSMISIZE cbBuffer)
 {
-    void* pvBuf;
-    if (!pHeap->fOffsetBased)
+    void* pvBuf = NULL;
+    if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_MA)
+    {
+        pvBuf = HGSMIMAAlloc(&pHeap->u.ma, cbBuffer);
+    }
+    else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_POINTER)
+    {
         pvBuf = RTHeapSimpleAlloc (pHeap->u.hPtr, cbBuffer, 0);
-    else
+    }
+    else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
+    {
         pvBuf = RTHeapOffsetAlloc (pHeap->u.hOff, cbBuffer, 0);
-
-    if (!pvBuf)
-        return NULL;
-
-    ++pHeap->cRefs;
+    }
+
+    if (pvBuf)
+    {
+        ++pHeap->cRefs;
+    }
+
     return pvBuf;
 }
 
 void HGSMIHeapBufferFree(HGSMIHEAP *pHeap,
-                    void *pvBuf)
-{
-    if (!pHeap->fOffsetBased)
+                         void *pvBuf)
+{
+    if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_MA)
+    {
+        HGSMIMAFree(&pHeap->u.ma, pvBuf);
+    }
+    else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_POINTER)
+    {
         RTHeapSimpleFree (pHeap->u.hPtr, pvBuf);
-    else
+    }
+    else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
+    {
         RTHeapOffsetFree (pHeap->u.hOff, pvBuf);
+    }
 
     --pHeap->cRefs;
@@ -377,5 +406,5 @@
     }
 
-    const HGSMIBUFFERHEADER *pHeader = HGSMIOffsetToPointer (pArea, offBuffer);
+    const HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer (pArea, offBuffer);
 
     /* Quick check of the data size, it should be less than the maximum
Index: /trunk/src/VBox/GuestHost/HGSMI/Makefile.kmk
===================================================================
--- /trunk/src/VBox/GuestHost/HGSMI/Makefile.kmk	(revision 50481)
+++ /trunk/src/VBox/GuestHost/HGSMI/Makefile.kmk	(revision 50482)
@@ -30,5 +30,6 @@
 HGSMIGuestR0Lib_DEFS         =
 HGSMIGuestR0Lib_SOURCES      = \
-	HGSMICommon.cpp
+	HGSMICommon.cpp \
+	HGSMIMemAlloc.cpp
 endif
 
@@ -40,5 +41,6 @@
 HGSMIHostR3Lib_DEFS          =
 HGSMIHostR3Lib_SOURCES       = \
-	HGSMICommon.cpp
+	HGSMICommon.cpp \
+	HGSMIMemAlloc.cpp
 
 include $(FILE_KBUILD_SUB_FOOTER)
