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
24 #include "wine/port.h"
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
37 #ifdef HAVE_SYS_SYSCTL_H
38 #include <sys/sysctl.h>
43 #ifdef HAVE_MACH_MACH_H
44 #include <mach/mach.h>
48 /* FIXME: Unfortunately swapctl can't be used with largefile.... */
49 # undef _FILE_OFFSET_BITS
50 # define _FILE_OFFSET_BITS 32
51 # ifdef HAVE_SYS_RESOURCE_H
52 # include <sys/resource.h>
54 # ifdef HAVE_SYS_STAT_H
55 # include <sys/stat.h>
57 # include <sys/swap.h>
66 #include "wine/exception.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
70 WINE_DECLARE_DEBUG_CHANNEL(globalmem
);
72 /* address where we try to map the system heap */
73 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
74 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
76 static HANDLE systemHeap
; /* globally shared heap */
79 /***********************************************************************
80 * HEAP_CreateSystemHeap
82 * Create the system heap.
84 static inline HANDLE
HEAP_CreateSystemHeap(void)
90 /* create the system heap event first */
91 event
= CreateEventA( NULL
, TRUE
, FALSE
, "__wine_system_heap_event" );
93 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
94 0, SYSTEM_HEAP_SIZE
, "__wine_system_heap" ))) return 0;
95 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
97 if (!(base
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
99 /* pre-defined address not available */
100 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
104 if (created
) /* newly created heap */
106 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
107 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
112 /* wait for the heap to be initialized */
113 WaitForSingleObject( event
, INFINITE
);
121 /***********************************************************************
122 * HeapCreate (KERNEL32.@)
124 * Create a heap object.
127 * Handle of heap: Success
130 HANDLE WINAPI
HeapCreate(
131 DWORD flags
, /* [in] Heap allocation flag */
132 SIZE_T initialSize
, /* [in] Initial heap size */
133 SIZE_T maxSize
/* [in] Maximum heap size */
137 if ( flags
& HEAP_SHARED
)
139 if (!systemHeap
) HEAP_CreateSystemHeap();
140 else WARN( "Shared Heap requested, returning system heap.\n" );
145 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
146 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
152 /***********************************************************************
153 * HeapDestroy (KERNEL32.@)
155 * Destroy a heap object.
161 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
163 if (heap
== systemHeap
)
165 WARN( "attempt to destroy system heap, returning TRUE!\n" );
168 if (!RtlDestroyHeap( heap
)) return TRUE
;
169 SetLastError( ERROR_INVALID_HANDLE
);
174 /***********************************************************************
175 * HeapCompact (KERNEL32.@)
177 SIZE_T WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
179 return RtlCompactHeap( heap
, flags
);
183 /***********************************************************************
184 * HeapValidate (KERNEL32.@)
185 * Validates a specified heap.
194 BOOL WINAPI
HeapValidate(
195 HANDLE heap
, /* [in] Handle to the heap */
196 DWORD flags
, /* [in] Bit flags that control access during operation */
197 LPCVOID block
/* [in] Optional pointer to memory block to validate */
199 return RtlValidateHeap( heap
, flags
, block
);
203 /***********************************************************************
204 * HeapWalk (KERNEL32.@)
205 * Enumerates the memory blocks in a specified heap.
208 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
209 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
215 BOOL WINAPI
HeapWalk(
216 HANDLE heap
, /* [in] Handle to heap to enumerate */
217 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
219 NTSTATUS ret
= RtlWalkHeap( heap
, entry
);
220 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
225 /***********************************************************************
226 * HeapLock (KERNEL32.@)
227 * Attempts to acquire the critical section object for a specified heap.
233 BOOL WINAPI
HeapLock(
234 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
236 return RtlLockHeap( heap
);
240 /***********************************************************************
241 * HeapUnlock (KERNEL32.@)
242 * Releases ownership of the critical section object.
248 BOOL WINAPI
HeapUnlock(
249 HANDLE heap
/* [in] Handle to the heap to unlock */
251 return RtlUnlockHeap( heap
);
255 /***********************************************************************
256 * GetProcessHeaps (KERNEL32.@)
258 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
260 return RtlGetProcessHeaps( count
, heaps
);
264 /* These are needed so that we can call the functions from inside kernel itself */
266 /***********************************************************************
267 * HeapAlloc (KERNEL32.@)
269 LPVOID WINAPI
HeapAlloc( HANDLE heap
, DWORD flags
, SIZE_T size
)
271 return RtlAllocateHeap( heap
, flags
, size
);
274 BOOL WINAPI
HeapFree( HANDLE heap
, DWORD flags
, LPVOID ptr
)
276 return RtlFreeHeap( heap
, flags
, ptr
);
279 LPVOID WINAPI
HeapReAlloc( HANDLE heap
, DWORD flags
, LPVOID ptr
, SIZE_T size
)
281 return RtlReAllocateHeap( heap
, flags
, ptr
, size
);
284 SIZE_T WINAPI
HeapSize( HANDLE heap
, DWORD flags
, LPCVOID ptr
)
286 return RtlSizeHeap( heap
, flags
, ptr
);
289 BOOL WINAPI
HeapQueryInformation( HANDLE heap
, HEAP_INFORMATION_CLASS info_class
,
290 PVOID info
, SIZE_T size_in
, PSIZE_T size_out
)
292 NTSTATUS ret
= RtlQueryHeapInformation( heap
, info_class
, info
, size_in
, size_out
);
293 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
297 BOOL WINAPI
HeapSetInformation( HANDLE heap
, HEAP_INFORMATION_CLASS infoclass
, PVOID info
, SIZE_T size
)
299 FIXME("%p %d %p %ld\n", heap
, infoclass
, info
, size
);
304 * Win32 Global heap functions (GlobalXXX).
305 * These functions included in Win32 for compatibility with 16 bit Windows
306 * Especially the moveable blocks and handles are oldish.
307 * But the ability to directly allocate memory with GPTR and LPTR is widely
310 * The handle stuff looks horrible, but it's implemented almost like Win95
315 #define MAGIC_GLOBAL_USED 0x5342
316 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
317 #define INTERN_TO_HANDLE(i) (&((i)->Pointer))
318 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
319 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
320 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
321 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
322 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
323 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
324 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
325 * the output jpeg's > 1 MB if not */
326 #define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
328 #include "pshpack1.h"
330 typedef struct __GLOBAL32_INTERN
336 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
340 /***********************************************************************
341 * GlobalAlloc (KERNEL32.@)
343 * Allocate a global memory object.
349 HGLOBAL WINAPI
GlobalAlloc(
350 UINT flags
, /* [in] Object allocation attributes */
351 SIZE_T size
/* [in] Number of bytes to allocate */
353 PGLOBAL32_INTERN pintern
;
357 if(flags
&GMEM_ZEROINIT
)
358 hpflags
=HEAP_ZERO_MEMORY
;
362 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
364 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
365 TRACE( "(flags=%04x) returning %p\n", flags
, palloc
);
370 if (size
> INT_MAX
-HGLOBAL_STORAGE
)
372 SetLastError(ERROR_OUTOFMEMORY
);
376 pintern
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
379 /* Mask out obsolete flags */
380 flags
&= ~(GMEM_LOWER
| GMEM_NOCOMPACT
| GMEM_NOT_BANKED
| GMEM_NOTIFY
);
382 pintern
->Magic
= MAGIC_GLOBAL_USED
;
383 pintern
->Flags
= flags
>> 8;
384 pintern
->LockCount
= 0;
388 palloc
= HeapAlloc(GetProcessHeap(), hpflags
, size
+HGLOBAL_STORAGE
);
391 HeapFree(GetProcessHeap(), 0, pintern
);
396 *(HGLOBAL
*)palloc
= INTERN_TO_HANDLE(pintern
);
397 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
401 pintern
->Pointer
= NULL
;
404 if (!pintern
) return 0;
405 TRACE( "(flags=%04x) returning handle %p pointer %p\n",
406 flags
, INTERN_TO_HANDLE(pintern
), pintern
->Pointer
);
407 return INTERN_TO_HANDLE(pintern
);
412 /***********************************************************************
413 * GlobalLock (KERNEL32.@)
415 * Lock a global memory object and return a pointer to first byte of the memory
418 * hmem [I] Handle of the global memory object
421 * Success: Pointer to first byte of the memory block
425 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
428 LPVOID WINAPI
GlobalLock(HGLOBAL hmem
)
430 PGLOBAL32_INTERN pintern
;
434 return IsBadReadPtr(hmem
, 1) ? NULL
: hmem
;
436 RtlLockHeap(GetProcessHeap());
439 pintern
= HANDLE_TO_INTERN(hmem
);
440 if (pintern
->Magic
== MAGIC_GLOBAL_USED
)
442 palloc
= pintern
->Pointer
;
443 if (!pintern
->Pointer
)
444 SetLastError(ERROR_DISCARDED
);
445 else if (pintern
->LockCount
< GMEM_LOCKCOUNT
)
446 pintern
->LockCount
++;
450 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
452 SetLastError(ERROR_INVALID_HANDLE
);
457 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem
);
459 SetLastError(ERROR_INVALID_HANDLE
);
462 RtlUnlockHeap(GetProcessHeap());
467 /***********************************************************************
468 * GlobalUnlock (KERNEL32.@)
470 * Unlock a global memory object.
473 * hmem [I] Handle of the global memory object
476 * Success: Object is still locked
477 * Failure: FALSE (The Object is unlocked)
480 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
483 BOOL WINAPI
GlobalUnlock(HGLOBAL hmem
)
485 PGLOBAL32_INTERN pintern
;
488 if (ISPOINTER(hmem
)) return TRUE
;
490 RtlLockHeap(GetProcessHeap());
493 pintern
=HANDLE_TO_INTERN(hmem
);
494 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
496 if(pintern
->LockCount
)
498 pintern
->LockCount
--;
499 locked
= (pintern
->LockCount
!= 0);
500 if (!locked
) SetLastError(NO_ERROR
);
504 WARN("%p not locked\n", hmem
);
505 SetLastError(ERROR_NOT_LOCKED
);
511 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
512 SetLastError(ERROR_INVALID_HANDLE
);
518 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem
);
519 SetLastError( ERROR_INVALID_PARAMETER
);
523 RtlUnlockHeap(GetProcessHeap());
528 /***********************************************************************
529 * GlobalHandle (KERNEL32.@)
531 * Get the handle associated with the pointer to a global memory block.
537 HGLOBAL WINAPI
GlobalHandle(
538 LPCVOID pmem
/* [in] Pointer to global memory block */
541 PGLOBAL32_INTERN maybe_intern
;
546 SetLastError( ERROR_INVALID_PARAMETER
);
550 RtlLockHeap(GetProcessHeap());
555 /* note that if pmem is a pointer to a block allocated by */
556 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
558 if (ISPOINTER(pmem
)) {
559 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
560 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
563 handle
= POINTER_TO_HANDLE(pmem
);
565 handle
= (HGLOBAL
)pmem
;
567 /* Now test handle either passed in or retrieved from pointer */
568 maybe_intern
= HANDLE_TO_INTERN( handle
);
569 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
570 test
= maybe_intern
->Pointer
;
571 if (HeapValidate( GetProcessHeap(), 0, (const char *)test
- HGLOBAL_STORAGE
) && /* obj(-handle) valid arena? */
572 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
573 break; /* valid moveable block */
576 SetLastError( ERROR_INVALID_HANDLE
);
580 SetLastError( ERROR_INVALID_HANDLE
);
584 RtlUnlockHeap(GetProcessHeap());
590 /***********************************************************************
591 * GlobalReAlloc (KERNEL32.@)
593 * Change the size or attributes of a global memory object.
599 HGLOBAL WINAPI
GlobalReAlloc(
600 HGLOBAL hmem
, /* [in] Handle of global memory object */
601 SIZE_T size
, /* [in] New size of block */
602 UINT flags
/* [in] How to reallocate object */
606 PGLOBAL32_INTERN pintern
;
607 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
610 RtlLockHeap(GetProcessHeap());
611 if(flags
& GMEM_MODIFY
) /* modify flags */
613 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
615 /* make a fixed block moveable
616 * actually only NT is able to do this. But it's soo simple
620 WARN("GlobalReAlloc with null handle!\n");
621 SetLastError( ERROR_NOACCESS
);
626 size
= HeapSize(GetProcessHeap(), 0, hmem
);
627 hnew
= GlobalAlloc(flags
, size
);
628 palloc
= GlobalLock(hnew
);
629 memcpy(palloc
, hmem
, size
);
634 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
636 /* change the flags to make our block "discardable" */
637 pintern
=HANDLE_TO_INTERN(hmem
);
638 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
643 SetLastError(ERROR_INVALID_PARAMETER
);
651 /* reallocate fixed memory */
652 if (!(flags
& GMEM_MOVEABLE
))
653 heap_flags
|= HEAP_REALLOC_IN_PLACE_ONLY
;
654 hnew
=HeapReAlloc(GetProcessHeap(), heap_flags
, hmem
, size
);
658 /* reallocate a moveable block */
659 pintern
=HANDLE_TO_INTERN(hmem
);
662 /* Apparently Windows doesn't care whether the handle is locked at this point */
663 /* See also the same comment in GlobalFree() */
664 if(pintern
->LockCount
>1) {
665 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
666 SetLastError(ERROR_INVALID_HANDLE
);
674 if(size
> INT_MAX
-HGLOBAL_STORAGE
)
676 SetLastError(ERROR_OUTOFMEMORY
);
679 else if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
680 (char *) pintern
->Pointer
-HGLOBAL_STORAGE
,
681 size
+HGLOBAL_STORAGE
)) == NULL
)
682 hnew
= 0; /* Block still valid */
684 pintern
->Pointer
= (char *)palloc
+HGLOBAL_STORAGE
;
688 if(size
> INT_MAX
-HGLOBAL_STORAGE
)
690 SetLastError(ERROR_OUTOFMEMORY
);
693 else if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+HGLOBAL_STORAGE
))
698 *(HGLOBAL
*)palloc
= hmem
;
699 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
705 if (pintern
->LockCount
== 0)
709 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-HGLOBAL_STORAGE
);
710 pintern
->Pointer
= NULL
;
715 WARN("not freeing memory associated with locked handle\n");
719 RtlUnlockHeap(GetProcessHeap());
724 /***********************************************************************
725 * GlobalFree (KERNEL32.@)
727 * Free a global memory object.
730 * hmem [I] Handle of the global memory object
734 * Failure: The provided handle
737 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
740 HGLOBAL WINAPI
GlobalFree(HGLOBAL hmem
)
742 PGLOBAL32_INTERN pintern
;
745 RtlLockHeap(GetProcessHeap());
749 if(ISPOINTER(hmem
)) /* POINTER */
751 if(!HeapFree(GetProcessHeap(), 0, hmem
))
753 SetLastError(ERROR_INVALID_HANDLE
);
759 pintern
=HANDLE_TO_INTERN(hmem
);
761 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
763 pintern
->Magic
= 0xdead;
765 /* WIN98 does not make this test. That is you can free a */
766 /* block you have not unlocked. Go figure!! */
767 /* if(pintern->LockCount!=0) */
768 /* SetLastError(ERROR_INVALID_HANDLE); */
771 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-HGLOBAL_STORAGE
))
773 if(!HeapFree(GetProcessHeap(), 0, pintern
))
778 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
779 SetLastError(ERROR_INVALID_HANDLE
);
786 ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem
);
787 SetLastError( ERROR_INVALID_PARAMETER
);
791 RtlUnlockHeap(GetProcessHeap());
796 /***********************************************************************
797 * GlobalSize (KERNEL32.@)
799 * Get the size of a global memory object.
802 * hmem [I] Handle of the global memory object
806 * Success: Size in Bytes of the global memory object
809 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
812 SIZE_T WINAPI
GlobalSize(HGLOBAL hmem
)
815 PGLOBAL32_INTERN pintern
;
817 if (!((ULONG_PTR
)hmem
>> 16))
819 SetLastError(ERROR_INVALID_HANDLE
);
825 retval
=HeapSize(GetProcessHeap(), 0, hmem
);
827 if (retval
== ~0ul) /* It might be a GMEM_MOVEABLE data pointer */
829 retval
= HeapSize(GetProcessHeap(), 0, (char*)hmem
- HGLOBAL_STORAGE
);
830 if (retval
!= ~0ul) retval
-= HGLOBAL_STORAGE
;
835 RtlLockHeap(GetProcessHeap());
836 pintern
=HANDLE_TO_INTERN(hmem
);
838 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
840 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
844 retval
= HeapSize(GetProcessHeap(), 0, (char *)pintern
->Pointer
- HGLOBAL_STORAGE
);
845 if (retval
!= ~0ul) retval
-= HGLOBAL_STORAGE
;
850 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
851 SetLastError(ERROR_INVALID_HANDLE
);
854 RtlUnlockHeap(GetProcessHeap());
856 if (retval
== ~0ul) retval
= 0;
861 /***********************************************************************
862 * GlobalWire (KERNEL32.@)
864 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
866 return GlobalLock( hmem
);
870 /***********************************************************************
871 * GlobalUnWire (KERNEL32.@)
873 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
875 return GlobalUnlock( hmem
);
879 /***********************************************************************
880 * GlobalFix (KERNEL32.@)
882 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
888 /***********************************************************************
889 * GlobalUnfix (KERNEL32.@)
891 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
897 /***********************************************************************
898 * GlobalFlags (KERNEL32.@)
900 * Get information about a global memory object.
903 * hmem [I] Handle of the global memory object
906 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
907 * Success: Value specifying allocation flags and lock count
910 UINT WINAPI
GlobalFlags(HGLOBAL hmem
)
913 PGLOBAL32_INTERN pintern
;
921 RtlLockHeap(GetProcessHeap());
922 pintern
=HANDLE_TO_INTERN(hmem
);
923 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
925 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
926 if(pintern
->Pointer
==0)
927 retval
|= GMEM_DISCARDED
;
931 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem
, pintern
->Magic
);
932 SetLastError(ERROR_INVALID_HANDLE
);
933 retval
= GMEM_INVALID_HANDLE
;
935 RtlUnlockHeap(GetProcessHeap());
941 /***********************************************************************
942 * GlobalCompact (KERNEL32.@)
944 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
946 return 0; /* GlobalCompact does nothing in Win32 */
950 /***********************************************************************
951 * LocalAlloc (KERNEL32.@)
953 * Allocate a local memory object.
960 * Windows memory management does not provide a separate local heap
963 HLOCAL WINAPI
LocalAlloc(
964 UINT flags
, /* [in] Allocation attributes */
965 SIZE_T size
/* [in] Number of bytes to allocate */
967 return GlobalAlloc( flags
, size
);
971 /***********************************************************************
972 * LocalCompact (KERNEL32.@)
974 SIZE_T WINAPI
LocalCompact( UINT minfree
)
976 return 0; /* LocalCompact does nothing in Win32 */
980 /***********************************************************************
981 * LocalFlags (KERNEL32.@)
983 * Get information about a local memory object.
986 * Value specifying allocation flags and lock count.
987 * LMEM_INVALID_HANDLE: Failure
990 * Windows memory management does not provide a separate local heap
993 UINT WINAPI
LocalFlags(
994 HLOCAL handle
/* [in] Handle of memory object */
996 return GlobalFlags( handle
);
1000 /***********************************************************************
1001 * LocalFree (KERNEL32.@)
1003 * Free a local memory object.
1010 * Windows memory management does not provide a separate local heap
1013 HLOCAL WINAPI
LocalFree(
1014 HLOCAL handle
/* [in] Handle of memory object */
1016 return GlobalFree( handle
);
1020 /***********************************************************************
1021 * LocalHandle (KERNEL32.@)
1023 * Get the handle associated with the pointer to a local memory block.
1030 * Windows memory management does not provide a separate local heap
1033 HLOCAL WINAPI
LocalHandle(
1034 LPCVOID ptr
/* [in] Address of local memory block */
1036 return GlobalHandle( ptr
);
1040 /***********************************************************************
1041 * LocalLock (KERNEL32.@)
1042 * Locks a local memory object and returns pointer to the first byte
1043 * of the memory block.
1050 * Windows memory management does not provide a separate local heap
1053 LPVOID WINAPI
LocalLock(
1054 HLOCAL handle
/* [in] Address of local memory object */
1056 return GlobalLock( handle
);
1060 /***********************************************************************
1061 * LocalReAlloc (KERNEL32.@)
1063 * Change the size or attributes of a local memory object.
1070 * Windows memory management does not provide a separate local heap
1073 HLOCAL WINAPI
LocalReAlloc(
1074 HLOCAL handle
, /* [in] Handle of memory object */
1075 SIZE_T size
, /* [in] New size of block */
1076 UINT flags
/* [in] How to reallocate object */
1078 return GlobalReAlloc( handle
, size
, flags
);
1082 /***********************************************************************
1083 * LocalShrink (KERNEL32.@)
1085 SIZE_T WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
1087 return 0; /* LocalShrink does nothing in Win32 */
1091 /***********************************************************************
1092 * LocalSize (KERNEL32.@)
1094 * Get the size of a local memory object.
1101 * Windows memory management does not provide a separate local heap
1104 SIZE_T WINAPI
LocalSize(
1105 HLOCAL handle
/* [in] Handle of memory object */
1107 return GlobalSize( handle
);
1111 /***********************************************************************
1112 * LocalUnlock (KERNEL32.@)
1114 * Unlock a local memory object.
1117 * TRUE: Object is still locked
1118 * FALSE: Object is unlocked
1121 * Windows memory management does not provide a separate local heap
1124 BOOL WINAPI
LocalUnlock(
1125 HLOCAL handle
/* [in] Handle of memory object */
1128 if (ISPOINTER( handle
))
1130 SetLastError( ERROR_NOT_LOCKED
);
1134 return GlobalUnlock( handle
);
1138 /***********************************************************************
1139 * GlobalMemoryStatusEx (KERNEL32.@)
1140 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1145 BOOL WINAPI
GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex
)
1147 static MEMORYSTATUSEX cached_memstatus
;
1148 static int cache_lastchecked
= 0;
1152 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
1165 struct xsw_usage swap
;
1168 unsigned long pagesize
,maxpages
,freepages
,swapspace
,swapfree
;
1169 struct anoninfo swapinf
;
1173 if (lpmemex
->dwLength
!= sizeof(*lpmemex
))
1175 SetLastError( ERROR_INVALID_PARAMETER
);
1179 if (time(NULL
)==cache_lastchecked
) {
1180 *lpmemex
= cached_memstatus
;
1183 cache_lastchecked
= time(NULL
);
1185 lpmemex
->dwMemoryLoad
= 0;
1186 lpmemex
->ullTotalPhys
= 16*1024*1024;
1187 lpmemex
->ullAvailPhys
= 16*1024*1024;
1188 lpmemex
->ullTotalPageFile
= 16*1024*1024;
1189 lpmemex
->ullAvailPageFile
= 16*1024*1024;
1192 f
= fopen( "/proc/meminfo", "r" );
1196 unsigned long total
, used
, free
, shared
, buffers
, cached
;
1198 lpmemex
->ullTotalPhys
= lpmemex
->ullAvailPhys
= 0;
1199 lpmemex
->ullTotalPageFile
= lpmemex
->ullAvailPageFile
= 0;
1200 while (fgets( buffer
, sizeof(buffer
), f
))
1202 /* old style /proc/meminfo ... */
1203 if (sscanf( buffer
, "Mem: %lu %lu %lu %lu %lu %lu",
1204 &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1206 lpmemex
->ullTotalPhys
+= total
;
1207 lpmemex
->ullAvailPhys
+= free
+ buffers
+ cached
;
1209 if (sscanf( buffer
, "Swap: %lu %lu %lu", &total
, &used
, &free
))
1211 lpmemex
->ullTotalPageFile
+= total
;
1212 lpmemex
->ullAvailPageFile
+= free
;
1215 /* new style /proc/meminfo ... */
1216 if (sscanf(buffer
, "MemTotal: %lu", &total
))
1217 lpmemex
->ullTotalPhys
= (ULONG64
)total
*1024;
1218 if (sscanf(buffer
, "MemFree: %lu", &free
))
1219 lpmemex
->ullAvailPhys
= (ULONG64
)free
*1024;
1220 if (sscanf(buffer
, "SwapTotal: %lu", &total
))
1221 lpmemex
->ullTotalPageFile
= (ULONG64
)total
*1024;
1222 if (sscanf(buffer
, "SwapFree: %lu", &free
))
1223 lpmemex
->ullAvailPageFile
= (ULONG64
)free
*1024;
1224 if (sscanf(buffer
, "Buffers: %lu", &buffers
))
1225 lpmemex
->ullAvailPhys
+= (ULONG64
)buffers
*1024;
1226 if (sscanf(buffer
, "Cached: %lu", &cached
))
1227 lpmemex
->ullAvailPhys
+= (ULONG64
)cached
*1024;
1231 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
1233 lpmemex
->ullAvailPhys
= 0;
1237 mib
[1] = HW_MEMSIZE
;
1238 size_sys
= sizeof(val64
);
1239 if (!sysctl(mib
, 2, &val64
, &size_sys
, NULL
, 0) && size_sys
== sizeof(val64
) && val64
)
1243 #ifdef HAVE_MACH_MACH_H
1245 host_name_port_t host
= mach_host_self();
1246 mach_msg_type_number_t count
;
1248 #ifdef HOST_VM_INFO64_COUNT
1249 vm_size_t page_size
;
1250 vm_statistics64_data_t vm_stat
;
1252 count
= HOST_VM_INFO64_COUNT
;
1253 if (host_statistics64(host
, HOST_VM_INFO64
, (host_info64_t
)&vm_stat
, &count
) == KERN_SUCCESS
&&
1254 host_page_size(host
, &page_size
) == KERN_SUCCESS
)
1255 lpmemex
->ullAvailPhys
= (vm_stat
.free_count
+ vm_stat
.inactive_count
) * (DWORDLONG
)page_size
;
1259 host_basic_info_data_t info
;
1260 count
= HOST_BASIC_INFO_COUNT
;
1261 if (host_info(host
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
) == KERN_SUCCESS
)
1262 total
= info
.max_mem
;
1265 mach_port_deallocate(mach_task_self(), host
);
1271 mib
[1] = HW_PHYSMEM
;
1272 size_sys
= sizeof(val
);
1273 if (!sysctl(mib
, 2, &val
, &size_sys
, NULL
, 0) && size_sys
== sizeof(val
) && val
)
1278 lpmemex
->ullTotalPhys
= total
;
1280 if (!lpmemex
->ullAvailPhys
)
1282 mib
[1] = HW_USERMEM
;
1283 size_sys
= sizeof(val
);
1284 if (!sysctl(mib
, 2, &val
, &size_sys
, NULL
, 0) && size_sys
== sizeof(val
) && val
)
1285 lpmemex
->ullAvailPhys
= val
;
1288 if (!lpmemex
->ullAvailPhys
)
1289 lpmemex
->ullAvailPhys
= lpmemex
->ullTotalPhys
;
1291 lpmemex
->ullTotalPageFile
= lpmemex
->ullAvailPhys
;
1292 lpmemex
->ullAvailPageFile
= lpmemex
->ullAvailPhys
;
1296 mib
[1] = VM_SWAPUSAGE
;
1297 size_sys
= sizeof(swap
);
1298 if (!sysctl(mib
, 2, &swap
, &size_sys
, NULL
, 0) && size_sys
== sizeof(swap
))
1300 lpmemex
->ullTotalPageFile
= swap
.xsu_total
;
1301 lpmemex
->ullAvailPageFile
= swap
.xsu_avail
;
1304 #elif defined ( sun )
1305 pagesize
=sysconf(_SC_PAGESIZE
);
1306 maxpages
=sysconf(_SC_PHYS_PAGES
);
1307 freepages
=sysconf(_SC_AVPHYS_PAGES
);
1308 rval
=swapctl(SC_AINFO
, &swapinf
);
1311 swapspace
=swapinf
.ani_max
*pagesize
;
1312 swapfree
=swapinf
.ani_free
*pagesize
;
1316 WARN("Swap size cannot be determined , assuming equal to physical memory\n");
1317 swapspace
=maxpages
*pagesize
;
1318 swapfree
=maxpages
*pagesize
;
1320 lpmemex
->ullTotalPhys
=pagesize
*maxpages
;
1321 lpmemex
->ullAvailPhys
= pagesize
*freepages
;
1322 lpmemex
->ullTotalPageFile
= swapspace
;
1323 lpmemex
->ullAvailPageFile
= swapfree
;
1326 if (lpmemex
->ullTotalPhys
)
1328 lpmemex
->dwMemoryLoad
= (lpmemex
->ullTotalPhys
-lpmemex
->ullAvailPhys
)
1329 / (lpmemex
->ullTotalPhys
/ 100);
1332 /* Win98 returns only the swapsize in ullTotalPageFile/ullAvailPageFile,
1333 WinXP returns the size of physical memory + swapsize;
1334 mimic the behavior of XP.
1335 Note: Project2k refuses to start if it sees less than 1Mb of free swap.
1337 lpmemex
->ullTotalPageFile
+= lpmemex
->ullTotalPhys
;
1338 lpmemex
->ullAvailPageFile
+= lpmemex
->ullAvailPhys
;
1340 /* Titan Quest refuses to run if TotalPageFile <= ullTotalPhys */
1341 if(lpmemex
->ullTotalPageFile
== lpmemex
->ullTotalPhys
)
1343 lpmemex
->ullTotalPhys
-= 1;
1344 lpmemex
->ullAvailPhys
-= 1;
1347 /* FIXME: should do something for other systems */
1349 lpmemex
->ullTotalVirtual
= (ULONG_PTR
)si
.lpMaximumApplicationAddress
-(ULONG_PTR
)si
.lpMinimumApplicationAddress
;
1350 /* FIXME: we should track down all the already allocated VM pages and subtract them, for now arbitrarily remove 64KB so that it matches NT */
1351 lpmemex
->ullAvailVirtual
= lpmemex
->ullTotalVirtual
-64*1024;
1353 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1354 memory in the extended portion of the virtual address space of the calling
1356 However, I don't know what this means, so set it to zero :(
1358 lpmemex
->ullAvailExtendedVirtual
= 0;
1360 cached_memstatus
= *lpmemex
;
1362 TRACE_(globalmem
)("<-- LPMEMORYSTATUSEX: dwLength %d, dwMemoryLoad %d, ullTotalPhys %s, ullAvailPhys %s,"
1363 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1364 lpmemex
->dwLength
, lpmemex
->dwMemoryLoad
, wine_dbgstr_longlong(lpmemex
->ullTotalPhys
),
1365 wine_dbgstr_longlong(lpmemex
->ullAvailPhys
), wine_dbgstr_longlong(lpmemex
->ullTotalPageFile
),
1366 wine_dbgstr_longlong(lpmemex
->ullAvailPageFile
), wine_dbgstr_longlong(lpmemex
->ullTotalVirtual
),
1367 wine_dbgstr_longlong(lpmemex
->ullAvailVirtual
) );
1372 /***********************************************************************
1373 * GlobalMemoryStatus (KERNEL32.@)
1374 * Provides information about the status of the memory, so apps can tell
1375 * roughly how much they are able to allocate
1380 VOID WINAPI
GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
)
1382 MEMORYSTATUSEX memstatus
;
1383 OSVERSIONINFOW osver
;
1384 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( GetModuleHandleW(0) );
1386 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1387 for one extra field in the struct, and the lack of a bug, we simply
1388 call GlobalMemoryStatusEx and copy the values across. */
1389 memstatus
.dwLength
= sizeof(memstatus
);
1390 GlobalMemoryStatusEx(&memstatus
);
1392 lpBuffer
->dwLength
= sizeof(*lpBuffer
);
1393 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
1395 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1396 * NT reports values modulo 4 Gb.
1399 osver
.dwOSVersionInfoSize
= sizeof(osver
);
1400 GetVersionExW(&osver
);
1402 if ( osver
.dwMajorVersion
>= 5 || osver
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
1404 lpBuffer
->dwTotalPhys
= min( memstatus
.ullTotalPhys
, MAXDWORD
);
1405 lpBuffer
->dwAvailPhys
= min( memstatus
.ullAvailPhys
, MAXDWORD
);
1406 /* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
1407 lpBuffer
->dwTotalPageFile
= min( memstatus
.ullTotalPageFile
, 0xfff7ffff );
1408 lpBuffer
->dwAvailPageFile
= min( memstatus
.ullAvailPageFile
, MAXDWORD
);
1409 lpBuffer
->dwTotalVirtual
= min( memstatus
.ullTotalVirtual
, MAXDWORD
);
1410 lpBuffer
->dwAvailVirtual
= min( memstatus
.ullAvailVirtual
, MAXDWORD
);
1413 else /* duplicate NT bug */
1415 lpBuffer
->dwTotalPhys
= memstatus
.ullTotalPhys
;
1416 lpBuffer
->dwAvailPhys
= memstatus
.ullAvailPhys
;
1417 lpBuffer
->dwTotalPageFile
= memstatus
.ullTotalPageFile
;
1418 lpBuffer
->dwAvailPageFile
= memstatus
.ullAvailPageFile
;
1419 lpBuffer
->dwTotalVirtual
= memstatus
.ullTotalVirtual
;
1420 lpBuffer
->dwAvailVirtual
= memstatus
.ullAvailVirtual
;
1423 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
1424 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
1425 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
))
1427 if (lpBuffer
->dwTotalPhys
> MAXLONG
) lpBuffer
->dwTotalPhys
= MAXLONG
;
1428 if (lpBuffer
->dwAvailPhys
> MAXLONG
) lpBuffer
->dwAvailPhys
= MAXLONG
;
1429 if (lpBuffer
->dwTotalVirtual
> MAXLONG
) lpBuffer
->dwTotalVirtual
= MAXLONG
;
1430 if (lpBuffer
->dwAvailVirtual
> MAXLONG
) lpBuffer
->dwAvailVirtual
= MAXLONG
;
1433 /* work around for broken photoshop 4 installer */
1434 if ( lpBuffer
->dwAvailPhys
+ lpBuffer
->dwAvailPageFile
>= 2U*1024*1024*1024)
1435 lpBuffer
->dwAvailPageFile
= 2U*1024*1024*1024 - lpBuffer
->dwAvailPhys
- 1;
1437 /* limit page file size for really old binaries */
1438 if (nt
->OptionalHeader
.MajorSubsystemVersion
< 4)
1440 if (lpBuffer
->dwTotalPageFile
> MAXLONG
) lpBuffer
->dwTotalPageFile
= MAXLONG
;
1441 if (lpBuffer
->dwAvailPageFile
> MAXLONG
) lpBuffer
->dwAvailPageFile
= MAXLONG
;
1444 TRACE_(globalmem
)("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
1445 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
1446 lpBuffer
->dwLength
, lpBuffer
->dwMemoryLoad
, lpBuffer
->dwTotalPhys
,
1447 lpBuffer
->dwAvailPhys
, lpBuffer
->dwTotalPageFile
, lpBuffer
->dwAvailPageFile
,
1448 lpBuffer
->dwTotalVirtual
, lpBuffer
->dwAvailVirtual
);