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
357 newptr = HeapReAlloc( GetProcessHeap(),
358 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
361 newptr = HeapAlloc( GetProcessHeap(),
362 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
369 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
370 if (pArena->pageLockCount <1)
372 if (pArena->flags & GA_DOSMEM)
373 DOSMEM_FreeBlock( (void *)pArena->base );
375 HeapFree( GetProcessHeap(), 0, ptr );
376 SELECTOR_FreeBlock( sel );
377 memset( pArena, 0, sizeof(GLOBALARENA) );
383 /* Reallocate the selector(s) */
385 sel = SELECTOR_ReallocBlock( sel, ptr, size );
388 if (pArena->flags & GA_DOSMEM)
389 DOSMEM_FreeBlock( (void *)pArena->base );
391 HeapFree( GetProcessHeap(), 0, ptr );
392 memset( pArena, 0, sizeof(GLOBALARENA) );
395 selcount = (size + 0xffff) / 0x10000;
397 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
399 if (pArena->flags & GA_DOSMEM)
400 DOSMEM_FreeBlock( (void *)pArena->base );
402 HeapFree( GetProcessHeap(), 0, ptr );
403 SELECTOR_FreeBlock( sel );
407 /* Fill the new arena block
408 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
410 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
411 pNewArena->base = (DWORD)ptr;
412 pNewArena->size = GetSelectorLimit16(sel) + 1;
413 pNewArena->selCount = selcount;
414 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
416 if (selcount > 1) /* clear the next arena blocks */
417 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
419 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
420 memset( (char *)ptr + oldsize, 0, size - oldsize );
421 return pNewArena->handle;
425 /***********************************************************************
426 * GlobalFree (KERNEL.17)
427 * GlobalFree16 (KERNEL32.31)
432 HGLOBAL16 WINAPI GlobalFree16(
433 HGLOBAL16 handle /* [in] Handle of global memory object */
437 if (!VALID_HANDLE(handle))
439 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
442 ptr = (void *)GET_ARENA_PTR(handle)->base;
444 TRACE("%04x\n", handle );
445 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
446 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
451 /**********************************************************************
452 * K32WOWGlobalLock16 (KERNEL32.60)
454 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
456 WORD sel = GlobalHandleToSel16( handle );
457 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
461 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
463 if (!VALID_HANDLE(handle)) {
464 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
467 else if (!GET_ARENA_PTR(handle)->base)
470 GET_ARENA_PTR(handle)->lockCount++;
473 return MAKESEGPTR( sel, 0 );
478 /***********************************************************************
479 * GlobalLock (KERNEL.18)
481 * This is the GlobalLock16() function used by 16-bit code.
483 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
485 SEGPTR ret = K32WOWGlobalLock16( handle );
486 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
491 /***********************************************************************
492 * GlobalLock16 (KERNEL32.25)
494 * This is the GlobalLock16() function used by 32-bit code.
497 * Pointer to first byte of memory block
500 LPVOID WINAPI GlobalLock16(
501 HGLOBAL16 handle /* [in] Handle of global memory object */
503 if (!handle) return 0;
504 if (!VALID_HANDLE(handle))
506 GET_ARENA_PTR(handle)->lockCount++;
507 return (LPVOID)GET_ARENA_PTR(handle)->base;
511 /***********************************************************************
512 * GlobalUnlock (KERNEL.19)
513 * GlobalUnlock16 (KERNEL32.26)
515 * Should the return values be cast to booleans?
518 * TRUE: Object is still locked
519 * FALSE: Object is unlocked
521 BOOL16 WINAPI GlobalUnlock16(
522 HGLOBAL16 handle /* [in] Handle of global memory object */
524 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
525 if (!VALID_HANDLE(handle)) {
526 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
529 TRACE("%04x\n", handle );
530 if (pArena->lockCount) pArena->lockCount--;
531 return pArena->lockCount;
534 /***********************************************************************
535 * GlobalChangeLockCount (KERNEL.365)
537 * This is declared as a register function as it has to preserve
538 * *all* registers, even AX/DX !
541 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
545 GlobalLock16( handle );
546 else if ( delta == -1 )
547 GlobalUnlock16( handle );
549 ERR("(%04X, %d): strange delta value\n", handle, delta );
552 /***********************************************************************
553 * GlobalSize (KERNEL.20)
554 * GlobalSize16 (KERNEL32.32)
556 * Size in bytes of object
559 DWORD WINAPI GlobalSize16(
560 HGLOBAL16 handle /* [in] Handle of global memory object */
562 TRACE("%04x\n", handle );
563 if (!handle) return 0;
564 if (!VALID_HANDLE(handle))
566 return GET_ARENA_PTR(handle)->size;
570 /***********************************************************************
571 * GlobalHandle (KERNEL.21)
573 * Why is GlobalHandleToSel used here with the sel as input?
579 DWORD WINAPI GlobalHandle16(
580 WORD sel /* [in] Address of global memory block */
582 TRACE("%04x\n", sel );
583 if (!VALID_HANDLE(sel)) {
584 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
587 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
590 /***********************************************************************
591 * GlobalHandleNoRIP (KERNEL.159)
593 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
596 for (i = globalArenaSize-1 ; i>=0 ; i--) {
597 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
598 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
604 /***********************************************************************
605 * GlobalFlags (KERNEL.22)
608 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
612 * Value specifying flags and lock count
613 * GMEM_INVALID_HANDLE: Invalid handle
615 UINT16 WINAPI GlobalFlags16(
616 HGLOBAL16 handle /* [in] Handle of global memory object */
620 TRACE("%04x\n", handle );
621 if (!VALID_HANDLE(handle)) {
622 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
625 pArena = GET_ARENA_PTR(handle);
626 return pArena->lockCount |
627 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
628 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
632 /***********************************************************************
633 * LockSegment (KERNEL.23)
635 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
637 TRACE("%04x\n", handle );
638 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
639 if (!VALID_HANDLE(handle)) {
640 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
643 GET_ARENA_PTR(handle)->lockCount++;
648 /***********************************************************************
649 * UnlockSegment (KERNEL.24)
651 void WINAPI UnlockSegment16( HGLOBAL16 handle )
653 TRACE("%04x\n", handle );
654 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
655 if (!VALID_HANDLE(handle)) {
656 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
659 GET_ARENA_PTR(handle)->lockCount--;
660 /* FIXME: this ought to return the lock count in CX (go figure...) */
664 /***********************************************************************
665 * GlobalCompact (KERNEL.25)
667 DWORD WINAPI GlobalCompact16( DWORD desired )
669 return GLOBAL_MAX_ALLOC_SIZE;
673 /***********************************************************************
674 * GlobalFreeAll (KERNEL.26)
676 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
681 pArena = pGlobalArena;
682 for (i = 0; i < globalArenaSize; i++, pArena++)
684 if ((pArena->size != 0) && (pArena->hOwner == owner))
685 GlobalFree16( pArena->handle );
690 /***********************************************************************
691 * GlobalWire (KERNEL.111)
692 * GlobalWire16 (KERNEL32.29)
694 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
696 return WIN16_GlobalLock16( handle );
700 /***********************************************************************
701 * GlobalUnWire (KERNEL.112)
702 * GlobalUnWire16 (KERNEL32.30)
704 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
706 return !GlobalUnlock16( handle );
710 /***********************************************************************
711 * SetSwapAreaSize (KERNEL.106)
713 LONG WINAPI SetSwapAreaSize16( WORD size )
715 FIXME("(%d) - stub!\n", size );
716 return MAKELONG( size, 0xffff );
720 /***********************************************************************
721 * GlobalLRUOldest (KERNEL.163)
723 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
725 TRACE("%04x\n", handle );
726 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
731 /***********************************************************************
732 * GlobalLRUNewest (KERNEL.164)
734 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
736 TRACE("%04x\n", handle );
737 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
742 /***********************************************************************
743 * GetFreeSpace (KERNEL.169)
745 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
748 GlobalMemoryStatus( &ms );
749 return ms.dwAvailVirtual;
752 /***********************************************************************
753 * GlobalDOSAlloc (KERNEL.184)
755 * Address (HW=Paragraph segment; LW=Selector)
757 DWORD WINAPI GlobalDOSAlloc16(
758 DWORD size /* [in] Number of bytes to be allocated */
761 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
765 HMODULE16 hModule = GetModuleHandle16("KERNEL");
769 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
770 pArena = GET_ARENA_PTR(wSelector);
771 pArena->flags |= GA_DOSMEM;
772 return MAKELONG(wSelector,uParagraph);
778 /***********************************************************************
779 * GlobalDOSFree (KERNEL.185)
784 WORD WINAPI GlobalDOSFree16(
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( lpBlock ) )
793 GLOBAL_FreeBlock( sel );
800 /***********************************************************************
801 * GlobalPageLock (KERNEL.191)
802 * GlobalSmartPageLock(KERNEL.230)
804 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
806 TRACE("%04x\n", handle );
807 if (!VALID_HANDLE(handle)) {
808 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
811 return ++(GET_ARENA_PTR(handle)->pageLockCount);
815 /***********************************************************************
816 * GlobalPageUnlock (KERNEL.192)
817 * GlobalSmartPageUnlock(KERNEL.231)
819 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
821 TRACE("%04x\n", handle );
822 if (!VALID_HANDLE(handle)) {
823 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
826 return --(GET_ARENA_PTR(handle)->pageLockCount);
830 /***********************************************************************
831 * GlobalFix (KERNEL.197)
832 * GlobalFix16 (KERNEL32.27)
834 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
836 TRACE("%04x\n", handle );
837 if (!VALID_HANDLE(handle)) {
838 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
841 GET_ARENA_PTR(handle)->lockCount++;
843 return GlobalHandleToSel16(handle);
847 /***********************************************************************
848 * GlobalUnfix (KERNEL.198)
849 * GlobalUnfix16 (KERNEL32.28)
851 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
853 TRACE("%04x\n", handle );
854 if (!VALID_HANDLE(handle)) {
855 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
858 GET_ARENA_PTR(handle)->lockCount--;
862 /***********************************************************************
863 * FarSetOwner (KERNEL.403)
865 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
867 if (!VALID_HANDLE(handle)) {
868 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
871 GET_ARENA_PTR(handle)->hOwner = hOwner;
875 /***********************************************************************
876 * FarGetOwner (KERNEL.404)
878 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
880 if (!VALID_HANDLE(handle)) {
881 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
884 return GET_ARENA_PTR(handle)->hOwner;
888 /***********************************************************************
889 * GlobalHandleToSel (TOOLHELP.50)
891 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
893 if (!handle) return 0;
894 if (!VALID_HANDLE(handle)) {
895 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
900 WARN("Program attempted invalid selector conversion\n" );
907 /***********************************************************************
908 * GlobalFirst (TOOLHELP.51)
910 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
912 if (wFlags == GLOBAL_LRU) return FALSE;
914 return GlobalNext16( pGlobal, wFlags );
918 /***********************************************************************
919 * GlobalNext (TOOLHELP.52)
921 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
925 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
926 pArena = pGlobalArena + pGlobal->dwNext;
927 if (wFlags == GLOBAL_FREE) /* only free blocks */
930 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
931 if (pArena->size == 0) break; /* block is free */
932 if (i >= globalArenaSize) return FALSE;
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 = (GetCurrentPDB16() == pArena->hOwner);
942 pGlobal->wHeapPresent = FALSE;
943 pGlobal->hOwner = pArena->hOwner;
944 pGlobal->wType = GT_UNKNOWN;
951 /***********************************************************************
952 * GlobalInfo (TOOLHELP.53)
954 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
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++;
968 /***********************************************************************
969 * GlobalEntryHandle (TOOLHELP.54)
971 BOOL16 WINAPI GlobalEntryHandle16( 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 = (GetCurrentPDB16() == pArena->hOwner);
981 pGlobal->wHeapPresent = FALSE;
982 pGlobal->hOwner = pArena->hOwner;
983 pGlobal->wType = GT_UNKNOWN;
990 /***********************************************************************
991 * GlobalEntryModule (TOOLHELP.55)
993 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
996 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1001 /***********************************************************************
1002 * MemManInfo (TOOLHELP.72)
1004 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1006 MEMORYSTATUS status;
1009 * Not unsurprisingly although the documention says you
1010 * _must_ provide the size in the dwSize field, this function
1011 * (under Windows) always fills the structure and returns true.
1013 GlobalMemoryStatus( &status );
1014 info->wPageSize = getpagesize();
1015 info->dwLargestFreeBlock = status.dwAvailVirtual;
1016 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1017 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1018 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1019 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1020 info->dwFreePages = info->dwMaxPagesAvailable;
1021 info->dwTotalPages = info->dwTotalLinearSpace;
1022 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1023 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1027 /***********************************************************************
1028 * GetFreeMemInfo (KERNEL.316)
1030 DWORD WINAPI GetFreeMemInfo16(void)
1033 MemManInfo16( &info );
1034 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1037 /***********************************************************************
1038 * A20Proc (KERNEL.165)
1039 * A20_Proc (SYSTEM.20)
1041 void WINAPI A20Proc16( WORD unused )
1043 /* this is also a NOP in Windows */
1046 /***********************************************************************
1047 * LimitEMSPages (KERNEL.156)
1049 DWORD WINAPI LimitEMSPages16( DWORD unused )