Fix a regression in IE where the Favourites menu didn't appear
[wine/testsucceed.git] / dlls / kernel / global16.c
blobad7918a63bc7383c305c26246aa624dace3a9e62
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "ntstatus.h"
42 #include "toolhelp.h"
43 #include "winternl.h"
44 #include "kernel_private.h"
45 #include "kernel16_private.h"
46 #include "wine/debug.h"
47 #include "winerror.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(global);
51 /* Global arena block */
52 typedef struct
54 DWORD base; /* Base address (0 if discarded) */
55 DWORD size; /* Size in bytes (0 indicates a free block) */
56 HGLOBAL16 handle; /* Handle for this block */
57 HGLOBAL16 hOwner; /* Owner of this block */
58 BYTE lockCount; /* Count of GlobalFix() calls */
59 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
60 BYTE flags; /* Allocation flags */
61 BYTE selCount; /* Number of selectors allocated for this block */
62 } GLOBALARENA;
64 /* Flags definitions */
65 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
66 #define GA_DGROUP 0x04
67 #define GA_DISCARDABLE 0x08
68 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
69 #define GA_DOSMEM 0x20
71 /* Arena array (FIXME) */
72 static GLOBALARENA *pGlobalArena;
73 static int globalArenaSize;
75 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
77 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
78 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
80 static inline void* DOSMEM_AllocBlock(UINT size, UINT16* pseg)
82 if (!winedos.AllocDosBlock) load_winedos();
83 return winedos.AllocDosBlock ? winedos.AllocDosBlock(size, pseg) : NULL;
86 static inline BOOL DOSMEM_FreeBlock(void* ptr)
88 if (!winedos.FreeDosBlock) load_winedos();
89 return winedos.FreeDosBlock ? winedos.FreeDosBlock( ptr ) : FALSE;
92 static inline UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
94 if (!winedos.ResizeDosBlock) load_winedos();
95 return winedos.ResizeDosBlock ? winedos.ResizeDosBlock(ptr, size, TRUE) : 0;
98 /***********************************************************************
99 * GLOBAL_GetArena
101 * Return the arena for a given selector, growing the arena array if needed.
103 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
105 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
107 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
108 GLOBALARENA *pNewArena = realloc( pGlobalArena,
109 newsize * sizeof(GLOBALARENA) );
110 if (!pNewArena) return 0;
111 pGlobalArena = pNewArena;
112 memset( pGlobalArena + globalArenaSize, 0,
113 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
114 globalArenaSize = newsize;
116 return pGlobalArena + (sel >> __AHSHIFT);
119 void debug_handles(void)
121 int printed=0;
122 int i;
123 for (i = globalArenaSize-1 ; i>=0 ; i--) {
124 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
125 printed=1;
126 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
129 if (printed)
130 DPRINTF("\n");
134 /***********************************************************************
135 * GLOBAL_CreateBlock
137 * Create a global heap block for a fixed range of linear memory.
139 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
140 HGLOBAL16 hOwner, unsigned char selflags )
142 WORD sel, selcount;
143 GLOBALARENA *pArena;
145 /* Allocate the selector(s) */
147 sel = SELECTOR_AllocBlock( ptr, size, selflags );
148 if (!sel) return 0;
149 selcount = (size + 0xffff) / 0x10000;
151 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
153 SELECTOR_FreeBlock( sel );
154 return 0;
157 /* Fill the arena block */
159 pArena->base = (DWORD)ptr;
160 pArena->size = GetSelectorLimit16(sel) + 1;
161 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
162 pArena->hOwner = hOwner;
163 pArena->lockCount = 0;
164 pArena->pageLockCount = 0;
165 pArena->flags = flags & GA_MOVEABLE;
166 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
167 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
168 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
169 pArena->selCount = selcount;
170 if (selcount > 1) /* clear the next arena blocks */
171 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
173 return pArena->handle;
177 /***********************************************************************
178 * GLOBAL_FreeBlock
180 * Free a block allocated by GLOBAL_CreateBlock, without touching
181 * the associated linear memory range.
183 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
185 WORD sel;
186 GLOBALARENA *pArena;
188 if (!handle) return TRUE;
189 sel = GlobalHandleToSel16( handle );
190 if (!VALID_HANDLE(sel)) return FALSE;
191 pArena = GET_ARENA_PTR(sel);
192 SELECTOR_FreeBlock( sel );
193 memset( pArena, 0, sizeof(GLOBALARENA) );
194 return TRUE;
197 /***********************************************************************
198 * GLOBAL_MoveBlock
200 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
202 WORD sel;
203 GLOBALARENA *pArena;
205 if (!handle) return TRUE;
206 sel = GlobalHandleToSel16( handle );
207 if (!VALID_HANDLE(sel)) return FALSE;
208 pArena = GET_ARENA_PTR(sel);
209 if (pArena->selCount != 1)
210 return FALSE;
212 pArena->base = (DWORD)ptr;
213 pArena->size = size;
214 SELECTOR_ReallocBlock( sel, ptr, size );
215 return TRUE;
218 /***********************************************************************
219 * GLOBAL_Alloc
221 * Implementation of GlobalAlloc16()
223 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
225 void *ptr;
226 HGLOBAL16 handle;
228 TRACE("%ld flags=%04x\n", size, flags );
230 /* If size is 0, create a discarded block */
232 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
234 /* Fixup the size */
236 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
237 size = (size + 0x1f) & ~0x1f;
239 /* Allocate the linear memory */
240 ptr = HeapAlloc( GetProcessHeap(), 0, size );
241 /* FIXME: free discardable blocks and try again? */
242 if (!ptr) return 0;
244 /* Allocate the selector(s) */
246 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
247 if (!handle)
249 HeapFree( GetProcessHeap(), 0, ptr );
250 return 0;
253 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
254 return handle;
257 /***********************************************************************
258 * GlobalAlloc (KERNEL.15)
259 * GlobalAlloc16 (KERNEL32.24)
260 * RETURNS
261 * Handle: Success
262 * NULL: Failure
264 HGLOBAL16 WINAPI GlobalAlloc16(
265 UINT16 flags, /* [in] Object allocation attributes */
266 DWORD size /* [in] Number of bytes to allocate */
268 HANDLE16 owner = GetCurrentPDB16();
270 if (flags & GMEM_DDESHARE)
271 owner = GetExePtr(owner); /* Make it a module handle */
272 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
276 /***********************************************************************
277 * GlobalReAlloc (KERNEL.16)
278 * RETURNS
279 * Handle: Success
280 * NULL: Failure
282 HGLOBAL16 WINAPI GlobalReAlloc16(
283 HGLOBAL16 handle, /* [in] Handle of global memory object */
284 DWORD size, /* [in] New size of block */
285 UINT16 flags /* [in] How to reallocate object */
287 WORD selcount;
288 DWORD oldsize;
289 void *ptr, *newptr;
290 GLOBALARENA *pArena, *pNewArena;
291 WORD sel = GlobalHandleToSel16( handle );
293 TRACE("%04x %ld flags=%04x\n",
294 handle, size, flags );
295 if (!handle) return 0;
297 if (!VALID_HANDLE(handle))
299 WARN("Invalid handle 0x%04x!\n", handle);
300 return 0;
302 pArena = GET_ARENA_PTR( handle );
304 /* Discard the block if requested */
306 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
308 if (!(pArena->flags & GA_MOVEABLE) ||
309 !(pArena->flags & GA_DISCARDABLE) ||
310 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
311 if (pArena->flags & GA_DOSMEM)
312 DOSMEM_FreeBlock( (void *)pArena->base );
313 else
314 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
315 pArena->base = 0;
317 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
318 * change the selector if we are shrinking the block.
319 * FIXME: shouldn't we keep selectors until the block is deleted?
321 SELECTOR_ReallocBlock( sel, 0, 1 );
322 return handle;
325 /* Fixup the size */
327 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
328 if (size == 0) size = 0x20;
329 else size = (size + 0x1f) & ~0x1f;
331 /* Change the flags */
333 if (flags & GMEM_MODIFY)
335 /* Change the flags, leaving GA_DGROUP alone */
336 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
337 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
338 return handle;
341 /* Reallocate the linear memory */
343 ptr = (void *)pArena->base;
344 oldsize = pArena->size;
345 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
346 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
348 if (pArena->flags & GA_DOSMEM)
350 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
351 newptr = ptr;
352 else if(pArena->pageLockCount > 0)
353 newptr = 0;
354 else
356 newptr = DOSMEM_AllocBlock( size, 0 );
357 if (newptr)
359 memcpy( newptr, ptr, oldsize );
360 DOSMEM_FreeBlock( ptr );
364 else
367 * if more than one reader (e.g. some pointer has been
368 * given out by GetVDMPointer32W16),
369 * only try to realloc in place
372 if (ptr)
373 newptr = HeapReAlloc( GetProcessHeap(),
374 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
375 ptr, size );
376 else
377 newptr = HeapAlloc( GetProcessHeap(),
378 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
379 size );
383 if (!newptr)
385 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
386 if (pArena->pageLockCount <1)
388 if (pArena->flags & GA_DOSMEM)
389 DOSMEM_FreeBlock( (void *)pArena->base );
390 else
391 HeapFree( GetProcessHeap(), 0, ptr );
392 SELECTOR_FreeBlock( sel );
393 memset( pArena, 0, sizeof(GLOBALARENA) );
395 return 0;
397 ptr = newptr;
399 /* Reallocate the selector(s) */
401 sel = SELECTOR_ReallocBlock( sel, ptr, size );
402 if (!sel)
404 if (pArena->flags & GA_DOSMEM)
405 DOSMEM_FreeBlock( (void *)pArena->base );
406 else
407 HeapFree( GetProcessHeap(), 0, ptr );
408 memset( pArena, 0, sizeof(GLOBALARENA) );
409 return 0;
411 selcount = (size + 0xffff) / 0x10000;
413 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
415 if (pArena->flags & GA_DOSMEM)
416 DOSMEM_FreeBlock( (void *)pArena->base );
417 else
418 HeapFree( GetProcessHeap(), 0, ptr );
419 SELECTOR_FreeBlock( sel );
420 return 0;
423 /* Fill the new arena block
424 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
426 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
427 pNewArena->base = (DWORD)ptr;
428 pNewArena->size = GetSelectorLimit16(sel) + 1;
429 pNewArena->selCount = selcount;
430 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
432 if (selcount > 1) /* clear the next arena blocks */
433 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
435 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
436 memset( (char *)ptr + oldsize, 0, size - oldsize );
437 return pNewArena->handle;
441 /***********************************************************************
442 * GlobalFree (KERNEL.17)
443 * GlobalFree16 (KERNEL32.31)
444 * RETURNS
445 * NULL: Success
446 * Handle: Failure
448 HGLOBAL16 WINAPI GlobalFree16(
449 HGLOBAL16 handle /* [in] Handle of global memory object */
451 void *ptr;
453 if (!VALID_HANDLE(handle))
455 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
456 return 0;
458 ptr = (void *)GET_ARENA_PTR(handle)->base;
460 TRACE("%04x\n", handle );
461 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
462 HeapFree( GetProcessHeap(), 0, ptr );
463 return 0;
467 /**********************************************************************
468 * K32WOWGlobalLock16 (KERNEL32.60)
470 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
472 WORD sel = GlobalHandleToSel16( handle );
473 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
475 if (handle)
477 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
479 if (!VALID_HANDLE(handle)) {
480 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
481 sel = 0;
483 else if (!GET_ARENA_PTR(handle)->base)
484 sel = 0;
485 else
486 GET_ARENA_PTR(handle)->lockCount++;
489 return MAKESEGPTR( sel, 0 );
494 /***********************************************************************
495 * GlobalLock (KERNEL.18)
497 * This is the GlobalLock16() function used by 16-bit code.
499 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
501 SEGPTR ret = K32WOWGlobalLock16( handle );
502 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
503 return ret;
507 /***********************************************************************
508 * GlobalLock16 (KERNEL32.25)
510 * This is the GlobalLock16() function used by 32-bit code.
512 * RETURNS
513 * Pointer to first byte of memory block
514 * NULL: Failure
516 LPVOID WINAPI GlobalLock16(
517 HGLOBAL16 handle /* [in] Handle of global memory object */
519 if (!handle) return 0;
520 if (!VALID_HANDLE(handle))
521 return 0;
522 GET_ARENA_PTR(handle)->lockCount++;
523 return (LPVOID)GET_ARENA_PTR(handle)->base;
527 /***********************************************************************
528 * GlobalUnlock (KERNEL.19)
529 * GlobalUnlock16 (KERNEL32.26)
530 * NOTES
531 * Should the return values be cast to booleans?
533 * RETURNS
534 * TRUE: Object is still locked
535 * FALSE: Object is unlocked
537 BOOL16 WINAPI GlobalUnlock16(
538 HGLOBAL16 handle /* [in] Handle of global memory object */
540 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
541 if (!VALID_HANDLE(handle)) {
542 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
543 return 0;
545 TRACE("%04x\n", handle );
546 if (pArena->lockCount) pArena->lockCount--;
547 return pArena->lockCount;
550 /***********************************************************************
551 * GlobalChangeLockCount (KERNEL.365)
553 * This is declared as a register function as it has to preserve
554 * *all* registers, even AX/DX !
557 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
558 CONTEXT86 *context )
560 if ( delta == 1 )
561 GlobalLock16( handle );
562 else if ( delta == -1 )
563 GlobalUnlock16( handle );
564 else
565 ERR("(%04X, %d): strange delta value\n", handle, delta );
568 /***********************************************************************
569 * GlobalSize (KERNEL.20)
570 * GlobalSize16 (KERNEL32.32)
571 * RETURNS
572 * Size in bytes of object
573 * 0: Failure
575 DWORD WINAPI GlobalSize16(
576 HGLOBAL16 handle /* [in] Handle of global memory object */
578 TRACE("%04x\n", handle );
579 if (!handle) return 0;
580 if (!VALID_HANDLE(handle))
581 return 0;
582 return GET_ARENA_PTR(handle)->size;
586 /***********************************************************************
587 * GlobalHandle (KERNEL.21)
588 * NOTES
589 * Why is GlobalHandleToSel used here with the sel as input?
591 * RETURNS
592 * Handle: Success
593 * NULL: Failure
595 DWORD WINAPI GlobalHandle16(
596 WORD sel /* [in] Address of global memory block */
598 TRACE("%04x\n", sel );
599 if (!VALID_HANDLE(sel)) {
600 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
601 return 0;
603 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
606 /***********************************************************************
607 * GlobalHandleNoRIP (KERNEL.159)
609 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
611 int i;
612 for (i = globalArenaSize-1 ; i>=0 ; i--) {
613 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
614 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
616 return 0;
620 /***********************************************************************
621 * GlobalFlags (KERNEL.22)
623 * NOTES
624 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
625 * handle?
627 * RETURNS
628 * Value specifying flags and lock count
629 * GMEM_INVALID_HANDLE: Invalid handle
631 UINT16 WINAPI GlobalFlags16(
632 HGLOBAL16 handle /* [in] Handle of global memory object */
634 GLOBALARENA *pArena;
636 TRACE("%04x\n", handle );
637 if (!VALID_HANDLE(handle)) {
638 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
639 return 0;
641 pArena = GET_ARENA_PTR(handle);
642 return pArena->lockCount |
643 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
644 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
648 /***********************************************************************
649 * LockSegment (KERNEL.23)
651 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
653 TRACE("%04x\n", handle );
654 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
655 if (!VALID_HANDLE(handle)) {
656 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
657 return 0;
659 GET_ARENA_PTR(handle)->lockCount++;
660 return handle;
664 /***********************************************************************
665 * UnlockSegment (KERNEL.24)
667 void WINAPI UnlockSegment16( HGLOBAL16 handle )
669 TRACE("%04x\n", handle );
670 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
671 if (!VALID_HANDLE(handle)) {
672 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
673 return;
675 GET_ARENA_PTR(handle)->lockCount--;
676 /* FIXME: this ought to return the lock count in CX (go figure...) */
680 /***********************************************************************
681 * GlobalCompact (KERNEL.25)
683 DWORD WINAPI GlobalCompact16( DWORD desired )
685 return GLOBAL_MAX_ALLOC_SIZE;
689 /***********************************************************************
690 * GlobalFreeAll (KERNEL.26)
692 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
694 int i;
695 GLOBALARENA *pArena;
697 pArena = pGlobalArena;
698 for (i = 0; i < globalArenaSize; i++, pArena++)
700 if ((pArena->size != 0) && (pArena->hOwner == owner))
701 GlobalFree16( pArena->handle );
706 /***********************************************************************
707 * GlobalWire (KERNEL.111)
708 * GlobalWire16 (KERNEL32.29)
710 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
712 return WIN16_GlobalLock16( handle );
716 /***********************************************************************
717 * GlobalUnWire (KERNEL.112)
718 * GlobalUnWire16 (KERNEL32.30)
720 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
722 return !GlobalUnlock16( handle );
726 /***********************************************************************
727 * SetSwapAreaSize (KERNEL.106)
729 LONG WINAPI SetSwapAreaSize16( WORD size )
731 FIXME("(%d) - stub!\n", size );
732 return MAKELONG( size, 0xffff );
736 /***********************************************************************
737 * GlobalLRUOldest (KERNEL.163)
739 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
741 TRACE("%04x\n", handle );
742 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
743 return handle;
747 /***********************************************************************
748 * GlobalLRUNewest (KERNEL.164)
750 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
752 TRACE("%04x\n", handle );
753 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
754 return handle;
758 /***********************************************************************
759 * GetFreeSpace (KERNEL.169)
761 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
763 MEMORYSTATUS ms;
764 GlobalMemoryStatus( &ms );
765 return ms.dwAvailVirtual;
768 /***********************************************************************
769 * GlobalDOSAlloc (KERNEL.184)
770 * RETURNS
771 * Address (HW=Paragraph segment; LW=Selector)
773 DWORD WINAPI GlobalDOSAlloc16(
774 DWORD size /* [in] Number of bytes to be allocated */
776 UINT16 uParagraph;
777 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
779 if( lpBlock )
781 HMODULE16 hModule = GetModuleHandle16("KERNEL");
782 WORD wSelector;
783 GLOBALARENA *pArena;
785 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
786 pArena = GET_ARENA_PTR(wSelector);
787 pArena->flags |= GA_DOSMEM;
788 return MAKELONG(wSelector,uParagraph);
790 return 0;
794 /***********************************************************************
795 * GlobalDOSFree (KERNEL.185)
796 * RETURNS
797 * NULL: Success
798 * sel: Failure
800 WORD WINAPI GlobalDOSFree16(
801 WORD sel /* [in] Selector */
803 DWORD block = GetSelectorBase(sel);
805 if( block && block < 0x100000 )
807 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
808 if( DOSMEM_FreeBlock( lpBlock ) )
809 GLOBAL_FreeBlock( sel );
810 sel = 0;
812 return sel;
816 /***********************************************************************
817 * GlobalPageLock (KERNEL.191)
818 * GlobalSmartPageLock(KERNEL.230)
820 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
822 TRACE("%04x\n", handle );
823 if (!VALID_HANDLE(handle)) {
824 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
825 return 0;
827 return ++(GET_ARENA_PTR(handle)->pageLockCount);
831 /***********************************************************************
832 * GlobalPageUnlock (KERNEL.192)
833 * GlobalSmartPageUnlock(KERNEL.231)
835 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
837 TRACE("%04x\n", handle );
838 if (!VALID_HANDLE(handle)) {
839 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
840 return 0;
842 return --(GET_ARENA_PTR(handle)->pageLockCount);
846 /***********************************************************************
847 * GlobalFix (KERNEL.197)
848 * GlobalFix16 (KERNEL32.27)
850 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
852 TRACE("%04x\n", handle );
853 if (!VALID_HANDLE(handle)) {
854 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
855 return 0;
857 GET_ARENA_PTR(handle)->lockCount++;
859 return GlobalHandleToSel16(handle);
863 /***********************************************************************
864 * GlobalUnfix (KERNEL.198)
865 * GlobalUnfix16 (KERNEL32.28)
867 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
869 TRACE("%04x\n", handle );
870 if (!VALID_HANDLE(handle)) {
871 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
872 return;
874 GET_ARENA_PTR(handle)->lockCount--;
878 /***********************************************************************
879 * FarSetOwner (KERNEL.403)
881 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
883 if (!VALID_HANDLE(handle)) {
884 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
885 return;
887 GET_ARENA_PTR(handle)->hOwner = hOwner;
891 /***********************************************************************
892 * FarGetOwner (KERNEL.404)
894 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
896 if (!VALID_HANDLE(handle)) {
897 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
898 return 0;
900 return GET_ARENA_PTR(handle)->hOwner;
904 /***********************************************************************
905 * GlobalHandleToSel (TOOLHELP.50)
907 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
909 if (!handle) return 0;
910 if (!VALID_HANDLE(handle)) {
911 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
912 return 0;
914 if (!(handle & 7))
916 WARN("Program attempted invalid selector conversion\n" );
917 return handle - 1;
919 return handle | 7;
923 /***********************************************************************
924 * GlobalFirst (TOOLHELP.51)
926 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
928 if (wFlags == GLOBAL_LRU) return FALSE;
929 pGlobal->dwNext = 0;
930 return GlobalNext16( pGlobal, wFlags );
934 /***********************************************************************
935 * GlobalNext (TOOLHELP.52)
937 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
939 GLOBALARENA *pArena;
941 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
942 pArena = pGlobalArena + pGlobal->dwNext;
943 if (wFlags == GLOBAL_FREE) /* only free blocks */
945 int i;
946 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
947 if (pArena->size == 0) break; /* block is free */
948 if (i >= globalArenaSize) return FALSE;
949 pGlobal->dwNext = i;
952 pGlobal->dwAddress = pArena->base;
953 pGlobal->dwBlockSize = pArena->size;
954 pGlobal->hBlock = pArena->handle;
955 pGlobal->wcLock = pArena->lockCount;
956 pGlobal->wcPageLock = pArena->pageLockCount;
957 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
958 pGlobal->wHeapPresent = FALSE;
959 pGlobal->hOwner = pArena->hOwner;
960 pGlobal->wType = GT_UNKNOWN;
961 pGlobal->wData = 0;
962 pGlobal->dwNext++;
963 return TRUE;
967 /***********************************************************************
968 * GlobalInfo (TOOLHELP.53)
970 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
972 int i;
973 GLOBALARENA *pArena;
975 pInfo->wcItems = globalArenaSize;
976 pInfo->wcItemsFree = 0;
977 pInfo->wcItemsLRU = 0;
978 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
979 if (pArena->size == 0) pInfo->wcItemsFree++;
980 return TRUE;
984 /***********************************************************************
985 * GlobalEntryHandle (TOOLHELP.54)
987 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
989 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
991 pGlobal->dwAddress = pArena->base;
992 pGlobal->dwBlockSize = pArena->size;
993 pGlobal->hBlock = pArena->handle;
994 pGlobal->wcLock = pArena->lockCount;
995 pGlobal->wcPageLock = pArena->pageLockCount;
996 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
997 pGlobal->wHeapPresent = FALSE;
998 pGlobal->hOwner = pArena->hOwner;
999 pGlobal->wType = GT_UNKNOWN;
1000 pGlobal->wData = 0;
1001 pGlobal->dwNext++;
1002 return TRUE;
1006 /***********************************************************************
1007 * GlobalEntryModule (TOOLHELP.55)
1009 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1010 WORD wSeg )
1012 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1013 return FALSE;
1017 /***********************************************************************
1018 * MemManInfo (TOOLHELP.72)
1020 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1022 MEMORYSTATUS status;
1025 * Not unsurprisingly although the documention says you
1026 * _must_ provide the size in the dwSize field, this function
1027 * (under Windows) always fills the structure and returns true.
1029 GlobalMemoryStatus( &status );
1030 info->wPageSize = getpagesize();
1031 info->dwLargestFreeBlock = status.dwAvailVirtual;
1032 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1033 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1034 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1035 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1036 info->dwFreePages = info->dwMaxPagesAvailable;
1037 info->dwTotalPages = info->dwTotalLinearSpace;
1038 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1039 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1040 return TRUE;
1043 /***********************************************************************
1044 * GetFreeMemInfo (KERNEL.316)
1046 DWORD WINAPI GetFreeMemInfo16(void)
1048 MEMMANINFO info;
1049 MemManInfo16( &info );
1050 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1053 /***********************************************************************
1054 * A20Proc (KERNEL.165)
1055 * A20_Proc (SYSTEM.20)
1057 void WINAPI A20Proc16( WORD unused )
1059 /* this is also a NOP in Windows */
1062 /***********************************************************************
1063 * LimitEMSPages (KERNEL.156)
1065 DWORD WINAPI LimitEMSPages16( DWORD unused )
1067 return 0;