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