kernel32/nls: Add missing translations for dea.nls.
[wine/hacks.git] / dlls / kernel32 / global16.c
blob6d70767d95200fca4c097f6e9e498668e0402e4d
1 /*
2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
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.
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.
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
20 /* 0xffff sometimes seems to mean: CURRENT_DS */
22 #include "config.h"
23 #include "wine/port.h"
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
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"
47 WINE_DEFAULT_DEBUG_CHANNEL(global);
49 /* Global arena block */
50 typedef struct
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;
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
69 /* Arena array (FIXME) */
70 static GLOBALARENA *pGlobalArena;
71 static int globalArenaSize;
73 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
75 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
76 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
78 static inline void* DOSMEM_AllocBlock(UINT size, UINT16* pseg)
80 if (!winedos.AllocDosBlock) load_winedos();
81 return winedos.AllocDosBlock ? winedos.AllocDosBlock(size, pseg) : NULL;
84 static inline BOOL DOSMEM_FreeBlock(void* ptr)
86 if (!winedos.FreeDosBlock) load_winedos();
87 return winedos.FreeDosBlock ? winedos.FreeDosBlock( ptr ) : FALSE;
90 static inline UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
92 if (!winedos.ResizeDosBlock) load_winedos();
93 return winedos.ResizeDosBlock ? winedos.ResizeDosBlock(ptr, size, TRUE) : 0;
96 static HANDLE get_win16_heap(void)
98 static HANDLE win16_heap;
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;
106 /***********************************************************************
107 * GLOBAL_GetArena
109 * Return the arena for a given selector, growing the arena array if needed.
111 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
113 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
115 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
116 GLOBALARENA *pNewArena;
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;
127 return pGlobalArena + (sel >> __AHSHIFT);
130 void debug_handles(void)
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);
140 if (printed)
141 DPRINTF("\n");
145 /***********************************************************************
146 * GLOBAL_CreateBlock
148 * Create a global heap block for a fixed range of linear memory.
150 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, void *ptr, DWORD size,
151 HGLOBAL16 hOwner, unsigned char selflags )
153 WORD sel, selcount;
154 GLOBALARENA *pArena;
156 /* Allocate the selector(s) */
158 sel = SELECTOR_AllocBlock( ptr, size, selflags );
159 if (!sel) return 0;
160 selcount = (size + 0xffff) / 0x10000;
162 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
164 SELECTOR_FreeBlock( sel );
165 return 0;
168 /* Fill the arena block */
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) );
184 return pArena->handle;
188 /***********************************************************************
189 * GLOBAL_FreeBlock
191 * Free a block allocated by GLOBAL_CreateBlock, without touching
192 * the associated linear memory range.
194 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
196 WORD sel;
197 GLOBALARENA *pArena;
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;
208 /***********************************************************************
209 * GLOBAL_MoveBlock
211 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, void *ptr, DWORD size )
213 WORD sel;
214 GLOBALARENA *pArena;
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;
223 pArena->base = ptr;
224 pArena->size = size;
225 SELECTOR_ReallocBlock( sel, ptr, size );
226 return TRUE;
229 /***********************************************************************
230 * GLOBAL_Alloc
232 * Implementation of GlobalAlloc16()
234 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
236 void *ptr;
237 HGLOBAL16 handle;
239 TRACE("%d flags=%04x\n", size, flags );
241 /* If size is 0, create a discarded block */
243 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
245 /* Fixup the size */
247 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
248 size = (size + 0x1f) & ~0x1f;
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;
255 /* Allocate the selector(s) */
257 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
258 if (!handle)
260 HeapFree( get_win16_heap(), 0, ptr );
261 return 0;
264 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
265 return handle;
268 /***********************************************************************
269 * GlobalAlloc (KERNEL.15)
270 * GlobalAlloc16 (KERNEL32.24)
272 * Allocate a global memory object.
274 * RETURNS
275 * Handle: Success
276 * NULL: Failure
278 HGLOBAL16 WINAPI GlobalAlloc16(
279 UINT16 flags, /* [in] Object allocation attributes */
280 DWORD size /* [in] Number of bytes to allocate */
282 HANDLE16 owner = GetCurrentPDB16();
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 );
290 /***********************************************************************
291 * GlobalReAlloc (KERNEL.16)
293 * Change the size or attributes of a global memory object.
295 * RETURNS
296 * Handle: Success
297 * NULL: Failure
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 */
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();
311 TRACE("%04x %d flags=%04x\n",
312 handle, size, flags );
313 if (!handle) return 0;
315 if (!VALID_HANDLE(handle))
317 WARN("Invalid handle 0x%04x!\n", handle);
318 return 0;
320 pArena = GET_ARENA_PTR( handle );
322 /* Discard the block if requested */
324 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
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;
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?
339 SELECTOR_ReallocBlock( sel, 0, 1 );
340 return handle;
343 /* Fixup the size */
345 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
346 if (size == 0) size = 0x20;
347 else size = (size + 0x1f) & ~0x1f;
349 /* Change the flags */
351 if (flags & GMEM_MODIFY)
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;
359 /* Reallocate the linear memory */
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 */
366 if (pArena->flags & GA_DOSMEM)
368 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
369 newptr = ptr;
370 else if(pArena->pageLockCount > 0)
371 newptr = 0;
372 else
374 newptr = DOSMEM_AllocBlock( size, 0 );
375 if (newptr)
377 memcpy( newptr, ptr, oldsize );
378 DOSMEM_FreeBlock( ptr );
382 else
385 * if more than one reader (e.g. some pointer has been
386 * given out by GetVDMPointer32W16),
387 * only try to realloc in place
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 );
399 if (!newptr)
401 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
402 if (pArena->pageLockCount <1)
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) );
411 return 0;
413 ptr = newptr;
415 /* Reallocate the selector(s) */
417 sel = SELECTOR_ReallocBlock( sel, ptr, size );
418 if (!sel)
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;
427 selcount = (size + 0xffff) / 0x10000;
429 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
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;
439 /* Fill the new arena block
440 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
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;
448 if (selcount > 1) /* clear the next arena blocks */
449 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
451 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
452 memset( (char *)ptr + oldsize, 0, size - oldsize );
453 return pNewArena->handle;
457 /***********************************************************************
458 * GlobalFree (KERNEL.17)
459 * GlobalFree16 (KERNEL32.31)
460 * RETURNS
461 * NULL: Success
462 * Handle: Failure
464 HGLOBAL16 WINAPI GlobalFree16(
465 HGLOBAL16 handle /* [in] Handle of global memory object */
467 void *ptr;
469 if (!VALID_HANDLE(handle))
471 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
472 return 0;
474 ptr = GET_ARENA_PTR(handle)->base;
476 TRACE("%04x\n", handle );
477 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
478 HeapFree( get_win16_heap(), 0, ptr );
479 return 0;
483 /**********************************************************************
484 * K32WOWGlobalLock16 (KERNEL32.60)
486 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
488 WORD sel = GlobalHandleToSel16( handle );
489 TRACE("(%04x) -> %08x\n", handle, MAKELONG( 0, sel ) );
491 if (handle)
493 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
495 if (!VALID_HANDLE(handle)) {
496 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
497 sel = 0;
499 else if (!GET_ARENA_PTR(handle)->base)
500 sel = 0;
501 else
502 GET_ARENA_PTR(handle)->lockCount++;
505 return MAKESEGPTR( sel, 0 );
510 /***********************************************************************
511 * GlobalLock (KERNEL.18)
513 * This is the GlobalLock16() function used by 16-bit code.
515 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
517 SEGPTR ret = K32WOWGlobalLock16( handle );
518 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
519 return ret;
523 /***********************************************************************
524 * GlobalLock16 (KERNEL32.25)
526 * This is the GlobalLock16() function used by 32-bit code.
528 * RETURNS
529 * Pointer to first byte of memory block
530 * NULL: Failure
532 LPVOID WINAPI GlobalLock16(
533 HGLOBAL16 handle /* [in] Handle of global memory object */
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;
543 /***********************************************************************
544 * GlobalUnlock (KERNEL.19)
545 * GlobalUnlock16 (KERNEL32.26)
546 * NOTES
547 * Should the return values be cast to booleans?
549 * RETURNS
550 * TRUE: Object is still locked
551 * FALSE: Object is unlocked
553 BOOL16 WINAPI GlobalUnlock16(
554 HGLOBAL16 handle /* [in] Handle of global memory object */
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;
561 TRACE("%04x\n", handle );
562 if (pArena->lockCount) pArena->lockCount--;
563 return pArena->lockCount;
566 /***********************************************************************
567 * GlobalChangeLockCount (KERNEL.365)
569 * This is declared as a register function as it has to preserve
570 * *all* registers, even AX/DX !
573 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
574 CONTEXT86 *context )
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 );
584 /***********************************************************************
585 * GlobalSize (KERNEL.20)
586 * GlobalSize16 (KERNEL32.32)
588 * Get the current size of a global memory object.
590 * RETURNS
591 * Size in bytes of object
592 * 0: Failure
594 DWORD WINAPI GlobalSize16(
595 HGLOBAL16 handle /* [in] Handle of global memory object */
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;
605 /***********************************************************************
606 * GlobalHandle (KERNEL.21)
608 * Get the handle associated with a pointer to the global memory block.
610 * NOTES
611 * Why is GlobalHandleToSel used here with the sel as input?
613 * RETURNS
614 * Handle: Success
615 * NULL: Failure
617 DWORD WINAPI GlobalHandle16(
618 WORD sel /* [in] Address of global memory block */
620 TRACE("%04x\n", sel );
621 if (!VALID_HANDLE(sel)) {
622 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
623 return 0;
625 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
628 /***********************************************************************
629 * GlobalHandleNoRIP (KERNEL.159)
631 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
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) );
638 return 0;
642 /***********************************************************************
643 * GlobalFlags (KERNEL.22)
645 * Get information about a global memory object.
647 * NOTES
648 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
649 * handle?
651 * RETURNS
652 * Value specifying flags and lock count
653 * GMEM_INVALID_HANDLE: Invalid handle
655 UINT16 WINAPI GlobalFlags16(
656 HGLOBAL16 handle /* [in] Handle of global memory object */
658 GLOBALARENA *pArena;
660 TRACE("%04x\n", handle );
661 if (!VALID_HANDLE(handle)) {
662 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
663 return 0;
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);
672 /***********************************************************************
673 * LockSegment (KERNEL.23)
675 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
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;
683 GET_ARENA_PTR(handle)->lockCount++;
684 return handle;
688 /***********************************************************************
689 * UnlockSegment (KERNEL.24)
691 void WINAPI UnlockSegment16( HGLOBAL16 handle )
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;
699 GET_ARENA_PTR(handle)->lockCount--;
700 /* FIXME: this ought to return the lock count in CX (go figure...) */
704 /***********************************************************************
705 * GlobalCompact (KERNEL.25)
707 DWORD WINAPI GlobalCompact16( DWORD desired )
709 return GLOBAL_MAX_ALLOC_SIZE;
713 /***********************************************************************
714 * GlobalFreeAll (KERNEL.26)
716 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
718 int i;
719 GLOBALARENA *pArena;
721 pArena = pGlobalArena;
722 for (i = 0; i < globalArenaSize; i++, pArena++)
724 if ((pArena->size != 0) && (pArena->hOwner == owner))
725 GlobalFree16( pArena->handle );
730 /***********************************************************************
731 * GlobalWire (KERNEL.111)
732 * GlobalWire16 (KERNEL32.29)
734 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
736 return WIN16_GlobalLock16( handle );
740 /***********************************************************************
741 * GlobalUnWire (KERNEL.112)
742 * GlobalUnWire16 (KERNEL32.30)
744 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
746 return !GlobalUnlock16( handle );
750 /***********************************************************************
751 * SetSwapAreaSize (KERNEL.106)
753 LONG WINAPI SetSwapAreaSize16( WORD size )
755 FIXME("(%d) - stub!\n", size );
756 return MAKELONG( size, 0xffff );
760 /***********************************************************************
761 * GlobalLRUOldest (KERNEL.163)
763 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
765 TRACE("%04x\n", handle );
766 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
767 return handle;
771 /***********************************************************************
772 * GlobalLRUNewest (KERNEL.164)
774 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
776 TRACE("%04x\n", handle );
777 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
778 return handle;
782 /***********************************************************************
783 * GetFreeSpace (KERNEL.169)
785 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
787 MEMORYSTATUS ms;
788 GlobalMemoryStatus( &ms );
789 return ms.dwAvailVirtual;
792 /***********************************************************************
793 * GlobalDOSAlloc (KERNEL.184)
795 * Allocate memory in the first MB.
797 * RETURNS
798 * Address (HW=Paragraph segment; LW=Selector)
800 DWORD WINAPI GlobalDOSAlloc16(
801 DWORD size /* [in] Number of bytes to be allocated */
803 UINT16 uParagraph;
804 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
806 if( lpBlock )
808 HMODULE16 hModule = GetModuleHandle16("KERNEL");
809 WORD wSelector;
810 GLOBALARENA *pArena;
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);
817 return 0;
821 /***********************************************************************
822 * GlobalDOSFree (KERNEL.185)
824 * Free memory allocated with GlobalDOSAlloc
826 * RETURNS
827 * NULL: Success
828 * sel: Failure
830 WORD WINAPI GlobalDOSFree16(
831 WORD sel /* [in] Selector */
833 DWORD block = GetSelectorBase(sel);
835 if( block && block < 0x100000 )
837 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
838 if( DOSMEM_FreeBlock( lpBlock ) )
839 GLOBAL_FreeBlock( sel );
840 sel = 0;
842 return sel;
846 /***********************************************************************
847 * GlobalPageLock (KERNEL.191)
848 * GlobalSmartPageLock(KERNEL.230)
850 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
852 TRACE("%04x\n", handle );
853 if (!VALID_HANDLE(handle)) {
854 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
855 return 0;
857 return ++(GET_ARENA_PTR(handle)->pageLockCount);
861 /***********************************************************************
862 * GlobalPageUnlock (KERNEL.192)
863 * GlobalSmartPageUnlock(KERNEL.231)
865 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
867 TRACE("%04x\n", handle );
868 if (!VALID_HANDLE(handle)) {
869 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
870 return 0;
872 return --(GET_ARENA_PTR(handle)->pageLockCount);
876 /***********************************************************************
877 * GlobalFix (KERNEL.197)
878 * GlobalFix16 (KERNEL32.27)
880 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
882 TRACE("%04x\n", handle );
883 if (!VALID_HANDLE(handle)) {
884 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
885 return 0;
887 GET_ARENA_PTR(handle)->lockCount++;
889 return GlobalHandleToSel16(handle);
893 /***********************************************************************
894 * GlobalUnfix (KERNEL.198)
895 * GlobalUnfix16 (KERNEL32.28)
897 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
899 TRACE("%04x\n", handle );
900 if (!VALID_HANDLE(handle)) {
901 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
902 return;
904 GET_ARENA_PTR(handle)->lockCount--;
908 /***********************************************************************
909 * FarSetOwner (KERNEL.403)
911 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
913 if (!VALID_HANDLE(handle)) {
914 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
915 return;
917 GET_ARENA_PTR(handle)->hOwner = hOwner;
921 /***********************************************************************
922 * FarGetOwner (KERNEL.404)
924 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
926 if (!VALID_HANDLE(handle)) {
927 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
928 return 0;
930 return GET_ARENA_PTR(handle)->hOwner;
934 /************************************************************************
935 * GlobalMasterHandle (KERNEL.28)
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"
944 DWORD WINAPI GlobalMasterHandle16(void)
946 FIXME(": stub\n");
947 return 0;
950 /***********************************************************************
951 * GlobalHandleToSel (TOOLHELP.50)
953 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
955 if (!handle) return 0;
956 if (!VALID_HANDLE(handle)) {
957 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
958 return 0;
960 if (!(handle & 7))
962 WARN("Program attempted invalid selector conversion\n" );
963 return handle - 1;
965 return handle | 7;
969 /***********************************************************************
970 * GlobalFirst (TOOLHELP.51)
972 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
974 if (wFlags == GLOBAL_LRU) return FALSE;
975 pGlobal->dwNext = 0;
976 return GlobalNext16( pGlobal, wFlags );
980 /***********************************************************************
981 * GlobalNext (TOOLHELP.52)
983 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
985 GLOBALARENA *pArena;
987 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
988 pArena = pGlobalArena + pGlobal->dwNext;
989 if (wFlags == GLOBAL_FREE) /* only free blocks */
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;
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;
1013 /***********************************************************************
1014 * GlobalInfo (TOOLHELP.53)
1016 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
1018 int i;
1019 GLOBALARENA *pArena;
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;
1030 /***********************************************************************
1031 * GlobalEntryHandle (TOOLHELP.54)
1033 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
1035 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
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;
1052 /***********************************************************************
1053 * GlobalEntryModule (TOOLHELP.55)
1055 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1056 WORD wSeg )
1058 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1059 return FALSE;
1063 /***********************************************************************
1064 * MemManInfo (TOOLHELP.72)
1066 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1068 MEMORYSTATUS status;
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.
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;
1089 /***********************************************************************
1090 * GetFreeMemInfo (KERNEL.316)
1092 DWORD WINAPI GetFreeMemInfo16(void)
1094 MEMMANINFO info;
1095 MemManInfo16( &info );
1096 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1099 /***********************************************************************
1100 * A20Proc (KERNEL.165)
1102 void WINAPI A20Proc16( WORD unused )
1104 /* this is also a NOP in Windows */
1107 /***********************************************************************
1108 * LimitEMSPages (KERNEL.156)
1110 DWORD WINAPI LimitEMSPages16( DWORD unused )
1112 return 0;