2006-06-24 18:11:57 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Win32 Base API
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: dll/win32/kernel32/mem/global.c
|
|
|
|
* PURPOSE: Global Memory APIs (sits on top of Heap*)
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
2001-01-20 12:20:43 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2003-01-15 21:24:36 +00:00
|
|
|
#include <k32.h>
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2000-01-20 22:57:01 +00:00
|
|
|
#define NDEBUG
|
2007-09-02 19:42:22 +00:00
|
|
|
#include <debug.h>
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* TYPES *********************************************************************/
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
extern SYSTEM_BASIC_INFORMATION BaseCachedSysInfo;
|
2006-06-24 19:00:07 +00:00
|
|
|
RTL_HANDLE_TABLE BaseHeapHandleTable;
|
2001-04-05 01:54:42 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS ***************************************************************/
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
HGLOBAL
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalAlloc(UINT uFlags,
|
2003-10-13 04:45:30 +00:00
|
|
|
DWORD dwBytes)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
ULONG Flags = 0;
|
|
|
|
PVOID Ptr = NULL;
|
|
|
|
HANDLE hMemory;
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
BASE_TRACE_ALLOC(dwBytes, uFlags);
|
|
|
|
ASSERT(hProcessHeap);
|
|
|
|
|
|
|
|
/* Make sure the flags are valid */
|
|
|
|
if (uFlags & ~GMEM_VALID_FLAGS)
|
|
|
|
{
|
|
|
|
/* They aren't, fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Convert ZEROINIT */
|
|
|
|
if (uFlags & GMEM_ZEROINIT) Flags |= HEAP_ZERO_MEMORY;
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if we're not movable, which means pointer-based heap */
|
|
|
|
if (!(uFlags & GMEM_MOVEABLE))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this is DDESHARE (deprecated) */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (uFlags & GMEM_DDESHARE) Flags |= BASE_HEAP_ENTRY_FLAG_DDESHARE;
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Allocate heap for it */
|
|
|
|
Ptr = RtlAllocateHeap(hProcessHeap, Flags, dwBytes);
|
|
|
|
BASE_TRACE_ALLOC2(Ptr);
|
|
|
|
return Ptr;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* This is heap based, so lock it in first */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable locking, enable custom flags, and write the
|
|
|
|
* movable flag (deprecated)
|
|
|
|
*/
|
|
|
|
Flags |= HEAP_NO_SERIALIZE |
|
|
|
|
HEAP_SETTABLE_USER_VALUE |
|
2006-06-24 19:00:07 +00:00
|
|
|
BASE_HEAP_FLAG_MOVABLE;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Allocate the handle */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapAllocEntry();
|
2006-06-24 18:11:57 +00:00
|
|
|
if (!HandleEntry)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Fail */
|
|
|
|
hMemory = NULL;
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the object and make sure we have size */
|
|
|
|
hMemory = &HandleEntry->Object;
|
|
|
|
if (dwBytes)
|
|
|
|
{
|
|
|
|
/* Allocate the actual memory for it */
|
|
|
|
Ptr = RtlAllocateHeap(hProcessHeap, Flags, dwBytes);
|
|
|
|
BASE_TRACE_PTR(HandleEntry, Ptr);
|
|
|
|
if (!Ptr)
|
2004-02-18 23:20:14 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* We failed, manually set the allocate flag and free the handle */
|
|
|
|
HandleEntry->Flags = RTL_HANDLE_VALID;
|
2006-06-24 19:00:07 +00:00
|
|
|
BaseHeapFreeEntry(HandleEntry);
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* For the cleanup case */
|
|
|
|
HandleEntry = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* All worked well, save our heap entry */
|
|
|
|
RtlSetUserValueHeap(hProcessHeap, HEAP_NO_SERIALIZE, Ptr, hMemory);
|
2008-07-10 09:46:25 +00:00
|
|
|
RtlSetUserFlagsHeap(hProcessHeap, HEAP_NO_SERIALIZE, Ptr, Flags);
|
2004-02-18 23:20:14 +00:00
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
Quickie:
|
|
|
|
/* Cleanup! First unlock the heap */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
|
|
|
|
/* Check if a handle was allocated */
|
|
|
|
if (HandleEntry)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Set the pointer and allocated flag */
|
|
|
|
HandleEntry->Object = Ptr;
|
|
|
|
HandleEntry->Flags = RTL_HANDLE_VALID;
|
|
|
|
if (!Ptr)
|
|
|
|
{
|
|
|
|
/* We don't have a valid pointer, but so reuse this handle */
|
2006-06-26 22:48:12 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_REUSE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if the handle is discardable */
|
|
|
|
if (uFlags & GMEM_DISCARDABLE)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Save it in the handle entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_REUSABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if the handle is moveable */
|
|
|
|
if (uFlags & GMEM_MOVEABLE)
|
|
|
|
{
|
|
|
|
/* Save it in the handle entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_MOVABLE;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if the handle is DDE Shared */
|
|
|
|
if (uFlags & GMEM_DDESHARE)
|
2004-02-18 23:20:14 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Save it in the handle entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_DDESHARE;
|
2004-02-18 23:20:14 +00:00
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Set the pointer */
|
|
|
|
Ptr = hMemory;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Return the pointer */
|
|
|
|
return Ptr;
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
SIZE_T
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalCompact(DWORD dwMinFree)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Call the RTL Heap Manager */
|
|
|
|
return RtlCompactHeap(hProcessHeap, 0);
|
2001-04-05 01:54:42 +00:00
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalFix(HGLOBAL hMem)
|
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Lock the memory if it the handle is valid */
|
|
|
|
if (INVALID_HANDLE_VALUE != hMem) GlobalLock(hMem);
|
2001-04-05 01:54:42 +00:00
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
2003-10-13 04:45:30 +00:00
|
|
|
* @implemented
|
2003-07-10 18:50:51 +00:00
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
UINT
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalFlags(HGLOBAL hMem)
|
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
HANDLE Handle = NULL;
|
|
|
|
ULONG Flags = 0;
|
|
|
|
UINT uFlags = GMEM_INVALID_HANDLE;
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Start by locking the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this is a simple RTL Heap Managed block */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Then we'll query RTL Heap */
|
|
|
|
RtlGetUserInfoHeap(hProcessHeap, Flags, hMem, &Handle, &Flags);
|
|
|
|
BASE_TRACE_PTR(Handle, hMem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if RTL Heap didn't find a handle associated with us or
|
|
|
|
* said that this heap isn't movable, which means something we're
|
|
|
|
* really not a handle-based heap.
|
|
|
|
*/
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!(Handle) || !(Flags & BASE_HEAP_FLAG_MOVABLE))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Then set the flags to 0 */
|
|
|
|
uFlags = 0;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise we're handle-based, so get the internal handle */
|
|
|
|
hMem = Handle;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if the handle is actually an entry in our table */
|
2006-06-24 19:00:07 +00:00
|
|
|
if ((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Then get the entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
2006-06-24 18:11:57 +00:00
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Make sure it's a valid handle */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (BaseHeapValidateEntry(HandleEntry))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Get the lock count first */
|
|
|
|
uFlags = HandleEntry->LockCount & GMEM_LOCKCOUNT;
|
|
|
|
|
2009-07-11 22:52:36 +00:00
|
|
|
/* Now check if it's discardable */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (HandleEntry->Flags & BASE_HEAP_ENTRY_FLAG_REUSABLE)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Set the Win32 Flag */
|
2009-07-11 22:14:19 +00:00
|
|
|
uFlags |= GMEM_DISCARDABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if it's DDE Shared */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (HandleEntry->Flags & BASE_HEAP_ENTRY_FLAG_DDESHARE)
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
|
|
|
/* Set the Win32 Flag */
|
|
|
|
uFlags |= GMEM_DDESHARE;
|
|
|
|
}
|
2009-07-11 22:14:19 +00:00
|
|
|
|
2009-07-11 22:52:36 +00:00
|
|
|
/* Now check if it's discarded */
|
|
|
|
if (HandleEntry->Flags & BASE_HEAP_ENTRY_FLAG_REUSE)
|
|
|
|
/* Set the Win32 Flag */
|
|
|
|
uFlags |= GMEM_DISCARDED;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if by now, we still haven't gotten any useful flags */
|
|
|
|
if (uFlags == GMEM_INVALID_HANDLE) SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
|
|
|
|
/* All done! Unlock heap and return Win32 Flags */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return uFlags;
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
HGLOBAL
|
|
|
|
NTAPI
|
|
|
|
GlobalFree(HGLOBAL hMem)
|
2001-04-05 01:54:42 +00:00
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
LPVOID Ptr;
|
|
|
|
BASE_TRACE_DEALLOC(hMem);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this was a simple allocated heap entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Free it with the RTL Heap Manager */
|
|
|
|
if (RtlFreeHeap(hProcessHeap, 0, hMem))
|
|
|
|
{
|
|
|
|
/* Return NULL since there's no handle */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return hMem;
|
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* It's a handle probably, so lock the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Make sure that this is an entry in our handle database */
|
2006-06-24 19:00:07 +00:00
|
|
|
if ((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Get the entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
2006-06-24 18:11:57 +00:00
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
|
|
|
|
|
|
|
/* Make sure the handle is valid */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!BaseHeapValidateEntry(HandleEntry))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* It's not, fail */
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
Ptr = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's valid, so get the pointer */
|
|
|
|
Ptr = HandleEntry->Object;
|
|
|
|
|
|
|
|
/* Free this handle */
|
2006-06-24 19:00:07 +00:00
|
|
|
BaseHeapFreeEntry(HandleEntry);
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* If the pointer is 0, then we don't have a handle either */
|
|
|
|
if (!Ptr) hMem = NULL;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise, reuse the handle as a pointer */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
Ptr = hMem;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if we got here with a valid heap pointer */
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Free it */
|
|
|
|
RtlFreeHeap(hProcessHeap, HEAP_NO_SERIALIZE, Ptr);
|
|
|
|
hMem = NULL;
|
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* We're done, so unlock the heap and return the handle */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return hMem;
|
2001-04-05 01:54:42 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
2003-10-13 04:45:30 +00:00
|
|
|
* @implemented
|
2003-07-10 18:50:51 +00:00
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
HGLOBAL
|
|
|
|
NTAPI
|
|
|
|
GlobalHandle(LPCVOID pMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
HANDLE Handle = NULL;
|
|
|
|
ULONG Flags;
|
|
|
|
|
|
|
|
/* Lock the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
|
|
|
|
|
|
|
/* Query RTL Heap */
|
|
|
|
RtlGetUserInfoHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
(PVOID)pMem,
|
|
|
|
&Handle,
|
|
|
|
&Flags);
|
|
|
|
BASE_TRACE_PTR(Handle, pMem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if RTL Heap didn't find a handle for us or said that
|
|
|
|
* this heap isn't movable.
|
|
|
|
*/
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!(Handle) || !(Flags & BASE_HEAP_FLAG_MOVABLE))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* We're actually handle-based, so the pointer is a handle */
|
|
|
|
Handle = (HANDLE)pMem;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* All done, unlock the heap and return the handle */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return Handle;
|
2001-04-05 01:54:42 +00:00
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2004-04-22 02:20:52 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
LPVOID
|
|
|
|
NTAPI
|
|
|
|
GlobalLock(HGLOBAL hMem)
|
2004-04-22 02:20:52 +00:00
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
LPVOID Ptr;
|
2004-04-22 02:20:52 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this was a simple allocated heap entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY))
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Verify and return the pointer */
|
|
|
|
return IsBadReadPtr(hMem, 1) ? NULL : hMem;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2004-04-22 02:20:52 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise, lock the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
2004-04-22 02:20:52 +00:00
|
|
|
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Get the handle entry */
|
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
|
|
|
|
|
|
|
/* Make sure it's valid */
|
|
|
|
if (!BaseHeapValidateEntry(HandleEntry))
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* It's not, fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
Ptr = NULL;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Otherwise, get the pointer */
|
|
|
|
Ptr = HandleEntry->Object;
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Increase the lock count, unless we've went too far */
|
|
|
|
if (HandleEntry->LockCount++ == GMEM_LOCKCOUNT)
|
|
|
|
{
|
|
|
|
/* In which case we simply unlock once */
|
|
|
|
HandleEntry->LockCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The handle is still there but the memory was already freed */
|
|
|
|
SetLastError(ERROR_DISCARDED);
|
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
Ptr = NULL;
|
|
|
|
}
|
|
|
|
_SEH2_END
|
2004-04-22 02:20:52 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* All done. Unlock the heap and return the pointer */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return Ptr;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
HGLOBAL
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalReAlloc(HGLOBAL hMem,
|
2006-06-24 18:11:57 +00:00
|
|
|
DWORD dwBytes,
|
|
|
|
UINT uFlags)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
HANDLE Handle;
|
|
|
|
LPVOID Ptr;
|
|
|
|
ULONG Flags = 0;
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Convert ZEROINIT */
|
|
|
|
if (uFlags & GMEM_ZEROINIT) Flags |= HEAP_ZERO_MEMORY;
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* If this wasn't a movable heap, then we MUST re-alloc in place */
|
|
|
|
if (!(uFlags & GMEM_MOVEABLE)) Flags |= HEAP_REALLOC_IN_PLACE_ONLY;
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Lock the heap and disable built-in locking in the RTL Heap funcitons */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
|
|
|
Flags |= HEAP_NO_SERIALIZE;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this is a simple handle-based block */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Get the entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
2006-06-24 18:11:57 +00:00
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
|
|
|
|
|
|
|
/* Make sure the handle is valid */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!BaseHeapValidateEntry(HandleEntry))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
hMem = NULL;
|
|
|
|
}
|
|
|
|
else if (uFlags & GMEM_MODIFY)
|
|
|
|
{
|
|
|
|
/* User is changing flags... check if the memory was discardable */
|
|
|
|
if (uFlags & GMEM_DISCARDABLE)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Then set the flag */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_REUSABLE;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise, remove the flag */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags &= BASE_HEAP_ENTRY_FLAG_REUSABLE;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise, get the object and check if we have no size */
|
|
|
|
Ptr = HandleEntry->Object;
|
|
|
|
if (!dwBytes)
|
2005-10-17 20:11:21 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Clear the handle and check for a pointer */
|
|
|
|
hMem = NULL;
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Make sure the handle isn't locked */
|
2006-06-26 22:48:12 +00:00
|
|
|
if ((uFlags & GMEM_MOVEABLE) && !(HandleEntry->LockCount))
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
|
|
|
/* Free the current heap */
|
|
|
|
RtlFreeHeap(hProcessHeap, Flags, Ptr);
|
|
|
|
|
|
|
|
/* Free the handle */
|
|
|
|
HandleEntry->Object = NULL;
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_REUSE;
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Get the object pointer */
|
|
|
|
hMem = &HandleEntry->Object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise just return the object pointer */
|
|
|
|
hMem = &HandleEntry->Object;
|
|
|
|
}
|
2005-10-17 20:11:21 +00:00
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, we're allocating, so set the new flags needed */
|
2006-06-24 19:00:07 +00:00
|
|
|
Flags |= HEAP_SETTABLE_USER_VALUE | BASE_HEAP_FLAG_MOVABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
if (!Ptr)
|
|
|
|
{
|
|
|
|
/* We don't have a base, so allocate one */
|
|
|
|
Ptr = RtlAllocateHeap(hProcessHeap, Flags, dwBytes);
|
|
|
|
BASE_TRACE_ALLOC2(Ptr);
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Allocation succeeded, so save our entry */
|
|
|
|
RtlSetUserValueHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
Ptr,
|
|
|
|
hMem);
|
2008-07-10 09:46:25 +00:00
|
|
|
RtlSetUserFlagsHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
Ptr,
|
|
|
|
Flags);
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If it's not movable or currently locked, we MUST allocate
|
|
|
|
* in-place!
|
|
|
|
*/
|
|
|
|
if (!(uFlags & GMEM_MOVEABLE) && (HandleEntry->LockCount))
|
|
|
|
{
|
|
|
|
/* Set the flag */
|
|
|
|
Flags |= HEAP_REALLOC_IN_PLACE_ONLY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise clear the flag if we set it previously */
|
|
|
|
Flags &= ~HEAP_REALLOC_IN_PLACE_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And do the re-allocation */
|
|
|
|
Ptr = RtlReAllocateHeap(hProcessHeap, Flags, Ptr, dwBytes);
|
2009-05-13 09:48:32 +00:00
|
|
|
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Allocation succeeded, so save our entry */
|
|
|
|
RtlSetUserValueHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
Ptr,
|
|
|
|
hMem);
|
|
|
|
RtlSetUserFlagsHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
Ptr,
|
|
|
|
Flags);
|
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2005-10-17 20:11:21 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Make sure we have a pointer by now */
|
|
|
|
if (Ptr)
|
|
|
|
{
|
|
|
|
/* Write it in the handle entry and mark it in use */
|
|
|
|
HandleEntry->Object = Ptr;
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags &= ~BASE_HEAP_ENTRY_FLAG_REUSE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise we failed */
|
|
|
|
hMem = NULL;
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
else if (uFlags & GMEM_MODIFY)
|
|
|
|
{
|
|
|
|
/* This is not a handle-based heap and the caller wants it to be one */
|
|
|
|
if (uFlags & GMEM_MOVEABLE)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Get information on its current state */
|
|
|
|
Handle = hMem;
|
|
|
|
DPRINT1("h h %lx %lx\n", Handle, hMem);
|
|
|
|
RtlGetUserInfoHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
hMem,
|
|
|
|
&Handle,
|
|
|
|
&Flags);
|
|
|
|
DPRINT1("h h %lx %lx\n", Handle, hMem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the handle matches the pointer or if the moveable flag
|
|
|
|
* isn't there, which is what we expect since it currenly isn't.
|
|
|
|
*/
|
2006-06-24 19:00:07 +00:00
|
|
|
if (Handle == hMem || !(Flags & BASE_HEAP_FLAG_MOVABLE))
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Allocate a handle for it */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapAllocEntry();
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Calculate the size of the current heap */
|
|
|
|
dwBytes = RtlSizeHeap(hProcessHeap, HEAP_NO_SERIALIZE, hMem);
|
|
|
|
|
|
|
|
/* Set the movable flag */
|
2006-06-24 19:00:07 +00:00
|
|
|
Flags |= HEAP_SETTABLE_USER_VALUE | BASE_HEAP_FLAG_MOVABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Now allocate the actual heap for it */
|
|
|
|
HandleEntry->Object = RtlAllocateHeap(hProcessHeap,
|
|
|
|
Flags,
|
|
|
|
dwBytes);
|
|
|
|
BASE_TRACE_PTR(HandleEntry->Object, HandleEntry);
|
|
|
|
if (!HandleEntry->Object)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/*
|
|
|
|
* We failed, manually set the allocate flag and
|
|
|
|
* free the handle
|
|
|
|
*/
|
|
|
|
HandleEntry->Flags = RTL_HANDLE_VALID;
|
2006-06-24 19:00:07 +00:00
|
|
|
BaseHeapFreeEntry(HandleEntry);
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* For the cleanup case */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
HandleEntry = NULL;
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, copy the current heap and free the old one */
|
|
|
|
RtlMoveMemory(HandleEntry->Object, hMem, dwBytes);
|
|
|
|
RtlFreeHeap(hProcessHeap, HEAP_NO_SERIALIZE, hMem);
|
|
|
|
|
|
|
|
/* Select the heap pointer */
|
|
|
|
hMem = (HANDLE)&HandleEntry->Object;
|
|
|
|
|
|
|
|
/* Initialize the count and default flags */
|
|
|
|
HandleEntry->LockCount = 0;
|
|
|
|
HandleEntry->Flags = RTL_HANDLE_VALID |
|
2006-06-24 19:00:07 +00:00
|
|
|
BASE_HEAP_ENTRY_FLAG_MOVABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* Check if it's also discardable */
|
|
|
|
if (uFlags & GMEM_DISCARDABLE)
|
|
|
|
{
|
|
|
|
/* Set the internal flag */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_REUSABLE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it's also DDE Shared */
|
|
|
|
if (uFlags & GMEM_DDESHARE)
|
|
|
|
{
|
|
|
|
/* Set the internal flag */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry->Flags |= BASE_HEAP_ENTRY_FLAG_DDESHARE;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocation succeeded, so save our entry */
|
|
|
|
RtlSetUserValueHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
HandleEntry->Object,
|
|
|
|
hMem);
|
2008-07-10 09:46:25 +00:00
|
|
|
RtlSetUserFlagsHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
HandleEntry->Object,
|
2009-05-12 13:03:38 +00:00
|
|
|
Flags);
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, this is a simple RTL Managed Heap, so just call it */
|
|
|
|
hMem = RtlReAllocateHeap(hProcessHeap,
|
|
|
|
Flags | HEAP_NO_SERIALIZE,
|
|
|
|
hMem,
|
|
|
|
dwBytes);
|
|
|
|
if (!hMem)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 20:11:21 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* All done, unlock the heap and return the pointer */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return hMem;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
DWORD
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalSize(HGLOBAL hMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
PVOID Handle = NULL;
|
|
|
|
ULONG Flags = 0;
|
|
|
|
SIZE_T dwSize = MAXULONG_PTR;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Lock the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_TRY
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Check if this is a simple RTL Heap Managed block */
|
|
|
|
if (!((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY))
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Then we'll query RTL Heap */
|
|
|
|
RtlGetUserInfoHeap(hProcessHeap, Flags, hMem, &Handle, &Flags);
|
|
|
|
BASE_TRACE_PTR(Handle, hMem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if RTL Heap didn't give us a handle or said that this heap
|
|
|
|
* isn't movable.
|
|
|
|
*/
|
|
|
|
if (!(Handle) || !(Flags & BASE_HEAP_FLAG_MOVABLE))
|
|
|
|
{
|
|
|
|
/* This implies we're not a handle heap, so use the generic call */
|
|
|
|
dwSize = RtlSizeHeap(hProcessHeap, HEAP_NO_SERIALIZE, hMem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise we're a handle heap, so get the internal handle */
|
|
|
|
hMem = Handle;
|
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
|
|
|
|
/* Make sure that this is an entry in our handle database */
|
|
|
|
if ((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY)
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Get the entry */
|
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Make sure the handle is valid */
|
|
|
|
if (!BaseHeapValidateEntry(HandleEntry))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
else if (HandleEntry->Flags & BASE_HEAP_ENTRY_FLAG_REUSE)
|
|
|
|
{
|
|
|
|
/* We've reused this block, but we've saved the size for you */
|
|
|
|
dwSize = HandleEntry->OldSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, query RTL about it */
|
|
|
|
dwSize = RtlSizeHeap(hProcessHeap,
|
|
|
|
HEAP_NO_SERIALIZE,
|
|
|
|
HandleEntry->Object);
|
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Check if by now, we still haven't gotten any useful size */
|
|
|
|
if (dwSize == MAXULONG_PTR)
|
2003-10-13 04:45:30 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2010-05-16 22:24:26 +00:00
|
|
|
dwSize = 0;
|
2003-10-13 04:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
dwSize = 0;
|
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_END
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* All done! Unlock heap and return the size */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return dwSize;
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalUnfix(HGLOBAL hMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* If the handle is valid, unlock it */
|
|
|
|
if (hMem != INVALID_HANDLE_VALUE) GlobalUnlock(hMem);
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
2003-10-13 04:45:30 +00:00
|
|
|
* @implemented
|
2003-07-10 18:50:51 +00:00
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
BOOL
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalUnlock(HGLOBAL hMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 19:00:07 +00:00
|
|
|
PBASE_HEAP_HANDLE_ENTRY HandleEntry;
|
2006-06-24 18:11:57 +00:00
|
|
|
BOOL RetVal = TRUE;
|
2003-10-13 04:45:30 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Check if this was a simple allocated heap entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
if (!((ULONG_PTR)hMem & BASE_HEAP_IS_HANDLE_ENTRY)) return RetVal;
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2006-06-24 18:11:57 +00:00
|
|
|
/* Otherwise, lock the heap */
|
|
|
|
RtlLockHeap(hProcessHeap);
|
|
|
|
|
|
|
|
/* Get the handle entry */
|
2006-06-24 19:00:07 +00:00
|
|
|
HandleEntry = BaseHeapGetEntry(hMem);
|
2006-06-24 18:11:57 +00:00
|
|
|
BASE_TRACE_HANDLE(HandleEntry, hMem);
|
|
|
|
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Make sure it's valid */
|
|
|
|
if (!BaseHeapValidateEntry(HandleEntry))
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* It's not, fail */
|
|
|
|
BASE_TRACE_FAILURE();
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2006-06-24 18:11:57 +00:00
|
|
|
RetVal = FALSE;
|
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
else
|
2006-06-24 18:11:57 +00:00
|
|
|
{
|
2010-05-16 22:24:26 +00:00
|
|
|
/* Otherwise, decrement lock count, unless we're already at 0*/
|
|
|
|
if (!HandleEntry->LockCount--)
|
|
|
|
{
|
|
|
|
/* In which case we simply lock it back and fail */
|
|
|
|
HandleEntry->LockCount++;
|
|
|
|
SetLastError(ERROR_NOT_LOCKED);
|
|
|
|
RetVal = FALSE;
|
|
|
|
}
|
|
|
|
else if (!HandleEntry->LockCount)
|
|
|
|
{
|
|
|
|
/* Nothing to unlock */
|
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
RetVal = FALSE;
|
|
|
|
}
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-16 22:24:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
RetVal = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END
|
2006-06-24 18:11:57 +00:00
|
|
|
|
|
|
|
/* All done. Unlock the heap and return the pointer */
|
|
|
|
RtlUnlockHeap(hProcessHeap);
|
|
|
|
return RetVal;
|
|
|
|
}
|
2001-04-05 01:54:42 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
BOOL
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalUnWire(HGLOBAL hMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* This is simply an unlock */
|
|
|
|
return GlobalUnlock(hMem);
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2006-06-24 18:11:57 +00:00
|
|
|
LPVOID
|
|
|
|
NTAPI
|
2001-04-05 01:54:42 +00:00
|
|
|
GlobalWire(HGLOBAL hMem)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2006-06-24 18:11:57 +00:00
|
|
|
/* This is just a lock */
|
|
|
|
return GlobalLock(hMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
NTAPI
|
|
|
|
GlobalMemoryStatusEx(LPMEMORYSTATUSEX lpBuffer)
|
|
|
|
{
|
|
|
|
SYSTEM_PERFORMANCE_INFORMATION PerformanceInfo;
|
|
|
|
VM_COUNTERS VmCounters;
|
|
|
|
QUOTA_LIMITS QuotaLimits;
|
|
|
|
ULONGLONG PageFile, PhysicalMemory;
|
|
|
|
|
|
|
|
/* Query performance information */
|
|
|
|
NtQuerySystemInformation(SystemPerformanceInformation,
|
|
|
|
&PerformanceInfo,
|
|
|
|
sizeof(PerformanceInfo),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Calculate memory load */
|
|
|
|
lpBuffer->dwMemoryLoad = ((DWORD)(BaseCachedSysInfo.NumberOfPhysicalPages -
|
|
|
|
PerformanceInfo.AvailablePages) * 100) /
|
|
|
|
BaseCachedSysInfo.NumberOfPhysicalPages;
|
|
|
|
|
|
|
|
/* Save physical memory */
|
|
|
|
PhysicalMemory = BaseCachedSysInfo.NumberOfPhysicalPages *
|
|
|
|
BaseCachedSysInfo.PageSize;
|
|
|
|
lpBuffer->ullTotalPhys = PhysicalMemory;
|
|
|
|
|
|
|
|
/* Now save available physical memory */
|
|
|
|
PhysicalMemory = PerformanceInfo.AvailablePages *
|
|
|
|
BaseCachedSysInfo.PageSize;
|
|
|
|
lpBuffer->ullAvailPhys = PhysicalMemory;
|
|
|
|
|
|
|
|
/* Query VM and Quota Limits */
|
|
|
|
NtQueryInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessQuotaLimits,
|
|
|
|
&QuotaLimits,
|
|
|
|
sizeof(QUOTA_LIMITS),
|
|
|
|
NULL);
|
|
|
|
NtQueryInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessVmCounters,
|
|
|
|
&VmCounters,
|
|
|
|
sizeof(VM_COUNTERS),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Save the commit limit */
|
|
|
|
lpBuffer->ullTotalPageFile = min(QuotaLimits.PagefileLimit,
|
|
|
|
PerformanceInfo.CommitLimit);
|
|
|
|
|
|
|
|
/* Calculate how many pages are left */
|
|
|
|
PageFile = PerformanceInfo.CommitLimit - PerformanceInfo.CommittedPages;
|
|
|
|
|
|
|
|
/* Save the total */
|
|
|
|
lpBuffer->ullAvailPageFile = min(PageFile,
|
|
|
|
QuotaLimits.PagefileLimit -
|
|
|
|
VmCounters.PagefileUsage);
|
|
|
|
lpBuffer->ullAvailPageFile *= BaseCachedSysInfo.PageSize;
|
|
|
|
|
|
|
|
/* Now calculate the total virtual space */
|
|
|
|
lpBuffer->ullTotalVirtual = (BaseCachedSysInfo.MaximumUserModeAddress -
|
|
|
|
BaseCachedSysInfo.MinimumUserModeAddress) + 1;
|
|
|
|
|
|
|
|
/* And finally the avilable virtual space */
|
|
|
|
lpBuffer->ullAvailVirtual = lpBuffer->ullTotalVirtual -
|
|
|
|
VmCounters.VirtualSize;
|
|
|
|
lpBuffer->ullAvailExtendedVirtual = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
|
|
|
|
{
|
|
|
|
MEMORYSTATUSEX lpBufferEx;
|
|
|
|
|
|
|
|
/* Call the extended function */
|
|
|
|
lpBufferEx.dwLength = sizeof(MEMORYSTATUSEX);
|
|
|
|
if (GlobalMemoryStatusEx(&lpBufferEx))
|
|
|
|
{
|
|
|
|
/* Reset the right size and fill out the information */
|
|
|
|
lpBuffer->dwLength = sizeof(MEMORYSTATUS);
|
|
|
|
lpBuffer->dwMemoryLoad = lpBufferEx.dwMemoryLoad;
|
2007-01-26 07:22:19 +00:00
|
|
|
lpBuffer->dwTotalPhys = (SIZE_T)lpBufferEx.ullTotalPhys;
|
|
|
|
lpBuffer->dwAvailPhys = (SIZE_T)lpBufferEx.ullAvailPhys;
|
|
|
|
lpBuffer->dwTotalPageFile = (SIZE_T)lpBufferEx.ullTotalPageFile;
|
|
|
|
lpBuffer->dwAvailPageFile = (SIZE_T)lpBufferEx.ullAvailPageFile;
|
|
|
|
lpBuffer->dwTotalVirtual = (SIZE_T)lpBufferEx.ullTotalVirtual;
|
|
|
|
lpBuffer->dwAvailVirtual = (SIZE_T)lpBufferEx.ullAvailVirtual;
|
2006-06-24 18:11:57 +00:00
|
|
|
}
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
2000-07-01 17:07:02 +00:00
|
|
|
/* EOF */
|