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 void *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 static HANDLE
get_win16_heap(void)
98 static HANDLE win16_heap
;
100 /* we create global memory block with execute permission. The access can be limited
101 * for 16-bit code on selector level */
102 if (!win16_heap
) win16_heap
= HeapCreate(HEAP_CREATE_ENABLE_EXECUTE
, 0, 0);
106 /***********************************************************************
109 * Return the arena for a given selector, growing the arena array if needed.
111 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
113 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
115 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
116 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
117 newsize
* sizeof(GLOBALARENA
) );
118 if (!pNewArena
) return 0;
119 pGlobalArena
= pNewArena
;
120 memset( pGlobalArena
+ globalArenaSize
, 0,
121 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
122 globalArenaSize
= newsize
;
124 return pGlobalArena
+ (sel
>> __AHSHIFT
);
127 void debug_handles(void)
131 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
132 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
134 DPRINTF("0x%08x, ",pGlobalArena
[i
].handle
);
142 /***********************************************************************
145 * Create a global heap block for a fixed range of linear memory.
147 HGLOBAL16
GLOBAL_CreateBlock( WORD flags
, void *ptr
, DWORD size
,
148 HGLOBAL16 hOwner
, unsigned char selflags
)
153 /* Allocate the selector(s) */
155 sel
= SELECTOR_AllocBlock( ptr
, size
, selflags
);
157 selcount
= (size
+ 0xffff) / 0x10000;
159 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
161 SELECTOR_FreeBlock( sel
);
165 /* Fill the arena block */
168 pArena
->size
= GetSelectorLimit16(sel
) + 1;
169 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
170 pArena
->hOwner
= hOwner
;
171 pArena
->lockCount
= 0;
172 pArena
->pageLockCount
= 0;
173 pArena
->flags
= flags
& GA_MOVEABLE
;
174 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
175 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
176 if (!(selflags
& (WINE_LDT_FLAGS_CODE
^WINE_LDT_FLAGS_DATA
))) pArena
->flags
|= GA_DGROUP
;
177 pArena
->selCount
= selcount
;
178 if (selcount
> 1) /* clear the next arena blocks */
179 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
181 return pArena
->handle
;
185 /***********************************************************************
188 * Free a block allocated by GLOBAL_CreateBlock, without touching
189 * the associated linear memory range.
191 BOOL16
GLOBAL_FreeBlock( HGLOBAL16 handle
)
196 if (!handle
) return TRUE
;
197 sel
= GlobalHandleToSel16( handle
);
198 if (!VALID_HANDLE(sel
)) return FALSE
;
199 pArena
= GET_ARENA_PTR(sel
);
200 SELECTOR_FreeBlock( sel
);
201 memset( pArena
, 0, sizeof(GLOBALARENA
) );
205 /***********************************************************************
208 BOOL16
GLOBAL_MoveBlock( HGLOBAL16 handle
, void *ptr
, DWORD size
)
213 if (!handle
) return TRUE
;
214 sel
= GlobalHandleToSel16( handle
);
215 if (!VALID_HANDLE(sel
)) return FALSE
;
216 pArena
= GET_ARENA_PTR(sel
);
217 if (pArena
->selCount
!= 1)
222 SELECTOR_ReallocBlock( sel
, ptr
, size
);
226 /***********************************************************************
229 * Implementation of GlobalAlloc16()
231 HGLOBAL16
GLOBAL_Alloc( UINT16 flags
, DWORD size
, HGLOBAL16 hOwner
, unsigned char selflags
)
236 TRACE("%d flags=%04x\n", size
, flags
);
238 /* If size is 0, create a discarded block */
240 if (size
== 0) return GLOBAL_CreateBlock( flags
, NULL
, 1, hOwner
, selflags
);
244 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
245 size
= (size
+ 0x1f) & ~0x1f;
247 /* Allocate the linear memory */
248 ptr
= HeapAlloc( get_win16_heap(), 0, size
);
249 /* FIXME: free discardable blocks and try again? */
252 /* Allocate the selector(s) */
254 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
, selflags
);
257 HeapFree( get_win16_heap(), 0, ptr
);
261 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
265 /***********************************************************************
266 * GlobalAlloc (KERNEL.15)
267 * GlobalAlloc16 (KERNEL32.24)
269 * Allocate a global memory object.
275 HGLOBAL16 WINAPI
GlobalAlloc16(
276 UINT16 flags
, /* [in] Object allocation attributes */
277 DWORD size
/* [in] Number of bytes to allocate */
279 HANDLE16 owner
= GetCurrentPDB16();
281 if (flags
& GMEM_DDESHARE
)
282 owner
= GetExePtr(owner
); /* Make it a module handle */
283 return GLOBAL_Alloc( flags
, size
, owner
, WINE_LDT_FLAGS_DATA
);
287 /***********************************************************************
288 * GlobalReAlloc (KERNEL.16)
290 * Change the size or attributes of a global memory object.
296 HGLOBAL16 WINAPI
GlobalReAlloc16(
297 HGLOBAL16 handle
, /* [in] Handle of global memory object */
298 DWORD size
, /* [in] New size of block */
299 UINT16 flags
/* [in] How to reallocate object */
304 GLOBALARENA
*pArena
, *pNewArena
;
305 WORD sel
= GlobalHandleToSel16( handle
);
306 HANDLE heap
= get_win16_heap();
308 TRACE("%04x %d flags=%04x\n",
309 handle
, size
, flags
);
310 if (!handle
) return 0;
312 if (!VALID_HANDLE(handle
))
314 WARN("Invalid handle 0x%04x!\n", handle
);
317 pArena
= GET_ARENA_PTR( handle
);
319 /* Discard the block if requested */
321 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
323 if (!(pArena
->flags
& GA_MOVEABLE
) ||
324 !(pArena
->flags
& GA_DISCARDABLE
) ||
325 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
326 if (pArena
->flags
& GA_DOSMEM
)
327 DOSMEM_FreeBlock( pArena
->base
);
329 HeapFree( heap
, 0, pArena
->base
);
332 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
333 * change the selector if we are shrinking the block.
334 * FIXME: shouldn't we keep selectors until the block is deleted?
336 SELECTOR_ReallocBlock( sel
, 0, 1 );
342 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
343 if (size
== 0) size
= 0x20;
344 else size
= (size
+ 0x1f) & ~0x1f;
346 /* Change the flags */
348 if (flags
& GMEM_MODIFY
)
350 /* Change the flags, leaving GA_DGROUP alone */
351 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
352 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
356 /* Reallocate the linear memory */
359 oldsize
= pArena
->size
;
360 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr
,oldsize
,size
);
361 if (ptr
&& (size
== oldsize
)) return handle
; /* Nothing to do */
363 if (pArena
->flags
& GA_DOSMEM
)
365 if (DOSMEM_ResizeBlock(ptr
, size
, TRUE
) == size
)
367 else if(pArena
->pageLockCount
> 0)
371 newptr
= DOSMEM_AllocBlock( size
, 0 );
374 memcpy( newptr
, ptr
, oldsize
);
375 DOSMEM_FreeBlock( ptr
);
382 * if more than one reader (e.g. some pointer has been
383 * given out by GetVDMPointer32W16),
384 * only try to realloc in place
388 newptr
= HeapReAlloc( heap
,
389 (pArena
->pageLockCount
> 0) ? HEAP_REALLOC_IN_PLACE_ONLY
: 0,
392 newptr
= HeapAlloc( heap
, 0, size
);
398 FIXME("Realloc failed lock %d\n",pArena
->pageLockCount
);
399 if (pArena
->pageLockCount
<1)
401 if (pArena
->flags
& GA_DOSMEM
)
402 DOSMEM_FreeBlock( pArena
->base
);
404 HeapFree( heap
, 0, ptr
);
405 SELECTOR_FreeBlock( sel
);
406 memset( pArena
, 0, sizeof(GLOBALARENA
) );
412 /* Reallocate the selector(s) */
414 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
);
417 if (pArena
->flags
& GA_DOSMEM
)
418 DOSMEM_FreeBlock( pArena
->base
);
420 HeapFree( heap
, 0, ptr
);
421 memset( pArena
, 0, sizeof(GLOBALARENA
) );
424 selcount
= (size
+ 0xffff) / 0x10000;
426 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
428 if (pArena
->flags
& GA_DOSMEM
)
429 DOSMEM_FreeBlock( pArena
->base
);
431 HeapFree( heap
, 0, ptr
);
432 SELECTOR_FreeBlock( sel
);
436 /* Fill the new arena block
437 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
439 if (pNewArena
!= pArena
) memmove( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
440 pNewArena
->base
= ptr
;
441 pNewArena
->size
= GetSelectorLimit16(sel
) + 1;
442 pNewArena
->selCount
= selcount
;
443 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
445 if (selcount
> 1) /* clear the next arena blocks */
446 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
448 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
449 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
450 return pNewArena
->handle
;
454 /***********************************************************************
455 * GlobalFree (KERNEL.17)
456 * GlobalFree16 (KERNEL32.31)
461 HGLOBAL16 WINAPI
GlobalFree16(
462 HGLOBAL16 handle
/* [in] Handle of global memory object */
466 if (!VALID_HANDLE(handle
))
468 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle
);
471 ptr
= GET_ARENA_PTR(handle
)->base
;
473 TRACE("%04x\n", handle
);
474 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
475 HeapFree( get_win16_heap(), 0, ptr
);
480 /**********************************************************************
481 * K32WOWGlobalLock16 (KERNEL32.60)
483 SEGPTR WINAPI
K32WOWGlobalLock16( HGLOBAL16 handle
)
485 WORD sel
= GlobalHandleToSel16( handle
);
486 TRACE("(%04x) -> %08x\n", handle
, MAKELONG( 0, sel
) );
490 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
492 if (!VALID_HANDLE(handle
)) {
493 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle
);
496 else if (!GET_ARENA_PTR(handle
)->base
)
499 GET_ARENA_PTR(handle
)->lockCount
++;
502 return MAKESEGPTR( sel
, 0 );
507 /***********************************************************************
508 * GlobalLock (KERNEL.18)
510 * This is the GlobalLock16() function used by 16-bit code.
512 SEGPTR WINAPI
WIN16_GlobalLock16( HGLOBAL16 handle
)
514 SEGPTR ret
= K32WOWGlobalLock16( handle
);
515 CURRENT_STACK16
->ecx
= SELECTOROF(ret
); /* selector must be returned in CX as well */
520 /***********************************************************************
521 * GlobalLock16 (KERNEL32.25)
523 * This is the GlobalLock16() function used by 32-bit code.
526 * Pointer to first byte of memory block
529 LPVOID WINAPI
GlobalLock16(
530 HGLOBAL16 handle
/* [in] Handle of global memory object */
532 if (!handle
) return 0;
533 if (!VALID_HANDLE(handle
))
535 GET_ARENA_PTR(handle
)->lockCount
++;
536 return GET_ARENA_PTR(handle
)->base
;
540 /***********************************************************************
541 * GlobalUnlock (KERNEL.19)
542 * GlobalUnlock16 (KERNEL32.26)
544 * Should the return values be cast to booleans?
547 * TRUE: Object is still locked
548 * FALSE: Object is unlocked
550 BOOL16 WINAPI
GlobalUnlock16(
551 HGLOBAL16 handle
/* [in] Handle of global memory object */
553 GLOBALARENA
*pArena
= GET_ARENA_PTR(handle
);
554 if (!VALID_HANDLE(handle
)) {
555 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle
);
558 TRACE("%04x\n", handle
);
559 if (pArena
->lockCount
) pArena
->lockCount
--;
560 return pArena
->lockCount
;
563 /***********************************************************************
564 * GlobalChangeLockCount (KERNEL.365)
566 * This is declared as a register function as it has to preserve
567 * *all* registers, even AX/DX !
570 void WINAPI
GlobalChangeLockCount16( HGLOBAL16 handle
, INT16 delta
,
574 GlobalLock16( handle
);
575 else if ( delta
== -1 )
576 GlobalUnlock16( handle
);
578 ERR("(%04X, %d): strange delta value\n", handle
, delta
);
581 /***********************************************************************
582 * GlobalSize (KERNEL.20)
583 * GlobalSize16 (KERNEL32.32)
585 * Get the current size of a global memory object.
588 * Size in bytes of object
591 DWORD WINAPI
GlobalSize16(
592 HGLOBAL16 handle
/* [in] Handle of global memory object */
594 TRACE("%04x\n", handle
);
595 if (!handle
) return 0;
596 if (!VALID_HANDLE(handle
))
598 return GET_ARENA_PTR(handle
)->size
;
602 /***********************************************************************
603 * GlobalHandle (KERNEL.21)
605 * Get the handle associated with a pointer to the global memory block.
608 * Why is GlobalHandleToSel used here with the sel as input?
614 DWORD WINAPI
GlobalHandle16(
615 WORD sel
/* [in] Address of global memory block */
617 TRACE("%04x\n", sel
);
618 if (!VALID_HANDLE(sel
)) {
619 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel
);
622 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
625 /***********************************************************************
626 * GlobalHandleNoRIP (KERNEL.159)
628 DWORD WINAPI
GlobalHandleNoRIP16( WORD sel
)
631 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
632 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== sel
)
633 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel16(sel
) );
639 /***********************************************************************
640 * GlobalFlags (KERNEL.22)
642 * Get information about a global memory object.
645 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
649 * Value specifying flags and lock count
650 * GMEM_INVALID_HANDLE: Invalid handle
652 UINT16 WINAPI
GlobalFlags16(
653 HGLOBAL16 handle
/* [in] Handle of global memory object */
657 TRACE("%04x\n", handle
);
658 if (!VALID_HANDLE(handle
)) {
659 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle
);
662 pArena
= GET_ARENA_PTR(handle
);
663 return pArena
->lockCount
|
664 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
665 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
669 /***********************************************************************
670 * LockSegment (KERNEL.23)
672 HGLOBAL16 WINAPI
LockSegment16( HGLOBAL16 handle
)
674 TRACE("%04x\n", handle
);
675 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
676 if (!VALID_HANDLE(handle
)) {
677 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle
);
680 GET_ARENA_PTR(handle
)->lockCount
++;
685 /***********************************************************************
686 * UnlockSegment (KERNEL.24)
688 void WINAPI
UnlockSegment16( HGLOBAL16 handle
)
690 TRACE("%04x\n", handle
);
691 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
692 if (!VALID_HANDLE(handle
)) {
693 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle
);
696 GET_ARENA_PTR(handle
)->lockCount
--;
697 /* FIXME: this ought to return the lock count in CX (go figure...) */
701 /***********************************************************************
702 * GlobalCompact (KERNEL.25)
704 DWORD WINAPI
GlobalCompact16( DWORD desired
)
706 return GLOBAL_MAX_ALLOC_SIZE
;
710 /***********************************************************************
711 * GlobalFreeAll (KERNEL.26)
713 void WINAPI
GlobalFreeAll16( HGLOBAL16 owner
)
718 pArena
= pGlobalArena
;
719 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
721 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
722 GlobalFree16( pArena
->handle
);
727 /***********************************************************************
728 * GlobalWire (KERNEL.111)
729 * GlobalWire16 (KERNEL32.29)
731 SEGPTR WINAPI
GlobalWire16( HGLOBAL16 handle
)
733 return WIN16_GlobalLock16( handle
);
737 /***********************************************************************
738 * GlobalUnWire (KERNEL.112)
739 * GlobalUnWire16 (KERNEL32.30)
741 BOOL16 WINAPI
GlobalUnWire16( HGLOBAL16 handle
)
743 return !GlobalUnlock16( handle
);
747 /***********************************************************************
748 * SetSwapAreaSize (KERNEL.106)
750 LONG WINAPI
SetSwapAreaSize16( WORD size
)
752 FIXME("(%d) - stub!\n", size
);
753 return MAKELONG( size
, 0xffff );
757 /***********************************************************************
758 * GlobalLRUOldest (KERNEL.163)
760 HGLOBAL16 WINAPI
GlobalLRUOldest16( HGLOBAL16 handle
)
762 TRACE("%04x\n", handle
);
763 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
768 /***********************************************************************
769 * GlobalLRUNewest (KERNEL.164)
771 HGLOBAL16 WINAPI
GlobalLRUNewest16( HGLOBAL16 handle
)
773 TRACE("%04x\n", handle
);
774 if (handle
== (HGLOBAL16
)-1) handle
= CURRENT_DS
;
779 /***********************************************************************
780 * GetFreeSpace (KERNEL.169)
782 DWORD WINAPI
GetFreeSpace16( UINT16 wFlags
)
785 GlobalMemoryStatus( &ms
);
786 return ms
.dwAvailVirtual
;
789 /***********************************************************************
790 * GlobalDOSAlloc (KERNEL.184)
792 * Allocate memory in the first MB.
795 * Address (HW=Paragraph segment; LW=Selector)
797 DWORD WINAPI
GlobalDOSAlloc16(
798 DWORD size
/* [in] Number of bytes to be allocated */
801 LPVOID lpBlock
= DOSMEM_AllocBlock( size
, &uParagraph
);
805 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
809 wSelector
= GLOBAL_CreateBlock(GMEM_FIXED
, lpBlock
, size
, hModule
, WINE_LDT_FLAGS_DATA
);
810 pArena
= GET_ARENA_PTR(wSelector
);
811 pArena
->flags
|= GA_DOSMEM
;
812 return MAKELONG(wSelector
,uParagraph
);
818 /***********************************************************************
819 * GlobalDOSFree (KERNEL.185)
821 * Free memory allocated with GlobalDOSAlloc
827 WORD WINAPI
GlobalDOSFree16(
828 WORD sel
/* [in] Selector */
830 DWORD block
= GetSelectorBase(sel
);
832 if( block
&& block
< 0x100000 )
834 LPVOID lpBlock
= DOSMEM_MapDosToLinear( block
);
835 if( DOSMEM_FreeBlock( lpBlock
) )
836 GLOBAL_FreeBlock( sel
);
843 /***********************************************************************
844 * GlobalPageLock (KERNEL.191)
845 * GlobalSmartPageLock(KERNEL.230)
847 WORD WINAPI
GlobalPageLock16( HGLOBAL16 handle
)
849 TRACE("%04x\n", handle
);
850 if (!VALID_HANDLE(handle
)) {
851 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle
);
854 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
858 /***********************************************************************
859 * GlobalPageUnlock (KERNEL.192)
860 * GlobalSmartPageUnlock(KERNEL.231)
862 WORD WINAPI
GlobalPageUnlock16( HGLOBAL16 handle
)
864 TRACE("%04x\n", handle
);
865 if (!VALID_HANDLE(handle
)) {
866 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle
);
869 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
873 /***********************************************************************
874 * GlobalFix (KERNEL.197)
875 * GlobalFix16 (KERNEL32.27)
877 WORD WINAPI
GlobalFix16( HGLOBAL16 handle
)
879 TRACE("%04x\n", handle
);
880 if (!VALID_HANDLE(handle
)) {
881 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle
);
884 GET_ARENA_PTR(handle
)->lockCount
++;
886 return GlobalHandleToSel16(handle
);
890 /***********************************************************************
891 * GlobalUnfix (KERNEL.198)
892 * GlobalUnfix16 (KERNEL32.28)
894 void WINAPI
GlobalUnfix16( HGLOBAL16 handle
)
896 TRACE("%04x\n", handle
);
897 if (!VALID_HANDLE(handle
)) {
898 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle
);
901 GET_ARENA_PTR(handle
)->lockCount
--;
905 /***********************************************************************
906 * FarSetOwner (KERNEL.403)
908 void WINAPI
FarSetOwner16( HGLOBAL16 handle
, HANDLE16 hOwner
)
910 if (!VALID_HANDLE(handle
)) {
911 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle
);
914 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
918 /***********************************************************************
919 * FarGetOwner (KERNEL.404)
921 HANDLE16 WINAPI
FarGetOwner16( HGLOBAL16 handle
)
923 if (!VALID_HANDLE(handle
)) {
924 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle
);
927 return GET_ARENA_PTR(handle
)->hOwner
;
931 /************************************************************************
932 * GlobalMasterHandle (KERNEL.28)
935 * Should return selector and handle of the information structure for
936 * the global heap. selector and handle are stored in the THHOOK as
937 * pGlobalHeap and hGlobalHeap.
938 * As Wine doesn't have this structure, we return both values as zero
939 * Applications should interpret this as "No Global Heap"
941 DWORD WINAPI
GlobalMasterHandle16(void)
947 /***********************************************************************
948 * GlobalHandleToSel (TOOLHELP.50)
950 WORD WINAPI
GlobalHandleToSel16( HGLOBAL16 handle
)
952 if (!handle
) return 0;
953 if (!VALID_HANDLE(handle
)) {
954 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle
);
959 WARN("Program attempted invalid selector conversion\n" );
966 /***********************************************************************
967 * GlobalFirst (TOOLHELP.51)
969 BOOL16 WINAPI
GlobalFirst16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
971 if (wFlags
== GLOBAL_LRU
) return FALSE
;
973 return GlobalNext16( pGlobal
, wFlags
);
977 /***********************************************************************
978 * GlobalNext (TOOLHELP.52)
980 BOOL16 WINAPI
GlobalNext16( GLOBALENTRY
*pGlobal
, WORD wFlags
)
984 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
985 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
986 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
989 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
990 if (pArena
->size
== 0) break; /* block is free */
991 if (i
>= globalArenaSize
) return FALSE
;
995 pGlobal
->dwAddress
= (DWORD_PTR
)pArena
->base
;
996 pGlobal
->dwBlockSize
= pArena
->size
;
997 pGlobal
->hBlock
= pArena
->handle
;
998 pGlobal
->wcLock
= pArena
->lockCount
;
999 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
1000 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
1001 pGlobal
->wHeapPresent
= FALSE
;
1002 pGlobal
->hOwner
= pArena
->hOwner
;
1003 pGlobal
->wType
= GT_UNKNOWN
;
1010 /***********************************************************************
1011 * GlobalInfo (TOOLHELP.53)
1013 BOOL16 WINAPI
GlobalInfo16( GLOBALINFO
*pInfo
)
1016 GLOBALARENA
*pArena
;
1018 pInfo
->wcItems
= globalArenaSize
;
1019 pInfo
->wcItemsFree
= 0;
1020 pInfo
->wcItemsLRU
= 0;
1021 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
1022 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
1027 /***********************************************************************
1028 * GlobalEntryHandle (TOOLHELP.54)
1030 BOOL16 WINAPI
GlobalEntryHandle16( GLOBALENTRY
*pGlobal
, HGLOBAL16 hItem
)
1032 GLOBALARENA
*pArena
= GET_ARENA_PTR(hItem
);
1034 pGlobal
->dwAddress
= (DWORD_PTR
)pArena
->base
;
1035 pGlobal
->dwBlockSize
= pArena
->size
;
1036 pGlobal
->hBlock
= pArena
->handle
;
1037 pGlobal
->wcLock
= pArena
->lockCount
;
1038 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
1039 pGlobal
->wFlags
= (GetCurrentPDB16() == pArena
->hOwner
);
1040 pGlobal
->wHeapPresent
= FALSE
;
1041 pGlobal
->hOwner
= pArena
->hOwner
;
1042 pGlobal
->wType
= GT_UNKNOWN
;
1049 /***********************************************************************
1050 * GlobalEntryModule (TOOLHELP.55)
1052 BOOL16 WINAPI
GlobalEntryModule16( GLOBALENTRY
*pGlobal
, HMODULE16 hModule
,
1055 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal
, hModule
, wSeg
);
1060 /***********************************************************************
1061 * MemManInfo (TOOLHELP.72)
1063 BOOL16 WINAPI
MemManInfo16( MEMMANINFO
*info
)
1065 MEMORYSTATUS status
;
1068 * Not unsurprisingly although the documentation says you
1069 * _must_ provide the size in the dwSize field, this function
1070 * (under Windows) always fills the structure and returns true.
1072 GlobalMemoryStatus( &status
);
1073 info
->wPageSize
= getpagesize();
1074 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1075 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1076 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1077 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1078 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1079 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1080 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1081 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1082 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1086 /***********************************************************************
1087 * GetFreeMemInfo (KERNEL.316)
1089 DWORD WINAPI
GetFreeMemInfo16(void)
1092 MemManInfo16( &info
);
1093 return MAKELONG( info
.dwTotalLinearSpace
, info
.dwMaxPagesAvailable
);
1096 /***********************************************************************
1097 * A20Proc (KERNEL.165)
1099 void WINAPI
A20Proc16( WORD unused
)
1101 /* this is also a NOP in Windows */
1104 /***********************************************************************
1105 * LimitEMSPages (KERNEL.156)
1107 DWORD WINAPI
LimitEMSPages16( DWORD unused
)