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"
47 WINE_DEFAULT_DEBUG_CHANNEL(global
);
49 /* Global arena block */
52 DWORD_PTR base
; /* Base address (0 if discarded) */
53 DWORD size
; /* Size in bytes (0 indicates a free block) */
54 HGLOBAL16 handle
; /* Handle for this block */
55 HGLOBAL16 hOwner
; /* Owner of this block */
56 BYTE lockCount
; /* Count of GlobalFix() calls */
57 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
58 BYTE flags
; /* Allocation flags */
59 BYTE selCount
; /* Number of selectors allocated for this block */
62 /* Flags definitions */
63 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
64 #define GA_DGROUP 0x04
65 #define GA_DISCARDABLE 0x08
66 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
67 #define GA_DOSMEM 0x20
69 /* Arena array (FIXME) */
70 static GLOBALARENA
*pGlobalArena
;
71 static int globalArenaSize
;
73 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
75 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
76 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
78 static inline void* DOSMEM_AllocBlock(UINT size
, UINT16
* pseg
)
80 if (!winedos
.AllocDosBlock
) load_winedos();
81 return winedos
.AllocDosBlock
? winedos
.AllocDosBlock(size
, pseg
) : NULL
;
84 static inline BOOL
DOSMEM_FreeBlock(void* ptr
)
86 if (!winedos
.FreeDosBlock
) load_winedos();
87 return winedos
.FreeDosBlock
? winedos
.FreeDosBlock( ptr
) : FALSE
;
90 static inline UINT
DOSMEM_ResizeBlock(void *ptr
, UINT size
, BOOL exact
)
92 if (!winedos
.ResizeDosBlock
) load_winedos();
93 return winedos
.ResizeDosBlock
? winedos
.ResizeDosBlock(ptr
, size
, TRUE
) : 0;
96 /***********************************************************************
99 * Return the arena for a given selector, growing the arena array if needed.
101 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
103 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
105 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
106 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
107 newsize
* sizeof(GLOBALARENA
) );
108 if (!pNewArena
) return 0;
109 pGlobalArena
= pNewArena
;
110 memset( pGlobalArena
+ globalArenaSize
, 0,
111 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
112 globalArenaSize
= newsize
;
114 return pGlobalArena
+ (sel
>> __AHSHIFT
);
117 void debug_handles(void)
121 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
122 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
124 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
132 /***********************************************************************
135 * Create a global heap block for a fixed range of linear memory.
137 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
138 HGLOBAL16 hOwner
, unsigned char selflags
)
143 /* Allocate the selector(s) */
145 sel
= SELECTOR_AllocBlock( ptr
, size
, selflags
);
147 selcount
= (size
+ 0xffff) / 0x10000;
149 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
151 SELECTOR_FreeBlock( sel
);
155 /* Fill the arena block */
157 pArena
->base
= (DWORD_PTR
)ptr
;
158 pArena
->size
= GetSelectorLimit16(sel
) + 1;
159 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
160 pArena
->hOwner
= hOwner
;
161 pArena
->lockCount
= 0;
162 pArena
->pageLockCount
= 0;
163 pArena
->flags
= flags
& GA_MOVEABLE
;
164 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
165 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
166 if (!(selflags
& (WINE_LDT_FLAGS_CODE
^WINE_LDT_FLAGS_DATA
))) pArena
->flags
|= GA_DGROUP
;
167 pArena
->selCount
= selcount
;
168 if (selcount
> 1) /* clear the next arena blocks */
169 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
171 return pArena
->handle
;
175 /***********************************************************************
178 * Free a block allocated by GLOBAL_CreateBlock, without touching
179 * the associated linear memory range.
181 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
186 if (!handle
) return TRUE
;
187 sel
= GlobalHandleToSel16( handle
);
188 if (!VALID_HANDLE(sel
)) return FALSE
;
189 pArena
= GET_ARENA_PTR(sel
);
190 SELECTOR_FreeBlock( sel
);
191 memset( pArena
, 0, sizeof(GLOBALARENA
) );
195 /***********************************************************************
198 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, const void *ptr
, DWORD size
)
203 if (!handle
) return TRUE
;
204 sel
= GlobalHandleToSel16( handle
);
205 if (!VALID_HANDLE(sel
)) return FALSE
;
206 pArena
= GET_ARENA_PTR(sel
);
207 if (pArena
->selCount
!= 1)
210 pArena
->base
= (DWORD
)ptr
;
212 SELECTOR_ReallocBlock( sel
, ptr
, size
);
216 /***********************************************************************
219 * Implementation of GlobalAlloc16()
221 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
, unsigned char selflags
)
226 TRACE("%d flags=%04x\n", size
, flags
);
228 /* If size is 0, create a discarded block */
230 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, selflags
);
234 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
235 size
= (size
+ 0x1f) & ~0x1f;
237 /* Allocate the linear memory */
238 ptr
= HeapAlloc( GetProcessHeap(), 0, size
);
239 /* FIXME: free discardable blocks and try again? */
242 /* Allocate the selector(s) */
244 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
, selflags
);
247 HeapFree( GetProcessHeap(), 0, ptr
);
251 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
255 /***********************************************************************
256 * GlobalAlloc (KERNEL.15)
257 * GlobalAlloc16 (KERNEL32.24)
259 * Allocate a global memory object.
265 HGLOBAL16 WINAPI
GlobalAlloc16(
266 UINT16 flags
, /* [in] Object allocation attributes */
267 DWORD size
/* [in] Number of bytes to allocate */
269 HANDLE16 owner
= GetCurrentPDB16();
271 if (flags
& GMEM_DDESHARE
)
272 owner
= GetExePtr(owner
); /* Make it a module handle */
273 return GLOBAL_Alloc( flags
, size
, owner
, WINE_LDT_FLAGS_DATA
);
277 /***********************************************************************
278 * GlobalReAlloc (KERNEL.16)
280 * Change the size or attributes of a global memory object.
286 HGLOBAL16 WINAPI
GlobalReAlloc16(
287 HGLOBAL16 handle
, /* [in] Handle of global memory object */
288 DWORD size
, /* [in] New size of block */
289 UINT16 flags
/* [in] How to reallocate object */
294 GLOBALARENA
*pArena
, *pNewArena
;
295 WORD sel
= GlobalHandleToSel16( handle
);
297 TRACE("%04x %d flags=%04x\n",
298 handle
, size
, flags
);
299 if (!handle
) return 0;
301 if (!VALID_HANDLE(handle
))
303 WARN("Invalid handle 0x%04x!\n", handle
);
306 pArena
= GET_ARENA_PTR( handle
);
308 /* Discard the block if requested */
310 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
312 if (!(pArena
->flags
& GA_MOVEABLE
) ||
313 !(pArena
->flags
& GA_DISCARDABLE
) ||
314 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
315 if (pArena
->flags
& GA_DOSMEM
)
316 DOSMEM_FreeBlock( (void *)pArena
->base
);
318 HeapFree( GetProcessHeap(), 0, (void *)pArena
->base
);
321 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
322 * change the selector if we are shrinking the block.
323 * FIXME: shouldn't we keep selectors until the block is deleted?
325 SELECTOR_ReallocBlock( sel
, 0, 1 );
331 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
332 if (size
== 0) size
= 0x20;
333 else size
= (size
+ 0x1f) & ~0x1f;
335 /* Change the flags */
337 if (flags
& GMEM_MODIFY
)
339 /* Change the flags, leaving GA_DGROUP alone */
340 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
341 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
345 /* Reallocate the linear memory */
347 ptr
= (void *)pArena
->base
;
348 oldsize
= pArena
->size
;
349 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr
,oldsize
,size
);
350 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
352 if (pArena
->flags
& GA_DOSMEM
)
354 if (DOSMEM_ResizeBlock(ptr
, size
, TRUE
) == size
)
356 else if(pArena
->pageLockCount
> 0)
360 newptr
= DOSMEM_AllocBlock( size
, 0 );
363 memcpy( newptr
, ptr
, oldsize
);
364 DOSMEM_FreeBlock( ptr
);
371 * if more than one reader (e.g. some pointer has been
372 * given out by GetVDMPointer32W16),
373 * only try to realloc in place
377 newptr
= HeapReAlloc( GetProcessHeap(),
378 (pArena
->pageLockCount
> 0) ? HEAP_REALLOC_IN_PLACE_ONLY
: 0,
381 newptr
= HeapAlloc( GetProcessHeap(),
382 (pArena
->pageLockCount
> 0) ? HEAP_REALLOC_IN_PLACE_ONLY
: 0,
389 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
390 if (pArena
->pageLockCount
<1)
392 if (pArena
->flags
& GA_DOSMEM
)
393 DOSMEM_FreeBlock( (void *)pArena
->base
);
395 HeapFree( GetProcessHeap(), 0, ptr
);
396 SELECTOR_FreeBlock( sel
);
397 memset( pArena
, 0, sizeof(GLOBALARENA
) );
403 /* Reallocate the selector(s) */
405 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
408 if (pArena
->flags
& GA_DOSMEM
)
409 DOSMEM_FreeBlock( (void *)pArena
->base
);
411 HeapFree( GetProcessHeap(), 0, ptr
);
412 memset( pArena
, 0, sizeof(GLOBALARENA
) );
415 selcount
= (size
+ 0xffff) / 0x10000;
417 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
419 if (pArena
->flags
& GA_DOSMEM
)
420 DOSMEM_FreeBlock( (void *)pArena
->base
);
422 HeapFree( GetProcessHeap(), 0, ptr
);
423 SELECTOR_FreeBlock( sel
);
427 /* Fill the new arena block
428 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
430 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
431 pNewArena
->base
= (DWORD
)ptr
;
432 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
433 pNewArena
->selCount
= selcount
;
434 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
436 if (selcount
> 1) /* clear the next arena blocks */
437 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
439 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
440 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
441 return pNewArena
->handle
;
445 /***********************************************************************
446 * GlobalFree (KERNEL.17)
447 * GlobalFree16 (KERNEL32.31)
452 HGLOBAL16 WINAPI
GlobalFree16(
453 HGLOBAL16 handle
/* [in] Handle of global memory object */
457 if (!VALID_HANDLE(handle
))
459 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
462 ptr
= (void *)GET_ARENA_PTR(handle
)->base
;
464 TRACE("%04x\n", handle
);
465 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
466 HeapFree( GetProcessHeap(), 0, ptr
);
471 /**********************************************************************
472 * K32WOWGlobalLock16 (KERNEL32.60)
474 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 handle
)
476 WORD sel
= GlobalHandleToSel16( handle
);
477 TRACE("(%04x) -> %08x\n", handle
, MAKELONG( 0, sel
) );
481 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
483 if (!VALID_HANDLE(handle
)) {
484 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
487 else if (!GET_ARENA_PTR(handle
)->base
)
490 GET_ARENA_PTR(handle
)->lockCount
++;
493 return MAKESEGPTR( sel
, 0 );
498 /***********************************************************************
499 * GlobalLock (KERNEL.18)
501 * This is the GlobalLock16() function used by 16-bit code.
503 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
505 SEGPTR ret
= K32WOWGlobalLock16( handle
);
506 CURRENT_STACK16
->ecx
= SELECTOROF(ret
); /* selector must be returned in CX as well */
511 /***********************************************************************
512 * GlobalLock16 (KERNEL32.25)
514 * This is the GlobalLock16() function used by 32-bit code.
517 * Pointer to first byte of memory block
520 LPVOID WINAPI
GlobalLock16(
521 HGLOBAL16 handle
/* [in] Handle of global memory object */
523 if (!handle
) return 0;
524 if (!VALID_HANDLE(handle
))
526 GET_ARENA_PTR(handle
)->lockCount
++;
527 return (LPVOID
)GET_ARENA_PTR(handle
)->base
;
531 /***********************************************************************
532 * GlobalUnlock (KERNEL.19)
533 * GlobalUnlock16 (KERNEL32.26)
535 * Should the return values be cast to booleans?
538 * TRUE: Object is still locked
539 * FALSE: Object is unlocked
541 BOOL16 WINAPI
GlobalUnlock16(
542 HGLOBAL16 handle
/* [in] Handle of global memory object */
544 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
545 if (!VALID_HANDLE(handle
)) {
546 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
549 TRACE("%04x\n", handle
);
550 if (pArena
->lockCount
) pArena
->lockCount
--;
551 return pArena
->lockCount
;
554 /***********************************************************************
555 * GlobalChangeLockCount (KERNEL.365)
557 * This is declared as a register function as it has to preserve
558 * *all* registers, even AX/DX !
561 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
565 GlobalLock16( handle
);
566 else if ( delta
== -1 )
567 GlobalUnlock16( handle
);
569 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
572 /***********************************************************************
573 * GlobalSize (KERNEL.20)
574 * GlobalSize16 (KERNEL32.32)
576 * Get the current size of a global memory object.
579 * Size in bytes of object
582 DWORD WINAPI
GlobalSize16(
583 HGLOBAL16 handle
/* [in] Handle of global memory object */
585 TRACE("%04x\n", handle
);
586 if (!handle
) return 0;
587 if (!VALID_HANDLE(handle
))
589 return GET_ARENA_PTR(handle
)->size
;
593 /***********************************************************************
594 * GlobalHandle (KERNEL.21)
596 * Get the handle associated with a pointer to the global memory block.
599 * Why is GlobalHandleToSel used here with the sel as input?
605 DWORD WINAPI
GlobalHandle16(
606 WORD sel
/* [in] Address of global memory block */
608 TRACE("%04x\n", sel
);
609 if (!VALID_HANDLE(sel
)) {
610 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
613 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
616 /***********************************************************************
617 * GlobalHandleNoRIP (KERNEL.159)
619 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
622 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
623 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
624 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
630 /***********************************************************************
631 * GlobalFlags (KERNEL.22)
633 * Get information about a global memory object.
636 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
640 * Value specifying flags and lock count
641 * GMEM_INVALID_HANDLE: Invalid handle
643 UINT16 WINAPI
GlobalFlags16(
644 HGLOBAL16 handle
/* [in] Handle of global memory object */
648 TRACE("%04x\n", handle
);
649 if (!VALID_HANDLE(handle
)) {
650 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
653 pArena
= GET_ARENA_PTR(handle
);
654 return pArena
->lockCount
|
655 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
656 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
660 /***********************************************************************
661 * LockSegment (KERNEL.23)
663 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
665 TRACE("%04x\n", handle
);
666 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
667 if (!VALID_HANDLE(handle
)) {
668 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
671 GET_ARENA_PTR(handle
)->lockCount
++;
676 /***********************************************************************
677 * UnlockSegment (KERNEL.24)
679 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
681 TRACE("%04x\n", handle
);
682 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
683 if (!VALID_HANDLE(handle
)) {
684 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
687 GET_ARENA_PTR(handle
)->lockCount
--;
688 /* FIXME: this ought to return the lock count in CX (go figure...) */
692 /***********************************************************************
693 * GlobalCompact (KERNEL.25)
695 DWORD WINAPI
GlobalCompact16( DWORD desired
)
697 return GLOBAL_MAX_ALLOC_SIZE
;
701 /***********************************************************************
702 * GlobalFreeAll (KERNEL.26)
704 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
709 pArena
= pGlobalArena
;
710 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
712 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
713 GlobalFree16( pArena
->handle
);
718 /***********************************************************************
719 * GlobalWire (KERNEL.111)
720 * GlobalWire16 (KERNEL32.29)
722 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
724 return WIN16_GlobalLock16( handle
);
728 /***********************************************************************
729 * GlobalUnWire (KERNEL.112)
730 * GlobalUnWire16 (KERNEL32.30)
732 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
734 return !GlobalUnlock16( handle
);
738 /***********************************************************************
739 * SetSwapAreaSize (KERNEL.106)
741 LONG WINAPI
SetSwapAreaSize16( WORD size
)
743 FIXME("(%d) - stub!\n", size
);
744 return MAKELONG( size
, 0xffff );
748 /***********************************************************************
749 * GlobalLRUOldest (KERNEL.163)
751 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
753 TRACE("%04x\n", handle
);
754 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
759 /***********************************************************************
760 * GlobalLRUNewest (KERNEL.164)
762 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
764 TRACE("%04x\n", handle
);
765 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
770 /***********************************************************************
771 * GetFreeSpace (KERNEL.169)
773 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
776 GlobalMemoryStatus( &ms
);
777 return ms
.dwAvailVirtual
;
780 /***********************************************************************
781 * GlobalDOSAlloc (KERNEL.184)
783 * Allocate memory in the first MB.
786 * Address (HW=Paragraph segment; LW=Selector)
788 DWORD WINAPI
GlobalDOSAlloc16(
789 DWORD size
/* [in] Number of bytes to be allocated */
792 LPVOID lpBlock
= DOSMEM_AllocBlock( size
, &uParagraph
);
796 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
800 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
801 pArena
= GET_ARENA_PTR(wSelector
);
802 pArena
->flags
|= GA_DOSMEM
;
803 return MAKELONG(wSelector
,uParagraph
);
809 /***********************************************************************
810 * GlobalDOSFree (KERNEL.185)
812 * Free memory allocated with GlobalDOSAlloc
818 WORD WINAPI
GlobalDOSFree16(
819 WORD sel
/* [in] Selector */
821 DWORD block
= GetSelectorBase(sel
);
823 if( block
&& block
< 0x100000 )
825 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
826 if( DOSMEM_FreeBlock( lpBlock
) )
827 GLOBAL_FreeBlock( sel
);
834 /***********************************************************************
835 * GlobalPageLock (KERNEL.191)
836 * GlobalSmartPageLock(KERNEL.230)
838 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
840 TRACE("%04x\n", handle
);
841 if (!VALID_HANDLE(handle
)) {
842 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
845 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
849 /***********************************************************************
850 * GlobalPageUnlock (KERNEL.192)
851 * GlobalSmartPageUnlock(KERNEL.231)
853 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
855 TRACE("%04x\n", handle
);
856 if (!VALID_HANDLE(handle
)) {
857 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
860 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
864 /***********************************************************************
865 * GlobalFix (KERNEL.197)
866 * GlobalFix16 (KERNEL32.27)
868 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
870 TRACE("%04x\n", handle
);
871 if (!VALID_HANDLE(handle
)) {
872 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
875 GET_ARENA_PTR(handle
)->lockCount
++;
877 return GlobalHandleToSel16(handle
);
881 /***********************************************************************
882 * GlobalUnfix (KERNEL.198)
883 * GlobalUnfix16 (KERNEL32.28)
885 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
887 TRACE("%04x\n", handle
);
888 if (!VALID_HANDLE(handle
)) {
889 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
892 GET_ARENA_PTR(handle
)->lockCount
--;
896 /***********************************************************************
897 * FarSetOwner (KERNEL.403)
899 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
901 if (!VALID_HANDLE(handle
)) {
902 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
905 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
909 /***********************************************************************
910 * FarGetOwner (KERNEL.404)
912 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
914 if (!VALID_HANDLE(handle
)) {
915 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
918 return GET_ARENA_PTR(handle
)->hOwner
;
922 /************************************************************************
923 * GlobalMasterHandle (KERNEL.28)
926 * Should return selector and handle of the information structure for
927 * the global heap. selector and handle are stored in the THHOOK as
928 * pGlobalHeap and hGlobalHeap.
929 * As Wine doesn't have this structure, we return both values as zero
930 * Applications should interpret this as "No Global Heap"
932 DWORD WINAPI
GlobalMasterHandle16(void)
938 /***********************************************************************
939 * GlobalHandleToSel (TOOLHELP.50)
941 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
943 if (!handle
) return 0;
944 if (!VALID_HANDLE(handle
)) {
945 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
950 WARN("Program attempted invalid selector conversion\n" );
957 /***********************************************************************
958 * GlobalFirst (TOOLHELP.51)
960 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
962 if (wFlags
== GLOBAL_LRU
) return FALSE
;
964 return GlobalNext16( pGlobal
, wFlags
);
968 /***********************************************************************
969 * GlobalNext (TOOLHELP.52)
971 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
975 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
976 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
977 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
980 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
981 if (pArena
->size
== 0) break; /* block is free */
982 if (i
>= globalArenaSize
) return FALSE
;
986 pGlobal
->dwAddress
= pArena
->base
;
987 pGlobal
->dwBlockSize
= pArena
->size
;
988 pGlobal
->hBlock
= pArena
->handle
;
989 pGlobal
->wcLock
= pArena
->lockCount
;
990 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
991 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
992 pGlobal
->wHeapPresent
= FALSE
;
993 pGlobal
->hOwner
= pArena
->hOwner
;
994 pGlobal
->wType
= GT_UNKNOWN
;
1001 /***********************************************************************
1002 * GlobalInfo (TOOLHELP.53)
1004 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
1007 GLOBALARENA
*pArena
;
1009 pInfo
->wcItems
= globalArenaSize
;
1010 pInfo
->wcItemsFree
= 0;
1011 pInfo
->wcItemsLRU
= 0;
1012 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
1013 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
1018 /***********************************************************************
1019 * GlobalEntryHandle (TOOLHELP.54)
1021 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
1023 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
1025 pGlobal
->dwAddress
= pArena
->base
;
1026 pGlobal
->dwBlockSize
= pArena
->size
;
1027 pGlobal
->hBlock
= pArena
->handle
;
1028 pGlobal
->wcLock
= pArena
->lockCount
;
1029 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
1030 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
1031 pGlobal
->wHeapPresent
= FALSE
;
1032 pGlobal
->hOwner
= pArena
->hOwner
;
1033 pGlobal
->wType
= GT_UNKNOWN
;
1040 /***********************************************************************
1041 * GlobalEntryModule (TOOLHELP.55)
1043 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
1046 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
1051 /***********************************************************************
1052 * MemManInfo (TOOLHELP.72)
1054 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
1056 MEMORYSTATUS status
;
1059 * Not unsurprisingly although the documention says you
1060 * _must_ provide the size in the dwSize field, this function
1061 * (under Windows) always fills the structure and returns true.
1063 GlobalMemoryStatus( &status
);
1064 info
->wPageSize
= getpagesize();
1065 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1066 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1067 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1068 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1069 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1070 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1071 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1072 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1073 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1077 /***********************************************************************
1078 * GetFreeMemInfo (KERNEL.316)
1080 DWORD WINAPI
GetFreeMemInfo16(void)
1083 MemManInfo16( &info
);
1084 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1087 /***********************************************************************
1088 * A20Proc (KERNEL.165)
1090 void WINAPI
A20Proc16( WORD unused
)
1092 /* this is also a NOP in Windows */
1095 /***********************************************************************
1096 * LimitEMSPages (KERNEL.156)
1098 DWORD WINAPI
LimitEMSPages16( DWORD unused
)