Duplicated wsprintf implementation in shlwapi.
[wine.git] / memory / global.c
blob17e6b001fdb9cc2426deb647d8132295e7d71871
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 #include <unistd.h>
30 #include <string.h>
31 #ifdef HAVE_SYS_SYSCTL_H
32 #include <sys/sysctl.h>
33 #endif
35 #include "wine/winbase16.h"
36 #include "wine/exception.h"
37 #include "global.h"
38 #include "toolhelp.h"
39 #include "selectors.h"
40 #include "miscemu.h"
41 #include "stackframe.h"
42 #include "module.h"
43 #include "wine/debug.h"
44 #include "winerror.h"
45 #include "msvcrt/excpt.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(global);
49 /* Global arena block */
50 typedef struct
52 DWORD base; /* Base address (0 if discarded) */
53 DWORD size; /* Size in bytes (0 indicates a free block) */
54 HGLOBAL16 handle; /* Handle for this block */
55 HGLOBAL16 hOwner; /* Owner of this block */
56 BYTE lockCount; /* Count of GlobalFix() calls */
57 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
58 BYTE flags; /* Allocation flags */
59 BYTE selCount; /* Number of selectors allocated for this block */
60 } GLOBALARENA;
62 /* Flags definitions */
63 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
64 #define GA_DGROUP 0x04
65 #define GA_DISCARDABLE 0x08
66 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
67 #define GA_DOSMEM 0x20
69 /* Arena array */
70 static GLOBALARENA *pGlobalArena = NULL;
71 static int globalArenaSize = 0;
73 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
75 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
76 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
78 /* filter for page-fault exceptions */
79 /* It is possible for a bogus global pointer to cause a */
80 /* page zero reference, so I include EXCEPTION_PRIV_INSTRUCTION too. */
82 static WINE_EXCEPTION_FILTER(page_fault)
84 switch (GetExceptionCode()) {
85 case (EXCEPTION_ACCESS_VIOLATION):
86 case (EXCEPTION_PRIV_INSTRUCTION):
87 return EXCEPTION_EXECUTE_HANDLER;
88 default:
89 return EXCEPTION_CONTINUE_SEARCH;
93 /***********************************************************************
94 * GLOBAL_GetArena
96 * Return the arena for a given selector, growing the arena array if needed.
98 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
100 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
102 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
103 GLOBALARENA *pNewArena = realloc( pGlobalArena,
104 newsize * sizeof(GLOBALARENA) );
105 if (!pNewArena) return 0;
106 pGlobalArena = pNewArena;
107 memset( pGlobalArena + globalArenaSize, 0,
108 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
109 globalArenaSize = newsize;
111 return pGlobalArena + (sel >> __AHSHIFT);
114 void debug_handles(void)
116 int printed=0;
117 int i;
118 for (i = globalArenaSize-1 ; i>=0 ; i--) {
119 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
120 printed=1;
121 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
124 if (printed)
125 DPRINTF("\n");
129 /***********************************************************************
130 * GLOBAL_CreateBlock
132 * Create a global heap block for a fixed range of linear memory.
134 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
135 HGLOBAL16 hOwner, unsigned char selflags )
137 WORD sel, selcount;
138 GLOBALARENA *pArena;
140 /* Allocate the selector(s) */
142 sel = SELECTOR_AllocBlock( ptr, size, selflags );
143 if (!sel) return 0;
144 selcount = (size + 0xffff) / 0x10000;
146 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
148 SELECTOR_FreeBlock( sel );
149 return 0;
152 /* Fill the arena block */
154 pArena->base = (DWORD)ptr;
155 pArena->size = GetSelectorLimit16(sel) + 1;
156 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
157 pArena->hOwner = hOwner;
158 pArena->lockCount = 0;
159 pArena->pageLockCount = 0;
160 pArena->flags = flags & GA_MOVEABLE;
161 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
162 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
163 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
164 pArena->selCount = selcount;
165 if (selcount > 1) /* clear the next arena blocks */
166 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
168 return pArena->handle;
172 /***********************************************************************
173 * GLOBAL_FreeBlock
175 * Free a block allocated by GLOBAL_CreateBlock, without touching
176 * the associated linear memory range.
178 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
180 WORD sel;
181 GLOBALARENA *pArena;
183 if (!handle) return TRUE;
184 sel = GlobalHandleToSel16( handle );
185 if (!VALID_HANDLE(sel))
186 return FALSE;
187 pArena = GET_ARENA_PTR(sel);
188 SELECTOR_FreeBlock( sel );
189 memset( pArena, 0, sizeof(GLOBALARENA) );
190 return TRUE;
193 /***********************************************************************
194 * GLOBAL_MoveBlock
196 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
198 WORD sel;
199 GLOBALARENA *pArena;
201 if (!handle) return TRUE;
202 sel = GlobalHandleToSel16( handle );
203 if (!VALID_HANDLE(sel))
204 return FALSE;
205 pArena = GET_ARENA_PTR(sel);
206 if (pArena->selCount != 1)
207 return FALSE;
209 pArena->base = (DWORD)ptr;
210 pArena->size = size;
211 SELECTOR_ReallocBlock( sel, ptr, size );
212 return TRUE;
215 /***********************************************************************
216 * GLOBAL_Alloc
218 * Implementation of GlobalAlloc16()
220 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
222 void *ptr;
223 HGLOBAL16 handle;
225 TRACE("%ld flags=%04x\n", size, flags );
227 /* If size is 0, create a discarded block */
229 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
231 /* Fixup the size */
233 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
234 size = (size + 0x1f) & ~0x1f;
236 /* Allocate the linear memory */
237 ptr = HeapAlloc( GetProcessHeap(), 0, size );
238 /* FIXME: free discardable blocks and try again? */
239 if (!ptr) return 0;
241 /* Allocate the selector(s) */
243 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
244 if (!handle)
246 HeapFree( GetProcessHeap(), 0, ptr );
247 return 0;
250 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
251 return handle;
254 /***********************************************************************
255 * GlobalAlloc (KERNEL.15)
256 * GlobalAlloc16 (KERNEL32.24)
257 * RETURNS
258 * Handle: Success
259 * NULL: Failure
261 HGLOBAL16 WINAPI GlobalAlloc16(
262 UINT16 flags, /* [in] Object allocation attributes */
263 DWORD size /* [in] Number of bytes to allocate */
265 HANDLE16 owner = GetCurrentPDB16();
267 if (flags & GMEM_DDESHARE)
268 owner = GetExePtr(owner); /* Make it a module handle */
269 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
273 /***********************************************************************
274 * GlobalReAlloc (KERNEL.16)
275 * GlobalReAlloc16 (KERNEL32.@)
276 * RETURNS
277 * Handle: Success
278 * NULL: Failure
280 HGLOBAL16 WINAPI GlobalReAlloc16(
281 HGLOBAL16 handle, /* [in] Handle of global memory object */
282 DWORD size, /* [in] New size of block */
283 UINT16 flags /* [in] How to reallocate object */
285 WORD selcount;
286 DWORD oldsize;
287 void *ptr, *newptr;
288 GLOBALARENA *pArena, *pNewArena;
289 WORD sel = GlobalHandleToSel16( handle );
291 TRACE("%04x %ld flags=%04x\n",
292 handle, size, flags );
293 if (!handle) return 0;
295 if (!VALID_HANDLE(handle)) {
296 WARN("Invalid handle 0x%04x!\n", handle);
297 return 0;
299 pArena = GET_ARENA_PTR( handle );
301 /* Discard the block if requested */
303 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
305 if (!(pArena->flags & GA_MOVEABLE) ||
306 !(pArena->flags & GA_DISCARDABLE) ||
307 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
308 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
309 pArena->base = 0;
311 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
312 * change the selector if we are shrinking the block.
313 * FIXME: shouldn't we keep selectors until the block is deleted?
315 SELECTOR_ReallocBlock( sel, 0, 1 );
316 return handle;
319 /* Fixup the size */
321 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
322 if (size == 0) size = 0x20;
323 else size = (size + 0x1f) & ~0x1f;
325 /* Change the flags */
327 if (flags & GMEM_MODIFY)
329 /* Change the flags, leaving GA_DGROUP alone */
330 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
331 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
332 return handle;
335 /* Reallocate the linear memory */
337 ptr = (void *)pArena->base;
338 oldsize = pArena->size;
339 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
340 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
342 if (pArena->flags & GA_DOSMEM)
343 newptr = DOSMEM_ResizeBlock(ptr, size, NULL);
344 else
345 /* if more then one reader (e.g. some pointer has been given out by GetVDMPointer32W16),
346 only try to realloc in place */
347 newptr = HeapReAlloc( GetProcessHeap(),
348 (pArena->pageLockCount > 0)?HEAP_REALLOC_IN_PLACE_ONLY:0, ptr, size );
349 if (!newptr)
351 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
352 if (pArena->pageLockCount <1)
354 HeapFree( GetProcessHeap(), 0, ptr );
355 SELECTOR_FreeBlock( sel );
356 memset( pArena, 0, sizeof(GLOBALARENA) );
358 return 0;
360 ptr = newptr;
362 /* Reallocate the selector(s) */
364 sel = SELECTOR_ReallocBlock( sel, ptr, size );
365 if (!sel)
367 HeapFree( GetProcessHeap(), 0, ptr );
368 memset( pArena, 0, sizeof(GLOBALARENA) );
369 return 0;
371 selcount = (size + 0xffff) / 0x10000;
373 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
375 HeapFree( GetProcessHeap(), 0, ptr );
376 SELECTOR_FreeBlock( sel );
377 return 0;
380 /* Fill the new arena block
381 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
383 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
384 pNewArena->base = (DWORD)ptr;
385 pNewArena->size = GetSelectorLimit16(sel) + 1;
386 pNewArena->selCount = selcount;
387 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
389 if (selcount > 1) /* clear the next arena blocks */
390 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
392 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
393 memset( (char *)ptr + oldsize, 0, size - oldsize );
394 return pNewArena->handle;
398 /***********************************************************************
399 * GlobalFree (KERNEL.17)
400 * GlobalFree16 (KERNEL32.31)
401 * RETURNS
402 * NULL: Success
403 * Handle: Failure
405 HGLOBAL16 WINAPI GlobalFree16(
406 HGLOBAL16 handle /* [in] Handle of global memory object */
408 void *ptr;
410 if (!VALID_HANDLE(handle)) {
411 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
412 return 0;
414 ptr = (void *)GET_ARENA_PTR(handle)->base;
416 TRACE("%04x\n", handle );
417 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
418 if (ptr) HeapFree( GetProcessHeap(), 0, ptr );
419 return 0;
423 /***********************************************************************
424 * GlobalLock (KERNEL.18)
426 * This is the GlobalLock16() function used by 16-bit code.
428 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
430 WORD sel = GlobalHandleToSel16( handle );
431 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
433 if (handle)
435 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
437 if (!VALID_HANDLE(handle)) {
438 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
439 sel = 0;
441 else if (!GET_ARENA_PTR(handle)->base)
442 sel = 0;
443 else
444 GET_ARENA_PTR(handle)->lockCount++;
447 CURRENT_STACK16->ecx = sel; /* selector must be returned in CX as well */
448 return MAKESEGPTR( sel, 0 );
452 /**********************************************************************
453 * K32WOWGlobalLock16 (KERNEL32.60)
455 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 hMem )
457 return WIN16_GlobalLock16( hMem );
461 /***********************************************************************
462 * GlobalLock16 (KERNEL32.25)
464 * This is the GlobalLock16() function used by 32-bit code.
466 * RETURNS
467 * Pointer to first byte of memory block
468 * NULL: Failure
470 LPVOID WINAPI GlobalLock16(
471 HGLOBAL16 handle /* [in] Handle of global memory object */
473 if (!handle) return 0;
474 if (!VALID_HANDLE(handle))
475 return (LPVOID)0;
476 GET_ARENA_PTR(handle)->lockCount++;
477 return (LPVOID)GET_ARENA_PTR(handle)->base;
481 /***********************************************************************
482 * GlobalUnlock (KERNEL.19)
483 * GlobalUnlock16 (KERNEL32.26)
484 * NOTES
485 * Should the return values be cast to booleans?
487 * RETURNS
488 * TRUE: Object is still locked
489 * FALSE: Object is unlocked
491 BOOL16 WINAPI GlobalUnlock16(
492 HGLOBAL16 handle /* [in] Handle of global memory object */
494 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
495 if (!VALID_HANDLE(handle)) {
496 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
497 return 0;
499 TRACE("%04x\n", handle );
500 if (pArena->lockCount) pArena->lockCount--;
501 return pArena->lockCount;
504 /***********************************************************************
505 * GlobalChangeLockCount (KERNEL.365)
507 * This is declared as a register function as it has to preserve
508 * *all* registers, even AX/DX !
511 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
512 CONTEXT86 *context )
514 if ( delta == 1 )
515 GlobalLock16( handle );
516 else if ( delta == -1 )
517 GlobalUnlock16( handle );
518 else
519 ERR("(%04X, %d): strange delta value\n", handle, delta );
522 /***********************************************************************
523 * GlobalSize (KERNEL.20)
524 * GlobalSize16 (KERNEL32.32)
525 * RETURNS
526 * Size in bytes of object
527 * 0: Failure
529 DWORD WINAPI GlobalSize16(
530 HGLOBAL16 handle /* [in] Handle of global memory object */
532 TRACE("%04x\n", handle );
533 if (!handle) return 0;
534 if (!VALID_HANDLE(handle))
535 return 0;
536 return GET_ARENA_PTR(handle)->size;
540 /***********************************************************************
541 * GlobalHandle (KERNEL.21)
542 * NOTES
543 * Why is GlobalHandleToSel used here with the sel as input?
545 * RETURNS
546 * Handle: Success
547 * NULL: Failure
549 DWORD WINAPI GlobalHandle16(
550 WORD sel /* [in] Address of global memory block */
552 TRACE("%04x\n", sel );
553 if (!VALID_HANDLE(sel)) {
554 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
555 return 0;
557 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
560 /***********************************************************************
561 * GlobalHandleNoRIP (KERNEL.159)
563 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
565 int i;
566 for (i = globalArenaSize-1 ; i>=0 ; i--) {
567 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
568 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
570 return 0;
574 /***********************************************************************
575 * GlobalFlags (KERNEL.22)
576 * GlobalFlags16 (KERNEL32.@)
577 * NOTES
578 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
579 * handle?
581 * RETURNS
582 * Value specifying flags and lock count
583 * GMEM_INVALID_HANDLE: Invalid handle
585 UINT16 WINAPI GlobalFlags16(
586 HGLOBAL16 handle /* [in] Handle of global memory object */
588 GLOBALARENA *pArena;
590 TRACE("%04x\n", handle );
591 if (!VALID_HANDLE(handle)) {
592 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
593 return 0;
595 pArena = GET_ARENA_PTR(handle);
596 return pArena->lockCount |
597 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
598 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
602 /***********************************************************************
603 * LockSegment (KERNEL.23)
605 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
607 TRACE("%04x\n", handle );
608 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
609 if (!VALID_HANDLE(handle)) {
610 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
611 return 0;
613 GET_ARENA_PTR(handle)->lockCount++;
614 return handle;
618 /***********************************************************************
619 * UnlockSegment (KERNEL.24)
621 void WINAPI UnlockSegment16( HGLOBAL16 handle )
623 TRACE("%04x\n", handle );
624 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
625 if (!VALID_HANDLE(handle)) {
626 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
627 return;
629 GET_ARENA_PTR(handle)->lockCount--;
630 /* FIXME: this ought to return the lock count in CX (go figure...) */
634 /***********************************************************************
635 * GlobalCompact (KERNEL.25)
637 DWORD WINAPI GlobalCompact16( DWORD desired )
639 return GLOBAL_MAX_ALLOC_SIZE;
643 /***********************************************************************
644 * GlobalFreeAll (KERNEL.26)
646 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
648 DWORD i;
649 GLOBALARENA *pArena;
651 pArena = pGlobalArena;
652 for (i = 0; i < globalArenaSize; i++, pArena++)
654 if ((pArena->size != 0) && (pArena->hOwner == owner))
655 GlobalFree16( pArena->handle );
660 /***********************************************************************
661 * GlobalWire (KERNEL.111)
662 * GlobalWire16 (KERNEL32.29)
664 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
666 return WIN16_GlobalLock16( handle );
670 /***********************************************************************
671 * GlobalUnWire (KERNEL.112)
672 * GlobalUnWire16 (KERNEL32.30)
674 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
676 return !GlobalUnlock16( handle );
680 /***********************************************************************
681 * SetSwapAreaSize (KERNEL.106)
683 LONG WINAPI SetSwapAreaSize16( WORD size )
685 FIXME("(%d) - stub!\n", size );
686 return MAKELONG( size, 0xffff );
690 /***********************************************************************
691 * GlobalLRUOldest (KERNEL.163)
693 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
695 TRACE("%04x\n", handle );
696 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
697 return handle;
701 /***********************************************************************
702 * GlobalLRUNewest (KERNEL.164)
704 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
706 TRACE("%04x\n", handle );
707 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
708 return handle;
712 /***********************************************************************
713 * GetFreeSpace (KERNEL.169)
715 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
717 MEMORYSTATUS ms;
718 GlobalMemoryStatus( &ms );
719 return ms.dwAvailVirtual;
722 /***********************************************************************
723 * GlobalDOSAlloc (KERNEL.184)
724 * RETURNS
725 * Address (HW=Paragraph segment; LW=Selector)
727 DWORD WINAPI GlobalDOSAlloc16(
728 DWORD size /* [in] Number of bytes to be allocated */
730 UINT16 uParagraph;
731 LPVOID lpBlock = DOSMEM_GetBlock( size, &uParagraph );
733 if( lpBlock )
735 HMODULE16 hModule = GetModuleHandle16("KERNEL");
736 WORD wSelector;
737 GLOBALARENA *pArena;
739 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
740 pArena = GET_ARENA_PTR(wSelector);
741 pArena->flags |= GA_DOSMEM;
742 return MAKELONG(wSelector,uParagraph);
744 return 0;
748 /***********************************************************************
749 * GlobalDOSFree (KERNEL.185)
750 * RETURNS
751 * NULL: Success
752 * sel: Failure
754 WORD WINAPI GlobalDOSFree16(
755 WORD sel /* [in] Selector */
757 DWORD block = GetSelectorBase(sel);
759 if( block && block < 0x100000 )
761 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
762 if( DOSMEM_FreeBlock( lpBlock ) )
763 GLOBAL_FreeBlock( sel );
764 sel = 0;
766 return sel;
770 /***********************************************************************
771 * GlobalPageLock (KERNEL.191)
772 * GlobalSmartPageLock(KERNEL.230)
774 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
776 TRACE("%04x\n", handle );
777 if (!VALID_HANDLE(handle)) {
778 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
779 return 0;
781 return ++(GET_ARENA_PTR(handle)->pageLockCount);
785 /***********************************************************************
786 * GlobalPageUnlock (KERNEL.192)
787 * GlobalSmartPageUnlock(KERNEL.231)
789 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
791 TRACE("%04x\n", handle );
792 if (!VALID_HANDLE(handle)) {
793 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
794 return 0;
796 return --(GET_ARENA_PTR(handle)->pageLockCount);
800 /***********************************************************************
801 * GlobalFix (KERNEL.197)
802 * GlobalFix16 (KERNEL32.27)
804 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
806 TRACE("%04x\n", handle );
807 if (!VALID_HANDLE(handle)) {
808 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
809 return 0;
811 GET_ARENA_PTR(handle)->lockCount++;
813 return GlobalHandleToSel16(handle);
817 /***********************************************************************
818 * GlobalUnfix (KERNEL.198)
819 * GlobalUnfix16 (KERNEL32.28)
821 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
823 TRACE("%04x\n", handle );
824 if (!VALID_HANDLE(handle)) {
825 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
826 return;
828 GET_ARENA_PTR(handle)->lockCount--;
832 /***********************************************************************
833 * FarSetOwner (KERNEL.403)
835 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
837 if (!VALID_HANDLE(handle)) {
838 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
839 return;
841 GET_ARENA_PTR(handle)->hOwner = hOwner;
845 /***********************************************************************
846 * FarGetOwner (KERNEL.404)
848 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
850 if (!VALID_HANDLE(handle)) {
851 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
852 return 0;
854 return GET_ARENA_PTR(handle)->hOwner;
858 /***********************************************************************
859 * GlobalHandleToSel (TOOLHELP.50)
861 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
863 if (!handle) return 0;
864 if (!VALID_HANDLE(handle)) {
865 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
866 return 0;
868 if (!(handle & 7))
870 WARN("Program attempted invalid selector conversion\n" );
871 return handle - 1;
873 return handle | 7;
877 /***********************************************************************
878 * GlobalFirst (TOOLHELP.51)
880 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
882 if (wFlags == GLOBAL_LRU) return FALSE;
883 pGlobal->dwNext = 0;
884 return GlobalNext16( pGlobal, wFlags );
888 /***********************************************************************
889 * GlobalNext (TOOLHELP.52)
891 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
893 GLOBALARENA *pArena;
895 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
896 pArena = pGlobalArena + pGlobal->dwNext;
897 if (wFlags == GLOBAL_FREE) /* only free blocks */
899 int i;
900 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
901 if (pArena->size == 0) break; /* block is free */
902 if (i >= globalArenaSize) return FALSE;
903 pGlobal->dwNext = i;
906 pGlobal->dwAddress = pArena->base;
907 pGlobal->dwBlockSize = pArena->size;
908 pGlobal->hBlock = pArena->handle;
909 pGlobal->wcLock = pArena->lockCount;
910 pGlobal->wcPageLock = pArena->pageLockCount;
911 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
912 pGlobal->wHeapPresent = FALSE;
913 pGlobal->hOwner = pArena->hOwner;
914 pGlobal->wType = GT_UNKNOWN;
915 pGlobal->wData = 0;
916 pGlobal->dwNext++;
917 return TRUE;
921 /***********************************************************************
922 * GlobalInfo (TOOLHELP.53)
924 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
926 int i;
927 GLOBALARENA *pArena;
929 pInfo->wcItems = globalArenaSize;
930 pInfo->wcItemsFree = 0;
931 pInfo->wcItemsLRU = 0;
932 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
933 if (pArena->size == 0) pInfo->wcItemsFree++;
934 return TRUE;
938 /***********************************************************************
939 * GlobalEntryHandle (TOOLHELP.54)
941 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
943 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
945 pGlobal->dwAddress = pArena->base;
946 pGlobal->dwBlockSize = pArena->size;
947 pGlobal->hBlock = pArena->handle;
948 pGlobal->wcLock = pArena->lockCount;
949 pGlobal->wcPageLock = pArena->pageLockCount;
950 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
951 pGlobal->wHeapPresent = FALSE;
952 pGlobal->hOwner = pArena->hOwner;
953 pGlobal->wType = GT_UNKNOWN;
954 pGlobal->wData = 0;
955 pGlobal->dwNext++;
956 return TRUE;
960 /***********************************************************************
961 * GlobalEntryModule (TOOLHELP.55)
963 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
964 WORD wSeg )
966 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
967 return FALSE;
971 /***********************************************************************
972 * MemManInfo (TOOLHELP.72)
974 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
976 MEMORYSTATUS status;
979 * Not unsurprisingly although the documention says you
980 * _must_ provide the size in the dwSize field, this function
981 * (under Windows) always fills the structure and returns true.
983 GlobalMemoryStatus( &status );
984 info->wPageSize = getpagesize();
985 info->dwLargestFreeBlock = status.dwAvailVirtual;
986 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
987 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
988 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
989 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
990 info->dwFreePages = info->dwMaxPagesAvailable;
991 info->dwTotalPages = info->dwTotalLinearSpace;
992 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
993 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
994 return TRUE;
997 /***********************************************************************
998 * GetFreeMemInfo (KERNEL.316)
1000 DWORD WINAPI GetFreeMemInfo16(void)
1002 MEMMANINFO info;
1003 MemManInfo16( &info );
1004 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1008 * Win32 Global heap functions (GlobalXXX).
1009 * These functions included in Win32 for compatibility with 16 bit Windows
1010 * Especially the moveable blocks and handles are oldish.
1011 * But the ability to directly allocate memory with GPTR and LPTR is widely
1012 * used.
1014 * The handle stuff looks horrible, but it's implemented almost like Win95
1015 * does it.
1019 #define MAGIC_GLOBAL_USED 0x5342
1020 #define GLOBAL_LOCK_MAX 0xFF
1021 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
1022 #define INTERN_TO_HANDLE(i) ((HGLOBAL) &((i)->Pointer))
1023 #define POINTER_TO_HANDLE(p) (*(((HGLOBAL *)(p))-1))
1024 #define ISHANDLE(h) (((DWORD)(h)&2)!=0)
1025 #define ISPOINTER(h) (((DWORD)(h)&2)==0)
1027 typedef struct __GLOBAL32_INTERN
1029 WORD Magic;
1030 LPVOID Pointer WINE_PACKED;
1031 BYTE Flags;
1032 BYTE LockCount;
1033 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
1036 /***********************************************************************
1037 * GlobalAlloc (KERNEL32.@)
1038 * RETURNS
1039 * Handle: Success
1040 * NULL: Failure
1042 HGLOBAL WINAPI GlobalAlloc(
1043 UINT flags, /* [in] Object allocation attributes */
1044 DWORD size /* [in] Number of bytes to allocate */
1046 PGLOBAL32_INTERN pintern;
1047 DWORD hpflags;
1048 LPVOID palloc;
1050 if(flags&GMEM_ZEROINIT)
1051 hpflags=HEAP_ZERO_MEMORY;
1052 else
1053 hpflags=0;
1055 TRACE("() flags=%04x\n", flags );
1057 if((flags & GMEM_MOVEABLE)==0) /* POINTER */
1059 palloc=HeapAlloc(GetProcessHeap(), hpflags, size);
1060 return (HGLOBAL) palloc;
1062 else /* HANDLE */
1064 /* HeapLock(heap); */
1066 pintern=HeapAlloc(GetProcessHeap(), 0, sizeof(GLOBAL32_INTERN));
1067 if (!pintern) return 0;
1068 if(size)
1070 if (!(palloc=HeapAlloc(GetProcessHeap(), hpflags, size+sizeof(HGLOBAL)))) {
1071 HeapFree(GetProcessHeap(), 0, pintern);
1072 return 0;
1074 *(HGLOBAL *)palloc=INTERN_TO_HANDLE(pintern);
1075 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1077 else
1078 pintern->Pointer=NULL;
1079 pintern->Magic=MAGIC_GLOBAL_USED;
1080 pintern->Flags=flags>>8;
1081 pintern->LockCount=0;
1083 /* HeapUnlock(heap); */
1085 return INTERN_TO_HANDLE(pintern);
1090 /***********************************************************************
1091 * GlobalLock (KERNEL32.@)
1092 * RETURNS
1093 * Pointer to first byte of block
1094 * NULL: Failure
1096 LPVOID WINAPI GlobalLock(
1097 HGLOBAL hmem /* [in] Handle of global memory object */
1099 PGLOBAL32_INTERN pintern;
1100 LPVOID palloc;
1102 if(ISPOINTER(hmem))
1103 return (LPVOID) hmem;
1105 /* HeapLock(GetProcessHeap()); */
1107 pintern=HANDLE_TO_INTERN(hmem);
1108 if(pintern->Magic==MAGIC_GLOBAL_USED)
1110 if(pintern->LockCount<GLOBAL_LOCK_MAX)
1111 pintern->LockCount++;
1112 palloc=pintern->Pointer;
1114 else
1116 WARN("invalid handle\n");
1117 palloc=(LPVOID) NULL;
1118 SetLastError(ERROR_INVALID_HANDLE);
1120 /* HeapUnlock(GetProcessHeap()); */;
1121 return palloc;
1125 /***********************************************************************
1126 * GlobalUnlock (KERNEL32.@)
1127 * RETURNS
1128 * TRUE: Object is still locked
1129 * FALSE: Object is unlocked
1131 BOOL WINAPI GlobalUnlock(
1132 HGLOBAL hmem /* [in] Handle of global memory object */
1134 PGLOBAL32_INTERN pintern;
1135 BOOL locked;
1137 if (ISPOINTER(hmem)) return FALSE;
1139 __TRY
1141 /* HeapLock(GetProcessHeap()); */
1142 pintern=HANDLE_TO_INTERN(hmem);
1143 if(pintern->Magic==MAGIC_GLOBAL_USED)
1145 if((pintern->LockCount<GLOBAL_LOCK_MAX)&&(pintern->LockCount>0))
1146 pintern->LockCount--;
1148 locked = (pintern->LockCount != 0);
1149 if (!locked) SetLastError(NO_ERROR);
1151 else
1153 WARN("invalid handle\n");
1154 SetLastError(ERROR_INVALID_HANDLE);
1155 locked=FALSE;
1157 /* HeapUnlock(GetProcessHeap()); */
1159 __EXCEPT(page_fault)
1161 ERR("page fault occurred ! Caused by bug ?\n");
1162 SetLastError( ERROR_INVALID_PARAMETER );
1163 return FALSE;
1165 __ENDTRY
1166 return locked;
1170 /***********************************************************************
1171 * GlobalHandle (KERNEL32.@)
1172 * Returns the handle associated with the specified pointer.
1174 * RETURNS
1175 * Handle: Success
1176 * NULL: Failure
1178 HGLOBAL WINAPI GlobalHandle(
1179 LPCVOID pmem /* [in] Pointer to global memory block */
1181 HGLOBAL handle;
1182 PGLOBAL32_INTERN maybe_intern;
1183 LPCVOID test;
1185 if (!pmem)
1187 SetLastError( ERROR_INVALID_PARAMETER );
1188 return 0;
1191 __TRY
1193 handle = 0;
1195 /* note that if pmem is a pointer to a a block allocated by */
1196 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
1197 /* will fail. */
1198 if (ISPOINTER(pmem)) {
1199 if (HeapValidate( GetProcessHeap(), 0, pmem )) {
1200 handle = (HGLOBAL)pmem; /* valid fixed block */
1201 break;
1203 handle = POINTER_TO_HANDLE(pmem);
1204 } else
1205 handle = (HGLOBAL)pmem;
1207 /* Now test handle either passed in or retrieved from pointer */
1208 maybe_intern = HANDLE_TO_INTERN( handle );
1209 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
1210 test = maybe_intern->Pointer;
1211 if (HeapValidate( GetProcessHeap(), 0, ((HGLOBAL *)test)-1 ) && /* obj(-handle) valid arena? */
1212 HeapValidate( GetProcessHeap(), 0, maybe_intern )) /* intern valid arena? */
1213 break; /* valid moveable block */
1215 handle = 0;
1216 SetLastError( ERROR_INVALID_HANDLE );
1218 __EXCEPT(page_fault)
1220 SetLastError( ERROR_INVALID_HANDLE );
1221 return 0;
1223 __ENDTRY
1225 return handle;
1229 /***********************************************************************
1230 * GlobalReAlloc (KERNEL32.@)
1231 * RETURNS
1232 * Handle: Success
1233 * NULL: Failure
1235 HGLOBAL WINAPI GlobalReAlloc(
1236 HGLOBAL hmem, /* [in] Handle of global memory object */
1237 DWORD size, /* [in] New size of block */
1238 UINT flags /* [in] How to reallocate object */
1240 LPVOID palloc;
1241 HGLOBAL hnew;
1242 PGLOBAL32_INTERN pintern;
1243 DWORD heap_flags = (flags & GMEM_ZEROINIT) ? HEAP_ZERO_MEMORY : 0;
1245 hnew = 0;
1246 /* HeapLock(heap); */
1247 if(flags & GMEM_MODIFY) /* modify flags */
1249 if( ISPOINTER(hmem) && (flags & GMEM_MOVEABLE))
1251 /* make a fixed block moveable
1252 * actually only NT is able to do this. But it's soo simple
1254 if (hmem == 0)
1256 ERR("GlobalReAlloc32 with null handle!\n");
1257 SetLastError( ERROR_NOACCESS );
1258 return 0;
1260 size=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1261 hnew=GlobalAlloc( flags, size);
1262 palloc=GlobalLock(hnew);
1263 memcpy(palloc, (LPVOID) hmem, size);
1264 GlobalUnlock(hnew);
1265 GlobalFree(hmem);
1267 else if( ISPOINTER(hmem) &&(flags & GMEM_DISCARDABLE))
1269 /* change the flags to make our block "discardable" */
1270 pintern=HANDLE_TO_INTERN(hmem);
1271 pintern->Flags = pintern->Flags | (GMEM_DISCARDABLE >> 8);
1272 hnew=hmem;
1274 else
1276 SetLastError(ERROR_INVALID_PARAMETER);
1277 hnew = 0;
1280 else
1282 if(ISPOINTER(hmem))
1284 /* reallocate fixed memory */
1285 hnew=(HGLOBAL)HeapReAlloc(GetProcessHeap(), heap_flags, (LPVOID) hmem, size);
1287 else
1289 /* reallocate a moveable block */
1290 pintern=HANDLE_TO_INTERN(hmem);
1292 #if 0
1293 /* Apparently Windows doesn't care whether the handle is locked at this point */
1294 /* See also the same comment in GlobalFree() */
1295 if(pintern->LockCount>1) {
1296 ERR("handle 0x%08lx is still locked, cannot realloc!\n",(DWORD)hmem);
1297 SetLastError(ERROR_INVALID_HANDLE);
1298 } else
1299 #endif
1300 if(size!=0)
1302 hnew=hmem;
1303 if(pintern->Pointer)
1305 if((palloc = HeapReAlloc(GetProcessHeap(), heap_flags,
1306 (char *) pintern->Pointer-sizeof(HGLOBAL),
1307 size+sizeof(HGLOBAL))) == NULL)
1308 return 0; /* Block still valid */
1309 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1311 else
1313 if((palloc=HeapAlloc(GetProcessHeap(), heap_flags, size+sizeof(HGLOBAL)))
1314 == NULL)
1315 return 0;
1316 *(HGLOBAL *)palloc=hmem;
1317 pintern->Pointer=(char *) palloc+sizeof(HGLOBAL);
1320 else
1322 if(pintern->Pointer)
1324 HeapFree(GetProcessHeap(), 0, (char *) pintern->Pointer-sizeof(HGLOBAL));
1325 pintern->Pointer=NULL;
1330 /* HeapUnlock(heap); */
1331 return hnew;
1335 /***********************************************************************
1336 * GlobalFree (KERNEL32.@)
1337 * RETURNS
1338 * NULL: Success
1339 * Handle: Failure
1341 HGLOBAL WINAPI GlobalFree(
1342 HGLOBAL hmem /* [in] Handle of global memory object */
1344 PGLOBAL32_INTERN pintern;
1345 HGLOBAL hreturned;
1347 __TRY
1349 hreturned = 0;
1350 if(ISPOINTER(hmem)) /* POINTER */
1352 if(!HeapFree(GetProcessHeap(), 0, (LPVOID) hmem)) hmem = 0;
1354 else /* HANDLE */
1356 /* HeapLock(heap); */
1357 pintern=HANDLE_TO_INTERN(hmem);
1359 if(pintern->Magic==MAGIC_GLOBAL_USED)
1362 /* WIN98 does not make this test. That is you can free a */
1363 /* block you have not unlocked. Go figure!! */
1364 /* if(pintern->LockCount!=0) */
1365 /* SetLastError(ERROR_INVALID_HANDLE); */
1367 if(pintern->Pointer)
1368 if(!HeapFree(GetProcessHeap(), 0, (char *)(pintern->Pointer)-sizeof(HGLOBAL)))
1369 hreturned=hmem;
1370 if(!HeapFree(GetProcessHeap(), 0, pintern))
1371 hreturned=hmem;
1373 /* HeapUnlock(heap); */
1376 __EXCEPT(page_fault)
1378 ERR("page fault occurred ! Caused by bug ?\n");
1379 SetLastError( ERROR_INVALID_PARAMETER );
1380 return hmem;
1382 __ENDTRY
1383 return hreturned;
1387 /***********************************************************************
1388 * GlobalSize (KERNEL32.@)
1389 * RETURNS
1390 * Size in bytes of the global memory object
1391 * 0: Failure
1393 DWORD WINAPI GlobalSize(
1394 HGLOBAL hmem /* [in] Handle of global memory object */
1396 DWORD retval;
1397 PGLOBAL32_INTERN pintern;
1399 if(ISPOINTER(hmem))
1401 retval=HeapSize(GetProcessHeap(), 0, (LPVOID) hmem);
1403 else
1405 /* HeapLock(heap); */
1406 pintern=HANDLE_TO_INTERN(hmem);
1408 if(pintern->Magic==MAGIC_GLOBAL_USED)
1410 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
1411 return 0;
1412 retval=HeapSize(GetProcessHeap(), 0,
1413 (char *)(pintern->Pointer)-sizeof(HGLOBAL))-4;
1414 if (retval == 0xffffffff-4) retval = 0;
1416 else
1418 WARN("invalid handle\n");
1419 retval=0;
1421 /* HeapUnlock(heap); */
1423 /* HeapSize returns 0xffffffff on failure */
1424 if (retval == 0xffffffff) retval = 0;
1425 return retval;
1429 /***********************************************************************
1430 * GlobalWire (KERNEL32.@)
1432 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
1434 return GlobalLock( hmem );
1438 /***********************************************************************
1439 * GlobalUnWire (KERNEL32.@)
1441 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
1443 return GlobalUnlock( hmem);
1447 /***********************************************************************
1448 * GlobalFix (KERNEL32.@)
1450 VOID WINAPI GlobalFix(HGLOBAL hmem)
1452 GlobalLock( hmem );
1456 /***********************************************************************
1457 * GlobalUnfix (KERNEL32.@)
1459 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
1461 GlobalUnlock( hmem);
1465 /***********************************************************************
1466 * GlobalFlags (KERNEL32.@)
1467 * Returns information about the specified global memory object
1469 * NOTES
1470 * Should this return GMEM_INVALID_HANDLE on invalid handle?
1472 * RETURNS
1473 * Value specifying allocation flags and lock count
1474 * GMEM_INVALID_HANDLE: Failure
1476 UINT WINAPI GlobalFlags(
1477 HGLOBAL hmem /* [in] Handle to global memory object */
1479 DWORD retval;
1480 PGLOBAL32_INTERN pintern;
1482 if(ISPOINTER(hmem))
1484 retval=0;
1486 else
1488 /* HeapLock(GetProcessHeap()); */
1489 pintern=HANDLE_TO_INTERN(hmem);
1490 if(pintern->Magic==MAGIC_GLOBAL_USED)
1492 retval=pintern->LockCount + (pintern->Flags<<8);
1493 if(pintern->Pointer==0)
1494 retval|= GMEM_DISCARDED;
1496 else
1498 WARN("Invalid handle: %04x\n", hmem);
1499 retval=0;
1501 /* HeapUnlock(GetProcessHeap()); */
1503 return retval;
1507 /***********************************************************************
1508 * GlobalCompact (KERNEL32.@)
1510 DWORD WINAPI GlobalCompact( DWORD minfree )
1512 return 0; /* GlobalCompact does nothing in Win32 */
1516 /***********************************************************************
1517 * GlobalMemoryStatus (KERNEL32.@)
1518 * RETURNS
1519 * None
1521 VOID WINAPI GlobalMemoryStatus(
1522 LPMEMORYSTATUS lpmem
1524 static MEMORYSTATUS cached_memstatus;
1525 static int cache_lastchecked = 0;
1526 SYSTEM_INFO si;
1527 #ifdef linux
1528 FILE *f;
1529 #endif
1530 #ifdef __FreeBSD__
1531 int *tmp;
1532 int size_sys;
1533 #endif
1534 if (time(NULL)==cache_lastchecked) {
1535 memcpy(lpmem,&cached_memstatus,sizeof(MEMORYSTATUS));
1536 return;
1538 cache_lastchecked = time(NULL);
1540 lpmem->dwMemoryLoad = 0;
1541 lpmem->dwTotalPhys = 16*1024*1024;
1542 lpmem->dwAvailPhys = 16*1024*1024;
1543 lpmem->dwTotalPageFile = 16*1024*1024;
1544 lpmem->dwAvailPageFile = 16*1024*1024;
1546 #ifdef linux
1547 f = fopen( "/proc/meminfo", "r" );
1548 if (f)
1550 char buffer[256];
1551 int total, used, free, shared, buffers, cached;
1553 lpmem->dwLength = sizeof(MEMORYSTATUS);
1554 lpmem->dwTotalPhys = lpmem->dwAvailPhys = 0;
1555 lpmem->dwTotalPageFile = lpmem->dwAvailPageFile = 0;
1556 while (fgets( buffer, sizeof(buffer), f ))
1558 /* old style /proc/meminfo ... */
1559 if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached ))
1561 lpmem->dwTotalPhys += total;
1562 lpmem->dwAvailPhys += free + buffers + cached;
1564 if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free ))
1566 lpmem->dwTotalPageFile += total;
1567 lpmem->dwAvailPageFile += free;
1570 /* new style /proc/meminfo ... */
1571 if (sscanf(buffer, "MemTotal: %d", &total))
1572 lpmem->dwTotalPhys = total*1024;
1573 if (sscanf(buffer, "MemFree: %d", &free))
1574 lpmem->dwAvailPhys = free*1024;
1575 if (sscanf(buffer, "SwapTotal: %d", &total))
1576 lpmem->dwTotalPageFile = total*1024;
1577 if (sscanf(buffer, "SwapFree: %d", &free))
1578 lpmem->dwAvailPageFile = free*1024;
1579 if (sscanf(buffer, "Buffers: %d", &buffers))
1580 lpmem->dwAvailPhys += buffers*1024;
1581 if (sscanf(buffer, "Cached: %d", &cached))
1582 lpmem->dwAvailPhys += cached*1024;
1584 fclose( f );
1586 if (lpmem->dwTotalPhys)
1588 DWORD TotalPhysical = lpmem->dwTotalPhys+lpmem->dwTotalPageFile;
1589 DWORD AvailPhysical = lpmem->dwAvailPhys+lpmem->dwAvailPageFile;
1590 lpmem->dwMemoryLoad = (TotalPhysical-AvailPhysical)
1591 / (TotalPhysical / 100);
1594 #elif defined(__FreeBSD__)
1595 sysctlbyname("hw.physmem", NULL, &size_sys, NULL, 0);
1596 tmp = malloc(size_sys * sizeof(int));
1597 sysctlbyname("hw.physmem", tmp, &size_sys, NULL, 0);
1598 if (tmp && *tmp)
1600 lpmem->dwTotalPhys = *tmp;
1601 free(tmp);
1602 sysctlbyname("hw.usermem", NULL, &size_sys, NULL, 0);
1603 tmp = malloc(size_sys * sizeof(int));
1604 sysctlbyname("hw.usermem", tmp, &size_sys, NULL, 0);
1605 if (tmp && *tmp)
1607 lpmem->dwAvailPhys = *tmp;
1608 lpmem->dwTotalPageFile = *tmp;
1609 lpmem->dwAvailPageFile = *tmp;
1610 lpmem->dwMemoryLoad = lpmem->dwTotalPhys - lpmem->dwAvailPhys;
1611 } else
1613 lpmem->dwAvailPhys = lpmem->dwTotalPhys;
1614 lpmem->dwTotalPageFile = lpmem->dwTotalPhys;
1615 lpmem->dwAvailPageFile = lpmem->dwTotalPhys;
1616 lpmem->dwMemoryLoad = 0;
1618 free(tmp);
1621 #endif
1622 /* FIXME: should do something for other systems */
1623 GetSystemInfo(&si);
1624 lpmem->dwTotalVirtual = si.lpMaximumApplicationAddress-si.lpMinimumApplicationAddress;
1625 /* FIXME: we should track down all the already allocated VM pages and substract them, for now arbitrarily remove 64KB so that it matches NT */
1626 lpmem->dwAvailVirtual = lpmem->dwTotalVirtual-64*1024;
1627 memcpy(&cached_memstatus,lpmem,sizeof(MEMORYSTATUS));
1629 /* it appears some memory display programs want to divide by these values */
1630 if(lpmem->dwTotalPageFile==0)
1631 lpmem->dwTotalPageFile++;
1633 if(lpmem->dwAvailPageFile==0)
1634 lpmem->dwAvailPageFile++;
1636 TRACE("<-- LPMEMORYSTATUS: dwLength %ld, dwMemoryLoad %ld, dwTotalPhys %ld, dwAvailPhys %ld,"
1637 " dwTotalPageFile %ld, dwAvailPageFile %ld, dwTotalVirtual %ld, dwAvailVirtual %ld\n",
1638 lpmem->dwLength, lpmem->dwMemoryLoad, lpmem->dwTotalPhys, lpmem->dwAvailPhys,
1639 lpmem->dwTotalPageFile, lpmem->dwAvailPageFile, lpmem->dwTotalVirtual,
1640 lpmem->dwAvailVirtual);
1643 /***********************************************************************
1644 * A20Proc (KERNEL.165)
1645 * A20_Proc (SYSTEM.20)
1647 void WINAPI A20Proc16( WORD unused )
1649 /* this is also a NOP in Windows */
1652 /***********************************************************************
1653 * LimitEMSPages (KERNEL.156)
1655 DWORD WINAPI LimitEMSPages16( DWORD unused )
1657 return 0;