kernel32/lcformat: Avoid back jumps on failure.
[wine/multimedia.git] / dlls / kernel32 / global16.c
blobc5dcc14e34977143cc36d27e220a2c3a8eb50107
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 "kernel_private.h"
43 #include "kernel16_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(global);
48 /* Global arena block */
49 typedef struct
51 void *base; /* Base address (0 if discarded) */
52 DWORD size; /* Size in bytes (0 indicates a free block) */
53 HGLOBAL16 handle; /* Handle for this block */
54 HGLOBAL16 hOwner; /* Owner of this block */
55 BYTE lockCount; /* Count of GlobalFix() calls */
56 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
57 BYTE flags; /* Allocation flags */
58 BYTE selCount; /* Number of selectors allocated for this block */
59 } GLOBALARENA;
61 /* Flags definitions */
62 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
63 #define GA_DGROUP 0x04
64 #define GA_DISCARDABLE 0x08
65 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
66 #define GA_DOSMEM 0x20
68 /* Arena array (FIXME) */
69 static GLOBALARENA *pGlobalArena;
70 static int globalArenaSize;
72 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
73 #define GLOBAL_MAX_COUNT 8192 /* Max number of allocated blocks */
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);
103 return win16_heap;
106 /***********************************************************************
107 * GLOBAL_GetArena
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;
117 if (!pGlobalArena)
119 pGlobalArena = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 GLOBAL_MAX_COUNT * sizeof(GLOBALARENA) );
121 if (!pGlobalArena) return 0;
122 /* Hack: store a pointer to it in THHOOK instead of a handle */
123 *(GLOBALARENA **)&pThhook->hGlobalHeap = pGlobalArena;
125 if (newsize > GLOBAL_MAX_COUNT) return 0;
126 globalArenaSize = newsize;
128 return pGlobalArena + (sel >> __AHSHIFT);
131 void debug_handles(void)
133 int printed=0;
134 int i;
135 for (i = globalArenaSize-1 ; i>=0 ; i--) {
136 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
137 printed=1;
138 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
141 if (printed)
142 DPRINTF("\n");
146 /***********************************************************************
147 * GLOBAL_CreateBlock
149 * Create a global heap block for a fixed range of linear memory.
151 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
152 HGLOBAL16 hOwner, unsigned char selflags )
154 WORD sel, selcount;
155 GLOBALARENA *pArena;
157 /* Allocate the selector(s) */
159 sel = SELECTOR_AllocBlock( ptr, size, selflags );
160 if (!sel) return 0;
161 selcount = (size + 0xffff) / 0x10000;
163 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
165 SELECTOR_FreeBlock( sel );
166 return 0;
169 /* Fill the arena block */
171 pArena->base = ptr;
172 pArena->size = GetSelectorLimit16(sel) + 1;
173 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
174 pArena->hOwner = hOwner;
175 pArena->lockCount = 0;
176 pArena->pageLockCount = 0;
177 pArena->flags = flags & GA_MOVEABLE;
178 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
179 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
180 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
181 pArena->selCount = selcount;
182 if (selcount > 1) /* clear the next arena blocks */
183 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
185 return pArena->handle;
189 /***********************************************************************
190 * GLOBAL_FreeBlock
192 * Free a block allocated by GLOBAL_CreateBlock, without touching
193 * the associated linear memory range.
195 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
197 WORD sel;
198 GLOBALARENA *pArena;
200 if (!handle) return TRUE;
201 sel = GlobalHandleToSel16( handle );
202 if (!VALID_HANDLE(sel)) return FALSE;
203 pArena = GET_ARENA_PTR(sel);
204 SELECTOR_FreeBlock( sel );
205 memset( pArena, 0, sizeof(GLOBALARENA) );
206 return TRUE;
209 /***********************************************************************
210 * GLOBAL_MoveBlock
212 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
214 WORD sel;
215 GLOBALARENA *pArena;
217 if (!handle) return TRUE;
218 sel = GlobalHandleToSel16( handle );
219 if (!VALID_HANDLE(sel)) return FALSE;
220 pArena = GET_ARENA_PTR(sel);
221 if (pArena->selCount != 1)
222 return FALSE;
224 pArena->base = ptr;
225 pArena->size = size;
226 SELECTOR_ReallocBlock( sel, ptr, size );
227 return TRUE;
230 /***********************************************************************
231 * GLOBAL_Alloc
233 * Implementation of GlobalAlloc16()
235 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
237 void *ptr;
238 HGLOBAL16 handle;
240 TRACE("%d flags=%04x\n", size, flags );
242 /* If size is 0, create a discarded block */
244 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
246 /* Fixup the size */
248 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
249 size = (size + 0x1f) & ~0x1f;
251 /* Allocate the linear memory */
252 ptr = HeapAlloc( get_win16_heap(), 0, size );
253 /* FIXME: free discardable blocks and try again? */
254 if (!ptr) return 0;
256 /* Allocate the selector(s) */
258 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
259 if (!handle)
261 HeapFree( get_win16_heap(), 0, ptr );
262 return 0;
265 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
266 return handle;
269 /***********************************************************************
270 * GlobalAlloc (KERNEL.15)
271 * GlobalAlloc16 (KERNEL32.24)
273 * Allocate a global memory object.
275 * RETURNS
276 * Handle: Success
277 * NULL: Failure
279 HGLOBAL16 WINAPI GlobalAlloc16(
280 UINT16 flags, /* [in] Object allocation attributes */
281 DWORD size /* [in] Number of bytes to allocate */
283 HANDLE16 owner = GetCurrentPDB16();
285 if (flags & GMEM_DDESHARE)
286 owner = GetExePtr(owner); /* Make it a module handle */
287 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
291 /***********************************************************************
292 * GlobalReAlloc (KERNEL.16)
294 * Change the size or attributes of a global memory object.
296 * RETURNS
297 * Handle: Success
298 * NULL: Failure
300 HGLOBAL16 WINAPI GlobalReAlloc16(
301 HGLOBAL16 handle, /* [in] Handle of global memory object */
302 DWORD size, /* [in] New size of block */
303 UINT16 flags /* [in] How to reallocate object */
305 WORD selcount;
306 DWORD oldsize;
307 void *ptr, *newptr;
308 GLOBALARENA *pArena, *pNewArena;
309 WORD sel = GlobalHandleToSel16( handle );
310 HANDLE heap = get_win16_heap();
312 TRACE("%04x %d flags=%04x\n",
313 handle, size, flags );
314 if (!handle) return 0;
316 if (!VALID_HANDLE(handle))
318 WARN("Invalid handle 0x%04x!\n", handle);
319 return 0;
321 pArena = GET_ARENA_PTR( handle );
323 /* Discard the block if requested */
325 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
327 if (!(pArena->flags & GA_MOVEABLE) ||
328 !(pArena->flags & GA_DISCARDABLE) ||
329 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
330 if (pArena->flags & GA_DOSMEM)
331 DOSMEM_FreeBlock( pArena->base );
332 else
333 HeapFree( heap, 0, pArena->base );
334 pArena->base = 0;
336 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
337 * change the selector if we are shrinking the block.
338 * FIXME: shouldn't we keep selectors until the block is deleted?
340 SELECTOR_ReallocBlock( sel, 0, 1 );
341 return handle;
344 /* Fixup the size */
346 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
347 if (size == 0) size = 0x20;
348 else size = (size + 0x1f) & ~0x1f;
350 /* Change the flags */
352 if (flags & GMEM_MODIFY)
354 /* Change the flags, leaving GA_DGROUP alone */
355 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
356 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
357 return handle;
360 /* Reallocate the linear memory */
362 ptr = pArena->base;
363 oldsize = pArena->size;
364 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr,oldsize,size);
365 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
367 if (pArena->flags & GA_DOSMEM)
369 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
370 newptr = ptr;
371 else if(pArena->pageLockCount > 0)
372 newptr = 0;
373 else
375 newptr = DOSMEM_AllocBlock( size, 0 );
376 if (newptr)
378 memcpy( newptr, ptr, oldsize );
379 DOSMEM_FreeBlock( ptr );
383 else
386 * if more than one reader (e.g. some pointer has been
387 * given out by GetVDMPointer32W16),
388 * only try to realloc in place
391 if (ptr)
392 newptr = HeapReAlloc( heap,
393 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
394 ptr, size );
395 else
396 newptr = HeapAlloc( heap, 0, size );
400 if (!newptr)
402 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
403 if (pArena->pageLockCount <1)
405 if (pArena->flags & GA_DOSMEM)
406 DOSMEM_FreeBlock( pArena->base );
407 else
408 HeapFree( heap, 0, ptr );
409 SELECTOR_FreeBlock( sel );
410 memset( pArena, 0, sizeof(GLOBALARENA) );
412 return 0;
414 ptr = newptr;
416 /* Reallocate the selector(s) */
418 sel = SELECTOR_ReallocBlock( sel, ptr, size );
419 if (!sel)
421 if (pArena->flags & GA_DOSMEM)
422 DOSMEM_FreeBlock( pArena->base );
423 else
424 HeapFree( heap, 0, ptr );
425 memset( pArena, 0, sizeof(GLOBALARENA) );
426 return 0;
428 selcount = (size + 0xffff) / 0x10000;
430 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
432 if (pArena->flags & GA_DOSMEM)
433 DOSMEM_FreeBlock( pArena->base );
434 else
435 HeapFree( heap, 0, ptr );
436 SELECTOR_FreeBlock( sel );
437 return 0;
440 /* Fill the new arena block
441 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
443 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
444 pNewArena->base = ptr;
445 pNewArena->size = GetSelectorLimit16(sel) + 1;
446 pNewArena->selCount = selcount;
447 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
449 if (selcount > 1) /* clear the next arena blocks */
450 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
452 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
453 memset( (char *)ptr + oldsize, 0, size - oldsize );
454 return pNewArena->handle;
458 /***********************************************************************
459 * GlobalFree (KERNEL.17)
460 * GlobalFree16 (KERNEL32.31)
461 * RETURNS
462 * NULL: Success
463 * Handle: Failure
465 HGLOBAL16 WINAPI GlobalFree16(
466 HGLOBAL16 handle /* [in] Handle of global memory object */
468 void *ptr;
470 if (!VALID_HANDLE(handle))
472 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
473 return 0;
475 ptr = GET_ARENA_PTR(handle)->base;
477 TRACE("%04x\n", handle );
478 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
479 HeapFree( get_win16_heap(), 0, ptr );
480 return 0;
484 /**********************************************************************
485 * K32WOWGlobalLock16 (KERNEL32.60)
487 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
489 WORD sel = GlobalHandleToSel16( handle );
490 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
492 if (handle)
494 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
496 if (!VALID_HANDLE(handle)) {
497 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
498 sel = 0;
500 else if (!GET_ARENA_PTR(handle)->base)
501 sel = 0;
502 else
503 GET_ARENA_PTR(handle)->lockCount++;
506 return MAKESEGPTR( sel, 0 );
511 /***********************************************************************
512 * GlobalLock (KERNEL.18)
514 * This is the GlobalLock16() function used by 16-bit code.
516 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
518 SEGPTR ret = K32WOWGlobalLock16( handle );
519 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
520 return ret;
524 /***********************************************************************
525 * GlobalLock16 (KERNEL32.25)
527 * This is the GlobalLock16() function used by 32-bit code.
529 * RETURNS
530 * Pointer to first byte of memory block
531 * NULL: Failure
533 LPVOID WINAPI GlobalLock16(
534 HGLOBAL16 handle /* [in] Handle of global memory object */
536 if (!handle) return 0;
537 if (!VALID_HANDLE(handle))
538 return 0;
539 GET_ARENA_PTR(handle)->lockCount++;
540 return GET_ARENA_PTR(handle)->base;
544 /***********************************************************************
545 * GlobalUnlock (KERNEL.19)
546 * GlobalUnlock16 (KERNEL32.26)
547 * NOTES
548 * Should the return values be cast to booleans?
550 * RETURNS
551 * TRUE: Object is still locked
552 * FALSE: Object is unlocked
554 BOOL16 WINAPI GlobalUnlock16(
555 HGLOBAL16 handle /* [in] Handle of global memory object */
557 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
558 if (!VALID_HANDLE(handle)) {
559 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
560 return 0;
562 TRACE("%04x\n", handle );
563 if (pArena->lockCount) pArena->lockCount--;
564 return pArena->lockCount;
567 /***********************************************************************
568 * GlobalChangeLockCount (KERNEL.365)
570 * This is declared as a register function as it has to preserve
571 * *all* registers, even AX/DX !
574 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
575 CONTEXT86 *context )
577 if ( delta == 1 )
578 GlobalLock16( handle );
579 else if ( delta == -1 )
580 GlobalUnlock16( handle );
581 else
582 ERR("(%04X, %d): strange delta value\n", handle, delta );
585 /***********************************************************************
586 * GlobalSize (KERNEL.20)
587 * GlobalSize16 (KERNEL32.32)
589 * Get the current size of a global memory object.
591 * RETURNS
592 * Size in bytes of object
593 * 0: Failure
595 DWORD WINAPI GlobalSize16(
596 HGLOBAL16 handle /* [in] Handle of global memory object */
598 TRACE("%04x\n", handle );
599 if (!handle) return 0;
600 if (!VALID_HANDLE(handle))
601 return 0;
602 return GET_ARENA_PTR(handle)->size;
606 /***********************************************************************
607 * GlobalHandle (KERNEL.21)
609 * Get the handle associated with a pointer to the global memory block.
611 * NOTES
612 * Why is GlobalHandleToSel used here with the sel as input?
614 * RETURNS
615 * Handle: Success
616 * NULL: Failure
618 DWORD WINAPI GlobalHandle16(
619 WORD sel /* [in] Address of global memory block */
621 TRACE("%04x\n", sel );
622 if (!VALID_HANDLE(sel)) {
623 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
624 return 0;
626 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
629 /***********************************************************************
630 * GlobalHandleNoRIP (KERNEL.159)
632 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
634 int i;
635 for (i = globalArenaSize-1 ; i>=0 ; i--) {
636 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
637 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
639 return 0;
643 /***********************************************************************
644 * GlobalFlags (KERNEL.22)
646 * Get information about a global memory object.
648 * NOTES
649 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
650 * handle?
652 * RETURNS
653 * Value specifying flags and lock count
654 * GMEM_INVALID_HANDLE: Invalid handle
656 UINT16 WINAPI GlobalFlags16(
657 HGLOBAL16 handle /* [in] Handle of global memory object */
659 GLOBALARENA *pArena;
661 TRACE("%04x\n", handle );
662 if (!VALID_HANDLE(handle)) {
663 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
664 return 0;
666 pArena = GET_ARENA_PTR(handle);
667 return pArena->lockCount |
668 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
669 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
673 /***********************************************************************
674 * LockSegment (KERNEL.23)
676 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
678 TRACE("%04x\n", handle );
679 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
680 if (!VALID_HANDLE(handle)) {
681 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
682 return 0;
684 GET_ARENA_PTR(handle)->lockCount++;
685 return handle;
689 /***********************************************************************
690 * UnlockSegment (KERNEL.24)
692 void WINAPI UnlockSegment16( HGLOBAL16 handle )
694 TRACE("%04x\n", handle );
695 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
696 if (!VALID_HANDLE(handle)) {
697 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
698 return;
700 GET_ARENA_PTR(handle)->lockCount--;
701 /* FIXME: this ought to return the lock count in CX (go figure...) */
705 /***********************************************************************
706 * GlobalCompact (KERNEL.25)
708 DWORD WINAPI GlobalCompact16( DWORD desired )
710 return GLOBAL_MAX_ALLOC_SIZE;
714 /***********************************************************************
715 * GlobalFreeAll (KERNEL.26)
717 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
719 int i;
720 GLOBALARENA *pArena;
722 pArena = pGlobalArena;
723 for (i = 0; i < globalArenaSize; i++, pArena++)
725 if ((pArena->size != 0) && (pArena->hOwner == owner))
726 GlobalFree16( pArena->handle );
731 /***********************************************************************
732 * GlobalWire (KERNEL.111)
733 * GlobalWire16 (KERNEL32.29)
735 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
737 return WIN16_GlobalLock16( handle );
741 /***********************************************************************
742 * GlobalUnWire (KERNEL.112)
743 * GlobalUnWire16 (KERNEL32.30)
745 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
747 return !GlobalUnlock16( handle );
751 /***********************************************************************
752 * SetSwapAreaSize (KERNEL.106)
754 LONG WINAPI SetSwapAreaSize16( WORD size )
756 FIXME("(%d) - stub!\n", size );
757 return MAKELONG( size, 0xffff );
761 /***********************************************************************
762 * GlobalLRUOldest (KERNEL.163)
764 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
766 TRACE("%04x\n", handle );
767 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
768 return handle;
772 /***********************************************************************
773 * GlobalLRUNewest (KERNEL.164)
775 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
777 TRACE("%04x\n", handle );
778 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
779 return handle;
783 /***********************************************************************
784 * GetFreeSpace (KERNEL.169)
786 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
788 MEMORYSTATUS ms;
789 GlobalMemoryStatus( &ms );
790 return ms.dwAvailVirtual;
793 /***********************************************************************
794 * GlobalDOSAlloc (KERNEL.184)
796 * Allocate memory in the first MB.
798 * RETURNS
799 * Address (HW=Paragraph segment; LW=Selector)
801 DWORD WINAPI GlobalDOSAlloc16(
802 DWORD size /* [in] Number of bytes to be allocated */
804 UINT16 uParagraph;
805 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
807 if( lpBlock )
809 HMODULE16 hModule = GetModuleHandle16("KERNEL");
810 WORD wSelector;
811 GLOBALARENA *pArena;
813 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
814 pArena = GET_ARENA_PTR(wSelector);
815 pArena->flags |= GA_DOSMEM;
816 return MAKELONG(wSelector,uParagraph);
818 return 0;
822 /***********************************************************************
823 * GlobalDOSFree (KERNEL.185)
825 * Free memory allocated with GlobalDOSAlloc
827 * RETURNS
828 * NULL: Success
829 * sel: Failure
831 WORD WINAPI GlobalDOSFree16(
832 WORD sel /* [in] Selector */
834 DWORD block = GetSelectorBase(sel);
836 if( block && block < 0x100000 )
838 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
839 if( DOSMEM_FreeBlock( lpBlock ) )
840 GLOBAL_FreeBlock( sel );
841 sel = 0;
843 return sel;
847 /***********************************************************************
848 * GlobalPageLock (KERNEL.191)
849 * GlobalSmartPageLock(KERNEL.230)
851 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
853 TRACE("%04x\n", handle );
854 if (!VALID_HANDLE(handle)) {
855 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
856 return 0;
858 return ++(GET_ARENA_PTR(handle)->pageLockCount);
862 /***********************************************************************
863 * GlobalPageUnlock (KERNEL.192)
864 * GlobalSmartPageUnlock(KERNEL.231)
866 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
868 TRACE("%04x\n", handle );
869 if (!VALID_HANDLE(handle)) {
870 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
871 return 0;
873 return --(GET_ARENA_PTR(handle)->pageLockCount);
877 /***********************************************************************
878 * GlobalFix (KERNEL.197)
879 * GlobalFix16 (KERNEL32.27)
881 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
883 TRACE("%04x\n", handle );
884 if (!VALID_HANDLE(handle)) {
885 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
886 return 0;
888 GET_ARENA_PTR(handle)->lockCount++;
890 return GlobalHandleToSel16(handle);
894 /***********************************************************************
895 * GlobalUnfix (KERNEL.198)
896 * GlobalUnfix16 (KERNEL32.28)
898 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
900 TRACE("%04x\n", handle );
901 if (!VALID_HANDLE(handle)) {
902 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
903 return;
905 GET_ARENA_PTR(handle)->lockCount--;
909 /***********************************************************************
910 * FarSetOwner (KERNEL.403)
912 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
914 if (!VALID_HANDLE(handle)) {
915 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
916 return;
918 GET_ARENA_PTR(handle)->hOwner = hOwner;
922 /***********************************************************************
923 * FarGetOwner (KERNEL.404)
925 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
927 if (!VALID_HANDLE(handle)) {
928 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
929 return 0;
931 return GET_ARENA_PTR(handle)->hOwner;
935 /************************************************************************
936 * GlobalMasterHandle (KERNEL.28)
939 * Should return selector and handle of the information structure for
940 * the global heap. selector and handle are stored in the THHOOK as
941 * pGlobalHeap and hGlobalHeap.
942 * As Wine doesn't have this structure, we return both values as zero
943 * Applications should interpret this as "No Global Heap"
945 DWORD WINAPI GlobalMasterHandle16(void)
947 FIXME(": stub\n");
948 return 0;
951 /***********************************************************************
952 * GlobalHandleToSel (TOOLHELP.50)
954 * FIXME: This is in TOOLHELP but we keep a copy here for now.
956 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
958 if (!handle) return 0;
959 if (!VALID_HANDLE(handle)) {
960 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
961 return 0;
963 if (!(handle & 7))
965 WARN("Program attempted invalid selector conversion\n" );
966 return handle - 1;
968 return handle | 7;
972 /***********************************************************************
973 * GetFreeMemInfo (KERNEL.316)
975 DWORD WINAPI GetFreeMemInfo16(void)
977 MEMORYSTATUS status;
978 GlobalMemoryStatus( &status );
979 return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
982 /***********************************************************************
983 * A20Proc (KERNEL.165)
985 void WINAPI A20Proc16( WORD unused )
987 /* this is also a NOP in Windows */
990 /***********************************************************************
991 * LimitEMSPages (KERNEL.156)
993 DWORD WINAPI LimitEMSPages16( DWORD unused )
995 return 0;