push 6e61d6ca5bcaf95ac09a664b4ba4f88238c927be
[wine/hacks.git] / dlls / krnl386.exe16 / global.c
blobba4d7df645dbe08c9e1731dfe0d585c147e18a76
1 /*
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 */
22 #include "config.h"
23 #include "wine/port.h"
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
35 #endif
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
38 #endif
40 #include "wine/winbase16.h"
41 #include "winternl.h"
42 #include "kernel16_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(global);
47 /* Global arena block */
48 typedef struct
50 void *base; /* Base address (0 if discarded) */
51 DWORD size; /* Size in bytes (0 indicates a free block) */
52 HGLOBAL16 handle; /* Handle for this block */
53 HGLOBAL16 hOwner; /* Owner of this block */
54 BYTE lockCount; /* Count of GlobalFix() calls */
55 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
56 BYTE flags; /* Allocation flags */
57 BYTE selCount; /* Number of selectors allocated for this block */
58 } GLOBALARENA;
60 /* Flags definitions */
61 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
62 #define GA_DGROUP 0x04
63 #define GA_DISCARDABLE 0x08
64 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
65 #define GA_DOSMEM 0x20
67 /* Arena array (FIXME) */
68 static GLOBALARENA *pGlobalArena;
69 static int globalArenaSize;
71 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
72 #define GLOBAL_MAX_COUNT 8192 /* Max number of allocated blocks */
74 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
75 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
77 static HANDLE get_win16_heap(void)
79 static HANDLE win16_heap;
81 /* we create global memory block with execute permission. The access can be limited
82 * for 16-bit code on selector level */
83 if (!win16_heap) win16_heap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
84 return win16_heap;
87 /***********************************************************************
88 * GLOBAL_GetArena
90 * Return the arena for a given selector, growing the arena array if needed.
92 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
94 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
96 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
98 if (!pGlobalArena)
100 pGlobalArena = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
101 GLOBAL_MAX_COUNT * sizeof(GLOBALARENA) );
102 if (!pGlobalArena) return 0;
103 /* Hack: store a pointer to it in THHOOK instead of a handle */
104 *(GLOBALARENA **)&pThhook->hGlobalHeap = pGlobalArena;
106 if (newsize > GLOBAL_MAX_COUNT) return 0;
107 globalArenaSize = newsize;
109 return pGlobalArena + (sel >> __AHSHIFT);
112 void debug_handles(void)
114 int printed=0;
115 int i;
116 for (i = globalArenaSize-1 ; i>=0 ; i--) {
117 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
118 printed=1;
119 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
122 if (printed)
123 DPRINTF("\n");
127 /***********************************************************************
128 * GLOBAL_CreateBlock
130 * Create a global heap block for a fixed range of linear memory.
132 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
133 HGLOBAL16 hOwner, unsigned char selflags )
135 WORD sel, selcount;
136 GLOBALARENA *pArena;
138 /* Allocate the selector(s) */
140 sel = SELECTOR_AllocBlock( ptr, size, selflags );
141 if (!sel) return 0;
142 selcount = (size + 0xffff) / 0x10000;
144 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
146 SELECTOR_FreeBlock( sel );
147 return 0;
150 /* Fill the arena block */
152 pArena->base = ptr;
153 pArena->size = GetSelectorLimit16(sel) + 1;
154 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
155 pArena->hOwner = hOwner;
156 pArena->lockCount = 0;
157 pArena->pageLockCount = 0;
158 pArena->flags = flags & GA_MOVEABLE;
159 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
160 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
161 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
162 pArena->selCount = selcount;
163 if (selcount > 1) /* clear the next arena blocks */
164 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
166 return pArena->handle;
170 /***********************************************************************
171 * GLOBAL_FreeBlock
173 * Free a block allocated by GLOBAL_CreateBlock, without touching
174 * the associated linear memory range.
176 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
178 WORD sel;
179 GLOBALARENA *pArena;
181 if (!handle) return TRUE;
182 sel = GlobalHandleToSel16( handle );
183 if (!VALID_HANDLE(sel)) return FALSE;
184 pArena = GET_ARENA_PTR(sel);
185 if (!pArena->size)
187 WARN( "already free %x\n", handle );
188 return FALSE;
190 SELECTOR_FreeBlock( sel );
191 memset( pArena, 0, sizeof(GLOBALARENA) );
192 return TRUE;
195 /***********************************************************************
196 * GLOBAL_MoveBlock
198 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
200 WORD sel;
201 GLOBALARENA *pArena;
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)
208 return FALSE;
210 pArena->base = ptr;
211 pArena->size = size;
212 SELECTOR_ReallocBlock( sel, ptr, size );
213 return TRUE;
216 /***********************************************************************
217 * GLOBAL_Alloc
219 * Implementation of GlobalAlloc16()
221 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
223 void *ptr;
224 HGLOBAL16 handle;
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 );
232 /* Fixup the size */
234 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
235 size = (size + 0x1f) & ~0x1f;
237 /* Allocate the linear memory */
238 ptr = HeapAlloc( get_win16_heap(), 0, size );
239 /* FIXME: free discardable blocks and try again? */
240 if (!ptr) return 0;
242 /* Allocate the selector(s) */
244 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
245 if (!handle)
247 HeapFree( get_win16_heap(), 0, ptr );
248 return 0;
251 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
252 return handle;
255 /***********************************************************************
256 * GlobalAlloc (KERNEL.15)
257 * GlobalAlloc16 (KERNEL32.24)
259 * Allocate a global memory object.
261 * RETURNS
262 * Handle: Success
263 * NULL: Failure
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)
273 /* make it owned by the calling module */
274 STACK16FRAME *frame = CURRENT_STACK16;
275 owner = GetExePtr( frame->cs );
277 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
281 /***********************************************************************
282 * GlobalReAlloc (KERNEL.16)
284 * Change the size or attributes of a global memory object.
286 * RETURNS
287 * Handle: Success
288 * NULL: Failure
290 HGLOBAL16 WINAPI GlobalReAlloc16(
291 HGLOBAL16 handle, /* [in] Handle of global memory object */
292 DWORD size, /* [in] New size of block */
293 UINT16 flags /* [in] How to reallocate object */
295 WORD selcount;
296 DWORD oldsize;
297 void *ptr, *newptr;
298 GLOBALARENA *pArena, *pNewArena;
299 WORD sel = GlobalHandleToSel16( handle );
300 HANDLE heap = get_win16_heap();
302 TRACE("%04x %d flags=%04x\n",
303 handle, size, flags );
304 if (!handle) return 0;
306 if (!VALID_HANDLE(handle))
308 WARN("Invalid handle 0x%04x!\n", handle);
309 return 0;
311 pArena = GET_ARENA_PTR( handle );
313 /* Discard the block if requested */
315 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
317 if (!(pArena->flags & GA_MOVEABLE) ||
318 !(pArena->flags & GA_DISCARDABLE) ||
319 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
320 if (pArena->flags & GA_DOSMEM)
321 DOSMEM_FreeBlock( pArena->base );
322 else
323 HeapFree( heap, 0, pArena->base );
324 pArena->base = 0;
326 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
327 * change the selector if we are shrinking the block.
328 * FIXME: shouldn't we keep selectors until the block is deleted?
330 SELECTOR_ReallocBlock( sel, 0, 1 );
331 return handle;
334 /* Fixup the size */
336 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
337 if (size == 0) size = 0x20;
338 else size = (size + 0x1f) & ~0x1f;
340 /* Change the flags */
342 if (flags & GMEM_MODIFY)
344 /* Change the flags, leaving GA_DGROUP alone */
345 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
346 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
347 return handle;
350 /* Reallocate the linear memory */
352 ptr = pArena->base;
353 oldsize = pArena->size;
354 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr,oldsize,size);
355 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
357 if (pArena->flags & GA_DOSMEM)
359 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
360 newptr = ptr;
361 else if(pArena->pageLockCount > 0)
362 newptr = 0;
363 else
365 newptr = DOSMEM_AllocBlock( size, 0 );
366 if (newptr)
368 memcpy( newptr, ptr, oldsize );
369 DOSMEM_FreeBlock( ptr );
373 else
376 * if more than one reader (e.g. some pointer has been
377 * given out by GetVDMPointer32W16),
378 * only try to realloc in place
381 if (ptr)
382 newptr = HeapReAlloc( heap,
383 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
384 ptr, size );
385 else
386 newptr = HeapAlloc( heap, 0, size );
390 if (!newptr)
392 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
393 if (pArena->pageLockCount <1)
395 if (pArena->flags & GA_DOSMEM)
396 DOSMEM_FreeBlock( pArena->base );
397 else
398 HeapFree( heap, 0, ptr );
399 SELECTOR_FreeBlock( sel );
400 memset( pArena, 0, sizeof(GLOBALARENA) );
402 return 0;
404 ptr = newptr;
406 /* Reallocate the selector(s) */
408 sel = SELECTOR_ReallocBlock( sel, ptr, size );
409 if (!sel)
411 if (pArena->flags & GA_DOSMEM)
412 DOSMEM_FreeBlock( pArena->base );
413 else
414 HeapFree( heap, 0, ptr );
415 memset( pArena, 0, sizeof(GLOBALARENA) );
416 return 0;
418 selcount = (size + 0xffff) / 0x10000;
420 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
422 if (pArena->flags & GA_DOSMEM)
423 DOSMEM_FreeBlock( pArena->base );
424 else
425 HeapFree( heap, 0, ptr );
426 SELECTOR_FreeBlock( sel );
427 return 0;
430 /* Fill the new arena block
431 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
433 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
434 pNewArena->base = ptr;
435 pNewArena->size = GetSelectorLimit16(sel) + 1;
436 pNewArena->selCount = selcount;
437 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
439 if (selcount > 1) /* clear the next arena blocks */
440 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
442 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
443 memset( (char *)ptr + oldsize, 0, size - oldsize );
444 return pNewArena->handle;
448 /***********************************************************************
449 * GlobalFree (KERNEL.17)
450 * GlobalFree16 (KERNEL32.31)
451 * RETURNS
452 * NULL: Success
453 * Handle: Failure
455 HGLOBAL16 WINAPI GlobalFree16(
456 HGLOBAL16 handle /* [in] Handle of global memory object */
458 void *ptr;
460 if (!VALID_HANDLE(handle))
462 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
463 return 0;
465 ptr = GET_ARENA_PTR(handle)->base;
467 TRACE("%04x\n", handle );
468 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
469 HeapFree( get_win16_heap(), 0, ptr );
470 return 0;
474 /**********************************************************************
475 * K32WOWGlobalLock16 (KERNEL32.60)
477 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
479 WORD sel = GlobalHandleToSel16( handle );
480 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
482 if (handle)
484 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
486 if (!VALID_HANDLE(handle)) {
487 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
488 sel = 0;
490 else if (!GET_ARENA_PTR(handle)->base)
491 sel = 0;
492 else
493 GET_ARENA_PTR(handle)->lockCount++;
496 return MAKESEGPTR( sel, 0 );
501 /***********************************************************************
502 * GlobalLock (KERNEL.18)
504 * This is the GlobalLock16() function used by 16-bit code.
506 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
508 SEGPTR ret = K32WOWGlobalLock16( handle );
509 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
510 return ret;
514 /***********************************************************************
515 * GlobalLock16 (KERNEL32.25)
517 * This is the GlobalLock16() function used by 32-bit code.
519 * RETURNS
520 * Pointer to first byte of memory block
521 * NULL: Failure
523 LPVOID WINAPI GlobalLock16(
524 HGLOBAL16 handle /* [in] Handle of global memory object */
526 if (!handle) return 0;
527 if (!VALID_HANDLE(handle))
528 return 0;
529 GET_ARENA_PTR(handle)->lockCount++;
530 return GET_ARENA_PTR(handle)->base;
534 /***********************************************************************
535 * GlobalUnlock (KERNEL.19)
536 * GlobalUnlock16 (KERNEL32.26)
537 * NOTES
538 * Should the return values be cast to booleans?
540 * RETURNS
541 * TRUE: Object is still locked
542 * FALSE: Object is unlocked
544 BOOL16 WINAPI GlobalUnlock16(
545 HGLOBAL16 handle /* [in] Handle of global memory object */
547 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
548 if (!VALID_HANDLE(handle)) {
549 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
550 return 0;
552 TRACE("%04x\n", handle );
553 if (pArena->lockCount) pArena->lockCount--;
554 return pArena->lockCount;
557 /***********************************************************************
558 * GlobalChangeLockCount (KERNEL.365)
560 * This is declared as a register function as it has to preserve
561 * *all* registers, even AX/DX !
564 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
565 CONTEXT86 *context )
567 if ( delta == 1 )
568 GlobalLock16( handle );
569 else if ( delta == -1 )
570 GlobalUnlock16( handle );
571 else
572 ERR("(%04X, %d): strange delta value\n", handle, delta );
575 /***********************************************************************
576 * GlobalSize (KERNEL.20)
577 * GlobalSize16 (KERNEL32.32)
579 * Get the current size of a global memory object.
581 * RETURNS
582 * Size in bytes of object
583 * 0: Failure
585 DWORD WINAPI GlobalSize16(
586 HGLOBAL16 handle /* [in] Handle of global memory object */
588 TRACE("%04x\n", handle );
589 if (!handle) return 0;
590 if (!VALID_HANDLE(handle))
591 return 0;
592 return GET_ARENA_PTR(handle)->size;
596 /***********************************************************************
597 * GlobalHandle (KERNEL.21)
599 * Get the handle associated with a pointer to the global memory block.
601 * NOTES
602 * Why is GlobalHandleToSel used here with the sel as input?
604 * RETURNS
605 * Handle: Success
606 * NULL: Failure
608 DWORD WINAPI GlobalHandle16(
609 WORD sel /* [in] Address of global memory block */
611 TRACE("%04x\n", sel );
612 if (!VALID_HANDLE(sel)) {
613 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
614 return 0;
616 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
619 /***********************************************************************
620 * GlobalHandleNoRIP (KERNEL.159)
622 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
624 int i;
625 for (i = globalArenaSize-1 ; i>=0 ; i--) {
626 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
627 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
629 return 0;
633 /***********************************************************************
634 * GlobalFlags (KERNEL.22)
636 * Get information about a global memory object.
638 * NOTES
639 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
640 * handle?
642 * RETURNS
643 * Value specifying flags and lock count
644 * GMEM_INVALID_HANDLE: Invalid handle
646 UINT16 WINAPI GlobalFlags16(
647 HGLOBAL16 handle /* [in] Handle of global memory object */
649 GLOBALARENA *pArena;
651 TRACE("%04x\n", handle );
652 if (!VALID_HANDLE(handle)) {
653 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
654 return 0;
656 pArena = GET_ARENA_PTR(handle);
657 return pArena->lockCount |
658 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
659 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
663 /***********************************************************************
664 * LockSegment (KERNEL.23)
666 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
668 TRACE("%04x\n", handle );
669 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
670 if (!VALID_HANDLE(handle)) {
671 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
672 return 0;
674 GET_ARENA_PTR(handle)->lockCount++;
675 return handle;
679 /***********************************************************************
680 * UnlockSegment (KERNEL.24)
682 void WINAPI UnlockSegment16( HGLOBAL16 handle )
684 TRACE("%04x\n", handle );
685 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
686 if (!VALID_HANDLE(handle)) {
687 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
688 return;
690 GET_ARENA_PTR(handle)->lockCount--;
691 /* FIXME: this ought to return the lock count in CX (go figure...) */
695 /***********************************************************************
696 * GlobalCompact (KERNEL.25)
698 DWORD WINAPI GlobalCompact16( DWORD desired )
700 return GLOBAL_MAX_ALLOC_SIZE;
704 /***********************************************************************
705 * GlobalFreeAll (KERNEL.26)
707 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
709 int i;
710 GLOBALARENA *pArena;
712 pArena = pGlobalArena;
713 for (i = 0; i < globalArenaSize; i++, pArena++)
715 if ((pArena->size != 0) && (pArena->hOwner == owner))
716 GlobalFree16( pArena->handle );
721 /***********************************************************************
722 * GlobalWire (KERNEL.111)
723 * GlobalWire16 (KERNEL32.29)
725 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
727 return WIN16_GlobalLock16( handle );
731 /***********************************************************************
732 * GlobalUnWire (KERNEL.112)
733 * GlobalUnWire16 (KERNEL32.30)
735 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
737 return !GlobalUnlock16( handle );
741 /***********************************************************************
742 * SetSwapAreaSize (KERNEL.106)
744 LONG WINAPI SetSwapAreaSize16( WORD size )
746 FIXME("(%d) - stub!\n", size );
747 return MAKELONG( size, 0xffff );
751 /***********************************************************************
752 * GlobalLRUOldest (KERNEL.163)
754 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
756 TRACE("%04x\n", handle );
757 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
758 return handle;
762 /***********************************************************************
763 * GlobalLRUNewest (KERNEL.164)
765 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
767 TRACE("%04x\n", handle );
768 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
769 return handle;
773 /***********************************************************************
774 * GetFreeSpace (KERNEL.169)
776 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
778 MEMORYSTATUS ms;
779 GlobalMemoryStatus( &ms );
780 return ms.dwAvailVirtual;
783 /***********************************************************************
784 * GlobalDOSAlloc (KERNEL.184)
786 * Allocate memory in the first MB.
788 * RETURNS
789 * Address (HW=Paragraph segment; LW=Selector)
791 DWORD WINAPI GlobalDOSAlloc16(
792 DWORD size /* [in] Number of bytes to be allocated */
794 UINT16 uParagraph;
795 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
797 if( lpBlock )
799 HMODULE16 hModule = GetModuleHandle16("KERNEL");
800 WORD wSelector;
801 GLOBALARENA *pArena;
803 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
804 pArena = GET_ARENA_PTR(wSelector);
805 pArena->flags |= GA_DOSMEM;
806 return MAKELONG(wSelector,uParagraph);
808 return 0;
812 /***********************************************************************
813 * GlobalDOSFree (KERNEL.185)
815 * Free memory allocated with GlobalDOSAlloc
817 * RETURNS
818 * NULL: Success
819 * sel: Failure
821 WORD WINAPI GlobalDOSFree16(
822 WORD sel /* [in] Selector */
824 DWORD block = GetSelectorBase(sel);
826 if( block && block < 0x100000 )
828 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
829 if( DOSMEM_FreeBlock( lpBlock ) )
830 GLOBAL_FreeBlock( sel );
831 sel = 0;
833 return sel;
837 /***********************************************************************
838 * GlobalPageLock (KERNEL.191)
839 * GlobalSmartPageLock(KERNEL.230)
841 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
843 TRACE("%04x\n", handle );
844 if (!VALID_HANDLE(handle)) {
845 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
846 return 0;
848 return ++(GET_ARENA_PTR(handle)->pageLockCount);
852 /***********************************************************************
853 * GlobalPageUnlock (KERNEL.192)
854 * GlobalSmartPageUnlock(KERNEL.231)
856 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
858 TRACE("%04x\n", handle );
859 if (!VALID_HANDLE(handle)) {
860 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
861 return 0;
863 return --(GET_ARENA_PTR(handle)->pageLockCount);
867 /***********************************************************************
868 * GlobalFix (KERNEL.197)
869 * GlobalFix16 (KERNEL32.27)
871 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
873 TRACE("%04x\n", handle );
874 if (!VALID_HANDLE(handle)) {
875 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
876 return 0;
878 GET_ARENA_PTR(handle)->lockCount++;
880 return GlobalHandleToSel16(handle);
884 /***********************************************************************
885 * GlobalUnfix (KERNEL.198)
886 * GlobalUnfix16 (KERNEL32.28)
888 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
890 TRACE("%04x\n", handle );
891 if (!VALID_HANDLE(handle)) {
892 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
893 return;
895 GET_ARENA_PTR(handle)->lockCount--;
899 /***********************************************************************
900 * FarSetOwner (KERNEL.403)
902 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
904 if (!VALID_HANDLE(handle)) {
905 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
906 return;
908 GET_ARENA_PTR(handle)->hOwner = hOwner;
912 /***********************************************************************
913 * FarGetOwner (KERNEL.404)
915 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
917 if (!VALID_HANDLE(handle)) {
918 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
919 return 0;
921 return GET_ARENA_PTR(handle)->hOwner;
925 /************************************************************************
926 * GlobalMasterHandle (KERNEL.28)
929 * Should return selector and handle of the information structure for
930 * the global heap. selector and handle are stored in the THHOOK as
931 * pGlobalHeap and hGlobalHeap.
932 * As Wine doesn't have this structure, we return both values as zero
933 * Applications should interpret this as "No Global Heap"
935 DWORD WINAPI GlobalMasterHandle16(void)
937 FIXME(": stub\n");
938 return 0;
941 /***********************************************************************
942 * GlobalHandleToSel (TOOLHELP.50)
944 * FIXME: This is in TOOLHELP but we keep a copy here for now.
946 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
948 if (!handle) return 0;
949 if (!VALID_HANDLE(handle)) {
950 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
951 return 0;
953 if (!(handle & 7))
955 WARN("Program attempted invalid selector conversion\n" );
956 return handle - 1;
958 return handle | 7;
962 /***********************************************************************
963 * GetFreeMemInfo (KERNEL.316)
965 DWORD WINAPI GetFreeMemInfo16(void)
967 MEMORYSTATUS status;
968 GlobalMemoryStatus( &status );
969 return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
972 /***********************************************************************
973 * A20Proc (KERNEL.165)
975 void WINAPI A20Proc16( WORD unused )
977 /* this is also a NOP in Windows */
980 /***********************************************************************
981 * LimitEMSPages (KERNEL.156)
983 DWORD WINAPI LimitEMSPages16( DWORD unused )
985 return 0;