Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / heap.c
blobeb1fa9b10e9d6bedfc8ac0c5e7d7300fae349dd5
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*)0x80000000)
58 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
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;
89 /* create the system heap event first */
90 event = CreateEventA( NULL, TRUE, FALSE, "__wine_system_heap_event" );
92 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
93 0, SYSTEM_HEAP_SIZE, "__wine_system_heap" ))) return 0;
94 created = (GetLastError() != ERROR_ALREADY_EXISTS);
96 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
98 /* pre-defined address not available */
99 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
100 return 0;
103 if (created) /* newly created heap */
105 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
106 SYSTEM_HEAP_SIZE, NULL, NULL );
107 SetEvent( event );
109 else
111 /* wait for the heap to be initialized */
112 WaitForSingleObject( event, INFINITE );
113 systemHeap = (HANDLE)base;
115 CloseHandle( map );
116 return systemHeap;
120 /***********************************************************************
121 * HeapCreate (KERNEL32.@)
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.@)
150 * RETURNS
151 * TRUE: Success
152 * FALSE: Failure
154 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
156 if (heap == systemHeap)
158 WARN( "attempt to destroy system heap, returning TRUE!\n" );
159 return TRUE;
161 if (!RtlDestroyHeap( heap )) return TRUE;
162 SetLastError( ERROR_INVALID_HANDLE );
163 return FALSE;
167 /***********************************************************************
168 * HeapCompact (KERNEL32.@)
170 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
172 return RtlCompactHeap( heap, flags );
176 /***********************************************************************
177 * HeapValidate (KERNEL32.@)
178 * Validates a specified heap.
180 * NOTES
181 * Flags is ignored.
183 * RETURNS
184 * TRUE: Success
185 * FALSE: Failure
187 BOOL WINAPI HeapValidate(
188 HANDLE heap, /* [in] Handle to the heap */
189 DWORD flags, /* [in] Bit flags that control access during operation */
190 LPCVOID block /* [in] Optional pointer to memory block to validate */
192 return RtlValidateHeap( heap, flags, block );
196 /***********************************************************************
197 * HeapWalk (KERNEL32.@)
198 * Enumerates the memory blocks in a specified heap.
200 * TODO
201 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
202 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
204 * RETURNS
205 * TRUE: Success
206 * FALSE: Failure
208 BOOL WINAPI HeapWalk(
209 HANDLE heap, /* [in] Handle to heap to enumerate */
210 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
212 NTSTATUS ret = RtlWalkHeap( heap, entry );
213 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
214 return !ret;
218 /***********************************************************************
219 * HeapLock (KERNEL32.@)
220 * Attempts to acquire the critical section object for a specified heap.
222 * RETURNS
223 * TRUE: Success
224 * FALSE: Failure
226 BOOL WINAPI HeapLock(
227 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
229 return RtlLockHeap( heap );
233 /***********************************************************************
234 * HeapUnlock (KERNEL32.@)
235 * Releases ownership of the critical section object.
237 * RETURNS
238 * TRUE: Success
239 * FALSE: Failure
241 BOOL WINAPI HeapUnlock(
242 HANDLE heap /* [in] Handle to the heap to unlock */
244 return RtlUnlockHeap( heap );
248 /***********************************************************************
249 * GetProcessHeap (KERNEL32.@)
251 HANDLE WINAPI GetProcessHeap(void)
253 return NtCurrentTeb()->Peb->ProcessHeap;
257 /***********************************************************************
258 * GetProcessHeaps (KERNEL32.@)
260 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
262 return RtlGetProcessHeaps( count, heaps );
266 /* These are needed so that we can call the functions from inside kernel itself */
268 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
270 return RtlAllocateHeap( heap, flags, size );
273 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
275 return RtlFreeHeap( heap, flags, ptr );
278 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
280 return RtlReAllocateHeap( heap, flags, ptr, size );
283 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
285 return RtlSizeHeap( heap, flags, ptr );
289 * Win32 Global heap functions (GlobalXXX).
290 * These functions included in Win32 for compatibility with 16 bit Windows
291 * Especially the moveable blocks and handles are oldish.
292 * But the ability to directly allocate memory with GPTR and LPTR is widely
293 * used.
295 * The handle stuff looks horrible, but it's implemented almost like Win95
296 * does it.
300 #define MAGIC_GLOBAL_USED 0x5342
301 #define GLOBAL_LOCK_MAX 0xFF
302 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
303 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
304 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
305 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
306 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
307 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
308 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
309 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
310 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
311 * the output jpeg's > 1 MB if not */
312 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
314 #include "pshpack1.h"
316 typedef struct __GLOBAL32_INTERN
318 WORD Magic;
319 LPVOID Pointer;
320 BYTE Flags;
321 BYTE LockCount;
322 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
324 #include "poppack.h"
326 /***********************************************************************
327 * GlobalAlloc (KERNEL32.@)
328 * RETURNS
329 * Handle: Success
330 * NULL: Failure
332 HGLOBAL WINAPI GlobalAlloc(
333 UINT flags, /* [in] Object allocation attributes */
334 SIZE_T size /* [in] Number of bytes to allocate */
336 PGLOBAL32_INTERN pintern;
337 DWORD hpflags;
338 LPVOID palloc;
340 if(flags&GMEM_ZEROINIT)
341 hpflags=HEAP_ZERO_MEMORY;
342 else
343 hpflags=0;
345 TRACE("() flags=%04x\n", flags );
347 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
349 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
350 return (HGLOBAL) palloc;
352 else /* HANDLE */
354 RtlLockHeap(GetProcessHeap());
356 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
357 if (pintern)
359 pintern->Magic = MAGIC_GLOBAL_USED;
360 pintern->Flags = flags >> 8;
361 pintern->LockCount = 0;
363 if (size)
365 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
366 if (!palloc)
368 HeapFree(GetProcessHeap(), 0, pintern);
369 pintern = NULL;
371 else
373 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
374 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
377 else
378 pintern->Pointer = NULL;
381 RtlUnlockHeap(GetProcessHeap());
382 return pintern ? INTERN_TO_HANDLE(pintern) : 0;
387 /***********************************************************************
388 * GlobalLock (KERNEL32.@)
389 * RETURNS
390 * Pointer to first byte of block
391 * NULL: Failure
393 LPVOID WINAPI GlobalLock(
394 HGLOBAL hmem /* [in] Handle of global memory object */
397 PGLOBAL32_INTERN pintern;
398 LPVOID palloc;
400 if (ISPOINTER(hmem))
401 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
403 RtlLockHeap(GetProcessHeap());
404 __TRY
406 pintern = HANDLE_TO_INTERN(hmem);
407 if (pintern->Magic == MAGIC_GLOBAL_USED)
409 if (pintern->LockCount < GLOBAL_LOCK_MAX)
410 pintern->LockCount++;
411 palloc = pintern->Pointer;
413 else
415 WARN("invalid handle %p\n", hmem);
416 palloc = NULL;
417 SetLastError(ERROR_INVALID_HANDLE);
420 __EXCEPT(page_fault)
422 WARN("page fault on %p\n", hmem);
423 palloc = NULL;
424 SetLastError(ERROR_INVALID_HANDLE);
426 __ENDTRY
427 RtlUnlockHeap(GetProcessHeap());
428 return palloc;
432 /***********************************************************************
433 * GlobalUnlock (KERNEL32.@)
434 * RETURNS
435 * TRUE: Object is still locked
436 * FALSE: Object is unlocked
438 BOOL WINAPI GlobalUnlock(
439 HGLOBAL hmem /* [in] Handle of global memory object */
441 PGLOBAL32_INTERN pintern;
442 BOOL locked;
444 if (ISPOINTER(hmem)) return FALSE;
446 RtlLockHeap(GetProcessHeap());
447 __TRY
449 pintern=HANDLE_TO_INTERN(hmem);
450 if(pintern->Magic==MAGIC_GLOBAL_USED)
452 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
453 pintern->LockCount--;
455 locked = (pintern->LockCount != 0);
456 if (!locked) SetLastError(NO_ERROR);
458 else
460 WARN("invalid handle\n");
461 SetLastError(ERROR_INVALID_HANDLE);
462 locked=FALSE;
465 __EXCEPT(page_fault)
467 ERR("page fault occurred ! Caused by bug ?\n");
468 SetLastError( ERROR_INVALID_PARAMETER );
469 locked=FALSE;
471 __ENDTRY
472 RtlUnlockHeap(GetProcessHeap());
473 return locked;
477 /***********************************************************************
478 * GlobalHandle (KERNEL32.@)
479 * Returns the handle associated with the specified pointer.
481 * RETURNS
482 * Handle: Success
483 * NULL: Failure
485 HGLOBAL WINAPI GlobalHandle(
486 LPCVOID pmem /* [in] Pointer to global memory block */
488 HGLOBAL handle;
489 PGLOBAL32_INTERN maybe_intern;
490 LPCVOID test;
492 if (!pmem)
494 SetLastError( ERROR_INVALID_PARAMETER );
495 return 0;
498 RtlLockHeap(GetProcessHeap());
499 __TRY
501 handle = 0;
503 /* note that if pmem is a pointer to a a block allocated by */
504 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
505 /* will fail. */
506 if (ISPOINTER(pmem)) {
507 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
508 handle = (HGLOBAL)pmem; /* valid fixed block */
509 break;
511 handle = POINTER_TO_HANDLE(pmem);
512 } else
513 handle = (HGLOBAL)pmem;
515 /* Now test handle either passed in or retrieved from pointer */
516 maybe_intern = HANDLE_TO_INTERN( handle );
517 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
518 test = maybe_intern->Pointer;
519 if (HeapValidate( GetProcessHeap(), 0, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
520 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
521 break; /* valid moveable block */
523 handle = 0;
524 SetLastError( ERROR_INVALID_HANDLE );
526 __EXCEPT(page_fault)
528 SetLastError( ERROR_INVALID_HANDLE );
529 handle = 0;
531 __ENDTRY
532 RtlUnlockHeap(GetProcessHeap());
534 return handle;
538 /***********************************************************************
539 * GlobalReAlloc (KERNEL32.@)
540 * RETURNS
541 * Handle: Success
542 * NULL: Failure
544 HGLOBAL WINAPI GlobalReAlloc(
545 HGLOBAL hmem, /* [in] Handle of global memory object */
546 SIZE_T size, /* [in] New size of block */
547 UINT flags /* [in] How to reallocate object */
549 LPVOID palloc;
550 HGLOBAL hnew;
551 PGLOBAL32_INTERN pintern;
552 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
554 hnew = 0;
555 RtlLockHeap(GetProcessHeap());
556 if(flags & GMEM_MODIFY) /* modify flags */
558 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
560 /* make a fixed block moveable
561 * actually only NT is able to do this. But it's soo simple
563 if (hmem == 0)
565 WARN("GlobalReAlloc with null handle!\n");
566 SetLastError( ERROR_NOACCESS );
567 hnew = 0;
569 else
571 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
572 hnew = GlobalAlloc(flags, size);
573 palloc = GlobalLock(hnew);
574 memcpy(palloc, (LPVOID)hmem, size);
575 GlobalUnlock(hnew);
576 GlobalFree(hmem);
579 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
581 /* change the flags to make our block "discardable" */
582 pintern=HANDLE_TO_INTERN(hmem);
583 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
584 hnew=hmem;
586 else
588 SetLastError(ERROR_INVALID_PARAMETER);
589 hnew = 0;
592 else
594 if(ISPOINTER(hmem))
596 /* reallocate fixed memory */
597 hnew=HeapReAlloc(GetProcessHeap(), heap_flags, hmem, size);
599 else
601 /* reallocate a moveable block */
602 pintern=HANDLE_TO_INTERN(hmem);
604 #if 0
605 /* Apparently Windows doesn't care whether the handle is locked at this point */
606 /* See also the same comment in GlobalFree() */
607 if(pintern->LockCount>1) {
608 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
609 SetLastError(ERROR_INVALID_HANDLE);
610 } else
611 #endif
612 if(size!=0)
614 hnew=hmem;
615 if(pintern->Pointer)
617 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
618 (char *) pintern->Pointer-HGLOBAL_STORAGE,
619 size+HGLOBAL_STORAGE)) == NULL)
620 hnew = 0; /* Block still valid */
621 else
622 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
624 else
626 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
627 == NULL)
628 hnew = 0;
629 else
631 *(HGLOBAL *)palloc = hmem;
632 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
636 else
638 if(pintern->Pointer)
640 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
641 pintern->Pointer=NULL;
646 RtlUnlockHeap(GetProcessHeap());
647 return hnew;
651 /***********************************************************************
652 * GlobalFree (KERNEL32.@)
653 * RETURNS
654 * NULL: Success
655 * Handle: Failure
657 HGLOBAL WINAPI GlobalFree(
658 HGLOBAL hmem /* [in] Handle of global memory object */
660 PGLOBAL32_INTERN pintern;
661 HGLOBAL hreturned;
663 RtlLockHeap(GetProcessHeap());
664 __TRY
666 hreturned = 0;
667 if(ISPOINTER(hmem)) /* POINTER */
669 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
671 else /* HANDLE */
673 pintern=HANDLE_TO_INTERN(hmem);
675 if(pintern->Magic==MAGIC_GLOBAL_USED)
678 /* WIN98 does not make this test. That is you can free a */
679 /* block you have not unlocked. Go figure!! */
680 /* if(pintern->LockCount!=0) */
681 /* SetLastError(ERROR_INVALID_HANDLE); */
683 if(pintern->Pointer)
684 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
685 hreturned=hmem;
686 if(!HeapFree(GetProcessHeap(), 0, pintern))
687 hreturned=hmem;
691 __EXCEPT(page_fault)
693 ERR("page fault occurred ! Caused by bug ?\n");
694 SetLastError( ERROR_INVALID_PARAMETER );
695 hreturned = hmem;
697 __ENDTRY
698 RtlUnlockHeap(GetProcessHeap());
699 return hreturned;
703 /***********************************************************************
704 * GlobalSize (KERNEL32.@)
705 * RETURNS
706 * Size in bytes of the global memory object
707 * 0: Failure
709 SIZE_T WINAPI GlobalSize(
710 HGLOBAL hmem /* [in] Handle of global memory object */
712 DWORD retval;
713 PGLOBAL32_INTERN pintern;
715 if (!hmem) return 0;
717 if(ISPOINTER(hmem))
719 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
721 else
723 RtlLockHeap(GetProcessHeap());
724 pintern=HANDLE_TO_INTERN(hmem);
726 if(pintern->Magic==MAGIC_GLOBAL_USED)
728 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
729 retval = 0;
730 else
732 retval = HeapSize(GetProcessHeap(), 0,
733 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
734 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
737 else
739 WARN("invalid handle\n");
740 retval=0;
742 RtlUnlockHeap(GetProcessHeap());
744 /* HeapSize returns 0xffffffff on failure */
745 if (retval == 0xffffffff) retval = 0;
746 return retval;
750 /***********************************************************************
751 * GlobalWire (KERNEL32.@)
753 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
755 return GlobalLock( hmem );
759 /***********************************************************************
760 * GlobalUnWire (KERNEL32.@)
762 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
764 return GlobalUnlock( hmem);
768 /***********************************************************************
769 * GlobalFix (KERNEL32.@)
771 VOID WINAPI GlobalFix(HGLOBAL hmem)
773 GlobalLock( hmem );
777 /***********************************************************************
778 * GlobalUnfix (KERNEL32.@)
780 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
782 GlobalUnlock( hmem);
786 /***********************************************************************
787 * GlobalFlags (KERNEL32.@)
788 * Returns information about the specified global memory object
790 * NOTES
791 * Should this return GMEM_INVALID_HANDLE on invalid handle?
793 * RETURNS
794 * Value specifying allocation flags and lock count
795 * GMEM_INVALID_HANDLE: Failure
797 UINT WINAPI GlobalFlags(
798 HGLOBAL hmem /* [in] Handle to global memory object */
800 DWORD retval;
801 PGLOBAL32_INTERN pintern;
803 if(ISPOINTER(hmem))
805 retval=0;
807 else
809 RtlLockHeap(GetProcessHeap());
810 pintern=HANDLE_TO_INTERN(hmem);
811 if(pintern->Magic==MAGIC_GLOBAL_USED)
813 retval=pintern->LockCount + (pintern->Flags<<8);
814 if(pintern->Pointer==0)
815 retval|= GMEM_DISCARDED;
817 else
819 WARN("Invalid handle: %p\n", hmem);
820 retval=0;
822 RtlUnlockHeap(GetProcessHeap());
824 return retval;
828 /***********************************************************************
829 * GlobalCompact (KERNEL32.@)
831 SIZE_T WINAPI GlobalCompact( DWORD minfree )
833 return 0; /* GlobalCompact does nothing in Win32 */
837 /***********************************************************************
838 * LocalAlloc (KERNEL32.@)
839 * RETURNS
840 * Handle: Success
841 * NULL: Failure
843 HLOCAL WINAPI LocalAlloc(
844 UINT flags, /* [in] Allocation attributes */
845 SIZE_T size /* [in] Number of bytes to allocate */
847 return (HLOCAL)GlobalAlloc( flags, size );
851 /***********************************************************************
852 * LocalCompact (KERNEL32.@)
854 SIZE_T WINAPI LocalCompact( UINT minfree )
856 return 0; /* LocalCompact does nothing in Win32 */
860 /***********************************************************************
861 * LocalFlags (KERNEL32.@)
862 * RETURNS
863 * Value specifying allocation flags and lock count.
864 * LMEM_INVALID_HANDLE: Failure
866 UINT WINAPI LocalFlags(
867 HLOCAL handle /* [in] Handle of memory object */
869 return GlobalFlags( (HGLOBAL)handle );
873 /***********************************************************************
874 * LocalFree (KERNEL32.@)
875 * RETURNS
876 * NULL: Success
877 * Handle: Failure
879 HLOCAL WINAPI LocalFree(
880 HLOCAL handle /* [in] Handle of memory object */
882 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
886 /***********************************************************************
887 * LocalHandle (KERNEL32.@)
888 * RETURNS
889 * Handle: Success
890 * NULL: Failure
892 HLOCAL WINAPI LocalHandle(
893 LPCVOID ptr /* [in] Address of local memory object */
895 return (HLOCAL)GlobalHandle( ptr );
899 /***********************************************************************
900 * LocalLock (KERNEL32.@)
901 * Locks a local memory object and returns pointer to the first byte
902 * of the memory block.
904 * RETURNS
905 * Pointer: Success
906 * NULL: Failure
908 LPVOID WINAPI LocalLock(
909 HLOCAL handle /* [in] Address of local memory object */
911 return GlobalLock( (HGLOBAL)handle );
915 /***********************************************************************
916 * LocalReAlloc (KERNEL32.@)
917 * RETURNS
918 * Handle: Success
919 * NULL: Failure
921 HLOCAL WINAPI LocalReAlloc(
922 HLOCAL handle, /* [in] Handle of memory object */
923 SIZE_T size, /* [in] New size of block */
924 UINT flags /* [in] How to reallocate object */
926 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
930 /***********************************************************************
931 * LocalShrink (KERNEL32.@)
933 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
935 return 0; /* LocalShrink does nothing in Win32 */
939 /***********************************************************************
940 * LocalSize (KERNEL32.@)
941 * RETURNS
942 * Size: Success
943 * 0: Failure
945 SIZE_T WINAPI LocalSize(
946 HLOCAL handle /* [in] Handle of memory object */
948 return GlobalSize( (HGLOBAL)handle );
952 /***********************************************************************
953 * LocalUnlock (KERNEL32.@)
954 * RETURNS
955 * TRUE: Object is still locked
956 * FALSE: Object is unlocked
958 BOOL WINAPI LocalUnlock(
959 HLOCAL handle /* [in] Handle of memory object */
961 return GlobalUnlock( (HGLOBAL)handle );
965 /**********************************************************************
966 * AllocMappedBuffer (KERNEL32.38)
968 * This is an undocumented KERNEL32 function that
969 * SMapLS's a GlobalAlloc'ed buffer.
971 * Input: EDI register: size of buffer to allocate
972 * Output: EDI register: pointer to buffer
974 * Note: The buffer is preceded by 8 bytes:
975 * ...
976 * edi+0 buffer
977 * edi-4 SEGPTR to buffer
978 * edi-8 some magic Win95 needs for SUnMapLS
979 * (we use it for the memory handle)
981 * The SEGPTR is used by the caller!
983 void WINAPI __regs_AllocMappedBuffer( CONTEXT86 *context )
985 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
986 DWORD *buffer = (DWORD *)GlobalLock(handle);
987 DWORD ptr = 0;
989 if (buffer)
990 if (!(ptr = MapLS(buffer + 2)))
992 GlobalUnlock(handle);
993 GlobalFree(handle);
996 if (!ptr)
997 context->Eax = context->Edi = 0;
998 else
1000 buffer[0] = (DWORD)handle;
1001 buffer[1] = ptr;
1003 context->Eax = (DWORD) ptr;
1004 context->Edi = (DWORD)(buffer + 2);
1007 #ifdef DEFINE_REGS_ENTRYPOINT
1008 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0, 0 );
1009 #endif
1011 /**********************************************************************
1012 * FreeMappedBuffer (KERNEL32.39)
1014 * Free a buffer allocated by AllocMappedBuffer
1016 * Input: EDI register: pointer to buffer
1018 void WINAPI __regs_FreeMappedBuffer( CONTEXT86 *context )
1020 if (context->Edi)
1022 DWORD *buffer = (DWORD *)context->Edi - 2;
1024 UnMapLS(buffer[1]);
1026 GlobalUnlock((HGLOBAL)buffer[0]);
1027 GlobalFree((HGLOBAL)buffer[0]);
1030 #ifdef DEFINE_REGS_ENTRYPOINT
1031 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0, 0 );
1032 #endif
1034 /***********************************************************************
1035 * GlobalMemoryStatusEx (KERNEL32.@)
1036 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1038 * RETURNS
1039 * TRUE
1041 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
1043 static MEMORYSTATUSEX cached_memstatus;
1044 static int cache_lastchecked = 0;
1045 SYSTEM_INFO si;
1046 #ifdef linux
1047 FILE *f;
1048 #endif
1049 #if defined(__FreeBSD__) || defined(__NetBSD__)
1050 int *tmp;
1051 int size_sys;
1052 int mib[2] = { CTL_HW };
1053 #endif
1054 if (time(NULL)==cache_lastchecked) {
1055 memcpy(lpmemex,&cached_memstatus,sizeof(*lpmemex));
1056 return TRUE;
1058 cache_lastchecked = time(NULL);
1060 lpmemex->dwLength = sizeof(*lpmemex);
1061 lpmemex->dwMemoryLoad = 0;
1062 lpmemex->ullTotalPhys = 16*1024*1024;
1063 lpmemex->ullAvailPhys = 16*1024*1024;
1064 lpmemex->ullTotalPageFile = 16*1024*1024;
1065 lpmemex->ullAvailPageFile = 16*1024*1024;
1067 #ifdef linux
1068 f = fopen( "/proc/meminfo", "r" );
1069 if (f)
1071 char buffer[256];
1072 int total, used, free, shared, buffers, cached;
1074 lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
1075 lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
1076 while (fgets( buffer, sizeof(buffer), f ))
1078 /* old style /proc/meminfo ... */
1079 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1081 lpmemex->ullTotalPhys += total;
1082 lpmemex->ullAvailPhys += free + buffers + cached;
1084 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1086 lpmemex->ullTotalPageFile += total;
1087 lpmemex->ullAvailPageFile += free;
1090 /* new style /proc/meminfo ... */
1091 if (sscanf(buffer, "MemTotal: %d", &total))
1092 lpmemex->ullTotalPhys = total*1024;
1093 if (sscanf(buffer, "MemFree: %d", &free))
1094 lpmemex->ullAvailPhys = free*1024;
1095 if (sscanf(buffer, "SwapTotal: %d", &total))
1096 lpmemex->ullTotalPageFile = total*1024;
1097 if (sscanf(buffer, "SwapFree: %d", &free))
1098 lpmemex->ullAvailPageFile = free*1024;
1099 if (sscanf(buffer, "Buffers: %d", &buffers))
1100 lpmemex->ullAvailPhys += buffers*1024;
1101 if (sscanf(buffer, "Cached: %d", &cached))
1102 lpmemex->ullAvailPhys += cached*1024;
1104 fclose( f );
1106 if (lpmemex->ullTotalPhys)
1108 DWORDLONG TotalPhysical = lpmemex->ullTotalPhys+lpmemex->ullTotalPageFile;
1109 DWORDLONG AvailPhysical = lpmemex->ullAvailPhys+lpmemex->ullAvailPageFile;
1110 lpmemex->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1111 / (TotalPhysical / 100);
1114 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1115 mib[1] = HW_PHYSMEM;
1116 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1117 tmp = malloc(size_sys * sizeof(int));
1118 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1119 if (tmp && *tmp)
1121 lpmemex->ullTotalPhys = *tmp;
1122 free(tmp);
1123 mib[1] = HW_USERMEM;
1124 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1125 tmp = malloc(size_sys * sizeof(int));
1126 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1127 if (tmp && *tmp)
1129 lpmemex->ullAvailPhys = *tmp;
1130 lpmemex->ullTotalPageFile = *tmp;
1131 lpmemex->ullAvailPageFile = *tmp;
1132 lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys;
1133 } else
1135 lpmemex->ullAvailPhys = lpmemex->ullTotalPhys;
1136 lpmemex->ullTotalPageFile = lpmemex->ullTotalPhys;
1137 lpmemex->ullAvailPageFile = lpmemex->ullTotalPhys;
1138 lpmemex->dwMemoryLoad = 0;
1140 free(tmp);
1143 #endif
1145 /* FIXME: should do something for other systems */
1146 GetSystemInfo(&si);
1147 lpmemex->ullTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1148 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1149 lpmemex->ullAvailVirtual = lpmemex->ullTotalVirtual-64*1024;
1150 memcpy(&cached_memstatus,lpmemex,sizeof(*lpmemex));
1152 /* it appears some memory display programs want to divide by these values */
1153 if(lpmemex->ullTotalPageFile==0)
1154 lpmemex->ullTotalPageFile++;
1156 if(lpmemex->ullAvailPageFile==0)
1157 lpmemex->ullAvailPageFile++;
1159 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1160 memory in the extended portion of the virtual address space of the calling
1161 process, in bytes.
1162 However, I don't know what this means, so set it to zero :(
1164 lpmemex->ullAvailExtendedVirtual = 0;
1166 TRACE("<-- LPMEMORYSTATUSEX: dwLength %ld, dwMemoryLoad %ld, ullTotalPhys %s, ullAvailPhys %s,"
1167 " ullTotalPageFile %s, ullAvailPageFile %s, ullTotalVirtual %s, ullAvailVirtual %s\n",
1168 lpmemex->dwLength, lpmemex->dwMemoryLoad, wine_dbgstr_longlong(lpmemex->ullTotalPhys),
1169 wine_dbgstr_longlong(lpmemex->ullAvailPhys), wine_dbgstr_longlong(lpmemex->ullTotalPageFile),
1170 wine_dbgstr_longlong(lpmemex->ullAvailPageFile), wine_dbgstr_longlong(lpmemex->ullTotalVirtual),
1171 wine_dbgstr_longlong(lpmemex->ullAvailVirtual) );
1173 return TRUE;
1176 /***********************************************************************
1177 * GlobalMemoryStatus (KERNEL32.@)
1178 * Provides information about the status of the memory, so apps can tell
1179 * roughly how much they are able to allocate
1181 * RETURNS
1182 * None
1184 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
1186 MEMORYSTATUSEX memstatus;
1187 OSVERSIONINFOW osver;
1189 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
1190 for one extra field in the struct, and the lack of a bug, we simply
1191 call GlobalMemoryStatusEx and copy the values across. */
1192 GlobalMemoryStatusEx(&memstatus);
1194 lpBuffer->dwLength = sizeof(*lpBuffer);
1195 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1197 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
1198 * NT reports values modulo 4 Gb.
1199 * Values between 2 Gb and 4 Gb are rounded down to 2 Gb.
1202 osver.dwOSVersionInfoSize = sizeof(osver);
1203 GetVersionExW(&osver);
1205 if ( osver.dwMajorVersion >= 5 )
1207 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? MAXDWORD :
1208 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1209 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? MAXDWORD :
1210 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1211 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? MAXDWORD :
1212 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1213 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? MAXDWORD :
1214 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1215 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? MAXDWORD :
1216 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1217 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? MAXDWORD :
1218 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1220 else /* duplicate NT bug */
1222 lpBuffer->dwTotalPhys = (memstatus.ullTotalPhys > MAXDWORD) ? memstatus.ullTotalPhys :
1223 (memstatus.ullTotalPhys > MAXLONG) ? MAXLONG : memstatus.ullTotalPhys;
1224 lpBuffer->dwAvailPhys = (memstatus.ullAvailPhys > MAXDWORD) ? memstatus.ullAvailPhys :
1225 (memstatus.ullAvailPhys > MAXLONG) ? MAXLONG : memstatus.ullAvailPhys;
1226 lpBuffer->dwTotalPageFile = (memstatus.ullTotalPageFile > MAXDWORD) ? memstatus.ullTotalPageFile :
1227 (memstatus.ullTotalPageFile > MAXLONG) ? MAXLONG : memstatus.ullTotalPageFile;
1228 lpBuffer->dwAvailPageFile = (memstatus.ullAvailPageFile > MAXDWORD) ? memstatus.ullAvailPageFile :
1229 (memstatus.ullAvailPageFile > MAXLONG) ? MAXLONG : memstatus.ullAvailPageFile;
1230 lpBuffer->dwTotalVirtual = (memstatus.ullTotalVirtual > MAXDWORD) ? memstatus.ullTotalVirtual :
1231 (memstatus.ullTotalVirtual > MAXLONG) ? MAXLONG : memstatus.ullTotalVirtual;
1232 lpBuffer->dwAvailVirtual = (memstatus.ullAvailVirtual > MAXDWORD) ? memstatus.ullAvailVirtual :
1233 (memstatus.ullAvailVirtual > MAXLONG) ? MAXLONG : memstatus.ullAvailVirtual;
1236 /* work around for broken photoshop 4 installer */
1237 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
1238 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;