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>
44 /* FIXME: Unfortunately swapctl can't be used with largefile.... */
45 # undef _FILE_OFFSET_BITS
46 # define _FILE_OFFSET_BITS 32
47 # ifdef HAVE_SYS_RESOURCE_H
48 # include <sys/resource.h>
50 # ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h>
53 # 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 inline static 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 );
110 systemHeap = (HANDLE)base;
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 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
273 return RtlAllocateHeap( heap, flags, size );
276 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
278 return RtlFreeHeap( heap, flags, ptr );
281 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
283 return RtlReAllocateHeap( heap, flags, ptr, size );
286 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
288 return RtlSizeHeap( heap, flags, ptr );
292 * Win32 Global heap functions (GlobalXXX).
293 * These functions included in Win32 for compatibility with 16 bit Windows
294 * Especially the moveable blocks and handles are oldish.
295 * But the ability to directly allocate memory with GPTR and LPTR is widely
298 * The handle stuff looks horrible, but it's implemented almost like Win95
303 #define MAGIC_GLOBAL_USED 0x5342
304 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
305 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
306 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
307 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
308 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
309 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
310 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
311 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
312 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
313 * the output jpeg's > 1 MB if not */
314 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
316 #include "pshpack1.h"
318 typedef struct __GLOBAL32_INTERN
324 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
328 /***********************************************************************
329 * GlobalAlloc (KERNEL32.@)
331 * Allocate a global memory object.
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 * Lock a global memory object.
398 * Pointer to first byte of block
401 LPVOID WINAPI GlobalLock(
402 HGLOBAL hmem /* [in] Handle of global memory object */
405 PGLOBAL32_INTERN pintern;
409 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
411 RtlLockHeap(GetProcessHeap());
414 pintern = HANDLE_TO_INTERN(hmem);
415 if (pintern->Magic == MAGIC_GLOBAL_USED)
417 palloc = pintern->Pointer;
418 if (!pintern->Pointer)
419 SetLastError(ERROR_DISCARDED);
420 else if (pintern->LockCount < GMEM_LOCKCOUNT)
421 pintern->LockCount++;
425 WARN("invalid handle %p\n", hmem);
427 SetLastError(ERROR_INVALID_HANDLE);
432 WARN("page fault on %p\n", hmem);
434 SetLastError(ERROR_INVALID_HANDLE);
437 RtlUnlockHeap(GetProcessHeap());
442 /***********************************************************************
443 * GlobalUnlock (KERNEL32.@)
445 * Unlock a global memory object.
448 * TRUE: Object is still locked
449 * FALSE: Object is unlocked
451 BOOL WINAPI GlobalUnlock(
452 HGLOBAL hmem /* [in] Handle of global memory object */
454 PGLOBAL32_INTERN pintern;
457 if (ISPOINTER(hmem)) return FALSE;
459 RtlLockHeap(GetProcessHeap());
462 pintern=HANDLE_TO_INTERN(hmem);
463 if(pintern->Magic==MAGIC_GLOBAL_USED)
465 if(pintern->LockCount)
467 pintern->LockCount--;
468 locked = (pintern->LockCount != 0);
469 if (!locked) SetLastError(NO_ERROR);
473 WARN("%p not locked\n", hmem);
474 SetLastError(ERROR_NOT_LOCKED);
480 WARN("invalid handle\n");
481 SetLastError(ERROR_INVALID_HANDLE);
487 ERR("page fault occurred ! Caused by bug ?\n");
488 SetLastError( ERROR_INVALID_PARAMETER );
492 RtlUnlockHeap(GetProcessHeap());
497 /***********************************************************************
498 * GlobalHandle (KERNEL32.@)
500 * Get the handle associated with the pointer to a global memory block.
506 HGLOBAL WINAPI GlobalHandle(
507 LPCVOID pmem /* [in] Pointer to global memory block */
510 PGLOBAL32_INTERN maybe_intern;
515 SetLastError( ERROR_INVALID_PARAMETER );
519 RtlLockHeap(GetProcessHeap());
524 /* note that if pmem is a pointer to a a block allocated by */
525 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
527 if (ISPOINTER(pmem)) {
528 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
529 handle = (HGLOBAL)pmem; /* valid fixed block */
532 handle = POINTER_TO_HANDLE(pmem);
534 handle = (HGLOBAL)pmem;
536 /* Now test handle either passed in or retrieved from pointer */
537 maybe_intern = HANDLE_TO_INTERN( handle );
538 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
539 test = maybe_intern->Pointer;
540 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
541 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
542 break; /* valid moveable block */
545 SetLastError( ERROR_INVALID_HANDLE );
549 SetLastError( ERROR_INVALID_HANDLE );
553 RtlUnlockHeap(GetProcessHeap());
559 /***********************************************************************
560 * GlobalReAlloc (KERNEL32.@)
562 * Change the size or attributes of a global memory object.
568 HGLOBAL WINAPI GlobalReAlloc(
569 HGLOBAL hmem, /* [in] Handle of global memory object */
570 SIZE_T size, /* [in] New size of block */
571 UINT flags /* [in] How to reallocate object */
575 PGLOBAL32_INTERN pintern;
576 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
579 RtlLockHeap(GetProcessHeap());
580 if(flags & GMEM_MODIFY) /* modify flags */
582 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
584 /* make a fixed block moveable
585 * actually only NT is able to do this. But it's soo simple
589 WARN("GlobalReAlloc with null handle!\n");
590 SetLastError( ERROR_NOACCESS );
595 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
596 hnew = GlobalAlloc(flags, size);
597 palloc = GlobalLock(hnew);
598 memcpy(palloc, (LPVOID)hmem, size);
603 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
605 /* change the flags to make our block "discardable" */
606 pintern=HANDLE_TO_INTERN(hmem);
607 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
612 SetLastError(ERROR_INVALID_PARAMETER);
620 /* reallocate fixed memory */
621 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
625 /* reallocate a moveable block */
626 pintern=HANDLE_TO_INTERN(hmem);
629 /* Apparently Windows doesn't care whether the handle is locked at this point */
630 /* See also the same comment in GlobalFree() */
631 if(pintern->LockCount>1) {
632 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
633 SetLastError(ERROR_INVALID_HANDLE);
641 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
642 (char *) pintern->Pointer-HGLOBAL_STORAGE,
643 size+HGLOBAL_STORAGE)) == NULL)
644 hnew = 0; /* Block still valid */
646 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
650 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
655 *(HGLOBAL *)palloc = hmem;
656 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
662 if (pintern->LockCount == 0)
666 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
667 pintern->Pointer = NULL;
672 WARN("not freeing memory associated with locked handle\n");
676 RtlUnlockHeap(GetProcessHeap());
681 /***********************************************************************
682 * GlobalFree (KERNEL32.@)
684 * Free a global memory object.
687 * hmem [I] Handle of the global memory object
691 * Failure: The provided handle
694 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
697 HGLOBAL WINAPI GlobalFree(HGLOBAL hmem)
699 PGLOBAL32_INTERN pintern;
702 RtlLockHeap(GetProcessHeap());
706 if(ISPOINTER(hmem)) /* POINTER */
708 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
712 pintern=HANDLE_TO_INTERN(hmem);
714 if(pintern->Magic==MAGIC_GLOBAL_USED)
716 pintern->Magic = 0xdead;
718 /* WIN98 does not make this test. That is you can free a */
719 /* block you have not unlocked. Go figure!! */
720 /* if(pintern->LockCount!=0) */
721 /* SetLastError(ERROR_INVALID_HANDLE); */
724 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
726 if(!HeapFree(GetProcessHeap(), 0, pintern))
731 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
732 SetLastError(ERROR_INVALID_HANDLE);
739 ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
740 SetLastError( ERROR_INVALID_PARAMETER );
744 RtlUnlockHeap(GetProcessHeap());
749 /***********************************************************************
750 * GlobalSize (KERNEL32.@)
752 * Get the size of a global memory object.
755 * hmem [I] Handle of the global memory object
759 * Success: Size in Bytes of the global memory object
762 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
765 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
768 PGLOBAL32_INTERN pintern;
774 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
778 RtlLockHeap(GetProcessHeap());
779 pintern=HANDLE_TO_INTERN(hmem);
781 if(pintern->Magic==MAGIC_GLOBAL_USED)
783 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
787 retval = HeapSize(GetProcessHeap(), 0,
788 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
789 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
794 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
795 SetLastError(ERROR_INVALID_HANDLE);
798 RtlUnlockHeap(GetProcessHeap());
800 /* HeapSize returns 0xffffffff on failure */
801 if (retval == 0xffffffff) retval = 0;
806 /***********************************************************************
807 * GlobalWire (KERNEL32.@)
809 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
811 return GlobalLock( hmem );
815 /***********************************************************************
816 * GlobalUnWire (KERNEL32.@)
818 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
820 return GlobalUnlock( hmem);
824 /***********************************************************************
825 * GlobalFix (KERNEL32.@)
827 VOID WINAPI GlobalFix(HGLOBAL hmem)
833 /***********************************************************************
834 * GlobalUnfix (KERNEL32.@)
836 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
842 /***********************************************************************
843 * GlobalFlags (KERNEL32.@)
845 * Get information about a global memory object.
848 * hmem [I] Handle of the global memory object
851 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
852 * Success: Value specifying allocation flags and lock count
855 UINT WINAPI GlobalFlags(HGLOBAL hmem)
858 PGLOBAL32_INTERN pintern;
866 RtlLockHeap(GetProcessHeap());
867 pintern=HANDLE_TO_INTERN(hmem);
868 if(pintern->Magic==MAGIC_GLOBAL_USED)
870 retval=pintern->LockCount + (pintern->Flags<<8);
871 if(pintern->Pointer==0)
872 retval|= GMEM_DISCARDED;
876 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
877 SetLastError(ERROR_INVALID_HANDLE);
878 retval = GMEM_INVALID_HANDLE;
880 RtlUnlockHeap(GetProcessHeap());
886 /***********************************************************************
887 * GlobalCompact (KERNEL32.@)
889 SIZE_T WINAPI GlobalCompact( DWORD minfree )
891 return 0; /* GlobalCompact does nothing in Win32 */
895 /***********************************************************************
896 * LocalAlloc (KERNEL32.@)
898 * Allocate a local memory object.
905 * Windows memory management does not provide a separate local heap
908 HLOCAL WINAPI LocalAlloc(
909 UINT flags, /* [in] Allocation attributes */
910 SIZE_T size /* [in] Number of bytes to allocate */
912 return (HLOCAL)GlobalAlloc( flags, size );
916 /***********************************************************************
917 * LocalCompact (KERNEL32.@)
919 SIZE_T WINAPI LocalCompact( UINT minfree )
921 return 0; /* LocalCompact does nothing in Win32 */
925 /***********************************************************************
926 * LocalFlags (KERNEL32.@)
928 * Get information about a local memory object.
931 * Value specifying allocation flags and lock count.
932 * LMEM_INVALID_HANDLE: Failure
935 * Windows memory management does not provide a separate local heap
938 UINT WINAPI LocalFlags(
939 HLOCAL handle /* [in] Handle of memory object */
941 return GlobalFlags( (HGLOBAL)handle );
945 /***********************************************************************
946 * LocalFree (KERNEL32.@)
948 * Free a local memory object.
955 * Windows memory management does not provide a separate local heap
958 HLOCAL WINAPI LocalFree(
959 HLOCAL handle /* [in] Handle of memory object */
961 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
965 /***********************************************************************
966 * LocalHandle (KERNEL32.@)
968 * Get the handle associated with the pointer to a local memory block.
975 * Windows memory management does not provide a separate local heap
978 HLOCAL WINAPI LocalHandle(
979 LPCVOID ptr /* [in] Address of local memory block */
981 return (HLOCAL)GlobalHandle( ptr );
985 /***********************************************************************
986 * LocalLock (KERNEL32.@)
987 * Locks a local memory object and returns pointer to the first byte
988 * of the memory block.
995 * Windows memory management does not provide a separate local heap
998 LPVOID WINAPI LocalLock(
999 HLOCAL handle /* [in] Address of local memory object */
1001 return GlobalLock( (HGLOBAL)handle );
1005 /***********************************************************************
1006 * LocalReAlloc (KERNEL32.@)
1008 * Change the size or attributes of a local memory object.
1015 * Windows memory management does not provide a separate local heap
1018 HLOCAL WINAPI LocalReAlloc(
1019 HLOCAL handle, /* [in] Handle of memory object */
1020 SIZE_T size, /* [in] New size of block */
1021 UINT flags /* [in] How to reallocate object */
1023 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1027 /***********************************************************************
1028 * LocalShrink (KERNEL32.@)
1030 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1032 return 0; /* LocalShrink does nothing in Win32 */
1036 /***********************************************************************
1037 * LocalSize (KERNEL32.@)
1039 * Get the size of a local memory object.
1046 * Windows memory management does not provide a separate local heap
1049 SIZE_T WINAPI LocalSize(
1050 HLOCAL handle /* [in] Handle of memory object */
1052 return GlobalSize( (HGLOBAL)handle );
1056 /***********************************************************************
1057 * LocalUnlock (KERNEL32.@)
1059 * Unlock a local memory object.
1062 * TRUE: Object is still locked
1063 * FALSE: Object is unlocked
1066 * Windows memory management does not provide a separate local heap
1069 BOOL WINAPI LocalUnlock(
1070 HLOCAL handle /* [in] Handle of memory object */
1072 return GlobalUnlock( (HGLOBAL)handle );
1076 /**********************************************************************
1077 * AllocMappedBuffer (KERNEL32.38)
1079 * This is an undocumented KERNEL32 function that
1080 * SMapLS's a GlobalAlloc'ed buffer.
1083 * EDI register: pointer to buffer
1086 * The buffer is preceded by 8 bytes:
1089 * edi-4 SEGPTR to buffer
1090 * edi-8 some magic Win95 needs for SUnMapLS
1091 * (we use it for the memory handle)
1093 * The SEGPTR is used by the caller!
1095 void WINAPI __regs_AllocMappedBuffer(
1096 CONTEXT86 *context /* [in] EDI register: size of buffer to allocate */
1098 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1099 DWORD *buffer = (DWORD *)GlobalLock(handle);
1103 if (!(ptr = MapLS(buffer + 2)))
1105 GlobalUnlock(handle);
1110 context->Eax = context->Edi = 0;
1113 buffer[0] = (DWORD)handle;
1116 context->Eax = (DWORD) ptr;
1117 context->Edi = (DWORD)(buffer + 2);
1120 #ifdef DEFINE_REGS_ENTRYPOINT
1121 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0, 0 );
1124 /**********************************************************************
1125 * FreeMappedBuffer (KERNEL32.39)
1127 * Free a buffer allocated by AllocMappedBuffer
1132 void WINAPI __regs_FreeMappedBuffer(
1133 CONTEXT86 *context /* [in] EDI register: pointer to buffer */
1137 DWORD *buffer = (DWORD *)context->Edi - 2;
1141 GlobalUnlock((HGLOBAL)buffer[0]);
1142 GlobalFree((HGLOBAL)buffer[0]);
1145 #ifdef DEFINE_REGS_ENTRYPOINT
1146 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0, 0 );
1149 /***********************************************************************
1150 * GlobalMemoryStatusEx (KERNEL32.@)
1151 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1156 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1158 static MEMORYSTATUSEX cached_memstatus;
1159 static int cache_lastchecked = 0;
1164 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1167 int mib[2] = { CTL_HW };
1170 long pagesize,maxpages,freepages,swapspace,swapfree;
1171 struct anoninfo swapinf;
1175 if (time(NULL)==cache_lastchecked) {
1176 memcpy(lpmemex,&cached_memstatus,sizeof(*lpmemex));
1179 cache_lastchecked = time(NULL);
1181 lpmemex->dwLength = sizeof(*lpmemex);
1182 lpmemex->dwMemoryLoad = 0;
1183 lpmemex->ullTotalPhys = 16*1024*1024;
1184 lpmemex->ullAvailPhys = 16*1024*1024;
1185 lpmemex->ullTotalPageFile = 16*1024*1024;
1186 lpmemex->ullAvailPageFile = 16*1024*1024;
1189 f = fopen( "/proc/meminfo", "r" );
1193 int total, used, free, shared, buffers, cached;
1195 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1196 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1197 while (fgets( buffer, sizeof(buffer), f ))
1199 /* old style /proc/meminfo ... */
1200 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1202 lpmemex->ullTotalPhys += total;
1203 lpmemex->ullAvailPhys += free + buffers + cached;
1205 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1207 lpmemex->ullTotalPageFile += total;
1208 lpmemex->ullAvailPageFile += free;
1211 /* new style /proc/meminfo ... */
1212 if (sscanf(buffer, "MemTotal: %d", &total))
1213 lpmemex->ullTotalPhys = total*1024;
1214 if (sscanf(buffer, "MemFree: %d", &free))
1215 lpmemex->ullAvailPhys = free*1024;
1216 if (sscanf(buffer, "SwapTotal: %d", &total))
1217 lpmemex->ullTotalPageFile = total*1024;
1218 if (sscanf(buffer, "SwapFree: %d", &free))
1219 lpmemex->ullAvailPageFile = free*1024;
1220 if (sscanf(buffer, "Buffers: %d", &buffers))
1221 lpmemex->ullAvailPhys += buffers*1024;
1222 if (sscanf(buffer, "Cached: %d", &cached))
1223 lpmemex->ullAvailPhys += cached*1024;
1227 if (lpmemex->ullTotalPhys)
1229 DWORDLONG TotalPhysical = lpmemex->ullTotalPhys+lpmemex->ullTotalPageFile;
1230 DWORDLONG AvailPhysical = lpmemex->ullAvailPhys+lpmemex->ullAvailPageFile;
1231 lpmemex->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1232 / (TotalPhysical / 100);
1235 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1236 mib[1] = HW_PHYSMEM;
1237 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1238 tmp = malloc(size_sys * sizeof(int));
1239 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1242 lpmemex->ullTotalPhys = *tmp;
1244 mib[1] = HW_USERMEM;
1245 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1246 tmp = malloc(size_sys * sizeof(int));
1247 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1250 lpmemex->ullAvailPhys = *tmp;
1251 lpmemex->ullTotalPageFile = *tmp;
1252 lpmemex->ullAvailPageFile = *tmp;
1253 lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys;
1256 lpmemex->ullAvailPhys = lpmemex->ullTotalPhys;
1257 lpmemex->ullTotalPageFile = lpmemex->ullTotalPhys;
1258 lpmemex->ullAvailPageFile = lpmemex->ullTotalPhys;
1259 lpmemex->dwMemoryLoad = 0;
1264 #elif defined ( sun )
1265 pagesize=sysconf(_SC_PAGESIZE);
1266 maxpages=sysconf(_SC_PHYS_PAGES);
1267 freepages=sysconf(_SC_AVPHYS_PAGES);
1268 rval=swapctl(SC_AINFO, &swapinf);
1271 swapspace=swapinf.ani_max*pagesize;
1272 swapfree=swapinf.ani_free*pagesize;
1276 WARN("Swap size cannot be determined , assuming equal to physical memory\n");
1277 swapspace=maxpages*pagesize;
1278 swapfree=maxpages*pagesize;
1280 lpmemex->ullTotalPhys=pagesize*maxpages;
1281 lpmemex->ullAvailPhys = pagesize*freepages;
1282 lpmemex->ullTotalPageFile = swapspace;
1283 lpmemex->ullAvailPageFile = swapfree;
1284 lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys;
1287 /* Project2k refuses to start if it sees less than 1Mb of free swap */
1288 if (lpmemex->ullTotalPageFile < lpmemex->ullTotalPhys)
1289 lpmemex->ullTotalPageFile = lpmemex->ullTotalPhys;
1290 if (lpmemex->ullAvailPageFile < lpmemex->ullAvailPhys)
1291 lpmemex->ullAvailPageFile = lpmemex->ullAvailPhys;
1293 /* FIXME: should do something for other systems */
1295 lpmemex->ullTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1296 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1297 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1299 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1300 memory in the extended portion of the virtual address space of the calling
1302 However, I don't know what this means, so set it to zero :(
1304 lpmemex->ullAvailExtendedVirtual = 0;
1306 memcpy(&cached_memstatus,lpmemex,sizeof(*lpmemex));
1308 TRACE("<-- LPMEMORYSTATUSEX: dwLength %ld, dwMemoryLoad %ld, ullTotalPhys %s, ullAvailPhys %s,"
1309 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1310 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1311 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1312 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1313 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1318 /***********************************************************************
1319 * GlobalMemoryStatus (KERNEL32.@)
1320 * Provides information about the status of the memory, so apps can tell
1321 * roughly how much they are able to allocate
1326 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1328 MEMORYSTATUSEX memstatus;
1329 OSVERSIONINFOW osver;
1331 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1332 for one extra field in the struct, and the lack of a bug, we simply
1333 call GlobalMemoryStatusEx and copy the values across. */
1334 GlobalMemoryStatusEx(&memstatus);
1336 lpBuffer->dwLength = sizeof(*lpBuffer);
1337 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1339 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1340 * NT reports values modulo 4 Gb.
1341 * Values between 2 Gb and 4 Gb are rounded down to 2 Gb.
1344 osver.dwOSVersionInfoSize = sizeof(osver);
1345 GetVersionExW(&osver);
1347 if ( osver.dwMajorVersion >= 5 )
1349 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? MAXDWORD :
1350 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1351 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? MAXDWORD :
1352 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1353 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? MAXDWORD :
1354 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1355 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? MAXDWORD :
1356 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1357 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? MAXDWORD :
1358 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1359 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? MAXDWORD :
1360 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1362 else /* duplicate NT bug */
1364 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? memstatus.ullTotalPhys :
1365 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1366 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? memstatus.ullAvailPhys :
1367 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1368 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? memstatus.ullTotalPageFile :
1369 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1370 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? memstatus.ullAvailPageFile :
1371 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1372 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? memstatus.ullTotalVirtual :
1373 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1374 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? memstatus.ullAvailVirtual :
1375 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1378 /* work around for broken photoshop 4 installer */
1379 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1380 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;