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"
45 #include "selectors.h"
47 #include "stackframe.h"
48 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(global
);
54 /* Global arena block */
57 DWORD base
; /* Base address (0 if discarded) */
58 DWORD size
; /* Size in bytes (0 indicates a free block) */
59 HGLOBAL16 handle
; /* Handle for this block */
60 HGLOBAL16 hOwner
; /* Owner of this block */
61 BYTE lockCount
; /* Count of GlobalFix() calls */
62 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
63 BYTE flags
; /* Allocation flags */
64 BYTE selCount
; /* Number of selectors allocated for this block */
67 /* Flags definitions */
68 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
69 #define GA_DGROUP 0x04
70 #define GA_DISCARDABLE 0x08
71 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
72 #define GA_DOSMEM 0x20
75 static GLOBALARENA
*pGlobalArena
= NULL
;
76 static int globalArenaSize
= 0;
78 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
80 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
81 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
83 /* filter for page-fault exceptions */
84 /* It is possible for a bogus global pointer to cause a */
85 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
87 static WINE_EXCEPTION_FILTER(page_fault
)
89 switch (GetExceptionCode()) {
90 case (EXCEPTION_ACCESS_VIOLATION
):
91 case (EXCEPTION_PRIV_INSTRUCTION
):
92 return EXCEPTION_EXECUTE_HANDLER
;
94 return EXCEPTION_CONTINUE_SEARCH
;
98 /***********************************************************************
101 * Return the arena for a given selector, growing the arena array if needed.
103 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
105 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
107 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
108 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
109 newsize
* sizeof(GLOBALARENA
) );
110 if (!pNewArena
) return 0;
111 pGlobalArena
= pNewArena
;
112 memset( pGlobalArena
+ globalArenaSize
, 0,
113 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
114 globalArenaSize
= newsize
;
116 return pGlobalArena
+ (sel
>> __AHSHIFT
);
119 void debug_handles(void)
123 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
124 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
126 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
134 /***********************************************************************
137 * Create a global heap block for a fixed range of linear memory.
139 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
140 HGLOBAL16 hOwner
, unsigned char selflags
)
145 /* Allocate the selector(s) */
147 sel
= SELECTOR_AllocBlock( ptr
, size
, selflags
);
149 selcount
= (size
+ 0xffff) / 0x10000;
151 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
153 SELECTOR_FreeBlock( sel
);
157 /* Fill the arena block */
159 pArena
->base
= (DWORD
)ptr
;
160 pArena
->size
= GetSelectorLimit16(sel
) + 1;
161 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
162 pArena
->hOwner
= hOwner
;
163 pArena
->lockCount
= 0;
164 pArena
->pageLockCount
= 0;
165 pArena
->flags
= flags
& GA_MOVEABLE
;
166 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
167 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
168 if (!(selflags
& (WINE_LDT_FLAGS_CODE
^WINE_LDT_FLAGS_DATA
))) pArena
->flags
|= GA_DGROUP
;
169 pArena
->selCount
= selcount
;
170 if (selcount
> 1) /* clear the next arena blocks */
171 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
173 return pArena
->handle
;
177 /***********************************************************************
180 * Free a block allocated by GLOBAL_CreateBlock, without touching
181 * the associated linear memory range.
183 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
188 if (!handle
) return TRUE
;
189 sel
= GlobalHandleToSel16( handle
);
190 if (!VALID_HANDLE(sel
))
192 pArena
= GET_ARENA_PTR(sel
);
193 SELECTOR_FreeBlock( sel
);
194 memset( pArena
, 0, sizeof(GLOBALARENA
) );
198 /***********************************************************************
201 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
206 if (!handle
) return TRUE
;
207 sel
= GlobalHandleToSel16( handle
);
208 if (!VALID_HANDLE(sel
))
210 pArena
= GET_ARENA_PTR(sel
);
211 if (pArena
->selCount
!= 1)
214 pArena
->base
= (DWORD
)ptr
;
216 SELECTOR_ReallocBlock( sel
, ptr
, size
);
220 /***********************************************************************
223 * Implementation of GlobalAlloc16()
225 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
, unsigned char selflags
)
230 TRACE("%ld flags=%04x\n", size
, flags
);
232 /* If size is 0, create a discarded block */
234 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, selflags
);
238 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
239 size
= (size
+ 0x1f) & ~0x1f;
241 /* Allocate the linear memory */
242 ptr
= HeapAlloc( GetProcessHeap(), 0, size
);
243 /* FIXME: free discardable blocks and try again? */
246 /* Allocate the selector(s) */
248 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
, selflags
);
251 HeapFree( GetProcessHeap(), 0, ptr
);
255 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
259 /***********************************************************************
260 * GlobalAlloc (KERNEL.15)
261 * GlobalAlloc16 (KERNEL32.24)
266 HGLOBAL16 WINAPI
GlobalAlloc16(
267 UINT16 flags
, /* [in] Object allocation attributes */
268 DWORD size
/* [in] Number of bytes to allocate */
270 HANDLE16 owner
= GetCurrentPDB16();
272 if (flags
& GMEM_DDESHARE
)
273 owner
= GetExePtr(owner
); /* Make it a module handle */
274 return GLOBAL_Alloc( flags
, size
, owner
, WINE_LDT_FLAGS_DATA
);
278 /***********************************************************************
279 * GlobalReAlloc (KERNEL.16)
284 HGLOBAL16 WINAPI
GlobalReAlloc16(
285 HGLOBAL16 handle
, /* [in] Handle of global memory object */
286 DWORD size
, /* [in] New size of block */
287 UINT16 flags
/* [in] How to reallocate object */
292 GLOBALARENA
*pArena
, *pNewArena
;
293 WORD sel
= GlobalHandleToSel16( handle
);
295 TRACE("%04x %ld flags=%04x\n",
296 handle
, size
, flags
);
297 if (!handle
) return 0;
299 if (!VALID_HANDLE(handle
)) {
300 WARN("Invalid handle 0x%04x!\n", handle
);
303 pArena
= GET_ARENA_PTR( handle
);
305 /* Discard the block if requested */
307 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
309 if (!(pArena
->flags
& GA_MOVEABLE
) ||
310 !(pArena
->flags
& GA_DISCARDABLE
) ||
311 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
312 if (pArena
->flags
& GA_DOSMEM
)
313 DOSMEM_FreeBlock( (void *)pArena
->base
);
315 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
318 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
319 * change the selector if we are shrinking the block.
320 * FIXME: shouldn't we keep selectors until the block is deleted?
322 SELECTOR_ReallocBlock( sel
, 0, 1 );
328 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
329 if (size
== 0) size
= 0x20;
330 else size
= (size
+ 0x1f) & ~0x1f;
332 /* Change the flags */
334 if (flags
& GMEM_MODIFY
)
336 /* Change the flags, leaving GA_DGROUP alone */
337 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
338 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
342 /* Reallocate the linear memory */
344 ptr
= (void *)pArena
->base
;
345 oldsize
= pArena
->size
;
346 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr
,oldsize
,size
);
347 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
349 if (pArena
->flags
& GA_DOSMEM
)
351 if (DOSMEM_ResizeBlock(ptr
, size
, TRUE
) == size
)
353 else if(pArena
->pageLockCount
> 0)
357 newptr
= DOSMEM_GetBlock( size
, 0 );
360 memcpy( newptr
, ptr
, oldsize
);
361 DOSMEM_FreeBlock( ptr
);
368 * if more than one reader (e.g. some pointer has been
369 * given out by GetVDMPointer32W16),
370 * only try to realloc in place
372 newptr
= HeapReAlloc( GetProcessHeap(),
373 (pArena
->pageLockCount
> 0) ?
374 HEAP_REALLOC_IN_PLACE_ONLY
: 0,
380 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
381 if (pArena
->pageLockCount
<1)
383 if (pArena
->flags
& GA_DOSMEM
)
384 DOSMEM_FreeBlock( (void *)pArena
->base
);
386 HeapFree( GetProcessHeap(), 0, ptr
);
387 SELECTOR_FreeBlock( sel
);
388 memset( pArena
, 0, sizeof(GLOBALARENA
) );
394 /* Reallocate the selector(s) */
396 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
399 if (pArena
->flags
& GA_DOSMEM
)
400 DOSMEM_FreeBlock( (void *)pArena
->base
);
402 HeapFree( GetProcessHeap(), 0, ptr
);
403 memset( pArena
, 0, sizeof(GLOBALARENA
) );
406 selcount
= (size
+ 0xffff) / 0x10000;
408 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
410 if (pArena
->flags
& GA_DOSMEM
)
411 DOSMEM_FreeBlock( (void *)pArena
->base
);
413 HeapFree( GetProcessHeap(), 0, ptr
);
414 SELECTOR_FreeBlock( sel
);
418 /* Fill the new arena block
419 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
421 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
422 pNewArena
->base
= (DWORD
)ptr
;
423 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
424 pNewArena
->selCount
= selcount
;
425 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
427 if (selcount
> 1) /* clear the next arena blocks */
428 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
430 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
431 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
432 return pNewArena
->handle
;
436 /***********************************************************************
437 * GlobalFree (KERNEL.17)
438 * GlobalFree16 (KERNEL32.31)
443 HGLOBAL16 WINAPI
GlobalFree16(
444 HGLOBAL16 handle
/* [in] Handle of global memory object */
448 if (!VALID_HANDLE(handle
)) {
449 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
452 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
454 TRACE("%04x\n", handle
);
455 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
456 if (ptr
) HeapFree( GetProcessHeap(), 0, ptr
);
461 /***********************************************************************
462 * GlobalLock (KERNEL.18)
464 * This is the GlobalLock16() function used by 16-bit code.
466 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
468 WORD sel
= GlobalHandleToSel16( handle
);
469 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
473 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
475 if (!VALID_HANDLE(handle
)) {
476 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
479 else if (!GET_ARENA_PTR(handle
)->base
)
482 GET_ARENA_PTR(handle
)->lockCount
++;
485 CURRENT_STACK16
->ecx
= sel
; /* selector must be returned in CX as well */
486 return MAKESEGPTR( sel
, 0 );
490 /**********************************************************************
491 * K32WOWGlobalLock16 (KERNEL32.60)
493 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 hMem
)
495 return WIN16_GlobalLock16( hMem
);
499 /***********************************************************************
500 * GlobalLock16 (KERNEL32.25)
502 * This is the GlobalLock16() function used by 32-bit code.
505 * Pointer to first byte of memory block
508 LPVOID WINAPI
GlobalLock16(
509 HGLOBAL16 handle
/* [in] Handle of global memory object */
511 if (!handle
) return 0;
512 if (!VALID_HANDLE(handle
))
514 GET_ARENA_PTR(handle
)->lockCount
++;
515 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
519 /***********************************************************************
520 * GlobalUnlock (KERNEL.19)
521 * GlobalUnlock16 (KERNEL32.26)
523 * Should the return values be cast to booleans?
526 * TRUE: Object is still locked
527 * FALSE: Object is unlocked
529 BOOL16 WINAPI
GlobalUnlock16(
530 HGLOBAL16 handle
/* [in] Handle of global memory object */
532 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
533 if (!VALID_HANDLE(handle
)) {
534 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
537 TRACE("%04x\n", handle
);
538 if (pArena
->lockCount
) pArena
->lockCount
--;
539 return pArena
->lockCount
;
542 /***********************************************************************
543 * GlobalChangeLockCount (KERNEL.365)
545 * This is declared as a register function as it has to preserve
546 * *all* registers, even AX/DX !
549 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
553 GlobalLock16( handle
);
554 else if ( delta
== -1 )
555 GlobalUnlock16( handle
);
557 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
560 /***********************************************************************
561 * GlobalSize (KERNEL.20)
562 * GlobalSize16 (KERNEL32.32)
564 * Size in bytes of object
567 DWORD WINAPI
GlobalSize16(
568 HGLOBAL16 handle
/* [in] Handle of global memory object */
570 TRACE("%04x\n", handle
);
571 if (!handle
) return 0;
572 if (!VALID_HANDLE(handle
))
574 return GET_ARENA_PTR(handle
)->size
;
578 /***********************************************************************
579 * GlobalHandle (KERNEL.21)
581 * Why is GlobalHandleToSel used here with the sel as input?
587 DWORD WINAPI
GlobalHandle16(
588 WORD sel
/* [in] Address of global memory block */
590 TRACE("%04x\n", sel
);
591 if (!VALID_HANDLE(sel
)) {
592 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
595 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
598 /***********************************************************************
599 * GlobalHandleNoRIP (KERNEL.159)
601 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
604 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
605 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
606 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
612 /***********************************************************************
613 * GlobalFlags (KERNEL.22)
616 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
620 * Value specifying flags and lock count
621 * GMEM_INVALID_HANDLE: Invalid handle
623 UINT16 WINAPI
GlobalFlags16(
624 HGLOBAL16 handle
/* [in] Handle of global memory object */
628 TRACE("%04x\n", handle
);
629 if (!VALID_HANDLE(handle
)) {
630 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
633 pArena
= GET_ARENA_PTR(handle
);
634 return pArena
->lockCount
|
635 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
636 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
640 /***********************************************************************
641 * LockSegment (KERNEL.23)
643 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
645 TRACE("%04x\n", handle
);
646 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
647 if (!VALID_HANDLE(handle
)) {
648 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
651 GET_ARENA_PTR(handle
)->lockCount
++;
656 /***********************************************************************
657 * UnlockSegment (KERNEL.24)
659 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
661 TRACE("%04x\n", handle
);
662 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
663 if (!VALID_HANDLE(handle
)) {
664 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
667 GET_ARENA_PTR(handle
)->lockCount
--;
668 /* FIXME: this ought to return the lock count in CX (go figure...) */
672 /***********************************************************************
673 * GlobalCompact (KERNEL.25)
675 DWORD WINAPI
GlobalCompact16( DWORD desired
)
677 return GLOBAL_MAX_ALLOC_SIZE
;
681 /***********************************************************************
682 * GlobalFreeAll (KERNEL.26)
684 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
689 pArena
= pGlobalArena
;
690 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
692 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
693 GlobalFree16( pArena
->handle
);
698 /***********************************************************************
699 * GlobalWire (KERNEL.111)
700 * GlobalWire16 (KERNEL32.29)
702 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
704 return WIN16_GlobalLock16( handle
);
708 /***********************************************************************
709 * GlobalUnWire (KERNEL.112)
710 * GlobalUnWire16 (KERNEL32.30)
712 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
714 return !GlobalUnlock16( handle
);
718 /***********************************************************************
719 * SetSwapAreaSize (KERNEL.106)
721 LONG WINAPI
SetSwapAreaSize16( WORD size
)
723 FIXME("(%d) - stub!\n", size
);
724 return MAKELONG( size
, 0xffff );
728 /***********************************************************************
729 * GlobalLRUOldest (KERNEL.163)
731 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
733 TRACE("%04x\n", handle
);
734 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
739 /***********************************************************************
740 * GlobalLRUNewest (KERNEL.164)
742 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
744 TRACE("%04x\n", handle
);
745 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
750 /***********************************************************************
751 * GetFreeSpace (KERNEL.169)
753 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
756 GlobalMemoryStatus( &ms
);
757 return ms
.dwAvailVirtual
;
760 /***********************************************************************
761 * GlobalDOSAlloc (KERNEL.184)
763 * Address (HW=Paragraph segment; LW=Selector)
765 DWORD WINAPI
GlobalDOSAlloc16(
766 DWORD size
/* [in] Number of bytes to be allocated */
769 LPVOID lpBlock
= DOSMEM_GetBlock( size
, &uParagraph
);
773 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
777 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
778 pArena
= GET_ARENA_PTR(wSelector
);
779 pArena
->flags
|= GA_DOSMEM
;
780 return MAKELONG(wSelector
,uParagraph
);
786 /***********************************************************************
787 * GlobalDOSFree (KERNEL.185)
792 WORD WINAPI
GlobalDOSFree16(
793 WORD sel
/* [in] Selector */
795 DWORD block
= GetSelectorBase(sel
);
797 if( block
&& block
< 0x100000 )
799 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
800 if( DOSMEM_FreeBlock( lpBlock
) )
801 GLOBAL_FreeBlock( sel
);
808 /***********************************************************************
809 * GlobalPageLock (KERNEL.191)
810 * GlobalSmartPageLock(KERNEL.230)
812 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
814 TRACE("%04x\n", handle
);
815 if (!VALID_HANDLE(handle
)) {
816 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
819 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
823 /***********************************************************************
824 * GlobalPageUnlock (KERNEL.192)
825 * GlobalSmartPageUnlock(KERNEL.231)
827 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
829 TRACE("%04x\n", handle
);
830 if (!VALID_HANDLE(handle
)) {
831 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
834 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
838 /***********************************************************************
839 * GlobalFix (KERNEL.197)
840 * GlobalFix16 (KERNEL32.27)
842 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
844 TRACE("%04x\n", handle
);
845 if (!VALID_HANDLE(handle
)) {
846 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
849 GET_ARENA_PTR(handle
)->lockCount
++;
851 return GlobalHandleToSel16(handle
);
855 /***********************************************************************
856 * GlobalUnfix (KERNEL.198)
857 * GlobalUnfix16 (KERNEL32.28)
859 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
861 TRACE("%04x\n", handle
);
862 if (!VALID_HANDLE(handle
)) {
863 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
866 GET_ARENA_PTR(handle
)->lockCount
--;
870 /***********************************************************************
871 * FarSetOwner (KERNEL.403)
873 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
875 if (!VALID_HANDLE(handle
)) {
876 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
879 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
883 /***********************************************************************
884 * FarGetOwner (KERNEL.404)
886 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
888 if (!VALID_HANDLE(handle
)) {
889 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
892 return GET_ARENA_PTR(handle
)->hOwner
;
896 /***********************************************************************
897 * GlobalHandleToSel (TOOLHELP.50)
899 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
901 if (!handle
) return 0;
902 if (!VALID_HANDLE(handle
)) {
903 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
908 WARN("Program attempted invalid selector conversion\n" );
915 /***********************************************************************
916 * GlobalFirst (TOOLHELP.51)
918 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
920 if (wFlags
== GLOBAL_LRU
) return FALSE
;
922 return GlobalNext16( pGlobal
, wFlags
);
926 /***********************************************************************
927 * GlobalNext (TOOLHELP.52)
929 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
933 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
934 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
935 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
938 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
939 if (pArena
->size
== 0) break; /* block is free */
940 if (i
>= globalArenaSize
) return FALSE
;
944 pGlobal
->dwAddress
= pArena
->base
;
945 pGlobal
->dwBlockSize
= pArena
->size
;
946 pGlobal
->hBlock
= pArena
->handle
;
947 pGlobal
->wcLock
= pArena
->lockCount
;
948 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
949 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
950 pGlobal
->wHeapPresent
= FALSE
;
951 pGlobal
->hOwner
= pArena
->hOwner
;
952 pGlobal
->wType
= GT_UNKNOWN
;
959 /***********************************************************************
960 * GlobalInfo (TOOLHELP.53)
962 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
967 pInfo
->wcItems
= globalArenaSize
;
968 pInfo
->wcItemsFree
= 0;
969 pInfo
->wcItemsLRU
= 0;
970 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
971 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
976 /***********************************************************************
977 * GlobalEntryHandle (TOOLHELP.54)
979 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
981 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
983 pGlobal
->dwAddress
= pArena
->base
;
984 pGlobal
->dwBlockSize
= pArena
->size
;
985 pGlobal
->hBlock
= pArena
->handle
;
986 pGlobal
->wcLock
= pArena
->lockCount
;
987 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
988 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
989 pGlobal
->wHeapPresent
= FALSE
;
990 pGlobal
->hOwner
= pArena
->hOwner
;
991 pGlobal
->wType
= GT_UNKNOWN
;
998 /***********************************************************************
999 * GlobalEntryModule (TOOLHELP.55)
1001 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
1004 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
1009 /***********************************************************************
1010 * MemManInfo (TOOLHELP.72)
1012 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
1014 MEMORYSTATUS status
;
1017 * Not unsurprisingly although the documention says you
1018 * _must_ provide the size in the dwSize field, this function
1019 * (under Windows) always fills the structure and returns true.
1021 GlobalMemoryStatus( &status
);
1022 info
->wPageSize
= getpagesize();
1023 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1024 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1025 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1026 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1027 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1028 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1029 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1030 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1031 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1035 /***********************************************************************
1036 * GetFreeMemInfo (KERNEL.316)
1038 DWORD WINAPI
GetFreeMemInfo16(void)
1041 MemManInfo16( &info
);
1042 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1046 * Win32 Global heap functions (GlobalXXX).
1047 * These functions included in Win32 for compatibility with 16 bit Windows
1048 * Especially the moveable blocks and handles are oldish.
1049 * But the ability to directly allocate memory with GPTR and LPTR is widely
1052 * The handle stuff looks horrible, but it's implemented almost like Win95
1057 #define MAGIC_GLOBAL_USED 0x5342
1058 #define GLOBAL_LOCK_MAX 0xFF
1059 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1060 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1061 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
1062 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
1063 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
1064 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
1065 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
1066 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
1067 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
1068 * the output jpeg's > 1 MB if not */
1069 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
1071 typedef struct __GLOBAL32_INTERN
1074 LPVOID Pointer WINE_PACKED
;
1077 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
1080 /***********************************************************************
1081 * GlobalAlloc (KERNEL32.@)
1086 HGLOBAL WINAPI
GlobalAlloc(
1087 UINT flags
, /* [in] Object allocation attributes */
1088 SIZE_T size
/* [in] Number of bytes to allocate */
1090 PGLOBAL32_INTERN pintern
;
1094 if(flags
&GMEM_ZEROINIT
)
1095 hpflags
=HEAP_ZERO_MEMORY
;
1099 TRACE("() flags=%04x\n", flags
);
1101 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1103 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1104 return (HGLOBAL
) palloc
;
1108 RtlLockHeap(GetProcessHeap());
1110 pintern
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1113 pintern
->Magic
= MAGIC_GLOBAL_USED
;
1114 pintern
->Flags
= flags
>> 8;
1115 pintern
->LockCount
= 0;
1119 palloc
= HeapAlloc(GetProcessHeap(), hpflags
, size
+HGLOBAL_STORAGE
);
1122 HeapFree(GetProcessHeap(), 0, pintern
);
1127 *(HGLOBAL
*)palloc
= INTERN_TO_HANDLE(pintern
);
1128 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
1132 pintern
->Pointer
= NULL
;
1135 RtlUnlockHeap(GetProcessHeap());
1136 return pintern
? INTERN_TO_HANDLE(pintern
) : 0;
1141 /***********************************************************************
1142 * GlobalLock (KERNEL32.@)
1144 * Pointer to first byte of block
1147 LPVOID WINAPI
GlobalLock(
1148 HGLOBAL hmem
/* [in] Handle of global memory object */
1151 PGLOBAL32_INTERN pintern
;
1154 if (ISPOINTER(hmem
))
1155 return IsBadReadPtr(hmem
, 1) ? NULL
: hmem
;
1157 RtlLockHeap(GetProcessHeap());
1160 pintern
= HANDLE_TO_INTERN(hmem
);
1161 if (pintern
->Magic
== MAGIC_GLOBAL_USED
)
1163 if (pintern
->LockCount
< GLOBAL_LOCK_MAX
)
1164 pintern
->LockCount
++;
1165 palloc
= pintern
->Pointer
;
1169 WARN("invalid handle %p\n", hmem
);
1171 SetLastError(ERROR_INVALID_HANDLE
);
1174 __EXCEPT(page_fault
)
1176 WARN("page fault on %p\n", hmem
);
1178 SetLastError(ERROR_INVALID_HANDLE
);
1181 RtlUnlockHeap(GetProcessHeap());
1186 /***********************************************************************
1187 * GlobalUnlock (KERNEL32.@)
1189 * TRUE: Object is still locked
1190 * FALSE: Object is unlocked
1192 BOOL WINAPI
GlobalUnlock(
1193 HGLOBAL hmem
/* [in] Handle of global memory object */
1195 PGLOBAL32_INTERN pintern
;
1198 if (ISPOINTER(hmem
)) return FALSE
;
1200 RtlLockHeap(GetProcessHeap());
1203 pintern
=HANDLE_TO_INTERN(hmem
);
1204 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1206 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1207 pintern
->LockCount
--;
1209 locked
= (pintern
->LockCount
!= 0);
1210 if (!locked
) SetLastError(NO_ERROR
);
1214 WARN("invalid handle\n");
1215 SetLastError(ERROR_INVALID_HANDLE
);
1219 __EXCEPT(page_fault
)
1221 ERR("page fault occurred ! Caused by bug ?\n");
1222 SetLastError( ERROR_INVALID_PARAMETER
);
1226 RtlUnlockHeap(GetProcessHeap());
1231 /***********************************************************************
1232 * GlobalHandle (KERNEL32.@)
1233 * Returns the handle associated with the specified pointer.
1239 HGLOBAL WINAPI
GlobalHandle(
1240 LPCVOID pmem
/* [in] Pointer to global memory block */
1243 PGLOBAL32_INTERN maybe_intern
;
1248 SetLastError( ERROR_INVALID_PARAMETER
);
1252 RtlLockHeap(GetProcessHeap());
1257 /* note that if pmem is a pointer to a a block allocated by */
1258 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1260 if (ISPOINTER(pmem
)) {
1261 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
1262 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
1265 handle
= POINTER_TO_HANDLE(pmem
);
1267 handle
= (HGLOBAL
)pmem
;
1269 /* Now test handle either passed in or retrieved from pointer */
1270 maybe_intern
= HANDLE_TO_INTERN( handle
);
1271 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1272 test
= maybe_intern
->Pointer
;
1273 if (HeapValidate( GetProcessHeap(), 0, (char *)test
- HGLOBAL_STORAGE
) && /* obj(-handle) valid arena? */
1274 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
1275 break; /* valid moveable block */
1278 SetLastError( ERROR_INVALID_HANDLE
);
1280 __EXCEPT(page_fault
)
1282 SetLastError( ERROR_INVALID_HANDLE
);
1286 RtlUnlockHeap(GetProcessHeap());
1292 /***********************************************************************
1293 * GlobalReAlloc (KERNEL32.@)
1298 HGLOBAL WINAPI
GlobalReAlloc(
1299 HGLOBAL hmem
, /* [in] Handle of global memory object */
1300 SIZE_T size
, /* [in] New size of block */
1301 UINT flags
/* [in] How to reallocate object */
1305 PGLOBAL32_INTERN pintern
;
1306 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1309 RtlLockHeap(GetProcessHeap());
1310 if(flags
& GMEM_MODIFY
) /* modify flags */
1312 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1314 /* make a fixed block moveable
1315 * actually only NT is able to do this. But it's soo simple
1319 WARN("GlobalReAlloc with null handle!\n");
1320 SetLastError( ERROR_NOACCESS
);
1325 size
= HeapSize(GetProcessHeap(), 0, (LPVOID
)hmem
);
1326 hnew
= GlobalAlloc(flags
, size
);
1327 palloc
= GlobalLock(hnew
);
1328 memcpy(palloc
, (LPVOID
)hmem
, size
);
1333 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1335 /* change the flags to make our block "discardable" */
1336 pintern
=HANDLE_TO_INTERN(hmem
);
1337 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1342 SetLastError(ERROR_INVALID_PARAMETER
);
1350 /* reallocate fixed memory */
1351 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), heap_flags
, (LPVOID
) hmem
, size
);
1355 /* reallocate a moveable block */
1356 pintern
=HANDLE_TO_INTERN(hmem
);
1359 /* Apparently Windows doesn't care whether the handle is locked at this point */
1360 /* See also the same comment in GlobalFree() */
1361 if(pintern
->LockCount
>1) {
1362 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1363 SetLastError(ERROR_INVALID_HANDLE
);
1369 if(pintern
->Pointer
)
1371 if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
1372 (char *) pintern
->Pointer
-HGLOBAL_STORAGE
,
1373 size
+HGLOBAL_STORAGE
)) == NULL
)
1374 hnew
= 0; /* Block still valid */
1376 pintern
->Pointer
= (char *)palloc
+HGLOBAL_STORAGE
;
1380 if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+HGLOBAL_STORAGE
))
1385 *(HGLOBAL
*)palloc
= hmem
;
1386 pintern
->Pointer
= (char *)palloc
+ HGLOBAL_STORAGE
;
1392 if(pintern
->Pointer
)
1394 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-HGLOBAL_STORAGE
);
1395 pintern
->Pointer
=NULL
;
1400 RtlUnlockHeap(GetProcessHeap());
1405 /***********************************************************************
1406 * GlobalFree (KERNEL32.@)
1411 HGLOBAL WINAPI
GlobalFree(
1412 HGLOBAL hmem
/* [in] Handle of global memory object */
1414 PGLOBAL32_INTERN pintern
;
1417 RtlLockHeap(GetProcessHeap());
1421 if(ISPOINTER(hmem
)) /* POINTER */
1423 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1427 pintern
=HANDLE_TO_INTERN(hmem
);
1429 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1432 /* WIN98 does not make this test. That is you can free a */
1433 /* block you have not unlocked. Go figure!! */
1434 /* if(pintern->LockCount!=0) */
1435 /* SetLastError(ERROR_INVALID_HANDLE); */
1437 if(pintern
->Pointer
)
1438 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-HGLOBAL_STORAGE
))
1440 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1445 __EXCEPT(page_fault
)
1447 ERR("page fault occurred ! Caused by bug ?\n");
1448 SetLastError( ERROR_INVALID_PARAMETER
);
1452 RtlUnlockHeap(GetProcessHeap());
1457 /***********************************************************************
1458 * GlobalSize (KERNEL32.@)
1460 * Size in bytes of the global memory object
1463 SIZE_T WINAPI
GlobalSize(
1464 HGLOBAL hmem
/* [in] Handle of global memory object */
1467 PGLOBAL32_INTERN pintern
;
1469 if (!hmem
) return 0;
1473 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1477 RtlLockHeap(GetProcessHeap());
1478 pintern
=HANDLE_TO_INTERN(hmem
);
1480 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1482 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1486 retval
= HeapSize(GetProcessHeap(), 0,
1487 (char *)(pintern
->Pointer
) - HGLOBAL_STORAGE
);
1488 if (retval
!= (DWORD
)-1) retval
-= HGLOBAL_STORAGE
;
1493 WARN("invalid handle\n");
1496 RtlUnlockHeap(GetProcessHeap());
1498 /* HeapSize returns 0xffffffff on failure */
1499 if (retval
== 0xffffffff) retval
= 0;
1504 /***********************************************************************
1505 * GlobalWire (KERNEL32.@)
1507 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1509 return GlobalLock( hmem
);
1513 /***********************************************************************
1514 * GlobalUnWire (KERNEL32.@)
1516 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1518 return GlobalUnlock( hmem
);
1522 /***********************************************************************
1523 * GlobalFix (KERNEL32.@)
1525 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1531 /***********************************************************************
1532 * GlobalUnfix (KERNEL32.@)
1534 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1536 GlobalUnlock( hmem
);
1540 /***********************************************************************
1541 * GlobalFlags (KERNEL32.@)
1542 * Returns information about the specified global memory object
1545 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1548 * Value specifying allocation flags and lock count
1549 * GMEM_INVALID_HANDLE: Failure
1551 UINT WINAPI
GlobalFlags(
1552 HGLOBAL hmem
/* [in] Handle to global memory object */
1555 PGLOBAL32_INTERN pintern
;
1563 RtlLockHeap(GetProcessHeap());
1564 pintern
=HANDLE_TO_INTERN(hmem
);
1565 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1567 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1568 if(pintern
->Pointer
==0)
1569 retval
|= GMEM_DISCARDED
;
1573 WARN("Invalid handle: %p\n", hmem
);
1576 RtlUnlockHeap(GetProcessHeap());
1582 /***********************************************************************
1583 * GlobalCompact (KERNEL32.@)
1585 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
1587 return 0; /* GlobalCompact does nothing in Win32 */
1591 /***********************************************************************
1592 * GlobalMemoryStatus (KERNEL32.@)
1593 * Provides information about the status of the memory, so apps can tell
1594 * roughly how much they are able to allocate
1599 VOID WINAPI
GlobalMemoryStatus(
1600 LPMEMORYSTATUS lpmem
1602 static MEMORYSTATUS cached_memstatus
;
1603 static int cache_lastchecked
= 0;
1608 #if defined(__FreeBSD__) || defined(__NetBSD__)
1611 int mib
[2] = { CTL_HW
};
1613 if (time(NULL
)==cache_lastchecked
) {
1614 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1617 cache_lastchecked
= time(NULL
);
1619 lpmem
->dwMemoryLoad
= 0;
1620 lpmem
->dwTotalPhys
= 16*1024*1024;
1621 lpmem
->dwAvailPhys
= 16*1024*1024;
1622 lpmem
->dwTotalPageFile
= 16*1024*1024;
1623 lpmem
->dwAvailPageFile
= 16*1024*1024;
1626 f
= fopen( "/proc/meminfo", "r" );
1630 int total
, used
, free
, shared
, buffers
, cached
;
1632 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1633 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1634 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1635 while (fgets( buffer
, sizeof(buffer
), f
))
1637 /* old style /proc/meminfo ... */
1638 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1640 lpmem
->dwTotalPhys
+= total
;
1641 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1643 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1645 lpmem
->dwTotalPageFile
+= total
;
1646 lpmem
->dwAvailPageFile
+= free
;
1649 /* new style /proc/meminfo ... */
1650 if (sscanf(buffer
, "MemTotal: %d", &total
))
1651 lpmem
->dwTotalPhys
= total
*1024;
1652 if (sscanf(buffer
, "MemFree: %d", &free
))
1653 lpmem
->dwAvailPhys
= free
*1024;
1654 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1655 lpmem
->dwTotalPageFile
= total
*1024;
1656 if (sscanf(buffer
, "SwapFree: %d", &free
))
1657 lpmem
->dwAvailPageFile
= free
*1024;
1658 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1659 lpmem
->dwAvailPhys
+= buffers
*1024;
1660 if (sscanf(buffer
, "Cached: %d", &cached
))
1661 lpmem
->dwAvailPhys
+= cached
*1024;
1665 if (lpmem
->dwTotalPhys
)
1667 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1668 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1669 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1670 / (TotalPhysical
/ 100);
1673 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1674 mib
[1] = HW_PHYSMEM
;
1675 sysctl(mib
, 2, NULL
, &size_sys
, NULL
, 0);
1676 tmp
= malloc(size_sys
* sizeof(int));
1677 sysctl(mib
, 2, tmp
, &size_sys
, NULL
, 0);
1680 lpmem
->dwTotalPhys
= *tmp
;
1682 mib
[1] = HW_USERMEM
;
1683 sysctl(mib
, 2, NULL
, &size_sys
, NULL
, 0);
1684 tmp
= malloc(size_sys
* sizeof(int));
1685 sysctl(mib
, 2, tmp
, &size_sys
, NULL
, 0);
1688 lpmem
->dwAvailPhys
= *tmp
;
1689 lpmem
->dwTotalPageFile
= *tmp
;
1690 lpmem
->dwAvailPageFile
= *tmp
;
1691 lpmem
->dwMemoryLoad
= lpmem
->dwTotalPhys
- lpmem
->dwAvailPhys
;
1694 lpmem
->dwAvailPhys
= lpmem
->dwTotalPhys
;
1695 lpmem
->dwTotalPageFile
= lpmem
->dwTotalPhys
;
1696 lpmem
->dwAvailPageFile
= lpmem
->dwTotalPhys
;
1697 lpmem
->dwMemoryLoad
= 0;
1703 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1704 * is more than 2GB of physical memory.
1706 if (lpmem
->dwTotalPhys
>2U*1024*1024*1024)
1708 lpmem
->dwTotalPhys
=2U*1024*1024*1024;
1709 lpmem
->dwAvailPhys
=2U*1024*1024*1024;
1712 /* FIXME: should do something for other systems */
1714 lpmem
->dwTotalVirtual
= (char*)si
.lpMaximumApplicationAddress
-(char*)si
.lpMinimumApplicationAddress
;
1715 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1716 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1717 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1719 /* it appears some memory display programs want to divide by these values */
1720 if(lpmem
->dwTotalPageFile
==0)
1721 lpmem
->dwTotalPageFile
++;
1723 if(lpmem
->dwAvailPageFile
==0)
1724 lpmem
->dwAvailPageFile
++;
1726 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1727 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1728 lpmem
->dwLength
, lpmem
->dwMemoryLoad
, lpmem
->dwTotalPhys
, lpmem
->dwAvailPhys
,
1729 lpmem
->dwTotalPageFile
, lpmem
->dwAvailPageFile
, lpmem
->dwTotalVirtual
,
1730 lpmem
->dwAvailVirtual
);
1733 /***********************************************************************
1734 * GlobalMemoryStatusEx (KERNEL32.@)
1735 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1740 BOOL WINAPI
GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer
) {
1741 MEMORYSTATUS memstatus
;
1743 /* Because GlobalMemoryStatusEx is identical to GlobalMemoryStatus save
1744 for one extra field in the struct, and the lack of a bug, we simply
1745 call GlobalMemoryStatus and copy the values across. */
1746 FIXME("we should emulate the 4GB bug here, as per MSDN\n");
1747 GlobalMemoryStatus(&memstatus
);
1748 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
1749 lpBuffer
->ullTotalPhys
= memstatus
.dwTotalPhys
;
1750 lpBuffer
->ullAvailPhys
= memstatus
.dwAvailPhys
;
1751 lpBuffer
->ullTotalPageFile
= memstatus
.dwTotalPageFile
;
1752 lpBuffer
->ullAvailPageFile
= memstatus
.dwAvailPageFile
;
1753 lpBuffer
->ullTotalVirtual
= memstatus
.dwTotalVirtual
;
1754 lpBuffer
->ullAvailVirtual
= memstatus
.dwAvailVirtual
;
1755 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1756 memory in the extended portion of the virtual address space of the calling
1759 However, I don't know what this means, so set it to zero :(
1761 lpBuffer
->ullAvailExtendedVirtual
= 0;
1765 /***********************************************************************
1766 * A20Proc (KERNEL.165)
1767 * A20_Proc (SYSTEM.20)
1769 void WINAPI
A20Proc16( WORD unused
)
1771 /* this is also a NOP in Windows */
1774 /***********************************************************************
1775 * LimitEMSPages (KERNEL.156)
1777 DWORD WINAPI
LimitEMSPages16( DWORD unused
)