#ifdef guard the declaration of type IID to be compatible with
[wine.git] / dlls / kernel / heap.c
blobf16abde7682bd8fd16869a9f2383d7f7cf0b4315
1 /*
2 * Win32 heap functions
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <time.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
35 #endif
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winerror.h"
46 #include "winnt.h"
47 #include "winreg.h"
48 #include "winternl.h"
49 #include "excpt.h"
50 #include "thread.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;
73 default:
74 return EXCEPTION_CONTINUE_SEARCH;
78 /***********************************************************************
79 * HEAP_CreateSystemHeap
81 * Create the system heap.
83 inline static HANDLE HEAP_CreateSystemHeap(void)
85 int created;
86 void *base;
87 HANDLE map, event;
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 );
99 return 0;
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 );
118 else
120 /* wait for the heap to be initialized */
121 WaitForSingleObject( event, INFINITE );
122 systemHeap = (HANDLE)base;
124 CloseHandle( map );
125 return systemHeap;
129 /***********************************************************************
130 * HeapCreate (KERNEL32.@)
131 * RETURNS
132 * Handle of heap: Success
133 * NULL: Failure
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 */
140 HANDLE ret;
142 if ( flags & HEAP_SHARED )
144 if (!systemHeap) HEAP_CreateSystemHeap();
145 else WARN( "Shared Heap requested, returning system heap.\n" );
146 ret = systemHeap;
148 else
150 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
151 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
153 return ret;
157 /***********************************************************************
158 * HeapDestroy (KERNEL32.@)
159 * RETURNS
160 * TRUE: Success
161 * FALSE: Failure
163 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
165 if (heap == systemHeap)
167 WARN( "attempt to destroy system heap, returning TRUE!\n" );
168 return TRUE;
170 if (!RtlDestroyHeap( heap )) return TRUE;
171 SetLastError( ERROR_INVALID_HANDLE );
172 return FALSE;
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.
189 * NOTES
190 * Flags is ignored.
192 * RETURNS
193 * TRUE: Success
194 * FALSE: Failure
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.
209 * TODO
210 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
211 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
213 * RETURNS
214 * TRUE: Success
215 * FALSE: Failure
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) );
223 return !ret;
227 /***********************************************************************
228 * HeapLock (KERNEL32.@)
229 * Attempts to acquire the critical section object for a specified heap.
231 * RETURNS
232 * TRUE: Success
233 * FALSE: Failure
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.
246 * RETURNS
247 * TRUE: Success
248 * FALSE: Failure
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
302 * used.
304 * The handle stuff looks horrible, but it's implemented almost like Win95
305 * does it.
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
327 WORD Magic;
328 LPVOID Pointer;
329 BYTE Flags;
330 BYTE LockCount;
331 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
333 #include "poppack.h"
335 /***********************************************************************
336 * GlobalAlloc (KERNEL32.@)
337 * RETURNS
338 * Handle: Success
339 * NULL: Failure
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;
346 DWORD hpflags;
347 LPVOID palloc;
349 if(flags&GMEM_ZEROINIT)
350 hpflags=HEAP_ZERO_MEMORY;
351 else
352 hpflags=0;
354 TRACE("() flags=%04x\n", flags );
356 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
358 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
359 return (HGLOBAL) palloc;
361 else /* HANDLE */
363 RtlLockHeap(GetProcessHeap());
365 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
366 if (pintern)
368 pintern->Magic = MAGIC_GLOBAL_USED;
369 pintern->Flags = flags >> 8;
370 pintern->LockCount = 0;
372 if (size)
374 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
375 if (!palloc)
377 HeapFree(GetProcessHeap(), 0, pintern);
378 pintern = NULL;
380 else
382 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
383 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
386 else
387 pintern->Pointer = NULL;
390 RtlUnlockHeap(GetProcessHeap());
391 return pintern ? INTERN_TO_HANDLE(pintern) : 0;
396 /***********************************************************************
397 * GlobalLock (KERNEL32.@)
398 * RETURNS
399 * Pointer to first byte of block
400 * NULL: Failure
402 LPVOID WINAPI GlobalLock(
403 HGLOBAL hmem /* [in] Handle of global memory object */
406 PGLOBAL32_INTERN pintern;
407 LPVOID palloc;
409 if (ISPOINTER(hmem))
410 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
412 RtlLockHeap(GetProcessHeap());
413 __TRY
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;
422 else
424 WARN("invalid handle %p\n", hmem);
425 palloc = NULL;
426 SetLastError(ERROR_INVALID_HANDLE);
429 __EXCEPT(page_fault)
431 WARN("page fault on %p\n", hmem);
432 palloc = NULL;
433 SetLastError(ERROR_INVALID_HANDLE);
435 __ENDTRY
436 RtlUnlockHeap(GetProcessHeap());
437 return palloc;
441 /***********************************************************************
442 * GlobalUnlock (KERNEL32.@)
443 * RETURNS
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;
451 BOOL locked;
453 if (ISPOINTER(hmem)) return FALSE;
455 RtlLockHeap(GetProcessHeap());
456 __TRY
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);
467 else
469 WARN("invalid handle\n");
470 SetLastError(ERROR_INVALID_HANDLE);
471 locked=FALSE;
474 __EXCEPT(page_fault)
476 ERR("page fault occurred ! Caused by bug ?\n");
477 SetLastError( ERROR_INVALID_PARAMETER );
478 locked=FALSE;
480 __ENDTRY
481 RtlUnlockHeap(GetProcessHeap());
482 return locked;
486 /***********************************************************************
487 * GlobalHandle (KERNEL32.@)
488 * Returns the handle associated with the specified pointer.
490 * RETURNS
491 * Handle: Success
492 * NULL: Failure
494 HGLOBAL WINAPI GlobalHandle(
495 LPCVOID pmem /* [in] Pointer to global memory block */
497 HGLOBAL handle;
498 PGLOBAL32_INTERN maybe_intern;
499 LPCVOID test;
501 if (!pmem)
503 SetLastError( ERROR_INVALID_PARAMETER );
504 return 0;
507 RtlLockHeap(GetProcessHeap());
508 __TRY
510 handle = 0;
512 /* note that if pmem is a pointer to a a block allocated by */
513 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
514 /* will fail. */
515 if (ISPOINTER(pmem)) {
516 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
517 handle = (HGLOBAL)pmem; /* valid fixed block */
518 break;
520 handle = POINTER_TO_HANDLE(pmem);
521 } else
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 */
532 handle = 0;
533 SetLastError( ERROR_INVALID_HANDLE );
535 __EXCEPT(page_fault)
537 SetLastError( ERROR_INVALID_HANDLE );
538 handle = 0;
540 __ENDTRY
541 RtlUnlockHeap(GetProcessHeap());
543 return handle;
547 /***********************************************************************
548 * GlobalReAlloc (KERNEL32.@)
549 * RETURNS
550 * Handle: Success
551 * NULL: Failure
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 */
558 LPVOID palloc;
559 HGLOBAL hnew;
560 PGLOBAL32_INTERN pintern;
561 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
563 hnew = 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
572 if (hmem == 0)
574 WARN("GlobalReAlloc with null handle!\n");
575 SetLastError( ERROR_NOACCESS );
576 hnew = 0;
578 else
580 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
581 hnew = GlobalAlloc(flags, size);
582 palloc = GlobalLock(hnew);
583 memcpy(palloc, (LPVOID)hmem, size);
584 GlobalUnlock(hnew);
585 GlobalFree(hmem);
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);
593 hnew=hmem;
595 else
597 SetLastError(ERROR_INVALID_PARAMETER);
598 hnew = 0;
601 else
603 if(ISPOINTER(hmem))
605 /* reallocate fixed memory */
606 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
608 else
610 /* reallocate a moveable block */
611 pintern=HANDLE_TO_INTERN(hmem);
613 #if 0
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);
619 } else
620 #endif
621 if(size!=0)
623 hnew=hmem;
624 if(pintern->Pointer)
626 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
627 (char *) pintern->Pointer-HGLOBAL_STORAGE,
628 size+HGLOBAL_STORAGE)) == NULL)
629 hnew = 0; /* Block still valid */
630 else
631 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
633 else
635 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
636 == NULL)
637 hnew = 0;
638 else
640 *(HGLOBAL *)palloc = hmem;
641 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
645 else
647 if(pintern->Pointer)
649 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
650 pintern->Pointer=NULL;
655 RtlUnlockHeap(GetProcessHeap());
656 return hnew;
660 /***********************************************************************
661 * GlobalFree (KERNEL32.@)
662 * RETURNS
663 * NULL: Success
664 * Handle: Failure
666 HGLOBAL WINAPI GlobalFree(
667 HGLOBAL hmem /* [in] Handle of global memory object */
669 PGLOBAL32_INTERN pintern;
670 HGLOBAL hreturned;
672 RtlLockHeap(GetProcessHeap());
673 __TRY
675 hreturned = 0;
676 if(ISPOINTER(hmem)) /* POINTER */
678 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
680 else /* HANDLE */
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); */
692 if(pintern->Pointer)
693 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
694 hreturned=hmem;
695 if(!HeapFree(GetProcessHeap(), 0, pintern))
696 hreturned=hmem;
700 __EXCEPT(page_fault)
702 ERR("page fault occurred ! Caused by bug ?\n");
703 SetLastError( ERROR_INVALID_PARAMETER );
704 hreturned = hmem;
706 __ENDTRY
707 RtlUnlockHeap(GetProcessHeap());
708 return hreturned;
712 /***********************************************************************
713 * GlobalSize (KERNEL32.@)
714 * RETURNS
715 * Size in bytes of the global memory object
716 * 0: Failure
718 SIZE_T WINAPI GlobalSize(
719 HGLOBAL hmem /* [in] Handle of global memory object */
721 DWORD retval;
722 PGLOBAL32_INTERN pintern;
724 if (!hmem) return 0;
726 if(ISPOINTER(hmem))
728 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
730 else
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) */
738 retval = 0;
739 else
741 retval = HeapSize(GetProcessHeap(), 0,
742 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
743 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
746 else
748 WARN("invalid handle\n");
749 retval=0;
751 RtlUnlockHeap(GetProcessHeap());
753 /* HeapSize returns 0xffffffff on failure */
754 if (retval == 0xffffffff) retval = 0;
755 return retval;
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)
782 GlobalLock( hmem );
786 /***********************************************************************
787 * GlobalUnfix (KERNEL32.@)
789 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
791 GlobalUnlock( hmem);
795 /***********************************************************************
796 * GlobalFlags (KERNEL32.@)
797 * Returns information about the specified global memory object
799 * NOTES
800 * Should this return GMEM_INVALID_HANDLE on invalid handle?
802 * RETURNS
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 */
809 DWORD retval;
810 PGLOBAL32_INTERN pintern;
812 if(ISPOINTER(hmem))
814 retval=0;
816 else
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;
826 else
828 WARN("Invalid handle: %p\n", hmem);
829 retval=0;
831 RtlUnlockHeap(GetProcessHeap());
833 return retval;
837 /***********************************************************************
838 * GlobalCompact (KERNEL32.@)
840 SIZE_T WINAPI GlobalCompact( DWORD minfree )
842 return 0; /* GlobalCompact does nothing in Win32 */
846 /***********************************************************************
847 * LocalAlloc (KERNEL32.@)
848 * RETURNS
849 * Handle: Success
850 * NULL: Failure
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.@)
871 * RETURNS
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.@)
884 * RETURNS
885 * NULL: Success
886 * Handle: Failure
888 HLOCAL WINAPI LocalFree(
889 HLOCAL handle /* [in] Handle of memory object */
891 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
895 /***********************************************************************
896 * LocalHandle (KERNEL32.@)
897 * RETURNS
898 * Handle: Success
899 * NULL: Failure
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.
913 * RETURNS
914 * Pointer: Success
915 * NULL: Failure
917 LPVOID WINAPI LocalLock(
918 HLOCAL handle /* [in] Address of local memory object */
920 return GlobalLock( (HGLOBAL)handle );
924 /***********************************************************************
925 * LocalReAlloc (KERNEL32.@)
926 * RETURNS
927 * Handle: Success
928 * NULL: Failure
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.@)
950 * RETURNS
951 * Size: Success
952 * 0: Failure
954 SIZE_T WINAPI LocalSize(
955 HLOCAL handle /* [in] Handle of memory object */
957 return GlobalSize( (HGLOBAL)handle );
961 /***********************************************************************
962 * LocalUnlock (KERNEL32.@)
963 * RETURNS
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:
984 * ...
985 * edi+0 buffer
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);
996 DWORD ptr = 0;
998 if (buffer)
999 if (!(ptr = MapLS(buffer + 2)))
1001 GlobalUnlock(handle);
1002 GlobalFree(handle);
1005 if (!ptr)
1006 context->Eax = context->Edi = 0;
1007 else
1009 buffer[0] = (DWORD)handle;
1010 buffer[1] = ptr;
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 )
1027 if (context->Edi)
1029 DWORD *buffer = (DWORD *)context->Edi - 2;
1031 UnMapLS(buffer[1]);
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
1043 * RETURNS
1044 * None
1046 VOID WINAPI GlobalMemoryStatus(
1047 LPMEMORYSTATUS lpmem
1049 static MEMORYSTATUS cached_memstatus;
1050 static int cache_lastchecked = 0;
1051 SYSTEM_INFO si;
1052 #ifdef linux
1053 FILE *f;
1054 #endif
1055 #if defined(__FreeBSD__) || defined(__NetBSD__)
1056 int *tmp;
1057 int size_sys;
1058 int mib[2] = { CTL_HW };
1059 #endif
1060 if (time(NULL)==cache_lastchecked) {
1061 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1062 return;
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;
1072 #ifdef linux
1073 f = fopen( "/proc/meminfo", "r" );
1074 if (f)
1076 char buffer[256];
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;
1110 fclose( f );
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);
1125 if (tmp && *tmp)
1127 lpmem->dwTotalPhys = *tmp;
1128 free(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);
1133 if (tmp && *tmp)
1135 lpmem->dwAvailPhys = *tmp;
1136 lpmem->dwTotalPageFile = *tmp;
1137 lpmem->dwAvailPageFile = *tmp;
1138 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1139 } else
1141 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1142 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1143 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1144 lpmem->dwMemoryLoad = 0;
1146 free(tmp);
1149 #endif
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 */
1160 GetSystemInfo(&si);
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
1184 * RETURNS
1185 * None
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
1205 process, in bytes.
1206 However, I don't know what this means, so set it to zero :(
1208 lpBuffer->ullAvailExtendedVirtual = 0;
1209 return 1;