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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
31 #include <sys/types.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
51 #include "wine/exception.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(heap);
56 /* address where we try to map the system heap */
57 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
58 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
60 static HANDLE systemHeap; /* globally shared heap */
64 /* filter for page-fault exceptions */
65 /* It is possible for a bogus global pointer to cause a */
66 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
67 static WINE_EXCEPTION_FILTER(page_fault)
69 switch (GetExceptionCode()) {
70 case (EXCEPTION_ACCESS_VIOLATION):
71 case (EXCEPTION_PRIV_INSTRUCTION):
72 return EXCEPTION_EXECUTE_HANDLER;
74 return EXCEPTION_CONTINUE_SEARCH;
78 /***********************************************************************
79 * HEAP_CreateSystemHeap
81 * Create the system heap.
83 inline static HANDLE HEAP_CreateSystemHeap(void)
89 /* create the system heap event first */
90 event = CreateEventA( NULL, TRUE, FALSE, "__wine_system_heap_event" );
92 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
93 0, SYSTEM_HEAP_SIZE, "__wine_system_heap" ))) return 0;
94 created = (GetLastError() != ERROR_ALREADY_EXISTS);
96 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
98 /* pre-defined address not available */
99 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
103 if (created) /* newly created heap */
105 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
106 SYSTEM_HEAP_SIZE, NULL, NULL );
111 /* wait for the heap to be initialized */
112 WaitForSingleObject( event, INFINITE );
113 systemHeap = (HANDLE)base;
120 /***********************************************************************
121 * HeapCreate (KERNEL32.@)
123 * Handle of heap: Success
126 HANDLE WINAPI HeapCreate(
127 DWORD flags, /* [in] Heap allocation flag */
128 SIZE_T initialSize, /* [in] Initial heap size */
129 SIZE_T maxSize /* [in] Maximum heap size */
133 if ( flags & HEAP_SHARED )
135 if (!systemHeap) HEAP_CreateSystemHeap();
136 else WARN( "Shared Heap requested, returning system heap.\n" );
141 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
142 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
148 /***********************************************************************
149 * HeapDestroy (KERNEL32.@)
154 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
156 if (heap == systemHeap)
158 WARN( "attempt to destroy system heap, returning TRUE!\n" );
161 if (!RtlDestroyHeap( heap )) return TRUE;
162 SetLastError( ERROR_INVALID_HANDLE );
167 /***********************************************************************
168 * HeapCompact (KERNEL32.@)
170 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
172 return RtlCompactHeap( heap, flags );
176 /***********************************************************************
177 * HeapValidate (KERNEL32.@)
178 * Validates a specified heap.
187 BOOL WINAPI HeapValidate(
188 HANDLE heap, /* [in] Handle to the heap */
189 DWORD flags, /* [in] Bit flags that control access during operation */
190 LPCVOID block /* [in] Optional pointer to memory block to validate */
192 return RtlValidateHeap( heap, flags, block );
196 /***********************************************************************
197 * HeapWalk (KERNEL32.@)
198 * Enumerates the memory blocks in a specified heap.
201 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
202 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
208 BOOL WINAPI HeapWalk(
209 HANDLE heap, /* [in] Handle to heap to enumerate */
210 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
212 NTSTATUS ret = RtlWalkHeap( heap, entry );
213 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
218 /***********************************************************************
219 * HeapLock (KERNEL32.@)
220 * Attempts to acquire the critical section object for a specified heap.
226 BOOL WINAPI HeapLock(
227 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
229 return RtlLockHeap( heap );
233 /***********************************************************************
234 * HeapUnlock (KERNEL32.@)
235 * Releases ownership of the critical section object.
241 BOOL WINAPI HeapUnlock(
242 HANDLE heap /* [in] Handle to the heap to unlock */
244 return RtlUnlockHeap( heap );
248 /***********************************************************************
249 * GetProcessHeap (KERNEL32.@)
251 HANDLE WINAPI GetProcessHeap(void)
253 return NtCurrentTeb()->Peb->ProcessHeap;
257 /***********************************************************************
258 * GetProcessHeaps (KERNEL32.@)
260 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
262 return RtlGetProcessHeaps( count, heaps );
266 /* These are needed so that we can call the functions from inside kernel itself */
268 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
270 return RtlAllocateHeap( heap, flags, size );
273 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
275 return RtlFreeHeap( heap, flags, ptr );
278 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
280 return RtlReAllocateHeap( heap, flags, ptr, size );
283 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
285 return RtlSizeHeap( heap, flags, ptr );
289 * Win32 Global heap functions (GlobalXXX).
290 * These functions included in Win32 for compatibility with 16 bit Windows
291 * Especially the moveable blocks and handles are oldish.
292 * But the ability to directly allocate memory with GPTR and LPTR is widely
295 * The handle stuff looks horrible, but it's implemented almost like Win95
300 #define MAGIC_GLOBAL_USED 0x5342
301 #define GLOBAL_LOCK_MAX 0xFF
302 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
303 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
304 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
305 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
306 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
307 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
308 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
309 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
310 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
311 * the output jpeg's > 1 MB if not */
312 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
314 #include "pshpack1.h"
316 typedef struct __GLOBAL32_INTERN
322 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
326 /***********************************************************************
327 * GlobalAlloc (KERNEL32.@)
332 HGLOBAL WINAPI GlobalAlloc(
333 UINT flags, /* [in] Object allocation attributes */
334 SIZE_T size /* [in] Number of bytes to allocate */
336 PGLOBAL32_INTERN pintern;
340 if(flags&GMEM_ZEROINIT)
341 hpflags=HEAP_ZERO_MEMORY;
345 TRACE("() flags=%04x\n", flags );
347 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
349 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
350 return (HGLOBAL) palloc;
354 RtlLockHeap(GetProcessHeap());
356 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
359 pintern->Magic = MAGIC_GLOBAL_USED;
360 pintern->Flags = flags >> 8;
361 pintern->LockCount = 0;
365 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
368 HeapFree(GetProcessHeap(), 0, pintern);
373 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
374 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
378 pintern->Pointer = NULL;
381 RtlUnlockHeap(GetProcessHeap());
382 return pintern ? INTERN_TO_HANDLE(pintern) : 0;
387 /***********************************************************************
388 * GlobalLock (KERNEL32.@)
390 * Pointer to first byte of block
393 LPVOID WINAPI GlobalLock(
394 HGLOBAL hmem /* [in] Handle of global memory object */
397 PGLOBAL32_INTERN pintern;
401 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
403 RtlLockHeap(GetProcessHeap());
406 pintern = HANDLE_TO_INTERN(hmem);
407 if (pintern->Magic == MAGIC_GLOBAL_USED)
409 if (pintern->LockCount < GLOBAL_LOCK_MAX)
410 pintern->LockCount++;
411 palloc = pintern->Pointer;
415 WARN("invalid handle %p\n", hmem);
417 SetLastError(ERROR_INVALID_HANDLE);
422 WARN("page fault on %p\n", hmem);
424 SetLastError(ERROR_INVALID_HANDLE);
427 RtlUnlockHeap(GetProcessHeap());
432 /***********************************************************************
433 * GlobalUnlock (KERNEL32.@)
435 * TRUE: Object is still locked
436 * FALSE: Object is unlocked
438 BOOL WINAPI GlobalUnlock(
439 HGLOBAL hmem /* [in] Handle of global memory object */
441 PGLOBAL32_INTERN pintern;
444 if (ISPOINTER(hmem)) return FALSE;
446 RtlLockHeap(GetProcessHeap());
449 pintern=HANDLE_TO_INTERN(hmem);
450 if(pintern->Magic==MAGIC_GLOBAL_USED)
452 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
453 pintern->LockCount--;
455 locked = (pintern->LockCount != 0);
456 if (!locked) SetLastError(NO_ERROR);
460 WARN("invalid handle\n");
461 SetLastError(ERROR_INVALID_HANDLE);
467 ERR("page fault occurred ! Caused by bug ?\n");
468 SetLastError( ERROR_INVALID_PARAMETER );
472 RtlUnlockHeap(GetProcessHeap());
477 /***********************************************************************
478 * GlobalHandle (KERNEL32.@)
479 * Returns the handle associated with the specified pointer.
485 HGLOBAL WINAPI GlobalHandle(
486 LPCVOID pmem /* [in] Pointer to global memory block */
489 PGLOBAL32_INTERN maybe_intern;
494 SetLastError( ERROR_INVALID_PARAMETER );
498 RtlLockHeap(GetProcessHeap());
503 /* note that if pmem is a pointer to a a block allocated by */
504 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
506 if (ISPOINTER(pmem)) {
507 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
508 handle = (HGLOBAL)pmem; /* valid fixed block */
511 handle = POINTER_TO_HANDLE(pmem);
513 handle = (HGLOBAL)pmem;
515 /* Now test handle either passed in or retrieved from pointer */
516 maybe_intern = HANDLE_TO_INTERN( handle );
517 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
518 test = maybe_intern->Pointer;
519 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
520 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
521 break; /* valid moveable block */
524 SetLastError( ERROR_INVALID_HANDLE );
528 SetLastError( ERROR_INVALID_HANDLE );
532 RtlUnlockHeap(GetProcessHeap());
538 /***********************************************************************
539 * GlobalReAlloc (KERNEL32.@)
544 HGLOBAL WINAPI GlobalReAlloc(
545 HGLOBAL hmem, /* [in] Handle of global memory object */
546 SIZE_T size, /* [in] New size of block */
547 UINT flags /* [in] How to reallocate object */
551 PGLOBAL32_INTERN pintern;
552 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
555 RtlLockHeap(GetProcessHeap());
556 if(flags & GMEM_MODIFY) /* modify flags */
558 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
560 /* make a fixed block moveable
561 * actually only NT is able to do this. But it's soo simple
565 WARN("GlobalReAlloc with null handle!\n");
566 SetLastError( ERROR_NOACCESS );
571 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
572 hnew = GlobalAlloc(flags, size);
573 palloc = GlobalLock(hnew);
574 memcpy(palloc, (LPVOID)hmem, size);
579 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
581 /* change the flags to make our block "discardable" */
582 pintern=HANDLE_TO_INTERN(hmem);
583 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
588 SetLastError(ERROR_INVALID_PARAMETER);
596 /* reallocate fixed memory */
597 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
601 /* reallocate a moveable block */
602 pintern=HANDLE_TO_INTERN(hmem);
605 /* Apparently Windows doesn't care whether the handle is locked at this point */
606 /* See also the same comment in GlobalFree() */
607 if(pintern->LockCount>1) {
608 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
609 SetLastError(ERROR_INVALID_HANDLE);
617 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
618 (char *) pintern->Pointer-HGLOBAL_STORAGE,
619 size+HGLOBAL_STORAGE)) == NULL)
620 hnew = 0; /* Block still valid */
622 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
626 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
631 *(HGLOBAL *)palloc = hmem;
632 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
640 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
641 pintern->Pointer=NULL;
646 RtlUnlockHeap(GetProcessHeap());
651 /***********************************************************************
652 * GlobalFree (KERNEL32.@)
657 HGLOBAL WINAPI GlobalFree(
658 HGLOBAL hmem /* [in] Handle of global memory object */
660 PGLOBAL32_INTERN pintern;
663 RtlLockHeap(GetProcessHeap());
667 if(ISPOINTER(hmem)) /* POINTER */
669 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
673 pintern=HANDLE_TO_INTERN(hmem);
675 if(pintern->Magic==MAGIC_GLOBAL_USED)
678 /* WIN98 does not make this test. That is you can free a */
679 /* block you have not unlocked. Go figure!! */
680 /* if(pintern->LockCount!=0) */
681 /* SetLastError(ERROR_INVALID_HANDLE); */
684 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
686 if(!HeapFree(GetProcessHeap(), 0, pintern))
693 ERR("page fault occurred ! Caused by bug ?\n");
694 SetLastError( ERROR_INVALID_PARAMETER );
698 RtlUnlockHeap(GetProcessHeap());
703 /***********************************************************************
704 * GlobalSize (KERNEL32.@)
706 * Size in bytes of the global memory object
709 SIZE_T WINAPI GlobalSize(
710 HGLOBAL hmem /* [in] Handle of global memory object */
713 PGLOBAL32_INTERN pintern;
719 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
723 RtlLockHeap(GetProcessHeap());
724 pintern=HANDLE_TO_INTERN(hmem);
726 if(pintern->Magic==MAGIC_GLOBAL_USED)
728 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
732 retval = HeapSize(GetProcessHeap(), 0,
733 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
734 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
739 WARN("invalid handle\n");
742 RtlUnlockHeap(GetProcessHeap());
744 /* HeapSize returns 0xffffffff on failure */
745 if (retval == 0xffffffff) retval = 0;
750 /***********************************************************************
751 * GlobalWire (KERNEL32.@)
753 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
755 return GlobalLock( hmem );
759 /***********************************************************************
760 * GlobalUnWire (KERNEL32.@)
762 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
764 return GlobalUnlock( hmem);
768 /***********************************************************************
769 * GlobalFix (KERNEL32.@)
771 VOID WINAPI GlobalFix(HGLOBAL hmem)
777 /***********************************************************************
778 * GlobalUnfix (KERNEL32.@)
780 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
786 /***********************************************************************
787 * GlobalFlags (KERNEL32.@)
788 * Returns information about the specified global memory object
791 * Should this return GMEM_INVALID_HANDLE on invalid handle?
794 * Value specifying allocation flags and lock count
795 * GMEM_INVALID_HANDLE: Failure
797 UINT WINAPI GlobalFlags(
798 HGLOBAL hmem /* [in] Handle to global memory object */
801 PGLOBAL32_INTERN pintern;
809 RtlLockHeap(GetProcessHeap());
810 pintern=HANDLE_TO_INTERN(hmem);
811 if(pintern->Magic==MAGIC_GLOBAL_USED)
813 retval=pintern->LockCount + (pintern->Flags<<8);
814 if(pintern->Pointer==0)
815 retval|= GMEM_DISCARDED;
819 WARN("Invalid handle: %p\n", hmem);
822 RtlUnlockHeap(GetProcessHeap());
828 /***********************************************************************
829 * GlobalCompact (KERNEL32.@)
831 SIZE_T WINAPI GlobalCompact( DWORD minfree )
833 return 0; /* GlobalCompact does nothing in Win32 */
837 /***********************************************************************
838 * LocalAlloc (KERNEL32.@)
843 HLOCAL WINAPI LocalAlloc(
844 UINT flags, /* [in] Allocation attributes */
845 SIZE_T size /* [in] Number of bytes to allocate */
847 return (HLOCAL)GlobalAlloc( flags, size );
851 /***********************************************************************
852 * LocalCompact (KERNEL32.@)
854 SIZE_T WINAPI LocalCompact( UINT minfree )
856 return 0; /* LocalCompact does nothing in Win32 */
860 /***********************************************************************
861 * LocalFlags (KERNEL32.@)
863 * Value specifying allocation flags and lock count.
864 * LMEM_INVALID_HANDLE: Failure
866 UINT WINAPI LocalFlags(
867 HLOCAL handle /* [in] Handle of memory object */
869 return GlobalFlags( (HGLOBAL)handle );
873 /***********************************************************************
874 * LocalFree (KERNEL32.@)
879 HLOCAL WINAPI LocalFree(
880 HLOCAL handle /* [in] Handle of memory object */
882 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
886 /***********************************************************************
887 * LocalHandle (KERNEL32.@)
892 HLOCAL WINAPI LocalHandle(
893 LPCVOID ptr /* [in] Address of local memory object */
895 return (HLOCAL)GlobalHandle( ptr );
899 /***********************************************************************
900 * LocalLock (KERNEL32.@)
901 * Locks a local memory object and returns pointer to the first byte
902 * of the memory block.
908 LPVOID WINAPI LocalLock(
909 HLOCAL handle /* [in] Address of local memory object */
911 return GlobalLock( (HGLOBAL)handle );
915 /***********************************************************************
916 * LocalReAlloc (KERNEL32.@)
921 HLOCAL WINAPI LocalReAlloc(
922 HLOCAL handle, /* [in] Handle of memory object */
923 SIZE_T size, /* [in] New size of block */
924 UINT flags /* [in] How to reallocate object */
926 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
930 /***********************************************************************
931 * LocalShrink (KERNEL32.@)
933 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
935 return 0; /* LocalShrink does nothing in Win32 */
939 /***********************************************************************
940 * LocalSize (KERNEL32.@)
945 SIZE_T WINAPI LocalSize(
946 HLOCAL handle /* [in] Handle of memory object */
948 return GlobalSize( (HGLOBAL)handle );
952 /***********************************************************************
953 * LocalUnlock (KERNEL32.@)
955 * TRUE: Object is still locked
956 * FALSE: Object is unlocked
958 BOOL WINAPI LocalUnlock(
959 HLOCAL handle /* [in] Handle of memory object */
961 return GlobalUnlock( (HGLOBAL)handle );
965 /**********************************************************************
966 * AllocMappedBuffer (KERNEL32.38)
968 * This is an undocumented KERNEL32 function that
969 * SMapLS's a GlobalAlloc'ed buffer.
971 * Input: EDI register: size of buffer to allocate
972 * Output: EDI register: pointer to buffer
974 * Note: The buffer is preceded by 8 bytes:
977 * edi-4 SEGPTR to buffer
978 * edi-8 some magic Win95 needs for SUnMapLS
979 * (we use it for the memory handle)
981 * The SEGPTR is used by the caller!
983 void WINAPI __regs_AllocMappedBuffer( CONTEXT86 *context )
985 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
986 DWORD *buffer = (DWORD *)GlobalLock(handle);
990 if (!(ptr = MapLS(buffer + 2)))
992 GlobalUnlock(handle);
997 context->Eax = context->Edi = 0;
1000 buffer[0] = (DWORD)handle;
1003 context->Eax = (DWORD) ptr;
1004 context->Edi = (DWORD)(buffer + 2);
1007 #ifdef DEFINE_REGS_ENTRYPOINT
1008 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0, 0 );
1011 /**********************************************************************
1012 * FreeMappedBuffer (KERNEL32.39)
1014 * Free a buffer allocated by AllocMappedBuffer
1016 * Input: EDI register: pointer to buffer
1018 void WINAPI __regs_FreeMappedBuffer( CONTEXT86 *context )
1022 DWORD *buffer = (DWORD *)context->Edi - 2;
1026 GlobalUnlock((HGLOBAL)buffer[0]);
1027 GlobalFree((HGLOBAL)buffer[0]);
1030 #ifdef DEFINE_REGS_ENTRYPOINT
1031 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0, 0 );
1034 /***********************************************************************
1035 * GlobalMemoryStatusEx (KERNEL32.@)
1036 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1041 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1043 static MEMORYSTATUSEX cached_memstatus;
1044 static int cache_lastchecked = 0;
1049 #if defined(__FreeBSD__) || defined(__NetBSD__)
1052 int mib[2] = { CTL_HW };
1054 if (time(NULL)==cache_lastchecked) {
1055 memcpy(lpmemex,&cached_memstatus,sizeof(*lpmemex));
1058 cache_lastchecked = time(NULL);
1060 lpmemex->dwLength = sizeof(*lpmemex);
1061 lpmemex->dwMemoryLoad = 0;
1062 lpmemex->ullTotalPhys = 16*1024*1024;
1063 lpmemex->ullAvailPhys = 16*1024*1024;
1064 lpmemex->ullTotalPageFile = 16*1024*1024;
1065 lpmemex->ullAvailPageFile = 16*1024*1024;
1068 f = fopen( "/proc/meminfo", "r" );
1072 int total, used, free, shared, buffers, cached;
1074 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1075 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1076 while (fgets( buffer, sizeof(buffer), f ))
1078 /* old style /proc/meminfo ... */
1079 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1081 lpmemex->ullTotalPhys += total;
1082 lpmemex->ullAvailPhys += free + buffers + cached;
1084 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1086 lpmemex->ullTotalPageFile += total;
1087 lpmemex->ullAvailPageFile += free;
1090 /* new style /proc/meminfo ... */
1091 if (sscanf(buffer, "MemTotal: %d", &total))
1092 lpmemex->ullTotalPhys = total*1024;
1093 if (sscanf(buffer, "MemFree: %d", &free))
1094 lpmemex->ullAvailPhys = free*1024;
1095 if (sscanf(buffer, "SwapTotal: %d", &total))
1096 lpmemex->ullTotalPageFile = total*1024;
1097 if (sscanf(buffer, "SwapFree: %d", &free))
1098 lpmemex->ullAvailPageFile = free*1024;
1099 if (sscanf(buffer, "Buffers: %d", &buffers))
1100 lpmemex->ullAvailPhys += buffers*1024;
1101 if (sscanf(buffer, "Cached: %d", &cached))
1102 lpmemex->ullAvailPhys += cached*1024;
1106 if (lpmemex->ullTotalPhys)
1108 DWORDLONG TotalPhysical = lpmemex->ullTotalPhys+lpmemex->ullTotalPageFile;
1109 DWORDLONG AvailPhysical = lpmemex->ullAvailPhys+lpmemex->ullAvailPageFile;
1110 lpmemex->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1111 / (TotalPhysical / 100);
1114 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1115 mib[1] = HW_PHYSMEM;
1116 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1117 tmp = malloc(size_sys * sizeof(int));
1118 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1121 lpmemex->ullTotalPhys = *tmp;
1123 mib[1] = HW_USERMEM;
1124 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1125 tmp = malloc(size_sys * sizeof(int));
1126 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1129 lpmemex->ullAvailPhys = *tmp;
1130 lpmemex->ullTotalPageFile = *tmp;
1131 lpmemex->ullAvailPageFile = *tmp;
1132 lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys;
1135 lpmemex->ullAvailPhys = lpmemex->ullTotalPhys;
1136 lpmemex->ullTotalPageFile = lpmemex->ullTotalPhys;
1137 lpmemex->ullAvailPageFile = lpmemex->ullTotalPhys;
1138 lpmemex->dwMemoryLoad = 0;
1145 /* FIXME: should do something for other systems */
1147 lpmemex->ullTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1148 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1149 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1150 memcpy(&cached_memstatus,lpmemex,sizeof(*lpmemex));
1152 /* it appears some memory display programs want to divide by these values */
1153 if(lpmemex->ullTotalPageFile==0)
1154 lpmemex->ullTotalPageFile++;
1156 if(lpmemex->ullAvailPageFile==0)
1157 lpmemex->ullAvailPageFile++;
1159 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1160 memory in the extended portion of the virtual address space of the calling
1162 However, I don't know what this means, so set it to zero :(
1164 lpmemex->ullAvailExtendedVirtual = 0;
1166 TRACE("<-- LPMEMORYSTATUSEX: dwLength %ld, dwMemoryLoad %ld, ullTotalPhys %s, ullAvailPhys %s,"
1167 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1168 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1169 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1170 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1171 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1176 /***********************************************************************
1177 * GlobalMemoryStatus (KERNEL32.@)
1178 * Provides information about the status of the memory, so apps can tell
1179 * roughly how much they are able to allocate
1184 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1186 MEMORYSTATUSEX memstatus;
1187 OSVERSIONINFOW osver;
1189 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1190 for one extra field in the struct, and the lack of a bug, we simply
1191 call GlobalMemoryStatusEx and copy the values across. */
1192 GlobalMemoryStatusEx(&memstatus);
1194 lpBuffer->dwLength = sizeof(*lpBuffer);
1195 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1197 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1198 * NT reports values modulo 4 Gb.
1199 * Values between 2 Gb and 4 Gb are rounded down to 2 Gb.
1202 osver.dwOSVersionInfoSize = sizeof(osver);
1203 GetVersionExW(&osver);
1205 if ( osver.dwMajorVersion >= 5 )
1207 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? MAXDWORD :
1208 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1209 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? MAXDWORD :
1210 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1211 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? MAXDWORD :
1212 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1213 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? MAXDWORD :
1214 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1215 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? MAXDWORD :
1216 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1217 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? MAXDWORD :
1218 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1220 else /* duplicate NT bug */
1222 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? memstatus.ullTotalPhys :
1223 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1224 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? memstatus.ullAvailPhys :
1225 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1226 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? memstatus.ullTotalPageFile :
1227 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1228 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? memstatus.ullAvailPageFile :
1229 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1230 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? memstatus.ullTotalVirtual :
1231 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1232 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? memstatus.ullAvailVirtual :
1233 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1236 /* work around for broken photoshop 4 installer */
1237 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1238 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;