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>
49 #include "wine/exception.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
54 /* address where we try to map the system heap */
55 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
56 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
58 static HANDLE systemHeap
; /* globally shared heap */
62 /* filter for page-fault exceptions */
63 /* It is possible for a bogus global pointer to cause a */
64 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
65 static WINE_EXCEPTION_FILTER(page_fault
)
67 switch (GetExceptionCode()) {
68 case (EXCEPTION_ACCESS_VIOLATION
):
69 case (EXCEPTION_PRIV_INSTRUCTION
):
70 return EXCEPTION_EXECUTE_HANDLER
;
72 return EXCEPTION_CONTINUE_SEARCH
;
76 /***********************************************************************
77 * HEAP_CreateSystemHeap
79 * Create the system heap.
81 inline static HANDLE
HEAP_CreateSystemHeap(void)
87 /* create the system heap event first */
88 event
= CreateEventA( NULL
, TRUE
, FALSE
, "__wine_system_heap_event" );
90 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
91 0, SYSTEM_HEAP_SIZE
, "__wine_system_heap" ))) 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 if (created
) /* newly created heap */
103 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
104 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
109 /* wait for the heap to be initialized */
110 WaitForSingleObject( event
, INFINITE
);
111 systemHeap
= (HANDLE
)base
;
118 /***********************************************************************
119 * HeapCreate (KERNEL32.@)
121 * Handle of heap: Success
124 HANDLE WINAPI
HeapCreate(
125 DWORD flags
, /* [in] Heap allocation flag */
126 SIZE_T initialSize
, /* [in] Initial heap size */
127 SIZE_T maxSize
/* [in] Maximum heap size */
131 if ( flags
& HEAP_SHARED
)
133 if (!systemHeap
) HEAP_CreateSystemHeap();
134 else WARN( "Shared Heap requested, returning system heap.\n" );
139 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
140 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
146 /***********************************************************************
147 * HeapDestroy (KERNEL32.@)
152 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
154 if (heap
== systemHeap
)
156 WARN( "attempt to destroy system heap, returning TRUE!\n" );
159 if (!RtlDestroyHeap( heap
)) return TRUE
;
160 SetLastError( ERROR_INVALID_HANDLE
);
165 /***********************************************************************
166 * HeapCompact (KERNEL32.@)
168 SIZE_T WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
170 return RtlCompactHeap( heap
, flags
);
174 /***********************************************************************
175 * HeapValidate (KERNEL32.@)
176 * Validates a specified heap.
185 BOOL WINAPI
HeapValidate(
186 HANDLE heap
, /* [in] Handle to the heap */
187 DWORD flags
, /* [in] Bit flags that control access during operation */
188 LPCVOID block
/* [in] Optional pointer to memory block to validate */
190 return RtlValidateHeap( heap
, flags
, block
);
194 /***********************************************************************
195 * HeapWalk (KERNEL32.@)
196 * Enumerates the memory blocks in a specified heap.
199 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
200 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
206 BOOL WINAPI
HeapWalk(
207 HANDLE heap
, /* [in] Handle to heap to enumerate */
208 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
210 NTSTATUS ret
= RtlWalkHeap( heap
, entry
);
211 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
216 /***********************************************************************
217 * HeapLock (KERNEL32.@)
218 * Attempts to acquire the critical section object for a specified heap.
224 BOOL WINAPI
HeapLock(
225 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
227 return RtlLockHeap( heap
);
231 /***********************************************************************
232 * HeapUnlock (KERNEL32.@)
233 * Releases ownership of the critical section object.
239 BOOL WINAPI
HeapUnlock(
240 HANDLE heap
/* [in] Handle to the heap to unlock */
242 return RtlUnlockHeap( heap
);
246 /***********************************************************************
247 * GetProcessHeap (KERNEL32.@)
249 HANDLE WINAPI
GetProcessHeap(void)
251 return NtCurrentTeb()->Peb
->ProcessHeap
;
255 /***********************************************************************
256 * GetProcessHeaps (KERNEL32.@)
258 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
260 return RtlGetProcessHeaps( count
, heaps
);
264 /* These are needed so that we can call the functions from inside kernel itself */
266 LPVOID WINAPI
HeapAlloc( HANDLE heap
, DWORD flags
, SIZE_T size
)
268 return RtlAllocateHeap( heap
, flags
, size
);
271 BOOL WINAPI
HeapFree( HANDLE heap
, DWORD flags
, LPVOID ptr
)
273 return RtlFreeHeap( heap
, flags
, ptr
);
276 LPVOID WINAPI
HeapReAlloc( HANDLE heap
, DWORD flags
, LPVOID ptr
, SIZE_T size
)
278 return RtlReAllocateHeap( heap
, flags
, ptr
, size
);
281 SIZE_T WINAPI
HeapSize( HANDLE heap
, DWORD flags
, LPVOID ptr
)
283 return RtlSizeHeap( heap
, flags
, ptr
);
287 * Win32 Global heap functions (GlobalXXX).
288 * These functions included in Win32 for compatibility with 16 bit Windows
289 * Especially the moveable blocks and handles are oldish.
290 * But the ability to directly allocate memory with GPTR and LPTR is widely
293 * The handle stuff looks horrible, but it's implemented almost like Win95
298 #define MAGIC_GLOBAL_USED 0x5342
299 #define GLOBAL_LOCK_MAX 0xFF
300 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
301 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
302 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
303 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
304 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
305 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
306 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
307 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
308 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
309 * the output jpeg's > 1 MB if not */
310 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
312 #include "pshpack1.h"
314 typedef struct __GLOBAL32_INTERN
320 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
324 /***********************************************************************
325 * GlobalAlloc (KERNEL32.@)
330 HGLOBAL WINAPI
GlobalAlloc(
331 UINT flags
, /* [in] Object allocation attributes */
332 SIZE_T size
/* [in] Number of bytes to allocate */
334 PGLOBAL32_INTERN pintern
;
338 if(flags
&GMEM_ZEROINIT
)
339 hpflags
=HEAP_ZERO_MEMORY
;
343 TRACE("() flags=%04x\n", flags
);
345 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
347 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
348 return (HGLOBAL
) palloc
;
352 RtlLockHeap(GetProcessHeap());
354 pintern
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
357 pintern
->Magic
= MAGIC_GLOBAL_USED
;
358 pintern
->Flags
= flags
>> 8;
359 pintern
->LockCount
= 0;
363 palloc
= HeapAlloc(GetProcessHeap(), hpflags
, size
+HGLOBAL_STORAGE
);
366 HeapFree(GetProcessHeap(), 0, pintern
);
371 *(HGLOBAL
*)palloc
= INTERN_TO_HANDLE(pintern
);
372 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
376 pintern
->Pointer
= NULL
;
379 RtlUnlockHeap(GetProcessHeap());
380 return pintern
? INTERN_TO_HANDLE(pintern
) : 0;
385 /***********************************************************************
386 * GlobalLock (KERNEL32.@)
388 * Pointer to first byte of block
391 LPVOID WINAPI
GlobalLock(
392 HGLOBAL hmem
/* [in] Handle of global memory object */
395 PGLOBAL32_INTERN pintern
;
399 return IsBadReadPtr(hmem
, 1) ? NULL
: hmem
;
401 RtlLockHeap(GetProcessHeap());
404 pintern
= HANDLE_TO_INTERN(hmem
);
405 if (pintern
->Magic
== MAGIC_GLOBAL_USED
)
407 if (pintern
->LockCount
< GLOBAL_LOCK_MAX
)
408 pintern
->LockCount
++;
409 palloc
= pintern
->Pointer
;
413 WARN("invalid handle %p\n", hmem
);
415 SetLastError(ERROR_INVALID_HANDLE
);
420 WARN("page fault on %p\n", hmem
);
422 SetLastError(ERROR_INVALID_HANDLE
);
425 RtlUnlockHeap(GetProcessHeap());
430 /***********************************************************************
431 * GlobalUnlock (KERNEL32.@)
433 * TRUE: Object is still locked
434 * FALSE: Object is unlocked
436 BOOL WINAPI
GlobalUnlock(
437 HGLOBAL hmem
/* [in] Handle of global memory object */
439 PGLOBAL32_INTERN pintern
;
442 if (ISPOINTER(hmem
)) return FALSE
;
444 RtlLockHeap(GetProcessHeap());
447 pintern
=HANDLE_TO_INTERN(hmem
);
448 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
450 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
451 pintern
->LockCount
--;
453 locked
= (pintern
->LockCount
!= 0);
454 if (!locked
) SetLastError(NO_ERROR
);
458 WARN("invalid handle\n");
459 SetLastError(ERROR_INVALID_HANDLE
);
465 ERR("page fault occurred ! Caused by bug ?\n");
466 SetLastError( ERROR_INVALID_PARAMETER
);
470 RtlUnlockHeap(GetProcessHeap());
475 /***********************************************************************
476 * GlobalHandle (KERNEL32.@)
477 * Returns the handle associated with the specified pointer.
483 HGLOBAL WINAPI
GlobalHandle(
484 LPCVOID pmem
/* [in] Pointer to global memory block */
487 PGLOBAL32_INTERN maybe_intern
;
492 SetLastError( ERROR_INVALID_PARAMETER
);
496 RtlLockHeap(GetProcessHeap());
501 /* note that if pmem is a pointer to a a block allocated by */
502 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
504 if (ISPOINTER(pmem
)) {
505 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
506 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
509 handle
= POINTER_TO_HANDLE(pmem
);
511 handle
= (HGLOBAL
)pmem
;
513 /* Now test handle either passed in or retrieved from pointer */
514 maybe_intern
= HANDLE_TO_INTERN( handle
);
515 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
516 test
= maybe_intern
->Pointer
;
517 if (HeapValidate( GetProcessHeap(), 0, (const char *)test
- HGLOBAL_STORAGE
) && /* obj(-handle) valid arena? */
518 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
519 break; /* valid moveable block */
522 SetLastError( ERROR_INVALID_HANDLE
);
526 SetLastError( ERROR_INVALID_HANDLE
);
530 RtlUnlockHeap(GetProcessHeap());
536 /***********************************************************************
537 * GlobalReAlloc (KERNEL32.@)
542 HGLOBAL WINAPI
GlobalReAlloc(
543 HGLOBAL hmem
, /* [in] Handle of global memory object */
544 SIZE_T size
, /* [in] New size of block */
545 UINT flags
/* [in] How to reallocate object */
549 PGLOBAL32_INTERN pintern
;
550 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
553 RtlLockHeap(GetProcessHeap());
554 if(flags
& GMEM_MODIFY
) /* modify flags */
556 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
558 /* make a fixed block moveable
559 * actually only NT is able to do this. But it's soo simple
563 WARN("GlobalReAlloc with null handle!\n");
564 SetLastError( ERROR_NOACCESS
);
569 size
= HeapSize(GetProcessHeap(), 0, (LPVOID
)hmem
);
570 hnew
= GlobalAlloc(flags
, size
);
571 palloc
= GlobalLock(hnew
);
572 memcpy(palloc
, (LPVOID
)hmem
, size
);
577 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
579 /* change the flags to make our block "discardable" */
580 pintern
=HANDLE_TO_INTERN(hmem
);
581 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
586 SetLastError(ERROR_INVALID_PARAMETER
);
594 /* reallocate fixed memory */
595 hnew
=HeapReAlloc(GetProcessHeap(), heap_flags
, hmem
, size
);
599 /* reallocate a moveable block */
600 pintern
=HANDLE_TO_INTERN(hmem
);
603 /* Apparently Windows doesn't care whether the handle is locked at this point */
604 /* See also the same comment in GlobalFree() */
605 if(pintern
->LockCount
>1) {
606 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
607 SetLastError(ERROR_INVALID_HANDLE
);
615 if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
616 (char *) pintern
->Pointer
-HGLOBAL_STORAGE
,
617 size
+HGLOBAL_STORAGE
)) == NULL
)
618 hnew
= 0; /* Block still valid */
620 pintern
->Pointer
= (char *)palloc
+HGLOBAL_STORAGE
;
624 if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+HGLOBAL_STORAGE
))
629 *(HGLOBAL
*)palloc
= hmem
;
630 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
638 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-HGLOBAL_STORAGE
);
639 pintern
->Pointer
=NULL
;
644 RtlUnlockHeap(GetProcessHeap());
649 /***********************************************************************
650 * GlobalFree (KERNEL32.@)
655 HGLOBAL WINAPI
GlobalFree(
656 HGLOBAL hmem
/* [in] Handle of global memory object */
658 PGLOBAL32_INTERN pintern
;
661 RtlLockHeap(GetProcessHeap());
665 if(ISPOINTER(hmem
)) /* POINTER */
667 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
671 pintern
=HANDLE_TO_INTERN(hmem
);
673 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
676 /* WIN98 does not make this test. That is you can free a */
677 /* block you have not unlocked. Go figure!! */
678 /* if(pintern->LockCount!=0) */
679 /* SetLastError(ERROR_INVALID_HANDLE); */
682 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-HGLOBAL_STORAGE
))
684 if(!HeapFree(GetProcessHeap(), 0, pintern
))
691 ERR("page fault occurred ! Caused by bug ?\n");
692 SetLastError( ERROR_INVALID_PARAMETER
);
696 RtlUnlockHeap(GetProcessHeap());
701 /***********************************************************************
702 * GlobalSize (KERNEL32.@)
704 * Size in bytes of the global memory object
707 SIZE_T WINAPI
GlobalSize(
708 HGLOBAL hmem
/* [in] Handle of global memory object */
711 PGLOBAL32_INTERN pintern
;
717 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
721 RtlLockHeap(GetProcessHeap());
722 pintern
=HANDLE_TO_INTERN(hmem
);
724 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
726 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
730 retval
= HeapSize(GetProcessHeap(), 0,
731 (char *)(pintern
->Pointer
) - HGLOBAL_STORAGE
);
732 if (retval
!= (DWORD
)-1) retval
-= HGLOBAL_STORAGE
;
737 WARN("invalid handle\n");
740 RtlUnlockHeap(GetProcessHeap());
742 /* HeapSize returns 0xffffffff on failure */
743 if (retval
== 0xffffffff) retval
= 0;
748 /***********************************************************************
749 * GlobalWire (KERNEL32.@)
751 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
753 return GlobalLock( hmem
);
757 /***********************************************************************
758 * GlobalUnWire (KERNEL32.@)
760 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
762 return GlobalUnlock( hmem
);
766 /***********************************************************************
767 * GlobalFix (KERNEL32.@)
769 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
775 /***********************************************************************
776 * GlobalUnfix (KERNEL32.@)
778 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
784 /***********************************************************************
785 * GlobalFlags (KERNEL32.@)
786 * Returns information about the specified global memory object
789 * Should this return GMEM_INVALID_HANDLE on invalid handle?
792 * Value specifying allocation flags and lock count
793 * GMEM_INVALID_HANDLE: Failure
795 UINT WINAPI
GlobalFlags(
796 HGLOBAL hmem
/* [in] Handle to global memory object */
799 PGLOBAL32_INTERN pintern
;
807 RtlLockHeap(GetProcessHeap());
808 pintern
=HANDLE_TO_INTERN(hmem
);
809 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
811 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
812 if(pintern
->Pointer
==0)
813 retval
|= GMEM_DISCARDED
;
817 WARN("Invalid handle: %p\n", hmem
);
820 RtlUnlockHeap(GetProcessHeap());
826 /***********************************************************************
827 * GlobalCompact (KERNEL32.@)
829 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
831 return 0; /* GlobalCompact does nothing in Win32 */
835 /***********************************************************************
836 * LocalAlloc (KERNEL32.@)
841 HLOCAL WINAPI
LocalAlloc(
842 UINT flags
, /* [in] Allocation attributes */
843 SIZE_T size
/* [in] Number of bytes to allocate */
845 return (HLOCAL
)GlobalAlloc( flags
, size
);
849 /***********************************************************************
850 * LocalCompact (KERNEL32.@)
852 SIZE_T WINAPI
LocalCompact( UINT minfree
)
854 return 0; /* LocalCompact does nothing in Win32 */
858 /***********************************************************************
859 * LocalFlags (KERNEL32.@)
861 * Value specifying allocation flags and lock count.
862 * LMEM_INVALID_HANDLE: Failure
864 UINT WINAPI
LocalFlags(
865 HLOCAL handle
/* [in] Handle of memory object */
867 return GlobalFlags( (HGLOBAL
)handle
);
871 /***********************************************************************
872 * LocalFree (KERNEL32.@)
877 HLOCAL WINAPI
LocalFree(
878 HLOCAL handle
/* [in] Handle of memory object */
880 return (HLOCAL
)GlobalFree( (HGLOBAL
)handle
);
884 /***********************************************************************
885 * LocalHandle (KERNEL32.@)
890 HLOCAL WINAPI
LocalHandle(
891 LPCVOID ptr
/* [in] Address of local memory object */
893 return (HLOCAL
)GlobalHandle( ptr
);
897 /***********************************************************************
898 * LocalLock (KERNEL32.@)
899 * Locks a local memory object and returns pointer to the first byte
900 * of the memory block.
906 LPVOID WINAPI
LocalLock(
907 HLOCAL handle
/* [in] Address of local memory object */
909 return GlobalLock( (HGLOBAL
)handle
);
913 /***********************************************************************
914 * LocalReAlloc (KERNEL32.@)
919 HLOCAL WINAPI
LocalReAlloc(
920 HLOCAL handle
, /* [in] Handle of memory object */
921 SIZE_T size
, /* [in] New size of block */
922 UINT flags
/* [in] How to reallocate object */
924 return (HLOCAL
)GlobalReAlloc( (HGLOBAL
)handle
, size
, flags
);
928 /***********************************************************************
929 * LocalShrink (KERNEL32.@)
931 SIZE_T WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
933 return 0; /* LocalShrink does nothing in Win32 */
937 /***********************************************************************
938 * LocalSize (KERNEL32.@)
943 SIZE_T WINAPI
LocalSize(
944 HLOCAL handle
/* [in] Handle of memory object */
946 return GlobalSize( (HGLOBAL
)handle
);
950 /***********************************************************************
951 * LocalUnlock (KERNEL32.@)
953 * TRUE: Object is still locked
954 * FALSE: Object is unlocked
956 BOOL WINAPI
LocalUnlock(
957 HLOCAL handle
/* [in] Handle of memory object */
959 return GlobalUnlock( (HGLOBAL
)handle
);
963 /**********************************************************************
964 * AllocMappedBuffer (KERNEL32.38)
966 * This is an undocumented KERNEL32 function that
967 * SMapLS's a GlobalAlloc'ed buffer.
969 * Input: EDI register: size of buffer to allocate
970 * Output: EDI register: pointer to buffer
972 * Note: The buffer is preceded by 8 bytes:
975 * edi-4 SEGPTR to buffer
976 * edi-8 some magic Win95 needs for SUnMapLS
977 * (we use it for the memory handle)
979 * The SEGPTR is used by the caller!
981 void WINAPI
__regs_AllocMappedBuffer( CONTEXT86
*context
)
983 HGLOBAL handle
= GlobalAlloc(0, context
->Edi
+ 8);
984 DWORD
*buffer
= (DWORD
*)GlobalLock(handle
);
988 if (!(ptr
= MapLS(buffer
+ 2)))
990 GlobalUnlock(handle
);
995 context
->Eax
= context
->Edi
= 0;
998 buffer
[0] = (DWORD
)handle
;
1001 context
->Eax
= (DWORD
) ptr
;
1002 context
->Edi
= (DWORD
)(buffer
+ 2);
1005 #ifdef DEFINE_REGS_ENTRYPOINT
1006 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer
, 0, 0 );
1009 /**********************************************************************
1010 * FreeMappedBuffer (KERNEL32.39)
1012 * Free a buffer allocated by AllocMappedBuffer
1014 * Input: EDI register: pointer to buffer
1016 void WINAPI
__regs_FreeMappedBuffer( CONTEXT86
*context
)
1020 DWORD
*buffer
= (DWORD
*)context
->Edi
- 2;
1024 GlobalUnlock((HGLOBAL
)buffer
[0]);
1025 GlobalFree((HGLOBAL
)buffer
[0]);
1028 #ifdef DEFINE_REGS_ENTRYPOINT
1029 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer
, 0, 0 );
1032 /***********************************************************************
1033 * GlobalMemoryStatusEx (KERNEL32.@)
1034 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1039 BOOL WINAPI
GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex
)
1041 static MEMORYSTATUSEX cached_memstatus
;
1042 static int cache_lastchecked
= 0;
1047 #if defined(__FreeBSD__) || defined(__NetBSD__)
1050 int mib
[2] = { CTL_HW
};
1052 if (time(NULL
)==cache_lastchecked
) {
1053 memcpy(lpmemex
,&cached_memstatus
,sizeof(*lpmemex
));
1056 cache_lastchecked
= time(NULL
);
1058 lpmemex
->dwLength
= sizeof(*lpmemex
);
1059 lpmemex
->dwMemoryLoad
= 0;
1060 lpmemex
->ullTotalPhys
= 16*1024*1024;
1061 lpmemex
->ullAvailPhys
= 16*1024*1024;
1062 lpmemex
->ullTotalPageFile
= 16*1024*1024;
1063 lpmemex
->ullAvailPageFile
= 16*1024*1024;
1066 f
= fopen( "/proc/meminfo", "r" );
1070 int total
, used
, free
, shared
, buffers
, cached
;
1072 lpmemex
->ullTotalPhys
= lpmemex
->ullAvailPhys
= 0;
1073 lpmemex
->ullTotalPageFile
= lpmemex
->ullAvailPageFile
= 0;
1074 while (fgets( buffer
, sizeof(buffer
), f
))
1076 /* old style /proc/meminfo ... */
1077 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1079 lpmemex
->ullTotalPhys
+= total
;
1080 lpmemex
->ullAvailPhys
+= free
+ buffers
+ cached
;
1082 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1084 lpmemex
->ullTotalPageFile
+= total
;
1085 lpmemex
->ullAvailPageFile
+= free
;
1088 /* new style /proc/meminfo ... */
1089 if (sscanf(buffer
, "MemTotal: %d", &total
))
1090 lpmemex
->ullTotalPhys
= total
*1024;
1091 if (sscanf(buffer
, "MemFree: %d", &free
))
1092 lpmemex
->ullAvailPhys
= free
*1024;
1093 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1094 lpmemex
->ullTotalPageFile
= total
*1024;
1095 if (sscanf(buffer
, "SwapFree: %d", &free
))
1096 lpmemex
->ullAvailPageFile
= free
*1024;
1097 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1098 lpmemex
->ullAvailPhys
+= buffers
*1024;
1099 if (sscanf(buffer
, "Cached: %d", &cached
))
1100 lpmemex
->ullAvailPhys
+= cached
*1024;
1104 if (lpmemex
->ullTotalPhys
)
1106 DWORDLONG TotalPhysical
= lpmemex
->ullTotalPhys
+lpmemex
->ullTotalPageFile
;
1107 DWORDLONG AvailPhysical
= lpmemex
->ullAvailPhys
+lpmemex
->ullAvailPageFile
;
1108 lpmemex
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1109 / (TotalPhysical
/ 100);
1112 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1113 mib
[1] = HW_PHYSMEM
;
1114 sysctl(mib
, 2, NULL
, &size_sys
, NULL
, 0);
1115 tmp
= malloc(size_sys
* sizeof(int));
1116 sysctl(mib
, 2, tmp
, &size_sys
, NULL
, 0);
1119 lpmemex
->ullTotalPhys
= *tmp
;
1121 mib
[1] = HW_USERMEM
;
1122 sysctl(mib
, 2, NULL
, &size_sys
, NULL
, 0);
1123 tmp
= malloc(size_sys
* sizeof(int));
1124 sysctl(mib
, 2, tmp
, &size_sys
, NULL
, 0);
1127 lpmemex
->ullAvailPhys
= *tmp
;
1128 lpmemex
->ullTotalPageFile
= *tmp
;
1129 lpmemex
->ullAvailPageFile
= *tmp
;
1130 lpmemex
->dwMemoryLoad
= lpmemex
->ullTotalPhys
- lpmemex
->ullAvailPhys
;
1133 lpmemex
->ullAvailPhys
= lpmemex
->ullTotalPhys
;
1134 lpmemex
->ullTotalPageFile
= lpmemex
->ullTotalPhys
;
1135 lpmemex
->ullAvailPageFile
= lpmemex
->ullTotalPhys
;
1136 lpmemex
->dwMemoryLoad
= 0;
1143 /* Project2k refuses to start if it sees less than 1Mb of free swap */
1144 if (lpmemex
->ullTotalPageFile
< lpmemex
->ullTotalPhys
)
1145 lpmemex
->ullTotalPageFile
= lpmemex
->ullTotalPhys
;
1146 if (lpmemex
->ullAvailPageFile
< lpmemex
->ullAvailPhys
)
1147 lpmemex
->ullAvailPageFile
= lpmemex
->ullAvailPhys
;
1149 /* FIXME: should do something for other systems */
1151 lpmemex
->ullTotalVirtual
= (char*)si
.lpMaximumApplicationAddress
-(char*)si
.lpMinimumApplicationAddress
;
1152 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1153 lpmemex
->ullAvailVirtual
= lpmemex
->ullTotalVirtual
-64*1024;
1155 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1156 memory in the extended portion of the virtual address space of the calling
1158 However, I don't know what this means, so set it to zero :(
1160 lpmemex
->ullAvailExtendedVirtual
= 0;
1162 memcpy(&cached_memstatus
,lpmemex
,sizeof(*lpmemex
));
1164 TRACE("<-- LPMEMORYSTATUSEX: dwLength %ld, dwMemoryLoad %ld, ullTotalPhys %s, ullAvailPhys %s,"
1165 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1166 lpmemex
->dwLength
, lpmemex
->dwMemoryLoad
, wine_dbgstr_longlong(lpmemex
->ullTotalPhys
),
1167 wine_dbgstr_longlong(lpmemex
->ullAvailPhys
), wine_dbgstr_longlong(lpmemex
->ullTotalPageFile
),
1168 wine_dbgstr_longlong(lpmemex
->ullAvailPageFile
), wine_dbgstr_longlong(lpmemex
->ullTotalVirtual
),
1169 wine_dbgstr_longlong(lpmemex
->ullAvailVirtual
) );
1174 /***********************************************************************
1175 * GlobalMemoryStatus (KERNEL32.@)
1176 * Provides information about the status of the memory, so apps can tell
1177 * roughly how much they are able to allocate
1182 VOID WINAPI
GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
)
1184 MEMORYSTATUSEX memstatus
;
1185 OSVERSIONINFOW osver
;
1187 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1188 for one extra field in the struct, and the lack of a bug, we simply
1189 call GlobalMemoryStatusEx and copy the values across. */
1190 GlobalMemoryStatusEx(&memstatus
);
1192 lpBuffer
->dwLength
= sizeof(*lpBuffer
);
1193 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
1195 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1196 * NT reports values modulo 4 Gb.
1197 * Values between 2 Gb and 4 Gb are rounded down to 2 Gb.
1200 osver
.dwOSVersionInfoSize
= sizeof(osver
);
1201 GetVersionExW(&osver
);
1203 if ( osver
.dwMajorVersion
>= 5 )
1205 lpBuffer
->dwTotalPhys
= (memstatus
.ullTotalPhys
> MAXDWORD
) ? MAXDWORD
:
1206 (memstatus
.ullTotalPhys
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalPhys
;
1207 lpBuffer
->dwAvailPhys
= (memstatus
.ullAvailPhys
> MAXDWORD
) ? MAXDWORD
:
1208 (memstatus
.ullAvailPhys
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailPhys
;
1209 lpBuffer
->dwTotalPageFile
= (memstatus
.ullTotalPageFile
> MAXDWORD
) ? MAXDWORD
:
1210 (memstatus
.ullTotalPageFile
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalPageFile
;
1211 lpBuffer
->dwAvailPageFile
= (memstatus
.ullAvailPageFile
> MAXDWORD
) ? MAXDWORD
:
1212 (memstatus
.ullAvailPageFile
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailPageFile
;
1213 lpBuffer
->dwTotalVirtual
= (memstatus
.ullTotalVirtual
> MAXDWORD
) ? MAXDWORD
:
1214 (memstatus
.ullTotalVirtual
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalVirtual
;
1215 lpBuffer
->dwAvailVirtual
= (memstatus
.ullAvailVirtual
> MAXDWORD
) ? MAXDWORD
:
1216 (memstatus
.ullAvailVirtual
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailVirtual
;
1218 else /* duplicate NT bug */
1220 lpBuffer
->dwTotalPhys
= (memstatus
.ullTotalPhys
> MAXDWORD
) ? memstatus
.ullTotalPhys
:
1221 (memstatus
.ullTotalPhys
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalPhys
;
1222 lpBuffer
->dwAvailPhys
= (memstatus
.ullAvailPhys
> MAXDWORD
) ? memstatus
.ullAvailPhys
:
1223 (memstatus
.ullAvailPhys
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailPhys
;
1224 lpBuffer
->dwTotalPageFile
= (memstatus
.ullTotalPageFile
> MAXDWORD
) ? memstatus
.ullTotalPageFile
:
1225 (memstatus
.ullTotalPageFile
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalPageFile
;
1226 lpBuffer
->dwAvailPageFile
= (memstatus
.ullAvailPageFile
> MAXDWORD
) ? memstatus
.ullAvailPageFile
:
1227 (memstatus
.ullAvailPageFile
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailPageFile
;
1228 lpBuffer
->dwTotalVirtual
= (memstatus
.ullTotalVirtual
> MAXDWORD
) ? memstatus
.ullTotalVirtual
:
1229 (memstatus
.ullTotalVirtual
> MAXLONG
) ? MAXLONG
: memstatus
.ullTotalVirtual
;
1230 lpBuffer
->dwAvailVirtual
= (memstatus
.ullAvailVirtual
> MAXDWORD
) ? memstatus
.ullAvailVirtual
:
1231 (memstatus
.ullAvailVirtual
> MAXLONG
) ? MAXLONG
: memstatus
.ullAvailVirtual
;
1234 /* work around for broken photoshop 4 installer */
1235 if ( lpBuffer
->dwAvailPhys
+ lpBuffer
->dwAvailPageFile
>= 2U*1024*1024*1024)
1236 lpBuffer
->dwAvailPageFile
= 2U*1024*1024*1024 - lpBuffer
->dwAvailPhys
- 1;