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,
105 /* Allocate the selector(s) */
107 sel = SELECTOR_AllocBlock( ptr, size,
108 isCode ? SEGMENT_CODE : SEGMENT_DATA,
109 is32Bit, isReadOnly );
112 selcount = (size + 0xffff) / 0x10000;
114 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
116 SELECTOR_FreeBlock( sel, selcount );
120 /* Fill the arena block */
122 pArena->base = (DWORD)ptr;
123 pArena->size = GET_SEL_LIMIT(sel) + 1;
124 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
125 pArena->hOwner = hOwner;
126 pArena->lockCount = 0;
127 pArena->pageLockCount = 0;
128 pArena->flags = flags & GA_MOVEABLE;
129 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
130 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
131 if (!isCode) pArena->flags |= GA_DGROUP;
132 pArena->selCount = selcount;
133 if (selcount > 1) /* clear the next arena blocks */
134 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
136 return pArena->handle;
140 /***********************************************************************
143 * Free a block allocated by GLOBAL_CreateBlock, without touching
144 * the associated linear memory range.
146 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
151 if (!handle) return TRUE;
152 sel = GlobalHandleToSel16( handle );
153 if (!VALID_HANDLE(sel))
155 pArena = GET_ARENA_PTR(sel);
156 SELECTOR_FreeBlock( sel, (pArena->size + 0xffff) / 0x10000 );
157 memset( pArena, 0, sizeof(GLOBALARENA) );
161 /***********************************************************************
164 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
169 if (!handle) return TRUE;
170 sel = GlobalHandleToSel16( handle );
171 if (!VALID_HANDLE(sel))
173 pArena = GET_ARENA_PTR(sel);
174 if (pArena->selCount != 1)
177 pArena->base = (DWORD)ptr;
180 SELECTOR_MoveBlock( sel, ptr );
181 SetSelectorLimit16( sel, size-1 );
186 /***********************************************************************
189 * Implementation of GlobalAlloc16()
191 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner,
192 BOOL16 isCode, BOOL16 is32Bit, BOOL16 isReadOnly )
198 TRACE("%ld flags=%04x\n", size, flags );
200 /* If size is 0, create a discarded block */
202 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, isCode,
203 is32Bit, isReadOnly, NULL );
207 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
208 size = (size + 0x1f) & ~0x1f;
210 /* Allocate the linear memory */
211 ptr = HeapAlloc( GetProcessHeap(), 0, size );
212 /* FIXME: free discardable blocks and try again? */
215 /* Allocate the selector(s) */
217 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner,
218 isCode, is32Bit, isReadOnly, &shmdata);
221 HeapFree( GetProcessHeap(), 0, ptr );
225 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
229 /***********************************************************************
230 * GlobalAlloc16 (KERNEL.15)
235 HGLOBAL16 WINAPI GlobalAlloc16(
236 UINT16 flags, /* [in] Object allocation attributes */
237 DWORD size /* [in] Number of bytes to allocate */
239 HANDLE16 owner = GetCurrentPDB16();
241 if (flags & GMEM_DDESHARE)
242 owner = GetExePtr(owner); /* Make it a module handle */
243 return GLOBAL_Alloc( flags, size, owner, FALSE, FALSE, FALSE );
247 /***********************************************************************
248 * GlobalReAlloc16 (KERNEL.16)
253 HGLOBAL16 WINAPI GlobalReAlloc16(
254 HGLOBAL16 handle, /* [in] Handle of global memory object */
255 DWORD size, /* [in] New size of block */
256 UINT16 flags /* [in] How to reallocate object */
261 GLOBALARENA *pArena, *pNewArena;
262 WORD sel = GlobalHandleToSel16( handle );
264 TRACE("%04x %ld flags=%04x\n",
265 handle, size, flags );
266 if (!handle) return 0;
268 if (!VALID_HANDLE(handle)) {
269 WARN("Invalid handle 0x%04x!\n", handle);
272 pArena = GET_ARENA_PTR( handle );
274 /* Discard the block if requested */
276 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
278 if (!(pArena->flags & GA_MOVEABLE) ||
279 !(pArena->flags & GA_DISCARDABLE) ||
280 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
281 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
284 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
285 * change the selector if we are shrinking the block.
286 * FIXME: shouldn't we keep selectors until the block is deleted?
288 SELECTOR_ReallocBlock( sel, 0, 1 );
294 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
295 if (size == 0) size = 0x20;
296 else size = (size + 0x1f) & ~0x1f;
298 /* Change the flags */
300 if (flags & GMEM_MODIFY)
302 /* Change the flags, leaving GA_DGROUP alone */
303 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
304 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
308 /* Reallocate the linear memory */
310 ptr = (void *)pArena->base;
311 oldsize = pArena->size;
312 TRACE("oldsize %08lx\n",oldsize);
313 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
315 if (((char *)ptr >= DOSMEM_MemoryBase(0)) &&
316 ((char *)ptr <= DOSMEM_MemoryBase(0) + 0x100000))
317 ptr = DOSMEM_ResizeBlock(0, ptr, size, NULL);
319 ptr = HeapReAlloc( GetProcessHeap(), 0, ptr, size );
322 SELECTOR_FreeBlock( sel, (oldsize + 0xffff) / 0x10000 );
323 memset( pArena, 0, sizeof(GLOBALARENA) );
327 /* Reallocate the selector(s) */
329 sel = SELECTOR_ReallocBlock( sel, ptr, size );
332 HeapFree( GetProcessHeap(), 0, ptr );
333 memset( pArena, 0, sizeof(GLOBALARENA) );
336 selcount = (size + 0xffff) / 0x10000;
338 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
340 HeapFree( GetProcessHeap(), 0, ptr );
341 SELECTOR_FreeBlock( sel, selcount );
345 /* Fill the new arena block */
347 if (pNewArena != pArena) memcpy( pNewArena, pArena, sizeof(GLOBALARENA) );
348 pNewArena->base = (DWORD)ptr;
349 pNewArena->size = GET_SEL_LIMIT(sel) + 1;
350 pNewArena->selCount = selcount;
351 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
353 if (selcount > 1) /* clear the next arena blocks */
354 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
356 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
357 memset( (char *)ptr + oldsize, 0, size - oldsize );
358 return pNewArena->handle;
362 /***********************************************************************
363 * GlobalFree16 (KERNEL.17)
368 HGLOBAL16 WINAPI GlobalFree16(
369 HGLOBAL16 handle /* [in] Handle of global memory object */
373 if (!VALID_HANDLE(handle)) {
374 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
377 ptr = (void *)GET_ARENA_PTR(handle)->base;
379 TRACE("%04x\n", handle );
380 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
381 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
386 /***********************************************************************
387 * WIN16_GlobalLock16 (KERNEL.18)
389 * This is the GlobalLock16() function used by 16-bit code.
391 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
393 WORD sel = GlobalHandleToSel16( handle );
394 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
398 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
400 if (!VALID_HANDLE(handle)) {
401 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
404 else if (!GET_ARENA_PTR(handle)->base)
407 GET_ARENA_PTR(handle)->lockCount++;
410 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
411 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
415 /***********************************************************************
416 * GlobalLock16 (KERNEL.18)
418 * This is the GlobalLock16() function used by 32-bit code.
421 * Pointer to first byte of memory block
424 LPVOID WINAPI GlobalLock16(
425 HGLOBAL16 handle /* [in] Handle of global memory object */
427 if (!handle) return 0;
428 if (!VALID_HANDLE(handle))
430 GET_ARENA_PTR(handle)->lockCount++;
431 return (LPVOID)GET_ARENA_PTR(handle)->base;
435 /***********************************************************************
436 * GlobalUnlock16 (KERNEL.19)
438 * Should the return values be cast to booleans?
441 * TRUE: Object is still locked
442 * FALSE: Object is unlocked
444 BOOL16 WINAPI GlobalUnlock16(
445 HGLOBAL16 handle /* [in] Handle of global memory object */
447 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
448 if (!VALID_HANDLE(handle)) {
449 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
452 TRACE("%04x\n", handle );
453 if (pArena->lockCount) pArena->lockCount--;
454 return pArena->lockCount;
457 /***********************************************************************
458 * GlobalChangeLockCount (KERNEL.365)
460 * This is declared as a register function as it has to preserve
461 * *all* registers, even AX/DX !
464 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
468 GlobalLock16( handle );
469 else if ( delta == -1 )
470 GlobalUnlock16( handle );
472 ERR("(%04X, %d): strange delta value\n", handle, delta );
475 /***********************************************************************
476 * GlobalSize16 (KERNEL.20)
478 * Size in bytes of object
481 DWORD WINAPI GlobalSize16(
482 HGLOBAL16 handle /* [in] Handle of global memory object */
484 TRACE("%04x\n", handle );
485 if (!handle) return 0;
486 if (!VALID_HANDLE(handle))
488 return GET_ARENA_PTR(handle)->size;
492 /***********************************************************************
493 * GlobalHandle16 (KERNEL.21)
495 * Why is GlobalHandleToSel used here with the sel as input?
501 DWORD WINAPI GlobalHandle16(
502 WORD sel /* [in] Address of global memory block */
504 TRACE("%04x\n", sel );
505 if (!VALID_HANDLE(sel)) {
506 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
509 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
512 /***********************************************************************
513 * GlobalHandleNoRIP (KERNEL.159)
515 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
518 for (i = globalArenaSize-1 ; i>=0 ; i--) {
519 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
520 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
526 /***********************************************************************
527 * GlobalFlags16 (KERNEL.22)
529 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
533 * Value specifying flags and lock count
534 * GMEM_INVALID_HANDLE: Invalid handle
536 UINT16 WINAPI GlobalFlags16(
537 HGLOBAL16 handle /* [in] Handle of global memory object */
541 TRACE("%04x\n", handle );
542 if (!VALID_HANDLE(handle)) {
543 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
546 pArena = GET_ARENA_PTR(handle);
547 return pArena->lockCount |
548 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
549 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
553 /***********************************************************************
554 * LockSegment16 (KERNEL.23)
556 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
558 TRACE("%04x\n", handle );
559 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
560 if (!VALID_HANDLE(handle)) {
561 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
564 GET_ARENA_PTR(handle)->lockCount++;
569 /***********************************************************************
570 * UnlockSegment16 (KERNEL.24)
572 void WINAPI UnlockSegment16( HGLOBAL16 handle )
574 TRACE("%04x\n", handle );
575 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
576 if (!VALID_HANDLE(handle)) {
577 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
580 GET_ARENA_PTR(handle)->lockCount--;
581 /* FIXME: this ought to return the lock count in CX (go figure...) */
585 /***********************************************************************
586 * GlobalCompact16 (KERNEL.25)
588 DWORD WINAPI GlobalCompact16( DWORD desired )
590 return GLOBAL_MAX_ALLOC_SIZE;
594 /***********************************************************************
595 * GlobalFreeAll (KERNEL.26)
597 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
602 pArena = pGlobalArena;
603 for (i = 0; i < globalArenaSize; i++, pArena++)
605 if ((pArena->size != 0) && (pArena->hOwner == owner))
606 GlobalFree16( pArena->handle );
611 /***********************************************************************
612 * GlobalWire16 (KERNEL.111)
614 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
616 return WIN16_GlobalLock16( handle );
620 /***********************************************************************
621 * GlobalUnWire16 (KERNEL.112)
623 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
625 return !GlobalUnlock16( handle );
629 /***********************************************************************
630 * SetSwapAreaSize16 (KERNEL.106)
632 LONG WINAPI SetSwapAreaSize16( WORD size )
634 FIXME("(%d) - stub!\n", size );
635 return MAKELONG( size, 0xffff );
639 /***********************************************************************
640 * GlobalLRUOldest (KERNEL.163)
642 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
644 TRACE("%04x\n", handle );
645 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
650 /***********************************************************************
651 * GlobalLRUNewest (KERNEL.164)
653 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
655 TRACE("%04x\n", handle );
656 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
661 /***********************************************************************
662 * GetFreeSpace16 (KERNEL.169)
664 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
667 GlobalMemoryStatus( &ms );
668 return ms.dwAvailVirtual;
671 /***********************************************************************
672 * GlobalDOSAlloc (KERNEL.184)
674 * Address (HW=Paragraph segment; LW=Selector)
676 DWORD WINAPI GlobalDOSAlloc16(
677 DWORD size /* [in] Number of bytes to be allocated */
680 LPVOID lpBlock = DOSMEM_GetBlock( 0, size, &uParagraph );
684 HMODULE16 hModule = GetModuleHandle16("KERNEL");
687 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size,
688 hModule, 0, 0, 0, NULL );
689 return MAKELONG(wSelector,uParagraph);
695 /***********************************************************************
696 * GlobalDOSFree (KERNEL.185)
701 WORD WINAPI GlobalDOSFree16(
702 WORD sel /* [in] Selector */
704 DWORD block = GetSelectorBase(sel);
706 if( block && block < 0x100000 )
708 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
709 if( DOSMEM_FreeBlock( 0, lpBlock ) )
710 GLOBAL_FreeBlock( sel );
717 /***********************************************************************
718 * GlobalPageLock (KERNEL.191)
720 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
722 TRACE("%04x\n", handle );
723 if (!VALID_HANDLE(handle)) {
724 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
727 return ++(GET_ARENA_PTR(handle)->pageLockCount);
731 /***********************************************************************
732 * GlobalPageUnlock (KERNEL.192)
734 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
736 TRACE("%04x\n", handle );
737 if (!VALID_HANDLE(handle)) {
738 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
741 return --(GET_ARENA_PTR(handle)->pageLockCount);
745 /***********************************************************************
746 * GlobalFix16 (KERNEL.197)
748 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
750 TRACE("%04x\n", handle );
751 if (!VALID_HANDLE(handle)) {
752 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
755 GET_ARENA_PTR(handle)->lockCount++;
757 return GlobalHandleToSel16(handle);
761 /***********************************************************************
762 * GlobalUnfix16 (KERNEL.198)
764 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
766 TRACE("%04x\n", handle );
767 if (!VALID_HANDLE(handle)) {
768 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
771 GET_ARENA_PTR(handle)->lockCount--;
775 /***********************************************************************
776 * FarSetOwner (KERNEL.403)
778 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
780 if (!VALID_HANDLE(handle)) {
781 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
784 GET_ARENA_PTR(handle)->hOwner = hOwner;
788 /***********************************************************************
789 * FarGetOwner (KERNEL.404)
791 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
793 if (!VALID_HANDLE(handle)) {
794 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
797 return GET_ARENA_PTR(handle)->hOwner;
801 /***********************************************************************
802 * GlobalHandleToSel (TOOLHELP.50)
804 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
806 if (!handle) return 0;
807 if (!VALID_HANDLE(handle)) {
808 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
813 WARN("Program attempted invalid selector conversion\n" );
820 /***********************************************************************
821 * GlobalFirst (TOOLHELP.51)
823 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
825 if (wFlags == GLOBAL_LRU) return FALSE;
827 return GlobalNext16( pGlobal, wFlags );
831 /***********************************************************************
832 * GlobalNext (TOOLHELP.52)
834 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
838 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
839 pArena = pGlobalArena + pGlobal->dwNext;
840 if (wFlags == GLOBAL_FREE) /* only free blocks */
843 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
844 if (pArena->size == 0) break; /* block is free */
845 if (i >= globalArenaSize) return FALSE;
849 pGlobal->dwAddress = pArena->base;
850 pGlobal->dwBlockSize = pArena->size;
851 pGlobal->hBlock = pArena->handle;
852 pGlobal->wcLock = pArena->lockCount;
853 pGlobal->wcPageLock = pArena->pageLockCount;
854 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
855 pGlobal->wHeapPresent = FALSE;
856 pGlobal->hOwner = pArena->hOwner;
857 pGlobal->wType = GT_UNKNOWN;
864 /***********************************************************************
865 * GlobalInfo (TOOLHELP.53)
867 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
872 pInfo->wcItems = globalArenaSize;
873 pInfo->wcItemsFree = 0;
874 pInfo->wcItemsLRU = 0;
875 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
876 if (pArena->size == 0) pInfo->wcItemsFree++;
881 /***********************************************************************
882 * GlobalEntryHandle (TOOLHELP.54)
884 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
886 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
888 pGlobal->dwAddress = pArena->base;
889 pGlobal->dwBlockSize = pArena->size;
890 pGlobal->hBlock = pArena->handle;
891 pGlobal->wcLock = pArena->lockCount;
892 pGlobal->wcPageLock = pArena->pageLockCount;
893 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
894 pGlobal->wHeapPresent = FALSE;
895 pGlobal->hOwner = pArena->hOwner;
896 pGlobal->wType = GT_UNKNOWN;
903 /***********************************************************************
904 * GlobalEntryModule (TOOLHELP.55)
906 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
913 /***********************************************************************
914 * MemManInfo (TOOLHELP.72)
916 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
921 * Not unsurprisingly although the documention says you
922 * _must_ provide the size in the dwSize field, this function
923 * (under Windows) always fills the structure and returns true.
925 GlobalMemoryStatus( &status );
926 info->wPageSize = VIRTUAL_GetPageSize();
927 info->dwLargestFreeBlock = status.dwAvailVirtual;
928 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
929 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
930 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
931 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
932 info->dwFreePages = info->dwMaxPagesAvailable;
933 info->dwTotalPages = info->dwTotalLinearSpace;
934 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
935 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
939 /***********************************************************************
940 * GetFreeMemInfo (KERNEL.316)
942 DWORD WINAPI GetFreeMemInfo16(void)
945 MemManInfo16( &info );
946 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
950 * Win32 Global heap functions (GlobalXXX).
951 * These functions included in Win32 for compatibility with 16 bit Windows
952 * Especially the moveable blocks and handles are oldish.
953 * But the ability to directly allocate memory with GPTR and LPTR is widely
956 * The handle stuff looks horrible, but it's implemented almost like Win95
961 #define MAGIC_GLOBAL_USED 0x5342
962 #define GLOBAL_LOCK_MAX 0xFF
963 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
964 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
965 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
966 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
967 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
969 typedef struct __GLOBAL32_INTERN
972 LPVOID Pointer WINE_PACKED;
975 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
977 /***********************************************************************
980 * Returns the appropriate heap to be used. If the object was created
981 * With GMEM_DDESHARE we allocated it on the system heap.
983 static HANDLE GLOBAL_GetHeap( HGLOBAL hmem )
987 TRACE("() hmem=%x\n", hmem);
989 /* Get the appropriate heap to be used for this object */
991 heap = GetProcessHeap();
994 PGLOBAL32_INTERN pintern;
995 pintern=HANDLE_TO_INTERN(hmem);
997 /* If it was DDESHARE it was created on the shared system heap */
998 pintern=HANDLE_TO_INTERN(hmem);
999 heap = ( pintern->Flags & (GMEM_DDESHARE >> 8) )
1000 ? SystemHeap : GetProcessHeap();
1006 /***********************************************************************
1007 * GlobalAlloc (KERNEL32.315)
1012 HGLOBAL WINAPI GlobalAlloc(
1013 UINT flags, /* [in] Object allocation attributes */
1014 DWORD size /* [in] Number of bytes to allocate */
1016 PGLOBAL32_INTERN pintern;
1020 if(flags&GMEM_ZEROINIT)
1021 hpflags=HEAP_ZERO_MEMORY;
1025 TRACE("() flags=%04x\n", flags );
1027 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1029 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1030 return (HGLOBAL) palloc;
1036 /* If DDESHARE is set, create on the shared system heap */
1037 heap = (flags & GMEM_DDESHARE) ? SystemHeap : GetProcessHeap();
1039 /* HeapLock(heap); */
1041 pintern=HeapAlloc(heap, 0, sizeof(GLOBAL32_INTERN));
1044 palloc=HeapAlloc(heap, hpflags, size+sizeof(HGLOBAL));
1045 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1046 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1049 pintern->Pointer=NULL;
1050 pintern->Magic=MAGIC_GLOBAL_USED;
1051 pintern->Flags=flags>>8;
1052 pintern->LockCount=0;
1054 /* HeapUnlock(heap); */
1056 return INTERN_TO_HANDLE(pintern);
1061 /***********************************************************************
1062 * GlobalLock (KERNEL32.326)
1064 * Pointer to first byte of block
1067 LPVOID WINAPI GlobalLock(
1068 HGLOBAL hmem /* [in] Handle of global memory object */
1070 PGLOBAL32_INTERN pintern;
1074 return (LPVOID) hmem;
1076 /* HeapLock(GetProcessHeap()); */
1078 pintern=HANDLE_TO_INTERN(hmem);
1079 if(pintern->Magic==MAGIC_GLOBAL_USED)
1081 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1082 pintern->LockCount++;
1083 palloc=pintern->Pointer;
1087 WARN("invalid handle\n");
1088 palloc=(LPVOID) NULL;
1090 /* HeapUnlock(GetProcessHeap()); */;
1095 /***********************************************************************
1096 * GlobalUnlock (KERNEL32.332)
1098 * TRUE: Object is still locked
1099 * FALSE: Object is unlocked
1101 BOOL WINAPI GlobalUnlock(
1102 HGLOBAL hmem /* [in] Handle of global memory object */
1104 PGLOBAL32_INTERN pintern;
1110 /* HeapLock(GetProcessHeap()); */
1111 pintern=HANDLE_TO_INTERN(hmem);
1113 if(pintern->Magic==MAGIC_GLOBAL_USED)
1115 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1116 pintern->LockCount--;
1118 locked=(pintern->LockCount==0) ? FALSE : TRUE;
1122 WARN("invalid handle\n");
1125 /* HeapUnlock(GetProcessHeap()); */
1130 /***********************************************************************
1131 * GlobalHandle (KERNEL32.325)
1132 * Returns the handle associated with the specified pointer.
1135 * Since there in only one goto, can it be removed and the return
1142 HGLOBAL WINAPI GlobalHandle(
1143 LPCVOID pmem /* [in] Pointer to global memory block */
1147 PGLOBAL32_INTERN maybe_intern;
1152 SetLastError( ERROR_INVALID_PARAMETER );
1156 /* note that if pmem is a pointer to a a block allocated by */
1157 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1159 if (ISPOINTER(pmem)) {
1160 heap = GLOBAL_GetHeap( (HGLOBAL)pmem );
1161 if (HeapValidate( heap, 0, pmem ))
1162 return (HGLOBAL)pmem; /* valid fixed block */
1163 handle = POINTER_TO_HANDLE(pmem);
1165 handle = (HGLOBAL)pmem;
1167 /* Now test handle either passed in or retrieved from pointer */
1168 heap = GLOBAL_GetHeap( handle );
1169 maybe_intern = HANDLE_TO_INTERN( handle );
1170 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1171 test = maybe_intern->Pointer;
1172 if (HeapValidate( heap, 0, ((HGLOBAL *)test)-1 ) &&
1173 /* obj(-handle) valid arena? */
1174 HeapValidate( heap, 0, maybe_intern )) /* intern valid arena? */
1175 return handle; /* valid moveable block */
1178 SetLastError( ERROR_INVALID_HANDLE );
1183 /***********************************************************************
1184 * GlobalReAlloc (KERNEL32.328)
1189 HGLOBAL WINAPI GlobalReAlloc(
1190 HGLOBAL hmem, /* [in] Handle of global memory object */
1191 DWORD size, /* [in] New size of block */
1192 UINT flags /* [in] How to reallocate object */
1196 PGLOBAL32_INTERN pintern;
1197 HANDLE heap = GLOBAL_GetHeap( hmem );
1198 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1201 /* HeapLock(heap); */
1202 if(flags & GMEM_MODIFY) /* modify flags */
1204 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1206 /* make a fixed block moveable
1207 * actually only NT is able to do this. But it's soo simple
1211 ERR("GlobalReAlloc32 with null handle!\n");
1212 SetLastError( ERROR_NOACCESS );
1215 size=HeapSize(heap, 0, (LPVOID) hmem);
1216 hnew=GlobalAlloc( flags, size);
1217 palloc=GlobalLock(hnew);
1218 memcpy(palloc, (LPVOID) hmem, size);
1222 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1224 /* change the flags to make our block "discardable" */
1225 pintern=HANDLE_TO_INTERN(hmem);
1226 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1231 SetLastError(ERROR_INVALID_PARAMETER);
1239 /* reallocate fixed memory */
1240 hnew=(HGLOBAL)HeapReAlloc(heap, heap_flags, (LPVOID) hmem, size);
1244 /* reallocate a moveable block */
1245 pintern=HANDLE_TO_INTERN(hmem);
1246 if(pintern->LockCount>1) {
1247 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1248 SetLastError(ERROR_INVALID_HANDLE);
1252 if(pintern->Pointer)
1254 palloc=HeapReAlloc(heap, heap_flags,
1255 (char *) pintern->Pointer-sizeof(HGLOBAL),
1256 size+sizeof(HGLOBAL) );
1257 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1261 palloc=HeapAlloc(heap, heap_flags, size+sizeof(HGLOBAL));
1262 *(HGLOBAL *)palloc=hmem;
1263 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1268 if(pintern->Pointer)
1270 HeapFree(heap, 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1271 pintern->Pointer=NULL;
1276 /* HeapUnlock(heap); */
1281 /***********************************************************************
1282 * GlobalFree (KERNEL32.322)
1287 HGLOBAL WINAPI GlobalFree(
1288 HGLOBAL hmem /* [in] Handle of global memory object */
1290 PGLOBAL32_INTERN pintern;
1291 HGLOBAL hreturned = 0;
1292 HANDLE heap = GLOBAL_GetHeap( hmem );
1294 if(ISPOINTER(hmem)) /* POINTER */
1296 if(!HeapFree(heap, 0, (LPVOID) hmem)) hmem = 0;
1300 /* HeapLock(heap); */
1301 pintern=HANDLE_TO_INTERN(hmem);
1303 if(pintern->Magic==MAGIC_GLOBAL_USED)
1306 /* WIN98 does not make this test. That is you can free a */
1307 /* block you have not unlocked. Go figure!! */
1308 /* if(pintern->LockCount!=0) */
1309 /* SetLastError(ERROR_INVALID_HANDLE); */
1311 if(pintern->Pointer)
1312 if(!HeapFree(heap, 0,
1313 (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1315 if(!HeapFree(heap, 0, pintern))
1318 /* HeapUnlock(heap); */
1324 /***********************************************************************
1325 * GlobalSize (KERNEL32.329)
1327 * Size in bytes of the global memory object
1330 DWORD WINAPI GlobalSize(
1331 HGLOBAL hmem /* [in] Handle of global memory object */
1334 PGLOBAL32_INTERN pintern;
1335 HANDLE heap = GLOBAL_GetHeap( hmem );
1339 retval=HeapSize(heap, 0, (LPVOID) hmem);
1343 /* HeapLock(heap); */
1344 pintern=HANDLE_TO_INTERN(hmem);
1346 if(pintern->Magic==MAGIC_GLOBAL_USED)
1348 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1350 retval=HeapSize(heap, 0,
1351 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1352 if (retval == 0xffffffff-4) retval = 0;
1356 WARN("invalid handle\n");
1359 /* HeapUnlock(heap); */
1361 /* HeapSize returns 0xffffffff on failure */
1362 if (retval == 0xffffffff) retval = 0;
1367 /***********************************************************************
1368 * GlobalWire (KERNEL32.333)
1370 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1372 return GlobalLock( hmem );
1376 /***********************************************************************
1377 * GlobalUnWire (KERNEL32.330)
1379 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1381 return GlobalUnlock( hmem);
1385 /***********************************************************************
1386 * GlobalFix (KERNEL32.320)
1388 VOID WINAPI GlobalFix(HGLOBAL hmem)
1394 /***********************************************************************
1395 * GlobalUnfix (KERNEL32.331)
1397 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1399 GlobalUnlock( hmem);
1403 /***********************************************************************
1404 * GlobalFlags (KERNEL32.321)
1405 * Returns information about the specified global memory object
1408 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1411 * Value specifying allocation flags and lock count
1412 * GMEM_INVALID_HANDLE: Failure
1414 UINT WINAPI GlobalFlags(
1415 HGLOBAL hmem /* [in] Handle to global memory object */
1418 PGLOBAL32_INTERN pintern;
1426 /* HeapLock(GetProcessHeap()); */
1427 pintern=HANDLE_TO_INTERN(hmem);
1428 if(pintern->Magic==MAGIC_GLOBAL_USED)
1430 retval=pintern->LockCount + (pintern->Flags<<8);
1431 if(pintern->Pointer==0)
1432 retval|= GMEM_DISCARDED;
1436 WARN("Invalid handle: %04x", hmem);
1439 /* HeapUnlock(GetProcessHeap()); */
1445 /***********************************************************************
1446 * GlobalCompact (KERNEL32.316)
1448 DWORD WINAPI GlobalCompact( DWORD minfree )
1450 return 0; /* GlobalCompact does nothing in Win32 */
1454 /***********************************************************************
1455 * GlobalMemoryStatus (KERNEL32.327)
1459 VOID WINAPI GlobalMemoryStatus(
1460 LPMEMORYSTATUS lpmem
1462 static MEMORYSTATUS cached_memstatus;
1463 static int cache_lastchecked = 0;
1468 if (time(NULL)==cache_lastchecked) {
1469 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1472 cache_lastchecked = time(NULL);
1475 f = fopen( "/proc/meminfo", "r" );
1479 int total, used, free, shared, buffers, cached;
1481 lpmem->dwLength = sizeof(MEMORYSTATUS);
1482 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1483 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1484 while (fgets( buffer, sizeof(buffer), f ))
1486 /* old style /proc/meminfo ... */
1487 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1489 lpmem->dwTotalPhys += total;
1490 lpmem->dwAvailPhys += free + buffers + cached;
1492 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1494 lpmem->dwTotalPageFile += total;
1495 lpmem->dwAvailPageFile += free;
1498 /* new style /proc/meminfo ... */
1499 if (sscanf(buffer, "MemTotal: %d", &total))
1500 lpmem->dwTotalPhys = total*1024;
1501 if (sscanf(buffer, "MemFree: %d", &free))
1502 lpmem->dwAvailPhys = free*1024;
1503 if (sscanf(buffer, "SwapTotal: %d", &total))
1504 lpmem->dwTotalPageFile = total*1024;
1505 if (sscanf(buffer, "SwapFree: %d", &free))
1506 lpmem->dwAvailPageFile = free*1024;
1507 if (sscanf(buffer, "Buffers: %d", &buffers))
1508 lpmem->dwAvailPhys += buffers*1024;
1509 if (sscanf(buffer, "Cached: %d", &cached))
1510 lpmem->dwAvailPhys += cached*1024;
1514 if (lpmem->dwTotalPhys)
1516 lpmem->dwTotalVirtual = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1517 lpmem->dwAvailVirtual = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1518 lpmem->dwMemoryLoad = (lpmem->dwTotalVirtual-lpmem->dwAvailVirtual)
1519 / (lpmem->dwTotalVirtual / 100);
1524 /* FIXME: should do something for other systems */
1525 lpmem->dwMemoryLoad = 0;
1526 lpmem->dwTotalPhys = 16*1024*1024;
1527 lpmem->dwAvailPhys = 16*1024*1024;
1528 lpmem->dwTotalPageFile = 16*1024*1024;
1529 lpmem->dwAvailPageFile = 16*1024*1024;
1530 lpmem->dwTotalVirtual = 32*1024*1024;
1531 lpmem->dwAvailVirtual = 32*1024*1024;
1533 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1536 /***********************************************************************
1537 * A20Proc16 (KERNEL.165)
1539 void WINAPI A20Proc16( WORD unused )
1541 /* this is also a NOP in Windows */
1544 /***********************************************************************
1545 * LimitEMSPages16 (KERNEL.156)
1547 DWORD WINAPI LimitEMSPages16( DWORD unused )