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"
48 #include "wine/debug.h"
50 #include "msvcrt/excpt.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)
280 * GlobalReAlloc16 (KERNEL32.@)
285 HGLOBAL16 WINAPI
GlobalReAlloc16(
286 HGLOBAL16 handle
, /* [in] Handle of global memory object */
287 DWORD size
, /* [in] New size of block */
288 UINT16 flags
/* [in] How to reallocate object */
293 GLOBALARENA
*pArena
, *pNewArena
;
294 WORD sel
= GlobalHandleToSel16( handle
);
296 TRACE("%04x %ld flags=%04x\n",
297 handle
, size
, flags
);
298 if (!handle
) return 0;
300 if (!VALID_HANDLE(handle
)) {
301 WARN("Invalid handle 0x%04x!\n", handle
);
304 pArena
= GET_ARENA_PTR( handle
);
306 /* Discard the block if requested */
308 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
310 if (!(pArena
->flags
& GA_MOVEABLE
) ||
311 !(pArena
->flags
& GA_DISCARDABLE
) ||
312 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
313 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
316 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
317 * change the selector if we are shrinking the block.
318 * FIXME: shouldn't we keep selectors until the block is deleted?
320 SELECTOR_ReallocBlock( sel
, 0, 1 );
326 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
327 if (size
== 0) size
= 0x20;
328 else size
= (size
+ 0x1f) & ~0x1f;
330 /* Change the flags */
332 if (flags
& GMEM_MODIFY
)
334 /* Change the flags, leaving GA_DGROUP alone */
335 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
336 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
340 /* Reallocate the linear memory */
342 ptr
= (void *)pArena
->base
;
343 oldsize
= pArena
->size
;
344 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr
,oldsize
,size
);
345 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
347 if (pArena
->flags
& GA_DOSMEM
)
348 newptr
= DOSMEM_ResizeBlock(ptr
, size
, NULL
);
350 /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16),
351 only try to realloc in place */
352 newptr
= HeapReAlloc( GetProcessHeap(),
353 (pArena
->pageLockCount
> 0)?HEAP_REALLOC_IN_PLACE_ONLY
:0, ptr
, size
);
356 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
357 if (pArena
->pageLockCount
<1)
359 HeapFree( GetProcessHeap(), 0, ptr
);
360 SELECTOR_FreeBlock( sel
);
361 memset( pArena
, 0, sizeof(GLOBALARENA
) );
367 /* Reallocate the selector(s) */
369 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
372 HeapFree( GetProcessHeap(), 0, ptr
);
373 memset( pArena
, 0, sizeof(GLOBALARENA
) );
376 selcount
= (size
+ 0xffff) / 0x10000;
378 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
380 HeapFree( GetProcessHeap(), 0, ptr
);
381 SELECTOR_FreeBlock( sel
);
385 /* Fill the new arena block
386 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
388 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
389 pNewArena
->base
= (DWORD
)ptr
;
390 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
391 pNewArena
->selCount
= selcount
;
392 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
394 if (selcount
> 1) /* clear the next arena blocks */
395 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
397 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
398 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
399 return pNewArena
->handle
;
403 /***********************************************************************
404 * GlobalFree (KERNEL.17)
405 * GlobalFree16 (KERNEL32.31)
410 HGLOBAL16 WINAPI
GlobalFree16(
411 HGLOBAL16 handle
/* [in] Handle of global memory object */
415 if (!VALID_HANDLE(handle
)) {
416 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
419 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
421 TRACE("%04x\n", handle
);
422 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
423 if (ptr
) HeapFree( GetProcessHeap(), 0, ptr
);
428 /***********************************************************************
429 * GlobalLock (KERNEL.18)
431 * This is the GlobalLock16() function used by 16-bit code.
433 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
435 WORD sel
= GlobalHandleToSel16( handle
);
436 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
440 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
442 if (!VALID_HANDLE(handle
)) {
443 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
446 else if (!GET_ARENA_PTR(handle
)->base
)
449 GET_ARENA_PTR(handle
)->lockCount
++;
452 CURRENT_STACK16
->ecx
= sel
; /* selector must be returned in CX as well */
453 return MAKESEGPTR( sel
, 0 );
457 /**********************************************************************
458 * K32WOWGlobalLock16 (KERNEL32.60)
460 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 hMem
)
462 return WIN16_GlobalLock16( hMem
);
466 /***********************************************************************
467 * GlobalLock16 (KERNEL32.25)
469 * This is the GlobalLock16() function used by 32-bit code.
472 * Pointer to first byte of memory block
475 LPVOID WINAPI
GlobalLock16(
476 HGLOBAL16 handle
/* [in] Handle of global memory object */
478 if (!handle
) return 0;
479 if (!VALID_HANDLE(handle
))
481 GET_ARENA_PTR(handle
)->lockCount
++;
482 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
486 /***********************************************************************
487 * GlobalUnlock (KERNEL.19)
488 * GlobalUnlock16 (KERNEL32.26)
490 * Should the return values be cast to booleans?
493 * TRUE: Object is still locked
494 * FALSE: Object is unlocked
496 BOOL16 WINAPI
GlobalUnlock16(
497 HGLOBAL16 handle
/* [in] Handle of global memory object */
499 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
500 if (!VALID_HANDLE(handle
)) {
501 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
504 TRACE("%04x\n", handle
);
505 if (pArena
->lockCount
) pArena
->lockCount
--;
506 return pArena
->lockCount
;
509 /***********************************************************************
510 * GlobalChangeLockCount (KERNEL.365)
512 * This is declared as a register function as it has to preserve
513 * *all* registers, even AX/DX !
516 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
520 GlobalLock16( handle
);
521 else if ( delta
== -1 )
522 GlobalUnlock16( handle
);
524 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
527 /***********************************************************************
528 * GlobalSize (KERNEL.20)
529 * GlobalSize16 (KERNEL32.32)
531 * Size in bytes of object
534 DWORD WINAPI
GlobalSize16(
535 HGLOBAL16 handle
/* [in] Handle of global memory object */
537 TRACE("%04x\n", handle
);
538 if (!handle
) return 0;
539 if (!VALID_HANDLE(handle
))
541 return GET_ARENA_PTR(handle
)->size
;
545 /***********************************************************************
546 * GlobalHandle (KERNEL.21)
548 * Why is GlobalHandleToSel used here with the sel as input?
554 DWORD WINAPI
GlobalHandle16(
555 WORD sel
/* [in] Address of global memory block */
557 TRACE("%04x\n", sel
);
558 if (!VALID_HANDLE(sel
)) {
559 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
562 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
565 /***********************************************************************
566 * GlobalHandleNoRIP (KERNEL.159)
568 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
571 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
572 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
573 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
579 /***********************************************************************
580 * GlobalFlags (KERNEL.22)
581 * GlobalFlags16 (KERNEL32.@)
583 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
587 * Value specifying flags and lock count
588 * GMEM_INVALID_HANDLE: Invalid handle
590 UINT16 WINAPI
GlobalFlags16(
591 HGLOBAL16 handle
/* [in] Handle of global memory object */
595 TRACE("%04x\n", handle
);
596 if (!VALID_HANDLE(handle
)) {
597 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
600 pArena
= GET_ARENA_PTR(handle
);
601 return pArena
->lockCount
|
602 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
603 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
607 /***********************************************************************
608 * LockSegment (KERNEL.23)
610 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
612 TRACE("%04x\n", handle
);
613 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
614 if (!VALID_HANDLE(handle
)) {
615 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
618 GET_ARENA_PTR(handle
)->lockCount
++;
623 /***********************************************************************
624 * UnlockSegment (KERNEL.24)
626 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
628 TRACE("%04x\n", handle
);
629 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
630 if (!VALID_HANDLE(handle
)) {
631 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
634 GET_ARENA_PTR(handle
)->lockCount
--;
635 /* FIXME: this ought to return the lock count in CX (go figure...) */
639 /***********************************************************************
640 * GlobalCompact (KERNEL.25)
642 DWORD WINAPI
GlobalCompact16( DWORD desired
)
644 return GLOBAL_MAX_ALLOC_SIZE
;
648 /***********************************************************************
649 * GlobalFreeAll (KERNEL.26)
651 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
656 pArena
= pGlobalArena
;
657 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
659 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
660 GlobalFree16( pArena
->handle
);
665 /***********************************************************************
666 * GlobalWire (KERNEL.111)
667 * GlobalWire16 (KERNEL32.29)
669 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
671 return WIN16_GlobalLock16( handle
);
675 /***********************************************************************
676 * GlobalUnWire (KERNEL.112)
677 * GlobalUnWire16 (KERNEL32.30)
679 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
681 return !GlobalUnlock16( handle
);
685 /***********************************************************************
686 * SetSwapAreaSize (KERNEL.106)
688 LONG WINAPI
SetSwapAreaSize16( WORD size
)
690 FIXME("(%d) - stub!\n", size
);
691 return MAKELONG( size
, 0xffff );
695 /***********************************************************************
696 * GlobalLRUOldest (KERNEL.163)
698 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
700 TRACE("%04x\n", handle
);
701 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
706 /***********************************************************************
707 * GlobalLRUNewest (KERNEL.164)
709 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
711 TRACE("%04x\n", handle
);
712 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
717 /***********************************************************************
718 * GetFreeSpace (KERNEL.169)
720 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
723 GlobalMemoryStatus( &ms
);
724 return ms
.dwAvailVirtual
;
727 /***********************************************************************
728 * GlobalDOSAlloc (KERNEL.184)
730 * Address (HW=Paragraph segment; LW=Selector)
732 DWORD WINAPI
GlobalDOSAlloc16(
733 DWORD size
/* [in] Number of bytes to be allocated */
736 LPVOID lpBlock
= DOSMEM_GetBlock( size
, &uParagraph
);
740 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
744 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
745 pArena
= GET_ARENA_PTR(wSelector
);
746 pArena
->flags
|= GA_DOSMEM
;
747 return MAKELONG(wSelector
,uParagraph
);
753 /***********************************************************************
754 * GlobalDOSFree (KERNEL.185)
759 WORD WINAPI
GlobalDOSFree16(
760 WORD sel
/* [in] Selector */
762 DWORD block
= GetSelectorBase(sel
);
764 if( block
&& block
< 0x100000 )
766 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
767 if( DOSMEM_FreeBlock( lpBlock
) )
768 GLOBAL_FreeBlock( sel
);
775 /***********************************************************************
776 * GlobalPageLock (KERNEL.191)
777 * GlobalSmartPageLock(KERNEL.230)
779 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
781 TRACE("%04x\n", handle
);
782 if (!VALID_HANDLE(handle
)) {
783 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
786 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
790 /***********************************************************************
791 * GlobalPageUnlock (KERNEL.192)
792 * GlobalSmartPageUnlock(KERNEL.231)
794 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
796 TRACE("%04x\n", handle
);
797 if (!VALID_HANDLE(handle
)) {
798 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
801 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
805 /***********************************************************************
806 * GlobalFix (KERNEL.197)
807 * GlobalFix16 (KERNEL32.27)
809 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
811 TRACE("%04x\n", handle
);
812 if (!VALID_HANDLE(handle
)) {
813 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
816 GET_ARENA_PTR(handle
)->lockCount
++;
818 return GlobalHandleToSel16(handle
);
822 /***********************************************************************
823 * GlobalUnfix (KERNEL.198)
824 * GlobalUnfix16 (KERNEL32.28)
826 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
828 TRACE("%04x\n", handle
);
829 if (!VALID_HANDLE(handle
)) {
830 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
833 GET_ARENA_PTR(handle
)->lockCount
--;
837 /***********************************************************************
838 * FarSetOwner (KERNEL.403)
840 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
842 if (!VALID_HANDLE(handle
)) {
843 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
846 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
850 /***********************************************************************
851 * FarGetOwner (KERNEL.404)
853 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
855 if (!VALID_HANDLE(handle
)) {
856 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
859 return GET_ARENA_PTR(handle
)->hOwner
;
863 /***********************************************************************
864 * GlobalHandleToSel (TOOLHELP.50)
866 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
868 if (!handle
) return 0;
869 if (!VALID_HANDLE(handle
)) {
870 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
875 WARN("Program attempted invalid selector conversion\n" );
882 /***********************************************************************
883 * GlobalFirst (TOOLHELP.51)
885 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
887 if (wFlags
== GLOBAL_LRU
) return FALSE
;
889 return GlobalNext16( pGlobal
, wFlags
);
893 /***********************************************************************
894 * GlobalNext (TOOLHELP.52)
896 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
900 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
901 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
902 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
905 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
906 if (pArena
->size
== 0) break; /* block is free */
907 if (i
>= globalArenaSize
) return FALSE
;
911 pGlobal
->dwAddress
= pArena
->base
;
912 pGlobal
->dwBlockSize
= pArena
->size
;
913 pGlobal
->hBlock
= pArena
->handle
;
914 pGlobal
->wcLock
= pArena
->lockCount
;
915 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
916 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
917 pGlobal
->wHeapPresent
= FALSE
;
918 pGlobal
->hOwner
= pArena
->hOwner
;
919 pGlobal
->wType
= GT_UNKNOWN
;
926 /***********************************************************************
927 * GlobalInfo (TOOLHELP.53)
929 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
934 pInfo
->wcItems
= globalArenaSize
;
935 pInfo
->wcItemsFree
= 0;
936 pInfo
->wcItemsLRU
= 0;
937 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
938 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
943 /***********************************************************************
944 * GlobalEntryHandle (TOOLHELP.54)
946 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
948 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
950 pGlobal
->dwAddress
= pArena
->base
;
951 pGlobal
->dwBlockSize
= pArena
->size
;
952 pGlobal
->hBlock
= pArena
->handle
;
953 pGlobal
->wcLock
= pArena
->lockCount
;
954 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
955 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
956 pGlobal
->wHeapPresent
= FALSE
;
957 pGlobal
->hOwner
= pArena
->hOwner
;
958 pGlobal
->wType
= GT_UNKNOWN
;
965 /***********************************************************************
966 * GlobalEntryModule (TOOLHELP.55)
968 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
971 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
976 /***********************************************************************
977 * MemManInfo (TOOLHELP.72)
979 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
984 * Not unsurprisingly although the documention says you
985 * _must_ provide the size in the dwSize field, this function
986 * (under Windows) always fills the structure and returns true.
988 GlobalMemoryStatus( &status
);
989 info
->wPageSize
= getpagesize();
990 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
991 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
992 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
993 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
994 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
995 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
996 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
997 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
998 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1002 /***********************************************************************
1003 * GetFreeMemInfo (KERNEL.316)
1005 DWORD WINAPI
GetFreeMemInfo16(void)
1008 MemManInfo16( &info
);
1009 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1013 * Win32 Global heap functions (GlobalXXX).
1014 * These functions included in Win32 for compatibility with 16 bit Windows
1015 * Especially the moveable blocks and handles are oldish.
1016 * But the ability to directly allocate memory with GPTR and LPTR is widely
1019 * The handle stuff looks horrible, but it's implemented almost like Win95
1024 #define MAGIC_GLOBAL_USED 0x5342
1025 #define GLOBAL_LOCK_MAX 0xFF
1026 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1027 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1028 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
1029 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1030 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1032 typedef struct __GLOBAL32_INTERN
1035 LPVOID Pointer WINE_PACKED
;
1038 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
1041 /***********************************************************************
1042 * GlobalAlloc (KERNEL32.@)
1047 HGLOBAL WINAPI
GlobalAlloc(
1048 UINT flags
, /* [in] Object allocation attributes */
1049 DWORD size
/* [in] Number of bytes to allocate */
1051 PGLOBAL32_INTERN pintern
;
1055 if(flags
&GMEM_ZEROINIT
)
1056 hpflags
=HEAP_ZERO_MEMORY
;
1060 TRACE("() flags=%04x\n", flags
);
1062 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1064 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1065 return (HGLOBAL
) palloc
;
1069 /* HeapLock(heap); */
1071 pintern
=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1072 if (!pintern
) return 0;
1075 if (!(palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
+sizeof(HGLOBAL
)))) {
1076 HeapFree(GetProcessHeap(), 0, pintern
);
1079 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1080 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1083 pintern
->Pointer
=NULL
;
1084 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1085 pintern
->Flags
=flags
>>8;
1086 pintern
->LockCount
=0;
1088 /* HeapUnlock(heap); */
1090 return INTERN_TO_HANDLE(pintern
);
1095 /***********************************************************************
1096 * GlobalLock (KERNEL32.@)
1098 * Pointer to first byte of block
1101 LPVOID WINAPI
GlobalLock(
1102 HGLOBAL hmem
/* [in] Handle of global memory object */
1104 PGLOBAL32_INTERN pintern
;
1108 return (LPVOID
) hmem
;
1110 /* HeapLock(GetProcessHeap()); */
1112 pintern
=HANDLE_TO_INTERN(hmem
);
1113 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1115 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1116 pintern
->LockCount
++;
1117 palloc
=pintern
->Pointer
;
1121 WARN("invalid handle\n");
1122 palloc
=(LPVOID
) NULL
;
1123 SetLastError(ERROR_INVALID_HANDLE
);
1125 /* HeapUnlock(GetProcessHeap()); */;
1130 /***********************************************************************
1131 * GlobalUnlock (KERNEL32.@)
1133 * TRUE: Object is still locked
1134 * FALSE: Object is unlocked
1136 BOOL WINAPI
GlobalUnlock(
1137 HGLOBAL hmem
/* [in] Handle of global memory object */
1139 PGLOBAL32_INTERN pintern
;
1142 if (ISPOINTER(hmem
)) return FALSE
;
1146 /* HeapLock(GetProcessHeap()); */
1147 pintern
=HANDLE_TO_INTERN(hmem
);
1148 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1150 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1151 pintern
->LockCount
--;
1153 locked
= (pintern
->LockCount
!= 0);
1154 if (!locked
) SetLastError(NO_ERROR
);
1158 WARN("invalid handle\n");
1159 SetLastError(ERROR_INVALID_HANDLE
);
1162 /* HeapUnlock(GetProcessHeap()); */
1164 __EXCEPT(page_fault
)
1166 ERR("page fault occurred ! Caused by bug ?\n");
1167 SetLastError( ERROR_INVALID_PARAMETER
);
1175 /***********************************************************************
1176 * GlobalHandle (KERNEL32.@)
1177 * Returns the handle associated with the specified pointer.
1183 HGLOBAL WINAPI
GlobalHandle(
1184 LPCVOID pmem
/* [in] Pointer to global memory block */
1187 PGLOBAL32_INTERN maybe_intern
;
1192 SetLastError( ERROR_INVALID_PARAMETER
);
1200 /* note that if pmem is a pointer to a a block allocated by */
1201 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1203 if (ISPOINTER(pmem
)) {
1204 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
1205 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
1208 handle
= POINTER_TO_HANDLE(pmem
);
1210 handle
= (HGLOBAL
)pmem
;
1212 /* Now test handle either passed in or retrieved from pointer */
1213 maybe_intern
= HANDLE_TO_INTERN( handle
);
1214 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1215 test
= maybe_intern
->Pointer
;
1216 if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL
*)test
)-1 ) && /* obj(-handle) valid arena? */
1217 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
1218 break; /* valid moveable block */
1221 SetLastError( ERROR_INVALID_HANDLE
);
1223 __EXCEPT(page_fault
)
1225 SetLastError( ERROR_INVALID_HANDLE
);
1234 /***********************************************************************
1235 * GlobalReAlloc (KERNEL32.@)
1240 HGLOBAL WINAPI
GlobalReAlloc(
1241 HGLOBAL hmem
, /* [in] Handle of global memory object */
1242 DWORD size
, /* [in] New size of block */
1243 UINT flags
/* [in] How to reallocate object */
1247 PGLOBAL32_INTERN pintern
;
1248 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1251 /* HeapLock(heap); */
1252 if(flags
& GMEM_MODIFY
) /* modify flags */
1254 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1256 /* make a fixed block moveable
1257 * actually only NT is able to do this. But it's soo simple
1261 ERR("GlobalReAlloc32 with null handle!\n");
1262 SetLastError( ERROR_NOACCESS
);
1265 size
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1266 hnew
=GlobalAlloc( flags
, size
);
1267 palloc
=GlobalLock(hnew
);
1268 memcpy(palloc
, (LPVOID
) hmem
, size
);
1272 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1274 /* change the flags to make our block "discardable" */
1275 pintern
=HANDLE_TO_INTERN(hmem
);
1276 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1281 SetLastError(ERROR_INVALID_PARAMETER
);
1289 /* reallocate fixed memory */
1290 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), heap_flags
, (LPVOID
) hmem
, size
);
1294 /* reallocate a moveable block */
1295 pintern
=HANDLE_TO_INTERN(hmem
);
1298 /* Apparently Windows doesn't care whether the handle is locked at this point */
1299 /* See also the same comment in GlobalFree() */
1300 if(pintern
->LockCount
>1) {
1301 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1302 SetLastError(ERROR_INVALID_HANDLE
);
1308 if(pintern
->Pointer
)
1310 if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
1311 (char *) pintern
->Pointer
-sizeof(HGLOBAL
),
1312 size
+sizeof(HGLOBAL
))) == NULL
)
1313 return 0; /* Block still valid */
1314 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1318 if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+sizeof(HGLOBAL
)))
1321 *(HGLOBAL
*)palloc
=hmem
;
1322 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1327 if(pintern
->Pointer
)
1329 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-sizeof(HGLOBAL
));
1330 pintern
->Pointer
=NULL
;
1335 /* HeapUnlock(heap); */
1340 /***********************************************************************
1341 * GlobalFree (KERNEL32.@)
1346 HGLOBAL WINAPI
GlobalFree(
1347 HGLOBAL hmem
/* [in] Handle of global memory object */
1349 PGLOBAL32_INTERN pintern
;
1355 if(ISPOINTER(hmem
)) /* POINTER */
1357 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1361 /* HeapLock(heap); */
1362 pintern
=HANDLE_TO_INTERN(hmem
);
1364 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1367 /* WIN98 does not make this test. That is you can free a */
1368 /* block you have not unlocked. Go figure!! */
1369 /* if(pintern->LockCount!=0) */
1370 /* SetLastError(ERROR_INVALID_HANDLE); */
1372 if(pintern
->Pointer
)
1373 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1375 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1378 /* HeapUnlock(heap); */
1381 __EXCEPT(page_fault
)
1383 ERR("page fault occurred ! Caused by bug ?\n");
1384 SetLastError( ERROR_INVALID_PARAMETER
);
1392 /***********************************************************************
1393 * GlobalSize (KERNEL32.@)
1395 * Size in bytes of the global memory object
1398 DWORD WINAPI
GlobalSize(
1399 HGLOBAL hmem
/* [in] Handle of global memory object */
1402 PGLOBAL32_INTERN pintern
;
1406 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1410 /* HeapLock(heap); */
1411 pintern
=HANDLE_TO_INTERN(hmem
);
1413 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1415 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1417 retval
=HeapSize(GetProcessHeap(), 0,
1418 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1419 if (retval
== 0xffffffff-4) retval
= 0;
1423 WARN("invalid handle\n");
1426 /* HeapUnlock(heap); */
1428 /* HeapSize returns 0xffffffff on failure */
1429 if (retval
== 0xffffffff) retval
= 0;
1434 /***********************************************************************
1435 * GlobalWire (KERNEL32.@)
1437 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1439 return GlobalLock( hmem
);
1443 /***********************************************************************
1444 * GlobalUnWire (KERNEL32.@)
1446 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1448 return GlobalUnlock( hmem
);
1452 /***********************************************************************
1453 * GlobalFix (KERNEL32.@)
1455 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1461 /***********************************************************************
1462 * GlobalUnfix (KERNEL32.@)
1464 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1466 GlobalUnlock( hmem
);
1470 /***********************************************************************
1471 * GlobalFlags (KERNEL32.@)
1472 * Returns information about the specified global memory object
1475 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1478 * Value specifying allocation flags and lock count
1479 * GMEM_INVALID_HANDLE: Failure
1481 UINT WINAPI
GlobalFlags(
1482 HGLOBAL hmem
/* [in] Handle to global memory object */
1485 PGLOBAL32_INTERN pintern
;
1493 /* HeapLock(GetProcessHeap()); */
1494 pintern
=HANDLE_TO_INTERN(hmem
);
1495 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1497 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1498 if(pintern
->Pointer
==0)
1499 retval
|= GMEM_DISCARDED
;
1503 WARN("Invalid handle: %04x\n", hmem
);
1506 /* HeapUnlock(GetProcessHeap()); */
1512 /***********************************************************************
1513 * GlobalCompact (KERNEL32.@)
1515 DWORD WINAPI
GlobalCompact( DWORD minfree
)
1517 return 0; /* GlobalCompact does nothing in Win32 */
1521 /***********************************************************************
1522 * GlobalMemoryStatus (KERNEL32.@)
1526 VOID WINAPI
GlobalMemoryStatus(
1527 LPMEMORYSTATUS lpmem
1529 static MEMORYSTATUS cached_memstatus
;
1530 static int cache_lastchecked
= 0;
1539 if (time(NULL
)==cache_lastchecked
) {
1540 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1543 cache_lastchecked
= time(NULL
);
1545 lpmem
->dwMemoryLoad
= 0;
1546 lpmem
->dwTotalPhys
= 16*1024*1024;
1547 lpmem
->dwAvailPhys
= 16*1024*1024;
1548 lpmem
->dwTotalPageFile
= 16*1024*1024;
1549 lpmem
->dwAvailPageFile
= 16*1024*1024;
1552 f
= fopen( "/proc/meminfo", "r" );
1556 int total
, used
, free
, shared
, buffers
, cached
;
1558 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1559 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1560 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1561 while (fgets( buffer
, sizeof(buffer
), f
))
1563 /* old style /proc/meminfo ... */
1564 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1566 lpmem
->dwTotalPhys
+= total
;
1567 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1569 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1571 lpmem
->dwTotalPageFile
+= total
;
1572 lpmem
->dwAvailPageFile
+= free
;
1575 /* new style /proc/meminfo ... */
1576 if (sscanf(buffer
, "MemTotal: %d", &total
))
1577 lpmem
->dwTotalPhys
= total
*1024;
1578 if (sscanf(buffer
, "MemFree: %d", &free
))
1579 lpmem
->dwAvailPhys
= free
*1024;
1580 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1581 lpmem
->dwTotalPageFile
= total
*1024;
1582 if (sscanf(buffer
, "SwapFree: %d", &free
))
1583 lpmem
->dwAvailPageFile
= free
*1024;
1584 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1585 lpmem
->dwAvailPhys
+= buffers
*1024;
1586 if (sscanf(buffer
, "Cached: %d", &cached
))
1587 lpmem
->dwAvailPhys
+= cached
*1024;
1591 if (lpmem
->dwTotalPhys
)
1593 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1594 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1595 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1596 / (TotalPhysical
/ 100);
1599 #elif defined(__FreeBSD__)
1600 sysctlbyname("hw.physmem", NULL
, &size_sys
, NULL
, 0);
1601 tmp
= malloc(size_sys
* sizeof(int));
1602 sysctlbyname("hw.physmem", tmp
, &size_sys
, NULL
, 0);
1605 lpmem
->dwTotalPhys
= *tmp
;
1607 sysctlbyname("hw.usermem", NULL
, &size_sys
, NULL
, 0);
1608 tmp
= malloc(size_sys
* sizeof(int));
1609 sysctlbyname("hw.usermem", tmp
, &size_sys
, NULL
, 0);
1612 lpmem
->dwAvailPhys
= *tmp
;
1613 lpmem
->dwTotalPageFile
= *tmp
;
1614 lpmem
->dwAvailPageFile
= *tmp
;
1615 lpmem
->dwMemoryLoad
= lpmem
->dwTotalPhys
- lpmem
->dwAvailPhys
;
1618 lpmem
->dwAvailPhys
= lpmem
->dwTotalPhys
;
1619 lpmem
->dwTotalPageFile
= lpmem
->dwTotalPhys
;
1620 lpmem
->dwAvailPageFile
= lpmem
->dwTotalPhys
;
1621 lpmem
->dwMemoryLoad
= 0;
1627 /* FIXME: should do something for other systems */
1629 lpmem
->dwTotalVirtual
= (char*)si
.lpMaximumApplicationAddress
-(char*)si
.lpMinimumApplicationAddress
;
1630 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1631 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1632 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1634 /* it appears some memory display programs want to divide by these values */
1635 if(lpmem
->dwTotalPageFile
==0)
1636 lpmem
->dwTotalPageFile
++;
1638 if(lpmem
->dwAvailPageFile
==0)
1639 lpmem
->dwAvailPageFile
++;
1641 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1642 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1643 lpmem
->dwLength
, lpmem
->dwMemoryLoad
, lpmem
->dwTotalPhys
, lpmem
->dwAvailPhys
,
1644 lpmem
->dwTotalPageFile
, lpmem
->dwAvailPageFile
, lpmem
->dwTotalVirtual
,
1645 lpmem
->dwAvailVirtual
);
1648 /***********************************************************************
1649 * A20Proc (KERNEL.165)
1650 * A20_Proc (SYSTEM.20)
1652 void WINAPI
A20Proc16( WORD unused
)
1654 /* this is also a NOP in Windows */
1657 /***********************************************************************
1658 * LimitEMSPages (KERNEL.156)
1660 DWORD WINAPI
LimitEMSPages16( DWORD unused
)