4 * Copyright 1995, 1996 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
31 #include <sys/types.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
51 #include "wine/exception.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
56 /* address where we try to map the system heap */
57 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
58 #define SYSTEM_HEAP_SIZE 0x100000 /* Default heap size = 1Mb */
60 static HANDLE systemHeap
; /* globally shared heap */
64 /* filter for page-fault exceptions */
65 /* It is possible for a bogus global pointer to cause a */
66 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
67 static WINE_EXCEPTION_FILTER(page_fault
)
69 switch (GetExceptionCode()) {
70 case (EXCEPTION_ACCESS_VIOLATION
):
71 case (EXCEPTION_PRIV_INSTRUCTION
):
72 return EXCEPTION_EXECUTE_HANDLER
;
74 return EXCEPTION_CONTINUE_SEARCH
;
78 /***********************************************************************
79 * HEAP_CreateSystemHeap
81 * Create the system heap.
83 inline static HANDLE
HEAP_CreateSystemHeap(void)
88 UNICODE_STRING event_name
;
89 OBJECT_ATTRIBUTES event_attr
;
91 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
92 0, SYSTEM_HEAP_SIZE
, "__SystemHeap" ))) return 0;
93 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
95 if (!(base
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
97 /* pre-defined address not available */
98 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
102 /* create the system heap event */
103 RtlCreateUnicodeStringFromAsciiz( &event_name
, "__SystemHeapEvent" );
104 event_attr
.Length
= sizeof(event_attr
);
105 event_attr
.RootDirectory
= 0;
106 event_attr
.ObjectName
= &event_name
;
107 event_attr
.Attributes
= 0;
108 event_attr
.SecurityDescriptor
= NULL
;
109 event_attr
.SecurityQualityOfService
= NULL
;
110 NtCreateEvent( &event
, EVENT_ALL_ACCESS
, &event_attr
, TRUE
, FALSE
);
112 if (created
) /* newly created heap */
114 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
115 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
116 NtSetEvent( event
, NULL
);
120 /* wait for the heap to be initialized */
121 WaitForSingleObject( event
, INFINITE
);
122 systemHeap
= (HANDLE
)base
;
129 /***********************************************************************
130 * HeapCreate (KERNEL32.@)
132 * Handle of heap: Success
135 HANDLE WINAPI
HeapCreate(
136 DWORD flags
, /* [in] Heap allocation flag */
137 SIZE_T initialSize
, /* [in] Initial heap size */
138 SIZE_T maxSize
/* [in] Maximum heap size */
142 if ( flags
& HEAP_SHARED
)
144 if (!systemHeap
) HEAP_CreateSystemHeap();
145 else WARN( "Shared Heap requested, returning system heap.\n" );
150 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
151 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
157 /***********************************************************************
158 * HeapDestroy (KERNEL32.@)
163 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
165 if (heap
== systemHeap
)
167 WARN( "attempt to destroy system heap, returning TRUE!\n" );
170 if (!RtlDestroyHeap( heap
)) return TRUE
;
171 SetLastError( ERROR_INVALID_HANDLE
);
176 /***********************************************************************
177 * HeapCompact (KERNEL32.@)
179 SIZE_T WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
181 return RtlCompactHeap( heap
, flags
);
185 /***********************************************************************
186 * HeapValidate (KERNEL32.@)
187 * Validates a specified heap.
196 BOOL WINAPI
HeapValidate(
197 HANDLE heap
, /* [in] Handle to the heap */
198 DWORD flags
, /* [in] Bit flags that control access during operation */
199 LPCVOID block
/* [in] Optional pointer to memory block to validate */
201 return RtlValidateHeap( heap
, flags
, block
);
205 /***********************************************************************
206 * HeapWalk (KERNEL32.@)
207 * Enumerates the memory blocks in a specified heap.
210 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
211 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
217 BOOL WINAPI
HeapWalk(
218 HANDLE heap
, /* [in] Handle to heap to enumerate */
219 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
221 NTSTATUS ret
= RtlWalkHeap( heap
, entry
);
222 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
227 /***********************************************************************
228 * HeapLock (KERNEL32.@)
229 * Attempts to acquire the critical section object for a specified heap.
235 BOOL WINAPI
HeapLock(
236 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
238 return RtlLockHeap( heap
);
242 /***********************************************************************
243 * HeapUnlock (KERNEL32.@)
244 * Releases ownership of the critical section object.
250 BOOL WINAPI
HeapUnlock(
251 HANDLE heap
/* [in] Handle to the heap to unlock */
253 return RtlUnlockHeap( heap
);
257 /***********************************************************************
258 * GetProcessHeap (KERNEL32.@)
260 HANDLE WINAPI
GetProcessHeap(void)
262 return NtCurrentTeb()->Peb
->ProcessHeap
;
266 /***********************************************************************
267 * GetProcessHeaps (KERNEL32.@)
269 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
271 return RtlGetProcessHeaps( count
, heaps
);
275 /* These are needed so that we can call the functions from inside kernel itself */
277 LPVOID WINAPI
HeapAlloc( HANDLE heap
, DWORD flags
, SIZE_T size
)
279 return RtlAllocateHeap( heap
, flags
, size
);
282 BOOL WINAPI
HeapFree( HANDLE heap
, DWORD flags
, LPVOID ptr
)
284 return RtlFreeHeap( heap
, flags
, ptr
);
287 LPVOID WINAPI
HeapReAlloc( HANDLE heap
, DWORD flags
, LPVOID ptr
, SIZE_T size
)
289 return RtlReAllocateHeap( heap
, flags
, ptr
, size
);
292 SIZE_T WINAPI
HeapSize( HANDLE heap
, DWORD flags
, LPVOID ptr
)
294 return RtlSizeHeap( heap
, flags
, ptr
);
298 * Win32 Global heap functions (GlobalXXX).
299 * These functions included in Win32 for compatibility with 16 bit Windows
300 * Especially the moveable blocks and handles are oldish.
301 * But the ability to directly allocate memory with GPTR and LPTR is widely
304 * The handle stuff looks horrible, but it's implemented almost like Win95
309 #define MAGIC_GLOBAL_USED 0x5342
310 #define GLOBAL_LOCK_MAX 0xFF
311 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
312 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
313 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
314 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
315 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
316 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
317 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
318 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
319 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
320 * the output jpeg's > 1 MB if not */
321 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
323 #include "pshpack1.h"
325 typedef struct __GLOBAL32_INTERN
331 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
335 /***********************************************************************
336 * GlobalAlloc (KERNEL32.@)
341 HGLOBAL WINAPI
GlobalAlloc(
342 UINT flags
, /* [in] Object allocation attributes */
343 SIZE_T size
/* [in] Number of bytes to allocate */
345 PGLOBAL32_INTERN pintern
;
349 if(flags
&GMEM_ZEROINIT
)
350 hpflags
=HEAP_ZERO_MEMORY
;
354 TRACE("() flags=%04x\n", flags
);
356 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
358 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
359 return (HGLOBAL
) palloc
;
363 RtlLockHeap(GetProcessHeap());
365 pintern
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
368 pintern
->Magic
= MAGIC_GLOBAL_USED
;
369 pintern
->Flags
= flags
>> 8;
370 pintern
->LockCount
= 0;
374 palloc
= HeapAlloc(GetProcessHeap(), hpflags
, size
+HGLOBAL_STORAGE
);
377 HeapFree(GetProcessHeap(), 0, pintern
);
382 *(HGLOBAL
*)palloc
= INTERN_TO_HANDLE(pintern
);
383 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
387 pintern
->Pointer
= NULL
;
390 RtlUnlockHeap(GetProcessHeap());
391 return pintern
? INTERN_TO_HANDLE(pintern
) : 0;
396 /***********************************************************************
397 * GlobalLock (KERNEL32.@)
399 * Pointer to first byte of block
402 LPVOID WINAPI
GlobalLock(
403 HGLOBAL hmem
/* [in] Handle of global memory object */
406 PGLOBAL32_INTERN pintern
;
410 return IsBadReadPtr(hmem
, 1) ? NULL
: hmem
;
412 RtlLockHeap(GetProcessHeap());
415 pintern
= HANDLE_TO_INTERN(hmem
);
416 if (pintern
->Magic
== MAGIC_GLOBAL_USED
)
418 if (pintern
->LockCount
< GLOBAL_LOCK_MAX
)
419 pintern
->LockCount
++;
420 palloc
= pintern
->Pointer
;
424 WARN("invalid handle %p\n", hmem
);
426 SetLastError(ERROR_INVALID_HANDLE
);
431 WARN("page fault on %p\n", hmem
);
433 SetLastError(ERROR_INVALID_HANDLE
);
436 RtlUnlockHeap(GetProcessHeap());
441 /***********************************************************************
442 * GlobalUnlock (KERNEL32.@)
444 * TRUE: Object is still locked
445 * FALSE: Object is unlocked
447 BOOL WINAPI
GlobalUnlock(
448 HGLOBAL hmem
/* [in] Handle of global memory object */
450 PGLOBAL32_INTERN pintern
;
453 if (ISPOINTER(hmem
)) return FALSE
;
455 RtlLockHeap(GetProcessHeap());
458 pintern
=HANDLE_TO_INTERN(hmem
);
459 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
461 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
462 pintern
->LockCount
--;
464 locked
= (pintern
->LockCount
!= 0);
465 if (!locked
) SetLastError(NO_ERROR
);
469 WARN("invalid handle\n");
470 SetLastError(ERROR_INVALID_HANDLE
);
476 ERR("page fault occurred ! Caused by bug ?\n");
477 SetLastError( ERROR_INVALID_PARAMETER
);
481 RtlUnlockHeap(GetProcessHeap());
486 /***********************************************************************
487 * GlobalHandle (KERNEL32.@)
488 * Returns the handle associated with the specified pointer.
494 HGLOBAL WINAPI
GlobalHandle(
495 LPCVOID pmem
/* [in] Pointer to global memory block */
498 PGLOBAL32_INTERN maybe_intern
;
503 SetLastError( ERROR_INVALID_PARAMETER
);
507 RtlLockHeap(GetProcessHeap());
512 /* note that if pmem is a pointer to a a block allocated by */
513 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
515 if (ISPOINTER(pmem
)) {
516 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
517 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
520 handle
= POINTER_TO_HANDLE(pmem
);
522 handle
= (HGLOBAL
)pmem
;
524 /* Now test handle either passed in or retrieved from pointer */
525 maybe_intern
= HANDLE_TO_INTERN( handle
);
526 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
527 test
= maybe_intern
->Pointer
;
528 if (HeapValidate( GetProcessHeap(), 0, (char *)test
- HGLOBAL_STORAGE
) && /* obj(-handle) valid arena? */
529 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
530 break; /* valid moveable block */
533 SetLastError( ERROR_INVALID_HANDLE
);
537 SetLastError( ERROR_INVALID_HANDLE
);
541 RtlUnlockHeap(GetProcessHeap());
547 /***********************************************************************
548 * GlobalReAlloc (KERNEL32.@)
553 HGLOBAL WINAPI
GlobalReAlloc(
554 HGLOBAL hmem
, /* [in] Handle of global memory object */
555 SIZE_T size
, /* [in] New size of block */
556 UINT flags
/* [in] How to reallocate object */
560 PGLOBAL32_INTERN pintern
;
561 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
564 RtlLockHeap(GetProcessHeap());
565 if(flags
& GMEM_MODIFY
) /* modify flags */
567 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
569 /* make a fixed block moveable
570 * actually only NT is able to do this. But it's soo simple
574 WARN("GlobalReAlloc with null handle!\n");
575 SetLastError( ERROR_NOACCESS
);
580 size
= HeapSize(GetProcessHeap(), 0, (LPVOID
)hmem
);
581 hnew
= GlobalAlloc(flags
, size
);
582 palloc
= GlobalLock(hnew
);
583 memcpy(palloc
, (LPVOID
)hmem
, size
);
588 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
590 /* change the flags to make our block "discardable" */
591 pintern
=HANDLE_TO_INTERN(hmem
);
592 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
597 SetLastError(ERROR_INVALID_PARAMETER
);
605 /* reallocate fixed memory */
606 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), heap_flags
, (LPVOID
) hmem
, size
);
610 /* reallocate a moveable block */
611 pintern
=HANDLE_TO_INTERN(hmem
);
614 /* Apparently Windows doesn't care whether the handle is locked at this point */
615 /* See also the same comment in GlobalFree() */
616 if(pintern
->LockCount
>1) {
617 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
618 SetLastError(ERROR_INVALID_HANDLE
);
626 if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
627 (char *) pintern
->Pointer
-HGLOBAL_STORAGE
,
628 size
+HGLOBAL_STORAGE
)) == NULL
)
629 hnew
= 0; /* Block still valid */
631 pintern
->Pointer
= (char *)palloc
+HGLOBAL_STORAGE
;
635 if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+HGLOBAL_STORAGE
))
640 *(HGLOBAL
*)palloc
= hmem
;
641 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
649 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-HGLOBAL_STORAGE
);
650 pintern
->Pointer
=NULL
;
655 RtlUnlockHeap(GetProcessHeap());
660 /***********************************************************************
661 * GlobalFree (KERNEL32.@)
666 HGLOBAL WINAPI
GlobalFree(
667 HGLOBAL hmem
/* [in] Handle of global memory object */
669 PGLOBAL32_INTERN pintern
;
672 RtlLockHeap(GetProcessHeap());
676 if(ISPOINTER(hmem
)) /* POINTER */
678 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
682 pintern
=HANDLE_TO_INTERN(hmem
);
684 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
687 /* WIN98 does not make this test. That is you can free a */
688 /* block you have not unlocked. Go figure!! */
689 /* if(pintern->LockCount!=0) */
690 /* SetLastError(ERROR_INVALID_HANDLE); */
693 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-HGLOBAL_STORAGE
))
695 if(!HeapFree(GetProcessHeap(), 0, pintern
))
702 ERR("page fault occurred ! Caused by bug ?\n");
703 SetLastError( ERROR_INVALID_PARAMETER
);
707 RtlUnlockHeap(GetProcessHeap());
712 /***********************************************************************
713 * GlobalSize (KERNEL32.@)
715 * Size in bytes of the global memory object
718 SIZE_T WINAPI
GlobalSize(
719 HGLOBAL hmem
/* [in] Handle of global memory object */
722 PGLOBAL32_INTERN pintern
;
728 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
732 RtlLockHeap(GetProcessHeap());
733 pintern
=HANDLE_TO_INTERN(hmem
);
735 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
737 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
741 retval
= HeapSize(GetProcessHeap(), 0,
742 (char *)(pintern
->Pointer
) - HGLOBAL_STORAGE
);
743 if (retval
!= (DWORD
)-1) retval
-= HGLOBAL_STORAGE
;
748 WARN("invalid handle\n");
751 RtlUnlockHeap(GetProcessHeap());
753 /* HeapSize returns 0xffffffff on failure */
754 if (retval
== 0xffffffff) retval
= 0;
759 /***********************************************************************
760 * GlobalWire (KERNEL32.@)
762 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
764 return GlobalLock( hmem
);
768 /***********************************************************************
769 * GlobalUnWire (KERNEL32.@)
771 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
773 return GlobalUnlock( hmem
);
777 /***********************************************************************
778 * GlobalFix (KERNEL32.@)
780 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
786 /***********************************************************************
787 * GlobalUnfix (KERNEL32.@)
789 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
795 /***********************************************************************
796 * GlobalFlags (KERNEL32.@)
797 * Returns information about the specified global memory object
800 * Should this return GMEM_INVALID_HANDLE on invalid handle?
803 * Value specifying allocation flags and lock count
804 * GMEM_INVALID_HANDLE: Failure
806 UINT WINAPI
GlobalFlags(
807 HGLOBAL hmem
/* [in] Handle to global memory object */
810 PGLOBAL32_INTERN pintern
;
818 RtlLockHeap(GetProcessHeap());
819 pintern
=HANDLE_TO_INTERN(hmem
);
820 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
822 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
823 if(pintern
->Pointer
==0)
824 retval
|= GMEM_DISCARDED
;
828 WARN("Invalid handle: %p\n", hmem
);
831 RtlUnlockHeap(GetProcessHeap());
837 /***********************************************************************
838 * GlobalCompact (KERNEL32.@)
840 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
842 return 0; /* GlobalCompact does nothing in Win32 */
846 /***********************************************************************
847 * LocalAlloc (KERNEL32.@)
852 HLOCAL WINAPI
LocalAlloc(
853 UINT flags
, /* [in] Allocation attributes */
854 SIZE_T size
/* [in] Number of bytes to allocate */
856 return (HLOCAL
)GlobalAlloc( flags
, size
);
860 /***********************************************************************
861 * LocalCompact (KERNEL32.@)
863 SIZE_T WINAPI
LocalCompact( UINT minfree
)
865 return 0; /* LocalCompact does nothing in Win32 */
869 /***********************************************************************
870 * LocalFlags (KERNEL32.@)
872 * Value specifying allocation flags and lock count.
873 * LMEM_INVALID_HANDLE: Failure
875 UINT WINAPI
LocalFlags(
876 HLOCAL handle
/* [in] Handle of memory object */
878 return GlobalFlags( (HGLOBAL
)handle
);
882 /***********************************************************************
883 * LocalFree (KERNEL32.@)
888 HLOCAL WINAPI
LocalFree(
889 HLOCAL handle
/* [in] Handle of memory object */
891 return (HLOCAL
)GlobalFree( (HGLOBAL
)handle
);
895 /***********************************************************************
896 * LocalHandle (KERNEL32.@)
901 HLOCAL WINAPI
LocalHandle(
902 LPCVOID ptr
/* [in] Address of local memory object */
904 return (HLOCAL
)GlobalHandle( ptr
);
908 /***********************************************************************
909 * LocalLock (KERNEL32.@)
910 * Locks a local memory object and returns pointer to the first byte
911 * of the memory block.
917 LPVOID WINAPI
LocalLock(
918 HLOCAL handle
/* [in] Address of local memory object */
920 return GlobalLock( (HGLOBAL
)handle
);
924 /***********************************************************************
925 * LocalReAlloc (KERNEL32.@)
930 HLOCAL WINAPI
LocalReAlloc(
931 HLOCAL handle
, /* [in] Handle of memory object */
932 SIZE_T size
, /* [in] New size of block */
933 UINT flags
/* [in] How to reallocate object */
935 return (HLOCAL
)GlobalReAlloc( (HGLOBAL
)handle
, size
, flags
);
939 /***********************************************************************
940 * LocalShrink (KERNEL32.@)
942 SIZE_T WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
944 return 0; /* LocalShrink does nothing in Win32 */
948 /***********************************************************************
949 * LocalSize (KERNEL32.@)
954 SIZE_T WINAPI
LocalSize(
955 HLOCAL handle
/* [in] Handle of memory object */
957 return GlobalSize( (HGLOBAL
)handle
);
961 /***********************************************************************
962 * LocalUnlock (KERNEL32.@)
964 * TRUE: Object is still locked
965 * FALSE: Object is unlocked
967 BOOL WINAPI
LocalUnlock(
968 HLOCAL handle
/* [in] Handle of memory object */
970 return GlobalUnlock( (HGLOBAL
)handle
);
974 /**********************************************************************
975 * AllocMappedBuffer (KERNEL32.38)
977 * This is a undocumented KERNEL32 function that
978 * SMapLS's a GlobalAlloc'ed buffer.
980 * Input: EDI register: size of buffer to allocate
981 * Output: EDI register: pointer to buffer
983 * Note: The buffer is preceded by 8 bytes:
986 * edi-4 SEGPTR to buffer
987 * edi-8 some magic Win95 needs for SUnMapLS
988 * (we use it for the memory handle)
990 * The SEGPTR is used by the caller!
992 void WINAPI
AllocMappedBuffer( CONTEXT86
*context
)
994 HGLOBAL handle
= GlobalAlloc(0, context
->Edi
+ 8);
995 DWORD
*buffer
= (DWORD
*)GlobalLock(handle
);
999 if (!(ptr
= MapLS(buffer
+ 2)))
1001 GlobalUnlock(handle
);
1006 context
->Eax
= context
->Edi
= 0;
1009 buffer
[0] = (DWORD
)handle
;
1012 context
->Eax
= (DWORD
) ptr
;
1013 context
->Edi
= (DWORD
)(buffer
+ 2);
1018 /**********************************************************************
1019 * FreeMappedBuffer (KERNEL32.39)
1021 * Free a buffer allocated by AllocMappedBuffer
1023 * Input: EDI register: pointer to buffer
1025 void WINAPI
FreeMappedBuffer( CONTEXT86
*context
)
1029 DWORD
*buffer
= (DWORD
*)context
->Edi
- 2;
1033 GlobalUnlock((HGLOBAL
)buffer
[0]);
1034 GlobalFree((HGLOBAL
)buffer
[0]);
1038 /***********************************************************************
1039 * GlobalMemoryStatus (KERNEL32.@)
1040 * Provides information about the status of the memory, so apps can tell
1041 * roughly how much they are able to allocate
1046 VOID WINAPI
GlobalMemoryStatus(
1047 LPMEMORYSTATUS lpmem
1049 static MEMORYSTATUS cached_memstatus
;
1050 static int cache_lastchecked
= 0;
1055 #if defined(__FreeBSD__) || defined(__NetBSD__)
1058 int mib
[2] = { CTL_HW
};
1060 if (time(NULL
)==cache_lastchecked
) {
1061 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1064 cache_lastchecked
= time(NULL
);
1066 lpmem
->dwMemoryLoad
= 0;
1067 lpmem
->dwTotalPhys
= 16*1024*1024;
1068 lpmem
->dwAvailPhys
= 16*1024*1024;
1069 lpmem
->dwTotalPageFile
= 16*1024*1024;
1070 lpmem
->dwAvailPageFile
= 16*1024*1024;
1073 f
= fopen( "/proc/meminfo", "r" );
1077 int total
, used
, free
, shared
, buffers
, cached
;
1079 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1080 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1081 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1082 while (fgets( buffer
, sizeof(buffer
), f
))
1084 /* old style /proc/meminfo ... */
1085 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1087 lpmem
->dwTotalPhys
+= total
;
1088 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1090 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1092 lpmem
->dwTotalPageFile
+= total
;
1093 lpmem
->dwAvailPageFile
+= free
;
1096 /* new style /proc/meminfo ... */
1097 if (sscanf(buffer
, "MemTotal: %d", &total
))
1098 lpmem
->dwTotalPhys
= total
*1024;
1099 if (sscanf(buffer
, "MemFree: %d", &free
))
1100 lpmem
->dwAvailPhys
= free
*1024;
1101 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1102 lpmem
->dwTotalPageFile
= total
*1024;
1103 if (sscanf(buffer
, "SwapFree: %d", &free
))
1104 lpmem
->dwAvailPageFile
= free
*1024;
1105 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1106 lpmem
->dwAvailPhys
+= buffers
*1024;
1107 if (sscanf(buffer
, "Cached: %d", &cached
))
1108 lpmem
->dwAvailPhys
+= cached
*1024;
1112 if (lpmem
->dwTotalPhys
)
1114 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1115 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1116 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1117 / (TotalPhysical
/ 100);
1120 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1121 mib
[1] = HW_PHYSMEM
;
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 lpmem
->dwTotalPhys
= *tmp
;
1129 mib
[1] = HW_USERMEM
;
1130 sysctl(mib
, 2, NULL
, &size_sys
, NULL
, 0);
1131 tmp
= malloc(size_sys
* sizeof(int));
1132 sysctl(mib
, 2, tmp
, &size_sys
, NULL
, 0);
1135 lpmem
->dwAvailPhys
= *tmp
;
1136 lpmem
->dwTotalPageFile
= *tmp
;
1137 lpmem
->dwAvailPageFile
= *tmp
;
1138 lpmem
->dwMemoryLoad
= lpmem
->dwTotalPhys
- lpmem
->dwAvailPhys
;
1141 lpmem
->dwAvailPhys
= lpmem
->dwTotalPhys
;
1142 lpmem
->dwTotalPageFile
= lpmem
->dwTotalPhys
;
1143 lpmem
->dwAvailPageFile
= lpmem
->dwTotalPhys
;
1144 lpmem
->dwMemoryLoad
= 0;
1150 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1151 * is more than 2GB of physical memory.
1153 if (lpmem
->dwTotalPhys
>2U*1024*1024*1024)
1155 lpmem
->dwTotalPhys
=2U*1024*1024*1024;
1156 lpmem
->dwAvailPhys
=2U*1024*1024*1024;
1159 /* FIXME: should do something for other systems */
1161 lpmem
->dwTotalVirtual
= (char*)si
.lpMaximumApplicationAddress
-(char*)si
.lpMinimumApplicationAddress
;
1162 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1163 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1164 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1166 /* it appears some memory display programs want to divide by these values */
1167 if(lpmem
->dwTotalPageFile
==0)
1168 lpmem
->dwTotalPageFile
++;
1170 if(lpmem
->dwAvailPageFile
==0)
1171 lpmem
->dwAvailPageFile
++;
1173 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1174 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1175 lpmem
->dwLength
, lpmem
->dwMemoryLoad
, lpmem
->dwTotalPhys
, lpmem
->dwAvailPhys
,
1176 lpmem
->dwTotalPageFile
, lpmem
->dwAvailPageFile
, lpmem
->dwTotalVirtual
,
1177 lpmem
->dwAvailVirtual
);
1180 /***********************************************************************
1181 * GlobalMemoryStatusEx (KERNEL32.@)
1182 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1187 BOOL WINAPI
GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer
)
1189 MEMORYSTATUS memstatus
;
1191 /* Because GlobalMemoryStatusEx is identical to GlobalMemoryStatus save
1192 for one extra field in the struct, and the lack of a bug, we simply
1193 call GlobalMemoryStatus and copy the values across. */
1194 FIXME("we should emulate the 4GB bug here, as per MSDN\n");
1195 GlobalMemoryStatus(&memstatus
);
1196 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
1197 lpBuffer
->ullTotalPhys
= memstatus
.dwTotalPhys
;
1198 lpBuffer
->ullAvailPhys
= memstatus
.dwAvailPhys
;
1199 lpBuffer
->ullTotalPageFile
= memstatus
.dwTotalPageFile
;
1200 lpBuffer
->ullAvailPageFile
= memstatus
.dwAvailPageFile
;
1201 lpBuffer
->ullTotalVirtual
= memstatus
.dwTotalVirtual
;
1202 lpBuffer
->ullAvailVirtual
= memstatus
.dwAvailVirtual
;
1203 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1204 memory in the extended portion of the virtual address space of the calling
1206 However, I don't know what this means, so set it to zero :(
1208 lpBuffer
->ullAvailExtendedVirtual
= 0;