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