4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
34 #include "wine/unicode.h"
35 #include "selectors.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
43 /* address where we try to map the system heap */
44 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
45 #define SYSTEM_HEAP_SIZE 0x100000 /* Default heap size = 1Mb */
48 #define HTABLE_SIZE 0x10000
49 #define HTABLE_PAGESIZE 0x1000
50 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
53 typedef struct _LOCAL32HEADER
55 WORD freeListFirst
[HTABLE_NPAGES
];
56 WORD freeListSize
[HTABLE_NPAGES
];
57 WORD freeListLast
[HTABLE_NPAGES
];
59 DWORD selectorTableOffset
;
60 WORD selectorTableSize
;
75 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
77 static HANDLE systemHeap
; /* globally shared heap */
79 /***********************************************************************
80 * HEAP_CreateSystemHeap
82 * Create the system heap.
84 inline static HANDLE
HEAP_CreateSystemHeap(void)
89 UNICODE_STRING event_name
;
90 OBJECT_ATTRIBUTES event_attr
;
92 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
93 0, SYSTEM_HEAP_SIZE
, "__SystemHeap" ))) return 0;
94 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
96 if (!(base
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
98 /* pre-defined address not available */
99 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
103 /* create the system heap event */
104 RtlCreateUnicodeStringFromAsciiz( &event_name
, "__SystemHeapEvent" );
105 event_attr
.Length
= sizeof(event_attr
);
106 event_attr
.RootDirectory
= 0;
107 event_attr
.ObjectName
= &event_name
;
108 event_attr
.Attributes
= 0;
109 event_attr
.SecurityDescriptor
= NULL
;
110 event_attr
.SecurityQualityOfService
= NULL
;
111 NtCreateEvent( &event
, EVENT_ALL_ACCESS
, &event_attr
, TRUE
, FALSE
);
113 if (created
) /* newly created heap */
115 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
116 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
117 NtSetEvent( event
, NULL
);
121 /* wait for the heap to be initialized */
122 WaitForSingleObject( event
, INFINITE
);
123 systemHeap
= (HANDLE
)base
;
130 /***********************************************************************
131 * HeapCreate (KERNEL32.@)
133 * Handle of heap: Success
136 HANDLE WINAPI
HeapCreate(
137 DWORD flags
, /* [in] Heap allocation flag */
138 DWORD initialSize
, /* [in] Initial heap size */
139 DWORD maxSize
/* [in] Maximum heap size */
143 if ( flags
& HEAP_SHARED
)
145 if (!systemHeap
) HEAP_CreateSystemHeap();
146 else WARN( "Shared Heap requested, returning system heap.\n" );
151 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
152 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
157 /***********************************************************************
158 * HeapDestroy (KERNEL32.@)
163 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
165 if (heap
== systemHeap
)
167 WARN( "attempt to destroy system heap, returning TRUE!\n" );
170 if (!RtlDestroyHeap( heap
)) return TRUE
;
171 SetLastError( ERROR_INVALID_HANDLE
);
176 /***********************************************************************
177 * HeapCompact (KERNEL32.@)
179 DWORD WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
181 return RtlCompactHeap( heap
, flags
);
185 /***********************************************************************
186 * HeapLock (KERNEL32.@)
187 * Attempts to acquire the critical section object for a specified heap.
193 BOOL WINAPI
HeapLock(
194 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
196 return RtlLockHeap( heap
);
200 /***********************************************************************
201 * HeapUnlock (KERNEL32.@)
202 * Releases ownership of the critical section object.
208 BOOL WINAPI
HeapUnlock(
209 HANDLE heap
/* [in] Handle to the heap to unlock */
211 return RtlUnlockHeap( heap
);
215 /***********************************************************************
216 * HeapValidate (KERNEL32.@)
217 * Validates a specified heap.
226 BOOL WINAPI
HeapValidate(
227 HANDLE heap
, /* [in] Handle to the heap */
228 DWORD flags
, /* [in] Bit flags that control access during operation */
229 LPCVOID block
/* [in] Optional pointer to memory block to validate */
231 return RtlValidateHeap( heap
, flags
, block
);
235 /***********************************************************************
236 * HeapWalk (KERNEL32.@)
237 * Enumerates the memory blocks in a specified heap.
240 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
241 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
247 BOOL WINAPI
HeapWalk(
248 HANDLE heap
, /* [in] Handle to heap to enumerate */
249 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
251 NTSTATUS ret
= RtlWalkHeap( heap
, entry
);
252 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
257 /***********************************************************************
258 * GetProcessHeap (KERNEL32.@)
260 HANDLE WINAPI
GetProcessHeap(void)
262 HANDLE
*pdb
= (HANDLE
*)NtCurrentTeb()->process
;
263 return pdb
[0x18 / sizeof(HANDLE
)]; /* get dword at offset 0x18 in pdb */
267 /***********************************************************************
268 * GetProcessHeaps (KERNEL32.@)
270 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
272 return RtlGetProcessHeaps( count
, heaps
);
276 /***********************************************************************
277 * 32-bit local heap functions (Win95; undocumented)
280 /***********************************************************************
283 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
284 DWORD heapSize
, DWORD flags
)
286 DWORD totSize
, segSize
= 0;
288 LOCAL32HEADER
*header
;
291 WORD selectorEven
, selectorOdd
;
294 /* Determine new heap size */
298 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
304 if ( heapSize
== -1L )
305 heapSize
= 1024L*1024L; /* FIXME */
307 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
308 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
309 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
312 /* Allocate memory and initialize heap */
314 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
317 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
318 MEM_COMMIT
, PAGE_READWRITE
) )
320 VirtualFree( base
, 0, MEM_RELEASE
);
324 if (!(heap
= RtlCreateHeap( 0, base
+ segSize
+ HTABLE_SIZE
, heapSize
, 0x10000, NULL
, NULL
)))
326 VirtualFree( base
, 0, MEM_RELEASE
);
331 /* Set up header and handle table */
333 header
= (LOCAL32HEADER
*)(base
+ segSize
);
335 header
->limit
= HTABLE_PAGESIZE
-1;
337 header
->magic
= LOCAL32_MAGIC
;
340 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
341 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
342 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
344 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
345 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
347 header
->freeListFirst
[1] = 0xffff;
350 /* Set up selector table */
352 nrBlocks
= (totSize
+ 0x7fff) >> 15;
353 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
354 selectorEven
= SELECTOR_AllocBlock( base
, totSize
, WINE_LDT_FLAGS_DATA
);
355 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000, WINE_LDT_FLAGS_DATA
);
356 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
358 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
359 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
);
360 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
);
361 HeapDestroy( header
->heap
);
362 VirtualFree( base
, 0, MEM_RELEASE
);
366 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
367 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
368 header
->selectorDelta
= selectorEven
- selectorOdd
;
369 header
->segment
= segment
? segment
: selectorEven
;
371 for (i
= 0; i
< nrBlocks
; i
++)
372 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
373 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
375 /* Move old segment */
379 /* FIXME: This is somewhat ugly and relies on implementation
380 details about 16-bit global memory handles ... */
382 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
383 memcpy( base
, oldBase
, segSize
);
384 GLOBAL_MoveBlock( segment
, base
, totSize
);
385 HeapFree( GetProcessHeap(), 0, oldBase
);
388 return (HANDLE
)header
;
391 /***********************************************************************
392 * Local32_SearchHandle
394 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
398 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
399 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
409 /***********************************************************************
412 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
413 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
420 case -2: /* 16:16 pointer, no handles */
421 *ptr
= MapSL( addr
);
422 *handle
= (LPDWORD
)*ptr
;
425 case -1: /* 32-bit offset, no handles */
426 *ptr
= header
->base
+ addr
;
427 *handle
= (LPDWORD
)*ptr
;
431 if ( addr
>= sizeof(LOCAL32HEADER
)
432 && addr
< header
->limit
&& !(addr
& 3)
433 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
435 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
436 *ptr
= header
->base
+ **handle
;
440 case 1: /* 16:16 pointer */
441 *ptr
= MapSL( addr
);
442 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
445 case 2: /* 32-bit offset */
446 *ptr
= header
->base
+ addr
;
447 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
452 /***********************************************************************
455 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
456 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
460 case -2: /* 16:16 pointer */
463 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
464 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
465 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
469 case -1: /* 32-bit offset */
471 *addr
= ptr
- header
->base
;
475 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
480 /***********************************************************************
483 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
485 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
490 /* Allocate memory */
491 ptr
= HeapAlloc( header
->heap
,
492 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
496 /* Allocate handle if requested */
501 /* Find first page of handle table with free slots */
502 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
503 if (header
->freeListFirst
[page
] != 0)
505 if (page
== HTABLE_NPAGES
)
507 WARN("Out of handles!\n" );
508 HeapFree( header
->heap
, 0, ptr
);
512 /* If virgin page, initialize it */
513 if (header
->freeListFirst
[page
] == 0xffff)
515 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
516 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
518 WARN("Cannot grow handle table!\n" );
519 HeapFree( header
->heap
, 0, ptr
);
523 header
->limit
+= HTABLE_PAGESIZE
;
525 header
->freeListFirst
[page
] = 0;
526 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
527 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
529 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
530 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
532 if (page
< HTABLE_NPAGES
-1)
533 header
->freeListFirst
[page
+1] = 0xffff;
536 /* Allocate handle slot from page */
537 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
538 if (--header
->freeListSize
[page
] == 0)
539 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
541 header
->freeListFirst
[page
] = *handle
;
543 /* Store 32-bit offset in handle slot */
544 *handle
= ptr
- header
->base
;
548 handle
= (LPDWORD
)ptr
;
553 /* Convert handle to requested output type */
554 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
558 /***********************************************************************
561 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
562 DWORD size
, DWORD flags
)
564 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
569 return Local32Alloc16( heap
, size
, type
, flags
);
571 /* Retrieve handle and pointer */
572 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
573 if (!handle
) return FALSE
;
575 /* Reallocate memory block */
576 ptr
= HeapReAlloc( header
->heap
,
577 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
583 *handle
= ptr
- header
->base
;
585 handle
= (LPDWORD
)ptr
;
587 /* Convert handle to requested output type */
588 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
592 /***********************************************************************
595 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
597 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
601 /* Retrieve handle and pointer */
602 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
603 if (!handle
) return FALSE
;
605 /* Free handle if necessary */
608 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
609 int page
= offset
>> 12;
611 /* Return handle slot to page free list */
612 if (header
->freeListSize
[page
]++ == 0)
613 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
615 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
616 header
->freeListLast
[page
] = offset
;
620 /* Shrink handle table when possible */
621 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
623 if ( VirtualFree( (LPBYTE
)header
+
624 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
625 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
628 header
->limit
-= HTABLE_PAGESIZE
;
629 header
->freeListFirst
[page
] = 0xffff;
635 return HeapFree( header
->heap
, 0, ptr
);
638 /***********************************************************************
641 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
643 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
647 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
648 if (!handle
) return 0;
650 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
654 /***********************************************************************
657 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
659 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
663 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
664 if (!handle
) return 0;
666 return HeapSize( header
->heap
, 0, ptr
);
669 /***********************************************************************
672 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
674 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
678 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
679 return handle
!= NULL
;
682 /***********************************************************************
685 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
687 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
688 return header
->segment
;
691 /***********************************************************************
694 static LOCAL32HEADER
*Local32_GetHeap( HGLOBAL16 handle
)
696 WORD selector
= GlobalHandleToSel16( handle
);
697 DWORD base
= GetSelectorBase( selector
);
698 DWORD limit
= GetSelectorLimit16( selector
);
700 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
703 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
704 return (LOCAL32HEADER
*)base
;
709 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
710 return (LOCAL32HEADER
*)base
;
715 /***********************************************************************
716 * Local32Info (KERNEL.444)
717 * Local32Info (TOOLHELP.84)
719 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
721 PROCESS_HEAP_ENTRY entry
;
724 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
725 if ( !header
) return FALSE
;
727 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
730 pLocal32Info
->dwMemReserved
= 0;
731 pLocal32Info
->dwMemCommitted
= 0;
732 pLocal32Info
->dwTotalFree
= 0;
733 pLocal32Info
->dwLargestFreeBlock
= 0;
735 while (HeapWalk( header
->heap
, &entry
))
737 if (entry
.wFlags
& PROCESS_HEAP_REGION
)
739 pLocal32Info
->dwMemReserved
+= entry
.u
.Region
.dwCommittedSize
740 + entry
.u
.Region
.dwUnCommittedSize
;
741 pLocal32Info
->dwMemCommitted
= entry
.u
.Region
.dwCommittedSize
;
743 else if (!(entry
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
))
745 DWORD size
= entry
.cbData
+ entry
.cbOverhead
;
746 pLocal32Info
->dwTotalFree
+= size
;
747 if (size
> pLocal32Info
->dwLargestFreeBlock
) pLocal32Info
->dwLargestFreeBlock
= size
;
751 pLocal32Info
->dwcFreeHandles
= 0;
752 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
754 if ( header
->freeListFirst
[i
] == 0xffff ) break;
755 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
757 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
762 /***********************************************************************
763 * Local32First (KERNEL.445)
764 * Local32First (TOOLHELP.85)
766 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
768 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
772 /***********************************************************************
773 * Local32Next (KERNEL.446)
774 * Local32Next (TOOLHELP.86)
776 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
778 FIXME("(%p): stub!\n", pLocal32Entry
);