Implement AbortDoc16, move AbortDoc32 and StartDoc32W to
[wine/multimedia.git] / memory / global.c
blobd114a02c4cde1c7a4cafb70eb7e64e9df045f403
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 <unistd.h>
11 #include <string.h>
13 #include "windows.h"
14 #include "global.h"
15 #include "heap.h"
16 #include "toolhelp.h"
17 #include "selectors.h"
18 #include "miscemu.h"
19 #include "dde_mem.h"
20 #include "stackframe.h"
21 #include "module.h"
22 #include "debug.h"
23 #include "winerror.h"
25 /* Global arena block */
26 typedef struct
28 DWORD base; /* Base address (0 if discarded) */
29 DWORD size; /* Size in bytes (0 indicates a free block) */
30 HGLOBAL16 handle; /* Handle for this block */
31 HGLOBAL16 hOwner; /* Owner of this block */
32 BYTE lockCount; /* Count of GlobalFix() calls */
33 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
34 BYTE flags; /* Allocation flags */
35 BYTE selCount; /* Number of selectors allocated for this block */
36 #ifdef CONFIG_IPC
37 int shmid;
38 #endif
39 } GLOBALARENA;
41 /* Flags definitions */
42 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
43 #define GA_DGROUP 0x04
44 #define GA_DISCARDABLE 0x08
45 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
47 /* Arena array */
48 static GLOBALARENA *pGlobalArena = NULL;
49 static int globalArenaSize = 0;
51 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
53 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
54 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
56 /***********************************************************************
57 * GLOBAL_GetArena
59 * Return the arena for a given selector, growing the arena array if needed.
61 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
63 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
65 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
66 GLOBALARENA *pNewArena = realloc( pGlobalArena,
67 newsize * sizeof(GLOBALARENA) );
68 if (!pNewArena) return 0;
69 pGlobalArena = pNewArena;
70 memset( pGlobalArena + globalArenaSize, 0,
71 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
72 globalArenaSize = newsize;
74 return pGlobalArena + (sel >> __AHSHIFT);
78 void debug_handles(void)
80 int printed=0;
81 int i;
82 for (i = globalArenaSize-1 ; i>=0 ; i--) {
83 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
84 printed=1;
85 DUMP("0x%08x, ",pGlobalArena[i].handle);
88 if (printed)
89 DUMP("\n");
93 /***********************************************************************
94 * GLOBAL_CreateBlock
96 * Create a global heap block for a fixed range of linear memory.
98 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
99 HGLOBAL16 hOwner, BOOL16 isCode,
100 BOOL16 is32Bit, BOOL16 isReadOnly,
101 SHMDATA *shmdata )
103 WORD sel, selcount;
104 GLOBALARENA *pArena;
106 /* Allocate the selector(s) */
108 sel = SELECTOR_AllocBlock( ptr, size,
109 isCode ? SEGMENT_CODE : SEGMENT_DATA,
110 is32Bit, isReadOnly );
112 if (!sel) return 0;
113 selcount = (size + 0xffff) / 0x10000;
115 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
117 SELECTOR_FreeBlock( sel, selcount );
118 return 0;
121 /* Fill the arena block */
123 pArena->base = (DWORD)ptr;
124 pArena->size = GET_SEL_LIMIT(sel) + 1;
126 #ifdef CONFIG_IPC
127 if (flags & GMEM_DDESHARE)
129 pArena->handle = shmdata->handle;
130 pArena->shmid = shmdata->shmid;
131 shmdata->sel = sel;
133 else
135 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
136 pArena->shmid = 0;
138 #else
139 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
140 #endif
141 pArena->hOwner = hOwner;
142 pArena->lockCount = 0;
143 pArena->pageLockCount = 0;
144 pArena->flags = flags & GA_MOVEABLE;
145 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
146 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
147 if (!isCode) pArena->flags |= GA_DGROUP;
148 pArena->selCount = selcount;
149 if (selcount > 1) /* clear the next arena blocks */
150 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
152 return pArena->handle;
156 /***********************************************************************
157 * GLOBAL_FreeBlock
159 * Free a block allocated by GLOBAL_CreateBlock, without touching
160 * the associated linear memory range.
162 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
164 WORD sel;
165 GLOBALARENA *pArena;
167 if (!handle) return TRUE;
168 sel = GlobalHandleToSel( handle );
169 if (!VALID_HANDLE(sel))
170 return FALSE;
171 pArena = GET_ARENA_PTR(sel);
172 SELECTOR_FreeBlock( sel, (pArena->size + 0xffff) / 0x10000 );
173 memset( pArena, 0, sizeof(GLOBALARENA) );
174 return TRUE;
177 /***********************************************************************
178 * GLOBAL_MoveBlock
180 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
182 WORD sel;
183 GLOBALARENA *pArena;
185 if (!handle) return TRUE;
186 sel = GlobalHandleToSel( handle );
187 if (!VALID_HANDLE(sel))
188 return FALSE;
189 pArena = GET_ARENA_PTR(sel);
190 if (pArena->selCount != 1)
191 return FALSE;
193 pArena->base = (DWORD)ptr;
194 pArena->size = size;
196 SELECTOR_MoveBlock( sel, ptr );
197 SetSelectorLimit( sel, size-1 );
199 return TRUE;
202 /***********************************************************************
203 * GLOBAL_Alloc
205 * Implementation of GlobalAlloc16()
207 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner,
208 BOOL16 isCode, BOOL16 is32Bit, BOOL16 isReadOnly )
210 void *ptr;
211 HGLOBAL16 handle;
212 SHMDATA shmdata;
214 TRACE(global, "%ld flags=%04x\n", size, flags );
216 /* If size is 0, create a discarded block */
218 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, isCode,
219 is32Bit, isReadOnly, NULL );
221 /* Fixup the size */
223 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
224 size = (size + 0x1f) & ~0x1f;
226 /* Allocate the linear memory */
228 #ifdef CONFIG_IPC
229 if (flags & GMEM_DDESHARE)
230 ptr = DDE_malloc(flags, size, &shmdata);
231 else
232 #endif /* CONFIG_IPC */
234 ptr = HeapAlloc( SystemHeap, 0, size );
236 /* FIXME: free discardable blocks and try again? */
237 if (!ptr) return 0;
239 /* Allocate the selector(s) */
241 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner,
242 isCode, is32Bit, isReadOnly, &shmdata);
243 if (!handle)
245 HeapFree( SystemHeap, 0, ptr );
246 return 0;
249 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
250 return handle;
254 #ifdef CONFIG_IPC
255 /***********************************************************************
256 * GLOBAL_FindArena
258 * Find the arena for a given handle
259 * (when handle is not serial - e.g. DDE)
261 static GLOBALARENA *GLOBAL_FindArena( HGLOBAL16 handle)
263 int i;
264 for (i = globalArenaSize-1 ; i>=0 ; i--) {
265 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == handle)
266 return ( &pGlobalArena[i] );
268 return NULL;
272 /***********************************************************************
273 * DDE_GlobalHandleToSel
276 WORD DDE_GlobalHandleToSel( HGLOBAL16 handle )
278 GLOBALARENA *pArena;
279 SEGPTR segptr;
281 pArena= GLOBAL_FindArena(handle);
282 if (pArena) {
283 int ArenaIdx = pArena - pGlobalArena;
285 /* See if synchronized to the shared memory */
286 return DDE_SyncHandle(handle, ( ArenaIdx << __AHSHIFT) | 7);
289 /* attach the block */
290 DDE_AttachHandle(handle, &segptr);
292 return SELECTOROF( segptr );
294 #endif /* CONFIG_IPC */
297 /***********************************************************************
298 * GlobalAlloc16 (KERNEL.15)
299 * RETURNS
300 * Handle: Success
301 * NULL: Failure
303 HGLOBAL16 WINAPI GlobalAlloc16(
304 UINT16 flags, /* [in] Object allocation attributes */
305 DWORD size /* [in] Number of bytes to allocate */
307 HANDLE16 owner = GetCurrentPDB();
309 if (flags & GMEM_DDESHARE)
310 owner = GetExePtr(owner); /* Make it a module handle */
311 return GLOBAL_Alloc( flags, size, owner, FALSE, FALSE, FALSE );
315 /***********************************************************************
316 * GlobalReAlloc16 (KERNEL.16)
317 * RETURNS
318 * Handle: Success
319 * NULL: Failure
321 HGLOBAL16 WINAPI GlobalReAlloc16(
322 HGLOBAL16 handle, /* [in] Handle of global memory object */
323 DWORD size, /* [in] New size of block */
324 UINT16 flags /* [in] How to reallocate object */
326 WORD selcount;
327 DWORD oldsize;
328 void *ptr;
329 GLOBALARENA *pArena, *pNewArena;
330 WORD sel = GlobalHandleToSel( handle );
332 TRACE(global, "%04x %ld flags=%04x\n",
333 handle, size, flags );
334 if (!handle) return 0;
336 #ifdef CONFIG_IPC
337 if (flags & GMEM_DDESHARE || is_dde_handle(handle))
339 FIXME(global, "shared memory reallocating unimplemented\n");
340 return 0;
342 #endif /* CONFIG_IPC */
344 if (!VALID_HANDLE(handle)) {
345 WARN(global, "Invalid handle 0x%04x!\n", handle);
346 return 0;
348 pArena = GET_ARENA_PTR( handle );
350 /* Discard the block if requested */
352 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
354 if (!(pArena->flags & GA_MOVEABLE) ||
355 !(pArena->flags & GA_DISCARDABLE) ||
356 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
357 HeapFree( SystemHeap, 0, (void *)pArena->base );
358 pArena->base = 0;
360 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
361 * change the selector if we are shrinking the block.
362 * FIXME: shouldn't we keep selectors until the block is deleted?
364 SELECTOR_ReallocBlock( sel, 0, 1, SEGMENT_DATA, 0, 0 );
365 return handle;
368 /* Fixup the size */
370 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
371 if (size == 0) size = 0x20;
372 else size = (size + 0x1f) & ~0x1f;
374 /* Change the flags */
376 if (flags & GMEM_MODIFY)
378 /* Change the flags, leaving GA_DGROUP alone */
379 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
380 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
381 return handle;
384 /* Reallocate the linear memory */
386 ptr = (void *)pArena->base;
387 oldsize = pArena->size;
388 TRACE(global,"oldsize %08lx\n",oldsize);
389 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
391 ptr = HeapReAlloc( SystemHeap, 0, ptr, size );
392 if (!ptr)
394 SELECTOR_FreeBlock( sel, (oldsize + 0xffff) / 0x10000 );
395 memset( pArena, 0, sizeof(GLOBALARENA) );
396 return 0;
399 /* Reallocate the selector(s) */
401 sel = SELECTOR_ReallocBlock( sel, ptr, size, SEGMENT_DATA, 0, 0 );
402 if (!sel)
404 HeapFree( SystemHeap, 0, ptr );
405 memset( pArena, 0, sizeof(GLOBALARENA) );
406 return 0;
408 selcount = (size + 0xffff) / 0x10000;
410 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
412 HeapFree( SystemHeap, 0, ptr );
413 SELECTOR_FreeBlock( sel, selcount );
414 return 0;
417 /* Fill the new arena block */
419 if (pNewArena != pArena) memcpy( pNewArena, pArena, sizeof(GLOBALARENA) );
420 pNewArena->base = (DWORD)ptr;
421 pNewArena->size = GET_SEL_LIMIT(sel) + 1;
422 pNewArena->selCount = selcount;
423 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
425 if (selcount > 1) /* clear the next arena blocks */
426 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
428 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
429 memset( (char *)ptr + oldsize, 0, size - oldsize );
430 return pNewArena->handle;
434 /***********************************************************************
435 * GlobalFree16 (KERNEL.17)
436 * RETURNS
437 * NULL: Success
438 * Handle: Failure
440 HGLOBAL16 WINAPI GlobalFree16(
441 HGLOBAL16 handle /* [in] Handle of global memory object */
443 void *ptr;
445 if (!VALID_HANDLE(handle)) {
446 WARN(global,"Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
447 return 0;
449 ptr = (void *)GET_ARENA_PTR(handle)->base;
451 TRACE(global, "%04x\n", handle );
452 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
453 #ifdef CONFIG_IPC
454 if (is_dde_handle(handle)) return DDE_GlobalFree(handle);
455 #endif /* CONFIG_IPC */
456 if (ptr) HeapFree( SystemHeap, 0, ptr );
457 return 0;
461 /***********************************************************************
462 * WIN16_GlobalLock16 (KERNEL.18)
464 * This is the GlobalLock16() function used by 16-bit code.
466 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
468 TRACE(global, "(%04x) -> %08lx\n",
469 handle, MAKELONG( 0, GlobalHandleToSel(handle)) );
470 if (handle)
472 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
474 #ifdef CONFIG_IPC
475 if (is_dde_handle(handle))
476 return PTR_SEG_OFF_TO_SEGPTR( DDE_GlobalHandleToSel(handle), 0 );
477 #endif /* CONFIG_IPC */
479 if (!VALID_HANDLE(handle)) {
480 WARN(global,"Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
481 return (SEGPTR)0;
483 if (!GET_ARENA_PTR(handle)->base) return (SEGPTR)0;
484 GET_ARENA_PTR(handle)->lockCount++;
485 return PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel(handle), 0 );
486 /* FIXME: put segment value in CX as well */
488 return (SEGPTR)0;
492 /***********************************************************************
493 * GlobalLock16 (KERNEL.18)
495 * This is the GlobalLock16() function used by 32-bit code.
497 * RETURNS
498 * Pointer to first byte of memory block
499 * NULL: Failure
501 LPVOID WINAPI GlobalLock16(
502 HGLOBAL16 handle /* [in] Handle of global memory object */
504 if (!handle) return 0;
505 if (!VALID_HANDLE(handle))
506 return (LPVOID)0;
507 GET_ARENA_PTR(handle)->lockCount++;
508 #ifdef CONFIG_IPC
509 if (is_dde_handle(handle)) return DDE_AttachHandle(handle, NULL);
510 #endif
511 return (LPVOID)GET_ARENA_PTR(handle)->base;
515 /***********************************************************************
516 * GlobalUnlock16 (KERNEL.19)
517 * NOTES
518 * Should the return values be cast to booleans?
520 * RETURNS
521 * TRUE: Object is still locked
522 * FALSE: Object is unlocked
524 BOOL16 WINAPI GlobalUnlock16(
525 HGLOBAL16 handle /* [in] Handle of global memory object */
527 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
528 if (!VALID_HANDLE(handle)) {
529 WARN(global,"Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
530 return 0;
532 TRACE(global, "%04x\n", handle );
533 if (pArena->lockCount) pArena->lockCount--;
534 return pArena->lockCount;
537 /***********************************************************************
538 * GlobalChangeLockCount (KERNEL.365)
540 * This is declared as a register function as it has to preserve
541 * *all* registers, even AX/DX !
544 void WINAPI GlobalChangeLockCount( CONTEXT *context )
546 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), SP_reg( context ) );
547 HGLOBAL16 handle = (HGLOBAL16)args[3];
548 INT16 delta = (INT16) args[2];
550 if ( delta == 1 )
551 GlobalLock16( handle );
552 else if ( delta == -1 )
553 GlobalUnlock16( handle );
554 else
555 ERR( global, "(%04X, %d): strange delta value\n", handle, delta );
558 /***********************************************************************
559 * GlobalSize16 (KERNEL.20)
560 * RETURNS
561 * Size in bytes of object
562 * 0: Failure
564 DWORD WINAPI GlobalSize16(
565 HGLOBAL16 handle /* [in] Handle of global memory object */
567 TRACE(global, "%04x\n", handle );
568 if (!handle) return 0;
569 if (!VALID_HANDLE(handle))
570 return 0;
571 return GET_ARENA_PTR(handle)->size;
575 /***********************************************************************
576 * GlobalHandle16 (KERNEL.21)
577 * NOTES
578 * Why is GlobalHandleToSel used here with the sel as input?
580 * RETURNS
581 * Handle: Success
582 * NULL: Failure
584 DWORD WINAPI GlobalHandle16(
585 WORD sel /* [in] Address of global memory block */
587 TRACE(global, "%04x\n", sel );
588 if (!VALID_HANDLE(sel)) {
589 WARN(global,"Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
590 return 0;
592 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel(sel) );
595 /***********************************************************************
596 * GlobalHandleNoRIP (KERNEL.159)
598 DWORD WINAPI GlobalHandleNoRIP( WORD sel )
600 int i;
601 for (i = globalArenaSize-1 ; i>=0 ; i--) {
602 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
603 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel(sel) );
605 return 0;
609 /***********************************************************************
610 * GlobalFlags16 (KERNEL.22)
611 * NOTES
612 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
613 * handle?
615 * RETURNS
616 * Value specifying flags and lock count
617 * GMEM_INVALID_HANDLE: Invalid handle
619 UINT16 WINAPI GlobalFlags16(
620 HGLOBAL16 handle /* [in] Handle of global memory object */
622 GLOBALARENA *pArena;
624 TRACE(global, "%04x\n", handle );
625 if (!VALID_HANDLE(handle)) {
626 WARN(global,"Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
627 return 0;
629 pArena = GET_ARENA_PTR(handle);
630 return pArena->lockCount |
631 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
632 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
636 /***********************************************************************
637 * LockSegment16 (KERNEL.23)
639 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
641 TRACE(global, "%04x\n", handle );
642 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
643 if (!VALID_HANDLE(handle)) {
644 WARN(global,"Invalid handle 0x%04x passed to LockSegment16!\n",handle);
645 return 0;
647 GET_ARENA_PTR(handle)->lockCount++;
648 return handle;
652 /***********************************************************************
653 * UnlockSegment16 (KERNEL.24)
655 void WINAPI UnlockSegment16( HGLOBAL16 handle )
657 TRACE(global, "%04x\n", handle );
658 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
659 if (!VALID_HANDLE(handle)) {
660 WARN(global,"Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
661 return;
663 GET_ARENA_PTR(handle)->lockCount--;
664 /* FIXME: this ought to return the lock count in CX (go figure...) */
668 /***********************************************************************
669 * GlobalCompact16 (KERNEL.25)
671 DWORD WINAPI GlobalCompact16( DWORD desired )
673 return GLOBAL_MAX_ALLOC_SIZE;
677 /***********************************************************************
678 * GlobalFreeAll (KERNEL.26)
680 void WINAPI GlobalFreeAll( HGLOBAL16 owner )
682 DWORD i;
683 GLOBALARENA *pArena;
685 pArena = pGlobalArena;
686 for (i = 0; i < globalArenaSize; i++, pArena++)
688 if ((pArena->size != 0) && (pArena->hOwner == owner))
689 GlobalFree16( pArena->handle );
694 /***********************************************************************
695 * GlobalWire16 (KERNEL.111)
697 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
699 return WIN16_GlobalLock16( handle );
703 /***********************************************************************
704 * GlobalUnWire16 (KERNEL.112)
706 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
708 return !GlobalUnlock16( handle );
712 /***********************************************************************
713 * SetSwapAreaSize16 (KERNEL.106)
715 LONG WINAPI SetSwapAreaSize16( WORD size )
717 FIXME(global, "(%d) - stub!\n", size );
718 return MAKELONG( size, 0xffff );
722 /***********************************************************************
723 * GlobalLRUOldest (KERNEL.163)
725 HGLOBAL16 WINAPI GlobalLRUOldest( HGLOBAL16 handle )
727 TRACE(global, "%04x\n", handle );
728 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
729 return handle;
733 /***********************************************************************
734 * GlobalLRUNewest (KERNEL.164)
736 HGLOBAL16 WINAPI GlobalLRUNewest( HGLOBAL16 handle )
738 TRACE(global, "%04x\n", handle );
739 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
740 return handle;
744 /***********************************************************************
745 * GetFreeSpace16 (KERNEL.169)
747 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
749 MEMORYSTATUS ms;
750 GlobalMemoryStatus( &ms );
751 return ms.dwAvailVirtual;
754 /***********************************************************************
755 * GlobalDOSAlloc (KERNEL.184)
756 * RETURNS
757 * Address (HW=Paragraph segment; LW=Selector)
759 DWORD WINAPI GlobalDOSAlloc(
760 DWORD size /* [in] Number of bytes to be allocated */
762 UINT16 uParagraph;
763 LPVOID lpBlock = DOSMEM_GetBlock( 0, size, &uParagraph );
765 if( lpBlock )
767 HMODULE16 hModule = GetModuleHandle16("KERNEL");
768 WORD wSelector;
770 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size,
771 hModule, 0, 0, 0, NULL );
772 return MAKELONG(wSelector,uParagraph);
774 return 0;
778 /***********************************************************************
779 * GlobalDOSFree (KERNEL.185)
780 * RETURNS
781 * NULL: Success
782 * sel: Failure
784 WORD WINAPI GlobalDOSFree(
785 WORD sel /* [in] Selector */
787 DWORD block = GetSelectorBase(sel);
789 if( block && block < 0x100000 )
791 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
792 if( DOSMEM_FreeBlock( 0, lpBlock ) )
793 GLOBAL_FreeBlock( sel );
794 sel = 0;
796 return sel;
800 /***********************************************************************
801 * GlobalPageLock (KERNEL.191)
803 WORD WINAPI GlobalPageLock( HGLOBAL16 handle )
805 TRACE(global, "%04x\n", handle );
806 if (!VALID_HANDLE(handle)) {
807 WARN(global,"Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
808 return 0;
810 return ++(GET_ARENA_PTR(handle)->pageLockCount);
814 /***********************************************************************
815 * GlobalPageUnlock (KERNEL.192)
817 WORD WINAPI GlobalPageUnlock( HGLOBAL16 handle )
819 TRACE(global, "%04x\n", handle );
820 if (!VALID_HANDLE(handle)) {
821 WARN(global,"Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
822 return 0;
824 return --(GET_ARENA_PTR(handle)->pageLockCount);
828 /***********************************************************************
829 * GlobalFix16 (KERNEL.197)
831 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
833 TRACE(global, "%04x\n", handle );
834 if (!VALID_HANDLE(handle)) {
835 WARN(global,"Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
836 return 0;
838 GET_ARENA_PTR(handle)->lockCount++;
840 return GlobalHandleToSel(handle);
844 /***********************************************************************
845 * GlobalUnfix16 (KERNEL.198)
847 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
849 TRACE(global, "%04x\n", handle );
850 if (!VALID_HANDLE(handle)) {
851 WARN(global,"Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
852 return;
854 GET_ARENA_PTR(handle)->lockCount--;
858 /***********************************************************************
859 * FarSetOwner (KERNEL.403)
861 void WINAPI FarSetOwner( HGLOBAL16 handle, HANDLE16 hOwner )
863 if (!VALID_HANDLE(handle)) {
864 WARN(global,"Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
865 return;
867 GET_ARENA_PTR(handle)->hOwner = hOwner;
871 /***********************************************************************
872 * FarGetOwner (KERNEL.404)
874 HANDLE16 WINAPI FarGetOwner( HGLOBAL16 handle )
876 if (!VALID_HANDLE(handle)) {
877 WARN(global,"Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
878 return 0;
880 return GET_ARENA_PTR(handle)->hOwner;
884 /***********************************************************************
885 * GlobalHandleToSel (TOOLHELP.50)
887 WORD WINAPI GlobalHandleToSel( HGLOBAL16 handle )
889 TRACE(toolhelp, "%04x\n", handle );
890 if (!handle) return 0;
891 #ifdef CONFIG_IPC
892 if (is_dde_handle(handle)) return DDE_GlobalHandleToSel(handle);
893 #endif
894 if (!VALID_HANDLE(handle)) {
895 WARN(global,"Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
896 return 0;
898 if (!(handle & 7))
900 WARN(global, "Program attempted invalid selector conversion\n" );
901 return handle - 1;
903 return handle | 7;
907 /***********************************************************************
908 * GlobalFirst (TOOLHELP.51)
910 BOOL16 WINAPI GlobalFirst( GLOBALENTRY *pGlobal, WORD wFlags )
912 if (wFlags == GLOBAL_LRU) return FALSE;
913 pGlobal->dwNext = 0;
914 return GlobalNext( pGlobal, wFlags );
918 /***********************************************************************
919 * GlobalNext (TOOLHELP.52)
921 BOOL16 WINAPI GlobalNext( GLOBALENTRY *pGlobal, WORD wFlags)
923 GLOBALARENA *pArena;
925 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
926 pArena = pGlobalArena + pGlobal->dwNext;
927 if (wFlags == GLOBAL_FREE) /* only free blocks */
929 int i;
930 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
931 if (pArena->size == 0) break; /* block is free */
932 if (i >= globalArenaSize) return FALSE;
933 pGlobal->dwNext = i;
936 pGlobal->dwAddress = pArena->base;
937 pGlobal->dwBlockSize = pArena->size;
938 pGlobal->hBlock = pArena->handle;
939 pGlobal->wcLock = pArena->lockCount;
940 pGlobal->wcPageLock = pArena->pageLockCount;
941 pGlobal->wFlags = (GetCurrentPDB() == pArena->hOwner);
942 pGlobal->wHeapPresent = FALSE;
943 pGlobal->hOwner = pArena->hOwner;
944 pGlobal->wType = GT_UNKNOWN;
945 pGlobal->wData = 0;
946 pGlobal->dwNext++;
947 return TRUE;
951 /***********************************************************************
952 * GlobalInfo (TOOLHELP.53)
954 BOOL16 WINAPI GlobalInfo( GLOBALINFO *pInfo )
956 int i;
957 GLOBALARENA *pArena;
959 pInfo->wcItems = globalArenaSize;
960 pInfo->wcItemsFree = 0;
961 pInfo->wcItemsLRU = 0;
962 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
963 if (pArena->size == 0) pInfo->wcItemsFree++;
964 return TRUE;
968 /***********************************************************************
969 * GlobalEntryHandle (TOOLHELP.54)
971 BOOL16 WINAPI GlobalEntryHandle( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
973 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
975 pGlobal->dwAddress = pArena->base;
976 pGlobal->dwBlockSize = pArena->size;
977 pGlobal->hBlock = pArena->handle;
978 pGlobal->wcLock = pArena->lockCount;
979 pGlobal->wcPageLock = pArena->pageLockCount;
980 pGlobal->wFlags = (GetCurrentPDB() == pArena->hOwner);
981 pGlobal->wHeapPresent = FALSE;
982 pGlobal->hOwner = pArena->hOwner;
983 pGlobal->wType = GT_UNKNOWN;
984 pGlobal->wData = 0;
985 pGlobal->dwNext++;
986 return TRUE;
990 /***********************************************************************
991 * GlobalEntryModule (TOOLHELP.55)
993 BOOL16 WINAPI GlobalEntryModule( GLOBALENTRY *pGlobal, HMODULE16 hModule,
994 WORD wSeg )
996 return FALSE;
1000 /***********************************************************************
1001 * MemManInfo (TOOLHELP.72)
1003 BOOL16 WINAPI MemManInfo( MEMMANINFO *info )
1005 MEMORYSTATUS status;
1008 * Not unsurprisingly although the documention says you
1009 * _must_ provide the size in the dwSize field, this function
1010 * (under Windows) always fills the structure and returns true.
1012 GlobalMemoryStatus( &status );
1013 info->wPageSize = VIRTUAL_GetPageSize();
1014 info->dwLargestFreeBlock = status.dwAvailVirtual;
1015 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1016 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1017 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1018 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1019 info->dwFreePages = info->dwMaxPagesAvailable;
1020 info->dwTotalPages = info->dwTotalLinearSpace;
1021 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1022 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1023 return TRUE;
1026 /***********************************************************************
1027 * GetFreeMemInfo (KERNEL.316)
1029 DWORD WINAPI GetFreeMemInfo(void)
1031 MEMMANINFO info;
1032 MemManInfo( &info );
1033 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1037 * Win32 Global heap functions (GlobalXXX).
1038 * These functions included in Win32 for compatibility with 16 bit Windows
1039 * Especially the moveable blocks and handles are oldish.
1040 * But the ability to directly allocate memory with GPTR and LPTR is widely
1041 * used.
1043 * The handle stuff looks horrible, but it's implemented almost like Win95
1044 * does it.
1048 #define MAGIC_GLOBAL_USED 0x5342
1049 #define GLOBAL_LOCK_MAX 0xFF
1050 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1051 #define INTERN_TO_HANDLE(i) ((HGLOBAL32) &((i)->Pointer))
1052 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL32 *)(p))-1))
1053 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1054 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1056 typedef struct __GLOBAL32_INTERN
1058 WORD Magic;
1059 LPVOID Pointer WINE_PACKED;
1060 BYTE Flags;
1061 BYTE LockCount;
1062 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1065 /***********************************************************************
1066 * GlobalAlloc32 (KERNEL32.315)
1067 * RETURNS
1068 * Handle: Success
1069 * NULL: Failure
1071 HGLOBAL32 WINAPI GlobalAlloc32(
1072 UINT32 flags, /* [in] Object allocation attributes */
1073 DWORD size /* [in] Number of bytes to allocate */
1075 PGLOBAL32_INTERN pintern;
1076 DWORD hpflags;
1077 LPVOID palloc;
1079 if(flags&GMEM_ZEROINIT)
1080 hpflags=HEAP_ZERO_MEMORY;
1081 else
1082 hpflags=0;
1084 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1086 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1087 return (HGLOBAL32) palloc;
1089 else /* HANDLE */
1091 /* HeapLock(GetProcessHeap()); */
1093 pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1094 if(size)
1096 palloc=HeapAlloc(GetProcessHeap(), hpflags, size+sizeof(HGLOBAL32));
1097 *(HGLOBAL32 *)palloc=INTERN_TO_HANDLE(pintern);
1098 pintern->Pointer=palloc+sizeof(HGLOBAL32);
1100 else
1101 pintern->Pointer=NULL;
1102 pintern->Magic=MAGIC_GLOBAL_USED;
1103 pintern->Flags=flags>>8;
1104 pintern->LockCount=0;
1106 /* HeapUnlock(GetProcessHeap()); */
1108 return INTERN_TO_HANDLE(pintern);
1113 /***********************************************************************
1114 * GlobalLock32 (KERNEL32.326)
1115 * RETURNS
1116 * Pointer to first byte of block
1117 * NULL: Failure
1119 LPVOID WINAPI GlobalLock32(
1120 HGLOBAL32 hmem /* [in] Handle of global memory object */
1122 PGLOBAL32_INTERN pintern;
1123 LPVOID palloc;
1125 if(ISPOINTER(hmem))
1126 return (LPVOID) hmem;
1128 /* HeapLock(GetProcessHeap()); */
1130 pintern=HANDLE_TO_INTERN(hmem);
1131 if(pintern->Magic==MAGIC_GLOBAL_USED)
1133 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1134 pintern->LockCount++;
1135 palloc=pintern->Pointer;
1137 else
1139 WARN(global, "invalid handle\n");
1140 palloc=(LPVOID) NULL;
1142 /* HeapUnlock(GetProcessHeap()); */;
1143 return palloc;
1147 /***********************************************************************
1148 * GlobalUnlock32 (KERNEL32.332)
1149 * RETURNS
1150 * TRUE: Object is still locked
1151 * FALSE: Object is unlocked
1153 BOOL32 WINAPI GlobalUnlock32(
1154 HGLOBAL32 hmem /* [in] Handle of global memory object */
1156 PGLOBAL32_INTERN pintern;
1157 BOOL32 locked;
1159 if(ISPOINTER(hmem))
1160 return FALSE;
1162 /* HeapLock(GetProcessHeap()); */
1163 pintern=HANDLE_TO_INTERN(hmem);
1165 if(pintern->Magic==MAGIC_GLOBAL_USED)
1167 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1168 pintern->LockCount--;
1170 locked=(pintern->LockCount==0) ? FALSE : TRUE;
1172 else
1174 WARN(global, "invalid handle\n");
1175 locked=FALSE;
1177 /* HeapUnlock(GetProcessHeap()); */
1178 return locked;
1182 /***********************************************************************
1183 * GlobalHandle32 (KERNEL32.325)
1184 * Returns the handle associated with the specified pointer.
1186 * NOTES
1187 * Since there in only one goto, can it be removed and the return
1188 * be put 'inline'?
1190 * RETURNS
1191 * Handle: Success
1192 * NULL: Failure
1194 HGLOBAL32 WINAPI GlobalHandle32(
1195 LPCVOID pmem /* [in] Pointer to global memory block */
1197 HGLOBAL32 handle;
1199 if (!HEAP_IsInsideHeap( GetProcessHeap(), 0, pmem )) goto error;
1200 handle = POINTER_TO_HANDLE(pmem);
1201 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, (LPCVOID)handle ))
1203 if (HANDLE_TO_INTERN(handle)->Magic == MAGIC_GLOBAL_USED)
1204 return handle; /* valid moveable block */
1206 /* maybe FIXED block */
1207 if (HeapValidate( GetProcessHeap(), 0, pmem ))
1208 return (HGLOBAL32)pmem; /* valid fixed block */
1210 error:
1211 SetLastError( ERROR_INVALID_HANDLE );
1212 return 0;
1216 /***********************************************************************
1217 * GlobalReAlloc32 (KERNEL32.328)
1218 * RETURNS
1219 * Handle: Success
1220 * NULL: Failure
1222 HGLOBAL32 WINAPI GlobalReAlloc32(
1223 HGLOBAL32 hmem, /* [in] Handle of global memory object */
1224 DWORD size, /* [in] New size of block */
1225 UINT32 flags /* [in] How to reallocate object */
1227 LPVOID palloc;
1228 HGLOBAL32 hnew;
1229 PGLOBAL32_INTERN pintern;
1231 hnew = 0;
1232 /* HeapLock(GetProcessHeap()); */
1233 if(flags & GMEM_MODIFY) /* modify flags */
1235 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1237 /* make a fixed block moveable
1238 * actually only NT is able to do this. But it's soo simple
1240 if (hmem == 0)
1242 ERR(global, "GlobalReAlloc32 with null handle!\n");
1243 SetLastError( ERROR_INVALID_ACCESS_TO_MEM );
1244 return 0;
1246 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1247 hnew=GlobalAlloc32( flags, size);
1248 palloc=GlobalLock32(hnew);
1249 memcpy(palloc, (LPVOID) hmem, size);
1250 GlobalUnlock32(hnew);
1251 GlobalFree32(hmem);
1253 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1255 /* change the flags to make our block "discardable" */
1256 pintern=HANDLE_TO_INTERN(hmem);
1257 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1258 hnew=hmem;
1260 else
1262 SetLastError(ERROR_INVALID_PARAMETER);
1263 hnew = 0;
1266 else
1268 if(ISPOINTER(hmem))
1270 /* reallocate fixed memory */
1271 hnew=(HGLOBAL32)HeapReAlloc(GetProcessHeap(), 0, (LPVOID) hmem, size);
1273 else
1275 /* reallocate a moveable block */
1276 pintern=HANDLE_TO_INTERN(hmem);
1277 if(pintern->LockCount>1) {
1278 ERR(global,"handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1279 SetLastError(ERROR_INVALID_HANDLE);
1280 } else if(size!=0)
1282 hnew=hmem;
1283 if(pintern->Pointer)
1285 palloc=HeapReAlloc(GetProcessHeap(), 0,
1286 pintern->Pointer-sizeof(HGLOBAL32),
1287 size+sizeof(HGLOBAL32) );
1288 pintern->Pointer=palloc+sizeof(HGLOBAL32);
1290 else
1292 palloc=HeapAlloc(GetProcessHeap(), 0, size+sizeof(HGLOBAL32));
1293 *(HGLOBAL32 *)palloc=hmem;
1294 pintern->Pointer=palloc+sizeof(HGLOBAL32);
1297 else
1299 if(pintern->Pointer)
1301 HeapFree(GetProcessHeap(), 0, pintern->Pointer-sizeof(HGLOBAL32));
1302 pintern->Pointer=NULL;
1307 /* HeapUnlock(GetProcessHeap()); */
1308 return hnew;
1312 /***********************************************************************
1313 * GlobalFree32 (KERNEL32.322)
1314 * RETURNS
1315 * NULL: Success
1316 * Handle: Failure
1318 HGLOBAL32 WINAPI GlobalFree32(
1319 HGLOBAL32 hmem /* [in] Handle of global memory object */
1321 PGLOBAL32_INTERN pintern;
1322 HGLOBAL32 hreturned = 0;
1324 if(ISPOINTER(hmem)) /* POINTER */
1326 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1328 else /* HANDLE */
1330 /* HeapLock(GetProcessHeap()); */
1331 pintern=HANDLE_TO_INTERN(hmem);
1333 if(pintern->Magic==MAGIC_GLOBAL_USED)
1335 if(pintern->LockCount!=0)
1336 SetLastError(ERROR_INVALID_HANDLE);
1337 if(pintern->Pointer)
1338 if(!HeapFree(GetProcessHeap(), 0,
1339 (char *)(pintern->Pointer)-sizeof(HGLOBAL32)))
1340 hreturned=hmem;
1341 if(!HeapFree(GetProcessHeap(), 0, pintern))
1342 hreturned=hmem;
1344 /* HeapUnlock(GetProcessHeap()); */
1346 return hreturned;
1350 /***********************************************************************
1351 * GlobalSize32 (KERNEL32.329)
1352 * RETURNS
1353 * Size in bytes of the global memory object
1354 * 0: Failure
1356 DWORD WINAPI GlobalSize32(
1357 HGLOBAL32 hmem /* [in] Handle of global memory object */
1359 DWORD retval;
1360 PGLOBAL32_INTERN pintern;
1362 if(ISPOINTER(hmem))
1364 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1366 else
1368 /* HeapLock(GetProcessHeap()); */
1369 pintern=HANDLE_TO_INTERN(hmem);
1371 if(pintern->Magic==MAGIC_GLOBAL_USED)
1373 retval=HeapSize(GetProcessHeap(), 0,
1374 (char *)(pintern->Pointer)-sizeof(HGLOBAL32))-4;
1376 else
1378 WARN(global, "invalid handle\n");
1379 retval=0;
1381 /* HeapUnlock(GetProcessHeap()); */
1383 return retval;
1387 /***********************************************************************
1388 * GlobalWire32 (KERNEL32.333)
1390 LPVOID WINAPI GlobalWire32(HGLOBAL32 hmem)
1392 return GlobalLock32( hmem );
1396 /***********************************************************************
1397 * GlobalUnWire32 (KERNEL32.330)
1399 BOOL32 WINAPI GlobalUnWire32(HGLOBAL32 hmem)
1401 return GlobalUnlock32( hmem);
1405 /***********************************************************************
1406 * GlobalFix32 (KERNEL32.320)
1408 VOID WINAPI GlobalFix32(HGLOBAL32 hmem)
1410 GlobalLock32( hmem );
1414 /***********************************************************************
1415 * GlobalUnfix32 (KERNEL32.331)
1417 VOID WINAPI GlobalUnfix32(HGLOBAL32 hmem)
1419 GlobalUnlock32( hmem);
1423 /***********************************************************************
1424 * GlobalFlags32 (KERNEL32.321)
1425 * Returns information about the specified global memory object
1427 * NOTES
1428 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1430 * RETURNS
1431 * Value specifying allocation flags and lock count
1432 * GMEM_INVALID_HANDLE: Failure
1434 UINT32 WINAPI GlobalFlags32(
1435 HGLOBAL32 hmem /* [in] Handle to global memory object */
1437 DWORD retval;
1438 PGLOBAL32_INTERN pintern;
1440 if(ISPOINTER(hmem))
1442 retval=0;
1444 else
1446 /* HeapLock(GetProcessHeap()); */
1447 pintern=HANDLE_TO_INTERN(hmem);
1448 if(pintern->Magic==MAGIC_GLOBAL_USED)
1450 retval=pintern->LockCount + (pintern->Flags<<8);
1451 if(pintern->Pointer==0)
1452 retval|= GMEM_DISCARDED;
1454 else
1456 WARN(global,"Invalid handle: %04x", hmem);
1457 retval=0;
1459 /* HeapUnlock(GetProcessHeap()); */
1461 return retval;
1465 /***********************************************************************
1466 * GlobalCompact32 (KERNEL32.316)
1468 DWORD WINAPI GlobalCompact32( DWORD minfree )
1470 return 0; /* GlobalCompact does nothing in Win32 */
1474 /***********************************************************************
1475 * GlobalMemoryStatus (KERNEL32.327)
1476 * RETURNS
1477 * None
1479 VOID WINAPI GlobalMemoryStatus(
1480 LPMEMORYSTATUS lpmem
1482 #ifdef linux
1483 FILE *f = fopen( "/proc/meminfo", "r" );
1484 if (f)
1486 char buffer[256];
1487 int total, used, free, shared, buffers, cached;
1489 lpmem->dwLength = sizeof(MEMORYSTATUS);
1490 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1491 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1492 while (fgets( buffer, sizeof(buffer), f ))
1494 /* old style /proc/meminfo ... */
1495 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1497 lpmem->dwTotalPhys += total;
1498 lpmem->dwAvailPhys += free + buffers + cached;
1500 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1502 lpmem->dwTotalPageFile += total;
1503 lpmem->dwAvailPageFile += free;
1506 /* new style /proc/meminfo ... */
1507 if (sscanf(buffer, "MemTotal: %d", &total))
1508 lpmem->dwTotalPhys = total*1024;
1509 if (sscanf(buffer, "MemFree: %d", &free))
1510 lpmem->dwAvailPhys = free*1024;
1511 if (sscanf(buffer, "SwapTotal: %d", &total))
1512 lpmem->dwTotalPageFile = total*1024;
1513 if (sscanf(buffer, "SwapFree: %d", &free))
1514 lpmem->dwAvailPageFile = free*1024;
1515 if (sscanf(buffer, "Buffers: %d", &buffers))
1516 lpmem->dwAvailPhys += buffers*1024;
1517 if (sscanf(buffer, "Cached: %d", &cached))
1518 lpmem->dwAvailPhys += cached*1024;
1520 fclose( f );
1522 if (lpmem->dwTotalPhys)
1524 lpmem->dwTotalVirtual = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1525 lpmem->dwAvailVirtual = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1526 lpmem->dwMemoryLoad = (lpmem->dwTotalVirtual-lpmem->dwAvailVirtual)
1527 / (lpmem->dwTotalVirtual / 100);
1528 return;
1531 #endif
1532 /* FIXME: should do something for other systems */
1533 lpmem->dwMemoryLoad = 0;
1534 lpmem->dwTotalPhys = 16*1024*1024;
1535 lpmem->dwAvailPhys = 16*1024*1024;
1536 lpmem->dwTotalPageFile = 16*1024*1024;
1537 lpmem->dwAvailPageFile = 16*1024*1024;
1538 lpmem->dwTotalVirtual = 32*1024*1024;
1539 lpmem->dwAvailVirtual = 32*1024*1024;
1543 /**********************************************************************
1544 * WOWGlobalAllocLock (KERNEL32.62)
1546 * Combined GlobalAlloc and GlobalLock.
1548 SEGPTR WINAPI WOWGlobalAllocLock16(DWORD flags,DWORD cb,HGLOBAL16 *hmem)
1550 HGLOBAL16 xhmem;
1551 xhmem = GlobalAlloc16(flags,cb);
1552 if (hmem) *hmem = xhmem;
1553 return WIN16_GlobalLock16(xhmem);
1557 /**********************************************************************
1558 * WOWGlobalUnlockFree (KERNEL32.64)
1560 * Combined GlobalUnlock and GlobalFree.
1562 WORD WINAPI WOWGlobalUnlockFree16(DWORD vpmem) {
1563 if (!GlobalUnlock16(HIWORD(vpmem)))
1564 return 0;
1565 return GlobalFree16(HIWORD(vpmem));