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
;
994 /***********************************************************************
997 * Returns the appropriate heap to be used. If the object was created
998 * With GMEM_DDESHARE we allocated it on the system heap.
1000 static HANDLE
GLOBAL_GetHeap( HGLOBAL hmem
)
1004 TRACE("() hmem=%x\n", hmem
);
1006 /* Get the appropriate heap to be used for this object */
1007 if (ISPOINTER(hmem
))
1008 heap
= GetProcessHeap();
1011 PGLOBAL32_INTERN pintern
;
1012 pintern
=HANDLE_TO_INTERN(hmem
);
1014 /* If it was DDESHARE it was created on the shared system heap */
1015 pintern
=HANDLE_TO_INTERN(hmem
);
1016 heap
= ( pintern
->Flags
& (GMEM_DDESHARE
>> 8) )
1017 ? SystemHeap
: GetProcessHeap();
1023 /***********************************************************************
1024 * GlobalAlloc (KERNEL32.315)
1029 HGLOBAL WINAPI
GlobalAlloc(
1030 UINT flags
, /* [in] Object allocation attributes */
1031 DWORD size
/* [in] Number of bytes to allocate */
1033 PGLOBAL32_INTERN pintern
;
1037 if(flags
&GMEM_ZEROINIT
)
1038 hpflags
=HEAP_ZERO_MEMORY
;
1042 TRACE("() flags=%04x\n", flags
);
1044 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1046 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1047 return (HGLOBAL
) palloc
;
1053 /* If DDESHARE is set, create on the shared system heap */
1054 heap
= (flags
& GMEM_DDESHARE
) ? SystemHeap
: GetProcessHeap();
1056 /* HeapLock(heap); */
1058 pintern
=HeapAlloc(heap
, 0, sizeof(GLOBAL32_INTERN
));
1059 if (!pintern
) return 0;
1062 if (!(palloc
=HeapAlloc(heap
, hpflags
, size
+sizeof(HGLOBAL
)))) {
1063 HeapFree(heap
, 0, pintern
);
1066 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1067 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1070 pintern
->Pointer
=NULL
;
1071 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1072 pintern
->Flags
=flags
>>8;
1073 pintern
->LockCount
=0;
1075 /* HeapUnlock(heap); */
1077 return INTERN_TO_HANDLE(pintern
);
1082 /***********************************************************************
1083 * GlobalLock (KERNEL32.326)
1085 * Pointer to first byte of block
1088 LPVOID WINAPI
GlobalLock(
1089 HGLOBAL hmem
/* [in] Handle of global memory object */
1091 PGLOBAL32_INTERN pintern
;
1095 return (LPVOID
) hmem
;
1097 /* HeapLock(GetProcessHeap()); */
1099 pintern
=HANDLE_TO_INTERN(hmem
);
1100 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1102 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1103 pintern
->LockCount
++;
1104 palloc
=pintern
->Pointer
;
1108 WARN("invalid handle\n");
1109 palloc
=(LPVOID
) NULL
;
1110 SetLastError(ERROR_INVALID_HANDLE
);
1112 /* HeapUnlock(GetProcessHeap()); */;
1117 /***********************************************************************
1118 * GlobalUnlock (KERNEL32.332)
1120 * TRUE: Object is still locked
1121 * FALSE: Object is unlocked
1123 BOOL WINAPI
GlobalUnlock(
1124 HGLOBAL hmem
/* [in] Handle of global memory object */
1126 PGLOBAL32_INTERN pintern
;
1132 /* HeapLock(GetProcessHeap()); */
1133 pintern
=HANDLE_TO_INTERN(hmem
);
1135 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1137 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1138 pintern
->LockCount
--;
1140 locked
= (pintern
->LockCount
!= 0);
1141 if (!locked
) SetLastError(NO_ERROR
);
1145 WARN("invalid handle\n");
1146 SetLastError(ERROR_INVALID_HANDLE
);
1149 /* HeapUnlock(GetProcessHeap()); */
1154 /***********************************************************************
1155 * GlobalHandle (KERNEL32.325)
1156 * Returns the handle associated with the specified pointer.
1162 HGLOBAL WINAPI
GlobalHandle(
1163 LPCVOID pmem
/* [in] Pointer to global memory block */
1167 PGLOBAL32_INTERN maybe_intern
;
1172 SetLastError( ERROR_INVALID_PARAMETER
);
1180 /* note that if pmem is a pointer to a a block allocated by */
1181 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1183 if (ISPOINTER(pmem
)) {
1184 heap
= GLOBAL_GetHeap( (HGLOBAL
)pmem
);
1185 if (HeapValidate( heap
, 0, pmem
)) {
1186 handle
= (HGLOBAL
)pmem
; /* valid fixed block */
1189 handle
= POINTER_TO_HANDLE(pmem
);
1191 handle
= (HGLOBAL
)pmem
;
1193 /* Now test handle either passed in or retrieved from pointer */
1194 heap
= GLOBAL_GetHeap( handle
);
1195 maybe_intern
= HANDLE_TO_INTERN( handle
);
1196 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1197 test
= maybe_intern
->Pointer
;
1198 if (HeapValidate( heap
, 0, ((HGLOBAL
*)test
)-1 ) && /* obj(-handle) valid arena? */
1199 HeapValidate( heap
, 0, maybe_intern
)) /* intern valid arena? */
1200 break; /* valid moveable block */
1203 SetLastError( ERROR_INVALID_HANDLE
);
1205 __EXCEPT(page_fault
)
1207 SetLastError( ERROR_INVALID_HANDLE
);
1216 /***********************************************************************
1217 * GlobalReAlloc (KERNEL32.328)
1222 HGLOBAL WINAPI
GlobalReAlloc(
1223 HGLOBAL hmem
, /* [in] Handle of global memory object */
1224 DWORD size
, /* [in] New size of block */
1225 UINT flags
/* [in] How to reallocate object */
1229 PGLOBAL32_INTERN pintern
;
1230 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1231 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1234 /* HeapLock(heap); */
1235 if(flags
& GMEM_MODIFY
) /* modify flags */
1237 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1239 /* make a fixed block moveable
1240 * actually only NT is able to do this. But it's soo simple
1244 ERR("GlobalReAlloc32 with null handle!\n");
1245 SetLastError( ERROR_NOACCESS
);
1248 size
=HeapSize(heap
, 0, (LPVOID
) hmem
);
1249 hnew
=GlobalAlloc( flags
, size
);
1250 palloc
=GlobalLock(hnew
);
1251 memcpy(palloc
, (LPVOID
) hmem
, size
);
1255 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1257 /* change the flags to make our block "discardable" */
1258 pintern
=HANDLE_TO_INTERN(hmem
);
1259 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1264 SetLastError(ERROR_INVALID_PARAMETER
);
1272 /* reallocate fixed memory */
1273 hnew
=(HGLOBAL
)HeapReAlloc(heap
, heap_flags
, (LPVOID
) hmem
, size
);
1277 /* reallocate a moveable block */
1278 pintern
=HANDLE_TO_INTERN(hmem
);
1281 /* Apparently Windows doesn't care whether the handle is locked at this point */
1282 /* See also the same comment in GlobalFree() */
1283 if(pintern
->LockCount
>1) {
1284 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1285 SetLastError(ERROR_INVALID_HANDLE
);
1291 if(pintern
->Pointer
)
1293 if((palloc
= HeapReAlloc(heap
, heap_flags
,
1294 (char *) pintern
->Pointer
-sizeof(HGLOBAL
),
1295 size
+sizeof(HGLOBAL
))) == NULL
)
1296 return 0; /* Block still valid */
1297 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1301 if((palloc
=HeapAlloc(heap
, heap_flags
, size
+sizeof(HGLOBAL
)))
1304 *(HGLOBAL
*)palloc
=hmem
;
1305 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1310 if(pintern
->Pointer
)
1312 HeapFree(heap
, 0, (char *) pintern
->Pointer
-sizeof(HGLOBAL
));
1313 pintern
->Pointer
=NULL
;
1318 /* HeapUnlock(heap); */
1323 /***********************************************************************
1324 * GlobalFree (KERNEL32.322)
1329 HGLOBAL WINAPI
GlobalFree(
1330 HGLOBAL hmem
/* [in] Handle of global memory object */
1332 PGLOBAL32_INTERN pintern
;
1333 HGLOBAL hreturned
= 0;
1334 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1336 if(ISPOINTER(hmem
)) /* POINTER */
1338 if(!HeapFree(heap
, 0, (LPVOID
) hmem
)) hmem
= 0;
1342 /* HeapLock(heap); */
1343 pintern
=HANDLE_TO_INTERN(hmem
);
1345 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1348 /* WIN98 does not make this test. That is you can free a */
1349 /* block you have not unlocked. Go figure!! */
1350 /* if(pintern->LockCount!=0) */
1351 /* SetLastError(ERROR_INVALID_HANDLE); */
1353 if(pintern
->Pointer
)
1354 if(!HeapFree(heap
, 0,
1355 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1357 if(!HeapFree(heap
, 0, pintern
))
1360 /* HeapUnlock(heap); */
1366 /***********************************************************************
1367 * GlobalSize (KERNEL32.329)
1369 * Size in bytes of the global memory object
1372 DWORD WINAPI
GlobalSize(
1373 HGLOBAL hmem
/* [in] Handle of global memory object */
1376 PGLOBAL32_INTERN pintern
;
1377 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1381 retval
=HeapSize(heap
, 0, (LPVOID
) hmem
);
1385 /* HeapLock(heap); */
1386 pintern
=HANDLE_TO_INTERN(hmem
);
1388 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1390 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1392 retval
=HeapSize(heap
, 0,
1393 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1394 if (retval
== 0xffffffff-4) retval
= 0;
1398 WARN("invalid handle\n");
1401 /* HeapUnlock(heap); */
1403 /* HeapSize returns 0xffffffff on failure */
1404 if (retval
== 0xffffffff) retval
= 0;
1409 /***********************************************************************
1410 * GlobalWire (KERNEL32.333)
1412 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1414 return GlobalLock( hmem
);
1418 /***********************************************************************
1419 * GlobalUnWire (KERNEL32.330)
1421 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1423 return GlobalUnlock( hmem
);
1427 /***********************************************************************
1428 * GlobalFix (KERNEL32.320)
1430 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1436 /***********************************************************************
1437 * GlobalUnfix (KERNEL32.331)
1439 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1441 GlobalUnlock( hmem
);
1445 /***********************************************************************
1446 * GlobalFlags (KERNEL32.321)
1447 * Returns information about the specified global memory object
1450 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1453 * Value specifying allocation flags and lock count
1454 * GMEM_INVALID_HANDLE: Failure
1456 UINT WINAPI
GlobalFlags(
1457 HGLOBAL hmem
/* [in] Handle to global memory object */
1460 PGLOBAL32_INTERN pintern
;
1468 /* HeapLock(GetProcessHeap()); */
1469 pintern
=HANDLE_TO_INTERN(hmem
);
1470 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1472 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1473 if(pintern
->Pointer
==0)
1474 retval
|= GMEM_DISCARDED
;
1478 WARN("Invalid handle: %04x", hmem
);
1481 /* HeapUnlock(GetProcessHeap()); */
1487 /***********************************************************************
1488 * GlobalCompact (KERNEL32.316)
1490 DWORD WINAPI
GlobalCompact( DWORD minfree
)
1492 return 0; /* GlobalCompact does nothing in Win32 */
1496 /***********************************************************************
1497 * GlobalMemoryStatus (KERNEL32.327)
1501 VOID WINAPI
GlobalMemoryStatus(
1502 LPMEMORYSTATUS lpmem
1504 static MEMORYSTATUS cached_memstatus
;
1505 static int cache_lastchecked
= 0;
1511 if (time(NULL
)==cache_lastchecked
) {
1512 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1515 cache_lastchecked
= time(NULL
);
1518 f
= fopen( "/proc/meminfo", "r" );
1522 int total
, used
, free
, shared
, buffers
, cached
;
1524 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1525 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1526 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1527 while (fgets( buffer
, sizeof(buffer
), f
))
1529 /* old style /proc/meminfo ... */
1530 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1532 lpmem
->dwTotalPhys
+= total
;
1533 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1535 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1537 lpmem
->dwTotalPageFile
+= total
;
1538 lpmem
->dwAvailPageFile
+= free
;
1541 /* new style /proc/meminfo ... */
1542 if (sscanf(buffer
, "MemTotal: %d", &total
))
1543 lpmem
->dwTotalPhys
= total
*1024;
1544 if (sscanf(buffer
, "MemFree: %d", &free
))
1545 lpmem
->dwAvailPhys
= free
*1024;
1546 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1547 lpmem
->dwTotalPageFile
= total
*1024;
1548 if (sscanf(buffer
, "SwapFree: %d", &free
))
1549 lpmem
->dwAvailPageFile
= free
*1024;
1550 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1551 lpmem
->dwAvailPhys
+= buffers
*1024;
1552 if (sscanf(buffer
, "Cached: %d", &cached
))
1553 lpmem
->dwAvailPhys
+= cached
*1024;
1557 if (lpmem
->dwTotalPhys
)
1559 DWORD TotalPhysical
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1560 DWORD AvailPhysical
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1561 lpmem
->dwMemoryLoad
= (TotalPhysical
-AvailPhysical
)
1562 / (TotalPhysical
/ 100);
1567 /* FIXME: should do something for other systems */
1568 lpmem
->dwMemoryLoad
= 0;
1569 lpmem
->dwTotalPhys
= 16*1024*1024;
1570 lpmem
->dwAvailPhys
= 16*1024*1024;
1571 lpmem
->dwTotalPageFile
= 16*1024*1024;
1572 lpmem
->dwAvailPageFile
= 16*1024*1024;
1575 lpmem
->dwTotalVirtual
= si
.lpMaximumApplicationAddress
-si
.lpMinimumApplicationAddress
;
1576 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1577 lpmem
->dwAvailVirtual
= lpmem
->dwTotalVirtual
-64*1024;
1578 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1581 /***********************************************************************
1582 * A20Proc16 (KERNEL.165)
1584 void WINAPI
A20Proc16( WORD unused
)
1586 /* this is also a NOP in Windows */
1589 /***********************************************************************
1590 * LimitEMSPages16 (KERNEL.156)
1592 DWORD WINAPI
LimitEMSPages16( DWORD unused
)