Remove header file generation, and related options.
[wine/multimedia.git] / memory / global.c
bloba471e60addf3a1e3ef26229ad6b929b27aec9d4c
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 "wine/debug.h"
48 #include "winerror.h"
49 #include "excpt.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(global);
53 /* Global arena block */
54 typedef struct
56 DWORD base; /* Base address (0 if discarded) */
57 DWORD size; /* Size in bytes (0 indicates a free block) */
58 HGLOBAL16 handle; /* Handle for this block */
59 HGLOBAL16 hOwner; /* Owner of this block */
60 BYTE lockCount; /* Count of GlobalFix() calls */
61 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
62 BYTE flags; /* Allocation flags */
63 BYTE selCount; /* Number of selectors allocated for this block */
64 } GLOBALARENA;
66 /* Flags definitions */
67 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
68 #define GA_DGROUP 0x04
69 #define GA_DISCARDABLE 0x08
70 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
71 #define GA_DOSMEM 0x20
73 /* Arena array */
74 static GLOBALARENA *pGlobalArena = NULL;
75 static int globalArenaSize = 0;
77 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
79 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
80 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
82 /* filter for page-fault exceptions */
83 /* It is possible for a bogus global pointer to cause a */
84 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
86 static WINE_EXCEPTION_FILTER(page_fault)
88 switch (GetExceptionCode()) {
89 case (EXCEPTION_ACCESS_VIOLATION):
90 case (EXCEPTION_PRIV_INSTRUCTION):
91 return EXCEPTION_EXECUTE_HANDLER;
92 default:
93 return EXCEPTION_CONTINUE_SEARCH;
97 /***********************************************************************
98 * GLOBAL_GetArena
100 * Return the arena for a given selector, growing the arena array if needed.
102 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
104 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
106 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
107 GLOBALARENA *pNewArena = realloc( pGlobalArena,
108 newsize * sizeof(GLOBALARENA) );
109 if (!pNewArena) return 0;
110 pGlobalArena = pNewArena;
111 memset( pGlobalArena + globalArenaSize, 0,
112 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
113 globalArenaSize = newsize;
115 return pGlobalArena + (sel >> __AHSHIFT);
118 void debug_handles(void)
120 int printed=0;
121 int i;
122 for (i = globalArenaSize-1 ; i>=0 ; i--) {
123 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
124 printed=1;
125 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
128 if (printed)
129 DPRINTF("\n");
133 /***********************************************************************
134 * GLOBAL_CreateBlock
136 * Create a global heap block for a fixed range of linear memory.
138 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
139 HGLOBAL16 hOwner, unsigned char selflags )
141 WORD sel, selcount;
142 GLOBALARENA *pArena;
144 /* Allocate the selector(s) */
146 sel = SELECTOR_AllocBlock( ptr, size, selflags );
147 if (!sel) return 0;
148 selcount = (size + 0xffff) / 0x10000;
150 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
152 SELECTOR_FreeBlock( sel );
153 return 0;
156 /* Fill the arena block */
158 pArena->base = (DWORD)ptr;
159 pArena->size = GetSelectorLimit16(sel) + 1;
160 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
161 pArena->hOwner = hOwner;
162 pArena->lockCount = 0;
163 pArena->pageLockCount = 0;
164 pArena->flags = flags & GA_MOVEABLE;
165 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
166 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
167 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
168 pArena->selCount = selcount;
169 if (selcount > 1) /* clear the next arena blocks */
170 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
172 return pArena->handle;
176 /***********************************************************************
177 * GLOBAL_FreeBlock
179 * Free a block allocated by GLOBAL_CreateBlock, without touching
180 * the associated linear memory range.
182 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
184 WORD sel;
185 GLOBALARENA *pArena;
187 if (!handle) return TRUE;
188 sel = GlobalHandleToSel16( handle );
189 if (!VALID_HANDLE(sel))
190 return FALSE;
191 pArena = GET_ARENA_PTR(sel);
192 SELECTOR_FreeBlock( sel );
193 memset( pArena, 0, sizeof(GLOBALARENA) );
194 return TRUE;
197 /***********************************************************************
198 * GLOBAL_MoveBlock
200 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
202 WORD sel;
203 GLOBALARENA *pArena;
205 if (!handle) return TRUE;
206 sel = GlobalHandleToSel16( handle );
207 if (!VALID_HANDLE(sel))
208 return FALSE;
209 pArena = GET_ARENA_PTR(sel);
210 if (pArena->selCount != 1)
211 return FALSE;
213 pArena->base = (DWORD)ptr;
214 pArena->size = size;
215 SELECTOR_ReallocBlock( sel, ptr, size );
216 return TRUE;
219 /***********************************************************************
220 * GLOBAL_Alloc
222 * Implementation of GlobalAlloc16()
224 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
226 void *ptr;
227 HGLOBAL16 handle;
229 TRACE("%ld flags=%04x\n", size, flags );
231 /* If size is 0, create a discarded block */
233 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
235 /* Fixup the size */
237 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
238 size = (size + 0x1f) & ~0x1f;
240 /* Allocate the linear memory */
241 ptr = HeapAlloc( GetProcessHeap(), 0, size );
242 /* FIXME: free discardable blocks and try again? */
243 if (!ptr) return 0;
245 /* Allocate the selector(s) */
247 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
248 if (!handle)
250 HeapFree( GetProcessHeap(), 0, ptr );
251 return 0;
254 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
255 return handle;
258 /***********************************************************************
259 * GlobalAlloc (KERNEL.15)
260 * GlobalAlloc16 (KERNEL32.24)
261 * RETURNS
262 * Handle: Success
263 * NULL: Failure
265 HGLOBAL16 WINAPI GlobalAlloc16(
266 UINT16 flags, /* [in] Object allocation attributes */
267 DWORD size /* [in] Number of bytes to allocate */
269 HANDLE16 owner = GetCurrentPDB16();
271 if (flags & GMEM_DDESHARE)
272 owner = GetExePtr(owner); /* Make it a module handle */
273 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
277 /***********************************************************************
278 * GlobalReAlloc (KERNEL.16)
279 * RETURNS
280 * Handle: Success
281 * NULL: Failure
283 HGLOBAL16 WINAPI GlobalReAlloc16(
284 HGLOBAL16 handle, /* [in] Handle of global memory object */
285 DWORD size, /* [in] New size of block */
286 UINT16 flags /* [in] How to reallocate object */
288 WORD selcount;
289 DWORD oldsize;
290 void *ptr, *newptr;
291 GLOBALARENA *pArena, *pNewArena;
292 WORD sel = GlobalHandleToSel16( handle );
294 TRACE("%04x %ld flags=%04x\n",
295 handle, size, flags );
296 if (!handle) return 0;
298 if (!VALID_HANDLE(handle)) {
299 WARN("Invalid handle 0x%04x!\n", handle);
300 return 0;
302 pArena = GET_ARENA_PTR( handle );
304 /* Discard the block if requested */
306 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
308 if (!(pArena->flags & GA_MOVEABLE) ||
309 !(pArena->flags & GA_DISCARDABLE) ||
310 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
311 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
312 pArena->base = 0;
314 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
315 * change the selector if we are shrinking the block.
316 * FIXME: shouldn't we keep selectors until the block is deleted?
318 SELECTOR_ReallocBlock( sel, 0, 1 );
319 return handle;
322 /* Fixup the size */
324 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
325 if (size == 0) size = 0x20;
326 else size = (size + 0x1f) & ~0x1f;
328 /* Change the flags */
330 if (flags & GMEM_MODIFY)
332 /* Change the flags, leaving GA_DGROUP alone */
333 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
334 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
335 return handle;
338 /* Reallocate the linear memory */
340 ptr = (void *)pArena->base;
341 oldsize = pArena->size;
342 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
343 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
345 if (pArena->flags & GA_DOSMEM)
346 newptr = DOSMEM_ResizeBlock(ptr, size, NULL);
347 else
348 /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16),
349 only try to realloc in place */
350 newptr = HeapReAlloc( GetProcessHeap(),
351 (pArena->pageLockCount > 0)?HEAP_REALLOC_IN_PLACE_ONLY:0, ptr, size );
352 if (!newptr)
354 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
355 if (pArena->pageLockCount <1)
357 HeapFree( GetProcessHeap(), 0, ptr );
358 SELECTOR_FreeBlock( sel );
359 memset( pArena, 0, sizeof(GLOBALARENA) );
361 return 0;
363 ptr = newptr;
365 /* Reallocate the selector(s) */
367 sel = SELECTOR_ReallocBlock( sel, ptr, size );
368 if (!sel)
370 HeapFree( GetProcessHeap(), 0, ptr );
371 memset( pArena, 0, sizeof(GLOBALARENA) );
372 return 0;
374 selcount = (size + 0xffff) / 0x10000;
376 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
378 HeapFree( GetProcessHeap(), 0, ptr );
379 SELECTOR_FreeBlock( sel );
380 return 0;
383 /* Fill the new arena block
384 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
386 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
387 pNewArena->base = (DWORD)ptr;
388 pNewArena->size = GetSelectorLimit16(sel) + 1;
389 pNewArena->selCount = selcount;
390 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
392 if (selcount > 1) /* clear the next arena blocks */
393 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
395 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
396 memset( (char *)ptr + oldsize, 0, size - oldsize );
397 return pNewArena->handle;
401 /***********************************************************************
402 * GlobalFree (KERNEL.17)
403 * GlobalFree16 (KERNEL32.31)
404 * RETURNS
405 * NULL: Success
406 * Handle: Failure
408 HGLOBAL16 WINAPI GlobalFree16(
409 HGLOBAL16 handle /* [in] Handle of global memory object */
411 void *ptr;
413 if (!VALID_HANDLE(handle)) {
414 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
415 return 0;
417 ptr = (void *)GET_ARENA_PTR(handle)->base;
419 TRACE("%04x\n", handle );
420 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
421 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
422 return 0;
426 /***********************************************************************
427 * GlobalLock (KERNEL.18)
429 * This is the GlobalLock16() function used by 16-bit code.
431 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
433 WORD sel = GlobalHandleToSel16( handle );
434 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
436 if (handle)
438 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
440 if (!VALID_HANDLE(handle)) {
441 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
442 sel = 0;
444 else if (!GET_ARENA_PTR(handle)->base)
445 sel = 0;
446 else
447 GET_ARENA_PTR(handle)->lockCount++;
450 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
451 return MAKESEGPTR( sel, 0 );
455 /**********************************************************************
456 * K32WOWGlobalLock16 (KERNEL32.60)
458 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
460 return WIN16_GlobalLock16( hMem );
464 /***********************************************************************
465 * GlobalLock16 (KERNEL32.25)
467 * This is the GlobalLock16() function used by 32-bit code.
469 * RETURNS
470 * Pointer to first byte of memory block
471 * NULL: Failure
473 LPVOID WINAPI GlobalLock16(
474 HGLOBAL16 handle /* [in] Handle of global memory object */
476 if (!handle) return 0;
477 if (!VALID_HANDLE(handle))
478 return 0;
479 GET_ARENA_PTR(handle)->lockCount++;
480 return (LPVOID)GET_ARENA_PTR(handle)->base;
484 /***********************************************************************
485 * GlobalUnlock (KERNEL.19)
486 * GlobalUnlock16 (KERNEL32.26)
487 * NOTES
488 * Should the return values be cast to booleans?
490 * RETURNS
491 * TRUE: Object is still locked
492 * FALSE: Object is unlocked
494 BOOL16 WINAPI GlobalUnlock16(
495 HGLOBAL16 handle /* [in] Handle of global memory object */
497 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
498 if (!VALID_HANDLE(handle)) {
499 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
500 return 0;
502 TRACE("%04x\n", handle );
503 if (pArena->lockCount) pArena->lockCount--;
504 return pArena->lockCount;
507 /***********************************************************************
508 * GlobalChangeLockCount (KERNEL.365)
510 * This is declared as a register function as it has to preserve
511 * *all* registers, even AX/DX !
514 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
515 CONTEXT86 *context )
517 if ( delta == 1 )
518 GlobalLock16( handle );
519 else if ( delta == -1 )
520 GlobalUnlock16( handle );
521 else
522 ERR("(%04X, %d): strange delta value\n", handle, delta );
525 /***********************************************************************
526 * GlobalSize (KERNEL.20)
527 * GlobalSize16 (KERNEL32.32)
528 * RETURNS
529 * Size in bytes of object
530 * 0: Failure
532 DWORD WINAPI GlobalSize16(
533 HGLOBAL16 handle /* [in] Handle of global memory object */
535 TRACE("%04x\n", handle );
536 if (!handle) return 0;
537 if (!VALID_HANDLE(handle))
538 return 0;
539 return GET_ARENA_PTR(handle)->size;
543 /***********************************************************************
544 * GlobalHandle (KERNEL.21)
545 * NOTES
546 * Why is GlobalHandleToSel used here with the sel as input?
548 * RETURNS
549 * Handle: Success
550 * NULL: Failure
552 DWORD WINAPI GlobalHandle16(
553 WORD sel /* [in] Address of global memory block */
555 TRACE("%04x\n", sel );
556 if (!VALID_HANDLE(sel)) {
557 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
558 return 0;
560 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
563 /***********************************************************************
564 * GlobalHandleNoRIP (KERNEL.159)
566 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
568 int i;
569 for (i = globalArenaSize-1 ; i>=0 ; i--) {
570 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
571 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
573 return 0;
577 /***********************************************************************
578 * GlobalFlags (KERNEL.22)
580 * NOTES
581 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
582 * handle?
584 * RETURNS
585 * Value specifying flags and lock count
586 * GMEM_INVALID_HANDLE: Invalid handle
588 UINT16 WINAPI GlobalFlags16(
589 HGLOBAL16 handle /* [in] Handle of global memory object */
591 GLOBALARENA *pArena;
593 TRACE("%04x\n", handle );
594 if (!VALID_HANDLE(handle)) {
595 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
596 return 0;
598 pArena = GET_ARENA_PTR(handle);
599 return pArena->lockCount |
600 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
601 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
605 /***********************************************************************
606 * LockSegment (KERNEL.23)
608 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
610 TRACE("%04x\n", handle );
611 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
612 if (!VALID_HANDLE(handle)) {
613 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
614 return 0;
616 GET_ARENA_PTR(handle)->lockCount++;
617 return handle;
621 /***********************************************************************
622 * UnlockSegment (KERNEL.24)
624 void WINAPI UnlockSegment16( HGLOBAL16 handle )
626 TRACE("%04x\n", handle );
627 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
628 if (!VALID_HANDLE(handle)) {
629 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
630 return;
632 GET_ARENA_PTR(handle)->lockCount--;
633 /* FIXME: this ought to return the lock count in CX (go figure...) */
637 /***********************************************************************
638 * GlobalCompact (KERNEL.25)
640 DWORD WINAPI GlobalCompact16( DWORD desired )
642 return GLOBAL_MAX_ALLOC_SIZE;
646 /***********************************************************************
647 * GlobalFreeAll (KERNEL.26)
649 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
651 DWORD i;
652 GLOBALARENA *pArena;
654 pArena = pGlobalArena;
655 for (i = 0; i < globalArenaSize; i++, pArena++)
657 if ((pArena->size != 0) && (pArena->hOwner == owner))
658 GlobalFree16( pArena->handle );
663 /***********************************************************************
664 * GlobalWire (KERNEL.111)
665 * GlobalWire16 (KERNEL32.29)
667 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
669 return WIN16_GlobalLock16( handle );
673 /***********************************************************************
674 * GlobalUnWire (KERNEL.112)
675 * GlobalUnWire16 (KERNEL32.30)
677 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
679 return !GlobalUnlock16( handle );
683 /***********************************************************************
684 * SetSwapAreaSize (KERNEL.106)
686 LONG WINAPI SetSwapAreaSize16( WORD size )
688 FIXME("(%d) - stub!\n", size );
689 return MAKELONG( size, 0xffff );
693 /***********************************************************************
694 * GlobalLRUOldest (KERNEL.163)
696 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
698 TRACE("%04x\n", handle );
699 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
700 return handle;
704 /***********************************************************************
705 * GlobalLRUNewest (KERNEL.164)
707 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
709 TRACE("%04x\n", handle );
710 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
711 return handle;
715 /***********************************************************************
716 * GetFreeSpace (KERNEL.169)
718 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
720 MEMORYSTATUS ms;
721 GlobalMemoryStatus( &ms );
722 return ms.dwAvailVirtual;
725 /***********************************************************************
726 * GlobalDOSAlloc (KERNEL.184)
727 * RETURNS
728 * Address (HW=Paragraph segment; LW=Selector)
730 DWORD WINAPI GlobalDOSAlloc16(
731 DWORD size /* [in] Number of bytes to be allocated */
733 UINT16 uParagraph;
734 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
736 if( lpBlock )
738 HMODULE16 hModule = GetModuleHandle16("KERNEL");
739 WORD wSelector;
740 GLOBALARENA *pArena;
742 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
743 pArena = GET_ARENA_PTR(wSelector);
744 pArena->flags |= GA_DOSMEM;
745 return MAKELONG(wSelector,uParagraph);
747 return 0;
751 /***********************************************************************
752 * GlobalDOSFree (KERNEL.185)
753 * RETURNS
754 * NULL: Success
755 * sel: Failure
757 WORD WINAPI GlobalDOSFree16(
758 WORD sel /* [in] Selector */
760 DWORD block = GetSelectorBase(sel);
762 if( block && block < 0x100000 )
764 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
765 if( DOSMEM_FreeBlock( lpBlock ) )
766 GLOBAL_FreeBlock( sel );
767 sel = 0;
769 return sel;
773 /***********************************************************************
774 * GlobalPageLock (KERNEL.191)
775 * GlobalSmartPageLock(KERNEL.230)
777 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
779 TRACE("%04x\n", handle );
780 if (!VALID_HANDLE(handle)) {
781 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
782 return 0;
784 return ++(GET_ARENA_PTR(handle)->pageLockCount);
788 /***********************************************************************
789 * GlobalPageUnlock (KERNEL.192)
790 * GlobalSmartPageUnlock(KERNEL.231)
792 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
794 TRACE("%04x\n", handle );
795 if (!VALID_HANDLE(handle)) {
796 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
797 return 0;
799 return --(GET_ARENA_PTR(handle)->pageLockCount);
803 /***********************************************************************
804 * GlobalFix (KERNEL.197)
805 * GlobalFix16 (KERNEL32.27)
807 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
809 TRACE("%04x\n", handle );
810 if (!VALID_HANDLE(handle)) {
811 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
812 return 0;
814 GET_ARENA_PTR(handle)->lockCount++;
816 return GlobalHandleToSel16(handle);
820 /***********************************************************************
821 * GlobalUnfix (KERNEL.198)
822 * GlobalUnfix16 (KERNEL32.28)
824 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
826 TRACE("%04x\n", handle );
827 if (!VALID_HANDLE(handle)) {
828 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
829 return;
831 GET_ARENA_PTR(handle)->lockCount--;
835 /***********************************************************************
836 * FarSetOwner (KERNEL.403)
838 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
840 if (!VALID_HANDLE(handle)) {
841 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
842 return;
844 GET_ARENA_PTR(handle)->hOwner = hOwner;
848 /***********************************************************************
849 * FarGetOwner (KERNEL.404)
851 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
853 if (!VALID_HANDLE(handle)) {
854 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
855 return 0;
857 return GET_ARENA_PTR(handle)->hOwner;
861 /***********************************************************************
862 * GlobalHandleToSel (TOOLHELP.50)
864 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
866 if (!handle) return 0;
867 if (!VALID_HANDLE(handle)) {
868 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
869 return 0;
871 if (!(handle & 7))
873 WARN("Program attempted invalid selector conversion\n" );
874 return handle - 1;
876 return handle | 7;
880 /***********************************************************************
881 * GlobalFirst (TOOLHELP.51)
883 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
885 if (wFlags == GLOBAL_LRU) return FALSE;
886 pGlobal->dwNext = 0;
887 return GlobalNext16( pGlobal, wFlags );
891 /***********************************************************************
892 * GlobalNext (TOOLHELP.52)
894 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
896 GLOBALARENA *pArena;
898 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
899 pArena = pGlobalArena + pGlobal->dwNext;
900 if (wFlags == GLOBAL_FREE) /* only free blocks */
902 int i;
903 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
904 if (pArena->size == 0) break; /* block is free */
905 if (i >= globalArenaSize) return FALSE;
906 pGlobal->dwNext = i;
909 pGlobal->dwAddress = pArena->base;
910 pGlobal->dwBlockSize = pArena->size;
911 pGlobal->hBlock = pArena->handle;
912 pGlobal->wcLock = pArena->lockCount;
913 pGlobal->wcPageLock = pArena->pageLockCount;
914 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
915 pGlobal->wHeapPresent = FALSE;
916 pGlobal->hOwner = pArena->hOwner;
917 pGlobal->wType = GT_UNKNOWN;
918 pGlobal->wData = 0;
919 pGlobal->dwNext++;
920 return TRUE;
924 /***********************************************************************
925 * GlobalInfo (TOOLHELP.53)
927 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
929 int i;
930 GLOBALARENA *pArena;
932 pInfo->wcItems = globalArenaSize;
933 pInfo->wcItemsFree = 0;
934 pInfo->wcItemsLRU = 0;
935 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
936 if (pArena->size == 0) pInfo->wcItemsFree++;
937 return TRUE;
941 /***********************************************************************
942 * GlobalEntryHandle (TOOLHELP.54)
944 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
946 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
948 pGlobal->dwAddress = pArena->base;
949 pGlobal->dwBlockSize = pArena->size;
950 pGlobal->hBlock = pArena->handle;
951 pGlobal->wcLock = pArena->lockCount;
952 pGlobal->wcPageLock = pArena->pageLockCount;
953 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
954 pGlobal->wHeapPresent = FALSE;
955 pGlobal->hOwner = pArena->hOwner;
956 pGlobal->wType = GT_UNKNOWN;
957 pGlobal->wData = 0;
958 pGlobal->dwNext++;
959 return TRUE;
963 /***********************************************************************
964 * GlobalEntryModule (TOOLHELP.55)
966 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
967 WORD wSeg )
969 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
970 return FALSE;
974 /***********************************************************************
975 * MemManInfo (TOOLHELP.72)
977 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
979 MEMORYSTATUS status;
982 * Not unsurprisingly although the documention says you
983 * _must_ provide the size in the dwSize field, this function
984 * (under Windows) always fills the structure and returns true.
986 GlobalMemoryStatus( &status );
987 info->wPageSize = getpagesize();
988 info->dwLargestFreeBlock = status.dwAvailVirtual;
989 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
990 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
991 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
992 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
993 info->dwFreePages = info->dwMaxPagesAvailable;
994 info->dwTotalPages = info->dwTotalLinearSpace;
995 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
996 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
997 return TRUE;
1000 /***********************************************************************
1001 * GetFreeMemInfo (KERNEL.316)
1003 DWORD WINAPI GetFreeMemInfo16(void)
1005 MEMMANINFO info;
1006 MemManInfo16( &info );
1007 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1011 * Win32 Global heap functions (GlobalXXX).
1012 * These functions included in Win32 for compatibility with 16 bit Windows
1013 * Especially the moveable blocks and handles are oldish.
1014 * But the ability to directly allocate memory with GPTR and LPTR is widely
1015 * used.
1017 * The handle stuff looks horrible, but it's implemented almost like Win95
1018 * does it.
1022 #define MAGIC_GLOBAL_USED 0x5342
1023 #define GLOBAL_LOCK_MAX 0xFF
1024 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1025 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1026 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-2))
1027 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1028 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1029 /* allign the storage needed for the HGLOBAL on an 8byte boundary thus
1030 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
1031 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
1032 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
1033 * the output jpeg's > 1 MB if not */
1034 #define HGLOBAL_STORAGE 8 /* sizeof(HGLOBAL)*2 */
1036 typedef struct __GLOBAL32_INTERN
1038 WORD Magic;
1039 LPVOID Pointer WINE_PACKED;
1040 BYTE Flags;
1041 BYTE LockCount;
1042 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1045 /***********************************************************************
1046 * GlobalAlloc (KERNEL32.@)
1047 * RETURNS
1048 * Handle: Success
1049 * NULL: Failure
1051 HGLOBAL WINAPI GlobalAlloc(
1052 UINT flags, /* [in] Object allocation attributes */
1053 SIZE_T size /* [in] Number of bytes to allocate */
1055 PGLOBAL32_INTERN pintern;
1056 DWORD hpflags;
1057 LPVOID palloc;
1059 if(flags&GMEM_ZEROINIT)
1060 hpflags=HEAP_ZERO_MEMORY;
1061 else
1062 hpflags=0;
1064 TRACE("() flags=%04x\n", flags );
1066 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1068 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1069 return (HGLOBAL) palloc;
1071 else /* HANDLE */
1073 /* HeapLock(heap); */
1075 pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1076 if (!pintern) return 0;
1077 if(size)
1079 if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+HGLOBAL_STORAGE))) {
1080 HeapFree(GetProcessHeap(), 0, pintern);
1081 return 0;
1083 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1084 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1086 else
1087 pintern->Pointer=NULL;
1088 pintern->Magic=MAGIC_GLOBAL_USED;
1089 pintern->Flags=flags>>8;
1090 pintern->LockCount=0;
1092 /* HeapUnlock(heap); */
1094 return INTERN_TO_HANDLE(pintern);
1099 /***********************************************************************
1100 * GlobalLock (KERNEL32.@)
1101 * RETURNS
1102 * Pointer to first byte of block
1103 * NULL: Failure
1105 LPVOID WINAPI GlobalLock(
1106 HGLOBAL hmem /* [in] Handle of global memory object */
1108 PGLOBAL32_INTERN pintern;
1109 LPVOID palloc;
1111 if(ISPOINTER(hmem))
1112 return (LPVOID) hmem;
1114 /* HeapLock(GetProcessHeap()); */
1116 pintern=HANDLE_TO_INTERN(hmem);
1117 if(pintern->Magic==MAGIC_GLOBAL_USED)
1119 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1120 pintern->LockCount++;
1121 palloc=pintern->Pointer;
1123 else
1125 WARN("invalid handle\n");
1126 palloc=NULL;
1127 SetLastError(ERROR_INVALID_HANDLE);
1129 /* HeapUnlock(GetProcessHeap()); */;
1130 return palloc;
1134 /***********************************************************************
1135 * GlobalUnlock (KERNEL32.@)
1136 * RETURNS
1137 * TRUE: Object is still locked
1138 * FALSE: Object is unlocked
1140 BOOL WINAPI GlobalUnlock(
1141 HGLOBAL hmem /* [in] Handle of global memory object */
1143 PGLOBAL32_INTERN pintern;
1144 BOOL locked;
1146 if (ISPOINTER(hmem)) return FALSE;
1148 __TRY
1150 /* HeapLock(GetProcessHeap()); */
1151 pintern=HANDLE_TO_INTERN(hmem);
1152 if(pintern->Magic==MAGIC_GLOBAL_USED)
1154 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1155 pintern->LockCount--;
1157 locked = (pintern->LockCount != 0);
1158 if (!locked) SetLastError(NO_ERROR);
1160 else
1162 WARN("invalid handle\n");
1163 SetLastError(ERROR_INVALID_HANDLE);
1164 locked=FALSE;
1166 /* HeapUnlock(GetProcessHeap()); */
1168 __EXCEPT(page_fault)
1170 ERR("page fault occurred ! Caused by bug ?\n");
1171 SetLastError( ERROR_INVALID_PARAMETER );
1172 return FALSE;
1174 __ENDTRY
1175 return locked;
1179 /***********************************************************************
1180 * GlobalHandle (KERNEL32.@)
1181 * Returns the handle associated with the specified pointer.
1183 * RETURNS
1184 * Handle: Success
1185 * NULL: Failure
1187 HGLOBAL WINAPI GlobalHandle(
1188 LPCVOID pmem /* [in] Pointer to global memory block */
1190 HGLOBAL handle;
1191 PGLOBAL32_INTERN maybe_intern;
1192 LPCVOID test;
1194 if (!pmem)
1196 SetLastError( ERROR_INVALID_PARAMETER );
1197 return 0;
1200 __TRY
1202 handle = 0;
1204 /* note that if pmem is a pointer to a a block allocated by */
1205 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1206 /* will fail. */
1207 if (ISPOINTER(pmem)) {
1208 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
1209 handle = (HGLOBAL)pmem; /* valid fixed block */
1210 break;
1212 handle = POINTER_TO_HANDLE(pmem);
1213 } else
1214 handle = (HGLOBAL)pmem;
1216 /* Now test handle either passed in or retrieved from pointer */
1217 maybe_intern = HANDLE_TO_INTERN( handle );
1218 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1219 test = maybe_intern->Pointer;
1220 if (HeapValidate( GetProcessHeap(), 0, (char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
1221 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
1222 break; /* valid moveable block */
1224 handle = 0;
1225 SetLastError( ERROR_INVALID_HANDLE );
1227 __EXCEPT(page_fault)
1229 SetLastError( ERROR_INVALID_HANDLE );
1230 return 0;
1232 __ENDTRY
1234 return handle;
1238 /***********************************************************************
1239 * GlobalReAlloc (KERNEL32.@)
1240 * RETURNS
1241 * Handle: Success
1242 * NULL: Failure
1244 HGLOBAL WINAPI GlobalReAlloc(
1245 HGLOBAL hmem, /* [in] Handle of global memory object */
1246 SIZE_T size, /* [in] New size of block */
1247 UINT flags /* [in] How to reallocate object */
1249 LPVOID palloc;
1250 HGLOBAL hnew;
1251 PGLOBAL32_INTERN pintern;
1252 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1254 hnew = 0;
1255 /* HeapLock(heap); */
1256 if(flags & GMEM_MODIFY) /* modify flags */
1258 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1260 /* make a fixed block moveable
1261 * actually only NT is able to do this. But it's soo simple
1263 if (hmem == 0)
1265 ERR("GlobalReAlloc32 with null handle!\n");
1266 SetLastError( ERROR_NOACCESS );
1267 return 0;
1269 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1270 hnew=GlobalAlloc( flags, size);
1271 palloc=GlobalLock(hnew);
1272 memcpy(palloc, (LPVOID) hmem, size);
1273 GlobalUnlock(hnew);
1274 GlobalFree(hmem);
1276 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1278 /* change the flags to make our block "discardable" */
1279 pintern=HANDLE_TO_INTERN(hmem);
1280 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1281 hnew=hmem;
1283 else
1285 SetLastError(ERROR_INVALID_PARAMETER);
1286 hnew = 0;
1289 else
1291 if(ISPOINTER(hmem))
1293 /* reallocate fixed memory */
1294 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
1296 else
1298 /* reallocate a moveable block */
1299 pintern=HANDLE_TO_INTERN(hmem);
1301 #if 0
1302 /* Apparently Windows doesn't care whether the handle is locked at this point */
1303 /* See also the same comment in GlobalFree() */
1304 if(pintern->LockCount>1) {
1305 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1306 SetLastError(ERROR_INVALID_HANDLE);
1307 } else
1308 #endif
1309 if(size!=0)
1311 hnew=hmem;
1312 if(pintern->Pointer)
1314 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
1315 (char *) pintern->Pointer-HGLOBAL_STORAGE,
1316 size+HGLOBAL_STORAGE)) == NULL)
1317 return 0; /* Block still valid */
1318 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1320 else
1322 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+HGLOBAL_STORAGE))
1323 == NULL)
1324 return 0;
1325 *(HGLOBAL *)palloc=hmem;
1326 pintern->Pointer=(char *) palloc+HGLOBAL_STORAGE;
1329 else
1331 if(pintern->Pointer)
1333 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-HGLOBAL_STORAGE);
1334 pintern->Pointer=NULL;
1339 /* HeapUnlock(heap); */
1340 return hnew;
1344 /***********************************************************************
1345 * GlobalFree (KERNEL32.@)
1346 * RETURNS
1347 * NULL: Success
1348 * Handle: Failure
1350 HGLOBAL WINAPI GlobalFree(
1351 HGLOBAL hmem /* [in] Handle of global memory object */
1353 PGLOBAL32_INTERN pintern;
1354 HGLOBAL hreturned;
1356 __TRY
1358 hreturned = 0;
1359 if(ISPOINTER(hmem)) /* POINTER */
1361 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1363 else /* HANDLE */
1365 /* HeapLock(heap); */
1366 pintern=HANDLE_TO_INTERN(hmem);
1368 if(pintern->Magic==MAGIC_GLOBAL_USED)
1371 /* WIN98 does not make this test. That is you can free a */
1372 /* block you have not unlocked. Go figure!! */
1373 /* if(pintern->LockCount!=0) */
1374 /* SetLastError(ERROR_INVALID_HANDLE); */
1376 if(pintern->Pointer)
1377 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-HGLOBAL_STORAGE))
1378 hreturned=hmem;
1379 if(!HeapFree(GetProcessHeap(), 0, pintern))
1380 hreturned=hmem;
1382 /* HeapUnlock(heap); */
1385 __EXCEPT(page_fault)
1387 ERR("page fault occurred ! Caused by bug ?\n");
1388 SetLastError( ERROR_INVALID_PARAMETER );
1389 return hmem;
1391 __ENDTRY
1392 return hreturned;
1396 /***********************************************************************
1397 * GlobalSize (KERNEL32.@)
1398 * RETURNS
1399 * Size in bytes of the global memory object
1400 * 0: Failure
1402 SIZE_T WINAPI GlobalSize(
1403 HGLOBAL hmem /* [in] Handle of global memory object */
1405 DWORD retval;
1406 PGLOBAL32_INTERN pintern;
1408 if(ISPOINTER(hmem))
1410 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1412 else
1414 /* HeapLock(heap); */
1415 pintern=HANDLE_TO_INTERN(hmem);
1417 if(pintern->Magic==MAGIC_GLOBAL_USED)
1419 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1420 return 0;
1421 retval=HeapSize(GetProcessHeap(), 0,
1422 (char *)(pintern->Pointer) - HGLOBAL_STORAGE );
1423 if (retval != (DWORD)-1) retval -= HGLOBAL_STORAGE;
1425 else
1427 WARN("invalid handle\n");
1428 retval=0;
1430 /* HeapUnlock(heap); */
1432 /* HeapSize returns 0xffffffff on failure */
1433 if (retval == 0xffffffff) retval = 0;
1434 return retval;
1438 /***********************************************************************
1439 * GlobalWire (KERNEL32.@)
1441 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1443 return GlobalLock( hmem );
1447 /***********************************************************************
1448 * GlobalUnWire (KERNEL32.@)
1450 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1452 return GlobalUnlock( hmem);
1456 /***********************************************************************
1457 * GlobalFix (KERNEL32.@)
1459 VOID WINAPI GlobalFix(HGLOBAL hmem)
1461 GlobalLock( hmem );
1465 /***********************************************************************
1466 * GlobalUnfix (KERNEL32.@)
1468 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1470 GlobalUnlock( hmem);
1474 /***********************************************************************
1475 * GlobalFlags (KERNEL32.@)
1476 * Returns information about the specified global memory object
1478 * NOTES
1479 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1481 * RETURNS
1482 * Value specifying allocation flags and lock count
1483 * GMEM_INVALID_HANDLE: Failure
1485 UINT WINAPI GlobalFlags(
1486 HGLOBAL hmem /* [in] Handle to global memory object */
1488 DWORD retval;
1489 PGLOBAL32_INTERN pintern;
1491 if(ISPOINTER(hmem))
1493 retval=0;
1495 else
1497 /* HeapLock(GetProcessHeap()); */
1498 pintern=HANDLE_TO_INTERN(hmem);
1499 if(pintern->Magic==MAGIC_GLOBAL_USED)
1501 retval=pintern->LockCount + (pintern->Flags<<8);
1502 if(pintern->Pointer==0)
1503 retval|= GMEM_DISCARDED;
1505 else
1507 WARN("Invalid handle: %p\n", hmem);
1508 retval=0;
1510 /* HeapUnlock(GetProcessHeap()); */
1512 return retval;
1516 /***********************************************************************
1517 * GlobalCompact (KERNEL32.@)
1519 SIZE_T WINAPI GlobalCompact( DWORD minfree )
1521 return 0; /* GlobalCompact does nothing in Win32 */
1525 /***********************************************************************
1526 * GlobalMemoryStatus (KERNEL32.@)
1527 * RETURNS
1528 * None
1530 VOID WINAPI GlobalMemoryStatus(
1531 LPMEMORYSTATUS lpmem
1533 static MEMORYSTATUS cached_memstatus;
1534 static int cache_lastchecked = 0;
1535 SYSTEM_INFO si;
1536 #ifdef linux
1537 FILE *f;
1538 #endif
1539 #ifdef __FreeBSD__
1540 int *tmp;
1541 int size_sys;
1542 #endif
1543 if (time(NULL)==cache_lastchecked) {
1544 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1545 return;
1547 cache_lastchecked = time(NULL);
1549 lpmem->dwMemoryLoad = 0;
1550 lpmem->dwTotalPhys = 16*1024*1024;
1551 lpmem->dwAvailPhys = 16*1024*1024;
1552 lpmem->dwTotalPageFile = 16*1024*1024;
1553 lpmem->dwAvailPageFile = 16*1024*1024;
1555 #ifdef linux
1556 f = fopen( "/proc/meminfo", "r" );
1557 if (f)
1559 char buffer[256];
1560 int total, used, free, shared, buffers, cached;
1562 lpmem->dwLength = sizeof(MEMORYSTATUS);
1563 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1564 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1565 while (fgets( buffer, sizeof(buffer), f ))
1567 /* old style /proc/meminfo ... */
1568 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1570 lpmem->dwTotalPhys += total;
1571 lpmem->dwAvailPhys += free + buffers + cached;
1573 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1575 lpmem->dwTotalPageFile += total;
1576 lpmem->dwAvailPageFile += free;
1579 /* new style /proc/meminfo ... */
1580 if (sscanf(buffer, "MemTotal: %d", &total))
1581 lpmem->dwTotalPhys = total*1024;
1582 if (sscanf(buffer, "MemFree: %d", &free))
1583 lpmem->dwAvailPhys = free*1024;
1584 if (sscanf(buffer, "SwapTotal: %d", &total))
1585 lpmem->dwTotalPageFile = total*1024;
1586 if (sscanf(buffer, "SwapFree: %d", &free))
1587 lpmem->dwAvailPageFile = free*1024;
1588 if (sscanf(buffer, "Buffers: %d", &buffers))
1589 lpmem->dwAvailPhys += buffers*1024;
1590 if (sscanf(buffer, "Cached: %d", &cached))
1591 lpmem->dwAvailPhys += cached*1024;
1593 fclose( f );
1595 if (lpmem->dwTotalPhys)
1597 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1598 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1599 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1600 / (TotalPhysical / 100);
1603 #elif defined(__FreeBSD__)
1604 sysctlbyname("hw.physmem", NULL, &size_sys, NULL, 0);
1605 tmp = malloc(size_sys * sizeof(int));
1606 sysctlbyname("hw.physmem", tmp, &size_sys, NULL, 0);
1607 if (tmp && *tmp)
1609 lpmem->dwTotalPhys = *tmp;
1610 free(tmp);
1611 sysctlbyname("hw.usermem", NULL, &size_sys, NULL, 0);
1612 tmp = malloc(size_sys * sizeof(int));
1613 sysctlbyname("hw.usermem", tmp, &size_sys, NULL, 0);
1614 if (tmp && *tmp)
1616 lpmem->dwAvailPhys = *tmp;
1617 lpmem->dwTotalPageFile = *tmp;
1618 lpmem->dwAvailPageFile = *tmp;
1619 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1620 } else
1622 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1623 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1624 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1625 lpmem->dwMemoryLoad = 0;
1627 free(tmp);
1630 #endif
1631 /* Some applications (e.g. QuickTime 6) crash if we tell them there
1632 * is more than 2GB of physical memory.
1634 if (lpmem->dwTotalPhys>2U*1024*1024*1024)
1636 lpmem->dwTotalPhys=2U*1024*1024*1024;
1637 lpmem->dwAvailPhys=2U*1024*1024*1024;
1640 /* FIXME: should do something for other systems */
1641 GetSystemInfo(&si);
1642 lpmem->dwTotalVirtual = (char*)si.lpMaximumApplicationAddress-(char*)si.lpMinimumApplicationAddress;
1643 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1644 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1645 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1647 /* it appears some memory display programs want to divide by these values */
1648 if(lpmem->dwTotalPageFile==0)
1649 lpmem->dwTotalPageFile++;
1651 if(lpmem->dwAvailPageFile==0)
1652 lpmem->dwAvailPageFile++;
1654 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1655 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1656 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1657 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1658 lpmem->dwAvailVirtual);
1661 /***********************************************************************
1662 * A20Proc (KERNEL.165)
1663 * A20_Proc (SYSTEM.20)
1665 void WINAPI A20Proc16( WORD unused )
1667 /* this is also a NOP in Windows */
1670 /***********************************************************************
1671 * LimitEMSPages (KERNEL.156)
1673 DWORD WINAPI LimitEMSPages16( DWORD unused )
1675 return 0;