4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 /* Note: the heap data structures are based on what Pietrek describes in his
10 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
11 * the same, but could be easily adapted if it turns out some programs
16 #include <ddk/ntddk.h>
17 #include <ntdll/rtl.h>
18 #include <ntos/heap.h>
19 #include <ntos/minmax.h>
22 #include <ntdll/ntdll.h>
24 #define DPRINTF DPRINT
26 #define SetLastError(x)
29 #define WARN_ON(x) (1)
33 #define TRACE_ON(x) (0)
36 #define TRACE_ON(x) (1)
41 static CRITICAL_SECTION RtlpProcessHeapsListLock;
44 typedef struct tagARENA_INUSE
46 DWORD size; /* Block size; must be the first field */
47 WORD threadId; /* Allocating thread id */
48 WORD magic; /* Magic number */
49 void *callerEIP; /* EIP of caller upon allocation */
52 typedef struct tagARENA_FREE
54 DWORD size; /* Block size; must be the first field */
55 WORD threadId; /* Freeing thread id */
56 WORD magic; /* Magic number */
57 struct tagARENA_FREE *next; /* Next free arena */
58 struct tagARENA_FREE *prev; /* Prev free arena */
61 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
62 #define ARENA_FLAG_PREV_FREE 0x00000002
63 #define ARENA_SIZE_MASK 0xfffffffc
64 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
65 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
67 #define ARENA_INUSE_FILLER 0x55
68 #define ARENA_FREE_FILLER 0xaa
70 #define QUIET 1 /* Suppress messages */
71 #define NOISY 0 /* Report all errors */
73 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
75 /* Max size of the blocks on the free lists */
76 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
78 0x20, 0x80, 0x200, 0xffffffff
89 typedef struct tagSUBHEAP
91 DWORD size; /* Size of the whole sub-heap */
92 DWORD commitSize; /* Committed size of the sub-heap */
93 DWORD headerSize; /* Size of the heap header */
94 struct tagSUBHEAP *next; /* Next sub-heap */
95 struct tagHEAP *heap; /* Main heap structure */
96 DWORD magic; /* Magic number */
97 WORD selector; /* Selector for HEAP_WINE_SEGPTR heaps */
100 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
102 typedef struct tagHEAP
104 SUBHEAP subheap; /* First sub-heap */
105 struct tagHEAP *next; /* Next heap for this process */
106 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
107 CRITICAL_SECTION critSection; /* Critical section for serialization */
108 DWORD flags; /* Heap flags */
109 DWORD magic; /* Magic number */
110 void *private; /* Private pointer for the user of the heap */
113 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
115 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
116 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
117 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
120 static BOOL HEAP_IsRealArena( HANDLE heap, DWORD flags, LPCVOID block, BOOL quiet );
123 #define GET_EIP() (__builtin_return_address(0))
124 #define SET_EIP(ptr) ((ARENA_INUSE*)(ptr) - 1)->callerEIP = GET_EIP()
127 #define SET_EIP(ptr) /* nothing */
128 #endif /* __GNUC__ */
131 /***********************************************************************
135 HEAP_Dump(PHEAP heap)
141 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
142 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
143 (DWORD)heap->next, (DWORD)&heap->subheap );
144 subheap = &heap->subheap;
145 while (subheap->next)
147 DPRINTF( " -> %08lx", (DWORD)subheap->next );
148 subheap = subheap->next;
151 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
152 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
153 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
154 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
155 heap->freeList[i].arena.threadId,
156 (DWORD)heap->freeList[i].arena.prev,
157 (DWORD)heap->freeList[i].arena.next );
159 subheap = &heap->subheap;
162 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
163 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
164 (DWORD)subheap, subheap->size, subheap->commitSize );
166 DPRINTF( "\n Block Stat Size Id\n" );
167 ptr = (char*)subheap + subheap->headerSize;
168 while (ptr < (char *)subheap + subheap->size)
170 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
172 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
173 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
174 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
175 pArena->threadId, (DWORD)pArena->prev,
176 (DWORD)pArena->next);
177 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
178 arenaSize += sizeof(ARENA_FREE);
179 freeSize += pArena->size & ARENA_SIZE_MASK;
181 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
183 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
184 DPRINTF( "%08lx Used %08lx %04x back=%08lx EIP=%p\n",
185 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
186 pArena->threadId, *((DWORD *)pArena - 1),
188 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
189 arenaSize += sizeof(ARENA_INUSE);
190 usedSize += pArena->size & ARENA_SIZE_MASK;
194 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
195 DPRINTF( "%08lx used %08lx %04x EIP=%p\n",
196 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
197 pArena->threadId, pArena->callerEIP );
198 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
199 arenaSize += sizeof(ARENA_INUSE);
200 usedSize += pArena->size & ARENA_SIZE_MASK;
203 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
204 subheap->size, subheap->commitSize, freeSize, usedSize,
205 arenaSize, (arenaSize * 100) / subheap->size );
206 subheap = subheap->next;
211 /***********************************************************************
214 * Pointer to the heap
218 HEAP_GetPtr(HANDLE heap) /* [in] Handle to the heap */
220 HEAP *heapPtr = (HEAP *)heap;
221 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
223 ERR("Invalid heap %08x!\n", heap );
226 if (TRACE_ON(heap) && !HEAP_IsRealArena( heap, 0, NULL, NOISY ))
228 HEAP_Dump( heapPtr );
236 /***********************************************************************
237 * HEAP_InsertFreeBlock
239 * Insert a free block into the free list.
242 HEAP_InsertFreeBlock(PHEAP heap,
246 FREE_LIST_ENTRY *pEntry = heap->freeList;
247 while (pEntry->size < pArena->size) pEntry++;
250 /* insert at end of free list, i.e. before next free list entry */
252 if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS])
254 pEntry = heap->freeList;
256 pArena->prev = pEntry->arena.prev;
257 pArena->prev->next = pArena;
258 pArena->next = &pEntry->arena;
259 pEntry->arena.prev = pArena;
263 /* insert at head of free list */
264 pArena->next = pEntry->arena.next;
265 pArena->next->prev = pArena;
266 pArena->prev = &pEntry->arena;
267 pEntry->arena.next = pArena;
269 pArena->size |= ARENA_FLAG_FREE;
273 /***********************************************************************
275 * Find the sub-heap containing a given address.
282 HEAP_FindSubHeap(HEAP *heap, /* [in] Heap pointer */
283 LPCVOID ptr) /* [in] Address */
285 SUBHEAP *sub = &heap->subheap;
288 if (((char *)ptr >= (char *)sub) &&
289 ((char *)ptr < (char *)sub + sub->size)) return sub;
296 /***********************************************************************
299 * Make sure the heap storage is committed up to (not including) ptr.
302 HEAP_Commit(SUBHEAP *subheap,
306 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
311 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
312 if (size > subheap->size) size = subheap->size;
313 if (size <= subheap->commitSize) return TRUE;
315 address = (PVOID)((char *)subheap + subheap->commitSize);
316 commitsize = size - subheap->commitSize;
318 if (!(flags & HEAP_NO_VALLOC))
320 Status = NtAllocateVirtualMemory(NtCurrentProcess(),
325 PAGE_EXECUTE_READWRITE);
326 if (!NT_SUCCESS(Status))
328 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
329 size - subheap->commitSize,
330 (DWORD)((char *)subheap + subheap->commitSize),
331 (DWORD)subheap->heap );
335 subheap->commitSize += commitsize;
340 /***********************************************************************
343 * If possible, decommit the heap storage from (including) 'ptr'.
345 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr, DWORD flags )
347 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
351 /* round to next block and add one full block */
352 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
353 if (size >= subheap->commitSize) return TRUE;
355 address = (PVOID)((char *)subheap + size);
356 decommitsize = subheap->commitSize - size;
358 if (!(flags & HEAP_NO_VALLOC))
360 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
364 if (!NT_SUCCESS(Status));
366 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
367 subheap->commitSize - size,
368 (DWORD)((char *)subheap + size),
369 (DWORD)subheap->heap );
373 subheap->commitSize -= decommitsize;
378 /***********************************************************************
379 * HEAP_CreateFreeBlock
381 * Create a free block at a specified address. 'size' is the size of the
382 * whole block, including the new arena.
384 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
389 /* Create a free arena */
391 pFree = (ARENA_FREE *)ptr;
392 pFree->threadId = (DWORD)NtCurrentTeb()->Cid.UniqueThread;
393 pFree->magic = ARENA_FREE_MAGIC;
395 /* If debugging, erase the freed block content */
399 char *pEnd = (char *)ptr + size;
400 if (pEnd > (char *)subheap + subheap->commitSize)
401 pEnd = (char *)subheap + subheap->commitSize;
402 if (pEnd > (char *)(pFree + 1))
403 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
406 /* Check if next block is free also */
408 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
409 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
411 /* Remove the next arena from the free list */
412 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
413 pNext->next->prev = pNext->prev;
414 pNext->prev->next = pNext->next;
415 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
417 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
420 /* Set the next block PREV_FREE flag and pointer */
422 last = ((char *)ptr + size >= (char *)subheap + subheap->size);
425 DWORD *pNext = (DWORD *)((char *)ptr + size);
426 *pNext |= ARENA_FLAG_PREV_FREE;
427 *(ARENA_FREE **)(pNext - 1) = pFree;
430 /* Last, insert the new block into the free list */
432 pFree->size = size - sizeof(*pFree);
433 HEAP_InsertFreeBlock( subheap->heap, pFree, last );
437 /***********************************************************************
438 * HEAP_MakeInUseBlockFree
440 * Turn an in-use block into a free block. Can also decommit the end of
441 * the heap, and possibly even free the sub-heap altogether.
443 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena,
447 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
449 /* Check if we can merge with previous block */
451 if (pArena->size & ARENA_FLAG_PREV_FREE)
453 pFree = *((ARENA_FREE **)pArena - 1);
454 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
455 /* Remove it from the free list */
456 pFree->next->prev = pFree->prev;
457 pFree->prev->next = pFree->next;
459 else pFree = (ARENA_FREE *)pArena;
461 /* Create a free block */
463 HEAP_CreateFreeBlock( subheap, pFree, size );
464 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
465 if ((char *)pFree + size < (char *)subheap + subheap->size)
466 return; /* Not the last block, so nothing more to do */
468 /* Free the whole sub-heap if it's empty and not the original one */
470 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
471 (subheap != &subheap->heap->subheap))
473 SUBHEAP *pPrev = &subheap->heap->subheap;
474 /* Remove the free block from the list */
475 pFree->next->prev = pFree->prev;
476 pFree->prev->next = pFree->next;
477 /* Remove the subheap from the list */
478 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
479 if (pPrev) pPrev->next = subheap->next;
480 /* Free the memory */
482 if (!(flags & HEAP_NO_VALLOC))
485 ZwFreeVirtualMemory(NtCurrentProcess(),
493 /* Decommit the end of the heap */
497 /***********************************************************************
500 * Shrink an in-use block.
502 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
504 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
506 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
507 (pArena->size & ARENA_SIZE_MASK) - size );
508 /* assign size plus previous arena flags */
509 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
513 /* Turn off PREV_FREE flag in next block */
514 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
515 if (pNext < (char *)subheap + subheap->size)
516 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
520 /***********************************************************************
523 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
524 DWORD commitSize, DWORD totalSize )
526 SUBHEAP *subheap = (SUBHEAP *)address;
528 FREE_LIST_ENTRY *pEntry;
533 if (!(flags & HEAP_NO_VALLOC))
535 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
540 PAGE_EXECUTE_READWRITE);
541 if (!NT_SUCCESS(Status))
543 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
544 commitSize, (DWORD)address );
549 /* Fill the sub-heap structure */
551 subheap = (SUBHEAP *)address;
552 subheap->heap = heap;
553 subheap->selector = selector;
554 subheap->size = totalSize;
555 subheap->commitSize = commitSize;
556 subheap->magic = SUBHEAP_MAGIC;
558 if ( subheap != (SUBHEAP *)heap )
560 /* If this is a secondary subheap, insert it into list */
562 subheap->headerSize = sizeof(SUBHEAP);
563 subheap->next = heap->subheap.next;
564 heap->subheap.next = subheap;
568 /* If this is a primary subheap, initialize main heap */
570 subheap->headerSize = sizeof(HEAP);
571 subheap->next = NULL;
574 heap->magic = HEAP_MAGIC;
576 /* Build the free lists */
578 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
580 pEntry->size = HEAP_freeListSizes[i];
581 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
582 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
583 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
584 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
585 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
586 pEntry->arena.threadId = 0;
587 pEntry->arena.magic = ARENA_FREE_MAGIC;
590 /* Initialize critical section */
592 RtlInitializeCriticalSection( &heap->critSection );
595 /* Create the first free block */
597 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
598 subheap->size - subheap->headerSize );
603 /***********************************************************************
606 * Create a sub-heap of the given size.
607 * If heap == NULL, creates a main heap.
609 static SUBHEAP *HEAP_CreateSubHeap(PVOID BaseAddress,
610 HEAP *heap, DWORD flags,
611 DWORD commitSize, DWORD totalSize )
616 /* Round-up sizes on a 64K boundary */
618 totalSize = (totalSize + 0xffff) & 0xffff0000;
619 commitSize = (commitSize + 0xffff) & 0xffff0000;
620 if (!commitSize) commitSize = 0x10000;
621 if (totalSize < commitSize) totalSize = commitSize;
623 /* Allocate the memory block */
624 address = BaseAddress;
625 if (!(flags & HEAP_NO_VALLOC))
627 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
631 MEM_RESERVE | MEM_COMMIT,
632 PAGE_EXECUTE_READWRITE);
633 if (!NT_SUCCESS(Status))
635 WARN("Could not VirtualAlloc %08lx bytes\n",
641 /* Initialize subheap */
643 if (!HEAP_InitSubHeap( heap? heap : (HEAP *)address,
644 address, flags, commitSize, totalSize ))
646 if (address && !(flags & HEAP_NO_VALLOC))
649 ZwFreeVirtualMemory(NtCurrentProcess(),
657 return (SUBHEAP *)address;
661 /***********************************************************************
664 * Find a free block at least as large as the requested size, and make sure
665 * the requested size is committed.
667 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
668 SUBHEAP **ppSubHeap )
672 FREE_LIST_ENTRY *pEntry = heap->freeList;
674 /* Find a suitable free list, and in it find a block large enough */
676 while (pEntry->size < size) pEntry++;
677 pArena = pEntry->arena.next;
678 while (pArena != &heap->freeList[0].arena)
680 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
681 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
682 if (arena_size >= size)
684 subheap = HEAP_FindSubHeap( heap, pArena );
685 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
686 + size + HEAP_MIN_BLOCK_SIZE,
689 *ppSubHeap = subheap;
693 pArena = pArena->next;
696 /* If no block was found, attempt to grow the heap */
698 if (!(heap->flags & HEAP_GROWABLE))
700 WARN("Not enough space in heap %08lx for %08lx bytes\n",
704 /* make sure that we have a big enough size *committed* to fit another
705 * last free arena in !
706 * So just one heap struct, one first free arena which will eventually
707 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
708 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
709 size += sizeof(SUBHEAP) + sizeof(ARENA_FREE) + HEAP_MIN_BLOCK_SIZE;
710 if (!(subheap = HEAP_CreateSubHeap( NULL, heap, heap->flags, size,
711 max( HEAP_DEF_SIZE, size ) )))
714 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
715 (DWORD)subheap, size, (DWORD)heap );
717 *ppSubHeap = subheap;
718 return (ARENA_FREE *)(subheap + 1);
722 /***********************************************************************
723 * HEAP_IsValidArenaPtr
725 * Check that the pointer is inside the range possible for arenas.
727 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
730 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
731 if (!subheap) return FALSE;
732 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
733 if (subheap != &heap->subheap) return FALSE;
734 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
735 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
740 /***********************************************************************
741 * HEAP_ValidateFreeArena
743 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
745 char *heapEnd = (char *)subheap + subheap->size;
747 /* Check magic number */
748 if (pArena->magic != ARENA_FREE_MAGIC)
750 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
751 (DWORD)subheap->heap, (DWORD)pArena );
754 /* Check size flags */
755 if (!(pArena->size & ARENA_FLAG_FREE) ||
756 (pArena->size & ARENA_FLAG_PREV_FREE))
758 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
759 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
761 /* Check arena size */
762 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
764 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
765 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
768 /* Check that next pointer is valid */
769 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
771 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
772 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
775 /* Check that next arena is free */
776 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
777 (pArena->next->magic != ARENA_FREE_MAGIC))
779 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
780 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
783 /* Check that prev pointer is valid */
784 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
786 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
787 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
790 /* Check that prev arena is free */
791 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
792 (pArena->prev->magic != ARENA_FREE_MAGIC))
794 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
795 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
798 /* Check that next block has PREV_FREE flag */
799 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
801 if (!(*(DWORD *)((char *)(pArena + 1) +
802 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
804 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
805 (DWORD)subheap->heap, (DWORD)pArena );
808 /* Check next block back pointer */
809 if (*((ARENA_FREE **)((char *)(pArena + 1) +
810 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
812 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
813 (DWORD)subheap->heap, (DWORD)pArena,
814 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
822 /***********************************************************************
823 * HEAP_ValidateInUseArena
825 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
827 char *heapEnd = (char *)subheap + subheap->size;
829 /* Check magic number */
830 if (pArena->magic != ARENA_INUSE_MAGIC)
832 if (quiet == NOISY) {
833 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
834 (DWORD)subheap->heap, (DWORD)pArena );
836 HEAP_Dump( subheap->heap );
837 } else if (WARN_ON(heap)) {
838 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
839 (DWORD)subheap->heap, (DWORD)pArena );
841 HEAP_Dump( subheap->heap );
845 /* Check size flags */
846 if (pArena->size & ARENA_FLAG_FREE)
848 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
849 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
852 /* Check arena size */
853 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
855 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
856 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
859 /* Check next arena PREV_FREE flag */
860 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
861 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
863 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
864 (DWORD)subheap->heap, (DWORD)pArena );
867 /* Check prev free arena */
868 if (pArena->size & ARENA_FLAG_PREV_FREE)
870 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
871 /* Check prev pointer */
872 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
874 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
875 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
878 /* Check that prev arena is free */
879 if (!(pPrev->size & ARENA_FLAG_FREE) ||
880 (pPrev->magic != ARENA_FREE_MAGIC))
882 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
883 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
886 /* Check that prev arena is really the previous block */
887 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
889 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
890 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
898 /***********************************************************************
900 * Checks whether the pointer points to a block inside a given heap.
903 * Should this return BOOL32?
909 int HEAP_IsInsideHeap(
910 HANDLE heap, /* [in] Heap */
911 DWORD flags, /* [in] Flags */
912 LPCVOID ptr /* [in] Pointer */
914 HEAP *heapPtr = HEAP_GetPtr( heap );
918 /* Validate the parameters */
920 if (!heapPtr) return 0;
921 flags |= heapPtr->flags;
922 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
923 ret = (((subheap = HEAP_FindSubHeap( heapPtr, ptr )) != NULL) &&
924 (((char *)ptr >= (char *)subheap + subheap->headerSize
925 + sizeof(ARENA_INUSE))));
926 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
933 /***********************************************************************
934 * HEAP_IsRealArena [Internal]
935 * Validates a block is a valid arena.
941 static BOOL HEAP_IsRealArena(
942 HANDLE heap, /* [in] Handle to the heap */
943 DWORD flags, /* [in] Bit flags that control access during operation */
944 LPCVOID block, /* [in] Optional pointer to memory block to validate */
945 BOOL quiet /* [in] Flag - if true, HEAP_ValidateInUseArena
946 * does not complain */
949 HEAP *heapPtr = (HEAP *)(heap);
952 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
954 ERR("Invalid heap %08x!\n", heap );
958 flags &= HEAP_NO_SERIALIZE;
959 flags |= heapPtr->flags;
960 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
961 if (!(flags & HEAP_NO_SERIALIZE))
962 RtlEnterCriticalSection( &heapPtr->critSection );
966 /* Only check this single memory block */
968 /* The following code is really HEAP_IsInsideHeap *
969 * with serialization already done. */
970 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
971 ((char *)block < (char *)subheap + subheap->headerSize
972 + sizeof(ARENA_INUSE)))
976 ERR("Heap %08lx: block %08lx is not inside heap\n",
977 (DWORD)heap, (DWORD)block );
979 else if (WARN_ON(heap))
981 WARN("Heap %08lx: block %08lx is not inside heap\n",
982 (DWORD)heap, (DWORD)block );
986 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
988 if (!(flags & HEAP_NO_SERIALIZE))
989 RtlLeaveCriticalSection( &heapPtr->critSection );
993 subheap = &heapPtr->subheap;
994 while (subheap && ret)
996 char *ptr = (char *)subheap + subheap->headerSize;
997 while (ptr < (char *)subheap + subheap->size)
999 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1001 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
1005 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1009 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
1013 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1016 subheap = subheap->next;
1019 if (!(flags & HEAP_NO_SERIALIZE))
1020 RtlLeaveCriticalSection( &heapPtr->critSection );
1025 /***********************************************************************
1026 * HeapCreate (KERNEL32.336)
1028 * Handle of heap: Success
1034 RtlCreateHeap(ULONG flags,
1039 PRTL_HEAP_DEFINITION Definition)
1044 /* Allocate the heap block */
1048 maxSize = HEAP_DEF_SIZE;
1049 flags |= HEAP_GROWABLE;
1051 if (!(subheap = HEAP_CreateSubHeap( BaseAddress, NULL, flags, initialSize, maxSize )))
1056 RtlEnterCriticalSection (&RtlpProcessHeapsListLock);
1057 for (i = 0; i < NtCurrentPeb ()->NumberOfHeaps; i++)
1059 if (NtCurrentPeb ()->ProcessHeaps[i] == NULL)
1061 NtCurrentPeb()->ProcessHeaps[i] = (PVOID)subheap;
1065 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock);
1067 return (HANDLE)subheap;
1070 /***********************************************************************
1071 * HeapDestroy (KERNEL32.337)
1079 RtlDestroyHeap(HANDLE heap) /* [in] Handle of heap */
1081 HEAP *heapPtr = HEAP_GetPtr( heap );
1085 TRACE("%08x\n", heap );
1086 if (!heapPtr) return FALSE;
1088 RtlEnterCriticalSection (&RtlpProcessHeapsListLock);
1089 for (i = 0; i < NtCurrentPeb ()->NumberOfHeaps; i++)
1091 if (NtCurrentPeb ()->ProcessHeaps[i] == heap)
1093 NtCurrentPeb()->ProcessHeaps[i] = NULL;
1097 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock);
1099 RtlDeleteCriticalSection( &heapPtr->critSection );
1100 subheap = &heapPtr->subheap;
1101 // We must save the flags. The first subheap is located after
1102 // the heap structure. If we release the first subheap,
1103 // we release also the heap structure.
1104 flags = heapPtr->flags;
1107 SUBHEAP *next = subheap->next;
1109 if (!(flags & HEAP_NO_VALLOC))
1111 ULONG dummySize = 0;
1112 ZwFreeVirtualMemory(NtCurrentProcess(),
1123 /***********************************************************************
1124 * HeapAlloc (KERNEL32.334)
1126 * Pointer to allocated memory block
1132 RtlAllocateHeap(HANDLE heap, /* [in] Handle of private heap block */
1133 ULONG flags, /* [in] Heap allocation control flags */
1134 ULONG size) /* [in] Number of bytes to allocate */
1137 ARENA_INUSE *pInUse;
1139 HEAP *heapPtr = HEAP_GetPtr( heap );
1141 /* Validate the parameters */
1145 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1148 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1149 flags |= heapPtr->flags;
1150 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1151 size = (size + 3) & ~3;
1152 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1154 /* Locate a suitable free block */
1156 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1158 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1159 heap, flags, size );
1160 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1161 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1165 /* Remove the arena from the free list */
1167 pArena->next->prev = pArena->prev;
1168 pArena->prev->next = pArena->next;
1170 /* Build the in-use arena */
1172 pInUse = (ARENA_INUSE *)pArena;
1173 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1174 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1175 pInUse->callerEIP = GET_EIP();
1176 pInUse->threadId = (DWORD)NtCurrentTeb()->Cid.UniqueThread;
1177 pInUse->magic = ARENA_INUSE_MAGIC;
1179 /* Shrink the block */
1181 HEAP_ShrinkBlock( subheap, pInUse, size );
1183 if (flags & HEAP_ZERO_MEMORY)
1184 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1185 else if (TRACE_ON(heap))
1186 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1188 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1190 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1191 heap, flags, size, (DWORD)(pInUse + 1) );
1192 return (LPVOID)(pInUse + 1);
1196 /***********************************************************************
1197 * HeapFree (KERNEL32.338)
1204 BOOLEAN STDCALL RtlFreeHeap(
1205 HANDLE heap, /* [in] Handle of heap */
1206 ULONG flags, /* [in] Heap freeing flags */
1207 PVOID ptr /* [in] Address of memory to free */
1209 ARENA_INUSE *pInUse;
1211 HEAP *heapPtr = HEAP_GetPtr( heap );
1213 /* Validate the parameters */
1215 if (!heapPtr) return FALSE;
1216 if (!ptr) /* Freeing a NULL ptr is doesn't indicate an error in Win2k */
1218 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
1219 heap, flags, (DWORD)ptr );
1223 flags &= HEAP_NO_SERIALIZE;
1224 flags |= heapPtr->flags;
1225 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1226 if (!HEAP_IsRealArena( heap, HEAP_NO_SERIALIZE, ptr, QUIET ))
1228 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1229 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1230 heap, flags, (DWORD)ptr );
1234 /* Turn the block into a free block */
1236 pInUse = (ARENA_INUSE *)ptr - 1;
1237 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1238 HEAP_MakeInUseBlockFree( subheap, pInUse, heapPtr->flags );
1240 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1242 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1243 heap, flags, (DWORD)ptr );
1248 /***********************************************************************
1249 * HeapReAlloc (KERNEL32.340)
1251 * Pointer to reallocated memory block
1256 LPVOID STDCALL RtlReAllocateHeap(
1257 HANDLE heap, /* [in] Handle of heap block */
1258 DWORD flags, /* [in] Heap reallocation flags */
1259 LPVOID ptr, /* [in] Address of memory to reallocate */
1260 DWORD size /* [in] Number of bytes to reallocate */
1262 ARENA_INUSE *pArena;
1267 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1268 if (!(heapPtr = HEAP_GetPtr( heap ))) return FALSE;
1270 /* Validate the parameters */
1272 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1273 HEAP_REALLOC_IN_PLACE_ONLY;
1274 flags |= heapPtr->flags;
1275 size = (size + 3) & ~3;
1276 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1278 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1279 if (!HEAP_IsRealArena( heap, HEAP_NO_SERIALIZE, ptr, QUIET ))
1281 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1282 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1283 heap, flags, (DWORD)ptr, size );
1284 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1288 /* Check if we need to grow the block */
1290 pArena = (ARENA_INUSE *)ptr - 1;
1291 pArena->threadId = (DWORD)NtCurrentTeb()->Cid.UniqueThread;
1293 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1294 oldSize = (pArena->size & ARENA_SIZE_MASK);
1297 char *pNext = (char *)(pArena + 1) + oldSize;
1298 if ((pNext < (char *)subheap + subheap->size) &&
1299 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1300 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1302 /* The next block is free and large enough */
1303 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1304 pFree->next->prev = pFree->prev;
1305 pFree->prev->next = pFree->next;
1306 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1307 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1308 + size + HEAP_MIN_BLOCK_SIZE,
1311 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1312 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1315 HEAP_ShrinkBlock( subheap, pArena, size );
1317 else /* Do it the hard way */
1320 ARENA_INUSE *pInUse;
1321 SUBHEAP *newsubheap;
1323 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1324 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1326 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1327 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1331 /* Build the in-use arena */
1333 pNew->next->prev = pNew->prev;
1334 pNew->prev->next = pNew->next;
1335 pInUse = (ARENA_INUSE *)pNew;
1336 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1337 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1338 pInUse->threadId = (DWORD)NtCurrentTeb()->Cid.UniqueThread;
1339 pInUse->magic = ARENA_INUSE_MAGIC;
1340 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1341 memcpy( pInUse + 1, pArena + 1, oldSize );
1343 /* Free the previous block */
1345 HEAP_MakeInUseBlockFree( subheap, pArena, flags );
1346 subheap = newsubheap;
1350 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1352 /* Clear the extra bytes if needed */
1356 if (flags & HEAP_ZERO_MEMORY)
1357 memset( (char *)(pArena + 1) + oldSize, 0,
1358 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1359 else if (TRACE_ON(heap))
1360 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1361 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1364 /* Return the new arena */
1366 pArena->callerEIP = GET_EIP();
1367 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1369 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1370 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1371 return (LPVOID)(pArena + 1);
1375 /***********************************************************************
1376 * HeapCompact (KERNEL32.335)
1380 DWORD STDCALL RtlCompactHeap( HANDLE heap, DWORD flags )
1382 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1387 /***********************************************************************
1388 * HeapLock (KERNEL32.339)
1389 * Attempts to acquire the critical section object for a specified heap.
1397 BOOL STDCALL RtlLockHeap(
1398 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
1400 HEAP *heapPtr = HEAP_GetPtr( heap );
1401 if (!heapPtr) return FALSE;
1402 RtlEnterCriticalSection( &heapPtr->critSection );
1407 /***********************************************************************
1408 * HeapUnlock (KERNEL32.342)
1409 * Releases ownership of the critical section object.
1417 BOOL STDCALL RtlUnlockHeap(
1418 HANDLE heap /* [in] Handle to the heap to unlock */
1420 HEAP *heapPtr = HEAP_GetPtr( heap );
1421 if (!heapPtr) return FALSE;
1422 RtlLeaveCriticalSection( &heapPtr->critSection );
1427 /***********************************************************************
1428 * HeapSize (KERNEL32.341)
1430 * Size in bytes of allocated memory
1431 * 0xffffffff: Failure
1435 DWORD STDCALL RtlSizeHeap(
1436 HANDLE heap, /* [in] Handle of heap */
1437 DWORD flags, /* [in] Heap size control flags */
1438 LPVOID ptr /* [in] Address of memory to return size for */
1441 HEAP *heapPtr = HEAP_GetPtr( heap );
1443 if (!heapPtr) return FALSE;
1444 flags &= HEAP_NO_SERIALIZE;
1445 flags |= heapPtr->flags;
1446 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1447 if (!HEAP_IsRealArena( heap, HEAP_NO_SERIALIZE, ptr, QUIET ))
1449 SetLastError( ERROR_INVALID_PARAMETER );
1454 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1455 ret = pArena->size & ARENA_SIZE_MASK;
1457 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1459 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1460 heap, flags, (DWORD)ptr, ret );
1465 /***********************************************************************
1466 * HeapValidate (KERNEL32.343)
1467 * Validates a specified heap.
1478 BOOL STDCALL RtlValidateHeap(
1479 HANDLE heap, /* [in] Handle to the heap */
1480 DWORD flags, /* [in] Bit flags that control access during operation */
1481 PVOID block /* [in] Optional pointer to memory block to validate */
1484 HEAP *heapPtr = HEAP_GetPtr( heap );
1485 if (!heapPtr) return FALSE;
1486 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1490 /***********************************************************************
1491 * HeapWalk (KERNEL32.344)
1492 * Enumerates the memory blocks in a specified heap.
1493 * See HEAP_Dump() for info on heap structure.
1496 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
1497 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
1504 BOOL STDCALL HeapWalk(
1505 HANDLE heap, /* [in] Handle to heap to enumerate */
1506 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
1508 HEAP *heapPtr = HEAP_GetPtr(heap);
1509 SUBHEAP *sub, *currentheap = NULL;
1512 int region_index = 0;
1514 if (!heapPtr || !entry)
1516 SetLastError(ERROR_INVALID_PARAMETER);
1520 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1522 /* set ptr to the next arena to be examined */
1524 if (!entry->lpData) /* first call (init) ? */
1526 TRACE("begin walking of heap 0x%08x.\n", heap);
1527 /*HEAP_Dump(heapPtr);*/
1528 currentheap = &heapPtr->subheap;
1529 ptr = (char*)currentheap + currentheap->headerSize;
1533 ptr = entry->lpData;
1534 sub = &heapPtr->subheap;
1537 if (((char *)ptr >= (char *)sub) &&
1538 ((char *)ptr < (char *)sub + sub->size))
1546 if (currentheap == NULL)
1548 ERR("no matching subheap found, shouldn't happen !\n");
1549 SetLastError(ERROR_NO_MORE_ITEMS);
1553 ptr += entry->cbData; /* point to next arena */
1554 if (ptr > (char *)currentheap + currentheap->size - 1)
1555 { /* proceed with next subheap */
1556 if (!(currentheap = currentheap->next))
1557 { /* successfully finished */
1558 TRACE("end reached.\n");
1559 SetLastError(ERROR_NO_MORE_ITEMS);
1562 ptr = (char*)currentheap + currentheap->headerSize;
1567 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1569 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1571 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1573 entry->lpData = pArena + 1;
1574 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1575 entry->cbOverhead = sizeof(ARENA_FREE);
1576 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1580 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1582 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1584 entry->lpData = pArena + 1;
1585 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1586 entry->cbOverhead = sizeof(ARENA_INUSE);
1587 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1588 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1589 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1592 entry->iRegionIndex = region_index;
1594 /* first element of heap ? */
1595 if (ptr == (char *)(currentheap + currentheap->headerSize))
1597 entry->wFlags |= PROCESS_HEAP_REGION;
1598 entry->Foo.Region.dwCommittedSize = currentheap->commitSize;
1599 entry->Foo.Region.dwUnCommittedSize =
1600 currentheap->size - currentheap->commitSize;
1601 entry->Foo.Region.lpFirstBlock = /* first valid block */
1602 currentheap + currentheap->headerSize;
1603 entry->Foo.Region.lpLastBlock = /* first invalid block */
1604 currentheap + currentheap->size;
1609 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1616 RtlInitializeHeapManager(VOID)
1620 Peb = NtCurrentPeb();
1622 Peb->NumberOfHeaps = 0;
1623 Peb->MaximumNumberOfHeaps = (PAGE_SIZE - sizeof(PEB)) / sizeof(HANDLE);
1624 Peb->ProcessHeaps = (PVOID)Peb + sizeof(PEB);
1626 RtlInitializeCriticalSection(&RtlpProcessHeapsListLock);
1634 RtlEnumProcessHeaps(DWORD STDCALL(*func)(void*,LONG),
1637 NTSTATUS Status = STATUS_SUCCESS;
1640 RtlEnterCriticalSection(&RtlpProcessHeapsListLock);
1642 for (i = 0; i < NtCurrentPeb()->NumberOfHeaps; i++)
1644 Status = func(NtCurrentPeb()->ProcessHeaps[i],lParam);
1645 if (!NT_SUCCESS(Status))
1649 RtlLeaveCriticalSection(&RtlpProcessHeapsListLock);
1659 RtlGetProcessHeaps(ULONG HeapCount,
1664 RtlEnterCriticalSection(&RtlpProcessHeapsListLock);
1666 if (NtCurrentPeb()->NumberOfHeaps <= HeapCount)
1668 Result = NtCurrentPeb()->NumberOfHeaps;
1670 NtCurrentPeb()->ProcessHeaps,
1671 Result * sizeof(HANDLE));
1674 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock);
1684 RtlValidateProcessHeaps(VOID)
1687 BOOLEAN Result = TRUE;
1691 HeapCount = RtlGetProcessHeaps(128, Heaps);
1692 for (i = 0; i < HeapCount; i++)
1694 if (!RtlValidateHeap(Heaps[i], 0, NULL))