2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* 0xffff sometimes seems to mean: CURRENT_DS */
23 #include "wine/port.h"
25 #include <sys/types.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
40 #include "wine/winbase16.h"
43 #include "kernel_private.h"
44 #include "kernel16_private.h"
45 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(global
);
50 /* Global arena block */
53 DWORD_PTR base
; /* Base address (0 if discarded) */
54 DWORD size
; /* Size in bytes (0 indicates a free block) */
55 HGLOBAL16 handle
; /* Handle for this block */
56 HGLOBAL16 hOwner
; /* Owner of this block */
57 BYTE lockCount
; /* Count of GlobalFix() calls */
58 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
59 BYTE flags
; /* Allocation flags */
60 BYTE selCount
; /* Number of selectors allocated for this block */
63 /* Flags definitions */
64 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
65 #define GA_DGROUP 0x04
66 #define GA_DISCARDABLE 0x08
67 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
68 #define GA_DOSMEM 0x20
70 /* Arena array (FIXME) */
71 static GLOBALARENA
*pGlobalArena
;
72 static int globalArenaSize
;
74 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
76 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
77 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
79 static inline void* DOSMEM_AllocBlock(UINT size
, UINT16
* pseg
)
81 if (!winedos
.AllocDosBlock
) load_winedos();
82 return winedos
.AllocDosBlock
? winedos
.AllocDosBlock(size
, pseg
) : NULL
;
85 static inline BOOL
DOSMEM_FreeBlock(void* ptr
)
87 if (!winedos
.FreeDosBlock
) load_winedos();
88 return winedos
.FreeDosBlock
? winedos
.FreeDosBlock( ptr
) : FALSE
;
91 static inline UINT
DOSMEM_ResizeBlock(void *ptr
, UINT size
, BOOL exact
)
93 if (!winedos
.ResizeDosBlock
) load_winedos();
94 return winedos
.ResizeDosBlock
? winedos
.ResizeDosBlock(ptr
, size
, TRUE
) : 0;
97 /***********************************************************************
100 * Return the arena for a given selector, growing the arena array if needed.
102 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
104 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
106 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
107 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
108 newsize
* sizeof(GLOBALARENA
) );
109 if (!pNewArena
) return 0;
110 pGlobalArena
= pNewArena
;
111 memset( pGlobalArena
+ globalArenaSize
, 0,
112 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
113 globalArenaSize
= newsize
;
115 return pGlobalArena
+ (sel
>> __AHSHIFT
);
118 void debug_handles(void)
122 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
123 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
125 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
133 /***********************************************************************
136 * Create a global heap block for a fixed range of linear memory.
138 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
139 HGLOBAL16 hOwner
, unsigned char selflags
)
144 /* Allocate the selector(s) */
146 sel
= SELECTOR_AllocBlock( ptr
, size
, selflags
);
148 selcount
= (size
+ 0xffff) / 0x10000;
150 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
152 SELECTOR_FreeBlock( sel
);
156 /* Fill the arena block */
158 pArena
->base
= (DWORD_PTR
)ptr
;
159 pArena
->size
= GetSelectorLimit16(sel
) + 1;
160 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
161 pArena
->hOwner
= hOwner
;
162 pArena
->lockCount
= 0;
163 pArena
->pageLockCount
= 0;
164 pArena
->flags
= flags
& GA_MOVEABLE
;
165 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
166 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
167 if (!(selflags
& (WINE_LDT_FLAGS_CODE
^WINE_LDT_FLAGS_DATA
))) pArena
->flags
|= GA_DGROUP
;
168 pArena
->selCount
= selcount
;
169 if (selcount
> 1) /* clear the next arena blocks */
170 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
172 return pArena
->handle
;
176 /***********************************************************************
179 * Free a block allocated by GLOBAL_CreateBlock, without touching
180 * the associated linear memory range.
182 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
187 if (!handle
) return TRUE
;
188 sel
= GlobalHandleToSel16( handle
);
189 if (!VALID_HANDLE(sel
)) return FALSE
;
190 pArena
= GET_ARENA_PTR(sel
);
191 SELECTOR_FreeBlock( sel
);
192 memset( pArena
, 0, sizeof(GLOBALARENA
) );
196 /***********************************************************************
199 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
204 if (!handle
) return TRUE
;
205 sel
= GlobalHandleToSel16( handle
);
206 if (!VALID_HANDLE(sel
)) return FALSE
;
207 pArena
= GET_ARENA_PTR(sel
);
208 if (pArena
->selCount
!= 1)
211 pArena
->base
= (DWORD
)ptr
;
213 SELECTOR_ReallocBlock( sel
, ptr
, size
);
217 /***********************************************************************
220 * Implementation of GlobalAlloc16()
222 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
, unsigned char selflags
)
227 TRACE("%ld flags=%04x\n", size
, flags
);
229 /* If size is 0, create a discarded block */
231 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, selflags
);
235 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
236 size
= (size
+ 0x1f) & ~0x1f;
238 /* Allocate the linear memory */
239 ptr
= HeapAlloc( GetProcessHeap(), 0, size
);
240 /* FIXME: free discardable blocks and try again? */
243 /* Allocate the selector(s) */
245 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
, selflags
);
248 HeapFree( GetProcessHeap(), 0, ptr
);
252 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
256 /***********************************************************************
257 * GlobalAlloc (KERNEL.15)
258 * GlobalAlloc16 (KERNEL32.24)
260 * Allocate a global memory object.
266 HGLOBAL16 WINAPI
GlobalAlloc16(
267 UINT16 flags
, /* [in] Object allocation attributes */
268 DWORD size
/* [in] Number of bytes to allocate */
270 HANDLE16 owner
= GetCurrentPDB16();
272 if (flags
& GMEM_DDESHARE
)
273 owner
= GetExePtr(owner
); /* Make it a module handle */
274 return GLOBAL_Alloc( flags
, size
, owner
, WINE_LDT_FLAGS_DATA
);
278 /***********************************************************************
279 * GlobalReAlloc (KERNEL.16)
281 * Change the size or attributes of a global memory object.
287 HGLOBAL16 WINAPI
GlobalReAlloc16(
288 HGLOBAL16 handle
, /* [in] Handle of global memory object */
289 DWORD size
, /* [in] New size of block */
290 UINT16 flags
/* [in] How to reallocate object */
295 GLOBALARENA
*pArena
, *pNewArena
;
296 WORD sel
= GlobalHandleToSel16( handle
);
298 TRACE("%04x %ld flags=%04x\n",
299 handle
, size
, flags
);
300 if (!handle
) return 0;
302 if (!VALID_HANDLE(handle
))
304 WARN("Invalid handle 0x%04x!\n", handle
);
307 pArena
= GET_ARENA_PTR( handle
);
309 /* Discard the block if requested */
311 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
313 if (!(pArena
->flags
& GA_MOVEABLE
) ||
314 !(pArena
->flags
& GA_DISCARDABLE
) ||
315 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
316 if (pArena
->flags
& GA_DOSMEM
)
317 DOSMEM_FreeBlock( (void *)pArena
->base
);
319 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
322 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
323 * change the selector if we are shrinking the block.
324 * FIXME: shouldn't we keep selectors until the block is deleted?
326 SELECTOR_ReallocBlock( sel
, 0, 1 );
332 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
333 if (size
== 0) size
= 0x20;
334 else size
= (size
+ 0x1f) & ~0x1f;
336 /* Change the flags */
338 if (flags
& GMEM_MODIFY
)
340 /* Change the flags, leaving GA_DGROUP alone */
341 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
342 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
346 /* Reallocate the linear memory */
348 ptr
= (void *)pArena
->base
;
349 oldsize
= pArena
->size
;
350 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr
,oldsize
,size
);
351 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
353 if (pArena
->flags
& GA_DOSMEM
)
355 if (DOSMEM_ResizeBlock(ptr
, size
, TRUE
) == size
)
357 else if(pArena
->pageLockCount
> 0)
361 newptr
= DOSMEM_AllocBlock( size
, 0 );
364 memcpy( newptr
, ptr
, oldsize
);
365 DOSMEM_FreeBlock( ptr
);
372 * if more than one reader (e.g. some pointer has been
373 * given out by GetVDMPointer32W16),
374 * only try to realloc in place
378 newptr
= HeapReAlloc( GetProcessHeap(),
379 (pArena
->pageLockCount
> 0) ? HEAP_REALLOC_IN_PLACE_ONLY
: 0,
382 newptr
= HeapAlloc( GetProcessHeap(),
383 (pArena
->pageLockCount
> 0) ? HEAP_REALLOC_IN_PLACE_ONLY
: 0,
390 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
391 if (pArena
->pageLockCount
<1)
393 if (pArena
->flags
& GA_DOSMEM
)
394 DOSMEM_FreeBlock( (void *)pArena
->base
);
396 HeapFree( GetProcessHeap(), 0, ptr
);
397 SELECTOR_FreeBlock( sel
);
398 memset( pArena
, 0, sizeof(GLOBALARENA
) );
404 /* Reallocate the selector(s) */
406 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
409 if (pArena
->flags
& GA_DOSMEM
)
410 DOSMEM_FreeBlock( (void *)pArena
->base
);
412 HeapFree( GetProcessHeap(), 0, ptr
);
413 memset( pArena
, 0, sizeof(GLOBALARENA
) );
416 selcount
= (size
+ 0xffff) / 0x10000;
418 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
420 if (pArena
->flags
& GA_DOSMEM
)
421 DOSMEM_FreeBlock( (void *)pArena
->base
);
423 HeapFree( GetProcessHeap(), 0, ptr
);
424 SELECTOR_FreeBlock( sel
);
428 /* Fill the new arena block
429 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
431 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
432 pNewArena
->base
= (DWORD
)ptr
;
433 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
434 pNewArena
->selCount
= selcount
;
435 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
437 if (selcount
> 1) /* clear the next arena blocks */
438 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
440 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
441 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
442 return pNewArena
->handle
;
446 /***********************************************************************
447 * GlobalFree (KERNEL.17)
448 * GlobalFree16 (KERNEL32.31)
453 HGLOBAL16 WINAPI
GlobalFree16(
454 HGLOBAL16 handle
/* [in] Handle of global memory object */
458 if (!VALID_HANDLE(handle
))
460 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
463 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
465 TRACE("%04x\n", handle
);
466 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
467 HeapFree( GetProcessHeap(), 0, ptr
);
472 /**********************************************************************
473 * K32WOWGlobalLock16 (KERNEL32.60)
475 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 handle
)
477 WORD sel
= GlobalHandleToSel16( handle
);
478 TRACE("(%04x) -> %08lx\n", handle
, MAKELONG( 0, sel
) );
482 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
484 if (!VALID_HANDLE(handle
)) {
485 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
488 else if (!GET_ARENA_PTR(handle
)->base
)
491 GET_ARENA_PTR(handle
)->lockCount
++;
494 return MAKESEGPTR( sel
, 0 );
499 /***********************************************************************
500 * GlobalLock (KERNEL.18)
502 * This is the GlobalLock16() function used by 16-bit code.
504 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
506 SEGPTR ret
= K32WOWGlobalLock16( handle
);
507 CURRENT_STACK16
->ecx
= SELECTOROF(ret
); /* selector must be returned in CX as well */
512 /***********************************************************************
513 * GlobalLock16 (KERNEL32.25)
515 * This is the GlobalLock16() function used by 32-bit code.
518 * Pointer to first byte of memory block
521 LPVOID WINAPI
GlobalLock16(
522 HGLOBAL16 handle
/* [in] Handle of global memory object */
524 if (!handle
) return 0;
525 if (!VALID_HANDLE(handle
))
527 GET_ARENA_PTR(handle
)->lockCount
++;
528 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
532 /***********************************************************************
533 * GlobalUnlock (KERNEL.19)
534 * GlobalUnlock16 (KERNEL32.26)
536 * Should the return values be cast to booleans?
539 * TRUE: Object is still locked
540 * FALSE: Object is unlocked
542 BOOL16 WINAPI
GlobalUnlock16(
543 HGLOBAL16 handle
/* [in] Handle of global memory object */
545 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
546 if (!VALID_HANDLE(handle
)) {
547 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
550 TRACE("%04x\n", handle
);
551 if (pArena
->lockCount
) pArena
->lockCount
--;
552 return pArena
->lockCount
;
555 /***********************************************************************
556 * GlobalChangeLockCount (KERNEL.365)
558 * This is declared as a register function as it has to preserve
559 * *all* registers, even AX/DX !
562 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
566 GlobalLock16( handle
);
567 else if ( delta
== -1 )
568 GlobalUnlock16( handle
);
570 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
573 /***********************************************************************
574 * GlobalSize (KERNEL.20)
575 * GlobalSize16 (KERNEL32.32)
577 * Get the current size of a global memory object.
580 * Size in bytes of object
583 DWORD WINAPI
GlobalSize16(
584 HGLOBAL16 handle
/* [in] Handle of global memory object */
586 TRACE("%04x\n", handle
);
587 if (!handle
) return 0;
588 if (!VALID_HANDLE(handle
))
590 return GET_ARENA_PTR(handle
)->size
;
594 /***********************************************************************
595 * GlobalHandle (KERNEL.21)
597 * Get the handle associated with a pointer to the global memory block.
600 * Why is GlobalHandleToSel used here with the sel as input?
606 DWORD WINAPI
GlobalHandle16(
607 WORD sel
/* [in] Address of global memory block */
609 TRACE("%04x\n", sel
);
610 if (!VALID_HANDLE(sel
)) {
611 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
614 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
617 /***********************************************************************
618 * GlobalHandleNoRIP (KERNEL.159)
620 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
623 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
624 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
625 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
631 /***********************************************************************
632 * GlobalFlags (KERNEL.22)
634 * Get information about a global memory object.
637 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
641 * Value specifying flags and lock count
642 * GMEM_INVALID_HANDLE: Invalid handle
644 UINT16 WINAPI
GlobalFlags16(
645 HGLOBAL16 handle
/* [in] Handle of global memory object */
649 TRACE("%04x\n", handle
);
650 if (!VALID_HANDLE(handle
)) {
651 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
654 pArena
= GET_ARENA_PTR(handle
);
655 return pArena
->lockCount
|
656 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
657 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
661 /***********************************************************************
662 * LockSegment (KERNEL.23)
664 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
666 TRACE("%04x\n", handle
);
667 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
668 if (!VALID_HANDLE(handle
)) {
669 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
672 GET_ARENA_PTR(handle
)->lockCount
++;
677 /***********************************************************************
678 * UnlockSegment (KERNEL.24)
680 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
682 TRACE("%04x\n", handle
);
683 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
684 if (!VALID_HANDLE(handle
)) {
685 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
688 GET_ARENA_PTR(handle
)->lockCount
--;
689 /* FIXME: this ought to return the lock count in CX (go figure...) */
693 /***********************************************************************
694 * GlobalCompact (KERNEL.25)
696 DWORD WINAPI
GlobalCompact16( DWORD desired
)
698 return GLOBAL_MAX_ALLOC_SIZE
;
702 /***********************************************************************
703 * GlobalFreeAll (KERNEL.26)
705 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
710 pArena
= pGlobalArena
;
711 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
713 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
714 GlobalFree16( pArena
->handle
);
719 /***********************************************************************
720 * GlobalWire (KERNEL.111)
721 * GlobalWire16 (KERNEL32.29)
723 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
725 return WIN16_GlobalLock16( handle
);
729 /***********************************************************************
730 * GlobalUnWire (KERNEL.112)
731 * GlobalUnWire16 (KERNEL32.30)
733 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
735 return !GlobalUnlock16( handle
);
739 /***********************************************************************
740 * SetSwapAreaSize (KERNEL.106)
742 LONG WINAPI
SetSwapAreaSize16( WORD size
)
744 FIXME("(%d) - stub!\n", size
);
745 return MAKELONG( size
, 0xffff );
749 /***********************************************************************
750 * GlobalLRUOldest (KERNEL.163)
752 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
754 TRACE("%04x\n", handle
);
755 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
760 /***********************************************************************
761 * GlobalLRUNewest (KERNEL.164)
763 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
765 TRACE("%04x\n", handle
);
766 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
771 /***********************************************************************
772 * GetFreeSpace (KERNEL.169)
774 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
777 GlobalMemoryStatus( &ms
);
778 return ms
.dwAvailVirtual
;
781 /***********************************************************************
782 * GlobalDOSAlloc (KERNEL.184)
784 * Allocate memory in the first MB.
787 * Address (HW=Paragraph segment; LW=Selector)
789 DWORD WINAPI
GlobalDOSAlloc16(
790 DWORD size
/* [in] Number of bytes to be allocated */
793 LPVOID lpBlock
= DOSMEM_AllocBlock( size
, &uParagraph
);
797 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
801 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
802 pArena
= GET_ARENA_PTR(wSelector
);
803 pArena
->flags
|= GA_DOSMEM
;
804 return MAKELONG(wSelector
,uParagraph
);
810 /***********************************************************************
811 * GlobalDOSFree (KERNEL.185)
813 * Free memory allocated with GlobalDOSAlloc
819 WORD WINAPI
GlobalDOSFree16(
820 WORD sel
/* [in] Selector */
822 DWORD block
= GetSelectorBase(sel
);
824 if( block
&& block
< 0x100000 )
826 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
827 if( DOSMEM_FreeBlock( lpBlock
) )
828 GLOBAL_FreeBlock( sel
);
835 /***********************************************************************
836 * GlobalPageLock (KERNEL.191)
837 * GlobalSmartPageLock(KERNEL.230)
839 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
841 TRACE("%04x\n", handle
);
842 if (!VALID_HANDLE(handle
)) {
843 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
846 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
850 /***********************************************************************
851 * GlobalPageUnlock (KERNEL.192)
852 * GlobalSmartPageUnlock(KERNEL.231)
854 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
856 TRACE("%04x\n", handle
);
857 if (!VALID_HANDLE(handle
)) {
858 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
861 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
865 /***********************************************************************
866 * GlobalFix (KERNEL.197)
867 * GlobalFix16 (KERNEL32.27)
869 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
871 TRACE("%04x\n", handle
);
872 if (!VALID_HANDLE(handle
)) {
873 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
876 GET_ARENA_PTR(handle
)->lockCount
++;
878 return GlobalHandleToSel16(handle
);
882 /***********************************************************************
883 * GlobalUnfix (KERNEL.198)
884 * GlobalUnfix16 (KERNEL32.28)
886 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
888 TRACE("%04x\n", handle
);
889 if (!VALID_HANDLE(handle
)) {
890 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
893 GET_ARENA_PTR(handle
)->lockCount
--;
897 /***********************************************************************
898 * FarSetOwner (KERNEL.403)
900 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
902 if (!VALID_HANDLE(handle
)) {
903 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
906 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
910 /***********************************************************************
911 * FarGetOwner (KERNEL.404)
913 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
915 if (!VALID_HANDLE(handle
)) {
916 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
919 return GET_ARENA_PTR(handle
)->hOwner
;
923 /************************************************************************
924 * GlobalMasterHandle (KERNEL.28)
927 * Should return selector and handle of the information structure for
928 * the global heap. selector and handle are stored in the THHOOK as
929 * pGlobalHeap and hGlobalHeap.
930 * As Wine doesn't have this structure, we return both values as zero
931 * Applications should interpret this as "No Global Heap"
933 DWORD WINAPI
GlobalMasterHandle16(void)
939 /***********************************************************************
940 * GlobalHandleToSel (TOOLHELP.50)
942 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
944 if (!handle
) return 0;
945 if (!VALID_HANDLE(handle
)) {
946 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
951 WARN("Program attempted invalid selector conversion\n" );
958 /***********************************************************************
959 * GlobalFirst (TOOLHELP.51)
961 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
963 if (wFlags
== GLOBAL_LRU
) return FALSE
;
965 return GlobalNext16( pGlobal
, wFlags
);
969 /***********************************************************************
970 * GlobalNext (TOOLHELP.52)
972 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
976 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
977 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
978 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
981 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
982 if (pArena
->size
== 0) break; /* block is free */
983 if (i
>= globalArenaSize
) return FALSE
;
987 pGlobal
->dwAddress
= pArena
->base
;
988 pGlobal
->dwBlockSize
= pArena
->size
;
989 pGlobal
->hBlock
= pArena
->handle
;
990 pGlobal
->wcLock
= pArena
->lockCount
;
991 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
992 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
993 pGlobal
->wHeapPresent
= FALSE
;
994 pGlobal
->hOwner
= pArena
->hOwner
;
995 pGlobal
->wType
= GT_UNKNOWN
;
1002 /***********************************************************************
1003 * GlobalInfo (TOOLHELP.53)
1005 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
1008 GLOBALARENA
*pArena
;
1010 pInfo
->wcItems
= globalArenaSize
;
1011 pInfo
->wcItemsFree
= 0;
1012 pInfo
->wcItemsLRU
= 0;
1013 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
1014 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
1019 /***********************************************************************
1020 * GlobalEntryHandle (TOOLHELP.54)
1022 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
1024 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
1026 pGlobal
->dwAddress
= pArena
->base
;
1027 pGlobal
->dwBlockSize
= pArena
->size
;
1028 pGlobal
->hBlock
= pArena
->handle
;
1029 pGlobal
->wcLock
= pArena
->lockCount
;
1030 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
1031 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
1032 pGlobal
->wHeapPresent
= FALSE
;
1033 pGlobal
->hOwner
= pArena
->hOwner
;
1034 pGlobal
->wType
= GT_UNKNOWN
;
1041 /***********************************************************************
1042 * GlobalEntryModule (TOOLHELP.55)
1044 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
1047 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
1052 /***********************************************************************
1053 * MemManInfo (TOOLHELP.72)
1055 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
1057 MEMORYSTATUS status
;
1060 * Not unsurprisingly although the documention says you
1061 * _must_ provide the size in the dwSize field, this function
1062 * (under Windows) always fills the structure and returns true.
1064 GlobalMemoryStatus( &status
);
1065 info
->wPageSize
= getpagesize();
1066 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1067 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1068 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1069 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1070 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1071 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1072 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1073 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1074 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1078 /***********************************************************************
1079 * GetFreeMemInfo (KERNEL.316)
1081 DWORD WINAPI
GetFreeMemInfo16(void)
1084 MemManInfo16( &info
);
1085 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1088 /***********************************************************************
1089 * A20Proc (KERNEL.165)
1091 void WINAPI
A20Proc16( WORD unused
)
1093 /* this is also a NOP in Windows */
1096 /***********************************************************************
1097 * LimitEMSPages (KERNEL.156)
1099 DWORD WINAPI
LimitEMSPages16( DWORD unused
)