diff --git a/reactos/lib/kernel32/mem/global.c b/reactos/lib/kernel32/mem/global.c index 5d8ee55196d..46432f59d37 100644 --- a/reactos/lib/kernel32/mem/global.c +++ b/reactos/lib/kernel32/mem/global.c @@ -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;