server: Get rid of the remaining PowerPC support.
[wine.git] / dlls / krnl386.exe16 / global.c
blob0a8da28e99bb12a54fed4d29ed77a50d9b2c0602
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 <sys/types.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "wine/winbase16.h"
29 #include "winternl.h"
30 #include "kernel16_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(global);
35 /* Global arena block */
36 typedef struct
38 void *base; /* Base address (0 if discarded) */
39 DWORD size; /* Size in bytes (0 indicates a free block) */
40 HGLOBAL16 handle; /* Handle for this block */
41 HGLOBAL16 hOwner; /* Owner of this block */
42 BYTE lockCount; /* Count of GlobalFix() calls */
43 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
44 BYTE flags; /* Allocation flags */
45 BYTE selCount; /* Number of selectors allocated for this block */
46 } GLOBALARENA;
48 /* Flags definitions */
49 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
50 #define GA_DGROUP 0x04
51 #define GA_DISCARDABLE 0x08
52 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
53 #define GA_DOSMEM 0x20
55 /* Arena array (FIXME) */
56 static GLOBALARENA *pGlobalArena;
57 static int globalArenaSize;
59 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
60 #define GLOBAL_MAX_COUNT 8192 /* Max number of allocated blocks */
62 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
63 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
65 static HANDLE get_win16_heap(void)
67 static HANDLE win16_heap;
69 /* we create global memory block with execute permission. The access can be limited
70 * for 16-bit code on selector level */
71 if (!win16_heap) win16_heap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
72 return win16_heap;
75 /***********************************************************************
76 * GLOBAL_GetArena
78 * Return the arena for a given selector, growing the arena array if needed.
80 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
82 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
84 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
86 if (!pGlobalArena)
88 pGlobalArena = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
89 GLOBAL_MAX_COUNT * sizeof(GLOBALARENA) );
90 if (!pGlobalArena) return 0;
91 /* Hack: store a pointer to it in THHOOK instead of a handle */
92 *(GLOBALARENA **)&pThhook->hGlobalHeap = pGlobalArena;
94 if (newsize > GLOBAL_MAX_COUNT) return 0;
95 globalArenaSize = newsize;
97 return pGlobalArena + (sel >> __AHSHIFT);
101 /***********************************************************************
102 * GLOBAL_CreateBlock
104 * Create a global heap block for a fixed range of linear memory.
106 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
107 HGLOBAL16 hOwner, unsigned char selflags )
109 WORD sel, selcount;
110 GLOBALARENA *pArena;
112 /* Allocate the selector(s) */
114 sel = SELECTOR_AllocBlock( ptr, size, selflags );
115 if (!sel) return 0;
116 selcount = (size + 0xffff) / 0x10000;
118 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
120 SELECTOR_FreeBlock( sel );
121 return 0;
124 /* Fill the arena block */
126 pArena->base = ptr;
127 pArena->size = GetSelectorLimit16(sel) + 1;
128 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
129 pArena->hOwner = hOwner;
130 pArena->lockCount = 0;
131 pArena->pageLockCount = 0;
132 pArena->flags = flags & GA_MOVEABLE;
133 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
134 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
135 if (!(selflags & (LDT_FLAGS_CODE ^ LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
136 pArena->selCount = selcount;
137 if (selcount > 1) /* clear the next arena blocks */
138 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
140 return pArena->handle;
144 /***********************************************************************
145 * GLOBAL_FreeBlock
147 * Free a block allocated by GLOBAL_CreateBlock, without touching
148 * the associated linear memory range.
150 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
152 WORD sel;
153 GLOBALARENA *pArena;
155 if (!handle) return TRUE;
156 sel = GlobalHandleToSel16( handle );
157 if (!VALID_HANDLE(sel)) return FALSE;
158 pArena = GET_ARENA_PTR(sel);
159 if (!pArena->size)
161 WARN( "already free %x\n", handle );
162 return FALSE;
164 SELECTOR_FreeBlock( sel );
165 memset( pArena, 0, sizeof(GLOBALARENA) );
166 return TRUE;
169 /***********************************************************************
170 * GLOBAL_MoveBlock
172 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
174 WORD sel;
175 GLOBALARENA *pArena;
177 if (!handle) return TRUE;
178 sel = GlobalHandleToSel16( handle );
179 if (!VALID_HANDLE(sel)) return FALSE;
180 pArena = GET_ARENA_PTR(sel);
181 if (pArena->selCount != 1)
182 return FALSE;
184 pArena->base = ptr;
185 pArena->size = size;
186 SELECTOR_ReallocBlock( sel, ptr, size );
187 return TRUE;
190 /***********************************************************************
191 * GLOBAL_Alloc
193 * Implementation of GlobalAlloc16()
195 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
197 void *ptr;
198 HGLOBAL16 handle;
200 TRACE("%d flags=%04x\n", size, flags );
202 /* If size is 0, create a discarded block */
204 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
206 /* Fixup the size */
208 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
209 size = (size + 0x1f) & ~0x1f;
211 /* Allocate the linear memory */
212 ptr = HeapAlloc( get_win16_heap(), 0, size );
213 /* FIXME: free discardable blocks and try again? */
214 if (!ptr) return 0;
216 /* Allocate the selector(s) */
218 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
219 if (!handle)
221 HeapFree( get_win16_heap(), 0, ptr );
222 return 0;
225 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
226 return handle;
229 /***********************************************************************
230 * GlobalAlloc (KERNEL.15)
231 * GlobalAlloc16 (KERNEL32.24)
233 * Allocate a global memory object.
235 * RETURNS
236 * Handle: Success
237 * NULL: Failure
239 HGLOBAL16 WINAPI GlobalAlloc16(
240 UINT16 flags, /* [in] Object allocation attributes */
241 DWORD size /* [in] Number of bytes to allocate */
243 HANDLE16 owner = GetCurrentPDB16();
245 if (flags & GMEM_DDESHARE)
247 /* make it owned by the calling module */
248 STACK16FRAME *frame = CURRENT_STACK16;
249 owner = GetExePtr( frame->cs );
251 return GLOBAL_Alloc( flags, size, owner, LDT_FLAGS_DATA );
255 /***********************************************************************
256 * GlobalReAlloc (KERNEL.16)
258 * Change the size or attributes of a global memory object.
260 * RETURNS
261 * Handle: Success
262 * NULL: Failure
264 HGLOBAL16 WINAPI GlobalReAlloc16(
265 HGLOBAL16 handle, /* [in] Handle of global memory object */
266 DWORD size, /* [in] New size of block */
267 UINT16 flags /* [in] How to reallocate object */
269 WORD selcount;
270 DWORD oldsize;
271 void *ptr, *newptr;
272 GLOBALARENA *pArena, *pNewArena;
273 WORD sel = GlobalHandleToSel16( handle );
274 HANDLE heap = get_win16_heap();
276 TRACE("%04x %d flags=%04x\n",
277 handle, size, flags );
278 if (!handle) return 0;
280 if (!VALID_HANDLE(handle))
282 WARN("Invalid handle 0x%04x!\n", handle);
283 return 0;
285 pArena = GET_ARENA_PTR( handle );
287 /* Discard the block if requested */
289 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
291 if (!(pArena->flags & GA_MOVEABLE) ||
292 !(pArena->flags & GA_DISCARDABLE) ||
293 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
294 if (pArena->flags & GA_DOSMEM)
295 DOSMEM_FreeBlock( pArena->base );
296 else
297 HeapFree( heap, 0, pArena->base );
298 pArena->base = 0;
300 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
301 * change the selector if we are shrinking the block.
302 * FIXME: shouldn't we keep selectors until the block is deleted?
304 SELECTOR_ReallocBlock( sel, 0, 1 );
305 return handle;
308 /* Fixup the size */
310 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
311 if (size == 0) size = 0x20;
312 else size = (size + 0x1f) & ~0x1f;
314 /* Change the flags */
316 if (flags & GMEM_MODIFY)
318 /* Change the flags, leaving GA_DGROUP alone */
319 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
320 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
321 return handle;
324 /* Reallocate the linear memory */
326 ptr = pArena->base;
327 oldsize = pArena->size;
328 TRACE("oldbase %p oldsize %08x newsize %08x\n", ptr,oldsize,size);
329 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
331 if (pArena->flags & GA_DOSMEM)
333 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
334 newptr = ptr;
335 else if(pArena->pageLockCount > 0)
336 newptr = 0;
337 else
339 newptr = DOSMEM_AllocBlock( size, NULL );
340 if (newptr)
342 memcpy( newptr, ptr, oldsize );
343 DOSMEM_FreeBlock( ptr );
347 else
350 * if more than one reader (e.g. some pointer has been
351 * given out by GetVDMPointer32W16),
352 * only try to realloc in place
355 if (ptr)
356 newptr = HeapReAlloc( heap,
357 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
358 ptr, size );
359 else
360 newptr = HeapAlloc( heap, 0, size );
364 if (!newptr)
366 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
367 if (pArena->pageLockCount <1)
369 if (pArena->flags & GA_DOSMEM)
370 DOSMEM_FreeBlock( pArena->base );
371 else
372 HeapFree( heap, 0, ptr );
373 SELECTOR_FreeBlock( sel );
374 memset( pArena, 0, sizeof(GLOBALARENA) );
376 return 0;
378 ptr = newptr;
380 /* Reallocate the selector(s) */
382 sel = SELECTOR_ReallocBlock( sel, ptr, size );
383 if (!sel)
385 if (pArena->flags & GA_DOSMEM)
386 DOSMEM_FreeBlock( pArena->base );
387 else
388 HeapFree( heap, 0, ptr );
389 memset( pArena, 0, sizeof(GLOBALARENA) );
390 return 0;
392 selcount = (size + 0xffff) / 0x10000;
394 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
396 if (pArena->flags & GA_DOSMEM)
397 DOSMEM_FreeBlock( pArena->base );
398 else
399 HeapFree( heap, 0, ptr );
400 SELECTOR_FreeBlock( sel );
401 return 0;
404 /* Fill the new arena block
405 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
407 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
408 pNewArena->base = ptr;
409 pNewArena->size = GetSelectorLimit16(sel) + 1;
410 pNewArena->selCount = selcount;
411 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
413 if (selcount > 1) /* clear the next arena blocks */
414 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
416 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
417 memset( (char *)ptr + oldsize, 0, size - oldsize );
418 return pNewArena->handle;
422 /***********************************************************************
423 * GlobalFree (KERNEL.17)
424 * GlobalFree16 (KERNEL32.31)
425 * RETURNS
426 * NULL: Success
427 * Handle: Failure
429 HGLOBAL16 WINAPI GlobalFree16(
430 HGLOBAL16 handle /* [in] Handle of global memory object */
432 void *ptr;
434 if (!VALID_HANDLE(handle))
436 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
437 return 0;
439 ptr = GET_ARENA_PTR(handle)->base;
441 TRACE("%04x\n", handle );
442 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
443 HeapFree( get_win16_heap(), 0, ptr );
444 return 0;
448 /**********************************************************************
449 * K32WOWGlobalLock16 (KERNEL32.60)
451 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
453 WORD sel = GlobalHandleToSel16( handle );
454 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
456 if (handle)
458 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
460 if (!VALID_HANDLE(handle)) {
461 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
462 sel = 0;
464 else if (!GET_ARENA_PTR(handle)->base)
465 sel = 0;
466 else
467 GET_ARENA_PTR(handle)->lockCount++;
470 return MAKESEGPTR( sel, 0 );
475 /***********************************************************************
476 * GlobalLock (KERNEL.18)
478 * This is the GlobalLock16() function used by 16-bit code.
480 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
482 SEGPTR ret = K32WOWGlobalLock16( handle );
483 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
484 return ret;
488 /***********************************************************************
489 * GlobalLock16 (KERNEL32.25)
491 * This is the GlobalLock16() function used by 32-bit code.
493 * RETURNS
494 * Pointer to first byte of memory block
495 * NULL: Failure
497 LPVOID WINAPI GlobalLock16(
498 HGLOBAL16 handle /* [in] Handle of global memory object */
500 if (!handle) return 0;
501 if (!VALID_HANDLE(handle))
502 return 0;
503 GET_ARENA_PTR(handle)->lockCount++;
504 return GET_ARENA_PTR(handle)->base;
508 /***********************************************************************
509 * GlobalUnlock (KERNEL.19)
510 * GlobalUnlock16 (KERNEL32.26)
511 * NOTES
512 * Should the return values be cast to booleans?
514 * RETURNS
515 * TRUE: Object is still locked
516 * FALSE: Object is unlocked
518 BOOL16 WINAPI GlobalUnlock16(
519 HGLOBAL16 handle /* [in] Handle of global memory object */
521 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
522 if (!VALID_HANDLE(handle)) {
523 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
524 return FALSE;
526 TRACE("%04x\n", handle );
527 if (pArena->lockCount) pArena->lockCount--;
528 return pArena->lockCount;
531 /***********************************************************************
532 * GlobalChangeLockCount (KERNEL.365)
534 * This is declared as a register function as it has to preserve
535 * *all* registers, even AX/DX !
538 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta, CONTEXT *context )
540 if ( delta == 1 )
541 GlobalLock16( handle );
542 else if ( delta == -1 )
543 GlobalUnlock16( handle );
544 else
545 ERR("(%04X, %d): strange delta value\n", handle, delta );
548 /***********************************************************************
549 * GlobalSize (KERNEL.20)
550 * GlobalSize16 (KERNEL32.32)
552 * Get the current size of a global memory object.
554 * RETURNS
555 * Size in bytes of object
556 * 0: Failure
558 DWORD WINAPI GlobalSize16(
559 HGLOBAL16 handle /* [in] Handle of global memory object */
561 TRACE("%04x\n", handle );
562 if (!handle) return 0;
563 if (!VALID_HANDLE(handle))
564 return 0;
565 return GET_ARENA_PTR(handle)->size;
569 /***********************************************************************
570 * GlobalHandle (KERNEL.21)
572 * Get the handle associated with a pointer to the global memory block.
574 * NOTES
575 * Why is GlobalHandleToSel used here with the sel as input?
577 * RETURNS
578 * Handle: Success
579 * NULL: Failure
581 DWORD WINAPI GlobalHandle16(
582 WORD sel /* [in] Address of global memory block */
584 TRACE("%04x\n", sel );
585 if (!VALID_HANDLE(sel)) {
586 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
587 return 0;
589 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
592 /***********************************************************************
593 * GlobalHandleNoRIP (KERNEL.159)
595 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
597 int i;
598 for (i = globalArenaSize-1 ; i>=0 ; i--) {
599 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
600 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
602 return 0;
606 /***********************************************************************
607 * GlobalFlags (KERNEL.22)
609 * Get information about a global memory object.
611 * NOTES
612 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
613 * handle?
615 * RETURNS
616 * Value specifying flags and lock count
617 * GMEM_INVALID_HANDLE: Invalid handle
619 UINT16 WINAPI GlobalFlags16(
620 HGLOBAL16 handle /* [in] Handle of global memory object */
622 GLOBALARENA *pArena;
624 TRACE("%04x\n", handle );
625 if (!VALID_HANDLE(handle)) {
626 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
627 return 0;
629 pArena = GET_ARENA_PTR(handle);
630 return pArena->lockCount |
631 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
632 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
636 /***********************************************************************
637 * LockSegment (KERNEL.23)
639 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
641 TRACE("%04x\n", handle );
642 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
643 if (!VALID_HANDLE(handle)) {
644 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
645 return 0;
647 GET_ARENA_PTR(handle)->lockCount++;
648 return handle;
652 /***********************************************************************
653 * UnlockSegment (KERNEL.24)
655 void WINAPI UnlockSegment16( HGLOBAL16 handle )
657 TRACE("%04x\n", handle );
658 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
659 if (!VALID_HANDLE(handle)) {
660 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
661 return;
663 GET_ARENA_PTR(handle)->lockCount--;
664 /* FIXME: this ought to return the lock count in CX (go figure...) */
668 /***********************************************************************
669 * GlobalCompact (KERNEL.25)
671 DWORD WINAPI GlobalCompact16( DWORD desired )
673 return GLOBAL_MAX_ALLOC_SIZE;
677 /***********************************************************************
678 * GlobalFreeAll (KERNEL.26)
680 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
682 int i;
683 GLOBALARENA *pArena;
685 pArena = pGlobalArena;
686 for (i = 0; i < globalArenaSize; i++, pArena++)
688 if ((pArena->size != 0) && (pArena->hOwner == owner))
689 GlobalFree16( pArena->handle );
694 /***********************************************************************
695 * GlobalWire (KERNEL.111)
696 * GlobalWire16 (KERNEL32.29)
698 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
700 return WIN16_GlobalLock16( handle );
704 /***********************************************************************
705 * GlobalUnWire (KERNEL.112)
706 * GlobalUnWire16 (KERNEL32.30)
708 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
710 return !GlobalUnlock16( handle );
714 /***********************************************************************
715 * SetSwapAreaSize (KERNEL.106)
717 LONG WINAPI SetSwapAreaSize16( WORD size )
719 FIXME("(%d) - stub!\n", size );
720 return MAKELONG( size, 0xffff );
724 /***********************************************************************
725 * GlobalLRUOldest (KERNEL.163)
727 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
729 TRACE("%04x\n", handle );
730 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
731 return handle;
735 /***********************************************************************
736 * GlobalLRUNewest (KERNEL.164)
738 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
740 TRACE("%04x\n", handle );
741 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
742 return handle;
746 /***********************************************************************
747 * GetFreeSpace (KERNEL.169)
749 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
751 MEMORYSTATUS ms;
752 GlobalMemoryStatus( &ms );
753 return min( ms.dwAvailVirtual, MAXLONG );
756 /***********************************************************************
757 * GlobalDOSAlloc (KERNEL.184)
759 * Allocate memory in the first MB.
761 * RETURNS
762 * Address (HW=Paragraph segment; LW=Selector)
764 DWORD WINAPI GlobalDOSAlloc16(
765 DWORD size /* [in] Number of bytes to be allocated */
767 UINT16 uParagraph;
768 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
770 if( lpBlock )
772 HMODULE16 hModule = GetModuleHandle16("KERNEL");
773 WORD wSelector;
774 GLOBALARENA *pArena;
776 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, LDT_FLAGS_DATA );
777 pArena = GET_ARENA_PTR(wSelector);
778 pArena->flags |= GA_DOSMEM;
779 return MAKELONG(wSelector,uParagraph);
781 return 0;
785 /***********************************************************************
786 * GlobalDOSFree (KERNEL.185)
788 * Free memory allocated with GlobalDOSAlloc
790 * RETURNS
791 * NULL: Success
792 * sel: Failure
794 WORD WINAPI GlobalDOSFree16(
795 WORD sel /* [in] Selector */
797 DWORD block = GetSelectorBase(sel);
799 if( block && block < 0x100000 )
801 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
802 if( DOSMEM_FreeBlock( lpBlock ) )
803 GLOBAL_FreeBlock( sel );
804 sel = 0;
806 return sel;
810 /***********************************************************************
811 * GlobalPageLock (KERNEL.191)
812 * GlobalSmartPageLock(KERNEL.230)
814 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
816 TRACE("%04x\n", handle );
817 if (!VALID_HANDLE(handle)) {
818 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
819 return 0;
821 return ++(GET_ARENA_PTR(handle)->pageLockCount);
825 /***********************************************************************
826 * GlobalPageUnlock (KERNEL.192)
827 * GlobalSmartPageUnlock(KERNEL.231)
829 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
831 TRACE("%04x\n", handle );
832 if (!VALID_HANDLE(handle)) {
833 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
834 return 0;
836 return --(GET_ARENA_PTR(handle)->pageLockCount);
840 /***********************************************************************
841 * GlobalFix (KERNEL.197)
842 * GlobalFix16 (KERNEL32.27)
844 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
846 TRACE("%04x\n", handle );
847 if (!VALID_HANDLE(handle)) {
848 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
849 return 0;
851 GET_ARENA_PTR(handle)->lockCount++;
853 return GlobalHandleToSel16(handle);
857 /***********************************************************************
858 * GlobalUnfix (KERNEL.198)
859 * GlobalUnfix16 (KERNEL32.28)
861 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
863 TRACE("%04x\n", handle );
864 if (!VALID_HANDLE(handle)) {
865 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
866 return;
868 GET_ARENA_PTR(handle)->lockCount--;
872 /***********************************************************************
873 * FarSetOwner (KERNEL.403)
875 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
877 if (!VALID_HANDLE(handle)) {
878 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
879 return;
881 GET_ARENA_PTR(handle)->hOwner = hOwner;
885 /***********************************************************************
886 * FarGetOwner (KERNEL.404)
888 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
890 if (!VALID_HANDLE(handle)) {
891 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
892 return 0;
894 return GET_ARENA_PTR(handle)->hOwner;
898 /************************************************************************
899 * GlobalMasterHandle (KERNEL.28)
902 * Should return selector and handle of the information structure for
903 * the global heap. selector and handle are stored in the THHOOK as
904 * pGlobalHeap and hGlobalHeap.
905 * As Wine doesn't have this structure, we return both values as zero
906 * Applications should interpret this as "No Global Heap"
908 DWORD WINAPI GlobalMasterHandle16(void)
910 FIXME(": stub\n");
911 return 0;
914 /***********************************************************************
915 * GlobalHandleToSel (TOOLHELP.50)
917 * FIXME: This is in TOOLHELP but we keep a copy here for now.
919 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
921 if (!handle) return 0;
922 if (!VALID_HANDLE(handle)) {
923 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
924 return 0;
926 if (!(handle & 7))
928 WARN("Program attempted invalid selector conversion\n" );
929 return handle - 1;
931 return handle | 7;
935 /***********************************************************************
936 * GetFreeMemInfo (KERNEL.316)
938 DWORD WINAPI GetFreeMemInfo16(void)
940 SYSTEM_BASIC_INFORMATION info;
941 MEMORYSTATUS status;
943 NtQuerySystemInformation( SystemBasicInformation, &info, sizeof(info), NULL );
944 GlobalMemoryStatus( &status );
945 return MAKELONG( status.dwTotalVirtual / info.PageSize, status.dwAvailVirtual / info.PageSize );
948 /***********************************************************************
949 * A20Proc (KERNEL.165)
951 void WINAPI A20Proc16( WORD unused )
953 /* this is also a NOP in Windows */
956 /***********************************************************************
957 * LimitEMSPages (KERNEL.156)
959 DWORD WINAPI LimitEMSPages16( DWORD unused )
961 return 0;