Fixed some 0 vs. FALSE mismatches.
[wine/hacks.git] / memory / global.c
blob7fb7a3d9a3d9d8cba5888757cfdded8d3bca8403
1 /*
2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
5 */
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
8 #include <sys/types.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/port.h"
18 #include "global.h"
19 #include "heap.h"
20 #include "toolhelp.h"
21 #include "selectors.h"
22 #include "miscemu.h"
23 #include "stackframe.h"
24 #include "module.h"
25 #include "debugtools.h"
26 #include "winerror.h"
28 DEFAULT_DEBUG_CHANNEL(global);
30 /* Global arena block */
31 typedef struct
33 DWORD base; /* Base address (0 if discarded) */
34 DWORD size; /* Size in bytes (0 indicates a free block) */
35 HGLOBAL16 handle; /* Handle for this block */
36 HGLOBAL16 hOwner; /* Owner of this block */
37 BYTE lockCount; /* Count of GlobalFix() calls */
38 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
39 BYTE flags; /* Allocation flags */
40 BYTE selCount; /* Number of selectors allocated for this block */
41 } GLOBALARENA;
43 /* Flags definitions */
44 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
45 #define GA_DGROUP 0x04
46 #define GA_DISCARDABLE 0x08
47 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
48 #define GA_DOSMEM 0x20
50 /* Arena array */
51 static GLOBALARENA *pGlobalArena = NULL;
52 static int globalArenaSize = 0;
54 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
56 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
57 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
59 /* filter for page-fault exceptions */
60 /* It is possible for a bogus global pointer to cause a */
61 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
63 static WINE_EXCEPTION_FILTER(page_fault)
65 switch (GetExceptionCode()) {
66 case (EXCEPTION_ACCESS_VIOLATION):
67 case (EXCEPTION_PRIV_INSTRUCTION):
68 return EXCEPTION_EXECUTE_HANDLER;
69 default:
70 return EXCEPTION_CONTINUE_SEARCH;
74 /***********************************************************************
75 * GLOBAL_GetArena
77 * Return the arena for a given selector, growing the arena array if needed.
79 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
81 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
83 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
84 GLOBALARENA *pNewArena = realloc( pGlobalArena,
85 newsize * sizeof(GLOBALARENA) );
86 if (!pNewArena) return 0;
87 pGlobalArena = pNewArena;
88 memset( pGlobalArena + globalArenaSize, 0,
89 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
90 globalArenaSize = newsize;
92 return pGlobalArena + (sel >> __AHSHIFT);
95 void debug_handles(void)
97 int printed=0;
98 int i;
99 for (i = globalArenaSize-1 ; i>=0 ; i--) {
100 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
101 printed=1;
102 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
105 if (printed)
106 DPRINTF("\n");
110 /***********************************************************************
111 * GLOBAL_CreateBlock
113 * Create a global heap block for a fixed range of linear memory.
115 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
116 HGLOBAL16 hOwner, unsigned char selflags )
118 WORD sel, selcount;
119 GLOBALARENA *pArena;
121 /* Allocate the selector(s) */
123 sel = SELECTOR_AllocBlock( ptr, size, selflags );
124 if (!sel) return 0;
125 selcount = (size + 0xffff) / 0x10000;
127 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
129 SELECTOR_FreeBlock( sel );
130 return 0;
133 /* Fill the arena block */
135 pArena->base = (DWORD)ptr;
136 pArena->size = GetSelectorLimit16(sel) + 1;
137 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
138 pArena->hOwner = hOwner;
139 pArena->lockCount = 0;
140 pArena->pageLockCount = 0;
141 pArena->flags = flags & GA_MOVEABLE;
142 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
143 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
144 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
145 pArena->selCount = selcount;
146 if (selcount > 1) /* clear the next arena blocks */
147 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
149 return pArena->handle;
153 /***********************************************************************
154 * GLOBAL_FreeBlock
156 * Free a block allocated by GLOBAL_CreateBlock, without touching
157 * the associated linear memory range.
159 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
161 WORD sel;
162 GLOBALARENA *pArena;
164 if (!handle) return TRUE;
165 sel = GlobalHandleToSel16( handle );
166 if (!VALID_HANDLE(sel))
167 return FALSE;
168 pArena = GET_ARENA_PTR(sel);
169 SELECTOR_FreeBlock( sel );
170 memset( pArena, 0, sizeof(GLOBALARENA) );
171 return TRUE;
174 /***********************************************************************
175 * GLOBAL_MoveBlock
177 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
179 WORD sel;
180 GLOBALARENA *pArena;
182 if (!handle) return TRUE;
183 sel = GlobalHandleToSel16( handle );
184 if (!VALID_HANDLE(sel))
185 return FALSE;
186 pArena = GET_ARENA_PTR(sel);
187 if (pArena->selCount != 1)
188 return FALSE;
190 pArena->base = (DWORD)ptr;
191 pArena->size = size;
192 SELECTOR_ReallocBlock( sel, ptr, size );
193 return TRUE;
196 /***********************************************************************
197 * GLOBAL_Alloc
199 * Implementation of GlobalAlloc16()
201 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
203 void *ptr;
204 HGLOBAL16 handle;
206 TRACE("%ld flags=%04x\n", size, flags );
208 /* If size is 0, create a discarded block */
210 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
212 /* Fixup the size */
214 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
215 size = (size + 0x1f) & ~0x1f;
217 /* Allocate the linear memory */
218 ptr = HeapAlloc( GetProcessHeap(), 0, size );
219 /* FIXME: free discardable blocks and try again? */
220 if (!ptr) return 0;
222 /* Allocate the selector(s) */
224 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
225 if (!handle)
227 HeapFree( GetProcessHeap(), 0, ptr );
228 return 0;
231 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
232 return handle;
235 /***********************************************************************
236 * GlobalAlloc16 (KERNEL.15)
237 * RETURNS
238 * Handle: Success
239 * NULL: Failure
241 HGLOBAL16 WINAPI GlobalAlloc16(
242 UINT16 flags, /* [in] Object allocation attributes */
243 DWORD size /* [in] Number of bytes to allocate */
245 HANDLE16 owner = GetCurrentPDB16();
247 if (flags & GMEM_DDESHARE)
248 owner = GetExePtr(owner); /* Make it a module handle */
249 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
253 /***********************************************************************
254 * GlobalReAlloc16 (KERNEL.16)
255 * RETURNS
256 * Handle: Success
257 * NULL: Failure
259 HGLOBAL16 WINAPI GlobalReAlloc16(
260 HGLOBAL16 handle, /* [in] Handle of global memory object */
261 DWORD size, /* [in] New size of block */
262 UINT16 flags /* [in] How to reallocate object */
264 WORD selcount;
265 DWORD oldsize;
266 void *ptr;
267 GLOBALARENA *pArena, *pNewArena;
268 WORD sel = GlobalHandleToSel16( handle );
270 TRACE("%04x %ld flags=%04x\n",
271 handle, size, flags );
272 if (!handle) return 0;
274 if (!VALID_HANDLE(handle)) {
275 WARN("Invalid handle 0x%04x!\n", handle);
276 return 0;
278 pArena = GET_ARENA_PTR( handle );
280 /* Discard the block if requested */
282 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
284 if (!(pArena->flags & GA_MOVEABLE) ||
285 !(pArena->flags & GA_DISCARDABLE) ||
286 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
287 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
288 pArena->base = 0;
290 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
291 * change the selector if we are shrinking the block.
292 * FIXME: shouldn't we keep selectors until the block is deleted?
294 SELECTOR_ReallocBlock( sel, 0, 1 );
295 return handle;
298 /* Fixup the size */
300 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
301 if (size == 0) size = 0x20;
302 else size = (size + 0x1f) & ~0x1f;
304 /* Change the flags */
306 if (flags & GMEM_MODIFY)
308 /* Change the flags, leaving GA_DGROUP alone */
309 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
310 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
311 return handle;
314 /* Reallocate the linear memory */
316 ptr = (void *)pArena->base;
317 oldsize = pArena->size;
318 TRACE("oldsize %08lx\n",oldsize);
319 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
321 if (pArena->flags & GA_DOSMEM)
322 ptr = DOSMEM_ResizeBlock(ptr, size, NULL);
323 else
324 ptr = HeapReAlloc( GetProcessHeap(), 0, ptr, size );
325 if (!ptr)
327 SELECTOR_FreeBlock( sel );
328 memset( pArena, 0, sizeof(GLOBALARENA) );
329 return 0;
332 /* Reallocate the selector(s) */
334 sel = SELECTOR_ReallocBlock( sel, ptr, size );
335 if (!sel)
337 HeapFree( GetProcessHeap(), 0, ptr );
338 memset( pArena, 0, sizeof(GLOBALARENA) );
339 return 0;
341 selcount = (size + 0xffff) / 0x10000;
343 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
345 HeapFree( GetProcessHeap(), 0, ptr );
346 SELECTOR_FreeBlock( sel );
347 return 0;
350 /* Fill the new arena block */
352 if (pNewArena != pArena) memcpy( pNewArena, pArena, sizeof(GLOBALARENA) );
353 pNewArena->base = (DWORD)ptr;
354 pNewArena->size = GetSelectorLimit16(sel) + 1;
355 pNewArena->selCount = selcount;
356 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
358 if (selcount > 1) /* clear the next arena blocks */
359 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
361 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
362 memset( (char *)ptr + oldsize, 0, size - oldsize );
363 return pNewArena->handle;
367 /***********************************************************************
368 * GlobalFree16 (KERNEL.17)
369 * RETURNS
370 * NULL: Success
371 * Handle: Failure
373 HGLOBAL16 WINAPI GlobalFree16(
374 HGLOBAL16 handle /* [in] Handle of global memory object */
376 void *ptr;
378 if (!VALID_HANDLE(handle)) {
379 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
380 return 0;
382 ptr = (void *)GET_ARENA_PTR(handle)->base;
384 TRACE("%04x\n", handle );
385 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
386 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
387 return 0;
391 /***********************************************************************
392 * WIN16_GlobalLock16 (KERNEL.18)
394 * This is the GlobalLock16() function used by 16-bit code.
396 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
398 WORD sel = GlobalHandleToSel16( handle );
399 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
401 if (handle)
403 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
405 if (!VALID_HANDLE(handle)) {
406 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
407 sel = 0;
409 else if (!GET_ARENA_PTR(handle)->base)
410 sel = 0;
411 else
412 GET_ARENA_PTR(handle)->lockCount++;
415 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
416 return MAKESEGPTR( sel, 0 );
420 /**********************************************************************
421 * K32WOWGlobalLock16 (WOW32.11) (KERNEL32.60)
423 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
425 return WIN16_GlobalLock16( hMem );
429 /***********************************************************************
430 * GlobalLock16 (KERNEL.18)
432 * This is the GlobalLock16() function used by 32-bit code.
434 * RETURNS
435 * Pointer to first byte of memory block
436 * NULL: Failure
438 LPVOID WINAPI GlobalLock16(
439 HGLOBAL16 handle /* [in] Handle of global memory object */
441 if (!handle) return 0;
442 if (!VALID_HANDLE(handle))
443 return (LPVOID)0;
444 GET_ARENA_PTR(handle)->lockCount++;
445 return (LPVOID)GET_ARENA_PTR(handle)->base;
449 /***********************************************************************
450 * GlobalUnlock16 (KERNEL.19)
451 * NOTES
452 * Should the return values be cast to booleans?
454 * RETURNS
455 * TRUE: Object is still locked
456 * FALSE: Object is unlocked
458 BOOL16 WINAPI GlobalUnlock16(
459 HGLOBAL16 handle /* [in] Handle of global memory object */
461 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
462 if (!VALID_HANDLE(handle)) {
463 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
464 return 0;
466 TRACE("%04x\n", handle );
467 if (pArena->lockCount) pArena->lockCount--;
468 return pArena->lockCount;
471 /***********************************************************************
472 * GlobalChangeLockCount (KERNEL.365)
474 * This is declared as a register function as it has to preserve
475 * *all* registers, even AX/DX !
478 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
479 CONTEXT86 *context )
481 if ( delta == 1 )
482 GlobalLock16( handle );
483 else if ( delta == -1 )
484 GlobalUnlock16( handle );
485 else
486 ERR("(%04X, %d): strange delta value\n", handle, delta );
489 /***********************************************************************
490 * GlobalSize16 (KERNEL.20)
491 * RETURNS
492 * Size in bytes of object
493 * 0: Failure
495 DWORD WINAPI GlobalSize16(
496 HGLOBAL16 handle /* [in] Handle of global memory object */
498 TRACE("%04x\n", handle );
499 if (!handle) return 0;
500 if (!VALID_HANDLE(handle))
501 return 0;
502 return GET_ARENA_PTR(handle)->size;
506 /***********************************************************************
507 * GlobalHandle16 (KERNEL.21)
508 * NOTES
509 * Why is GlobalHandleToSel used here with the sel as input?
511 * RETURNS
512 * Handle: Success
513 * NULL: Failure
515 DWORD WINAPI GlobalHandle16(
516 WORD sel /* [in] Address of global memory block */
518 TRACE("%04x\n", sel );
519 if (!VALID_HANDLE(sel)) {
520 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
521 return 0;
523 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
526 /***********************************************************************
527 * GlobalHandleNoRIP (KERNEL.159)
529 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
531 int i;
532 for (i = globalArenaSize-1 ; i>=0 ; i--) {
533 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
534 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
536 return 0;
540 /***********************************************************************
541 * GlobalFlags16 (KERNEL.22)
542 * NOTES
543 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
544 * handle?
546 * RETURNS
547 * Value specifying flags and lock count
548 * GMEM_INVALID_HANDLE: Invalid handle
550 UINT16 WINAPI GlobalFlags16(
551 HGLOBAL16 handle /* [in] Handle of global memory object */
553 GLOBALARENA *pArena;
555 TRACE("%04x\n", handle );
556 if (!VALID_HANDLE(handle)) {
557 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
558 return 0;
560 pArena = GET_ARENA_PTR(handle);
561 return pArena->lockCount |
562 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
563 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
567 /***********************************************************************
568 * LockSegment16 (KERNEL.23)
570 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
572 TRACE("%04x\n", handle );
573 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
574 if (!VALID_HANDLE(handle)) {
575 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
576 return 0;
578 GET_ARENA_PTR(handle)->lockCount++;
579 return handle;
583 /***********************************************************************
584 * UnlockSegment16 (KERNEL.24)
586 void WINAPI UnlockSegment16( HGLOBAL16 handle )
588 TRACE("%04x\n", handle );
589 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
590 if (!VALID_HANDLE(handle)) {
591 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
592 return;
594 GET_ARENA_PTR(handle)->lockCount--;
595 /* FIXME: this ought to return the lock count in CX (go figure...) */
599 /***********************************************************************
600 * GlobalCompact16 (KERNEL.25)
602 DWORD WINAPI GlobalCompact16( DWORD desired )
604 return GLOBAL_MAX_ALLOC_SIZE;
608 /***********************************************************************
609 * GlobalFreeAll (KERNEL.26)
611 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
613 DWORD i;
614 GLOBALARENA *pArena;
616 pArena = pGlobalArena;
617 for (i = 0; i < globalArenaSize; i++, pArena++)
619 if ((pArena->size != 0) && (pArena->hOwner == owner))
620 GlobalFree16( pArena->handle );
625 /***********************************************************************
626 * GlobalWire16 (KERNEL.111)
628 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
630 return WIN16_GlobalLock16( handle );
634 /***********************************************************************
635 * GlobalUnWire16 (KERNEL.112)
637 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
639 return !GlobalUnlock16( handle );
643 /***********************************************************************
644 * SetSwapAreaSize16 (KERNEL.106)
646 LONG WINAPI SetSwapAreaSize16( WORD size )
648 FIXME("(%d) - stub!\n", size );
649 return MAKELONG( size, 0xffff );
653 /***********************************************************************
654 * GlobalLRUOldest (KERNEL.163)
656 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
658 TRACE("%04x\n", handle );
659 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
660 return handle;
664 /***********************************************************************
665 * GlobalLRUNewest (KERNEL.164)
667 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
669 TRACE("%04x\n", handle );
670 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
671 return handle;
675 /***********************************************************************
676 * GetFreeSpace16 (KERNEL.169)
678 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
680 MEMORYSTATUS ms;
681 GlobalMemoryStatus( &ms );
682 return ms.dwAvailVirtual;
685 /***********************************************************************
686 * GlobalDOSAlloc (KERNEL.184)
687 * RETURNS
688 * Address (HW=Paragraph segment; LW=Selector)
690 DWORD WINAPI GlobalDOSAlloc16(
691 DWORD size /* [in] Number of bytes to be allocated */
693 UINT16 uParagraph;
694 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
696 if( lpBlock )
698 HMODULE16 hModule = GetModuleHandle16("KERNEL");
699 WORD wSelector;
700 GLOBALARENA *pArena;
702 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
703 pArena = GET_ARENA_PTR(wSelector);
704 pArena->flags |= GA_DOSMEM;
705 return MAKELONG(wSelector,uParagraph);
707 return 0;
711 /***********************************************************************
712 * GlobalDOSFree (KERNEL.185)
713 * RETURNS
714 * NULL: Success
715 * sel: Failure
717 WORD WINAPI GlobalDOSFree16(
718 WORD sel /* [in] Selector */
720 DWORD block = GetSelectorBase(sel);
722 if( block && block < 0x100000 )
724 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
725 if( DOSMEM_FreeBlock( lpBlock ) )
726 GLOBAL_FreeBlock( sel );
727 sel = 0;
729 return sel;
733 /***********************************************************************
734 * GlobalPageLock (KERNEL.191)
736 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
738 TRACE("%04x\n", handle );
739 if (!VALID_HANDLE(handle)) {
740 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
741 return 0;
743 return ++(GET_ARENA_PTR(handle)->pageLockCount);
747 /***********************************************************************
748 * GlobalPageUnlock (KERNEL.192)
750 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
752 TRACE("%04x\n", handle );
753 if (!VALID_HANDLE(handle)) {
754 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
755 return 0;
757 return --(GET_ARENA_PTR(handle)->pageLockCount);
761 /***********************************************************************
762 * GlobalFix16 (KERNEL.197)
764 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
766 TRACE("%04x\n", handle );
767 if (!VALID_HANDLE(handle)) {
768 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
769 return 0;
771 GET_ARENA_PTR(handle)->lockCount++;
773 return GlobalHandleToSel16(handle);
777 /***********************************************************************
778 * GlobalUnfix16 (KERNEL.198)
780 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
782 TRACE("%04x\n", handle );
783 if (!VALID_HANDLE(handle)) {
784 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
785 return;
787 GET_ARENA_PTR(handle)->lockCount--;
791 /***********************************************************************
792 * FarSetOwner (KERNEL.403)
794 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
796 if (!VALID_HANDLE(handle)) {
797 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
798 return;
800 GET_ARENA_PTR(handle)->hOwner = hOwner;
804 /***********************************************************************
805 * FarGetOwner (KERNEL.404)
807 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
809 if (!VALID_HANDLE(handle)) {
810 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
811 return 0;
813 return GET_ARENA_PTR(handle)->hOwner;
817 /***********************************************************************
818 * GlobalHandleToSel (TOOLHELP.50)
820 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
822 if (!handle) return 0;
823 if (!VALID_HANDLE(handle)) {
824 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
825 return 0;
827 if (!(handle & 7))
829 WARN("Program attempted invalid selector conversion\n" );
830 return handle - 1;
832 return handle | 7;
836 /***********************************************************************
837 * GlobalFirst (TOOLHELP.51)
839 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
841 if (wFlags == GLOBAL_LRU) return FALSE;
842 pGlobal->dwNext = 0;
843 return GlobalNext16( pGlobal, wFlags );
847 /***********************************************************************
848 * GlobalNext (TOOLHELP.52)
850 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
852 GLOBALARENA *pArena;
854 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
855 pArena = pGlobalArena + pGlobal->dwNext;
856 if (wFlags == GLOBAL_FREE) /* only free blocks */
858 int i;
859 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
860 if (pArena->size == 0) break; /* block is free */
861 if (i >= globalArenaSize) return FALSE;
862 pGlobal->dwNext = i;
865 pGlobal->dwAddress = pArena->base;
866 pGlobal->dwBlockSize = pArena->size;
867 pGlobal->hBlock = pArena->handle;
868 pGlobal->wcLock = pArena->lockCount;
869 pGlobal->wcPageLock = pArena->pageLockCount;
870 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
871 pGlobal->wHeapPresent = FALSE;
872 pGlobal->hOwner = pArena->hOwner;
873 pGlobal->wType = GT_UNKNOWN;
874 pGlobal->wData = 0;
875 pGlobal->dwNext++;
876 return TRUE;
880 /***********************************************************************
881 * GlobalInfo (TOOLHELP.53)
883 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
885 int i;
886 GLOBALARENA *pArena;
888 pInfo->wcItems = globalArenaSize;
889 pInfo->wcItemsFree = 0;
890 pInfo->wcItemsLRU = 0;
891 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
892 if (pArena->size == 0) pInfo->wcItemsFree++;
893 return TRUE;
897 /***********************************************************************
898 * GlobalEntryHandle (TOOLHELP.54)
900 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
902 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
904 pGlobal->dwAddress = pArena->base;
905 pGlobal->dwBlockSize = pArena->size;
906 pGlobal->hBlock = pArena->handle;
907 pGlobal->wcLock = pArena->lockCount;
908 pGlobal->wcPageLock = pArena->pageLockCount;
909 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
910 pGlobal->wHeapPresent = FALSE;
911 pGlobal->hOwner = pArena->hOwner;
912 pGlobal->wType = GT_UNKNOWN;
913 pGlobal->wData = 0;
914 pGlobal->dwNext++;
915 return TRUE;
919 /***********************************************************************
920 * GlobalEntryModule (TOOLHELP.55)
922 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
923 WORD wSeg )
925 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
926 return FALSE;
930 /***********************************************************************
931 * MemManInfo (TOOLHELP.72)
933 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
935 MEMORYSTATUS status;
938 * Not unsurprisingly although the documention says you
939 * _must_ provide the size in the dwSize field, this function
940 * (under Windows) always fills the structure and returns true.
942 GlobalMemoryStatus( &status );
943 info->wPageSize = getpagesize();
944 info->dwLargestFreeBlock = status.dwAvailVirtual;
945 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
946 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
947 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
948 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
949 info->dwFreePages = info->dwMaxPagesAvailable;
950 info->dwTotalPages = info->dwTotalLinearSpace;
951 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
952 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
953 return TRUE;
956 /***********************************************************************
957 * GetFreeMemInfo (KERNEL.316)
959 DWORD WINAPI GetFreeMemInfo16(void)
961 MEMMANINFO info;
962 MemManInfo16( &info );
963 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
967 * Win32 Global heap functions (GlobalXXX).
968 * These functions included in Win32 for compatibility with 16 bit Windows
969 * Especially the moveable blocks and handles are oldish.
970 * But the ability to directly allocate memory with GPTR and LPTR is widely
971 * used.
973 * The handle stuff looks horrible, but it's implemented almost like Win95
974 * does it.
978 #define MAGIC_GLOBAL_USED 0x5342
979 #define GLOBAL_LOCK_MAX 0xFF
980 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
981 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
982 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
983 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
984 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
986 typedef struct __GLOBAL32_INTERN
988 WORD Magic;
989 LPVOID Pointer WINE_PACKED;
990 BYTE Flags;
991 BYTE LockCount;
992 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
994 /***********************************************************************
995 * GLOBAL_GetHeap
997 * Returns the appropriate heap to be used. If the object was created
998 * With GMEM_DDESHARE we allocated it on the system heap.
1000 static HANDLE GLOBAL_GetHeap( HGLOBAL hmem )
1002 HANDLE heap;
1004 TRACE("() hmem=%x\n", hmem);
1006 /* Get the appropriate heap to be used for this object */
1007 if (ISPOINTER(hmem))
1008 heap = GetProcessHeap();
1009 else
1011 PGLOBAL32_INTERN pintern;
1012 pintern=HANDLE_TO_INTERN(hmem);
1014 /* If it was DDESHARE it was created on the shared system heap */
1015 pintern=HANDLE_TO_INTERN(hmem);
1016 heap = ( pintern->Flags & (GMEM_DDESHARE >> 8) )
1017 ? SystemHeap : GetProcessHeap();
1020 return heap;
1023 /***********************************************************************
1024 * GlobalAlloc (KERNEL32.315)
1025 * RETURNS
1026 * Handle: Success
1027 * NULL: Failure
1029 HGLOBAL WINAPI GlobalAlloc(
1030 UINT flags, /* [in] Object allocation attributes */
1031 DWORD size /* [in] Number of bytes to allocate */
1033 PGLOBAL32_INTERN pintern;
1034 DWORD hpflags;
1035 LPVOID palloc;
1037 if(flags&GMEM_ZEROINIT)
1038 hpflags=HEAP_ZERO_MEMORY;
1039 else
1040 hpflags=0;
1042 TRACE("() flags=%04x\n", flags );
1044 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1046 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1047 return (HGLOBAL) palloc;
1049 else /* HANDLE */
1051 HANDLE heap;
1053 /* If DDESHARE is set, create on the shared system heap */
1054 heap = (flags & GMEM_DDESHARE) ? SystemHeap : GetProcessHeap();
1056 /* HeapLock(heap); */
1058 pintern=HeapAlloc(heap, 0, sizeof(GLOBAL32_INTERN));
1059 if (!pintern) return 0;
1060 if(size)
1062 if (!(palloc=HeapAlloc(heap, hpflags, size+sizeof(HGLOBAL)))) {
1063 HeapFree(heap, 0, pintern);
1064 return 0;
1066 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1067 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1069 else
1070 pintern->Pointer=NULL;
1071 pintern->Magic=MAGIC_GLOBAL_USED;
1072 pintern->Flags=flags>>8;
1073 pintern->LockCount=0;
1075 /* HeapUnlock(heap); */
1077 return INTERN_TO_HANDLE(pintern);
1082 /***********************************************************************
1083 * GlobalLock (KERNEL32.326)
1084 * RETURNS
1085 * Pointer to first byte of block
1086 * NULL: Failure
1088 LPVOID WINAPI GlobalLock(
1089 HGLOBAL hmem /* [in] Handle of global memory object */
1091 PGLOBAL32_INTERN pintern;
1092 LPVOID palloc;
1094 if(ISPOINTER(hmem))
1095 return (LPVOID) hmem;
1097 /* HeapLock(GetProcessHeap()); */
1099 pintern=HANDLE_TO_INTERN(hmem);
1100 if(pintern->Magic==MAGIC_GLOBAL_USED)
1102 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1103 pintern->LockCount++;
1104 palloc=pintern->Pointer;
1106 else
1108 WARN("invalid handle\n");
1109 palloc=(LPVOID) NULL;
1110 SetLastError(ERROR_INVALID_HANDLE);
1112 /* HeapUnlock(GetProcessHeap()); */;
1113 return palloc;
1117 /***********************************************************************
1118 * GlobalUnlock (KERNEL32.332)
1119 * RETURNS
1120 * TRUE: Object is still locked
1121 * FALSE: Object is unlocked
1123 BOOL WINAPI GlobalUnlock(
1124 HGLOBAL hmem /* [in] Handle of global memory object */
1126 PGLOBAL32_INTERN pintern;
1127 BOOL locked;
1129 if(ISPOINTER(hmem))
1130 return FALSE;
1132 /* HeapLock(GetProcessHeap()); */
1133 pintern=HANDLE_TO_INTERN(hmem);
1135 if(pintern->Magic==MAGIC_GLOBAL_USED)
1137 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1138 pintern->LockCount--;
1140 locked = (pintern->LockCount != 0);
1141 if (!locked) SetLastError(NO_ERROR);
1143 else
1145 WARN("invalid handle\n");
1146 SetLastError(ERROR_INVALID_HANDLE);
1147 locked=FALSE;
1149 /* HeapUnlock(GetProcessHeap()); */
1150 return locked;
1154 /***********************************************************************
1155 * GlobalHandle (KERNEL32.325)
1156 * Returns the handle associated with the specified pointer.
1158 * RETURNS
1159 * Handle: Success
1160 * NULL: Failure
1162 HGLOBAL WINAPI GlobalHandle(
1163 LPCVOID pmem /* [in] Pointer to global memory block */
1165 HGLOBAL handle;
1166 HANDLE heap;
1167 PGLOBAL32_INTERN maybe_intern;
1168 LPCVOID test;
1170 if (!pmem)
1172 SetLastError( ERROR_INVALID_PARAMETER );
1173 return 0;
1176 __TRY
1178 handle = 0;
1180 /* note that if pmem is a pointer to a a block allocated by */
1181 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1182 /* will fail. */
1183 if (ISPOINTER(pmem)) {
1184 heap = GLOBAL_GetHeap( (HGLOBAL)pmem );
1185 if (HeapValidate( heap, 0, pmem )) {
1186 handle = (HGLOBAL)pmem; /* valid fixed block */
1187 break;
1189 handle = POINTER_TO_HANDLE(pmem);
1190 } else
1191 handle = (HGLOBAL)pmem;
1193 /* Now test handle either passed in or retrieved from pointer */
1194 heap = GLOBAL_GetHeap( handle );
1195 maybe_intern = HANDLE_TO_INTERN( handle );
1196 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1197 test = maybe_intern->Pointer;
1198 if (HeapValidate( heap, 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */
1199 HeapValidate( heap, 0, maybe_intern )) /* intern valid arena? */
1200 break; /* valid moveable block */
1202 handle = 0;
1203 SetLastError( ERROR_INVALID_HANDLE );
1205 __EXCEPT(page_fault)
1207 SetLastError( ERROR_INVALID_HANDLE );
1208 return 0;
1210 __ENDTRY
1212 return handle;
1216 /***********************************************************************
1217 * GlobalReAlloc (KERNEL32.328)
1218 * RETURNS
1219 * Handle: Success
1220 * NULL: Failure
1222 HGLOBAL WINAPI GlobalReAlloc(
1223 HGLOBAL hmem, /* [in] Handle of global memory object */
1224 DWORD size, /* [in] New size of block */
1225 UINT flags /* [in] How to reallocate object */
1227 LPVOID palloc;
1228 HGLOBAL hnew;
1229 PGLOBAL32_INTERN pintern;
1230 HANDLE heap = GLOBAL_GetHeap( hmem );
1231 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1233 hnew = 0;
1234 /* HeapLock(heap); */
1235 if(flags & GMEM_MODIFY) /* modify flags */
1237 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1239 /* make a fixed block moveable
1240 * actually only NT is able to do this. But it's soo simple
1242 if (hmem == 0)
1244 ERR("GlobalReAlloc32 with null handle!\n");
1245 SetLastError( ERROR_NOACCESS );
1246 return 0;
1248 size=HeapSize(heap, 0, (LPVOID) hmem);
1249 hnew=GlobalAlloc( flags, size);
1250 palloc=GlobalLock(hnew);
1251 memcpy(palloc, (LPVOID) hmem, size);
1252 GlobalUnlock(hnew);
1253 GlobalFree(hmem);
1255 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1257 /* change the flags to make our block "discardable" */
1258 pintern=HANDLE_TO_INTERN(hmem);
1259 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1260 hnew=hmem;
1262 else
1264 SetLastError(ERROR_INVALID_PARAMETER);
1265 hnew = 0;
1268 else
1270 if(ISPOINTER(hmem))
1272 /* reallocate fixed memory */
1273 hnew=(HGLOBAL)HeapReAlloc(heap, heap_flags, (LPVOID) hmem, size);
1275 else
1277 /* reallocate a moveable block */
1278 pintern=HANDLE_TO_INTERN(hmem);
1280 #if 0
1281 /* Apparently Windows doesn't care whether the handle is locked at this point */
1282 /* See also the same comment in GlobalFree() */
1283 if(pintern->LockCount>1) {
1284 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1285 SetLastError(ERROR_INVALID_HANDLE);
1286 } else
1287 #endif
1288 if(size!=0)
1290 hnew=hmem;
1291 if(pintern->Pointer)
1293 if((palloc = HeapReAlloc(heap, heap_flags,
1294 (char *) pintern->Pointer-sizeof(HGLOBAL),
1295 size+sizeof(HGLOBAL))) == NULL)
1296 return 0; /* Block still valid */
1297 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1299 else
1301 if((palloc=HeapAlloc(heap, heap_flags, size+sizeof(HGLOBAL)))
1302 == NULL)
1303 return 0;
1304 *(HGLOBAL *)palloc=hmem;
1305 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1308 else
1310 if(pintern->Pointer)
1312 HeapFree(heap, 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1313 pintern->Pointer=NULL;
1318 /* HeapUnlock(heap); */
1319 return hnew;
1323 /***********************************************************************
1324 * GlobalFree (KERNEL32.322)
1325 * RETURNS
1326 * NULL: Success
1327 * Handle: Failure
1329 HGLOBAL WINAPI GlobalFree(
1330 HGLOBAL hmem /* [in] Handle of global memory object */
1332 PGLOBAL32_INTERN pintern;
1333 HGLOBAL hreturned = 0;
1334 HANDLE heap = GLOBAL_GetHeap( hmem );
1336 if(ISPOINTER(hmem)) /* POINTER */
1338 if(!HeapFree(heap, 0, (LPVOID) hmem)) hmem = 0;
1340 else /* HANDLE */
1342 /* HeapLock(heap); */
1343 pintern=HANDLE_TO_INTERN(hmem);
1345 if(pintern->Magic==MAGIC_GLOBAL_USED)
1348 /* WIN98 does not make this test. That is you can free a */
1349 /* block you have not unlocked. Go figure!! */
1350 /* if(pintern->LockCount!=0) */
1351 /* SetLastError(ERROR_INVALID_HANDLE); */
1353 if(pintern->Pointer)
1354 if(!HeapFree(heap, 0,
1355 (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1356 hreturned=hmem;
1357 if(!HeapFree(heap, 0, pintern))
1358 hreturned=hmem;
1360 /* HeapUnlock(heap); */
1362 return hreturned;
1366 /***********************************************************************
1367 * GlobalSize (KERNEL32.329)
1368 * RETURNS
1369 * Size in bytes of the global memory object
1370 * 0: Failure
1372 DWORD WINAPI GlobalSize(
1373 HGLOBAL hmem /* [in] Handle of global memory object */
1375 DWORD retval;
1376 PGLOBAL32_INTERN pintern;
1377 HANDLE heap = GLOBAL_GetHeap( hmem );
1379 if(ISPOINTER(hmem))
1381 retval=HeapSize(heap, 0, (LPVOID) hmem);
1383 else
1385 /* HeapLock(heap); */
1386 pintern=HANDLE_TO_INTERN(hmem);
1388 if(pintern->Magic==MAGIC_GLOBAL_USED)
1390 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1391 return 0;
1392 retval=HeapSize(heap, 0,
1393 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1394 if (retval == 0xffffffff-4) retval = 0;
1396 else
1398 WARN("invalid handle\n");
1399 retval=0;
1401 /* HeapUnlock(heap); */
1403 /* HeapSize returns 0xffffffff on failure */
1404 if (retval == 0xffffffff) retval = 0;
1405 return retval;
1409 /***********************************************************************
1410 * GlobalWire (KERNEL32.333)
1412 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1414 return GlobalLock( hmem );
1418 /***********************************************************************
1419 * GlobalUnWire (KERNEL32.330)
1421 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1423 return GlobalUnlock( hmem);
1427 /***********************************************************************
1428 * GlobalFix (KERNEL32.320)
1430 VOID WINAPI GlobalFix(HGLOBAL hmem)
1432 GlobalLock( hmem );
1436 /***********************************************************************
1437 * GlobalUnfix (KERNEL32.331)
1439 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1441 GlobalUnlock( hmem);
1445 /***********************************************************************
1446 * GlobalFlags (KERNEL32.321)
1447 * Returns information about the specified global memory object
1449 * NOTES
1450 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1452 * RETURNS
1453 * Value specifying allocation flags and lock count
1454 * GMEM_INVALID_HANDLE: Failure
1456 UINT WINAPI GlobalFlags(
1457 HGLOBAL hmem /* [in] Handle to global memory object */
1459 DWORD retval;
1460 PGLOBAL32_INTERN pintern;
1462 if(ISPOINTER(hmem))
1464 retval=0;
1466 else
1468 /* HeapLock(GetProcessHeap()); */
1469 pintern=HANDLE_TO_INTERN(hmem);
1470 if(pintern->Magic==MAGIC_GLOBAL_USED)
1472 retval=pintern->LockCount + (pintern->Flags<<8);
1473 if(pintern->Pointer==0)
1474 retval|= GMEM_DISCARDED;
1476 else
1478 WARN("Invalid handle: %04x", hmem);
1479 retval=0;
1481 /* HeapUnlock(GetProcessHeap()); */
1483 return retval;
1487 /***********************************************************************
1488 * GlobalCompact (KERNEL32.316)
1490 DWORD WINAPI GlobalCompact( DWORD minfree )
1492 return 0; /* GlobalCompact does nothing in Win32 */
1496 /***********************************************************************
1497 * GlobalMemoryStatus (KERNEL32.327)
1498 * RETURNS
1499 * None
1501 VOID WINAPI GlobalMemoryStatus(
1502 LPMEMORYSTATUS lpmem
1504 static MEMORYSTATUS cached_memstatus;
1505 static int cache_lastchecked = 0;
1506 SYSTEM_INFO si;
1507 #ifdef linux
1508 FILE *f;
1509 #endif
1511 if (time(NULL)==cache_lastchecked) {
1512 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1513 return;
1515 cache_lastchecked = time(NULL);
1517 #ifdef linux
1518 f = fopen( "/proc/meminfo", "r" );
1519 if (f)
1521 char buffer[256];
1522 int total, used, free, shared, buffers, cached;
1524 lpmem->dwLength = sizeof(MEMORYSTATUS);
1525 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1526 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1527 while (fgets( buffer, sizeof(buffer), f ))
1529 /* old style /proc/meminfo ... */
1530 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1532 lpmem->dwTotalPhys += total;
1533 lpmem->dwAvailPhys += free + buffers + cached;
1535 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1537 lpmem->dwTotalPageFile += total;
1538 lpmem->dwAvailPageFile += free;
1541 /* new style /proc/meminfo ... */
1542 if (sscanf(buffer, "MemTotal: %d", &total))
1543 lpmem->dwTotalPhys = total*1024;
1544 if (sscanf(buffer, "MemFree: %d", &free))
1545 lpmem->dwAvailPhys = free*1024;
1546 if (sscanf(buffer, "SwapTotal: %d", &total))
1547 lpmem->dwTotalPageFile = total*1024;
1548 if (sscanf(buffer, "SwapFree: %d", &free))
1549 lpmem->dwAvailPageFile = free*1024;
1550 if (sscanf(buffer, "Buffers: %d", &buffers))
1551 lpmem->dwAvailPhys += buffers*1024;
1552 if (sscanf(buffer, "Cached: %d", &cached))
1553 lpmem->dwAvailPhys += cached*1024;
1555 fclose( f );
1557 if (lpmem->dwTotalPhys)
1559 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1560 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1561 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1562 / (TotalPhysical / 100);
1564 } else
1565 #endif
1567 /* FIXME: should do something for other systems */
1568 lpmem->dwMemoryLoad = 0;
1569 lpmem->dwTotalPhys = 16*1024*1024;
1570 lpmem->dwAvailPhys = 16*1024*1024;
1571 lpmem->dwTotalPageFile = 16*1024*1024;
1572 lpmem->dwAvailPageFile = 16*1024*1024;
1574 GetSystemInfo(&si);
1575 lpmem->dwTotalVirtual = si.lpMaximumApplicationAddress-si.lpMinimumApplicationAddress;
1576 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1577 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1578 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1581 /***********************************************************************
1582 * A20Proc16 (KERNEL.165)
1584 void WINAPI A20Proc16( WORD unused )
1586 /* this is also a NOP in Windows */
1589 /***********************************************************************
1590 * LimitEMSPages16 (KERNEL.156)
1592 DWORD WINAPI LimitEMSPages16( DWORD unused )
1594 return 0;