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
,
245 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
246 while (pEntry
->size
< pArena
->size
) pEntry
++;
247 pArena
->size
|= ARENA_FLAG_FREE
;
248 pArena
->next
= pEntry
->arena
.next
;
249 pArena
->next
->prev
= pArena
;
250 pArena
->prev
= &pEntry
->arena
;
251 pEntry
->arena
.next
= pArena
;
255 /***********************************************************************
257 * Find the sub-heap containing a given address.
264 HEAP_FindSubHeap(HEAP
*heap
, /* [in] Heap pointer */
265 LPCVOID ptr
) /* [in] Address */
267 SUBHEAP
*sub
= &heap
->subheap
;
270 if (((char *)ptr
>= (char *)sub
) &&
271 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
278 /***********************************************************************
281 * Make sure the heap storage is committed up to (not including) ptr.
284 HEAP_Commit(SUBHEAP
*subheap
,
288 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
293 size
= (size
+ COMMIT_MASK
) & ~COMMIT_MASK
;
294 if (size
> subheap
->size
) size
= subheap
->size
;
295 if (size
<= subheap
->commitSize
) return TRUE
;
297 address
= (PVOID
)((char *)subheap
+ subheap
->commitSize
);
298 commitsize
= size
- subheap
->commitSize
;
300 if (!(flags
& HEAP_NO_VALLOC
))
302 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
307 PAGE_EXECUTE_READWRITE
);
308 if (!NT_SUCCESS(Status
))
310 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
311 size
- subheap
->commitSize
,
312 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
313 (DWORD
)subheap
->heap
);
317 subheap
->commitSize
= size
;
322 /***********************************************************************
325 * If possible, decommit the heap storage from (including) 'ptr'.
327 static inline BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
, DWORD flags
)
329 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
333 /* round to next block and add one full block */
334 size
= ((size
+ COMMIT_MASK
) & ~COMMIT_MASK
) + COMMIT_MASK
+ 1;
335 if (size
>= subheap
->commitSize
) return TRUE
;
337 address
= (PVOID
)((char *)subheap
+ size
);
338 decommitsize
= subheap
->commitSize
- size
;
340 if (!(flags
& HEAP_NO_VALLOC
))
342 Status
= ZwFreeVirtualMemory(NtCurrentProcess(),
346 if (!NT_SUCCESS(Status
));
348 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
349 subheap
->commitSize
- size
,
350 (DWORD
)((char *)subheap
+ size
),
351 (DWORD
)subheap
->heap
);
355 subheap
->commitSize
= size
;
360 /***********************************************************************
361 * HEAP_CreateFreeBlock
363 * Create a free block at a specified address. 'size' is the size of the
364 * whole block, including the new arena.
366 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
370 /* Create a free arena */
372 pFree
= (ARENA_FREE
*)ptr
;
373 pFree
->threadId
= (DWORD
)NtCurrentTeb()->Cid
.UniqueThread
;
374 pFree
->magic
= ARENA_FREE_MAGIC
;
376 /* If debugging, erase the freed block content */
380 char *pEnd
= (char *)ptr
+ size
;
381 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
382 pEnd
= (char *)subheap
+ subheap
->commitSize
;
383 if (pEnd
> (char *)(pFree
+ 1))
384 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
387 /* Check if next block is free also */
389 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
390 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
392 /* Remove the next arena from the free list */
393 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
394 pNext
->next
->prev
= pNext
->prev
;
395 pNext
->prev
->next
= pNext
->next
;
396 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
398 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
401 /* Set the next block PREV_FREE flag and pointer */
403 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
405 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
406 *pNext
|= ARENA_FLAG_PREV_FREE
;
407 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
410 /* Last, insert the new block into the free list */
412 pFree
->size
= size
- sizeof(*pFree
);
413 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
417 /***********************************************************************
418 * HEAP_MakeInUseBlockFree
420 * Turn an in-use block into a free block. Can also decommit the end of
421 * the heap, and possibly even free the sub-heap altogether.
423 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
,
427 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
429 /* Check if we can merge with previous block */
431 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
433 pFree
= *((ARENA_FREE
**)pArena
- 1);
434 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
435 /* Remove it from the free list */
436 pFree
->next
->prev
= pFree
->prev
;
437 pFree
->prev
->next
= pFree
->next
;
439 else pFree
= (ARENA_FREE
*)pArena
;
441 /* Create a free block */
443 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
444 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
445 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
446 return; /* Not the last block, so nothing more to do */
448 /* Free the whole sub-heap if it's empty and not the original one */
450 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
451 (subheap
!= &subheap
->heap
->subheap
))
453 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
454 /* Remove the free block from the list */
455 pFree
->next
->prev
= pFree
->prev
;
456 pFree
->prev
->next
= pFree
->next
;
457 /* Remove the subheap from the list */
458 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
459 if (pPrev
) pPrev
->next
= subheap
->next
;
460 /* Free the memory */
462 if (!(flags
& HEAP_NO_VALLOC
))
465 ZwFreeVirtualMemory(NtCurrentProcess(),
473 /* Decommit the end of the heap */
477 /***********************************************************************
480 * Shrink an in-use block.
482 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
484 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
486 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
487 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
488 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
492 /* Turn off PREV_FREE flag in next block */
493 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
494 if (pNext
< (char *)subheap
+ subheap
->size
)
495 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
499 /***********************************************************************
502 static BOOL
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
503 DWORD commitSize
, DWORD totalSize
)
505 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
507 FREE_LIST_ENTRY
*pEntry
;
512 if (!(flags
& HEAP_NO_VALLOC
))
514 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
519 PAGE_EXECUTE_READWRITE
);
520 if (!NT_SUCCESS(Status
))
522 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
523 commitSize
, (DWORD
)address
);
528 /* Fill the sub-heap structure */
530 subheap
->heap
= heap
;
531 subheap
->selector
= selector
;
532 subheap
->size
= totalSize
;
533 subheap
->commitSize
= commitSize
;
534 subheap
->magic
= SUBHEAP_MAGIC
;
536 if ( subheap
!= (SUBHEAP
*)heap
)
538 /* If this is a secondary subheap, insert it into list */
540 subheap
->headerSize
= sizeof(SUBHEAP
);
541 subheap
->next
= heap
->subheap
.next
;
542 heap
->subheap
.next
= subheap
;
546 /* If this is a primary subheap, initialize main heap */
548 subheap
->headerSize
= sizeof(HEAP
);
549 subheap
->next
= NULL
;
552 heap
->magic
= HEAP_MAGIC
;
554 /* Build the free lists */
556 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
558 pEntry
->size
= HEAP_freeListSizes
[i
];
559 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
560 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
561 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
562 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
563 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
564 pEntry
->arena
.threadId
= 0;
565 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
568 /* Initialize critical section */
570 RtlInitializeCriticalSection( &heap
->critSection
);
573 /* Create the first free block */
575 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
576 subheap
->size
- subheap
->headerSize
);
581 /***********************************************************************
584 * Create a sub-heap of the given size.
585 * If heap == NULL, creates a main heap.
587 static SUBHEAP
*HEAP_CreateSubHeap(PVOID BaseAddress
,
588 HEAP
*heap
, DWORD flags
,
589 DWORD commitSize
, DWORD totalSize
)
594 /* Round-up sizes on a 64K boundary */
596 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
597 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
598 if (!commitSize
) commitSize
= 0x10000;
599 if (totalSize
< commitSize
) totalSize
= commitSize
;
601 /* Allocate the memory block */
602 address
= BaseAddress
;
603 if (!(flags
& HEAP_NO_VALLOC
))
605 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
609 MEM_RESERVE
| MEM_COMMIT
,
610 PAGE_EXECUTE_READWRITE
);
611 if (!NT_SUCCESS(Status
))
613 WARN("Could not VirtualAlloc %08lx bytes\n",
619 /* Initialize subheap */
621 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
622 address
, flags
, commitSize
, totalSize
))
624 if (!(flags
& HEAP_NO_VALLOC
))
627 ZwFreeVirtualMemory(NtCurrentProcess(),
635 return (SUBHEAP
*)address
;
639 /***********************************************************************
642 * Find a free block at least as large as the requested size, and make sure
643 * the requested size is committed.
645 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
646 SUBHEAP
**ppSubHeap
)
650 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
652 /* Find a suitable free list, and in it find a block large enough */
654 while (pEntry
->size
< size
) pEntry
++;
655 pArena
= pEntry
->arena
.next
;
656 while (pArena
!= &heap
->freeList
[0].arena
)
658 if (pArena
->size
> size
)
660 subheap
= HEAP_FindSubHeap( heap
, pArena
);
661 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
662 + size
+ HEAP_MIN_BLOCK_SIZE
,
665 *ppSubHeap
= subheap
;
669 pArena
= pArena
->next
;
672 /* If no block was found, attempt to grow the heap */
674 if (!(heap
->flags
& HEAP_GROWABLE
))
676 WARN("Not enough space in heap %08lx for %08lx bytes\n",
680 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
681 if (!(subheap
= HEAP_CreateSubHeap( NULL
, heap
, heap
->flags
, size
,
682 max( HEAP_DEF_SIZE
, size
) )))
685 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
686 (DWORD
)subheap
, size
, (DWORD
)heap
);
688 *ppSubHeap
= subheap
;
689 return (ARENA_FREE
*)(subheap
+ 1);
693 /***********************************************************************
694 * HEAP_IsValidArenaPtr
696 * Check that the pointer is inside the range possible for arenas.
698 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
701 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
702 if (!subheap
) return FALSE
;
703 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
704 if (subheap
!= &heap
->subheap
) return FALSE
;
705 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
706 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
711 /***********************************************************************
712 * HEAP_ValidateFreeArena
714 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
716 char *heapEnd
= (char *)subheap
+ subheap
->size
;
718 /* Check magic number */
719 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
721 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
722 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
725 /* Check size flags */
726 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
727 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
729 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
730 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
732 /* Check arena size */
733 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
735 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
736 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
739 /* Check that next pointer is valid */
740 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
742 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
743 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
746 /* Check that next arena is free */
747 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
748 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
750 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
751 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
754 /* Check that prev pointer is valid */
755 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
757 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
758 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
761 /* Check that prev arena is free */
762 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
763 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
765 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
766 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
769 /* Check that next block has PREV_FREE flag */
770 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
772 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
773 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
775 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
776 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
779 /* Check next block back pointer */
780 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
781 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
783 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
784 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
785 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
793 /***********************************************************************
794 * HEAP_ValidateInUseArena
796 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, BOOL quiet
)
798 char *heapEnd
= (char *)subheap
+ subheap
->size
;
800 /* Check magic number */
801 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
803 if (quiet
== NOISY
) {
804 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
805 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
807 HEAP_Dump( subheap
->heap
);
808 } else if (WARN_ON(heap
)) {
809 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
810 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
812 HEAP_Dump( subheap
->heap
);
816 /* Check size flags */
817 if (pArena
->size
& ARENA_FLAG_FREE
)
819 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
820 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
822 /* Check arena size */
823 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
825 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
826 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
829 /* Check next arena PREV_FREE flag */
830 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
831 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
833 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
834 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
837 /* Check prev free arena */
838 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
840 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
841 /* Check prev pointer */
842 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
844 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
845 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
848 /* Check that prev arena is free */
849 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
850 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
852 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
853 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
856 /* Check that prev arena is really the previous block */
857 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
859 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
860 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
868 /***********************************************************************
870 * Checks whether the pointer points to a block inside a given heap.
873 * Should this return BOOL32?
879 int HEAP_IsInsideHeap(
880 HANDLE heap
, /* [in] Heap */
881 DWORD flags
, /* [in] Flags */
882 LPCVOID ptr
/* [in] Pointer */
884 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
888 /* Validate the parameters */
890 if (!heapPtr
) return 0;
891 flags
|= heapPtr
->flags
;
892 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
893 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
894 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
895 + sizeof(ARENA_INUSE
))));
896 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
903 /***********************************************************************
904 * HEAP_IsRealArena [Internal]
905 * Validates a block is a valid arena.
911 static BOOL
HEAP_IsRealArena(
912 HANDLE heap
, /* [in] Handle to the heap */
913 DWORD flags
, /* [in] Bit flags that control access during operation */
914 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
915 BOOL quiet
/* [in] Flag - if true, HEAP_ValidateInUseArena
916 * does not complain */
919 HEAP
*heapPtr
= (HEAP
*)(heap
);
922 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
924 ERR("Invalid heap %08x!\n", heap
);
928 flags
&= HEAP_NO_SERIALIZE
;
929 flags
|= heapPtr
->flags
;
930 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
931 if (!(flags
& HEAP_NO_SERIALIZE
))
932 RtlEnterCriticalSection( &heapPtr
->critSection
);
936 /* Only check this single memory block */
938 /* The following code is really HEAP_IsInsideHeap *
939 * with serialization already done. */
940 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
941 ((char *)block
< (char *)subheap
+ subheap
->headerSize
942 + sizeof(ARENA_INUSE
)))
946 ERR("Heap %08lx: block %08lx is not inside heap\n",
947 (DWORD
)heap
, (DWORD
)block
);
949 else if (WARN_ON(heap
))
951 WARN("Heap %08lx: block %08lx is not inside heap\n",
952 (DWORD
)heap
, (DWORD
)block
);
956 ret
= HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1, quiet
);
958 if (!(flags
& HEAP_NO_SERIALIZE
))
959 RtlLeaveCriticalSection( &heapPtr
->critSection
);
963 subheap
= &heapPtr
->subheap
;
964 while (subheap
&& ret
)
966 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
967 while (ptr
< (char *)subheap
+ subheap
->size
)
969 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
971 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
975 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
979 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
983 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
986 subheap
= subheap
->next
;
989 if (!(flags
& HEAP_NO_SERIALIZE
))
990 RtlLeaveCriticalSection( &heapPtr
->critSection
);
995 /***********************************************************************
996 * HeapCreate (KERNEL32.336)
998 * Handle of heap: Success
1002 RtlCreateHeap(ULONG flags
,
1007 PRTL_HEAP_DEFINITION Definition
)
1012 /* Allocate the heap block */
1016 maxSize
= HEAP_DEF_SIZE
;
1017 flags
|= HEAP_GROWABLE
;
1019 if (!(subheap
= HEAP_CreateSubHeap( BaseAddress
, NULL
, flags
, initialSize
, maxSize
)))
1024 RtlEnterCriticalSection (&RtlpProcessHeapsListLock
);
1025 for (i
= 0; i
< NtCurrentPeb ()->NumberOfHeaps
; i
++)
1027 if (NtCurrentPeb ()->ProcessHeaps
[i
] == NULL
)
1029 NtCurrentPeb()->ProcessHeaps
[i
] = (PVOID
)subheap
;
1033 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock
);
1035 return (HANDLE
)subheap
;
1038 /***********************************************************************
1039 * HeapDestroy (KERNEL32.337)
1045 RtlDestroyHeap(HANDLE heap
) /* [in] Handle of heap */
1047 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1051 TRACE("%08x\n", heap
);
1052 if (!heapPtr
) return FALSE
;
1054 RtlEnterCriticalSection (&RtlpProcessHeapsListLock
);
1055 for (i
= 0; i
< NtCurrentPeb ()->NumberOfHeaps
; i
++)
1057 if (NtCurrentPeb ()->ProcessHeaps
[i
] == heap
)
1059 NtCurrentPeb()->ProcessHeaps
[i
] = NULL
;
1063 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock
);
1065 RtlDeleteCriticalSection( &heapPtr
->critSection
);
1066 subheap
= &heapPtr
->subheap
;
1067 // We must save the flags. The first subheap is located after
1068 // the heap structure. If we release the first subheap,
1069 // we release also the heap structure.
1070 flags
= heapPtr
->flags
;
1073 SUBHEAP
*next
= subheap
->next
;
1075 if (!(flags
& HEAP_NO_VALLOC
))
1077 ULONG dummySize
= 0;
1078 ZwFreeVirtualMemory(NtCurrentProcess(),
1089 /***********************************************************************
1090 * HeapAlloc (KERNEL32.334)
1092 * Pointer to allocated memory block
1096 RtlAllocateHeap(HANDLE heap
, /* [in] Handle of private heap block */
1097 ULONG flags
, /* [in] Heap allocation control flags */
1098 ULONG size
) /* [in] Number of bytes to allocate */
1101 ARENA_INUSE
*pInUse
;
1103 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1105 /* Validate the parameters */
1107 if (!heapPtr
) return NULL
;
1108 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1109 flags
|= heapPtr
->flags
;
1110 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1111 size
= (size
+ 3) & ~3;
1112 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1114 /* Locate a suitable free block */
1116 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
1118 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1119 heap
, flags
, size
);
1120 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1124 /* Remove the arena from the free list */
1126 pArena
->next
->prev
= pArena
->prev
;
1127 pArena
->prev
->next
= pArena
->next
;
1129 /* Build the in-use arena */
1131 pInUse
= (ARENA_INUSE
*)pArena
;
1132 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1133 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1134 pInUse
->callerEIP
= GET_EIP();
1135 pInUse
->threadId
= (DWORD
)NtCurrentTeb()->Cid
.UniqueThread
;
1136 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1138 /* Shrink the block */
1140 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
1142 if (flags
& HEAP_ZERO_MEMORY
)
1143 memset( pInUse
+ 1, 0, pInUse
->size
& ARENA_SIZE_MASK
);
1144 else if (TRACE_ON(heap
))
1145 memset( pInUse
+ 1, ARENA_INUSE_FILLER
, pInUse
->size
& ARENA_SIZE_MASK
);
1147 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1149 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1150 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
1151 return (LPVOID
)(pInUse
+ 1);
1155 /***********************************************************************
1156 * HeapFree (KERNEL32.338)
1161 BOOLEAN STDCALL
RtlFreeHeap(
1162 HANDLE heap
, /* [in] Handle of heap */
1163 ULONG flags
, /* [in] Heap freeing flags */
1164 PVOID ptr
/* [in] Address of memory to free */
1166 ARENA_INUSE
*pInUse
;
1168 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1170 /* Validate the parameters */
1172 if (!heapPtr
) return FALSE
;
1173 if (!ptr
) /* Freeing a NULL ptr is doesn't indicate an error in Win2k */
1175 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
1176 heap
, flags
, (DWORD
)ptr
);
1180 flags
&= HEAP_NO_SERIALIZE
;
1181 flags
|= heapPtr
->flags
;
1182 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1183 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1185 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1186 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1187 heap
, flags
, (DWORD
)ptr
);
1191 /* Turn the block into a free block */
1193 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1194 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1195 HEAP_MakeInUseBlockFree( subheap
, pInUse
, heapPtr
->flags
);
1197 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1199 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1200 heap
, flags
, (DWORD
)ptr
);
1205 /***********************************************************************
1206 * HeapReAlloc (KERNEL32.340)
1208 * Pointer to reallocated memory block
1211 LPVOID STDCALL
RtlReAllocateHeap(
1212 HANDLE heap
, /* [in] Handle of heap block */
1213 DWORD flags
, /* [in] Heap reallocation flags */
1214 LPVOID ptr
, /* [in] Address of memory to reallocate */
1215 DWORD size
/* [in] Number of bytes to reallocate */
1217 ARENA_INUSE
*pArena
;
1222 if (!ptr
) return RtlAllocateHeap( heap
, flags
, size
); /* FIXME: correct? */
1223 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1225 /* Validate the parameters */
1227 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1228 HEAP_REALLOC_IN_PLACE_ONLY
;
1229 flags
|= heapPtr
->flags
;
1230 size
= (size
+ 3) & ~3;
1231 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1233 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1234 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1236 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1237 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1238 heap
, flags
, (DWORD
)ptr
, size
);
1242 /* Check if we need to grow the block */
1244 pArena
= (ARENA_INUSE
*)ptr
- 1;
1245 pArena
->threadId
= (DWORD
)NtCurrentTeb()->Cid
.UniqueThread
;
1247 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1248 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1251 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1252 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1253 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1254 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1256 /* The next block is free and large enough */
1257 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1258 pFree
->next
->prev
= pFree
->prev
;
1259 pFree
->prev
->next
= pFree
->next
;
1260 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1261 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1262 + size
+ HEAP_MIN_BLOCK_SIZE
,
1265 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1268 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1270 else /* Do it the hard way */
1273 ARENA_INUSE
*pInUse
;
1274 SUBHEAP
*newsubheap
;
1276 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1277 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1279 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1283 /* Build the in-use arena */
1285 pNew
->next
->prev
= pNew
->prev
;
1286 pNew
->prev
->next
= pNew
->next
;
1287 pInUse
= (ARENA_INUSE
*)pNew
;
1288 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1289 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1290 pInUse
->threadId
= (DWORD
)NtCurrentTeb()->Cid
.UniqueThread
;
1291 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1292 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1293 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1295 /* Free the previous block */
1297 HEAP_MakeInUseBlockFree( subheap
, pArena
, flags
);
1298 subheap
= newsubheap
;
1302 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1304 /* Clear the extra bytes if needed */
1308 if (flags
& HEAP_ZERO_MEMORY
)
1309 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1310 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1311 else if (TRACE_ON(heap
))
1312 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1313 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1316 /* Return the new arena */
1318 pArena
->callerEIP
= GET_EIP();
1319 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1321 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1322 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1323 return (LPVOID
)(pArena
+ 1);
1327 /***********************************************************************
1328 * HeapCompact (KERNEL32.335)
1330 DWORD STDCALL
RtlCompactHeap( HANDLE heap
, DWORD flags
)
1332 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1337 /***********************************************************************
1338 * HeapLock (KERNEL32.339)
1339 * Attempts to acquire the critical section object for a specified heap.
1345 BOOL STDCALL
RtlLockHeap(
1346 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
1348 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1349 if (!heapPtr
) return FALSE
;
1350 RtlEnterCriticalSection( &heapPtr
->critSection
);
1355 /***********************************************************************
1356 * HeapUnlock (KERNEL32.342)
1357 * Releases ownership of the critical section object.
1363 BOOL STDCALL
RtlUnlockHeap(
1364 HANDLE heap
/* [in] Handle to the heap to unlock */
1366 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1367 if (!heapPtr
) return FALSE
;
1368 RtlLeaveCriticalSection( &heapPtr
->critSection
);
1373 /***********************************************************************
1374 * HeapSize (KERNEL32.341)
1376 * Size in bytes of allocated memory
1377 * 0xffffffff: Failure
1379 DWORD STDCALL
RtlSizeHeap(
1380 HANDLE heap
, /* [in] Handle of heap */
1381 DWORD flags
, /* [in] Heap size control flags */
1382 LPVOID ptr
/* [in] Address of memory to return size for */
1385 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1387 if (!heapPtr
) return FALSE
;
1388 flags
&= HEAP_NO_SERIALIZE
;
1389 flags
|= heapPtr
->flags
;
1390 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1391 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1393 SetLastError( ERROR_INVALID_PARAMETER
);
1398 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1399 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1401 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1403 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1404 heap
, flags
, (DWORD
)ptr
, ret
);
1409 /***********************************************************************
1410 * HeapValidate (KERNEL32.343)
1411 * Validates a specified heap.
1420 BOOL STDCALL
RtlValidateHeap(
1421 HANDLE heap
, /* [in] Handle to the heap */
1422 DWORD flags
, /* [in] Bit flags that control access during operation */
1423 PVOID block
/* [in] Optional pointer to memory block to validate */
1426 return HEAP_IsRealArena( heap
, flags
, block
, QUIET
);
1430 /***********************************************************************
1431 * HeapWalk (KERNEL32.344)
1432 * Enumerates the memory blocks in a specified heap.
1433 * See HEAP_Dump() for info on heap structure.
1436 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
1437 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
1444 BOOL STDCALL
HeapWalk(
1445 HANDLE heap
, /* [in] Handle to heap to enumerate */
1446 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
1448 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1449 SUBHEAP
*sub
, *currentheap
= NULL
;
1452 int region_index
= 0;
1454 if (!heapPtr
|| !entry
)
1456 SetLastError(ERROR_INVALID_PARAMETER
);
1460 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1462 /* set ptr to the next arena to be examined */
1464 if (!entry
->lpData
) /* first call (init) ? */
1466 TRACE("begin walking of heap 0x%08x.\n", heap
);
1467 /*HEAP_Dump(heapPtr);*/
1468 currentheap
= &heapPtr
->subheap
;
1469 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1473 ptr
= entry
->lpData
;
1474 sub
= &heapPtr
->subheap
;
1477 if (((char *)ptr
>= (char *)sub
) &&
1478 ((char *)ptr
< (char *)sub
+ sub
->size
))
1486 if (currentheap
== NULL
)
1488 ERR("no matching subheap found, shouldn't happen !\n");
1489 SetLastError(ERROR_NO_MORE_ITEMS
);
1493 ptr
+= entry
->cbData
; /* point to next arena */
1494 if (ptr
> (char *)currentheap
+ currentheap
->size
- 1)
1495 { /* proceed with next subheap */
1496 if (!(currentheap
= currentheap
->next
))
1497 { /* successfully finished */
1498 TRACE("end reached.\n");
1499 SetLastError(ERROR_NO_MORE_ITEMS
);
1502 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1507 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1509 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1511 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1513 entry
->lpData
= pArena
+ 1;
1514 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1515 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1516 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1520 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1522 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1524 entry
->lpData
= pArena
+ 1;
1525 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1526 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1527 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1528 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1529 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1532 entry
->iRegionIndex
= region_index
;
1534 /* first element of heap ? */
1535 if (ptr
== (char *)(currentheap
+ currentheap
->headerSize
))
1537 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1538 entry
->Foo
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1539 entry
->Foo
.Region
.dwUnCommittedSize
=
1540 currentheap
->size
- currentheap
->commitSize
;
1541 entry
->Foo
.Region
.lpFirstBlock
= /* first valid block */
1542 currentheap
+ currentheap
->headerSize
;
1543 entry
->Foo
.Region
.lpLastBlock
= /* first invalid block */
1544 currentheap
+ currentheap
->size
;
1549 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1556 RtlInitializeHeapManager(VOID
)
1560 Peb
= NtCurrentPeb();
1562 Peb
->NumberOfHeaps
= 0;
1563 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(HANDLE
);
1564 Peb
->ProcessHeaps
= (PVOID
)Peb
+ sizeof(PEB
);
1566 RtlInitializeCriticalSection(&RtlpProcessHeapsListLock
);
1571 RtlEnumProcessHeaps(DWORD
STDCALL(*func
)(void*,LONG
),
1574 NTSTATUS Status
= STATUS_SUCCESS
;
1577 RtlEnterCriticalSection(&RtlpProcessHeapsListLock
);
1579 for (i
= 0; i
< NtCurrentPeb()->NumberOfHeaps
; i
++)
1581 Status
= func(NtCurrentPeb()->ProcessHeaps
[i
],lParam
);
1582 if (!NT_SUCCESS(Status
))
1586 RtlLeaveCriticalSection(&RtlpProcessHeapsListLock
);
1593 RtlGetProcessHeaps(ULONG HeapCount
,
1598 RtlEnterCriticalSection(&RtlpProcessHeapsListLock
);
1600 if (NtCurrentPeb()->NumberOfHeaps
<= HeapCount
)
1602 Result
= NtCurrentPeb()->NumberOfHeaps
;
1604 NtCurrentPeb()->ProcessHeaps
,
1605 Result
* sizeof(HANDLE
));
1608 RtlLeaveCriticalSection (&RtlpProcessHeapsListLock
);
1615 RtlValidateProcessHeaps(VOID
)
1618 BOOLEAN Result
= TRUE
;
1622 HeapCount
= RtlGetProcessHeaps(128, Heaps
);
1623 for (i
= 0; i
< HeapCount
; i
++)
1625 if (!RtlValidateHeap(Heaps
[i
], 0, NULL
))