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 "kernel_private.h"
45 #include "kernel16_private.h"
46 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(global);
51 /* Global arena block */
54 DWORD base; /* Base address (0 if discarded) */
55 DWORD size; /* Size in bytes (0 indicates a free block) */
56 HGLOBAL16 handle; /* Handle for this block */
57 HGLOBAL16 hOwner; /* Owner of this block */
58 BYTE lockCount; /* Count of GlobalFix() calls */
59 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
60 BYTE flags; /* Allocation flags */
61 BYTE selCount; /* Number of selectors allocated for this block */
64 /* Flags definitions */
65 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
66 #define GA_DGROUP 0x04
67 #define GA_DISCARDABLE 0x08
68 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
69 #define GA_DOSMEM 0x20
71 /* Arena array (FIXME) */
72 static GLOBALARENA *pGlobalArena;
73 static int globalArenaSize;
75 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
77 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
78 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
80 static inline void* DOSMEM_AllocBlock(UINT size, UINT16* pseg)
82 if (!winedos.AllocDosBlock) load_winedos();
83 return winedos.AllocDosBlock ? winedos.AllocDosBlock(size, pseg) : NULL;
86 static inline BOOL DOSMEM_FreeBlock(void* ptr)
88 if (!winedos.FreeDosBlock) load_winedos();
89 return winedos.FreeDosBlock ? winedos.FreeDosBlock( ptr ) : FALSE;
92 static inline UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
94 if (!winedos.ResizeDosBlock) load_winedos();
95 return winedos.ResizeDosBlock ? winedos.ResizeDosBlock(ptr, size, TRUE) : 0;
98 /***********************************************************************
101 * Return the arena for a given selector, growing the arena array if needed.
103 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
105 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
107 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
108 GLOBALARENA *pNewArena = realloc( pGlobalArena,
109 newsize * sizeof(GLOBALARENA) );
110 if (!pNewArena) return 0;
111 pGlobalArena = pNewArena;
112 memset( pGlobalArena + globalArenaSize, 0,
113 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
114 globalArenaSize = newsize;
116 return pGlobalArena + (sel >> __AHSHIFT);
119 void debug_handles(void)
123 for (i = globalArenaSize-1 ; i>=0 ; i--) {
124 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
126 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
134 /***********************************************************************
137 * Create a global heap block for a fixed range of linear memory.
139 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
140 HGLOBAL16 hOwner, unsigned char selflags )
145 /* Allocate the selector(s) */
147 sel = SELECTOR_AllocBlock( ptr, size, selflags );
149 selcount = (size + 0xffff) / 0x10000;
151 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
153 SELECTOR_FreeBlock( sel );
157 /* Fill the arena block */
159 pArena->base = (DWORD)ptr;
160 pArena->size = GetSelectorLimit16(sel) + 1;
161 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
162 pArena->hOwner = hOwner;
163 pArena->lockCount = 0;
164 pArena->pageLockCount = 0;
165 pArena->flags = flags & GA_MOVEABLE;
166 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
167 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
168 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
169 pArena->selCount = selcount;
170 if (selcount > 1) /* clear the next arena blocks */
171 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
173 return pArena->handle;
177 /***********************************************************************
180 * Free a block allocated by GLOBAL_CreateBlock, without touching
181 * the associated linear memory range.
183 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
188 if (!handle) return TRUE;
189 sel = GlobalHandleToSel16( handle );
190 if (!VALID_HANDLE(sel)) return FALSE;
191 pArena = GET_ARENA_PTR(sel);
192 SELECTOR_FreeBlock( sel );
193 memset( pArena, 0, sizeof(GLOBALARENA) );
197 /***********************************************************************
200 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
205 if (!handle) return TRUE;
206 sel = GlobalHandleToSel16( handle );
207 if (!VALID_HANDLE(sel)) return FALSE;
208 pArena = GET_ARENA_PTR(sel);
209 if (pArena->selCount != 1)
212 pArena->base = (DWORD)ptr;
214 SELECTOR_ReallocBlock( sel, ptr, size );
218 /***********************************************************************
221 * Implementation of GlobalAlloc16()
223 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
228 TRACE("%ld flags=%04x\n", size, flags );
230 /* If size is 0, create a discarded block */
232 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
236 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
237 size = (size + 0x1f) & ~0x1f;
239 /* Allocate the linear memory */
240 ptr = HeapAlloc( GetProcessHeap(), 0, size );
241 /* FIXME: free discardable blocks and try again? */
244 /* Allocate the selector(s) */
246 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
249 HeapFree( GetProcessHeap(), 0, ptr );
253 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
257 /***********************************************************************
258 * GlobalAlloc (KERNEL.15)
259 * GlobalAlloc16 (KERNEL32.24)
261 * Allocate a global memory object.
267 HGLOBAL16 WINAPI GlobalAlloc16(
268 UINT16 flags, /* [in] Object allocation attributes */
269 DWORD size /* [in] Number of bytes to allocate */
271 HANDLE16 owner = GetCurrentPDB16();
273 if (flags & GMEM_DDESHARE)
274 owner = GetExePtr(owner); /* Make it a module handle */
275 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
279 /***********************************************************************
280 * GlobalReAlloc (KERNEL.16)
282 * Change the size or attributes of a global memory object.
288 HGLOBAL16 WINAPI GlobalReAlloc16(
289 HGLOBAL16 handle, /* [in] Handle of global memory object */
290 DWORD size, /* [in] New size of block */
291 UINT16 flags /* [in] How to reallocate object */
296 GLOBALARENA *pArena, *pNewArena;
297 WORD sel = GlobalHandleToSel16( handle );
299 TRACE("%04x %ld flags=%04x\n",
300 handle, size, flags );
301 if (!handle) return 0;
303 if (!VALID_HANDLE(handle))
305 WARN("Invalid handle 0x%04x!\n", handle);
308 pArena = GET_ARENA_PTR( handle );
310 /* Discard the block if requested */
312 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
314 if (!(pArena->flags & GA_MOVEABLE) ||
315 !(pArena->flags & GA_DISCARDABLE) ||
316 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
317 if (pArena->flags & GA_DOSMEM)
318 DOSMEM_FreeBlock( (void *)pArena->base );
320 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
323 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
324 * change the selector if we are shrinking the block.
325 * FIXME: shouldn't we keep selectors until the block is deleted?
327 SELECTOR_ReallocBlock( sel, 0, 1 );
333 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
334 if (size == 0) size = 0x20;
335 else size = (size + 0x1f) & ~0x1f;
337 /* Change the flags */
339 if (flags & GMEM_MODIFY)
341 /* Change the flags, leaving GA_DGROUP alone */
342 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
343 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
347 /* Reallocate the linear memory */
349 ptr = (void *)pArena->base;
350 oldsize = pArena->size;
351 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
352 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
354 if (pArena->flags & GA_DOSMEM)
356 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
358 else if(pArena->pageLockCount > 0)
362 newptr = DOSMEM_AllocBlock( size, 0 );
365 memcpy( newptr, ptr, oldsize );
366 DOSMEM_FreeBlock( ptr );
373 * if more than one reader (e.g. some pointer has been
374 * given out by GetVDMPointer32W16),
375 * only try to realloc in place
379 newptr = HeapReAlloc( GetProcessHeap(),
380 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
383 newptr = HeapAlloc( GetProcessHeap(),
384 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
391 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
392 if (pArena->pageLockCount <1)
394 if (pArena->flags & GA_DOSMEM)
395 DOSMEM_FreeBlock( (void *)pArena->base );
397 HeapFree( GetProcessHeap(), 0, ptr );
398 SELECTOR_FreeBlock( sel );
399 memset( pArena, 0, sizeof(GLOBALARENA) );
405 /* Reallocate the selector(s) */
407 sel = SELECTOR_ReallocBlock( sel, ptr, size );
410 if (pArena->flags & GA_DOSMEM)
411 DOSMEM_FreeBlock( (void *)pArena->base );
413 HeapFree( GetProcessHeap(), 0, ptr );
414 memset( pArena, 0, sizeof(GLOBALARENA) );
417 selcount = (size + 0xffff) / 0x10000;
419 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
421 if (pArena->flags & GA_DOSMEM)
422 DOSMEM_FreeBlock( (void *)pArena->base );
424 HeapFree( GetProcessHeap(), 0, ptr );
425 SELECTOR_FreeBlock( sel );
429 /* Fill the new arena block
430 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
432 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
433 pNewArena->base = (DWORD)ptr;
434 pNewArena->size = GetSelectorLimit16(sel) + 1;
435 pNewArena->selCount = selcount;
436 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
438 if (selcount > 1) /* clear the next arena blocks */
439 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
441 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
442 memset( (char *)ptr + oldsize, 0, size - oldsize );
443 return pNewArena->handle;
447 /***********************************************************************
448 * GlobalFree (KERNEL.17)
449 * GlobalFree16 (KERNEL32.31)
454 HGLOBAL16 WINAPI GlobalFree16(
455 HGLOBAL16 handle /* [in] Handle of global memory object */
459 if (!VALID_HANDLE(handle))
461 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
464 ptr = (void *)GET_ARENA_PTR(handle)->base;
466 TRACE("%04x\n", handle );
467 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
468 HeapFree( GetProcessHeap(), 0, ptr );
473 /**********************************************************************
474 * K32WOWGlobalLock16 (KERNEL32.60)
476 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
478 WORD sel = GlobalHandleToSel16( handle );
479 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
483 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
485 if (!VALID_HANDLE(handle)) {
486 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
489 else if (!GET_ARENA_PTR(handle)->base)
492 GET_ARENA_PTR(handle)->lockCount++;
495 return MAKESEGPTR( sel, 0 );
500 /***********************************************************************
501 * GlobalLock (KERNEL.18)
503 * This is the GlobalLock16() function used by 16-bit code.
505 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
507 SEGPTR ret = K32WOWGlobalLock16( handle );
508 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
513 /***********************************************************************
514 * GlobalLock16 (KERNEL32.25)
516 * This is the GlobalLock16() function used by 32-bit code.
519 * Pointer to first byte of memory block
522 LPVOID WINAPI GlobalLock16(
523 HGLOBAL16 handle /* [in] Handle of global memory object */
525 if (!handle) return 0;
526 if (!VALID_HANDLE(handle))
528 GET_ARENA_PTR(handle)->lockCount++;
529 return (LPVOID)GET_ARENA_PTR(handle)->base;
533 /***********************************************************************
534 * GlobalUnlock (KERNEL.19)
535 * GlobalUnlock16 (KERNEL32.26)
537 * Should the return values be cast to booleans?
540 * TRUE: Object is still locked
541 * FALSE: Object is unlocked
543 BOOL16 WINAPI GlobalUnlock16(
544 HGLOBAL16 handle /* [in] Handle of global memory object */
546 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
547 if (!VALID_HANDLE(handle)) {
548 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
551 TRACE("%04x\n", handle );
552 if (pArena->lockCount) pArena->lockCount--;
553 return pArena->lockCount;
556 /***********************************************************************
557 * GlobalChangeLockCount (KERNEL.365)
559 * This is declared as a register function as it has to preserve
560 * *all* registers, even AX/DX !
563 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
567 GlobalLock16( handle );
568 else if ( delta == -1 )
569 GlobalUnlock16( handle );
571 ERR("(%04X, %d): strange delta value\n", handle, delta );
574 /***********************************************************************
575 * GlobalSize (KERNEL.20)
576 * GlobalSize16 (KERNEL32.32)
578 * Get the current size of a global memory object.
581 * Size in bytes of object
584 DWORD WINAPI GlobalSize16(
585 HGLOBAL16 handle /* [in] Handle of global memory object */
587 TRACE("%04x\n", handle );
588 if (!handle) return 0;
589 if (!VALID_HANDLE(handle))
591 return GET_ARENA_PTR(handle)->size;
595 /***********************************************************************
596 * GlobalHandle (KERNEL.21)
598 * Get the handle associated with a pointer to the global memory block.
601 * Why is GlobalHandleToSel used here with the sel as input?
607 DWORD WINAPI GlobalHandle16(
608 WORD sel /* [in] Address of global memory block */
610 TRACE("%04x\n", sel );
611 if (!VALID_HANDLE(sel)) {
612 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
615 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
618 /***********************************************************************
619 * GlobalHandleNoRIP (KERNEL.159)
621 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
624 for (i = globalArenaSize-1 ; i>=0 ; i--) {
625 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
626 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
632 /***********************************************************************
633 * GlobalFlags (KERNEL.22)
635 * Get information about a global memory object.
638 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
642 * Value specifying flags and lock count
643 * GMEM_INVALID_HANDLE: Invalid handle
645 UINT16 WINAPI GlobalFlags16(
646 HGLOBAL16 handle /* [in] Handle of global memory object */
650 TRACE("%04x\n", handle );
651 if (!VALID_HANDLE(handle)) {
652 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
655 pArena = GET_ARENA_PTR(handle);
656 return pArena->lockCount |
657 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
658 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
662 /***********************************************************************
663 * LockSegment (KERNEL.23)
665 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
667 TRACE("%04x\n", handle );
668 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
669 if (!VALID_HANDLE(handle)) {
670 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
673 GET_ARENA_PTR(handle)->lockCount++;
678 /***********************************************************************
679 * UnlockSegment (KERNEL.24)
681 void WINAPI UnlockSegment16( HGLOBAL16 handle )
683 TRACE("%04x\n", handle );
684 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
685 if (!VALID_HANDLE(handle)) {
686 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
689 GET_ARENA_PTR(handle)->lockCount--;
690 /* FIXME: this ought to return the lock count in CX (go figure...) */
694 /***********************************************************************
695 * GlobalCompact (KERNEL.25)
697 DWORD WINAPI GlobalCompact16( DWORD desired )
699 return GLOBAL_MAX_ALLOC_SIZE;
703 /***********************************************************************
704 * GlobalFreeAll (KERNEL.26)
706 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
711 pArena = pGlobalArena;
712 for (i = 0; i < globalArenaSize; i++, pArena++)
714 if ((pArena->size != 0) && (pArena->hOwner == owner))
715 GlobalFree16( pArena->handle );
720 /***********************************************************************
721 * GlobalWire (KERNEL.111)
722 * GlobalWire16 (KERNEL32.29)
724 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
726 return WIN16_GlobalLock16( handle );
730 /***********************************************************************
731 * GlobalUnWire (KERNEL.112)
732 * GlobalUnWire16 (KERNEL32.30)
734 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
736 return !GlobalUnlock16( handle );
740 /***********************************************************************
741 * SetSwapAreaSize (KERNEL.106)
743 LONG WINAPI SetSwapAreaSize16( WORD size )
745 FIXME("(%d) - stub!\n", size );
746 return MAKELONG( size, 0xffff );
750 /***********************************************************************
751 * GlobalLRUOldest (KERNEL.163)
753 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
755 TRACE("%04x\n", handle );
756 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
761 /***********************************************************************
762 * GlobalLRUNewest (KERNEL.164)
764 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
766 TRACE("%04x\n", handle );
767 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
772 /***********************************************************************
773 * GetFreeSpace (KERNEL.169)
775 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
778 GlobalMemoryStatus( &ms );
779 return ms.dwAvailVirtual;
782 /***********************************************************************
783 * GlobalDOSAlloc (KERNEL.184)
785 * Allocate memory in the first MB.
788 * Address (HW=Paragraph segment; LW=Selector)
790 DWORD WINAPI GlobalDOSAlloc16(
791 DWORD size /* [in] Number of bytes to be allocated */
794 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
798 HMODULE16 hModule = GetModuleHandle16("KERNEL");
802 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
803 pArena = GET_ARENA_PTR(wSelector);
804 pArena->flags |= GA_DOSMEM;
805 return MAKELONG(wSelector,uParagraph);
811 /***********************************************************************
812 * GlobalDOSFree (KERNEL.185)
814 * Free memory allocated with GlobalDOSAlloc
820 WORD WINAPI GlobalDOSFree16(
821 WORD sel /* [in] Selector */
823 DWORD block = GetSelectorBase(sel);
825 if( block && block < 0x100000 )
827 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
828 if( DOSMEM_FreeBlock( lpBlock ) )
829 GLOBAL_FreeBlock( sel );
836 /***********************************************************************
837 * GlobalPageLock (KERNEL.191)
838 * GlobalSmartPageLock(KERNEL.230)
840 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
842 TRACE("%04x\n", handle );
843 if (!VALID_HANDLE(handle)) {
844 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
847 return ++(GET_ARENA_PTR(handle)->pageLockCount);
851 /***********************************************************************
852 * GlobalPageUnlock (KERNEL.192)
853 * GlobalSmartPageUnlock(KERNEL.231)
855 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
857 TRACE("%04x\n", handle );
858 if (!VALID_HANDLE(handle)) {
859 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
862 return --(GET_ARENA_PTR(handle)->pageLockCount);
866 /***********************************************************************
867 * GlobalFix (KERNEL.197)
868 * GlobalFix16 (KERNEL32.27)
870 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
872 TRACE("%04x\n", handle );
873 if (!VALID_HANDLE(handle)) {
874 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
877 GET_ARENA_PTR(handle)->lockCount++;
879 return GlobalHandleToSel16(handle);
883 /***********************************************************************
884 * GlobalUnfix (KERNEL.198)
885 * GlobalUnfix16 (KERNEL32.28)
887 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
889 TRACE("%04x\n", handle );
890 if (!VALID_HANDLE(handle)) {
891 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
894 GET_ARENA_PTR(handle)->lockCount--;
898 /***********************************************************************
899 * FarSetOwner (KERNEL.403)
901 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
903 if (!VALID_HANDLE(handle)) {
904 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
907 GET_ARENA_PTR(handle)->hOwner = hOwner;
911 /***********************************************************************
912 * FarGetOwner (KERNEL.404)
914 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
916 if (!VALID_HANDLE(handle)) {
917 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
920 return GET_ARENA_PTR(handle)->hOwner;
924 /************************************************************************
925 * GlobalMasterHandle (KERNEL.28)
928 * Should return selector and handle of the information structure for
929 * the global heap. selector and handle are stored in the THHOOK as
930 * pGlobalHeap and hGlobalHeap.
931 * As Wine doesn't have this structure, we return both values as zero
932 * Applications should interpret this as "No Global Heap"
934 DWORD WINAPI GlobalMasterHandle16(void)
940 /***********************************************************************
941 * GlobalHandleToSel (TOOLHELP.50)
943 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
945 if (!handle) return 0;
946 if (!VALID_HANDLE(handle)) {
947 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
952 WARN("Program attempted invalid selector conversion\n" );
959 /***********************************************************************
960 * GlobalFirst (TOOLHELP.51)
962 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
964 if (wFlags == GLOBAL_LRU) return FALSE;
966 return GlobalNext16( pGlobal, wFlags );
970 /***********************************************************************
971 * GlobalNext (TOOLHELP.52)
973 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
977 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
978 pArena = pGlobalArena + pGlobal->dwNext;
979 if (wFlags == GLOBAL_FREE) /* only free blocks */
982 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
983 if (pArena->size == 0) break; /* block is free */
984 if (i >= globalArenaSize) return FALSE;
988 pGlobal->dwAddress = pArena->base;
989 pGlobal->dwBlockSize = pArena->size;
990 pGlobal->hBlock = pArena->handle;
991 pGlobal->wcLock = pArena->lockCount;
992 pGlobal->wcPageLock = pArena->pageLockCount;
993 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
994 pGlobal->wHeapPresent = FALSE;
995 pGlobal->hOwner = pArena->hOwner;
996 pGlobal->wType = GT_UNKNOWN;
1003 /***********************************************************************
1004 * GlobalInfo (TOOLHELP.53)
1006 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
1009 GLOBALARENA *pArena;
1011 pInfo->wcItems = globalArenaSize;
1012 pInfo->wcItemsFree = 0;
1013 pInfo->wcItemsLRU = 0;
1014 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
1015 if (pArena->size == 0) pInfo->wcItemsFree++;
1020 /***********************************************************************
1021 * GlobalEntryHandle (TOOLHELP.54)
1023 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
1025 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
1027 pGlobal->dwAddress = pArena->base;
1028 pGlobal->dwBlockSize = pArena->size;
1029 pGlobal->hBlock = pArena->handle;
1030 pGlobal->wcLock = pArena->lockCount;
1031 pGlobal->wcPageLock = pArena->pageLockCount;
1032 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
1033 pGlobal->wHeapPresent = FALSE;
1034 pGlobal->hOwner = pArena->hOwner;
1035 pGlobal->wType = GT_UNKNOWN;
1042 /***********************************************************************
1043 * GlobalEntryModule (TOOLHELP.55)
1045 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1048 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1053 /***********************************************************************
1054 * MemManInfo (TOOLHELP.72)
1056 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1058 MEMORYSTATUS status;
1061 * Not unsurprisingly although the documention says you
1062 * _must_ provide the size in the dwSize field, this function
1063 * (under Windows) always fills the structure and returns true.
1065 GlobalMemoryStatus( &status );
1066 info->wPageSize = getpagesize();
1067 info->dwLargestFreeBlock = status.dwAvailVirtual;
1068 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1069 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1070 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1071 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1072 info->dwFreePages = info->dwMaxPagesAvailable;
1073 info->dwTotalPages = info->dwTotalLinearSpace;
1074 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1075 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1079 /***********************************************************************
1080 * GetFreeMemInfo (KERNEL.316)
1082 DWORD WINAPI GetFreeMemInfo16(void)
1085 MemManInfo16( &info );
1086 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1089 /***********************************************************************
1090 * A20Proc (KERNEL.165)
1092 void WINAPI A20Proc16( WORD unused )
1094 /* this is also a NOP in Windows */
1097 /***********************************************************************
1098 * LimitEMSPages (KERNEL.156)
1100 DWORD WINAPI LimitEMSPages16( DWORD unused )