2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* 0xffff sometimes seems to mean: CURRENT_DS */
23 #include "wine/port.h"
25 #include <sys/types.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
40 #include "wine/winbase16.h"
41 #include "wine/exception.h"
44 #include "selectors.h"
46 #include "stackframe.h"
47 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(global);
53 /* Global arena block */
56 DWORD base; /* Base address (0 if discarded) */
57 DWORD size; /* Size in bytes (0 indicates a free block) */
58 HGLOBAL16 handle; /* Handle for this block */
59 HGLOBAL16 hOwner; /* Owner of this block */
60 BYTE lockCount; /* Count of GlobalFix() calls */
61 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
62 BYTE flags; /* Allocation flags */
63 BYTE selCount; /* Number of selectors allocated for this block */
66 /* Flags definitions */
67 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
68 #define GA_DGROUP 0x04
69 #define GA_DISCARDABLE 0x08
70 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
71 #define GA_DOSMEM 0x20
74 static GLOBALARENA *pGlobalArena = NULL;
75 static int globalArenaSize = 0;
77 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
79 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
80 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
82 /* filter for page-fault exceptions */
83 /* It is possible for a bogus global pointer to cause a */
84 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
86 static WINE_EXCEPTION_FILTER(page_fault)
88 switch (GetExceptionCode()) {
89 case (EXCEPTION_ACCESS_VIOLATION):
90 case (EXCEPTION_PRIV_INSTRUCTION):
91 return EXCEPTION_EXECUTE_HANDLER;
93 return EXCEPTION_CONTINUE_SEARCH;
97 /***********************************************************************
100 * Return the arena for a given selector, growing the arena array if needed.
102 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
104 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
106 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
107 GLOBALARENA *pNewArena = realloc( pGlobalArena,
108 newsize * sizeof(GLOBALARENA) );
109 if (!pNewArena) return 0;
110 pGlobalArena = pNewArena;
111 memset( pGlobalArena + globalArenaSize, 0,
112 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
113 globalArenaSize = newsize;
115 return pGlobalArena + (sel >> __AHSHIFT);
118 void debug_handles(void)
122 for (i = globalArenaSize-1 ; i>=0 ; i--) {
123 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
125 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
133 /***********************************************************************
136 * Create a global heap block for a fixed range of linear memory.
138 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
139 HGLOBAL16 hOwner, unsigned char selflags )
144 /* Allocate the selector(s) */
146 sel = SELECTOR_AllocBlock( ptr, size, selflags );
148 selcount = (size + 0xffff) / 0x10000;
150 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
152 SELECTOR_FreeBlock( sel );
156 /* Fill the arena block */
158 pArena->base = (DWORD)ptr;
159 pArena->size = GetSelectorLimit16(sel) + 1;
160 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
161 pArena->hOwner = hOwner;
162 pArena->lockCount = 0;
163 pArena->pageLockCount = 0;
164 pArena->flags = flags & GA_MOVEABLE;
165 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
166 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
167 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
168 pArena->selCount = selcount;
169 if (selcount > 1) /* clear the next arena blocks */
170 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
172 return pArena->handle;
176 /***********************************************************************
179 * Free a block allocated by GLOBAL_CreateBlock, without touching
180 * the associated linear memory range.
182 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
187 if (!handle) return TRUE;
188 sel = GlobalHandleToSel16( handle );
189 if (!VALID_HANDLE(sel))
191 pArena = GET_ARENA_PTR(sel);
192 SELECTOR_FreeBlock( sel );
193 memset( pArena, 0, sizeof(GLOBALARENA) );
197 /***********************************************************************
200 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
205 if (!handle) return TRUE;
206 sel = GlobalHandleToSel16( handle );
207 if (!VALID_HANDLE(sel))
209 pArena = GET_ARENA_PTR(sel);
210 if (pArena->selCount != 1)
213 pArena->base = (DWORD)ptr;
215 SELECTOR_ReallocBlock( sel, ptr, size );
219 /***********************************************************************
222 * Implementation of GlobalAlloc16()
224 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
229 TRACE("%ld flags=%04x\n", size, flags );
231 /* If size is 0, create a discarded block */
233 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
237 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
238 size = (size + 0x1f) & ~0x1f;
240 /* Allocate the linear memory */
241 ptr = HeapAlloc( GetProcessHeap(), 0, size );
242 /* FIXME: free discardable blocks and try again? */
245 /* Allocate the selector(s) */
247 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
250 HeapFree( GetProcessHeap(), 0, ptr );
254 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
258 /***********************************************************************
259 * GlobalAlloc (KERNEL.15)
260 * GlobalAlloc16 (KERNEL32.24)
265 HGLOBAL16 WINAPI GlobalAlloc16(
266 UINT16 flags, /* [in] Object allocation attributes */
267 DWORD size /* [in] Number of bytes to allocate */
269 HANDLE16 owner = GetCurrentPDB16();
271 if (flags & GMEM_DDESHARE)
272 owner = GetExePtr(owner); /* Make it a module handle */
273 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
277 /***********************************************************************
278 * GlobalReAlloc (KERNEL.16)
283 HGLOBAL16 WINAPI GlobalReAlloc16(
284 HGLOBAL16 handle, /* [in] Handle of global memory object */
285 DWORD size, /* [in] New size of block */
286 UINT16 flags /* [in] How to reallocate object */
291 GLOBALARENA *pArena, *pNewArena;
292 WORD sel = GlobalHandleToSel16( handle );
294 TRACE("%04x %ld flags=%04x\n",
295 handle, size, flags );
296 if (!handle) return 0;
298 if (!VALID_HANDLE(handle)) {
299 WARN("Invalid handle 0x%04x!\n", handle);
302 pArena = GET_ARENA_PTR( handle );
304 /* Discard the block if requested */
306 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
308 if (!(pArena->flags & GA_MOVEABLE) ||
309 !(pArena->flags & GA_DISCARDABLE) ||
310 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
311 if (pArena->flags & GA_DOSMEM)
312 DOSMEM_FreeBlock( (void *)pArena->base );
314 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
317 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
318 * change the selector if we are shrinking the block.
319 * FIXME: shouldn't we keep selectors until the block is deleted?
321 SELECTOR_ReallocBlock( sel, 0, 1 );
327 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
328 if (size == 0) size = 0x20;
329 else size = (size + 0x1f) & ~0x1f;
331 /* Change the flags */
333 if (flags & GMEM_MODIFY)
335 /* Change the flags, leaving GA_DGROUP alone */
336 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
337 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
341 /* Reallocate the linear memory */
343 ptr = (void *)pArena->base;
344 oldsize = pArena->size;
345 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
346 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
348 if (pArena->flags & GA_DOSMEM)
350 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
352 else if(pArena->pageLockCount > 0)
356 newptr = DOSMEM_GetBlock( size, 0 );
359 memcpy( newptr, ptr, oldsize );
360 DOSMEM_FreeBlock( ptr );
367 * if more than one reader (e.g. some pointer has been
368 * given out by GetVDMPointer32W16),
369 * only try to realloc in place
371 newptr = HeapReAlloc( GetProcessHeap(),
372 (pArena->pageLockCount > 0) ?
373 HEAP_REALLOC_IN_PLACE_ONLY : 0,
379 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
380 if (pArena->pageLockCount <1)
382 if (pArena->flags & GA_DOSMEM)
383 DOSMEM_FreeBlock( (void *)pArena->base );
385 HeapFree( GetProcessHeap(), 0, ptr );
386 SELECTOR_FreeBlock( sel );
387 memset( pArena, 0, sizeof(GLOBALARENA) );
393 /* Reallocate the selector(s) */
395 sel = SELECTOR_ReallocBlock( sel, ptr, size );
398 if (pArena->flags & GA_DOSMEM)
399 DOSMEM_FreeBlock( (void *)pArena->base );
401 HeapFree( GetProcessHeap(), 0, ptr );
402 memset( pArena, 0, sizeof(GLOBALARENA) );
405 selcount = (size + 0xffff) / 0x10000;
407 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
409 if (pArena->flags & GA_DOSMEM)
410 DOSMEM_FreeBlock( (void *)pArena->base );
412 HeapFree( GetProcessHeap(), 0, ptr );
413 SELECTOR_FreeBlock( sel );
417 /* Fill the new arena block
418 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
420 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
421 pNewArena->base = (DWORD)ptr;
422 pNewArena->size = GetSelectorLimit16(sel) + 1;
423 pNewArena->selCount = selcount;
424 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
426 if (selcount > 1) /* clear the next arena blocks */
427 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
429 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
430 memset( (char *)ptr + oldsize, 0, size - oldsize );
431 return pNewArena->handle;
435 /***********************************************************************
436 * GlobalFree (KERNEL.17)
437 * GlobalFree16 (KERNEL32.31)
442 HGLOBAL16 WINAPI GlobalFree16(
443 HGLOBAL16 handle /* [in] Handle of global memory object */
447 if (!VALID_HANDLE(handle)) {
448 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
451 ptr = (void *)GET_ARENA_PTR(handle)->base;
453 TRACE("%04x\n", handle );
454 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
455 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
460 /***********************************************************************
461 * GlobalLock (KERNEL.18)
463 * This is the GlobalLock16() function used by 16-bit code.
465 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
467 WORD sel = GlobalHandleToSel16( handle );
468 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
472 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
474 if (!VALID_HANDLE(handle)) {
475 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
478 else if (!GET_ARENA_PTR(handle)->base)
481 GET_ARENA_PTR(handle)->lockCount++;
484 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
485 return MAKESEGPTR( sel, 0 );
489 /**********************************************************************
490 * K32WOWGlobalLock16 (KERNEL32.60)
492 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
494 return WIN16_GlobalLock16( hMem );
498 /***********************************************************************
499 * GlobalLock16 (KERNEL32.25)
501 * This is the GlobalLock16() function used by 32-bit code.
504 * Pointer to first byte of memory block
507 LPVOID WINAPI GlobalLock16(
508 HGLOBAL16 handle /* [in] Handle of global memory object */
510 if (!handle) return 0;
511 if (!VALID_HANDLE(handle))
513 GET_ARENA_PTR(handle)->lockCount++;
514 return (LPVOID)GET_ARENA_PTR(handle)->base;
518 /***********************************************************************
519 * GlobalUnlock (KERNEL.19)
520 * GlobalUnlock16 (KERNEL32.26)
522 * Should the return values be cast to booleans?
525 * TRUE: Object is still locked
526 * FALSE: Object is unlocked
528 BOOL16 WINAPI GlobalUnlock16(
529 HGLOBAL16 handle /* [in] Handle of global memory object */
531 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
532 if (!VALID_HANDLE(handle)) {
533 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
536 TRACE("%04x\n", handle );
537 if (pArena->lockCount) pArena->lockCount--;
538 return pArena->lockCount;
541 /***********************************************************************
542 * GlobalChangeLockCount (KERNEL.365)
544 * This is declared as a register function as it has to preserve
545 * *all* registers, even AX/DX !
548 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
552 GlobalLock16( handle );
553 else if ( delta == -1 )
554 GlobalUnlock16( handle );
556 ERR("(%04X, %d): strange delta value\n", handle, delta );
559 /***********************************************************************
560 * GlobalSize (KERNEL.20)
561 * GlobalSize16 (KERNEL32.32)
563 * Size in bytes of object
566 DWORD WINAPI GlobalSize16(
567 HGLOBAL16 handle /* [in] Handle of global memory object */
569 TRACE("%04x\n", handle );
570 if (!handle) return 0;
571 if (!VALID_HANDLE(handle))
573 return GET_ARENA_PTR(handle)->size;
577 /***********************************************************************
578 * GlobalHandle (KERNEL.21)
580 * Why is GlobalHandleToSel used here with the sel as input?
586 DWORD WINAPI GlobalHandle16(
587 WORD sel /* [in] Address of global memory block */
589 TRACE("%04x\n", sel );
590 if (!VALID_HANDLE(sel)) {
591 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
594 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
597 /***********************************************************************
598 * GlobalHandleNoRIP (KERNEL.159)
600 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
603 for (i = globalArenaSize-1 ; i>=0 ; i--) {
604 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
605 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
611 /***********************************************************************
612 * GlobalFlags (KERNEL.22)
615 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
619 * Value specifying flags and lock count
620 * GMEM_INVALID_HANDLE: Invalid handle
622 UINT16 WINAPI GlobalFlags16(
623 HGLOBAL16 handle /* [in] Handle of global memory object */
627 TRACE("%04x\n", handle );
628 if (!VALID_HANDLE(handle)) {
629 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
632 pArena = GET_ARENA_PTR(handle);
633 return pArena->lockCount |
634 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
635 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
639 /***********************************************************************
640 * LockSegment (KERNEL.23)
642 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
644 TRACE("%04x\n", handle );
645 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
646 if (!VALID_HANDLE(handle)) {
647 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
650 GET_ARENA_PTR(handle)->lockCount++;
655 /***********************************************************************
656 * UnlockSegment (KERNEL.24)
658 void WINAPI UnlockSegment16( HGLOBAL16 handle )
660 TRACE("%04x\n", handle );
661 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
662 if (!VALID_HANDLE(handle)) {
663 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
666 GET_ARENA_PTR(handle)->lockCount--;
667 /* FIXME: this ought to return the lock count in CX (go figure...) */
671 /***********************************************************************
672 * GlobalCompact (KERNEL.25)
674 DWORD WINAPI GlobalCompact16( DWORD desired )
676 return GLOBAL_MAX_ALLOC_SIZE;
680 /***********************************************************************
681 * GlobalFreeAll (KERNEL.26)
683 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
688 pArena = pGlobalArena;
689 for (i = 0; i < globalArenaSize; i++, pArena++)
691 if ((pArena->size != 0) && (pArena->hOwner == owner))
692 GlobalFree16( pArena->handle );
697 /***********************************************************************
698 * GlobalWire (KERNEL.111)
699 * GlobalWire16 (KERNEL32.29)
701 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
703 return WIN16_GlobalLock16( handle );
707 /***********************************************************************
708 * GlobalUnWire (KERNEL.112)
709 * GlobalUnWire16 (KERNEL32.30)
711 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
713 return !GlobalUnlock16( handle );
717 /***********************************************************************
718 * SetSwapAreaSize (KERNEL.106)
720 LONG WINAPI SetSwapAreaSize16( WORD size )
722 FIXME("(%d) - stub!\n", size );
723 return MAKELONG( size, 0xffff );
727 /***********************************************************************
728 * GlobalLRUOldest (KERNEL.163)
730 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
732 TRACE("%04x\n", handle );
733 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
738 /***********************************************************************
739 * GlobalLRUNewest (KERNEL.164)
741 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
743 TRACE("%04x\n", handle );
744 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
749 /***********************************************************************
750 * GetFreeSpace (KERNEL.169)
752 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
755 GlobalMemoryStatus( &ms );
756 return ms.dwAvailVirtual;
759 /***********************************************************************
760 * GlobalDOSAlloc (KERNEL.184)
762 * Address (HW=Paragraph segment; LW=Selector)
764 DWORD WINAPI GlobalDOSAlloc16(
765 DWORD size /* [in] Number of bytes to be allocated */
768 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
772 HMODULE16 hModule = GetModuleHandle16("KERNEL");
776 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
777 pArena = GET_ARENA_PTR(wSelector);
778 pArena->flags |= GA_DOSMEM;
779 return MAKELONG(wSelector,uParagraph);
785 /***********************************************************************
786 * GlobalDOSFree (KERNEL.185)
791 WORD WINAPI GlobalDOSFree16(
792 WORD sel /* [in] Selector */
794 DWORD block = GetSelectorBase(sel);
796 if( block && block < 0x100000 )
798 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
799 if( DOSMEM_FreeBlock( lpBlock ) )
800 GLOBAL_FreeBlock( sel );
807 /***********************************************************************
808 * GlobalPageLock (KERNEL.191)
809 * GlobalSmartPageLock(KERNEL.230)
811 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
813 TRACE("%04x\n", handle );
814 if (!VALID_HANDLE(handle)) {
815 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
818 return ++(GET_ARENA_PTR(handle)->pageLockCount);
822 /***********************************************************************
823 * GlobalPageUnlock (KERNEL.192)
824 * GlobalSmartPageUnlock(KERNEL.231)
826 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
828 TRACE("%04x\n", handle );
829 if (!VALID_HANDLE(handle)) {
830 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
833 return --(GET_ARENA_PTR(handle)->pageLockCount);
837 /***********************************************************************
838 * GlobalFix (KERNEL.197)
839 * GlobalFix16 (KERNEL32.27)
841 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
843 TRACE("%04x\n", handle );
844 if (!VALID_HANDLE(handle)) {
845 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
848 GET_ARENA_PTR(handle)->lockCount++;
850 return GlobalHandleToSel16(handle);
854 /***********************************************************************
855 * GlobalUnfix (KERNEL.198)
856 * GlobalUnfix16 (KERNEL32.28)
858 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
860 TRACE("%04x\n", handle );
861 if (!VALID_HANDLE(handle)) {
862 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
865 GET_ARENA_PTR(handle)->lockCount--;
869 /***********************************************************************
870 * FarSetOwner (KERNEL.403)
872 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
874 if (!VALID_HANDLE(handle)) {
875 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
878 GET_ARENA_PTR(handle)->hOwner = hOwner;
882 /***********************************************************************
883 * FarGetOwner (KERNEL.404)
885 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
887 if (!VALID_HANDLE(handle)) {
888 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
891 return GET_ARENA_PTR(handle)->hOwner;
895 /***********************************************************************
896 * GlobalHandleToSel (TOOLHELP.50)
898 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
900 if (!handle) return 0;
901 if (!VALID_HANDLE(handle)) {
902 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
907 WARN("Program attempted invalid selector conversion\n" );
914 /***********************************************************************
915 * GlobalFirst (TOOLHELP.51)
917 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
919 if (wFlags == GLOBAL_LRU) return FALSE;
921 return GlobalNext16( pGlobal, wFlags );
925 /***********************************************************************
926 * GlobalNext (TOOLHELP.52)
928 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
932 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
933 pArena = pGlobalArena + pGlobal->dwNext;
934 if (wFlags == GLOBAL_FREE) /* only free blocks */
937 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
938 if (pArena->size == 0) break; /* block is free */
939 if (i >= globalArenaSize) return FALSE;
943 pGlobal->dwAddress = pArena->base;
944 pGlobal->dwBlockSize = pArena->size;
945 pGlobal->hBlock = pArena->handle;
946 pGlobal->wcLock = pArena->lockCount;
947 pGlobal->wcPageLock = pArena->pageLockCount;
948 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
949 pGlobal->wHeapPresent = FALSE;
950 pGlobal->hOwner = pArena->hOwner;
951 pGlobal->wType = GT_UNKNOWN;
958 /***********************************************************************
959 * GlobalInfo (TOOLHELP.53)
961 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
966 pInfo->wcItems = globalArenaSize;
967 pInfo->wcItemsFree = 0;
968 pInfo->wcItemsLRU = 0;
969 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
970 if (pArena->size == 0) pInfo->wcItemsFree++;
975 /***********************************************************************
976 * GlobalEntryHandle (TOOLHELP.54)
978 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
980 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
982 pGlobal->dwAddress = pArena->base;
983 pGlobal->dwBlockSize = pArena->size;
984 pGlobal->hBlock = pArena->handle;
985 pGlobal->wcLock = pArena->lockCount;
986 pGlobal->wcPageLock = pArena->pageLockCount;
987 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
988 pGlobal->wHeapPresent = FALSE;
989 pGlobal->hOwner = pArena->hOwner;
990 pGlobal->wType = GT_UNKNOWN;
997 /***********************************************************************
998 * GlobalEntryModule (TOOLHELP.55)
1000 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1003 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1008 /***********************************************************************
1009 * MemManInfo (TOOLHELP.72)
1011 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1013 MEMORYSTATUS status;
1016 * Not unsurprisingly although the documention says you
1017 * _must_ provide the size in the dwSize field, this function
1018 * (under Windows) always fills the structure and returns true.
1020 GlobalMemoryStatus( &status );
1021 info->wPageSize = getpagesize();
1022 info->dwLargestFreeBlock = status.dwAvailVirtual;
1023 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1024 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1025 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1026 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1027 info->dwFreePages = info->dwMaxPagesAvailable;
1028 info->dwTotalPages = info->dwTotalLinearSpace;
1029 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1030 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1034 /***********************************************************************
1035 * GetFreeMemInfo (KERNEL.316)
1037 DWORD WINAPI GetFreeMemInfo16(void)
1040 MemManInfo16( &info );
1041 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1045 * Win32 Global heap functions (GlobalXXX).
1046 * These functions included in Win32 for compatibility with 16 bit Windows
1047 * Especially the moveable blocks and handles are oldish.
1048 * But the ability to directly allocate memory with GPTR and LPTR is widely
1051 * The handle stuff looks horrible, but it's implemented almost like Win95
1056 #define MAGIC_GLOBAL_USED 0x5342
1057 #define GLOBAL_LOCK_MAX 0xFF
1058 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1059 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1060 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
1061 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1062 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1063 /* allign the storage needed for the HGLOBAL on an 8byte boundary thus
1064 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
1065 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
1066 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
1067 * the output jpeg's > 1 MB if not */
1068 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
1070 typedef struct __GLOBAL32_INTERN
1073 LPVOID Pointer WINE_PACKED;
1076 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1079 /***********************************************************************
1080 * GlobalAlloc (KERNEL32.@)
1085 HGLOBAL WINAPI GlobalAlloc(
1086 UINT flags, /* [in] Object allocation attributes */
1087 SIZE_T size /* [in] Number of bytes to allocate */
1089 PGLOBAL32_INTERN pintern;
1093 if(flags&GMEM_ZEROINIT)
1094 hpflags=HEAP_ZERO_MEMORY;
1098 TRACE("() flags=%04x\n", flags );
1100 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1102 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1103 return (HGLOBAL) palloc;
1107 /* HeapLock(heap); */
1109 pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1110 if (!pintern) return 0;
1113 if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE))) {
1114 HeapFree(GetProcessHeap(), 0, pintern);
1117 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1118 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1121 pintern->Pointer=NULL;
1122 pintern->Magic=MAGIC_GLOBAL_USED;
1123 pintern->Flags=flags>>8;
1124 pintern->LockCount=0;
1126 /* HeapUnlock(heap); */
1128 return INTERN_TO_HANDLE(pintern);
1133 /***********************************************************************
1134 * GlobalLock (KERNEL32.@)
1136 * Pointer to first byte of block
1139 LPVOID WINAPI GlobalLock(
1140 HGLOBAL hmem /* [in] Handle of global memory object */
1142 PGLOBAL32_INTERN pintern;
1146 return (LPVOID) hmem;
1148 /* HeapLock(GetProcessHeap()); */
1150 pintern=HANDLE_TO_INTERN(hmem);
1151 if(pintern->Magic==MAGIC_GLOBAL_USED)
1153 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1154 pintern->LockCount++;
1155 palloc=pintern->Pointer;
1159 WARN("invalid handle\n");
1161 SetLastError(ERROR_INVALID_HANDLE);
1163 /* HeapUnlock(GetProcessHeap()); */;
1168 /***********************************************************************
1169 * GlobalUnlock (KERNEL32.@)
1171 * TRUE: Object is still locked
1172 * FALSE: Object is unlocked
1174 BOOL WINAPI GlobalUnlock(
1175 HGLOBAL hmem /* [in] Handle of global memory object */
1177 PGLOBAL32_INTERN pintern;
1180 if (ISPOINTER(hmem)) return FALSE;
1184 /* HeapLock(GetProcessHeap()); */
1185 pintern=HANDLE_TO_INTERN(hmem);
1186 if(pintern->Magic==MAGIC_GLOBAL_USED)
1188 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1189 pintern->LockCount--;
1191 locked = (pintern->LockCount != 0);
1192 if (!locked) SetLastError(NO_ERROR);
1196 WARN("invalid handle\n");
1197 SetLastError(ERROR_INVALID_HANDLE);
1200 /* HeapUnlock(GetProcessHeap()); */
1202 __EXCEPT(page_fault)
1204 ERR("page fault occurred ! Caused by bug ?\n");
1205 SetLastError( ERROR_INVALID_PARAMETER );
1213 /***********************************************************************
1214 * GlobalHandle (KERNEL32.@)
1215 * Returns the handle associated with the specified pointer.
1221 HGLOBAL WINAPI GlobalHandle(
1222 LPCVOID pmem /* [in] Pointer to global memory block */
1225 PGLOBAL32_INTERN maybe_intern;
1230 SetLastError( ERROR_INVALID_PARAMETER );
1238 /* note that if pmem is a pointer to a a block allocated by */
1239 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1241 if (ISPOINTER(pmem)) {
1242 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
1243 handle = (HGLOBAL)pmem; /* valid fixed block */
1246 handle = POINTER_TO_HANDLE(pmem);
1248 handle = (HGLOBAL)pmem;
1250 /* Now test handle either passed in or retrieved from pointer */
1251 maybe_intern = HANDLE_TO_INTERN( handle );
1252 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1253 test = maybe_intern->Pointer;
1254 if (HeapValidate( GetProcessHeap(), 0, (char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
1255 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
1256 break; /* valid moveable block */
1259 SetLastError( ERROR_INVALID_HANDLE );
1261 __EXCEPT(page_fault)
1263 SetLastError( ERROR_INVALID_HANDLE );
1272 /***********************************************************************
1273 * GlobalReAlloc (KERNEL32.@)
1278 HGLOBAL WINAPI GlobalReAlloc(
1279 HGLOBAL hmem, /* [in] Handle of global memory object */
1280 SIZE_T size, /* [in] New size of block */
1281 UINT flags /* [in] How to reallocate object */
1285 PGLOBAL32_INTERN pintern;
1286 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1289 /* HeapLock(heap); */
1290 if(flags & GMEM_MODIFY) /* modify flags */
1292 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1294 /* make a fixed block moveable
1295 * actually only NT is able to do this. But it's soo simple
1299 ERR("GlobalReAlloc32 with null handle!\n");
1300 SetLastError( ERROR_NOACCESS );
1303 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1304 hnew=GlobalAlloc( flags, size);
1305 palloc=GlobalLock(hnew);
1306 memcpy(palloc, (LPVOID) hmem, size);
1310 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1312 /* change the flags to make our block "discardable" */
1313 pintern=HANDLE_TO_INTERN(hmem);
1314 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1319 SetLastError(ERROR_INVALID_PARAMETER);
1327 /* reallocate fixed memory */
1328 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
1332 /* reallocate a moveable block */
1333 pintern=HANDLE_TO_INTERN(hmem);
1336 /* Apparently Windows doesn't care whether the handle is locked at this point */
1337 /* See also the same comment in GlobalFree() */
1338 if(pintern->LockCount>1) {
1339 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1340 SetLastError(ERROR_INVALID_HANDLE);
1346 if(pintern->Pointer)
1348 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
1349 (char *) pintern->Pointer-HGLOBAL_STORAGE,
1350 size+HGLOBAL_STORAGE)) == NULL)
1351 return 0; /* Block still valid */
1352 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1356 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
1359 *(HGLOBAL *)palloc=hmem;
1360 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1365 if(pintern->Pointer)
1367 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
1368 pintern->Pointer=NULL;
1373 /* HeapUnlock(heap); */
1378 /***********************************************************************
1379 * GlobalFree (KERNEL32.@)
1384 HGLOBAL WINAPI GlobalFree(
1385 HGLOBAL hmem /* [in] Handle of global memory object */
1387 PGLOBAL32_INTERN pintern;
1393 if(ISPOINTER(hmem)) /* POINTER */
1395 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1399 /* HeapLock(heap); */
1400 pintern=HANDLE_TO_INTERN(hmem);
1402 if(pintern->Magic==MAGIC_GLOBAL_USED)
1405 /* WIN98 does not make this test. That is you can free a */
1406 /* block you have not unlocked. Go figure!! */
1407 /* if(pintern->LockCount!=0) */
1408 /* SetLastError(ERROR_INVALID_HANDLE); */
1410 if(pintern->Pointer)
1411 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
1413 if(!HeapFree(GetProcessHeap(), 0, pintern))
1416 /* HeapUnlock(heap); */
1419 __EXCEPT(page_fault)
1421 ERR("page fault occurred ! Caused by bug ?\n");
1422 SetLastError( ERROR_INVALID_PARAMETER );
1430 /***********************************************************************
1431 * GlobalSize (KERNEL32.@)
1433 * Size in bytes of the global memory object
1436 SIZE_T WINAPI GlobalSize(
1437 HGLOBAL hmem /* [in] Handle of global memory object */
1440 PGLOBAL32_INTERN pintern;
1444 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1448 /* HeapLock(heap); */
1449 pintern=HANDLE_TO_INTERN(hmem);
1451 if(pintern->Magic==MAGIC_GLOBAL_USED)
1453 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1455 retval=HeapSize(GetProcessHeap(), 0,
1456 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
1457 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
1461 WARN("invalid handle\n");
1464 /* HeapUnlock(heap); */
1466 /* HeapSize returns 0xffffffff on failure */
1467 if (retval == 0xffffffff) retval = 0;
1472 /***********************************************************************
1473 * GlobalWire (KERNEL32.@)
1475 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1477 return GlobalLock( hmem );
1481 /***********************************************************************
1482 * GlobalUnWire (KERNEL32.@)
1484 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1486 return GlobalUnlock( hmem);
1490 /***********************************************************************
1491 * GlobalFix (KERNEL32.@)
1493 VOID WINAPI GlobalFix(HGLOBAL hmem)
1499 /***********************************************************************
1500 * GlobalUnfix (KERNEL32.@)
1502 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1504 GlobalUnlock( hmem);
1508 /***********************************************************************
1509 * GlobalFlags (KERNEL32.@)
1510 * Returns information about the specified global memory object
1513 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1516 * Value specifying allocation flags and lock count
1517 * GMEM_INVALID_HANDLE: Failure
1519 UINT WINAPI GlobalFlags(
1520 HGLOBAL hmem /* [in] Handle to global memory object */
1523 PGLOBAL32_INTERN pintern;
1531 /* HeapLock(GetProcessHeap()); */
1532 pintern=HANDLE_TO_INTERN(hmem);
1533 if(pintern->Magic==MAGIC_GLOBAL_USED)
1535 retval=pintern->LockCount + (pintern->Flags<<8);
1536 if(pintern->Pointer==0)
1537 retval|= GMEM_DISCARDED;
1541 WARN("Invalid handle: %p\n", hmem);
1544 /* HeapUnlock(GetProcessHeap()); */
1550 /***********************************************************************
1551 * GlobalCompact (KERNEL32.@)
1553 SIZE_T WINAPI GlobalCompact( DWORD minfree )
1555 return 0; /* GlobalCompact does nothing in Win32 */
1559 /***********************************************************************
1560 * GlobalMemoryStatus (KERNEL32.@)
1561 * Provides information about the status of the memory, so apps can tell
1562 * roughly how much they are able to allocate
1567 VOID WINAPI GlobalMemoryStatus(
1568 LPMEMORYSTATUS lpmem
1570 static MEMORYSTATUS cached_memstatus;
1571 static int cache_lastchecked = 0;
1576 #if defined(__FreeBSD__) || defined(__NetBSD__)
1579 int mib[2] = { CTL_HW };
1581 if (time(NULL)==cache_lastchecked) {
1582 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1585 cache_lastchecked = time(NULL);
1587 lpmem->dwMemoryLoad = 0;
1588 lpmem->dwTotalPhys = 16*1024*1024;
1589 lpmem->dwAvailPhys = 16*1024*1024;
1590 lpmem->dwTotalPageFile = 16*1024*1024;
1591 lpmem->dwAvailPageFile = 16*1024*1024;
1594 f = fopen( "/proc/meminfo", "r" );
1598 int total, used, free, shared, buffers, cached;
1600 lpmem->dwLength = sizeof(MEMORYSTATUS);
1601 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1602 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1603 while (fgets( buffer, sizeof(buffer), f ))
1605 /* old style /proc/meminfo ... */
1606 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1608 lpmem->dwTotalPhys += total;
1609 lpmem->dwAvailPhys += free + buffers + cached;
1611 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1613 lpmem->dwTotalPageFile += total;
1614 lpmem->dwAvailPageFile += free;
1617 /* new style /proc/meminfo ... */
1618 if (sscanf(buffer, "MemTotal: %d", &total))
1619 lpmem->dwTotalPhys = total*1024;
1620 if (sscanf(buffer, "MemFree: %d", &free))
1621 lpmem->dwAvailPhys = free*1024;
1622 if (sscanf(buffer, "SwapTotal: %d", &total))
1623 lpmem->dwTotalPageFile = total*1024;
1624 if (sscanf(buffer, "SwapFree: %d", &free))
1625 lpmem->dwAvailPageFile = free*1024;
1626 if (sscanf(buffer, "Buffers: %d", &buffers))
1627 lpmem->dwAvailPhys += buffers*1024;
1628 if (sscanf(buffer, "Cached: %d", &cached))
1629 lpmem->dwAvailPhys += cached*1024;
1633 if (lpmem->dwTotalPhys)
1635 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1636 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1637 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1638 / (TotalPhysical / 100);
1641 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1642 mib[1] = HW_PHYSMEM;
1643 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1644 tmp = malloc(size_sys * sizeof(int));
1645 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1648 lpmem->dwTotalPhys = *tmp;
1650 mib[1] = HW_USERMEM;
1651 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1652 tmp = malloc(size_sys * sizeof(int));
1653 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1656 lpmem->dwAvailPhys = *tmp;
1657 lpmem->dwTotalPageFile = *tmp;
1658 lpmem->dwAvailPageFile = *tmp;
1659 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1662 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1663 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1664 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1665 lpmem->dwMemoryLoad = 0;
1671 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1672 * is more than 2GB of physical memory.
1674 if (lpmem->dwTotalPhys>2U*1024*1024*1024)
1676 lpmem->dwTotalPhys=2U*1024*1024*1024;
1677 lpmem->dwAvailPhys=2U*1024*1024*1024;
1680 /* FIXME: should do something for other systems */
1682 lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1683 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1684 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1685 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1687 /* it appears some memory display programs want to divide by these values */
1688 if(lpmem->dwTotalPageFile==0)
1689 lpmem->dwTotalPageFile++;
1691 if(lpmem->dwAvailPageFile==0)
1692 lpmem->dwAvailPageFile++;
1694 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1695 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1696 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1697 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1698 lpmem->dwAvailVirtual);
1701 /***********************************************************************
1702 * GlobalMemoryStatusEx (KERNEL32.@)
1703 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1708 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer ) {
1709 MEMORYSTATUS memstatus;
1711 /* Because GlobalMemoryStatusEx is identical to GlobalMemoryStatus save
1712 for one extra field in the struct, and the lack of a bug, we simply
1713 call GlobalMemoryStatus and copy the values across. */
1714 FIXME("we should emulate the 4GB bug here, as per MSDN\n");
1715 GlobalMemoryStatus(&memstatus);
1716 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1717 lpBuffer->ullTotalPhys = memstatus.dwTotalPhys;
1718 lpBuffer->ullAvailPhys = memstatus.dwAvailPhys;
1719 lpBuffer->ullTotalPageFile = memstatus.dwTotalPageFile;
1720 lpBuffer->ullAvailPageFile = memstatus.dwAvailPageFile;
1721 lpBuffer->ullTotalVirtual = memstatus.dwTotalVirtual;
1722 lpBuffer->ullAvailVirtual = memstatus.dwAvailVirtual;
1723 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1724 memory in the extended portion of the virtual address space of the calling
1727 However, I don't know what this means, so set it to zero :(
1729 lpBuffer->ullAvailExtendedVirtual = 0;
1733 /***********************************************************************
1734 * A20Proc (KERNEL.165)
1735 * A20_Proc (SYSTEM.20)
1737 void WINAPI A20Proc16( WORD unused )
1739 /* this is also a NOP in Windows */
1742 /***********************************************************************
1743 * LimitEMSPages (KERNEL.156)
1745 DWORD WINAPI LimitEMSPages16( DWORD unused )