msi: Include run mode in fixmes.
[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     SELECTOR_FreeBlock( sel );
186     memset( pArena, 0, sizeof(GLOBALARENA) );
187     return TRUE;
188 }
189
190 /***********************************************************************
191  *           GLOBAL_MoveBlock
192  */
193 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
194 {
195     WORD sel;
196     GLOBALARENA *pArena;
197
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;
204
205     pArena->base = ptr;
206     pArena->size = size;
207     SELECTOR_ReallocBlock( sel, ptr, size );
208     return TRUE;
209 }
210
211 /***********************************************************************
212  *           GLOBAL_Alloc
213  *
214  * Implementation of GlobalAlloc16()
215  */
216 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
217 {
218     void *ptr;
219     HGLOBAL16 handle;
220
221     TRACE("%d flags=%04x\n", size, flags );
222
223     /* If size is 0, create a discarded block */
224
225     if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
226
227     /* Fixup the size */
228
229     if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
230     size = (size + 0x1f) & ~0x1f;
231
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;
236
237       /* Allocate the selector(s) */
238
239     handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
240     if (!handle)
241     {
242         HeapFree( get_win16_heap(), 0, ptr );
243         return 0;
244     }
245
246     if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
247     return handle;
248 }
249
250 /***********************************************************************
251  *           GlobalAlloc     (KERNEL.15)
252  *           GlobalAlloc16   (KERNEL32.24)
253  *
254  * Allocate a global memory object.
255  *
256  * RETURNS
257  *      Handle: Success
258  *      NULL: Failure
259  */
260 HGLOBAL16 WINAPI GlobalAlloc16(
261                  UINT16 flags, /* [in] Object allocation attributes */
262                  DWORD size    /* [in] Number of bytes to allocate */
263 ) {
264     HANDLE16 owner = GetCurrentPDB16();
265
266     if (flags & GMEM_DDESHARE)
267     {
268         /* make it owned by the calling module */
269         STACK16FRAME *frame = CURRENT_STACK16;
270         owner = GetExePtr( frame->cs );
271     }
272     return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
273 }
274
275
276 /***********************************************************************
277  *           GlobalReAlloc     (KERNEL.16)
278  *
279  * Change the size or attributes of a global memory object.
280  *
281  * RETURNS
282  *      Handle: Success
283  *      NULL: Failure
284  */
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 */
289 ) {
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();
296
297     TRACE("%04x %d flags=%04x\n",
298                     handle, size, flags );
299     if (!handle) return 0;
300
301     if (!VALID_HANDLE(handle))
302     {
303         WARN("Invalid handle 0x%04x!\n", handle);
304         return 0;
305     }
306     pArena = GET_ARENA_PTR( handle );
307
308       /* Discard the block if requested */
309
310     if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
311     {
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;
320
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?
324          */
325         SELECTOR_ReallocBlock( sel, 0, 1 );
326         return handle;
327     }
328
329       /* Fixup the size */
330
331     if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
332     if (size == 0) size = 0x20;
333     else size = (size + 0x1f) & ~0x1f;
334
335       /* Change the flags */
336
337     if (flags & GMEM_MODIFY)
338     {
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;
343     }
344
345       /* Reallocate the linear memory */
346
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 */
351
352     if (pArena->flags & GA_DOSMEM)
353     {
354         if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size) 
355             newptr = ptr;
356         else if(pArena->pageLockCount > 0)
357             newptr = 0;
358         else
359         {
360             newptr = DOSMEM_AllocBlock( size, 0 );
361             if (newptr)
362             {
363                 memcpy( newptr, ptr, oldsize );
364                 DOSMEM_FreeBlock( ptr );
365             }
366         }
367     }
368     else
369     {
370         /*
371          * if more than one reader (e.g. some pointer has been 
372          * given out by GetVDMPointer32W16),
373          * only try to realloc in place
374          */
375
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 );
382
383     }
384
385     if (!newptr)
386     {
387         FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
388         if (pArena->pageLockCount <1)
389         {
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) );
396         }
397         return 0;
398     }
399     ptr = newptr;
400
401       /* Reallocate the selector(s) */
402
403     sel = SELECTOR_ReallocBlock( sel, ptr, size );
404     if (!sel)
405     {
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;
412     }
413     selcount = (size + 0xffff) / 0x10000;
414
415     if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
416     {
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;
423     }
424
425       /* Fill the new arena block
426          As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
427
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;
433
434     if (selcount > 1)  /* clear the next arena blocks */
435         memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
436
437     if ((oldsize < size) && (flags & GMEM_ZEROINIT))
438         memset( (char *)ptr + oldsize, 0, size - oldsize );
439     return pNewArena->handle;
440 }
441
442
443 /***********************************************************************
444  *           GlobalFree     (KERNEL.17)
445  *           GlobalFree16   (KERNEL32.31)
446  * RETURNS
447  *      NULL: Success
448  *      Handle: Failure
449  */
450 HGLOBAL16 WINAPI GlobalFree16(
451                  HGLOBAL16 handle /* [in] Handle of global memory object */
452 ) {
453     void *ptr;
454
455     if (!VALID_HANDLE(handle))
456     {
457         WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
458         return 0;
459     }
460     ptr = GET_ARENA_PTR(handle)->base;
461
462     TRACE("%04x\n", handle );
463     if (!GLOBAL_FreeBlock( handle )) return handle;  /* failed */
464     HeapFree( get_win16_heap(), 0, ptr );
465     return 0;
466 }
467
468
469 /**********************************************************************
470  *           K32WOWGlobalLock16         (KERNEL32.60)
471  */
472 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
473 {
474     WORD sel = GlobalHandleToSel16( handle );
475     TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
476
477     if (handle)
478     {
479         if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
480
481         if (!VALID_HANDLE(handle)) {
482             WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
483             sel = 0;
484         }
485         else if (!GET_ARENA_PTR(handle)->base)
486             sel = 0;
487         else
488             GET_ARENA_PTR(handle)->lockCount++;
489     }
490
491     return MAKESEGPTR( sel, 0 );
492
493 }
494
495
496 /***********************************************************************
497  *           GlobalLock   (KERNEL.18)
498  *
499  * This is the GlobalLock16() function used by 16-bit code.
500  */
501 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
502 {
503     SEGPTR ret = K32WOWGlobalLock16( handle );
504     CURRENT_STACK16->ecx = SELECTOROF(ret);  /* selector must be returned in CX as well */
505     return ret;
506 }
507
508
509 /***********************************************************************
510  *           GlobalLock16   (KERNEL32.25)
511  *
512  * This is the GlobalLock16() function used by 32-bit code.
513  *
514  * RETURNS
515  *      Pointer to first byte of memory block
516  *      NULL: Failure
517  */
518 LPVOID WINAPI GlobalLock16(
519               HGLOBAL16 handle /* [in] Handle of global memory object */
520 ) {
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;
526 }
527
528
529 /***********************************************************************
530  *           GlobalUnlock     (KERNEL.19)
531  *           GlobalUnlock16   (KERNEL32.26)
532  * NOTES
533  *      Should the return values be cast to booleans?
534  *
535  * RETURNS
536  *      TRUE: Object is still locked
537  *      FALSE: Object is unlocked
538  */
539 BOOL16 WINAPI GlobalUnlock16(
540               HGLOBAL16 handle /* [in] Handle of global memory object */
541 ) {
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;
546     }
547     TRACE("%04x\n", handle );
548     if (pArena->lockCount) pArena->lockCount--;
549     return pArena->lockCount;
550 }
551
552 /***********************************************************************
553  *     GlobalChangeLockCount               (KERNEL.365)
554  *
555  * This is declared as a register function as it has to preserve
556  * *all* registers, even AX/DX !
557  *
558  */
559 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
560                                      CONTEXT86 *context )
561 {
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 );
568 }
569
570 /***********************************************************************
571  *           GlobalSize     (KERNEL.20)
572  *           GlobalSize16   (KERNEL32.32)
573  * 
574  * Get the current size of a global memory object.
575  *
576  * RETURNS
577  *      Size in bytes of object
578  *      0: Failure
579  */
580 DWORD WINAPI GlobalSize16(
581              HGLOBAL16 handle /* [in] Handle of global memory object */
582 ) {
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;
588 }
589
590
591 /***********************************************************************
592  *           GlobalHandle   (KERNEL.21)
593  *
594  * Get the handle associated with a pointer to the global memory block.
595  *
596  * NOTES
597  *      Why is GlobalHandleToSel used here with the sel as input?
598  *
599  * RETURNS
600  *      Handle: Success
601  *      NULL: Failure
602  */
603 DWORD WINAPI GlobalHandle16(
604              WORD sel /* [in] Address of global memory block */
605 ) {
606     TRACE("%04x\n", sel );
607     if (!VALID_HANDLE(sel)) {
608         WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
609         return 0;
610     }
611     return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
612 }
613
614 /***********************************************************************
615  *           GlobalHandleNoRIP   (KERNEL.159)
616  */
617 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
618 {
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) );
623     }
624     return 0;
625 }
626
627
628 /***********************************************************************
629  *           GlobalFlags     (KERNEL.22)
630  *
631  * Get information about a global memory object.
632  *
633  * NOTES
634  *      Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
635  *      handle?
636  *
637  * RETURNS
638  *      Value specifying flags and lock count
639  *      GMEM_INVALID_HANDLE: Invalid handle
640  */
641 UINT16 WINAPI GlobalFlags16(
642               HGLOBAL16 handle /* [in] Handle of global memory object */
643 ) {
644     GLOBALARENA *pArena;
645
646     TRACE("%04x\n", handle );
647     if (!VALID_HANDLE(handle)) {
648         WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
649         return 0;
650     }
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);
655 }
656
657
658 /***********************************************************************
659  *           LockSegment   (KERNEL.23)
660  */
661 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
662 {
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;
668     }
669     GET_ARENA_PTR(handle)->lockCount++;
670     return handle;
671 }
672
673
674 /***********************************************************************
675  *           UnlockSegment   (KERNEL.24)
676  */
677 void WINAPI UnlockSegment16( HGLOBAL16 handle )
678 {
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;
684     }
685     GET_ARENA_PTR(handle)->lockCount--;
686     /* FIXME: this ought to return the lock count in CX (go figure...) */
687 }
688
689
690 /***********************************************************************
691  *           GlobalCompact   (KERNEL.25)
692  */
693 DWORD WINAPI GlobalCompact16( DWORD desired )
694 {
695     return GLOBAL_MAX_ALLOC_SIZE;
696 }
697
698
699 /***********************************************************************
700  *           GlobalFreeAll   (KERNEL.26)
701  */
702 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
703 {
704     int i;
705     GLOBALARENA *pArena;
706
707     pArena = pGlobalArena;
708     for (i = 0; i < globalArenaSize; i++, pArena++)
709     {
710         if ((pArena->size != 0) && (pArena->hOwner == owner))
711             GlobalFree16( pArena->handle );
712     }
713 }
714
715
716 /***********************************************************************
717  *           GlobalWire     (KERNEL.111)
718  *           GlobalWire16   (KERNEL32.29)
719  */
720 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
721 {
722     return WIN16_GlobalLock16( handle );
723 }
724
725
726 /***********************************************************************
727  *           GlobalUnWire     (KERNEL.112)
728  *           GlobalUnWire16   (KERNEL32.30)
729  */
730 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
731 {
732     return !GlobalUnlock16( handle );
733 }
734
735
736 /***********************************************************************
737  *           SetSwapAreaSize   (KERNEL.106)
738  */
739 LONG WINAPI SetSwapAreaSize16( WORD size )
740 {
741     FIXME("(%d) - stub!\n", size );
742     return MAKELONG( size, 0xffff );
743 }
744
745
746 /***********************************************************************
747  *           GlobalLRUOldest   (KERNEL.163)
748  */
749 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
750 {
751     TRACE("%04x\n", handle );
752     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
753     return handle;
754 }
755
756
757 /***********************************************************************
758  *           GlobalLRUNewest   (KERNEL.164)
759  */
760 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
761 {
762     TRACE("%04x\n", handle );
763     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
764     return handle;
765 }
766
767
768 /***********************************************************************
769  *           GetFreeSpace   (KERNEL.169)
770  */
771 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
772 {
773     MEMORYSTATUS ms;
774     GlobalMemoryStatus( &ms );
775     return ms.dwAvailVirtual;
776 }
777
778 /***********************************************************************
779  *           GlobalDOSAlloc   (KERNEL.184)
780  *
781  * Allocate memory in the first MB.
782  *
783  * RETURNS
784  *      Address (HW=Paragraph segment; LW=Selector)
785  */
786 DWORD WINAPI GlobalDOSAlloc16(
787              DWORD size /* [in] Number of bytes to be allocated */
788 ) {
789    UINT16    uParagraph;
790    LPVOID    lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
791
792    if( lpBlock )
793    {
794        HMODULE16 hModule = GetModuleHandle16("KERNEL");
795        WORD      wSelector;
796        GLOBALARENA *pArena;
797
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);
802    }
803    return 0;
804 }
805
806
807 /***********************************************************************
808  *           GlobalDOSFree      (KERNEL.185)
809  *
810  * Free memory allocated with GlobalDOSAlloc
811  *
812  * RETURNS
813  *      NULL: Success
814  *      sel: Failure
815  */
816 WORD WINAPI GlobalDOSFree16(
817             WORD sel /* [in] Selector */
818 ) {
819    DWORD   block = GetSelectorBase(sel);
820
821    if( block && block < 0x100000 )
822    {
823        LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
824        if( DOSMEM_FreeBlock( lpBlock ) )
825            GLOBAL_FreeBlock( sel );
826        sel = 0;
827    }
828    return sel;
829 }
830
831
832 /***********************************************************************
833  *           GlobalPageLock   (KERNEL.191)
834  *           GlobalSmartPageLock(KERNEL.230)
835  */
836 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
837 {
838     TRACE("%04x\n", handle );
839     if (!VALID_HANDLE(handle)) {
840         WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
841         return 0;
842     }
843     return ++(GET_ARENA_PTR(handle)->pageLockCount);
844 }
845
846
847 /***********************************************************************
848  *           GlobalPageUnlock   (KERNEL.192)
849  *           GlobalSmartPageUnlock(KERNEL.231)
850  */
851 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
852 {
853     TRACE("%04x\n", handle );
854     if (!VALID_HANDLE(handle)) {
855         WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
856         return 0;
857     }
858     return --(GET_ARENA_PTR(handle)->pageLockCount);
859 }
860
861
862 /***********************************************************************
863  *           GlobalFix     (KERNEL.197)
864  *           GlobalFix16   (KERNEL32.27)
865  */
866 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
867 {
868     TRACE("%04x\n", handle );
869     if (!VALID_HANDLE(handle)) {
870         WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
871         return 0;
872     }
873     GET_ARENA_PTR(handle)->lockCount++;
874
875     return GlobalHandleToSel16(handle);
876 }
877
878
879 /***********************************************************************
880  *           GlobalUnfix     (KERNEL.198)
881  *           GlobalUnfix16   (KERNEL32.28)
882  */
883 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
884 {
885     TRACE("%04x\n", handle );
886     if (!VALID_HANDLE(handle)) {
887         WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
888         return;
889     }
890     GET_ARENA_PTR(handle)->lockCount--;
891 }
892
893
894 /***********************************************************************
895  *           FarSetOwner   (KERNEL.403)
896  */
897 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
898 {
899     if (!VALID_HANDLE(handle)) {
900         WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
901         return;
902     }
903     GET_ARENA_PTR(handle)->hOwner = hOwner;
904 }
905
906
907 /***********************************************************************
908  *           FarGetOwner   (KERNEL.404)
909  */
910 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
911 {
912     if (!VALID_HANDLE(handle)) {
913         WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
914         return 0;
915     }
916     return GET_ARENA_PTR(handle)->hOwner;
917 }
918
919
920 /************************************************************************
921  *              GlobalMasterHandle (KERNEL.28)
922  *
923  *
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"
929  */
930 DWORD WINAPI GlobalMasterHandle16(void)
931 {
932     FIXME(": stub\n");
933     return 0;
934 }
935
936 /***********************************************************************
937  *           GlobalHandleToSel   (TOOLHELP.50)
938  *
939  * FIXME: This is in TOOLHELP but we keep a copy here for now.
940  */
941 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
942 {
943     if (!handle) return 0;
944     if (!VALID_HANDLE(handle)) {
945         WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
946         return 0;
947     }
948     if (!(handle & 7))
949     {
950         WARN("Program attempted invalid selector conversion\n" );
951         return handle - 1;
952     }
953     return handle | 7;
954 }
955
956
957 /***********************************************************************
958  *           GetFreeMemInfo   (KERNEL.316)
959  */
960 DWORD WINAPI GetFreeMemInfo16(void)
961 {
962     MEMORYSTATUS status;
963     GlobalMemoryStatus( &status );
964     return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
965 }
966
967 /***********************************************************************
968  *           A20Proc   (KERNEL.165)
969  */
970 void WINAPI A20Proc16( WORD unused )
971 {
972     /* this is also a NOP in Windows */
973 }
974
975 /***********************************************************************
976  *           LimitEMSPages   (KERNEL.156)
977  */
978 DWORD WINAPI LimitEMSPages16( DWORD unused )
979 {
980     return 0;
981 }