Added dumping of message table resources.
[wine/multimedia.git] / memory / global.c
blobeeabd1ea92fb41c7947bb652882304a4bb2d028c
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 "wine/exception.h"
42 #include "ntstatus.h"
43 #include "global.h"
44 #include "toolhelp.h"
45 #include "selectors.h"
46 #include "miscemu.h"
47 #include "stackframe.h"
48 #include "wine/debug.h"
49 #include "winerror.h"
50 #include "excpt.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(global);
54 /* Global arena block */
55 typedef struct
57 DWORD base; /* Base address (0 if discarded) */
58 DWORD size; /* Size in bytes (0 indicates a free block) */
59 HGLOBAL16 handle; /* Handle for this block */
60 HGLOBAL16 hOwner; /* Owner of this block */
61 BYTE lockCount; /* Count of GlobalFix() calls */
62 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
63 BYTE flags; /* Allocation flags */
64 BYTE selCount; /* Number of selectors allocated for this block */
65 } GLOBALARENA;
67 /* Flags definitions */
68 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
69 #define GA_DGROUP 0x04
70 #define GA_DISCARDABLE 0x08
71 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
72 #define GA_DOSMEM 0x20
74 /* Arena array */
75 static GLOBALARENA *pGlobalArena = NULL;
76 static int globalArenaSize = 0;
78 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
80 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
81 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
83 /* filter for page-fault exceptions */
84 /* It is possible for a bogus global pointer to cause a */
85 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
87 static WINE_EXCEPTION_FILTER(page_fault)
89 switch (GetExceptionCode()) {
90 case (EXCEPTION_ACCESS_VIOLATION):
91 case (EXCEPTION_PRIV_INSTRUCTION):
92 return EXCEPTION_EXECUTE_HANDLER;
93 default:
94 return EXCEPTION_CONTINUE_SEARCH;
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))
191 return FALSE;
192 pArena = GET_ARENA_PTR(sel);
193 SELECTOR_FreeBlock( sel );
194 memset( pArena, 0, sizeof(GLOBALARENA) );
195 return TRUE;
198 /***********************************************************************
199 * GLOBAL_MoveBlock
201 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
203 WORD sel;
204 GLOBALARENA *pArena;
206 if (!handle) return TRUE;
207 sel = GlobalHandleToSel16( handle );
208 if (!VALID_HANDLE(sel))
209 return FALSE;
210 pArena = GET_ARENA_PTR(sel);
211 if (pArena->selCount != 1)
212 return FALSE;
214 pArena->base = (DWORD)ptr;
215 pArena->size = size;
216 SELECTOR_ReallocBlock( sel, ptr, size );
217 return TRUE;
220 /***********************************************************************
221 * GLOBAL_Alloc
223 * Implementation of GlobalAlloc16()
225 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
227 void *ptr;
228 HGLOBAL16 handle;
230 TRACE("%ld flags=%04x\n", size, flags );
232 /* If size is 0, create a discarded block */
234 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
236 /* Fixup the size */
238 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
239 size = (size + 0x1f) & ~0x1f;
241 /* Allocate the linear memory */
242 ptr = HeapAlloc( GetProcessHeap(), 0, size );
243 /* FIXME: free discardable blocks and try again? */
244 if (!ptr) return 0;
246 /* Allocate the selector(s) */
248 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
249 if (!handle)
251 HeapFree( GetProcessHeap(), 0, ptr );
252 return 0;
255 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
256 return handle;
259 /***********************************************************************
260 * GlobalAlloc (KERNEL.15)
261 * GlobalAlloc16 (KERNEL32.24)
262 * RETURNS
263 * Handle: Success
264 * NULL: Failure
266 HGLOBAL16 WINAPI GlobalAlloc16(
267 UINT16 flags, /* [in] Object allocation attributes */
268 DWORD size /* [in] Number of bytes to allocate */
270 HANDLE16 owner = GetCurrentPDB16();
272 if (flags & GMEM_DDESHARE)
273 owner = GetExePtr(owner); /* Make it a module handle */
274 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
278 /***********************************************************************
279 * GlobalReAlloc (KERNEL.16)
280 * RETURNS
281 * Handle: Success
282 * NULL: Failure
284 HGLOBAL16 WINAPI GlobalReAlloc16(
285 HGLOBAL16 handle, /* [in] Handle of global memory object */
286 DWORD size, /* [in] New size of block */
287 UINT16 flags /* [in] How to reallocate object */
289 WORD selcount;
290 DWORD oldsize;
291 void *ptr, *newptr;
292 GLOBALARENA *pArena, *pNewArena;
293 WORD sel = GlobalHandleToSel16( handle );
295 TRACE("%04x %ld flags=%04x\n",
296 handle, size, flags );
297 if (!handle) return 0;
299 if (!VALID_HANDLE(handle)) {
300 WARN("Invalid handle 0x%04x!\n", handle);
301 return 0;
303 pArena = GET_ARENA_PTR( handle );
305 /* Discard the block if requested */
307 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
309 if (!(pArena->flags & GA_MOVEABLE) ||
310 !(pArena->flags & GA_DISCARDABLE) ||
311 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
312 if (pArena->flags & GA_DOSMEM)
313 DOSMEM_FreeBlock( (void *)pArena->base );
314 else
315 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
316 pArena->base = 0;
318 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
319 * change the selector if we are shrinking the block.
320 * FIXME: shouldn't we keep selectors until the block is deleted?
322 SELECTOR_ReallocBlock( sel, 0, 1 );
323 return handle;
326 /* Fixup the size */
328 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
329 if (size == 0) size = 0x20;
330 else size = (size + 0x1f) & ~0x1f;
332 /* Change the flags */
334 if (flags & GMEM_MODIFY)
336 /* Change the flags, leaving GA_DGROUP alone */
337 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
338 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
339 return handle;
342 /* Reallocate the linear memory */
344 ptr = (void *)pArena->base;
345 oldsize = pArena->size;
346 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
347 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
349 if (pArena->flags & GA_DOSMEM)
351 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
352 newptr = ptr;
353 else if(pArena->pageLockCount > 0)
354 newptr = 0;
355 else
357 newptr = DOSMEM_GetBlock( size, 0 );
358 if (newptr)
360 memcpy( newptr, ptr, oldsize );
361 DOSMEM_FreeBlock( ptr );
365 else
368 * if more than one reader (e.g. some pointer has been
369 * given out by GetVDMPointer32W16),
370 * only try to realloc in place
372 newptr = HeapReAlloc( GetProcessHeap(),
373 (pArena->pageLockCount > 0) ?
374 HEAP_REALLOC_IN_PLACE_ONLY : 0,
375 ptr, size );
378 if (!newptr)
380 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
381 if (pArena->pageLockCount <1)
383 if (pArena->flags & GA_DOSMEM)
384 DOSMEM_FreeBlock( (void *)pArena->base );
385 else
386 HeapFree( GetProcessHeap(), 0, ptr );
387 SELECTOR_FreeBlock( sel );
388 memset( pArena, 0, sizeof(GLOBALARENA) );
390 return 0;
392 ptr = newptr;
394 /* Reallocate the selector(s) */
396 sel = SELECTOR_ReallocBlock( sel, ptr, size );
397 if (!sel)
399 if (pArena->flags & GA_DOSMEM)
400 DOSMEM_FreeBlock( (void *)pArena->base );
401 else
402 HeapFree( GetProcessHeap(), 0, ptr );
403 memset( pArena, 0, sizeof(GLOBALARENA) );
404 return 0;
406 selcount = (size + 0xffff) / 0x10000;
408 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
410 if (pArena->flags & GA_DOSMEM)
411 DOSMEM_FreeBlock( (void *)pArena->base );
412 else
413 HeapFree( GetProcessHeap(), 0, ptr );
414 SELECTOR_FreeBlock( sel );
415 return 0;
418 /* Fill the new arena block
419 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
421 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
422 pNewArena->base = (DWORD)ptr;
423 pNewArena->size = GetSelectorLimit16(sel) + 1;
424 pNewArena->selCount = selcount;
425 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
427 if (selcount > 1) /* clear the next arena blocks */
428 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
430 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
431 memset( (char *)ptr + oldsize, 0, size - oldsize );
432 return pNewArena->handle;
436 /***********************************************************************
437 * GlobalFree (KERNEL.17)
438 * GlobalFree16 (KERNEL32.31)
439 * RETURNS
440 * NULL: Success
441 * Handle: Failure
443 HGLOBAL16 WINAPI GlobalFree16(
444 HGLOBAL16 handle /* [in] Handle of global memory object */
446 void *ptr;
448 if (!VALID_HANDLE(handle)) {
449 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
450 return 0;
452 ptr = (void *)GET_ARENA_PTR(handle)->base;
454 TRACE("%04x\n", handle );
455 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
456 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
457 return 0;
461 /***********************************************************************
462 * GlobalLock (KERNEL.18)
464 * This is the GlobalLock16() function used by 16-bit code.
466 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
468 WORD sel = GlobalHandleToSel16( handle );
469 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
471 if (handle)
473 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
475 if (!VALID_HANDLE(handle)) {
476 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
477 sel = 0;
479 else if (!GET_ARENA_PTR(handle)->base)
480 sel = 0;
481 else
482 GET_ARENA_PTR(handle)->lockCount++;
485 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
486 return MAKESEGPTR( sel, 0 );
490 /**********************************************************************
491 * K32WOWGlobalLock16 (KERNEL32.60)
493 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
495 return WIN16_GlobalLock16( hMem );
499 /***********************************************************************
500 * GlobalLock16 (KERNEL32.25)
502 * This is the GlobalLock16() function used by 32-bit code.
504 * RETURNS
505 * Pointer to first byte of memory block
506 * NULL: Failure
508 LPVOID WINAPI GlobalLock16(
509 HGLOBAL16 handle /* [in] Handle of global memory object */
511 if (!handle) return 0;
512 if (!VALID_HANDLE(handle))
513 return 0;
514 GET_ARENA_PTR(handle)->lockCount++;
515 return (LPVOID)GET_ARENA_PTR(handle)->base;
519 /***********************************************************************
520 * GlobalUnlock (KERNEL.19)
521 * GlobalUnlock16 (KERNEL32.26)
522 * NOTES
523 * Should the return values be cast to booleans?
525 * RETURNS
526 * TRUE: Object is still locked
527 * FALSE: Object is unlocked
529 BOOL16 WINAPI GlobalUnlock16(
530 HGLOBAL16 handle /* [in] Handle of global memory object */
532 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
533 if (!VALID_HANDLE(handle)) {
534 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
535 return 0;
537 TRACE("%04x\n", handle );
538 if (pArena->lockCount) pArena->lockCount--;
539 return pArena->lockCount;
542 /***********************************************************************
543 * GlobalChangeLockCount (KERNEL.365)
545 * This is declared as a register function as it has to preserve
546 * *all* registers, even AX/DX !
549 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
550 CONTEXT86 *context )
552 if ( delta == 1 )
553 GlobalLock16( handle );
554 else if ( delta == -1 )
555 GlobalUnlock16( handle );
556 else
557 ERR("(%04X, %d): strange delta value\n", handle, delta );
560 /***********************************************************************
561 * GlobalSize (KERNEL.20)
562 * GlobalSize16 (KERNEL32.32)
563 * RETURNS
564 * Size in bytes of object
565 * 0: Failure
567 DWORD WINAPI GlobalSize16(
568 HGLOBAL16 handle /* [in] Handle of global memory object */
570 TRACE("%04x\n", handle );
571 if (!handle) return 0;
572 if (!VALID_HANDLE(handle))
573 return 0;
574 return GET_ARENA_PTR(handle)->size;
578 /***********************************************************************
579 * GlobalHandle (KERNEL.21)
580 * NOTES
581 * Why is GlobalHandleToSel used here with the sel as input?
583 * RETURNS
584 * Handle: Success
585 * NULL: Failure
587 DWORD WINAPI GlobalHandle16(
588 WORD sel /* [in] Address of global memory block */
590 TRACE("%04x\n", sel );
591 if (!VALID_HANDLE(sel)) {
592 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
593 return 0;
595 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
598 /***********************************************************************
599 * GlobalHandleNoRIP (KERNEL.159)
601 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
603 int i;
604 for (i = globalArenaSize-1 ; i>=0 ; i--) {
605 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
606 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
608 return 0;
612 /***********************************************************************
613 * GlobalFlags (KERNEL.22)
615 * NOTES
616 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
617 * handle?
619 * RETURNS
620 * Value specifying flags and lock count
621 * GMEM_INVALID_HANDLE: Invalid handle
623 UINT16 WINAPI GlobalFlags16(
624 HGLOBAL16 handle /* [in] Handle of global memory object */
626 GLOBALARENA *pArena;
628 TRACE("%04x\n", handle );
629 if (!VALID_HANDLE(handle)) {
630 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
631 return 0;
633 pArena = GET_ARENA_PTR(handle);
634 return pArena->lockCount |
635 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
636 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
640 /***********************************************************************
641 * LockSegment (KERNEL.23)
643 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
645 TRACE("%04x\n", handle );
646 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
647 if (!VALID_HANDLE(handle)) {
648 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
649 return 0;
651 GET_ARENA_PTR(handle)->lockCount++;
652 return handle;
656 /***********************************************************************
657 * UnlockSegment (KERNEL.24)
659 void WINAPI UnlockSegment16( HGLOBAL16 handle )
661 TRACE("%04x\n", handle );
662 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
663 if (!VALID_HANDLE(handle)) {
664 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
665 return;
667 GET_ARENA_PTR(handle)->lockCount--;
668 /* FIXME: this ought to return the lock count in CX (go figure...) */
672 /***********************************************************************
673 * GlobalCompact (KERNEL.25)
675 DWORD WINAPI GlobalCompact16( DWORD desired )
677 return GLOBAL_MAX_ALLOC_SIZE;
681 /***********************************************************************
682 * GlobalFreeAll (KERNEL.26)
684 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
686 DWORD i;
687 GLOBALARENA *pArena;
689 pArena = pGlobalArena;
690 for (i = 0; i < globalArenaSize; i++, pArena++)
692 if ((pArena->size != 0) && (pArena->hOwner == owner))
693 GlobalFree16( pArena->handle );
698 /***********************************************************************
699 * GlobalWire (KERNEL.111)
700 * GlobalWire16 (KERNEL32.29)
702 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
704 return WIN16_GlobalLock16( handle );
708 /***********************************************************************
709 * GlobalUnWire (KERNEL.112)
710 * GlobalUnWire16 (KERNEL32.30)
712 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
714 return !GlobalUnlock16( handle );
718 /***********************************************************************
719 * SetSwapAreaSize (KERNEL.106)
721 LONG WINAPI SetSwapAreaSize16( WORD size )
723 FIXME("(%d) - stub!\n", size );
724 return MAKELONG( size, 0xffff );
728 /***********************************************************************
729 * GlobalLRUOldest (KERNEL.163)
731 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
733 TRACE("%04x\n", handle );
734 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
735 return handle;
739 /***********************************************************************
740 * GlobalLRUNewest (KERNEL.164)
742 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
744 TRACE("%04x\n", handle );
745 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
746 return handle;
750 /***********************************************************************
751 * GetFreeSpace (KERNEL.169)
753 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
755 MEMORYSTATUS ms;
756 GlobalMemoryStatus( &ms );
757 return ms.dwAvailVirtual;
760 /***********************************************************************
761 * GlobalDOSAlloc (KERNEL.184)
762 * RETURNS
763 * Address (HW=Paragraph segment; LW=Selector)
765 DWORD WINAPI GlobalDOSAlloc16(
766 DWORD size /* [in] Number of bytes to be allocated */
768 UINT16 uParagraph;
769 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
771 if( lpBlock )
773 HMODULE16 hModule = GetModuleHandle16("KERNEL");
774 WORD wSelector;
775 GLOBALARENA *pArena;
777 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
778 pArena = GET_ARENA_PTR(wSelector);
779 pArena->flags |= GA_DOSMEM;
780 return MAKELONG(wSelector,uParagraph);
782 return 0;
786 /***********************************************************************
787 * GlobalDOSFree (KERNEL.185)
788 * RETURNS
789 * NULL: Success
790 * sel: Failure
792 WORD WINAPI GlobalDOSFree16(
793 WORD sel /* [in] Selector */
795 DWORD block = GetSelectorBase(sel);
797 if( block && block < 0x100000 )
799 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
800 if( DOSMEM_FreeBlock( lpBlock ) )
801 GLOBAL_FreeBlock( sel );
802 sel = 0;
804 return sel;
808 /***********************************************************************
809 * GlobalPageLock (KERNEL.191)
810 * GlobalSmartPageLock(KERNEL.230)
812 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
814 TRACE("%04x\n", handle );
815 if (!VALID_HANDLE(handle)) {
816 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
817 return 0;
819 return ++(GET_ARENA_PTR(handle)->pageLockCount);
823 /***********************************************************************
824 * GlobalPageUnlock (KERNEL.192)
825 * GlobalSmartPageUnlock(KERNEL.231)
827 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
829 TRACE("%04x\n", handle );
830 if (!VALID_HANDLE(handle)) {
831 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
832 return 0;
834 return --(GET_ARENA_PTR(handle)->pageLockCount);
838 /***********************************************************************
839 * GlobalFix (KERNEL.197)
840 * GlobalFix16 (KERNEL32.27)
842 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
844 TRACE("%04x\n", handle );
845 if (!VALID_HANDLE(handle)) {
846 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
847 return 0;
849 GET_ARENA_PTR(handle)->lockCount++;
851 return GlobalHandleToSel16(handle);
855 /***********************************************************************
856 * GlobalUnfix (KERNEL.198)
857 * GlobalUnfix16 (KERNEL32.28)
859 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
861 TRACE("%04x\n", handle );
862 if (!VALID_HANDLE(handle)) {
863 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
864 return;
866 GET_ARENA_PTR(handle)->lockCount--;
870 /***********************************************************************
871 * FarSetOwner (KERNEL.403)
873 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
875 if (!VALID_HANDLE(handle)) {
876 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
877 return;
879 GET_ARENA_PTR(handle)->hOwner = hOwner;
883 /***********************************************************************
884 * FarGetOwner (KERNEL.404)
886 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
888 if (!VALID_HANDLE(handle)) {
889 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
890 return 0;
892 return GET_ARENA_PTR(handle)->hOwner;
896 /***********************************************************************
897 * GlobalHandleToSel (TOOLHELP.50)
899 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
901 if (!handle) return 0;
902 if (!VALID_HANDLE(handle)) {
903 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
904 return 0;
906 if (!(handle & 7))
908 WARN("Program attempted invalid selector conversion\n" );
909 return handle - 1;
911 return handle | 7;
915 /***********************************************************************
916 * GlobalFirst (TOOLHELP.51)
918 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
920 if (wFlags == GLOBAL_LRU) return FALSE;
921 pGlobal->dwNext = 0;
922 return GlobalNext16( pGlobal, wFlags );
926 /***********************************************************************
927 * GlobalNext (TOOLHELP.52)
929 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
931 GLOBALARENA *pArena;
933 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
934 pArena = pGlobalArena + pGlobal->dwNext;
935 if (wFlags == GLOBAL_FREE) /* only free blocks */
937 int i;
938 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
939 if (pArena->size == 0) break; /* block is free */
940 if (i >= globalArenaSize) return FALSE;
941 pGlobal->dwNext = i;
944 pGlobal->dwAddress = pArena->base;
945 pGlobal->dwBlockSize = pArena->size;
946 pGlobal->hBlock = pArena->handle;
947 pGlobal->wcLock = pArena->lockCount;
948 pGlobal->wcPageLock = pArena->pageLockCount;
949 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
950 pGlobal->wHeapPresent = FALSE;
951 pGlobal->hOwner = pArena->hOwner;
952 pGlobal->wType = GT_UNKNOWN;
953 pGlobal->wData = 0;
954 pGlobal->dwNext++;
955 return TRUE;
959 /***********************************************************************
960 * GlobalInfo (TOOLHELP.53)
962 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
964 int i;
965 GLOBALARENA *pArena;
967 pInfo->wcItems = globalArenaSize;
968 pInfo->wcItemsFree = 0;
969 pInfo->wcItemsLRU = 0;
970 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
971 if (pArena->size == 0) pInfo->wcItemsFree++;
972 return TRUE;
976 /***********************************************************************
977 * GlobalEntryHandle (TOOLHELP.54)
979 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
981 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
983 pGlobal->dwAddress = pArena->base;
984 pGlobal->dwBlockSize = pArena->size;
985 pGlobal->hBlock = pArena->handle;
986 pGlobal->wcLock = pArena->lockCount;
987 pGlobal->wcPageLock = pArena->pageLockCount;
988 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
989 pGlobal->wHeapPresent = FALSE;
990 pGlobal->hOwner = pArena->hOwner;
991 pGlobal->wType = GT_UNKNOWN;
992 pGlobal->wData = 0;
993 pGlobal->dwNext++;
994 return TRUE;
998 /***********************************************************************
999 * GlobalEntryModule (TOOLHELP.55)
1001 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1002 WORD wSeg )
1004 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1005 return FALSE;
1009 /***********************************************************************
1010 * MemManInfo (TOOLHELP.72)
1012 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1014 MEMORYSTATUS status;
1017 * Not unsurprisingly although the documention says you
1018 * _must_ provide the size in the dwSize field, this function
1019 * (under Windows) always fills the structure and returns true.
1021 GlobalMemoryStatus( &status );
1022 info->wPageSize = getpagesize();
1023 info->dwLargestFreeBlock = status.dwAvailVirtual;
1024 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1025 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1026 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1027 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1028 info->dwFreePages = info->dwMaxPagesAvailable;
1029 info->dwTotalPages = info->dwTotalLinearSpace;
1030 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1031 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1032 return TRUE;
1035 /***********************************************************************
1036 * GetFreeMemInfo (KERNEL.316)
1038 DWORD WINAPI GetFreeMemInfo16(void)
1040 MEMMANINFO info;
1041 MemManInfo16( &info );
1042 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1046 * Win32 Global heap functions (GlobalXXX).
1047 * These functions included in Win32 for compatibility with 16 bit Windows
1048 * Especially the moveable blocks and handles are oldish.
1049 * But the ability to directly allocate memory with GPTR and LPTR is widely
1050 * used.
1052 * The handle stuff looks horrible, but it's implemented almost like Win95
1053 * does it.
1057 #define MAGIC_GLOBAL_USED 0x5342
1058 #define GLOBAL_LOCK_MAX 0xFF
1059 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1060 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1061 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
1062 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
1063 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
1064 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
1065 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
1066 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
1067 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
1068 * the output jpeg's > 1 MB if not */
1069 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
1071 typedef struct __GLOBAL32_INTERN
1073 WORD Magic;
1074 LPVOID Pointer WINE_PACKED;
1075 BYTE Flags;
1076 BYTE LockCount;
1077 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1080 /***********************************************************************
1081 * GlobalAlloc (KERNEL32.@)
1082 * RETURNS
1083 * Handle: Success
1084 * NULL: Failure
1086 HGLOBAL WINAPI GlobalAlloc(
1087 UINT flags, /* [in] Object allocation attributes */
1088 SIZE_T size /* [in] Number of bytes to allocate */
1090 PGLOBAL32_INTERN pintern;
1091 DWORD hpflags;
1092 LPVOID palloc;
1094 if(flags&GMEM_ZEROINIT)
1095 hpflags=HEAP_ZERO_MEMORY;
1096 else
1097 hpflags=0;
1099 TRACE("() flags=%04x\n", flags );
1101 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1103 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1104 return (HGLOBAL) palloc;
1106 else /* HANDLE */
1108 RtlLockHeap(GetProcessHeap());
1110 pintern = HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1111 if (pintern)
1113 pintern->Magic = MAGIC_GLOBAL_USED;
1114 pintern->Flags = flags >> 8;
1115 pintern->LockCount = 0;
1117 if (size)
1119 palloc = HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE);
1120 if (!palloc)
1122 HeapFree(GetProcessHeap(), 0, pintern);
1123 pintern = NULL;
1125 else
1127 *(HGLOBAL *)palloc = INTERN_TO_HANDLE(pintern);
1128 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
1131 else
1132 pintern->Pointer = NULL;
1135 RtlUnlockHeap(GetProcessHeap());
1136 return pintern ? INTERN_TO_HANDLE(pintern) : 0;
1141 /***********************************************************************
1142 * GlobalLock (KERNEL32.@)
1143 * RETURNS
1144 * Pointer to first byte of block
1145 * NULL: Failure
1147 LPVOID WINAPI GlobalLock(
1148 HGLOBAL hmem /* [in] Handle of global memory object */
1151 PGLOBAL32_INTERN pintern;
1152 LPVOID palloc;
1154 if (ISPOINTER(hmem))
1155 return IsBadReadPtr(hmem, 1) ? NULL : hmem;
1157 RtlLockHeap(GetProcessHeap());
1158 __TRY
1160 pintern = HANDLE_TO_INTERN(hmem);
1161 if (pintern->Magic == MAGIC_GLOBAL_USED)
1163 if (pintern->LockCount < GLOBAL_LOCK_MAX)
1164 pintern->LockCount++;
1165 palloc = pintern->Pointer;
1167 else
1169 WARN("invalid handle %p\n", hmem);
1170 palloc = NULL;
1171 SetLastError(ERROR_INVALID_HANDLE);
1174 __EXCEPT(page_fault)
1176 WARN("page fault on %p\n", hmem);
1177 palloc = NULL;
1178 SetLastError(ERROR_INVALID_HANDLE);
1180 __ENDTRY
1181 RtlUnlockHeap(GetProcessHeap());
1182 return palloc;
1186 /***********************************************************************
1187 * GlobalUnlock (KERNEL32.@)
1188 * RETURNS
1189 * TRUE: Object is still locked
1190 * FALSE: Object is unlocked
1192 BOOL WINAPI GlobalUnlock(
1193 HGLOBAL hmem /* [in] Handle of global memory object */
1195 PGLOBAL32_INTERN pintern;
1196 BOOL locked;
1198 if (ISPOINTER(hmem)) return FALSE;
1200 RtlLockHeap(GetProcessHeap());
1201 __TRY
1203 pintern=HANDLE_TO_INTERN(hmem);
1204 if(pintern->Magic==MAGIC_GLOBAL_USED)
1206 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1207 pintern->LockCount--;
1209 locked = (pintern->LockCount != 0);
1210 if (!locked) SetLastError(NO_ERROR);
1212 else
1214 WARN("invalid handle\n");
1215 SetLastError(ERROR_INVALID_HANDLE);
1216 locked=FALSE;
1219 __EXCEPT(page_fault)
1221 ERR("page fault occurred ! Caused by bug ?\n");
1222 SetLastError( ERROR_INVALID_PARAMETER );
1223 locked=FALSE;
1225 __ENDTRY
1226 RtlUnlockHeap(GetProcessHeap());
1227 return locked;
1231 /***********************************************************************
1232 * GlobalHandle (KERNEL32.@)
1233 * Returns the handle associated with the specified pointer.
1235 * RETURNS
1236 * Handle: Success
1237 * NULL: Failure
1239 HGLOBAL WINAPI GlobalHandle(
1240 LPCVOID pmem /* [in] Pointer to global memory block */
1242 HGLOBAL handle;
1243 PGLOBAL32_INTERN maybe_intern;
1244 LPCVOID test;
1246 if (!pmem)
1248 SetLastError( ERROR_INVALID_PARAMETER );
1249 return 0;
1252 RtlLockHeap(GetProcessHeap());
1253 __TRY
1255 handle = 0;
1257 /* note that if pmem is a pointer to a a block allocated by */
1258 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1259 /* will fail. */
1260 if (ISPOINTER(pmem)) {
1261 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
1262 handle = (HGLOBAL)pmem; /* valid fixed block */
1263 break;
1265 handle = POINTER_TO_HANDLE(pmem);
1266 } else
1267 handle = (HGLOBAL)pmem;
1269 /* Now test handle either passed in or retrieved from pointer */
1270 maybe_intern = HANDLE_TO_INTERN( handle );
1271 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1272 test = maybe_intern->Pointer;
1273 if (HeapValidate( GetProcessHeap(), 0, (char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
1274 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
1275 break; /* valid moveable block */
1277 handle = 0;
1278 SetLastError( ERROR_INVALID_HANDLE );
1280 __EXCEPT(page_fault)
1282 SetLastError( ERROR_INVALID_HANDLE );
1283 handle = 0;
1285 __ENDTRY
1286 RtlUnlockHeap(GetProcessHeap());
1288 return handle;
1292 /***********************************************************************
1293 * GlobalReAlloc (KERNEL32.@)
1294 * RETURNS
1295 * Handle: Success
1296 * NULL: Failure
1298 HGLOBAL WINAPI GlobalReAlloc(
1299 HGLOBAL hmem, /* [in] Handle of global memory object */
1300 SIZE_T size, /* [in] New size of block */
1301 UINT flags /* [in] How to reallocate object */
1303 LPVOID palloc;
1304 HGLOBAL hnew;
1305 PGLOBAL32_INTERN pintern;
1306 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1308 hnew = 0;
1309 RtlLockHeap(GetProcessHeap());
1310 if(flags & GMEM_MODIFY) /* modify flags */
1312 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1314 /* make a fixed block moveable
1315 * actually only NT is able to do this. But it's soo simple
1317 if (hmem == 0)
1319 WARN("GlobalReAlloc with null handle!\n");
1320 SetLastError( ERROR_NOACCESS );
1321 hnew = 0;
1323 else
1325 size = HeapSize(GetProcessHeap(), 0, (LPVOID)hmem);
1326 hnew = GlobalAlloc(flags, size);
1327 palloc = GlobalLock(hnew);
1328 memcpy(palloc, (LPVOID)hmem, size);
1329 GlobalUnlock(hnew);
1330 GlobalFree(hmem);
1333 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1335 /* change the flags to make our block "discardable" */
1336 pintern=HANDLE_TO_INTERN(hmem);
1337 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1338 hnew=hmem;
1340 else
1342 SetLastError(ERROR_INVALID_PARAMETER);
1343 hnew = 0;
1346 else
1348 if(ISPOINTER(hmem))
1350 /* reallocate fixed memory */
1351 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
1353 else
1355 /* reallocate a moveable block */
1356 pintern=HANDLE_TO_INTERN(hmem);
1358 #if 0
1359 /* Apparently Windows doesn't care whether the handle is locked at this point */
1360 /* See also the same comment in GlobalFree() */
1361 if(pintern->LockCount>1) {
1362 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1363 SetLastError(ERROR_INVALID_HANDLE);
1364 } else
1365 #endif
1366 if(size!=0)
1368 hnew=hmem;
1369 if(pintern->Pointer)
1371 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
1372 (char *) pintern->Pointer-HGLOBAL_STORAGE,
1373 size+HGLOBAL_STORAGE)) == NULL)
1374 hnew = 0; /* Block still valid */
1375 else
1376 pintern->Pointer = (char *)palloc+HGLOBAL_STORAGE;
1378 else
1380 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
1381 == NULL)
1382 hnew = 0;
1383 else
1385 *(HGLOBAL *)palloc = hmem;
1386 pintern->Pointer = (char *)palloc + HGLOBAL_STORAGE;
1390 else
1392 if(pintern->Pointer)
1394 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
1395 pintern->Pointer=NULL;
1400 RtlUnlockHeap(GetProcessHeap());
1401 return hnew;
1405 /***********************************************************************
1406 * GlobalFree (KERNEL32.@)
1407 * RETURNS
1408 * NULL: Success
1409 * Handle: Failure
1411 HGLOBAL WINAPI GlobalFree(
1412 HGLOBAL hmem /* [in] Handle of global memory object */
1414 PGLOBAL32_INTERN pintern;
1415 HGLOBAL hreturned;
1417 RtlLockHeap(GetProcessHeap());
1418 __TRY
1420 hreturned = 0;
1421 if(ISPOINTER(hmem)) /* POINTER */
1423 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1425 else /* HANDLE */
1427 pintern=HANDLE_TO_INTERN(hmem);
1429 if(pintern->Magic==MAGIC_GLOBAL_USED)
1432 /* WIN98 does not make this test. That is you can free a */
1433 /* block you have not unlocked. Go figure!! */
1434 /* if(pintern->LockCount!=0) */
1435 /* SetLastError(ERROR_INVALID_HANDLE); */
1437 if(pintern->Pointer)
1438 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
1439 hreturned=hmem;
1440 if(!HeapFree(GetProcessHeap(), 0, pintern))
1441 hreturned=hmem;
1445 __EXCEPT(page_fault)
1447 ERR("page fault occurred ! Caused by bug ?\n");
1448 SetLastError( ERROR_INVALID_PARAMETER );
1449 hreturned = hmem;
1451 __ENDTRY
1452 RtlUnlockHeap(GetProcessHeap());
1453 return hreturned;
1457 /***********************************************************************
1458 * GlobalSize (KERNEL32.@)
1459 * RETURNS
1460 * Size in bytes of the global memory object
1461 * 0: Failure
1463 SIZE_T WINAPI GlobalSize(
1464 HGLOBAL hmem /* [in] Handle of global memory object */
1466 DWORD retval;
1467 PGLOBAL32_INTERN pintern;
1469 if (!hmem) return 0;
1471 if(ISPOINTER(hmem))
1473 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1475 else
1477 RtlLockHeap(GetProcessHeap());
1478 pintern=HANDLE_TO_INTERN(hmem);
1480 if(pintern->Magic==MAGIC_GLOBAL_USED)
1482 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1483 retval = 0;
1484 else
1486 retval = HeapSize(GetProcessHeap(), 0,
1487 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
1488 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
1491 else
1493 WARN("invalid handle\n");
1494 retval=0;
1496 RtlUnlockHeap(GetProcessHeap());
1498 /* HeapSize returns 0xffffffff on failure */
1499 if (retval == 0xffffffff) retval = 0;
1500 return retval;
1504 /***********************************************************************
1505 * GlobalWire (KERNEL32.@)
1507 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1509 return GlobalLock( hmem );
1513 /***********************************************************************
1514 * GlobalUnWire (KERNEL32.@)
1516 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1518 return GlobalUnlock( hmem);
1522 /***********************************************************************
1523 * GlobalFix (KERNEL32.@)
1525 VOID WINAPI GlobalFix(HGLOBAL hmem)
1527 GlobalLock( hmem );
1531 /***********************************************************************
1532 * GlobalUnfix (KERNEL32.@)
1534 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1536 GlobalUnlock( hmem);
1540 /***********************************************************************
1541 * GlobalFlags (KERNEL32.@)
1542 * Returns information about the specified global memory object
1544 * NOTES
1545 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1547 * RETURNS
1548 * Value specifying allocation flags and lock count
1549 * GMEM_INVALID_HANDLE: Failure
1551 UINT WINAPI GlobalFlags(
1552 HGLOBAL hmem /* [in] Handle to global memory object */
1554 DWORD retval;
1555 PGLOBAL32_INTERN pintern;
1557 if(ISPOINTER(hmem))
1559 retval=0;
1561 else
1563 RtlLockHeap(GetProcessHeap());
1564 pintern=HANDLE_TO_INTERN(hmem);
1565 if(pintern->Magic==MAGIC_GLOBAL_USED)
1567 retval=pintern->LockCount + (pintern->Flags<<8);
1568 if(pintern->Pointer==0)
1569 retval|= GMEM_DISCARDED;
1571 else
1573 WARN("Invalid handle: %p\n", hmem);
1574 retval=0;
1576 RtlUnlockHeap(GetProcessHeap());
1578 return retval;
1582 /***********************************************************************
1583 * GlobalCompact (KERNEL32.@)
1585 SIZE_T WINAPI GlobalCompact( DWORD minfree )
1587 return 0; /* GlobalCompact does nothing in Win32 */
1591 /***********************************************************************
1592 * GlobalMemoryStatus (KERNEL32.@)
1593 * Provides information about the status of the memory, so apps can tell
1594 * roughly how much they are able to allocate
1596 * RETURNS
1597 * None
1599 VOID WINAPI GlobalMemoryStatus(
1600 LPMEMORYSTATUS lpmem
1602 static MEMORYSTATUS cached_memstatus;
1603 static int cache_lastchecked = 0;
1604 SYSTEM_INFO si;
1605 #ifdef linux
1606 FILE *f;
1607 #endif
1608 #if defined(__FreeBSD__) || defined(__NetBSD__)
1609 int *tmp;
1610 int size_sys;
1611 int mib[2] = { CTL_HW };
1612 #endif
1613 if (time(NULL)==cache_lastchecked) {
1614 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1615 return;
1617 cache_lastchecked = time(NULL);
1619 lpmem->dwMemoryLoad = 0;
1620 lpmem->dwTotalPhys = 16*1024*1024;
1621 lpmem->dwAvailPhys = 16*1024*1024;
1622 lpmem->dwTotalPageFile = 16*1024*1024;
1623 lpmem->dwAvailPageFile = 16*1024*1024;
1625 #ifdef linux
1626 f = fopen( "/proc/meminfo", "r" );
1627 if (f)
1629 char buffer[256];
1630 int total, used, free, shared, buffers, cached;
1632 lpmem->dwLength = sizeof(MEMORYSTATUS);
1633 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1634 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1635 while (fgets( buffer, sizeof(buffer), f ))
1637 /* old style /proc/meminfo ... */
1638 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1640 lpmem->dwTotalPhys += total;
1641 lpmem->dwAvailPhys += free + buffers + cached;
1643 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1645 lpmem->dwTotalPageFile += total;
1646 lpmem->dwAvailPageFile += free;
1649 /* new style /proc/meminfo ... */
1650 if (sscanf(buffer, "MemTotal: %d", &total))
1651 lpmem->dwTotalPhys = total*1024;
1652 if (sscanf(buffer, "MemFree: %d", &free))
1653 lpmem->dwAvailPhys = free*1024;
1654 if (sscanf(buffer, "SwapTotal: %d", &total))
1655 lpmem->dwTotalPageFile = total*1024;
1656 if (sscanf(buffer, "SwapFree: %d", &free))
1657 lpmem->dwAvailPageFile = free*1024;
1658 if (sscanf(buffer, "Buffers: %d", &buffers))
1659 lpmem->dwAvailPhys += buffers*1024;
1660 if (sscanf(buffer, "Cached: %d", &cached))
1661 lpmem->dwAvailPhys += cached*1024;
1663 fclose( f );
1665 if (lpmem->dwTotalPhys)
1667 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1668 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1669 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1670 / (TotalPhysical / 100);
1673 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1674 mib[1] = HW_PHYSMEM;
1675 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1676 tmp = malloc(size_sys * sizeof(int));
1677 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1678 if (tmp && *tmp)
1680 lpmem->dwTotalPhys = *tmp;
1681 free(tmp);
1682 mib[1] = HW_USERMEM;
1683 sysctl(mib, 2, NULL, &size_sys, NULL, 0);
1684 tmp = malloc(size_sys * sizeof(int));
1685 sysctl(mib, 2, tmp, &size_sys, NULL, 0);
1686 if (tmp && *tmp)
1688 lpmem->dwAvailPhys = *tmp;
1689 lpmem->dwTotalPageFile = *tmp;
1690 lpmem->dwAvailPageFile = *tmp;
1691 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1692 } else
1694 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1695 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1696 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1697 lpmem->dwMemoryLoad = 0;
1699 free(tmp);
1702 #endif
1703 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1704 * is more than 2GB of physical memory.
1706 if (lpmem->dwTotalPhys>2U*1024*1024*1024)
1708 lpmem->dwTotalPhys=2U*1024*1024*1024;
1709 lpmem->dwAvailPhys=2U*1024*1024*1024;
1712 /* FIXME: should do something for other systems */
1713 GetSystemInfo(&si);
1714 lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1715 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1716 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1717 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1719 /* it appears some memory display programs want to divide by these values */
1720 if(lpmem->dwTotalPageFile==0)
1721 lpmem->dwTotalPageFile++;
1723 if(lpmem->dwAvailPageFile==0)
1724 lpmem->dwAvailPageFile++;
1726 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1727 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1728 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1729 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1730 lpmem->dwAvailVirtual);
1733 /***********************************************************************
1734 * GlobalMemoryStatusEx (KERNEL32.@)
1735 * A version of GlobalMemoryStatus that can deal with memory over 4GB
1737 * RETURNS
1738 * None
1740 BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer ) {
1741 MEMORYSTATUS memstatus;
1743 /* Because GlobalMemoryStatusEx is identical to GlobalMemoryStatus save
1744 for one extra field in the struct, and the lack of a bug, we simply
1745 call GlobalMemoryStatus and copy the values across. */
1746 FIXME("we should emulate the 4GB bug here, as per MSDN\n");
1747 GlobalMemoryStatus(&memstatus);
1748 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
1749 lpBuffer->ullTotalPhys = memstatus.dwTotalPhys;
1750 lpBuffer->ullAvailPhys = memstatus.dwAvailPhys;
1751 lpBuffer->ullTotalPageFile = memstatus.dwTotalPageFile;
1752 lpBuffer->ullAvailPageFile = memstatus.dwAvailPageFile;
1753 lpBuffer->ullTotalVirtual = memstatus.dwTotalVirtual;
1754 lpBuffer->ullAvailVirtual = memstatus.dwAvailVirtual;
1755 /* MSDN says about AvailExtendedVirtual: Size of unreserved and uncommitted
1756 memory in the extended portion of the virtual address space of the calling
1757 process, in bytes.
1759 However, I don't know what this means, so set it to zero :(
1761 lpBuffer->ullAvailExtendedVirtual = 0;
1762 return 1;
1765 /***********************************************************************
1766 * A20Proc (KERNEL.165)
1767 * A20_Proc (SYSTEM.20)
1769 void WINAPI A20Proc16( WORD unused )
1771 /* this is also a NOP in Windows */
1774 /***********************************************************************
1775 * LimitEMSPages (KERNEL.156)
1777 DWORD WINAPI LimitEMSPages16( DWORD unused )
1779 return 0;