msxml3: Some put_dataType() tests for nodes without typed data.
[wine/multimedia.git] / dlls / kernel32 / heap.c
blobdb21b7ad8cad452edf68b40e80ea80b0e8aa88e5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYS_SYSCTL_H
38 #include <sys/sysctl.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
44 #ifdef sun
45 /* FIXME: Unfortunately swapctl can't be used with largefile.... */
46 # undef _FILE_OFFSET_BITS
47 # define _FILE_OFFSET_BITS 32
48 # ifdef HAVE_SYS_RESOURCE_H
49 # include <sys/resource.h>
50 # endif
51 # ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
53 # endif
54 # include <sys/swap.h>
55 #endif
58 #include "windef.h"
59 #include "winbase.h"
60 #include "winerror.h"
61 #include "winnt.h"
62 #include "winternl.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(heap);
68 /* address where we try to map the system heap */
69 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
70 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
72 static HANDLE systemHeap; /* globally shared heap */
75 /***********************************************************************
76 * HEAP_CreateSystemHeap
78 * Create the system heap.
80 static inline HANDLE HEAP_CreateSystemHeap(void)
82 int created;
83 void *base;
84 HANDLE map, event;
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 );
97 return 0;
100 if (created) /* newly created heap */
102 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
103 SYSTEM_HEAP_SIZE, NULL, NULL );
104 SetEvent( event );
106 else
108 /* wait for the heap to be initialized */
109 WaitForSingleObject( event, INFINITE );
110 systemHeap = base;
112 CloseHandle( map );
113 return systemHeap;
117 /***********************************************************************
118 * HeapCreate (KERNEL32.@)
120 * Create a heap object.
122 * RETURNS
123 * Handle of heap: Success
124 * NULL: Failure
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 */
131 HANDLE ret;
133 if ( flags & HEAP_SHARED )
135 if (!systemHeap) HEAP_CreateSystemHeap();
136 else WARN( "Shared Heap requested, returning system heap.\n" );
137 ret = systemHeap;
139 else
141 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
142 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
144 return ret;
148 /***********************************************************************
149 * HeapDestroy (KERNEL32.@)
151 * Destroy a heap object.
153 * RETURNS
154 * TRUE: Success
155 * FALSE: Failure
157 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
159 if (heap == systemHeap)
161 WARN( "attempt to destroy system heap, returning TRUE!\n" );
162 return TRUE;
164 if (!RtlDestroyHeap( heap )) return TRUE;
165 SetLastError( ERROR_INVALID_HANDLE );
166 return FALSE;
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.
183 * NOTES
184 * Flags is ignored.
186 * RETURNS
187 * TRUE: Success
188 * FALSE: Failure
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.
203 * TODO
204 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
205 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
207 * RETURNS
208 * TRUE: Success
209 * FALSE: Failure
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) );
217 return !ret;
221 /***********************************************************************
222 * HeapLock (KERNEL32.@)
223 * Attempts to acquire the critical section object for a specified heap.
225 * RETURNS
226 * TRUE: Success
227 * FALSE: Failure
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.
240 * RETURNS
241 * TRUE: Success
242 * FALSE: Failure
244 BOOL WINAPI HeapUnlock(
245 HANDLE heap /* [in] Handle to the heap to unlock */
247 return RtlUnlockHeap( heap );
251 /***********************************************************************
252 * GetProcessHeaps (KERNEL32.@)
254 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
256 return RtlGetProcessHeaps( count, heaps );
260 /* These are needed so that we can call the functions from inside kernel itself */
262 /***********************************************************************
263 * HeapAlloc (KERNEL32.@)
265 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
267 return RtlAllocateHeap( heap, flags, size );
270 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
272 return RtlFreeHeap( heap, flags, ptr );
275 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
277 return RtlReAllocateHeap( heap, flags, ptr, size );
280 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPCVOID ptr )
282 return RtlSizeHeap( heap, flags, ptr );
285 BOOL WINAPI HeapQueryInformation( HANDLE heap, HEAP_INFORMATION_CLASS info_class,
286 PVOID info, SIZE_T size_in, PSIZE_T size_out)
288 NTSTATUS ret = RtlQueryHeapInformation( heap, info_class, info, size_in, size_out );
289 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
290 return !ret;
293 BOOL WINAPI HeapSetInformation( HANDLE heap, HEAP_INFORMATION_CLASS infoclass, PVOID info, SIZE_T size)
295 FIXME("%p %d %p %ld\n", heap, infoclass, info, size );
296 return TRUE;
300 * Win32 Global heap functions (GlobalXXX).
301 * These functions included in Win32 for compatibility with 16 bit Windows
302 * Especially the moveable blocks and handles are oldish.
303 * But the ability to directly allocate memory with GPTR and LPTR is widely
304 * used.
306 * The handle stuff looks horrible, but it's implemented almost like Win95
307 * does it.
311 #define MAGIC_GLOBAL_USED 0x5342
312 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
313 #define INTERN_TO_HANDLE(i) (&((i)->Pointer))
314 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
315 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
316 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
317 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
318 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
319 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
320 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
321 * the output jpeg's > 1 MB if not */
322 #define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
324 #include "pshpack1.h"
326 typedef struct __GLOBAL32_INTERN
328 WORD Magic;
329 LPVOID Pointer;
330 BYTE Flags;
331 BYTE LockCount;
332 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
334 #include "poppack.h"
336 /***********************************************************************
337 * GlobalAlloc (KERNEL32.@)
339 * Allocate a global memory object.
341 * RETURNS
342 * Handle: Success
343 * NULL: Failure
345 HGLOBAL WINAPI GlobalAlloc(
346 UINT flags, /* [in] Object allocation attributes */
347 SIZE_T size /* [in] Number of bytes to allocate */
349 PGLOBAL32_INTERN pintern;
350 DWORD hpflags;
351 LPVOID palloc;
353 if(flags&GMEM_ZEROINIT)
354 hpflags=HEAP_ZERO_MEMORY;
355 else
356 hpflags=0;
358 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
360 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
361 TRACE( "(flags=%04x) returning %p\n", flags, palloc );
362 return palloc;
364 else /* HANDLE */
366 if (size > INT_MAX-HGLOBAL_STORAGE)
368 SetLastError(ERROR_OUTOFMEMORY);
369 return 0;
372 RtlLockHeap(GetProcessHeap());
374 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
375 if (pintern)
377 /* Mask out obsolete flags */
378 flags &= ~(GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NOT_BANKED | GMEM_NOTIFY);
380 pintern->Magic = MAGIC_GLOBAL_USED;
381 pintern->Flags = flags >> 8;
382 pintern->LockCount = 0;
384 if (size)
386 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
387 if (!palloc)
389 HeapFree(GetProcessHeap(), 0, pintern);
390 pintern = NULL;
392 else
394 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
395 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
398 else
399 pintern->Pointer = NULL;
402 RtlUnlockHeap(GetProcessHeap());
403 if (!pintern) return 0;
404 TRACE( "(flags=%04x) returning handle %p pointer %p\n",
405 flags, INTERN_TO_HANDLE(pintern), pintern->Pointer );
406 return INTERN_TO_HANDLE(pintern);
411 /***********************************************************************
412 * GlobalLock (KERNEL32.@)
414 * Lock a global memory object and return a pointer to first byte of the memory
416 * PARAMS
417 * hmem [I] Handle of the global memory object
419 * RETURNS
420 * Success: Pointer to first byte of the memory block
421 * Failure: NULL
423 * NOTES
424 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
427 LPVOID WINAPI GlobalLock(HGLOBAL hmem)
429 PGLOBAL32_INTERN pintern;
430 LPVOID palloc;
432 if (ISPOINTER(hmem))
433 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
435 RtlLockHeap(GetProcessHeap());
436 __TRY
438 pintern = HANDLE_TO_INTERN(hmem);
439 if (pintern->Magic == MAGIC_GLOBAL_USED)
441 palloc = pintern->Pointer;
442 if (!pintern->Pointer)
443 SetLastError(ERROR_DISCARDED);
444 else if (pintern->LockCount < GMEM_LOCKCOUNT)
445 pintern->LockCount++;
447 else
449 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
450 palloc = NULL;
451 SetLastError(ERROR_INVALID_HANDLE);
454 __EXCEPT_PAGE_FAULT
456 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
457 palloc = NULL;
458 SetLastError(ERROR_INVALID_HANDLE);
460 __ENDTRY
461 RtlUnlockHeap(GetProcessHeap());
462 return palloc;
466 /***********************************************************************
467 * GlobalUnlock (KERNEL32.@)
469 * Unlock a global memory object.
471 * PARAMS
472 * hmem [I] Handle of the global memory object
474 * RETURNS
475 * Success: Object is still locked
476 * Failure: FALSE (The Object is unlocked)
478 * NOTES
479 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
482 BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
484 PGLOBAL32_INTERN pintern;
485 BOOL locked;
487 if (ISPOINTER(hmem)) return TRUE;
489 RtlLockHeap(GetProcessHeap());
490 __TRY
492 pintern=HANDLE_TO_INTERN(hmem);
493 if(pintern->Magic==MAGIC_GLOBAL_USED)
495 if(pintern->LockCount)
497 pintern->LockCount--;
498 locked = (pintern->LockCount != 0);
499 if (!locked) SetLastError(NO_ERROR);
501 else
503 WARN("%p not locked\n", hmem);
504 SetLastError(ERROR_NOT_LOCKED);
505 locked = FALSE;
508 else
510 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
511 SetLastError(ERROR_INVALID_HANDLE);
512 locked=FALSE;
515 __EXCEPT_PAGE_FAULT
517 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
518 SetLastError( ERROR_INVALID_PARAMETER );
519 locked=FALSE;
521 __ENDTRY
522 RtlUnlockHeap(GetProcessHeap());
523 return locked;
527 /***********************************************************************
528 * GlobalHandle (KERNEL32.@)
530 * Get the handle associated with the pointer to a global memory block.
532 * RETURNS
533 * Handle: Success
534 * NULL: Failure
536 HGLOBAL WINAPI GlobalHandle(
537 LPCVOID pmem /* [in] Pointer to global memory block */
539 HGLOBAL handle;
540 PGLOBAL32_INTERN maybe_intern;
541 LPCVOID test;
543 if (!pmem)
545 SetLastError( ERROR_INVALID_PARAMETER );
546 return 0;
549 RtlLockHeap(GetProcessHeap());
550 __TRY
552 handle = 0;
554 /* note that if pmem is a pointer to a block allocated by */
555 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
556 /* will fail. */
557 if (ISPOINTER(pmem)) {
558 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
559 handle = (HGLOBAL)pmem; /* valid fixed block */
560 break;
562 handle = POINTER_TO_HANDLE(pmem);
563 } else
564 handle = (HGLOBAL)pmem;
566 /* Now test handle either passed in or retrieved from pointer */
567 maybe_intern = HANDLE_TO_INTERN( handle );
568 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
569 test = maybe_intern->Pointer;
570 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
571 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
572 break; /* valid moveable block */
574 handle = 0;
575 SetLastError( ERROR_INVALID_HANDLE );
577 __EXCEPT_PAGE_FAULT
579 SetLastError( ERROR_INVALID_HANDLE );
580 handle = 0;
582 __ENDTRY
583 RtlUnlockHeap(GetProcessHeap());
585 return handle;
589 /***********************************************************************
590 * GlobalReAlloc (KERNEL32.@)
592 * Change the size or attributes of a global memory object.
594 * RETURNS
595 * Handle: Success
596 * NULL: Failure
598 HGLOBAL WINAPI GlobalReAlloc(
599 HGLOBAL hmem, /* [in] Handle of global memory object */
600 SIZE_T size, /* [in] New size of block */
601 UINT flags /* [in] How to reallocate object */
603 LPVOID palloc;
604 HGLOBAL hnew;
605 PGLOBAL32_INTERN pintern;
606 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
608 hnew = 0;
609 RtlLockHeap(GetProcessHeap());
610 if(flags & GMEM_MODIFY) /* modify flags */
612 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
614 /* make a fixed block moveable
615 * actually only NT is able to do this. But it's soo simple
617 if (hmem == 0)
619 WARN("GlobalReAlloc with null handle!\n");
620 SetLastError( ERROR_NOACCESS );
621 hnew = 0;
623 else
625 size = HeapSize(GetProcessHeap(), 0, hmem);
626 hnew = GlobalAlloc(flags, size);
627 palloc = GlobalLock(hnew);
628 memcpy(palloc, hmem, size);
629 GlobalUnlock(hnew);
630 GlobalFree(hmem);
633 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
635 /* change the flags to make our block "discardable" */
636 pintern=HANDLE_TO_INTERN(hmem);
637 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
638 hnew=hmem;
640 else
642 SetLastError(ERROR_INVALID_PARAMETER);
643 hnew = 0;
646 else
648 if(ISPOINTER(hmem))
650 /* reallocate fixed memory */
651 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
653 else
655 /* reallocate a moveable block */
656 pintern=HANDLE_TO_INTERN(hmem);
658 #if 0
659 /* Apparently Windows doesn't care whether the handle is locked at this point */
660 /* See also the same comment in GlobalFree() */
661 if(pintern->LockCount>1) {
662 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
663 SetLastError(ERROR_INVALID_HANDLE);
664 } else
665 #endif
666 if(size!=0)
668 hnew=hmem;
669 if(pintern->Pointer)
671 if(size > INT_MAX-HGLOBAL_STORAGE)
673 SetLastError(ERROR_OUTOFMEMORY);
674 hnew = 0;
676 else if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
677 (char *) pintern->Pointer-HGLOBAL_STORAGE,
678 size+HGLOBAL_STORAGE)) == NULL)
679 hnew = 0; /* Block still valid */
680 else
681 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
683 else
685 if(size > INT_MAX-HGLOBAL_STORAGE)
687 SetLastError(ERROR_OUTOFMEMORY);
688 hnew = 0;
690 else if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
691 == NULL)
692 hnew = 0;
693 else
695 *(HGLOBAL *)palloc = hmem;
696 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
700 else
702 if (pintern->LockCount == 0)
704 if(pintern->Pointer)
706 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
707 pintern->Pointer = NULL;
709 hnew = hmem;
711 else
712 WARN("not freeing memory associated with locked handle\n");
716 RtlUnlockHeap(GetProcessHeap());
717 return hnew;
721 /***********************************************************************
722 * GlobalFree (KERNEL32.@)
724 * Free a global memory object.
726 * PARAMS
727 * hmem [I] Handle of the global memory object
729 * RETURNS
730 * Success: NULL
731 * Failure: The provided handle
733 * NOTES
734 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
737 HGLOBAL WINAPI GlobalFree(HGLOBAL hmem)
739 PGLOBAL32_INTERN pintern;
740 HGLOBAL hreturned;
742 RtlLockHeap(GetProcessHeap());
743 __TRY
745 hreturned = 0;
746 if(ISPOINTER(hmem)) /* POINTER */
748 if(!HeapFree(GetProcessHeap(), 0, hmem))
750 SetLastError(ERROR_INVALID_HANDLE);
751 hreturned = hmem;
754 else /* HANDLE */
756 pintern=HANDLE_TO_INTERN(hmem);
758 if(pintern->Magic==MAGIC_GLOBAL_USED)
760 pintern->Magic = 0xdead;
762 /* WIN98 does not make this test. That is you can free a */
763 /* block you have not unlocked. Go figure!! */
764 /* if(pintern->LockCount!=0) */
765 /* SetLastError(ERROR_INVALID_HANDLE); */
767 if(pintern->Pointer)
768 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
769 hreturned=hmem;
770 if(!HeapFree(GetProcessHeap(), 0, pintern))
771 hreturned=hmem;
773 else
775 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
776 SetLastError(ERROR_INVALID_HANDLE);
777 hreturned = hmem;
781 __EXCEPT_PAGE_FAULT
783 ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
784 SetLastError( ERROR_INVALID_PARAMETER );
785 hreturned = hmem;
787 __ENDTRY
788 RtlUnlockHeap(GetProcessHeap());
789 return hreturned;
793 /***********************************************************************
794 * GlobalSize (KERNEL32.@)
796 * Get the size of a global memory object.
798 * PARAMS
799 * hmem [I] Handle of the global memory object
801 * RETURNS
802 * Failure: 0
803 * Success: Size in Bytes of the global memory object
805 * NOTES
806 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
809 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
811 SIZE_T retval;
812 PGLOBAL32_INTERN pintern;
814 if (!((ULONG_PTR)hmem >> 16))
816 SetLastError(ERROR_INVALID_HANDLE);
817 return 0;
820 if(ISPOINTER(hmem))
822 retval=HeapSize(GetProcessHeap(), 0, hmem);
824 if (retval == ~0ul) /* It might be a GMEM_MOVEABLE data pointer */
826 retval = HeapSize(GetProcessHeap(), 0, (char*)hmem - HGLOBAL_STORAGE);
827 if (retval != ~0ul) retval -= HGLOBAL_STORAGE;
830 else
832 RtlLockHeap(GetProcessHeap());
833 pintern=HANDLE_TO_INTERN(hmem);
835 if(pintern->Magic==MAGIC_GLOBAL_USED)
837 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
838 retval = 0;
839 else
841 retval = HeapSize(GetProcessHeap(), 0, (char *)pintern->Pointer - HGLOBAL_STORAGE );
842 if (retval != ~0ul) retval -= HGLOBAL_STORAGE;
845 else
847 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
848 SetLastError(ERROR_INVALID_HANDLE);
849 retval=0;
851 RtlUnlockHeap(GetProcessHeap());
853 if (retval == ~0ul) retval = 0;
854 return retval;
858 /***********************************************************************
859 * GlobalWire (KERNEL32.@)
861 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
863 return GlobalLock( hmem );
867 /***********************************************************************
868 * GlobalUnWire (KERNEL32.@)
870 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
872 return GlobalUnlock( hmem);
876 /***********************************************************************
877 * GlobalFix (KERNEL32.@)
879 VOID WINAPI GlobalFix(HGLOBAL hmem)
881 GlobalLock( hmem );
885 /***********************************************************************
886 * GlobalUnfix (KERNEL32.@)
888 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
890 GlobalUnlock( hmem);
894 /***********************************************************************
895 * GlobalFlags (KERNEL32.@)
897 * Get information about a global memory object.
899 * PARAMS
900 * hmem [I] Handle of the global memory object
902 * RETURNS
903 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
904 * Success: Value specifying allocation flags and lock count
907 UINT WINAPI GlobalFlags(HGLOBAL hmem)
909 DWORD retval;
910 PGLOBAL32_INTERN pintern;
912 if(ISPOINTER(hmem))
914 retval=0;
916 else
918 RtlLockHeap(GetProcessHeap());
919 pintern=HANDLE_TO_INTERN(hmem);
920 if(pintern->Magic==MAGIC_GLOBAL_USED)
922 retval=pintern->LockCount + (pintern->Flags<<8);
923 if(pintern->Pointer==0)
924 retval|= GMEM_DISCARDED;
926 else
928 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
929 SetLastError(ERROR_INVALID_HANDLE);
930 retval = GMEM_INVALID_HANDLE;
932 RtlUnlockHeap(GetProcessHeap());
934 return retval;
938 /***********************************************************************
939 * GlobalCompact (KERNEL32.@)
941 SIZE_T WINAPI GlobalCompact( DWORD minfree )
943 return 0; /* GlobalCompact does nothing in Win32 */
947 /***********************************************************************
948 * LocalAlloc (KERNEL32.@)
950 * Allocate a local memory object.
952 * RETURNS
953 * Handle: Success
954 * NULL: Failure
956 * NOTES
957 * Windows memory management does not provide a separate local heap
958 * and global heap.
960 HLOCAL WINAPI LocalAlloc(
961 UINT flags, /* [in] Allocation attributes */
962 SIZE_T size /* [in] Number of bytes to allocate */
964 return GlobalAlloc( flags, size );
968 /***********************************************************************
969 * LocalCompact (KERNEL32.@)
971 SIZE_T WINAPI LocalCompact( UINT minfree )
973 return 0; /* LocalCompact does nothing in Win32 */
977 /***********************************************************************
978 * LocalFlags (KERNEL32.@)
980 * Get information about a local memory object.
982 * RETURNS
983 * Value specifying allocation flags and lock count.
984 * LMEM_INVALID_HANDLE: Failure
986 * NOTES
987 * Windows memory management does not provide a separate local heap
988 * and global heap.
990 UINT WINAPI LocalFlags(
991 HLOCAL handle /* [in] Handle of memory object */
993 return GlobalFlags( handle );
997 /***********************************************************************
998 * LocalFree (KERNEL32.@)
1000 * Free a local memory object.
1002 * RETURNS
1003 * NULL: Success
1004 * Handle: Failure
1006 * NOTES
1007 * Windows memory management does not provide a separate local heap
1008 * and global heap.
1010 HLOCAL WINAPI LocalFree(
1011 HLOCAL handle /* [in] Handle of memory object */
1013 return GlobalFree( handle );
1017 /***********************************************************************
1018 * LocalHandle (KERNEL32.@)
1020 * Get the handle associated with the pointer to a local memory block.
1022 * RETURNS
1023 * Handle: Success
1024 * NULL: Failure
1026 * NOTES
1027 * Windows memory management does not provide a separate local heap
1028 * and global heap.
1030 HLOCAL WINAPI LocalHandle(
1031 LPCVOID ptr /* [in] Address of local memory block */
1033 return GlobalHandle( ptr );
1037 /***********************************************************************
1038 * LocalLock (KERNEL32.@)
1039 * Locks a local memory object and returns pointer to the first byte
1040 * of the memory block.
1042 * RETURNS
1043 * Pointer: Success
1044 * NULL: Failure
1046 * NOTES
1047 * Windows memory management does not provide a separate local heap
1048 * and global heap.
1050 LPVOID WINAPI LocalLock(
1051 HLOCAL handle /* [in] Address of local memory object */
1053 return GlobalLock( handle );
1057 /***********************************************************************
1058 * LocalReAlloc (KERNEL32.@)
1060 * Change the size or attributes of a local memory object.
1062 * RETURNS
1063 * Handle: Success
1064 * NULL: Failure
1066 * NOTES
1067 * Windows memory management does not provide a separate local heap
1068 * and global heap.
1070 HLOCAL WINAPI LocalReAlloc(
1071 HLOCAL handle, /* [in] Handle of memory object */
1072 SIZE_T size, /* [in] New size of block */
1073 UINT flags /* [in] How to reallocate object */
1075 return GlobalReAlloc( handle, size, flags );
1079 /***********************************************************************
1080 * LocalShrink (KERNEL32.@)
1082 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1084 return 0; /* LocalShrink does nothing in Win32 */
1088 /***********************************************************************
1089 * LocalSize (KERNEL32.@)
1091 * Get the size of a local memory object.
1093 * RETURNS
1094 * Size: Success
1095 * 0: Failure
1097 * NOTES
1098 * Windows memory management does not provide a separate local heap
1099 * and global heap.
1101 SIZE_T WINAPI LocalSize(
1102 HLOCAL handle /* [in] Handle of memory object */
1104 return GlobalSize( handle );
1108 /***********************************************************************
1109 * LocalUnlock (KERNEL32.@)
1111 * Unlock a local memory object.
1113 * RETURNS
1114 * TRUE: Object is still locked
1115 * FALSE: Object is unlocked
1117 * NOTES
1118 * Windows memory management does not provide a separate local heap
1119 * and global heap.
1121 BOOL WINAPI LocalUnlock(
1122 HLOCAL handle /* [in] Handle of memory object */
1125 if (ISPOINTER( handle ))
1127 SetLastError( ERROR_NOT_LOCKED );
1128 return FALSE;
1131 return GlobalUnlock( handle );
1135 /***********************************************************************
1136 * GlobalMemoryStatusEx (KERNEL32.@)
1137 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1139 * RETURNS
1140 * TRUE
1142 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1144 static MEMORYSTATUSEX cached_memstatus;
1145 static int cache_lastchecked = 0;
1146 SYSTEM_INFO si;
1147 #ifdef linux
1148 FILE *f;
1149 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
1150 unsigned long val;
1151 int mib[2];
1152 size_t size_sys;
1153 #elif defined(__APPLE__)
1154 unsigned int val;
1155 int mib[2];
1156 size_t size_sys;
1157 #elif defined(sun)
1158 unsigned long pagesize,maxpages,freepages,swapspace,swapfree;
1159 struct anoninfo swapinf;
1160 int rval;
1161 #endif
1163 if (lpmemex->dwLength != sizeof(*lpmemex))
1165 SetLastError( ERROR_INVALID_PARAMETER );
1166 return FALSE;
1169 if (time(NULL)==cache_lastchecked) {
1170 *lpmemex = cached_memstatus;
1171 return TRUE;
1173 cache_lastchecked = time(NULL);
1175 lpmemex->dwMemoryLoad = 0;
1176 lpmemex->ullTotalPhys = 16*1024*1024;
1177 lpmemex->ullAvailPhys = 16*1024*1024;
1178 lpmemex->ullTotalPageFile = 16*1024*1024;
1179 lpmemex->ullAvailPageFile = 16*1024*1024;
1181 #ifdef linux
1182 f = fopen( "/proc/meminfo", "r" );
1183 if (f)
1185 char buffer[256];
1186 unsigned long total, used, free, shared, buffers, cached;
1188 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1189 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1190 while (fgets( buffer, sizeof(buffer), f ))
1192 /* old style /proc/meminfo ... */
1193 if (sscanf( buffer, "Mem: %lu %lu %lu %lu %lu %lu",
1194 &total, &used, &free, &shared, &buffers, &cached ))
1196 lpmemex->ullTotalPhys += total;
1197 lpmemex->ullAvailPhys += free + buffers + cached;
1199 if (sscanf( buffer, "Swap: %lu %lu %lu", &total, &used, &free ))
1201 lpmemex->ullTotalPageFile += total;
1202 lpmemex->ullAvailPageFile += free;
1205 /* new style /proc/meminfo ... */
1206 if (sscanf(buffer, "MemTotal: %lu", &total))
1207 lpmemex->ullTotalPhys = (ULONG64)total*1024;
1208 if (sscanf(buffer, "MemFree: %lu", &free))
1209 lpmemex->ullAvailPhys = (ULONG64)free*1024;
1210 if (sscanf(buffer, "SwapTotal: %lu", &total))
1211 lpmemex->ullTotalPageFile = (ULONG64)total*1024;
1212 if (sscanf(buffer, "SwapFree: %lu", &free))
1213 lpmemex->ullAvailPageFile = (ULONG64)free*1024;
1214 if (sscanf(buffer, "Buffers: %lu", &buffers))
1215 lpmemex->ullAvailPhys += (ULONG64)buffers*1024;
1216 if (sscanf(buffer, "Cached: %lu", &cached))
1217 lpmemex->ullAvailPhys += (ULONG64)cached*1024;
1219 fclose( f );
1221 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
1222 mib[0] = CTL_HW;
1223 mib[1] = HW_PHYSMEM;
1224 size_sys = sizeof(val);
1225 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1226 if (val) lpmemex->ullTotalPhys = val;
1227 mib[1] = HW_USERMEM;
1228 size_sys = sizeof(val);
1229 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1230 if (!val) val = lpmemex->ullTotalPhys;
1231 lpmemex->ullAvailPhys = val;
1232 lpmemex->ullTotalPageFile = val;
1233 lpmemex->ullAvailPageFile = val;
1234 #elif defined ( sun )
1235 pagesize=sysconf(_SC_PAGESIZE);
1236 maxpages=sysconf(_SC_PHYS_PAGES);
1237 freepages=sysconf(_SC_AVPHYS_PAGES);
1238 rval=swapctl(SC_AINFO, &swapinf);
1239 if(rval >-1)
1241 swapspace=swapinf.ani_max*pagesize;
1242 swapfree=swapinf.ani_free*pagesize;
1243 }else
1246 WARN("Swap size cannot be determined , assuming equal to physical memory\n");
1247 swapspace=maxpages*pagesize;
1248 swapfree=maxpages*pagesize;
1250 lpmemex->ullTotalPhys=pagesize*maxpages;
1251 lpmemex->ullAvailPhys = pagesize*freepages;
1252 lpmemex->ullTotalPageFile = swapspace;
1253 lpmemex->ullAvailPageFile = swapfree;
1254 #endif
1256 if (lpmemex->ullTotalPhys)
1258 lpmemex->dwMemoryLoad = (lpmemex->ullTotalPhys-lpmemex->ullAvailPhys)
1259 / (lpmemex->ullTotalPhys / 100);
1262 /* Win98 returns only the swapsize in ullTotalPageFile/ullAvailPageFile,
1263 WinXP returns the size of physical memory + swapsize;
1264 mimic the behavior of XP.
1265 Note: Project2k refuses to start if it sees less than 1Mb of free swap.
1267 lpmemex->ullTotalPageFile += lpmemex->ullTotalPhys;
1268 lpmemex->ullAvailPageFile += lpmemex->ullAvailPhys;
1270 /* Titan Quest refuses to run if TotalPageFile <= ullTotalPhys */
1271 if(lpmemex->ullTotalPageFile == lpmemex->ullTotalPhys)
1273 lpmemex->ullTotalPhys -= 1;
1274 lpmemex->ullAvailPhys -= 1;
1277 /* FIXME: should do something for other systems */
1278 GetSystemInfo(&si);
1279 lpmemex->ullTotalVirtual = (ULONG_PTR)si.lpMaximumApplicationAddress-(ULONG_PTR)si.lpMinimumApplicationAddress;
1280 /* FIXME: we should track down all the already allocated VM pages and subtract them, for now arbitrarily remove 64KB so that it matches NT */
1281 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1283 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1284 memory in the extended portion of the virtual address space of the calling
1285 process, in bytes.
1286 However, I don't know what this means, so set it to zero :(
1288 lpmemex->ullAvailExtendedVirtual = 0;
1290 cached_memstatus = *lpmemex;
1292 TRACE("<-- LPMEMORYSTATUSEX: dwLength %d, dwMemoryLoad %d, ullTotalPhys %s, ullAvailPhys %s,"
1293 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1294 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1295 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1296 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1297 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1299 return TRUE;
1302 /***********************************************************************
1303 * GlobalMemoryStatus (KERNEL32.@)
1304 * Provides information about the status of the memory, so apps can tell
1305 * roughly how much they are able to allocate
1307 * RETURNS
1308 * None
1310 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1312 MEMORYSTATUSEX memstatus;
1313 OSVERSIONINFOW osver;
1314 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
1316 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1317 for one extra field in the struct, and the lack of a bug, we simply
1318 call GlobalMemoryStatusEx and copy the values across. */
1319 memstatus.dwLength = sizeof(memstatus);
1320 GlobalMemoryStatusEx(&memstatus);
1322 lpBuffer->dwLength = sizeof(*lpBuffer);
1323 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1325 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1326 * NT reports values modulo 4 Gb.
1329 osver.dwOSVersionInfoSize = sizeof(osver);
1330 GetVersionExW(&osver);
1332 if ( osver.dwMajorVersion >= 5 || osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
1334 lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
1335 lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
1336 lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, MAXDWORD );
1337 lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
1338 lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
1339 lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
1342 else /* duplicate NT bug */
1344 lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
1345 lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
1346 lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
1347 lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
1348 lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
1349 lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
1352 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
1353 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
1354 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
1356 if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
1357 if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
1358 if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
1359 if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
1362 /* work around for broken photoshop 4 installer */
1363 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1364 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
1366 /* limit page file size for really old binaries */
1367 if (nt->OptionalHeader.MajorSubsystemVersion < 4)
1369 if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
1370 if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
1373 TRACE("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
1374 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
1375 lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
1376 lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
1377 lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );