2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/port.h"
21 #include "selectors.h"
23 #include "stackframe.h"
25 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(global
);
30 /* Global arena block */
33 DWORD base
; /* Base address (0 if discarded) */
34 DWORD size
; /* Size in bytes (0 indicates a free block) */
35 HGLOBAL16 handle
; /* Handle for this block */
36 HGLOBAL16 hOwner
; /* Owner of this block */
37 BYTE lockCount
; /* Count of GlobalFix() calls */
38 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
39 BYTE flags
; /* Allocation flags */
40 BYTE selCount
; /* Number of selectors allocated for this block */
43 /* Flags definitions */
44 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
45 #define GA_DGROUP 0x04
46 #define GA_DISCARDABLE 0x08
47 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
48 #define GA_DOSMEM 0x20
51 static GLOBALARENA
*pGlobalArena
= NULL
;
52 static int globalArenaSize
= 0;
54 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
56 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
57 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
59 /* filter for page-fault exceptions */
60 /* It is possible for a bogus global pointer to cause a */
61 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
63 static WINE_EXCEPTION_FILTER(page_fault
)
65 switch (GetExceptionCode()) {
66 case (EXCEPTION_ACCESS_VIOLATION
):
67 case (EXCEPTION_PRIV_INSTRUCTION
):
68 return EXCEPTION_EXECUTE_HANDLER
;
70 return EXCEPTION_CONTINUE_SEARCH
;
74 /***********************************************************************
77 * Return the arena for a given selector, growing the arena array if needed.
79 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
81 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
83 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
84 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
85 newsize
* sizeof(GLOBALARENA
) );
86 if (!pNewArena
) return 0;
87 pGlobalArena
= pNewArena
;
88 memset( pGlobalArena
+ globalArenaSize
, 0,
89 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
90 globalArenaSize
= newsize
;
92 return pGlobalArena
+ (sel
>> __AHSHIFT
);
95 void debug_handles(void)
99 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
100 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
102 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
110 /***********************************************************************
113 * Create a global heap block for a fixed range of linear memory.
115 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
116 HGLOBAL16 hOwner
, unsigned char selflags
)
121 /* Allocate the selector(s) */
123 sel
= SELECTOR_AllocBlock( ptr
, size
, selflags
);
125 selcount
= (size
+ 0xffff) / 0x10000;
127 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
129 SELECTOR_FreeBlock( sel
);
133 /* Fill the arena block */
135 pArena
->base
= (DWORD
)ptr
;
136 pArena
->size
= GetSelectorLimit16(sel
) + 1;
137 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
138 pArena
->hOwner
= hOwner
;
139 pArena
->lockCount
= 0;
140 pArena
->pageLockCount
= 0;
141 pArena
->flags
= flags
& GA_MOVEABLE
;
142 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
143 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
144 if (!(selflags
& (WINE_LDT_FLAGS_CODE
^WINE_LDT_FLAGS_DATA
))) pArena
->flags
|= GA_DGROUP
;
145 pArena
->selCount
= selcount
;
146 if (selcount
> 1) /* clear the next arena blocks */
147 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
149 return pArena
->handle
;
153 /***********************************************************************
156 * Free a block allocated by GLOBAL_CreateBlock, without touching
157 * the associated linear memory range.
159 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
164 if (!handle
) return TRUE
;
165 sel
= GlobalHandleToSel16( handle
);
166 if (!VALID_HANDLE(sel
))
168 pArena
= GET_ARENA_PTR(sel
);
169 SELECTOR_FreeBlock( sel
);
170 memset( pArena
, 0, sizeof(GLOBALARENA
) );
174 /***********************************************************************
177 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
182 if (!handle
) return TRUE
;
183 sel
= GlobalHandleToSel16( handle
);
184 if (!VALID_HANDLE(sel
))
186 pArena
= GET_ARENA_PTR(sel
);
187 if (pArena
->selCount
!= 1)
190 pArena
->base
= (DWORD
)ptr
;
192 SELECTOR_ReallocBlock( sel
, ptr
, size
);
196 /***********************************************************************
199 * Implementation of GlobalAlloc16()
201 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
, unsigned char selflags
)
206 TRACE("%ld flags=%04x\n", size
, flags
);
208 /* If size is 0, create a discarded block */
210 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, selflags
);
214 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
215 size
= (size
+ 0x1f) & ~0x1f;
217 /* Allocate the linear memory */
218 ptr
= HeapAlloc( GetProcessHeap(), 0, size
);
219 /* FIXME: free discardable blocks and try again? */
222 /* Allocate the selector(s) */
224 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
, selflags
);
227 HeapFree( GetProcessHeap(), 0, ptr
);
231 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
235 /***********************************************************************
236 * GlobalAlloc16 (KERNEL.15)
241 HGLOBAL16 WINAPI
GlobalAlloc16(
242 UINT16 flags
, /* [in] Object allocation attributes */
243 DWORD size
/* [in] Number of bytes to allocate */
245 HANDLE16 owner
= GetCurrentPDB16();
247 if (flags
& GMEM_DDESHARE
)
248 owner
= GetExePtr(owner
); /* Make it a module handle */
249 return GLOBAL_Alloc( flags
, size
, owner
, WINE_LDT_FLAGS_DATA
);
253 /***********************************************************************
254 * GlobalReAlloc16 (KERNEL.16)
259 HGLOBAL16 WINAPI
GlobalReAlloc16(
260 HGLOBAL16 handle
, /* [in] Handle of global memory object */
261 DWORD size
, /* [in] New size of block */
262 UINT16 flags
/* [in] How to reallocate object */
267 GLOBALARENA
*pArena
, *pNewArena
;
268 WORD sel
= GlobalHandleToSel16( handle
);
270 TRACE("%04x %ld flags=%04x\n",
271 handle
, size
, flags
);
272 if (!handle
) return 0;
274 if (!VALID_HANDLE(handle
)) {
275 WARN("Invalid handle 0x%04x!\n", handle
);
278 pArena
= GET_ARENA_PTR( handle
);
280 /* Discard the block if requested */
282 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
284 if (!(pArena
->flags
& GA_MOVEABLE
) ||
285 !(pArena
->flags
& GA_DISCARDABLE
) ||
286 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
287 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
290 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
291 * change the selector if we are shrinking the block.
292 * FIXME: shouldn't we keep selectors until the block is deleted?
294 SELECTOR_ReallocBlock( sel
, 0, 1 );
300 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
301 if (size
== 0) size
= 0x20;
302 else size
= (size
+ 0x1f) & ~0x1f;
304 /* Change the flags */
306 if (flags
& GMEM_MODIFY
)
308 /* Change the flags, leaving GA_DGROUP alone */
309 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
310 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
314 /* Reallocate the linear memory */
316 ptr
= (void *)pArena
->base
;
317 oldsize
= pArena
->size
;
318 TRACE("oldsize %08lx\n",oldsize
);
319 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
321 if (pArena
->flags
& GA_DOSMEM
)
322 ptr
= DOSMEM_ResizeBlock(ptr
, size
, NULL
);
324 ptr
= HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
327 SELECTOR_FreeBlock( sel
);
328 memset( pArena
, 0, sizeof(GLOBALARENA
) );
332 /* Reallocate the selector(s) */
334 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
337 HeapFree( GetProcessHeap(), 0, ptr
);
338 memset( pArena
, 0, sizeof(GLOBALARENA
) );
341 selcount
= (size
+ 0xffff) / 0x10000;
343 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
345 HeapFree( GetProcessHeap(), 0, ptr
);
346 SELECTOR_FreeBlock( sel
);
350 /* Fill the new arena block */
352 if (pNewArena
!= pArena
) memcpy( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
353 pNewArena
->base
= (DWORD
)ptr
;
354 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
355 pNewArena
->selCount
= selcount
;
356 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
358 if (selcount
> 1) /* clear the next arena blocks */
359 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
361 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
362 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
363 return pNewArena
->handle
;
367 /***********************************************************************
368 * GlobalFree16 (KERNEL.17)
373 HGLOBAL16 WINAPI
GlobalFree16(
374 HGLOBAL16 handle
/* [in] Handle of global memory object */
378 if (!VALID_HANDLE(handle
)) {
379 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
382 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
384 TRACE("%04x\n", handle
);
385 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
386 if (ptr
) HeapFree( GetProcessHeap(), 0, ptr
);
391 /***********************************************************************
392 * WIN16_GlobalLock16 (KERNEL.18)
394 * This is the GlobalLock16() function used by 16-bit code.
396 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
398 WORD sel
= GlobalHandleToSel16( handle
);
399 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
403 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
405 if (!VALID_HANDLE(handle
)) {
406 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
409 else if (!GET_ARENA_PTR(handle
)->base
)
412 GET_ARENA_PTR(handle
)->lockCount
++;
415 CURRENT_STACK16
->ecx
= sel
; /* selector must be returned in CX as well */
416 return MAKESEGPTR( sel
, 0 );
420 /**********************************************************************
421 * K32WOWGlobalLock16 (WOW32.11) (KERNEL32.60)
423 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 hMem
)
425 return WIN16_GlobalLock16( hMem
);
429 /***********************************************************************
430 * GlobalLock16 (KERNEL.18)
432 * This is the GlobalLock16() function used by 32-bit code.
435 * Pointer to first byte of memory block
438 LPVOID WINAPI
GlobalLock16(
439 HGLOBAL16 handle
/* [in] Handle of global memory object */
441 if (!handle
) return 0;
442 if (!VALID_HANDLE(handle
))
444 GET_ARENA_PTR(handle
)->lockCount
++;
445 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
449 /***********************************************************************
450 * GlobalUnlock16 (KERNEL.19)
452 * Should the return values be cast to booleans?
455 * TRUE: Object is still locked
456 * FALSE: Object is unlocked
458 BOOL16 WINAPI
GlobalUnlock16(
459 HGLOBAL16 handle
/* [in] Handle of global memory object */
461 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
462 if (!VALID_HANDLE(handle
)) {
463 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
466 TRACE("%04x\n", handle
);
467 if (pArena
->lockCount
) pArena
->lockCount
--;
468 return pArena
->lockCount
;
471 /***********************************************************************
472 * GlobalChangeLockCount (KERNEL.365)
474 * This is declared as a register function as it has to preserve
475 * *all* registers, even AX/DX !
478 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
482 GlobalLock16( handle
);
483 else if ( delta
== -1 )
484 GlobalUnlock16( handle
);
486 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
489 /***********************************************************************
490 * GlobalSize16 (KERNEL.20)
492 * Size in bytes of object
495 DWORD WINAPI
GlobalSize16(
496 HGLOBAL16 handle
/* [in] Handle of global memory object */
498 TRACE("%04x\n", handle
);
499 if (!handle
) return 0;
500 if (!VALID_HANDLE(handle
))
502 return GET_ARENA_PTR(handle
)->size
;
506 /***********************************************************************
507 * GlobalHandle16 (KERNEL.21)
509 * Why is GlobalHandleToSel used here with the sel as input?
515 DWORD WINAPI
GlobalHandle16(
516 WORD sel
/* [in] Address of global memory block */
518 TRACE("%04x\n", sel
);
519 if (!VALID_HANDLE(sel
)) {
520 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
523 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
526 /***********************************************************************
527 * GlobalHandleNoRIP (KERNEL.159)
529 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
532 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
533 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
534 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
540 /***********************************************************************
541 * GlobalFlags16 (KERNEL.22)
543 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
547 * Value specifying flags and lock count
548 * GMEM_INVALID_HANDLE: Invalid handle
550 UINT16 WINAPI
GlobalFlags16(
551 HGLOBAL16 handle
/* [in] Handle of global memory object */
555 TRACE("%04x\n", handle
);
556 if (!VALID_HANDLE(handle
)) {
557 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
560 pArena
= GET_ARENA_PTR(handle
);
561 return pArena
->lockCount
|
562 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
563 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
567 /***********************************************************************
568 * LockSegment16 (KERNEL.23)
570 HGLOBAL16 WINAPI
LockSegment16( 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 LockSegment16!\n",handle
);
578 GET_ARENA_PTR(handle
)->lockCount
++;
583 /***********************************************************************
584 * UnlockSegment16 (KERNEL.24)
586 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
588 TRACE("%04x\n", handle
);
589 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
590 if (!VALID_HANDLE(handle
)) {
591 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
594 GET_ARENA_PTR(handle
)->lockCount
--;
595 /* FIXME: this ought to return the lock count in CX (go figure...) */
599 /***********************************************************************
600 * GlobalCompact16 (KERNEL.25)
602 DWORD WINAPI
GlobalCompact16( DWORD desired
)
604 return GLOBAL_MAX_ALLOC_SIZE
;
608 /***********************************************************************
609 * GlobalFreeAll (KERNEL.26)
611 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
616 pArena
= pGlobalArena
;
617 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
619 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
620 GlobalFree16( pArena
->handle
);
625 /***********************************************************************
626 * GlobalWire16 (KERNEL.111)
628 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
630 return WIN16_GlobalLock16( handle
);
634 /***********************************************************************
635 * GlobalUnWire16 (KERNEL.112)
637 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
639 return !GlobalUnlock16( handle
);
643 /***********************************************************************
644 * SetSwapAreaSize16 (KERNEL.106)
646 LONG WINAPI
SetSwapAreaSize16( WORD size
)
648 FIXME("(%d) - stub!\n", size
);
649 return MAKELONG( size
, 0xffff );
653 /***********************************************************************
654 * GlobalLRUOldest (KERNEL.163)
656 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
658 TRACE("%04x\n", handle
);
659 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
664 /***********************************************************************
665 * GlobalLRUNewest (KERNEL.164)
667 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
669 TRACE("%04x\n", handle
);
670 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
675 /***********************************************************************
676 * GetFreeSpace16 (KERNEL.169)
678 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
681 GlobalMemoryStatus( &ms
);
682 return ms
.dwAvailVirtual
;
685 /***********************************************************************
686 * GlobalDOSAlloc (KERNEL.184)
688 * Address (HW=Paragraph segment; LW=Selector)
690 DWORD WINAPI
GlobalDOSAlloc16(
691 DWORD size
/* [in] Number of bytes to be allocated */
694 LPVOID lpBlock
= DOSMEM_GetBlock( size
, &uParagraph
);
698 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
702 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
703 pArena
= GET_ARENA_PTR(wSelector
);
704 pArena
->flags
|= GA_DOSMEM
;
705 return MAKELONG(wSelector
,uParagraph
);
711 /***********************************************************************
712 * GlobalDOSFree (KERNEL.185)
717 WORD WINAPI
GlobalDOSFree16(
718 WORD sel
/* [in] Selector */
720 DWORD block
= GetSelectorBase(sel
);
722 if( block
&& block
< 0x100000 )
724 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
725 if( DOSMEM_FreeBlock( lpBlock
) )
726 GLOBAL_FreeBlock( sel
);
733 /***********************************************************************
734 * GlobalPageLock (KERNEL.191)
736 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
738 TRACE("%04x\n", handle
);
739 if (!VALID_HANDLE(handle
)) {
740 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
743 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
747 /***********************************************************************
748 * GlobalPageUnlock (KERNEL.192)
750 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
752 TRACE("%04x\n", handle
);
753 if (!VALID_HANDLE(handle
)) {
754 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
757 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
761 /***********************************************************************
762 * GlobalFix16 (KERNEL.197)
764 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
766 TRACE("%04x\n", handle
);
767 if (!VALID_HANDLE(handle
)) {
768 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
771 GET_ARENA_PTR(handle
)->lockCount
++;
773 return GlobalHandleToSel16(handle
);
777 /***********************************************************************
778 * GlobalUnfix16 (KERNEL.198)
780 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
782 TRACE("%04x\n", handle
);
783 if (!VALID_HANDLE(handle
)) {
784 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
787 GET_ARENA_PTR(handle
)->lockCount
--;
791 /***********************************************************************
792 * FarSetOwner (KERNEL.403)
794 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
796 if (!VALID_HANDLE(handle
)) {
797 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
800 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
804 /***********************************************************************
805 * FarGetOwner (KERNEL.404)
807 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
809 if (!VALID_HANDLE(handle
)) {
810 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
813 return GET_ARENA_PTR(handle
)->hOwner
;
817 /***********************************************************************
818 * GlobalHandleToSel (TOOLHELP.50)
820 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
822 if (!handle
) return 0;
823 if (!VALID_HANDLE(handle
)) {
824 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
829 WARN("Program attempted invalid selector conversion\n" );
836 /***********************************************************************
837 * GlobalFirst (TOOLHELP.51)
839 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
841 if (wFlags
== GLOBAL_LRU
) return FALSE
;
843 return GlobalNext16( pGlobal
, wFlags
);
847 /***********************************************************************
848 * GlobalNext (TOOLHELP.52)
850 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
854 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
855 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
856 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
859 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
860 if (pArena
->size
== 0) break; /* block is free */
861 if (i
>= globalArenaSize
) return FALSE
;
865 pGlobal
->dwAddress
= pArena
->base
;
866 pGlobal
->dwBlockSize
= pArena
->size
;
867 pGlobal
->hBlock
= pArena
->handle
;
868 pGlobal
->wcLock
= pArena
->lockCount
;
869 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
870 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
871 pGlobal
->wHeapPresent
= FALSE
;
872 pGlobal
->hOwner
= pArena
->hOwner
;
873 pGlobal
->wType
= GT_UNKNOWN
;
880 /***********************************************************************
881 * GlobalInfo (TOOLHELP.53)
883 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
888 pInfo
->wcItems
= globalArenaSize
;
889 pInfo
->wcItemsFree
= 0;
890 pInfo
->wcItemsLRU
= 0;
891 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
892 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
897 /***********************************************************************
898 * GlobalEntryHandle (TOOLHELP.54)
900 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
902 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
904 pGlobal
->dwAddress
= pArena
->base
;
905 pGlobal
->dwBlockSize
= pArena
->size
;
906 pGlobal
->hBlock
= pArena
->handle
;
907 pGlobal
->wcLock
= pArena
->lockCount
;
908 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
909 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
910 pGlobal
->wHeapPresent
= FALSE
;
911 pGlobal
->hOwner
= pArena
->hOwner
;
912 pGlobal
->wType
= GT_UNKNOWN
;
919 /***********************************************************************
920 * GlobalEntryModule (TOOLHELP.55)
922 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
925 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
930 /***********************************************************************
931 * MemManInfo (TOOLHELP.72)
933 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
938 * Not unsurprisingly although the documention says you
939 * _must_ provide the size in the dwSize field, this function
940 * (under Windows) always fills the structure and returns true.
942 GlobalMemoryStatus( &status
);
943 info
->wPageSize
= getpagesize();
944 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
945 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
946 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
947 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
948 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
949 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
950 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
951 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
952 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
956 /***********************************************************************
957 * GetFreeMemInfo (KERNEL.316)
959 DWORD WINAPI
GetFreeMemInfo16(void)
962 MemManInfo16( &info
);
963 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
967 * Win32 Global heap functions (GlobalXXX).
968 * These functions included in Win32 for compatibility with 16 bit Windows
969 * Especially the moveable blocks and handles are oldish.
970 * But the ability to directly allocate memory with GPTR and LPTR is widely
973 * The handle stuff looks horrible, but it's implemented almost like Win95
978 #define MAGIC_GLOBAL_USED 0x5342
979 #define GLOBAL_LOCK_MAX 0xFF
980 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
981 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
982 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
983 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
984 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
986 typedef struct __GLOBAL32_INTERN
989 LPVOID Pointer WINE_PACKED
;
992 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
995 /***********************************************************************
996 * GlobalAlloc (KERNEL32.315)
1001 HGLOBAL WINAPI
GlobalAlloc(
1002 UINT flags
, /* [in] Object allocation attributes */
1003 DWORD size
/* [in] Number of bytes to allocate */
1005 PGLOBAL32_INTERN pintern
;
1009 if(flags
&GMEM_ZEROINIT
)
1010 hpflags
=HEAP_ZERO_MEMORY
;
1014 TRACE("() flags=%04x\n", flags
);
1016 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1018 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1019 return (HGLOBAL
) palloc
;
1023 /* HeapLock(heap); */
1025 pintern
=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1026 if (!pintern
) return 0;
1029 if (!(palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
+sizeof(HGLOBAL
)))) {
1030 HeapFree(GetProcessHeap(), 0, pintern
);
1033 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1034 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1037 pintern
->Pointer
=NULL
;
1038 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1039 pintern
->Flags
=flags
>>8;
1040 pintern
->LockCount
=0;
1042 /* HeapUnlock(heap); */
1044 return INTERN_TO_HANDLE(pintern
);
1049 /***********************************************************************
1050 * GlobalLock (KERNEL32.326)
1052 * Pointer to first byte of block
1055 LPVOID WINAPI
GlobalLock(
1056 HGLOBAL hmem
/* [in] Handle of global memory object */
1058 PGLOBAL32_INTERN pintern
;
1062 return (LPVOID
) hmem
;
1064 /* HeapLock(GetProcessHeap()); */
1066 pintern
=HANDLE_TO_INTERN(hmem
);
1067 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1069 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1070 pintern
->LockCount
++;
1071 palloc
=pintern
->Pointer
;
1075 WARN("invalid handle\n");
1076 palloc
=(LPVOID
) NULL
;
1077 SetLastError(ERROR_INVALID_HANDLE
);
1079 /* HeapUnlock(GetProcessHeap()); */;
1084 /***********************************************************************
1085 * GlobalUnlock (KERNEL32.332)
1087 * TRUE: Object is still locked
1088 * FALSE: Object is unlocked
1090 BOOL WINAPI
GlobalUnlock(
1091 HGLOBAL hmem
/* [in] Handle of global memory object */
1093 PGLOBAL32_INTERN pintern
;
1099 /* HeapLock(GetProcessHeap()); */
1100 pintern
=HANDLE_TO_INTERN(hmem
);
1102 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1104 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1105 pintern
->LockCount
--;
1107 locked
= (pintern
->LockCount
!= 0);
1108 if (!locked
) SetLastError(NO_ERROR
);
1112 WARN("invalid handle\n");
1113 SetLastError(ERROR_INVALID_HANDLE
);
1116 /* HeapUnlock(GetProcessHeap()); */
1121 /***********************************************************************
1122 * GlobalHandle (KERNEL32.325)
1123 * Returns the handle associated with the specified pointer.
1129 HGLOBAL WINAPI
GlobalHandle(
1130 LPCVOID pmem
/* [in] Pointer to global memory block */
1133 PGLOBAL32_INTERN maybe_intern
;
1138 SetLastError( ERROR_INVALID_PARAMETER
);
1146 /* note that if pmem is a pointer to a a block allocated by */
1147 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1149 if (ISPOINTER(pmem
)) {
1150 if (HeapValidate( GetProcessHeap(), 0, pmem
)) {
1151 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
1154 handle
= POINTER_TO_HANDLE(pmem
);
1156 handle
= (HGLOBAL
)pmem
;
1158 /* Now test handle either passed in or retrieved from pointer */
1159 maybe_intern
= HANDLE_TO_INTERN( handle
);
1160 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1161 test
= maybe_intern
->Pointer
;
1162 if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL
*)test
)-1 ) && /* obj(-handle) valid arena? */
1163 HeapValidate( GetProcessHeap(), 0, maybe_intern
)) /* intern valid arena? */
1164 break; /* valid moveable block */
1167 SetLastError( ERROR_INVALID_HANDLE
);
1169 __EXCEPT(page_fault
)
1171 SetLastError( ERROR_INVALID_HANDLE
);
1180 /***********************************************************************
1181 * GlobalReAlloc (KERNEL32.328)
1186 HGLOBAL WINAPI
GlobalReAlloc(
1187 HGLOBAL hmem
, /* [in] Handle of global memory object */
1188 DWORD size
, /* [in] New size of block */
1189 UINT flags
/* [in] How to reallocate object */
1193 PGLOBAL32_INTERN pintern
;
1194 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1197 /* HeapLock(heap); */
1198 if(flags
& GMEM_MODIFY
) /* modify flags */
1200 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1202 /* make a fixed block moveable
1203 * actually only NT is able to do this. But it's soo simple
1207 ERR("GlobalReAlloc32 with null handle!\n");
1208 SetLastError( ERROR_NOACCESS
);
1211 size
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1212 hnew
=GlobalAlloc( flags
, size
);
1213 palloc
=GlobalLock(hnew
);
1214 memcpy(palloc
, (LPVOID
) hmem
, size
);
1218 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1220 /* change the flags to make our block "discardable" */
1221 pintern
=HANDLE_TO_INTERN(hmem
);
1222 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1227 SetLastError(ERROR_INVALID_PARAMETER
);
1235 /* reallocate fixed memory */
1236 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), heap_flags
, (LPVOID
) hmem
, size
);
1240 /* reallocate a moveable block */
1241 pintern
=HANDLE_TO_INTERN(hmem
);
1244 /* Apparently Windows doesn't care whether the handle is locked at this point */
1245 /* See also the same comment in GlobalFree() */
1246 if(pintern
->LockCount
>1) {
1247 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1248 SetLastError(ERROR_INVALID_HANDLE
);
1254 if(pintern
->Pointer
)
1256 if((palloc
= HeapReAlloc(GetProcessHeap(), 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(GetProcessHeap(), heap_flags
, size
+sizeof(HGLOBAL
)))
1267 *(HGLOBAL
*)palloc
=hmem
;
1268 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1273 if(pintern
->Pointer
)
1275 HeapFree(GetProcessHeap(), 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;
1298 if(ISPOINTER(hmem
)) /* POINTER */
1300 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1304 /* HeapLock(heap); */
1305 pintern
=HANDLE_TO_INTERN(hmem
);
1307 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1310 /* WIN98 does not make this test. That is you can free a */
1311 /* block you have not unlocked. Go figure!! */
1312 /* if(pintern->LockCount!=0) */
1313 /* SetLastError(ERROR_INVALID_HANDLE); */
1315 if(pintern
->Pointer
)
1316 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1318 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1321 /* HeapUnlock(heap); */
1327 /***********************************************************************
1328 * GlobalSize (KERNEL32.329)
1330 * Size in bytes of the global memory object
1333 DWORD WINAPI
GlobalSize(
1334 HGLOBAL hmem
/* [in] Handle of global memory object */
1337 PGLOBAL32_INTERN pintern
;
1341 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1345 /* HeapLock(heap); */
1346 pintern
=HANDLE_TO_INTERN(hmem
);
1348 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1350 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1352 retval
=HeapSize(GetProcessHeap(), 0,
1353 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1354 if (retval
== 0xffffffff-4) retval
= 0;
1358 WARN("invalid handle\n");
1361 /* HeapUnlock(heap); */
1363 /* HeapSize returns 0xffffffff on failure */
1364 if (retval
== 0xffffffff) retval
= 0;
1369 /***********************************************************************
1370 * GlobalWire (KERNEL32.333)
1372 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1374 return GlobalLock( hmem
);
1378 /***********************************************************************
1379 * GlobalUnWire (KERNEL32.330)
1381 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1383 return GlobalUnlock( hmem
);
1387 /***********************************************************************
1388 * GlobalFix (KERNEL32.320)
1390 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1396 /***********************************************************************
1397 * GlobalUnfix (KERNEL32.331)
1399 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1401 GlobalUnlock( hmem
);
1405 /***********************************************************************
1406 * GlobalFlags (KERNEL32.321)
1407 * Returns information about the specified global memory object
1410 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1413 * Value specifying allocation flags and lock count
1414 * GMEM_INVALID_HANDLE: Failure
1416 UINT WINAPI
GlobalFlags(
1417 HGLOBAL hmem
/* [in] Handle to global memory object */
1420 PGLOBAL32_INTERN pintern
;
1428 /* HeapLock(GetProcessHeap()); */
1429 pintern
=HANDLE_TO_INTERN(hmem
);
1430 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1432 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1433 if(pintern
->Pointer
==0)
1434 retval
|= GMEM_DISCARDED
;
1438 WARN("Invalid handle: %04x", hmem
);
1441 /* HeapUnlock(GetProcessHeap()); */
1447 /***********************************************************************
1448 * GlobalCompact (KERNEL32.316)
1450 DWORD WINAPI
GlobalCompact( DWORD minfree
)
1452 return 0; /* GlobalCompact does nothing in Win32 */
1456 /***********************************************************************
1457 * GlobalMemoryStatus (KERNEL32.327)
1461 VOID WINAPI
GlobalMemoryStatus(
1462 LPMEMORYSTATUS lpmem
1464 static MEMORYSTATUS cached_memstatus
;
1465 static int cache_lastchecked
= 0;
1471 if (time(NULL
)==cache_lastchecked
) {
1472 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1475 cache_lastchecked
= time(NULL
);
1478 f
= fopen( "/proc/meminfo", "r" );
1482 int total
, used
, free
, shared
, buffers
, cached
;
1484 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1485 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1486 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1487 while (fgets( buffer
, sizeof(buffer
), f
))
1489 /* old style /proc/meminfo ... */
1490 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1492 lpmem
->dwTotalPhys
+= total
;
1493 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1495 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1497 lpmem
->dwTotalPageFile
+= total
;
1498 lpmem
->dwAvailPageFile
+= free
;
1501 /* new style /proc/meminfo ... */
1502 if (sscanf(buffer
, "MemTotal: %d", &total
))
1503 lpmem
->dwTotalPhys
= total
*1024;
1504 if (sscanf(buffer
, "MemFree: %d", &free
))
1505 lpmem
->dwAvailPhys
= free
*1024;
1506 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1507 lpmem
->dwTotalPageFile
= total
*1024;
1508 if (sscanf(buffer
, "SwapFree: %d", &free
))
1509 lpmem
->dwAvailPageFile
= free
*1024;
1510 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1511 lpmem
->dwAvailPhys
+= buffers
*1024;
1512 if (sscanf(buffer
, "Cached: %d", &cached
))
1513 lpmem
->dwAvailPhys
+= cached
*1024;
1517 if (lpmem
->dwTotalPhys
)
1519 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1520 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1521 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1522 / (TotalPhysical
/ 100);
1527 /* FIXME: should do something for other systems */
1528 lpmem
->dwMemoryLoad
= 0;
1529 lpmem
->dwTotalPhys
= 16*1024*1024;
1530 lpmem
->dwAvailPhys
= 16*1024*1024;
1531 lpmem
->dwTotalPageFile
= 16*1024*1024;
1532 lpmem
->dwAvailPageFile
= 16*1024*1024;
1535 lpmem
->dwTotalVirtual
= si
.lpMaximumApplicationAddress
-si
.lpMinimumApplicationAddress
;
1536 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1537 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1538 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1540 /* it appears some memory display programs want to divide by these values */
1541 if(lpmem
->dwTotalPageFile
==0)
1542 lpmem
->dwTotalPageFile
++;
1544 if(lpmem
->dwAvailPageFile
==0)
1545 lpmem
->dwAvailPageFile
++;
1548 /***********************************************************************
1549 * A20Proc16 (KERNEL.165)
1551 void WINAPI
A20Proc16( WORD unused
)
1553 /* this is also a NOP in Windows */
1556 /***********************************************************************
1557 * LimitEMSPages16 (KERNEL.156)
1559 DWORD WINAPI
LimitEMSPages16( DWORD unused
)