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>
45 /* FIXME: Unfortunately swapctl can't be used with largefile.... */
46 # undef _FILE_OFFSET_BITS
47 # define _FILE_OFFSET_BITS 32
48 # ifdef HAVE_SYS_RESOURCE_H
49 # include <sys/resource.h>
51 # ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
54 # include <sys/swap.h>
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(heap);
68 /* address where we try to map the system heap */
69 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
70 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
72 static HANDLE systemHeap; /* globally shared heap */
75 /***********************************************************************
76 * HEAP_CreateSystemHeap
78 * Create the system heap.
80 static inline HANDLE HEAP_CreateSystemHeap(void)
86 /* create the system heap event first */
87 event = CreateEventA( NULL, TRUE, FALSE, "__wine_system_heap_event" );
89 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
90 0, SYSTEM_HEAP_SIZE, "__wine_system_heap" ))) return 0;
91 created = (GetLastError() != ERROR_ALREADY_EXISTS);
93 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
95 /* pre-defined address not available */
96 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
100 if (created) /* newly created heap */
102 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
103 SYSTEM_HEAP_SIZE, NULL, NULL );
108 /* wait for the heap to be initialized */
109 WaitForSingleObject( event, INFINITE );
117 /***********************************************************************
118 * HeapCreate (KERNEL32.@)
120 * Create a heap object.
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.@)
151 * Destroy a heap object.
157 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
159 if (heap == systemHeap)
161 WARN( "attempt to destroy system heap, returning TRUE!\n" );
164 if (!RtlDestroyHeap( heap )) return TRUE;
165 SetLastError( ERROR_INVALID_HANDLE );
170 /***********************************************************************
171 * HeapCompact (KERNEL32.@)
173 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
175 return RtlCompactHeap( heap, flags );
179 /***********************************************************************
180 * HeapValidate (KERNEL32.@)
181 * Validates a specified heap.
190 BOOL WINAPI HeapValidate(
191 HANDLE heap, /* [in] Handle to the heap */
192 DWORD flags, /* [in] Bit flags that control access during operation */
193 LPCVOID block /* [in] Optional pointer to memory block to validate */
195 return RtlValidateHeap( heap, flags, block );
199 /***********************************************************************
200 * HeapWalk (KERNEL32.@)
201 * Enumerates the memory blocks in a specified heap.
204 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
205 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
211 BOOL WINAPI HeapWalk(
212 HANDLE heap, /* [in] Handle to heap to enumerate */
213 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
215 NTSTATUS ret = RtlWalkHeap( heap, entry );
216 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
221 /***********************************************************************
222 * HeapLock (KERNEL32.@)
223 * Attempts to acquire the critical section object for a specified heap.
229 BOOL WINAPI HeapLock(
230 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
232 return RtlLockHeap( heap );
236 /***********************************************************************
237 * HeapUnlock (KERNEL32.@)
238 * Releases ownership of the critical section object.
244 BOOL WINAPI HeapUnlock(
245 HANDLE heap /* [in] Handle to the heap to unlock */
247 return RtlUnlockHeap( heap );
251 /***********************************************************************
252 * GetProcessHeap (KERNEL32.@)
254 HANDLE WINAPI GetProcessHeap(void)
256 return NtCurrentTeb()->Peb->ProcessHeap;
260 /***********************************************************************
261 * GetProcessHeaps (KERNEL32.@)
263 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
265 return RtlGetProcessHeaps( count, heaps );
269 /* These are needed so that we can call the functions from inside kernel itself */
271 /***********************************************************************
272 * HeapAlloc (KERNEL32.@)
274 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
276 return RtlAllocateHeap( heap, flags, size );
279 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
281 return RtlFreeHeap( heap, flags, ptr );
284 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
286 return RtlReAllocateHeap( heap, flags, ptr, size );
289 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPCVOID ptr )
291 return RtlSizeHeap( heap, flags, ptr );
294 BOOL WINAPI HeapSetInformation( HANDLE heap, HEAP_INFORMATION_CLASS infoclass, PVOID info, SIZE_T size)
296 FIXME("%p %d %p %ld\n", heap, infoclass, info, size );
301 * Win32 Global heap functions (GlobalXXX).
302 * These functions included in Win32 for compatibility with 16 bit Windows
303 * Especially the moveable blocks and handles are oldish.
304 * But the ability to directly allocate memory with GPTR and LPTR is widely
307 * The handle stuff looks horrible, but it's implemented almost like Win95
312 #define MAGIC_GLOBAL_USED 0x5342
313 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
314 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
315 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
316 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
317 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
318 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
319 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
320 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
321 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
322 * the output jpeg's > 1 MB if not */
323 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
325 #include "pshpack1.h"
327 typedef struct __GLOBAL32_INTERN
333 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
337 /***********************************************************************
338 * GlobalAlloc (KERNEL32.@)
340 * Allocate a global memory object.
346 HGLOBAL WINAPI GlobalAlloc(
347 UINT flags, /* [in] Object allocation attributes */
348 SIZE_T size /* [in] Number of bytes to allocate */
350 PGLOBAL32_INTERN pintern;
354 if(flags&GMEM_ZEROINIT)
355 hpflags=HEAP_ZERO_MEMORY;
359 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
361 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
362 TRACE( "(flags=%04x) returning %p\n", flags, palloc );
367 if (size > INT_MAX-HGLOBAL_STORAGE)
369 SetLastError(ERROR_OUTOFMEMORY);
373 RtlLockHeap(GetProcessHeap());
375 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
378 pintern->Magic = MAGIC_GLOBAL_USED;
379 pintern->Flags = flags >> 8;
380 pintern->LockCount = 0;
384 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
387 HeapFree(GetProcessHeap(), 0, pintern);
392 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
393 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
397 pintern->Pointer = NULL;
400 RtlUnlockHeap(GetProcessHeap());
401 if (!pintern) return 0;
402 TRACE( "(flags=%04x) returning handle %p pointer %p\n",
403 flags, INTERN_TO_HANDLE(pintern), pintern->Pointer );
404 return INTERN_TO_HANDLE(pintern);
409 /***********************************************************************
410 * GlobalLock (KERNEL32.@)
412 * Lock a global memory object and return a pointer to first byte of the memory
415 * hmem [I] Handle of the global memory object
418 * Success: Pointer to first byte of the memory block
422 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
425 LPVOID WINAPI GlobalLock(HGLOBAL hmem)
427 PGLOBAL32_INTERN pintern;
431 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
433 RtlLockHeap(GetProcessHeap());
436 pintern = HANDLE_TO_INTERN(hmem);
437 if (pintern->Magic == MAGIC_GLOBAL_USED)
439 palloc = pintern->Pointer;
440 if (!pintern->Pointer)
441 SetLastError(ERROR_DISCARDED);
442 else if (pintern->LockCount < GMEM_LOCKCOUNT)
443 pintern->LockCount++;
447 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
449 SetLastError(ERROR_INVALID_HANDLE);
454 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
456 SetLastError(ERROR_INVALID_HANDLE);
459 RtlUnlockHeap(GetProcessHeap());
464 /***********************************************************************
465 * GlobalUnlock (KERNEL32.@)
467 * Unlock a global memory object.
470 * hmem [I] Handle of the global memory object
473 * Success: Object is still locked
474 * Failure: FALSE (The Object is unlocked)
477 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
480 BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
482 PGLOBAL32_INTERN pintern;
485 if (ISPOINTER(hmem)) return TRUE;
487 RtlLockHeap(GetProcessHeap());
490 pintern=HANDLE_TO_INTERN(hmem);
491 if(pintern->Magic==MAGIC_GLOBAL_USED)
493 if(pintern->LockCount)
495 pintern->LockCount--;
496 locked = (pintern->LockCount != 0);
497 if (!locked) SetLastError(NO_ERROR);
501 WARN("%p not locked\n", hmem);
502 SetLastError(ERROR_NOT_LOCKED);
508 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
509 SetLastError(ERROR_INVALID_HANDLE);
515 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
516 SetLastError( ERROR_INVALID_PARAMETER );
520 RtlUnlockHeap(GetProcessHeap());
525 /***********************************************************************
526 * GlobalHandle (KERNEL32.@)
528 * Get the handle associated with the pointer to a global memory block.
534 HGLOBAL WINAPI GlobalHandle(
535 LPCVOID pmem /* [in] Pointer to global memory block */
538 PGLOBAL32_INTERN maybe_intern;
543 SetLastError( ERROR_INVALID_PARAMETER );
547 RtlLockHeap(GetProcessHeap());
552 /* note that if pmem is a pointer to a block allocated by */
553 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
555 if (ISPOINTER(pmem)) {
556 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
557 handle = (HGLOBAL)pmem; /* valid fixed block */
560 handle = POINTER_TO_HANDLE(pmem);
562 handle = (HGLOBAL)pmem;
564 /* Now test handle either passed in or retrieved from pointer */
565 maybe_intern = HANDLE_TO_INTERN( handle );
566 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
567 test = maybe_intern->Pointer;
568 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
569 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
570 break; /* valid moveable block */
573 SetLastError( ERROR_INVALID_HANDLE );
577 SetLastError( ERROR_INVALID_HANDLE );
581 RtlUnlockHeap(GetProcessHeap());
587 /***********************************************************************
588 * GlobalReAlloc (KERNEL32.@)
590 * Change the size or attributes of a global memory object.
596 HGLOBAL WINAPI GlobalReAlloc(
597 HGLOBAL hmem, /* [in] Handle of global memory object */
598 SIZE_T size, /* [in] New size of block */
599 UINT flags /* [in] How to reallocate object */
603 PGLOBAL32_INTERN pintern;
604 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
607 RtlLockHeap(GetProcessHeap());
608 if(flags & GMEM_MODIFY) /* modify flags */
610 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
612 /* make a fixed block moveable
613 * actually only NT is able to do this. But it's soo simple
617 WARN("GlobalReAlloc with null handle!\n");
618 SetLastError( ERROR_NOACCESS );
623 size = HeapSize(GetProcessHeap(), 0, hmem);
624 hnew = GlobalAlloc(flags, size);
625 palloc = GlobalLock(hnew);
626 memcpy(palloc, hmem, size);
631 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
633 /* change the flags to make our block "discardable" */
634 pintern=HANDLE_TO_INTERN(hmem);
635 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
640 SetLastError(ERROR_INVALID_PARAMETER);
648 /* reallocate fixed memory */
649 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
653 /* reallocate a moveable block */
654 pintern=HANDLE_TO_INTERN(hmem);
657 /* Apparently Windows doesn't care whether the handle is locked at this point */
658 /* See also the same comment in GlobalFree() */
659 if(pintern->LockCount>1) {
660 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
661 SetLastError(ERROR_INVALID_HANDLE);
669 if(size > INT_MAX-HGLOBAL_STORAGE)
671 SetLastError(ERROR_OUTOFMEMORY);
674 else if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
675 (char *) pintern->Pointer-HGLOBAL_STORAGE,
676 size+HGLOBAL_STORAGE)) == NULL)
677 hnew = 0; /* Block still valid */
679 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
683 if(size > INT_MAX-HGLOBAL_STORAGE)
685 SetLastError(ERROR_OUTOFMEMORY);
688 else if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
693 *(HGLOBAL *)palloc = hmem;
694 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
700 if (pintern->LockCount == 0)
704 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
705 pintern->Pointer = NULL;
710 WARN("not freeing memory associated with locked handle\n");
714 RtlUnlockHeap(GetProcessHeap());
719 /***********************************************************************
720 * GlobalFree (KERNEL32.@)
722 * Free a global memory object.
725 * hmem [I] Handle of the global memory object
729 * Failure: The provided handle
732 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
735 HGLOBAL WINAPI GlobalFree(HGLOBAL hmem)
737 PGLOBAL32_INTERN pintern;
740 RtlLockHeap(GetProcessHeap());
744 if(ISPOINTER(hmem)) /* POINTER */
746 if(!HeapFree(GetProcessHeap(), 0, hmem))
748 SetLastError(ERROR_INVALID_HANDLE);
754 pintern=HANDLE_TO_INTERN(hmem);
756 if(pintern->Magic==MAGIC_GLOBAL_USED)
758 pintern->Magic = 0xdead;
760 /* WIN98 does not make this test. That is you can free a */
761 /* block you have not unlocked. Go figure!! */
762 /* if(pintern->LockCount!=0) */
763 /* SetLastError(ERROR_INVALID_HANDLE); */
766 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
768 if(!HeapFree(GetProcessHeap(), 0, pintern))
773 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
774 SetLastError(ERROR_INVALID_HANDLE);
781 ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
782 SetLastError( ERROR_INVALID_PARAMETER );
786 RtlUnlockHeap(GetProcessHeap());
791 /***********************************************************************
792 * GlobalSize (KERNEL32.@)
794 * Get the size of a global memory object.
797 * hmem [I] Handle of the global memory object
801 * Success: Size in Bytes of the global memory object
804 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
807 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
810 PGLOBAL32_INTERN pintern;
812 if (!((ULONG_PTR)hmem >> 16))
814 SetLastError(ERROR_INVALID_HANDLE);
820 retval=HeapSize(GetProcessHeap(), 0, hmem);
824 RtlLockHeap(GetProcessHeap());
825 pintern=HANDLE_TO_INTERN(hmem);
827 if(pintern->Magic==MAGIC_GLOBAL_USED)
829 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
833 retval = HeapSize(GetProcessHeap(), 0,
834 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
835 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
840 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
841 SetLastError(ERROR_INVALID_HANDLE);
844 RtlUnlockHeap(GetProcessHeap());
846 /* HeapSize returns 0xffffffff on failure */
847 if (retval == 0xffffffff) retval = 0;
852 /***********************************************************************
853 * GlobalWire (KERNEL32.@)
855 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
857 return GlobalLock( hmem );
861 /***********************************************************************
862 * GlobalUnWire (KERNEL32.@)
864 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
866 return GlobalUnlock( hmem);
870 /***********************************************************************
871 * GlobalFix (KERNEL32.@)
873 VOID WINAPI GlobalFix(HGLOBAL hmem)
879 /***********************************************************************
880 * GlobalUnfix (KERNEL32.@)
882 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
888 /***********************************************************************
889 * GlobalFlags (KERNEL32.@)
891 * Get information about a global memory object.
894 * hmem [I] Handle of the global memory object
897 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
898 * Success: Value specifying allocation flags and lock count
901 UINT WINAPI GlobalFlags(HGLOBAL hmem)
904 PGLOBAL32_INTERN pintern;
912 RtlLockHeap(GetProcessHeap());
913 pintern=HANDLE_TO_INTERN(hmem);
914 if(pintern->Magic==MAGIC_GLOBAL_USED)
916 retval=pintern->LockCount + (pintern->Flags<<8);
917 if(pintern->Pointer==0)
918 retval|= GMEM_DISCARDED;
922 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
923 SetLastError(ERROR_INVALID_HANDLE);
924 retval = GMEM_INVALID_HANDLE;
926 RtlUnlockHeap(GetProcessHeap());
932 /***********************************************************************
933 * GlobalCompact (KERNEL32.@)
935 SIZE_T WINAPI GlobalCompact( DWORD minfree )
937 return 0; /* GlobalCompact does nothing in Win32 */
941 /***********************************************************************
942 * LocalAlloc (KERNEL32.@)
944 * Allocate a local memory object.
951 * Windows memory management does not provide a separate local heap
954 HLOCAL WINAPI LocalAlloc(
955 UINT flags, /* [in] Allocation attributes */
956 SIZE_T size /* [in] Number of bytes to allocate */
958 return GlobalAlloc( flags, size );
962 /***********************************************************************
963 * LocalCompact (KERNEL32.@)
965 SIZE_T WINAPI LocalCompact( UINT minfree )
967 return 0; /* LocalCompact does nothing in Win32 */
971 /***********************************************************************
972 * LocalFlags (KERNEL32.@)
974 * Get information about a local memory object.
977 * Value specifying allocation flags and lock count.
978 * LMEM_INVALID_HANDLE: Failure
981 * Windows memory management does not provide a separate local heap
984 UINT WINAPI LocalFlags(
985 HLOCAL handle /* [in] Handle of memory object */
987 return GlobalFlags( handle );
991 /***********************************************************************
992 * LocalFree (KERNEL32.@)
994 * Free a local memory object.
1001 * Windows memory management does not provide a separate local heap
1004 HLOCAL WINAPI LocalFree(
1005 HLOCAL handle /* [in] Handle of memory object */
1007 return GlobalFree( handle );
1011 /***********************************************************************
1012 * LocalHandle (KERNEL32.@)
1014 * Get the handle associated with the pointer to a local memory block.
1021 * Windows memory management does not provide a separate local heap
1024 HLOCAL WINAPI LocalHandle(
1025 LPCVOID ptr /* [in] Address of local memory block */
1027 return GlobalHandle( ptr );
1031 /***********************************************************************
1032 * LocalLock (KERNEL32.@)
1033 * Locks a local memory object and returns pointer to the first byte
1034 * of the memory block.
1041 * Windows memory management does not provide a separate local heap
1044 LPVOID WINAPI LocalLock(
1045 HLOCAL handle /* [in] Address of local memory object */
1047 return GlobalLock( handle );
1051 /***********************************************************************
1052 * LocalReAlloc (KERNEL32.@)
1054 * Change the size or attributes of a local memory object.
1061 * Windows memory management does not provide a separate local heap
1064 HLOCAL WINAPI LocalReAlloc(
1065 HLOCAL handle, /* [in] Handle of memory object */
1066 SIZE_T size, /* [in] New size of block */
1067 UINT flags /* [in] How to reallocate object */
1069 return GlobalReAlloc( handle, size, flags );
1073 /***********************************************************************
1074 * LocalShrink (KERNEL32.@)
1076 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1078 return 0; /* LocalShrink does nothing in Win32 */
1082 /***********************************************************************
1083 * LocalSize (KERNEL32.@)
1085 * Get the size of a local memory object.
1092 * Windows memory management does not provide a separate local heap
1095 SIZE_T WINAPI LocalSize(
1096 HLOCAL handle /* [in] Handle of memory object */
1098 return GlobalSize( handle );
1102 /***********************************************************************
1103 * LocalUnlock (KERNEL32.@)
1105 * Unlock a local memory object.
1108 * TRUE: Object is still locked
1109 * FALSE: Object is unlocked
1112 * Windows memory management does not provide a separate local heap
1115 BOOL WINAPI LocalUnlock(
1116 HLOCAL handle /* [in] Handle of memory object */
1118 return GlobalUnlock( handle );
1122 /**********************************************************************
1123 * AllocMappedBuffer (KERNEL32.38)
1125 * This is an undocumented KERNEL32 function that
1126 * SMapLS's a GlobalAlloc'ed buffer.
1129 * EDI register: pointer to buffer
1132 * The buffer is preceded by 8 bytes:
1135 * edi-4 SEGPTR to buffer
1136 * edi-8 some magic Win95 needs for SUnMapLS
1137 * (we use it for the memory handle)
1139 * The SEGPTR is used by the caller!
1141 void WINAPI __regs_AllocMappedBuffer(
1142 CONTEXT86 *context /* [in] EDI register: size of buffer to allocate */
1144 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1145 DWORD *buffer = (DWORD *)GlobalLock(handle);
1149 if (!(ptr = MapLS(buffer + 2)))
1151 GlobalUnlock(handle);
1156 context->Eax = context->Edi = 0;
1159 buffer[0] = (DWORD)handle;
1163 context->Edi = (DWORD)(buffer + 2);
1166 #ifdef DEFINE_REGS_ENTRYPOINT
1167 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0, 0 )
1170 /**********************************************************************
1171 * FreeMappedBuffer (KERNEL32.39)
1173 * Free a buffer allocated by AllocMappedBuffer
1178 void WINAPI __regs_FreeMappedBuffer(
1179 CONTEXT86 *context /* [in] EDI register: pointer to buffer */
1183 DWORD *buffer = (DWORD *)context->Edi - 2;
1187 GlobalUnlock((HGLOBAL)buffer[0]);
1188 GlobalFree((HGLOBAL)buffer[0]);
1191 #ifdef DEFINE_REGS_ENTRYPOINT
1192 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0, 0 )
1195 /***********************************************************************
1196 * GlobalMemoryStatusEx (KERNEL32.@)
1197 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1202 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1204 static MEMORYSTATUSEX cached_memstatus;
1205 static int cache_lastchecked = 0;
1209 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1213 #elif defined(__APPLE__)
1218 unsigned long pagesize,maxpages,freepages,swapspace,swapfree;
1219 struct anoninfo swapinf;
1223 if (lpmemex->dwLength != sizeof(*lpmemex))
1225 SetLastError( ERROR_INVALID_PARAMETER );
1229 if (time(NULL)==cache_lastchecked) {
1230 *lpmemex = cached_memstatus;
1233 cache_lastchecked = time(NULL);
1235 lpmemex->dwMemoryLoad = 0;
1236 lpmemex->ullTotalPhys = 16*1024*1024;
1237 lpmemex->ullAvailPhys = 16*1024*1024;
1238 lpmemex->ullTotalPageFile = 16*1024*1024;
1239 lpmemex->ullAvailPageFile = 16*1024*1024;
1242 f = fopen( "/proc/meminfo", "r" );
1246 unsigned long total, used, free, shared, buffers, cached;
1248 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1249 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1250 while (fgets( buffer, sizeof(buffer), f ))
1252 /* old style /proc/meminfo ... */
1253 if (sscanf( buffer, "Mem: %lu %lu %lu %lu %lu %lu",
1254 &total, &used, &free, &shared, &buffers, &cached ))
1256 lpmemex->ullTotalPhys += total;
1257 lpmemex->ullAvailPhys += free + buffers + cached;
1259 if (sscanf( buffer, "Swap: %lu %lu %lu", &total, &used, &free ))
1261 lpmemex->ullTotalPageFile += total;
1262 lpmemex->ullAvailPageFile += free;
1265 /* new style /proc/meminfo ... */
1266 if (sscanf(buffer, "MemTotal: %lu", &total))
1267 lpmemex->ullTotalPhys = total*1024;
1268 if (sscanf(buffer, "MemFree: %lu", &free))
1269 lpmemex->ullAvailPhys = free*1024;
1270 if (sscanf(buffer, "SwapTotal: %lu", &total))
1271 lpmemex->ullTotalPageFile = total*1024;
1272 if (sscanf(buffer, "SwapFree: %lu", &free))
1273 lpmemex->ullAvailPageFile = free*1024;
1274 if (sscanf(buffer, "Buffers: %lu", &buffers))
1275 lpmemex->ullAvailPhys += buffers*1024;
1276 if (sscanf(buffer, "Cached: %lu", &cached))
1277 lpmemex->ullAvailPhys += cached*1024;
1281 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1283 mib[1] = HW_PHYSMEM;
1284 size_sys = sizeof(val);
1285 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1286 if (val) lpmemex->ullTotalPhys = val;
1287 mib[1] = HW_USERMEM;
1288 size_sys = sizeof(val);
1289 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1290 if (!val) val = lpmemex->ullTotalPhys;
1291 lpmemex->ullAvailPhys = val;
1292 lpmemex->ullTotalPageFile = val;
1293 lpmemex->ullAvailPageFile = val;
1294 #elif defined ( sun )
1295 pagesize=sysconf(_SC_PAGESIZE);
1296 maxpages=sysconf(_SC_PHYS_PAGES);
1297 freepages=sysconf(_SC_AVPHYS_PAGES);
1298 rval=swapctl(SC_AINFO, &swapinf);
1301 swapspace=swapinf.ani_max*pagesize;
1302 swapfree=swapinf.ani_free*pagesize;
1306 WARN("Swap size cannot be determined , assuming equal to physical memory\n");
1307 swapspace=maxpages*pagesize;
1308 swapfree=maxpages*pagesize;
1310 lpmemex->ullTotalPhys=pagesize*maxpages;
1311 lpmemex->ullAvailPhys = pagesize*freepages;
1312 lpmemex->ullTotalPageFile = swapspace;
1313 lpmemex->ullAvailPageFile = swapfree;
1316 if (lpmemex->ullTotalPhys)
1318 lpmemex->dwMemoryLoad = (lpmemex->ullTotalPhys-lpmemex->ullAvailPhys)
1319 / (lpmemex->ullTotalPhys / 100);
1322 /* Win98 returns only the swapsize in ullTotalPageFile/ullAvailPageFile,
1323 WinXP returns the size of physical memory + swapsize;
1324 mimic the behavior of XP.
1325 Note: Project2k refuses to start if it sees less than 1Mb of free swap.
1327 lpmemex->ullTotalPageFile += lpmemex->ullTotalPhys;
1328 lpmemex->ullAvailPageFile += lpmemex->ullAvailPhys;
1330 /* Titan Quest refuses to run if TotalPageFile <= ullTotalPhys */
1331 if(lpmemex->ullTotalPageFile == lpmemex->ullTotalPhys)
1333 lpmemex->ullTotalPhys -= 1;
1334 lpmemex->ullAvailPhys -= 1;
1337 /* FIXME: should do something for other systems */
1339 lpmemex->ullTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1340 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1341 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1343 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1344 memory in the extended portion of the virtual address space of the calling
1346 However, I don't know what this means, so set it to zero :(
1348 lpmemex->ullAvailExtendedVirtual = 0;
1350 cached_memstatus = *lpmemex;
1352 TRACE("<-- LPMEMORYSTATUSEX: dwLength %d, dwMemoryLoad %d, ullTotalPhys %s, ullAvailPhys %s,"
1353 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1354 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1355 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1356 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1357 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1362 /***********************************************************************
1363 * GlobalMemoryStatus (KERNEL32.@)
1364 * Provides information about the status of the memory, so apps can tell
1365 * roughly how much they are able to allocate
1370 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1372 MEMORYSTATUSEX memstatus;
1373 OSVERSIONINFOW osver;
1374 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
1376 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1377 for one extra field in the struct, and the lack of a bug, we simply
1378 call GlobalMemoryStatusEx and copy the values across. */
1379 memstatus.dwLength = sizeof(memstatus);
1380 GlobalMemoryStatusEx(&memstatus);
1382 lpBuffer->dwLength = sizeof(*lpBuffer);
1383 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1385 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1386 * NT reports values modulo 4 Gb.
1389 osver.dwOSVersionInfoSize = sizeof(osver);
1390 GetVersionExW(&osver);
1392 if ( osver.dwMajorVersion >= 5 )
1394 lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
1395 lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
1396 lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, MAXDWORD );
1397 lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
1398 lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
1399 lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
1402 else /* duplicate NT bug */
1404 lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
1405 lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
1406 lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
1407 lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
1408 lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
1409 lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
1412 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
1413 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
1414 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
1416 if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
1417 if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
1418 if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
1419 if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
1422 /* work around for broken photoshop 4 installer */
1423 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1424 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
1426 /* limit page file size for really old binaries */
1427 if (nt->OptionalHeader.MajorSubsystemVersion < 4)
1429 if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
1430 if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
1433 TRACE("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
1434 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
1435 lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
1436 lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
1437 lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );