msdaps: Add support for remoting IRowsetLocate_GetRowsAt.
[wine/multimedia.git] / dlls / krnl386.exe16 / global.c
blobdb07477b5ecd2410c28c387e95f7cb5ad4c0752e
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 SELECTOR_FreeBlock( sel );
186 memset( pArena, 0, sizeof(GLOBALARENA) );
187 return TRUE;
190 /***********************************************************************
191 * GLOBAL_MoveBlock
193 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
195 WORD sel;
196 GLOBALARENA *pArena;
198 if (!handle) return TRUE;
199 sel = GlobalHandleToSel16( handle );
200 if (!VALID_HANDLE(sel)) return FALSE;
201 pArena = GET_ARENA_PTR(sel);
202 if (pArena->selCount != 1)
203 return FALSE;
205 pArena->base = ptr;
206 pArena->size = size;
207 SELECTOR_ReallocBlock( sel, ptr, size );
208 return TRUE;
211 /***********************************************************************
212 * GLOBAL_Alloc
214 * Implementation of GlobalAlloc16()
216 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
218 void *ptr;
219 HGLOBAL16 handle;
221 TRACE("%d flags=%04x\n", size, flags );
223 /* If size is 0, create a discarded block */
225 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
227 /* Fixup the size */
229 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
230 size = (size + 0x1f) & ~0x1f;
232 /* Allocate the linear memory */
233 ptr = HeapAlloc( get_win16_heap(), 0, size );
234 /* FIXME: free discardable blocks and try again? */
235 if (!ptr) return 0;
237 /* Allocate the selector(s) */
239 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
240 if (!handle)
242 HeapFree( get_win16_heap(), 0, ptr );
243 return 0;
246 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
247 return handle;
250 /***********************************************************************
251 * GlobalAlloc (KERNEL.15)
252 * GlobalAlloc16 (KERNEL32.24)
254 * Allocate a global memory object.
256 * RETURNS
257 * Handle: Success
258 * NULL: Failure
260 HGLOBAL16 WINAPI GlobalAlloc16(
261 UINT16 flags, /* [in] Object allocation attributes */
262 DWORD size /* [in] Number of bytes to allocate */
264 HANDLE16 owner = GetCurrentPDB16();
266 if (flags & GMEM_DDESHARE)
268 /* make it owned by the calling module */
269 STACK16FRAME *frame = CURRENT_STACK16;
270 owner = GetExePtr( frame->cs );
272 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
276 /***********************************************************************
277 * GlobalReAlloc (KERNEL.16)
279 * Change the size or attributes of a global memory object.
281 * RETURNS
282 * Handle: Success
283 * NULL: Failure
285 HGLOBAL16 WINAPI GlobalReAlloc16(
286 HGLOBAL16 handle, /* [in] Handle of global memory object */
287 DWORD size, /* [in] New size of block */
288 UINT16 flags /* [in] How to reallocate object */
290 WORD selcount;
291 DWORD oldsize;
292 void *ptr, *newptr;
293 GLOBALARENA *pArena, *pNewArena;
294 WORD sel = GlobalHandleToSel16( handle );
295 HANDLE heap = get_win16_heap();
297 TRACE("%04x %d flags=%04x\n",
298 handle, size, flags );
299 if (!handle) return 0;
301 if (!VALID_HANDLE(handle))
303 WARN("Invalid handle 0x%04x!\n", handle);
304 return 0;
306 pArena = GET_ARENA_PTR( handle );
308 /* Discard the block if requested */
310 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
312 if (!(pArena->flags & GA_MOVEABLE) ||
313 !(pArena->flags & GA_DISCARDABLE) ||
314 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
315 if (pArena->flags & GA_DOSMEM)
316 DOSMEM_FreeBlock( pArena->base );
317 else
318 HeapFree( heap, 0, pArena->base );
319 pArena->base = 0;
321 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
322 * change the selector if we are shrinking the block.
323 * FIXME: shouldn't we keep selectors until the block is deleted?
325 SELECTOR_ReallocBlock( sel, 0, 1 );
326 return handle;
329 /* Fixup the size */
331 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
332 if (size == 0) size = 0x20;
333 else size = (size + 0x1f) & ~0x1f;
335 /* Change the flags */
337 if (flags & GMEM_MODIFY)
339 /* Change the flags, leaving GA_DGROUP alone */
340 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
341 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
342 return handle;
345 /* Reallocate the linear memory */
347 ptr = pArena->base;
348 oldsize = pArena->size;
349 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr,oldsize,size);
350 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
352 if (pArena->flags & GA_DOSMEM)
354 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
355 newptr = ptr;
356 else if(pArena->pageLockCount > 0)
357 newptr = 0;
358 else
360 newptr = DOSMEM_AllocBlock( size, 0 );
361 if (newptr)
363 memcpy( newptr, ptr, oldsize );
364 DOSMEM_FreeBlock( ptr );
368 else
371 * if more than one reader (e.g. some pointer has been
372 * given out by GetVDMPointer32W16),
373 * only try to realloc in place
376 if (ptr)
377 newptr = HeapReAlloc( heap,
378 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
379 ptr, size );
380 else
381 newptr = HeapAlloc( heap, 0, size );
385 if (!newptr)
387 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
388 if (pArena->pageLockCount <1)
390 if (pArena->flags & GA_DOSMEM)
391 DOSMEM_FreeBlock( pArena->base );
392 else
393 HeapFree( heap, 0, ptr );
394 SELECTOR_FreeBlock( sel );
395 memset( pArena, 0, sizeof(GLOBALARENA) );
397 return 0;
399 ptr = newptr;
401 /* Reallocate the selector(s) */
403 sel = SELECTOR_ReallocBlock( sel, ptr, size );
404 if (!sel)
406 if (pArena->flags & GA_DOSMEM)
407 DOSMEM_FreeBlock( pArena->base );
408 else
409 HeapFree( heap, 0, ptr );
410 memset( pArena, 0, sizeof(GLOBALARENA) );
411 return 0;
413 selcount = (size + 0xffff) / 0x10000;
415 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
417 if (pArena->flags & GA_DOSMEM)
418 DOSMEM_FreeBlock( pArena->base );
419 else
420 HeapFree( heap, 0, ptr );
421 SELECTOR_FreeBlock( sel );
422 return 0;
425 /* Fill the new arena block
426 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
428 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
429 pNewArena->base = ptr;
430 pNewArena->size = GetSelectorLimit16(sel) + 1;
431 pNewArena->selCount = selcount;
432 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
434 if (selcount > 1) /* clear the next arena blocks */
435 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
437 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
438 memset( (char *)ptr + oldsize, 0, size - oldsize );
439 return pNewArena->handle;
443 /***********************************************************************
444 * GlobalFree (KERNEL.17)
445 * GlobalFree16 (KERNEL32.31)
446 * RETURNS
447 * NULL: Success
448 * Handle: Failure
450 HGLOBAL16 WINAPI GlobalFree16(
451 HGLOBAL16 handle /* [in] Handle of global memory object */
453 void *ptr;
455 if (!VALID_HANDLE(handle))
457 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
458 return 0;
460 ptr = GET_ARENA_PTR(handle)->base;
462 TRACE("%04x\n", handle );
463 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
464 HeapFree( get_win16_heap(), 0, ptr );
465 return 0;
469 /**********************************************************************
470 * K32WOWGlobalLock16 (KERNEL32.60)
472 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
474 WORD sel = GlobalHandleToSel16( handle );
475 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
477 if (handle)
479 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
481 if (!VALID_HANDLE(handle)) {
482 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
483 sel = 0;
485 else if (!GET_ARENA_PTR(handle)->base)
486 sel = 0;
487 else
488 GET_ARENA_PTR(handle)->lockCount++;
491 return MAKESEGPTR( sel, 0 );
496 /***********************************************************************
497 * GlobalLock (KERNEL.18)
499 * This is the GlobalLock16() function used by 16-bit code.
501 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
503 SEGPTR ret = K32WOWGlobalLock16( handle );
504 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
505 return ret;
509 /***********************************************************************
510 * GlobalLock16 (KERNEL32.25)
512 * This is the GlobalLock16() function used by 32-bit code.
514 * RETURNS
515 * Pointer to first byte of memory block
516 * NULL: Failure
518 LPVOID WINAPI GlobalLock16(
519 HGLOBAL16 handle /* [in] Handle of global memory object */
521 if (!handle) return 0;
522 if (!VALID_HANDLE(handle))
523 return 0;
524 GET_ARENA_PTR(handle)->lockCount++;
525 return GET_ARENA_PTR(handle)->base;
529 /***********************************************************************
530 * GlobalUnlock (KERNEL.19)
531 * GlobalUnlock16 (KERNEL32.26)
532 * NOTES
533 * Should the return values be cast to booleans?
535 * RETURNS
536 * TRUE: Object is still locked
537 * FALSE: Object is unlocked
539 BOOL16 WINAPI GlobalUnlock16(
540 HGLOBAL16 handle /* [in] Handle of global memory object */
542 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
543 if (!VALID_HANDLE(handle)) {
544 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
545 return 0;
547 TRACE("%04x\n", handle );
548 if (pArena->lockCount) pArena->lockCount--;
549 return pArena->lockCount;
552 /***********************************************************************
553 * GlobalChangeLockCount (KERNEL.365)
555 * This is declared as a register function as it has to preserve
556 * *all* registers, even AX/DX !
559 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
560 CONTEXT86 *context )
562 if ( delta == 1 )
563 GlobalLock16( handle );
564 else if ( delta == -1 )
565 GlobalUnlock16( handle );
566 else
567 ERR("(%04X, %d): strange delta value\n", handle, delta );
570 /***********************************************************************
571 * GlobalSize (KERNEL.20)
572 * GlobalSize16 (KERNEL32.32)
574 * Get the current size of a global memory object.
576 * RETURNS
577 * Size in bytes of object
578 * 0: Failure
580 DWORD WINAPI GlobalSize16(
581 HGLOBAL16 handle /* [in] Handle of global memory object */
583 TRACE("%04x\n", handle );
584 if (!handle) return 0;
585 if (!VALID_HANDLE(handle))
586 return 0;
587 return GET_ARENA_PTR(handle)->size;
591 /***********************************************************************
592 * GlobalHandle (KERNEL.21)
594 * Get the handle associated with a pointer to the global memory block.
596 * NOTES
597 * Why is GlobalHandleToSel used here with the sel as input?
599 * RETURNS
600 * Handle: Success
601 * NULL: Failure
603 DWORD WINAPI GlobalHandle16(
604 WORD sel /* [in] Address of global memory block */
606 TRACE("%04x\n", sel );
607 if (!VALID_HANDLE(sel)) {
608 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
609 return 0;
611 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
614 /***********************************************************************
615 * GlobalHandleNoRIP (KERNEL.159)
617 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
619 int i;
620 for (i = globalArenaSize-1 ; i>=0 ; i--) {
621 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
622 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
624 return 0;
628 /***********************************************************************
629 * GlobalFlags (KERNEL.22)
631 * Get information about a global memory object.
633 * NOTES
634 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
635 * handle?
637 * RETURNS
638 * Value specifying flags and lock count
639 * GMEM_INVALID_HANDLE: Invalid handle
641 UINT16 WINAPI GlobalFlags16(
642 HGLOBAL16 handle /* [in] Handle of global memory object */
644 GLOBALARENA *pArena;
646 TRACE("%04x\n", handle );
647 if (!VALID_HANDLE(handle)) {
648 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
649 return 0;
651 pArena = GET_ARENA_PTR(handle);
652 return pArena->lockCount |
653 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
654 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
658 /***********************************************************************
659 * LockSegment (KERNEL.23)
661 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
663 TRACE("%04x\n", handle );
664 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
665 if (!VALID_HANDLE(handle)) {
666 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
667 return 0;
669 GET_ARENA_PTR(handle)->lockCount++;
670 return handle;
674 /***********************************************************************
675 * UnlockSegment (KERNEL.24)
677 void WINAPI UnlockSegment16( HGLOBAL16 handle )
679 TRACE("%04x\n", handle );
680 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
681 if (!VALID_HANDLE(handle)) {
682 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
683 return;
685 GET_ARENA_PTR(handle)->lockCount--;
686 /* FIXME: this ought to return the lock count in CX (go figure...) */
690 /***********************************************************************
691 * GlobalCompact (KERNEL.25)
693 DWORD WINAPI GlobalCompact16( DWORD desired )
695 return GLOBAL_MAX_ALLOC_SIZE;
699 /***********************************************************************
700 * GlobalFreeAll (KERNEL.26)
702 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
704 int i;
705 GLOBALARENA *pArena;
707 pArena = pGlobalArena;
708 for (i = 0; i < globalArenaSize; i++, pArena++)
710 if ((pArena->size != 0) && (pArena->hOwner == owner))
711 GlobalFree16( pArena->handle );
716 /***********************************************************************
717 * GlobalWire (KERNEL.111)
718 * GlobalWire16 (KERNEL32.29)
720 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
722 return WIN16_GlobalLock16( handle );
726 /***********************************************************************
727 * GlobalUnWire (KERNEL.112)
728 * GlobalUnWire16 (KERNEL32.30)
730 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
732 return !GlobalUnlock16( handle );
736 /***********************************************************************
737 * SetSwapAreaSize (KERNEL.106)
739 LONG WINAPI SetSwapAreaSize16( WORD size )
741 FIXME("(%d) - stub!\n", size );
742 return MAKELONG( size, 0xffff );
746 /***********************************************************************
747 * GlobalLRUOldest (KERNEL.163)
749 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
751 TRACE("%04x\n", handle );
752 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
753 return handle;
757 /***********************************************************************
758 * GlobalLRUNewest (KERNEL.164)
760 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
762 TRACE("%04x\n", handle );
763 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
764 return handle;
768 /***********************************************************************
769 * GetFreeSpace (KERNEL.169)
771 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
773 MEMORYSTATUS ms;
774 GlobalMemoryStatus( &ms );
775 return ms.dwAvailVirtual;
778 /***********************************************************************
779 * GlobalDOSAlloc (KERNEL.184)
781 * Allocate memory in the first MB.
783 * RETURNS
784 * Address (HW=Paragraph segment; LW=Selector)
786 DWORD WINAPI GlobalDOSAlloc16(
787 DWORD size /* [in] Number of bytes to be allocated */
789 UINT16 uParagraph;
790 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
792 if( lpBlock )
794 HMODULE16 hModule = GetModuleHandle16("KERNEL");
795 WORD wSelector;
796 GLOBALARENA *pArena;
798 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
799 pArena = GET_ARENA_PTR(wSelector);
800 pArena->flags |= GA_DOSMEM;
801 return MAKELONG(wSelector,uParagraph);
803 return 0;
807 /***********************************************************************
808 * GlobalDOSFree (KERNEL.185)
810 * Free memory allocated with GlobalDOSAlloc
812 * RETURNS
813 * NULL: Success
814 * sel: Failure
816 WORD WINAPI GlobalDOSFree16(
817 WORD sel /* [in] Selector */
819 DWORD block = GetSelectorBase(sel);
821 if( block && block < 0x100000 )
823 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
824 if( DOSMEM_FreeBlock( lpBlock ) )
825 GLOBAL_FreeBlock( sel );
826 sel = 0;
828 return sel;
832 /***********************************************************************
833 * GlobalPageLock (KERNEL.191)
834 * GlobalSmartPageLock(KERNEL.230)
836 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
838 TRACE("%04x\n", handle );
839 if (!VALID_HANDLE(handle)) {
840 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
841 return 0;
843 return ++(GET_ARENA_PTR(handle)->pageLockCount);
847 /***********************************************************************
848 * GlobalPageUnlock (KERNEL.192)
849 * GlobalSmartPageUnlock(KERNEL.231)
851 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
853 TRACE("%04x\n", handle );
854 if (!VALID_HANDLE(handle)) {
855 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
856 return 0;
858 return --(GET_ARENA_PTR(handle)->pageLockCount);
862 /***********************************************************************
863 * GlobalFix (KERNEL.197)
864 * GlobalFix16 (KERNEL32.27)
866 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
868 TRACE("%04x\n", handle );
869 if (!VALID_HANDLE(handle)) {
870 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
871 return 0;
873 GET_ARENA_PTR(handle)->lockCount++;
875 return GlobalHandleToSel16(handle);
879 /***********************************************************************
880 * GlobalUnfix (KERNEL.198)
881 * GlobalUnfix16 (KERNEL32.28)
883 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
885 TRACE("%04x\n", handle );
886 if (!VALID_HANDLE(handle)) {
887 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
888 return;
890 GET_ARENA_PTR(handle)->lockCount--;
894 /***********************************************************************
895 * FarSetOwner (KERNEL.403)
897 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
899 if (!VALID_HANDLE(handle)) {
900 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
901 return;
903 GET_ARENA_PTR(handle)->hOwner = hOwner;
907 /***********************************************************************
908 * FarGetOwner (KERNEL.404)
910 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
912 if (!VALID_HANDLE(handle)) {
913 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
914 return 0;
916 return GET_ARENA_PTR(handle)->hOwner;
920 /************************************************************************
921 * GlobalMasterHandle (KERNEL.28)
924 * Should return selector and handle of the information structure for
925 * the global heap. selector and handle are stored in the THHOOK as
926 * pGlobalHeap and hGlobalHeap.
927 * As Wine doesn't have this structure, we return both values as zero
928 * Applications should interpret this as "No Global Heap"
930 DWORD WINAPI GlobalMasterHandle16(void)
932 FIXME(": stub\n");
933 return 0;
936 /***********************************************************************
937 * GlobalHandleToSel (TOOLHELP.50)
939 * FIXME: This is in TOOLHELP but we keep a copy here for now.
941 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
943 if (!handle) return 0;
944 if (!VALID_HANDLE(handle)) {
945 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
946 return 0;
948 if (!(handle & 7))
950 WARN("Program attempted invalid selector conversion\n" );
951 return handle - 1;
953 return handle | 7;
957 /***********************************************************************
958 * GetFreeMemInfo (KERNEL.316)
960 DWORD WINAPI GetFreeMemInfo16(void)
962 MEMORYSTATUS status;
963 GlobalMemoryStatus( &status );
964 return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
967 /***********************************************************************
968 * A20Proc (KERNEL.165)
970 void WINAPI A20Proc16( WORD unused )
972 /* this is also a NOP in Windows */
975 /***********************************************************************
976 * LimitEMSPages (KERNEL.156)
978 DWORD WINAPI LimitEMSPages16( DWORD unused )
980 return 0;