Fixed strict aliasing problem in
[wine/multimedia.git] / memory / global.c
blob57a4306eda6b1deff35856c5d7b92ba7df9c5309
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 "global.h"
43 #include "toolhelp.h"
44 #include "selectors.h"
45 #include "miscemu.h"
46 #include "stackframe.h"
47 #include "module.h"
48 #include "wine/debug.h"
49 #include "winerror.h"
50 #include "msvcrt/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 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
313 pArena->base = 0;
315 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
316 * change the selector if we are shrinking the block.
317 * FIXME: shouldn't we keep selectors until the block is deleted?
319 SELECTOR_ReallocBlock( sel, 0, 1 );
320 return handle;
323 /* Fixup the size */
325 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
326 if (size == 0) size = 0x20;
327 else size = (size + 0x1f) & ~0x1f;
329 /* Change the flags */
331 if (flags & GMEM_MODIFY)
333 /* Change the flags, leaving GA_DGROUP alone */
334 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
335 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
336 return handle;
339 /* Reallocate the linear memory */
341 ptr = (void *)pArena->base;
342 oldsize = pArena->size;
343 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
344 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
346 if (pArena->flags & GA_DOSMEM)
347 newptr = DOSMEM_ResizeBlock(ptr, size, NULL);
348 else
349 /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16),
350 only try to realloc in place */
351 newptr = HeapReAlloc( GetProcessHeap(),
352 (pArena->pageLockCount > 0)?HEAP_REALLOC_IN_PLACE_ONLY:0, ptr, size );
353 if (!newptr)
355 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
356 if (pArena->pageLockCount <1)
358 HeapFree( GetProcessHeap(), 0, ptr );
359 SELECTOR_FreeBlock( sel );
360 memset( pArena, 0, sizeof(GLOBALARENA) );
362 return 0;
364 ptr = newptr;
366 /* Reallocate the selector(s) */
368 sel = SELECTOR_ReallocBlock( sel, ptr, size );
369 if (!sel)
371 HeapFree( GetProcessHeap(), 0, ptr );
372 memset( pArena, 0, sizeof(GLOBALARENA) );
373 return 0;
375 selcount = (size + 0xffff) / 0x10000;
377 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
379 HeapFree( GetProcessHeap(), 0, ptr );
380 SELECTOR_FreeBlock( sel );
381 return 0;
384 /* Fill the new arena block
385 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
387 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
388 pNewArena->base = (DWORD)ptr;
389 pNewArena->size = GetSelectorLimit16(sel) + 1;
390 pNewArena->selCount = selcount;
391 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
393 if (selcount > 1) /* clear the next arena blocks */
394 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
396 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
397 memset( (char *)ptr + oldsize, 0, size - oldsize );
398 return pNewArena->handle;
402 /***********************************************************************
403 * GlobalFree (KERNEL.17)
404 * GlobalFree16 (KERNEL32.31)
405 * RETURNS
406 * NULL: Success
407 * Handle: Failure
409 HGLOBAL16 WINAPI GlobalFree16(
410 HGLOBAL16 handle /* [in] Handle of global memory object */
412 void *ptr;
414 if (!VALID_HANDLE(handle)) {
415 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
416 return 0;
418 ptr = (void *)GET_ARENA_PTR(handle)->base;
420 TRACE("%04x\n", handle );
421 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
422 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
423 return 0;
427 /***********************************************************************
428 * GlobalLock (KERNEL.18)
430 * This is the GlobalLock16() function used by 16-bit code.
432 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
434 WORD sel = GlobalHandleToSel16( handle );
435 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
437 if (handle)
439 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
441 if (!VALID_HANDLE(handle)) {
442 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
443 sel = 0;
445 else if (!GET_ARENA_PTR(handle)->base)
446 sel = 0;
447 else
448 GET_ARENA_PTR(handle)->lockCount++;
451 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
452 return MAKESEGPTR( sel, 0 );
456 /**********************************************************************
457 * K32WOWGlobalLock16 (KERNEL32.60)
459 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
461 return WIN16_GlobalLock16( hMem );
465 /***********************************************************************
466 * GlobalLock16 (KERNEL32.25)
468 * This is the GlobalLock16() function used by 32-bit code.
470 * RETURNS
471 * Pointer to first byte of memory block
472 * NULL: Failure
474 LPVOID WINAPI GlobalLock16(
475 HGLOBAL16 handle /* [in] Handle of global memory object */
477 if (!handle) return 0;
478 if (!VALID_HANDLE(handle))
479 return (LPVOID)0;
480 GET_ARENA_PTR(handle)->lockCount++;
481 return (LPVOID)GET_ARENA_PTR(handle)->base;
485 /***********************************************************************
486 * GlobalUnlock (KERNEL.19)
487 * GlobalUnlock16 (KERNEL32.26)
488 * NOTES
489 * Should the return values be cast to booleans?
491 * RETURNS
492 * TRUE: Object is still locked
493 * FALSE: Object is unlocked
495 BOOL16 WINAPI GlobalUnlock16(
496 HGLOBAL16 handle /* [in] Handle of global memory object */
498 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
499 if (!VALID_HANDLE(handle)) {
500 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
501 return 0;
503 TRACE("%04x\n", handle );
504 if (pArena->lockCount) pArena->lockCount--;
505 return pArena->lockCount;
508 /***********************************************************************
509 * GlobalChangeLockCount (KERNEL.365)
511 * This is declared as a register function as it has to preserve
512 * *all* registers, even AX/DX !
515 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
516 CONTEXT86 *context )
518 if ( delta == 1 )
519 GlobalLock16( handle );
520 else if ( delta == -1 )
521 GlobalUnlock16( handle );
522 else
523 ERR("(%04X, %d): strange delta value\n", handle, delta );
526 /***********************************************************************
527 * GlobalSize (KERNEL.20)
528 * GlobalSize16 (KERNEL32.32)
529 * RETURNS
530 * Size in bytes of object
531 * 0: Failure
533 DWORD WINAPI GlobalSize16(
534 HGLOBAL16 handle /* [in] Handle of global memory object */
536 TRACE("%04x\n", handle );
537 if (!handle) return 0;
538 if (!VALID_HANDLE(handle))
539 return 0;
540 return GET_ARENA_PTR(handle)->size;
544 /***********************************************************************
545 * GlobalHandle (KERNEL.21)
546 * NOTES
547 * Why is GlobalHandleToSel used here with the sel as input?
549 * RETURNS
550 * Handle: Success
551 * NULL: Failure
553 DWORD WINAPI GlobalHandle16(
554 WORD sel /* [in] Address of global memory block */
556 TRACE("%04x\n", sel );
557 if (!VALID_HANDLE(sel)) {
558 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
559 return 0;
561 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
564 /***********************************************************************
565 * GlobalHandleNoRIP (KERNEL.159)
567 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
569 int i;
570 for (i = globalArenaSize-1 ; i>=0 ; i--) {
571 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
572 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
574 return 0;
578 /***********************************************************************
579 * GlobalFlags (KERNEL.22)
581 * NOTES
582 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
583 * handle?
585 * RETURNS
586 * Value specifying flags and lock count
587 * GMEM_INVALID_HANDLE: Invalid handle
589 UINT16 WINAPI GlobalFlags16(
590 HGLOBAL16 handle /* [in] Handle of global memory object */
592 GLOBALARENA *pArena;
594 TRACE("%04x\n", handle );
595 if (!VALID_HANDLE(handle)) {
596 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
597 return 0;
599 pArena = GET_ARENA_PTR(handle);
600 return pArena->lockCount |
601 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
602 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
606 /***********************************************************************
607 * LockSegment (KERNEL.23)
609 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
611 TRACE("%04x\n", handle );
612 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
613 if (!VALID_HANDLE(handle)) {
614 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
615 return 0;
617 GET_ARENA_PTR(handle)->lockCount++;
618 return handle;
622 /***********************************************************************
623 * UnlockSegment (KERNEL.24)
625 void WINAPI UnlockSegment16( HGLOBAL16 handle )
627 TRACE("%04x\n", handle );
628 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
629 if (!VALID_HANDLE(handle)) {
630 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
631 return;
633 GET_ARENA_PTR(handle)->lockCount--;
634 /* FIXME: this ought to return the lock count in CX (go figure...) */
638 /***********************************************************************
639 * GlobalCompact (KERNEL.25)
641 DWORD WINAPI GlobalCompact16( DWORD desired )
643 return GLOBAL_MAX_ALLOC_SIZE;
647 /***********************************************************************
648 * GlobalFreeAll (KERNEL.26)
650 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
652 DWORD i;
653 GLOBALARENA *pArena;
655 pArena = pGlobalArena;
656 for (i = 0; i < globalArenaSize; i++, pArena++)
658 if ((pArena->size != 0) && (pArena->hOwner == owner))
659 GlobalFree16( pArena->handle );
664 /***********************************************************************
665 * GlobalWire (KERNEL.111)
666 * GlobalWire16 (KERNEL32.29)
668 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
670 return WIN16_GlobalLock16( handle );
674 /***********************************************************************
675 * GlobalUnWire (KERNEL.112)
676 * GlobalUnWire16 (KERNEL32.30)
678 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
680 return !GlobalUnlock16( handle );
684 /***********************************************************************
685 * SetSwapAreaSize (KERNEL.106)
687 LONG WINAPI SetSwapAreaSize16( WORD size )
689 FIXME("(%d) - stub!\n", size );
690 return MAKELONG( size, 0xffff );
694 /***********************************************************************
695 * GlobalLRUOldest (KERNEL.163)
697 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
699 TRACE("%04x\n", handle );
700 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
701 return handle;
705 /***********************************************************************
706 * GlobalLRUNewest (KERNEL.164)
708 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
710 TRACE("%04x\n", handle );
711 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
712 return handle;
716 /***********************************************************************
717 * GetFreeSpace (KERNEL.169)
719 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
721 MEMORYSTATUS ms;
722 GlobalMemoryStatus( &ms );
723 return ms.dwAvailVirtual;
726 /***********************************************************************
727 * GlobalDOSAlloc (KERNEL.184)
728 * RETURNS
729 * Address (HW=Paragraph segment; LW=Selector)
731 DWORD WINAPI GlobalDOSAlloc16(
732 DWORD size /* [in] Number of bytes to be allocated */
734 UINT16 uParagraph;
735 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
737 if( lpBlock )
739 HMODULE16 hModule = GetModuleHandle16("KERNEL");
740 WORD wSelector;
741 GLOBALARENA *pArena;
743 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
744 pArena = GET_ARENA_PTR(wSelector);
745 pArena->flags |= GA_DOSMEM;
746 return MAKELONG(wSelector,uParagraph);
748 return 0;
752 /***********************************************************************
753 * GlobalDOSFree (KERNEL.185)
754 * RETURNS
755 * NULL: Success
756 * sel: Failure
758 WORD WINAPI GlobalDOSFree16(
759 WORD sel /* [in] Selector */
761 DWORD block = GetSelectorBase(sel);
763 if( block && block < 0x100000 )
765 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
766 if( DOSMEM_FreeBlock( lpBlock ) )
767 GLOBAL_FreeBlock( sel );
768 sel = 0;
770 return sel;
774 /***********************************************************************
775 * GlobalPageLock (KERNEL.191)
776 * GlobalSmartPageLock(KERNEL.230)
778 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
780 TRACE("%04x\n", handle );
781 if (!VALID_HANDLE(handle)) {
782 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
783 return 0;
785 return ++(GET_ARENA_PTR(handle)->pageLockCount);
789 /***********************************************************************
790 * GlobalPageUnlock (KERNEL.192)
791 * GlobalSmartPageUnlock(KERNEL.231)
793 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
795 TRACE("%04x\n", handle );
796 if (!VALID_HANDLE(handle)) {
797 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
798 return 0;
800 return --(GET_ARENA_PTR(handle)->pageLockCount);
804 /***********************************************************************
805 * GlobalFix (KERNEL.197)
806 * GlobalFix16 (KERNEL32.27)
808 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
810 TRACE("%04x\n", handle );
811 if (!VALID_HANDLE(handle)) {
812 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
813 return 0;
815 GET_ARENA_PTR(handle)->lockCount++;
817 return GlobalHandleToSel16(handle);
821 /***********************************************************************
822 * GlobalUnfix (KERNEL.198)
823 * GlobalUnfix16 (KERNEL32.28)
825 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
827 TRACE("%04x\n", handle );
828 if (!VALID_HANDLE(handle)) {
829 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
830 return;
832 GET_ARENA_PTR(handle)->lockCount--;
836 /***********************************************************************
837 * FarSetOwner (KERNEL.403)
839 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
841 if (!VALID_HANDLE(handle)) {
842 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
843 return;
845 GET_ARENA_PTR(handle)->hOwner = hOwner;
849 /***********************************************************************
850 * FarGetOwner (KERNEL.404)
852 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
854 if (!VALID_HANDLE(handle)) {
855 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
856 return 0;
858 return GET_ARENA_PTR(handle)->hOwner;
862 /***********************************************************************
863 * GlobalHandleToSel (TOOLHELP.50)
865 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
867 if (!handle) return 0;
868 if (!VALID_HANDLE(handle)) {
869 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
870 return 0;
872 if (!(handle & 7))
874 WARN("Program attempted invalid selector conversion\n" );
875 return handle - 1;
877 return handle | 7;
881 /***********************************************************************
882 * GlobalFirst (TOOLHELP.51)
884 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
886 if (wFlags == GLOBAL_LRU) return FALSE;
887 pGlobal->dwNext = 0;
888 return GlobalNext16( pGlobal, wFlags );
892 /***********************************************************************
893 * GlobalNext (TOOLHELP.52)
895 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
897 GLOBALARENA *pArena;
899 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
900 pArena = pGlobalArena + pGlobal->dwNext;
901 if (wFlags == GLOBAL_FREE) /* only free blocks */
903 int i;
904 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
905 if (pArena->size == 0) break; /* block is free */
906 if (i >= globalArenaSize) return FALSE;
907 pGlobal->dwNext = i;
910 pGlobal->dwAddress = pArena->base;
911 pGlobal->dwBlockSize = pArena->size;
912 pGlobal->hBlock = pArena->handle;
913 pGlobal->wcLock = pArena->lockCount;
914 pGlobal->wcPageLock = pArena->pageLockCount;
915 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
916 pGlobal->wHeapPresent = FALSE;
917 pGlobal->hOwner = pArena->hOwner;
918 pGlobal->wType = GT_UNKNOWN;
919 pGlobal->wData = 0;
920 pGlobal->dwNext++;
921 return TRUE;
925 /***********************************************************************
926 * GlobalInfo (TOOLHELP.53)
928 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
930 int i;
931 GLOBALARENA *pArena;
933 pInfo->wcItems = globalArenaSize;
934 pInfo->wcItemsFree = 0;
935 pInfo->wcItemsLRU = 0;
936 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
937 if (pArena->size == 0) pInfo->wcItemsFree++;
938 return TRUE;
942 /***********************************************************************
943 * GlobalEntryHandle (TOOLHELP.54)
945 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
947 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
949 pGlobal->dwAddress = pArena->base;
950 pGlobal->dwBlockSize = pArena->size;
951 pGlobal->hBlock = pArena->handle;
952 pGlobal->wcLock = pArena->lockCount;
953 pGlobal->wcPageLock = pArena->pageLockCount;
954 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
955 pGlobal->wHeapPresent = FALSE;
956 pGlobal->hOwner = pArena->hOwner;
957 pGlobal->wType = GT_UNKNOWN;
958 pGlobal->wData = 0;
959 pGlobal->dwNext++;
960 return TRUE;
964 /***********************************************************************
965 * GlobalEntryModule (TOOLHELP.55)
967 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
968 WORD wSeg )
970 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
971 return FALSE;
975 /***********************************************************************
976 * MemManInfo (TOOLHELP.72)
978 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
980 MEMORYSTATUS status;
983 * Not unsurprisingly although the documention says you
984 * _must_ provide the size in the dwSize field, this function
985 * (under Windows) always fills the structure and returns true.
987 GlobalMemoryStatus( &status );
988 info->wPageSize = getpagesize();
989 info->dwLargestFreeBlock = status.dwAvailVirtual;
990 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
991 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
992 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
993 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
994 info->dwFreePages = info->dwMaxPagesAvailable;
995 info->dwTotalPages = info->dwTotalLinearSpace;
996 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
997 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
998 return TRUE;
1001 /***********************************************************************
1002 * GetFreeMemInfo (KERNEL.316)
1004 DWORD WINAPI GetFreeMemInfo16(void)
1006 MEMMANINFO info;
1007 MemManInfo16( &info );
1008 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1012 * Win32 Global heap functions (GlobalXXX).
1013 * These functions included in Win32 for compatibility with 16 bit Windows
1014 * Especially the moveable blocks and handles are oldish.
1015 * But the ability to directly allocate memory with GPTR and LPTR is widely
1016 * used.
1018 * The handle stuff looks horrible, but it's implemented almost like Win95
1019 * does it.
1023 #define MAGIC_GLOBAL_USED 0x5342
1024 #define GLOBAL_LOCK_MAX 0xFF
1025 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1026 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1027 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
1028 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1029 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1031 typedef struct __GLOBAL32_INTERN
1033 WORD Magic;
1034 LPVOID Pointer WINE_PACKED;
1035 BYTE Flags;
1036 BYTE LockCount;
1037 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1040 /***********************************************************************
1041 * GlobalAlloc (KERNEL32.@)
1042 * RETURNS
1043 * Handle: Success
1044 * NULL: Failure
1046 HGLOBAL WINAPI GlobalAlloc(
1047 UINT flags, /* [in] Object allocation attributes */
1048 SIZE_T size /* [in] Number of bytes to allocate */
1050 PGLOBAL32_INTERN pintern;
1051 DWORD hpflags;
1052 LPVOID palloc;
1054 if(flags&GMEM_ZEROINIT)
1055 hpflags=HEAP_ZERO_MEMORY;
1056 else
1057 hpflags=0;
1059 TRACE("() flags=%04x\n", flags );
1061 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1063 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1064 return (HGLOBAL) palloc;
1066 else /* HANDLE */
1068 /* HeapLock(heap); */
1070 pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1071 if (!pintern) return 0;
1072 if(size)
1074 if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+sizeof(HGLOBAL)))) {
1075 HeapFree(GetProcessHeap(), 0, pintern);
1076 return 0;
1078 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1079 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1081 else
1082 pintern->Pointer=NULL;
1083 pintern->Magic=MAGIC_GLOBAL_USED;
1084 pintern->Flags=flags>>8;
1085 pintern->LockCount=0;
1087 /* HeapUnlock(heap); */
1089 return INTERN_TO_HANDLE(pintern);
1094 /***********************************************************************
1095 * GlobalLock (KERNEL32.@)
1096 * RETURNS
1097 * Pointer to first byte of block
1098 * NULL: Failure
1100 LPVOID WINAPI GlobalLock(
1101 HGLOBAL hmem /* [in] Handle of global memory object */
1103 PGLOBAL32_INTERN pintern;
1104 LPVOID palloc;
1106 if(ISPOINTER(hmem))
1107 return (LPVOID) hmem;
1109 /* HeapLock(GetProcessHeap()); */
1111 pintern=HANDLE_TO_INTERN(hmem);
1112 if(pintern->Magic==MAGIC_GLOBAL_USED)
1114 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1115 pintern->LockCount++;
1116 palloc=pintern->Pointer;
1118 else
1120 WARN("invalid handle\n");
1121 palloc=(LPVOID) NULL;
1122 SetLastError(ERROR_INVALID_HANDLE);
1124 /* HeapUnlock(GetProcessHeap()); */;
1125 return palloc;
1129 /***********************************************************************
1130 * GlobalUnlock (KERNEL32.@)
1131 * RETURNS
1132 * TRUE: Object is still locked
1133 * FALSE: Object is unlocked
1135 BOOL WINAPI GlobalUnlock(
1136 HGLOBAL hmem /* [in] Handle of global memory object */
1138 PGLOBAL32_INTERN pintern;
1139 BOOL locked;
1141 if (ISPOINTER(hmem)) return FALSE;
1143 __TRY
1145 /* HeapLock(GetProcessHeap()); */
1146 pintern=HANDLE_TO_INTERN(hmem);
1147 if(pintern->Magic==MAGIC_GLOBAL_USED)
1149 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1150 pintern->LockCount--;
1152 locked = (pintern->LockCount != 0);
1153 if (!locked) SetLastError(NO_ERROR);
1155 else
1157 WARN("invalid handle\n");
1158 SetLastError(ERROR_INVALID_HANDLE);
1159 locked=FALSE;
1161 /* HeapUnlock(GetProcessHeap()); */
1163 __EXCEPT(page_fault)
1165 ERR("page fault occurred ! Caused by bug ?\n");
1166 SetLastError( ERROR_INVALID_PARAMETER );
1167 return FALSE;
1169 __ENDTRY
1170 return locked;
1174 /***********************************************************************
1175 * GlobalHandle (KERNEL32.@)
1176 * Returns the handle associated with the specified pointer.
1178 * RETURNS
1179 * Handle: Success
1180 * NULL: Failure
1182 HGLOBAL WINAPI GlobalHandle(
1183 LPCVOID pmem /* [in] Pointer to global memory block */
1185 HGLOBAL handle;
1186 PGLOBAL32_INTERN maybe_intern;
1187 LPCVOID test;
1189 if (!pmem)
1191 SetLastError( ERROR_INVALID_PARAMETER );
1192 return 0;
1195 __TRY
1197 handle = 0;
1199 /* note that if pmem is a pointer to a a block allocated by */
1200 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1201 /* will fail. */
1202 if (ISPOINTER(pmem)) {
1203 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
1204 handle = (HGLOBAL)pmem; /* valid fixed block */
1205 break;
1207 handle = POINTER_TO_HANDLE(pmem);
1208 } else
1209 handle = (HGLOBAL)pmem;
1211 /* Now test handle either passed in or retrieved from pointer */
1212 maybe_intern = HANDLE_TO_INTERN( handle );
1213 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1214 test = maybe_intern->Pointer;
1215 if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */
1216 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
1217 break; /* valid moveable block */
1219 handle = 0;
1220 SetLastError( ERROR_INVALID_HANDLE );
1222 __EXCEPT(page_fault)
1224 SetLastError( ERROR_INVALID_HANDLE );
1225 return 0;
1227 __ENDTRY
1229 return handle;
1233 /***********************************************************************
1234 * GlobalReAlloc (KERNEL32.@)
1235 * RETURNS
1236 * Handle: Success
1237 * NULL: Failure
1239 HGLOBAL WINAPI GlobalReAlloc(
1240 HGLOBAL hmem, /* [in] Handle of global memory object */
1241 SIZE_T size, /* [in] New size of block */
1242 UINT flags /* [in] How to reallocate object */
1244 LPVOID palloc;
1245 HGLOBAL hnew;
1246 PGLOBAL32_INTERN pintern;
1247 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1249 hnew = 0;
1250 /* HeapLock(heap); */
1251 if(flags & GMEM_MODIFY) /* modify flags */
1253 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1255 /* make a fixed block moveable
1256 * actually only NT is able to do this. But it's soo simple
1258 if (hmem == 0)
1260 ERR("GlobalReAlloc32 with null handle!\n");
1261 SetLastError( ERROR_NOACCESS );
1262 return 0;
1264 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1265 hnew=GlobalAlloc( flags, size);
1266 palloc=GlobalLock(hnew);
1267 memcpy(palloc, (LPVOID) hmem, size);
1268 GlobalUnlock(hnew);
1269 GlobalFree(hmem);
1271 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1273 /* change the flags to make our block "discardable" */
1274 pintern=HANDLE_TO_INTERN(hmem);
1275 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1276 hnew=hmem;
1278 else
1280 SetLastError(ERROR_INVALID_PARAMETER);
1281 hnew = 0;
1284 else
1286 if(ISPOINTER(hmem))
1288 /* reallocate fixed memory */
1289 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
1291 else
1293 /* reallocate a moveable block */
1294 pintern=HANDLE_TO_INTERN(hmem);
1296 #if 0
1297 /* Apparently Windows doesn't care whether the handle is locked at this point */
1298 /* See also the same comment in GlobalFree() */
1299 if(pintern->LockCount>1) {
1300 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1301 SetLastError(ERROR_INVALID_HANDLE);
1302 } else
1303 #endif
1304 if(size!=0)
1306 hnew=hmem;
1307 if(pintern->Pointer)
1309 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
1310 (char *) pintern->Pointer-sizeof(HGLOBAL),
1311 size+sizeof(HGLOBAL))) == NULL)
1312 return 0; /* Block still valid */
1313 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1315 else
1317 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+sizeof(HGLOBAL)))
1318 == NULL)
1319 return 0;
1320 *(HGLOBAL *)palloc=hmem;
1321 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1324 else
1326 if(pintern->Pointer)
1328 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1329 pintern->Pointer=NULL;
1334 /* HeapUnlock(heap); */
1335 return hnew;
1339 /***********************************************************************
1340 * GlobalFree (KERNEL32.@)
1341 * RETURNS
1342 * NULL: Success
1343 * Handle: Failure
1345 HGLOBAL WINAPI GlobalFree(
1346 HGLOBAL hmem /* [in] Handle of global memory object */
1348 PGLOBAL32_INTERN pintern;
1349 HGLOBAL hreturned;
1351 __TRY
1353 hreturned = 0;
1354 if(ISPOINTER(hmem)) /* POINTER */
1356 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1358 else /* HANDLE */
1360 /* HeapLock(heap); */
1361 pintern=HANDLE_TO_INTERN(hmem);
1363 if(pintern->Magic==MAGIC_GLOBAL_USED)
1366 /* WIN98 does not make this test. That is you can free a */
1367 /* block you have not unlocked. Go figure!! */
1368 /* if(pintern->LockCount!=0) */
1369 /* SetLastError(ERROR_INVALID_HANDLE); */
1371 if(pintern->Pointer)
1372 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1373 hreturned=hmem;
1374 if(!HeapFree(GetProcessHeap(), 0, pintern))
1375 hreturned=hmem;
1377 /* HeapUnlock(heap); */
1380 __EXCEPT(page_fault)
1382 ERR("page fault occurred ! Caused by bug ?\n");
1383 SetLastError( ERROR_INVALID_PARAMETER );
1384 return hmem;
1386 __ENDTRY
1387 return hreturned;
1391 /***********************************************************************
1392 * GlobalSize (KERNEL32.@)
1393 * RETURNS
1394 * Size in bytes of the global memory object
1395 * 0: Failure
1397 SIZE_T WINAPI GlobalSize(
1398 HGLOBAL hmem /* [in] Handle of global memory object */
1400 DWORD retval;
1401 PGLOBAL32_INTERN pintern;
1403 if(ISPOINTER(hmem))
1405 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1407 else
1409 /* HeapLock(heap); */
1410 pintern=HANDLE_TO_INTERN(hmem);
1412 if(pintern->Magic==MAGIC_GLOBAL_USED)
1414 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1415 return 0;
1416 retval=HeapSize(GetProcessHeap(), 0,
1417 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1418 if (retval == 0xffffffff-4) retval = 0;
1420 else
1422 WARN("invalid handle\n");
1423 retval=0;
1425 /* HeapUnlock(heap); */
1427 /* HeapSize returns 0xffffffff on failure */
1428 if (retval == 0xffffffff) retval = 0;
1429 return retval;
1433 /***********************************************************************
1434 * GlobalWire (KERNEL32.@)
1436 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1438 return GlobalLock( hmem );
1442 /***********************************************************************
1443 * GlobalUnWire (KERNEL32.@)
1445 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1447 return GlobalUnlock( hmem);
1451 /***********************************************************************
1452 * GlobalFix (KERNEL32.@)
1454 VOID WINAPI GlobalFix(HGLOBAL hmem)
1456 GlobalLock( hmem );
1460 /***********************************************************************
1461 * GlobalUnfix (KERNEL32.@)
1463 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1465 GlobalUnlock( hmem);
1469 /***********************************************************************
1470 * GlobalFlags (KERNEL32.@)
1471 * Returns information about the specified global memory object
1473 * NOTES
1474 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1476 * RETURNS
1477 * Value specifying allocation flags and lock count
1478 * GMEM_INVALID_HANDLE: Failure
1480 UINT WINAPI GlobalFlags(
1481 HGLOBAL hmem /* [in] Handle to global memory object */
1483 DWORD retval;
1484 PGLOBAL32_INTERN pintern;
1486 if(ISPOINTER(hmem))
1488 retval=0;
1490 else
1492 /* HeapLock(GetProcessHeap()); */
1493 pintern=HANDLE_TO_INTERN(hmem);
1494 if(pintern->Magic==MAGIC_GLOBAL_USED)
1496 retval=pintern->LockCount + (pintern->Flags<<8);
1497 if(pintern->Pointer==0)
1498 retval|= GMEM_DISCARDED;
1500 else
1502 WARN("Invalid handle: %04x\n", hmem);
1503 retval=0;
1505 /* HeapUnlock(GetProcessHeap()); */
1507 return retval;
1511 /***********************************************************************
1512 * GlobalCompact (KERNEL32.@)
1514 SIZE_T WINAPI GlobalCompact( DWORD minfree )
1516 return 0; /* GlobalCompact does nothing in Win32 */
1520 /***********************************************************************
1521 * GlobalMemoryStatus (KERNEL32.@)
1522 * RETURNS
1523 * None
1525 VOID WINAPI GlobalMemoryStatus(
1526 LPMEMORYSTATUS lpmem
1528 static MEMORYSTATUS cached_memstatus;
1529 static int cache_lastchecked = 0;
1530 SYSTEM_INFO si;
1531 #ifdef linux
1532 FILE *f;
1533 #endif
1534 #ifdef __FreeBSD__
1535 int *tmp;
1536 int size_sys;
1537 #endif
1538 if (time(NULL)==cache_lastchecked) {
1539 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1540 return;
1542 cache_lastchecked = time(NULL);
1544 lpmem->dwMemoryLoad = 0;
1545 lpmem->dwTotalPhys = 16*1024*1024;
1546 lpmem->dwAvailPhys = 16*1024*1024;
1547 lpmem->dwTotalPageFile = 16*1024*1024;
1548 lpmem->dwAvailPageFile = 16*1024*1024;
1550 #ifdef linux
1551 f = fopen( "/proc/meminfo", "r" );
1552 if (f)
1554 char buffer[256];
1555 int total, used, free, shared, buffers, cached;
1557 lpmem->dwLength = sizeof(MEMORYSTATUS);
1558 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1559 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1560 while (fgets( buffer, sizeof(buffer), f ))
1562 /* old style /proc/meminfo ... */
1563 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1565 lpmem->dwTotalPhys += total;
1566 lpmem->dwAvailPhys += free + buffers + cached;
1568 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1570 lpmem->dwTotalPageFile += total;
1571 lpmem->dwAvailPageFile += free;
1574 /* new style /proc/meminfo ... */
1575 if (sscanf(buffer, "MemTotal: %d", &total))
1576 lpmem->dwTotalPhys = total*1024;
1577 if (sscanf(buffer, "MemFree: %d", &free))
1578 lpmem->dwAvailPhys = free*1024;
1579 if (sscanf(buffer, "SwapTotal: %d", &total))
1580 lpmem->dwTotalPageFile = total*1024;
1581 if (sscanf(buffer, "SwapFree: %d", &free))
1582 lpmem->dwAvailPageFile = free*1024;
1583 if (sscanf(buffer, "Buffers: %d", &buffers))
1584 lpmem->dwAvailPhys += buffers*1024;
1585 if (sscanf(buffer, "Cached: %d", &cached))
1586 lpmem->dwAvailPhys += cached*1024;
1588 fclose( f );
1590 if (lpmem->dwTotalPhys)
1592 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1593 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1594 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1595 / (TotalPhysical / 100);
1598 #elif defined(__FreeBSD__)
1599 sysctlbyname("hw.physmem", NULL, &size_sys, NULL, 0);
1600 tmp = malloc(size_sys * sizeof(int));
1601 sysctlbyname("hw.physmem", tmp, &size_sys, NULL, 0);
1602 if (tmp && *tmp)
1604 lpmem->dwTotalPhys = *tmp;
1605 free(tmp);
1606 sysctlbyname("hw.usermem", NULL, &size_sys, NULL, 0);
1607 tmp = malloc(size_sys * sizeof(int));
1608 sysctlbyname("hw.usermem", tmp, &size_sys, NULL, 0);
1609 if (tmp && *tmp)
1611 lpmem->dwAvailPhys = *tmp;
1612 lpmem->dwTotalPageFile = *tmp;
1613 lpmem->dwAvailPageFile = *tmp;
1614 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1615 } else
1617 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1618 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1619 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1620 lpmem->dwMemoryLoad = 0;
1622 free(tmp);
1625 #endif
1626 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1627 * is more than 2GB of physical memory.
1629 if (lpmem->dwTotalPhys>2U*1024*1024*1024)
1631 lpmem->dwTotalPhys=2U*1024*1024*1024;
1632 lpmem->dwAvailPhys=2U*1024*1024*1024;
1635 /* FIXME: should do something for other systems */
1636 GetSystemInfo(&si);
1637 lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1638 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1639 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1640 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1642 /* it appears some memory display programs want to divide by these values */
1643 if(lpmem->dwTotalPageFile==0)
1644 lpmem->dwTotalPageFile++;
1646 if(lpmem->dwAvailPageFile==0)
1647 lpmem->dwAvailPageFile++;
1649 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1650 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1651 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1652 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1653 lpmem->dwAvailVirtual);
1656 /***********************************************************************
1657 * A20Proc (KERNEL.165)
1658 * A20_Proc (SYSTEM.20)
1660 void WINAPI A20Proc16( WORD unused )
1662 /* this is also a NOP in Windows */
1665 /***********************************************************************
1666 * LimitEMSPages (KERNEL.156)
1668 DWORD WINAPI LimitEMSPages16( DWORD unused )
1670 return 0;