4 * Copyright 1995, 1996 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <sys/types.h>
36 #include "wine/exception.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(globalmem
);
41 /* address where we try to map the system heap */
42 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
43 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
45 static HANDLE systemHeap
; /* globally shared heap */
48 /***********************************************************************
49 * HEAP_CreateSystemHeap
51 * Create the system heap.
53 static inline HANDLE
HEAP_CreateSystemHeap(void)
59 /* create the system heap event first */
60 event
= CreateEventA( NULL
, TRUE
, FALSE
, "__wine_system_heap_event" );
62 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
63 0, SYSTEM_HEAP_SIZE
, "__wine_system_heap" ))) return 0;
64 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
66 if (!(base
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
68 /* pre-defined address not available */
69 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
73 if (created
) /* newly created heap */
75 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
76 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
81 /* wait for the heap to be initialized */
82 WaitForSingleObject( event
, INFINITE
);
90 /***********************************************************************
91 * HeapCreate (KERNEL32.@)
93 * Create a heap object.
96 * Handle of heap: Success
99 HANDLE WINAPI
HeapCreate(
100 DWORD flags
, /* [in] Heap allocation flag */
101 SIZE_T initialSize
, /* [in] Initial heap size */
102 SIZE_T maxSize
/* [in] Maximum heap size */
106 if ( flags
& HEAP_SHARED
)
108 if (!systemHeap
) HEAP_CreateSystemHeap();
109 else WARN( "Shared Heap requested, returning system heap.\n" );
114 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
115 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
121 /***********************************************************************
122 * HeapDestroy (KERNEL32.@)
124 * Destroy a heap object.
130 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
132 if (heap
== systemHeap
)
134 WARN( "attempt to destroy system heap, returning TRUE!\n" );
137 if (!RtlDestroyHeap( heap
)) return TRUE
;
138 SetLastError( ERROR_INVALID_HANDLE
);
144 * Win32 Global heap functions (GlobalXXX).
145 * These functions included in Win32 for compatibility with 16 bit Windows
146 * Especially the moveable blocks and handles are oldish.
147 * But the ability to directly allocate memory with GPTR and LPTR is widely
150 * The handle stuff looks horrible, but it's implemented almost like Win95
155 #define MAGIC_GLOBAL_USED 0x5342
156 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
157 #define INTERN_TO_HANDLE(i) (&((i)->Pointer))
158 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
159 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
160 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
161 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
162 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
163 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
164 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
165 * the output jpeg's > 1 MB if not */
166 #define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
168 #include "pshpack1.h"
170 typedef struct __GLOBAL32_INTERN
176 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
180 /***********************************************************************
181 * GlobalLock (KERNEL32.@)
183 * Lock a global memory object and return a pointer to first byte of the memory
186 * hmem [I] Handle of the global memory object
189 * Success: Pointer to first byte of the memory block
193 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
196 LPVOID WINAPI
GlobalLock(HGLOBAL hmem
)
198 return LocalLock( hmem
);
202 /***********************************************************************
203 * GlobalUnlock (KERNEL32.@)
205 * Unlock a global memory object.
208 * hmem [I] Handle of the global memory object
211 * Success: Object is still locked
212 * Failure: FALSE (The Object is unlocked)
215 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
218 BOOL WINAPI
GlobalUnlock(HGLOBAL hmem
)
220 if (ISPOINTER( hmem
)) return TRUE
;
221 return LocalUnlock( hmem
);
225 /***********************************************************************
226 * GlobalHandle (KERNEL32.@)
228 * Get the handle associated with the pointer to a global memory block.
234 HGLOBAL WINAPI
GlobalHandle(
235 LPCVOID pmem
/* [in] Pointer to global memory block */
238 PGLOBAL32_INTERN maybe_intern
;
243 SetLastError( ERROR_INVALID_PARAMETER
);
247 RtlLockHeap(GetProcessHeap());
252 /* note that if pmem is a pointer to a block allocated by */
253 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
255 if (ISPOINTER(pmem
)) {
256 if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE
, pmem
)) {
257 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
260 handle
= POINTER_TO_HANDLE(pmem
);
262 handle
= (HGLOBAL
)pmem
;
264 /* Now test handle either passed in or retrieved from pointer */
265 maybe_intern
= HANDLE_TO_INTERN( handle
);
266 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
267 test
= maybe_intern
->Pointer
;
268 if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE
, (const char *)test
- HGLOBAL_STORAGE
) && /* obj(-handle) valid arena? */
269 HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE
, maybe_intern
)) /* intern valid arena? */
270 break; /* valid moveable block */
273 SetLastError( ERROR_INVALID_HANDLE
);
277 SetLastError( ERROR_INVALID_HANDLE
);
281 RtlUnlockHeap(GetProcessHeap());
287 /***********************************************************************
288 * GlobalReAlloc (KERNEL32.@)
290 * Change the size or attributes of a global memory object.
296 HGLOBAL WINAPI
GlobalReAlloc( HGLOBAL hmem
, SIZE_T size
, UINT flags
)
298 return LocalReAlloc( hmem
, size
, flags
);
302 /***********************************************************************
303 * GlobalSize (KERNEL32.@)
305 * Get the size of a global memory object.
308 * hmem [I] Handle of the global memory object
312 * Success: Size in Bytes of the global memory object
315 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
318 SIZE_T WINAPI
GlobalSize(HGLOBAL hmem
)
321 PGLOBAL32_INTERN pintern
;
323 if (!((ULONG_PTR
)hmem
>> 16))
325 SetLastError(ERROR_INVALID_HANDLE
);
331 retval
=HeapSize(GetProcessHeap(), 0, hmem
);
333 if (retval
== ~(SIZE_T
)0) /* It might be a GMEM_MOVEABLE data pointer */
335 retval
= HeapSize(GetProcessHeap(), 0, (char*)hmem
- HGLOBAL_STORAGE
);
336 if (retval
!= ~(SIZE_T
)0) retval
-= HGLOBAL_STORAGE
;
341 RtlLockHeap(GetProcessHeap());
342 pintern
=HANDLE_TO_INTERN(hmem
);
344 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
346 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
350 retval
= HeapSize(GetProcessHeap(), 0, (char *)pintern
->Pointer
- HGLOBAL_STORAGE
);
351 if (retval
!= ~(SIZE_T
)0) retval
-= HGLOBAL_STORAGE
;
356 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
357 SetLastError(ERROR_INVALID_HANDLE
);
360 RtlUnlockHeap(GetProcessHeap());
362 if (retval
== ~(SIZE_T
)0) retval
= 0;
367 /***********************************************************************
368 * GlobalWire (KERNEL32.@)
370 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
372 return GlobalLock( hmem
);
376 /***********************************************************************
377 * GlobalUnWire (KERNEL32.@)
379 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
381 return GlobalUnlock( hmem
);
385 /***********************************************************************
386 * GlobalFix (KERNEL32.@)
388 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
394 /***********************************************************************
395 * GlobalUnfix (KERNEL32.@)
397 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
403 /***********************************************************************
404 * GlobalFlags (KERNEL32.@)
406 * Get information about a global memory object.
409 * hmem [I] Handle of the global memory object
412 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
413 * Success: Value specifying allocation flags and lock count
416 UINT WINAPI
GlobalFlags(HGLOBAL hmem
)
419 PGLOBAL32_INTERN pintern
;
427 RtlLockHeap(GetProcessHeap());
428 pintern
=HANDLE_TO_INTERN(hmem
);
429 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
431 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
432 if(pintern
->Pointer
==0)
433 retval
|= GMEM_DISCARDED
;
437 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
438 SetLastError(ERROR_INVALID_HANDLE
);
439 retval
= GMEM_INVALID_HANDLE
;
441 RtlUnlockHeap(GetProcessHeap());
447 /***********************************************************************
448 * GlobalCompact (KERNEL32.@)
450 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
452 return 0; /* GlobalCompact does nothing in Win32 */
456 /***********************************************************************
457 * LocalCompact (KERNEL32.@)
459 SIZE_T WINAPI
LocalCompact( UINT minfree
)
461 return 0; /* LocalCompact does nothing in Win32 */
465 /***********************************************************************
466 * LocalFlags (KERNEL32.@)
468 * Get information about a local memory object.
471 * Value specifying allocation flags and lock count.
472 * LMEM_INVALID_HANDLE: Failure
475 * Windows memory management does not provide a separate local heap
478 UINT WINAPI
LocalFlags(
479 HLOCAL handle
/* [in] Handle of memory object */
481 return GlobalFlags( handle
);
485 /***********************************************************************
486 * LocalHandle (KERNEL32.@)
488 * Get the handle associated with the pointer to a local memory block.
495 * Windows memory management does not provide a separate local heap
498 HLOCAL WINAPI
LocalHandle(
499 LPCVOID ptr
/* [in] Address of local memory block */
501 return GlobalHandle( ptr
);
505 /***********************************************************************
506 * LocalShrink (KERNEL32.@)
508 SIZE_T WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
510 return 0; /* LocalShrink does nothing in Win32 */
514 /***********************************************************************
515 * LocalSize (KERNEL32.@)
517 * Get the size of a local memory object.
524 * Windows memory management does not provide a separate local heap
527 SIZE_T WINAPI
LocalSize(
528 HLOCAL handle
/* [in] Handle of memory object */
530 return GlobalSize( handle
);
534 /***********************************************************************
535 * GlobalMemoryStatus (KERNEL32.@)
536 * Provides information about the status of the memory, so apps can tell
537 * roughly how much they are able to allocate
542 VOID WINAPI
GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
)
544 MEMORYSTATUSEX memstatus
;
545 OSVERSIONINFOW osver
;
547 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( GetModuleHandleW(0) );
550 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
551 for one extra field in the struct, and the lack of a bug, we simply
552 call GlobalMemoryStatusEx and copy the values across. */
553 memstatus
.dwLength
= sizeof(memstatus
);
554 GlobalMemoryStatusEx(&memstatus
);
556 lpBuffer
->dwLength
= sizeof(*lpBuffer
);
557 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
559 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
560 * NT reports values modulo 4 Gb.
563 osver
.dwOSVersionInfoSize
= sizeof(osver
);
564 GetVersionExW(&osver
);
566 lpBuffer
->dwTotalPhys
= memstatus
.ullTotalPhys
;
567 lpBuffer
->dwAvailPhys
= memstatus
.ullAvailPhys
;
568 lpBuffer
->dwTotalPageFile
= memstatus
.ullTotalPageFile
;
569 lpBuffer
->dwAvailPageFile
= memstatus
.ullAvailPageFile
;
570 lpBuffer
->dwTotalVirtual
= memstatus
.ullTotalVirtual
;
571 lpBuffer
->dwAvailVirtual
= memstatus
.ullAvailVirtual
;
574 if ( osver
.dwMajorVersion
>= 5 || osver
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
576 lpBuffer
->dwTotalPhys
= min( memstatus
.ullTotalPhys
, MAXDWORD
);
577 lpBuffer
->dwAvailPhys
= min( memstatus
.ullAvailPhys
, MAXDWORD
);
578 /* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
579 lpBuffer
->dwTotalPageFile
= min( memstatus
.ullTotalPageFile
, 0xfff7ffff );
580 lpBuffer
->dwAvailPageFile
= min( memstatus
.ullAvailPageFile
, MAXDWORD
);
581 lpBuffer
->dwTotalVirtual
= min( memstatus
.ullTotalVirtual
, MAXDWORD
);
582 lpBuffer
->dwAvailVirtual
= min( memstatus
.ullAvailVirtual
, MAXDWORD
);
585 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
586 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
587 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
))
589 if (lpBuffer
->dwTotalPhys
> MAXLONG
) lpBuffer
->dwTotalPhys
= MAXLONG
;
590 if (lpBuffer
->dwAvailPhys
> MAXLONG
) lpBuffer
->dwAvailPhys
= MAXLONG
;
591 if (lpBuffer
->dwTotalVirtual
> MAXLONG
) lpBuffer
->dwTotalVirtual
= MAXLONG
;
592 if (lpBuffer
->dwAvailVirtual
> MAXLONG
) lpBuffer
->dwAvailVirtual
= MAXLONG
;
595 /* work around for broken photoshop 4 installer */
596 if ( lpBuffer
->dwAvailPhys
+ lpBuffer
->dwAvailPageFile
>= 2U*1024*1024*1024)
597 lpBuffer
->dwAvailPageFile
= 2U*1024*1024*1024 - lpBuffer
->dwAvailPhys
- 1;
599 /* limit page file size for really old binaries */
600 if (nt
->OptionalHeader
.MajorSubsystemVersion
< 4 ||
601 nt
->OptionalHeader
.MajorOperatingSystemVersion
< 4)
603 if (lpBuffer
->dwTotalPageFile
> MAXLONG
) lpBuffer
->dwTotalPageFile
= MAXLONG
;
604 if (lpBuffer
->dwAvailPageFile
> MAXLONG
) lpBuffer
->dwAvailPageFile
= MAXLONG
;
608 TRACE("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
609 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
610 lpBuffer
->dwLength
, lpBuffer
->dwMemoryLoad
, lpBuffer
->dwTotalPhys
,
611 lpBuffer
->dwAvailPhys
, lpBuffer
->dwTotalPageFile
, lpBuffer
->dwAvailPageFile
,
612 lpBuffer
->dwTotalVirtual
, lpBuffer
->dwAvailVirtual
);