wined3d: Reorganize IWineD3DImpl_CheckDeviceFormat() a bit.
[wine] / dlls / krnl386.exe16 / global.c
1 /*
2  * Global heap functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20 /* 0xffff sometimes seems to mean: CURRENT_DS */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
39
40 #include "wine/winbase16.h"
41 #include "winternl.h"
42 #include "kernel16_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(global);
46
47   /* Global arena block */
48 typedef struct
49 {
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;
59
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
66
67 /* Arena array (FIXME) */
68 static GLOBALARENA *pGlobalArena;
69 static int globalArenaSize;
70
71 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000  /* Largest allocation is 16M - 64K */
72 #define GLOBAL_MAX_COUNT      8192        /* Max number of allocated blocks */
73
74 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
75 #define GET_ARENA_PTR(handle)  (pGlobalArena + ((handle) >> __AHSHIFT))
76
77 static HANDLE get_win16_heap(void)
78 {
79     static HANDLE win16_heap;
80
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;
85 }
86
87 /***********************************************************************
88  *           GLOBAL_GetArena
89  *
90  * Return the arena for a given selector, growing the arena array if needed.
91  */
92 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
93 {
94     if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
95     {
96         int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
97
98         if (!pGlobalArena)
99         {
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;
105         }
106         if (newsize > GLOBAL_MAX_COUNT) return 0;
107         globalArenaSize = newsize;
108     }
109     return pGlobalArena + (sel >> __AHSHIFT);
110 }
111
112 void debug_handles(void)
113 {
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);
120         }
121     }
122     if (printed)
123         DPRINTF("\n");
124 }
125
126
127 /***********************************************************************
128  *           GLOBAL_CreateBlock
129  *
130  * Create a global heap block for a fixed range of linear memory.
131  */
132 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
133                               HGLOBAL16 hOwner, unsigned char selflags )
134 {
135     WORD sel, selcount;
136     GLOBALARENA *pArena;
137
138       /* Allocate the selector(s) */
139
140     sel = SELECTOR_AllocBlock( ptr, size, selflags );
141     if (!sel) return 0;
142     selcount = (size + 0xffff) / 0x10000;
143
144     if (!(pArena = GLOBAL_GetArena( sel, selcount )))
145     {
146         SELECTOR_FreeBlock( sel );
147         return 0;
148     }
149
150       /* Fill the arena block */
151
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) );
165
166     return pArena->handle;
167 }
168
169
170 /***********************************************************************
171  *           GLOBAL_FreeBlock
172  *
173  * Free a block allocated by GLOBAL_CreateBlock, without touching
174  * the associated linear memory range.
175  */
176 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
177 {
178     WORD sel;
179     GLOBALARENA *pArena;
180
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)
186     {
187         WARN( "already free %x\n", handle );
188         return FALSE;
189     }
190     SELECTOR_FreeBlock( sel );
191     memset( pArena, 0, sizeof(GLOBALARENA) );
192     return TRUE;
193 }
194
195 /***********************************************************************
196  *           GLOBAL_MoveBlock
197  */
198 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
199 {
200     WORD sel;
201     GLOBALARENA *pArena;
202
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;
209
210     pArena->base = ptr;
211     pArena->size = size;
212     SELECTOR_ReallocBlock( sel, ptr, size );
213     return TRUE;
214 }
215
216 /***********************************************************************
217  *           GLOBAL_Alloc
218  *
219  * Implementation of GlobalAlloc16()
220  */
221 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
222 {
223     void *ptr;
224     HGLOBAL16 handle;
225
226     TRACE("%d flags=%04x\n", size, flags );
227
228     /* If size is 0, create a discarded block */
229
230     if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
231
232     /* Fixup the size */
233
234     if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
235     size = (size + 0x1f) & ~0x1f;
236
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;
241
242       /* Allocate the selector(s) */
243
244     handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
245     if (!handle)
246     {
247         HeapFree( get_win16_heap(), 0, ptr );
248         return 0;
249     }
250
251     if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
252     return handle;
253 }
254
255 /***********************************************************************
256  *           GlobalAlloc     (KERNEL.15)
257  *           GlobalAlloc16   (KERNEL32.24)
258  *
259  * Allocate a global memory object.
260  *
261  * RETURNS
262  *      Handle: Success
263  *      NULL: Failure
264  */
265 HGLOBAL16 WINAPI GlobalAlloc16(
266                  UINT16 flags, /* [in] Object allocation attributes */
267                  DWORD size    /* [in] Number of bytes to allocate */
268 ) {
269     HANDLE16 owner = GetCurrentPDB16();
270
271     if (flags & GMEM_DDESHARE)
272     {
273         /* make it owned by the calling module */
274         STACK16FRAME *frame = CURRENT_STACK16;
275         owner = GetExePtr( frame->cs );
276     }
277     return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
278 }
279
280
281 /***********************************************************************
282  *           GlobalReAlloc     (KERNEL.16)
283  *
284  * Change the size or attributes of a global memory object.
285  *
286  * RETURNS
287  *      Handle: Success
288  *      NULL: Failure
289  */
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 */
294 ) {
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();
301
302     TRACE("%04x %d flags=%04x\n",
303                     handle, size, flags );
304     if (!handle) return 0;
305
306     if (!VALID_HANDLE(handle))
307     {
308         WARN("Invalid handle 0x%04x!\n", handle);
309         return 0;
310     }
311     pArena = GET_ARENA_PTR( handle );
312
313       /* Discard the block if requested */
314
315     if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
316     {
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;
325
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?
329          */
330         SELECTOR_ReallocBlock( sel, 0, 1 );
331         return handle;
332     }
333
334       /* Fixup the size */
335
336     if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
337     if (size == 0) size = 0x20;
338     else size = (size + 0x1f) & ~0x1f;
339
340       /* Change the flags */
341
342     if (flags & GMEM_MODIFY)
343     {
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;
348     }
349
350       /* Reallocate the linear memory */
351
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 */
356
357     if (pArena->flags & GA_DOSMEM)
358     {
359         if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size) 
360             newptr = ptr;
361         else if(pArena->pageLockCount > 0)
362             newptr = 0;
363         else
364         {
365             newptr = DOSMEM_AllocBlock( size, 0 );
366             if (newptr)
367             {
368                 memcpy( newptr, ptr, oldsize );
369                 DOSMEM_FreeBlock( ptr );
370             }
371         }
372     }
373     else
374     {
375         /*
376          * if more than one reader (e.g. some pointer has been 
377          * given out by GetVDMPointer32W16),
378          * only try to realloc in place
379          */
380
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 );
387
388     }
389
390     if (!newptr)
391     {
392         FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
393         if (pArena->pageLockCount <1)
394         {
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) );
401         }
402         return 0;
403     }
404     ptr = newptr;
405
406       /* Reallocate the selector(s) */
407
408     sel = SELECTOR_ReallocBlock( sel, ptr, size );
409     if (!sel)
410     {
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;
417     }
418     selcount = (size + 0xffff) / 0x10000;
419
420     if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
421     {
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;
428     }
429
430       /* Fill the new arena block
431          As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
432
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;
438
439     if (selcount > 1)  /* clear the next arena blocks */
440         memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
441
442     if ((oldsize < size) && (flags & GMEM_ZEROINIT))
443         memset( (char *)ptr + oldsize, 0, size - oldsize );
444     return pNewArena->handle;
445 }
446
447
448 /***********************************************************************
449  *           GlobalFree     (KERNEL.17)
450  *           GlobalFree16   (KERNEL32.31)
451  * RETURNS
452  *      NULL: Success
453  *      Handle: Failure
454  */
455 HGLOBAL16 WINAPI GlobalFree16(
456                  HGLOBAL16 handle /* [in] Handle of global memory object */
457 ) {
458     void *ptr;
459
460     if (!VALID_HANDLE(handle))
461     {
462         WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
463         return 0;
464     }
465     ptr = GET_ARENA_PTR(handle)->base;
466
467     TRACE("%04x\n", handle );
468     if (!GLOBAL_FreeBlock( handle )) return handle;  /* failed */
469     HeapFree( get_win16_heap(), 0, ptr );
470     return 0;
471 }
472
473
474 /**********************************************************************
475  *           K32WOWGlobalLock16         (KERNEL32.60)
476  */
477 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
478 {
479     WORD sel = GlobalHandleToSel16( handle );
480     TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
481
482     if (handle)
483     {
484         if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
485
486         if (!VALID_HANDLE(handle)) {
487             WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
488             sel = 0;
489         }
490         else if (!GET_ARENA_PTR(handle)->base)
491             sel = 0;
492         else
493             GET_ARENA_PTR(handle)->lockCount++;
494     }
495
496     return MAKESEGPTR( sel, 0 );
497
498 }
499
500
501 /***********************************************************************
502  *           GlobalLock   (KERNEL.18)
503  *
504  * This is the GlobalLock16() function used by 16-bit code.
505  */
506 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
507 {
508     SEGPTR ret = K32WOWGlobalLock16( handle );
509     CURRENT_STACK16->ecx = SELECTOROF(ret);  /* selector must be returned in CX as well */
510     return ret;
511 }
512
513
514 /***********************************************************************
515  *           GlobalLock16   (KERNEL32.25)
516  *
517  * This is the GlobalLock16() function used by 32-bit code.
518  *
519  * RETURNS
520  *      Pointer to first byte of memory block
521  *      NULL: Failure
522  */
523 LPVOID WINAPI GlobalLock16(
524               HGLOBAL16 handle /* [in] Handle of global memory object */
525 ) {
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;
531 }
532
533
534 /***********************************************************************
535  *           GlobalUnlock     (KERNEL.19)
536  *           GlobalUnlock16   (KERNEL32.26)
537  * NOTES
538  *      Should the return values be cast to booleans?
539  *
540  * RETURNS
541  *      TRUE: Object is still locked
542  *      FALSE: Object is unlocked
543  */
544 BOOL16 WINAPI GlobalUnlock16(
545               HGLOBAL16 handle /* [in] Handle of global memory object */
546 ) {
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;
551     }
552     TRACE("%04x\n", handle );
553     if (pArena->lockCount) pArena->lockCount--;
554     return pArena->lockCount;
555 }
556
557 /***********************************************************************
558  *     GlobalChangeLockCount               (KERNEL.365)
559  *
560  * This is declared as a register function as it has to preserve
561  * *all* registers, even AX/DX !
562  *
563  */
564 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
565                                      CONTEXT86 *context )
566 {
567     if ( delta == 1 )
568         GlobalLock16( handle );
569     else if ( delta == -1 )
570         GlobalUnlock16( handle );
571     else
572         ERR("(%04X, %d): strange delta value\n", handle, delta );
573 }
574
575 /***********************************************************************
576  *           GlobalSize     (KERNEL.20)
577  *           GlobalSize16   (KERNEL32.32)
578  * 
579  * Get the current size of a global memory object.
580  *
581  * RETURNS
582  *      Size in bytes of object
583  *      0: Failure
584  */
585 DWORD WINAPI GlobalSize16(
586              HGLOBAL16 handle /* [in] Handle of global memory object */
587 ) {
588     TRACE("%04x\n", handle );
589     if (!handle) return 0;
590     if (!VALID_HANDLE(handle))
591         return 0;
592     return GET_ARENA_PTR(handle)->size;
593 }
594
595
596 /***********************************************************************
597  *           GlobalHandle   (KERNEL.21)
598  *
599  * Get the handle associated with a pointer to the global memory block.
600  *
601  * NOTES
602  *      Why is GlobalHandleToSel used here with the sel as input?
603  *
604  * RETURNS
605  *      Handle: Success
606  *      NULL: Failure
607  */
608 DWORD WINAPI GlobalHandle16(
609              WORD sel /* [in] Address of global memory block */
610 ) {
611     TRACE("%04x\n", sel );
612     if (!VALID_HANDLE(sel)) {
613         WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
614         return 0;
615     }
616     return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
617 }
618
619 /***********************************************************************
620  *           GlobalHandleNoRIP   (KERNEL.159)
621  */
622 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
623 {
624     int i;
625     for (i = globalArenaSize-1 ; i>=0 ; i--) {
626         if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
627                 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
628     }
629     return 0;
630 }
631
632
633 /***********************************************************************
634  *           GlobalFlags     (KERNEL.22)
635  *
636  * Get information about a global memory object.
637  *
638  * NOTES
639  *      Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
640  *      handle?
641  *
642  * RETURNS
643  *      Value specifying flags and lock count
644  *      GMEM_INVALID_HANDLE: Invalid handle
645  */
646 UINT16 WINAPI GlobalFlags16(
647               HGLOBAL16 handle /* [in] Handle of global memory object */
648 ) {
649     GLOBALARENA *pArena;
650
651     TRACE("%04x\n", handle );
652     if (!VALID_HANDLE(handle)) {
653         WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
654         return 0;
655     }
656     pArena = GET_ARENA_PTR(handle);
657     return pArena->lockCount |
658            ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
659            ((pArena->base == 0) ? GMEM_DISCARDED : 0);
660 }
661
662
663 /***********************************************************************
664  *           LockSegment   (KERNEL.23)
665  */
666 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
667 {
668     TRACE("%04x\n", handle );
669     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
670     if (!VALID_HANDLE(handle)) {
671         WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
672         return 0;
673     }
674     GET_ARENA_PTR(handle)->lockCount++;
675     return handle;
676 }
677
678
679 /***********************************************************************
680  *           UnlockSegment   (KERNEL.24)
681  */
682 void WINAPI UnlockSegment16( HGLOBAL16 handle )
683 {
684     TRACE("%04x\n", handle );
685     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
686     if (!VALID_HANDLE(handle)) {
687         WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
688         return;
689     }
690     GET_ARENA_PTR(handle)->lockCount--;
691     /* FIXME: this ought to return the lock count in CX (go figure...) */
692 }
693
694
695 /***********************************************************************
696  *           GlobalCompact   (KERNEL.25)
697  */
698 DWORD WINAPI GlobalCompact16( DWORD desired )
699 {
700     return GLOBAL_MAX_ALLOC_SIZE;
701 }
702
703
704 /***********************************************************************
705  *           GlobalFreeAll   (KERNEL.26)
706  */
707 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
708 {
709     int i;
710     GLOBALARENA *pArena;
711
712     pArena = pGlobalArena;
713     for (i = 0; i < globalArenaSize; i++, pArena++)
714     {
715         if ((pArena->size != 0) && (pArena->hOwner == owner))
716             GlobalFree16( pArena->handle );
717     }
718 }
719
720
721 /***********************************************************************
722  *           GlobalWire     (KERNEL.111)
723  *           GlobalWire16   (KERNEL32.29)
724  */
725 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
726 {
727     return WIN16_GlobalLock16( handle );
728 }
729
730
731 /***********************************************************************
732  *           GlobalUnWire     (KERNEL.112)
733  *           GlobalUnWire16   (KERNEL32.30)
734  */
735 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
736 {
737     return !GlobalUnlock16( handle );
738 }
739
740
741 /***********************************************************************
742  *           SetSwapAreaSize   (KERNEL.106)
743  */
744 LONG WINAPI SetSwapAreaSize16( WORD size )
745 {
746     FIXME("(%d) - stub!\n", size );
747     return MAKELONG( size, 0xffff );
748 }
749
750
751 /***********************************************************************
752  *           GlobalLRUOldest   (KERNEL.163)
753  */
754 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
755 {
756     TRACE("%04x\n", handle );
757     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
758     return handle;
759 }
760
761
762 /***********************************************************************
763  *           GlobalLRUNewest   (KERNEL.164)
764  */
765 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
766 {
767     TRACE("%04x\n", handle );
768     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
769     return handle;
770 }
771
772
773 /***********************************************************************
774  *           GetFreeSpace   (KERNEL.169)
775  */
776 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
777 {
778     MEMORYSTATUS ms;
779     GlobalMemoryStatus( &ms );
780     return ms.dwAvailVirtual;
781 }
782
783 /***********************************************************************
784  *           GlobalDOSAlloc   (KERNEL.184)
785  *
786  * Allocate memory in the first MB.
787  *
788  * RETURNS
789  *      Address (HW=Paragraph segment; LW=Selector)
790  */
791 DWORD WINAPI GlobalDOSAlloc16(
792              DWORD size /* [in] Number of bytes to be allocated */
793 ) {
794    UINT16    uParagraph;
795    LPVOID    lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
796
797    if( lpBlock )
798    {
799        HMODULE16 hModule = GetModuleHandle16("KERNEL");
800        WORD      wSelector;
801        GLOBALARENA *pArena;
802
803        wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
804        pArena = GET_ARENA_PTR(wSelector);
805        pArena->flags |= GA_DOSMEM;
806        return MAKELONG(wSelector,uParagraph);
807    }
808    return 0;
809 }
810
811
812 /***********************************************************************
813  *           GlobalDOSFree      (KERNEL.185)
814  *
815  * Free memory allocated with GlobalDOSAlloc
816  *
817  * RETURNS
818  *      NULL: Success
819  *      sel: Failure
820  */
821 WORD WINAPI GlobalDOSFree16(
822             WORD sel /* [in] Selector */
823 ) {
824    DWORD   block = GetSelectorBase(sel);
825
826    if( block && block < 0x100000 )
827    {
828        LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
829        if( DOSMEM_FreeBlock( lpBlock ) )
830            GLOBAL_FreeBlock( sel );
831        sel = 0;
832    }
833    return sel;
834 }
835
836
837 /***********************************************************************
838  *           GlobalPageLock   (KERNEL.191)
839  *           GlobalSmartPageLock(KERNEL.230)
840  */
841 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
842 {
843     TRACE("%04x\n", handle );
844     if (!VALID_HANDLE(handle)) {
845         WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
846         return 0;
847     }
848     return ++(GET_ARENA_PTR(handle)->pageLockCount);
849 }
850
851
852 /***********************************************************************
853  *           GlobalPageUnlock   (KERNEL.192)
854  *           GlobalSmartPageUnlock(KERNEL.231)
855  */
856 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
857 {
858     TRACE("%04x\n", handle );
859     if (!VALID_HANDLE(handle)) {
860         WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
861         return 0;
862     }
863     return --(GET_ARENA_PTR(handle)->pageLockCount);
864 }
865
866
867 /***********************************************************************
868  *           GlobalFix     (KERNEL.197)
869  *           GlobalFix16   (KERNEL32.27)
870  */
871 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
872 {
873     TRACE("%04x\n", handle );
874     if (!VALID_HANDLE(handle)) {
875         WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
876         return 0;
877     }
878     GET_ARENA_PTR(handle)->lockCount++;
879
880     return GlobalHandleToSel16(handle);
881 }
882
883
884 /***********************************************************************
885  *           GlobalUnfix     (KERNEL.198)
886  *           GlobalUnfix16   (KERNEL32.28)
887  */
888 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
889 {
890     TRACE("%04x\n", handle );
891     if (!VALID_HANDLE(handle)) {
892         WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
893         return;
894     }
895     GET_ARENA_PTR(handle)->lockCount--;
896 }
897
898
899 /***********************************************************************
900  *           FarSetOwner   (KERNEL.403)
901  */
902 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
903 {
904     if (!VALID_HANDLE(handle)) {
905         WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
906         return;
907     }
908     GET_ARENA_PTR(handle)->hOwner = hOwner;
909 }
910
911
912 /***********************************************************************
913  *           FarGetOwner   (KERNEL.404)
914  */
915 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
916 {
917     if (!VALID_HANDLE(handle)) {
918         WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
919         return 0;
920     }
921     return GET_ARENA_PTR(handle)->hOwner;
922 }
923
924
925 /************************************************************************
926  *              GlobalMasterHandle (KERNEL.28)
927  *
928  *
929  * Should return selector and handle of the information structure for
930  * the global heap. selector and handle are stored in the THHOOK as
931  * pGlobalHeap and hGlobalHeap.
932  * As Wine doesn't have this structure, we return both values as zero
933  * Applications should interpret this as "No Global Heap"
934  */
935 DWORD WINAPI GlobalMasterHandle16(void)
936 {
937     FIXME(": stub\n");
938     return 0;
939 }
940
941 /***********************************************************************
942  *           GlobalHandleToSel   (TOOLHELP.50)
943  *
944  * FIXME: This is in TOOLHELP but we keep a copy here for now.
945  */
946 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
947 {
948     if (!handle) return 0;
949     if (!VALID_HANDLE(handle)) {
950         WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
951         return 0;
952     }
953     if (!(handle & 7))
954     {
955         WARN("Program attempted invalid selector conversion\n" );
956         return handle - 1;
957     }
958     return handle | 7;
959 }
960
961
962 /***********************************************************************
963  *           GetFreeMemInfo   (KERNEL.316)
964  */
965 DWORD WINAPI GetFreeMemInfo16(void)
966 {
967     MEMORYSTATUS status;
968     GlobalMemoryStatus( &status );
969     return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
970 }
971
972 /***********************************************************************
973  *           A20Proc   (KERNEL.165)
974  */
975 void WINAPI A20Proc16( WORD unused )
976 {
977     /* this is also a NOP in Windows */
978 }
979
980 /***********************************************************************
981  *           LimitEMSPages   (KERNEL.156)
982  */
983 DWORD WINAPI LimitEMSPages16( DWORD unused )
984 {
985     return 0;
986 }