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)
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 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
315 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
316 * change the selector if we are shrinking the block.
317 * FIXME: shouldn't we keep selectors until the block is deleted?
319 SELECTOR_ReallocBlock( sel
, 0, 1 );
325 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
326 if (size
== 0) size
= 0x20;
327 else size
= (size
+ 0x1f) & ~0x1f;
329 /* Change the flags */
331 if (flags
& GMEM_MODIFY
)
333 /* Change the flags, leaving GA_DGROUP alone */
334 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
335 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
339 /* Reallocate the linear memory */
341 ptr
= (void *)pArena
->base
;
342 oldsize
= pArena
->size
;
343 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr
,oldsize
,size
);
344 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
346 if (pArena
->flags
& GA_DOSMEM
)
347 newptr
= DOSMEM_ResizeBlock(ptr
, size
, NULL
);
349 /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16),
350 only try to realloc in place */
351 newptr
= HeapReAlloc( GetProcessHeap(),
352 (pArena
->pageLockCount
> 0)?HEAP_REALLOC_IN_PLACE_ONLY
:0, ptr
, size
);
355 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
356 if (pArena
->pageLockCount
<1)
358 HeapFree( GetProcessHeap(), 0, ptr
);
359 SELECTOR_FreeBlock( sel
);
360 memset( pArena
, 0, sizeof(GLOBALARENA
) );
366 /* Reallocate the selector(s) */
368 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
371 HeapFree( GetProcessHeap(), 0, ptr
);
372 memset( pArena
, 0, sizeof(GLOBALARENA
) );
375 selcount
= (size
+ 0xffff) / 0x10000;
377 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
379 HeapFree( GetProcessHeap(), 0, ptr
);
380 SELECTOR_FreeBlock( sel
);
384 /* Fill the new arena block
385 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
387 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
388 pNewArena
->base
= (DWORD
)ptr
;
389 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
390 pNewArena
->selCount
= selcount
;
391 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
393 if (selcount
> 1) /* clear the next arena blocks */
394 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
396 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
397 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
398 return pNewArena
->handle
;
402 /***********************************************************************
403 * GlobalFree (KERNEL.17)
404 * GlobalFree16 (KERNEL32.31)
409 HGLOBAL16 WINAPI
GlobalFree16(
410 HGLOBAL16 handle
/* [in] Handle of global memory object */
414 if (!VALID_HANDLE(handle
)) {
415 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
418 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
420 TRACE("%04x\n", handle
);
421 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
422 if (ptr
) HeapFree( GetProcessHeap(), 0, ptr
);
427 /***********************************************************************
428 * GlobalLock (KERNEL.18)
430 * This is the GlobalLock16() function used by 16-bit code.
432 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
434 WORD sel
= GlobalHandleToSel16( handle
);
435 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
439 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
441 if (!VALID_HANDLE(handle
)) {
442 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
445 else if (!GET_ARENA_PTR(handle
)->base
)
448 GET_ARENA_PTR(handle
)->lockCount
++;
451 CURRENT_STACK16
->ecx
= sel
; /* selector must be returned in CX as well */
452 return MAKESEGPTR( sel
, 0 );
456 /**********************************************************************
457 * K32WOWGlobalLock16 (KERNEL32.60)
459 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 hMem
)
461 return WIN16_GlobalLock16( hMem
);
465 /***********************************************************************
466 * GlobalLock16 (KERNEL32.25)
468 * This is the GlobalLock16() function used by 32-bit code.
471 * Pointer to first byte of memory block
474 LPVOID WINAPI
GlobalLock16(
475 HGLOBAL16 handle
/* [in] Handle of global memory object */
477 if (!handle
) return 0;
478 if (!VALID_HANDLE(handle
))
480 GET_ARENA_PTR(handle
)->lockCount
++;
481 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
485 /***********************************************************************
486 * GlobalUnlock (KERNEL.19)
487 * GlobalUnlock16 (KERNEL32.26)
489 * Should the return values be cast to booleans?
492 * TRUE: Object is still locked
493 * FALSE: Object is unlocked
495 BOOL16 WINAPI
GlobalUnlock16(
496 HGLOBAL16 handle
/* [in] Handle of global memory object */
498 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
499 if (!VALID_HANDLE(handle
)) {
500 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
503 TRACE("%04x\n", handle
);
504 if (pArena
->lockCount
) pArena
->lockCount
--;
505 return pArena
->lockCount
;
508 /***********************************************************************
509 * GlobalChangeLockCount (KERNEL.365)
511 * This is declared as a register function as it has to preserve
512 * *all* registers, even AX/DX !
515 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
519 GlobalLock16( handle
);
520 else if ( delta
== -1 )
521 GlobalUnlock16( handle
);
523 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
526 /***********************************************************************
527 * GlobalSize (KERNEL.20)
528 * GlobalSize16 (KERNEL32.32)
530 * Size in bytes of object
533 DWORD WINAPI
GlobalSize16(
534 HGLOBAL16 handle
/* [in] Handle of global memory object */
536 TRACE("%04x\n", handle
);
537 if (!handle
) return 0;
538 if (!VALID_HANDLE(handle
))
540 return GET_ARENA_PTR(handle
)->size
;
544 /***********************************************************************
545 * GlobalHandle (KERNEL.21)
547 * Why is GlobalHandleToSel used here with the sel as input?
553 DWORD WINAPI
GlobalHandle16(
554 WORD sel
/* [in] Address of global memory block */
556 TRACE("%04x\n", sel
);
557 if (!VALID_HANDLE(sel
)) {
558 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
561 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
564 /***********************************************************************
565 * GlobalHandleNoRIP (KERNEL.159)
567 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
570 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
571 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
572 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
578 /***********************************************************************
579 * GlobalFlags (KERNEL.22)
582 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
586 * Value specifying flags and lock count
587 * GMEM_INVALID_HANDLE: Invalid handle
589 UINT16 WINAPI
GlobalFlags16(
590 HGLOBAL16 handle
/* [in] Handle of global memory object */
594 TRACE("%04x\n", handle
);
595 if (!VALID_HANDLE(handle
)) {
596 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
599 pArena
= GET_ARENA_PTR(handle
);
600 return pArena
->lockCount
|
601 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
602 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
606 /***********************************************************************
607 * LockSegment (KERNEL.23)
609 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
611 TRACE("%04x\n", handle
);
612 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
613 if (!VALID_HANDLE(handle
)) {
614 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
617 GET_ARENA_PTR(handle
)->lockCount
++;
622 /***********************************************************************
623 * UnlockSegment (KERNEL.24)
625 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
627 TRACE("%04x\n", handle
);
628 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
629 if (!VALID_HANDLE(handle
)) {
630 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
633 GET_ARENA_PTR(handle
)->lockCount
--;
634 /* FIXME: this ought to return the lock count in CX (go figure...) */
638 /***********************************************************************
639 * GlobalCompact (KERNEL.25)
641 DWORD WINAPI
GlobalCompact16( DWORD desired
)
643 return GLOBAL_MAX_ALLOC_SIZE
;
647 /***********************************************************************
648 * GlobalFreeAll (KERNEL.26)
650 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
655 pArena
= pGlobalArena
;
656 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
658 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
659 GlobalFree16( pArena
->handle
);
664 /***********************************************************************
665 * GlobalWire (KERNEL.111)
666 * GlobalWire16 (KERNEL32.29)
668 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
670 return WIN16_GlobalLock16( handle
);
674 /***********************************************************************
675 * GlobalUnWire (KERNEL.112)
676 * GlobalUnWire16 (KERNEL32.30)
678 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
680 return !GlobalUnlock16( handle
);
684 /***********************************************************************
685 * SetSwapAreaSize (KERNEL.106)
687 LONG WINAPI
SetSwapAreaSize16( WORD size
)
689 FIXME("(%d) - stub!\n", size
);
690 return MAKELONG( size
, 0xffff );
694 /***********************************************************************
695 * GlobalLRUOldest (KERNEL.163)
697 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
699 TRACE("%04x\n", handle
);
700 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
705 /***********************************************************************
706 * GlobalLRUNewest (KERNEL.164)
708 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
710 TRACE("%04x\n", handle
);
711 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
716 /***********************************************************************
717 * GetFreeSpace (KERNEL.169)
719 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
722 GlobalMemoryStatus( &ms
);
723 return ms
.dwAvailVirtual
;
726 /***********************************************************************
727 * GlobalDOSAlloc (KERNEL.184)
729 * Address (HW=Paragraph segment; LW=Selector)
731 DWORD WINAPI
GlobalDOSAlloc16(
732 DWORD size
/* [in] Number of bytes to be allocated */
735 LPVOID lpBlock
= DOSMEM_GetBlock( size
, &uParagraph
);
739 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
743 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
744 pArena
= GET_ARENA_PTR(wSelector
);
745 pArena
->flags
|= GA_DOSMEM
;
746 return MAKELONG(wSelector
,uParagraph
);
752 /***********************************************************************
753 * GlobalDOSFree (KERNEL.185)
758 WORD WINAPI
GlobalDOSFree16(
759 WORD sel
/* [in] Selector */
761 DWORD block
= GetSelectorBase(sel
);
763 if( block
&& block
< 0x100000 )
765 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
766 if( DOSMEM_FreeBlock( lpBlock
) )
767 GLOBAL_FreeBlock( sel
);
774 /***********************************************************************
775 * GlobalPageLock (KERNEL.191)
776 * GlobalSmartPageLock(KERNEL.230)
778 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
780 TRACE("%04x\n", handle
);
781 if (!VALID_HANDLE(handle
)) {
782 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
785 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
789 /***********************************************************************
790 * GlobalPageUnlock (KERNEL.192)
791 * GlobalSmartPageUnlock(KERNEL.231)
793 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
795 TRACE("%04x\n", handle
);
796 if (!VALID_HANDLE(handle
)) {
797 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
800 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
804 /***********************************************************************
805 * GlobalFix (KERNEL.197)
806 * GlobalFix16 (KERNEL32.27)
808 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
810 TRACE("%04x\n", handle
);
811 if (!VALID_HANDLE(handle
)) {
812 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
815 GET_ARENA_PTR(handle
)->lockCount
++;
817 return GlobalHandleToSel16(handle
);
821 /***********************************************************************
822 * GlobalUnfix (KERNEL.198)
823 * GlobalUnfix16 (KERNEL32.28)
825 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
827 TRACE("%04x\n", handle
);
828 if (!VALID_HANDLE(handle
)) {
829 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
832 GET_ARENA_PTR(handle
)->lockCount
--;
836 /***********************************************************************
837 * FarSetOwner (KERNEL.403)
839 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
841 if (!VALID_HANDLE(handle
)) {
842 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
845 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
849 /***********************************************************************
850 * FarGetOwner (KERNEL.404)
852 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
854 if (!VALID_HANDLE(handle
)) {
855 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
858 return GET_ARENA_PTR(handle
)->hOwner
;
862 /***********************************************************************
863 * GlobalHandleToSel (TOOLHELP.50)
865 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
867 if (!handle
) return 0;
868 if (!VALID_HANDLE(handle
)) {
869 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
874 WARN("Program attempted invalid selector conversion\n" );
881 /***********************************************************************
882 * GlobalFirst (TOOLHELP.51)
884 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
886 if (wFlags
== GLOBAL_LRU
) return FALSE
;
888 return GlobalNext16( pGlobal
, wFlags
);
892 /***********************************************************************
893 * GlobalNext (TOOLHELP.52)
895 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
899 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
900 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
901 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
904 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
905 if (pArena
->size
== 0) break; /* block is free */
906 if (i
>= globalArenaSize
) return FALSE
;
910 pGlobal
->dwAddress
= pArena
->base
;
911 pGlobal
->dwBlockSize
= pArena
->size
;
912 pGlobal
->hBlock
= pArena
->handle
;
913 pGlobal
->wcLock
= pArena
->lockCount
;
914 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
915 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
916 pGlobal
->wHeapPresent
= FALSE
;
917 pGlobal
->hOwner
= pArena
->hOwner
;
918 pGlobal
->wType
= GT_UNKNOWN
;
925 /***********************************************************************
926 * GlobalInfo (TOOLHELP.53)
928 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
933 pInfo
->wcItems
= globalArenaSize
;
934 pInfo
->wcItemsFree
= 0;
935 pInfo
->wcItemsLRU
= 0;
936 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
937 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
942 /***********************************************************************
943 * GlobalEntryHandle (TOOLHELP.54)
945 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
947 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
949 pGlobal
->dwAddress
= pArena
->base
;
950 pGlobal
->dwBlockSize
= pArena
->size
;
951 pGlobal
->hBlock
= pArena
->handle
;
952 pGlobal
->wcLock
= pArena
->lockCount
;
953 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
954 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
955 pGlobal
->wHeapPresent
= FALSE
;
956 pGlobal
->hOwner
= pArena
->hOwner
;
957 pGlobal
->wType
= GT_UNKNOWN
;
964 /***********************************************************************
965 * GlobalEntryModule (TOOLHELP.55)
967 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
970 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
975 /***********************************************************************
976 * MemManInfo (TOOLHELP.72)
978 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
983 * Not unsurprisingly although the documention says you
984 * _must_ provide the size in the dwSize field, this function
985 * (under Windows) always fills the structure and returns true.
987 GlobalMemoryStatus( &status
);
988 info
->wPageSize
= getpagesize();
989 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
990 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
991 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
992 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
993 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
994 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
995 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
996 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
997 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1001 /***********************************************************************
1002 * GetFreeMemInfo (KERNEL.316)
1004 DWORD WINAPI
GetFreeMemInfo16(void)
1007 MemManInfo16( &info
);
1008 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1012 * Win32 Global heap functions (GlobalXXX).
1013 * These functions included in Win32 for compatibility with 16 bit Windows
1014 * Especially the moveable blocks and handles are oldish.
1015 * But the ability to directly allocate memory with GPTR and LPTR is widely
1018 * The handle stuff looks horrible, but it's implemented almost like Win95
1023 #define MAGIC_GLOBAL_USED 0x5342
1024 #define GLOBAL_LOCK_MAX 0xFF
1025 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1026 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1027 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
1028 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1029 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1031 typedef struct __GLOBAL32_INTERN
1034 LPVOID Pointer WINE_PACKED
;
1037 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
1040 /***********************************************************************
1041 * GlobalAlloc (KERNEL32.@)
1046 HGLOBAL WINAPI
GlobalAlloc(
1047 UINT flags
, /* [in] Object allocation attributes */
1048 SIZE_T size
/* [in] Number of bytes to allocate */
1050 PGLOBAL32_INTERN pintern
;
1054 if(flags
&GMEM_ZEROINIT
)
1055 hpflags
=HEAP_ZERO_MEMORY
;
1059 TRACE("() flags=%04x\n", flags
);
1061 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1063 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1064 return (HGLOBAL
) palloc
;
1068 /* HeapLock(heap); */
1070 pintern
=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1071 if (!pintern
) return 0;
1074 if (!(palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
+sizeof(HGLOBAL
)))) {
1075 HeapFree(GetProcessHeap(), 0, pintern
);
1078 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1079 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1082 pintern
->Pointer
=NULL
;
1083 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1084 pintern
->Flags
=flags
>>8;
1085 pintern
->LockCount
=0;
1087 /* HeapUnlock(heap); */
1089 return INTERN_TO_HANDLE(pintern
);
1094 /***********************************************************************
1095 * GlobalLock (KERNEL32.@)
1097 * Pointer to first byte of block
1100 LPVOID WINAPI
GlobalLock(
1101 HGLOBAL hmem
/* [in] Handle of global memory object */
1103 PGLOBAL32_INTERN pintern
;
1107 return (LPVOID
) hmem
;
1109 /* HeapLock(GetProcessHeap()); */
1111 pintern
=HANDLE_TO_INTERN(hmem
);
1112 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1114 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1115 pintern
->LockCount
++;
1116 palloc
=pintern
->Pointer
;
1120 WARN("invalid handle\n");
1121 palloc
=(LPVOID
) NULL
;
1122 SetLastError(ERROR_INVALID_HANDLE
);
1124 /* HeapUnlock(GetProcessHeap()); */;
1129 /***********************************************************************
1130 * GlobalUnlock (KERNEL32.@)
1132 * TRUE: Object is still locked
1133 * FALSE: Object is unlocked
1135 BOOL WINAPI
GlobalUnlock(
1136 HGLOBAL hmem
/* [in] Handle of global memory object */
1138 PGLOBAL32_INTERN pintern
;
1141 if (ISPOINTER(hmem
)) return FALSE
;
1145 /* HeapLock(GetProcessHeap()); */
1146 pintern
=HANDLE_TO_INTERN(hmem
);
1147 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1149 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1150 pintern
->LockCount
--;
1152 locked
= (pintern
->LockCount
!= 0);
1153 if (!locked
) SetLastError(NO_ERROR
);
1157 WARN("invalid handle\n");
1158 SetLastError(ERROR_INVALID_HANDLE
);
1161 /* HeapUnlock(GetProcessHeap()); */
1163 __EXCEPT(page_fault
)
1165 ERR("page fault occurred ! Caused by bug ?\n");
1166 SetLastError( ERROR_INVALID_PARAMETER
);
1174 /***********************************************************************
1175 * GlobalHandle (KERNEL32.@)
1176 * Returns the handle associated with the specified pointer.
1182 HGLOBAL WINAPI
GlobalHandle(
1183 LPCVOID pmem
/* [in] Pointer to global memory block */
1186 PGLOBAL32_INTERN maybe_intern
;
1191 SetLastError( ERROR_INVALID_PARAMETER
);
1199 /* note that if pmem is a pointer to a a block allocated by */
1200 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1202 if (ISPOINTER(pmem
)) {
1203 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
1204 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
1207 handle
= POINTER_TO_HANDLE(pmem
);
1209 handle
= (HGLOBAL
)pmem
;
1211 /* Now test handle either passed in or retrieved from pointer */
1212 maybe_intern
= HANDLE_TO_INTERN( handle
);
1213 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1214 test
= maybe_intern
->Pointer
;
1215 if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL
*)test
)-1 ) && /* obj(-handle) valid arena? */
1216 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
1217 break; /* valid moveable block */
1220 SetLastError( ERROR_INVALID_HANDLE
);
1222 __EXCEPT(page_fault
)
1224 SetLastError( ERROR_INVALID_HANDLE
);
1233 /***********************************************************************
1234 * GlobalReAlloc (KERNEL32.@)
1239 HGLOBAL WINAPI
GlobalReAlloc(
1240 HGLOBAL hmem
, /* [in] Handle of global memory object */
1241 SIZE_T size
, /* [in] New size of block */
1242 UINT flags
/* [in] How to reallocate object */
1246 PGLOBAL32_INTERN pintern
;
1247 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1250 /* HeapLock(heap); */
1251 if(flags
& GMEM_MODIFY
) /* modify flags */
1253 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1255 /* make a fixed block moveable
1256 * actually only NT is able to do this. But it's soo simple
1260 ERR("GlobalReAlloc32 with null handle!\n");
1261 SetLastError( ERROR_NOACCESS
);
1264 size
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1265 hnew
=GlobalAlloc( flags
, size
);
1266 palloc
=GlobalLock(hnew
);
1267 memcpy(palloc
, (LPVOID
) hmem
, size
);
1271 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1273 /* change the flags to make our block "discardable" */
1274 pintern
=HANDLE_TO_INTERN(hmem
);
1275 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1280 SetLastError(ERROR_INVALID_PARAMETER
);
1288 /* reallocate fixed memory */
1289 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), heap_flags
, (LPVOID
) hmem
, size
);
1293 /* reallocate a moveable block */
1294 pintern
=HANDLE_TO_INTERN(hmem
);
1297 /* Apparently Windows doesn't care whether the handle is locked at this point */
1298 /* See also the same comment in GlobalFree() */
1299 if(pintern
->LockCount
>1) {
1300 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1301 SetLastError(ERROR_INVALID_HANDLE
);
1307 if(pintern
->Pointer
)
1309 if((palloc
= HeapReAlloc(GetProcessHeap(), heap_flags
,
1310 (char *) pintern
->Pointer
-sizeof(HGLOBAL
),
1311 size
+sizeof(HGLOBAL
))) == NULL
)
1312 return 0; /* Block still valid */
1313 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1317 if((palloc
=HeapAlloc(GetProcessHeap(), heap_flags
, size
+sizeof(HGLOBAL
)))
1320 *(HGLOBAL
*)palloc
=hmem
;
1321 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1326 if(pintern
->Pointer
)
1328 HeapFree(GetProcessHeap(), 0, (char *) pintern
->Pointer
-sizeof(HGLOBAL
));
1329 pintern
->Pointer
=NULL
;
1334 /* HeapUnlock(heap); */
1339 /***********************************************************************
1340 * GlobalFree (KERNEL32.@)
1345 HGLOBAL WINAPI
GlobalFree(
1346 HGLOBAL hmem
/* [in] Handle of global memory object */
1348 PGLOBAL32_INTERN pintern
;
1354 if(ISPOINTER(hmem
)) /* POINTER */
1356 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1360 /* HeapLock(heap); */
1361 pintern
=HANDLE_TO_INTERN(hmem
);
1363 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1366 /* WIN98 does not make this test. That is you can free a */
1367 /* block you have not unlocked. Go figure!! */
1368 /* if(pintern->LockCount!=0) */
1369 /* SetLastError(ERROR_INVALID_HANDLE); */
1371 if(pintern
->Pointer
)
1372 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1374 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1377 /* HeapUnlock(heap); */
1380 __EXCEPT(page_fault
)
1382 ERR("page fault occurred ! Caused by bug ?\n");
1383 SetLastError( ERROR_INVALID_PARAMETER
);
1391 /***********************************************************************
1392 * GlobalSize (KERNEL32.@)
1394 * Size in bytes of the global memory object
1397 SIZE_T WINAPI
GlobalSize(
1398 HGLOBAL hmem
/* [in] Handle of global memory object */
1401 PGLOBAL32_INTERN pintern
;
1405 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1409 /* HeapLock(heap); */
1410 pintern
=HANDLE_TO_INTERN(hmem
);
1412 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1414 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1416 retval
=HeapSize(GetProcessHeap(), 0,
1417 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1418 if (retval
== 0xffffffff-4) retval
= 0;
1422 WARN("invalid handle\n");
1425 /* HeapUnlock(heap); */
1427 /* HeapSize returns 0xffffffff on failure */
1428 if (retval
== 0xffffffff) retval
= 0;
1433 /***********************************************************************
1434 * GlobalWire (KERNEL32.@)
1436 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1438 return GlobalLock( hmem
);
1442 /***********************************************************************
1443 * GlobalUnWire (KERNEL32.@)
1445 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1447 return GlobalUnlock( hmem
);
1451 /***********************************************************************
1452 * GlobalFix (KERNEL32.@)
1454 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1460 /***********************************************************************
1461 * GlobalUnfix (KERNEL32.@)
1463 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1465 GlobalUnlock( hmem
);
1469 /***********************************************************************
1470 * GlobalFlags (KERNEL32.@)
1471 * Returns information about the specified global memory object
1474 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1477 * Value specifying allocation flags and lock count
1478 * GMEM_INVALID_HANDLE: Failure
1480 UINT WINAPI
GlobalFlags(
1481 HGLOBAL hmem
/* [in] Handle to global memory object */
1484 PGLOBAL32_INTERN pintern
;
1492 /* HeapLock(GetProcessHeap()); */
1493 pintern
=HANDLE_TO_INTERN(hmem
);
1494 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1496 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1497 if(pintern
->Pointer
==0)
1498 retval
|= GMEM_DISCARDED
;
1502 WARN("Invalid handle: %04x\n", hmem
);
1505 /* HeapUnlock(GetProcessHeap()); */
1511 /***********************************************************************
1512 * GlobalCompact (KERNEL32.@)
1514 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
1516 return 0; /* GlobalCompact does nothing in Win32 */
1520 /***********************************************************************
1521 * GlobalMemoryStatus (KERNEL32.@)
1525 VOID WINAPI
GlobalMemoryStatus(
1526 LPMEMORYSTATUS lpmem
1528 static MEMORYSTATUS cached_memstatus
;
1529 static int cache_lastchecked
= 0;
1538 if (time(NULL
)==cache_lastchecked
) {
1539 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1542 cache_lastchecked
= time(NULL
);
1544 lpmem
->dwMemoryLoad
= 0;
1545 lpmem
->dwTotalPhys
= 16*1024*1024;
1546 lpmem
->dwAvailPhys
= 16*1024*1024;
1547 lpmem
->dwTotalPageFile
= 16*1024*1024;
1548 lpmem
->dwAvailPageFile
= 16*1024*1024;
1551 f
= fopen( "/proc/meminfo", "r" );
1555 int total
, used
, free
, shared
, buffers
, cached
;
1557 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1558 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1559 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1560 while (fgets( buffer
, sizeof(buffer
), f
))
1562 /* old style /proc/meminfo ... */
1563 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1565 lpmem
->dwTotalPhys
+= total
;
1566 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1568 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1570 lpmem
->dwTotalPageFile
+= total
;
1571 lpmem
->dwAvailPageFile
+= free
;
1574 /* new style /proc/meminfo ... */
1575 if (sscanf(buffer
, "MemTotal: %d", &total
))
1576 lpmem
->dwTotalPhys
= total
*1024;
1577 if (sscanf(buffer
, "MemFree: %d", &free
))
1578 lpmem
->dwAvailPhys
= free
*1024;
1579 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1580 lpmem
->dwTotalPageFile
= total
*1024;
1581 if (sscanf(buffer
, "SwapFree: %d", &free
))
1582 lpmem
->dwAvailPageFile
= free
*1024;
1583 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1584 lpmem
->dwAvailPhys
+= buffers
*1024;
1585 if (sscanf(buffer
, "Cached: %d", &cached
))
1586 lpmem
->dwAvailPhys
+= cached
*1024;
1590 if (lpmem
->dwTotalPhys
)
1592 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1593 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1594 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1595 / (TotalPhysical
/ 100);
1598 #elif defined(__FreeBSD__)
1599 sysctlbyname("hw.physmem", NULL
, &size_sys
, NULL
, 0);
1600 tmp
= malloc(size_sys
* sizeof(int));
1601 sysctlbyname("hw.physmem", tmp
, &size_sys
, NULL
, 0);
1604 lpmem
->dwTotalPhys
= *tmp
;
1606 sysctlbyname("hw.usermem", NULL
, &size_sys
, NULL
, 0);
1607 tmp
= malloc(size_sys
* sizeof(int));
1608 sysctlbyname("hw.usermem", tmp
, &size_sys
, NULL
, 0);
1611 lpmem
->dwAvailPhys
= *tmp
;
1612 lpmem
->dwTotalPageFile
= *tmp
;
1613 lpmem
->dwAvailPageFile
= *tmp
;
1614 lpmem
->dwMemoryLoad
= lpmem
->dwTotalPhys
- lpmem
->dwAvailPhys
;
1617 lpmem
->dwAvailPhys
= lpmem
->dwTotalPhys
;
1618 lpmem
->dwTotalPageFile
= lpmem
->dwTotalPhys
;
1619 lpmem
->dwAvailPageFile
= lpmem
->dwTotalPhys
;
1620 lpmem
->dwMemoryLoad
= 0;
1626 /* FIXME: should do something for other systems */
1628 lpmem
->dwTotalVirtual
= (char*)si
.lpMaximumApplicationAddress
-(char*)si
.lpMinimumApplicationAddress
;
1629 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1630 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1631 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1633 /* it appears some memory display programs want to divide by these values */
1634 if(lpmem
->dwTotalPageFile
==0)
1635 lpmem
->dwTotalPageFile
++;
1637 if(lpmem
->dwAvailPageFile
==0)
1638 lpmem
->dwAvailPageFile
++;
1640 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1641 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1642 lpmem
->dwLength
, lpmem
->dwMemoryLoad
, lpmem
->dwTotalPhys
, lpmem
->dwAvailPhys
,
1643 lpmem
->dwTotalPageFile
, lpmem
->dwAvailPageFile
, lpmem
->dwTotalVirtual
,
1644 lpmem
->dwAvailVirtual
);
1647 /***********************************************************************
1648 * A20Proc (KERNEL.165)
1649 * A20_Proc (SYSTEM.20)
1651 void WINAPI
A20Proc16( WORD unused
)
1653 /* this is also a NOP in Windows */
1656 /***********************************************************************
1657 * LimitEMSPages (KERNEL.156)
1659 DWORD WINAPI
LimitEMSPages16( DWORD unused
)