2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
15 #include "wine/winbase16.h"
19 #include "selectors.h"
21 #include "stackframe.h"
23 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(global
);
28 /* Global arena block */
31 DWORD base
; /* Base address (0 if discarded) */
32 DWORD size
; /* Size in bytes (0 indicates a free block) */
33 HGLOBAL16 handle
; /* Handle for this block */
34 HGLOBAL16 hOwner
; /* Owner of this block */
35 BYTE lockCount
; /* Count of GlobalFix() calls */
36 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
37 BYTE flags
; /* Allocation flags */
38 BYTE selCount
; /* Number of selectors allocated for this block */
41 /* Flags definitions */
42 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
43 #define GA_DGROUP 0x04
44 #define GA_DISCARDABLE 0x08
45 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
48 static GLOBALARENA
*pGlobalArena
= NULL
;
49 static int globalArenaSize
= 0;
51 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
53 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
54 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
56 /***********************************************************************
59 * Return the arena for a given selector, growing the arena array if needed.
61 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
63 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
65 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
66 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
67 newsize
* sizeof(GLOBALARENA
) );
68 if (!pNewArena
) return 0;
69 pGlobalArena
= pNewArena
;
70 memset( pGlobalArena
+ globalArenaSize
, 0,
71 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
72 globalArenaSize
= newsize
;
74 return pGlobalArena
+ (sel
>> __AHSHIFT
);
77 void debug_handles(void)
81 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
82 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
84 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
92 /***********************************************************************
95 * Create a global heap block for a fixed range of linear memory.
97 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
98 HGLOBAL16 hOwner
, BOOL16 isCode
,
99 BOOL16 is32Bit
, BOOL16 isReadOnly
,
105 /* Allocate the selector(s) */
107 sel
= SELECTOR_AllocBlock( ptr
, size
,
108 isCode
? SEGMENT_CODE
: SEGMENT_DATA
,
109 is32Bit
, isReadOnly
);
112 selcount
= (size
+ 0xffff) / 0x10000;
114 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
116 SELECTOR_FreeBlock( sel
, selcount
);
120 /* Fill the arena block */
122 pArena
->base
= (DWORD
)ptr
;
123 pArena
->size
= GET_SEL_LIMIT(sel
) + 1;
124 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
125 pArena
->hOwner
= hOwner
;
126 pArena
->lockCount
= 0;
127 pArena
->pageLockCount
= 0;
128 pArena
->flags
= flags
& GA_MOVEABLE
;
129 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
130 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
131 if (!isCode
) pArena
->flags
|= GA_DGROUP
;
132 pArena
->selCount
= selcount
;
133 if (selcount
> 1) /* clear the next arena blocks */
134 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
136 return pArena
->handle
;
140 /***********************************************************************
143 * Free a block allocated by GLOBAL_CreateBlock, without touching
144 * the associated linear memory range.
146 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
151 if (!handle
) return TRUE
;
152 sel
= GlobalHandleToSel16( handle
);
153 if (!VALID_HANDLE(sel
))
155 pArena
= GET_ARENA_PTR(sel
);
156 SELECTOR_FreeBlock( sel
, (pArena
->size
+ 0xffff) / 0x10000 );
157 memset( pArena
, 0, sizeof(GLOBALARENA
) );
161 /***********************************************************************
164 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
169 if (!handle
) return TRUE
;
170 sel
= GlobalHandleToSel16( handle
);
171 if (!VALID_HANDLE(sel
))
173 pArena
= GET_ARENA_PTR(sel
);
174 if (pArena
->selCount
!= 1)
177 pArena
->base
= (DWORD
)ptr
;
180 SELECTOR_MoveBlock( sel
, ptr
);
181 SetSelectorLimit16( sel
, size
-1 );
186 /***********************************************************************
189 * Implementation of GlobalAlloc16()
191 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
,
192 BOOL16 isCode
, BOOL16 is32Bit
, BOOL16 isReadOnly
)
198 TRACE("%ld flags=%04x\n", size
, flags
);
200 /* If size is 0, create a discarded block */
202 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, isCode
,
203 is32Bit
, isReadOnly
, NULL
);
207 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
208 size
= (size
+ 0x1f) & ~0x1f;
210 /* Allocate the linear memory */
211 ptr
= HeapAlloc( GetProcessHeap(), 0, size
);
212 /* FIXME: free discardable blocks and try again? */
215 /* Allocate the selector(s) */
217 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
,
218 isCode
, is32Bit
, isReadOnly
, &shmdata
);
221 HeapFree( GetProcessHeap(), 0, ptr
);
225 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
229 /***********************************************************************
230 * GlobalAlloc16 (KERNEL.15)
235 HGLOBAL16 WINAPI
GlobalAlloc16(
236 UINT16 flags
, /* [in] Object allocation attributes */
237 DWORD size
/* [in] Number of bytes to allocate */
239 HANDLE16 owner
= GetCurrentPDB16();
241 if (flags
& GMEM_DDESHARE
)
242 owner
= GetExePtr(owner
); /* Make it a module handle */
243 return GLOBAL_Alloc( flags
, size
, owner
, FALSE
, FALSE
, FALSE
);
247 /***********************************************************************
248 * GlobalReAlloc16 (KERNEL.16)
253 HGLOBAL16 WINAPI
GlobalReAlloc16(
254 HGLOBAL16 handle
, /* [in] Handle of global memory object */
255 DWORD size
, /* [in] New size of block */
256 UINT16 flags
/* [in] How to reallocate object */
261 GLOBALARENA
*pArena
, *pNewArena
;
262 WORD sel
= GlobalHandleToSel16( handle
);
264 TRACE("%04x %ld flags=%04x\n",
265 handle
, size
, flags
);
266 if (!handle
) return 0;
268 if (!VALID_HANDLE(handle
)) {
269 WARN("Invalid handle 0x%04x!\n", handle
);
272 pArena
= GET_ARENA_PTR( handle
);
274 /* Discard the block if requested */
276 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
278 if (!(pArena
->flags
& GA_MOVEABLE
) ||
279 !(pArena
->flags
& GA_DISCARDABLE
) ||
280 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
281 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
284 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
285 * change the selector if we are shrinking the block.
286 * FIXME: shouldn't we keep selectors until the block is deleted?
288 SELECTOR_ReallocBlock( sel
, 0, 1 );
294 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
295 if (size
== 0) size
= 0x20;
296 else size
= (size
+ 0x1f) & ~0x1f;
298 /* Change the flags */
300 if (flags
& GMEM_MODIFY
)
302 /* Change the flags, leaving GA_DGROUP alone */
303 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
304 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
308 /* Reallocate the linear memory */
310 ptr
= (void *)pArena
->base
;
311 oldsize
= pArena
->size
;
312 TRACE("oldsize %08lx\n",oldsize
);
313 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
315 if (((char *)ptr
>= DOSMEM_MemoryBase()) &&
316 ((char *)ptr
<= DOSMEM_MemoryBase() + 0x100000))
317 ptr
= DOSMEM_ResizeBlock(ptr
, size
, NULL
);
319 ptr
= HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
322 SELECTOR_FreeBlock( sel
, (oldsize
+ 0xffff) / 0x10000 );
323 memset( pArena
, 0, sizeof(GLOBALARENA
) );
327 /* Reallocate the selector(s) */
329 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
332 HeapFree( GetProcessHeap(), 0, ptr
);
333 memset( pArena
, 0, sizeof(GLOBALARENA
) );
336 selcount
= (size
+ 0xffff) / 0x10000;
338 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
340 HeapFree( GetProcessHeap(), 0, ptr
);
341 SELECTOR_FreeBlock( sel
, selcount
);
345 /* Fill the new arena block */
347 if (pNewArena
!= pArena
) memcpy( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
348 pNewArena
->base
= (DWORD
)ptr
;
349 pNewArena
->size
= GET_SEL_LIMIT(sel
) + 1;
350 pNewArena
->selCount
= selcount
;
351 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
353 if (selcount
> 1) /* clear the next arena blocks */
354 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
356 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
357 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
358 return pNewArena
->handle
;
362 /***********************************************************************
363 * GlobalFree16 (KERNEL.17)
368 HGLOBAL16 WINAPI
GlobalFree16(
369 HGLOBAL16 handle
/* [in] Handle of global memory object */
373 if (!VALID_HANDLE(handle
)) {
374 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
377 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
379 TRACE("%04x\n", handle
);
380 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
381 if (ptr
) HeapFree( GetProcessHeap(), 0, ptr
);
386 /***********************************************************************
387 * WIN16_GlobalLock16 (KERNEL.18)
389 * This is the GlobalLock16() function used by 16-bit code.
391 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
393 WORD sel
= GlobalHandleToSel16( handle
);
394 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
398 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
400 if (!VALID_HANDLE(handle
)) {
401 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
404 else if (!GET_ARENA_PTR(handle
)->base
)
407 GET_ARENA_PTR(handle
)->lockCount
++;
410 CURRENT_STACK16
->ecx
= sel
; /* selector must be returned in CX as well */
411 return PTR_SEG_OFF_TO_SEGPTR( sel
, 0 );
415 /***********************************************************************
416 * GlobalLock16 (KERNEL.18)
418 * This is the GlobalLock16() function used by 32-bit code.
421 * Pointer to first byte of memory block
424 LPVOID WINAPI
GlobalLock16(
425 HGLOBAL16 handle
/* [in] Handle of global memory object */
427 if (!handle
) return 0;
428 if (!VALID_HANDLE(handle
))
430 GET_ARENA_PTR(handle
)->lockCount
++;
431 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
435 /***********************************************************************
436 * GlobalUnlock16 (KERNEL.19)
438 * Should the return values be cast to booleans?
441 * TRUE: Object is still locked
442 * FALSE: Object is unlocked
444 BOOL16 WINAPI
GlobalUnlock16(
445 HGLOBAL16 handle
/* [in] Handle of global memory object */
447 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
448 if (!VALID_HANDLE(handle
)) {
449 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
452 TRACE("%04x\n", handle
);
453 if (pArena
->lockCount
) pArena
->lockCount
--;
454 return pArena
->lockCount
;
457 /***********************************************************************
458 * GlobalChangeLockCount (KERNEL.365)
460 * This is declared as a register function as it has to preserve
461 * *all* registers, even AX/DX !
464 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
468 GlobalLock16( handle
);
469 else if ( delta
== -1 )
470 GlobalUnlock16( handle
);
472 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
475 /***********************************************************************
476 * GlobalSize16 (KERNEL.20)
478 * Size in bytes of object
481 DWORD WINAPI
GlobalSize16(
482 HGLOBAL16 handle
/* [in] Handle of global memory object */
484 TRACE("%04x\n", handle
);
485 if (!handle
) return 0;
486 if (!VALID_HANDLE(handle
))
488 return GET_ARENA_PTR(handle
)->size
;
492 /***********************************************************************
493 * GlobalHandle16 (KERNEL.21)
495 * Why is GlobalHandleToSel used here with the sel as input?
501 DWORD WINAPI
GlobalHandle16(
502 WORD sel
/* [in] Address of global memory block */
504 TRACE("%04x\n", sel
);
505 if (!VALID_HANDLE(sel
)) {
506 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
509 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
512 /***********************************************************************
513 * GlobalHandleNoRIP (KERNEL.159)
515 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
518 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
519 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
520 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
526 /***********************************************************************
527 * GlobalFlags16 (KERNEL.22)
529 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
533 * Value specifying flags and lock count
534 * GMEM_INVALID_HANDLE: Invalid handle
536 UINT16 WINAPI
GlobalFlags16(
537 HGLOBAL16 handle
/* [in] Handle of global memory object */
541 TRACE("%04x\n", handle
);
542 if (!VALID_HANDLE(handle
)) {
543 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
546 pArena
= GET_ARENA_PTR(handle
);
547 return pArena
->lockCount
|
548 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
549 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
553 /***********************************************************************
554 * LockSegment16 (KERNEL.23)
556 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
558 TRACE("%04x\n", handle
);
559 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
560 if (!VALID_HANDLE(handle
)) {
561 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
564 GET_ARENA_PTR(handle
)->lockCount
++;
569 /***********************************************************************
570 * UnlockSegment16 (KERNEL.24)
572 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
574 TRACE("%04x\n", handle
);
575 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
576 if (!VALID_HANDLE(handle
)) {
577 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
580 GET_ARENA_PTR(handle
)->lockCount
--;
581 /* FIXME: this ought to return the lock count in CX (go figure...) */
585 /***********************************************************************
586 * GlobalCompact16 (KERNEL.25)
588 DWORD WINAPI
GlobalCompact16( DWORD desired
)
590 return GLOBAL_MAX_ALLOC_SIZE
;
594 /***********************************************************************
595 * GlobalFreeAll (KERNEL.26)
597 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
602 pArena
= pGlobalArena
;
603 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
605 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
606 GlobalFree16( pArena
->handle
);
611 /***********************************************************************
612 * GlobalWire16 (KERNEL.111)
614 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
616 return WIN16_GlobalLock16( handle
);
620 /***********************************************************************
621 * GlobalUnWire16 (KERNEL.112)
623 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
625 return !GlobalUnlock16( handle
);
629 /***********************************************************************
630 * SetSwapAreaSize16 (KERNEL.106)
632 LONG WINAPI
SetSwapAreaSize16( WORD size
)
634 FIXME("(%d) - stub!\n", size
);
635 return MAKELONG( size
, 0xffff );
639 /***********************************************************************
640 * GlobalLRUOldest (KERNEL.163)
642 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
644 TRACE("%04x\n", handle
);
645 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
650 /***********************************************************************
651 * GlobalLRUNewest (KERNEL.164)
653 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
655 TRACE("%04x\n", handle
);
656 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
661 /***********************************************************************
662 * GetFreeSpace16 (KERNEL.169)
664 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
667 GlobalMemoryStatus( &ms
);
668 return ms
.dwAvailVirtual
;
671 /***********************************************************************
672 * GlobalDOSAlloc (KERNEL.184)
674 * Address (HW=Paragraph segment; LW=Selector)
676 DWORD WINAPI
GlobalDOSAlloc16(
677 DWORD size
/* [in] Number of bytes to be allocated */
680 LPVOID lpBlock
= DOSMEM_GetBlock( size
, &uParagraph
);
684 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
687 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
,
688 hModule
, 0, 0, 0, NULL
);
689 return MAKELONG(wSelector
,uParagraph
);
695 /***********************************************************************
696 * GlobalDOSFree (KERNEL.185)
701 WORD WINAPI
GlobalDOSFree16(
702 WORD sel
/* [in] Selector */
704 DWORD block
= GetSelectorBase(sel
);
706 if( block
&& block
< 0x100000 )
708 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
709 if( DOSMEM_FreeBlock( lpBlock
) )
710 GLOBAL_FreeBlock( sel
);
717 /***********************************************************************
718 * GlobalPageLock (KERNEL.191)
720 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
722 TRACE("%04x\n", handle
);
723 if (!VALID_HANDLE(handle
)) {
724 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
727 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
731 /***********************************************************************
732 * GlobalPageUnlock (KERNEL.192)
734 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
736 TRACE("%04x\n", handle
);
737 if (!VALID_HANDLE(handle
)) {
738 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
741 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
745 /***********************************************************************
746 * GlobalFix16 (KERNEL.197)
748 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
750 TRACE("%04x\n", handle
);
751 if (!VALID_HANDLE(handle
)) {
752 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
755 GET_ARENA_PTR(handle
)->lockCount
++;
757 return GlobalHandleToSel16(handle
);
761 /***********************************************************************
762 * GlobalUnfix16 (KERNEL.198)
764 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
766 TRACE("%04x\n", handle
);
767 if (!VALID_HANDLE(handle
)) {
768 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
771 GET_ARENA_PTR(handle
)->lockCount
--;
775 /***********************************************************************
776 * FarSetOwner (KERNEL.403)
778 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
780 if (!VALID_HANDLE(handle
)) {
781 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
784 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
788 /***********************************************************************
789 * FarGetOwner (KERNEL.404)
791 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
793 if (!VALID_HANDLE(handle
)) {
794 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
797 return GET_ARENA_PTR(handle
)->hOwner
;
801 /***********************************************************************
802 * GlobalHandleToSel (TOOLHELP.50)
804 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
806 if (!handle
) return 0;
807 if (!VALID_HANDLE(handle
)) {
808 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
813 WARN("Program attempted invalid selector conversion\n" );
820 /***********************************************************************
821 * GlobalFirst (TOOLHELP.51)
823 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
825 if (wFlags
== GLOBAL_LRU
) return FALSE
;
827 return GlobalNext16( pGlobal
, wFlags
);
831 /***********************************************************************
832 * GlobalNext (TOOLHELP.52)
834 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
838 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
839 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
840 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
843 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
844 if (pArena
->size
== 0) break; /* block is free */
845 if (i
>= globalArenaSize
) return FALSE
;
849 pGlobal
->dwAddress
= pArena
->base
;
850 pGlobal
->dwBlockSize
= pArena
->size
;
851 pGlobal
->hBlock
= pArena
->handle
;
852 pGlobal
->wcLock
= pArena
->lockCount
;
853 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
854 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
855 pGlobal
->wHeapPresent
= FALSE
;
856 pGlobal
->hOwner
= pArena
->hOwner
;
857 pGlobal
->wType
= GT_UNKNOWN
;
864 /***********************************************************************
865 * GlobalInfo (TOOLHELP.53)
867 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
872 pInfo
->wcItems
= globalArenaSize
;
873 pInfo
->wcItemsFree
= 0;
874 pInfo
->wcItemsLRU
= 0;
875 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
876 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
881 /***********************************************************************
882 * GlobalEntryHandle (TOOLHELP.54)
884 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
886 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
888 pGlobal
->dwAddress
= pArena
->base
;
889 pGlobal
->dwBlockSize
= pArena
->size
;
890 pGlobal
->hBlock
= pArena
->handle
;
891 pGlobal
->wcLock
= pArena
->lockCount
;
892 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
893 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
894 pGlobal
->wHeapPresent
= FALSE
;
895 pGlobal
->hOwner
= pArena
->hOwner
;
896 pGlobal
->wType
= GT_UNKNOWN
;
903 /***********************************************************************
904 * GlobalEntryModule (TOOLHELP.55)
906 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
913 /***********************************************************************
914 * MemManInfo (TOOLHELP.72)
916 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
921 * Not unsurprisingly although the documention says you
922 * _must_ provide the size in the dwSize field, this function
923 * (under Windows) always fills the structure and returns true.
925 GlobalMemoryStatus( &status
);
926 info
->wPageSize
= VIRTUAL_GetPageSize();
927 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
928 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
929 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
930 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
931 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
932 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
933 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
934 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
935 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
939 /***********************************************************************
940 * GetFreeMemInfo (KERNEL.316)
942 DWORD WINAPI
GetFreeMemInfo16(void)
945 MemManInfo16( &info
);
946 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
950 * Win32 Global heap functions (GlobalXXX).
951 * These functions included in Win32 for compatibility with 16 bit Windows
952 * Especially the moveable blocks and handles are oldish.
953 * But the ability to directly allocate memory with GPTR and LPTR is widely
956 * The handle stuff looks horrible, but it's implemented almost like Win95
961 #define MAGIC_GLOBAL_USED 0x5342
962 #define GLOBAL_LOCK_MAX 0xFF
963 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
964 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
965 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
966 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
967 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
969 typedef struct __GLOBAL32_INTERN
972 LPVOID Pointer WINE_PACKED
;
975 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
977 /***********************************************************************
980 * Returns the appropriate heap to be used. If the object was created
981 * With GMEM_DDESHARE we allocated it on the system heap.
983 static HANDLE
GLOBAL_GetHeap( HGLOBAL hmem
)
987 TRACE("() hmem=%x\n", hmem
);
989 /* Get the appropriate heap to be used for this object */
991 heap
= GetProcessHeap();
994 PGLOBAL32_INTERN pintern
;
995 pintern
=HANDLE_TO_INTERN(hmem
);
997 /* If it was DDESHARE it was created on the shared system heap */
998 pintern
=HANDLE_TO_INTERN(hmem
);
999 heap
= ( pintern
->Flags
& (GMEM_DDESHARE
>> 8) )
1000 ? SystemHeap
: GetProcessHeap();
1006 /***********************************************************************
1007 * GlobalAlloc (KERNEL32.315)
1012 HGLOBAL WINAPI
GlobalAlloc(
1013 UINT flags
, /* [in] Object allocation attributes */
1014 DWORD size
/* [in] Number of bytes to allocate */
1016 PGLOBAL32_INTERN pintern
;
1020 if(flags
&GMEM_ZEROINIT
)
1021 hpflags
=HEAP_ZERO_MEMORY
;
1025 TRACE("() flags=%04x\n", flags
);
1027 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1029 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1030 return (HGLOBAL
) palloc
;
1036 /* If DDESHARE is set, create on the shared system heap */
1037 heap
= (flags
& GMEM_DDESHARE
) ? SystemHeap
: GetProcessHeap();
1039 /* HeapLock(heap); */
1041 pintern
=HeapAlloc(heap
, 0, sizeof(GLOBAL32_INTERN
));
1042 if (!pintern
) return NULL
;
1045 if (!(palloc
=HeapAlloc(heap
, hpflags
, size
+sizeof(HGLOBAL
)))) {
1046 HeapFree(heap
, 0, pintern
);
1049 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1050 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1053 pintern
->Pointer
=NULL
;
1054 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1055 pintern
->Flags
=flags
>>8;
1056 pintern
->LockCount
=0;
1058 /* HeapUnlock(heap); */
1060 return INTERN_TO_HANDLE(pintern
);
1065 /***********************************************************************
1066 * GlobalLock (KERNEL32.326)
1068 * Pointer to first byte of block
1071 LPVOID WINAPI
GlobalLock(
1072 HGLOBAL hmem
/* [in] Handle of global memory object */
1074 PGLOBAL32_INTERN pintern
;
1078 return (LPVOID
) hmem
;
1080 /* HeapLock(GetProcessHeap()); */
1082 pintern
=HANDLE_TO_INTERN(hmem
);
1083 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1085 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1086 pintern
->LockCount
++;
1087 palloc
=pintern
->Pointer
;
1091 WARN("invalid handle\n");
1092 palloc
=(LPVOID
) NULL
;
1094 /* HeapUnlock(GetProcessHeap()); */;
1099 /***********************************************************************
1100 * GlobalUnlock (KERNEL32.332)
1102 * TRUE: Object is still locked
1103 * FALSE: Object is unlocked
1105 BOOL WINAPI
GlobalUnlock(
1106 HGLOBAL hmem
/* [in] Handle of global memory object */
1108 PGLOBAL32_INTERN pintern
;
1114 /* HeapLock(GetProcessHeap()); */
1115 pintern
=HANDLE_TO_INTERN(hmem
);
1117 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1119 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1120 pintern
->LockCount
--;
1122 locked
=(pintern
->LockCount
==0) ? FALSE
: TRUE
;
1126 WARN("invalid handle\n");
1129 /* HeapUnlock(GetProcessHeap()); */
1134 /***********************************************************************
1135 * GlobalHandle (KERNEL32.325)
1136 * Returns the handle associated with the specified pointer.
1139 * Since there in only one goto, can it be removed and the return
1146 HGLOBAL WINAPI
GlobalHandle(
1147 LPCVOID pmem
/* [in] Pointer to global memory block */
1151 PGLOBAL32_INTERN maybe_intern
;
1156 SetLastError( ERROR_INVALID_PARAMETER
);
1160 /* note that if pmem is a pointer to a a block allocated by */
1161 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1163 if (ISPOINTER(pmem
)) {
1164 heap
= GLOBAL_GetHeap( (HGLOBAL
)pmem
);
1165 if (HeapValidate( heap
, 0, pmem
))
1166 return (HGLOBAL
)pmem
; /* valid fixed block */
1167 handle
= POINTER_TO_HANDLE(pmem
);
1169 handle
= (HGLOBAL
)pmem
;
1171 /* Now test handle either passed in or retrieved from pointer */
1172 heap
= GLOBAL_GetHeap( handle
);
1173 maybe_intern
= HANDLE_TO_INTERN( handle
);
1174 if (maybe_intern
->Magic
== MAGIC_GLOBAL_USED
) {
1175 test
= maybe_intern
->Pointer
;
1176 if (HeapValidate( heap
, 0, ((HGLOBAL
*)test
)-1 ) &&
1177 /* obj(-handle) valid arena? */
1178 HeapValidate( heap
, 0, maybe_intern
)) /* intern valid arena? */
1179 return handle
; /* valid moveable block */
1182 SetLastError( ERROR_INVALID_HANDLE
);
1187 /***********************************************************************
1188 * GlobalReAlloc (KERNEL32.328)
1193 HGLOBAL WINAPI
GlobalReAlloc(
1194 HGLOBAL hmem
, /* [in] Handle of global memory object */
1195 DWORD size
, /* [in] New size of block */
1196 UINT flags
/* [in] How to reallocate object */
1200 PGLOBAL32_INTERN pintern
;
1201 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1202 DWORD heap_flags
= (flags
& GMEM_ZEROINIT
) ? HEAP_ZERO_MEMORY
: 0;
1205 /* HeapLock(heap); */
1206 if(flags
& GMEM_MODIFY
) /* modify flags */
1208 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1210 /* make a fixed block moveable
1211 * actually only NT is able to do this. But it's soo simple
1215 ERR("GlobalReAlloc32 with null handle!\n");
1216 SetLastError( ERROR_NOACCESS
);
1219 size
=HeapSize(heap
, 0, (LPVOID
) hmem
);
1220 hnew
=GlobalAlloc( flags
, size
);
1221 palloc
=GlobalLock(hnew
);
1222 memcpy(palloc
, (LPVOID
) hmem
, size
);
1226 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1228 /* change the flags to make our block "discardable" */
1229 pintern
=HANDLE_TO_INTERN(hmem
);
1230 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1235 SetLastError(ERROR_INVALID_PARAMETER
);
1243 /* reallocate fixed memory */
1244 hnew
=(HGLOBAL
)HeapReAlloc(heap
, heap_flags
, (LPVOID
) hmem
, size
);
1248 /* reallocate a moveable block */
1249 pintern
=HANDLE_TO_INTERN(hmem
);
1250 if(pintern
->LockCount
>1) {
1251 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1252 SetLastError(ERROR_INVALID_HANDLE
);
1256 if(pintern
->Pointer
)
1258 if((palloc
= HeapReAlloc(heap
, heap_flags
,
1259 (char *) pintern
->Pointer
-sizeof(HGLOBAL
),
1260 size
+sizeof(HGLOBAL
))) == NULL
)
1261 return 0; /* Block still valid */
1262 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1266 if((palloc
=HeapAlloc(heap
, heap_flags
, size
+sizeof(HGLOBAL
)))
1269 *(HGLOBAL
*)palloc
=hmem
;
1270 pintern
->Pointer
=(char *) palloc
+sizeof(HGLOBAL
);
1275 if(pintern
->Pointer
)
1277 HeapFree(heap
, 0, (char *) pintern
->Pointer
-sizeof(HGLOBAL
));
1278 pintern
->Pointer
=NULL
;
1283 /* HeapUnlock(heap); */
1288 /***********************************************************************
1289 * GlobalFree (KERNEL32.322)
1294 HGLOBAL WINAPI
GlobalFree(
1295 HGLOBAL hmem
/* [in] Handle of global memory object */
1297 PGLOBAL32_INTERN pintern
;
1298 HGLOBAL hreturned
= 0;
1299 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1301 if(ISPOINTER(hmem
)) /* POINTER */
1303 if(!HeapFree(heap
, 0, (LPVOID
) hmem
)) hmem
= 0;
1307 /* HeapLock(heap); */
1308 pintern
=HANDLE_TO_INTERN(hmem
);
1310 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1313 /* WIN98 does not make this test. That is you can free a */
1314 /* block you have not unlocked. Go figure!! */
1315 /* if(pintern->LockCount!=0) */
1316 /* SetLastError(ERROR_INVALID_HANDLE); */
1318 if(pintern
->Pointer
)
1319 if(!HeapFree(heap
, 0,
1320 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1322 if(!HeapFree(heap
, 0, pintern
))
1325 /* HeapUnlock(heap); */
1331 /***********************************************************************
1332 * GlobalSize (KERNEL32.329)
1334 * Size in bytes of the global memory object
1337 DWORD WINAPI
GlobalSize(
1338 HGLOBAL hmem
/* [in] Handle of global memory object */
1341 PGLOBAL32_INTERN pintern
;
1342 HANDLE heap
= GLOBAL_GetHeap( hmem
);
1346 retval
=HeapSize(heap
, 0, (LPVOID
) hmem
);
1350 /* HeapLock(heap); */
1351 pintern
=HANDLE_TO_INTERN(hmem
);
1353 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1355 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1357 retval
=HeapSize(heap
, 0,
1358 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1359 if (retval
== 0xffffffff-4) retval
= 0;
1363 WARN("invalid handle\n");
1366 /* HeapUnlock(heap); */
1368 /* HeapSize returns 0xffffffff on failure */
1369 if (retval
== 0xffffffff) retval
= 0;
1374 /***********************************************************************
1375 * GlobalWire (KERNEL32.333)
1377 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1379 return GlobalLock( hmem
);
1383 /***********************************************************************
1384 * GlobalUnWire (KERNEL32.330)
1386 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1388 return GlobalUnlock( hmem
);
1392 /***********************************************************************
1393 * GlobalFix (KERNEL32.320)
1395 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1401 /***********************************************************************
1402 * GlobalUnfix (KERNEL32.331)
1404 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1406 GlobalUnlock( hmem
);
1410 /***********************************************************************
1411 * GlobalFlags (KERNEL32.321)
1412 * Returns information about the specified global memory object
1415 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1418 * Value specifying allocation flags and lock count
1419 * GMEM_INVALID_HANDLE: Failure
1421 UINT WINAPI
GlobalFlags(
1422 HGLOBAL hmem
/* [in] Handle to global memory object */
1425 PGLOBAL32_INTERN pintern
;
1433 /* HeapLock(GetProcessHeap()); */
1434 pintern
=HANDLE_TO_INTERN(hmem
);
1435 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1437 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1438 if(pintern
->Pointer
==0)
1439 retval
|= GMEM_DISCARDED
;
1443 WARN("Invalid handle: %04x", hmem
);
1446 /* HeapUnlock(GetProcessHeap()); */
1452 /***********************************************************************
1453 * GlobalCompact (KERNEL32.316)
1455 DWORD WINAPI
GlobalCompact( DWORD minfree
)
1457 return 0; /* GlobalCompact does nothing in Win32 */
1461 /***********************************************************************
1462 * GlobalMemoryStatus (KERNEL32.327)
1466 VOID WINAPI
GlobalMemoryStatus(
1467 LPMEMORYSTATUS lpmem
1469 static MEMORYSTATUS cached_memstatus
;
1470 static int cache_lastchecked
= 0;
1475 if (time(NULL
)==cache_lastchecked
) {
1476 memcpy(lpmem
,&cached_memstatus
,sizeof(MEMORYSTATUS
));
1479 cache_lastchecked
= time(NULL
);
1482 f
= fopen( "/proc/meminfo", "r" );
1486 int total
, used
, free
, shared
, buffers
, cached
;
1488 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1489 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1490 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1491 while (fgets( buffer
, sizeof(buffer
), f
))
1493 /* old style /proc/meminfo ... */
1494 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1496 lpmem
->dwTotalPhys
+= total
;
1497 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1499 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1501 lpmem
->dwTotalPageFile
+= total
;
1502 lpmem
->dwAvailPageFile
+= free
;
1505 /* new style /proc/meminfo ... */
1506 if (sscanf(buffer
, "MemTotal: %d", &total
))
1507 lpmem
->dwTotalPhys
= total
*1024;
1508 if (sscanf(buffer
, "MemFree: %d", &free
))
1509 lpmem
->dwAvailPhys
= free
*1024;
1510 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1511 lpmem
->dwTotalPageFile
= total
*1024;
1512 if (sscanf(buffer
, "SwapFree: %d", &free
))
1513 lpmem
->dwAvailPageFile
= free
*1024;
1514 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1515 lpmem
->dwAvailPhys
+= buffers
*1024;
1516 if (sscanf(buffer
, "Cached: %d", &cached
))
1517 lpmem
->dwAvailPhys
+= cached
*1024;
1521 if (lpmem
->dwTotalPhys
)
1523 lpmem
->dwTotalVirtual
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1524 lpmem
->dwAvailVirtual
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1525 lpmem
->dwMemoryLoad
= (lpmem
->dwTotalVirtual
-lpmem
->dwAvailVirtual
)
1526 / (lpmem
->dwTotalVirtual
/ 100);
1531 /* FIXME: should do something for other systems */
1532 lpmem
->dwMemoryLoad
= 0;
1533 lpmem
->dwTotalPhys
= 16*1024*1024;
1534 lpmem
->dwAvailPhys
= 16*1024*1024;
1535 lpmem
->dwTotalPageFile
= 16*1024*1024;
1536 lpmem
->dwAvailPageFile
= 16*1024*1024;
1537 lpmem
->dwTotalVirtual
= 32*1024*1024;
1538 lpmem
->dwAvailVirtual
= 32*1024*1024;
1540 memcpy(&cached_memstatus
,lpmem
,sizeof(MEMORYSTATUS
));
1543 /***********************************************************************
1544 * A20Proc16 (KERNEL.165)
1546 void WINAPI
A20Proc16( WORD unused
)
1548 /* this is also a NOP in Windows */
1551 /***********************************************************************
1552 * LimitEMSPages16 (KERNEL.156)
1554 DWORD WINAPI
LimitEMSPages16( DWORD unused
)