-dont return NULL when realloc to 0 bytes (fixes one kernel32_winetest heap test)
authorGunnar Dalsnes <hardon@online.no>
Mon, 17 Oct 2005 20:11:21 +0000 (20:11 +0000)
committerGunnar Dalsnes <hardon@online.no>
Mon, 17 Oct 2005 20:11:21 +0000 (20:11 +0000)
-only allow realloc to new position if GMEM_MOVEABLE is passed (fixed mem)
-only allow realloc to new position if GMEM_MOVEABLE is passed OR mem is not locked (moveable mem)

svn path=/trunk/; revision=18528

reactos/lib/kernel32/mem/global.c

index 5d8ee55..46432f5 100644 (file)
@@ -536,6 +536,11 @@ GlobalReAlloc(HGLOBAL hMem,
     {
         if(ISPOINTER(hMem))
         {
+            if (!(uFlags & GMEM_MOVEABLE))
+            {
+                heap_flags |= HEAP_REALLOC_IN_PLACE_ONLY;
+            }
+
             /* reallocate fixed memory */
             hnew = (HANDLE)RtlReAllocateHeap(GetProcessHeap(), heap_flags, (LPVOID) hMem, dwBytes);
         }
@@ -543,37 +548,47 @@ GlobalReAlloc(HGLOBAL hMem,
         {
             /* reallocate a moveable block */
             phandle= HANDLE_TO_INTERN(hMem);
+            hnew = hMem;
+            
             if (0 != dwBytes)
             {
-                hnew = hMem;
-                if(phandle->Pointer)
-                {
-                    palloc = RtlReAllocateHeap(GetProcessHeap(), heap_flags,
-                                         (PVOID)((ULONG_PTR)phandle->Pointer - HANDLE_SIZE),
-                                         dwBytes + HANDLE_SIZE);
-                    if (0 == palloc)
-                    {
-                        hnew = 0;
-                    }
-                    else
-                    {
-                        *(PHANDLE)palloc = hMem;
-                        phandle->Pointer = (PVOID)((ULONG_PTR)palloc + HANDLE_SIZE);
-                    }
-                }
-                else
-                {
-                    palloc = RtlAllocateHeap(GetProcessHeap(), heap_flags, dwBytes + HANDLE_SIZE);
-                    if (0 == palloc)
-                    {
-                        hnew = 0;
-                    }
-                    else
-                    {
-                        *(PHANDLE)palloc = hMem;
-                        phandle->Pointer = (PVOID)((ULONG_PTR)palloc + HANDLE_SIZE);
-                    }
-                }
+               if(phandle->Pointer)
+               {
+
+                   if (phandle->LockCount && !(uFlags & GMEM_MOVEABLE))
+                   {
+                       /* Locked memory cant normally move but the MEM_MOVEABLE flag
+                        * override this behaviour. But in this case that flag was not passed.
+                        */ 
+                       heap_flags |= HEAP_REALLOC_IN_PLACE_ONLY;
+                   }
+
+                   palloc = RtlReAllocateHeap(GetProcessHeap(), heap_flags,
+                                      (PVOID)((ULONG_PTR)phandle->Pointer - HANDLE_SIZE),
+                                      dwBytes + HANDLE_SIZE);
+                   if (0 == palloc)
+                   {
+                       hnew = 0;
+                   }
+                   else
+                   {
+                       *(PHANDLE)palloc = hMem;
+                       phandle->Pointer = (PVOID)((ULONG_PTR)palloc + HANDLE_SIZE);
+                   }
+               }
+               else
+               {
+                   palloc = RtlAllocateHeap(GetProcessHeap(), heap_flags, dwBytes + HANDLE_SIZE);
+                   if (0 == palloc)
+                   {
+                       hnew = 0;
+                   }
+                   else
+                   {
+                       *(PHANDLE)palloc = hMem;
+                       phandle->Pointer = (PVOID)((ULONG_PTR)palloc + HANDLE_SIZE);
+                   }
+               }
             }
             else
             {
@@ -585,6 +600,7 @@ GlobalReAlloc(HGLOBAL hMem,
             }
         }
     }
+
     HeapUnlock(GetProcessHeap());
 
     return hnew;