2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
15 #include "wine/winbase16.h"
19 #include "selectors.h"
21 #include "stackframe.h"
23 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(global);
28 /* Global arena block */
31 DWORD base; /* Base address (0 if discarded) */
32 DWORD size; /* Size in bytes (0 indicates a free block) */
33 HGLOBAL16 handle; /* Handle for this block */
34 HGLOBAL16 hOwner; /* Owner of this block */
35 BYTE lockCount; /* Count of GlobalFix() calls */
36 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
37 BYTE flags; /* Allocation flags */
38 BYTE selCount; /* Number of selectors allocated for this block */
41 /* Flags definitions */
42 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
43 #define GA_DGROUP 0x04
44 #define GA_DISCARDABLE 0x08
45 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
48 static GLOBALARENA *pGlobalArena = NULL;
49 static int globalArenaSize = 0;
51 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
53 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
54 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
56 /***********************************************************************
59 * Return the arena for a given selector, growing the arena array if needed.
61 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
63 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
65 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
66 GLOBALARENA *pNewArena = realloc( pGlobalArena,
67 newsize * sizeof(GLOBALARENA) );
68 if (!pNewArena) return 0;
69 pGlobalArena = pNewArena;
70 memset( pGlobalArena + globalArenaSize, 0,
71 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
72 globalArenaSize = newsize;
74 return pGlobalArena + (sel >> __AHSHIFT);
77 void debug_handles(void)
81 for (i = globalArenaSize-1 ; i>=0 ; i--) {
82 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
84 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
92 /***********************************************************************
95 * Create a global heap block for a fixed range of linear memory.
97 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
98 HGLOBAL16 hOwner, BOOL16 isCode,
99 BOOL16 is32Bit, BOOL16 isReadOnly )
104 /* Allocate the selector(s) */
106 sel = SELECTOR_AllocBlock( ptr, size,
107 isCode ? SEGMENT_CODE : SEGMENT_DATA,
108 is32Bit, isReadOnly );
111 selcount = (size + 0xffff) / 0x10000;
113 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
115 SELECTOR_FreeBlock( sel, selcount );
119 /* Fill the arena block */
121 pArena->base = (DWORD)ptr;
122 pArena->size = GET_SEL_LIMIT(sel) + 1;
123 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
124 pArena->hOwner = hOwner;
125 pArena->lockCount = 0;
126 pArena->pageLockCount = 0;
127 pArena->flags = flags & GA_MOVEABLE;
128 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
129 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
130 if (!isCode) pArena->flags |= GA_DGROUP;
131 pArena->selCount = selcount;
132 if (selcount > 1) /* clear the next arena blocks */
133 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
135 return pArena->handle;
139 /***********************************************************************
142 * Free a block allocated by GLOBAL_CreateBlock, without touching
143 * the associated linear memory range.
145 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
150 if (!handle) return TRUE;
151 sel = GlobalHandleToSel16( handle );
152 if (!VALID_HANDLE(sel))
154 pArena = GET_ARENA_PTR(sel);
155 SELECTOR_FreeBlock( sel, (pArena->size + 0xffff) / 0x10000 );
156 memset( pArena, 0, sizeof(GLOBALARENA) );
160 /***********************************************************************
163 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
168 if (!handle) return TRUE;
169 sel = GlobalHandleToSel16( handle );
170 if (!VALID_HANDLE(sel))
172 pArena = GET_ARENA_PTR(sel);
173 if (pArena->selCount != 1)
176 pArena->base = (DWORD)ptr;
179 SELECTOR_MoveBlock( sel, ptr );
180 SetSelectorLimit16( sel, size-1 );
185 /***********************************************************************
188 * Implementation of GlobalAlloc16()
190 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner,
191 BOOL16 isCode, BOOL16 is32Bit, BOOL16 isReadOnly )
196 TRACE("%ld flags=%04x\n", size, flags );
198 /* If size is 0, create a discarded block */
200 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, isCode,
201 is32Bit, isReadOnly );
205 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
206 size = (size + 0x1f) & ~0x1f;
208 /* Allocate the linear memory */
209 ptr = HeapAlloc( GetProcessHeap(), 0, size );
210 /* FIXME: free discardable blocks and try again? */
213 /* Allocate the selector(s) */
215 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner,
216 isCode, is32Bit, isReadOnly );
219 HeapFree( GetProcessHeap(), 0, ptr );
223 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
227 /***********************************************************************
228 * GlobalAlloc16 (KERNEL.15)
233 HGLOBAL16 WINAPI GlobalAlloc16(
234 UINT16 flags, /* [in] Object allocation attributes */
235 DWORD size /* [in] Number of bytes to allocate */
237 HANDLE16 owner = GetCurrentPDB16();
239 if (flags & GMEM_DDESHARE)
240 owner = GetExePtr(owner); /* Make it a module handle */
241 return GLOBAL_Alloc( flags, size, owner, FALSE, FALSE, FALSE );
245 /***********************************************************************
246 * GlobalReAlloc16 (KERNEL.16)
251 HGLOBAL16 WINAPI GlobalReAlloc16(
252 HGLOBAL16 handle, /* [in] Handle of global memory object */
253 DWORD size, /* [in] New size of block */
254 UINT16 flags /* [in] How to reallocate object */
259 GLOBALARENA *pArena, *pNewArena;
260 WORD sel = GlobalHandleToSel16( handle );
262 TRACE("%04x %ld flags=%04x\n",
263 handle, size, flags );
264 if (!handle) return 0;
266 if (!VALID_HANDLE(handle)) {
267 WARN("Invalid handle 0x%04x!\n", handle);
270 pArena = GET_ARENA_PTR( handle );
272 /* Discard the block if requested */
274 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
276 if (!(pArena->flags & GA_MOVEABLE) ||
277 !(pArena->flags & GA_DISCARDABLE) ||
278 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
279 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
282 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
283 * change the selector if we are shrinking the block.
284 * FIXME: shouldn't we keep selectors until the block is deleted?
286 SELECTOR_ReallocBlock( sel, 0, 1 );
292 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
293 if (size == 0) size = 0x20;
294 else size = (size + 0x1f) & ~0x1f;
296 /* Change the flags */
298 if (flags & GMEM_MODIFY)
300 /* Change the flags, leaving GA_DGROUP alone */
301 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
302 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
306 /* Reallocate the linear memory */
308 ptr = (void *)pArena->base;
309 oldsize = pArena->size;
310 TRACE("oldsize %08lx\n",oldsize);
311 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
313 if (((char *)ptr >= DOSMEM_MemoryBase()) &&
314 ((char *)ptr <= DOSMEM_MemoryBase() + 0x100000))
315 ptr = DOSMEM_ResizeBlock(ptr, size, NULL);
317 ptr = HeapReAlloc( GetProcessHeap(), 0, ptr, size );
320 SELECTOR_FreeBlock( sel, (oldsize + 0xffff) / 0x10000 );
321 memset( pArena, 0, sizeof(GLOBALARENA) );
325 /* Reallocate the selector(s) */
327 sel = SELECTOR_ReallocBlock( sel, ptr, size );
330 HeapFree( GetProcessHeap(), 0, ptr );
331 memset( pArena, 0, sizeof(GLOBALARENA) );
334 selcount = (size + 0xffff) / 0x10000;
336 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
338 HeapFree( GetProcessHeap(), 0, ptr );
339 SELECTOR_FreeBlock( sel, selcount );
343 /* Fill the new arena block */
345 if (pNewArena != pArena) memcpy( pNewArena, pArena, sizeof(GLOBALARENA) );
346 pNewArena->base = (DWORD)ptr;
347 pNewArena->size = GET_SEL_LIMIT(sel) + 1;
348 pNewArena->selCount = selcount;
349 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
351 if (selcount > 1) /* clear the next arena blocks */
352 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
354 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
355 memset( (char *)ptr + oldsize, 0, size - oldsize );
356 return pNewArena->handle;
360 /***********************************************************************
361 * GlobalFree16 (KERNEL.17)
366 HGLOBAL16 WINAPI GlobalFree16(
367 HGLOBAL16 handle /* [in] Handle of global memory object */
371 if (!VALID_HANDLE(handle)) {
372 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
375 ptr = (void *)GET_ARENA_PTR(handle)->base;
377 TRACE("%04x\n", handle );
378 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
379 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
384 /***********************************************************************
385 * WIN16_GlobalLock16 (KERNEL.18)
387 * This is the GlobalLock16() function used by 16-bit code.
389 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
391 WORD sel = GlobalHandleToSel16( handle );
392 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
396 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
398 if (!VALID_HANDLE(handle)) {
399 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
402 else if (!GET_ARENA_PTR(handle)->base)
405 GET_ARENA_PTR(handle)->lockCount++;
408 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
409 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
413 /***********************************************************************
414 * GlobalLock16 (KERNEL.18)
416 * This is the GlobalLock16() function used by 32-bit code.
419 * Pointer to first byte of memory block
422 LPVOID WINAPI GlobalLock16(
423 HGLOBAL16 handle /* [in] Handle of global memory object */
425 if (!handle) return 0;
426 if (!VALID_HANDLE(handle))
428 GET_ARENA_PTR(handle)->lockCount++;
429 return (LPVOID)GET_ARENA_PTR(handle)->base;
433 /***********************************************************************
434 * GlobalUnlock16 (KERNEL.19)
436 * Should the return values be cast to booleans?
439 * TRUE: Object is still locked
440 * FALSE: Object is unlocked
442 BOOL16 WINAPI GlobalUnlock16(
443 HGLOBAL16 handle /* [in] Handle of global memory object */
445 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
446 if (!VALID_HANDLE(handle)) {
447 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
450 TRACE("%04x\n", handle );
451 if (pArena->lockCount) pArena->lockCount--;
452 return pArena->lockCount;
455 /***********************************************************************
456 * GlobalChangeLockCount (KERNEL.365)
458 * This is declared as a register function as it has to preserve
459 * *all* registers, even AX/DX !
462 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
466 GlobalLock16( handle );
467 else if ( delta == -1 )
468 GlobalUnlock16( handle );
470 ERR("(%04X, %d): strange delta value\n", handle, delta );
473 /***********************************************************************
474 * GlobalSize16 (KERNEL.20)
476 * Size in bytes of object
479 DWORD WINAPI GlobalSize16(
480 HGLOBAL16 handle /* [in] Handle of global memory object */
482 TRACE("%04x\n", handle );
483 if (!handle) return 0;
484 if (!VALID_HANDLE(handle))
486 return GET_ARENA_PTR(handle)->size;
490 /***********************************************************************
491 * GlobalHandle16 (KERNEL.21)
493 * Why is GlobalHandleToSel used here with the sel as input?
499 DWORD WINAPI GlobalHandle16(
500 WORD sel /* [in] Address of global memory block */
502 TRACE("%04x\n", sel );
503 if (!VALID_HANDLE(sel)) {
504 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
507 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
510 /***********************************************************************
511 * GlobalHandleNoRIP (KERNEL.159)
513 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
516 for (i = globalArenaSize-1 ; i>=0 ; i--) {
517 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
518 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
524 /***********************************************************************
525 * GlobalFlags16 (KERNEL.22)
527 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
531 * Value specifying flags and lock count
532 * GMEM_INVALID_HANDLE: Invalid handle
534 UINT16 WINAPI GlobalFlags16(
535 HGLOBAL16 handle /* [in] Handle of global memory object */
539 TRACE("%04x\n", handle );
540 if (!VALID_HANDLE(handle)) {
541 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
544 pArena = GET_ARENA_PTR(handle);
545 return pArena->lockCount |
546 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
547 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
551 /***********************************************************************
552 * LockSegment16 (KERNEL.23)
554 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
556 TRACE("%04x\n", handle );
557 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
558 if (!VALID_HANDLE(handle)) {
559 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
562 GET_ARENA_PTR(handle)->lockCount++;
567 /***********************************************************************
568 * UnlockSegment16 (KERNEL.24)
570 void WINAPI UnlockSegment16( HGLOBAL16 handle )
572 TRACE("%04x\n", handle );
573 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
574 if (!VALID_HANDLE(handle)) {
575 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
578 GET_ARENA_PTR(handle)->lockCount--;
579 /* FIXME: this ought to return the lock count in CX (go figure...) */
583 /***********************************************************************
584 * GlobalCompact16 (KERNEL.25)
586 DWORD WINAPI GlobalCompact16( DWORD desired )
588 return GLOBAL_MAX_ALLOC_SIZE;
592 /***********************************************************************
593 * GlobalFreeAll (KERNEL.26)
595 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
600 pArena = pGlobalArena;
601 for (i = 0; i < globalArenaSize; i++, pArena++)
603 if ((pArena->size != 0) && (pArena->hOwner == owner))
604 GlobalFree16( pArena->handle );
609 /***********************************************************************
610 * GlobalWire16 (KERNEL.111)
612 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
614 return WIN16_GlobalLock16( handle );
618 /***********************************************************************
619 * GlobalUnWire16 (KERNEL.112)
621 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
623 return !GlobalUnlock16( handle );
627 /***********************************************************************
628 * SetSwapAreaSize16 (KERNEL.106)
630 LONG WINAPI SetSwapAreaSize16( WORD size )
632 FIXME("(%d) - stub!\n", size );
633 return MAKELONG( size, 0xffff );
637 /***********************************************************************
638 * GlobalLRUOldest (KERNEL.163)
640 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
642 TRACE("%04x\n", handle );
643 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
648 /***********************************************************************
649 * GlobalLRUNewest (KERNEL.164)
651 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
653 TRACE("%04x\n", handle );
654 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
659 /***********************************************************************
660 * GetFreeSpace16 (KERNEL.169)
662 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
665 GlobalMemoryStatus( &ms );
666 return ms.dwAvailVirtual;
669 /***********************************************************************
670 * GlobalDOSAlloc (KERNEL.184)
672 * Address (HW=Paragraph segment; LW=Selector)
674 DWORD WINAPI GlobalDOSAlloc16(
675 DWORD size /* [in] Number of bytes to be allocated */
678 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
682 HMODULE16 hModule = GetModuleHandle16("KERNEL");
685 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size,
687 return MAKELONG(wSelector,uParagraph);
693 /***********************************************************************
694 * GlobalDOSFree (KERNEL.185)
699 WORD WINAPI GlobalDOSFree16(
700 WORD sel /* [in] Selector */
702 DWORD block = GetSelectorBase(sel);
704 if( block && block < 0x100000 )
706 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
707 if( DOSMEM_FreeBlock( lpBlock ) )
708 GLOBAL_FreeBlock( sel );
715 /***********************************************************************
716 * GlobalPageLock (KERNEL.191)
718 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
720 TRACE("%04x\n", handle );
721 if (!VALID_HANDLE(handle)) {
722 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
725 return ++(GET_ARENA_PTR(handle)->pageLockCount);
729 /***********************************************************************
730 * GlobalPageUnlock (KERNEL.192)
732 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
734 TRACE("%04x\n", handle );
735 if (!VALID_HANDLE(handle)) {
736 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
739 return --(GET_ARENA_PTR(handle)->pageLockCount);
743 /***********************************************************************
744 * GlobalFix16 (KERNEL.197)
746 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
748 TRACE("%04x\n", handle );
749 if (!VALID_HANDLE(handle)) {
750 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
753 GET_ARENA_PTR(handle)->lockCount++;
755 return GlobalHandleToSel16(handle);
759 /***********************************************************************
760 * GlobalUnfix16 (KERNEL.198)
762 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
764 TRACE("%04x\n", handle );
765 if (!VALID_HANDLE(handle)) {
766 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
769 GET_ARENA_PTR(handle)->lockCount--;
773 /***********************************************************************
774 * FarSetOwner (KERNEL.403)
776 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
778 if (!VALID_HANDLE(handle)) {
779 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
782 GET_ARENA_PTR(handle)->hOwner = hOwner;
786 /***********************************************************************
787 * FarGetOwner (KERNEL.404)
789 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
791 if (!VALID_HANDLE(handle)) {
792 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
795 return GET_ARENA_PTR(handle)->hOwner;
799 /***********************************************************************
800 * GlobalHandleToSel (TOOLHELP.50)
802 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
804 if (!handle) return 0;
805 if (!VALID_HANDLE(handle)) {
806 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
811 WARN("Program attempted invalid selector conversion\n" );
818 /***********************************************************************
819 * GlobalFirst (TOOLHELP.51)
821 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
823 if (wFlags == GLOBAL_LRU) return FALSE;
825 return GlobalNext16( pGlobal, wFlags );
829 /***********************************************************************
830 * GlobalNext (TOOLHELP.52)
832 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
836 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
837 pArena = pGlobalArena + pGlobal->dwNext;
838 if (wFlags == GLOBAL_FREE) /* only free blocks */
841 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
842 if (pArena->size == 0) break; /* block is free */
843 if (i >= globalArenaSize) return FALSE;
847 pGlobal->dwAddress = pArena->base;
848 pGlobal->dwBlockSize = pArena->size;
849 pGlobal->hBlock = pArena->handle;
850 pGlobal->wcLock = pArena->lockCount;
851 pGlobal->wcPageLock = pArena->pageLockCount;
852 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
853 pGlobal->wHeapPresent = FALSE;
854 pGlobal->hOwner = pArena->hOwner;
855 pGlobal->wType = GT_UNKNOWN;
862 /***********************************************************************
863 * GlobalInfo (TOOLHELP.53)
865 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
870 pInfo->wcItems = globalArenaSize;
871 pInfo->wcItemsFree = 0;
872 pInfo->wcItemsLRU = 0;
873 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
874 if (pArena->size == 0) pInfo->wcItemsFree++;
879 /***********************************************************************
880 * GlobalEntryHandle (TOOLHELP.54)
882 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
884 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
886 pGlobal->dwAddress = pArena->base;
887 pGlobal->dwBlockSize = pArena->size;
888 pGlobal->hBlock = pArena->handle;
889 pGlobal->wcLock = pArena->lockCount;
890 pGlobal->wcPageLock = pArena->pageLockCount;
891 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
892 pGlobal->wHeapPresent = FALSE;
893 pGlobal->hOwner = pArena->hOwner;
894 pGlobal->wType = GT_UNKNOWN;
901 /***********************************************************************
902 * GlobalEntryModule (TOOLHELP.55)
904 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
911 /***********************************************************************
912 * MemManInfo (TOOLHELP.72)
914 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
919 * Not unsurprisingly although the documention says you
920 * _must_ provide the size in the dwSize field, this function
921 * (under Windows) always fills the structure and returns true.
923 GlobalMemoryStatus( &status );
924 info->wPageSize = VIRTUAL_GetPageSize();
925 info->dwLargestFreeBlock = status.dwAvailVirtual;
926 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
927 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
928 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
929 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
930 info->dwFreePages = info->dwMaxPagesAvailable;
931 info->dwTotalPages = info->dwTotalLinearSpace;
932 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
933 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
937 /***********************************************************************
938 * GetFreeMemInfo (KERNEL.316)
940 DWORD WINAPI GetFreeMemInfo16(void)
943 MemManInfo16( &info );
944 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
948 * Win32 Global heap functions (GlobalXXX).
949 * These functions included in Win32 for compatibility with 16 bit Windows
950 * Especially the moveable blocks and handles are oldish.
951 * But the ability to directly allocate memory with GPTR and LPTR is widely
954 * The handle stuff looks horrible, but it's implemented almost like Win95
959 #define MAGIC_GLOBAL_USED 0x5342
960 #define GLOBAL_LOCK_MAX 0xFF
961 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
962 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
963 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
964 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
965 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
967 typedef struct __GLOBAL32_INTERN
970 LPVOID Pointer WINE_PACKED;
973 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
975 /***********************************************************************
978 * Returns the appropriate heap to be used. If the object was created
979 * With GMEM_DDESHARE we allocated it on the system heap.
981 static HANDLE GLOBAL_GetHeap( HGLOBAL hmem )
985 TRACE("() hmem=%x\n", hmem);
987 /* Get the appropriate heap to be used for this object */
989 heap = GetProcessHeap();
992 PGLOBAL32_INTERN pintern;
993 pintern=HANDLE_TO_INTERN(hmem);
995 /* If it was DDESHARE it was created on the shared system heap */
996 pintern=HANDLE_TO_INTERN(hmem);
997 heap = ( pintern->Flags & (GMEM_DDESHARE >> 8) )
998 ? SystemHeap : GetProcessHeap();
1004 /***********************************************************************
1005 * GlobalAlloc (KERNEL32.315)
1010 HGLOBAL WINAPI GlobalAlloc(
1011 UINT flags, /* [in] Object allocation attributes */
1012 DWORD size /* [in] Number of bytes to allocate */
1014 PGLOBAL32_INTERN pintern;
1018 if(flags&GMEM_ZEROINIT)
1019 hpflags=HEAP_ZERO_MEMORY;
1023 TRACE("() flags=%04x\n", flags );
1025 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1027 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1028 return (HGLOBAL) palloc;
1034 /* If DDESHARE is set, create on the shared system heap */
1035 heap = (flags & GMEM_DDESHARE) ? SystemHeap : GetProcessHeap();
1037 /* HeapLock(heap); */
1039 pintern=HeapAlloc(heap, 0, sizeof(GLOBAL32_INTERN));
1040 if (!pintern) return NULL;
1043 if (!(palloc=HeapAlloc(heap, hpflags, size+sizeof(HGLOBAL)))) {
1044 HeapFree(heap, 0, pintern);
1047 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1048 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1051 pintern->Pointer=NULL;
1052 pintern->Magic=MAGIC_GLOBAL_USED;
1053 pintern->Flags=flags>>8;
1054 pintern->LockCount=0;
1056 /* HeapUnlock(heap); */
1058 return INTERN_TO_HANDLE(pintern);
1063 /***********************************************************************
1064 * GlobalLock (KERNEL32.326)
1066 * Pointer to first byte of block
1069 LPVOID WINAPI GlobalLock(
1070 HGLOBAL hmem /* [in] Handle of global memory object */
1072 PGLOBAL32_INTERN pintern;
1076 return (LPVOID) hmem;
1078 /* HeapLock(GetProcessHeap()); */
1080 pintern=HANDLE_TO_INTERN(hmem);
1081 if(pintern->Magic==MAGIC_GLOBAL_USED)
1083 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1084 pintern->LockCount++;
1085 palloc=pintern->Pointer;
1089 WARN("invalid handle\n");
1090 palloc=(LPVOID) NULL;
1092 /* HeapUnlock(GetProcessHeap()); */;
1097 /***********************************************************************
1098 * GlobalUnlock (KERNEL32.332)
1100 * TRUE: Object is still locked
1101 * FALSE: Object is unlocked
1103 BOOL WINAPI GlobalUnlock(
1104 HGLOBAL hmem /* [in] Handle of global memory object */
1106 PGLOBAL32_INTERN pintern;
1112 /* HeapLock(GetProcessHeap()); */
1113 pintern=HANDLE_TO_INTERN(hmem);
1115 if(pintern->Magic==MAGIC_GLOBAL_USED)
1117 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1118 pintern->LockCount--;
1120 locked=(pintern->LockCount==0) ? FALSE : TRUE;
1124 WARN("invalid handle\n");
1127 /* HeapUnlock(GetProcessHeap()); */
1132 /***********************************************************************
1133 * GlobalHandle (KERNEL32.325)
1134 * Returns the handle associated with the specified pointer.
1137 * Since there in only one goto, can it be removed and the return
1144 HGLOBAL WINAPI GlobalHandle(
1145 LPCVOID pmem /* [in] Pointer to global memory block */
1149 PGLOBAL32_INTERN maybe_intern;
1154 SetLastError( ERROR_INVALID_PARAMETER );
1158 /* note that if pmem is a pointer to a a block allocated by */
1159 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1161 if (ISPOINTER(pmem)) {
1162 heap = GLOBAL_GetHeap( (HGLOBAL)pmem );
1163 if (HeapValidate( heap, 0, pmem ))
1164 return (HGLOBAL)pmem; /* valid fixed block */
1165 handle = POINTER_TO_HANDLE(pmem);
1167 handle = (HGLOBAL)pmem;
1169 /* Now test handle either passed in or retrieved from pointer */
1170 heap = GLOBAL_GetHeap( handle );
1171 maybe_intern = HANDLE_TO_INTERN( handle );
1172 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1173 test = maybe_intern->Pointer;
1174 if (HeapValidate( heap, 0, ((HGLOBAL *)test)-1 ) &&
1175 /* obj(-handle) valid arena? */
1176 HeapValidate( heap, 0, maybe_intern )) /* intern valid arena? */
1177 return handle; /* valid moveable block */
1180 SetLastError( ERROR_INVALID_HANDLE );
1185 /***********************************************************************
1186 * GlobalReAlloc (KERNEL32.328)
1191 HGLOBAL WINAPI GlobalReAlloc(
1192 HGLOBAL hmem, /* [in] Handle of global memory object */
1193 DWORD size, /* [in] New size of block */
1194 UINT flags /* [in] How to reallocate object */
1198 PGLOBAL32_INTERN pintern;
1199 HANDLE heap = GLOBAL_GetHeap( hmem );
1200 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1203 /* HeapLock(heap); */
1204 if(flags & GMEM_MODIFY) /* modify flags */
1206 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1208 /* make a fixed block moveable
1209 * actually only NT is able to do this. But it's soo simple
1213 ERR("GlobalReAlloc32 with null handle!\n");
1214 SetLastError( ERROR_NOACCESS );
1217 size=HeapSize(heap, 0, (LPVOID) hmem);
1218 hnew=GlobalAlloc( flags, size);
1219 palloc=GlobalLock(hnew);
1220 memcpy(palloc, (LPVOID) hmem, size);
1224 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1226 /* change the flags to make our block "discardable" */
1227 pintern=HANDLE_TO_INTERN(hmem);
1228 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1233 SetLastError(ERROR_INVALID_PARAMETER);
1241 /* reallocate fixed memory */
1242 hnew=(HGLOBAL)HeapReAlloc(heap, heap_flags, (LPVOID) hmem, size);
1246 /* reallocate a moveable block */
1247 pintern=HANDLE_TO_INTERN(hmem);
1248 if(pintern->LockCount>1) {
1249 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1250 SetLastError(ERROR_INVALID_HANDLE);
1254 if(pintern->Pointer)
1256 if((palloc = HeapReAlloc(heap, heap_flags,
1257 (char *) pintern->Pointer-sizeof(HGLOBAL),
1258 size+sizeof(HGLOBAL))) == NULL)
1259 return 0; /* Block still valid */
1260 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1264 if((palloc=HeapAlloc(heap, heap_flags, size+sizeof(HGLOBAL)))
1267 *(HGLOBAL *)palloc=hmem;
1268 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1273 if(pintern->Pointer)
1275 HeapFree(heap, 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1276 pintern->Pointer=NULL;
1281 /* HeapUnlock(heap); */
1286 /***********************************************************************
1287 * GlobalFree (KERNEL32.322)
1292 HGLOBAL WINAPI GlobalFree(
1293 HGLOBAL hmem /* [in] Handle of global memory object */
1295 PGLOBAL32_INTERN pintern;
1296 HGLOBAL hreturned = 0;
1297 HANDLE heap = GLOBAL_GetHeap( hmem );
1299 if(ISPOINTER(hmem)) /* POINTER */
1301 if(!HeapFree(heap, 0, (LPVOID) hmem)) hmem = 0;
1305 /* HeapLock(heap); */
1306 pintern=HANDLE_TO_INTERN(hmem);
1308 if(pintern->Magic==MAGIC_GLOBAL_USED)
1311 /* WIN98 does not make this test. That is you can free a */
1312 /* block you have not unlocked. Go figure!! */
1313 /* if(pintern->LockCount!=0) */
1314 /* SetLastError(ERROR_INVALID_HANDLE); */
1316 if(pintern->Pointer)
1317 if(!HeapFree(heap, 0,
1318 (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1320 if(!HeapFree(heap, 0, pintern))
1323 /* HeapUnlock(heap); */
1329 /***********************************************************************
1330 * GlobalSize (KERNEL32.329)
1332 * Size in bytes of the global memory object
1335 DWORD WINAPI GlobalSize(
1336 HGLOBAL hmem /* [in] Handle of global memory object */
1339 PGLOBAL32_INTERN pintern;
1340 HANDLE heap = GLOBAL_GetHeap( hmem );
1344 retval=HeapSize(heap, 0, (LPVOID) hmem);
1348 /* HeapLock(heap); */
1349 pintern=HANDLE_TO_INTERN(hmem);
1351 if(pintern->Magic==MAGIC_GLOBAL_USED)
1353 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1355 retval=HeapSize(heap, 0,
1356 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1357 if (retval == 0xffffffff-4) retval = 0;
1361 WARN("invalid handle\n");
1364 /* HeapUnlock(heap); */
1366 /* HeapSize returns 0xffffffff on failure */
1367 if (retval == 0xffffffff) retval = 0;
1372 /***********************************************************************
1373 * GlobalWire (KERNEL32.333)
1375 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1377 return GlobalLock( hmem );
1381 /***********************************************************************
1382 * GlobalUnWire (KERNEL32.330)
1384 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1386 return GlobalUnlock( hmem);
1390 /***********************************************************************
1391 * GlobalFix (KERNEL32.320)
1393 VOID WINAPI GlobalFix(HGLOBAL hmem)
1399 /***********************************************************************
1400 * GlobalUnfix (KERNEL32.331)
1402 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1404 GlobalUnlock( hmem);
1408 /***********************************************************************
1409 * GlobalFlags (KERNEL32.321)
1410 * Returns information about the specified global memory object
1413 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1416 * Value specifying allocation flags and lock count
1417 * GMEM_INVALID_HANDLE: Failure
1419 UINT WINAPI GlobalFlags(
1420 HGLOBAL hmem /* [in] Handle to global memory object */
1423 PGLOBAL32_INTERN pintern;
1431 /* HeapLock(GetProcessHeap()); */
1432 pintern=HANDLE_TO_INTERN(hmem);
1433 if(pintern->Magic==MAGIC_GLOBAL_USED)
1435 retval=pintern->LockCount + (pintern->Flags<<8);
1436 if(pintern->Pointer==0)
1437 retval|= GMEM_DISCARDED;
1441 WARN("Invalid handle: %04x", hmem);
1444 /* HeapUnlock(GetProcessHeap()); */
1450 /***********************************************************************
1451 * GlobalCompact (KERNEL32.316)
1453 DWORD WINAPI GlobalCompact( DWORD minfree )
1455 return 0; /* GlobalCompact does nothing in Win32 */
1459 /***********************************************************************
1460 * GlobalMemoryStatus (KERNEL32.327)
1464 VOID WINAPI GlobalMemoryStatus(
1465 LPMEMORYSTATUS lpmem
1467 static MEMORYSTATUS cached_memstatus;
1468 static int cache_lastchecked = 0;
1473 if (time(NULL)==cache_lastchecked) {
1474 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1477 cache_lastchecked = time(NULL);
1480 f = fopen( "/proc/meminfo", "r" );
1484 int total, used, free, shared, buffers, cached;
1486 lpmem->dwLength = sizeof(MEMORYSTATUS);
1487 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1488 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1489 while (fgets( buffer, sizeof(buffer), f ))
1491 /* old style /proc/meminfo ... */
1492 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1494 lpmem->dwTotalPhys += total;
1495 lpmem->dwAvailPhys += free + buffers + cached;
1497 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1499 lpmem->dwTotalPageFile += total;
1500 lpmem->dwAvailPageFile += free;
1503 /* new style /proc/meminfo ... */
1504 if (sscanf(buffer, "MemTotal: %d", &total))
1505 lpmem->dwTotalPhys = total*1024;
1506 if (sscanf(buffer, "MemFree: %d", &free))
1507 lpmem->dwAvailPhys = free*1024;
1508 if (sscanf(buffer, "SwapTotal: %d", &total))
1509 lpmem->dwTotalPageFile = total*1024;
1510 if (sscanf(buffer, "SwapFree: %d", &free))
1511 lpmem->dwAvailPageFile = free*1024;
1512 if (sscanf(buffer, "Buffers: %d", &buffers))
1513 lpmem->dwAvailPhys += buffers*1024;
1514 if (sscanf(buffer, "Cached: %d", &cached))
1515 lpmem->dwAvailPhys += cached*1024;
1519 if (lpmem->dwTotalPhys)
1521 lpmem->dwTotalVirtual = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1522 lpmem->dwAvailVirtual = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1523 lpmem->dwMemoryLoad = (lpmem->dwTotalVirtual-lpmem->dwAvailVirtual)
1524 / (lpmem->dwTotalVirtual / 100);
1529 /* FIXME: should do something for other systems */
1530 lpmem->dwMemoryLoad = 0;
1531 lpmem->dwTotalPhys = 16*1024*1024;
1532 lpmem->dwAvailPhys = 16*1024*1024;
1533 lpmem->dwTotalPageFile = 16*1024*1024;
1534 lpmem->dwAvailPageFile = 16*1024*1024;
1535 lpmem->dwTotalVirtual = 32*1024*1024;
1536 lpmem->dwAvailVirtual = 32*1024*1024;
1538 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1541 /***********************************************************************
1542 * A20Proc16 (KERNEL.165)
1544 void WINAPI A20Proc16( WORD unused )
1546 /* this is also a NOP in Windows */
1549 /***********************************************************************
1550 * LimitEMSPages16 (KERNEL.156)
1552 DWORD WINAPI LimitEMSPages16( DWORD unused )