2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 /* 0xffff sometimes seems to mean: CURRENT_DS */
13 #include "wine/winbase16.h"
17 #include "selectors.h"
20 #include "stackframe.h"
25 DEFAULT_DEBUG_CHANNEL(global
)
27 /* Global arena block */
30 DWORD base
; /* Base address (0 if discarded) */
31 DWORD size
; /* Size in bytes (0 indicates a free block) */
32 HGLOBAL16 handle
; /* Handle for this block */
33 HGLOBAL16 hOwner
; /* Owner of this block */
34 BYTE lockCount
; /* Count of GlobalFix() calls */
35 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
36 BYTE flags
; /* Allocation flags */
37 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 */
50 static GLOBALARENA
*pGlobalArena
= NULL
;
51 static int globalArenaSize
= 0;
53 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
55 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
56 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
58 /***********************************************************************
61 * Return the arena for a given selector, growing the arena array if needed.
63 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
65 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
67 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
68 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
69 newsize
* sizeof(GLOBALARENA
) );
70 if (!pNewArena
) return 0;
71 pGlobalArena
= pNewArena
;
72 memset( pGlobalArena
+ globalArenaSize
, 0,
73 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
74 globalArenaSize
= newsize
;
76 return pGlobalArena
+ (sel
>> __AHSHIFT
);
80 void debug_handles(void)
84 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
85 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
87 DUMP("0x%08x, ",pGlobalArena
[i
].handle
);
95 /***********************************************************************
98 * Create a global heap block for a fixed range of linear memory.
100 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
101 HGLOBAL16 hOwner
, BOOL16 isCode
,
102 BOOL16 is32Bit
, BOOL16 isReadOnly
,
108 /* Allocate the selector(s) */
110 sel
= SELECTOR_AllocBlock( ptr
, size
,
111 isCode
? SEGMENT_CODE
: SEGMENT_DATA
,
112 is32Bit
, isReadOnly
);
115 selcount
= (size
+ 0xffff) / 0x10000;
117 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
119 SELECTOR_FreeBlock( sel
, selcount
);
123 /* Fill the arena block */
125 pArena
->base
= (DWORD
)ptr
;
126 pArena
->size
= GET_SEL_LIMIT(sel
) + 1;
129 if (flags
& GMEM_DDESHARE
)
131 pArena
->handle
= shmdata
->handle
;
132 pArena
->shmid
= shmdata
->shmid
;
137 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
141 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
143 pArena
->hOwner
= hOwner
;
144 pArena
->lockCount
= 0;
145 pArena
->pageLockCount
= 0;
146 pArena
->flags
= flags
& GA_MOVEABLE
;
147 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
148 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
149 if (!isCode
) pArena
->flags
|= GA_DGROUP
;
150 pArena
->selCount
= selcount
;
151 if (selcount
> 1) /* clear the next arena blocks */
152 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
154 return pArena
->handle
;
158 /***********************************************************************
161 * Free a block allocated by GLOBAL_CreateBlock, without touching
162 * the associated linear memory range.
164 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
169 if (!handle
) return TRUE
;
170 sel
= GlobalHandleToSel16( handle
);
171 if (!VALID_HANDLE(sel
))
173 pArena
= GET_ARENA_PTR(sel
);
174 SELECTOR_FreeBlock( sel
, (pArena
->size
+ 0xffff) / 0x10000 );
175 memset( pArena
, 0, sizeof(GLOBALARENA
) );
179 /***********************************************************************
182 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
187 if (!handle
) return TRUE
;
188 sel
= GlobalHandleToSel16( handle
);
189 if (!VALID_HANDLE(sel
))
191 pArena
= GET_ARENA_PTR(sel
);
192 if (pArena
->selCount
!= 1)
195 pArena
->base
= (DWORD
)ptr
;
198 SELECTOR_MoveBlock( sel
, ptr
);
199 SetSelectorLimit16( sel
, size
-1 );
204 /***********************************************************************
207 * Implementation of GlobalAlloc16()
209 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
,
210 BOOL16 isCode
, BOOL16 is32Bit
, BOOL16 isReadOnly
)
216 TRACE(global
, "%ld flags=%04x\n", size
, flags
);
218 /* If size is 0, create a discarded block */
220 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, isCode
,
221 is32Bit
, isReadOnly
, NULL
);
225 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
226 size
= (size
+ 0x1f) & ~0x1f;
228 /* Allocate the linear memory */
231 if (flags
& GMEM_DDESHARE
)
232 ptr
= DDE_malloc(flags
, size
, &shmdata
);
234 #endif /* CONFIG_IPC */
236 ptr
= HeapAlloc( SystemHeap
, 0, size
);
238 /* FIXME: free discardable blocks and try again? */
241 /* Allocate the selector(s) */
243 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
,
244 isCode
, is32Bit
, isReadOnly
, &shmdata
);
247 HeapFree( SystemHeap
, 0, ptr
);
251 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
257 /***********************************************************************
260 * Find the arena for a given handle
261 * (when handle is not serial - e.g. DDE)
263 static GLOBALARENA
*GLOBAL_FindArena( HGLOBAL16 handle
)
266 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
267 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== handle
)
268 return ( &pGlobalArena
[i
] );
274 /***********************************************************************
275 * DDE_GlobalHandleToSel
278 WORD
DDE_GlobalHandleToSel( HGLOBAL16 handle
)
283 pArena
= GLOBAL_FindArena(handle
);
285 int ArenaIdx
= pArena
- pGlobalArena
;
287 /* See if synchronized to the shared memory */
288 return DDE_SyncHandle(handle
, ( ArenaIdx
<< __AHSHIFT
) | 7);
291 /* attach the block */
292 DDE_AttachHandle(handle
, &segptr
);
294 return SELECTOROF( segptr
);
296 #endif /* CONFIG_IPC */
299 /***********************************************************************
300 * GlobalAlloc16 (KERNEL.15)
305 HGLOBAL16 WINAPI
GlobalAlloc16(
306 UINT16 flags
, /* [in] Object allocation attributes */
307 DWORD size
/* [in] Number of bytes to allocate */
309 HANDLE16 owner
= GetCurrentPDB16();
311 if (flags
& GMEM_DDESHARE
)
312 owner
= GetExePtr(owner
); /* Make it a module handle */
313 return GLOBAL_Alloc( flags
, size
, owner
, FALSE
, FALSE
, FALSE
);
317 /***********************************************************************
318 * GlobalReAlloc16 (KERNEL.16)
323 HGLOBAL16 WINAPI
GlobalReAlloc16(
324 HGLOBAL16 handle
, /* [in] Handle of global memory object */
325 DWORD size
, /* [in] New size of block */
326 UINT16 flags
/* [in] How to reallocate object */
331 GLOBALARENA
*pArena
, *pNewArena
;
332 WORD sel
= GlobalHandleToSel16( handle
);
334 TRACE(global
, "%04x %ld flags=%04x\n",
335 handle
, size
, flags
);
336 if (!handle
) return 0;
339 if (flags
& GMEM_DDESHARE
|| is_dde_handle(handle
))
341 FIXME(global
, "shared memory reallocating unimplemented\n");
344 #endif /* CONFIG_IPC */
346 if (!VALID_HANDLE(handle
)) {
347 WARN(global
, "Invalid handle 0x%04x!\n", handle
);
350 pArena
= GET_ARENA_PTR( handle
);
352 /* Discard the block if requested */
354 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
356 if (!(pArena
->flags
& GA_MOVEABLE
) ||
357 !(pArena
->flags
& GA_DISCARDABLE
) ||
358 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
359 HeapFree( SystemHeap
, 0, (void *)pArena
->base
);
362 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
363 * change the selector if we are shrinking the block.
364 * FIXME: shouldn't we keep selectors until the block is deleted?
366 SELECTOR_ReallocBlock( sel
, 0, 1 );
372 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
373 if (size
== 0) size
= 0x20;
374 else size
= (size
+ 0x1f) & ~0x1f;
376 /* Change the flags */
378 if (flags
& GMEM_MODIFY
)
380 /* Change the flags, leaving GA_DGROUP alone */
381 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
382 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
386 /* Reallocate the linear memory */
388 ptr
= (void *)pArena
->base
;
389 oldsize
= pArena
->size
;
390 TRACE(global
,"oldsize %08lx\n",oldsize
);
391 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
393 if (((char *)ptr
>= DOSMEM_MemoryBase(0)) &&
394 ((char *)ptr
<= DOSMEM_MemoryBase(0) + 0x100000))
395 ptr
= DOSMEM_ResizeBlock(0, ptr
, size
, NULL
);
397 ptr
= HeapReAlloc( SystemHeap
, 0, ptr
, size
);
400 SELECTOR_FreeBlock( sel
, (oldsize
+ 0xffff) / 0x10000 );
401 memset( pArena
, 0, sizeof(GLOBALARENA
) );
405 /* Reallocate the selector(s) */
407 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
410 HeapFree( SystemHeap
, 0, ptr
);
411 memset( pArena
, 0, sizeof(GLOBALARENA
) );
414 selcount
= (size
+ 0xffff) / 0x10000;
416 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
418 HeapFree( SystemHeap
, 0, ptr
);
419 SELECTOR_FreeBlock( sel
, selcount
);
423 /* Fill the new arena block */
425 if (pNewArena
!= pArena
) memcpy( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
426 pNewArena
->base
= (DWORD
)ptr
;
427 pNewArena
->size
= GET_SEL_LIMIT(sel
) + 1;
428 pNewArena
->selCount
= selcount
;
429 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
431 if (selcount
> 1) /* clear the next arena blocks */
432 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
434 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
435 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
436 return pNewArena
->handle
;
440 /***********************************************************************
441 * GlobalFree16 (KERNEL.17)
446 HGLOBAL16 WINAPI
GlobalFree16(
447 HGLOBAL16 handle
/* [in] Handle of global memory object */
451 if (!VALID_HANDLE(handle
)) {
452 WARN(global
,"Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
455 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
457 TRACE(global
, "%04x\n", handle
);
458 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
460 if (is_dde_handle(handle
)) return DDE_GlobalFree(handle
);
461 #endif /* CONFIG_IPC */
462 if (ptr
) HeapFree( SystemHeap
, 0, ptr
);
467 /***********************************************************************
468 * WIN16_GlobalLock16 (KERNEL.18)
470 * This is the GlobalLock16() function used by 16-bit code.
472 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
474 TRACE(global
, "(%04x) -> %08lx\n",
475 handle
, MAKELONG( 0, GlobalHandleToSel16(handle
)) );
478 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
481 if (is_dde_handle(handle
))
482 return PTR_SEG_OFF_TO_SEGPTR( DDE_GlobalHandleToSel(handle
), 0 );
483 #endif /* CONFIG_IPC */
485 if (!VALID_HANDLE(handle
)) {
486 WARN(global
,"Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
489 if (!GET_ARENA_PTR(handle
)->base
) return (SEGPTR
)0;
490 GET_ARENA_PTR(handle
)->lockCount
++;
491 return PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(handle
), 0 );
492 /* FIXME: put segment value in CX as well */
498 /***********************************************************************
499 * GlobalLock16 (KERNEL.18)
501 * This is the GlobalLock16() function used by 32-bit code.
504 * Pointer to first byte of memory block
507 LPVOID WINAPI
GlobalLock16(
508 HGLOBAL16 handle
/* [in] Handle of global memory object */
510 if (!handle
) return 0;
511 if (!VALID_HANDLE(handle
))
513 GET_ARENA_PTR(handle
)->lockCount
++;
515 if (is_dde_handle(handle
)) return DDE_AttachHandle(handle
, NULL
);
517 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
521 /***********************************************************************
522 * GlobalUnlock16 (KERNEL.19)
524 * Should the return values be cast to booleans?
527 * TRUE: Object is still locked
528 * FALSE: Object is unlocked
530 BOOL16 WINAPI
GlobalUnlock16(
531 HGLOBAL16 handle
/* [in] Handle of global memory object */
533 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
534 if (!VALID_HANDLE(handle
)) {
535 WARN(global
,"Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
538 TRACE(global
, "%04x\n", handle
);
539 if (pArena
->lockCount
) pArena
->lockCount
--;
540 return pArena
->lockCount
;
543 /***********************************************************************
544 * GlobalChangeLockCount (KERNEL.365)
546 * This is declared as a register function as it has to preserve
547 * *all* registers, even AX/DX !
550 void WINAPI
GlobalChangeLockCount16( CONTEXT
*context
)
552 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), SP_reg( context
) );
553 HGLOBAL16 handle
= (HGLOBAL16
)args
[3];
554 INT16 delta
= (INT16
) args
[2];
557 GlobalLock16( handle
);
558 else if ( delta
== -1 )
559 GlobalUnlock16( handle
);
561 ERR( global
, "(%04X, %d): strange delta value\n", handle
, delta
);
564 /***********************************************************************
565 * GlobalSize16 (KERNEL.20)
567 * Size in bytes of object
570 DWORD WINAPI
GlobalSize16(
571 HGLOBAL16 handle
/* [in] Handle of global memory object */
573 TRACE(global
, "%04x\n", handle
);
574 if (!handle
) return 0;
575 if (!VALID_HANDLE(handle
))
577 return GET_ARENA_PTR(handle
)->size
;
581 /***********************************************************************
582 * GlobalHandle16 (KERNEL.21)
584 * Why is GlobalHandleToSel used here with the sel as input?
590 DWORD WINAPI
GlobalHandle16(
591 WORD sel
/* [in] Address of global memory block */
593 TRACE(global
, "%04x\n", sel
);
594 if (!VALID_HANDLE(sel
)) {
595 WARN(global
,"Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
598 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
601 /***********************************************************************
602 * GlobalHandleNoRIP (KERNEL.159)
604 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
607 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
608 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
609 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
615 /***********************************************************************
616 * GlobalFlags16 (KERNEL.22)
618 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
622 * Value specifying flags and lock count
623 * GMEM_INVALID_HANDLE: Invalid handle
625 UINT16 WINAPI
GlobalFlags16(
626 HGLOBAL16 handle
/* [in] Handle of global memory object */
630 TRACE(global
, "%04x\n", handle
);
631 if (!VALID_HANDLE(handle
)) {
632 WARN(global
,"Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
635 pArena
= GET_ARENA_PTR(handle
);
636 return pArena
->lockCount
|
637 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
638 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
642 /***********************************************************************
643 * LockSegment16 (KERNEL.23)
645 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
647 TRACE(global
, "%04x\n", handle
);
648 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
649 if (!VALID_HANDLE(handle
)) {
650 WARN(global
,"Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
653 GET_ARENA_PTR(handle
)->lockCount
++;
658 /***********************************************************************
659 * UnlockSegment16 (KERNEL.24)
661 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
663 TRACE(global
, "%04x\n", handle
);
664 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
665 if (!VALID_HANDLE(handle
)) {
666 WARN(global
,"Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
669 GET_ARENA_PTR(handle
)->lockCount
--;
670 /* FIXME: this ought to return the lock count in CX (go figure...) */
674 /***********************************************************************
675 * GlobalCompact16 (KERNEL.25)
677 DWORD WINAPI
GlobalCompact16( DWORD desired
)
679 return GLOBAL_MAX_ALLOC_SIZE
;
683 /***********************************************************************
684 * GlobalFreeAll (KERNEL.26)
686 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
691 pArena
= pGlobalArena
;
692 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
694 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
695 GlobalFree16( pArena
->handle
);
700 /***********************************************************************
701 * GlobalWire16 (KERNEL.111)
703 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
705 return WIN16_GlobalLock16( handle
);
709 /***********************************************************************
710 * GlobalUnWire16 (KERNEL.112)
712 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
714 return !GlobalUnlock16( handle
);
718 /***********************************************************************
719 * SetSwapAreaSize16 (KERNEL.106)
721 LONG WINAPI
SetSwapAreaSize16( WORD size
)
723 FIXME(global
, "(%d) - stub!\n", size
);
724 return MAKELONG( size
, 0xffff );
728 /***********************************************************************
729 * GlobalLRUOldest (KERNEL.163)
731 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
733 TRACE(global
, "%04x\n", handle
);
734 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
739 /***********************************************************************
740 * GlobalLRUNewest (KERNEL.164)
742 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
744 TRACE(global
, "%04x\n", handle
);
745 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
750 /***********************************************************************
751 * GetFreeSpace16 (KERNEL.169)
753 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
756 GlobalMemoryStatus( &ms
);
757 return ms
.dwAvailVirtual
;
760 /***********************************************************************
761 * GlobalDOSAlloc (KERNEL.184)
763 * Address (HW=Paragraph segment; LW=Selector)
765 DWORD WINAPI
GlobalDOSAlloc16(
766 DWORD size
/* [in] Number of bytes to be allocated */
769 LPVOID lpBlock
= DOSMEM_GetBlock( 0, size
, &uParagraph
);
773 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
776 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
,
777 hModule
, 0, 0, 0, NULL
);
778 return MAKELONG(wSelector
,uParagraph
);
784 /***********************************************************************
785 * GlobalDOSFree (KERNEL.185)
790 WORD WINAPI
GlobalDOSFree16(
791 WORD sel
/* [in] Selector */
793 DWORD block
= GetSelectorBase(sel
);
795 if( block
&& block
< 0x100000 )
797 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
798 if( DOSMEM_FreeBlock( 0, lpBlock
) )
799 GLOBAL_FreeBlock( sel
);
806 /***********************************************************************
807 * GlobalPageLock (KERNEL.191)
809 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
811 TRACE(global
, "%04x\n", handle
);
812 if (!VALID_HANDLE(handle
)) {
813 WARN(global
,"Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
816 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
820 /***********************************************************************
821 * GlobalPageUnlock (KERNEL.192)
823 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
825 TRACE(global
, "%04x\n", handle
);
826 if (!VALID_HANDLE(handle
)) {
827 WARN(global
,"Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
830 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
834 /***********************************************************************
835 * GlobalFix16 (KERNEL.197)
837 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
839 TRACE(global
, "%04x\n", handle
);
840 if (!VALID_HANDLE(handle
)) {
841 WARN(global
,"Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
844 GET_ARENA_PTR(handle
)->lockCount
++;
846 return GlobalHandleToSel16(handle
);
850 /***********************************************************************
851 * GlobalUnfix16 (KERNEL.198)
853 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
855 TRACE(global
, "%04x\n", handle
);
856 if (!VALID_HANDLE(handle
)) {
857 WARN(global
,"Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
860 GET_ARENA_PTR(handle
)->lockCount
--;
864 /***********************************************************************
865 * FarSetOwner (KERNEL.403)
867 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
869 if (!VALID_HANDLE(handle
)) {
870 WARN(global
,"Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
873 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
877 /***********************************************************************
878 * FarGetOwner (KERNEL.404)
880 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
882 if (!VALID_HANDLE(handle
)) {
883 WARN(global
,"Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
886 return GET_ARENA_PTR(handle
)->hOwner
;
890 /***********************************************************************
891 * GlobalHandleToSel (TOOLHELP.50)
893 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
895 if (!handle
) return 0;
897 if (is_dde_handle(handle
)) return DDE_GlobalHandleToSel(handle
);
899 if (!VALID_HANDLE(handle
)) {
900 WARN(global
,"Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
905 WARN(global
, "Program attempted invalid selector conversion\n" );
912 /***********************************************************************
913 * GlobalFirst (TOOLHELP.51)
915 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
917 if (wFlags
== GLOBAL_LRU
) return FALSE
;
919 return GlobalNext16( pGlobal
, wFlags
);
923 /***********************************************************************
924 * GlobalNext (TOOLHELP.52)
926 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
930 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
931 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
932 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
935 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
936 if (pArena
->size
== 0) break; /* block is free */
937 if (i
>= globalArenaSize
) return FALSE
;
941 pGlobal
->dwAddress
= pArena
->base
;
942 pGlobal
->dwBlockSize
= pArena
->size
;
943 pGlobal
->hBlock
= pArena
->handle
;
944 pGlobal
->wcLock
= pArena
->lockCount
;
945 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
946 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
947 pGlobal
->wHeapPresent
= FALSE
;
948 pGlobal
->hOwner
= pArena
->hOwner
;
949 pGlobal
->wType
= GT_UNKNOWN
;
956 /***********************************************************************
957 * GlobalInfo (TOOLHELP.53)
959 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
964 pInfo
->wcItems
= globalArenaSize
;
965 pInfo
->wcItemsFree
= 0;
966 pInfo
->wcItemsLRU
= 0;
967 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
968 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
973 /***********************************************************************
974 * GlobalEntryHandle (TOOLHELP.54)
976 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
978 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
980 pGlobal
->dwAddress
= pArena
->base
;
981 pGlobal
->dwBlockSize
= pArena
->size
;
982 pGlobal
->hBlock
= pArena
->handle
;
983 pGlobal
->wcLock
= pArena
->lockCount
;
984 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
985 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
986 pGlobal
->wHeapPresent
= FALSE
;
987 pGlobal
->hOwner
= pArena
->hOwner
;
988 pGlobal
->wType
= GT_UNKNOWN
;
995 /***********************************************************************
996 * GlobalEntryModule (TOOLHELP.55)
998 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
1005 /***********************************************************************
1006 * MemManInfo (TOOLHELP.72)
1008 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
1010 MEMORYSTATUS status
;
1013 * Not unsurprisingly although the documention says you
1014 * _must_ provide the size in the dwSize field, this function
1015 * (under Windows) always fills the structure and returns true.
1017 GlobalMemoryStatus( &status
);
1018 info
->wPageSize
= VIRTUAL_GetPageSize();
1019 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1020 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1021 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1022 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1023 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1024 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1025 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1026 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1027 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1031 /***********************************************************************
1032 * GetFreeMemInfo (KERNEL.316)
1034 DWORD WINAPI
GetFreeMemInfo16(void)
1037 MemManInfo16( &info
);
1038 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1042 * Win32 Global heap functions (GlobalXXX).
1043 * These functions included in Win32 for compatibility with 16 bit Windows
1044 * Especially the moveable blocks and handles are oldish.
1045 * But the ability to directly allocate memory with GPTR and LPTR is widely
1048 * The handle stuff looks horrible, but it's implemented almost like Win95
1053 #define MAGIC_GLOBAL_USED 0x5342
1054 #define GLOBAL_LOCK_MAX 0xFF
1055 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1056 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1057 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
1058 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1059 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1061 typedef struct __GLOBAL32_INTERN
1064 LPVOID Pointer WINE_PACKED
;
1067 } GLOBAL32_INTERN
, *PGLOBAL32_INTERN
;
1070 /***********************************************************************
1071 * GlobalAlloc32 (KERNEL32.315)
1076 HGLOBAL WINAPI
GlobalAlloc(
1077 UINT flags
, /* [in] Object allocation attributes */
1078 DWORD size
/* [in] Number of bytes to allocate */
1080 PGLOBAL32_INTERN pintern
;
1084 if(flags
&GMEM_ZEROINIT
)
1085 hpflags
=HEAP_ZERO_MEMORY
;
1089 if((flags
& GMEM_MOVEABLE
)==0) /* POINTER */
1091 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
);
1092 return (HGLOBAL
) palloc
;
1096 /* HeapLock(GetProcessHeap()); */
1098 pintern
=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN
));
1101 palloc
=HeapAlloc(GetProcessHeap(), hpflags
, size
+sizeof(HGLOBAL
));
1102 *(HGLOBAL
*)palloc
=INTERN_TO_HANDLE(pintern
);
1103 pintern
->Pointer
=palloc
+sizeof(HGLOBAL
);
1106 pintern
->Pointer
=NULL
;
1107 pintern
->Magic
=MAGIC_GLOBAL_USED
;
1108 pintern
->Flags
=flags
>>8;
1109 pintern
->LockCount
=0;
1111 /* HeapUnlock(GetProcessHeap()); */
1113 return INTERN_TO_HANDLE(pintern
);
1118 /***********************************************************************
1119 * GlobalLock32 (KERNEL32.326)
1121 * Pointer to first byte of block
1124 LPVOID WINAPI
GlobalLock(
1125 HGLOBAL hmem
/* [in] Handle of global memory object */
1127 PGLOBAL32_INTERN pintern
;
1131 return (LPVOID
) hmem
;
1133 /* HeapLock(GetProcessHeap()); */
1135 pintern
=HANDLE_TO_INTERN(hmem
);
1136 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1138 if(pintern
->LockCount
<GLOBAL_LOCK_MAX
)
1139 pintern
->LockCount
++;
1140 palloc
=pintern
->Pointer
;
1144 WARN(global
, "invalid handle\n");
1145 palloc
=(LPVOID
) NULL
;
1147 /* HeapUnlock(GetProcessHeap()); */;
1152 /***********************************************************************
1153 * GlobalUnlock32 (KERNEL32.332)
1155 * TRUE: Object is still locked
1156 * FALSE: Object is unlocked
1158 BOOL WINAPI
GlobalUnlock(
1159 HGLOBAL hmem
/* [in] Handle of global memory object */
1161 PGLOBAL32_INTERN pintern
;
1167 /* HeapLock(GetProcessHeap()); */
1168 pintern
=HANDLE_TO_INTERN(hmem
);
1170 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1172 if((pintern
->LockCount
<GLOBAL_LOCK_MAX
)&&(pintern
->LockCount
>0))
1173 pintern
->LockCount
--;
1175 locked
=(pintern
->LockCount
==0) ? FALSE
: TRUE
;
1179 WARN(global
, "invalid handle\n");
1182 /* HeapUnlock(GetProcessHeap()); */
1187 /***********************************************************************
1188 * GlobalHandle32 (KERNEL32.325)
1189 * Returns the handle associated with the specified pointer.
1192 * Since there in only one goto, can it be removed and the return
1199 HGLOBAL WINAPI
GlobalHandle(
1200 LPCVOID pmem
/* [in] Pointer to global memory block */
1204 if (!HEAP_IsInsideHeap( GetProcessHeap(), 0, pmem
)) goto error
;
1205 handle
= POINTER_TO_HANDLE(pmem
);
1206 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, (LPCVOID
)handle
))
1208 if (HANDLE_TO_INTERN(handle
)->Magic
== MAGIC_GLOBAL_USED
)
1209 return handle
; /* valid moveable block */
1211 /* maybe FIXED block */
1212 if (HeapValidate( GetProcessHeap(), 0, pmem
))
1213 return (HGLOBAL
)pmem
; /* valid fixed block */
1216 SetLastError( ERROR_INVALID_HANDLE
);
1221 /***********************************************************************
1222 * GlobalReAlloc32 (KERNEL32.328)
1227 HGLOBAL WINAPI
GlobalReAlloc(
1228 HGLOBAL hmem
, /* [in] Handle of global memory object */
1229 DWORD size
, /* [in] New size of block */
1230 UINT flags
/* [in] How to reallocate object */
1234 PGLOBAL32_INTERN pintern
;
1237 /* HeapLock(GetProcessHeap()); */
1238 if(flags
& GMEM_MODIFY
) /* modify flags */
1240 if( ISPOINTER(hmem
) && (flags
& GMEM_MOVEABLE
))
1242 /* make a fixed block moveable
1243 * actually only NT is able to do this. But it's soo simple
1247 ERR(global
, "GlobalReAlloc32 with null handle!\n");
1248 SetLastError( ERROR_NOACCESS
);
1251 size
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1252 hnew
=GlobalAlloc( flags
, size
);
1253 palloc
=GlobalLock(hnew
);
1254 memcpy(palloc
, (LPVOID
) hmem
, size
);
1258 else if( ISPOINTER(hmem
) &&(flags
& GMEM_DISCARDABLE
))
1260 /* change the flags to make our block "discardable" */
1261 pintern
=HANDLE_TO_INTERN(hmem
);
1262 pintern
->Flags
= pintern
->Flags
| (GMEM_DISCARDABLE
>> 8);
1267 SetLastError(ERROR_INVALID_PARAMETER
);
1275 /* reallocate fixed memory */
1276 hnew
=(HGLOBAL
)HeapReAlloc(GetProcessHeap(), 0, (LPVOID
) hmem
, size
);
1280 /* reallocate a moveable block */
1281 pintern
=HANDLE_TO_INTERN(hmem
);
1282 if(pintern
->LockCount
>1) {
1283 ERR(global
,"handle 0x%08lx is still locked, cannot realloc!\n",(DWORD
)hmem
);
1284 SetLastError(ERROR_INVALID_HANDLE
);
1288 if(pintern
->Pointer
)
1290 palloc
=HeapReAlloc(GetProcessHeap(), 0,
1291 pintern
->Pointer
-sizeof(HGLOBAL
),
1292 size
+sizeof(HGLOBAL
) );
1293 pintern
->Pointer
=palloc
+sizeof(HGLOBAL
);
1297 palloc
=HeapAlloc(GetProcessHeap(), 0, size
+sizeof(HGLOBAL
));
1298 *(HGLOBAL
*)palloc
=hmem
;
1299 pintern
->Pointer
=palloc
+sizeof(HGLOBAL
);
1304 if(pintern
->Pointer
)
1306 HeapFree(GetProcessHeap(), 0, pintern
->Pointer
-sizeof(HGLOBAL
));
1307 pintern
->Pointer
=NULL
;
1312 /* HeapUnlock(GetProcessHeap()); */
1317 /***********************************************************************
1318 * GlobalFree32 (KERNEL32.322)
1323 HGLOBAL WINAPI
GlobalFree(
1324 HGLOBAL hmem
/* [in] Handle of global memory object */
1326 PGLOBAL32_INTERN pintern
;
1327 HGLOBAL hreturned
= 0;
1329 if(ISPOINTER(hmem
)) /* POINTER */
1331 if(!HeapFree(GetProcessHeap(), 0, (LPVOID
) hmem
)) hmem
= 0;
1335 /* HeapLock(GetProcessHeap()); */
1336 pintern
=HANDLE_TO_INTERN(hmem
);
1338 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1340 if(pintern
->LockCount
!=0)
1341 SetLastError(ERROR_INVALID_HANDLE
);
1342 if(pintern
->Pointer
)
1343 if(!HeapFree(GetProcessHeap(), 0,
1344 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
)))
1346 if(!HeapFree(GetProcessHeap(), 0, pintern
))
1349 /* HeapUnlock(GetProcessHeap()); */
1355 /***********************************************************************
1356 * GlobalSize32 (KERNEL32.329)
1358 * Size in bytes of the global memory object
1361 DWORD WINAPI
GlobalSize(
1362 HGLOBAL hmem
/* [in] Handle of global memory object */
1365 PGLOBAL32_INTERN pintern
;
1369 retval
=HeapSize(GetProcessHeap(), 0, (LPVOID
) hmem
);
1373 /* HeapLock(GetProcessHeap()); */
1374 pintern
=HANDLE_TO_INTERN(hmem
);
1376 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1378 if (!pintern
->Pointer
) /* handle case of GlobalAlloc( ??,0) */
1380 retval
=HeapSize(GetProcessHeap(), 0,
1381 (char *)(pintern
->Pointer
)-sizeof(HGLOBAL
))-4;
1385 WARN(global
, "invalid handle\n");
1388 /* HeapUnlock(GetProcessHeap()); */
1390 /* HeapSize returns 0xffffffff on failure */
1391 if (retval
== 0xffffffff) retval
= 0;
1396 /***********************************************************************
1397 * GlobalWire32 (KERNEL32.333)
1399 LPVOID WINAPI
GlobalWire(HGLOBAL hmem
)
1401 return GlobalLock( hmem
);
1405 /***********************************************************************
1406 * GlobalUnWire32 (KERNEL32.330)
1408 BOOL WINAPI
GlobalUnWire(HGLOBAL hmem
)
1410 return GlobalUnlock( hmem
);
1414 /***********************************************************************
1415 * GlobalFix32 (KERNEL32.320)
1417 VOID WINAPI
GlobalFix(HGLOBAL hmem
)
1423 /***********************************************************************
1424 * GlobalUnfix32 (KERNEL32.331)
1426 VOID WINAPI
GlobalUnfix(HGLOBAL hmem
)
1428 GlobalUnlock( hmem
);
1432 /***********************************************************************
1433 * GlobalFlags32 (KERNEL32.321)
1434 * Returns information about the specified global memory object
1437 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1440 * Value specifying allocation flags and lock count
1441 * GMEM_INVALID_HANDLE: Failure
1443 UINT WINAPI
GlobalFlags(
1444 HGLOBAL hmem
/* [in] Handle to global memory object */
1447 PGLOBAL32_INTERN pintern
;
1455 /* HeapLock(GetProcessHeap()); */
1456 pintern
=HANDLE_TO_INTERN(hmem
);
1457 if(pintern
->Magic
==MAGIC_GLOBAL_USED
)
1459 retval
=pintern
->LockCount
+ (pintern
->Flags
<<8);
1460 if(pintern
->Pointer
==0)
1461 retval
|= GMEM_DISCARDED
;
1465 WARN(global
,"Invalid handle: %04x", hmem
);
1468 /* HeapUnlock(GetProcessHeap()); */
1474 /***********************************************************************
1475 * GlobalCompact32 (KERNEL32.316)
1477 DWORD WINAPI
GlobalCompact( DWORD minfree
)
1479 return 0; /* GlobalCompact does nothing in Win32 */
1483 /***********************************************************************
1484 * GlobalMemoryStatus (KERNEL32.327)
1488 VOID WINAPI
GlobalMemoryStatus(
1489 LPMEMORYSTATUS lpmem
1492 FILE *f
= fopen( "/proc/meminfo", "r" );
1496 int total
, used
, free
, shared
, buffers
, cached
;
1498 lpmem
->dwLength
= sizeof(MEMORYSTATUS
);
1499 lpmem
->dwTotalPhys
= lpmem
->dwAvailPhys
= 0;
1500 lpmem
->dwTotalPageFile
= lpmem
->dwAvailPageFile
= 0;
1501 while (fgets( buffer
, sizeof(buffer
), f
))
1503 /* old style /proc/meminfo ... */
1504 if (sscanf( buffer
, "Mem: %d %d %d %d %d %d", &total
, &used
, &free
, &shared
, &buffers
, &cached
))
1506 lpmem
->dwTotalPhys
+= total
;
1507 lpmem
->dwAvailPhys
+= free
+ buffers
+ cached
;
1509 if (sscanf( buffer
, "Swap: %d %d %d", &total
, &used
, &free
))
1511 lpmem
->dwTotalPageFile
+= total
;
1512 lpmem
->dwAvailPageFile
+= free
;
1515 /* new style /proc/meminfo ... */
1516 if (sscanf(buffer
, "MemTotal: %d", &total
))
1517 lpmem
->dwTotalPhys
= total
*1024;
1518 if (sscanf(buffer
, "MemFree: %d", &free
))
1519 lpmem
->dwAvailPhys
= free
*1024;
1520 if (sscanf(buffer
, "SwapTotal: %d", &total
))
1521 lpmem
->dwTotalPageFile
= total
*1024;
1522 if (sscanf(buffer
, "SwapFree: %d", &free
))
1523 lpmem
->dwAvailPageFile
= free
*1024;
1524 if (sscanf(buffer
, "Buffers: %d", &buffers
))
1525 lpmem
->dwAvailPhys
+= buffers
*1024;
1526 if (sscanf(buffer
, "Cached: %d", &cached
))
1527 lpmem
->dwAvailPhys
+= cached
*1024;
1531 if (lpmem
->dwTotalPhys
)
1533 lpmem
->dwTotalVirtual
= lpmem
->dwTotalPhys
+lpmem
->dwTotalPageFile
;
1534 lpmem
->dwAvailVirtual
= lpmem
->dwAvailPhys
+lpmem
->dwAvailPageFile
;
1535 lpmem
->dwMemoryLoad
= (lpmem
->dwTotalVirtual
-lpmem
->dwAvailVirtual
)
1536 / (lpmem
->dwTotalVirtual
/ 100);
1541 /* FIXME: should do something for other systems */
1542 lpmem
->dwMemoryLoad
= 0;
1543 lpmem
->dwTotalPhys
= 16*1024*1024;
1544 lpmem
->dwAvailPhys
= 16*1024*1024;
1545 lpmem
->dwTotalPageFile
= 16*1024*1024;
1546 lpmem
->dwAvailPageFile
= 16*1024*1024;
1547 lpmem
->dwTotalVirtual
= 32*1024*1024;
1548 lpmem
->dwAvailVirtual
= 32*1024*1024;
1552 /**********************************************************************
1553 * WOWGlobalAllocLock (KERNEL32.62)
1555 * Combined GlobalAlloc and GlobalLock.
1557 SEGPTR WINAPI
WOWGlobalAllocLock16(DWORD flags
,DWORD cb
,HGLOBAL16
*hmem
)
1560 xhmem
= GlobalAlloc16(flags
,cb
);
1561 if (hmem
) *hmem
= xhmem
;
1562 return WIN16_GlobalLock16(xhmem
);
1566 /**********************************************************************
1567 * WOWGlobalUnlockFree (KERNEL32.64)
1569 * Combined GlobalUnlock and GlobalFree.
1571 WORD WINAPI
WOWGlobalUnlockFree16(DWORD vpmem
) {
1572 if (!GlobalUnlock16(HIWORD(vpmem
)))
1574 return GlobalFree16(HIWORD(vpmem
));