oleaut32: Add a test for loading/saving an empty picture.
[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, NULL );
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, CONTEXT *context )
565 {
566     if ( delta == 1 )
567         GlobalLock16( handle );
568     else if ( delta == -1 )
569         GlobalUnlock16( handle );
570     else
571         ERR("(%04X, %d): strange delta value\n", handle, delta );
572 }
573
574 /***********************************************************************
575  *           GlobalSize     (KERNEL.20)
576  *           GlobalSize16   (KERNEL32.32)
577  * 
578  * Get the current size of a global memory object.
579  *
580  * RETURNS
581  *      Size in bytes of object
582  *      0: Failure
583  */
584 DWORD WINAPI GlobalSize16(
585              HGLOBAL16 handle /* [in] Handle of global memory object */
586 ) {
587     TRACE("%04x\n", handle );
588     if (!handle) return 0;
589     if (!VALID_HANDLE(handle))
590         return 0;
591     return GET_ARENA_PTR(handle)->size;
592 }
593
594
595 /***********************************************************************
596  *           GlobalHandle   (KERNEL.21)
597  *
598  * Get the handle associated with a pointer to the global memory block.
599  *
600  * NOTES
601  *      Why is GlobalHandleToSel used here with the sel as input?
602  *
603  * RETURNS
604  *      Handle: Success
605  *      NULL: Failure
606  */
607 DWORD WINAPI GlobalHandle16(
608              WORD sel /* [in] Address of global memory block */
609 ) {
610     TRACE("%04x\n", sel );
611     if (!VALID_HANDLE(sel)) {
612         WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
613         return 0;
614     }
615     return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
616 }
617
618 /***********************************************************************
619  *           GlobalHandleNoRIP   (KERNEL.159)
620  */
621 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
622 {
623     int i;
624     for (i = globalArenaSize-1 ; i>=0 ; i--) {
625         if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
626                 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
627     }
628     return 0;
629 }
630
631
632 /***********************************************************************
633  *           GlobalFlags     (KERNEL.22)
634  *
635  * Get information about a global memory object.
636  *
637  * NOTES
638  *      Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
639  *      handle?
640  *
641  * RETURNS
642  *      Value specifying flags and lock count
643  *      GMEM_INVALID_HANDLE: Invalid handle
644  */
645 UINT16 WINAPI GlobalFlags16(
646               HGLOBAL16 handle /* [in] Handle of global memory object */
647 ) {
648     GLOBALARENA *pArena;
649
650     TRACE("%04x\n", handle );
651     if (!VALID_HANDLE(handle)) {
652         WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
653         return 0;
654     }
655     pArena = GET_ARENA_PTR(handle);
656     return pArena->lockCount |
657            ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
658            ((pArena->base == 0) ? GMEM_DISCARDED : 0);
659 }
660
661
662 /***********************************************************************
663  *           LockSegment   (KERNEL.23)
664  */
665 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
666 {
667     TRACE("%04x\n", handle );
668     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
669     if (!VALID_HANDLE(handle)) {
670         WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
671         return 0;
672     }
673     GET_ARENA_PTR(handle)->lockCount++;
674     return handle;
675 }
676
677
678 /***********************************************************************
679  *           UnlockSegment   (KERNEL.24)
680  */
681 void WINAPI UnlockSegment16( HGLOBAL16 handle )
682 {
683     TRACE("%04x\n", handle );
684     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
685     if (!VALID_HANDLE(handle)) {
686         WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
687         return;
688     }
689     GET_ARENA_PTR(handle)->lockCount--;
690     /* FIXME: this ought to return the lock count in CX (go figure...) */
691 }
692
693
694 /***********************************************************************
695  *           GlobalCompact   (KERNEL.25)
696  */
697 DWORD WINAPI GlobalCompact16( DWORD desired )
698 {
699     return GLOBAL_MAX_ALLOC_SIZE;
700 }
701
702
703 /***********************************************************************
704  *           GlobalFreeAll   (KERNEL.26)
705  */
706 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
707 {
708     int i;
709     GLOBALARENA *pArena;
710
711     pArena = pGlobalArena;
712     for (i = 0; i < globalArenaSize; i++, pArena++)
713     {
714         if ((pArena->size != 0) && (pArena->hOwner == owner))
715             GlobalFree16( pArena->handle );
716     }
717 }
718
719
720 /***********************************************************************
721  *           GlobalWire     (KERNEL.111)
722  *           GlobalWire16   (KERNEL32.29)
723  */
724 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
725 {
726     return WIN16_GlobalLock16( handle );
727 }
728
729
730 /***********************************************************************
731  *           GlobalUnWire     (KERNEL.112)
732  *           GlobalUnWire16   (KERNEL32.30)
733  */
734 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
735 {
736     return !GlobalUnlock16( handle );
737 }
738
739
740 /***********************************************************************
741  *           SetSwapAreaSize   (KERNEL.106)
742  */
743 LONG WINAPI SetSwapAreaSize16( WORD size )
744 {
745     FIXME("(%d) - stub!\n", size );
746     return MAKELONG( size, 0xffff );
747 }
748
749
750 /***********************************************************************
751  *           GlobalLRUOldest   (KERNEL.163)
752  */
753 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
754 {
755     TRACE("%04x\n", handle );
756     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
757     return handle;
758 }
759
760
761 /***********************************************************************
762  *           GlobalLRUNewest   (KERNEL.164)
763  */
764 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
765 {
766     TRACE("%04x\n", handle );
767     if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
768     return handle;
769 }
770
771
772 /***********************************************************************
773  *           GetFreeSpace   (KERNEL.169)
774  */
775 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
776 {
777     MEMORYSTATUS ms;
778     GlobalMemoryStatus( &ms );
779     return ms.dwAvailVirtual;
780 }
781
782 /***********************************************************************
783  *           GlobalDOSAlloc   (KERNEL.184)
784  *
785  * Allocate memory in the first MB.
786  *
787  * RETURNS
788  *      Address (HW=Paragraph segment; LW=Selector)
789  */
790 DWORD WINAPI GlobalDOSAlloc16(
791              DWORD size /* [in] Number of bytes to be allocated */
792 ) {
793    UINT16    uParagraph;
794    LPVOID    lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
795
796    if( lpBlock )
797    {
798        HMODULE16 hModule = GetModuleHandle16("KERNEL");
799        WORD      wSelector;
800        GLOBALARENA *pArena;
801
802        wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
803        pArena = GET_ARENA_PTR(wSelector);
804        pArena->flags |= GA_DOSMEM;
805        return MAKELONG(wSelector,uParagraph);
806    }
807    return 0;
808 }
809
810
811 /***********************************************************************
812  *           GlobalDOSFree      (KERNEL.185)
813  *
814  * Free memory allocated with GlobalDOSAlloc
815  *
816  * RETURNS
817  *      NULL: Success
818  *      sel: Failure
819  */
820 WORD WINAPI GlobalDOSFree16(
821             WORD sel /* [in] Selector */
822 ) {
823    DWORD   block = GetSelectorBase(sel);
824
825    if( block && block < 0x100000 )
826    {
827        LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
828        if( DOSMEM_FreeBlock( lpBlock ) )
829            GLOBAL_FreeBlock( sel );
830        sel = 0;
831    }
832    return sel;
833 }
834
835
836 /***********************************************************************
837  *           GlobalPageLock   (KERNEL.191)
838  *           GlobalSmartPageLock(KERNEL.230)
839  */
840 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
841 {
842     TRACE("%04x\n", handle );
843     if (!VALID_HANDLE(handle)) {
844         WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
845         return 0;
846     }
847     return ++(GET_ARENA_PTR(handle)->pageLockCount);
848 }
849
850
851 /***********************************************************************
852  *           GlobalPageUnlock   (KERNEL.192)
853  *           GlobalSmartPageUnlock(KERNEL.231)
854  */
855 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
856 {
857     TRACE("%04x\n", handle );
858     if (!VALID_HANDLE(handle)) {
859         WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
860         return 0;
861     }
862     return --(GET_ARENA_PTR(handle)->pageLockCount);
863 }
864
865
866 /***********************************************************************
867  *           GlobalFix     (KERNEL.197)
868  *           GlobalFix16   (KERNEL32.27)
869  */
870 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
871 {
872     TRACE("%04x\n", handle );
873     if (!VALID_HANDLE(handle)) {
874         WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
875         return 0;
876     }
877     GET_ARENA_PTR(handle)->lockCount++;
878
879     return GlobalHandleToSel16(handle);
880 }
881
882
883 /***********************************************************************
884  *           GlobalUnfix     (KERNEL.198)
885  *           GlobalUnfix16   (KERNEL32.28)
886  */
887 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
888 {
889     TRACE("%04x\n", handle );
890     if (!VALID_HANDLE(handle)) {
891         WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
892         return;
893     }
894     GET_ARENA_PTR(handle)->lockCount--;
895 }
896
897
898 /***********************************************************************
899  *           FarSetOwner   (KERNEL.403)
900  */
901 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
902 {
903     if (!VALID_HANDLE(handle)) {
904         WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
905         return;
906     }
907     GET_ARENA_PTR(handle)->hOwner = hOwner;
908 }
909
910
911 /***********************************************************************
912  *           FarGetOwner   (KERNEL.404)
913  */
914 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
915 {
916     if (!VALID_HANDLE(handle)) {
917         WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
918         return 0;
919     }
920     return GET_ARENA_PTR(handle)->hOwner;
921 }
922
923
924 /************************************************************************
925  *              GlobalMasterHandle (KERNEL.28)
926  *
927  *
928  * Should return selector and handle of the information structure for
929  * the global heap. selector and handle are stored in the THHOOK as
930  * pGlobalHeap and hGlobalHeap.
931  * As Wine doesn't have this structure, we return both values as zero
932  * Applications should interpret this as "No Global Heap"
933  */
934 DWORD WINAPI GlobalMasterHandle16(void)
935 {
936     FIXME(": stub\n");
937     return 0;
938 }
939
940 /***********************************************************************
941  *           GlobalHandleToSel   (TOOLHELP.50)
942  *
943  * FIXME: This is in TOOLHELP but we keep a copy here for now.
944  */
945 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
946 {
947     if (!handle) return 0;
948     if (!VALID_HANDLE(handle)) {
949         WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
950         return 0;
951     }
952     if (!(handle & 7))
953     {
954         WARN("Program attempted invalid selector conversion\n" );
955         return handle - 1;
956     }
957     return handle | 7;
958 }
959
960
961 /***********************************************************************
962  *           GetFreeMemInfo   (KERNEL.316)
963  */
964 DWORD WINAPI GetFreeMemInfo16(void)
965 {
966     MEMORYSTATUS status;
967     GlobalMemoryStatus( &status );
968     return MAKELONG( status.dwTotalVirtual/getpagesize(), status.dwAvailVirtual/getpagesize() );
969 }
970
971 /***********************************************************************
972  *           A20Proc   (KERNEL.165)
973  */
974 void WINAPI A20Proc16( WORD unused )
975 {
976     /* this is also a NOP in Windows */
977 }
978
979 /***********************************************************************
980  *           LimitEMSPages   (KERNEL.156)
981  */
982 DWORD WINAPI LimitEMSPages16( DWORD unused )
983 {
984     return 0;
985 }