Index: trunk/src/kernel32/heap.cpp
===================================================================
--- trunk/src/kernel32/heap.cpp	(revision 7359)
+++ trunk/src/kernel32/heap.cpp	(revision 7360)
@@ -19,4 +19,5 @@
 #include "os2heap.h"
 #include <heap.h>
+#include "os2heap.h"
 #include <odinwrap.h>
 #include "initterm.h"
@@ -27,8 +28,29 @@
 ODINDEBUGCHANNEL(KERNEL32-HEAP)
 
-
-static HANDLE processheap = NULL;
-OS2Heap *OS2ProcessHeap = NULL;
-
+//******************************************************************************
+// Local variables
+//******************************************************************************
+
+static OS2Heap *OS2ProcessHeap = NULL;
+
+static HANDLE i_initializeProcessHeap()
+{
+  HANDLE _processheap;
+  
+  //SvL: Only one process heap per process
+  OS2ProcessHeap = new OS2Heap(HEAP_GENERATE_EXCEPTIONS, 0x4000, 0);
+
+  if(OS2ProcessHeap == NULL)
+    return(NULL);
+  _processheap = OS2ProcessHeap->getHeapHandle();
+  
+  return _processheap;
+}
+
+static HANDLE processheap = i_initializeProcessHeap();
+
+// This macro could be mapped to GetProcessHeap() if there
+// is any change in functionality
+#define GETPROCESSHEAP processheap
 
 
@@ -46,4 +68,5 @@
   else                               \
     curheap = OS2Heap::find(hHeap);  \
+  
 
 
@@ -202,13 +225,4 @@
 {
 //    dprintf2(("KERNEL32: GetProcessHeap\n"));
-    //SvL: Only one process heap per process
-    if(processheap == NULL) {
-      OS2ProcessHeap = new OS2Heap(HEAP_GENERATE_EXCEPTIONS, 0x4000, 0);
-
-      if(OS2ProcessHeap == NULL) {
-               	return(NULL);
-      }
-      processheap = OS2ProcessHeap->getHeapHandle();
-    }
     return(processheap);
 }
@@ -270,5 +284,5 @@
    if((flags & GMEM_MOVEABLE)==0) /* POINTER */
    {
-      palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
+      palloc=HeapAlloc(GETPROCESSHEAP, hpflags, size);
       return (HGLOBAL) palloc;
    }
@@ -277,11 +291,11 @@
       /* HeapLock(heap); */
 
-      pintern=(PGLOBAL32_INTERN)HeapAlloc(GetProcessHeap(), 0,  sizeof(GLOBAL32_INTERN));
+      pintern=(PGLOBAL32_INTERN)HeapAlloc(GETPROCESSHEAP, 0,  sizeof(GLOBAL32_INTERN));
       if (!pintern) return 0;
       if(size)
       {
          //SvL: 2*sizeof for 8 byte alignment
-	 if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_SIZE))) {
-	    HeapFree(GetProcessHeap(), 0, pintern);
+	 if (!(palloc=HeapAlloc(GETPROCESSHEAP, hpflags, size+HGLOBAL_SIZE))) {
+	    HeapFree(GETPROCESSHEAP, 0, pintern);
 	    return 0;
 	 }
@@ -327,5 +341,5 @@
    }
 
-   /* HeapLock(GetProcessHeap()); */
+   /* HeapLock(GETPROCESSHEAP); */
    
    pintern=HANDLE_TO_INTERN(hmem);
@@ -342,5 +356,5 @@
       SetLastError(ERROR_INVALID_HANDLE);
    }
-   /* HeapUnlock(GetProcessHeap()); */;
+   /* HeapUnlock(GETPROCESSHEAP); */;
 
    dprintf(("KERNEL32: GlobalLock %x returned %x", hmem, palloc));
@@ -372,5 +386,5 @@
    }
 
-   /* HeapLock(GetProcessHeap()); */
+   /* HeapLock(GETPROCESSHEAP); */
    pintern=HANDLE_TO_INTERN(hmem);
    
@@ -389,5 +403,5 @@
       locked=FALSE;
    }
-   /* HeapUnlock(GetProcessHeap()); */
+   /* HeapUnlock(GETPROCESSHEAP); */
    return locked;
 }
@@ -422,5 +436,5 @@
         /* will fail.                                                      */
         if (ISPOINTER(pmem)) {
-            if (HeapValidate( GetProcessHeap(), 0, pmem )) {
+            if (HeapValidate( GETPROCESSHEAP, 0, pmem )) {
                 handle = (HGLOBAL)pmem;  /* valid fixed block */
                 return handle;
@@ -440,6 +454,6 @@
             test = maybe_intern->Pointer;
             //SvL: -2 for 8 byte alignment
-            if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL *)test)-2 ) && /* obj(-handle) valid arena? */
-                HeapValidate( GetProcessHeap(), 0, maybe_intern ))  /* intern valid arena? */
+            if (HeapValidate( GETPROCESSHEAP, 0, ((HGLOBAL *)test)-2 ) && /* obj(-handle) valid arena? */
+                HeapValidate( GETPROCESSHEAP, 0, maybe_intern ))  /* intern valid arena? */
             {
                 return handle;
@@ -457,5 +471,5 @@
         /* will fail.                                                      */
         if (ISPOINTER(pmem)) {
-            if (HeapValidate( GetProcessHeap(), 0, pmem )) {
+            if (HeapValidate( GETPROCESSHEAP, 0, pmem )) {
                 handle = (HGLOBAL)pmem;  /* valid fixed block */
                 break;
@@ -469,6 +483,6 @@
         if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
             test = maybe_intern->Pointer;
-            if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */
-                HeapValidate( GetProcessHeap(), 0, maybe_intern ))  /* intern valid arena? */
+            if (HeapValidate( GETPROCESSHEAP, 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */
+                HeapValidate( GETPROCESSHEAP, 0, maybe_intern ))  /* intern valid arena? */
                 break;  /* valid moveable block */
         }
@@ -525,5 +539,5 @@
     	     return 0;
          }
-	 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
+	 size=HeapSize(GETPROCESSHEAP, 0, (LPVOID) hmem);
 	 hnew=GlobalAlloc( flags, size);
 	 palloc=GlobalLock(hnew);
@@ -550,5 +564,5 @@
       {
 	 /* reallocate fixed memory */
-	 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
+	 hnew=(HGLOBAL)HeapReAlloc(GETPROCESSHEAP, heap_flags, (LPVOID) hmem, size);
       }
       else
@@ -570,5 +584,5 @@
 	    if(pintern->Pointer)
 	    {
-	       if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
+	       if((palloc = HeapReAlloc(GETPROCESSHEAP, heap_flags,
 				   (char *) pintern->Pointer-HGLOBAL_SIZE,
 				   size+HGLOBAL_SIZE)) == NULL)
@@ -578,5 +592,5 @@
 	    else
 	    {
-	        if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_SIZE))
+	        if((palloc=HeapAlloc(GETPROCESSHEAP, heap_flags, size+HGLOBAL_SIZE))
 		   == NULL)
 		    return 0;
@@ -589,5 +603,5 @@
 	    if(pintern->Pointer)
 	    {
-	       HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_SIZE);
+	       HeapFree(GETPROCESSHEAP, 0, (char *) pintern->Pointer-HGLOBAL_SIZE);
 	       pintern->Pointer=NULL;
 	    }
@@ -628,5 +642,5 @@
    if(ISPOINTER(hmem)) /* POINTER */
    {
-     if(HeapFree(GetProcessHeap(), 0, (LPVOID) hmem) == TRUE)
+     if(HeapFree(GETPROCESSHEAP, 0, (LPVOID) hmem) == TRUE)
        hreturned = 0;    // success
      else
@@ -647,7 +661,7 @@
 
 	 if(pintern->Pointer)
-	    if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_SIZE))
+	    if(!HeapFree(GETPROCESSHEAP, 0, (char *)(pintern->Pointer)-HGLOBAL_SIZE))
 	       hreturned=hmem;
-	 if(!HeapFree(GetProcessHeap(), 0, pintern))
+	 if(!HeapFree(GETPROCESSHEAP, 0, pintern))
 	    hreturned=hmem;
       }      
@@ -680,5 +694,5 @@
    if(ISPOINTER(hmem)) 
    {
-      retval=HeapSize(GetProcessHeap(), 0,  (LPVOID) hmem);
+      retval=HeapSize(GETPROCESSHEAP, 0,  (LPVOID) hmem);
    }
    else
@@ -691,5 +705,5 @@
         if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
             return 0;
-	 retval=HeapSize(GetProcessHeap(), 0,
+	 retval=HeapSize(GETPROCESSHEAP, 0,
 	                 (char *)(pintern->Pointer)-HGLOBAL_SIZE)-HGLOBAL_SIZE;
          //SvL: ???
@@ -773,5 +787,5 @@
    else
    {
-      /* HeapLock(GetProcessHeap()); */
+      /* HeapLock(GETPROCESSHEAP); */
       pintern=HANDLE_TO_INTERN(hmem);
       if(pintern->Magic==MAGIC_GLOBAL_USED)
@@ -786,5 +800,5 @@
 	 retval=0;
       }
-      /* HeapUnlock(GetProcessHeap()); */
+      /* HeapUnlock(GETPROCESSHEAP); */
    }
    return retval;
Index: trunk/src/kernel32/os2heap.cpp
===================================================================
--- trunk/src/kernel32/os2heap.cpp	(revision 7359)
+++ trunk/src/kernel32/os2heap.cpp	(revision 7360)
@@ -1,3 +1,3 @@
-/* $Id: os2heap.cpp,v 1.30 2001-11-16 12:57:00 phaller Exp $ */
+/* $Id: os2heap.cpp,v 1.31 2001-11-16 14:52:55 phaller Exp $ */
 
 /*
@@ -40,9 +40,9 @@
 #endif
 
-VMutex heaplistmutex;   //protects linked lists of heaps
-
 void * _LNK_CONV getmoreHeapMem(Heap_t pHeap, size_t *size, int *clean);
 void   _LNK_CONV releaseHeapMem(Heap_t pHeap, void *block, size_t size);
 
+VMutex heaplistmutex;   //protects linked lists of heaps
+
 
 //******************************************************************************
@@ -60,5 +60,7 @@
   OS2Heap *curheap = OS2Heap::heap;
   
+#ifdef DEBUG
   totalAlloc   = 0;
+#endif
   fInitialized = 0;
   nrHeaps      = 0;
@@ -158,6 +160,9 @@
       memset(lpMem, 0, dwBytes+HEAP_OVERHEAD);
   }
+  
+#ifdef DEBUG
   totalAlloc += dwBytes;
-
+#endif
+  
   //align at 8 byte boundary
   lpHeapObj = (HEAPELEM *)(((ULONG)lpMem+7) & ~7);
@@ -251,5 +256,7 @@
   int size = Size(0, lpMem);
   dprintf(("OS2Heap::Free lpMem = %X, size %d\n", lpMem, size));
+#ifdef DEBUG
   totalAlloc -= size;
+#endif
 #endif
 
@@ -272,8 +279,4 @@
   dprintf(("OS2Heap::Validate, %X %X", dwFlags, lpMem));
 
-  if(lpMem == NULL) {
-    	dprintf(("OS2Heap::Validate lpMem == NULL\n"));
-    	return(FALSE);
-  }
   /* verify lpMem address */
   if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
@@ -341,4 +344,5 @@
 OS2Heap *OS2Heap::heap = NULL;
 
+
 //******************************************************************************
 //******************************************************************************
@@ -353,5 +357,5 @@
   //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
   //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
-  *size = (*size / 65536) * 65536 + 65536;
+  *size = ( (*size / 65536) + 1) * 65536;
 
   rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
Index: trunk/src/kernel32/os2heap.h
===================================================================
--- trunk/src/kernel32/os2heap.h	(revision 7359)
+++ trunk/src/kernel32/os2heap.h	(revision 7360)
@@ -1,3 +1,3 @@
-/* $Id: os2heap.h,v 1.9 2001-10-17 13:38:50 phaller Exp $ */
+/* $Id: os2heap.h,v 1.10 2001-11-16 14:52:56 phaller Exp $ */
 
 /*
@@ -51,5 +51,8 @@
 protected:
 
-    DWORD     dwMaximumSize, dwInitialSize, flOptions, totalAlloc, nrHeaps;
+    DWORD     dwMaximumSize, dwInitialSize, flOptions, nrHeaps;
+#ifdef DEBUG
+    DWORD totalAlloc;
+#endif
     HANDLE    hPrimaryHeap;
     BOOL      fInitialized;
