2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
13 #include "wine/winbase16.h"
17 #include "selectors.h"
19 #include "stackframe.h"
21 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(global)
26 /* Global arena block */
29 DWORD base; /* Base address (0 if discarded) */
30 DWORD size; /* Size in bytes (0 indicates a free block) */
31 HGLOBAL16 handle; /* Handle for this block */
32 HGLOBAL16 hOwner; /* Owner of this block */
33 BYTE lockCount; /* Count of GlobalFix() calls */
34 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
35 BYTE flags; /* Allocation flags */
36 BYTE selCount; /* Number of selectors allocated for this block */
39 /* Flags definitions */
40 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
41 #define GA_DGROUP 0x04
42 #define GA_DISCARDABLE 0x08
43 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
46 static GLOBALARENA *pGlobalArena = NULL;
47 static int globalArenaSize = 0;
49 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
51 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
52 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
54 /***********************************************************************
57 * Return the arena for a given selector, growing the arena array if needed.
59 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
61 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
63 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
64 GLOBALARENA *pNewArena = realloc( pGlobalArena,
65 newsize * sizeof(GLOBALARENA) );
66 if (!pNewArena) return 0;
67 pGlobalArena = pNewArena;
68 memset( pGlobalArena + globalArenaSize, 0,
69 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
70 globalArenaSize = newsize;
72 return pGlobalArena + (sel >> __AHSHIFT);
75 void debug_handles(void)
79 for (i = globalArenaSize-1 ; i>=0 ; i--) {
80 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
82 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
90 /***********************************************************************
93 * Create a global heap block for a fixed range of linear memory.
95 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
96 HGLOBAL16 hOwner, BOOL16 isCode,
97 BOOL16 is32Bit, BOOL16 isReadOnly,
103 /* Allocate the selector(s) */
105 sel = SELECTOR_AllocBlock( ptr, size,
106 isCode ? SEGMENT_CODE : SEGMENT_DATA,
107 is32Bit, isReadOnly );
110 selcount = (size + 0xffff) / 0x10000;
112 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
114 SELECTOR_FreeBlock( sel, selcount );
118 /* Fill the arena block */
120 pArena->base = (DWORD)ptr;
121 pArena->size = GET_SEL_LIMIT(sel) + 1;
122 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
123 pArena->hOwner = hOwner;
124 pArena->lockCount = 0;
125 pArena->pageLockCount = 0;
126 pArena->flags = flags & GA_MOVEABLE;
127 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
128 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
129 if (!isCode) pArena->flags |= GA_DGROUP;
130 pArena->selCount = selcount;
131 if (selcount > 1) /* clear the next arena blocks */
132 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
134 return pArena->handle;
138 /***********************************************************************
141 * Free a block allocated by GLOBAL_CreateBlock, without touching
142 * the associated linear memory range.
144 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
149 if (!handle) return TRUE;
150 sel = GlobalHandleToSel16( handle );
151 if (!VALID_HANDLE(sel))
153 pArena = GET_ARENA_PTR(sel);
154 SELECTOR_FreeBlock( sel, (pArena->size + 0xffff) / 0x10000 );
155 memset( pArena, 0, sizeof(GLOBALARENA) );
159 /***********************************************************************
162 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
167 if (!handle) return TRUE;
168 sel = GlobalHandleToSel16( handle );
169 if (!VALID_HANDLE(sel))
171 pArena = GET_ARENA_PTR(sel);
172 if (pArena->selCount != 1)
175 pArena->base = (DWORD)ptr;
178 SELECTOR_MoveBlock( sel, ptr );
179 SetSelectorLimit16( sel, size-1 );
184 /***********************************************************************
187 * Implementation of GlobalAlloc16()
189 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner,
190 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, NULL );
205 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
206 size = (size + 0x1f) & ~0x1f;
208 /* Allocate the linear memory */
209 ptr = HeapAlloc( SystemHeap, 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, &shmdata);
219 HeapFree( SystemHeap, 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( SystemHeap, 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(0)) &&
314 ((char *)ptr <= DOSMEM_MemoryBase(0) + 0x100000))
315 ptr = DOSMEM_ResizeBlock(0, ptr, size, NULL);
317 ptr = HeapReAlloc( SystemHeap, 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( SystemHeap, 0, ptr );
331 memset( pArena, 0, sizeof(GLOBALARENA) );
334 selcount = (size + 0xffff) / 0x10000;
336 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
338 HeapFree( SystemHeap, 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( SystemHeap, 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( 0, size, &uParagraph );
682 HMODULE16 hModule = GetModuleHandle16("KERNEL");
685 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size,
686 hModule, 0, 0, 0, NULL );
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( 0, 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 * GlobalAlloc32 (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));
1042 palloc=HeapAlloc(heap, hpflags, size+sizeof(HGLOBAL));
1043 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1044 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1047 pintern->Pointer=NULL;
1048 pintern->Magic=MAGIC_GLOBAL_USED;
1049 pintern->Flags=flags>>8;
1050 pintern->LockCount=0;
1052 /* HeapUnlock(heap); */
1054 return INTERN_TO_HANDLE(pintern);
1059 /***********************************************************************
1060 * GlobalLock32 (KERNEL32.326)
1062 * Pointer to first byte of block
1065 LPVOID WINAPI GlobalLock(
1066 HGLOBAL hmem /* [in] Handle of global memory object */
1068 PGLOBAL32_INTERN pintern;
1072 return (LPVOID) hmem;
1074 /* HeapLock(GetProcessHeap()); */
1076 pintern=HANDLE_TO_INTERN(hmem);
1077 if(pintern->Magic==MAGIC_GLOBAL_USED)
1079 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1080 pintern->LockCount++;
1081 palloc=pintern->Pointer;
1085 WARN("invalid handle\n");
1086 palloc=(LPVOID) NULL;
1088 /* HeapUnlock(GetProcessHeap()); */;
1093 /***********************************************************************
1094 * GlobalUnlock32 (KERNEL32.332)
1096 * TRUE: Object is still locked
1097 * FALSE: Object is unlocked
1099 BOOL WINAPI GlobalUnlock(
1100 HGLOBAL hmem /* [in] Handle of global memory object */
1102 PGLOBAL32_INTERN pintern;
1108 /* HeapLock(GetProcessHeap()); */
1109 pintern=HANDLE_TO_INTERN(hmem);
1111 if(pintern->Magic==MAGIC_GLOBAL_USED)
1113 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1114 pintern->LockCount--;
1116 locked=(pintern->LockCount==0) ? FALSE : TRUE;
1120 WARN("invalid handle\n");
1123 /* HeapUnlock(GetProcessHeap()); */
1128 /***********************************************************************
1129 * GlobalHandle32 (KERNEL32.325)
1130 * Returns the handle associated with the specified pointer.
1133 * Since there in only one goto, can it be removed and the return
1140 HGLOBAL WINAPI GlobalHandle(
1141 LPCVOID pmem /* [in] Pointer to global memory block */
1144 HANDLE heap = GLOBAL_GetHeap( POINTER_TO_HANDLE(pmem) );
1146 if (!HEAP_IsInsideHeap( heap, 0, pmem )) goto error;
1147 handle = POINTER_TO_HANDLE(pmem);
1148 if (HEAP_IsInsideHeap( heap, 0, (LPCVOID)handle ))
1150 if (HANDLE_TO_INTERN(handle)->Magic == MAGIC_GLOBAL_USED)
1151 return handle; /* valid moveable block */
1153 /* maybe FIXED block */
1154 if (HeapValidate( heap, 0, pmem ))
1155 return (HGLOBAL)pmem; /* valid fixed block */
1158 SetLastError( ERROR_INVALID_HANDLE );
1163 /***********************************************************************
1164 * GlobalReAlloc32 (KERNEL32.328)
1169 HGLOBAL WINAPI GlobalReAlloc(
1170 HGLOBAL hmem, /* [in] Handle of global memory object */
1171 DWORD size, /* [in] New size of block */
1172 UINT flags /* [in] How to reallocate object */
1176 PGLOBAL32_INTERN pintern;
1177 HANDLE heap = GLOBAL_GetHeap( hmem );
1180 /* HeapLock(heap); */
1181 if(flags & GMEM_MODIFY) /* modify flags */
1183 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1185 /* make a fixed block moveable
1186 * actually only NT is able to do this. But it's soo simple
1190 ERR("GlobalReAlloc32 with null handle!\n");
1191 SetLastError( ERROR_NOACCESS );
1194 size=HeapSize(heap, 0, (LPVOID) hmem);
1195 hnew=GlobalAlloc( flags, size);
1196 palloc=GlobalLock(hnew);
1197 memcpy(palloc, (LPVOID) hmem, size);
1201 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1203 /* change the flags to make our block "discardable" */
1204 pintern=HANDLE_TO_INTERN(hmem);
1205 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1210 SetLastError(ERROR_INVALID_PARAMETER);
1218 /* reallocate fixed memory */
1219 hnew=(HGLOBAL)HeapReAlloc(heap, 0, (LPVOID) hmem, size);
1223 /* reallocate a moveable block */
1224 pintern=HANDLE_TO_INTERN(hmem);
1225 if(pintern->LockCount>1) {
1226 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1227 SetLastError(ERROR_INVALID_HANDLE);
1231 if(pintern->Pointer)
1233 palloc=HeapReAlloc(heap, 0,
1234 (char *) pintern->Pointer-sizeof(HGLOBAL),
1235 size+sizeof(HGLOBAL) );
1236 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1240 palloc=HeapAlloc(heap, 0, size+sizeof(HGLOBAL));
1241 *(HGLOBAL *)palloc=hmem;
1242 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1247 if(pintern->Pointer)
1249 HeapFree(heap, 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1250 pintern->Pointer=NULL;
1255 /* HeapUnlock(heap); */
1260 /***********************************************************************
1261 * GlobalFree32 (KERNEL32.322)
1266 HGLOBAL WINAPI GlobalFree(
1267 HGLOBAL hmem /* [in] Handle of global memory object */
1269 PGLOBAL32_INTERN pintern;
1270 HGLOBAL hreturned = 0;
1271 HANDLE heap = GLOBAL_GetHeap( hmem );
1273 if(ISPOINTER(hmem)) /* POINTER */
1275 if(!HeapFree(heap, 0, (LPVOID) hmem)) hmem = 0;
1279 /* HeapLock(heap); */
1280 pintern=HANDLE_TO_INTERN(hmem);
1282 if(pintern->Magic==MAGIC_GLOBAL_USED)
1284 if(pintern->LockCount!=0)
1285 SetLastError(ERROR_INVALID_HANDLE);
1286 if(pintern->Pointer)
1287 if(!HeapFree(heap, 0,
1288 (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1290 if(!HeapFree(heap, 0, pintern))
1293 /* HeapUnlock(heap); */
1299 /***********************************************************************
1300 * GlobalSize32 (KERNEL32.329)
1302 * Size in bytes of the global memory object
1305 DWORD WINAPI GlobalSize(
1306 HGLOBAL hmem /* [in] Handle of global memory object */
1309 PGLOBAL32_INTERN pintern;
1310 HANDLE heap = GLOBAL_GetHeap( hmem );
1314 retval=HeapSize(heap, 0, (LPVOID) hmem);
1318 /* HeapLock(heap); */
1319 pintern=HANDLE_TO_INTERN(hmem);
1321 if(pintern->Magic==MAGIC_GLOBAL_USED)
1323 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1325 retval=HeapSize(heap, 0,
1326 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1327 if (retval == 0xffffffff-4) retval = 0;
1331 WARN("invalid handle\n");
1334 /* HeapUnlock(heap); */
1336 /* HeapSize returns 0xffffffff on failure */
1337 if (retval == 0xffffffff) retval = 0;
1342 /***********************************************************************
1343 * GlobalWire32 (KERNEL32.333)
1345 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1347 return GlobalLock( hmem );
1351 /***********************************************************************
1352 * GlobalUnWire32 (KERNEL32.330)
1354 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1356 return GlobalUnlock( hmem);
1360 /***********************************************************************
1361 * GlobalFix32 (KERNEL32.320)
1363 VOID WINAPI GlobalFix(HGLOBAL hmem)
1369 /***********************************************************************
1370 * GlobalUnfix32 (KERNEL32.331)
1372 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1374 GlobalUnlock( hmem);
1378 /***********************************************************************
1379 * GlobalFlags32 (KERNEL32.321)
1380 * Returns information about the specified global memory object
1383 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1386 * Value specifying allocation flags and lock count
1387 * GMEM_INVALID_HANDLE: Failure
1389 UINT WINAPI GlobalFlags(
1390 HGLOBAL hmem /* [in] Handle to global memory object */
1393 PGLOBAL32_INTERN pintern;
1401 /* HeapLock(GetProcessHeap()); */
1402 pintern=HANDLE_TO_INTERN(hmem);
1403 if(pintern->Magic==MAGIC_GLOBAL_USED)
1405 retval=pintern->LockCount + (pintern->Flags<<8);
1406 if(pintern->Pointer==0)
1407 retval|= GMEM_DISCARDED;
1411 WARN("Invalid handle: %04x", hmem);
1414 /* HeapUnlock(GetProcessHeap()); */
1420 /***********************************************************************
1421 * GlobalCompact32 (KERNEL32.316)
1423 DWORD WINAPI GlobalCompact( DWORD minfree )
1425 return 0; /* GlobalCompact does nothing in Win32 */
1429 /***********************************************************************
1430 * GlobalMemoryStatus (KERNEL32.327)
1434 VOID WINAPI GlobalMemoryStatus(
1435 LPMEMORYSTATUS lpmem
1438 FILE *f = fopen( "/proc/meminfo", "r" );
1442 int total, used, free, shared, buffers, cached;
1444 lpmem->dwLength = sizeof(MEMORYSTATUS);
1445 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1446 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1447 while (fgets( buffer, sizeof(buffer), f ))
1449 /* old style /proc/meminfo ... */
1450 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1452 lpmem->dwTotalPhys += total;
1453 lpmem->dwAvailPhys += free + buffers + cached;
1455 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1457 lpmem->dwTotalPageFile += total;
1458 lpmem->dwAvailPageFile += free;
1461 /* new style /proc/meminfo ... */
1462 if (sscanf(buffer, "MemTotal: %d", &total))
1463 lpmem->dwTotalPhys = total*1024;
1464 if (sscanf(buffer, "MemFree: %d", &free))
1465 lpmem->dwAvailPhys = free*1024;
1466 if (sscanf(buffer, "SwapTotal: %d", &total))
1467 lpmem->dwTotalPageFile = total*1024;
1468 if (sscanf(buffer, "SwapFree: %d", &free))
1469 lpmem->dwAvailPageFile = free*1024;
1470 if (sscanf(buffer, "Buffers: %d", &buffers))
1471 lpmem->dwAvailPhys += buffers*1024;
1472 if (sscanf(buffer, "Cached: %d", &cached))
1473 lpmem->dwAvailPhys += cached*1024;
1477 if (lpmem->dwTotalPhys)
1479 lpmem->dwTotalVirtual = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1480 lpmem->dwAvailVirtual = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1481 lpmem->dwMemoryLoad = (lpmem->dwTotalVirtual-lpmem->dwAvailVirtual)
1482 / (lpmem->dwTotalVirtual / 100);
1487 /* FIXME: should do something for other systems */
1488 lpmem->dwMemoryLoad = 0;
1489 lpmem->dwTotalPhys = 16*1024*1024;
1490 lpmem->dwAvailPhys = 16*1024*1024;
1491 lpmem->dwTotalPageFile = 16*1024*1024;
1492 lpmem->dwAvailPageFile = 16*1024*1024;
1493 lpmem->dwTotalVirtual = 32*1024*1024;
1494 lpmem->dwAvailVirtual = 32*1024*1024;
1497 /***********************************************************************
1498 * A20Proc16 (KERNEL.165)
1500 void WINAPI A20Proc16( WORD unused )
1502 /* this is also a NOP in Windows */
1505 /***********************************************************************
1506 * LimitEMSPages16 (KERNEL.156)
1508 DWORD WINAPI LimitEMSPages16( DWORD unused )