wiaservc: Add the class factory and IWiaDevMgr stubs.
[wine/hramrach.git] / dlls / kernel32 / heap.c
blobead0778683ae1750dae9c527f7330382d9333ca4
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 * GetProcessHeap (KERNEL32.@)
254 HANDLE WINAPI GetProcessHeap(void)
256 return NtCurrentTeb()->Peb->ProcessHeap;
260 /***********************************************************************
261 * GetProcessHeaps (KERNEL32.@)
263 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
265 return RtlGetProcessHeaps( count, heaps );
269 /* These are needed so that we can call the functions from inside kernel itself */
271 /***********************************************************************
272 * HeapAlloc (KERNEL32.@)
274 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
276 return RtlAllocateHeap( heap, flags, size );
279 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
281 return RtlFreeHeap( heap, flags, ptr );
284 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
286 return RtlReAllocateHeap( heap, flags, ptr, size );
289 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPCVOID ptr )
291 return RtlSizeHeap( heap, flags, ptr );
294 BOOL WINAPI HeapQueryInformation( HANDLE heap, HEAP_INFORMATION_CLASS info_class,
295 PVOID info, SIZE_T size_in, PSIZE_T size_out)
297 NTSTATUS ret = RtlQueryHeapInformation( heap, info_class, info, size_in, size_out );
298 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
299 return !ret;
302 BOOL WINAPI HeapSetInformation( HANDLE heap, HEAP_INFORMATION_CLASS infoclass, PVOID info, SIZE_T size)
304 FIXME("%p %d %p %ld\n", heap, infoclass, info, size );
305 return TRUE;
309 * Win32 Global heap functions (GlobalXXX).
310 * These functions included in Win32 for compatibility with 16 bit Windows
311 * Especially the moveable blocks and handles are oldish.
312 * But the ability to directly allocate memory with GPTR and LPTR is widely
313 * used.
315 * The handle stuff looks horrible, but it's implemented almost like Win95
316 * does it.
320 #define MAGIC_GLOBAL_USED 0x5342
321 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
322 #define INTERN_TO_HANDLE(i) (&((i)->Pointer))
323 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
324 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
325 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
326 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
327 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
328 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
329 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
330 * the output jpeg's > 1 MB if not */
331 #define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
333 #include "pshpack1.h"
335 typedef struct __GLOBAL32_INTERN
337 WORD Magic;
338 LPVOID Pointer;
339 BYTE Flags;
340 BYTE LockCount;
341 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
343 #include "poppack.h"
345 /***********************************************************************
346 * GlobalAlloc (KERNEL32.@)
348 * Allocate a global memory object.
350 * RETURNS
351 * Handle: Success
352 * NULL: Failure
354 HGLOBAL WINAPI GlobalAlloc(
355 UINT flags, /* [in] Object allocation attributes */
356 SIZE_T size /* [in] Number of bytes to allocate */
358 PGLOBAL32_INTERN pintern;
359 DWORD hpflags;
360 LPVOID palloc;
362 if(flags&GMEM_ZEROINIT)
363 hpflags=HEAP_ZERO_MEMORY;
364 else
365 hpflags=0;
367 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
369 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
370 TRACE( "(flags=%04x) returning %p\n", flags, palloc );
371 return palloc;
373 else /* HANDLE */
375 if (size > INT_MAX-HGLOBAL_STORAGE)
377 SetLastError(ERROR_OUTOFMEMORY);
378 return 0;
381 RtlLockHeap(GetProcessHeap());
383 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
384 if (pintern)
386 /* Mask out obsolete flags */
387 flags &= ~(GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NOT_BANKED | GMEM_NOTIFY);
389 pintern->Magic = MAGIC_GLOBAL_USED;
390 pintern->Flags = flags >> 8;
391 pintern->LockCount = 0;
393 if (size)
395 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
396 if (!palloc)
398 HeapFree(GetProcessHeap(), 0, pintern);
399 pintern = NULL;
401 else
403 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
404 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
407 else
408 pintern->Pointer = NULL;
411 RtlUnlockHeap(GetProcessHeap());
412 if (!pintern) return 0;
413 TRACE( "(flags=%04x) returning handle %p pointer %p\n",
414 flags, INTERN_TO_HANDLE(pintern), pintern->Pointer );
415 return INTERN_TO_HANDLE(pintern);
420 /***********************************************************************
421 * GlobalLock (KERNEL32.@)
423 * Lock a global memory object and return a pointer to first byte of the memory
425 * PARAMS
426 * hmem [I] Handle of the global memory object
428 * RETURNS
429 * Success: Pointer to first byte of the memory block
430 * Failure: NULL
432 * NOTES
433 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
436 LPVOID WINAPI GlobalLock(HGLOBAL hmem)
438 PGLOBAL32_INTERN pintern;
439 LPVOID palloc;
441 if (ISPOINTER(hmem))
442 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
444 RtlLockHeap(GetProcessHeap());
445 __TRY
447 pintern = HANDLE_TO_INTERN(hmem);
448 if (pintern->Magic == MAGIC_GLOBAL_USED)
450 palloc = pintern->Pointer;
451 if (!pintern->Pointer)
452 SetLastError(ERROR_DISCARDED);
453 else if (pintern->LockCount < GMEM_LOCKCOUNT)
454 pintern->LockCount++;
456 else
458 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
459 palloc = NULL;
460 SetLastError(ERROR_INVALID_HANDLE);
463 __EXCEPT_PAGE_FAULT
465 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
466 palloc = NULL;
467 SetLastError(ERROR_INVALID_HANDLE);
469 __ENDTRY
470 RtlUnlockHeap(GetProcessHeap());
471 return palloc;
475 /***********************************************************************
476 * GlobalUnlock (KERNEL32.@)
478 * Unlock a global memory object.
480 * PARAMS
481 * hmem [I] Handle of the global memory object
483 * RETURNS
484 * Success: Object is still locked
485 * Failure: FALSE (The Object is unlocked)
487 * NOTES
488 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
491 BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
493 PGLOBAL32_INTERN pintern;
494 BOOL locked;
496 if (ISPOINTER(hmem)) return TRUE;
498 RtlLockHeap(GetProcessHeap());
499 __TRY
501 pintern=HANDLE_TO_INTERN(hmem);
502 if(pintern->Magic==MAGIC_GLOBAL_USED)
504 if(pintern->LockCount)
506 pintern->LockCount--;
507 locked = (pintern->LockCount != 0);
508 if (!locked) SetLastError(NO_ERROR);
510 else
512 WARN("%p not locked\n", hmem);
513 SetLastError(ERROR_NOT_LOCKED);
514 locked = FALSE;
517 else
519 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
520 SetLastError(ERROR_INVALID_HANDLE);
521 locked=FALSE;
524 __EXCEPT_PAGE_FAULT
526 WARN("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
527 SetLastError( ERROR_INVALID_PARAMETER );
528 locked=FALSE;
530 __ENDTRY
531 RtlUnlockHeap(GetProcessHeap());
532 return locked;
536 /***********************************************************************
537 * GlobalHandle (KERNEL32.@)
539 * Get the handle associated with the pointer to a global memory block.
541 * RETURNS
542 * Handle: Success
543 * NULL: Failure
545 HGLOBAL WINAPI GlobalHandle(
546 LPCVOID pmem /* [in] Pointer to global memory block */
548 HGLOBAL handle;
549 PGLOBAL32_INTERN maybe_intern;
550 LPCVOID test;
552 if (!pmem)
554 SetLastError( ERROR_INVALID_PARAMETER );
555 return 0;
558 RtlLockHeap(GetProcessHeap());
559 __TRY
561 handle = 0;
563 /* note that if pmem is a pointer to a block allocated by */
564 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
565 /* will fail. */
566 if (ISPOINTER(pmem)) {
567 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
568 handle = (HGLOBAL)pmem; /* valid fixed block */
569 break;
571 handle = POINTER_TO_HANDLE(pmem);
572 } else
573 handle = (HGLOBAL)pmem;
575 /* Now test handle either passed in or retrieved from pointer */
576 maybe_intern = HANDLE_TO_INTERN( handle );
577 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
578 test = maybe_intern->Pointer;
579 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
580 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
581 break; /* valid moveable block */
583 handle = 0;
584 SetLastError( ERROR_INVALID_HANDLE );
586 __EXCEPT_PAGE_FAULT
588 SetLastError( ERROR_INVALID_HANDLE );
589 handle = 0;
591 __ENDTRY
592 RtlUnlockHeap(GetProcessHeap());
594 return handle;
598 /***********************************************************************
599 * GlobalReAlloc (KERNEL32.@)
601 * Change the size or attributes of a global memory object.
603 * RETURNS
604 * Handle: Success
605 * NULL: Failure
607 HGLOBAL WINAPI GlobalReAlloc(
608 HGLOBAL hmem, /* [in] Handle of global memory object */
609 SIZE_T size, /* [in] New size of block */
610 UINT flags /* [in] How to reallocate object */
612 LPVOID palloc;
613 HGLOBAL hnew;
614 PGLOBAL32_INTERN pintern;
615 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
617 hnew = 0;
618 RtlLockHeap(GetProcessHeap());
619 if(flags & GMEM_MODIFY) /* modify flags */
621 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
623 /* make a fixed block moveable
624 * actually only NT is able to do this. But it's soo simple
626 if (hmem == 0)
628 WARN("GlobalReAlloc with null handle!\n");
629 SetLastError( ERROR_NOACCESS );
630 hnew = 0;
632 else
634 size = HeapSize(GetProcessHeap(), 0, hmem);
635 hnew = GlobalAlloc(flags, size);
636 palloc = GlobalLock(hnew);
637 memcpy(palloc, hmem, size);
638 GlobalUnlock(hnew);
639 GlobalFree(hmem);
642 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
644 /* change the flags to make our block "discardable" */
645 pintern=HANDLE_TO_INTERN(hmem);
646 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
647 hnew=hmem;
649 else
651 SetLastError(ERROR_INVALID_PARAMETER);
652 hnew = 0;
655 else
657 if(ISPOINTER(hmem))
659 /* reallocate fixed memory */
660 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
662 else
664 /* reallocate a moveable block */
665 pintern=HANDLE_TO_INTERN(hmem);
667 #if 0
668 /* Apparently Windows doesn't care whether the handle is locked at this point */
669 /* See also the same comment in GlobalFree() */
670 if(pintern->LockCount>1) {
671 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
672 SetLastError(ERROR_INVALID_HANDLE);
673 } else
674 #endif
675 if(size!=0)
677 hnew=hmem;
678 if(pintern->Pointer)
680 if(size > INT_MAX-HGLOBAL_STORAGE)
682 SetLastError(ERROR_OUTOFMEMORY);
683 hnew = 0;
685 else if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
686 (char *) pintern->Pointer-HGLOBAL_STORAGE,
687 size+HGLOBAL_STORAGE)) == NULL)
688 hnew = 0; /* Block still valid */
689 else
690 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
692 else
694 if(size > INT_MAX-HGLOBAL_STORAGE)
696 SetLastError(ERROR_OUTOFMEMORY);
697 hnew = 0;
699 else if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
700 == NULL)
701 hnew = 0;
702 else
704 *(HGLOBAL *)palloc = hmem;
705 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
709 else
711 if (pintern->LockCount == 0)
713 if(pintern->Pointer)
715 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
716 pintern->Pointer = NULL;
718 hnew = hmem;
720 else
721 WARN("not freeing memory associated with locked handle\n");
725 RtlUnlockHeap(GetProcessHeap());
726 return hnew;
730 /***********************************************************************
731 * GlobalFree (KERNEL32.@)
733 * Free a global memory object.
735 * PARAMS
736 * hmem [I] Handle of the global memory object
738 * RETURNS
739 * Success: NULL
740 * Failure: The provided handle
742 * NOTES
743 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
746 HGLOBAL WINAPI GlobalFree(HGLOBAL hmem)
748 PGLOBAL32_INTERN pintern;
749 HGLOBAL hreturned;
751 RtlLockHeap(GetProcessHeap());
752 __TRY
754 hreturned = 0;
755 if(ISPOINTER(hmem)) /* POINTER */
757 if(!HeapFree(GetProcessHeap(), 0, hmem))
759 SetLastError(ERROR_INVALID_HANDLE);
760 hreturned = hmem;
763 else /* HANDLE */
765 pintern=HANDLE_TO_INTERN(hmem);
767 if(pintern->Magic==MAGIC_GLOBAL_USED)
769 pintern->Magic = 0xdead;
771 /* WIN98 does not make this test. That is you can free a */
772 /* block you have not unlocked. Go figure!! */
773 /* if(pintern->LockCount!=0) */
774 /* SetLastError(ERROR_INVALID_HANDLE); */
776 if(pintern->Pointer)
777 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
778 hreturned=hmem;
779 if(!HeapFree(GetProcessHeap(), 0, pintern))
780 hreturned=hmem;
782 else
784 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
785 SetLastError(ERROR_INVALID_HANDLE);
786 hreturned = hmem;
790 __EXCEPT_PAGE_FAULT
792 ERR("(%p): Page fault occurred ! Caused by bug ?\n", hmem);
793 SetLastError( ERROR_INVALID_PARAMETER );
794 hreturned = hmem;
796 __ENDTRY
797 RtlUnlockHeap(GetProcessHeap());
798 return hreturned;
802 /***********************************************************************
803 * GlobalSize (KERNEL32.@)
805 * Get the size of a global memory object.
807 * PARAMS
808 * hmem [I] Handle of the global memory object
810 * RETURNS
811 * Failure: 0
812 * Success: Size in Bytes of the global memory object
814 * NOTES
815 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
818 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
820 DWORD retval;
821 PGLOBAL32_INTERN pintern;
823 if (!((ULONG_PTR)hmem >> 16))
825 SetLastError(ERROR_INVALID_HANDLE);
826 return 0;
829 if(ISPOINTER(hmem))
831 retval=HeapSize(GetProcessHeap(), 0, hmem);
833 else
835 RtlLockHeap(GetProcessHeap());
836 pintern=HANDLE_TO_INTERN(hmem);
838 if(pintern->Magic==MAGIC_GLOBAL_USED)
840 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
841 retval = 0;
842 else
844 retval = HeapSize(GetProcessHeap(), 0,
845 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
846 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
849 else
851 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
852 SetLastError(ERROR_INVALID_HANDLE);
853 retval=0;
855 RtlUnlockHeap(GetProcessHeap());
857 /* HeapSize returns 0xffffffff on failure */
858 if (retval == 0xffffffff) retval = 0;
859 return retval;
863 /***********************************************************************
864 * GlobalWire (KERNEL32.@)
866 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
868 return GlobalLock( hmem );
872 /***********************************************************************
873 * GlobalUnWire (KERNEL32.@)
875 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
877 return GlobalUnlock( hmem);
881 /***********************************************************************
882 * GlobalFix (KERNEL32.@)
884 VOID WINAPI GlobalFix(HGLOBAL hmem)
886 GlobalLock( hmem );
890 /***********************************************************************
891 * GlobalUnfix (KERNEL32.@)
893 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
895 GlobalUnlock( hmem);
899 /***********************************************************************
900 * GlobalFlags (KERNEL32.@)
902 * Get information about a global memory object.
904 * PARAMS
905 * hmem [I] Handle of the global memory object
907 * RETURNS
908 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
909 * Success: Value specifying allocation flags and lock count
912 UINT WINAPI GlobalFlags(HGLOBAL hmem)
914 DWORD retval;
915 PGLOBAL32_INTERN pintern;
917 if(ISPOINTER(hmem))
919 retval=0;
921 else
923 RtlLockHeap(GetProcessHeap());
924 pintern=HANDLE_TO_INTERN(hmem);
925 if(pintern->Magic==MAGIC_GLOBAL_USED)
927 retval=pintern->LockCount + (pintern->Flags<<8);
928 if(pintern->Pointer==0)
929 retval|= GMEM_DISCARDED;
931 else
933 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
934 SetLastError(ERROR_INVALID_HANDLE);
935 retval = GMEM_INVALID_HANDLE;
937 RtlUnlockHeap(GetProcessHeap());
939 return retval;
943 /***********************************************************************
944 * GlobalCompact (KERNEL32.@)
946 SIZE_T WINAPI GlobalCompact( DWORD minfree )
948 return 0; /* GlobalCompact does nothing in Win32 */
952 /***********************************************************************
953 * LocalAlloc (KERNEL32.@)
955 * Allocate a local memory object.
957 * RETURNS
958 * Handle: Success
959 * NULL: Failure
961 * NOTES
962 * Windows memory management does not provide a separate local heap
963 * and global heap.
965 HLOCAL WINAPI LocalAlloc(
966 UINT flags, /* [in] Allocation attributes */
967 SIZE_T size /* [in] Number of bytes to allocate */
969 return GlobalAlloc( flags, size );
973 /***********************************************************************
974 * LocalCompact (KERNEL32.@)
976 SIZE_T WINAPI LocalCompact( UINT minfree )
978 return 0; /* LocalCompact does nothing in Win32 */
982 /***********************************************************************
983 * LocalFlags (KERNEL32.@)
985 * Get information about a local memory object.
987 * RETURNS
988 * Value specifying allocation flags and lock count.
989 * LMEM_INVALID_HANDLE: Failure
991 * NOTES
992 * Windows memory management does not provide a separate local heap
993 * and global heap.
995 UINT WINAPI LocalFlags(
996 HLOCAL handle /* [in] Handle of memory object */
998 return GlobalFlags( handle );
1002 /***********************************************************************
1003 * LocalFree (KERNEL32.@)
1005 * Free a local memory object.
1007 * RETURNS
1008 * NULL: Success
1009 * Handle: Failure
1011 * NOTES
1012 * Windows memory management does not provide a separate local heap
1013 * and global heap.
1015 HLOCAL WINAPI LocalFree(
1016 HLOCAL handle /* [in] Handle of memory object */
1018 return GlobalFree( handle );
1022 /***********************************************************************
1023 * LocalHandle (KERNEL32.@)
1025 * Get the handle associated with the pointer to a local memory block.
1027 * RETURNS
1028 * Handle: Success
1029 * NULL: Failure
1031 * NOTES
1032 * Windows memory management does not provide a separate local heap
1033 * and global heap.
1035 HLOCAL WINAPI LocalHandle(
1036 LPCVOID ptr /* [in] Address of local memory block */
1038 return GlobalHandle( ptr );
1042 /***********************************************************************
1043 * LocalLock (KERNEL32.@)
1044 * Locks a local memory object and returns pointer to the first byte
1045 * of the memory block.
1047 * RETURNS
1048 * Pointer: Success
1049 * NULL: Failure
1051 * NOTES
1052 * Windows memory management does not provide a separate local heap
1053 * and global heap.
1055 LPVOID WINAPI LocalLock(
1056 HLOCAL handle /* [in] Address of local memory object */
1058 return GlobalLock( handle );
1062 /***********************************************************************
1063 * LocalReAlloc (KERNEL32.@)
1065 * Change the size or attributes of a local memory object.
1067 * RETURNS
1068 * Handle: Success
1069 * NULL: Failure
1071 * NOTES
1072 * Windows memory management does not provide a separate local heap
1073 * and global heap.
1075 HLOCAL WINAPI LocalReAlloc(
1076 HLOCAL handle, /* [in] Handle of memory object */
1077 SIZE_T size, /* [in] New size of block */
1078 UINT flags /* [in] How to reallocate object */
1080 return GlobalReAlloc( handle, size, flags );
1084 /***********************************************************************
1085 * LocalShrink (KERNEL32.@)
1087 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1089 return 0; /* LocalShrink does nothing in Win32 */
1093 /***********************************************************************
1094 * LocalSize (KERNEL32.@)
1096 * Get the size of a local memory object.
1098 * RETURNS
1099 * Size: Success
1100 * 0: Failure
1102 * NOTES
1103 * Windows memory management does not provide a separate local heap
1104 * and global heap.
1106 SIZE_T WINAPI LocalSize(
1107 HLOCAL handle /* [in] Handle of memory object */
1109 return GlobalSize( handle );
1113 /***********************************************************************
1114 * LocalUnlock (KERNEL32.@)
1116 * Unlock a local memory object.
1118 * RETURNS
1119 * TRUE: Object is still locked
1120 * FALSE: Object is unlocked
1122 * NOTES
1123 * Windows memory management does not provide a separate local heap
1124 * and global heap.
1126 BOOL WINAPI LocalUnlock(
1127 HLOCAL handle /* [in] Handle of memory object */
1129 return GlobalUnlock( handle );
1133 /***********************************************************************
1134 * GlobalMemoryStatusEx (KERNEL32.@)
1135 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1137 * RETURNS
1138 * TRUE
1140 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1142 static MEMORYSTATUSEX cached_memstatus;
1143 static int cache_lastchecked = 0;
1144 SYSTEM_INFO si;
1145 #ifdef linux
1146 FILE *f;
1147 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
1148 unsigned long val;
1149 int mib[2];
1150 size_t size_sys;
1151 #elif defined(__APPLE__)
1152 unsigned int val;
1153 int mib[2];
1154 size_t size_sys;
1155 #elif defined(sun)
1156 unsigned long pagesize,maxpages,freepages,swapspace,swapfree;
1157 struct anoninfo swapinf;
1158 int rval;
1159 #endif
1161 if (lpmemex->dwLength != sizeof(*lpmemex))
1163 SetLastError( ERROR_INVALID_PARAMETER );
1164 return FALSE;
1167 if (time(NULL)==cache_lastchecked) {
1168 *lpmemex = cached_memstatus;
1169 return TRUE;
1171 cache_lastchecked = time(NULL);
1173 lpmemex->dwMemoryLoad = 0;
1174 lpmemex->ullTotalPhys = 16*1024*1024;
1175 lpmemex->ullAvailPhys = 16*1024*1024;
1176 lpmemex->ullTotalPageFile = 16*1024*1024;
1177 lpmemex->ullAvailPageFile = 16*1024*1024;
1179 #ifdef linux
1180 f = fopen( "/proc/meminfo", "r" );
1181 if (f)
1183 char buffer[256];
1184 unsigned long total, used, free, shared, buffers, cached;
1186 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1187 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1188 while (fgets( buffer, sizeof(buffer), f ))
1190 /* old style /proc/meminfo ... */
1191 if (sscanf( buffer, "Mem: %lu %lu %lu %lu %lu %lu",
1192 &total, &used, &free, &shared, &buffers, &cached ))
1194 lpmemex->ullTotalPhys += total;
1195 lpmemex->ullAvailPhys += free + buffers + cached;
1197 if (sscanf( buffer, "Swap: %lu %lu %lu", &total, &used, &free ))
1199 lpmemex->ullTotalPageFile += total;
1200 lpmemex->ullAvailPageFile += free;
1203 /* new style /proc/meminfo ... */
1204 if (sscanf(buffer, "MemTotal: %lu", &total))
1205 lpmemex->ullTotalPhys = total*1024;
1206 if (sscanf(buffer, "MemFree: %lu", &free))
1207 lpmemex->ullAvailPhys = free*1024;
1208 if (sscanf(buffer, "SwapTotal: %lu", &total))
1209 lpmemex->ullTotalPageFile = total*1024;
1210 if (sscanf(buffer, "SwapFree: %lu", &free))
1211 lpmemex->ullAvailPageFile = free*1024;
1212 if (sscanf(buffer, "Buffers: %lu", &buffers))
1213 lpmemex->ullAvailPhys += buffers*1024;
1214 if (sscanf(buffer, "Cached: %lu", &cached))
1215 lpmemex->ullAvailPhys += cached*1024;
1217 fclose( f );
1219 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
1220 mib[0] = CTL_HW;
1221 mib[1] = HW_PHYSMEM;
1222 size_sys = sizeof(val);
1223 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1224 if (val) lpmemex->ullTotalPhys = val;
1225 mib[1] = HW_USERMEM;
1226 size_sys = sizeof(val);
1227 sysctl(mib, 2, &val, &size_sys, NULL, 0);
1228 if (!val) val = lpmemex->ullTotalPhys;
1229 lpmemex->ullAvailPhys = val;
1230 lpmemex->ullTotalPageFile = val;
1231 lpmemex->ullAvailPageFile = val;
1232 #elif defined ( sun )
1233 pagesize=sysconf(_SC_PAGESIZE);
1234 maxpages=sysconf(_SC_PHYS_PAGES);
1235 freepages=sysconf(_SC_AVPHYS_PAGES);
1236 rval=swapctl(SC_AINFO, &swapinf);
1237 if(rval >-1)
1239 swapspace=swapinf.ani_max*pagesize;
1240 swapfree=swapinf.ani_free*pagesize;
1241 }else
1244 WARN("Swap size cannot be determined , assuming equal to physical memory\n");
1245 swapspace=maxpages*pagesize;
1246 swapfree=maxpages*pagesize;
1248 lpmemex->ullTotalPhys=pagesize*maxpages;
1249 lpmemex->ullAvailPhys = pagesize*freepages;
1250 lpmemex->ullTotalPageFile = swapspace;
1251 lpmemex->ullAvailPageFile = swapfree;
1252 #endif
1254 if (lpmemex->ullTotalPhys)
1256 lpmemex->dwMemoryLoad = (lpmemex->ullTotalPhys-lpmemex->ullAvailPhys)
1257 / (lpmemex->ullTotalPhys / 100);
1260 /* Win98 returns only the swapsize in ullTotalPageFile/ullAvailPageFile,
1261 WinXP returns the size of physical memory + swapsize;
1262 mimic the behavior of XP.
1263 Note: Project2k refuses to start if it sees less than 1Mb of free swap.
1265 lpmemex->ullTotalPageFile += lpmemex->ullTotalPhys;
1266 lpmemex->ullAvailPageFile += lpmemex->ullAvailPhys;
1268 /* Titan Quest refuses to run if TotalPageFile <= ullTotalPhys */
1269 if(lpmemex->ullTotalPageFile == lpmemex->ullTotalPhys)
1271 lpmemex->ullTotalPhys -= 1;
1272 lpmemex->ullAvailPhys -= 1;
1275 /* FIXME: should do something for other systems */
1276 GetSystemInfo(&si);
1277 lpmemex->ullTotalVirtual = (ULONG_PTR)si.lpMaximumApplicationAddress-(ULONG_PTR)si.lpMinimumApplicationAddress;
1278 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1279 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1281 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1282 memory in the extended portion of the virtual address space of the calling
1283 process, in bytes.
1284 However, I don't know what this means, so set it to zero :(
1286 lpmemex->ullAvailExtendedVirtual = 0;
1288 cached_memstatus = *lpmemex;
1290 TRACE("<-- LPMEMORYSTATUSEX: dwLength %d, dwMemoryLoad %d, ullTotalPhys %s, ullAvailPhys %s,"
1291 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1292 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1293 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1294 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1295 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1297 return TRUE;
1300 /***********************************************************************
1301 * GlobalMemoryStatus (KERNEL32.@)
1302 * Provides information about the status of the memory, so apps can tell
1303 * roughly how much they are able to allocate
1305 * RETURNS
1306 * None
1308 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1310 MEMORYSTATUSEX memstatus;
1311 OSVERSIONINFOW osver;
1312 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
1314 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1315 for one extra field in the struct, and the lack of a bug, we simply
1316 call GlobalMemoryStatusEx and copy the values across. */
1317 memstatus.dwLength = sizeof(memstatus);
1318 GlobalMemoryStatusEx(&memstatus);
1320 lpBuffer->dwLength = sizeof(*lpBuffer);
1321 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1323 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1324 * NT reports values modulo 4 Gb.
1327 osver.dwOSVersionInfoSize = sizeof(osver);
1328 GetVersionExW(&osver);
1330 if ( osver.dwMajorVersion >= 5 )
1332 lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
1333 lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
1334 lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, MAXDWORD );
1335 lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
1336 lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
1337 lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
1340 else /* duplicate NT bug */
1342 lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
1343 lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
1344 lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
1345 lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
1346 lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
1347 lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
1350 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
1351 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
1352 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
1354 if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
1355 if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
1356 if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
1357 if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
1360 /* work around for broken photoshop 4 installer */
1361 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1362 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
1364 /* limit page file size for really old binaries */
1365 if (nt->OptionalHeader.MajorSubsystemVersion < 4)
1367 if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
1368 if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
1371 TRACE("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
1372 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
1373 lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
1374 lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
1375 lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );