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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #ifdef HAVE_VALGRIND_MEMCHECK_H
31 #include <valgrind/memcheck.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #define WIN32_NO_STATUS
41 #include "wine/list.h"
42 #include "wine/debug.h"
43 #include "wine/server.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
47 /* Note: the heap data structures are loosely based on what Pietrek describes in his
48 * book 'Windows 95 System Programming Secrets', with some adaptations for
49 * better compatibility with NT.
52 typedef struct tagARENA_INUSE
54 DWORD size
; /* Block size; must be the first field */
55 DWORD magic
: 24; /* Magic number */
56 DWORD unused_bytes
: 8; /* Number of bytes in the block not used by user data (max value is HEAP_MIN_DATA_SIZE+HEAP_MIN_SHRINK_SIZE) */
59 typedef struct tagARENA_FREE
61 DWORD size
; /* Block size; must be the first field */
62 DWORD magic
; /* Magic number */
63 struct list entry
; /* Entry in free list */
68 struct list entry
; /* entry in heap large blocks list */
69 SIZE_T data_size
; /* size of user data */
70 SIZE_T block_size
; /* total size of virtual memory block */
71 DWORD pad
[2]; /* padding to ensure 16-byte alignment of data */
72 DWORD size
; /* fields for compatibility with normal arenas */
73 DWORD magic
; /* these must remain at the end of the structure */
76 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
77 #define ARENA_FLAG_PREV_FREE 0x00000002
78 #define ARENA_SIZE_MASK (~3)
79 #define ARENA_LARGE_SIZE 0xfedcba90 /* magic value for 'size' field in large blocks */
81 /* Value for arena 'magic' field */
82 #define ARENA_INUSE_MAGIC 0x455355
83 #define ARENA_FREE_MAGIC 0x45455246
84 #define ARENA_LARGE_MAGIC 0x6752614c
86 #define ARENA_INUSE_FILLER 0x55
87 #define ARENA_FREE_FILLER 0xaa
89 /* everything is aligned on 8 byte boundaries (16 for Win64) */
90 #define ALIGNMENT (2*sizeof(void*))
91 #define LARGE_ALIGNMENT 16 /* large blocks have stricter alignment */
92 #define ARENA_OFFSET (ALIGNMENT - sizeof(ARENA_INUSE))
94 C_ASSERT( sizeof(ARENA_LARGE
) % LARGE_ALIGNMENT
== 0 );
96 #define ROUND_SIZE(size) ((((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1)) + ARENA_OFFSET)
98 #define QUIET 1 /* Suppress messages */
99 #define NOISY 0 /* Report all errors */
101 /* minimum data size (without arenas) of an allocated block */
102 /* make sure that it's larger than a free list entry */
103 #define HEAP_MIN_DATA_SIZE ROUND_SIZE(2 * sizeof(struct list))
104 /* minimum size that must remain to shrink an allocated block */
105 #define HEAP_MIN_SHRINK_SIZE (HEAP_MIN_DATA_SIZE+sizeof(ARENA_FREE))
106 /* minimum size to start allocating large blocks */
107 #define HEAP_MIN_LARGE_BLOCK_SIZE 0x7f000
109 /* Max size of the blocks on the free lists */
110 static const SIZE_T HEAP_freeListSizes
[] =
112 0x10, 0x20, 0x30, 0x40, 0x60, 0x80, 0x100, 0x200, 0x400, 0x1000, ~0UL
114 #define HEAP_NB_FREE_LISTS (sizeof(HEAP_freeListSizes)/sizeof(HEAP_freeListSizes[0]))
124 typedef struct tagSUBHEAP
126 void *base
; /* Base address of the sub-heap memory block */
127 SIZE_T size
; /* Size of the whole sub-heap */
128 SIZE_T min_commit
; /* Minimum committed size */
129 SIZE_T commitSize
; /* Committed size of the sub-heap */
130 struct list entry
; /* Entry in sub-heap list */
131 struct tagHEAP
*heap
; /* Main heap structure */
132 DWORD headerSize
; /* Size of the heap header */
133 DWORD magic
; /* Magic number */
136 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
138 typedef struct tagHEAP
141 DWORD flags
; /* Heap flags */
142 DWORD force_flags
; /* Forced heap flags for debugging */
143 SUBHEAP subheap
; /* First sub-heap */
144 struct list entry
; /* Entry in process heap list */
145 struct list subheap_list
; /* Sub-heap list */
146 struct list large_list
; /* Large blocks list */
147 SIZE_T grow_size
; /* Size of next subheap for growing heap */
148 DWORD magic
; /* Magic number */
149 RTL_CRITICAL_SECTION critSection
; /* Critical section for serialization */
150 FREE_LIST_ENTRY
*freeList
; /* Free lists */
153 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
155 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
156 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
158 /* some undocumented flags (names are made up) */
159 #define HEAP_PAGE_ALLOCS 0x01000000
160 #define HEAP_VALIDATE 0x10000000
161 #define HEAP_VALIDATE_ALL 0x20000000
162 #define HEAP_VALIDATE_PARAMS 0x40000000
164 static HEAP
*processHeap
; /* main process heap */
166 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, DWORD flags
, LPCVOID block
, BOOL quiet
);
168 /* mark a block of memory as free for debugging purposes */
169 static inline void mark_block_free( void *ptr
, SIZE_T size
)
171 if (TRACE_ON(heap
) || WARN_ON(heap
)) memset( ptr
, ARENA_FREE_FILLER
, size
);
172 #if defined(VALGRIND_MAKE_MEM_NOACCESS)
173 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr
, size
));
174 #elif defined( VALGRIND_MAKE_NOACCESS)
175 VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr
, size
));
179 /* mark a block of memory as initialized for debugging purposes */
180 static inline void mark_block_initialized( void *ptr
, SIZE_T size
)
182 #if defined(VALGRIND_MAKE_MEM_DEFINED)
183 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_DEFINED( ptr
, size
));
184 #elif defined(VALGRIND_MAKE_READABLE)
185 VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr
, size
));
189 /* mark a block of memory as uninitialized for debugging purposes */
190 static inline void mark_block_uninitialized( void *ptr
, SIZE_T size
)
192 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
193 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
194 #elif defined(VALGRIND_MAKE_WRITABLE)
195 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
197 if (TRACE_ON(heap
) || WARN_ON(heap
))
199 memset( ptr
, ARENA_INUSE_FILLER
, size
);
200 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
201 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
202 #elif defined(VALGRIND_MAKE_WRITABLE)
203 /* make it uninitialized to valgrind again */
204 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
209 /* clear contents of a block of memory */
210 static inline void clear_block( void *ptr
, SIZE_T size
)
212 mark_block_initialized( ptr
, size
);
213 memset( ptr
, 0, size
);
216 /* notify that a new block of memory has been allocated for debugging purposes */
217 static inline void notify_alloc( void *ptr
, SIZE_T size
, BOOL init
)
219 #ifdef VALGRIND_MALLOCLIKE_BLOCK
220 VALGRIND_MALLOCLIKE_BLOCK( ptr
, size
, 0, init
);
224 /* notify that a block of memory has been freed for debugging purposes */
225 static inline void notify_free( void const *ptr
)
227 #ifdef VALGRIND_FREELIKE_BLOCK
228 VALGRIND_FREELIKE_BLOCK( ptr
, 0 );
232 static void subheap_notify_free_all(SUBHEAP
const *subheap
)
234 #ifdef VALGRIND_FREELIKE_BLOCK
235 char const *ptr
= (char const *)subheap
->base
+ subheap
->headerSize
;
237 if (!RUNNING_ON_VALGRIND
) return;
239 while (ptr
< (char const *)subheap
->base
+ subheap
->size
)
241 if (*(const DWORD
*)ptr
& ARENA_FLAG_FREE
)
243 ARENA_FREE
const *pArena
= (ARENA_FREE
const *)ptr
;
244 if (pArena
->magic
!=ARENA_FREE_MAGIC
) ERR("bad free_magic @%p\n", pArena
);
245 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
249 ARENA_INUSE
const *pArena
= (ARENA_INUSE
const *)ptr
;
250 if (pArena
->magic
!=ARENA_INUSE_MAGIC
) ERR("bad inuse_magic @%p\n", pArena
);
251 notify_free(pArena
+ 1);
252 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
258 /* locate a free list entry of the appropriate size */
259 /* size is the size of the whole block including the arena header */
260 static inline unsigned int get_freelist_index( SIZE_T size
)
264 size
-= sizeof(ARENA_FREE
);
265 for (i
= 0; i
< HEAP_NB_FREE_LISTS
- 1; i
++) if (size
<= HEAP_freeListSizes
[i
]) break;
269 /* get the memory protection type to use for a given heap */
270 static inline ULONG
get_protection_type( DWORD flags
)
272 return (flags
& HEAP_CREATE_ENABLE_EXECUTE
) ? PAGE_EXECUTE_READWRITE
: PAGE_READWRITE
;
275 static RTL_CRITICAL_SECTION_DEBUG process_heap_critsect_debug
=
277 0, 0, NULL
, /* will be set later */
278 { &process_heap_critsect_debug
.ProcessLocksList
, &process_heap_critsect_debug
.ProcessLocksList
},
279 0, 0, { (DWORD_PTR
)(__FILE__
": main process heap section") }
283 /***********************************************************************
286 static void HEAP_Dump( HEAP
*heap
)
292 DPRINTF( "Heap: %p\n", heap
);
293 DPRINTF( "Next: %p Sub-heaps:", LIST_ENTRY( heap
->entry
.next
, HEAP
, entry
) );
294 LIST_FOR_EACH_ENTRY( subheap
, &heap
->subheap_list
, SUBHEAP
, entry
) DPRINTF( " %p", subheap
);
296 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
297 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
298 DPRINTF( "%p free %08lx prev=%p next=%p\n",
299 &heap
->freeList
[i
].arena
, HEAP_freeListSizes
[i
],
300 LIST_ENTRY( heap
->freeList
[i
].arena
.entry
.prev
, ARENA_FREE
, entry
),
301 LIST_ENTRY( heap
->freeList
[i
].arena
.entry
.next
, ARENA_FREE
, entry
));
303 LIST_FOR_EACH_ENTRY( subheap
, &heap
->subheap_list
, SUBHEAP
, entry
)
305 SIZE_T freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
306 DPRINTF( "\n\nSub-heap %p: base=%p size=%08lx committed=%08lx\n",
307 subheap
, subheap
->base
, subheap
->size
, subheap
->commitSize
);
309 DPRINTF( "\n Block Arena Stat Size Id\n" );
310 ptr
= (char *)subheap
->base
+ subheap
->headerSize
;
311 while (ptr
< (char *)subheap
->base
+ subheap
->size
)
313 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
315 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
316 DPRINTF( "%p %08x free %08x prev=%p next=%p\n",
317 pArena
, pArena
->magic
,
318 pArena
->size
& ARENA_SIZE_MASK
,
319 LIST_ENTRY( pArena
->entry
.prev
, ARENA_FREE
, entry
),
320 LIST_ENTRY( pArena
->entry
.next
, ARENA_FREE
, entry
) );
321 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
322 arenaSize
+= sizeof(ARENA_FREE
);
323 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
325 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
327 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
328 DPRINTF( "%p %08x Used %08x back=%p\n",
329 pArena
, pArena
->magic
, pArena
->size
& ARENA_SIZE_MASK
, *((ARENA_FREE
**)pArena
- 1) );
330 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
331 arenaSize
+= sizeof(ARENA_INUSE
);
332 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
336 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
337 DPRINTF( "%p %08x used %08x\n", pArena
, pArena
->magic
, pArena
->size
& ARENA_SIZE_MASK
);
338 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
339 arenaSize
+= sizeof(ARENA_INUSE
);
340 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
343 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
344 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
345 arenaSize
, (arenaSize
* 100) / subheap
->size
);
350 static void HEAP_DumpEntry( LPPROCESS_HEAP_ENTRY entry
)
353 TRACE( "Dumping entry %p\n", entry
);
354 TRACE( "lpData\t\t: %p\n", entry
->lpData
);
355 TRACE( "cbData\t\t: %08x\n", entry
->cbData
);
356 TRACE( "cbOverhead\t: %08x\n", entry
->cbOverhead
);
357 TRACE( "iRegionIndex\t: %08x\n", entry
->iRegionIndex
);
358 TRACE( "WFlags\t\t: ");
359 if (entry
->wFlags
& PROCESS_HEAP_REGION
)
360 TRACE( "PROCESS_HEAP_REGION ");
361 if (entry
->wFlags
& PROCESS_HEAP_UNCOMMITTED_RANGE
)
362 TRACE( "PROCESS_HEAP_UNCOMMITTED_RANGE ");
363 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_BUSY
)
364 TRACE( "PROCESS_HEAP_ENTRY_BUSY ");
365 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_MOVEABLE
)
366 TRACE( "PROCESS_HEAP_ENTRY_MOVEABLE ");
367 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_DDESHARE
)
368 TRACE( "PROCESS_HEAP_ENTRY_DDESHARE ");
369 rem_flags
= entry
->wFlags
&
370 ~(PROCESS_HEAP_REGION
| PROCESS_HEAP_UNCOMMITTED_RANGE
|
371 PROCESS_HEAP_ENTRY_BUSY
| PROCESS_HEAP_ENTRY_MOVEABLE
|
372 PROCESS_HEAP_ENTRY_DDESHARE
);
374 TRACE( "Unknown %08x", rem_flags
);
376 if ((entry
->wFlags
& PROCESS_HEAP_ENTRY_BUSY
)
377 && (entry
->wFlags
& PROCESS_HEAP_ENTRY_MOVEABLE
))
380 TRACE( "BLOCK->hMem\t\t:%p\n", entry
->u
.Block
.hMem
);
382 if (entry
->wFlags
& PROCESS_HEAP_REGION
)
384 TRACE( "Region.dwCommittedSize\t:%08x\n",entry
->u
.Region
.dwCommittedSize
);
385 TRACE( "Region.dwUnCommittedSize\t:%08x\n",entry
->u
.Region
.dwUnCommittedSize
);
386 TRACE( "Region.lpFirstBlock\t:%p\n",entry
->u
.Region
.lpFirstBlock
);
387 TRACE( "Region.lpLastBlock\t:%p\n",entry
->u
.Region
.lpLastBlock
);
391 /***********************************************************************
394 * Pointer to the heap
397 static HEAP
*HEAP_GetPtr(
398 HANDLE heap
/* [in] Handle to the heap */
400 HEAP
*heapPtr
= heap
;
401 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
403 ERR("Invalid heap %p!\n", heap
);
406 if (TRACE_ON(heap
) && !HEAP_IsRealArena( heapPtr
, 0, NULL
, NOISY
))
408 HEAP_Dump( heapPtr
);
416 /***********************************************************************
417 * HEAP_InsertFreeBlock
419 * Insert a free block into the free list.
421 static inline void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
, BOOL last
)
423 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
+ get_freelist_index( pArena
->size
+ sizeof(*pArena
) );
426 /* insert at end of free list, i.e. before the next free list entry */
428 if (pEntry
== &heap
->freeList
[HEAP_NB_FREE_LISTS
]) pEntry
= heap
->freeList
;
429 list_add_before( &pEntry
->arena
.entry
, &pArena
->entry
);
433 /* insert at head of free list */
434 list_add_after( &pEntry
->arena
.entry
, &pArena
->entry
);
436 pArena
->size
|= ARENA_FLAG_FREE
;
440 /***********************************************************************
442 * Find the sub-heap containing a given address.
448 static SUBHEAP
*HEAP_FindSubHeap(
449 const HEAP
*heap
, /* [in] Heap pointer */
450 LPCVOID ptr
) /* [in] Address */
453 LIST_FOR_EACH_ENTRY( sub
, &heap
->subheap_list
, SUBHEAP
, entry
)
454 if ((ptr
>= sub
->base
) &&
455 ((const char *)ptr
< (const char *)sub
->base
+ sub
->size
- sizeof(ARENA_INUSE
)))
461 /***********************************************************************
464 * Make sure the heap storage is committed for a given size in the specified arena.
466 static inline BOOL
HEAP_Commit( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, SIZE_T data_size
)
468 void *ptr
= (char *)(pArena
+ 1) + data_size
+ sizeof(ARENA_FREE
);
469 SIZE_T size
= (char *)ptr
- (char *)subheap
->base
;
470 size
= (size
+ COMMIT_MASK
) & ~COMMIT_MASK
;
471 if (size
> subheap
->size
) size
= subheap
->size
;
472 if (size
<= subheap
->commitSize
) return TRUE
;
473 size
-= subheap
->commitSize
;
474 ptr
= (char *)subheap
->base
+ subheap
->commitSize
;
475 if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0,
476 &size
, MEM_COMMIT
, get_protection_type( subheap
->heap
->flags
) ))
478 WARN("Could not commit %08lx bytes at %p for heap %p\n",
479 size
, ptr
, subheap
->heap
);
482 subheap
->commitSize
+= size
;
487 /***********************************************************************
490 * If possible, decommit the heap storage from (including) 'ptr'.
492 static inline BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
495 SIZE_T decommit_size
;
496 SIZE_T size
= (char *)ptr
- (char *)subheap
->base
;
498 /* round to next block and add one full block */
499 size
= ((size
+ COMMIT_MASK
) & ~COMMIT_MASK
) + COMMIT_MASK
+ 1;
500 size
= max( size
, subheap
->min_commit
);
501 if (size
>= subheap
->commitSize
) return TRUE
;
502 decommit_size
= subheap
->commitSize
- size
;
503 addr
= (char *)subheap
->base
+ size
;
505 if (NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &decommit_size
, MEM_DECOMMIT
))
507 WARN("Could not decommit %08lx bytes at %p for heap %p\n",
508 decommit_size
, (char *)subheap
->base
+ size
, subheap
->heap
);
511 subheap
->commitSize
-= decommit_size
;
516 /***********************************************************************
517 * HEAP_CreateFreeBlock
519 * Create a free block at a specified address. 'size' is the size of the
520 * whole block, including the new arena.
522 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, SIZE_T size
)
528 /* Create a free arena */
529 mark_block_uninitialized( ptr
, sizeof( ARENA_FREE
) );
531 pFree
->magic
= ARENA_FREE_MAGIC
;
533 /* If debugging, erase the freed block content */
535 pEnd
= (char *)ptr
+ size
;
536 if (pEnd
> (char *)subheap
->base
+ subheap
->commitSize
)
537 pEnd
= (char *)subheap
->base
+ subheap
->commitSize
;
538 if (pEnd
> (char *)(pFree
+ 1)) mark_block_free( pFree
+ 1, pEnd
- (char *)(pFree
+ 1) );
540 /* Check if next block is free also */
542 if (((char *)ptr
+ size
< (char *)subheap
->base
+ subheap
->size
) &&
543 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
545 /* Remove the next arena from the free list */
546 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
547 list_remove( &pNext
->entry
);
548 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
549 mark_block_free( pNext
, sizeof(ARENA_FREE
) );
552 /* Set the next block PREV_FREE flag and pointer */
554 last
= ((char *)ptr
+ size
>= (char *)subheap
->base
+ subheap
->size
);
557 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
558 *pNext
|= ARENA_FLAG_PREV_FREE
;
559 mark_block_initialized( pNext
- 1, sizeof( ARENA_FREE
* ) );
560 *((ARENA_FREE
**)pNext
- 1) = pFree
;
563 /* Last, insert the new block into the free list */
565 pFree
->size
= size
- sizeof(*pFree
);
566 HEAP_InsertFreeBlock( subheap
->heap
, pFree
, last
);
570 /***********************************************************************
571 * HEAP_MakeInUseBlockFree
573 * Turn an in-use block into a free block. Can also decommit the end of
574 * the heap, and possibly even free the sub-heap altogether.
576 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
579 SIZE_T size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
581 /* Check if we can merge with previous block */
583 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
585 pFree
= *((ARENA_FREE
**)pArena
- 1);
586 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
587 /* Remove it from the free list */
588 list_remove( &pFree
->entry
);
590 else pFree
= (ARENA_FREE
*)pArena
;
592 /* Create a free block */
594 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
595 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
596 if ((char *)pFree
+ size
< (char *)subheap
->base
+ subheap
->size
)
597 return; /* Not the last block, so nothing more to do */
599 /* Free the whole sub-heap if it's empty and not the original one */
601 if (((char *)pFree
== (char *)subheap
->base
+ subheap
->headerSize
) &&
602 (subheap
!= &subheap
->heap
->subheap
))
605 void *addr
= subheap
->base
;
606 /* Remove the free block from the list */
607 list_remove( &pFree
->entry
);
608 /* Remove the subheap from the list */
609 list_remove( &subheap
->entry
);
610 /* Free the memory */
612 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
616 /* Decommit the end of the heap */
618 if (!(subheap
->heap
->flags
& HEAP_SHARED
)) HEAP_Decommit( subheap
, pFree
+ 1 );
622 /***********************************************************************
625 * Shrink an in-use block.
627 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, SIZE_T size
)
629 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_SHRINK_SIZE
)
631 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
632 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
633 /* assign size plus previous arena flags */
634 pArena
->size
= size
| (pArena
->size
& ~ARENA_SIZE_MASK
);
638 /* Turn off PREV_FREE flag in next block */
639 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
640 if (pNext
< (char *)subheap
->base
+ subheap
->size
)
641 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
646 /***********************************************************************
647 * allocate_large_block
649 static void *allocate_large_block( HEAP
*heap
, DWORD flags
, SIZE_T size
)
652 SIZE_T block_size
= sizeof(*arena
) + ROUND_SIZE(size
);
653 LPVOID address
= NULL
;
655 if (block_size
< size
) return NULL
; /* overflow */
656 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 5,
657 &block_size
, MEM_COMMIT
, get_protection_type( flags
) ))
659 WARN("Could not allocate block for %08lx bytes\n", size
);
663 arena
->data_size
= size
;
664 arena
->block_size
= block_size
;
665 arena
->size
= ARENA_LARGE_SIZE
;
666 arena
->magic
= ARENA_LARGE_MAGIC
;
667 list_add_tail( &heap
->large_list
, &arena
->entry
);
672 /***********************************************************************
675 static void free_large_block( HEAP
*heap
, DWORD flags
, void *ptr
)
677 ARENA_LARGE
*arena
= (ARENA_LARGE
*)ptr
- 1;
678 LPVOID address
= arena
;
681 list_remove( &arena
->entry
);
682 NtFreeVirtualMemory( NtCurrentProcess(), &address
, &size
, MEM_RELEASE
);
686 /***********************************************************************
687 * realloc_large_block
689 static void *realloc_large_block( HEAP
*heap
, DWORD flags
, void *ptr
, SIZE_T size
)
691 ARENA_LARGE
*arena
= (ARENA_LARGE
*)ptr
- 1;
694 if (arena
->block_size
- sizeof(*arena
) >= size
)
696 /* FIXME: we could remap zero-pages instead */
697 if ((flags
& HEAP_ZERO_MEMORY
) && size
> arena
->data_size
)
698 memset( (char *)ptr
+ arena
->data_size
, 0, size
- arena
->data_size
);
699 arena
->data_size
= size
;
702 if (flags
& HEAP_REALLOC_IN_PLACE_ONLY
) return NULL
;
703 if (!(new_ptr
= allocate_large_block( heap
, flags
, size
)))
705 WARN("Could not allocate block for %08lx bytes\n", size
);
708 memcpy( new_ptr
, ptr
, arena
->data_size
);
709 free_large_block( heap
, flags
, ptr
);
714 /***********************************************************************
717 static ARENA_LARGE
*find_large_block( HEAP
*heap
, const void *ptr
)
721 LIST_FOR_EACH_ENTRY( arena
, &heap
->large_list
, ARENA_LARGE
, entry
)
722 if (ptr
== arena
+ 1) return arena
;
728 /***********************************************************************
729 * validate_large_arena
731 static BOOL
validate_large_arena( HEAP
*heap
, const ARENA_LARGE
*arena
, BOOL quiet
)
733 if ((ULONG_PTR
)arena
% getpagesize())
737 ERR( "Heap %p: invalid large arena pointer %p\n", heap
, arena
);
738 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
740 else if (WARN_ON(heap
))
742 WARN( "Heap %p: unaligned arena pointer %p\n", heap
, arena
);
743 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
747 if (arena
->size
!= ARENA_LARGE_SIZE
|| arena
->magic
!= ARENA_LARGE_MAGIC
)
751 ERR( "Heap %p: invalid large arena %p values %x/%x\n",
752 heap
, arena
, arena
->size
, arena
->magic
);
753 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
755 else if (WARN_ON(heap
))
757 WARN( "Heap %p: invalid large arena %p values %x/%x\n",
758 heap
, arena
, arena
->size
, arena
->magic
);
759 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
767 /***********************************************************************
770 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
771 SIZE_T commitSize
, SIZE_T totalSize
)
774 FREE_LIST_ENTRY
*pEntry
;
779 /* round-up sizes on a 64K boundary */
780 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
781 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
782 if (!commitSize
) commitSize
= 0x10000;
783 totalSize
= min( totalSize
, 0xffff0000 ); /* don't allow a heap larger than 4Gb */
784 if (totalSize
< commitSize
) totalSize
= commitSize
;
785 if (flags
& HEAP_SHARED
) commitSize
= totalSize
; /* always commit everything in a shared heap */
787 /* allocate the memory block */
788 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 0, &totalSize
,
789 MEM_RESERVE
, get_protection_type( flags
) ))
791 WARN("Could not allocate %08lx bytes\n", totalSize
);
794 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 0,
795 &commitSize
, MEM_COMMIT
, get_protection_type( flags
) ))
797 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize
, address
);
804 /* If this is a secondary subheap, insert it into list */
807 subheap
->base
= address
;
808 subheap
->heap
= heap
;
809 subheap
->size
= totalSize
;
810 subheap
->min_commit
= 0x10000;
811 subheap
->commitSize
= commitSize
;
812 subheap
->magic
= SUBHEAP_MAGIC
;
813 subheap
->headerSize
= ROUND_SIZE( sizeof(SUBHEAP
) );
814 list_add_head( &heap
->subheap_list
, &subheap
->entry
);
818 /* If this is a primary subheap, initialize main heap */
822 heap
->magic
= HEAP_MAGIC
;
823 heap
->grow_size
= max( HEAP_DEF_SIZE
, totalSize
);
824 list_init( &heap
->subheap_list
);
825 list_init( &heap
->large_list
);
827 subheap
= &heap
->subheap
;
828 subheap
->base
= address
;
829 subheap
->heap
= heap
;
830 subheap
->size
= totalSize
;
831 subheap
->min_commit
= commitSize
;
832 subheap
->commitSize
= commitSize
;
833 subheap
->magic
= SUBHEAP_MAGIC
;
834 subheap
->headerSize
= ROUND_SIZE( sizeof(HEAP
) );
835 list_add_head( &heap
->subheap_list
, &subheap
->entry
);
837 /* Build the free lists */
839 heap
->freeList
= (FREE_LIST_ENTRY
*)((char *)heap
+ subheap
->headerSize
);
840 subheap
->headerSize
+= HEAP_NB_FREE_LISTS
* sizeof(FREE_LIST_ENTRY
);
841 list_init( &heap
->freeList
[0].arena
.entry
);
842 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
844 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
845 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
846 if (i
) list_add_after( &pEntry
[-1].arena
.entry
, &pEntry
->arena
.entry
);
849 /* Initialize critical section */
851 if (!processHeap
) /* do it by hand to avoid memory allocations */
853 heap
->critSection
.DebugInfo
= &process_heap_critsect_debug
;
854 heap
->critSection
.LockCount
= -1;
855 heap
->critSection
.RecursionCount
= 0;
856 heap
->critSection
.OwningThread
= 0;
857 heap
->critSection
.LockSemaphore
= 0;
858 heap
->critSection
.SpinCount
= 0;
859 process_heap_critsect_debug
.CriticalSection
= &heap
->critSection
;
863 RtlInitializeCriticalSection( &heap
->critSection
);
864 heap
->critSection
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": HEAP.critSection");
867 if (flags
& HEAP_SHARED
)
869 /* let's assume that only one thread at a time will try to do this */
870 HANDLE sem
= heap
->critSection
.LockSemaphore
;
871 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
873 NtDuplicateObject( NtCurrentProcess(), sem
, NtCurrentProcess(), &sem
, 0, 0,
874 DUP_HANDLE_MAKE_GLOBAL
| DUP_HANDLE_SAME_ACCESS
| DUP_HANDLE_CLOSE_SOURCE
);
875 heap
->critSection
.LockSemaphore
= sem
;
876 RtlFreeHeap( processHeap
, 0, heap
->critSection
.DebugInfo
);
877 heap
->critSection
.DebugInfo
= NULL
;
881 /* Create the first free block */
883 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
->base
+ subheap
->headerSize
,
884 subheap
->size
- subheap
->headerSize
);
890 /***********************************************************************
893 * Find a free block at least as large as the requested size, and make sure
894 * the requested size is committed.
896 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, SIZE_T size
,
897 SUBHEAP
**ppSubHeap
)
902 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
+ get_freelist_index( size
+ sizeof(ARENA_INUSE
) );
904 /* Find a suitable free list, and in it find a block large enough */
906 ptr
= &pEntry
->arena
.entry
;
907 while ((ptr
= list_next( &heap
->freeList
[0].arena
.entry
, ptr
)))
909 ARENA_FREE
*pArena
= LIST_ENTRY( ptr
, ARENA_FREE
, entry
);
910 SIZE_T arena_size
= (pArena
->size
& ARENA_SIZE_MASK
) +
911 sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
912 if (arena_size
>= size
)
914 subheap
= HEAP_FindSubHeap( heap
, pArena
);
915 if (!HEAP_Commit( subheap
, (ARENA_INUSE
*)pArena
, size
)) return NULL
;
916 *ppSubHeap
= subheap
;
921 /* If no block was found, attempt to grow the heap */
923 if (!(heap
->flags
& HEAP_GROWABLE
))
925 WARN("Not enough space in heap %p for %08lx bytes\n", heap
, size
);
928 /* make sure that we have a big enough size *committed* to fit another
929 * last free arena in !
930 * So just one heap struct, one first free arena which will eventually
931 * get used, and a second free arena that might get assigned all remaining
932 * free space in HEAP_ShrinkBlock() */
933 total_size
= size
+ ROUND_SIZE(sizeof(SUBHEAP
)) + sizeof(ARENA_INUSE
) + sizeof(ARENA_FREE
);
934 if (total_size
< size
) return NULL
; /* overflow */
936 if ((subheap
= HEAP_CreateSubHeap( heap
, NULL
, heap
->flags
, total_size
,
937 max( heap
->grow_size
, total_size
) )))
939 if (heap
->grow_size
< 128 * 1024 * 1024) heap
->grow_size
*= 2;
941 else while (!subheap
) /* shrink the grow size again if we are running out of space */
943 if (heap
->grow_size
<= total_size
|| heap
->grow_size
<= 4 * 1024 * 1024) return NULL
;
944 heap
->grow_size
/= 2;
945 subheap
= HEAP_CreateSubHeap( heap
, NULL
, heap
->flags
, total_size
,
946 max( heap
->grow_size
, total_size
) );
949 TRACE("created new sub-heap %p of %08lx bytes for heap %p\n",
950 subheap
, subheap
->size
, heap
);
952 *ppSubHeap
= subheap
;
953 return (ARENA_FREE
*)((char *)subheap
->base
+ subheap
->headerSize
);
957 /***********************************************************************
958 * HEAP_IsValidArenaPtr
960 * Check that the pointer is inside the range possible for arenas.
962 static BOOL
HEAP_IsValidArenaPtr( const HEAP
*heap
, const ARENA_FREE
*ptr
)
965 const SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
966 if (!subheap
) return FALSE
;
967 if ((const char *)ptr
>= (const char *)subheap
->base
+ subheap
->headerSize
) return TRUE
;
968 if (subheap
!= &heap
->subheap
) return FALSE
;
969 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
970 if (ptr
== &heap
->freeList
[i
].arena
) return TRUE
;
975 /***********************************************************************
976 * HEAP_ValidateFreeArena
978 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
980 ARENA_FREE
*prev
, *next
;
981 char *heapEnd
= (char *)subheap
->base
+ subheap
->size
;
983 /* Check for unaligned pointers */
984 if ((ULONG_PTR
)pArena
% ALIGNMENT
!= ARENA_OFFSET
)
986 ERR("Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
990 /* Check magic number */
991 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
993 ERR("Heap %p: invalid free arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
996 /* Check size flags */
997 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
998 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
1000 ERR("Heap %p: bad flags %08x for free arena %p\n",
1001 subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, pArena
);
1004 /* Check arena size */
1005 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
1007 ERR("Heap %p: bad size %08x for free arena %p\n",
1008 subheap
->heap
, pArena
->size
& ARENA_SIZE_MASK
, pArena
);
1011 /* Check that next pointer is valid */
1012 next
= LIST_ENTRY( pArena
->entry
.next
, ARENA_FREE
, entry
);
1013 if (!HEAP_IsValidArenaPtr( subheap
->heap
, next
))
1015 ERR("Heap %p: bad next ptr %p for arena %p\n",
1016 subheap
->heap
, next
, pArena
);
1019 /* Check that next arena is free */
1020 if (!(next
->size
& ARENA_FLAG_FREE
) || (next
->magic
!= ARENA_FREE_MAGIC
))
1022 ERR("Heap %p: next arena %p invalid for %p\n",
1023 subheap
->heap
, next
, pArena
);
1026 /* Check that prev pointer is valid */
1027 prev
= LIST_ENTRY( pArena
->entry
.prev
, ARENA_FREE
, entry
);
1028 if (!HEAP_IsValidArenaPtr( subheap
->heap
, prev
))
1030 ERR("Heap %p: bad prev ptr %p for arena %p\n",
1031 subheap
->heap
, prev
, pArena
);
1034 /* Check that prev arena is free */
1035 if (!(prev
->size
& ARENA_FLAG_FREE
) || (prev
->magic
!= ARENA_FREE_MAGIC
))
1037 /* this often means that the prev arena got overwritten
1038 * by a memory write before that prev arena */
1039 ERR("Heap %p: prev arena %p invalid for %p\n",
1040 subheap
->heap
, prev
, pArena
);
1043 /* Check that next block has PREV_FREE flag */
1044 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
1046 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
1047 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
1049 ERR("Heap %p: free arena %p next block has no PREV_FREE flag\n",
1050 subheap
->heap
, pArena
);
1053 /* Check next block back pointer */
1054 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
1055 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
1057 ERR("Heap %p: arena %p has wrong back ptr %p\n",
1058 subheap
->heap
, pArena
,
1059 *((ARENA_FREE
**)((char *)(pArena
+1) + (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
1067 /***********************************************************************
1068 * HEAP_ValidateInUseArena
1070 static BOOL
HEAP_ValidateInUseArena( const SUBHEAP
*subheap
, const ARENA_INUSE
*pArena
, BOOL quiet
)
1072 const char *heapEnd
= (const char *)subheap
->base
+ subheap
->size
;
1074 /* Check for unaligned pointers */
1075 if ((ULONG_PTR
)pArena
% ALIGNMENT
!= ARENA_OFFSET
)
1077 if ( quiet
== NOISY
)
1079 ERR( "Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
1080 if ( TRACE_ON(heap
) )
1081 HEAP_Dump( subheap
->heap
);
1083 else if ( WARN_ON(heap
) )
1085 WARN( "Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
1086 if ( TRACE_ON(heap
) )
1087 HEAP_Dump( subheap
->heap
);
1092 /* Check magic number */
1093 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
1095 if (quiet
== NOISY
) {
1096 ERR("Heap %p: invalid in-use arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
1098 HEAP_Dump( subheap
->heap
);
1099 } else if (WARN_ON(heap
)) {
1100 WARN("Heap %p: invalid in-use arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
1102 HEAP_Dump( subheap
->heap
);
1106 /* Check size flags */
1107 if (pArena
->size
& ARENA_FLAG_FREE
)
1109 ERR("Heap %p: bad flags %08x for in-use arena %p\n",
1110 subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, pArena
);
1113 /* Check arena size */
1114 if ((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
1116 ERR("Heap %p: bad size %08x for in-use arena %p\n",
1117 subheap
->heap
, pArena
->size
& ARENA_SIZE_MASK
, pArena
);
1120 /* Check next arena PREV_FREE flag */
1121 if (((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
1122 (*(const DWORD
*)((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
1124 ERR("Heap %p: in-use arena %p next block has PREV_FREE flag\n",
1125 subheap
->heap
, pArena
);
1128 /* Check prev free arena */
1129 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
1131 const ARENA_FREE
*pPrev
= *((const ARENA_FREE
* const*)pArena
- 1);
1132 /* Check prev pointer */
1133 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
1135 ERR("Heap %p: bad back ptr %p for arena %p\n",
1136 subheap
->heap
, pPrev
, pArena
);
1139 /* Check that prev arena is free */
1140 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
1141 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
1143 ERR("Heap %p: prev arena %p invalid for in-use %p\n",
1144 subheap
->heap
, pPrev
, pArena
);
1147 /* Check that prev arena is really the previous block */
1148 if ((const char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (const char *)pArena
)
1150 ERR("Heap %p: prev arena %p is not prev for in-use %p\n",
1151 subheap
->heap
, pPrev
, pArena
);
1159 /***********************************************************************
1160 * HEAP_IsRealArena [Internal]
1161 * Validates a block is a valid arena.
1167 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, /* [in] ptr to the heap */
1168 DWORD flags
, /* [in] Bit flags that control access during operation */
1169 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
1170 BOOL quiet
) /* [in] Flag - if true, HEAP_ValidateInUseArena
1171 * does not complain */
1175 const ARENA_LARGE
*large_arena
;
1177 flags
&= HEAP_NO_SERIALIZE
;
1178 flags
|= heapPtr
->flags
;
1179 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
1180 if (!(flags
& HEAP_NO_SERIALIZE
))
1181 RtlEnterCriticalSection( &heapPtr
->critSection
);
1183 if (block
) /* only check this single memory block */
1185 const ARENA_INUSE
*arena
= (const ARENA_INUSE
*)block
- 1;
1187 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, arena
)) ||
1188 ((const char *)arena
< (char *)subheap
->base
+ subheap
->headerSize
))
1190 if (!(large_arena
= find_large_block( heapPtr
, block
)))
1193 ERR("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
1194 else if (WARN_ON(heap
))
1195 WARN("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
1199 ret
= validate_large_arena( heapPtr
, large_arena
, quiet
);
1201 ret
= HEAP_ValidateInUseArena( subheap
, arena
, quiet
);
1203 if (!(flags
& HEAP_NO_SERIALIZE
))
1204 RtlLeaveCriticalSection( &heapPtr
->critSection
);
1208 LIST_FOR_EACH_ENTRY( subheap
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1210 char *ptr
= (char *)subheap
->base
+ subheap
->headerSize
;
1211 while (ptr
< (char *)subheap
->base
+ subheap
->size
)
1213 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1215 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
1219 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1223 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
1227 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1233 LIST_FOR_EACH_ENTRY( large_arena
, &heapPtr
->large_list
, ARENA_LARGE
, entry
)
1234 if (!(ret
= validate_large_arena( heapPtr
, large_arena
, quiet
))) break;
1236 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1241 /***********************************************************************
1242 * heap_set_debug_flags
1244 void heap_set_debug_flags( HANDLE handle
)
1246 HEAP
*heap
= HEAP_GetPtr( handle
);
1247 ULONG global_flags
= RtlGetNtGlobalFlags();
1250 if (global_flags
& FLG_HEAP_ENABLE_TAIL_CHECK
) flags
|= HEAP_TAIL_CHECKING_ENABLED
;
1251 if (global_flags
& FLG_HEAP_ENABLE_FREE_CHECK
) flags
|= HEAP_FREE_CHECKING_ENABLED
;
1252 if (global_flags
& FLG_HEAP_DISABLE_COALESCING
) flags
|= HEAP_DISABLE_COALESCE_ON_FREE
;
1253 if (global_flags
& FLG_HEAP_PAGE_ALLOCS
) flags
|= HEAP_PAGE_ALLOCS
| HEAP_GROWABLE
;
1255 if (global_flags
& FLG_HEAP_VALIDATE_PARAMETERS
)
1256 flags
|= HEAP_VALIDATE
| HEAP_VALIDATE_PARAMS
|
1257 HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1258 if (global_flags
& FLG_HEAP_VALIDATE_ALL
)
1259 flags
|= HEAP_VALIDATE
| HEAP_VALIDATE_ALL
|
1260 HEAP_TAIL_CHECKING_ENABLED
| HEAP_FREE_CHECKING_ENABLED
;
1262 heap
->flags
|= flags
;
1263 heap
->force_flags
|= flags
& ~(HEAP_VALIDATE
| HEAP_DISABLE_COALESCE_ON_FREE
);
1267 /***********************************************************************
1268 * RtlCreateHeap (NTDLL.@)
1270 * Create a new Heap.
1273 * flags [I] HEAP_ flags from "winnt.h"
1274 * addr [I] Desired base address
1275 * totalSize [I] Total size of the heap, or 0 for a growable heap
1276 * commitSize [I] Amount of heap space to commit
1277 * unknown [I] Not yet understood
1278 * definition [I] Heap definition
1281 * Success: A HANDLE to the newly created heap.
1282 * Failure: a NULL HANDLE.
1284 HANDLE WINAPI
RtlCreateHeap( ULONG flags
, PVOID addr
, SIZE_T totalSize
, SIZE_T commitSize
,
1285 PVOID unknown
, PRTL_HEAP_DEFINITION definition
)
1289 /* Allocate the heap block */
1293 totalSize
= HEAP_DEF_SIZE
;
1294 flags
|= HEAP_GROWABLE
;
1297 if (!(subheap
= HEAP_CreateSubHeap( NULL
, addr
, flags
, commitSize
, totalSize
))) return 0;
1299 /* link it into the per-process heap list */
1302 HEAP
*heapPtr
= subheap
->heap
;
1303 RtlEnterCriticalSection( &processHeap
->critSection
);
1304 list_add_head( &processHeap
->entry
, &heapPtr
->entry
);
1305 RtlLeaveCriticalSection( &processHeap
->critSection
);
1309 processHeap
= subheap
->heap
; /* assume the first heap we create is the process main heap */
1310 list_init( &processHeap
->entry
);
1313 heap_set_debug_flags( subheap
->heap
);
1314 return subheap
->heap
;
1318 /***********************************************************************
1319 * RtlDestroyHeap (NTDLL.@)
1321 * Destroy a Heap created with RtlCreateHeap().
1324 * heap [I] Heap to destroy.
1327 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1328 * Failure: The Heap handle, if heap is the process heap.
1330 HANDLE WINAPI
RtlDestroyHeap( HANDLE heap
)
1332 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1333 SUBHEAP
*subheap
, *next
;
1334 ARENA_LARGE
*arena
, *arena_next
;
1338 TRACE("%p\n", heap
);
1339 if (!heapPtr
) return heap
;
1341 if (heap
== processHeap
) return heap
; /* cannot delete the main process heap */
1343 /* remove it from the per-process list */
1344 RtlEnterCriticalSection( &processHeap
->critSection
);
1345 list_remove( &heapPtr
->entry
);
1346 RtlLeaveCriticalSection( &processHeap
->critSection
);
1348 heapPtr
->critSection
.DebugInfo
->Spare
[0] = 0;
1349 RtlDeleteCriticalSection( &heapPtr
->critSection
);
1351 LIST_FOR_EACH_ENTRY_SAFE( arena
, arena_next
, &heapPtr
->large_list
, ARENA_LARGE
, entry
)
1353 list_remove( &arena
->entry
);
1356 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1358 LIST_FOR_EACH_ENTRY_SAFE( subheap
, next
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1360 if (subheap
== &heapPtr
->subheap
) continue; /* do this one last */
1361 subheap_notify_free_all(subheap
);
1362 list_remove( &subheap
->entry
);
1364 addr
= subheap
->base
;
1365 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1367 subheap_notify_free_all(&heapPtr
->subheap
);
1369 addr
= heapPtr
->subheap
.base
;
1370 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1375 /***********************************************************************
1376 * RtlAllocateHeap (NTDLL.@)
1378 * Allocate a memory block from a Heap.
1381 * heap [I] Heap to allocate block from
1382 * flags [I] HEAP_ flags from "winnt.h"
1383 * size [I] Size of the memory block to allocate
1386 * Success: A pointer to the newly allocated block
1390 * This call does not SetLastError().
1392 PVOID WINAPI
RtlAllocateHeap( HANDLE heap
, ULONG flags
, SIZE_T size
)
1395 ARENA_INUSE
*pInUse
;
1397 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1398 SIZE_T rounded_size
;
1400 /* Validate the parameters */
1402 if (!heapPtr
) return NULL
;
1403 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1404 flags
|= heapPtr
->flags
;
1405 rounded_size
= ROUND_SIZE(size
);
1406 if (rounded_size
< size
) /* overflow */
1408 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1411 if (rounded_size
< HEAP_MIN_DATA_SIZE
) rounded_size
= HEAP_MIN_DATA_SIZE
;
1413 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1415 if (rounded_size
>= HEAP_MIN_LARGE_BLOCK_SIZE
&& (flags
& HEAP_GROWABLE
))
1417 void *ret
= allocate_large_block( heap
, flags
, size
);
1418 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1419 if (!ret
&& (flags
& HEAP_GENERATE_EXCEPTIONS
)) RtlRaiseStatus( STATUS_NO_MEMORY
);
1420 notify_alloc( ret
, size
, flags
& HEAP_ZERO_MEMORY
);
1421 TRACE("(%p,%08x,%08lx): returning %p\n", heap
, flags
, size
, ret
);
1425 /* Locate a suitable free block */
1427 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, rounded_size
, &subheap
)))
1429 TRACE("(%p,%08x,%08lx): returning NULL\n",
1430 heap
, flags
, size
);
1431 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1432 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1436 /* Remove the arena from the free list */
1438 list_remove( &pArena
->entry
);
1440 /* Build the in-use arena */
1442 pInUse
= (ARENA_INUSE
*)pArena
;
1444 /* in-use arena is smaller than free arena,
1445 * so we have to add the difference to the size */
1446 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
) + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1447 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1449 /* Shrink the block */
1451 HEAP_ShrinkBlock( subheap
, pInUse
, rounded_size
);
1452 pInUse
->unused_bytes
= (pInUse
->size
& ARENA_SIZE_MASK
) - size
;
1454 notify_alloc( pInUse
+ 1, size
, flags
& HEAP_ZERO_MEMORY
);
1456 if (flags
& HEAP_ZERO_MEMORY
)
1458 clear_block( pInUse
+ 1, size
);
1459 mark_block_uninitialized( (char *)(pInUse
+ 1) + size
, pInUse
->unused_bytes
);
1462 mark_block_uninitialized( pInUse
+ 1, pInUse
->size
& ARENA_SIZE_MASK
);
1464 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1466 TRACE("(%p,%08x,%08lx): returning %p\n", heap
, flags
, size
, pInUse
+ 1 );
1471 /***********************************************************************
1472 * RtlFreeHeap (NTDLL.@)
1474 * Free a memory block allocated with RtlAllocateHeap().
1477 * heap [I] Heap that block was allocated from
1478 * flags [I] HEAP_ flags from "winnt.h"
1479 * ptr [I] Block to free
1482 * Success: TRUE, if ptr is NULL or was freed successfully.
1485 BOOLEAN WINAPI
RtlFreeHeap( HANDLE heap
, ULONG flags
, PVOID ptr
)
1487 ARENA_INUSE
*pInUse
;
1491 /* Validate the parameters */
1493 if (!ptr
) return TRUE
; /* freeing a NULL ptr isn't an error in Win2k */
1495 heapPtr
= HEAP_GetPtr( heap
);
1498 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1502 flags
&= HEAP_NO_SERIALIZE
;
1503 flags
|= heapPtr
->flags
;
1504 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1506 /* Inform valgrind we are trying to free memory, so it can throw up an error message */
1509 /* Some sanity checks */
1510 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1511 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
)))
1513 if (!find_large_block( heapPtr
, ptr
)) goto error
;
1514 free_large_block( heapPtr
, flags
, ptr
);
1517 if ((char *)pInUse
< (char *)subheap
->base
+ subheap
->headerSize
) goto error
;
1518 if (!HEAP_ValidateInUseArena( subheap
, pInUse
, QUIET
)) goto error
;
1520 /* Turn the block into a free block */
1522 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1525 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1526 TRACE("(%p,%08x,%p): returning TRUE\n", heap
, flags
, ptr
);
1530 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1531 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1532 TRACE("(%p,%08x,%p): returning FALSE\n", heap
, flags
, ptr
);
1537 /***********************************************************************
1538 * RtlReAllocateHeap (NTDLL.@)
1540 * Change the size of a memory block allocated with RtlAllocateHeap().
1543 * heap [I] Heap that block was allocated from
1544 * flags [I] HEAP_ flags from "winnt.h"
1545 * ptr [I] Block to resize
1546 * size [I] Size of the memory block to allocate
1549 * Success: A pointer to the resized block (which may be different).
1552 PVOID WINAPI
RtlReAllocateHeap( HANDLE heap
, ULONG flags
, PVOID ptr
, SIZE_T size
)
1554 ARENA_INUSE
*pArena
;
1557 SIZE_T oldBlockSize
, oldActualSize
, rounded_size
;
1560 if (!ptr
) return NULL
;
1561 if (!(heapPtr
= HEAP_GetPtr( heap
)))
1563 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1567 /* Validate the parameters */
1569 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1570 HEAP_REALLOC_IN_PLACE_ONLY
;
1571 flags
|= heapPtr
->flags
;
1572 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1574 rounded_size
= ROUND_SIZE(size
);
1575 if (rounded_size
< size
) goto oom
; /* overflow */
1576 if (rounded_size
< HEAP_MIN_DATA_SIZE
) rounded_size
= HEAP_MIN_DATA_SIZE
;
1578 pArena
= (ARENA_INUSE
*)ptr
- 1;
1579 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, pArena
)))
1581 if (!find_large_block( heapPtr
, ptr
)) goto error
;
1582 if (!(ret
= realloc_large_block( heapPtr
, flags
, ptr
, size
))) goto oom
;
1584 notify_alloc( ret
, size
, flags
& HEAP_ZERO_MEMORY
);
1587 if ((char *)pArena
< (char *)subheap
->base
+ subheap
->headerSize
) goto error
;
1588 if (!HEAP_ValidateInUseArena( subheap
, pArena
, QUIET
)) goto error
;
1590 /* Check if we need to grow the block */
1592 oldBlockSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1593 oldActualSize
= (pArena
->size
& ARENA_SIZE_MASK
) - pArena
->unused_bytes
;
1594 if (rounded_size
> oldBlockSize
)
1596 char *pNext
= (char *)(pArena
+ 1) + oldBlockSize
;
1598 if (rounded_size
>= HEAP_MIN_LARGE_BLOCK_SIZE
&& (flags
& HEAP_GROWABLE
))
1600 if (flags
& HEAP_REALLOC_IN_PLACE_ONLY
) goto oom
;
1601 if (!(ret
= allocate_large_block( heapPtr
, flags
, size
))) goto oom
;
1602 notify_alloc( ret
, size
, flags
& HEAP_ZERO_MEMORY
);
1603 memcpy( ret
, pArena
+ 1, oldActualSize
);
1604 notify_free( pArena
+ 1 );
1605 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1608 if ((pNext
< (char *)subheap
->base
+ subheap
->size
) &&
1609 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1610 (oldBlockSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= rounded_size
))
1612 /* The next block is free and large enough */
1613 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1614 list_remove( &pFree
->entry
);
1615 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1616 if (!HEAP_Commit( subheap
, pArena
, rounded_size
)) goto oom
;
1617 notify_free( pArena
+ 1 );
1618 HEAP_ShrinkBlock( subheap
, pArena
, rounded_size
);
1619 notify_alloc( pArena
+ 1, size
, FALSE
);
1620 /* FIXME: this is wrong as we may lose old VBits settings */
1621 mark_block_initialized( pArena
+ 1, oldActualSize
);
1623 else /* Do it the hard way */
1626 ARENA_INUSE
*pInUse
;
1627 SUBHEAP
*newsubheap
;
1629 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1630 !(pNew
= HEAP_FindFreeBlock( heapPtr
, rounded_size
, &newsubheap
)))
1633 /* Build the in-use arena */
1635 list_remove( &pNew
->entry
);
1636 pInUse
= (ARENA_INUSE
*)pNew
;
1637 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1638 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1639 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1640 HEAP_ShrinkBlock( newsubheap
, pInUse
, rounded_size
);
1642 mark_block_initialized( pInUse
+ 1, oldActualSize
);
1643 notify_alloc( pInUse
+ 1, size
, FALSE
);
1644 memcpy( pInUse
+ 1, pArena
+ 1, oldActualSize
);
1646 /* Free the previous block */
1648 notify_free( pArena
+ 1 );
1649 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1650 subheap
= newsubheap
;
1656 /* Shrink the block */
1657 notify_free( pArena
+ 1 );
1658 HEAP_ShrinkBlock( subheap
, pArena
, rounded_size
);
1659 notify_alloc( pArena
+ 1, size
, FALSE
);
1660 /* FIXME: this is wrong as we may lose old VBits settings */
1661 mark_block_initialized( pArena
+ 1, size
);
1664 pArena
->unused_bytes
= (pArena
->size
& ARENA_SIZE_MASK
) - size
;
1666 /* Clear the extra bytes if needed */
1668 if (size
> oldActualSize
)
1670 if (flags
& HEAP_ZERO_MEMORY
)
1672 clear_block( (char *)(pArena
+ 1) + oldActualSize
, size
- oldActualSize
);
1673 mark_block_uninitialized( (char *)(pArena
+ 1) + size
, pArena
->unused_bytes
);
1676 mark_block_uninitialized( (char *)(pArena
+ 1) + oldActualSize
,
1677 (pArena
->size
& ARENA_SIZE_MASK
) - oldActualSize
);
1680 /* Return the new arena */
1684 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1685 TRACE("(%p,%08x,%p,%08lx): returning %p\n", heap
, flags
, ptr
, size
, ret
);
1689 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1690 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1691 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY
);
1692 TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap
, flags
, ptr
, size
);
1696 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1697 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1698 TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap
, flags
, ptr
, size
);
1703 /***********************************************************************
1704 * RtlCompactHeap (NTDLL.@)
1706 * Compact the free space in a Heap.
1709 * heap [I] Heap that block was allocated from
1710 * flags [I] HEAP_ flags from "winnt.h"
1713 * The number of bytes compacted.
1716 * This function is a harmless stub.
1718 ULONG WINAPI
RtlCompactHeap( HANDLE heap
, ULONG flags
)
1720 static BOOL reported
;
1721 if (!reported
++) FIXME( "(%p, 0x%x) stub\n", heap
, flags
);
1726 /***********************************************************************
1727 * RtlLockHeap (NTDLL.@)
1732 * heap [I] Heap to lock
1735 * Success: TRUE. The Heap is locked.
1736 * Failure: FALSE, if heap is invalid.
1738 BOOLEAN WINAPI
RtlLockHeap( HANDLE heap
)
1740 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1741 if (!heapPtr
) return FALSE
;
1742 RtlEnterCriticalSection( &heapPtr
->critSection
);
1747 /***********************************************************************
1748 * RtlUnlockHeap (NTDLL.@)
1753 * heap [I] Heap to unlock
1756 * Success: TRUE. The Heap is unlocked.
1757 * Failure: FALSE, if heap is invalid.
1759 BOOLEAN WINAPI
RtlUnlockHeap( HANDLE heap
)
1761 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1762 if (!heapPtr
) return FALSE
;
1763 RtlLeaveCriticalSection( &heapPtr
->critSection
);
1768 /***********************************************************************
1769 * RtlSizeHeap (NTDLL.@)
1771 * Get the actual size of a memory block allocated from a Heap.
1774 * heap [I] Heap that block was allocated from
1775 * flags [I] HEAP_ flags from "winnt.h"
1776 * ptr [I] Block to get the size of
1779 * Success: The size of the block.
1780 * Failure: -1, heap or ptr are invalid.
1783 * The size may be bigger than what was passed to RtlAllocateHeap().
1785 SIZE_T WINAPI
RtlSizeHeap( HANDLE heap
, ULONG flags
, const void *ptr
)
1788 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1792 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1795 flags
&= HEAP_NO_SERIALIZE
;
1796 flags
|= heapPtr
->flags
;
1797 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1798 if (!HEAP_IsRealArena( heapPtr
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1800 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1805 const ARENA_INUSE
*pArena
= (const ARENA_INUSE
*)ptr
- 1;
1806 if (pArena
->size
== ARENA_LARGE_SIZE
)
1808 const ARENA_LARGE
*large_arena
= (const ARENA_LARGE
*)ptr
- 1;
1809 ret
= large_arena
->data_size
;
1811 else ret
= (pArena
->size
& ARENA_SIZE_MASK
) - pArena
->unused_bytes
;
1813 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1815 TRACE("(%p,%08x,%p): returning %08lx\n", heap
, flags
, ptr
, ret
);
1820 /***********************************************************************
1821 * RtlValidateHeap (NTDLL.@)
1823 * Determine if a block is a valid allocation from a heap.
1826 * heap [I] Heap that block was allocated from
1827 * flags [I] HEAP_ flags from "winnt.h"
1828 * ptr [I] Block to check
1831 * Success: TRUE. The block was allocated from heap.
1832 * Failure: FALSE, if heap is invalid or ptr was not allocated from it.
1834 BOOLEAN WINAPI
RtlValidateHeap( HANDLE heap
, ULONG flags
, LPCVOID ptr
)
1836 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1837 if (!heapPtr
) return FALSE
;
1838 return HEAP_IsRealArena( heapPtr
, flags
, ptr
, QUIET
);
1842 /***********************************************************************
1843 * RtlWalkHeap (NTDLL.@)
1846 * The PROCESS_HEAP_ENTRY flag values seem different between this
1847 * function and HeapWalk(). To be checked.
1849 NTSTATUS WINAPI
RtlWalkHeap( HANDLE heap
, PVOID entry_ptr
)
1851 LPPROCESS_HEAP_ENTRY entry
= entry_ptr
; /* FIXME */
1852 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1853 SUBHEAP
*sub
, *currentheap
= NULL
;
1856 int region_index
= 0;
1858 if (!heapPtr
|| !entry
) return STATUS_INVALID_PARAMETER
;
1860 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1862 /* FIXME: enumerate large blocks too */
1864 /* set ptr to the next arena to be examined */
1866 if (!entry
->lpData
) /* first call (init) ? */
1868 TRACE("begin walking of heap %p.\n", heap
);
1869 currentheap
= &heapPtr
->subheap
;
1870 ptr
= (char*)currentheap
->base
+ currentheap
->headerSize
;
1874 ptr
= entry
->lpData
;
1875 LIST_FOR_EACH_ENTRY( sub
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1877 if ((ptr
>= (char *)sub
->base
) &&
1878 (ptr
< (char *)sub
->base
+ sub
->size
))
1885 if (currentheap
== NULL
)
1887 ERR("no matching subheap found, shouldn't happen !\n");
1888 ret
= STATUS_NO_MORE_ENTRIES
;
1892 if (((ARENA_INUSE
*)ptr
- 1)->magic
== ARENA_INUSE_MAGIC
)
1894 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1895 ptr
+= pArena
->size
& ARENA_SIZE_MASK
;
1897 else if (((ARENA_FREE
*)ptr
- 1)->magic
== ARENA_FREE_MAGIC
)
1899 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
- 1;
1900 ptr
+= pArena
->size
& ARENA_SIZE_MASK
;
1903 ptr
+= entry
->cbData
; /* point to next arena */
1905 if (ptr
> (char *)currentheap
->base
+ currentheap
->size
- 1)
1906 { /* proceed with next subheap */
1907 struct list
*next
= list_next( &heapPtr
->subheap_list
, ¤theap
->entry
);
1909 { /* successfully finished */
1910 TRACE("end reached.\n");
1911 ret
= STATUS_NO_MORE_ENTRIES
;
1914 currentheap
= LIST_ENTRY( next
, SUBHEAP
, entry
);
1915 ptr
= (char *)currentheap
->base
+ currentheap
->headerSize
;
1920 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1922 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1924 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1926 entry
->lpData
= pArena
+ 1;
1927 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1928 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1929 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1933 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1935 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1937 entry
->lpData
= pArena
+ 1;
1938 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1939 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1940 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1941 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1942 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1945 entry
->iRegionIndex
= region_index
;
1947 /* first element of heap ? */
1948 if (ptr
== (char *)currentheap
->base
+ currentheap
->headerSize
)
1950 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1951 entry
->u
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1952 entry
->u
.Region
.dwUnCommittedSize
=
1953 currentheap
->size
- currentheap
->commitSize
;
1954 entry
->u
.Region
.lpFirstBlock
= /* first valid block */
1955 (char *)currentheap
->base
+ currentheap
->headerSize
;
1956 entry
->u
.Region
.lpLastBlock
= /* first invalid block */
1957 (char *)currentheap
->base
+ currentheap
->size
;
1959 ret
= STATUS_SUCCESS
;
1960 if (TRACE_ON(heap
)) HEAP_DumpEntry(entry
);
1963 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1968 /***********************************************************************
1969 * RtlGetProcessHeaps (NTDLL.@)
1971 * Get the Heaps belonging to the current process.
1974 * count [I] size of heaps
1975 * heaps [O] Destination array for heap HANDLE's
1978 * Success: The number of Heaps allocated by the process.
1981 ULONG WINAPI
RtlGetProcessHeaps( ULONG count
, HANDLE
*heaps
)
1983 ULONG total
= 1; /* main heap */
1986 RtlEnterCriticalSection( &processHeap
->critSection
);
1987 LIST_FOR_EACH( ptr
, &processHeap
->entry
) total
++;
1990 *heaps
++ = processHeap
;
1991 LIST_FOR_EACH( ptr
, &processHeap
->entry
)
1992 *heaps
++ = LIST_ENTRY( ptr
, HEAP
, entry
);
1994 RtlLeaveCriticalSection( &processHeap
->critSection
);
1998 /***********************************************************************
1999 * RtlQueryHeapInformation (NTDLL.@)
2001 NTSTATUS WINAPI
RtlQueryHeapInformation( HANDLE heap
, HEAP_INFORMATION_CLASS info_class
,
2002 PVOID info
, SIZE_T size_in
, PSIZE_T size_out
)
2006 case HeapCompatibilityInformation
:
2007 if (size_out
) *size_out
= sizeof(ULONG
);
2009 if (size_in
< sizeof(ULONG
))
2010 return STATUS_BUFFER_TOO_SMALL
;
2012 *(ULONG
*)info
= 0; /* standard heap */
2013 return STATUS_SUCCESS
;
2016 FIXME("Unknown heap information class %u\n", info_class
);
2017 return STATUS_INVALID_INFO_CLASS
;