4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifdef HAVE_VALGRIND_MEMCHECK_H
30 #include <valgrind/memcheck.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
40 #include "wine/winbase16.h"
44 #include "wine/debug.h"
46 #include "wine/server_protocol.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(heap);
50 /* Note: the heap data structures are based on what Pietrek describes in his
51 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
52 * the same, but could be easily adapted if it turns out some programs
56 typedef struct tagARENA_INUSE
58 DWORD size; /* Block size; must be the first field */
59 DWORD magic; /* Magic number */
62 typedef struct tagARENA_FREE
64 DWORD size; /* Block size; must be the first field */
65 DWORD magic; /* Magic number */
66 struct tagARENA_FREE *next; /* Next free arena */
67 struct tagARENA_FREE *prev; /* Prev free arena */
70 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
71 #define ARENA_FLAG_PREV_FREE 0x00000002
72 #define ARENA_SIZE_MASK (~3)
73 #define ARENA_INUSE_MAGIC 0x44455355 /* Value for arena 'magic' field */
74 #define ARENA_FREE_MAGIC 0x45455246 /* Value for arena 'magic' field */
76 #define ARENA_INUSE_FILLER 0x55
77 #define ARENA_FREE_FILLER 0xaa
79 #define ALIGNMENT 8 /* everything is aligned on 8 byte boundaries */
80 #define ROUND_SIZE(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
82 #define QUIET 1 /* Suppress messages */
83 #define NOISY 0 /* Report all errors */
85 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
87 /* Max size of the blocks on the free lists */
88 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
90 0x20, 0x80, 0x200, ~0UL
101 typedef struct tagSUBHEAP
103 DWORD size; /* Size of the whole sub-heap */
104 DWORD commitSize; /* Committed size of the sub-heap */
105 DWORD headerSize; /* Size of the heap header */
106 struct tagSUBHEAP *next; /* Next sub-heap */
107 struct tagHEAP *heap; /* Main heap structure */
108 DWORD magic; /* Magic number */
111 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
113 typedef struct tagHEAP
115 SUBHEAP subheap; /* First sub-heap */
116 struct tagHEAP *next; /* Next heap for this process */
117 CRITICAL_SECTION critSection; /* Critical section for serialization */
118 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
119 DWORD flags; /* Heap flags */
120 DWORD magic; /* Magic number */
123 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
125 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
126 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
127 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
129 static HANDLE processHeap; /* main process heap */
131 static HEAP *firstHeap; /* head of secondary heaps list */
133 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
135 /* SetLastError for ntdll */
136 inline static void set_status( NTSTATUS status )
138 #if defined(__i386__) && defined(__GNUC__)
139 /* in this case SetLastError is an inline function so we can use it */
140 SetLastError( RtlNtStatusToDosError( status ) );
142 /* cannot use SetLastError, do it manually */
143 NtCurrentTeb()->last_error = RtlNtStatusToDosError( status );
147 /* set the process main heap */
148 static void set_process_heap( HANDLE heap )
150 NtCurrentTeb()->Peb->ProcessHeap = heap;
154 /* mark a block of memory as free for debugging purposes */
155 static inline void mark_block_free( void *ptr, size_t size )
157 if (TRACE_ON(heap)) memset( ptr, ARENA_FREE_FILLER, size );
158 #ifdef VALGRIND_MAKE_NOACCESS
159 VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
163 /* mark a block of memory as initialized for debugging purposes */
164 static inline void mark_block_initialized( void *ptr, size_t size )
166 #ifdef VALGRIND_MAKE_READABLE
167 VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ));
171 /* mark a block of memory as uninitialized for debugging purposes */
172 static inline void mark_block_uninitialized( void *ptr, size_t size )
174 #ifdef VALGRIND_MAKE_WRITABLE
175 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
179 memset( ptr, ARENA_INUSE_FILLER, size );
180 #ifdef VALGRIND_MAKE_WRITABLE
181 /* make it uninitialized to valgrind again */
182 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
187 /* clear contents of a block of memory */
188 static inline void clear_block( void *ptr, size_t size )
190 mark_block_initialized( ptr, size );
191 memset( ptr, 0, size );
194 /***********************************************************************
197 void HEAP_Dump( HEAP *heap )
203 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
204 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
205 (DWORD)heap->next, (DWORD)&heap->subheap );
206 subheap = &heap->subheap;
207 while (subheap->next)
209 DPRINTF( " -> %08lx", (DWORD)subheap->next );
210 subheap = subheap->next;
213 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
214 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
215 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
216 (DWORD)&heap->freeList[i].arena, heap->freeList[i].size,
217 (DWORD)heap->freeList[i].arena.prev,
218 (DWORD)heap->freeList[i].arena.next );
220 subheap = &heap->subheap;
223 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
224 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
225 (DWORD)subheap, subheap->size, subheap->commitSize );
227 DPRINTF( "\n Block Stat Size Id\n" );
228 ptr = (char*)subheap + subheap->headerSize;
229 while (ptr < (char *)subheap + subheap->size)
231 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
233 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
234 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
235 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
236 (DWORD)pArena->prev, (DWORD)pArena->next);
237 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
238 arenaSize += sizeof(ARENA_FREE);
239 freeSize += pArena->size & ARENA_SIZE_MASK;
241 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
243 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
244 DPRINTF( "%08lx Used %08lx back=%08lx\n",
245 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK, *((DWORD *)pArena - 1) );
246 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
247 arenaSize += sizeof(ARENA_INUSE);
248 usedSize += pArena->size & ARENA_SIZE_MASK;
252 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
253 DPRINTF( "%08lx used %08lx\n",
254 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK );
255 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
256 arenaSize += sizeof(ARENA_INUSE);
257 usedSize += pArena->size & ARENA_SIZE_MASK;
260 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
261 subheap->size, subheap->commitSize, freeSize, usedSize,
262 arenaSize, (arenaSize * 100) / subheap->size );
263 subheap = subheap->next;
268 /***********************************************************************
271 * Pointer to the heap
274 static HEAP *HEAP_GetPtr(
275 HANDLE heap /* [in] Handle to the heap */
277 HEAP *heapPtr = (HEAP *)heap;
278 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
280 ERR("Invalid heap %p!\n", heap );
283 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
285 HEAP_Dump( heapPtr );
293 /***********************************************************************
294 * HEAP_InsertFreeBlock
296 * Insert a free block into the free list.
298 static inline void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena, BOOL last )
300 FREE_LIST_ENTRY *pEntry = heap->freeList;
301 while (pEntry->size < pArena->size) pEntry++;
304 /* insert at end of free list, i.e. before the next free list entry */
306 if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS]) pEntry = heap->freeList;
307 pArena->prev = pEntry->arena.prev;
308 pArena->prev->next = pArena;
309 pArena->next = &pEntry->arena;
310 pEntry->arena.prev = pArena;
314 /* insert at head of free list */
315 pArena->next = pEntry->arena.next;
316 pArena->next->prev = pArena;
317 pArena->prev = &pEntry->arena;
318 pEntry->arena.next = pArena;
320 pArena->size |= ARENA_FLAG_FREE;
324 /***********************************************************************
326 * Find the sub-heap containing a given address.
332 static SUBHEAP *HEAP_FindSubHeap(
333 HEAP *heap, /* [in] Heap pointer */
334 LPCVOID ptr /* [in] Address */
336 SUBHEAP *sub = &heap->subheap;
339 if (((char *)ptr >= (char *)sub) &&
340 ((char *)ptr < (char *)sub + sub->size)) return sub;
347 /***********************************************************************
350 * Make sure the heap storage is committed up to (not including) ptr.
352 static inline BOOL HEAP_Commit( SUBHEAP *subheap, void *ptr )
354 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
355 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
356 if (size > subheap->size) size = subheap->size;
357 if (size <= subheap->commitSize) return TRUE;
358 size -= subheap->commitSize;
359 if (NtAllocateVirtualMemory( GetCurrentProcess(), &ptr, (char *)subheap + subheap->commitSize,
360 &size, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
362 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
363 size, (DWORD)((char *)subheap + subheap->commitSize),
364 (DWORD)subheap->heap );
367 subheap->commitSize += size;
372 /***********************************************************************
375 * If possible, decommit the heap storage from (including) 'ptr'.
377 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
382 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
383 /* round to next block and add one full block */
384 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
385 if (size >= subheap->commitSize) return TRUE;
386 decommit_size = subheap->commitSize - size;
387 addr = (char *)subheap + size;
389 if (NtFreeVirtualMemory( GetCurrentProcess(), &addr, &decommit_size, MEM_DECOMMIT ))
391 WARN("Could not decommit %08lx bytes at %08lx for heap %p\n",
392 decommit_size, (DWORD)((char *)subheap + size), subheap->heap );
395 subheap->commitSize -= decommit_size;
400 /***********************************************************************
401 * HEAP_CreateFreeBlock
403 * Create a free block at a specified address. 'size' is the size of the
404 * whole block, including the new arena.
406 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
412 /* Create a free arena */
413 mark_block_uninitialized( ptr, sizeof( ARENA_FREE ) );
414 pFree = (ARENA_FREE *)ptr;
415 pFree->magic = ARENA_FREE_MAGIC;
417 /* If debugging, erase the freed block content */
419 pEnd = (char *)ptr + size;
420 if (pEnd > (char *)subheap + subheap->commitSize) pEnd = (char *)subheap + subheap->commitSize;
421 if (pEnd > (char *)(pFree + 1)) mark_block_free( pFree + 1, pEnd - (char *)(pFree + 1) );
423 /* Check if next block is free also */
425 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
426 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
428 /* Remove the next arena from the free list */
429 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
430 pNext->next->prev = pNext->prev;
431 pNext->prev->next = pNext->next;
432 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
433 mark_block_free( pNext, sizeof(ARENA_FREE) );
436 /* Set the next block PREV_FREE flag and pointer */
438 last = ((char *)ptr + size >= (char *)subheap + subheap->size);
441 DWORD *pNext = (DWORD *)((char *)ptr + size);
442 *pNext |= ARENA_FLAG_PREV_FREE;
443 mark_block_initialized( pNext - 1, sizeof( ARENA_FREE * ) );
444 *(ARENA_FREE **)(pNext - 1) = pFree;
447 /* Last, insert the new block into the free list */
449 pFree->size = size - sizeof(*pFree);
450 HEAP_InsertFreeBlock( subheap->heap, pFree, last );
454 /***********************************************************************
455 * HEAP_MakeInUseBlockFree
457 * Turn an in-use block into a free block. Can also decommit the end of
458 * the heap, and possibly even free the sub-heap altogether.
460 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
463 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
465 /* Check if we can merge with previous block */
467 if (pArena->size & ARENA_FLAG_PREV_FREE)
469 pFree = *((ARENA_FREE **)pArena - 1);
470 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
471 /* Remove it from the free list */
472 pFree->next->prev = pFree->prev;
473 pFree->prev->next = pFree->next;
475 else pFree = (ARENA_FREE *)pArena;
477 /* Create a free block */
479 HEAP_CreateFreeBlock( subheap, pFree, size );
480 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
481 if ((char *)pFree + size < (char *)subheap + subheap->size)
482 return; /* Not the last block, so nothing more to do */
484 /* Free the whole sub-heap if it's empty and not the original one */
486 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
487 (subheap != &subheap->heap->subheap))
490 SUBHEAP *pPrev = &subheap->heap->subheap;
491 /* Remove the free block from the list */
492 pFree->next->prev = pFree->prev;
493 pFree->prev->next = pFree->next;
494 /* Remove the subheap from the list */
495 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
496 if (pPrev) pPrev->next = subheap->next;
497 /* Free the memory */
499 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&subheap, &size, MEM_RELEASE );
503 /* Decommit the end of the heap */
505 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
509 /***********************************************************************
512 * Shrink an in-use block.
514 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
516 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
518 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
519 (pArena->size & ARENA_SIZE_MASK) - size );
520 /* assign size plus previous arena flags */
521 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
525 /* Turn off PREV_FREE flag in next block */
526 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
527 if (pNext < (char *)subheap + subheap->size)
528 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
532 /***********************************************************************
535 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
536 DWORD commitSize, DWORD totalSize )
539 FREE_LIST_ENTRY *pEntry;
544 if (flags & HEAP_SHARED)
545 commitSize = totalSize; /* always commit everything in a shared heap */
546 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, address,
547 &commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
549 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
553 /* Fill the sub-heap structure */
555 subheap = (SUBHEAP *)address;
556 subheap->heap = heap;
557 subheap->size = totalSize;
558 subheap->commitSize = commitSize;
559 subheap->magic = SUBHEAP_MAGIC;
561 if ( subheap != (SUBHEAP *)heap )
563 /* If this is a secondary subheap, insert it into list */
565 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
566 subheap->next = heap->subheap.next;
567 heap->subheap.next = subheap;
571 /* If this is a primary subheap, initialize main heap */
573 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
574 subheap->next = NULL;
577 heap->magic = HEAP_MAGIC;
579 /* Build the free lists */
581 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
583 pEntry->size = HEAP_freeListSizes[i];
584 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
585 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
586 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
587 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
588 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
589 pEntry->arena.magic = ARENA_FREE_MAGIC;
592 /* Initialize critical section */
594 RtlInitializeCriticalSection( &heap->critSection );
595 if (flags & HEAP_SHARED)
597 /* let's assume that only one thread at a time will try to do this */
598 HANDLE sem = heap->critSection.LockSemaphore;
599 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
601 NtDuplicateObject( GetCurrentProcess(), sem, GetCurrentProcess(), &sem, 0, 0,
602 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
603 heap->critSection.LockSemaphore = sem;
607 /* Create the first free block */
609 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
610 subheap->size - subheap->headerSize );
615 /***********************************************************************
618 * Create a sub-heap of the given size.
619 * If heap == NULL, creates a main heap.
621 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
622 DWORD commitSize, DWORD totalSize )
624 LPVOID address = base;
626 /* round-up sizes on a 64K boundary */
627 totalSize = (totalSize + 0xffff) & 0xffff0000;
628 commitSize = (commitSize + 0xffff) & 0xffff0000;
629 if (!commitSize) commitSize = 0x10000;
630 if (totalSize < commitSize) totalSize = commitSize;
634 /* allocate the memory block */
635 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, NULL, &totalSize,
636 MEM_RESERVE, PAGE_EXECUTE_READWRITE ))
638 WARN("Could not allocate %08lx bytes\n", totalSize );
643 /* Initialize subheap */
645 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
646 address, flags, commitSize, totalSize ))
649 if (!base) NtFreeVirtualMemory( GetCurrentProcess(), &address, &size, MEM_RELEASE );
653 return (SUBHEAP *)address;
657 /***********************************************************************
660 * Find a free block at least as large as the requested size, and make sure
661 * the requested size is committed.
663 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
664 SUBHEAP **ppSubHeap )
668 FREE_LIST_ENTRY *pEntry = heap->freeList;
670 /* Find a suitable free list, and in it find a block large enough */
672 while (pEntry->size < size) pEntry++;
673 pArena = pEntry->arena.next;
674 while (pArena != &heap->freeList[0].arena)
676 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
677 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
678 if (arena_size >= size)
680 subheap = HEAP_FindSubHeap( heap, pArena );
681 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
682 + size + HEAP_MIN_BLOCK_SIZE))
684 *ppSubHeap = subheap;
687 pArena = pArena->next;
690 /* If no block was found, attempt to grow the heap */
692 if (!(heap->flags & HEAP_GROWABLE))
694 WARN("Not enough space in heap %08lx for %08lx bytes\n",
698 /* make sure that we have a big enough size *committed* to fit another
699 * last free arena in !
700 * So just one heap struct, one first free arena which will eventually
701 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
702 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
703 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
704 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
705 max( HEAP_DEF_SIZE, size ) )))
708 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
709 (DWORD)subheap, size, (DWORD)heap );
711 *ppSubHeap = subheap;
712 return (ARENA_FREE *)(subheap + 1);
716 /***********************************************************************
717 * HEAP_IsValidArenaPtr
719 * Check that the pointer is inside the range possible for arenas.
721 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
724 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
725 if (!subheap) return FALSE;
726 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
727 if (subheap != &heap->subheap) return FALSE;
728 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
729 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
734 /***********************************************************************
735 * HEAP_ValidateFreeArena
737 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
739 char *heapEnd = (char *)subheap + subheap->size;
741 /* Check for unaligned pointers */
742 if ( (long)pArena % ALIGNMENT != 0 )
744 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
745 (DWORD)subheap->heap, (DWORD)pArena );
749 /* Check magic number */
750 if (pArena->magic != ARENA_FREE_MAGIC)
752 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
753 (DWORD)subheap->heap, (DWORD)pArena );
756 /* Check size flags */
757 if (!(pArena->size & ARENA_FLAG_FREE) ||
758 (pArena->size & ARENA_FLAG_PREV_FREE))
760 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
761 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
763 /* Check arena size */
764 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
766 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
767 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
770 /* Check that next pointer is valid */
771 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
773 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
774 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
777 /* Check that next arena is free */
778 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
779 (pArena->next->magic != ARENA_FREE_MAGIC))
781 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
782 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
785 /* Check that prev pointer is valid */
786 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
788 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
789 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
792 /* Check that prev arena is free */
793 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
794 (pArena->prev->magic != ARENA_FREE_MAGIC))
796 /* this often means that the prev arena got overwritten
797 * by a memory write before that prev arena */
798 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
799 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
802 /* Check that next block has PREV_FREE flag */
803 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
805 if (!(*(DWORD *)((char *)(pArena + 1) +
806 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
808 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
809 (DWORD)subheap->heap, (DWORD)pArena );
812 /* Check next block back pointer */
813 if (*((ARENA_FREE **)((char *)(pArena + 1) +
814 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
816 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
817 (DWORD)subheap->heap, (DWORD)pArena,
818 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
826 /***********************************************************************
827 * HEAP_ValidateInUseArena
829 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
831 char *heapEnd = (char *)subheap + subheap->size;
833 /* Check for unaligned pointers */
834 if ( (long)pArena % ALIGNMENT != 0 )
836 if ( quiet == NOISY )
838 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
839 (DWORD)subheap->heap, (DWORD)pArena );
840 if ( TRACE_ON(heap) )
841 HEAP_Dump( subheap->heap );
843 else if ( WARN_ON(heap) )
845 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
846 (DWORD)subheap->heap, (DWORD)pArena );
847 if ( TRACE_ON(heap) )
848 HEAP_Dump( subheap->heap );
853 /* Check magic number */
854 if (pArena->magic != ARENA_INUSE_MAGIC)
856 if (quiet == NOISY) {
857 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
858 (DWORD)subheap->heap, (DWORD)pArena );
860 HEAP_Dump( subheap->heap );
861 } else if (WARN_ON(heap)) {
862 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
863 (DWORD)subheap->heap, (DWORD)pArena );
865 HEAP_Dump( subheap->heap );
869 /* Check size flags */
870 if (pArena->size & ARENA_FLAG_FREE)
872 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
873 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
876 /* Check arena size */
877 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
879 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
880 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
883 /* Check next arena PREV_FREE flag */
884 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
885 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
887 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
888 (DWORD)subheap->heap, (DWORD)pArena );
891 /* Check prev free arena */
892 if (pArena->size & ARENA_FLAG_PREV_FREE)
894 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
895 /* Check prev pointer */
896 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
898 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
899 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
902 /* Check that prev arena is free */
903 if (!(pPrev->size & ARENA_FLAG_FREE) ||
904 (pPrev->magic != ARENA_FREE_MAGIC))
906 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
907 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
910 /* Check that prev arena is really the previous block */
911 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
913 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
914 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
922 /***********************************************************************
923 * HEAP_IsRealArena [Internal]
924 * Validates a block is a valid arena.
930 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
931 DWORD flags, /* [in] Bit flags that control access during operation */
932 LPCVOID block, /* [in] Optional pointer to memory block to validate */
933 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
934 * does not complain */
939 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
941 ERR("Invalid heap %p!\n", heapPtr );
945 flags &= HEAP_NO_SERIALIZE;
946 flags |= heapPtr->flags;
947 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
948 if (!(flags & HEAP_NO_SERIALIZE))
949 RtlEnterCriticalSection( &heapPtr->critSection );
953 /* Only check this single memory block */
955 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
956 ((char *)block < (char *)subheap + subheap->headerSize
957 + sizeof(ARENA_INUSE)))
960 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
961 else if (WARN_ON(heap))
962 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
965 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
967 if (!(flags & HEAP_NO_SERIALIZE))
968 RtlLeaveCriticalSection( &heapPtr->critSection );
972 subheap = &heapPtr->subheap;
973 while (subheap && ret)
975 char *ptr = (char *)subheap + subheap->headerSize;
976 while (ptr < (char *)subheap + subheap->size)
978 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
980 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
984 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
988 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
992 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
995 subheap = subheap->next;
998 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1003 /***********************************************************************
1004 * RtlCreateHeap (NTDLL.@)
1006 * Create a new Heap.
1009 * flags [I] HEAP_ flags from "winnt.h"
1010 * addr [I] Desired base address
1011 * totalSize [I] Total size of the heap, or 0 for a growable heap
1012 * commitSize [I] Amount of heap space to commit
1013 * unknown [I] Not yet understood
1014 * definition [I] Heap definition
1017 * Success: A HANDLE to the newly created heap.
1018 * Failure: a NULL HANDLE.
1020 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
1021 PVOID unknown, PRTL_HEAP_DEFINITION definition )
1025 /* Allocate the heap block */
1029 totalSize = HEAP_DEF_SIZE;
1030 flags |= HEAP_GROWABLE;
1033 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
1035 /* link it into the per-process heap list */
1038 HEAP *heapPtr = subheap->heap;
1039 RtlLockHeap( processHeap );
1040 heapPtr->next = firstHeap;
1041 firstHeap = heapPtr;
1042 RtlUnlockHeap( processHeap );
1044 else /* assume the first heap we create is the process main heap */
1046 set_process_heap( (HANDLE)subheap->heap );
1048 return (HANDLE)subheap;
1052 /***********************************************************************
1053 * RtlDestroyHeap (NTDLL.@)
1055 * Destroy a Heap created with RtlCreateHeap().
1058 * heap [I] Heap to destroy.
1061 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1062 * Failure: The Heap handle, if heap is the process heap.
1064 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
1066 HEAP *heapPtr = HEAP_GetPtr( heap );
1069 TRACE("%p\n", heap );
1070 if (!heapPtr) return heap;
1072 if (heap == processHeap) return heap; /* cannot delete the main process heap */
1073 else /* remove it from the per-process list */
1076 RtlLockHeap( processHeap );
1078 while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
1079 if (*pptr) *pptr = (*pptr)->next;
1080 RtlUnlockHeap( processHeap );
1083 RtlDeleteCriticalSection( &heapPtr->critSection );
1084 subheap = &heapPtr->subheap;
1087 SUBHEAP *next = subheap->next;
1089 void *addr = subheap;
1090 NtFreeVirtualMemory( GetCurrentProcess(), &addr, &size, MEM_RELEASE );
1097 /***********************************************************************
1098 * RtlAllocateHeap (NTDLL.@)
1100 * Allocate a memory block from a Heap.
1103 * heap [I] Heap to allocate block from
1104 * flags [I] HEAP_ flags from "winnt.h"
1105 * size [I] Size of the memory block to allocate
1108 * Success: A pointer to the newly allocated block
1112 * This call does not SetLastError().
1114 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
1117 ARENA_INUSE *pInUse;
1119 HEAP *heapPtr = HEAP_GetPtr( heap );
1121 /* Validate the parameters */
1123 if (!heapPtr) return NULL;
1124 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1125 flags |= heapPtr->flags;
1126 size = ROUND_SIZE(size);
1127 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1129 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1130 /* Locate a suitable free block */
1132 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1134 TRACE("(%p,%08lx,%08lx): returning NULL\n",
1135 heap, flags, size );
1136 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1137 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1141 /* Remove the arena from the free list */
1143 pArena->next->prev = pArena->prev;
1144 pArena->prev->next = pArena->next;
1146 /* Build the in-use arena */
1148 pInUse = (ARENA_INUSE *)pArena;
1150 /* in-use arena is smaller than free arena,
1151 * so we have to add the difference to the size */
1152 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1153 pInUse->magic = ARENA_INUSE_MAGIC;
1155 /* Shrink the block */
1157 HEAP_ShrinkBlock( subheap, pInUse, size );
1159 if (flags & HEAP_ZERO_MEMORY)
1160 clear_block( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1162 mark_block_uninitialized( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1164 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1166 TRACE("(%p,%08lx,%08lx): returning %08lx\n",
1167 heap, flags, size, (DWORD)(pInUse + 1) );
1168 return (LPVOID)(pInUse + 1);
1172 /***********************************************************************
1173 * RtlFreeHeap (NTDLL.@)
1175 * Free a memory block allocated with RtlAllocateHeap().
1178 * heap [I] Heap that block was allocated from
1179 * flags [I] HEAP_ flags from "winnt.h"
1180 * ptr [I] Block to free
1183 * Success: TRUE, if ptr is NULL or was freed successfully.
1186 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1188 ARENA_INUSE *pInUse;
1190 HEAP *heapPtr = HEAP_GetPtr( heap );
1192 /* Validate the parameters */
1194 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1197 set_status( STATUS_INVALID_HANDLE );
1201 flags &= HEAP_NO_SERIALIZE;
1202 flags |= heapPtr->flags;
1203 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1204 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1206 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1207 set_status( STATUS_INVALID_PARAMETER );
1208 TRACE("(%p,%08lx,%08lx): returning FALSE\n",
1209 heap, flags, (DWORD)ptr );
1213 /* Turn the block into a free block */
1215 pInUse = (ARENA_INUSE *)ptr - 1;
1216 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1217 HEAP_MakeInUseBlockFree( subheap, pInUse );
1219 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1221 TRACE("(%p,%08lx,%08lx): returning TRUE\n",
1222 heap, flags, (DWORD)ptr );
1227 /***********************************************************************
1228 * RtlReAllocateHeap (NTDLL.@)
1230 * Change the size of a memory block allocated with RtlAllocateHeap().
1233 * heap [I] Heap that block was allocated from
1234 * flags [I] HEAP_ flags from "winnt.h"
1235 * ptr [I] Block to resize
1236 * size [I] Size of the memory block to allocate
1239 * Success: A pointer to the resized block (which may be different).
1242 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1244 ARENA_INUSE *pArena;
1249 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1250 if (!(heapPtr = HEAP_GetPtr( heap )))
1252 set_status( STATUS_INVALID_HANDLE );
1256 /* Validate the parameters */
1258 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1259 HEAP_REALLOC_IN_PLACE_ONLY;
1260 flags |= heapPtr->flags;
1261 size = ROUND_SIZE(size);
1262 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1264 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1265 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1267 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1268 set_status( STATUS_INVALID_PARAMETER );
1269 TRACE("(%p,%08lx,%08lx,%08lx): returning NULL\n",
1270 heap, flags, (DWORD)ptr, size );
1274 /* Check if we need to grow the block */
1276 pArena = (ARENA_INUSE *)ptr - 1;
1277 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1278 oldSize = (pArena->size & ARENA_SIZE_MASK);
1281 char *pNext = (char *)(pArena + 1) + oldSize;
1282 if ((pNext < (char *)subheap + subheap->size) &&
1283 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1284 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1286 /* The next block is free and large enough */
1287 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1288 pFree->next->prev = pFree->prev;
1289 pFree->prev->next = pFree->next;
1290 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1291 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1292 + size + HEAP_MIN_BLOCK_SIZE))
1294 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1295 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1296 set_status( STATUS_NO_MEMORY );
1299 HEAP_ShrinkBlock( subheap, pArena, size );
1301 else /* Do it the hard way */
1304 ARENA_INUSE *pInUse;
1305 SUBHEAP *newsubheap;
1307 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1308 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1310 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1311 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1312 set_status( STATUS_NO_MEMORY );
1316 /* Build the in-use arena */
1318 pNew->next->prev = pNew->prev;
1319 pNew->prev->next = pNew->next;
1320 pInUse = (ARENA_INUSE *)pNew;
1321 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1322 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1323 pInUse->magic = ARENA_INUSE_MAGIC;
1324 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1325 mark_block_initialized( pInUse + 1, oldSize );
1326 memcpy( pInUse + 1, pArena + 1, oldSize );
1328 /* Free the previous block */
1330 HEAP_MakeInUseBlockFree( subheap, pArena );
1331 subheap = newsubheap;
1335 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1337 /* Clear the extra bytes if needed */
1341 if (flags & HEAP_ZERO_MEMORY)
1342 clear_block( (char *)(pArena + 1) + oldSize,
1343 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1345 mark_block_uninitialized( (char *)(pArena + 1) + oldSize,
1346 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1349 /* Return the new arena */
1351 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1353 TRACE("(%p,%08lx,%08lx,%08lx): returning %08lx\n",
1354 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1355 return (LPVOID)(pArena + 1);
1359 /***********************************************************************
1360 * RtlCompactHeap (NTDLL.@)
1362 * Compact the free space in a Heap.
1365 * heap [I] Heap that block was allocated from
1366 * flags [I] HEAP_ flags from "winnt.h"
1369 * The number of bytes compacted.
1372 * This function is a harmless stub.
1374 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1381 /***********************************************************************
1382 * RtlLockHeap (NTDLL.@)
1387 * heap [I] Heap to lock
1390 * Success: TRUE. The Heap is locked.
1391 * Failure: FALSE, if heap is invalid.
1393 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1395 HEAP *heapPtr = HEAP_GetPtr( heap );
1396 if (!heapPtr) return FALSE;
1397 RtlEnterCriticalSection( &heapPtr->critSection );
1402 /***********************************************************************
1403 * RtlUnlockHeap (NTDLL.@)
1408 * heap [I] Heap to unlock
1411 * Success: TRUE. The Heap is unlocked.
1412 * Failure: FALSE, if heap is invalid.
1414 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1416 HEAP *heapPtr = HEAP_GetPtr( heap );
1417 if (!heapPtr) return FALSE;
1418 RtlLeaveCriticalSection( &heapPtr->critSection );
1423 /***********************************************************************
1424 * RtlSizeHeap (NTDLL.@)
1426 * Get the actual size of a memory block allocated from a Heap.
1429 * heap [I] Heap that block was allocated from
1430 * flags [I] HEAP_ flags from "winnt.h"
1431 * ptr [I] Block to get the size of
1434 * Success: The size of the block.
1435 * Failure: -1, heap or ptr are invalid.
1438 * The size may be bigger than what was passed to RtlAllocateHeap().
1440 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1443 HEAP *heapPtr = HEAP_GetPtr( heap );
1447 set_status( STATUS_INVALID_HANDLE );
1450 flags &= HEAP_NO_SERIALIZE;
1451 flags |= heapPtr->flags;
1452 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1453 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1455 set_status( STATUS_INVALID_PARAMETER );
1460 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1461 ret = pArena->size & ARENA_SIZE_MASK;
1463 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1465 TRACE("(%p,%08lx,%08lx): returning %08lx\n",
1466 heap, flags, (DWORD)ptr, ret );
1471 /***********************************************************************
1472 * RtlValidateHeap (NTDLL.@)
1474 * Determine if a block is a valid alloction from a heap.
1477 * heap [I] Heap that block was allocated from
1478 * flags [I] HEAP_ flags from "winnt.h"
1479 * ptr [I] Block to check
1482 * Success: TRUE. The block was allocated from heap.
1483 * Failure: FALSE, if heap is invalid or ptr was not allocated from it.
1485 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, LPCVOID ptr )
1487 HEAP *heapPtr = HEAP_GetPtr( heap );
1488 if (!heapPtr) return FALSE;
1489 return HEAP_IsRealArena( heapPtr, flags, ptr, QUIET );
1493 /***********************************************************************
1494 * RtlWalkHeap (NTDLL.@)
1497 * The PROCESS_HEAP_ENTRY flag values seem different between this
1498 * function and HeapWalk(). To be checked.
1500 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1502 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1503 HEAP *heapPtr = HEAP_GetPtr(heap);
1504 SUBHEAP *sub, *currentheap = NULL;
1507 int region_index = 0;
1509 FIXME( "not fully compatible\n" );
1511 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1513 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1515 /* set ptr to the next arena to be examined */
1517 if (!entry->lpData) /* first call (init) ? */
1519 TRACE("begin walking of heap %p.\n", heap);
1520 currentheap = &heapPtr->subheap;
1521 ptr = (char*)currentheap + currentheap->headerSize;
1525 ptr = entry->lpData;
1526 sub = &heapPtr->subheap;
1529 if (((char *)ptr >= (char *)sub) &&
1530 ((char *)ptr < (char *)sub + sub->size))
1538 if (currentheap == NULL)
1540 ERR("no matching subheap found, shouldn't happen !\n");
1541 ret = STATUS_NO_MORE_ENTRIES;
1545 ptr += entry->cbData; /* point to next arena */
1546 if (ptr > (char *)currentheap + currentheap->size - 1)
1547 { /* proceed with next subheap */
1548 if (!(currentheap = currentheap->next))
1549 { /* successfully finished */
1550 TRACE("end reached.\n");
1551 ret = STATUS_NO_MORE_ENTRIES;
1554 ptr = (char*)currentheap + currentheap->headerSize;
1559 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1561 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1563 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1565 entry->lpData = pArena + 1;
1566 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1567 entry->cbOverhead = sizeof(ARENA_FREE);
1568 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1572 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1574 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1576 entry->lpData = pArena + 1;
1577 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1578 entry->cbOverhead = sizeof(ARENA_INUSE);
1579 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1580 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1581 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1584 entry->iRegionIndex = region_index;
1586 /* first element of heap ? */
1587 if (ptr == (char *)(currentheap + currentheap->headerSize))
1589 entry->wFlags |= PROCESS_HEAP_REGION;
1590 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1591 entry->u.Region.dwUnCommittedSize =
1592 currentheap->size - currentheap->commitSize;
1593 entry->u.Region.lpFirstBlock = /* first valid block */
1594 currentheap + currentheap->headerSize;
1595 entry->u.Region.lpLastBlock = /* first invalid block */
1596 currentheap + currentheap->size;
1598 ret = STATUS_SUCCESS;
1601 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1606 /***********************************************************************
1607 * RtlGetProcessHeaps (NTDLL.@)
1609 * Get the Heaps belonging to the current process.
1612 * count [I] size of heaps
1613 * heaps [O] Destination array for heap HANDLE's
1616 * Success: The number of Heaps allocated by the process.
1619 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1624 if (!processHeap) return 0; /* should never happen */
1625 total = 1; /* main heap */
1626 RtlLockHeap( processHeap );
1627 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1630 *heaps++ = (HANDLE)processHeap;
1631 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1633 RtlUnlockHeap( processHeap );