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
29 #ifdef HAVE_VALGRIND_MEMCHECK_H
30 #include <valgrind/memcheck.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
36 #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 based on what Pietrek describes in his
48 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
49 * the same, but could be easily adapted if it turns out some programs
53 /* FIXME: use SIZE_T for 'size' structure members, but we need to make sure
54 * that there is no unaligned accesses to structure fields.
57 typedef struct tagARENA_INUSE
59 DWORD size; /* Block size; must be the first field */
60 DWORD magic : 24; /* Magic number */
61 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) */
64 typedef struct tagARENA_FREE
66 DWORD size; /* Block size; must be the first field */
67 DWORD magic; /* Magic number */
68 struct list entry; /* Entry in free list */
71 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
72 #define ARENA_FLAG_PREV_FREE 0x00000002
73 #define ARENA_SIZE_MASK (~3)
74 #define ARENA_INUSE_MAGIC 0x455355 /* Value for arena 'magic' field */
75 #define ARENA_FREE_MAGIC 0x45455246 /* Value for arena 'magic' field */
77 #define ARENA_INUSE_FILLER 0x55
78 #define ARENA_FREE_FILLER 0xaa
80 #define ALIGNMENT 8 /* everything is aligned on 8 byte boundaries */
81 #define ROUND_SIZE(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
83 #define QUIET 1 /* Suppress messages */
84 #define NOISY 0 /* Report all errors */
86 /* minimum data size (without arenas) of an allocated block */
87 /* make sure that it's larger than a free list entry */
88 #define HEAP_MIN_DATA_SIZE (2 * sizeof(struct list))
89 /* minimum size that must remain to shrink an allocated block */
90 #define HEAP_MIN_SHRINK_SIZE (HEAP_MIN_DATA_SIZE+sizeof(ARENA_FREE))
92 /* Max size of the blocks on the free lists */
93 static const DWORD HEAP_freeListSizes[] =
95 0x10, 0x20, 0x30, 0x40, 0x60, 0x80, 0x100, 0x200, 0x400, 0x1000, ~0UL
97 #define HEAP_NB_FREE_LISTS (sizeof(HEAP_freeListSizes)/sizeof(HEAP_freeListSizes[0]))
106 typedef struct tagSUBHEAP
108 DWORD size; /* Size of the whole sub-heap */
109 DWORD commitSize; /* Committed size of the sub-heap */
110 DWORD headerSize; /* Size of the heap header */
111 struct tagSUBHEAP *next; /* Next sub-heap */
112 struct tagHEAP *heap; /* Main heap structure */
113 DWORD magic; /* Magic number */
116 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
118 typedef struct tagHEAP
120 SUBHEAP subheap; /* First sub-heap */
121 struct list entry; /* Entry in process heap list */
122 RTL_CRITICAL_SECTION critSection; /* Critical section for serialization */
123 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
124 DWORD flags; /* Heap flags */
125 DWORD magic; /* Magic number */
128 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
130 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
131 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
133 static HEAP *processHeap; /* main process heap */
135 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
137 /* mark a block of memory as free for debugging purposes */
138 static inline void mark_block_free( void *ptr, SIZE_T size )
140 if (TRACE_ON(heap)) memset( ptr, ARENA_FREE_FILLER, size );
141 #ifdef VALGRIND_MAKE_NOACCESS
142 VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
146 /* mark a block of memory as initialized for debugging purposes */
147 static inline void mark_block_initialized( void *ptr, SIZE_T size )
149 #ifdef VALGRIND_MAKE_READABLE
150 VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ));
154 /* mark a block of memory as uninitialized for debugging purposes */
155 static inline void mark_block_uninitialized( void *ptr, SIZE_T size )
157 #ifdef VALGRIND_MAKE_WRITABLE
158 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
162 memset( ptr, ARENA_INUSE_FILLER, size );
163 #ifdef VALGRIND_MAKE_WRITABLE
164 /* make it uninitialized to valgrind again */
165 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
170 /* clear contents of a block of memory */
171 static inline void clear_block( void *ptr, SIZE_T size )
173 mark_block_initialized( ptr, size );
174 memset( ptr, 0, size );
177 /* notify that a new block of memory has been allocated for debugging purposes */
178 static inline void notify_alloc( void *ptr, SIZE_T size, BOOL init )
180 #ifdef VALGRIND_MALLOCLIKE_BLOCK
181 VALGRIND_MALLOCLIKE_BLOCK( ptr, size, 0, init );
185 /* notify that a block of memory has been freed for debugging purposes */
186 static inline void notify_free( void *ptr )
188 #ifdef VALGRIND_FREELIKE_BLOCK
189 VALGRIND_FREELIKE_BLOCK( ptr, 0 );
193 /* locate a free list entry of the appropriate size */
194 /* size is the size of the whole block including the arena header */
195 static inline unsigned int get_freelist_index( SIZE_T size )
199 size -= sizeof(ARENA_FREE);
200 for (i = 0; i < HEAP_NB_FREE_LISTS - 1; i++) if (size <= HEAP_freeListSizes[i]) break;
204 static RTL_CRITICAL_SECTION_DEBUG process_heap_critsect_debug =
206 0, 0, NULL, /* will be set later */
207 { &process_heap_critsect_debug.ProcessLocksList, &process_heap_critsect_debug.ProcessLocksList },
208 0, 0, { (DWORD_PTR)(__FILE__ ": main process heap section") }
212 /***********************************************************************
215 static void HEAP_Dump( HEAP *heap )
221 DPRINTF( "Heap: %p\n", heap );
222 DPRINTF( "Next: %p Sub-heaps: %p",
223 LIST_ENTRY( heap->entry.next, HEAP, entry ), &heap->subheap );
224 subheap = &heap->subheap;
225 while (subheap->next)
227 DPRINTF( " -> %p", subheap->next );
228 subheap = subheap->next;
231 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
232 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
233 DPRINTF( "%p free %08x prev=%p next=%p\n",
234 &heap->freeList[i].arena, HEAP_freeListSizes[i],
235 LIST_ENTRY( heap->freeList[i].arena.entry.prev, ARENA_FREE, entry ),
236 LIST_ENTRY( heap->freeList[i].arena.entry.next, ARENA_FREE, entry ));
238 subheap = &heap->subheap;
241 SIZE_T freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
242 DPRINTF( "\n\nSub-heap %p: size=%08x committed=%08x\n",
243 subheap, subheap->size, subheap->commitSize );
245 DPRINTF( "\n Block Stat Size Id\n" );
246 ptr = (char*)subheap + subheap->headerSize;
247 while (ptr < (char *)subheap + subheap->size)
249 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
251 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
252 DPRINTF( "%p free %08x prev=%p next=%p\n",
253 pArena, pArena->size & ARENA_SIZE_MASK,
254 LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry ),
255 LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry ) );
256 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
257 arenaSize += sizeof(ARENA_FREE);
258 freeSize += pArena->size & ARENA_SIZE_MASK;
260 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
262 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
263 DPRINTF( "%p Used %08x back=%p\n",
264 pArena, pArena->size & ARENA_SIZE_MASK, *((ARENA_FREE **)pArena - 1) );
265 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
266 arenaSize += sizeof(ARENA_INUSE);
267 usedSize += pArena->size & ARENA_SIZE_MASK;
271 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
272 DPRINTF( "%p used %08x\n", pArena, pArena->size & ARENA_SIZE_MASK );
273 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
274 arenaSize += sizeof(ARENA_INUSE);
275 usedSize += pArena->size & ARENA_SIZE_MASK;
278 DPRINTF( "\nTotal: Size=%08x Committed=%08x Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
279 subheap->size, subheap->commitSize, freeSize, usedSize,
280 arenaSize, (arenaSize * 100) / subheap->size );
281 subheap = subheap->next;
286 static void HEAP_DumpEntry( LPPROCESS_HEAP_ENTRY entry )
289 TRACE( "Dumping entry %p\n", entry );
290 TRACE( "lpData\t\t: %p\n", entry->lpData );
291 TRACE( "cbData\t\t: %08x\n", entry->cbData);
292 TRACE( "cbOverhead\t: %08x\n", entry->cbOverhead);
293 TRACE( "iRegionIndex\t: %08x\n", entry->iRegionIndex);
294 TRACE( "WFlags\t\t: ");
295 if (entry->wFlags & PROCESS_HEAP_REGION)
296 TRACE( "PROCESS_HEAP_REGION ");
297 if (entry->wFlags & PROCESS_HEAP_UNCOMMITTED_RANGE)
298 TRACE( "PROCESS_HEAP_UNCOMMITTED_RANGE ");
299 if (entry->wFlags & PROCESS_HEAP_ENTRY_BUSY)
300 TRACE( "PROCESS_HEAP_ENTRY_BUSY ");
301 if (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE)
302 TRACE( "PROCESS_HEAP_ENTRY_MOVEABLE ");
303 if (entry->wFlags & PROCESS_HEAP_ENTRY_DDESHARE)
304 TRACE( "PROCESS_HEAP_ENTRY_DDESHARE ");
305 rem_flags = entry->wFlags &
306 ~(PROCESS_HEAP_REGION | PROCESS_HEAP_UNCOMMITTED_RANGE |
307 PROCESS_HEAP_ENTRY_BUSY | PROCESS_HEAP_ENTRY_MOVEABLE|
308 PROCESS_HEAP_ENTRY_DDESHARE);
310 TRACE( "Unknown %08x", rem_flags);
312 if ((entry->wFlags & PROCESS_HEAP_ENTRY_BUSY )
313 && (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE))
316 TRACE( "BLOCK->hMem\t\t:%p\n", entry->u.Block.hMem);
318 if (entry->wFlags & PROCESS_HEAP_REGION)
320 TRACE( "Region.dwCommittedSize\t:%08x\n",entry->u.Region.dwCommittedSize);
321 TRACE( "Region.dwUnCommittedSize\t:%08x\n",entry->u.Region.dwUnCommittedSize);
322 TRACE( "Region.lpFirstBlock\t:%p\n",entry->u.Region.lpFirstBlock);
323 TRACE( "Region.lpLastBlock\t:%p\n",entry->u.Region.lpLastBlock);
327 /***********************************************************************
330 * Pointer to the heap
333 static HEAP *HEAP_GetPtr(
334 HANDLE heap /* [in] Handle to the heap */
336 HEAP *heapPtr = (HEAP *)heap;
337 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
339 ERR("Invalid heap %p!\n", heap );
342 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
344 HEAP_Dump( heapPtr );
352 /***********************************************************************
353 * HEAP_InsertFreeBlock
355 * Insert a free block into the free list.
357 static inline void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena, BOOL last )
359 FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( pArena->size + sizeof(*pArena) );
362 /* insert at end of free list, i.e. before the next free list entry */
364 if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS]) pEntry = heap->freeList;
365 list_add_before( &pEntry->arena.entry, &pArena->entry );
369 /* insert at head of free list */
370 list_add_after( &pEntry->arena.entry, &pArena->entry );
372 pArena->size |= ARENA_FLAG_FREE;
376 /***********************************************************************
378 * Find the sub-heap containing a given address.
384 static SUBHEAP *HEAP_FindSubHeap(
385 const HEAP *heap, /* [in] Heap pointer */
386 LPCVOID ptr /* [in] Address */
388 const SUBHEAP *sub = &heap->subheap;
391 if (((const char *)ptr >= (const char *)sub) &&
392 ((const char *)ptr < (const char *)sub + sub->size)) return (SUBHEAP*)sub;
399 /***********************************************************************
402 * Make sure the heap storage is committed for a given size in the specified arena.
404 static inline BOOL HEAP_Commit( SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T data_size )
406 void *ptr = (char *)(pArena + 1) + data_size + sizeof(ARENA_FREE);
407 SIZE_T size = (char *)ptr - (char *)subheap;
408 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
409 if (size > subheap->size) size = subheap->size;
410 if (size <= subheap->commitSize) return TRUE;
411 size -= subheap->commitSize;
412 ptr = (char *)subheap + subheap->commitSize;
413 if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0,
414 &size, MEM_COMMIT, PAGE_READWRITE ))
416 WARN("Could not commit %08lx bytes at %p for heap %p\n",
417 size, ptr, subheap->heap );
420 subheap->commitSize += size;
425 /***********************************************************************
428 * If possible, decommit the heap storage from (including) 'ptr'.
430 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
433 SIZE_T decommit_size;
434 SIZE_T size = (char *)ptr - (char *)subheap;
436 /* round to next block and add one full block */
437 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
438 if (size >= subheap->commitSize) return TRUE;
439 decommit_size = subheap->commitSize - size;
440 addr = (char *)subheap + size;
442 if (NtFreeVirtualMemory( NtCurrentProcess(), &addr, &decommit_size, MEM_DECOMMIT ))
444 WARN("Could not decommit %08lx bytes at %p for heap %p\n",
445 decommit_size, (char *)subheap + size, subheap->heap );
448 subheap->commitSize -= decommit_size;
453 /***********************************************************************
454 * HEAP_CreateFreeBlock
456 * Create a free block at a specified address. 'size' is the size of the
457 * whole block, including the new arena.
459 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, SIZE_T size )
465 /* Create a free arena */
466 mark_block_uninitialized( ptr, sizeof( ARENA_FREE ) );
467 pFree = (ARENA_FREE *)ptr;
468 pFree->magic = ARENA_FREE_MAGIC;
470 /* If debugging, erase the freed block content */
472 pEnd = (char *)ptr + size;
473 if (pEnd > (char *)subheap + subheap->commitSize) pEnd = (char *)subheap + subheap->commitSize;
474 if (pEnd > (char *)(pFree + 1)) mark_block_free( pFree + 1, pEnd - (char *)(pFree + 1) );
476 /* Check if next block is free also */
478 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
479 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
481 /* Remove the next arena from the free list */
482 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
483 list_remove( &pNext->entry );
484 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
485 mark_block_free( pNext, sizeof(ARENA_FREE) );
488 /* Set the next block PREV_FREE flag and pointer */
490 last = ((char *)ptr + size >= (char *)subheap + subheap->size);
493 DWORD *pNext = (DWORD *)((char *)ptr + size);
494 *pNext |= ARENA_FLAG_PREV_FREE;
495 mark_block_initialized( pNext - 1, sizeof( ARENA_FREE * ) );
496 *((ARENA_FREE **)pNext - 1) = pFree;
499 /* Last, insert the new block into the free list */
501 pFree->size = size - sizeof(*pFree);
502 HEAP_InsertFreeBlock( subheap->heap, pFree, last );
506 /***********************************************************************
507 * HEAP_MakeInUseBlockFree
509 * Turn an in-use block into a free block. Can also decommit the end of
510 * the heap, and possibly even free the sub-heap altogether.
512 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
515 SIZE_T size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
517 /* Check if we can merge with previous block */
519 if (pArena->size & ARENA_FLAG_PREV_FREE)
521 pFree = *((ARENA_FREE **)pArena - 1);
522 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
523 /* Remove it from the free list */
524 list_remove( &pFree->entry );
526 else pFree = (ARENA_FREE *)pArena;
528 /* Create a free block */
530 HEAP_CreateFreeBlock( subheap, pFree, size );
531 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
532 if ((char *)pFree + size < (char *)subheap + subheap->size)
533 return; /* Not the last block, so nothing more to do */
535 /* Free the whole sub-heap if it's empty and not the original one */
537 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
538 (subheap != &subheap->heap->subheap))
541 SUBHEAP *pPrev = &subheap->heap->subheap;
542 /* Remove the free block from the list */
543 list_remove( &pFree->entry );
544 /* Remove the subheap from the list */
545 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
546 if (pPrev) pPrev->next = subheap->next;
547 /* Free the memory */
549 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&subheap, &size, MEM_RELEASE );
553 /* Decommit the end of the heap */
555 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
559 /***********************************************************************
562 * Shrink an in-use block.
564 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T size)
566 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_SHRINK_SIZE)
568 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
569 (pArena->size & ARENA_SIZE_MASK) - size );
570 /* assign size plus previous arena flags */
571 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
575 /* Turn off PREV_FREE flag in next block */
576 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
577 if (pNext < (char *)subheap + subheap->size)
578 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
582 /***********************************************************************
585 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
586 SIZE_T commitSize, SIZE_T totalSize )
589 FREE_LIST_ENTRY *pEntry;
594 if (flags & HEAP_SHARED)
595 commitSize = totalSize; /* always commit everything in a shared heap */
596 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address, 0,
597 &commitSize, MEM_COMMIT, PAGE_READWRITE ))
599 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
603 /* Fill the sub-heap structure */
605 subheap = (SUBHEAP *)address;
606 subheap->heap = heap;
607 subheap->size = totalSize;
608 subheap->commitSize = commitSize;
609 subheap->magic = SUBHEAP_MAGIC;
611 if ( subheap != (SUBHEAP *)heap )
613 /* If this is a secondary subheap, insert it into list */
615 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
616 subheap->next = heap->subheap.next;
617 heap->subheap.next = subheap;
621 /* If this is a primary subheap, initialize main heap */
623 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
624 subheap->next = NULL;
626 heap->magic = HEAP_MAGIC;
628 /* Build the free lists */
630 list_init( &heap->freeList[0].arena.entry );
631 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
633 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
634 pEntry->arena.magic = ARENA_FREE_MAGIC;
635 if (i) list_add_after( &pEntry[-1].arena.entry, &pEntry->arena.entry );
638 /* Initialize critical section */
640 if (!processHeap) /* do it by hand to avoid memory allocations */
642 heap->critSection.DebugInfo = &process_heap_critsect_debug;
643 heap->critSection.LockCount = -1;
644 heap->critSection.RecursionCount = 0;
645 heap->critSection.OwningThread = 0;
646 heap->critSection.LockSemaphore = 0;
647 heap->critSection.SpinCount = 0;
648 process_heap_critsect_debug.CriticalSection = &heap->critSection;
650 else RtlInitializeCriticalSection( &heap->critSection );
652 if (flags & HEAP_SHARED)
654 /* let's assume that only one thread at a time will try to do this */
655 HANDLE sem = heap->critSection.LockSemaphore;
656 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
658 NtDuplicateObject( NtCurrentProcess(), sem, NtCurrentProcess(), &sem, 0, 0,
659 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
660 heap->critSection.LockSemaphore = sem;
664 /* Create the first free block */
666 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
667 subheap->size - subheap->headerSize );
672 /***********************************************************************
675 * Create a sub-heap of the given size.
676 * If heap == NULL, creates a main heap.
678 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
679 SIZE_T commitSize, SIZE_T totalSize )
681 LPVOID address = base;
683 /* round-up sizes on a 64K boundary */
684 totalSize = (totalSize + 0xffff) & 0xffff0000;
685 commitSize = (commitSize + 0xffff) & 0xffff0000;
686 if (!commitSize) commitSize = 0x10000;
687 if (totalSize < commitSize) totalSize = commitSize;
691 /* allocate the memory block */
692 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address, 0, &totalSize,
693 MEM_RESERVE, PAGE_READWRITE ))
695 WARN("Could not allocate %08lx bytes\n", totalSize );
700 /* Initialize subheap */
702 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
703 address, flags, commitSize, totalSize ))
706 if (!base) NtFreeVirtualMemory( NtCurrentProcess(), &address, &size, MEM_RELEASE );
710 return (SUBHEAP *)address;
714 /***********************************************************************
717 * Find a free block at least as large as the requested size, and make sure
718 * the requested size is committed.
720 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, SIZE_T size,
721 SUBHEAP **ppSubHeap )
725 FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( size + sizeof(ARENA_INUSE) );
727 /* Find a suitable free list, and in it find a block large enough */
729 ptr = &pEntry->arena.entry;
730 while ((ptr = list_next( &heap->freeList[0].arena.entry, ptr )))
732 ARENA_FREE *pArena = LIST_ENTRY( ptr, ARENA_FREE, entry );
733 SIZE_T arena_size = (pArena->size & ARENA_SIZE_MASK) +
734 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
735 if (arena_size >= size)
737 subheap = HEAP_FindSubHeap( heap, pArena );
738 if (!HEAP_Commit( subheap, (ARENA_INUSE *)pArena, size )) return NULL;
739 *ppSubHeap = subheap;
744 /* If no block was found, attempt to grow the heap */
746 if (!(heap->flags & HEAP_GROWABLE))
748 WARN("Not enough space in heap %p for %08lx bytes\n", heap, size );
751 /* make sure that we have a big enough size *committed* to fit another
752 * last free arena in !
753 * So just one heap struct, one first free arena which will eventually
754 * get used, and a second free arena that might get assigned all remaining
755 * free space in HEAP_ShrinkBlock() */
756 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + sizeof(ARENA_FREE);
757 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
758 max( HEAP_DEF_SIZE, size ) )))
761 TRACE("created new sub-heap %p of %08lx bytes for heap %p\n",
762 subheap, size, heap );
764 *ppSubHeap = subheap;
765 return (ARENA_FREE *)(subheap + 1);
769 /***********************************************************************
770 * HEAP_IsValidArenaPtr
772 * Check that the pointer is inside the range possible for arenas.
774 static BOOL HEAP_IsValidArenaPtr( const HEAP *heap, const void *ptr )
777 const SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
778 if (!subheap) return FALSE;
779 if ((const char *)ptr >= (const char *)subheap + subheap->headerSize) return TRUE;
780 if (subheap != &heap->subheap) return FALSE;
781 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
782 if (ptr == (const void *)&heap->freeList[i].arena) return TRUE;
787 /***********************************************************************
788 * HEAP_ValidateFreeArena
790 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
792 ARENA_FREE *prev, *next;
793 char *heapEnd = (char *)subheap + subheap->size;
795 /* Check for unaligned pointers */
796 if ( (ULONG_PTR)pArena % ALIGNMENT != 0 )
798 ERR("Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
802 /* Check magic number */
803 if (pArena->magic != ARENA_FREE_MAGIC)
805 ERR("Heap %p: invalid free arena magic for %p\n", subheap->heap, pArena );
808 /* Check size flags */
809 if (!(pArena->size & ARENA_FLAG_FREE) ||
810 (pArena->size & ARENA_FLAG_PREV_FREE))
812 ERR("Heap %p: bad flags %08x for free arena %p\n",
813 subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
816 /* Check arena size */
817 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
819 ERR("Heap %p: bad size %08x for free arena %p\n",
820 subheap->heap, pArena->size & ARENA_SIZE_MASK, pArena );
823 /* Check that next pointer is valid */
824 next = LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry );
825 if (!HEAP_IsValidArenaPtr( subheap->heap, next ))
827 ERR("Heap %p: bad next ptr %p for arena %p\n",
828 subheap->heap, next, pArena );
831 /* Check that next arena is free */
832 if (!(next->size & ARENA_FLAG_FREE) || (next->magic != ARENA_FREE_MAGIC))
834 ERR("Heap %p: next arena %p invalid for %p\n",
835 subheap->heap, next, pArena );
838 /* Check that prev pointer is valid */
839 prev = LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry );
840 if (!HEAP_IsValidArenaPtr( subheap->heap, prev ))
842 ERR("Heap %p: bad prev ptr %p for arena %p\n",
843 subheap->heap, prev, pArena );
846 /* Check that prev arena is free */
847 if (!(prev->size & ARENA_FLAG_FREE) || (prev->magic != ARENA_FREE_MAGIC))
849 /* this often means that the prev arena got overwritten
850 * by a memory write before that prev arena */
851 ERR("Heap %p: prev arena %p invalid for %p\n",
852 subheap->heap, prev, pArena );
855 /* Check that next block has PREV_FREE flag */
856 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
858 if (!(*(DWORD *)((char *)(pArena + 1) +
859 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
861 ERR("Heap %p: free arena %p next block has no PREV_FREE flag\n",
862 subheap->heap, pArena );
865 /* Check next block back pointer */
866 if (*((ARENA_FREE **)((char *)(pArena + 1) +
867 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
869 ERR("Heap %p: arena %p has wrong back ptr %p\n",
870 subheap->heap, pArena,
871 *((ARENA_FREE **)((char *)(pArena+1) + (pArena->size & ARENA_SIZE_MASK)) - 1));
879 /***********************************************************************
880 * HEAP_ValidateInUseArena
882 static BOOL HEAP_ValidateInUseArena( const SUBHEAP *subheap, const ARENA_INUSE *pArena, BOOL quiet )
884 const char *heapEnd = (const char *)subheap + subheap->size;
886 /* Check for unaligned pointers */
887 if ( (ULONG_PTR)pArena % ALIGNMENT != 0 )
889 if ( quiet == NOISY )
891 ERR( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
892 if ( TRACE_ON(heap) )
893 HEAP_Dump( subheap->heap );
895 else if ( WARN_ON(heap) )
897 WARN( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
898 if ( TRACE_ON(heap) )
899 HEAP_Dump( subheap->heap );
904 /* Check magic number */
905 if (pArena->magic != ARENA_INUSE_MAGIC)
907 if (quiet == NOISY) {
908 ERR("Heap %p: invalid in-use arena magic for %p\n", subheap->heap, pArena );
910 HEAP_Dump( subheap->heap );
911 } else if (WARN_ON(heap)) {
912 WARN("Heap %p: invalid in-use arena magic for %p\n", subheap->heap, pArena );
914 HEAP_Dump( subheap->heap );
918 /* Check size flags */
919 if (pArena->size & ARENA_FLAG_FREE)
921 ERR("Heap %p: bad flags %08x for in-use arena %p\n",
922 subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
925 /* Check arena size */
926 if ((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
928 ERR("Heap %p: bad size %08x for in-use arena %p\n",
929 subheap->heap, pArena->size & ARENA_SIZE_MASK, pArena );
932 /* Check next arena PREV_FREE flag */
933 if (((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
934 (*(const DWORD *)((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
936 ERR("Heap %p: in-use arena %p next block has PREV_FREE flag\n",
937 subheap->heap, pArena );
940 /* Check prev free arena */
941 if (pArena->size & ARENA_FLAG_PREV_FREE)
943 const ARENA_FREE *pPrev = *((const ARENA_FREE * const*)pArena - 1);
944 /* Check prev pointer */
945 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
947 ERR("Heap %p: bad back ptr %p for arena %p\n",
948 subheap->heap, pPrev, pArena );
951 /* Check that prev arena is free */
952 if (!(pPrev->size & ARENA_FLAG_FREE) ||
953 (pPrev->magic != ARENA_FREE_MAGIC))
955 ERR("Heap %p: prev arena %p invalid for in-use %p\n",
956 subheap->heap, pPrev, pArena );
959 /* Check that prev arena is really the previous block */
960 if ((const char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (const char *)pArena)
962 ERR("Heap %p: prev arena %p is not prev for in-use %p\n",
963 subheap->heap, pPrev, pArena );
971 /***********************************************************************
972 * HEAP_IsRealArena [Internal]
973 * Validates a block is a valid arena.
979 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
980 DWORD flags, /* [in] Bit flags that control access during operation */
981 LPCVOID block, /* [in] Optional pointer to memory block to validate */
982 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
983 * does not complain */
988 flags &= HEAP_NO_SERIALIZE;
989 flags |= heapPtr->flags;
990 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
991 if (!(flags & HEAP_NO_SERIALIZE))
992 RtlEnterCriticalSection( &heapPtr->critSection );
996 /* Only check this single memory block */
998 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
999 ((const char *)block < (char *)subheap + subheap->headerSize
1000 + sizeof(ARENA_INUSE)))
1003 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
1004 else if (WARN_ON(heap))
1005 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
1008 ret = HEAP_ValidateInUseArena( subheap, (const ARENA_INUSE *)block - 1, quiet );
1010 if (!(flags & HEAP_NO_SERIALIZE))
1011 RtlLeaveCriticalSection( &heapPtr->critSection );
1015 subheap = &heapPtr->subheap;
1016 while (subheap && ret)
1018 char *ptr = (char *)subheap + subheap->headerSize;
1019 while (ptr < (char *)subheap + subheap->size)
1021 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1023 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
1027 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1031 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
1035 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1038 subheap = subheap->next;
1041 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1046 /***********************************************************************
1047 * RtlCreateHeap (NTDLL.@)
1049 * Create a new Heap.
1052 * flags [I] HEAP_ flags from "winnt.h"
1053 * addr [I] Desired base address
1054 * totalSize [I] Total size of the heap, or 0 for a growable heap
1055 * commitSize [I] Amount of heap space to commit
1056 * unknown [I] Not yet understood
1057 * definition [I] Heap definition
1060 * Success: A HANDLE to the newly created heap.
1061 * Failure: a NULL HANDLE.
1063 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, SIZE_T totalSize, SIZE_T commitSize,
1064 PVOID unknown, PRTL_HEAP_DEFINITION definition )
1068 /* Allocate the heap block */
1072 totalSize = HEAP_DEF_SIZE;
1073 flags |= HEAP_GROWABLE;
1076 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
1078 /* link it into the per-process heap list */
1081 HEAP *heapPtr = subheap->heap;
1082 RtlEnterCriticalSection( &processHeap->critSection );
1083 list_add_head( &processHeap->entry, &heapPtr->entry );
1084 RtlLeaveCriticalSection( &processHeap->critSection );
1088 processHeap = subheap->heap; /* assume the first heap we create is the process main heap */
1089 list_init( &processHeap->entry );
1092 return (HANDLE)subheap;
1096 /***********************************************************************
1097 * RtlDestroyHeap (NTDLL.@)
1099 * Destroy a Heap created with RtlCreateHeap().
1102 * heap [I] Heap to destroy.
1105 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1106 * Failure: The Heap handle, if heap is the process heap.
1108 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
1110 HEAP *heapPtr = HEAP_GetPtr( heap );
1113 TRACE("%p\n", heap );
1114 if (!heapPtr) return heap;
1116 if (heap == processHeap) return heap; /* cannot delete the main process heap */
1118 /* remove it from the per-process list */
1119 RtlEnterCriticalSection( &processHeap->critSection );
1120 list_remove( &heapPtr->entry );
1121 RtlLeaveCriticalSection( &processHeap->critSection );
1123 RtlDeleteCriticalSection( &heapPtr->critSection );
1124 subheap = &heapPtr->subheap;
1127 SUBHEAP *next = subheap->next;
1129 void *addr = subheap;
1130 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
1137 /***********************************************************************
1138 * RtlAllocateHeap (NTDLL.@)
1140 * Allocate a memory block from a Heap.
1143 * heap [I] Heap to allocate block from
1144 * flags [I] HEAP_ flags from "winnt.h"
1145 * size [I] Size of the memory block to allocate
1148 * Success: A pointer to the newly allocated block
1152 * This call does not SetLastError().
1154 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, SIZE_T size )
1157 ARENA_INUSE *pInUse;
1159 HEAP *heapPtr = HEAP_GetPtr( heap );
1160 SIZE_T rounded_size;
1162 /* Validate the parameters */
1164 if (!heapPtr) return NULL;
1165 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1166 flags |= heapPtr->flags;
1167 rounded_size = ROUND_SIZE(size);
1168 if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
1170 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1171 /* Locate a suitable free block */
1173 if (!(pArena = HEAP_FindFreeBlock( heapPtr, rounded_size, &subheap )))
1175 TRACE("(%p,%08x,%08lx): returning NULL\n",
1176 heap, flags, size );
1177 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1178 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1182 /* Remove the arena from the free list */
1184 list_remove( &pArena->entry );
1186 /* Build the in-use arena */
1188 pInUse = (ARENA_INUSE *)pArena;
1190 /* in-use arena is smaller than free arena,
1191 * so we have to add the difference to the size */
1192 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1193 pInUse->magic = ARENA_INUSE_MAGIC;
1195 /* Shrink the block */
1197 HEAP_ShrinkBlock( subheap, pInUse, rounded_size );
1198 pInUse->unused_bytes = (pInUse->size & ARENA_SIZE_MASK) - size;
1200 notify_alloc( pInUse + 1, size, flags & HEAP_ZERO_MEMORY );
1202 if (flags & HEAP_ZERO_MEMORY)
1203 clear_block( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1205 mark_block_uninitialized( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1207 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1209 TRACE("(%p,%08x,%08lx): returning %p\n", heap, flags, size, pInUse + 1 );
1210 return (LPVOID)(pInUse + 1);
1214 /***********************************************************************
1215 * RtlFreeHeap (NTDLL.@)
1217 * Free a memory block allocated with RtlAllocateHeap().
1220 * heap [I] Heap that block was allocated from
1221 * flags [I] HEAP_ flags from "winnt.h"
1222 * ptr [I] Block to free
1225 * Success: TRUE, if ptr is NULL or was freed successfully.
1228 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1230 ARENA_INUSE *pInUse;
1234 /* Validate the parameters */
1236 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1238 heapPtr = HEAP_GetPtr( heap );
1241 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1245 flags &= HEAP_NO_SERIALIZE;
1246 flags |= heapPtr->flags;
1247 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1249 /* Some sanity checks */
1251 pInUse = (ARENA_INUSE *)ptr - 1;
1252 if (!(subheap = HEAP_FindSubHeap( heapPtr, pInUse ))) goto error;
1253 if ((char *)pInUse < (char *)subheap + subheap->headerSize) goto error;
1254 if (!HEAP_ValidateInUseArena( subheap, pInUse, QUIET )) goto error;
1256 /* Turn the block into a free block */
1260 HEAP_MakeInUseBlockFree( subheap, pInUse );
1262 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1264 TRACE("(%p,%08x,%p): returning TRUE\n", heap, flags, ptr );
1268 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1269 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1270 TRACE("(%p,%08x,%p): returning FALSE\n", heap, flags, ptr );
1275 /***********************************************************************
1276 * RtlReAllocateHeap (NTDLL.@)
1278 * Change the size of a memory block allocated with RtlAllocateHeap().
1281 * heap [I] Heap that block was allocated from
1282 * flags [I] HEAP_ flags from "winnt.h"
1283 * ptr [I] Block to resize
1284 * size [I] Size of the memory block to allocate
1287 * Success: A pointer to the resized block (which may be different).
1290 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, SIZE_T size )
1292 ARENA_INUSE *pArena;
1295 SIZE_T oldSize, rounded_size;
1297 if (!ptr) return NULL;
1298 if (!(heapPtr = HEAP_GetPtr( heap )))
1300 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1304 /* Validate the parameters */
1306 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1307 HEAP_REALLOC_IN_PLACE_ONLY;
1308 flags |= heapPtr->flags;
1309 rounded_size = ROUND_SIZE(size);
1310 if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
1312 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1314 pArena = (ARENA_INUSE *)ptr - 1;
1315 if (!(subheap = HEAP_FindSubHeap( heapPtr, pArena ))) goto error;
1316 if ((char *)pArena < (char *)subheap + subheap->headerSize) goto error;
1317 if (!HEAP_ValidateInUseArena( subheap, pArena, QUIET )) goto error;
1319 /* Check if we need to grow the block */
1321 oldSize = (pArena->size & ARENA_SIZE_MASK);
1322 if (rounded_size > oldSize)
1324 char *pNext = (char *)(pArena + 1) + oldSize;
1325 if ((pNext < (char *)subheap + subheap->size) &&
1326 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1327 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= rounded_size))
1329 /* The next block is free and large enough */
1330 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1331 list_remove( &pFree->entry );
1332 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1333 if (!HEAP_Commit( subheap, pArena, rounded_size ))
1335 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1336 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1337 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY );
1340 notify_free( pArena + 1 );
1341 HEAP_ShrinkBlock( subheap, pArena, rounded_size );
1342 notify_alloc( pArena + 1, size, FALSE );
1343 /* FIXME: this is wrong as we may lose old VBits settings */
1344 mark_block_initialized( pArena + 1, oldSize );
1346 else /* Do it the hard way */
1349 ARENA_INUSE *pInUse;
1350 SUBHEAP *newsubheap;
1352 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1353 !(pNew = HEAP_FindFreeBlock( heapPtr, rounded_size, &newsubheap )))
1355 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1356 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1357 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY );
1361 /* Build the in-use arena */
1363 list_remove( &pNew->entry );
1364 pInUse = (ARENA_INUSE *)pNew;
1365 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1366 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1367 pInUse->magic = ARENA_INUSE_MAGIC;
1368 HEAP_ShrinkBlock( newsubheap, pInUse, rounded_size );
1369 mark_block_initialized( pInUse + 1, oldSize );
1370 notify_alloc( pInUse + 1, size, FALSE );
1371 memcpy( pInUse + 1, pArena + 1, oldSize );
1373 /* Free the previous block */
1375 notify_free( pArena + 1 );
1376 HEAP_MakeInUseBlockFree( subheap, pArena );
1377 subheap = newsubheap;
1383 /* Shrink the block */
1384 notify_free( pArena + 1 );
1385 HEAP_ShrinkBlock( subheap, pArena, rounded_size );
1386 notify_alloc( pArena + 1, size, FALSE );
1387 /* FIXME: this is wrong as we may lose old VBits settings */
1388 mark_block_initialized( pArena + 1, size );
1391 pArena->unused_bytes = (pArena->size & ARENA_SIZE_MASK) - size;
1393 /* Clear the extra bytes if needed */
1395 if (rounded_size > oldSize)
1397 if (flags & HEAP_ZERO_MEMORY)
1398 clear_block( (char *)(pArena + 1) + oldSize,
1399 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1401 mark_block_uninitialized( (char *)(pArena + 1) + oldSize,
1402 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1405 /* Return the new arena */
1407 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1409 TRACE("(%p,%08x,%p,%08lx): returning %p\n", heap, flags, ptr, size, pArena + 1 );
1410 return (LPVOID)(pArena + 1);
1413 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1414 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1415 TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
1420 /***********************************************************************
1421 * RtlCompactHeap (NTDLL.@)
1423 * Compact the free space in a Heap.
1426 * heap [I] Heap that block was allocated from
1427 * flags [I] HEAP_ flags from "winnt.h"
1430 * The number of bytes compacted.
1433 * This function is a harmless stub.
1435 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1442 /***********************************************************************
1443 * RtlLockHeap (NTDLL.@)
1448 * heap [I] Heap to lock
1451 * Success: TRUE. The Heap is locked.
1452 * Failure: FALSE, if heap is invalid.
1454 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1456 HEAP *heapPtr = HEAP_GetPtr( heap );
1457 if (!heapPtr) return FALSE;
1458 RtlEnterCriticalSection( &heapPtr->critSection );
1463 /***********************************************************************
1464 * RtlUnlockHeap (NTDLL.@)
1469 * heap [I] Heap to unlock
1472 * Success: TRUE. The Heap is unlocked.
1473 * Failure: FALSE, if heap is invalid.
1475 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1477 HEAP *heapPtr = HEAP_GetPtr( heap );
1478 if (!heapPtr) return FALSE;
1479 RtlLeaveCriticalSection( &heapPtr->critSection );
1484 /***********************************************************************
1485 * RtlSizeHeap (NTDLL.@)
1487 * Get the actual size of a memory block allocated from a Heap.
1490 * heap [I] Heap that block was allocated from
1491 * flags [I] HEAP_ flags from "winnt.h"
1492 * ptr [I] Block to get the size of
1495 * Success: The size of the block.
1496 * Failure: -1, heap or ptr are invalid.
1499 * The size may be bigger than what was passed to RtlAllocateHeap().
1501 SIZE_T WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1504 HEAP *heapPtr = HEAP_GetPtr( heap );
1508 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1511 flags &= HEAP_NO_SERIALIZE;
1512 flags |= heapPtr->flags;
1513 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1514 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1516 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1521 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1522 ret = (pArena->size & ARENA_SIZE_MASK) - pArena->unused_bytes;
1524 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1526 TRACE("(%p,%08x,%p): returning %08lx\n", heap, flags, ptr, ret );
1531 /***********************************************************************
1532 * RtlValidateHeap (NTDLL.@)
1534 * Determine if a block is a valid allocation from a heap.
1537 * heap [I] Heap that block was allocated from
1538 * flags [I] HEAP_ flags from "winnt.h"
1539 * ptr [I] Block to check
1542 * Success: TRUE. The block was allocated from heap.
1543 * Failure: FALSE, if heap is invalid or ptr was not allocated from it.
1545 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, LPCVOID ptr )
1547 HEAP *heapPtr = HEAP_GetPtr( heap );
1548 if (!heapPtr) return FALSE;
1549 return HEAP_IsRealArena( heapPtr, flags, ptr, QUIET );
1553 /***********************************************************************
1554 * RtlWalkHeap (NTDLL.@)
1557 * The PROCESS_HEAP_ENTRY flag values seem different between this
1558 * function and HeapWalk(). To be checked.
1560 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1562 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1563 HEAP *heapPtr = HEAP_GetPtr(heap);
1564 SUBHEAP *sub, *currentheap = NULL;
1567 int region_index = 0;
1569 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1571 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1573 /* set ptr to the next arena to be examined */
1575 if (!entry->lpData) /* first call (init) ? */
1577 TRACE("begin walking of heap %p.\n", heap);
1578 currentheap = &heapPtr->subheap;
1579 ptr = (char*)currentheap + currentheap->headerSize;
1583 ptr = entry->lpData;
1584 sub = &heapPtr->subheap;
1587 if (((char *)ptr >= (char *)sub) &&
1588 ((char *)ptr < (char *)sub + sub->size))
1596 if (currentheap == NULL)
1598 ERR("no matching subheap found, shouldn't happen !\n");
1599 ret = STATUS_NO_MORE_ENTRIES;
1603 ptr += entry->cbData; /* point to next arena */
1604 if (ptr > (char *)currentheap + currentheap->size - 1)
1605 { /* proceed with next subheap */
1606 if (!(currentheap = currentheap->next))
1607 { /* successfully finished */
1608 TRACE("end reached.\n");
1609 ret = STATUS_NO_MORE_ENTRIES;
1612 ptr = (char*)currentheap + currentheap->headerSize;
1617 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1619 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1621 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1623 entry->lpData = pArena + 1;
1624 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1625 entry->cbOverhead = sizeof(ARENA_FREE);
1626 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1630 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1632 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1634 entry->lpData = pArena + 1;
1635 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1636 entry->cbOverhead = sizeof(ARENA_INUSE);
1637 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1638 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1639 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1642 entry->iRegionIndex = region_index;
1644 /* first element of heap ? */
1645 if (ptr == (char *)(currentheap + currentheap->headerSize))
1647 entry->wFlags |= PROCESS_HEAP_REGION;
1648 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1649 entry->u.Region.dwUnCommittedSize =
1650 currentheap->size - currentheap->commitSize;
1651 entry->u.Region.lpFirstBlock = /* first valid block */
1652 currentheap + currentheap->headerSize;
1653 entry->u.Region.lpLastBlock = /* first invalid block */
1654 currentheap + currentheap->size;
1656 ret = STATUS_SUCCESS;
1657 if (TRACE_ON(heap)) HEAP_DumpEntry(entry);
1660 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1665 /***********************************************************************
1666 * RtlGetProcessHeaps (NTDLL.@)
1668 * Get the Heaps belonging to the current process.
1671 * count [I] size of heaps
1672 * heaps [O] Destination array for heap HANDLE's
1675 * Success: The number of Heaps allocated by the process.
1678 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1680 ULONG total = 1; /* main heap */
1683 RtlEnterCriticalSection( &processHeap->critSection );
1684 LIST_FOR_EACH( ptr, &processHeap->entry ) total++;
1687 *heaps++ = processHeap;
1688 LIST_FOR_EACH( ptr, &processHeap->entry )
1689 *heaps++ = LIST_ENTRY( ptr, HEAP, entry );
1691 RtlLeaveCriticalSection( &processHeap->critSection );