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
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
35 #ifdef HAVE_SYS_SYSCTL_H
36 #include <sys/sysctl.h>
50 #include "wine/exception.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(heap);
55 /* address where we try to map the system heap */
56 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
57 #define SYSTEM_HEAP_SIZE 0x100000 /* Default heap size = 1Mb */
59 static HANDLE systemHeap; /* globally shared heap */
63 /* filter for page-fault exceptions */
64 /* It is possible for a bogus global pointer to cause a */
65 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
66 static WINE_EXCEPTION_FILTER(page_fault)
68 switch (GetExceptionCode()) {
69 case (EXCEPTION_ACCESS_VIOLATION):
70 case (EXCEPTION_PRIV_INSTRUCTION):
71 return EXCEPTION_EXECUTE_HANDLER;
73 return EXCEPTION_CONTINUE_SEARCH;
77 /***********************************************************************
78 * HEAP_CreateSystemHeap
80 * Create the system heap.
82 inline static HANDLE HEAP_CreateSystemHeap(void)
87 UNICODE_STRING event_name;
88 OBJECT_ATTRIBUTES event_attr;
90 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
91 0, SYSTEM_HEAP_SIZE, "__SystemHeap" ))) return 0;
92 created = (GetLastError() != ERROR_ALREADY_EXISTS);
94 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
96 /* pre-defined address not available */
97 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
101 /* create the system heap event */
102 RtlCreateUnicodeStringFromAsciiz( &event_name, "__SystemHeapEvent" );
103 event_attr.Length = sizeof(event_attr);
104 event_attr.RootDirectory = 0;
105 event_attr.ObjectName = &event_name;
106 event_attr.Attributes = 0;
107 event_attr.SecurityDescriptor = NULL;
108 event_attr.SecurityQualityOfService = NULL;
109 NtCreateEvent( &event, EVENT_ALL_ACCESS, &event_attr, TRUE, FALSE );
111 if (created) /* newly created heap */
113 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
114 SYSTEM_HEAP_SIZE, NULL, NULL );
115 NtSetEvent( event, NULL );
119 /* wait for the heap to be initialized */
120 WaitForSingleObject( event, INFINITE );
121 systemHeap = (HANDLE)base;
128 /***********************************************************************
129 * HeapCreate (KERNEL32.@)
131 * Handle of heap: Success
134 HANDLE WINAPI HeapCreate(
135 DWORD flags, /* [in] Heap allocation flag */
136 SIZE_T initialSize, /* [in] Initial heap size */
137 SIZE_T maxSize /* [in] Maximum heap size */
141 if ( flags & HEAP_SHARED )
143 if (!systemHeap) HEAP_CreateSystemHeap();
144 else WARN( "Shared Heap requested, returning system heap.\n" );
149 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
150 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
156 /***********************************************************************
157 * HeapDestroy (KERNEL32.@)
162 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
164 if (heap == systemHeap)
166 WARN( "attempt to destroy system heap, returning TRUE!\n" );
169 if (!RtlDestroyHeap( heap )) return TRUE;
170 SetLastError( ERROR_INVALID_HANDLE );
175 /***********************************************************************
176 * HeapCompact (KERNEL32.@)
178 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
180 return RtlCompactHeap( heap, flags );
184 /***********************************************************************
185 * HeapValidate (KERNEL32.@)
186 * Validates a specified heap.
195 BOOL WINAPI HeapValidate(
196 HANDLE heap, /* [in] Handle to the heap */
197 DWORD flags, /* [in] Bit flags that control access during operation */
198 LPCVOID block /* [in] Optional pointer to memory block to validate */
200 return RtlValidateHeap( heap, flags, block );
204 /***********************************************************************
205 * HeapWalk (KERNEL32.@)
206 * Enumerates the memory blocks in a specified heap.
209 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
210 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
216 BOOL WINAPI HeapWalk(
217 HANDLE heap, /* [in] Handle to heap to enumerate */
218 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
220 NTSTATUS ret = RtlWalkHeap( heap, entry );
221 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
226 /***********************************************************************
227 * HeapLock (KERNEL32.@)
228 * Attempts to acquire the critical section object for a specified heap.
234 BOOL WINAPI HeapLock(
235 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
237 return RtlLockHeap( heap );
241 /***********************************************************************
242 * HeapUnlock (KERNEL32.@)
243 * Releases ownership of the critical section object.
249 BOOL WINAPI HeapUnlock(
250 HANDLE heap /* [in] Handle to the heap to unlock */
252 return RtlUnlockHeap( heap );
256 /***********************************************************************
257 * GetProcessHeap (KERNEL32.@)
259 HANDLE WINAPI GetProcessHeap(void)
261 return NtCurrentTeb()->Peb->ProcessHeap;
265 /***********************************************************************
266 * GetProcessHeaps (KERNEL32.@)
268 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
270 return RtlGetProcessHeaps( count, heaps );
274 /* These are needed so that we can call the functions from inside kernel itself */
276 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
278 return RtlAllocateHeap( heap, flags, size );
281 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
283 return RtlFreeHeap( heap, flags, ptr );
286 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
288 return RtlReAllocateHeap( heap, flags, ptr, size );
291 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
293 return RtlSizeHeap( heap, flags, ptr );
297 * Win32 Global heap functions (GlobalXXX).
298 * These functions included in Win32 for compatibility with 16 bit Windows
299 * Especially the moveable blocks and handles are oldish.
300 * But the ability to directly allocate memory with GPTR and LPTR is widely
303 * The handle stuff looks horrible, but it's implemented almost like Win95
308 #define MAGIC_GLOBAL_USED 0x5342
309 #define GLOBAL_LOCK_MAX 0xFF
310 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
311 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
312 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
313 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
314 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
315 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
316 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
317 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
318 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
319 * the output jpeg's > 1 MB if not */
320 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
322 typedef struct __GLOBAL32_INTERN
325 LPVOID Pointer WINE_PACKED;
328 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
331 /***********************************************************************
332 * GlobalAlloc (KERNEL32.@)
337 HGLOBAL WINAPI GlobalAlloc(
338 UINT flags, /* [in] Object allocation attributes */
339 SIZE_T size /* [in] Number of bytes to allocate */
341 PGLOBAL32_INTERN pintern;
345 if(flags&GMEM_ZEROINIT)
346 hpflags=HEAP_ZERO_MEMORY;
350 TRACE("() flags=%04x\n", flags );
352 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
354 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
355 return (HGLOBAL) palloc;
359 RtlLockHeap(GetProcessHeap());
361 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
364 pintern->Magic = MAGIC_GLOBAL_USED;
365 pintern->Flags = flags >> 8;
366 pintern->LockCount = 0;
370 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
373 HeapFree(GetProcessHeap(), 0, pintern);
378 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
379 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
383 pintern->Pointer = NULL;
386 RtlUnlockHeap(GetProcessHeap());
387 return pintern ? INTERN_TO_HANDLE(pintern) : 0;
392 /***********************************************************************
393 * GlobalLock (KERNEL32.@)
395 * Pointer to first byte of block
398 LPVOID WINAPI GlobalLock(
399 HGLOBAL hmem /* [in] Handle of global memory object */
402 PGLOBAL32_INTERN pintern;
406 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
408 RtlLockHeap(GetProcessHeap());
411 pintern = HANDLE_TO_INTERN(hmem);
412 if (pintern->Magic == MAGIC_GLOBAL_USED)
414 if (pintern->LockCount < GLOBAL_LOCK_MAX)
415 pintern->LockCount++;
416 palloc = pintern->Pointer;
420 WARN("invalid handle %p\n", hmem);
422 SetLastError(ERROR_INVALID_HANDLE);
427 WARN("page fault on %p\n", hmem);
429 SetLastError(ERROR_INVALID_HANDLE);
432 RtlUnlockHeap(GetProcessHeap());
437 /***********************************************************************
438 * GlobalUnlock (KERNEL32.@)
440 * TRUE: Object is still locked
441 * FALSE: Object is unlocked
443 BOOL WINAPI GlobalUnlock(
444 HGLOBAL hmem /* [in] Handle of global memory object */
446 PGLOBAL32_INTERN pintern;
449 if (ISPOINTER(hmem)) return FALSE;
451 RtlLockHeap(GetProcessHeap());
454 pintern=HANDLE_TO_INTERN(hmem);
455 if(pintern->Magic==MAGIC_GLOBAL_USED)
457 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
458 pintern->LockCount--;
460 locked = (pintern->LockCount != 0);
461 if (!locked) SetLastError(NO_ERROR);
465 WARN("invalid handle\n");
466 SetLastError(ERROR_INVALID_HANDLE);
472 ERR("page fault occurred ! Caused by bug ?\n");
473 SetLastError( ERROR_INVALID_PARAMETER );
477 RtlUnlockHeap(GetProcessHeap());
482 /***********************************************************************
483 * GlobalHandle (KERNEL32.@)
484 * Returns the handle associated with the specified pointer.
490 HGLOBAL WINAPI GlobalHandle(
491 LPCVOID pmem /* [in] Pointer to global memory block */
494 PGLOBAL32_INTERN maybe_intern;
499 SetLastError( ERROR_INVALID_PARAMETER );
503 RtlLockHeap(GetProcessHeap());
508 /* note that if pmem is a pointer to a a block allocated by */
509 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
511 if (ISPOINTER(pmem)) {
512 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
513 handle = (HGLOBAL)pmem; /* valid fixed block */
516 handle = POINTER_TO_HANDLE(pmem);
518 handle = (HGLOBAL)pmem;
520 /* Now test handle either passed in or retrieved from pointer */
521 maybe_intern = HANDLE_TO_INTERN( handle );
522 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
523 test = maybe_intern->Pointer;
524 if (HeapValidate( GetProcessHeap(), 0, (char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
525 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
526 break; /* valid moveable block */
529 SetLastError( ERROR_INVALID_HANDLE );
533 SetLastError( ERROR_INVALID_HANDLE );
537 RtlUnlockHeap(GetProcessHeap());
543 /***********************************************************************
544 * GlobalReAlloc (KERNEL32.@)
549 HGLOBAL WINAPI GlobalReAlloc(
550 HGLOBAL hmem, /* [in] Handle of global memory object */
551 SIZE_T size, /* [in] New size of block */
552 UINT flags /* [in] How to reallocate object */
556 PGLOBAL32_INTERN pintern;
557 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
560 RtlLockHeap(GetProcessHeap());
561 if(flags & GMEM_MODIFY) /* modify flags */
563 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
565 /* make a fixed block moveable
566 * actually only NT is able to do this. But it's soo simple
570 WARN("GlobalReAlloc with null handle!\n");
571 SetLastError( ERROR_NOACCESS );
576 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
577 hnew = GlobalAlloc(flags, size);
578 palloc = GlobalLock(hnew);
579 memcpy(palloc, (LPVOID)hmem, size);
584 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
586 /* change the flags to make our block "discardable" */
587 pintern=HANDLE_TO_INTERN(hmem);
588 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
593 SetLastError(ERROR_INVALID_PARAMETER);
601 /* reallocate fixed memory */
602 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
606 /* reallocate a moveable block */
607 pintern=HANDLE_TO_INTERN(hmem);
610 /* Apparently Windows doesn't care whether the handle is locked at this point */
611 /* See also the same comment in GlobalFree() */
612 if(pintern->LockCount>1) {
613 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
614 SetLastError(ERROR_INVALID_HANDLE);
622 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
623 (char *) pintern->Pointer-HGLOBAL_STORAGE,
624 size+HGLOBAL_STORAGE)) == NULL)
625 hnew = 0; /* Block still valid */
627 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
631 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
636 *(HGLOBAL *)palloc = hmem;
637 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
645 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
646 pintern->Pointer=NULL;
651 RtlUnlockHeap(GetProcessHeap());
656 /***********************************************************************
657 * GlobalFree (KERNEL32.@)
662 HGLOBAL WINAPI GlobalFree(
663 HGLOBAL hmem /* [in] Handle of global memory object */
665 PGLOBAL32_INTERN pintern;
668 RtlLockHeap(GetProcessHeap());
672 if(ISPOINTER(hmem)) /* POINTER */
674 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
678 pintern=HANDLE_TO_INTERN(hmem);
680 if(pintern->Magic==MAGIC_GLOBAL_USED)
683 /* WIN98 does not make this test. That is you can free a */
684 /* block you have not unlocked. Go figure!! */
685 /* if(pintern->LockCount!=0) */
686 /* SetLastError(ERROR_INVALID_HANDLE); */
689 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
691 if(!HeapFree(GetProcessHeap(), 0, pintern))
698 ERR("page fault occurred ! Caused by bug ?\n");
699 SetLastError( ERROR_INVALID_PARAMETER );
703 RtlUnlockHeap(GetProcessHeap());
708 /***********************************************************************
709 * GlobalSize (KERNEL32.@)
711 * Size in bytes of the global memory object
714 SIZE_T WINAPI GlobalSize(
715 HGLOBAL hmem /* [in] Handle of global memory object */
718 PGLOBAL32_INTERN pintern;
724 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
728 RtlLockHeap(GetProcessHeap());
729 pintern=HANDLE_TO_INTERN(hmem);
731 if(pintern->Magic==MAGIC_GLOBAL_USED)
733 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
737 retval = HeapSize(GetProcessHeap(), 0,
738 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
739 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
744 WARN("invalid handle\n");
747 RtlUnlockHeap(GetProcessHeap());
749 /* HeapSize returns 0xffffffff on failure */
750 if (retval == 0xffffffff) retval = 0;
755 /***********************************************************************
756 * GlobalWire (KERNEL32.@)
758 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
760 return GlobalLock( hmem );
764 /***********************************************************************
765 * GlobalUnWire (KERNEL32.@)
767 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
769 return GlobalUnlock( hmem);
773 /***********************************************************************
774 * GlobalFix (KERNEL32.@)
776 VOID WINAPI GlobalFix(HGLOBAL hmem)
782 /***********************************************************************
783 * GlobalUnfix (KERNEL32.@)
785 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
791 /***********************************************************************
792 * GlobalFlags (KERNEL32.@)
793 * Returns information about the specified global memory object
796 * Should this return GMEM_INVALID_HANDLE on invalid handle?
799 * Value specifying allocation flags and lock count
800 * GMEM_INVALID_HANDLE: Failure
802 UINT WINAPI GlobalFlags(
803 HGLOBAL hmem /* [in] Handle to global memory object */
806 PGLOBAL32_INTERN pintern;
814 RtlLockHeap(GetProcessHeap());
815 pintern=HANDLE_TO_INTERN(hmem);
816 if(pintern->Magic==MAGIC_GLOBAL_USED)
818 retval=pintern->LockCount + (pintern->Flags<<8);
819 if(pintern->Pointer==0)
820 retval|= GMEM_DISCARDED;
824 WARN("Invalid handle: %p\n", hmem);
827 RtlUnlockHeap(GetProcessHeap());
833 /***********************************************************************
834 * GlobalCompact (KERNEL32.@)
836 SIZE_T WINAPI GlobalCompact( DWORD minfree )
838 return 0; /* GlobalCompact does nothing in Win32 */
842 /***********************************************************************
843 * LocalAlloc (KERNEL32.@)
848 HLOCAL WINAPI LocalAlloc(
849 UINT flags, /* [in] Allocation attributes */
850 SIZE_T size /* [in] Number of bytes to allocate */
852 return (HLOCAL)GlobalAlloc( flags, size );
856 /***********************************************************************
857 * LocalCompact (KERNEL32.@)
859 SIZE_T WINAPI LocalCompact( UINT minfree )
861 return 0; /* LocalCompact does nothing in Win32 */
865 /***********************************************************************
866 * LocalFlags (KERNEL32.@)
868 * Value specifying allocation flags and lock count.
869 * LMEM_INVALID_HANDLE: Failure
871 UINT WINAPI LocalFlags(
872 HLOCAL handle /* [in] Handle of memory object */
874 return GlobalFlags( (HGLOBAL)handle );
878 /***********************************************************************
879 * LocalFree (KERNEL32.@)
884 HLOCAL WINAPI LocalFree(
885 HLOCAL handle /* [in] Handle of memory object */
887 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
891 /***********************************************************************
892 * LocalHandle (KERNEL32.@)
897 HLOCAL WINAPI LocalHandle(
898 LPCVOID ptr /* [in] Address of local memory object */
900 return (HLOCAL)GlobalHandle( ptr );
904 /***********************************************************************
905 * LocalLock (KERNEL32.@)
906 * Locks a local memory object and returns pointer to the first byte
907 * of the memory block.
913 LPVOID WINAPI LocalLock(
914 HLOCAL handle /* [in] Address of local memory object */
916 return GlobalLock( (HGLOBAL)handle );
920 /***********************************************************************
921 * LocalReAlloc (KERNEL32.@)
926 HLOCAL WINAPI LocalReAlloc(
927 HLOCAL handle, /* [in] Handle of memory object */
928 SIZE_T size, /* [in] New size of block */
929 UINT flags /* [in] How to reallocate object */
931 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
935 /***********************************************************************
936 * LocalShrink (KERNEL32.@)
938 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
940 return 0; /* LocalShrink does nothing in Win32 */
944 /***********************************************************************
945 * LocalSize (KERNEL32.@)
950 SIZE_T WINAPI LocalSize(
951 HLOCAL handle /* [in] Handle of memory object */
953 return GlobalSize( (HGLOBAL)handle );
957 /***********************************************************************
958 * LocalUnlock (KERNEL32.@)
960 * TRUE: Object is still locked
961 * FALSE: Object is unlocked
963 BOOL WINAPI LocalUnlock(
964 HLOCAL handle /* [in] Handle of memory object */
966 return GlobalUnlock( (HGLOBAL)handle );
970 /**********************************************************************
971 * AllocMappedBuffer (KERNEL32.38)
973 * This is a undocumented KERNEL32 function that
974 * SMapLS's a GlobalAlloc'ed buffer.
976 * Input: EDI register: size of buffer to allocate
977 * Output: EDI register: pointer to buffer
979 * Note: The buffer is preceded by 8 bytes:
982 * edi-4 SEGPTR to buffer
983 * edi-8 some magic Win95 needs for SUnMapLS
984 * (we use it for the memory handle)
986 * The SEGPTR is used by the caller!
988 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
990 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
991 DWORD *buffer = (DWORD *)GlobalLock(handle);
995 if (!(ptr = MapLS(buffer + 2)))
997 GlobalUnlock(handle);
1002 context->Eax = context->Edi = 0;
1005 buffer[0] = (DWORD)handle;
1008 context->Eax = (DWORD) ptr;
1009 context->Edi = (DWORD)(buffer + 2);
1014 /**********************************************************************
1015 * FreeMappedBuffer (KERNEL32.39)
1017 * Free a buffer allocated by AllocMappedBuffer
1019 * Input: EDI register: pointer to buffer
1021 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
1025 DWORD *buffer = (DWORD *)context->Edi - 2;
1029 GlobalUnlock((HGLOBAL)buffer[0]);
1030 GlobalFree((HGLOBAL)buffer[0]);
1034 /***********************************************************************
1035 * GlobalMemoryStatus (KERNEL32.@)
1036 * Provides information about the status of the memory, so apps can tell
1037 * roughly how much they are able to allocate
1042 VOID WINAPI GlobalMemoryStatus(
1043 LPMEMORYSTATUS lpmem
1045 static MEMORYSTATUS cached_memstatus;
1046 static int cache_lastchecked = 0;
1051 #if defined(__FreeBSD__) || defined(__NetBSD__)
1054 int mib[2] = { CTL_HW };
1056 if (time(NULL)==cache_lastchecked) {
1057 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1060 cache_lastchecked = time(NULL);
1062 lpmem->dwMemoryLoad = 0;
1063 lpmem->dwTotalPhys = 16*1024*1024;
1064 lpmem->dwAvailPhys = 16*1024*1024;
1065 lpmem->dwTotalPageFile = 16*1024*1024;
1066 lpmem->dwAvailPageFile = 16*1024*1024;
1069 f = fopen( "/proc/meminfo", "r" );
1073 int total, used, free, shared, buffers, cached;
1075 lpmem->dwLength = sizeof(MEMORYSTATUS);
1076 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1077 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1078 while (fgets( buffer, sizeof(buffer), f ))
1080 /* old style /proc/meminfo ... */
1081 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1083 lpmem->dwTotalPhys += total;
1084 lpmem->dwAvailPhys += free + buffers + cached;
1086 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1088 lpmem->dwTotalPageFile += total;
1089 lpmem->dwAvailPageFile += free;
1092 /* new style /proc/meminfo ... */
1093 if (sscanf(buffer, "MemTotal: %d", &total))
1094 lpmem->dwTotalPhys = total*1024;
1095 if (sscanf(buffer, "MemFree: %d", &free))
1096 lpmem->dwAvailPhys = free*1024;
1097 if (sscanf(buffer, "SwapTotal: %d", &total))
1098 lpmem->dwTotalPageFile = total*1024;
1099 if (sscanf(buffer, "SwapFree: %d", &free))
1100 lpmem->dwAvailPageFile = free*1024;
1101 if (sscanf(buffer, "Buffers: %d", &buffers))
1102 lpmem->dwAvailPhys += buffers*1024;
1103 if (sscanf(buffer, "Cached: %d", &cached))
1104 lpmem->dwAvailPhys += cached*1024;
1108 if (lpmem->dwTotalPhys)
1110 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1111 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1112 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1113 / (TotalPhysical / 100);
1116 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1117 mib[1] = HW_PHYSMEM;
1118 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1119 tmp = malloc(size_sys * sizeof(int));
1120 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1123 lpmem->dwTotalPhys = *tmp;
1125 mib[1] = HW_USERMEM;
1126 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1127 tmp = malloc(size_sys * sizeof(int));
1128 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1131 lpmem->dwAvailPhys = *tmp;
1132 lpmem->dwTotalPageFile = *tmp;
1133 lpmem->dwAvailPageFile = *tmp;
1134 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1137 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1138 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1139 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1140 lpmem->dwMemoryLoad = 0;
1146 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1147 * is more than 2GB of physical memory.
1149 if (lpmem->dwTotalPhys>2U*1024*1024*1024)
1151 lpmem->dwTotalPhys=2U*1024*1024*1024;
1152 lpmem->dwAvailPhys=2U*1024*1024*1024;
1155 /* FIXME: should do something for other systems */
1157 lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1158 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1159 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1160 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1162 /* it appears some memory display programs want to divide by these values */
1163 if(lpmem->dwTotalPageFile==0)
1164 lpmem->dwTotalPageFile++;
1166 if(lpmem->dwAvailPageFile==0)
1167 lpmem->dwAvailPageFile++;
1169 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1170 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1171 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1172 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1173 lpmem->dwAvailVirtual);
1176 /***********************************************************************
1177 * GlobalMemoryStatusEx (KERNEL32.@)
1178 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1183 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer )
1185 MEMORYSTATUS memstatus;
1187 /* Because GlobalMemoryStatusEx is identical to GlobalMemoryStatus save
1188 for one extra field in the struct, and the lack of a bug, we simply
1189 call GlobalMemoryStatus and copy the values across. */
1190 FIXME("we should emulate the 4GB bug here, as per MSDN\n");
1191 GlobalMemoryStatus(&memstatus);
1192 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1193 lpBuffer->ullTotalPhys = memstatus.dwTotalPhys;
1194 lpBuffer->ullAvailPhys = memstatus.dwAvailPhys;
1195 lpBuffer->ullTotalPageFile = memstatus.dwTotalPageFile;
1196 lpBuffer->ullAvailPageFile = memstatus.dwAvailPageFile;
1197 lpBuffer->ullTotalVirtual = memstatus.dwTotalVirtual;
1198 lpBuffer->ullAvailVirtual = memstatus.dwAvailVirtual;
1199 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1200 memory in the extended portion of the virtual address space of the calling
1202 However, I don't know what this means, so set it to zero :(
1204 lpBuffer->ullAvailExtendedVirtual = 0;