wrc: Remove the context prefix when generating the po file for English.
[wine.git] / dlls / krnl386.exe16 / global.c
blob7f3807e28a566f8321da363f569e010148d77907
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, CONTEXT *context )
566 if ( delta == 1 )
567 GlobalLock16( handle );
568 else if ( delta == -1 )
569 GlobalUnlock16( handle );
570 else
571 ERR("(%04X, %d): strange delta value\n", handle, delta );
574 /***********************************************************************
575 * GlobalSize (KERNEL.20)
576 * GlobalSize16 (KERNEL32.32)
578 * Get the current size of a global memory object.
580 * RETURNS
581 * Size in bytes of object
582 * 0: Failure
584 DWORD WINAPI GlobalSize16(
585 HGLOBAL16 handle /* [in] Handle of global memory object */
587 TRACE("%04x\n", handle );
588 if (!handle) return 0;
589 if (!VALID_HANDLE(handle))
590 return 0;
591 return GET_ARENA_PTR(handle)->size;
595 /***********************************************************************
596 * GlobalHandle (KERNEL.21)
598 * Get the handle associated with a pointer to the global memory block.
600 * NOTES
601 * Why is GlobalHandleToSel used here with the sel as input?
603 * RETURNS
604 * Handle: Success
605 * NULL: Failure
607 DWORD WINAPI GlobalHandle16(
608 WORD sel /* [in] Address of global memory block */
610 TRACE("%04x\n", sel );
611 if (!VALID_HANDLE(sel)) {
612 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
613 return 0;
615 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
618 /***********************************************************************
619 * GlobalHandleNoRIP (KERNEL.159)
621 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
623 int i;
624 for (i = globalArenaSize-1 ; i>=0 ; i--) {
625 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
626 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
628 return 0;
632 /***********************************************************************
633 * GlobalFlags (KERNEL.22)
635 * Get information about a global memory object.
637 * NOTES
638 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
639 * handle?
641 * RETURNS
642 * Value specifying flags and lock count
643 * GMEM_INVALID_HANDLE: Invalid handle
645 UINT16 WINAPI GlobalFlags16(
646 HGLOBAL16 handle /* [in] Handle of global memory object */
648 GLOBALARENA *pArena;
650 TRACE("%04x\n", handle );
651 if (!VALID_HANDLE(handle)) {
652 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
653 return 0;
655 pArena = GET_ARENA_PTR(handle);
656 return pArena->lockCount |
657 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
658 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
662 /***********************************************************************
663 * LockSegment (KERNEL.23)
665 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
667 TRACE("%04x\n", handle );
668 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
669 if (!VALID_HANDLE(handle)) {
670 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
671 return 0;
673 GET_ARENA_PTR(handle)->lockCount++;
674 return handle;
678 /***********************************************************************
679 * UnlockSegment (KERNEL.24)
681 void WINAPI UnlockSegment16( HGLOBAL16 handle )
683 TRACE("%04x\n", handle );
684 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
685 if (!VALID_HANDLE(handle)) {
686 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
687 return;
689 GET_ARENA_PTR(handle)->lockCount--;
690 /* FIXME: this ought to return the lock count in CX (go figure...) */
694 /***********************************************************************
695 * GlobalCompact (KERNEL.25)
697 DWORD WINAPI GlobalCompact16( DWORD desired )
699 return GLOBAL_MAX_ALLOC_SIZE;
703 /***********************************************************************
704 * GlobalFreeAll (KERNEL.26)
706 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
708 int i;
709 GLOBALARENA *pArena;
711 pArena = pGlobalArena;
712 for (i = 0; i < globalArenaSize; i++, pArena++)
714 if ((pArena->size != 0) && (pArena->hOwner == owner))
715 GlobalFree16( pArena->handle );
720 /***********************************************************************
721 * GlobalWire (KERNEL.111)
722 * GlobalWire16 (KERNEL32.29)
724 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
726 return WIN16_GlobalLock16( handle );
730 /***********************************************************************
731 * GlobalUnWire (KERNEL.112)
732 * GlobalUnWire16 (KERNEL32.30)
734 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
736 return !GlobalUnlock16( handle );
740 /***********************************************************************
741 * SetSwapAreaSize (KERNEL.106)
743 LONG WINAPI SetSwapAreaSize16( WORD size )
745 FIXME("(%d) - stub!\n", size );
746 return MAKELONG( size, 0xffff );
750 /***********************************************************************
751 * GlobalLRUOldest (KERNEL.163)
753 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
755 TRACE("%04x\n", handle );
756 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
757 return handle;
761 /***********************************************************************
762 * GlobalLRUNewest (KERNEL.164)
764 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
766 TRACE("%04x\n", handle );
767 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
768 return handle;
772 /***********************************************************************
773 * GetFreeSpace (KERNEL.169)
775 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
777 MEMORYSTATUS ms;
778 GlobalMemoryStatus( &ms );
779 return ms.dwAvailVirtual;
782 /***********************************************************************
783 * GlobalDOSAlloc (KERNEL.184)
785 * Allocate memory in the first MB.
787 * RETURNS
788 * Address (HW=Paragraph segment; LW=Selector)
790 DWORD WINAPI GlobalDOSAlloc16(
791 DWORD size /* [in] Number of bytes to be allocated */
793 UINT16 uParagraph;
794 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
796 if( lpBlock )
798 HMODULE16 hModule = GetModuleHandle16("KERNEL");
799 WORD wSelector;
800 GLOBALARENA *pArena;
802 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
803 pArena = GET_ARENA_PTR(wSelector);
804 pArena->flags |= GA_DOSMEM;
805 return MAKELONG(wSelector,uParagraph);
807 return 0;
811 /***********************************************************************
812 * GlobalDOSFree (KERNEL.185)
814 * Free memory allocated with GlobalDOSAlloc
816 * RETURNS
817 * NULL: Success
818 * sel: Failure
820 WORD WINAPI GlobalDOSFree16(
821 WORD sel /* [in] Selector */
823 DWORD block = GetSelectorBase(sel);
825 if( block && block < 0x100000 )
827 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
828 if( DOSMEM_FreeBlock( lpBlock ) )
829 GLOBAL_FreeBlock( sel );
830 sel = 0;
832 return sel;
836 /***********************************************************************
837 * GlobalPageLock (KERNEL.191)
838 * GlobalSmartPageLock(KERNEL.230)
840 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
842 TRACE("%04x\n", handle );
843 if (!VALID_HANDLE(handle)) {
844 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
845 return 0;
847 return ++(GET_ARENA_PTR(handle)->pageLockCount);
851 /***********************************************************************
852 * GlobalPageUnlock (KERNEL.192)
853 * GlobalSmartPageUnlock(KERNEL.231)
855 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
857 TRACE("%04x\n", handle );
858 if (!VALID_HANDLE(handle)) {
859 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
860 return 0;
862 return --(GET_ARENA_PTR(handle)->pageLockCount);
866 /***********************************************************************
867 * GlobalFix (KERNEL.197)
868 * GlobalFix16 (KERNEL32.27)
870 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
872 TRACE("%04x\n", handle );
873 if (!VALID_HANDLE(handle)) {
874 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
875 return 0;
877 GET_ARENA_PTR(handle)->lockCount++;
879 return GlobalHandleToSel16(handle);
883 /***********************************************************************
884 * GlobalUnfix (KERNEL.198)
885 * GlobalUnfix16 (KERNEL32.28)
887 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
889 TRACE("%04x\n", handle );
890 if (!VALID_HANDLE(handle)) {
891 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
892 return;
894 GET_ARENA_PTR(handle)->lockCount--;
898 /***********************************************************************
899 * FarSetOwner (KERNEL.403)
901 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
903 if (!VALID_HANDLE(handle)) {
904 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
905 return;
907 GET_ARENA_PTR(handle)->hOwner = hOwner;
911 /***********************************************************************
912 * FarGetOwner (KERNEL.404)
914 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
916 if (!VALID_HANDLE(handle)) {
917 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
918 return 0;
920 return GET_ARENA_PTR(handle)->hOwner;
924 /************************************************************************
925 * GlobalMasterHandle (KERNEL.28)
928 * Should return selector and handle of the information structure for
929 * the global heap. selector and handle are stored in the THHOOK as
930 * pGlobalHeap and hGlobalHeap.
931 * As Wine doesn't have this structure, we return both values as zero
932 * Applications should interpret this as "No Global Heap"
934 DWORD WINAPI GlobalMasterHandle16(void)
936 FIXME(": stub\n");
937 return 0;
940 /***********************************************************************
941 * GlobalHandleToSel (TOOLHELP.50)
943 * FIXME: This is in TOOLHELP but we keep a copy here for now.
945 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
947 if (!handle) return 0;
948 if (!VALID_HANDLE(handle)) {
949 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
950 return 0;
952 if (!(handle & 7))
954 WARN("Program attempted invalid selector conversion\n" );
955 return handle - 1;
957 return handle | 7;
961 /***********************************************************************
962 * GetFreeMemInfo (KERNEL.316)
964 DWORD WINAPI GetFreeMemInfo16(void)
966 MEMORYSTATUS status;
967 GlobalMemoryStatus( &status );
968 return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
971 /***********************************************************************
972 * A20Proc (KERNEL.165)
974 void WINAPI A20Proc16( WORD unused )
976 /* this is also a NOP in Windows */
979 /***********************************************************************
980 * LimitEMSPages (KERNEL.156)
982 DWORD WINAPI LimitEMSPages16( DWORD unused )
984 return 0;