2003-09-16 22:34:14 +02:00
|
|
|
/*
|
|
|
|
* Win32 heap functions
|
|
|
|
*
|
|
|
|
* Copyright 1995, 1996 Alexandre Julliard
|
|
|
|
* Copyright 1996 Huw Davies
|
|
|
|
* Copyright 1998 Ulrich Weigand
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2003-11-27 01:59:36 +01:00
|
|
|
#include "wine/port.h"
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
2007-03-08 13:12:59 +01:00
|
|
|
#include <limits.h>
|
2003-09-16 22:34:14 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-09-18 22:58:41 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2014-02-17 03:43:45 +01:00
|
|
|
#ifdef HAVE_MACH_MACH_H
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
2003-09-16 22:34:14 +02:00
|
|
|
|
2005-11-08 17:10:01 +01:00
|
|
|
#ifdef sun
|
|
|
|
/* FIXME: Unfortunately swapctl can't be used with largefile.... */
|
|
|
|
# undef _FILE_OFFSET_BITS
|
|
|
|
# define _FILE_OFFSET_BITS 32
|
2006-03-02 19:37:37 +01:00
|
|
|
# ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
# include <sys/resource.h>
|
|
|
|
# endif
|
2005-11-11 11:53:30 +01:00
|
|
|
# ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# endif
|
2005-11-08 17:10:01 +01:00
|
|
|
# include <sys/swap.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "winnt.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(heap);
|
2012-05-02 21:32:14 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(globalmem);
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
/* address where we try to map the system heap */
|
2004-05-25 07:13:44 +02:00
|
|
|
#define SYSTEM_HEAP_BASE ((void*)0x80000000)
|
|
|
|
#define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
static HANDLE systemHeap; /* globally shared heap */
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_CreateSystemHeap
|
|
|
|
*
|
|
|
|
* Create the system heap.
|
|
|
|
*/
|
2007-03-17 11:22:45 +01:00
|
|
|
static inline HANDLE HEAP_CreateSystemHeap(void)
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
|
|
|
int created;
|
|
|
|
void *base;
|
|
|
|
HANDLE map, event;
|
2004-04-29 22:02:01 +02:00
|
|
|
|
|
|
|
/* create the system heap event first */
|
2004-05-25 07:13:44 +02:00
|
|
|
event = CreateEventA( NULL, TRUE, FALSE, "__wine_system_heap_event" );
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
|
2004-05-25 07:13:44 +02:00
|
|
|
0, SYSTEM_HEAP_SIZE, "__wine_system_heap" ))) return 0;
|
2003-09-16 22:34:14 +02:00
|
|
|
created = (GetLastError() != ERROR_ALREADY_EXISTS);
|
|
|
|
|
|
|
|
if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
|
|
|
|
{
|
|
|
|
/* pre-defined address not available */
|
|
|
|
ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (created) /* newly created heap */
|
|
|
|
{
|
|
|
|
systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
|
|
|
|
SYSTEM_HEAP_SIZE, NULL, NULL );
|
2004-04-29 22:02:01 +02:00
|
|
|
SetEvent( event );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* wait for the heap to be initialized */
|
|
|
|
WaitForSingleObject( event, INFINITE );
|
2007-12-13 23:29:37 +01:00
|
|
|
systemHeap = base;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
CloseHandle( map );
|
|
|
|
return systemHeap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapCreate (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Create a heap object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle of heap: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
HANDLE WINAPI HeapCreate(
|
|
|
|
DWORD flags, /* [in] Heap allocation flag */
|
|
|
|
SIZE_T initialSize, /* [in] Initial heap size */
|
|
|
|
SIZE_T maxSize /* [in] Maximum heap size */
|
|
|
|
) {
|
|
|
|
HANDLE ret;
|
|
|
|
|
|
|
|
if ( flags & HEAP_SHARED )
|
|
|
|
{
|
|
|
|
if (!systemHeap) HEAP_CreateSystemHeap();
|
|
|
|
else WARN( "Shared Heap requested, returning system heap.\n" );
|
|
|
|
ret = systemHeap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
|
|
|
|
if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapDestroy (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Destroy a heap object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
|
|
|
|
{
|
|
|
|
if (heap == systemHeap)
|
|
|
|
{
|
|
|
|
WARN( "attempt to destroy system heap, returning TRUE!\n" );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (!RtlDestroyHeap( heap )) return TRUE;
|
|
|
|
SetLastError( ERROR_INVALID_HANDLE );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapCompact (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
|
|
|
|
{
|
|
|
|
return RtlCompactHeap( heap, flags );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapValidate (KERNEL32.@)
|
|
|
|
* Validates a specified heap.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Flags is ignored.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
BOOL WINAPI HeapValidate(
|
|
|
|
HANDLE heap, /* [in] Handle to the heap */
|
|
|
|
DWORD flags, /* [in] Bit flags that control access during operation */
|
|
|
|
LPCVOID block /* [in] Optional pointer to memory block to validate */
|
|
|
|
) {
|
|
|
|
return RtlValidateHeap( heap, flags, block );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapWalk (KERNEL32.@)
|
|
|
|
* Enumerates the memory blocks in a specified heap.
|
|
|
|
*
|
|
|
|
* TODO
|
|
|
|
* - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
|
|
|
|
* PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
BOOL WINAPI HeapWalk(
|
|
|
|
HANDLE heap, /* [in] Handle to heap to enumerate */
|
|
|
|
LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
|
|
|
|
) {
|
|
|
|
NTSTATUS ret = RtlWalkHeap( heap, entry );
|
|
|
|
if (ret) SetLastError( RtlNtStatusToDosError(ret) );
|
|
|
|
return !ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 05:57:02 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* HeapLock (KERNEL32.@)
|
|
|
|
* Attempts to acquire the critical section object for a specified heap.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
BOOL WINAPI HeapLock(
|
|
|
|
HANDLE heap /* [in] Handle of heap to lock for exclusive access */
|
|
|
|
) {
|
|
|
|
return RtlLockHeap( heap );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapUnlock (KERNEL32.@)
|
|
|
|
* Releases ownership of the critical section object.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
BOOL WINAPI HeapUnlock(
|
|
|
|
HANDLE heap /* [in] Handle to the heap to unlock */
|
|
|
|
) {
|
|
|
|
return RtlUnlockHeap( heap );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* GetProcessHeaps (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
|
|
|
|
{
|
|
|
|
return RtlGetProcessHeaps( count, heaps );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 05:57:02 +02:00
|
|
|
/* These are needed so that we can call the functions from inside kernel itself */
|
|
|
|
|
2006-11-12 20:23:28 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* HeapAlloc (KERNEL32.@)
|
|
|
|
*/
|
2003-10-08 05:57:02 +02:00
|
|
|
LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
|
|
|
|
{
|
|
|
|
return RtlAllocateHeap( heap, flags, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
|
|
|
|
{
|
|
|
|
return RtlFreeHeap( heap, flags, ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
|
|
|
|
{
|
|
|
|
return RtlReAllocateHeap( heap, flags, ptr, size );
|
|
|
|
}
|
|
|
|
|
2007-05-02 22:24:46 +02:00
|
|
|
SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPCVOID ptr )
|
2003-10-08 05:57:02 +02:00
|
|
|
{
|
|
|
|
return RtlSizeHeap( heap, flags, ptr );
|
|
|
|
}
|
|
|
|
|
2009-10-12 10:20:57 +02:00
|
|
|
BOOL WINAPI HeapQueryInformation( HANDLE heap, HEAP_INFORMATION_CLASS info_class,
|
|
|
|
PVOID info, SIZE_T size_in, PSIZE_T size_out)
|
|
|
|
{
|
|
|
|
NTSTATUS ret = RtlQueryHeapInformation( heap, info_class, info, size_in, size_out );
|
|
|
|
if (ret) SetLastError( RtlNtStatusToDosError(ret) );
|
|
|
|
return !ret;
|
|
|
|
}
|
|
|
|
|
2006-11-02 09:23:26 +01:00
|
|
|
BOOL WINAPI HeapSetInformation( HANDLE heap, HEAP_INFORMATION_CLASS infoclass, PVOID info, SIZE_T size)
|
|
|
|
{
|
2014-12-15 18:15:07 +01:00
|
|
|
NTSTATUS ret = RtlSetHeapInformation( heap, infoclass, info, size );
|
|
|
|
if (ret) SetLastError( RtlNtStatusToDosError(ret) );
|
|
|
|
return !ret;
|
2006-11-02 09:23:26 +01:00
|
|
|
}
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
/*
|
|
|
|
* Win32 Global heap functions (GlobalXXX).
|
|
|
|
* These functions included in Win32 for compatibility with 16 bit Windows
|
|
|
|
* Especially the moveable blocks and handles are oldish.
|
|
|
|
* But the ability to directly allocate memory with GPTR and LPTR is widely
|
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* The handle stuff looks horrible, but it's implemented almost like Win95
|
|
|
|
* does it.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAGIC_GLOBAL_USED 0x5342
|
|
|
|
#define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
|
2009-03-11 09:39:16 +01:00
|
|
|
#define INTERN_TO_HANDLE(i) (&((i)->Pointer))
|
2006-09-14 18:39:53 +02:00
|
|
|
#define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
|
2003-09-16 22:34:14 +02:00
|
|
|
#define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
|
|
|
|
#define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
|
|
|
|
/* align the storage needed for the HGLOBAL on an 8byte boundary thus
|
|
|
|
* GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
|
|
|
|
* size = 8*k, where k=1,2,3,... alloc's exactly the given size.
|
|
|
|
* The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
|
|
|
|
* the output jpeg's > 1 MB if not */
|
2009-06-17 20:53:46 +02:00
|
|
|
#define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
|
2003-09-16 22:34:14 +02:00
|
|
|
|
2004-01-09 23:18:49 +01:00
|
|
|
#include "pshpack1.h"
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
typedef struct __GLOBAL32_INTERN
|
|
|
|
{
|
|
|
|
WORD Magic;
|
2004-01-09 23:18:49 +01:00
|
|
|
LPVOID Pointer;
|
2003-09-16 22:34:14 +02:00
|
|
|
BYTE Flags;
|
|
|
|
BYTE LockCount;
|
|
|
|
} GLOBAL32_INTERN, *PGLOBAL32_INTERN;
|
|
|
|
|
2004-01-09 23:18:49 +01:00
|
|
|
#include "poppack.h"
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalAlloc (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Allocate a global memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
HGLOBAL WINAPI GlobalAlloc(
|
|
|
|
UINT flags, /* [in] Object allocation attributes */
|
|
|
|
SIZE_T size /* [in] Number of bytes to allocate */
|
|
|
|
) {
|
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
DWORD hpflags;
|
|
|
|
LPVOID palloc;
|
|
|
|
|
|
|
|
if(flags&GMEM_ZEROINIT)
|
|
|
|
hpflags=HEAP_ZERO_MEMORY;
|
|
|
|
else
|
|
|
|
hpflags=0;
|
|
|
|
|
|
|
|
if((flags & GMEM_MOVEABLE)==0) /* POINTER */
|
|
|
|
{
|
2016-09-13 13:48:08 +02:00
|
|
|
palloc = HeapAlloc( GetProcessHeap(), hpflags, max( 1, size ));
|
2007-10-02 13:31:59 +02:00
|
|
|
TRACE( "(flags=%04x) returning %p\n", flags, palloc );
|
2007-12-13 23:29:37 +01:00
|
|
|
return palloc;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else /* HANDLE */
|
|
|
|
{
|
2006-12-18 00:47:06 +01:00
|
|
|
if (size > INT_MAX-HGLOBAL_STORAGE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
|
|
|
|
if (pintern)
|
|
|
|
{
|
2009-05-27 19:27:56 +02:00
|
|
|
/* Mask out obsolete flags */
|
|
|
|
flags &= ~(GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NOT_BANKED | GMEM_NOTIFY);
|
|
|
|
|
2003-09-16 22:34:14 +02:00
|
|
|
pintern->Magic = MAGIC_GLOBAL_USED;
|
|
|
|
pintern->Flags = flags >> 8;
|
|
|
|
pintern->LockCount = 0;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
|
|
|
|
if (!palloc)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, pintern);
|
|
|
|
pintern = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
|
|
|
|
pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pintern->Pointer = NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-02 13:31:59 +02:00
|
|
|
if (!pintern) return 0;
|
|
|
|
TRACE( "(flags=%04x) returning handle %p pointer %p\n",
|
|
|
|
flags, INTERN_TO_HANDLE(pintern), pintern->Pointer );
|
|
|
|
return INTERN_TO_HANDLE(pintern);
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalLock (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
2006-04-14 09:57:19 +02:00
|
|
|
* Lock a global memory object and return a pointer to first byte of the memory
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* hmem [I] Handle of the global memory object
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
2006-04-14 09:57:19 +02:00
|
|
|
* Success: Pointer to first byte of the memory block
|
|
|
|
* Failure: NULL
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2006-04-14 09:57:19 +02:00
|
|
|
LPVOID WINAPI GlobalLock(HGLOBAL hmem)
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
LPVOID palloc;
|
|
|
|
|
|
|
|
if (ISPOINTER(hmem))
|
|
|
|
return IsBadReadPtr(hmem, 1) ? NULL : hmem;
|
|
|
|
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
pintern = HANDLE_TO_INTERN(hmem);
|
|
|
|
if (pintern->Magic == MAGIC_GLOBAL_USED)
|
|
|
|
{
|
|
|
|
palloc = pintern->Pointer;
|
2006-01-09 17:16:32 +01:00
|
|
|
if (!pintern->Pointer)
|
|
|
|
SetLastError(ERROR_DISCARDED);
|
2006-01-10 19:59:15 +01:00
|
|
|
else if (pintern->LockCount < GMEM_LOCKCOUNT)
|
|
|
|
pintern->LockCount++;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-14 09:57:19 +02:00
|
|
|
WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
|
2003-09-16 22:34:14 +02:00
|
|
|
palloc = NULL;
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
2006-04-14 09:57:19 +02:00
|
|
|
WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
|
2003-09-16 22:34:14 +02:00
|
|
|
palloc = NULL;
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
return palloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalUnlock (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Unlock a global memory object.
|
|
|
|
*
|
2006-04-14 09:57:19 +02:00
|
|
|
* PARAMS
|
|
|
|
* hmem [I] Handle of the global memory object
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
2006-04-14 09:57:19 +02:00
|
|
|
* Success: Object is still locked
|
|
|
|
* Failure: FALSE (The Object is unlocked)
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2006-04-14 09:57:19 +02:00
|
|
|
BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
|
|
|
|
{
|
2003-09-16 22:34:14 +02:00
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
BOOL locked;
|
|
|
|
|
2007-09-17 01:30:20 +02:00
|
|
|
if (ISPOINTER(hmem)) return TRUE;
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
if(pintern->Magic==MAGIC_GLOBAL_USED)
|
|
|
|
{
|
2006-01-10 19:59:15 +01:00
|
|
|
if(pintern->LockCount)
|
|
|
|
{
|
2003-09-16 22:34:14 +02:00
|
|
|
pintern->LockCount--;
|
2006-01-10 19:59:15 +01:00
|
|
|
locked = (pintern->LockCount != 0);
|
|
|
|
if (!locked) SetLastError(NO_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WARN("%p not locked\n", hmem);
|
|
|
|
SetLastError(ERROR_NOT_LOCKED);
|
|
|
|
locked = FALSE;
|
|
|
|
}
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-14 09:57:19 +02:00
|
|
|
WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
|
2003-09-16 22:34:14 +02:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
locked=FALSE;
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
2006-04-14 09:57:19 +02:00
|
|
|
WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
|
2003-09-16 22:34:14 +02:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
locked=FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalHandle (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get the handle associated with the pointer to a global memory block.
|
2003-09-16 22:34:14 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
HGLOBAL WINAPI GlobalHandle(
|
|
|
|
LPCVOID pmem /* [in] Pointer to global memory block */
|
|
|
|
) {
|
|
|
|
HGLOBAL handle;
|
|
|
|
PGLOBAL32_INTERN maybe_intern;
|
|
|
|
LPCVOID test;
|
|
|
|
|
|
|
|
if (!pmem)
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
handle = 0;
|
|
|
|
|
2006-10-15 17:05:01 +02:00
|
|
|
/* note that if pmem is a pointer to a block allocated by */
|
2003-09-16 22:34:14 +02:00
|
|
|
/* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
|
|
|
|
/* will fail. */
|
|
|
|
if (ISPOINTER(pmem)) {
|
2016-09-13 13:48:49 +02:00
|
|
|
if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, pmem )) {
|
2003-09-16 22:34:14 +02:00
|
|
|
handle = (HGLOBAL)pmem; /* valid fixed block */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
handle = POINTER_TO_HANDLE(pmem);
|
|
|
|
} else
|
|
|
|
handle = (HGLOBAL)pmem;
|
|
|
|
|
|
|
|
/* Now test handle either passed in or retrieved from pointer */
|
|
|
|
maybe_intern = HANDLE_TO_INTERN( handle );
|
|
|
|
if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
|
|
|
|
test = maybe_intern->Pointer;
|
2016-09-13 13:48:49 +02:00
|
|
|
if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
|
|
|
|
HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, maybe_intern )) /* intern valid arena? */
|
2003-09-16 22:34:14 +02:00
|
|
|
break; /* valid moveable block */
|
|
|
|
}
|
|
|
|
handle = 0;
|
|
|
|
SetLastError( ERROR_INVALID_HANDLE );
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_HANDLE );
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalReAlloc (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Change the size or attributes of a global memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
HGLOBAL WINAPI GlobalReAlloc(
|
|
|
|
HGLOBAL hmem, /* [in] Handle of global memory object */
|
|
|
|
SIZE_T size, /* [in] New size of block */
|
|
|
|
UINT flags /* [in] How to reallocate object */
|
|
|
|
) {
|
|
|
|
LPVOID palloc;
|
|
|
|
HGLOBAL hnew;
|
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
|
|
|
|
|
|
|
|
hnew = 0;
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
if(flags & GMEM_MODIFY) /* modify flags */
|
|
|
|
{
|
|
|
|
if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
|
|
|
|
{
|
|
|
|
/* make a fixed block moveable
|
|
|
|
* actually only NT is able to do this. But it's soo simple
|
|
|
|
*/
|
|
|
|
if (hmem == 0)
|
|
|
|
{
|
|
|
|
WARN("GlobalReAlloc with null handle!\n");
|
|
|
|
SetLastError( ERROR_NOACCESS );
|
|
|
|
hnew = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-13 23:29:37 +01:00
|
|
|
size = HeapSize(GetProcessHeap(), 0, hmem);
|
2003-09-16 22:34:14 +02:00
|
|
|
hnew = GlobalAlloc(flags, size);
|
|
|
|
palloc = GlobalLock(hnew);
|
2007-12-13 23:29:37 +01:00
|
|
|
memcpy(palloc, hmem, size);
|
2003-09-16 22:34:14 +02:00
|
|
|
GlobalUnlock(hnew);
|
|
|
|
GlobalFree(hmem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
|
|
|
|
{
|
|
|
|
/* change the flags to make our block "discardable" */
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
|
|
|
|
hnew=hmem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
hnew = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(ISPOINTER(hmem))
|
|
|
|
{
|
|
|
|
/* reallocate fixed memory */
|
2013-06-01 15:01:41 +02:00
|
|
|
if (!(flags & GMEM_MOVEABLE))
|
|
|
|
heap_flags |= HEAP_REALLOC_IN_PLACE_ONLY;
|
2005-03-24 22:01:35 +01:00
|
|
|
hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* reallocate a moveable block */
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Apparently Windows doesn't care whether the handle is locked at this point */
|
|
|
|
/* See also the same comment in GlobalFree() */
|
|
|
|
if(pintern->LockCount>1) {
|
|
|
|
ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if(size!=0)
|
|
|
|
{
|
|
|
|
hnew=hmem;
|
|
|
|
if(pintern->Pointer)
|
|
|
|
{
|
2006-12-18 00:47:06 +01:00
|
|
|
if(size > INT_MAX-HGLOBAL_STORAGE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
hnew = 0;
|
|
|
|
}
|
|
|
|
else if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
|
2003-09-16 22:34:14 +02:00
|
|
|
(char *) pintern->Pointer-HGLOBAL_STORAGE,
|
|
|
|
size+HGLOBAL_STORAGE)) == NULL)
|
|
|
|
hnew = 0; /* Block still valid */
|
|
|
|
else
|
|
|
|
pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-18 00:47:06 +01:00
|
|
|
if(size > INT_MAX-HGLOBAL_STORAGE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
hnew = 0;
|
|
|
|
}
|
|
|
|
else if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
|
2003-09-16 22:34:14 +02:00
|
|
|
== NULL)
|
|
|
|
hnew = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(HGLOBAL *)palloc = hmem;
|
|
|
|
pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-12 11:55:25 +01:00
|
|
|
if (pintern->LockCount == 0)
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
2006-01-12 11:55:25 +01:00
|
|
|
if(pintern->Pointer)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
|
|
|
|
pintern->Pointer = NULL;
|
|
|
|
}
|
|
|
|
hnew = hmem;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
2006-01-12 11:55:25 +01:00
|
|
|
else
|
|
|
|
WARN("not freeing memory associated with locked handle\n");
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
return hnew;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalFree (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Free a global memory object.
|
|
|
|
*
|
2006-03-19 19:35:36 +01:00
|
|
|
* PARAMS
|
|
|
|
* hmem [I] Handle of the global memory object
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
2006-03-19 19:35:36 +01:00
|
|
|
* Success: NULL
|
|
|
|
* Failure: The provided handle
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2006-03-19 19:35:36 +01:00
|
|
|
HGLOBAL WINAPI GlobalFree(HGLOBAL hmem)
|
|
|
|
{
|
2003-09-16 22:34:14 +02:00
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
HGLOBAL hreturned;
|
|
|
|
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
hreturned = 0;
|
|
|
|
if(ISPOINTER(hmem)) /* POINTER */
|
|
|
|
{
|
2016-09-13 13:48:49 +02:00
|
|
|
if(!HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, hmem))
|
2007-09-17 01:28:56 +02:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
hreturned = hmem;
|
|
|
|
}
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else /* HANDLE */
|
|
|
|
{
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
|
|
|
|
if(pintern->Magic==MAGIC_GLOBAL_USED)
|
|
|
|
{
|
2006-03-19 19:35:36 +01:00
|
|
|
pintern->Magic = 0xdead;
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
/* WIN98 does not make this test. That is you can free a */
|
|
|
|
/* block you have not unlocked. Go figure!! */
|
|
|
|
/* if(pintern->LockCount!=0) */
|
|
|
|
/* SetLastError(ERROR_INVALID_HANDLE); */
|
|
|
|
|
|
|
|
if(pintern->Pointer)
|
2016-09-13 13:48:49 +02:00
|
|
|
if(!HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
|
2003-09-16 22:34:14 +02:00
|
|
|
hreturned=hmem;
|
2016-09-13 13:48:49 +02:00
|
|
|
if(!HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, pintern))
|
2003-09-16 22:34:14 +02:00
|
|
|
hreturned=hmem;
|
|
|
|
}
|
2006-03-19 19:35:36 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
hreturned = hmem;
|
|
|
|
}
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-09-16 22:34:14 +02:00
|
|
|
{
|
2006-03-30 10:37:52 +02:00
|
|
|
ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
|
2003-09-16 22:34:14 +02:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
hreturned = hmem;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
return hreturned;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalSize (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get the size of a global memory object.
|
|
|
|
*
|
2006-03-31 12:27:30 +02:00
|
|
|
* PARAMS
|
|
|
|
* hmem [I] Handle of the global memory object
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
2006-03-31 12:27:30 +02:00
|
|
|
* Failure: 0
|
|
|
|
* Success: Size in Bytes of the global memory object
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2006-03-31 12:27:30 +02:00
|
|
|
SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
|
|
|
|
{
|
2011-06-13 11:40:53 +02:00
|
|
|
SIZE_T retval;
|
2003-09-16 22:34:14 +02:00
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
|
2007-09-20 08:49:26 +02:00
|
|
|
if (!((ULONG_PTR)hmem >> 16))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-16 22:34:14 +02:00
|
|
|
|
|
|
|
if(ISPOINTER(hmem))
|
|
|
|
{
|
2007-12-13 23:29:37 +01:00
|
|
|
retval=HeapSize(GetProcessHeap(), 0, hmem);
|
2011-06-13 08:22:15 +02:00
|
|
|
|
2011-06-13 11:40:53 +02:00
|
|
|
if (retval == ~0ul) /* It might be a GMEM_MOVEABLE data pointer */
|
2011-06-13 08:22:15 +02:00
|
|
|
{
|
2011-06-13 11:40:53 +02:00
|
|
|
retval = HeapSize(GetProcessHeap(), 0, (char*)hmem - HGLOBAL_STORAGE);
|
|
|
|
if (retval != ~0ul) retval -= HGLOBAL_STORAGE;
|
2011-06-13 08:22:15 +02:00
|
|
|
}
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
|
|
|
|
if(pintern->Magic==MAGIC_GLOBAL_USED)
|
|
|
|
{
|
|
|
|
if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
|
|
|
|
retval = 0;
|
|
|
|
else
|
|
|
|
{
|
2011-06-13 11:40:53 +02:00
|
|
|
retval = HeapSize(GetProcessHeap(), 0, (char *)pintern->Pointer - HGLOBAL_STORAGE );
|
|
|
|
if (retval != ~0ul) retval -= HGLOBAL_STORAGE;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-31 12:27:30 +02:00
|
|
|
WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2003-09-16 22:34:14 +02:00
|
|
|
retval=0;
|
|
|
|
}
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
}
|
2011-06-13 11:40:53 +02:00
|
|
|
if (retval == ~0ul) retval = 0;
|
2003-09-16 22:34:14 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalWire (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI GlobalWire(HGLOBAL hmem)
|
|
|
|
{
|
|
|
|
return GlobalLock( hmem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalUnWire (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
|
|
|
|
{
|
|
|
|
return GlobalUnlock( hmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalFix (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
VOID WINAPI GlobalFix(HGLOBAL hmem)
|
|
|
|
{
|
|
|
|
GlobalLock( hmem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalUnfix (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
VOID WINAPI GlobalUnfix(HGLOBAL hmem)
|
|
|
|
{
|
|
|
|
GlobalUnlock( hmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalFlags (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get information about a global memory object.
|
2003-09-16 22:34:14 +02:00
|
|
|
*
|
2006-03-29 20:48:39 +02:00
|
|
|
* PARAMS
|
|
|
|
* hmem [I] Handle of the global memory object
|
2003-09-16 22:34:14 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
2006-03-29 20:48:39 +02:00
|
|
|
* Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
|
|
|
|
* Success: Value specifying allocation flags and lock count
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2006-03-29 20:48:39 +02:00
|
|
|
UINT WINAPI GlobalFlags(HGLOBAL hmem)
|
|
|
|
{
|
2003-09-16 22:34:14 +02:00
|
|
|
DWORD retval;
|
|
|
|
PGLOBAL32_INTERN pintern;
|
|
|
|
|
|
|
|
if(ISPOINTER(hmem))
|
|
|
|
{
|
|
|
|
retval=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlLockHeap(GetProcessHeap());
|
|
|
|
pintern=HANDLE_TO_INTERN(hmem);
|
|
|
|
if(pintern->Magic==MAGIC_GLOBAL_USED)
|
|
|
|
{
|
|
|
|
retval=pintern->LockCount + (pintern->Flags<<8);
|
|
|
|
if(pintern->Pointer==0)
|
|
|
|
retval|= GMEM_DISCARDED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-29 20:48:39 +02:00
|
|
|
WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
retval = GMEM_INVALID_HANDLE;
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
RtlUnlockHeap(GetProcessHeap());
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GlobalCompact (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
SIZE_T WINAPI GlobalCompact( DWORD minfree )
|
|
|
|
{
|
|
|
|
return 0; /* GlobalCompact does nothing in Win32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalAlloc (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Allocate a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
2016-09-13 13:48:08 +02:00
|
|
|
HLOCAL WINAPI LocalAlloc( UINT flags, SIZE_T size )
|
|
|
|
{
|
|
|
|
/* LocalAlloc allows a 0-size fixed block, but GlobalAlloc doesn't */
|
|
|
|
if (!(flags & LMEM_MOVEABLE))
|
|
|
|
{
|
|
|
|
DWORD heap_flags = (flags & LMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
|
|
|
|
void *ret = HeapAlloc( GetProcessHeap(), heap_flags, size );
|
|
|
|
TRACE( "(flags=%04x) returning %p\n", flags, ret );
|
|
|
|
return ret;
|
|
|
|
}
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalAlloc( flags, size );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalCompact (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
SIZE_T WINAPI LocalCompact( UINT minfree )
|
|
|
|
{
|
|
|
|
return 0; /* LocalCompact does nothing in Win32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalFlags (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get information about a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Value specifying allocation flags and lock count.
|
|
|
|
* LMEM_INVALID_HANDLE: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
UINT WINAPI LocalFlags(
|
|
|
|
HLOCAL handle /* [in] Handle of memory object */
|
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalFlags( handle );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalFree (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Free a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* NULL: Success
|
|
|
|
* Handle: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
HLOCAL WINAPI LocalFree(
|
|
|
|
HLOCAL handle /* [in] Handle of memory object */
|
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalFree( handle );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalHandle (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get the handle associated with the pointer to a local memory block.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
HLOCAL WINAPI LocalHandle(
|
2005-11-08 12:01:03 +01:00
|
|
|
LPCVOID ptr /* [in] Address of local memory block */
|
2003-09-16 22:34:14 +02:00
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalHandle( ptr );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalLock (KERNEL32.@)
|
|
|
|
* Locks a local memory object and returns pointer to the first byte
|
|
|
|
* of the memory block.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Pointer: Success
|
|
|
|
* NULL: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
LPVOID WINAPI LocalLock(
|
|
|
|
HLOCAL handle /* [in] Address of local memory object */
|
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalLock( handle );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalReAlloc (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Change the size or attributes of a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Handle: Success
|
|
|
|
* NULL: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
HLOCAL WINAPI LocalReAlloc(
|
|
|
|
HLOCAL handle, /* [in] Handle of memory object */
|
|
|
|
SIZE_T size, /* [in] New size of block */
|
|
|
|
UINT flags /* [in] How to reallocate object */
|
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalReAlloc( handle, size, flags );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalShrink (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
|
|
|
|
{
|
|
|
|
return 0; /* LocalShrink does nothing in Win32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalSize (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Get the size of a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* Size: Success
|
|
|
|
* 0: Failure
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
SIZE_T WINAPI LocalSize(
|
|
|
|
HLOCAL handle /* [in] Handle of memory object */
|
|
|
|
) {
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalSize( handle );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* LocalUnlock (KERNEL32.@)
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* Unlock a local memory object.
|
|
|
|
*
|
2003-09-16 22:34:14 +02:00
|
|
|
* RETURNS
|
|
|
|
* TRUE: Object is still locked
|
|
|
|
* FALSE: Object is unlocked
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Windows memory management does not provide a separate local heap
|
|
|
|
* and global heap.
|
2003-09-16 22:34:14 +02:00
|
|
|
*/
|
|
|
|
BOOL WINAPI LocalUnlock(
|
|
|
|
HLOCAL handle /* [in] Handle of memory object */
|
2010-05-09 00:21:50 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (ISPOINTER( handle ))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_LOCKED );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-08 23:23:13 +01:00
|
|
|
return GlobalUnlock( handle );
|
2003-09-16 22:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-18 22:58:41 +02:00
|
|
|
/***********************************************************************
|
2004-08-13 02:41:34 +02:00
|
|
|
* GlobalMemoryStatusEx (KERNEL32.@)
|
|
|
|
* A version of GlobalMemoryStatus that can deal with memory over 4GB
|
2003-09-18 22:58:41 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
2004-08-13 02:41:34 +02:00
|
|
|
* TRUE
|
2003-09-18 22:58:41 +02:00
|
|
|
*/
|
2004-08-13 02:41:34 +02:00
|
|
|
BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
|
|
|
|
{
|
|
|
|
static MEMORYSTATUSEX cached_memstatus;
|
2003-09-18 22:58:41 +02:00
|
|
|
static int cache_lastchecked = 0;
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
#ifdef linux
|
|
|
|
FILE *f;
|
2014-02-17 03:43:35 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
|
2014-02-17 03:43:38 +01:00
|
|
|
DWORDLONG total;
|
2014-02-17 03:43:35 +01:00
|
|
|
#ifdef __APPLE__
|
2006-06-15 11:38:20 +02:00
|
|
|
unsigned int val;
|
2014-02-17 03:43:35 +01:00
|
|
|
#else
|
|
|
|
unsigned long val;
|
|
|
|
#endif
|
2006-06-15 11:38:20 +02:00
|
|
|
int mib[2];
|
2006-06-09 11:52:50 +02:00
|
|
|
size_t size_sys;
|
2014-02-17 03:43:38 +01:00
|
|
|
#ifdef HW_MEMSIZE
|
|
|
|
uint64_t val64;
|
|
|
|
#endif
|
2014-02-17 03:43:42 +01:00
|
|
|
#ifdef VM_SWAPUSAGE
|
|
|
|
struct xsw_usage swap;
|
|
|
|
#endif
|
2006-06-09 11:52:50 +02:00
|
|
|
#elif defined(sun)
|
2006-06-15 11:38:20 +02:00
|
|
|
unsigned long pagesize,maxpages,freepages,swapspace,swapfree;
|
2005-11-08 17:10:01 +01:00
|
|
|
struct anoninfo swapinf;
|
|
|
|
int rval;
|
|
|
|
#endif
|
|
|
|
|
2007-10-15 22:32:36 +02:00
|
|
|
if (lpmemex->dwLength != sizeof(*lpmemex))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-09-18 22:58:41 +02:00
|
|
|
if (time(NULL)==cache_lastchecked) {
|
2008-03-03 23:27:46 +01:00
|
|
|
*lpmemex = cached_memstatus;
|
2004-08-13 02:41:34 +02:00
|
|
|
return TRUE;
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
|
|
|
cache_lastchecked = time(NULL);
|
|
|
|
|
2004-08-13 02:41:34 +02:00
|
|
|
lpmemex->dwMemoryLoad = 0;
|
|
|
|
lpmemex->ullTotalPhys = 16*1024*1024;
|
|
|
|
lpmemex->ullAvailPhys = 16*1024*1024;
|
|
|
|
lpmemex->ullTotalPageFile = 16*1024*1024;
|
|
|
|
lpmemex->ullAvailPageFile = 16*1024*1024;
|
2003-09-18 22:58:41 +02:00
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
f = fopen( "/proc/meminfo", "r" );
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
2006-06-15 11:38:20 +02:00
|
|
|
unsigned long total, used, free, shared, buffers, cached;
|
2003-09-18 22:58:41 +02:00
|
|
|
|
2004-08-13 02:41:34 +02:00
|
|
|
lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
|
|
|
|
lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
|
2003-09-18 22:58:41 +02:00
|
|
|
while (fgets( buffer, sizeof(buffer), f ))
|
|
|
|
{
|
|
|
|
/* old style /proc/meminfo ... */
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf( buffer, "Mem: %lu %lu %lu %lu %lu %lu",
|
|
|
|
&total, &used, &free, &shared, &buffers, &cached ))
|
2003-09-18 22:58:41 +02:00
|
|
|
{
|
2004-08-13 02:41:34 +02:00
|
|
|
lpmemex->ullTotalPhys += total;
|
|
|
|
lpmemex->ullAvailPhys += free + buffers + cached;
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf( buffer, "Swap: %lu %lu %lu", &total, &used, &free ))
|
2003-09-18 22:58:41 +02:00
|
|
|
{
|
2004-08-13 02:41:34 +02:00
|
|
|
lpmemex->ullTotalPageFile += total;
|
|
|
|
lpmemex->ullAvailPageFile += free;
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* new style /proc/meminfo ... */
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "MemTotal: %lu", &total))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullTotalPhys = (ULONG64)total*1024;
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "MemFree: %lu", &free))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullAvailPhys = (ULONG64)free*1024;
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "SwapTotal: %lu", &total))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullTotalPageFile = (ULONG64)total*1024;
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "SwapFree: %lu", &free))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullAvailPageFile = (ULONG64)free*1024;
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "Buffers: %lu", &buffers))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullAvailPhys += (ULONG64)buffers*1024;
|
2006-06-15 11:38:20 +02:00
|
|
|
if (sscanf(buffer, "Cached: %lu", &cached))
|
2011-09-10 11:22:59 +02:00
|
|
|
lpmemex->ullAvailPhys += (ULONG64)cached*1024;
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
|
|
|
fclose( f );
|
|
|
|
}
|
2012-05-28 19:21:08 +02:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
|
2014-02-17 03:43:38 +01:00
|
|
|
total = 0;
|
2014-02-19 11:35:16 +01:00
|
|
|
lpmemex->ullAvailPhys = 0;
|
2014-02-17 03:43:38 +01:00
|
|
|
|
2006-06-09 11:52:50 +02:00
|
|
|
mib[0] = CTL_HW;
|
2014-02-17 03:43:38 +01:00
|
|
|
#ifdef HW_MEMSIZE
|
|
|
|
mib[1] = HW_MEMSIZE;
|
|
|
|
size_sys = sizeof(val64);
|
|
|
|
if (!sysctl(mib, 2, &val64, &size_sys, NULL, 0) && size_sys == sizeof(val64) && val64)
|
|
|
|
total = val64;
|
|
|
|
#endif
|
|
|
|
|
2014-02-17 03:43:45 +01:00
|
|
|
#ifdef HAVE_MACH_MACH_H
|
|
|
|
{
|
2014-02-19 11:35:16 +01:00
|
|
|
host_name_port_t host = mach_host_self();
|
|
|
|
mach_msg_type_number_t count;
|
2014-02-17 03:43:48 +01:00
|
|
|
|
2014-02-19 11:35:16 +01:00
|
|
|
#ifdef HOST_VM_INFO64_COUNT
|
|
|
|
vm_size_t page_size;
|
|
|
|
vm_statistics64_data_t vm_stat;
|
|
|
|
|
|
|
|
count = HOST_VM_INFO64_COUNT;
|
|
|
|
if (host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) == KERN_SUCCESS &&
|
2014-02-19 23:12:11 +01:00
|
|
|
host_page_size(host, &page_size) == KERN_SUCCESS)
|
2014-02-19 11:35:16 +01:00
|
|
|
lpmemex->ullAvailPhys = (vm_stat.free_count + vm_stat.inactive_count) * (DWORDLONG)page_size;
|
|
|
|
#endif
|
|
|
|
if (!total)
|
|
|
|
{
|
|
|
|
host_basic_info_data_t info;
|
|
|
|
count = HOST_BASIC_INFO_COUNT;
|
|
|
|
if (host_info(host, HOST_BASIC_INFO, (host_info_t)&info, &count) == KERN_SUCCESS)
|
|
|
|
total = info.max_mem;
|
|
|
|
}
|
2014-02-17 03:43:48 +01:00
|
|
|
|
2014-02-19 11:35:16 +01:00
|
|
|
mach_port_deallocate(mach_task_self(), host);
|
|
|
|
}
|
2014-02-17 03:43:45 +01:00
|
|
|
#endif
|
|
|
|
|
2014-02-17 03:43:38 +01:00
|
|
|
if (!total)
|
|
|
|
{
|
|
|
|
mib[1] = HW_PHYSMEM;
|
|
|
|
size_sys = sizeof(val);
|
|
|
|
if (!sysctl(mib, 2, &val, &size_sys, NULL, 0) && size_sys == sizeof(val) && val)
|
|
|
|
total = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total)
|
|
|
|
lpmemex->ullTotalPhys = total;
|
|
|
|
|
2014-02-17 03:43:48 +01:00
|
|
|
if (!lpmemex->ullAvailPhys)
|
|
|
|
{
|
|
|
|
mib[1] = HW_USERMEM;
|
|
|
|
size_sys = sizeof(val);
|
|
|
|
if (!sysctl(mib, 2, &val, &size_sys, NULL, 0) && size_sys == sizeof(val) && val)
|
|
|
|
lpmemex->ullAvailPhys = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lpmemex->ullAvailPhys)
|
2014-02-17 03:43:32 +01:00
|
|
|
lpmemex->ullAvailPhys = lpmemex->ullTotalPhys;
|
|
|
|
|
|
|
|
lpmemex->ullTotalPageFile = lpmemex->ullAvailPhys;
|
|
|
|
lpmemex->ullAvailPageFile = lpmemex->ullAvailPhys;
|
2014-02-17 03:43:42 +01:00
|
|
|
|
|
|
|
#ifdef VM_SWAPUSAGE
|
|
|
|
mib[0] = CTL_VM;
|
|
|
|
mib[1] = VM_SWAPUSAGE;
|
|
|
|
size_sys = sizeof(swap);
|
|
|
|
if (!sysctl(mib, 2, &swap, &size_sys, NULL, 0) && size_sys == sizeof(swap))
|
|
|
|
{
|
|
|
|
lpmemex->ullTotalPageFile = swap.xsu_total;
|
|
|
|
lpmemex->ullAvailPageFile = swap.xsu_avail;
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-08 17:10:01 +01:00
|
|
|
#elif defined ( sun )
|
|
|
|
pagesize=sysconf(_SC_PAGESIZE);
|
|
|
|
maxpages=sysconf(_SC_PHYS_PAGES);
|
|
|
|
freepages=sysconf(_SC_AVPHYS_PAGES);
|
|
|
|
rval=swapctl(SC_AINFO, &swapinf);
|
|
|
|
if(rval >-1)
|
|
|
|
{
|
|
|
|
swapspace=swapinf.ani_max*pagesize;
|
|
|
|
swapfree=swapinf.ani_free*pagesize;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
|
|
|
|
WARN("Swap size cannot be determined , assuming equal to physical memory\n");
|
|
|
|
swapspace=maxpages*pagesize;
|
|
|
|
swapfree=maxpages*pagesize;
|
|
|
|
}
|
|
|
|
lpmemex->ullTotalPhys=pagesize*maxpages;
|
|
|
|
lpmemex->ullAvailPhys = pagesize*freepages;
|
|
|
|
lpmemex->ullTotalPageFile = swapspace;
|
|
|
|
lpmemex->ullAvailPageFile = swapfree;
|
2003-09-18 22:58:41 +02:00
|
|
|
#endif
|
2004-06-24 06:08:33 +02:00
|
|
|
|
2007-03-15 00:20:37 +01:00
|
|
|
if (lpmemex->ullTotalPhys)
|
|
|
|
{
|
|
|
|
lpmemex->dwMemoryLoad = (lpmemex->ullTotalPhys-lpmemex->ullAvailPhys)
|
|
|
|
/ (lpmemex->ullTotalPhys / 100);
|
|
|
|
}
|
|
|
|
|
2007-03-15 00:21:25 +01:00
|
|
|
/* Win98 returns only the swapsize in ullTotalPageFile/ullAvailPageFile,
|
|
|
|
WinXP returns the size of physical memory + swapsize;
|
|
|
|
mimic the behavior of XP.
|
|
|
|
Note: Project2k refuses to start if it sees less than 1Mb of free swap.
|
|
|
|
*/
|
|
|
|
lpmemex->ullTotalPageFile += lpmemex->ullTotalPhys;
|
|
|
|
lpmemex->ullAvailPageFile += lpmemex->ullAvailPhys;
|
2005-08-16 18:00:36 +02:00
|
|
|
|
2007-03-15 00:22:53 +01:00
|
|
|
/* Titan Quest refuses to run if TotalPageFile <= ullTotalPhys */
|
|
|
|
if(lpmemex->ullTotalPageFile == lpmemex->ullTotalPhys)
|
|
|
|
{
|
|
|
|
lpmemex->ullTotalPhys -= 1;
|
|
|
|
lpmemex->ullAvailPhys -= 1;
|
|
|
|
}
|
|
|
|
|
2003-09-18 22:58:41 +02:00
|
|
|
/* FIXME: should do something for other systems */
|
|
|
|
GetSystemInfo(&si);
|
2009-10-14 10:23:45 +02:00
|
|
|
lpmemex->ullTotalVirtual = (ULONG_PTR)si.lpMaximumApplicationAddress-(ULONG_PTR)si.lpMinimumApplicationAddress;
|
2010-07-26 19:44:35 +02:00
|
|
|
/* FIXME: we should track down all the already allocated VM pages and subtract them, for now arbitrarily remove 64KB so that it matches NT */
|
2004-08-13 02:41:34 +02:00
|
|
|
lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
|
|
|
|
|
|
|
|
/* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
|
|
|
|
memory in the extended portion of the virtual address space of the calling
|
|
|
|
process, in bytes.
|
|
|
|
However, I don't know what this means, so set it to zero :(
|
|
|
|
*/
|
|
|
|
lpmemex->ullAvailExtendedVirtual = 0;
|
|
|
|
|
2008-03-03 23:27:46 +01:00
|
|
|
cached_memstatus = *lpmemex;
|
2005-08-16 18:00:36 +02:00
|
|
|
|
2012-05-02 21:32:14 +02:00
|
|
|
TRACE_(globalmem)("<-- LPMEMORYSTATUSEX: dwLength %d, dwMemoryLoad %d, ullTotalPhys %s, ullAvailPhys %s,"
|
2004-08-13 02:41:34 +02:00
|
|
|
" ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
|
|
|
|
lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
|
|
|
|
wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
|
|
|
|
wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
|
|
|
|
wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
|
|
|
|
|
|
|
|
return TRUE;
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-08-13 02:41:34 +02:00
|
|
|
* GlobalMemoryStatus (KERNEL32.@)
|
|
|
|
* Provides information about the status of the memory, so apps can tell
|
|
|
|
* roughly how much they are able to allocate
|
2003-09-18 22:58:41 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
2012-04-10 22:04:30 +02:00
|
|
|
* None
|
2003-09-18 22:58:41 +02:00
|
|
|
*/
|
2004-08-13 02:41:34 +02:00
|
|
|
VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
|
2003-09-18 22:58:41 +02:00
|
|
|
{
|
2004-08-13 02:41:34 +02:00
|
|
|
MEMORYSTATUSEX memstatus;
|
2004-12-27 18:06:18 +01:00
|
|
|
OSVERSIONINFOW osver;
|
2007-10-15 22:32:36 +02:00
|
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
|
2004-08-13 02:41:34 +02:00
|
|
|
|
|
|
|
/* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
|
|
|
|
for one extra field in the struct, and the lack of a bug, we simply
|
|
|
|
call GlobalMemoryStatusEx and copy the values across. */
|
2007-10-15 22:32:36 +02:00
|
|
|
memstatus.dwLength = sizeof(memstatus);
|
2004-08-13 02:41:34 +02:00
|
|
|
GlobalMemoryStatusEx(&memstatus);
|
|
|
|
|
|
|
|
lpBuffer->dwLength = sizeof(*lpBuffer);
|
|
|
|
lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
|
|
|
|
|
|
|
|
/* Windows 2000 and later report -1 when values are greater than 4 Gb.
|
|
|
|
* NT reports values modulo 4 Gb.
|
|
|
|
*/
|
2004-12-27 18:06:18 +01:00
|
|
|
|
|
|
|
osver.dwOSVersionInfoSize = sizeof(osver);
|
|
|
|
GetVersionExW(&osver);
|
2004-08-13 02:41:34 +02:00
|
|
|
|
2010-10-06 12:05:01 +02:00
|
|
|
if ( osver.dwMajorVersion >= 5 || osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
|
2004-08-13 02:41:34 +02:00
|
|
|
{
|
2007-10-15 22:32:36 +02:00
|
|
|
lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
|
|
|
|
lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
|
2012-04-10 22:04:30 +02:00
|
|
|
/* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
|
|
|
|
lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, 0xfff7ffff );
|
2007-10-15 22:32:36 +02:00
|
|
|
lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
|
|
|
|
lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
|
|
|
|
lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
|
|
|
|
|
2004-08-13 02:41:34 +02:00
|
|
|
}
|
2012-04-10 22:04:30 +02:00
|
|
|
else /* duplicate NT bug */
|
2004-08-13 02:41:34 +02:00
|
|
|
{
|
2007-10-15 22:32:36 +02:00
|
|
|
lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
|
|
|
|
lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
|
|
|
|
lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
|
|
|
|
lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
|
|
|
|
lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
|
|
|
|
lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
|
|
|
|
/* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
|
|
|
|
if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
|
|
|
|
{
|
|
|
|
if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
|
|
|
|
if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
|
|
|
|
if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
|
|
|
|
if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
|
2004-08-13 02:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* work around for broken photoshop 4 installer */
|
|
|
|
if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
|
|
|
|
lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
|
2007-12-21 14:27:40 +01:00
|
|
|
|
|
|
|
/* limit page file size for really old binaries */
|
2015-12-01 13:02:19 +01:00
|
|
|
if (nt->OptionalHeader.MajorSubsystemVersion < 4 ||
|
|
|
|
nt->OptionalHeader.MajorOperatingSystemVersion < 4)
|
2007-12-21 14:27:40 +01:00
|
|
|
{
|
|
|
|
if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
|
|
|
|
if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
|
|
|
|
}
|
|
|
|
|
2012-05-02 21:32:14 +02:00
|
|
|
TRACE_(globalmem)("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
|
2007-12-21 14:27:40 +01:00
|
|
|
" TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
|
|
|
|
lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
|
|
|
|
lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
|
|
|
|
lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );
|
2003-09-18 22:58:41 +02:00
|
|
|
}
|
2014-10-07 21:34:51 +02:00
|
|
|
|
2015-10-19 12:41:48 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* GetPhysicallyInstalledSystemMemory (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GetPhysicallyInstalledSystemMemory(ULONGLONG *total_memory)
|
|
|
|
{
|
|
|
|
MEMORYSTATUSEX memstatus;
|
|
|
|
|
|
|
|
FIXME("stub: %p\n", total_memory);
|
|
|
|
|
|
|
|
if (!total_memory)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memstatus.dwLength = sizeof(memstatus);
|
|
|
|
GlobalMemoryStatusEx(&memstatus);
|
|
|
|
*total_memory = memstatus.ullTotalPhys / 1024;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:34:51 +02:00
|
|
|
BOOL WINAPI GetSystemFileCacheSize(PSIZE_T mincache, PSIZE_T maxcache, PDWORD flags)
|
|
|
|
{
|
|
|
|
FIXME("stub: %p %p %p\n", mincache, maxcache, flags);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-10-07 21:35:03 +02:00
|
|
|
|
|
|
|
BOOL WINAPI SetSystemFileCacheSize(SIZE_T mincache, SIZE_T maxcache, DWORD flags)
|
|
|
|
{
|
|
|
|
FIXME("stub: %ld %ld %d\n", mincache, maxcache, flags);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-10-27 06:00:40 +01:00
|
|
|
|
|
|
|
BOOL WINAPI AllocateUserPhysicalPages(HANDLE process, ULONG_PTR *pages, ULONG_PTR *userarray)
|
|
|
|
{
|
|
|
|
FIXME("stub: %p %p %p\n",process, pages, userarray);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-04-28 05:41:38 +02:00
|
|
|
|
|
|
|
BOOL WINAPI FreeUserPhysicalPages(HANDLE process, ULONG_PTR *pages, ULONG_PTR *userarray)
|
|
|
|
{
|
|
|
|
FIXME("stub: %p %p %p\n", process, pages, userarray);
|
|
|
|
*pages = 0;
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|