Moved heap functions to ntdll.
[wine/dcerpc.git] / dlls / ntdll / heap.c
blobac177e21a32831c8368622e50570c3c8c25b286e
1 /*
2 * Win32 heap functions
4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include "config.h"
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
15 #include "ntddk.h"
16 #include "wine/winbase16.h"
17 #include "winbase.h"
18 #include "winerror.h"
19 #include "winnt.h"
20 #include "heap.h"
21 #include "thread.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(heap);
26 /* Note: the heap data structures are based on what Pietrek describes in his
27 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
28 * the same, but could be easily adapted if it turns out some programs
29 * require it.
32 typedef struct tagARENA_INUSE
34 DWORD size; /* Block size; must be the first field */
35 DWORD magic; /* Magic number */
36 } ARENA_INUSE;
38 typedef struct tagARENA_FREE
40 DWORD size; /* Block size; must be the first field */
41 DWORD magic; /* Magic number */
42 struct tagARENA_FREE *next; /* Next free arena */
43 struct tagARENA_FREE *prev; /* Prev free arena */
44 } ARENA_FREE;
46 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
47 #define ARENA_FLAG_PREV_FREE 0x00000002
48 #define ARENA_SIZE_MASK (~3)
49 #define ARENA_INUSE_MAGIC 0x44455355 /* Value for arena 'magic' field */
50 #define ARENA_FREE_MAGIC 0x45455246 /* Value for arena 'magic' field */
52 #define ARENA_INUSE_FILLER 0x55
53 #define ARENA_FREE_FILLER 0xaa
55 #define QUIET 1 /* Suppress messages */
56 #define NOISY 0 /* Report all errors */
58 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
60 /* Max size of the blocks on the free lists */
61 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
63 0x20, 0x80, 0x200, ~0UL
66 typedef struct
68 DWORD size;
69 ARENA_FREE arena;
70 } FREE_LIST_ENTRY;
72 struct tagHEAP;
74 typedef struct tagSUBHEAP
76 DWORD size; /* Size of the whole sub-heap */
77 DWORD commitSize; /* Committed size of the sub-heap */
78 DWORD headerSize; /* Size of the heap header */
79 struct tagSUBHEAP *next; /* Next sub-heap */
80 struct tagHEAP *heap; /* Main heap structure */
81 DWORD magic; /* Magic number */
82 } SUBHEAP;
84 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
86 typedef struct tagHEAP
88 SUBHEAP subheap; /* First sub-heap */
89 struct tagHEAP *next; /* Next heap for this process */
90 CRITICAL_SECTION critSection; /* Critical section for serialization */
91 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
92 DWORD flags; /* Heap flags */
93 DWORD magic; /* Magic number */
94 } HEAP;
96 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
98 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
99 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
100 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
102 static HANDLE processHeap; /* main process heap */
104 static HEAP *firstHeap; /* head of secondary heaps list */
106 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
108 /* SetLastError for ntdll */
109 inline static void set_status( NTSTATUS status )
111 #if defined(__i386__) && defined(__GNUC__)
112 /* in this case SetLastError is an inline function so we can use it */
113 SetLastError( RtlNtStatusToDosError( status ) );
114 #else
115 /* cannot use SetLastError, do it manually */
116 NtCurrentTeb()->last_error = RtlNtStatusToDosError( status );
117 #endif
120 /* set the process main heap */
121 static void set_process_heap( HANDLE heap )
123 HANDLE *pdb = (HANDLE *)NtCurrentTeb()->process;
124 pdb[0x18 / sizeof(HANDLE)] = heap; /* heap is at offset 0x18 in pdb */
125 processHeap = heap;
129 /***********************************************************************
130 * HEAP_Dump
132 void HEAP_Dump( HEAP *heap )
134 int i;
135 SUBHEAP *subheap;
136 char *ptr;
138 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
139 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
140 (DWORD)heap->next, (DWORD)&heap->subheap );
141 subheap = &heap->subheap;
142 while (subheap->next)
144 DPRINTF( " -> %08lx", (DWORD)subheap->next );
145 subheap = subheap->next;
148 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
149 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
150 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
151 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
152 (DWORD)heap->freeList[i].arena.prev,
153 (DWORD)heap->freeList[i].arena.next );
155 subheap = &heap->subheap;
156 while (subheap)
158 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
159 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
160 (DWORD)subheap, subheap->size, subheap->commitSize );
162 DPRINTF( "\n Block Stat Size Id\n" );
163 ptr = (char*)subheap + subheap->headerSize;
164 while (ptr < (char *)subheap + subheap->size)
166 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
168 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
169 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
170 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
171 (DWORD)pArena->prev, (DWORD)pArena->next);
172 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
173 arenaSize += sizeof(ARENA_FREE);
174 freeSize += pArena->size & ARENA_SIZE_MASK;
176 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
178 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
179 DPRINTF( "%08lx Used %08lx back=%08lx\n",
180 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK, *((DWORD *)pArena - 1) );
181 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
182 arenaSize += sizeof(ARENA_INUSE);
183 usedSize += pArena->size & ARENA_SIZE_MASK;
185 else
187 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
188 DPRINTF( "%08lx used %08lx\n",
189 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK );
190 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
191 arenaSize += sizeof(ARENA_INUSE);
192 usedSize += pArena->size & ARENA_SIZE_MASK;
195 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
196 subheap->size, subheap->commitSize, freeSize, usedSize,
197 arenaSize, (arenaSize * 100) / subheap->size );
198 subheap = subheap->next;
203 /***********************************************************************
204 * HEAP_GetPtr
205 * RETURNS
206 * Pointer to the heap
207 * NULL: Failure
209 static HEAP *HEAP_GetPtr(
210 HANDLE heap /* [in] Handle to the heap */
212 HEAP *heapPtr = (HEAP *)heap;
213 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
215 ERR("Invalid heap %08x!\n", heap );
216 return NULL;
218 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
220 HEAP_Dump( heapPtr );
221 assert( FALSE );
222 return NULL;
224 return heapPtr;
228 /***********************************************************************
229 * HEAP_InsertFreeBlock
231 * Insert a free block into the free list.
233 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
235 FREE_LIST_ENTRY *pEntry = heap->freeList;
236 while (pEntry->size < pArena->size) pEntry++;
237 pArena->size |= ARENA_FLAG_FREE;
238 pArena->next = pEntry->arena.next;
239 pArena->next->prev = pArena;
240 pArena->prev = &pEntry->arena;
241 pEntry->arena.next = pArena;
245 /***********************************************************************
246 * HEAP_FindSubHeap
247 * Find the sub-heap containing a given address.
249 * RETURNS
250 * Pointer: Success
251 * NULL: Failure
253 static SUBHEAP *HEAP_FindSubHeap(
254 HEAP *heap, /* [in] Heap pointer */
255 LPCVOID ptr /* [in] Address */
257 SUBHEAP *sub = &heap->subheap;
258 while (sub)
260 if (((char *)ptr >= (char *)sub) &&
261 ((char *)ptr < (char *)sub + sub->size)) return sub;
262 sub = sub->next;
264 return NULL;
268 /***********************************************************************
269 * HEAP_Commit
271 * Make sure the heap storage is committed up to (not including) ptr.
273 static inline BOOL HEAP_Commit( SUBHEAP *subheap, void *ptr )
275 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
276 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
277 if (size > subheap->size) size = subheap->size;
278 if (size <= subheap->commitSize) return TRUE;
279 if (!VirtualAlloc( (char *)subheap + subheap->commitSize,
280 size - subheap->commitSize, MEM_COMMIT,
281 PAGE_EXECUTE_READWRITE))
283 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
284 size - subheap->commitSize,
285 (DWORD)((char *)subheap + subheap->commitSize),
286 (DWORD)subheap->heap );
287 return FALSE;
289 subheap->commitSize = size;
290 return TRUE;
294 /***********************************************************************
295 * HEAP_Decommit
297 * If possible, decommit the heap storage from (including) 'ptr'.
299 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
301 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
302 /* round to next block and add one full block */
303 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
304 if (size >= subheap->commitSize) return TRUE;
305 if (!VirtualFree( (char *)subheap + size,
306 subheap->commitSize - size, MEM_DECOMMIT ))
308 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
309 subheap->commitSize - size,
310 (DWORD)((char *)subheap + size),
311 (DWORD)subheap->heap );
312 return FALSE;
314 subheap->commitSize = size;
315 return TRUE;
319 /***********************************************************************
320 * HEAP_CreateFreeBlock
322 * Create a free block at a specified address. 'size' is the size of the
323 * whole block, including the new arena.
325 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
327 ARENA_FREE *pFree;
329 /* Create a free arena */
331 pFree = (ARENA_FREE *)ptr;
332 pFree->magic = ARENA_FREE_MAGIC;
334 /* If debugging, erase the freed block content */
336 if (TRACE_ON(heap))
338 char *pEnd = (char *)ptr + size;
339 if (pEnd > (char *)subheap + subheap->commitSize)
340 pEnd = (char *)subheap + subheap->commitSize;
341 if (pEnd > (char *)(pFree + 1))
342 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
345 /* Check if next block is free also */
347 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
348 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
350 /* Remove the next arena from the free list */
351 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
352 pNext->next->prev = pNext->prev;
353 pNext->prev->next = pNext->next;
354 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
355 if (TRACE_ON(heap))
356 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
359 /* Set the next block PREV_FREE flag and pointer */
361 if ((char *)ptr + size < (char *)subheap + subheap->size)
363 DWORD *pNext = (DWORD *)((char *)ptr + size);
364 *pNext |= ARENA_FLAG_PREV_FREE;
365 *(ARENA_FREE **)(pNext - 1) = pFree;
368 /* Last, insert the new block into the free list */
370 pFree->size = size - sizeof(*pFree);
371 HEAP_InsertFreeBlock( subheap->heap, pFree );
375 /***********************************************************************
376 * HEAP_MakeInUseBlockFree
378 * Turn an in-use block into a free block. Can also decommit the end of
379 * the heap, and possibly even free the sub-heap altogether.
381 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
383 ARENA_FREE *pFree;
384 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
386 /* Check if we can merge with previous block */
388 if (pArena->size & ARENA_FLAG_PREV_FREE)
390 pFree = *((ARENA_FREE **)pArena - 1);
391 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
392 /* Remove it from the free list */
393 pFree->next->prev = pFree->prev;
394 pFree->prev->next = pFree->next;
396 else pFree = (ARENA_FREE *)pArena;
398 /* Create a free block */
400 HEAP_CreateFreeBlock( subheap, pFree, size );
401 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
402 if ((char *)pFree + size < (char *)subheap + subheap->size)
403 return; /* Not the last block, so nothing more to do */
405 /* Free the whole sub-heap if it's empty and not the original one */
407 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
408 (subheap != &subheap->heap->subheap))
410 SUBHEAP *pPrev = &subheap->heap->subheap;
411 /* Remove the free block from the list */
412 pFree->next->prev = pFree->prev;
413 pFree->prev->next = pFree->next;
414 /* Remove the subheap from the list */
415 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
416 if (pPrev) pPrev->next = subheap->next;
417 /* Free the memory */
418 subheap->magic = 0;
419 VirtualFree( subheap, 0, MEM_RELEASE );
420 return;
423 /* Decommit the end of the heap */
425 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
429 /***********************************************************************
430 * HEAP_ShrinkBlock
432 * Shrink an in-use block.
434 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
436 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
438 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
439 (pArena->size & ARENA_SIZE_MASK) - size );
440 /* assign size plus previous arena flags */
441 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
443 else
445 /* Turn off PREV_FREE flag in next block */
446 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
447 if (pNext < (char *)subheap + subheap->size)
448 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
452 /***********************************************************************
453 * HEAP_InitSubHeap
455 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
456 DWORD commitSize, DWORD totalSize )
458 SUBHEAP *subheap = (SUBHEAP *)address;
459 FREE_LIST_ENTRY *pEntry;
460 int i;
462 /* Commit memory */
464 if (flags & HEAP_SHARED)
465 commitSize = totalSize; /* always commit everything in a shared heap */
466 if (!VirtualAlloc(address, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
468 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
469 commitSize, (DWORD)address );
470 return FALSE;
473 /* Fill the sub-heap structure */
475 subheap->heap = heap;
476 subheap->size = totalSize;
477 subheap->commitSize = commitSize;
478 subheap->magic = SUBHEAP_MAGIC;
480 if ( subheap != (SUBHEAP *)heap )
482 /* If this is a secondary subheap, insert it into list */
484 subheap->headerSize = sizeof(SUBHEAP);
485 subheap->next = heap->subheap.next;
486 heap->subheap.next = subheap;
488 else
490 /* If this is a primary subheap, initialize main heap */
492 subheap->headerSize = sizeof(HEAP);
493 subheap->next = NULL;
494 heap->next = NULL;
495 heap->flags = flags;
496 heap->magic = HEAP_MAGIC;
498 /* Build the free lists */
500 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
502 pEntry->size = HEAP_freeListSizes[i];
503 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
504 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
505 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
506 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
507 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
508 pEntry->arena.magic = ARENA_FREE_MAGIC;
511 /* Initialize critical section */
513 RtlInitializeCriticalSection( &heap->critSection );
516 /* Create the first free block */
518 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
519 subheap->size - subheap->headerSize );
521 return TRUE;
524 /***********************************************************************
525 * HEAP_CreateSubHeap
527 * Create a sub-heap of the given size.
528 * If heap == NULL, creates a main heap.
530 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
531 DWORD commitSize, DWORD totalSize )
533 LPVOID address = base;
535 if (!address)
537 /* round-up sizes on a 64K boundary */
538 totalSize = (totalSize + 0xffff) & 0xffff0000;
539 commitSize = (commitSize + 0xffff) & 0xffff0000;
540 if (!commitSize) commitSize = 0x10000;
541 if (totalSize < commitSize) totalSize = commitSize;
543 /* allocate the memory block */
544 if (!(address = VirtualAlloc( NULL, totalSize, MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
546 WARN("Could not VirtualAlloc %08lx bytes\n",
547 totalSize );
548 return NULL;
552 /* Initialize subheap */
554 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
555 address, flags, commitSize, totalSize ))
557 if (!base) VirtualFree( address, 0, MEM_RELEASE );
558 return NULL;
561 return (SUBHEAP *)address;
565 /***********************************************************************
566 * HEAP_FindFreeBlock
568 * Find a free block at least as large as the requested size, and make sure
569 * the requested size is committed.
571 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
572 SUBHEAP **ppSubHeap )
574 SUBHEAP *subheap;
575 ARENA_FREE *pArena;
576 FREE_LIST_ENTRY *pEntry = heap->freeList;
578 /* Find a suitable free list, and in it find a block large enough */
580 while (pEntry->size < size) pEntry++;
581 pArena = pEntry->arena.next;
582 while (pArena != &heap->freeList[0].arena)
584 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
585 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
586 if (arena_size >= size)
588 subheap = HEAP_FindSubHeap( heap, pArena );
589 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
590 + size + HEAP_MIN_BLOCK_SIZE))
591 return NULL;
592 *ppSubHeap = subheap;
593 return pArena;
595 pArena = pArena->next;
598 /* If no block was found, attempt to grow the heap */
600 if (!(heap->flags & HEAP_GROWABLE))
602 WARN("Not enough space in heap %08lx for %08lx bytes\n",
603 (DWORD)heap, size );
604 return NULL;
606 /* make sure that we have a big enough size *committed* to fit another
607 * last free arena in !
608 * So just one heap struct, one first free arena which will eventually
609 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
610 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
611 size += sizeof(SUBHEAP) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
612 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
613 max( HEAP_DEF_SIZE, size ) )))
614 return NULL;
616 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
617 (DWORD)subheap, size, (DWORD)heap );
619 *ppSubHeap = subheap;
620 return (ARENA_FREE *)(subheap + 1);
624 /***********************************************************************
625 * HEAP_IsValidArenaPtr
627 * Check that the pointer is inside the range possible for arenas.
629 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
631 int i;
632 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
633 if (!subheap) return FALSE;
634 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
635 if (subheap != &heap->subheap) return FALSE;
636 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
637 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
638 return FALSE;
642 /***********************************************************************
643 * HEAP_ValidateFreeArena
645 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
647 char *heapEnd = (char *)subheap + subheap->size;
649 #if !defined(ALLOW_UNALIGNED_ACCESS)
650 /* Check for unaligned pointers */
651 if ( (long)pArena % sizeof(void *) != 0 )
653 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
654 (DWORD)subheap->heap, (DWORD)pArena );
655 return FALSE;
657 #endif
659 /* Check magic number */
660 if (pArena->magic != ARENA_FREE_MAGIC)
662 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
663 (DWORD)subheap->heap, (DWORD)pArena );
664 return FALSE;
666 /* Check size flags */
667 if (!(pArena->size & ARENA_FLAG_FREE) ||
668 (pArena->size & ARENA_FLAG_PREV_FREE))
670 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
671 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
673 /* Check arena size */
674 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
676 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
677 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
678 return FALSE;
680 /* Check that next pointer is valid */
681 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
683 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
684 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
685 return FALSE;
687 /* Check that next arena is free */
688 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
689 (pArena->next->magic != ARENA_FREE_MAGIC))
691 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
692 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
693 return FALSE;
695 /* Check that prev pointer is valid */
696 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
698 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
699 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
700 return FALSE;
702 /* Check that prev arena is free */
703 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
704 (pArena->prev->magic != ARENA_FREE_MAGIC))
706 /* this often means that the prev arena got overwritten
707 * by a memory write before that prev arena */
708 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
709 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
710 return FALSE;
712 /* Check that next block has PREV_FREE flag */
713 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
715 if (!(*(DWORD *)((char *)(pArena + 1) +
716 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
718 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
719 (DWORD)subheap->heap, (DWORD)pArena );
720 return FALSE;
722 /* Check next block back pointer */
723 if (*((ARENA_FREE **)((char *)(pArena + 1) +
724 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
726 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
727 (DWORD)subheap->heap, (DWORD)pArena,
728 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
729 return FALSE;
732 return TRUE;
736 /***********************************************************************
737 * HEAP_ValidateInUseArena
739 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
741 char *heapEnd = (char *)subheap + subheap->size;
743 #if !defined(ALLOW_UNALIGNED_ACCESS)
744 /* Check for unaligned pointers */
745 if ( (long)pArena % sizeof(void *) != 0 )
747 if ( quiet == NOISY )
749 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
750 (DWORD)subheap->heap, (DWORD)pArena );
751 if ( TRACE_ON(heap) )
752 HEAP_Dump( subheap->heap );
754 else if ( WARN_ON(heap) )
756 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
757 (DWORD)subheap->heap, (DWORD)pArena );
758 if ( TRACE_ON(heap) )
759 HEAP_Dump( subheap->heap );
761 return FALSE;
763 #endif
765 /* Check magic number */
766 if (pArena->magic != ARENA_INUSE_MAGIC)
768 if (quiet == NOISY) {
769 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
770 (DWORD)subheap->heap, (DWORD)pArena );
771 if (TRACE_ON(heap))
772 HEAP_Dump( subheap->heap );
773 } else if (WARN_ON(heap)) {
774 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
775 (DWORD)subheap->heap, (DWORD)pArena );
776 if (TRACE_ON(heap))
777 HEAP_Dump( subheap->heap );
779 return FALSE;
781 /* Check size flags */
782 if (pArena->size & ARENA_FLAG_FREE)
784 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
785 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
787 /* Check arena size */
788 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
790 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
791 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
792 return FALSE;
794 /* Check next arena PREV_FREE flag */
795 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
796 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
798 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
799 (DWORD)subheap->heap, (DWORD)pArena );
800 return FALSE;
802 /* Check prev free arena */
803 if (pArena->size & ARENA_FLAG_PREV_FREE)
805 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
806 /* Check prev pointer */
807 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
809 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
810 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
811 return FALSE;
813 /* Check that prev arena is free */
814 if (!(pPrev->size & ARENA_FLAG_FREE) ||
815 (pPrev->magic != ARENA_FREE_MAGIC))
817 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
818 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
819 return FALSE;
821 /* Check that prev arena is really the previous block */
822 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
824 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
825 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
826 return FALSE;
829 return TRUE;
833 /***********************************************************************
834 * HEAP_IsRealArena [Internal]
835 * Validates a block is a valid arena.
837 * RETURNS
838 * TRUE: Success
839 * FALSE: Failure
841 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
842 DWORD flags, /* [in] Bit flags that control access during operation */
843 LPCVOID block, /* [in] Optional pointer to memory block to validate */
844 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
845 * does not complain */
847 SUBHEAP *subheap;
848 BOOL ret = TRUE;
850 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
852 ERR("Invalid heap %p!\n", heapPtr );
853 return FALSE;
856 flags &= HEAP_NO_SERIALIZE;
857 flags |= heapPtr->flags;
858 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
859 if (!(flags & HEAP_NO_SERIALIZE))
860 RtlEnterCriticalSection( &heapPtr->critSection );
862 if (block)
864 /* Only check this single memory block */
866 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
867 ((char *)block < (char *)subheap + subheap->headerSize
868 + sizeof(ARENA_INUSE)))
870 if (quiet == NOISY)
871 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
872 else if (WARN_ON(heap))
873 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
874 ret = FALSE;
875 } else
876 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
878 if (!(flags & HEAP_NO_SERIALIZE))
879 RtlLeaveCriticalSection( &heapPtr->critSection );
880 return ret;
883 subheap = &heapPtr->subheap;
884 while (subheap && ret)
886 char *ptr = (char *)subheap + subheap->headerSize;
887 while (ptr < (char *)subheap + subheap->size)
889 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
891 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
892 ret = FALSE;
893 break;
895 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
897 else
899 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
900 ret = FALSE;
901 break;
903 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
906 subheap = subheap->next;
909 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
910 return ret;
914 /***********************************************************************
915 * RtlCreateHeap (NTDLL.@)
917 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
918 PVOID unknown, PRTL_HEAP_DEFINITION definition )
920 SUBHEAP *subheap;
922 /* Allocate the heap block */
924 if (!totalSize)
926 totalSize = HEAP_DEF_SIZE;
927 flags |= HEAP_GROWABLE;
929 /* round up sizes */
930 totalSize = (totalSize + 0xffff) & 0xffff0000;
931 commitSize = (commitSize + 0xffff) & 0xffff0000;
932 if (!commitSize) commitSize = 0x10000;
933 if (totalSize < commitSize) totalSize = commitSize;
935 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
937 /* link it into the per-process heap list */
938 if (processHeap)
940 HEAP *heapPtr = subheap->heap;
941 RtlLockHeap( processHeap );
942 heapPtr->next = firstHeap;
943 firstHeap = heapPtr;
944 RtlUnlockHeap( processHeap );
946 else /* assume the first heap we create is the process main heap */
948 set_process_heap( (HANDLE)subheap->heap );
950 return (HANDLE)subheap;
954 /***********************************************************************
955 * RtlDestroyHeap (NTDLL.@)
957 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
959 HEAP *heapPtr = HEAP_GetPtr( heap );
960 SUBHEAP *subheap;
962 TRACE("%08x\n", heap );
963 if (!heapPtr) return heap;
965 if (heap == processHeap) return heap; /* cannot delete the main process heap */
966 else /* remove it from the per-process list */
968 HEAP **pptr;
969 RtlLockHeap( processHeap );
970 pptr = &firstHeap;
971 while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
972 if (*pptr) *pptr = (*pptr)->next;
973 RtlUnlockHeap( processHeap );
976 RtlDeleteCriticalSection( &heapPtr->critSection );
977 subheap = &heapPtr->subheap;
978 while (subheap)
980 SUBHEAP *next = subheap->next;
981 VirtualFree( subheap, 0, MEM_RELEASE );
982 subheap = next;
984 return 0;
988 /***********************************************************************
989 * RtlAllocateHeap (NTDLL.@)
991 * NOTE: does not set last error.
993 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
995 ARENA_FREE *pArena;
996 ARENA_INUSE *pInUse;
997 SUBHEAP *subheap;
998 HEAP *heapPtr = HEAP_GetPtr( heap );
1000 /* Validate the parameters */
1002 if (!heapPtr) return NULL;
1003 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1004 flags |= heapPtr->flags;
1005 size = (size + 3) & ~3;
1006 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1008 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1009 /* Locate a suitable free block */
1011 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1013 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1014 heap, flags, size );
1015 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1016 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1017 return NULL;
1020 /* Remove the arena from the free list */
1022 pArena->next->prev = pArena->prev;
1023 pArena->prev->next = pArena->next;
1025 /* Build the in-use arena */
1027 pInUse = (ARENA_INUSE *)pArena;
1029 /* in-use arena is smaller than free arena,
1030 * so we have to add the difference to the size */
1031 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1032 pInUse->magic = ARENA_INUSE_MAGIC;
1034 /* Shrink the block */
1036 HEAP_ShrinkBlock( subheap, pInUse, size );
1038 if (flags & HEAP_ZERO_MEMORY)
1039 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1040 else if (TRACE_ON(heap))
1041 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1043 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1045 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1046 heap, flags, size, (DWORD)(pInUse + 1) );
1047 return (LPVOID)(pInUse + 1);
1051 /***********************************************************************
1052 * RtlFreeHeap (NTDLL.@)
1054 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1056 ARENA_INUSE *pInUse;
1057 SUBHEAP *subheap;
1058 HEAP *heapPtr = HEAP_GetPtr( heap );
1060 /* Validate the parameters */
1062 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1063 if (!heapPtr)
1065 set_status( STATUS_INVALID_HANDLE );
1066 return FALSE;
1069 flags &= HEAP_NO_SERIALIZE;
1070 flags |= heapPtr->flags;
1071 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1072 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1074 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1075 set_status( STATUS_INVALID_PARAMETER );
1076 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1077 heap, flags, (DWORD)ptr );
1078 return FALSE;
1081 /* Turn the block into a free block */
1083 pInUse = (ARENA_INUSE *)ptr - 1;
1084 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1085 HEAP_MakeInUseBlockFree( subheap, pInUse );
1087 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1089 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1090 heap, flags, (DWORD)ptr );
1091 return TRUE;
1095 /***********************************************************************
1096 * RtlReAllocateHeap (NTDLL.@)
1098 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1100 ARENA_INUSE *pArena;
1101 DWORD oldSize;
1102 HEAP *heapPtr;
1103 SUBHEAP *subheap;
1105 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1106 if (!(heapPtr = HEAP_GetPtr( heap )))
1108 set_status( STATUS_INVALID_HANDLE );
1109 return FALSE;
1112 /* Validate the parameters */
1114 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1115 HEAP_REALLOC_IN_PLACE_ONLY;
1116 flags |= heapPtr->flags;
1117 size = (size + 3) & ~3;
1118 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1120 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1121 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1123 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1124 set_status( STATUS_INVALID_PARAMETER );
1125 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1126 heap, flags, (DWORD)ptr, size );
1127 return NULL;
1130 /* Check if we need to grow the block */
1132 pArena = (ARENA_INUSE *)ptr - 1;
1133 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1134 oldSize = (pArena->size & ARENA_SIZE_MASK);
1135 if (size > oldSize)
1137 char *pNext = (char *)(pArena + 1) + oldSize;
1138 if ((pNext < (char *)subheap + subheap->size) &&
1139 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1140 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1142 /* The next block is free and large enough */
1143 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1144 pFree->next->prev = pFree->prev;
1145 pFree->prev->next = pFree->next;
1146 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1147 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1148 + size + HEAP_MIN_BLOCK_SIZE))
1150 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1151 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1152 set_status( STATUS_NO_MEMORY );
1153 return NULL;
1155 HEAP_ShrinkBlock( subheap, pArena, size );
1157 else /* Do it the hard way */
1159 ARENA_FREE *pNew;
1160 ARENA_INUSE *pInUse;
1161 SUBHEAP *newsubheap;
1163 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1164 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1166 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1167 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1168 set_status( STATUS_NO_MEMORY );
1169 return NULL;
1172 /* Build the in-use arena */
1174 pNew->next->prev = pNew->prev;
1175 pNew->prev->next = pNew->next;
1176 pInUse = (ARENA_INUSE *)pNew;
1177 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1178 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1179 pInUse->magic = ARENA_INUSE_MAGIC;
1180 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1181 memcpy( pInUse + 1, pArena + 1, oldSize );
1183 /* Free the previous block */
1185 HEAP_MakeInUseBlockFree( subheap, pArena );
1186 subheap = newsubheap;
1187 pArena = pInUse;
1190 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1192 /* Clear the extra bytes if needed */
1194 if (size > oldSize)
1196 if (flags & HEAP_ZERO_MEMORY)
1197 memset( (char *)(pArena + 1) + oldSize, 0,
1198 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1199 else if (TRACE_ON(heap))
1200 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1201 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1204 /* Return the new arena */
1206 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1208 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1209 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1210 return (LPVOID)(pArena + 1);
1214 /***********************************************************************
1215 * RtlCompactHeap (NTDLL.@)
1217 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1219 FIXME( "stub\n" );
1220 return 0;
1224 /***********************************************************************
1225 * RtlLockHeap (NTDLL.@)
1227 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1229 HEAP *heapPtr = HEAP_GetPtr( heap );
1230 if (!heapPtr) return FALSE;
1231 RtlEnterCriticalSection( &heapPtr->critSection );
1232 return TRUE;
1236 /***********************************************************************
1237 * RtlUnlockHeap (NTDLL.@)
1239 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1241 HEAP *heapPtr = HEAP_GetPtr( heap );
1242 if (!heapPtr) return FALSE;
1243 RtlLeaveCriticalSection( &heapPtr->critSection );
1244 return TRUE;
1248 /***********************************************************************
1249 * RtlSizeHeap (NTDLL.@)
1251 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1253 DWORD ret;
1254 HEAP *heapPtr = HEAP_GetPtr( heap );
1256 if (!heapPtr)
1258 set_status( STATUS_INVALID_HANDLE );
1259 return (ULONG)-1;
1261 flags &= HEAP_NO_SERIALIZE;
1262 flags |= heapPtr->flags;
1263 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1264 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1266 set_status( STATUS_INVALID_PARAMETER );
1267 ret = (ULONG)-1;
1269 else
1271 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1272 ret = pArena->size & ARENA_SIZE_MASK;
1274 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1276 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1277 heap, flags, (DWORD)ptr, ret );
1278 return ret;
1282 /***********************************************************************
1283 * RtlValidateHeap (NTDLL.@)
1285 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, PCVOID block )
1287 HEAP *heapPtr = HEAP_GetPtr( heap );
1288 if (!heapPtr) return FALSE;
1289 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1293 /***********************************************************************
1294 * RtlWalkHeap (NTDLL.@)
1296 * FIXME: the PROCESS_HEAP_ENTRY flag values seem different between this
1297 * function and HeapWalk. To be checked.
1299 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1301 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1302 HEAP *heapPtr = HEAP_GetPtr(heap);
1303 SUBHEAP *sub, *currentheap = NULL;
1304 NTSTATUS ret;
1305 char *ptr;
1306 int region_index = 0;
1308 FIXME( "not fully compatible\n" );
1310 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1312 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) EnterCriticalSection( &heapPtr->critSection );
1314 /* set ptr to the next arena to be examined */
1316 if (!entry->lpData) /* first call (init) ? */
1318 TRACE("begin walking of heap 0x%08x.\n", heap);
1319 currentheap = &heapPtr->subheap;
1320 ptr = (char*)currentheap + currentheap->headerSize;
1322 else
1324 ptr = entry->lpData;
1325 sub = &heapPtr->subheap;
1326 while (sub)
1328 if (((char *)ptr >= (char *)sub) &&
1329 ((char *)ptr < (char *)sub + sub->size))
1331 currentheap = sub;
1332 break;
1334 sub = sub->next;
1335 region_index++;
1337 if (currentheap == NULL)
1339 ERR("no matching subheap found, shouldn't happen !\n");
1340 ret = STATUS_NO_MORE_ENTRIES;
1341 goto HW_end;
1344 ptr += entry->cbData; /* point to next arena */
1345 if (ptr > (char *)currentheap + currentheap->size - 1)
1346 { /* proceed with next subheap */
1347 if (!(currentheap = currentheap->next))
1348 { /* successfully finished */
1349 TRACE("end reached.\n");
1350 ret = STATUS_NO_MORE_ENTRIES;
1351 goto HW_end;
1353 ptr = (char*)currentheap + currentheap->headerSize;
1357 entry->wFlags = 0;
1358 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1360 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1362 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1364 entry->lpData = pArena + 1;
1365 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1366 entry->cbOverhead = sizeof(ARENA_FREE);
1367 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1369 else
1371 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1373 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1375 entry->lpData = pArena + 1;
1376 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1377 entry->cbOverhead = sizeof(ARENA_INUSE);
1378 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1379 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1380 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1383 entry->iRegionIndex = region_index;
1385 /* first element of heap ? */
1386 if (ptr == (char *)(currentheap + currentheap->headerSize))
1388 entry->wFlags |= PROCESS_HEAP_REGION;
1389 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1390 entry->u.Region.dwUnCommittedSize =
1391 currentheap->size - currentheap->commitSize;
1392 entry->u.Region.lpFirstBlock = /* first valid block */
1393 currentheap + currentheap->headerSize;
1394 entry->u.Region.lpLastBlock = /* first invalid block */
1395 currentheap + currentheap->size;
1397 ret = STATUS_SUCCESS;
1399 HW_end:
1400 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) LeaveCriticalSection( &heapPtr->critSection );
1401 return ret;
1405 /***********************************************************************
1406 * RtlGetProcessHeaps (NTDLL.@)
1408 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1410 DWORD total;
1411 HEAP *ptr;
1413 if (!processHeap) return 0; /* should never happen */
1414 total = 1; /* main heap */
1415 RtlLockHeap( processHeap );
1416 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1417 if (total <= count)
1419 *heaps++ = (HANDLE)processHeap;
1420 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1422 RtlUnlockHeap( processHeap );
1423 return total;