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"
44 #include "selectors.h"
46 #include "stackframe.h"
47 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(global);
52 /* Global arena block */
55 DWORD base; /* Base address (0 if discarded) */
56 DWORD size; /* Size in bytes (0 indicates a free block) */
57 HGLOBAL16 handle; /* Handle for this block */
58 HGLOBAL16 hOwner; /* Owner of this block */
59 BYTE lockCount; /* Count of GlobalFix() calls */
60 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
61 BYTE flags; /* Allocation flags */
62 BYTE selCount; /* Number of selectors allocated for this block */
65 /* Flags definitions */
66 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
67 #define GA_DGROUP 0x04
68 #define GA_DISCARDABLE 0x08
69 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
70 #define GA_DOSMEM 0x20
72 /* Arena array (FIXME) */
73 static GLOBALARENA *pGlobalArena;
74 static int globalArenaSize;
76 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
78 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
79 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
82 /***********************************************************************
85 * Return the arena for a given selector, growing the arena array if needed.
87 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
89 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
91 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
92 GLOBALARENA *pNewArena = realloc( pGlobalArena,
93 newsize * sizeof(GLOBALARENA) );
94 if (!pNewArena) return 0;
95 pGlobalArena = pNewArena;
96 memset( pGlobalArena + globalArenaSize, 0,
97 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
98 globalArenaSize = newsize;
100 return pGlobalArena + (sel >> __AHSHIFT);
103 void debug_handles(void)
107 for (i = globalArenaSize-1 ; i>=0 ; i--) {
108 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
110 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
118 /***********************************************************************
121 * Create a global heap block for a fixed range of linear memory.
123 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
124 HGLOBAL16 hOwner, unsigned char selflags )
129 /* Allocate the selector(s) */
131 sel = SELECTOR_AllocBlock( ptr, size, selflags );
133 selcount = (size + 0xffff) / 0x10000;
135 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
137 SELECTOR_FreeBlock( sel );
141 /* Fill the arena block */
143 pArena->base = (DWORD)ptr;
144 pArena->size = GetSelectorLimit16(sel) + 1;
145 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
146 pArena->hOwner = hOwner;
147 pArena->lockCount = 0;
148 pArena->pageLockCount = 0;
149 pArena->flags = flags & GA_MOVEABLE;
150 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
151 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
152 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
153 pArena->selCount = selcount;
154 if (selcount > 1) /* clear the next arena blocks */
155 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
157 return pArena->handle;
161 /***********************************************************************
164 * Free a block allocated by GLOBAL_CreateBlock, without touching
165 * the associated linear memory range.
167 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
172 if (!handle) return TRUE;
173 sel = GlobalHandleToSel16( handle );
174 if (!VALID_HANDLE(sel)) return FALSE;
175 pArena = GET_ARENA_PTR(sel);
176 SELECTOR_FreeBlock( sel );
177 memset( pArena, 0, sizeof(GLOBALARENA) );
181 /***********************************************************************
184 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
189 if (!handle) return TRUE;
190 sel = GlobalHandleToSel16( handle );
191 if (!VALID_HANDLE(sel)) return FALSE;
192 pArena = GET_ARENA_PTR(sel);
193 if (pArena->selCount != 1)
196 pArena->base = (DWORD)ptr;
198 SELECTOR_ReallocBlock( sel, ptr, size );
202 /***********************************************************************
205 * Implementation of GlobalAlloc16()
207 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
212 TRACE("%ld flags=%04x\n", size, flags );
214 /* If size is 0, create a discarded block */
216 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
220 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
221 size = (size + 0x1f) & ~0x1f;
223 /* Allocate the linear memory */
224 ptr = HeapAlloc( GetProcessHeap(), 0, size );
225 /* FIXME: free discardable blocks and try again? */
228 /* Allocate the selector(s) */
230 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
233 HeapFree( GetProcessHeap(), 0, ptr );
237 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
241 /***********************************************************************
242 * GlobalAlloc (KERNEL.15)
243 * GlobalAlloc16 (KERNEL32.24)
248 HGLOBAL16 WINAPI GlobalAlloc16(
249 UINT16 flags, /* [in] Object allocation attributes */
250 DWORD size /* [in] Number of bytes to allocate */
252 HANDLE16 owner = GetCurrentPDB16();
254 if (flags & GMEM_DDESHARE)
255 owner = GetExePtr(owner); /* Make it a module handle */
256 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
260 /***********************************************************************
261 * GlobalReAlloc (KERNEL.16)
266 HGLOBAL16 WINAPI GlobalReAlloc16(
267 HGLOBAL16 handle, /* [in] Handle of global memory object */
268 DWORD size, /* [in] New size of block */
269 UINT16 flags /* [in] How to reallocate object */
274 GLOBALARENA *pArena, *pNewArena;
275 WORD sel = GlobalHandleToSel16( handle );
277 TRACE("%04x %ld flags=%04x\n",
278 handle, size, flags );
279 if (!handle) return 0;
281 if (!VALID_HANDLE(handle))
283 WARN("Invalid handle 0x%04x!\n", handle);
286 pArena = GET_ARENA_PTR( handle );
288 /* Discard the block if requested */
290 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
292 if (!(pArena->flags & GA_MOVEABLE) ||
293 !(pArena->flags & GA_DISCARDABLE) ||
294 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
295 if (pArena->flags & GA_DOSMEM)
296 DOSMEM_FreeBlock( (void *)pArena->base );
298 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
301 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
302 * change the selector if we are shrinking the block.
303 * FIXME: shouldn't we keep selectors until the block is deleted?
305 SELECTOR_ReallocBlock( sel, 0, 1 );
311 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
312 if (size == 0) size = 0x20;
313 else size = (size + 0x1f) & ~0x1f;
315 /* Change the flags */
317 if (flags & GMEM_MODIFY)
319 /* Change the flags, leaving GA_DGROUP alone */
320 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
321 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
325 /* Reallocate the linear memory */
327 ptr = (void *)pArena->base;
328 oldsize = pArena->size;
329 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
330 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
332 if (pArena->flags & GA_DOSMEM)
334 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
336 else if(pArena->pageLockCount > 0)
340 newptr = DOSMEM_GetBlock( size, 0 );
343 memcpy( newptr, ptr, oldsize );
344 DOSMEM_FreeBlock( ptr );
351 * if more than one reader (e.g. some pointer has been
352 * given out by GetVDMPointer32W16),
353 * only try to realloc in place
355 newptr = HeapReAlloc( GetProcessHeap(),
356 (pArena->pageLockCount > 0) ?
357 HEAP_REALLOC_IN_PLACE_ONLY : 0,
363 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
364 if (pArena->pageLockCount <1)
366 if (pArena->flags & GA_DOSMEM)
367 DOSMEM_FreeBlock( (void *)pArena->base );
369 HeapFree( GetProcessHeap(), 0, ptr );
370 SELECTOR_FreeBlock( sel );
371 memset( pArena, 0, sizeof(GLOBALARENA) );
377 /* Reallocate the selector(s) */
379 sel = SELECTOR_ReallocBlock( sel, ptr, size );
382 if (pArena->flags & GA_DOSMEM)
383 DOSMEM_FreeBlock( (void *)pArena->base );
385 HeapFree( GetProcessHeap(), 0, ptr );
386 memset( pArena, 0, sizeof(GLOBALARENA) );
389 selcount = (size + 0xffff) / 0x10000;
391 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
393 if (pArena->flags & GA_DOSMEM)
394 DOSMEM_FreeBlock( (void *)pArena->base );
396 HeapFree( GetProcessHeap(), 0, ptr );
397 SELECTOR_FreeBlock( sel );
401 /* Fill the new arena block
402 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
404 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
405 pNewArena->base = (DWORD)ptr;
406 pNewArena->size = GetSelectorLimit16(sel) + 1;
407 pNewArena->selCount = selcount;
408 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
410 if (selcount > 1) /* clear the next arena blocks */
411 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
413 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
414 memset( (char *)ptr + oldsize, 0, size - oldsize );
415 return pNewArena->handle;
419 /***********************************************************************
420 * GlobalFree (KERNEL.17)
421 * GlobalFree16 (KERNEL32.31)
426 HGLOBAL16 WINAPI GlobalFree16(
427 HGLOBAL16 handle /* [in] Handle of global memory object */
431 if (!VALID_HANDLE(handle))
433 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
436 ptr = (void *)GET_ARENA_PTR(handle)->base;
438 TRACE("%04x\n", handle );
439 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
440 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
445 /**********************************************************************
446 * K32WOWGlobalLock16 (KERNEL32.60)
448 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
450 WORD sel = GlobalHandleToSel16( handle );
451 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
455 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
457 if (!VALID_HANDLE(handle)) {
458 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
461 else if (!GET_ARENA_PTR(handle)->base)
464 GET_ARENA_PTR(handle)->lockCount++;
467 return MAKESEGPTR( sel, 0 );
472 /***********************************************************************
473 * GlobalLock (KERNEL.18)
475 * This is the GlobalLock16() function used by 16-bit code.
477 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
479 SEGPTR ret = K32WOWGlobalLock16( handle );
480 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
485 /***********************************************************************
486 * GlobalLock16 (KERNEL32.25)
488 * This is the GlobalLock16() function used by 32-bit code.
491 * Pointer to first byte of memory block
494 LPVOID WINAPI GlobalLock16(
495 HGLOBAL16 handle /* [in] Handle of global memory object */
497 if (!handle) return 0;
498 if (!VALID_HANDLE(handle))
500 GET_ARENA_PTR(handle)->lockCount++;
501 return (LPVOID)GET_ARENA_PTR(handle)->base;
505 /***********************************************************************
506 * GlobalUnlock (KERNEL.19)
507 * GlobalUnlock16 (KERNEL32.26)
509 * Should the return values be cast to booleans?
512 * TRUE: Object is still locked
513 * FALSE: Object is unlocked
515 BOOL16 WINAPI GlobalUnlock16(
516 HGLOBAL16 handle /* [in] Handle of global memory object */
518 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
519 if (!VALID_HANDLE(handle)) {
520 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
523 TRACE("%04x\n", handle );
524 if (pArena->lockCount) pArena->lockCount--;
525 return pArena->lockCount;
528 /***********************************************************************
529 * GlobalChangeLockCount (KERNEL.365)
531 * This is declared as a register function as it has to preserve
532 * *all* registers, even AX/DX !
535 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
539 GlobalLock16( handle );
540 else if ( delta == -1 )
541 GlobalUnlock16( handle );
543 ERR("(%04X, %d): strange delta value\n", handle, delta );
546 /***********************************************************************
547 * GlobalSize (KERNEL.20)
548 * GlobalSize16 (KERNEL32.32)
550 * Size in bytes of object
553 DWORD WINAPI GlobalSize16(
554 HGLOBAL16 handle /* [in] Handle of global memory object */
556 TRACE("%04x\n", handle );
557 if (!handle) return 0;
558 if (!VALID_HANDLE(handle))
560 return GET_ARENA_PTR(handle)->size;
564 /***********************************************************************
565 * GlobalHandle (KERNEL.21)
567 * Why is GlobalHandleToSel used here with the sel as input?
573 DWORD WINAPI GlobalHandle16(
574 WORD sel /* [in] Address of global memory block */
576 TRACE("%04x\n", sel );
577 if (!VALID_HANDLE(sel)) {
578 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
581 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
584 /***********************************************************************
585 * GlobalHandleNoRIP (KERNEL.159)
587 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
590 for (i = globalArenaSize-1 ; i>=0 ; i--) {
591 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
592 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
598 /***********************************************************************
599 * GlobalFlags (KERNEL.22)
602 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
606 * Value specifying flags and lock count
607 * GMEM_INVALID_HANDLE: Invalid handle
609 UINT16 WINAPI GlobalFlags16(
610 HGLOBAL16 handle /* [in] Handle of global memory object */
614 TRACE("%04x\n", handle );
615 if (!VALID_HANDLE(handle)) {
616 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
619 pArena = GET_ARENA_PTR(handle);
620 return pArena->lockCount |
621 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
622 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
626 /***********************************************************************
627 * LockSegment (KERNEL.23)
629 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
631 TRACE("%04x\n", handle );
632 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
633 if (!VALID_HANDLE(handle)) {
634 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
637 GET_ARENA_PTR(handle)->lockCount++;
642 /***********************************************************************
643 * UnlockSegment (KERNEL.24)
645 void WINAPI UnlockSegment16( HGLOBAL16 handle )
647 TRACE("%04x\n", handle );
648 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
649 if (!VALID_HANDLE(handle)) {
650 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
653 GET_ARENA_PTR(handle)->lockCount--;
654 /* FIXME: this ought to return the lock count in CX (go figure...) */
658 /***********************************************************************
659 * GlobalCompact (KERNEL.25)
661 DWORD WINAPI GlobalCompact16( DWORD desired )
663 return GLOBAL_MAX_ALLOC_SIZE;
667 /***********************************************************************
668 * GlobalFreeAll (KERNEL.26)
670 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
675 pArena = pGlobalArena;
676 for (i = 0; i < globalArenaSize; i++, pArena++)
678 if ((pArena->size != 0) && (pArena->hOwner == owner))
679 GlobalFree16( pArena->handle );
684 /***********************************************************************
685 * GlobalWire (KERNEL.111)
686 * GlobalWire16 (KERNEL32.29)
688 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
690 return WIN16_GlobalLock16( handle );
694 /***********************************************************************
695 * GlobalUnWire (KERNEL.112)
696 * GlobalUnWire16 (KERNEL32.30)
698 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
700 return !GlobalUnlock16( handle );
704 /***********************************************************************
705 * SetSwapAreaSize (KERNEL.106)
707 LONG WINAPI SetSwapAreaSize16( WORD size )
709 FIXME("(%d) - stub!\n", size );
710 return MAKELONG( size, 0xffff );
714 /***********************************************************************
715 * GlobalLRUOldest (KERNEL.163)
717 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
719 TRACE("%04x\n", handle );
720 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
725 /***********************************************************************
726 * GlobalLRUNewest (KERNEL.164)
728 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
730 TRACE("%04x\n", handle );
731 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
736 /***********************************************************************
737 * GetFreeSpace (KERNEL.169)
739 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
742 GlobalMemoryStatus( &ms );
743 return ms.dwAvailVirtual;
746 /***********************************************************************
747 * GlobalDOSAlloc (KERNEL.184)
749 * Address (HW=Paragraph segment; LW=Selector)
751 DWORD WINAPI GlobalDOSAlloc16(
752 DWORD size /* [in] Number of bytes to be allocated */
755 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
759 HMODULE16 hModule = GetModuleHandle16("KERNEL");
763 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
764 pArena = GET_ARENA_PTR(wSelector);
765 pArena->flags |= GA_DOSMEM;
766 return MAKELONG(wSelector,uParagraph);
772 /***********************************************************************
773 * GlobalDOSFree (KERNEL.185)
778 WORD WINAPI GlobalDOSFree16(
779 WORD sel /* [in] Selector */
781 DWORD block = GetSelectorBase(sel);
783 if( block && block < 0x100000 )
785 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
786 if( DOSMEM_FreeBlock( lpBlock ) )
787 GLOBAL_FreeBlock( sel );
794 /***********************************************************************
795 * GlobalPageLock (KERNEL.191)
796 * GlobalSmartPageLock(KERNEL.230)
798 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
800 TRACE("%04x\n", handle );
801 if (!VALID_HANDLE(handle)) {
802 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
805 return ++(GET_ARENA_PTR(handle)->pageLockCount);
809 /***********************************************************************
810 * GlobalPageUnlock (KERNEL.192)
811 * GlobalSmartPageUnlock(KERNEL.231)
813 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
815 TRACE("%04x\n", handle );
816 if (!VALID_HANDLE(handle)) {
817 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
820 return --(GET_ARENA_PTR(handle)->pageLockCount);
824 /***********************************************************************
825 * GlobalFix (KERNEL.197)
826 * GlobalFix16 (KERNEL32.27)
828 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
830 TRACE("%04x\n", handle );
831 if (!VALID_HANDLE(handle)) {
832 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
835 GET_ARENA_PTR(handle)->lockCount++;
837 return GlobalHandleToSel16(handle);
841 /***********************************************************************
842 * GlobalUnfix (KERNEL.198)
843 * GlobalUnfix16 (KERNEL32.28)
845 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
847 TRACE("%04x\n", handle );
848 if (!VALID_HANDLE(handle)) {
849 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
852 GET_ARENA_PTR(handle)->lockCount--;
856 /***********************************************************************
857 * FarSetOwner (KERNEL.403)
859 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
861 if (!VALID_HANDLE(handle)) {
862 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
865 GET_ARENA_PTR(handle)->hOwner = hOwner;
869 /***********************************************************************
870 * FarGetOwner (KERNEL.404)
872 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
874 if (!VALID_HANDLE(handle)) {
875 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
878 return GET_ARENA_PTR(handle)->hOwner;
882 /***********************************************************************
883 * GlobalHandleToSel (TOOLHELP.50)
885 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
887 if (!handle) return 0;
888 if (!VALID_HANDLE(handle)) {
889 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
894 WARN("Program attempted invalid selector conversion\n" );
901 /***********************************************************************
902 * GlobalFirst (TOOLHELP.51)
904 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
906 if (wFlags == GLOBAL_LRU) return FALSE;
908 return GlobalNext16( pGlobal, wFlags );
912 /***********************************************************************
913 * GlobalNext (TOOLHELP.52)
915 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
919 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
920 pArena = pGlobalArena + pGlobal->dwNext;
921 if (wFlags == GLOBAL_FREE) /* only free blocks */
924 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
925 if (pArena->size == 0) break; /* block is free */
926 if (i >= globalArenaSize) return FALSE;
930 pGlobal->dwAddress = pArena->base;
931 pGlobal->dwBlockSize = pArena->size;
932 pGlobal->hBlock = pArena->handle;
933 pGlobal->wcLock = pArena->lockCount;
934 pGlobal->wcPageLock = pArena->pageLockCount;
935 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
936 pGlobal->wHeapPresent = FALSE;
937 pGlobal->hOwner = pArena->hOwner;
938 pGlobal->wType = GT_UNKNOWN;
945 /***********************************************************************
946 * GlobalInfo (TOOLHELP.53)
948 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
953 pInfo->wcItems = globalArenaSize;
954 pInfo->wcItemsFree = 0;
955 pInfo->wcItemsLRU = 0;
956 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
957 if (pArena->size == 0) pInfo->wcItemsFree++;
962 /***********************************************************************
963 * GlobalEntryHandle (TOOLHELP.54)
965 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
967 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
969 pGlobal->dwAddress = pArena->base;
970 pGlobal->dwBlockSize = pArena->size;
971 pGlobal->hBlock = pArena->handle;
972 pGlobal->wcLock = pArena->lockCount;
973 pGlobal->wcPageLock = pArena->pageLockCount;
974 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
975 pGlobal->wHeapPresent = FALSE;
976 pGlobal->hOwner = pArena->hOwner;
977 pGlobal->wType = GT_UNKNOWN;
984 /***********************************************************************
985 * GlobalEntryModule (TOOLHELP.55)
987 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
990 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
995 /***********************************************************************
996 * MemManInfo (TOOLHELP.72)
998 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1000 MEMORYSTATUS status;
1003 * Not unsurprisingly although the documention says you
1004 * _must_ provide the size in the dwSize field, this function
1005 * (under Windows) always fills the structure and returns true.
1007 GlobalMemoryStatus( &status );
1008 info->wPageSize = getpagesize();
1009 info->dwLargestFreeBlock = status.dwAvailVirtual;
1010 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1011 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1012 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1013 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1014 info->dwFreePages = info->dwMaxPagesAvailable;
1015 info->dwTotalPages = info->dwTotalLinearSpace;
1016 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1017 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1021 /***********************************************************************
1022 * GetFreeMemInfo (KERNEL.316)
1024 DWORD WINAPI GetFreeMemInfo16(void)
1027 MemManInfo16( &info );
1028 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1031 /***********************************************************************
1032 * A20Proc (KERNEL.165)
1033 * A20_Proc (SYSTEM.20)
1035 void WINAPI A20Proc16( WORD unused )
1037 /* this is also a NOP in Windows */
1040 /***********************************************************************
1041 * LimitEMSPages (KERNEL.156)
1043 DWORD WINAPI LimitEMSPages16( DWORD unused )