2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
15 #include "selectors.h"
17 #include "stackframe.h"
22 /* Global arena block */
25 DWORD base
; /* Base address */
26 DWORD size
; /* Size in bytes (0 indicates a free block) */
27 HGLOBAL handle
; /* Handle for this block */
28 HGLOBAL hOwner
; /* Owner of this block */
29 BYTE lockCount
; /* Count of GlobalFix() calls */
30 BYTE pageLockCount
; /* Count of GlobalPageLock() calls */
31 BYTE flags
; /* Allocation flags */
32 BYTE selCount
; /* Number of selectors allocated for this block */
36 /* Flags definitions */
37 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
38 #define GA_DGROUP 0x04
39 #define GA_DISCARDABLE 0x08
40 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
43 static GLOBALARENA
*pGlobalArena
= NULL
;
44 static int globalArenaSize
= 0;
46 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
48 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
50 /***********************************************************************
53 * Return the arena for a given selector, growing the arena array if needed.
55 static GLOBALARENA
*GLOBAL_GetArena( WORD sel
, WORD selcount
)
57 if (((sel
>> __AHSHIFT
) + selcount
) > globalArenaSize
)
59 int newsize
= ((sel
>> __AHSHIFT
) + selcount
+ 0xff) & ~0xff;
60 GLOBALARENA
*pNewArena
= realloc( pGlobalArena
,
61 newsize
* sizeof(GLOBALARENA
) );
62 if (!pNewArena
) return 0;
63 pGlobalArena
= pNewArena
;
64 memset( pGlobalArena
+ globalArenaSize
, 0,
65 (newsize
- globalArenaSize
) * sizeof(GLOBALARENA
) );
66 globalArenaSize
= newsize
;
68 return pGlobalArena
+ (sel
>> __AHSHIFT
);
76 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
77 if (pGlobalArena
[i
].size
!=0 && (pGlobalArena
[i
].handle
& 0x8000)){
79 printf("0x%08x, ",pGlobalArena
[i
].handle
);
87 /***********************************************************************
90 * Create a global heap block for a fixed range of linear memory.
92 HGLOBAL
GLOBAL_CreateBlock( WORD flags
, const void *ptr
, DWORD size
,
93 HGLOBAL hOwner
, BOOL isCode
,
94 BOOL is32Bit
, BOOL isReadOnly
,
100 /* Allocate the selector(s) */
102 sel
= SELECTOR_AllocBlock( ptr
, size
,
103 isCode
? SEGMENT_CODE
: SEGMENT_DATA
,
104 is32Bit
, isReadOnly
);
107 selcount
= (size
+ 0xffff) / 0x10000;
109 if (!(pArena
= GLOBAL_GetArena( sel
, selcount
)))
115 /* Fill the arena block */
117 pArena
->base
= (DWORD
)ptr
;
118 pArena
->size
= GET_SEL_LIMIT(sel
) + 1;
119 if ((flags
& GMEM_DDESHARE
) && Options
.ipc
)
121 pArena
->handle
= shmdata
->handle
;
122 pArena
->shmid
= shmdata
->shmid
;
127 pArena
->handle
= (flags
& GMEM_MOVEABLE
) ? sel
- 1 : sel
;
130 pArena
->hOwner
= hOwner
;
131 pArena
->lockCount
= 0;
132 pArena
->pageLockCount
= 0;
133 pArena
->flags
= flags
& GA_MOVEABLE
;
134 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
135 if (flags
& GMEM_DDESHARE
) pArena
->flags
|= GA_IPCSHARE
;
136 if (!isCode
) pArena
->flags
|= GA_DGROUP
;
137 pArena
->selCount
= selcount
;
138 if (selcount
> 1) /* clear the next arena blocks */
139 memset( pArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
141 return pArena
->handle
;
145 /***********************************************************************
148 * Free a block allocated by GLOBAL_CreateBlock, without touching
149 * the associated linear memory range.
151 BOOL
GLOBAL_FreeBlock( HGLOBAL handle
)
155 if (!handle
) return TRUE
;
156 sel
= GlobalHandleToSel( handle
);
157 if (FreeSelector( sel
)) return FALSE
; /* failed */
158 memset( GET_ARENA_PTR(sel
), 0, sizeof(GLOBALARENA
) );
163 /***********************************************************************
166 * Implementation of GlobalAlloc()
168 HGLOBAL
GLOBAL_Alloc( WORD flags
, DWORD size
, HGLOBAL hOwner
,
169 BOOL isCode
, BOOL is32Bit
, BOOL isReadOnly
)
175 dprintf_global( stddeb
, "GlobalAlloc: %ld flags=%04x\n", size
, flags
);
179 if (size
>= GLOBAL_MAX_ALLOC_SIZE
- 0x1f) return 0;
180 if (size
== 0) size
= 0x20;
181 else size
= (size
+ 0x1f) & ~0x1f;
183 /* Allocate the linear memory */
186 if ((flags
& GMEM_DDESHARE
) && Options
.ipc
)
187 ptr
= DDE_malloc(flags
, size
, &shmdata
);
189 #endif /* CONFIG_IPC */
190 ptr
= malloc( size
);
193 /* Allocate the selector(s) */
195 handle
= GLOBAL_CreateBlock( flags
, ptr
, size
, hOwner
,
196 isCode
, is32Bit
, isReadOnly
, &shmdata
);
203 if (flags
& GMEM_ZEROINIT
) memset( ptr
, 0, size
);
209 /***********************************************************************
212 * Find the arena for a given handle
213 * (when handle is not serial - e.g. DDE)
215 static GLOBALARENA
*GLOBAL_FindArena( HGLOBAL handle
)
218 for (i
= globalArenaSize
-1 ; i
>=0 ; i
--) {
219 if (pGlobalArena
[i
].size
!=0 && pGlobalArena
[i
].handle
== handle
)
220 return ( &pGlobalArena
[i
] );
226 /***********************************************************************
227 * DDE_GlobalHandleToSel
230 WORD
DDE_GlobalHandleToSel( HGLOBAL handle
)
235 pArena
= GLOBAL_FindArena(handle
);
237 int ArenaIdx
= pArena
- pGlobalArena
;
239 /* See if synchronized to the shared memory */
240 return DDE_SyncHandle(handle
, ( ArenaIdx
<< __AHSHIFT
) | 7);
243 /* attach the block */
244 DDE_AttachHandle(handle
, &segptr
);
246 return SELECTOROF( segptr
);
248 #endif /* CONFIG_IPC */
251 /***********************************************************************
252 * GlobalAlloc (KERNEL.15)
254 HGLOBAL
GlobalAlloc( WORD flags
, DWORD size
)
256 HANDLE owner
= GetCurrentPDB();
258 if (flags
& GMEM_DDESHARE
)
259 owner
= GetExePtr(owner
); /* Make it a module handle */
260 return GLOBAL_Alloc( flags
, size
, owner
, FALSE
, FALSE
, FALSE
);
264 /***********************************************************************
265 * GlobalReAlloc (KERNEL.16)
267 HGLOBAL
GlobalReAlloc( HGLOBAL handle
, DWORD size
, WORD flags
)
272 GLOBALARENA
*pArena
, *pNewArena
;
273 WORD sel
= GlobalHandleToSel( handle
);
275 dprintf_global( stddeb
, "GlobalReAlloc: %04x %ld flags=%04x\n",
276 handle
, size
, flags
);
277 if (!handle
) return 0;
280 if (Options
.ipc
&& (flags
& GMEM_DDESHARE
|| is_dde_handle(handle
))) {
282 "GlobalReAlloc: shared memory reallocating unimplemented\n");
285 #endif /* CONFIG_IPC */
287 pArena
= GET_ARENA_PTR( handle
);
289 /* Discard the block if requested */
291 if ((size
== 0) && (flags
& GMEM_MOVEABLE
) && !(flags
& GMEM_MODIFY
))
293 if (!(pArena
->flags
& GA_MOVEABLE
) ||
294 !(pArena
->flags
& GA_DISCARDABLE
) ||
295 (pArena
->lockCount
> 0) || (pArena
->pageLockCount
> 0)) return 0;
296 free( (void *)pArena
->base
);
298 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't */
299 /* change the selector if we are shrinking the block */
300 SELECTOR_ReallocBlock( sel
, 0, 1, SEGMENT_DATA
, 0, 0 );
306 if (size
> GLOBAL_MAX_ALLOC_SIZE
- 0x20) return 0;
307 if (size
== 0) size
= 0x20;
308 else size
= (size
+ 0x1f) & ~0x1f;
310 /* Change the flags */
312 if (flags
& GMEM_MODIFY
)
314 /* Change the flags, leaving GA_DGROUP alone */
315 pArena
->flags
= (pArena
->flags
& GA_DGROUP
) | (flags
& GA_MOVEABLE
);
316 if (flags
& GMEM_DISCARDABLE
) pArena
->flags
|= GA_DISCARDABLE
;
320 /* Reallocate the linear memory */
322 ptr
= (void *)pArena
->base
;
323 oldsize
= pArena
->size
;
324 dprintf_global(stddeb
,"oldsize %08lx\n",oldsize
);
325 if (size
== oldsize
) return handle
; /* Nothing to do */
327 ptr
= realloc( ptr
, size
);
331 memset( pArena
, 0, sizeof(GLOBALARENA
) );
335 /* Reallocate the selector(s) */
337 sel
= SELECTOR_ReallocBlock( sel
, ptr
, size
, SEGMENT_DATA
, 0, 0 );
341 memset( pArena
, 0, sizeof(GLOBALARENA
) );
344 selcount
= (size
+ 0xffff) / 0x10000;
346 if (!(pNewArena
= GLOBAL_GetArena( sel
, selcount
)))
353 /* Fill the new arena block */
355 if (pNewArena
!= pArena
) memcpy( pNewArena
, pArena
, sizeof(GLOBALARENA
) );
356 pNewArena
->base
= (DWORD
)ptr
;
357 pNewArena
->size
= GET_SEL_LIMIT(sel
) + 1;
358 pNewArena
->selCount
= selcount
;
359 pNewArena
->handle
= (pNewArena
->flags
& GA_MOVEABLE
) ? sel
- 1 : sel
;
361 if (selcount
> 1) /* clear the next arena blocks */
362 memset( pNewArena
+ 1, 0, (selcount
- 1) * sizeof(GLOBALARENA
) );
364 if ((oldsize
< size
) && (flags
& GMEM_ZEROINIT
))
365 memset( (char *)ptr
+ oldsize
, 0, size
- oldsize
);
366 return pNewArena
->handle
;
370 /***********************************************************************
371 * GlobalFree (KERNEL.17)
373 HGLOBAL
GlobalFree( HGLOBAL handle
)
375 void *ptr
= GlobalLock( handle
);
377 dprintf_global( stddeb
, "GlobalFree: %04x\n", handle
);
378 if (!GLOBAL_FreeBlock( handle
)) return handle
; /* failed */
380 if (is_dde_handle(handle
)) return DDE_GlobalFree(handle
);
381 #endif /* CONFIG_IPC */
382 if (ptr
) free( ptr
);
387 /***********************************************************************
388 * WIN16_GlobalLock (KERNEL.18)
390 * This is the GlobalLock() function used by 16-bit code.
392 SEGPTR
WIN16_GlobalLock( HGLOBAL handle
)
394 dprintf_global( stddeb
, "WIN16_GlobalLock(%04x) -> %08lx\n",
395 handle
, MAKELONG( 0, GlobalHandleToSel(handle
)) );
396 if (!handle
) return 0;
399 if (is_dde_handle(handle
))
400 return (SEGPTR
)MAKELONG( 0, DDE_GlobalHandleToSel(handle
) );
401 #endif /* CONFIG_IPC */
403 if (!GET_ARENA_PTR(handle
)->base
) return (SEGPTR
)0;
404 return (SEGPTR
)MAKELONG( 0, GlobalHandleToSel(handle
) );
408 /***********************************************************************
409 * GlobalLock (KERNEL.18)
411 * This is the GlobalLock() function used by 32-bit code.
413 LPVOID
GlobalLock( HGLOBAL handle
)
415 if (!handle
) return 0;
417 if (is_dde_handle(handle
)) return DDE_AttachHandle(handle
, NULL
);
419 return (LPSTR
)GET_ARENA_PTR(handle
)->base
;
423 /***********************************************************************
424 * GlobalUnlock (KERNEL.19)
426 BOOL
GlobalUnlock( HGLOBAL handle
)
428 dprintf_global( stddeb
, "GlobalUnlock: %04x\n", handle
);
433 /***********************************************************************
434 * GlobalSize (KERNEL.20)
436 DWORD
GlobalSize( HGLOBAL handle
)
438 dprintf_global( stddeb
, "GlobalSize: %04x\n", handle
);
439 if (!handle
) return 0;
440 return GET_ARENA_PTR(handle
)->size
;
444 /***********************************************************************
445 * GlobalHandle (KERNEL.21)
447 DWORD
GlobalHandle( WORD sel
)
449 dprintf_global( stddeb
, "GlobalHandle: %04x\n", sel
);
450 return MAKELONG( GET_ARENA_PTR(sel
)->handle
, GlobalHandleToSel(sel
) );
454 /***********************************************************************
455 * GlobalFlags (KERNEL.22)
457 WORD
GlobalFlags( HGLOBAL handle
)
461 dprintf_global( stddeb
, "GlobalFlags: %04x\n", handle
);
462 pArena
= GET_ARENA_PTR(handle
);
463 return pArena
->lockCount
|
464 ((pArena
->flags
& GA_DISCARDABLE
) ? GMEM_DISCARDABLE
: 0) |
465 ((pArena
->base
== 0) ? GMEM_DISCARDED
: 0);
469 /***********************************************************************
470 * LockSegment (KERNEL.23)
472 HGLOBAL
LockSegment( HGLOBAL handle
)
474 dprintf_global( stddeb
, "LockSegment: %04x\n", handle
);
475 if (handle
== (HGLOBAL
)-1) handle
= CURRENT_DS
;
476 GET_ARENA_PTR(handle
)->lockCount
++;
481 /***********************************************************************
482 * UnlockSegment (KERNEL.24)
484 void UnlockSegment( HGLOBAL handle
)
486 dprintf_global( stddeb
, "UnlockSegment: %04x\n", handle
);
487 if (handle
== (HGLOBAL
)-1) handle
= CURRENT_DS
;
488 GET_ARENA_PTR(handle
)->lockCount
--;
489 /* FIXME: this ought to return the lock count in CX (go figure...) */
493 /***********************************************************************
494 * GlobalCompact (KERNEL.25)
496 DWORD
GlobalCompact( DWORD desired
)
498 return GLOBAL_MAX_ALLOC_SIZE
;
502 /***********************************************************************
503 * GlobalFreeAll (KERNEL.26)
505 void GlobalFreeAll( HANDLE owner
)
510 pArena
= pGlobalArena
;
511 for (i
= 0; i
< globalArenaSize
; i
++, pArena
++)
513 if ((pArena
->size
!= 0) && (pArena
->hOwner
== owner
))
514 GlobalFree( pArena
->handle
);
519 /***********************************************************************
520 * GlobalWire (KERNEL.111)
522 SEGPTR
GlobalWire( HGLOBAL handle
)
524 return WIN16_GlobalLock( handle
);
528 /***********************************************************************
529 * GlobalUnWire (KERNEL.112)
531 BOOL
GlobalUnWire( HGLOBAL handle
)
533 return GlobalUnlock( handle
);
537 /***********************************************************************
538 * GlobalDOSAlloc (KERNEL.184)
540 DWORD
GlobalDOSAlloc( DWORD size
)
542 WORD sel
= GlobalAlloc( GMEM_FIXED
, size
);
544 return MAKELONG( sel
, sel
/* this one ought to be a real-mode segment */ );
548 /***********************************************************************
549 * GlobalDOSFree (KERNEL.185)
551 WORD
GlobalDOSFree( WORD sel
)
553 return GlobalFree( GlobalHandle(sel
) ) ? sel
: 0;
557 /***********************************************************************
558 * SetSwapAreaSize (KERNEL.106)
560 LONG
SetSwapAreaSize( WORD size
)
562 dprintf_global(stdnimp
, "STUB: SetSwapAreaSize(%d)\n", size
);
563 return MAKELONG( size
, 0xffff );
567 /***********************************************************************
568 * GlobalLRUOldest (KERNEL.163)
570 HGLOBAL
GlobalLRUOldest( HGLOBAL handle
)
572 dprintf_global( stddeb
, "GlobalLRUOldest: %04x\n", handle
);
573 if (handle
== (HGLOBAL
)-1) handle
= CURRENT_DS
;
578 /***********************************************************************
579 * GlobalLRUNewest (KERNEL.164)
581 HGLOBAL
GlobalLRUNewest( HGLOBAL handle
)
583 dprintf_global( stddeb
, "GlobalLRUNewest: %04x\n", handle
);
584 if (handle
== (HGLOBAL
)-1) handle
= CURRENT_DS
;
589 /***********************************************************************
590 * GetFreeSpace (KERNEL.169)
592 DWORD
GetFreeSpace( UINT wFlags
)
594 return GLOBAL_MAX_ALLOC_SIZE
;
598 /***********************************************************************
599 * GlobalPageLock (KERNEL.191)
601 WORD
GlobalPageLock( HGLOBAL handle
)
603 dprintf_global( stddeb
, "GlobalPageLock: %04x\n", handle
);
604 return ++(GET_ARENA_PTR(handle
)->pageLockCount
);
608 /***********************************************************************
609 * GlobalPageUnlock (KERNEL.192)
611 WORD
GlobalPageUnlock( HGLOBAL handle
)
613 dprintf_global( stddeb
, "GlobalPageUnlock: %04x\n", handle
);
614 return --(GET_ARENA_PTR(handle
)->pageLockCount
);
618 /***********************************************************************
619 * GlobalFix (KERNEL.197)
621 void GlobalFix( HGLOBAL handle
)
623 dprintf_global( stddeb
, "GlobalFix: %04x\n", handle
);
624 GET_ARENA_PTR(handle
)->lockCount
++;
628 /***********************************************************************
629 * GlobalUnfix (KERNEL.198)
631 void GlobalUnfix( HGLOBAL handle
)
633 dprintf_global( stddeb
, "GlobalUnfix: %04x\n", handle
);
634 GET_ARENA_PTR(handle
)->lockCount
--;
638 /***********************************************************************
639 * FarSetOwner (KERNEL.403)
641 void FarSetOwner( HANDLE handle
, WORD hOwner
)
643 GET_ARENA_PTR(handle
)->hOwner
= hOwner
;
647 /***********************************************************************
648 * FarGetOwner (KERNEL.404)
650 WORD
FarGetOwner( HANDLE handle
)
652 return GET_ARENA_PTR(handle
)->hOwner
;
656 /***********************************************************************
657 * GlobalHandleToSel (TOOLHELP.50)
659 WORD
GlobalHandleToSel( HGLOBAL handle
)
661 dprintf_toolhelp( stddeb
, "GlobalHandleToSel: %04x\n", handle
);
662 if (!handle
) return 0;
664 if (is_dde_handle(handle
)) return DDE_GlobalHandleToSel(handle
);
668 fprintf( stderr
, "Program attempted invalid selector conversion\n" );
675 /***********************************************************************
676 * GlobalFirst (TOOLHELP.51)
678 BOOL
GlobalFirst( GLOBALENTRY
*pGlobal
, WORD wFlags
)
680 if (wFlags
== GLOBAL_LRU
) return FALSE
;
682 return GlobalNext( pGlobal
, wFlags
);
686 /***********************************************************************
687 * GlobalNext (TOOLHELP.52)
689 BOOL
GlobalNext( GLOBALENTRY
*pGlobal
, WORD wFlags
)
693 if (pGlobal
->dwNext
>= globalArenaSize
) return FALSE
;
694 pArena
= pGlobalArena
+ pGlobal
->dwNext
;
695 if (wFlags
== GLOBAL_FREE
) /* only free blocks */
698 for (i
= pGlobal
->dwNext
; i
< globalArenaSize
; i
++, pArena
++)
699 if (pArena
->size
== 0) break; /* block is free */
700 if (i
>= globalArenaSize
) return FALSE
;
704 pGlobal
->dwAddress
= pArena
->base
;
705 pGlobal
->dwBlockSize
= pArena
->size
;
706 pGlobal
->hBlock
= pArena
->handle
;
707 pGlobal
->wcLock
= pArena
->lockCount
;
708 pGlobal
->wcPageLock
= pArena
->pageLockCount
;
709 pGlobal
->wFlags
= (GetCurrentPDB() == pArena
->hOwner
);
710 pGlobal
->wHeapPresent
= FALSE
;
711 pGlobal
->hOwner
= pArena
->hOwner
;
712 pGlobal
->wType
= GT_UNKNOWN
;
719 /***********************************************************************
720 * GlobalInfo (TOOLHELP.53)
722 BOOL
GlobalInfo( GLOBALINFO
*pInfo
)
727 pInfo
->wcItems
= globalArenaSize
;
728 pInfo
->wcItemsFree
= 0;
729 pInfo
->wcItemsLRU
= 0;
730 for (i
= 0, pArena
= pGlobalArena
; i
< globalArenaSize
; i
++, pArena
++)
731 if (pArena
->size
== 0) pInfo
->wcItemsFree
++;
736 /***********************************************************************
737 * GlobalEntryHandle (TOOLHELP.54)
739 BOOL
GlobalEntryHandle( GLOBALENTRY
*pGlobal
, HGLOBAL hItem
)
745 /***********************************************************************
746 * GlobalEntryModule (TOOLHELP.55)
748 BOOL
GlobalEntryModule( GLOBALENTRY
*pGlobal
, HMODULE hModule
, WORD wSeg
)
754 /***********************************************************************
755 * MemManInfo (TOOLHELP.72)
757 BOOL
MemManInfo( MEMMANINFO
*pInfo
)
760 /* FIXME: does not take into account the dwSize member
761 * could be corrupting memory therefore
763 /* shamefully stolen from free */
771 if ((meminfo
= fopen("/proc/meminfo", "r")) < 0) {
772 perror("wine: open");
776 fgets(buf
, 80, meminfo
); /* read first line */
777 while ( fgets(buf
, 80, meminfo
) ) {
778 n
= sscanf( buf
, "%*s %d %d %d %d %d", &col
[0], &col
[1], &col
[2], &col
[3], &col
[4]);
779 if ( n
< 1 ) continue; /* escape the loop at the top */
781 availmem
+= col
[2] + col
[4];
784 fprintf(stderr
,"MemManInfo called with dwSize = %ld\n",pInfo
->dwSize
);
785 pInfo
->wPageSize
= getpagesize();
786 pInfo
->dwLargestFreeBlock
= availmem
;
787 pInfo
->dwTotalLinearSpace
= totalmem
/ pInfo
->wPageSize
;
788 pInfo
->dwMaxPagesAvailable
= pInfo
->dwLargestFreeBlock
/ pInfo
->wPageSize
;
789 pInfo
->dwMaxPagesLockable
= pInfo
->dwMaxPagesLockable
;
790 /* FIXME: the next three are not quite correct */
791 pInfo
->dwTotalUnlockedPages
= pInfo
->dwMaxPagesAvailable
;
792 pInfo
->dwFreePages
= pInfo
->dwMaxPagesAvailable
;
793 pInfo
->dwTotalPages
= pInfo
->dwMaxPagesAvailable
;
794 /* FIXME: the three above are not quite correct */
795 pInfo
->dwFreeLinearSpace
= pInfo
->dwMaxPagesAvailable
;
796 pInfo
->dwSwapFilePages
= 0L;
803 /***********************************************************************
805 * implements GlobalAlloc (KERNEL32.316)
806 * LocalAlloc (KERNEL32.372)
808 void *GlobalAlloc32(int flags
,int size
)
810 dprintf_global(stddeb
,"GlobalAlloc32(%x,%x)\n",flags
,size
);