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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
43 #include "kernel_private.h"
44 #include "kernel16_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(global);
49 /* Global arena block */
52 void *base; /* Base address (0 if discarded) */
53 DWORD size; /* Size in bytes (0 indicates a free block) */
54 HGLOBAL16 handle; /* Handle for this block */
55 HGLOBAL16 hOwner; /* Owner of this block */
56 BYTE lockCount; /* Count of GlobalFix() calls */
57 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
58 BYTE flags; /* Allocation flags */
59 BYTE selCount; /* Number of selectors allocated for this block */
62 /* Flags definitions */
63 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
64 #define GA_DGROUP 0x04
65 #define GA_DISCARDABLE 0x08
66 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
67 #define GA_DOSMEM 0x20
69 /* Arena array (FIXME) */
70 static GLOBALARENA *pGlobalArena;
71 static int globalArenaSize;
73 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
75 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
76 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
78 static inline void* DOSMEM_AllocBlock(UINT size, UINT16* pseg)
80 if (!winedos.AllocDosBlock) load_winedos();
81 return winedos.AllocDosBlock ? winedos.AllocDosBlock(size, pseg) : NULL;
84 static inline BOOL DOSMEM_FreeBlock(void* ptr)
86 if (!winedos.FreeDosBlock) load_winedos();
87 return winedos.FreeDosBlock ? winedos.FreeDosBlock( ptr ) : FALSE;
90 static inline UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
92 if (!winedos.ResizeDosBlock) load_winedos();
93 return winedos.ResizeDosBlock ? winedos.ResizeDosBlock(ptr, size, TRUE) : 0;
96 static HANDLE get_win16_heap(void)
98 static HANDLE win16_heap;
100 /* we create global memory block with execute permission. The access can be limited
101 * for 16-bit code on selector level */
102 if (!win16_heap) win16_heap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
106 /***********************************************************************
109 * Return the arena for a given selector, growing the arena array if needed.
111 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
113 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
115 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
116 GLOBALARENA *pNewArena;
119 pNewArena = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 pGlobalArena, newsize * sizeof(GLOBALARENA) );
122 pNewArena = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, newsize * sizeof(GLOBALARENA) );
123 if (!pNewArena) return 0;
124 pGlobalArena = pNewArena;
125 globalArenaSize = newsize;
127 return pGlobalArena + (sel >> __AHSHIFT);
130 void debug_handles(void)
134 for (i = globalArenaSize-1 ; i>=0 ; i--) {
135 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
137 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
145 /***********************************************************************
148 * Create a global heap block for a fixed range of linear memory.
150 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
151 HGLOBAL16 hOwner, unsigned char selflags )
156 /* Allocate the selector(s) */
158 sel = SELECTOR_AllocBlock( ptr, size, selflags );
160 selcount = (size + 0xffff) / 0x10000;
162 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
164 SELECTOR_FreeBlock( sel );
168 /* Fill the arena block */
171 pArena->size = GetSelectorLimit16(sel) + 1;
172 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
173 pArena->hOwner = hOwner;
174 pArena->lockCount = 0;
175 pArena->pageLockCount = 0;
176 pArena->flags = flags & GA_MOVEABLE;
177 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
178 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
179 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
180 pArena->selCount = selcount;
181 if (selcount > 1) /* clear the next arena blocks */
182 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
184 return pArena->handle;
188 /***********************************************************************
191 * Free a block allocated by GLOBAL_CreateBlock, without touching
192 * the associated linear memory range.
194 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
199 if (!handle) return TRUE;
200 sel = GlobalHandleToSel16( handle );
201 if (!VALID_HANDLE(sel)) return FALSE;
202 pArena = GET_ARENA_PTR(sel);
203 SELECTOR_FreeBlock( sel );
204 memset( pArena, 0, sizeof(GLOBALARENA) );
208 /***********************************************************************
211 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
216 if (!handle) return TRUE;
217 sel = GlobalHandleToSel16( handle );
218 if (!VALID_HANDLE(sel)) return FALSE;
219 pArena = GET_ARENA_PTR(sel);
220 if (pArena->selCount != 1)
225 SELECTOR_ReallocBlock( sel, ptr, size );
229 /***********************************************************************
232 * Implementation of GlobalAlloc16()
234 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
239 TRACE("%d flags=%04x\n", size, flags );
241 /* If size is 0, create a discarded block */
243 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
247 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
248 size = (size + 0x1f) & ~0x1f;
250 /* Allocate the linear memory */
251 ptr = HeapAlloc( get_win16_heap(), 0, size );
252 /* FIXME: free discardable blocks and try again? */
255 /* Allocate the selector(s) */
257 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
260 HeapFree( get_win16_heap(), 0, ptr );
264 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
268 /***********************************************************************
269 * GlobalAlloc (KERNEL.15)
270 * GlobalAlloc16 (KERNEL32.24)
272 * Allocate a global memory object.
278 HGLOBAL16 WINAPI GlobalAlloc16(
279 UINT16 flags, /* [in] Object allocation attributes */
280 DWORD size /* [in] Number of bytes to allocate */
282 HANDLE16 owner = GetCurrentPDB16();
284 if (flags & GMEM_DDESHARE)
285 owner = GetExePtr(owner); /* Make it a module handle */
286 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
290 /***********************************************************************
291 * GlobalReAlloc (KERNEL.16)
293 * Change the size or attributes of a global memory object.
299 HGLOBAL16 WINAPI GlobalReAlloc16(
300 HGLOBAL16 handle, /* [in] Handle of global memory object */
301 DWORD size, /* [in] New size of block */
302 UINT16 flags /* [in] How to reallocate object */
307 GLOBALARENA *pArena, *pNewArena;
308 WORD sel = GlobalHandleToSel16( handle );
309 HANDLE heap = get_win16_heap();
311 TRACE("%04x %d flags=%04x\n",
312 handle, size, flags );
313 if (!handle) return 0;
315 if (!VALID_HANDLE(handle))
317 WARN("Invalid handle 0x%04x!\n", handle);
320 pArena = GET_ARENA_PTR( handle );
322 /* Discard the block if requested */
324 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
326 if (!(pArena->flags & GA_MOVEABLE) ||
327 !(pArena->flags & GA_DISCARDABLE) ||
328 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
329 if (pArena->flags & GA_DOSMEM)
330 DOSMEM_FreeBlock( pArena->base );
332 HeapFree( heap, 0, pArena->base );
335 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
336 * change the selector if we are shrinking the block.
337 * FIXME: shouldn't we keep selectors until the block is deleted?
339 SELECTOR_ReallocBlock( sel, 0, 1 );
345 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
346 if (size == 0) size = 0x20;
347 else size = (size + 0x1f) & ~0x1f;
349 /* Change the flags */
351 if (flags & GMEM_MODIFY)
353 /* Change the flags, leaving GA_DGROUP alone */
354 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
355 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
359 /* Reallocate the linear memory */
362 oldsize = pArena->size;
363 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr,oldsize,size);
364 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
366 if (pArena->flags & GA_DOSMEM)
368 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
370 else if(pArena->pageLockCount > 0)
374 newptr = DOSMEM_AllocBlock( size, 0 );
377 memcpy( newptr, ptr, oldsize );
378 DOSMEM_FreeBlock( ptr );
385 * if more than one reader (e.g. some pointer has been
386 * given out by GetVDMPointer32W16),
387 * only try to realloc in place
391 newptr = HeapReAlloc( heap,
392 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
395 newptr = HeapAlloc( heap, 0, size );
401 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
402 if (pArena->pageLockCount <1)
404 if (pArena->flags & GA_DOSMEM)
405 DOSMEM_FreeBlock( pArena->base );
407 HeapFree( heap, 0, ptr );
408 SELECTOR_FreeBlock( sel );
409 memset( pArena, 0, sizeof(GLOBALARENA) );
415 /* Reallocate the selector(s) */
417 sel = SELECTOR_ReallocBlock( sel, ptr, size );
420 if (pArena->flags & GA_DOSMEM)
421 DOSMEM_FreeBlock( pArena->base );
423 HeapFree( heap, 0, ptr );
424 memset( pArena, 0, sizeof(GLOBALARENA) );
427 selcount = (size + 0xffff) / 0x10000;
429 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
431 if (pArena->flags & GA_DOSMEM)
432 DOSMEM_FreeBlock( pArena->base );
434 HeapFree( heap, 0, ptr );
435 SELECTOR_FreeBlock( sel );
439 /* Fill the new arena block
440 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
442 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
443 pNewArena->base = ptr;
444 pNewArena->size = GetSelectorLimit16(sel) + 1;
445 pNewArena->selCount = selcount;
446 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
448 if (selcount > 1) /* clear the next arena blocks */
449 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
451 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
452 memset( (char *)ptr + oldsize, 0, size - oldsize );
453 return pNewArena->handle;
457 /***********************************************************************
458 * GlobalFree (KERNEL.17)
459 * GlobalFree16 (KERNEL32.31)
464 HGLOBAL16 WINAPI GlobalFree16(
465 HGLOBAL16 handle /* [in] Handle of global memory object */
469 if (!VALID_HANDLE(handle))
471 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
474 ptr = GET_ARENA_PTR(handle)->base;
476 TRACE("%04x\n", handle );
477 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
478 HeapFree( get_win16_heap(), 0, ptr );
483 /**********************************************************************
484 * K32WOWGlobalLock16 (KERNEL32.60)
486 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
488 WORD sel = GlobalHandleToSel16( handle );
489 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
493 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
495 if (!VALID_HANDLE(handle)) {
496 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
499 else if (!GET_ARENA_PTR(handle)->base)
502 GET_ARENA_PTR(handle)->lockCount++;
505 return MAKESEGPTR( sel, 0 );
510 /***********************************************************************
511 * GlobalLock (KERNEL.18)
513 * This is the GlobalLock16() function used by 16-bit code.
515 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
517 SEGPTR ret = K32WOWGlobalLock16( handle );
518 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
523 /***********************************************************************
524 * GlobalLock16 (KERNEL32.25)
526 * This is the GlobalLock16() function used by 32-bit code.
529 * Pointer to first byte of memory block
532 LPVOID WINAPI GlobalLock16(
533 HGLOBAL16 handle /* [in] Handle of global memory object */
535 if (!handle) return 0;
536 if (!VALID_HANDLE(handle))
538 GET_ARENA_PTR(handle)->lockCount++;
539 return GET_ARENA_PTR(handle)->base;
543 /***********************************************************************
544 * GlobalUnlock (KERNEL.19)
545 * GlobalUnlock16 (KERNEL32.26)
547 * Should the return values be cast to booleans?
550 * TRUE: Object is still locked
551 * FALSE: Object is unlocked
553 BOOL16 WINAPI GlobalUnlock16(
554 HGLOBAL16 handle /* [in] Handle of global memory object */
556 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
557 if (!VALID_HANDLE(handle)) {
558 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
561 TRACE("%04x\n", handle );
562 if (pArena->lockCount) pArena->lockCount--;
563 return pArena->lockCount;
566 /***********************************************************************
567 * GlobalChangeLockCount (KERNEL.365)
569 * This is declared as a register function as it has to preserve
570 * *all* registers, even AX/DX !
573 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
577 GlobalLock16( handle );
578 else if ( delta == -1 )
579 GlobalUnlock16( handle );
581 ERR("(%04X, %d): strange delta value\n", handle, delta );
584 /***********************************************************************
585 * GlobalSize (KERNEL.20)
586 * GlobalSize16 (KERNEL32.32)
588 * Get the current size of a global memory object.
591 * Size in bytes of object
594 DWORD WINAPI GlobalSize16(
595 HGLOBAL16 handle /* [in] Handle of global memory object */
597 TRACE("%04x\n", handle );
598 if (!handle) return 0;
599 if (!VALID_HANDLE(handle))
601 return GET_ARENA_PTR(handle)->size;
605 /***********************************************************************
606 * GlobalHandle (KERNEL.21)
608 * Get the handle associated with a pointer to the global memory block.
611 * Why is GlobalHandleToSel used here with the sel as input?
617 DWORD WINAPI GlobalHandle16(
618 WORD sel /* [in] Address of global memory block */
620 TRACE("%04x\n", sel );
621 if (!VALID_HANDLE(sel)) {
622 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
625 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
628 /***********************************************************************
629 * GlobalHandleNoRIP (KERNEL.159)
631 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
634 for (i = globalArenaSize-1 ; i>=0 ; i--) {
635 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
636 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
642 /***********************************************************************
643 * GlobalFlags (KERNEL.22)
645 * Get information about a global memory object.
648 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
652 * Value specifying flags and lock count
653 * GMEM_INVALID_HANDLE: Invalid handle
655 UINT16 WINAPI GlobalFlags16(
656 HGLOBAL16 handle /* [in] Handle of global memory object */
660 TRACE("%04x\n", handle );
661 if (!VALID_HANDLE(handle)) {
662 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
665 pArena = GET_ARENA_PTR(handle);
666 return pArena->lockCount |
667 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
668 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
672 /***********************************************************************
673 * LockSegment (KERNEL.23)
675 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
677 TRACE("%04x\n", handle );
678 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
679 if (!VALID_HANDLE(handle)) {
680 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
683 GET_ARENA_PTR(handle)->lockCount++;
688 /***********************************************************************
689 * UnlockSegment (KERNEL.24)
691 void WINAPI UnlockSegment16( HGLOBAL16 handle )
693 TRACE("%04x\n", handle );
694 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
695 if (!VALID_HANDLE(handle)) {
696 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
699 GET_ARENA_PTR(handle)->lockCount--;
700 /* FIXME: this ought to return the lock count in CX (go figure...) */
704 /***********************************************************************
705 * GlobalCompact (KERNEL.25)
707 DWORD WINAPI GlobalCompact16( DWORD desired )
709 return GLOBAL_MAX_ALLOC_SIZE;
713 /***********************************************************************
714 * GlobalFreeAll (KERNEL.26)
716 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
721 pArena = pGlobalArena;
722 for (i = 0; i < globalArenaSize; i++, pArena++)
724 if ((pArena->size != 0) && (pArena->hOwner == owner))
725 GlobalFree16( pArena->handle );
730 /***********************************************************************
731 * GlobalWire (KERNEL.111)
732 * GlobalWire16 (KERNEL32.29)
734 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
736 return WIN16_GlobalLock16( handle );
740 /***********************************************************************
741 * GlobalUnWire (KERNEL.112)
742 * GlobalUnWire16 (KERNEL32.30)
744 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
746 return !GlobalUnlock16( handle );
750 /***********************************************************************
751 * SetSwapAreaSize (KERNEL.106)
753 LONG WINAPI SetSwapAreaSize16( WORD size )
755 FIXME("(%d) - stub!\n", size );
756 return MAKELONG( size, 0xffff );
760 /***********************************************************************
761 * GlobalLRUOldest (KERNEL.163)
763 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
765 TRACE("%04x\n", handle );
766 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
771 /***********************************************************************
772 * GlobalLRUNewest (KERNEL.164)
774 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
776 TRACE("%04x\n", handle );
777 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
782 /***********************************************************************
783 * GetFreeSpace (KERNEL.169)
785 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
788 GlobalMemoryStatus( &ms );
789 return ms.dwAvailVirtual;
792 /***********************************************************************
793 * GlobalDOSAlloc (KERNEL.184)
795 * Allocate memory in the first MB.
798 * Address (HW=Paragraph segment; LW=Selector)
800 DWORD WINAPI GlobalDOSAlloc16(
801 DWORD size /* [in] Number of bytes to be allocated */
804 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
808 HMODULE16 hModule = GetModuleHandle16("KERNEL");
812 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
813 pArena = GET_ARENA_PTR(wSelector);
814 pArena->flags |= GA_DOSMEM;
815 return MAKELONG(wSelector,uParagraph);
821 /***********************************************************************
822 * GlobalDOSFree (KERNEL.185)
824 * Free memory allocated with GlobalDOSAlloc
830 WORD WINAPI GlobalDOSFree16(
831 WORD sel /* [in] Selector */
833 DWORD block = GetSelectorBase(sel);
835 if( block && block < 0x100000 )
837 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
838 if( DOSMEM_FreeBlock( lpBlock ) )
839 GLOBAL_FreeBlock( sel );
846 /***********************************************************************
847 * GlobalPageLock (KERNEL.191)
848 * GlobalSmartPageLock(KERNEL.230)
850 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
852 TRACE("%04x\n", handle );
853 if (!VALID_HANDLE(handle)) {
854 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
857 return ++(GET_ARENA_PTR(handle)->pageLockCount);
861 /***********************************************************************
862 * GlobalPageUnlock (KERNEL.192)
863 * GlobalSmartPageUnlock(KERNEL.231)
865 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
867 TRACE("%04x\n", handle );
868 if (!VALID_HANDLE(handle)) {
869 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
872 return --(GET_ARENA_PTR(handle)->pageLockCount);
876 /***********************************************************************
877 * GlobalFix (KERNEL.197)
878 * GlobalFix16 (KERNEL32.27)
880 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
882 TRACE("%04x\n", handle );
883 if (!VALID_HANDLE(handle)) {
884 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
887 GET_ARENA_PTR(handle)->lockCount++;
889 return GlobalHandleToSel16(handle);
893 /***********************************************************************
894 * GlobalUnfix (KERNEL.198)
895 * GlobalUnfix16 (KERNEL32.28)
897 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
899 TRACE("%04x\n", handle );
900 if (!VALID_HANDLE(handle)) {
901 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
904 GET_ARENA_PTR(handle)->lockCount--;
908 /***********************************************************************
909 * FarSetOwner (KERNEL.403)
911 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
913 if (!VALID_HANDLE(handle)) {
914 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
917 GET_ARENA_PTR(handle)->hOwner = hOwner;
921 /***********************************************************************
922 * FarGetOwner (KERNEL.404)
924 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
926 if (!VALID_HANDLE(handle)) {
927 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
930 return GET_ARENA_PTR(handle)->hOwner;
934 /************************************************************************
935 * GlobalMasterHandle (KERNEL.28)
938 * Should return selector and handle of the information structure for
939 * the global heap. selector and handle are stored in the THHOOK as
940 * pGlobalHeap and hGlobalHeap.
941 * As Wine doesn't have this structure, we return both values as zero
942 * Applications should interpret this as "No Global Heap"
944 DWORD WINAPI GlobalMasterHandle16(void)
950 /***********************************************************************
951 * GlobalHandleToSel (TOOLHELP.50)
953 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
955 if (!handle) return 0;
956 if (!VALID_HANDLE(handle)) {
957 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
962 WARN("Program attempted invalid selector conversion\n" );
969 /***********************************************************************
970 * GlobalFirst (TOOLHELP.51)
972 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
974 if (wFlags == GLOBAL_LRU) return FALSE;
976 return GlobalNext16( pGlobal, wFlags );
980 /***********************************************************************
981 * GlobalNext (TOOLHELP.52)
983 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
987 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
988 pArena = pGlobalArena + pGlobal->dwNext;
989 if (wFlags == GLOBAL_FREE) /* only free blocks */
992 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
993 if (pArena->size == 0) break; /* block is free */
994 if (i >= globalArenaSize) return FALSE;
998 pGlobal->dwAddress = (DWORD_PTR)pArena->base;
999 pGlobal->dwBlockSize = pArena->size;
1000 pGlobal->hBlock = pArena->handle;
1001 pGlobal->wcLock = pArena->lockCount;
1002 pGlobal->wcPageLock = pArena->pageLockCount;
1003 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
1004 pGlobal->wHeapPresent = FALSE;
1005 pGlobal->hOwner = pArena->hOwner;
1006 pGlobal->wType = GT_UNKNOWN;
1013 /***********************************************************************
1014 * GlobalInfo (TOOLHELP.53)
1016 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
1019 GLOBALARENA *pArena;
1021 pInfo->wcItems = globalArenaSize;
1022 pInfo->wcItemsFree = 0;
1023 pInfo->wcItemsLRU = 0;
1024 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
1025 if (pArena->size == 0) pInfo->wcItemsFree++;
1030 /***********************************************************************
1031 * GlobalEntryHandle (TOOLHELP.54)
1033 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
1035 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
1037 pGlobal->dwAddress = (DWORD_PTR)pArena->base;
1038 pGlobal->dwBlockSize = pArena->size;
1039 pGlobal->hBlock = pArena->handle;
1040 pGlobal->wcLock = pArena->lockCount;
1041 pGlobal->wcPageLock = pArena->pageLockCount;
1042 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
1043 pGlobal->wHeapPresent = FALSE;
1044 pGlobal->hOwner = pArena->hOwner;
1045 pGlobal->wType = GT_UNKNOWN;
1052 /***********************************************************************
1053 * GlobalEntryModule (TOOLHELP.55)
1055 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1058 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1063 /***********************************************************************
1064 * MemManInfo (TOOLHELP.72)
1066 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1068 MEMORYSTATUS status;
1071 * Not unsurprisingly although the documentation says you
1072 * _must_ provide the size in the dwSize field, this function
1073 * (under Windows) always fills the structure and returns true.
1075 GlobalMemoryStatus( &status );
1076 info->wPageSize = getpagesize();
1077 info->dwLargestFreeBlock = status.dwAvailVirtual;
1078 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1079 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1080 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1081 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1082 info->dwFreePages = info->dwMaxPagesAvailable;
1083 info->dwTotalPages = info->dwTotalLinearSpace;
1084 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1085 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1089 /***********************************************************************
1090 * GetFreeMemInfo (KERNEL.316)
1092 DWORD WINAPI GetFreeMemInfo16(void)
1095 MemManInfo16( &info );
1096 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1099 /***********************************************************************
1100 * A20Proc (KERNEL.165)
1102 void WINAPI A20Proc16( WORD unused )
1104 /* this is also a NOP in Windows */
1107 /***********************************************************************
1108 * LimitEMSPages (KERNEL.156)
1110 DWORD WINAPI LimitEMSPages16( DWORD unused )