2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
17 #include "selectors.h"
20 #include "stackframe.h"
25 /* Global arena block */
28 DWORD base
; /* Base address (0 if discarded) */
29 DWORD size
; /* Size in bytes (0 indicates a free block) */
30 HGLOBAL16 handle
; /* Handle for this block */
31 HGLOBAL16 hOwner
; /* Owner of this block */
32 BYTE lockCount
; /* Count of GlobalFix() calls */
33 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
34 BYTE flags
; /* Allocation flags */
35 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
);
82 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
83 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
85 DUMP("0x%08x, ",pGlobalArena
[i
].handle
);
93 /***********************************************************************
96 * Create a global heap block for a fixed range of linear memory.
98 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
99 HGLOBAL16 hOwner
, BOOL16 isCode
,
100 BOOL16 is32Bit
, BOOL16 isReadOnly
,
106 /* Allocate the selector(s) */
108 sel
= SELECTOR_AllocBlock( ptr
, size
,
109 isCode
? SEGMENT_CODE
: SEGMENT_DATA
,
110 is32Bit
, isReadOnly
);
113 selcount
= (size
+ 0xffff) / 0x10000;
115 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
117 SELECTOR_FreeBlock( sel
, selcount
);
121 /* Fill the arena block */
123 pArena
->base
= (DWORD
)ptr
;
124 pArena
->size
= GET_SEL_LIMIT(sel
) + 1;
127 if (flags
& GMEM_DDESHARE
)
129 pArena
->handle
= shmdata
->handle
;
130 pArena
->shmid
= shmdata
->shmid
;
135 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
139 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
141 pArena
->hOwner
= hOwner
;
142 pArena
->lockCount
= 0;
143 pArena
->pageLockCount
= 0;
144 pArena
->flags
= flags
& GA_MOVEABLE
;
145 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
146 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
147 if (!isCode
) pArena
->flags
|= GA_DGROUP
;
148 pArena
->selCount
= selcount
;
149 if (selcount
> 1) /* clear the next arena blocks */
150 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
152 return pArena
->handle
;
156 /***********************************************************************
159 * Free a block allocated by GLOBAL_CreateBlock, without touching
160 * the associated linear memory range.
162 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
167 if (!handle
) return TRUE
;
168 sel
= GlobalHandleToSel( handle
);
169 if (!VALID_HANDLE(sel
))
171 pArena
= GET_ARENA_PTR(sel
);
172 SELECTOR_FreeBlock( sel
, (pArena
->size
+ 0xffff) / 0x10000 );
173 memset( pArena
, 0, sizeof(GLOBALARENA
) );
177 /***********************************************************************
180 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
185 if (!handle
) return TRUE
;
186 sel
= GlobalHandleToSel( handle
);
187 if (!VALID_HANDLE(sel
))
189 pArena
= GET_ARENA_PTR(sel
);
190 if (pArena
->selCount
!= 1)
193 pArena
->base
= (DWORD
)ptr
;
196 SELECTOR_MoveBlock( sel
, ptr
);
197 SetSelectorLimit( sel
, size
-1 );
202 /***********************************************************************
205 * Implementation of GlobalAlloc16()
207 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
,
208 BOOL16 isCode
, BOOL16 is32Bit
, BOOL16 isReadOnly
)
214 TRACE(global
, "%ld flags=%04x\n", size
, flags
);
216 /* If size is 0, create a discarded block */
218 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, isCode
,
219 is32Bit
, isReadOnly
, NULL
);
223 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
224 size
= (size
+ 0x1f) & ~0x1f;
226 /* Allocate the linear memory */
229 if (flags
& GMEM_DDESHARE
)
230 ptr
= DDE_malloc(flags
, size
, &shmdata
);
232 #endif /* CONFIG_IPC */
234 ptr
= HeapAlloc( SystemHeap
, 0, size
);
236 /* FIXME: free discardable blocks and try again? */
239 /* Allocate the selector(s) */
241 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
,
242 isCode
, is32Bit
, isReadOnly
, &shmdata
);
245 HeapFree( SystemHeap
, 0, ptr
);
249 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
255 /***********************************************************************
258 * Find the arena for a given handle
259 * (when handle is not serial - e.g. DDE)
261 static GLOBALARENA
*GLOBAL_FindArena( HGLOBAL16 handle
)
264 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
265 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== handle
)
266 return ( &pGlobalArena
[i
] );
272 /***********************************************************************
273 * DDE_GlobalHandleToSel
276 WORD
DDE_GlobalHandleToSel( HGLOBAL16 handle
)
281 pArena
= GLOBAL_FindArena(handle
);
283 int ArenaIdx
= pArena
- pGlobalArena
;
285 /* See if synchronized to the shared memory */
286 return DDE_SyncHandle(handle
, ( ArenaIdx
<< __AHSHIFT
) | 7);
289 /* attach the block */
290 DDE_AttachHandle(handle
, &segptr
);
292 return SELECTOROF( segptr
);
294 #endif /* CONFIG_IPC */
297 /***********************************************************************
298 * GlobalAlloc16 (KERNEL.15)
303 HGLOBAL16 WINAPI
GlobalAlloc16(
304 UINT16 flags
, /* [in] Object allocation attributes */
305 DWORD size
/* [in] Number of bytes to allocate */
307 HANDLE16 owner
= GetCurrentPDB();
309 if (flags
& GMEM_DDESHARE
)
310 owner
= GetExePtr(owner
); /* Make it a module handle */
311 return GLOBAL_Alloc( flags
, size
, owner
, FALSE
, FALSE
, FALSE
);
315 /***********************************************************************
316 * GlobalReAlloc16 (KERNEL.16)
321 HGLOBAL16 WINAPI
GlobalReAlloc16(
322 HGLOBAL16 handle
, /* [in] Handle of global memory object */
323 DWORD size
, /* [in] New size of block */
324 UINT16 flags
/* [in] How to reallocate object */
329 GLOBALARENA
*pArena
, *pNewArena
;
330 WORD sel
= GlobalHandleToSel( handle
);
332 TRACE(global
, "%04x %ld flags=%04x\n",
333 handle
, size
, flags
);
334 if (!handle
) return 0;
337 if (flags
& GMEM_DDESHARE
|| is_dde_handle(handle
))
339 FIXME(global
, "shared memory reallocating unimplemented\n");
342 #endif /* CONFIG_IPC */
344 if (!VALID_HANDLE(handle
)) {
345 WARN(global
, "Invalid handle 0x%04x!\n", handle
);
348 pArena
= GET_ARENA_PTR( handle
);
350 /* Discard the block if requested */
352 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
354 if (!(pArena
->flags
& GA_MOVEABLE
) ||
355 !(pArena
->flags
& GA_DISCARDABLE
) ||
356 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
357 HeapFree( SystemHeap
, 0, (void *)pArena
->base
);
360 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
361 * change the selector if we are shrinking the block.
362 * FIXME: shouldn't we keep selectors until the block is deleted?
364 SELECTOR_ReallocBlock( sel
, 0, 1, SEGMENT_DATA
, 0, 0 );
370 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
371 if (size
== 0) size
= 0x20;
372 else size
= (size
+ 0x1f) & ~0x1f;
374 /* Change the flags */
376 if (flags
& GMEM_MODIFY
)
378 /* Change the flags, leaving GA_DGROUP alone */
379 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
380 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
384 /* Reallocate the linear memory */
386 ptr
= (void *)pArena
->base
;
387 oldsize
= pArena
->size
;
388 TRACE(global
,"oldsize %08lx\n",oldsize
);
389 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
391 ptr
= HeapReAlloc( SystemHeap
, 0, ptr
, size
);
394 SELECTOR_FreeBlock( sel
, (oldsize
+ 0xffff) / 0x10000 );
395 memset( pArena
, 0, sizeof(GLOBALARENA
) );
399 /* Reallocate the selector(s) */
401 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
, SEGMENT_DATA
, 0, 0 );
404 HeapFree( SystemHeap
, 0, ptr
);
405 memset( pArena
, 0, sizeof(GLOBALARENA
) );
408 selcount
= (size
+ 0xffff) / 0x10000;
410 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
412 HeapFree( SystemHeap
, 0, ptr
);
413 SELECTOR_FreeBlock( sel
, selcount
);
417 /* Fill the new arena block */
419 if (pNewArena
!= pArena
) memcpy( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
420 pNewArena
->base
= (DWORD
)ptr
;
421 pNewArena
->size
= GET_SEL_LIMIT(sel
) + 1;
422 pNewArena
->selCount
= selcount
;
423 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
425 if (selcount
> 1) /* clear the next arena blocks */
426 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
428 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
429 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
430 return pNewArena
->handle
;
434 /***********************************************************************
435 * GlobalFree16 (KERNEL.17)
440 HGLOBAL16 WINAPI
GlobalFree16(
441 HGLOBAL16 handle
/* [in] Handle of global memory object */
445 if (!VALID_HANDLE(handle
)) {
446 WARN(global
,"Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
449 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
451 TRACE(global
, "%04x\n", handle
);
452 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
454 if (is_dde_handle(handle
)) return DDE_GlobalFree(handle
);
455 #endif /* CONFIG_IPC */
456 if (ptr
) HeapFree( SystemHeap
, 0, ptr
);
461 /***********************************************************************
462 * WIN16_GlobalLock16 (KERNEL.18)
464 * This is the GlobalLock16() function used by 16-bit code.
466 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
468 TRACE(global
, "(%04x) -> %08lx\n",
469 handle
, MAKELONG( 0, GlobalHandleToSel(handle
)) );
472 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
475 if (is_dde_handle(handle
))
476 return PTR_SEG_OFF_TO_SEGPTR( DDE_GlobalHandleToSel(handle
), 0 );
477 #endif /* CONFIG_IPC */
479 if (!VALID_HANDLE(handle
)) {
480 WARN(global
,"Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
483 if (!GET_ARENA_PTR(handle
)->base
) return (SEGPTR
)0;
484 GET_ARENA_PTR(handle
)->lockCount
++;
485 return PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel(handle
), 0 );
486 /* FIXME: put segment value in CX as well */
492 /***********************************************************************
493 * GlobalLock16 (KERNEL.18)
495 * This is the GlobalLock16() function used by 32-bit code.
498 * Pointer to first byte of memory block
501 LPVOID WINAPI
GlobalLock16(
502 HGLOBAL16 handle
/* [in] Handle of global memory object */
504 if (!handle
) return 0;
505 if (!VALID_HANDLE(handle
))
507 GET_ARENA_PTR(handle
)->lockCount
++;
509 if (is_dde_handle(handle
)) return DDE_AttachHandle(handle
, NULL
);
511 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
515 /***********************************************************************
516 * GlobalUnlock16 (KERNEL.19)
518 * Should the return values be cast to booleans?
521 * TRUE: Object is still locked
522 * FALSE: Object is unlocked
524 BOOL16 WINAPI
GlobalUnlock16(
525 HGLOBAL16 handle
/* [in] Handle of global memory object */
527 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
528 if (!VALID_HANDLE(handle
)) {
529 WARN(global
,"Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
532 TRACE(global
, "%04x\n", handle
);
533 if (pArena
->lockCount
) pArena
->lockCount
--;
534 return pArena
->lockCount
;
538 /***********************************************************************
539 * GlobalSize16 (KERNEL.20)
541 * Size in bytes of object
544 DWORD WINAPI
GlobalSize16(
545 HGLOBAL16 handle
/* [in] Handle of global memory object */
547 TRACE(global
, "%04x\n", handle
);
548 if (!handle
) return 0;
549 if (!VALID_HANDLE(handle
))
551 return GET_ARENA_PTR(handle
)->size
;
555 /***********************************************************************
556 * GlobalHandle16 (KERNEL.21)
558 * Why is GlobalHandleToSel used here with the sel as input?
564 DWORD WINAPI
GlobalHandle16(
565 WORD sel
/* [in] Address of global memory block */
567 TRACE(global
, "%04x\n", sel
);
568 if (!VALID_HANDLE(sel
)) {
569 WARN(global
,"Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
572 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel(sel
) );
575 /***********************************************************************
576 * GlobalHandleNoRIP (KERNEL.159)
578 DWORD WINAPI
GlobalHandleNoRIP( WORD sel
)
581 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
582 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
583 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel(sel
) );
589 /***********************************************************************
590 * GlobalFlags16 (KERNEL.22)
592 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
596 * Value specifying flags and lock count
597 * GMEM_INVALID_HANDLE: Invalid handle
599 UINT16 WINAPI
GlobalFlags16(
600 HGLOBAL16 handle
/* [in] Handle of global memory object */
604 TRACE(global
, "%04x\n", handle
);
605 if (!VALID_HANDLE(handle
)) {
606 WARN(global
,"Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
609 pArena
= GET_ARENA_PTR(handle
);
610 return pArena
->lockCount
|
611 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
612 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
616 /***********************************************************************
617 * LockSegment16 (KERNEL.23)
619 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
621 TRACE(global
, "%04x\n", handle
);
622 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
623 if (!VALID_HANDLE(handle
)) {
624 WARN(global
,"Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
627 GET_ARENA_PTR(handle
)->lockCount
++;
632 /***********************************************************************
633 * UnlockSegment16 (KERNEL.24)
635 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
637 TRACE(global
, "%04x\n", handle
);
638 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
639 if (!VALID_HANDLE(handle
)) {
640 WARN(global
,"Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
643 GET_ARENA_PTR(handle
)->lockCount
--;
644 /* FIXME: this ought to return the lock count in CX (go figure...) */
648 /***********************************************************************
649 * GlobalCompact16 (KERNEL.25)
651 DWORD WINAPI
GlobalCompact16( DWORD desired
)
653 return GLOBAL_MAX_ALLOC_SIZE
;
657 /***********************************************************************
658 * GlobalFreeAll (KERNEL.26)
660 void WINAPI
GlobalFreeAll( HGLOBAL16 owner
)
665 pArena
= pGlobalArena
;
666 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
668 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
669 GlobalFree16( pArena
->handle
);
674 /***********************************************************************
675 * GlobalWire16 (KERNEL.111)
677 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
679 return WIN16_GlobalLock16( handle
);
683 /***********************************************************************
684 * GlobalUnWire16 (KERNEL.112)
686 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
688 return !GlobalUnlock16( handle
);
692 /***********************************************************************
693 * SetSwapAreaSize16 (KERNEL.106)
695 LONG WINAPI
SetSwapAreaSize16( WORD size
)
697 FIXME(global
, "(%d) - stub!\n", size
);
698 return MAKELONG( size
, 0xffff );
702 /***********************************************************************
703 * GlobalLRUOldest (KERNEL.163)
705 HGLOBAL16 WINAPI
GlobalLRUOldest( HGLOBAL16 handle
)
707 TRACE(global
, "%04x\n", handle
);
708 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
713 /***********************************************************************
714 * GlobalLRUNewest (KERNEL.164)
716 HGLOBAL16 WINAPI
GlobalLRUNewest( HGLOBAL16 handle
)
718 TRACE(global
, "%04x\n", handle
);
719 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
724 /***********************************************************************
725 * GetFreeSpace16 (KERNEL.169)
727 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
730 GlobalMemoryStatus( &ms
);
731 return ms
.dwAvailVirtual
;
734 /***********************************************************************
735 * GlobalDOSAlloc (KERNEL.184)
737 * Address (HW=Paragraph segment; LW=Selector)
739 DWORD WINAPI
GlobalDOSAlloc(
740 DWORD size
/* [in] Number of bytes to be allocated */
743 LPVOID lpBlock
= DOSMEM_GetBlock( 0, size
, &uParagraph
);
747 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
750 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
,
751 hModule
, 0, 0, 0, NULL
);
752 return MAKELONG(wSelector
,uParagraph
);
758 /***********************************************************************
759 * GlobalDOSFree (KERNEL.185)
764 WORD WINAPI
GlobalDOSFree(
765 WORD sel
/* [in] Selector */
767 DWORD block
= GetSelectorBase(sel
);
769 if( block
&& block
< 0x100000 )
771 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
772 if( DOSMEM_FreeBlock( 0, lpBlock
) )
773 GLOBAL_FreeBlock( sel
);
780 /***********************************************************************
781 * GlobalPageLock (KERNEL.191)
783 WORD WINAPI
GlobalPageLock( HGLOBAL16 handle
)
785 TRACE(global
, "%04x\n", handle
);
786 if (!VALID_HANDLE(handle
)) {
787 WARN(global
,"Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
790 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
794 /***********************************************************************
795 * GlobalPageUnlock (KERNEL.192)
797 WORD WINAPI
GlobalPageUnlock( HGLOBAL16 handle
)
799 TRACE(global
, "%04x\n", handle
);
800 if (!VALID_HANDLE(handle
)) {
801 WARN(global
,"Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
804 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
808 /***********************************************************************
809 * GlobalFix16 (KERNEL.197)
811 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
813 TRACE(global
, "%04x\n", handle
);
814 if (!VALID_HANDLE(handle
)) {
815 WARN(global
,"Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
818 GET_ARENA_PTR(handle
)->lockCount
++;
820 return GlobalHandleToSel(handle
);
824 /***********************************************************************
825 * GlobalUnfix16 (KERNEL.198)
827 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
829 TRACE(global
, "%04x\n", handle
);
830 if (!VALID_HANDLE(handle
)) {
831 WARN(global
,"Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
834 GET_ARENA_PTR(handle
)->lockCount
--;
838 /***********************************************************************
839 * FarSetOwner (KERNEL.403)
841 void WINAPI
FarSetOwner( HGLOBAL16 handle
, HANDLE16 hOwner
)
843 if (!VALID_HANDLE(handle
)) {
844 WARN(global
,"Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
847 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
851 /***********************************************************************
852 * FarGetOwner (KERNEL.404)
854 HANDLE16 WINAPI
FarGetOwner( HGLOBAL16 handle
)
856 if (!VALID_HANDLE(handle
)) {
857 WARN(global
,"Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
860 return GET_ARENA_PTR(handle
)->hOwner
;
864 /***********************************************************************
865 * GlobalHandleToSel (TOOLHELP.50)
867 WORD WINAPI
GlobalHandleToSel( HGLOBAL16 handle
)
869 TRACE(toolhelp
, "%04x\n", handle
);
870 if (!handle
) return 0;
872 if (is_dde_handle(handle
)) return DDE_GlobalHandleToSel(handle
);
874 if (!VALID_HANDLE(handle
)) {
875 WARN(global
,"Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
880 WARN(global
, "Program attempted invalid selector conversion\n" );
887 /***********************************************************************
888 * GlobalFirst (TOOLHELP.51)
890 BOOL16 WINAPI
GlobalFirst( GLOBALENTRY
*pGlobal
, WORD wFlags
)
892 if (wFlags
== GLOBAL_LRU
) return FALSE
;
894 return GlobalNext( pGlobal
, wFlags
);
898 /***********************************************************************
899 * GlobalNext (TOOLHELP.52)
901 BOOL16 WINAPI
GlobalNext( GLOBALENTRY
*pGlobal
, WORD wFlags
)
905 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
906 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
907 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
910 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
911 if (pArena
->size
== 0) break; /* block is free */
912 if (i
>= globalArenaSize
) return FALSE
;
916 pGlobal
->dwAddress
= pArena
->base
;
917 pGlobal
->dwBlockSize
= pArena
->size
;
918 pGlobal
->hBlock
= pArena
->handle
;
919 pGlobal
->wcLock
= pArena
->lockCount
;
920 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
921 pGlobal
->wFlags
= (GetCurrentPDB() == pArena
->hOwner
);
922 pGlobal
->wHeapPresent
= FALSE
;
923 pGlobal
->hOwner
= pArena
->hOwner
;
924 pGlobal
->wType
= GT_UNKNOWN
;
931 /***********************************************************************
932 * GlobalInfo (TOOLHELP.53)
934 BOOL16 WINAPI
GlobalInfo( GLOBALINFO
*pInfo
)
939 pInfo
->wcItems
= globalArenaSize
;
940 pInfo
->wcItemsFree
= 0;
941 pInfo
->wcItemsLRU
= 0;
942 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
943 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
948 /***********************************************************************
949 * GlobalEntryHandle (TOOLHELP.54)
951 BOOL16 WINAPI
GlobalEntryHandle( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
953 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
955 pGlobal
->dwAddress
= pArena
->base
;
956 pGlobal
->dwBlockSize
= pArena
->size
;
957 pGlobal
->hBlock
= pArena
->handle
;
958 pGlobal
->wcLock
= pArena
->lockCount
;
959 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
960 pGlobal
->wFlags
= (GetCurrentPDB() == pArena
->hOwner
);
961 pGlobal
->wHeapPresent
= FALSE
;
962 pGlobal
->hOwner
= pArena
->hOwner
;
963 pGlobal
->wType
= GT_UNKNOWN
;
970 /***********************************************************************
971 * GlobalEntryModule (TOOLHELP.55)
973 BOOL16 WINAPI
GlobalEntryModule( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
980 /***********************************************************************
981 * MemManInfo (TOOLHELP.72)
983 BOOL16 WINAPI
MemManInfo( MEMMANINFO
*info
)
988 * Not unsurprisingly although the documention says you
989 * _must_ provide the size in the dwSize field, this function
990 * (under Windows) always fills the structure and returns true.
992 GlobalMemoryStatus( &status
);
993 info
->wPageSize
= VIRTUAL_GetPageSize();
994 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
995 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
996 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
997 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
998 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
999 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1000 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1001 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1002 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1006 /***********************************************************************
1007 * GetFreeMemInfo (KERNEL.316)
1009 DWORD WINAPI
GetFreeMemInfo(void)
1012 MemManInfo( &info
);
1013 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1017 * Win32 Global heap functions (GlobalXXX).
1018 * These functions included in Win32 for compatibility with 16 bit Windows
1019 * Especially the moveable blocks and handles are oldish.
1020 * But the ability to directly allocate memory with GPTR and LPTR is widely
1023 * The handle stuff looks horrible, but it's implemented almost like Win95
1028 #define MAGIC_GLOBAL_USED 0x5342
1029 #define GLOBAL_LOCK_MAX 0xFF
1030 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1031 #define INTERN_TO_HANDLE(i) ((HGLOBAL32) &((i)->Pointer))
1032 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL32 *)(p))-1))
1033 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1034 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1036 typedef struct __GLOBAL32_INTERN
1039 LPVOID Pointer WINE_PACKED
;
1042 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
1045 /***********************************************************************
1046 * GlobalAlloc32 (KERNEL32.315)
1051 HGLOBAL32 WINAPI
GlobalAlloc32(
1052 UINT32 flags
, /* [in] Object allocation attributes */
1053 DWORD size
/* [in] Number of bytes to allocate */
1055 PGLOBAL32_INTERN pintern
;
1059 if(flags
&GMEM_ZEROINIT
)
1060 hpflags
=HEAP_ZERO_MEMORY
;
1064 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1066 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1067 return (HGLOBAL32
) palloc
;
1071 /* HeapLock(GetProcessHeap()); */
1073 pintern
=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1076 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
+sizeof(HGLOBAL32
));
1077 *(HGLOBAL32
*)palloc
=INTERN_TO_HANDLE(pintern
);
1078 pintern
->Pointer
=palloc
+sizeof(HGLOBAL32
);
1081 pintern
->Pointer
=NULL
;
1082 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1083 pintern
->Flags
=flags
>>8;
1084 pintern
->LockCount
=0;
1086 /* HeapUnlock(GetProcessHeap()); */
1088 return INTERN_TO_HANDLE(pintern
);
1093 /***********************************************************************
1094 * GlobalLock32 (KERNEL32.326)
1096 * Pointer to first byte of block
1099 LPVOID WINAPI
GlobalLock32(
1100 HGLOBAL32 hmem
/* [in] Handle of global memory object */
1102 PGLOBAL32_INTERN pintern
;
1106 return (LPVOID
) hmem
;
1108 /* HeapLock(GetProcessHeap()); */
1110 pintern
=HANDLE_TO_INTERN(hmem
);
1111 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1113 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1114 pintern
->LockCount
++;
1115 palloc
=pintern
->Pointer
;
1119 WARN(global
, "invalid handle\n");
1120 palloc
=(LPVOID
) NULL
;
1122 /* HeapUnlock(GetProcessHeap()); */;
1127 /***********************************************************************
1128 * GlobalUnlock32 (KERNEL32.332)
1130 * TRUE: Object is still locked
1131 * FALSE: Object is unlocked
1133 BOOL32 WINAPI
GlobalUnlock32(
1134 HGLOBAL32 hmem
/* [in] Handle of global memory object */
1136 PGLOBAL32_INTERN pintern
;
1142 /* HeapLock(GetProcessHeap()); */
1143 pintern
=HANDLE_TO_INTERN(hmem
);
1145 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1147 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1148 pintern
->LockCount
--;
1150 locked
=(pintern
->LockCount
==0) ? FALSE
: TRUE
;
1154 WARN(global
, "invalid handle\n");
1157 /* HeapUnlock(GetProcessHeap()); */
1162 /***********************************************************************
1163 * GlobalHandle32 (KERNEL32.325)
1164 * Returns the handle associated with the specified pointer.
1167 * Since there in only one goto, can it be removed and the return
1174 HGLOBAL32 WINAPI
GlobalHandle32(
1175 LPCVOID pmem
/* [in] Pointer to global memory block */
1179 if (!HEAP_IsInsideHeap( GetProcessHeap(), 0, pmem
)) goto error
;
1180 handle
= POINTER_TO_HANDLE(pmem
);
1181 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, (LPCVOID
)handle
))
1183 if (HANDLE_TO_INTERN(handle
)->Magic
== MAGIC_GLOBAL_USED
)
1184 return handle
; /* valid moveable block */
1186 /* maybe FIXED block */
1187 if (HeapValidate( GetProcessHeap(), 0, pmem
))
1188 return (HGLOBAL32
)pmem
; /* valid fixed block */
1191 SetLastError( ERROR_INVALID_HANDLE
);
1196 /***********************************************************************
1197 * GlobalReAlloc32 (KERNEL32.328)
1202 HGLOBAL32 WINAPI
GlobalReAlloc32(
1203 HGLOBAL32 hmem
, /* [in] Handle of global memory object */
1204 DWORD size
, /* [in] New size of block */
1205 UINT32 flags
/* [in] How to reallocate object */
1209 PGLOBAL32_INTERN pintern
;
1212 /* HeapLock(GetProcessHeap()); */
1213 if(flags
& GMEM_MODIFY
) /* modify flags */
1215 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1217 /* make a fixed block moveable
1218 * actually only NT is able to do this. But it's soo simple
1222 ERR(global
, "GlobalReAlloc32 with null handle!\n");
1223 SetLastError( ERROR_INVALID_ACCESS_TO_MEM
);
1226 size
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1227 hnew
=GlobalAlloc32( flags
, size
);
1228 palloc
=GlobalLock32(hnew
);
1229 memcpy(palloc
, (LPVOID
) hmem
, size
);
1230 GlobalUnlock32(hnew
);
1233 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1235 /* change the flags to make our block "discardable" */
1236 pintern
=HANDLE_TO_INTERN(hmem
);
1237 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1242 SetLastError(ERROR_INVALID_PARAMETER
);
1250 /* reallocate fixed memory */
1251 hnew
=(HGLOBAL32
)HeapReAlloc(GetProcessHeap(), 0, (LPVOID
) hmem
, size
);
1255 /* reallocate a moveable block */
1256 pintern
=HANDLE_TO_INTERN(hmem
);
1257 if(pintern
->LockCount
>1) {
1258 ERR(global
,"handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1259 SetLastError(ERROR_INVALID_HANDLE
);
1263 if(pintern
->Pointer
)
1265 palloc
=HeapReAlloc(GetProcessHeap(), 0,
1266 pintern
->Pointer
-sizeof(HGLOBAL32
),
1267 size
+sizeof(HGLOBAL32
) );
1268 pintern
->Pointer
=palloc
+sizeof(HGLOBAL32
);
1272 palloc
=HeapAlloc(GetProcessHeap(), 0, size
+sizeof(HGLOBAL32
));
1273 *(HGLOBAL32
*)palloc
=hmem
;
1274 pintern
->Pointer
=palloc
+sizeof(HGLOBAL32
);
1279 if(pintern
->Pointer
)
1281 HeapFree(GetProcessHeap(), 0, pintern
->Pointer
-sizeof(HGLOBAL32
));
1282 pintern
->Pointer
=NULL
;
1287 /* HeapUnlock(GetProcessHeap()); */
1292 /***********************************************************************
1293 * GlobalFree32 (KERNEL32.322)
1298 HGLOBAL32 WINAPI
GlobalFree32(
1299 HGLOBAL32 hmem
/* [in] Handle of global memory object */
1301 PGLOBAL32_INTERN pintern
;
1302 HGLOBAL32 hreturned
= 0;
1304 if(ISPOINTER(hmem
)) /* POINTER */
1306 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1310 /* HeapLock(GetProcessHeap()); */
1311 pintern
=HANDLE_TO_INTERN(hmem
);
1313 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1315 if(pintern
->LockCount
!=0)
1316 SetLastError(ERROR_INVALID_HANDLE
);
1317 if(pintern
->Pointer
)
1318 if(!HeapFree(GetProcessHeap(), 0,
1319 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL32
)))
1321 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1324 /* HeapUnlock(GetProcessHeap()); */
1330 /***********************************************************************
1331 * GlobalSize32 (KERNEL32.329)
1333 * Size in bytes of the global memory object
1336 DWORD WINAPI
GlobalSize32(
1337 HGLOBAL32 hmem
/* [in] Handle of global memory object */
1340 PGLOBAL32_INTERN pintern
;
1344 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1348 /* HeapLock(GetProcessHeap()); */
1349 pintern
=HANDLE_TO_INTERN(hmem
);
1351 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1353 retval
=HeapSize(GetProcessHeap(), 0,
1354 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL32
))-4;
1358 WARN(global
, "invalid handle\n");
1361 /* HeapUnlock(GetProcessHeap()); */
1367 /***********************************************************************
1368 * GlobalWire32 (KERNEL32.333)
1370 LPVOID WINAPI
GlobalWire32(HGLOBAL32 hmem
)
1372 return GlobalLock32( hmem
);
1376 /***********************************************************************
1377 * GlobalUnWire32 (KERNEL32.330)
1379 BOOL32 WINAPI
GlobalUnWire32(HGLOBAL32 hmem
)
1381 return GlobalUnlock32( hmem
);
1385 /***********************************************************************
1386 * GlobalFix32 (KERNEL32.320)
1388 VOID WINAPI
GlobalFix32(HGLOBAL32 hmem
)
1390 GlobalLock32( hmem
);
1394 /***********************************************************************
1395 * GlobalUnfix32 (KERNEL32.331)
1397 VOID WINAPI
GlobalUnfix32(HGLOBAL32 hmem
)
1399 GlobalUnlock32( hmem
);
1403 /***********************************************************************
1404 * GlobalFlags32 (KERNEL32.321)
1405 * Returns information about the specified global memory object
1408 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1411 * Value specifying allocation flags and lock count
1412 * GMEM_INVALID_HANDLE: Failure
1414 UINT32 WINAPI
GlobalFlags32(
1415 HGLOBAL32 hmem
/* [in] Handle to global memory object */
1418 PGLOBAL32_INTERN pintern
;
1426 /* HeapLock(GetProcessHeap()); */
1427 pintern
=HANDLE_TO_INTERN(hmem
);
1428 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1430 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1431 if(pintern
->Pointer
==0)
1432 retval
|= GMEM_DISCARDED
;
1436 WARN(global
,"Invalid handle: %04x", hmem
);
1439 /* HeapUnlock(GetProcessHeap()); */
1445 /***********************************************************************
1446 * GlobalCompact32 (KERNEL32.316)
1448 DWORD WINAPI
GlobalCompact32( DWORD minfree
)
1450 return 0; /* GlobalCompact does nothing in Win32 */
1454 /***********************************************************************
1455 * GlobalMemoryStatus (KERNEL32.327)
1459 VOID WINAPI
GlobalMemoryStatus(
1460 LPMEMORYSTATUS lpmem
1463 FILE *f
= fopen( "/proc/meminfo", "r" );
1467 int total
, used
, free
;
1469 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1470 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1471 while (fgets( buffer
, sizeof(buffer
), f
))
1473 /* old style /proc/meminfo ... */
1474 if (sscanf( buffer
, "Mem: %d %d %d", &total
, &used
, &free
))
1476 lpmem
->dwTotalPhys
+= total
;
1477 lpmem
->dwAvailPhys
+= free
;
1479 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1481 lpmem
->dwTotalPageFile
+= total
;
1482 lpmem
->dwAvailPageFile
+= free
;
1485 /* new style /proc/meminfo ... */
1486 if (sscanf(buffer
, "MemTotal: %d", &total
))
1487 lpmem
->dwTotalPhys
= total
*1024;
1488 if (sscanf(buffer
, "MemFree: %d", &free
))
1489 lpmem
->dwAvailPhys
= free
*1024;
1490 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1491 lpmem
->dwTotalPageFile
= total
*1024;
1492 if (sscanf(buffer
, "SwapFree: %d", &free
))
1493 lpmem
->dwAvailPageFile
= free
*1024;
1497 if (lpmem
->dwTotalPhys
)
1499 lpmem
->dwTotalVirtual
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1500 lpmem
->dwAvailVirtual
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1501 lpmem
->dwMemoryLoad
= (lpmem
->dwTotalVirtual
-lpmem
->dwAvailVirtual
)
1502 / (lpmem
->dwTotalVirtual
/ 100);
1507 /* FIXME: should do something for other systems */
1508 lpmem
->dwMemoryLoad
= 0;
1509 lpmem
->dwTotalPhys
= 16*1024*1024;
1510 lpmem
->dwAvailPhys
= 16*1024*1024;
1511 lpmem
->dwTotalPageFile
= 16*1024*1024;
1512 lpmem
->dwAvailPageFile
= 16*1024*1024;
1513 lpmem
->dwTotalVirtual
= 32*1024*1024;
1514 lpmem
->dwAvailVirtual
= 32*1024*1024;
1518 /**********************************************************************
1519 * WOWGlobalAllocLock (KERNEL32.62)
1521 * Combined GlobalAlloc and GlobalLock.
1523 SEGPTR WINAPI
WOWGlobalAllocLock16(DWORD flags
,DWORD cb
,HGLOBAL16
*hmem
)
1526 xhmem
= GlobalAlloc16(flags
,cb
);
1527 if (hmem
) *hmem
= xhmem
;
1528 return WIN16_GlobalLock16(xhmem
);
1532 /**********************************************************************
1533 * WOWGlobalUnlockFree (KERNEL32.64)
1535 * Combined GlobalUnlock and GlobalFree.
1537 WORD WINAPI
WOWGlobalUnlockFree16(DWORD vpmem
) {
1538 if (!GlobalUnlock16(HIWORD(vpmem
)))
1540 return GlobalFree16(HIWORD(vpmem
));