4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(heap);
40 /* Note: the heap data structures are based on what Pietrek describes in his
41 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
42 * the same, but could be easily adapted if it turns out some programs
46 typedef struct tagARENA_INUSE
48 DWORD size; /* Block size; must be the first field */
49 DWORD magic; /* Magic number */
52 typedef struct tagARENA_FREE
54 DWORD size; /* Block size; must be the first field */
55 DWORD magic; /* Magic number */
56 struct tagARENA_FREE *next; /* Next free arena */
57 struct tagARENA_FREE *prev; /* Prev free arena */
60 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
61 #define ARENA_FLAG_PREV_FREE 0x00000002
62 #define ARENA_SIZE_MASK (~3)
63 #define ARENA_INUSE_MAGIC 0x44455355 /* Value for arena 'magic' field */
64 #define ARENA_FREE_MAGIC 0x45455246 /* Value for arena 'magic' field */
66 #define ARENA_INUSE_FILLER 0x55
67 #define ARENA_FREE_FILLER 0xaa
69 #define ALIGNMENT 8 /* everything is aligned on 8 byte boundaries */
70 #define ROUND_SIZE(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
72 #define QUIET 1 /* Suppress messages */
73 #define NOISY 0 /* Report all errors */
75 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
77 /* Max size of the blocks on the free lists */
78 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
80 0x20, 0x80, 0x200, ~0UL
91 typedef struct tagSUBHEAP
93 DWORD size; /* Size of the whole sub-heap */
94 DWORD commitSize; /* Committed size of the sub-heap */
95 DWORD headerSize; /* Size of the heap header */
96 struct tagSUBHEAP *next; /* Next sub-heap */
97 struct tagHEAP *heap; /* Main heap structure */
98 DWORD magic; /* Magic number */
101 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
103 typedef struct tagHEAP
105 SUBHEAP subheap; /* First sub-heap */
106 struct tagHEAP *next; /* Next heap for this process */
107 CRITICAL_SECTION critSection; /* Critical section for serialization */
108 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
109 DWORD flags; /* Heap flags */
110 DWORD magic; /* Magic number */
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 */
119 static HANDLE processHeap; /* main process heap */
121 static HEAP *firstHeap; /* head of secondary heaps list */
123 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
125 /* SetLastError for ntdll */
126 inline static void set_status( NTSTATUS status )
128 #if defined(__i386__) && defined(__GNUC__)
129 /* in this case SetLastError is an inline function so we can use it */
130 SetLastError( RtlNtStatusToDosError( status ) );
132 /* cannot use SetLastError, do it manually */
133 NtCurrentTeb()->last_error = RtlNtStatusToDosError( status );
137 /* set the process main heap */
138 static void set_process_heap( HANDLE heap )
140 HANDLE *pdb = (HANDLE *)NtCurrentTeb()->process;
141 pdb[0x18 / sizeof(HANDLE)] = heap; /* heap is at offset 0x18 in pdb */
146 /***********************************************************************
149 void HEAP_Dump( HEAP *heap )
155 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
156 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
157 (DWORD)heap->next, (DWORD)&heap->subheap );
158 subheap = &heap->subheap;
159 while (subheap->next)
161 DPRINTF( " -> %08lx", (DWORD)subheap->next );
162 subheap = subheap->next;
165 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
166 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
167 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
168 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
169 (DWORD)heap->freeList[i].arena.prev,
170 (DWORD)heap->freeList[i].arena.next );
172 subheap = &heap->subheap;
175 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
176 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
177 (DWORD)subheap, subheap->size, subheap->commitSize );
179 DPRINTF( "\n Block Stat Size Id\n" );
180 ptr = (char*)subheap + subheap->headerSize;
181 while (ptr < (char *)subheap + subheap->size)
183 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
185 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
186 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
187 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
188 (DWORD)pArena->prev, (DWORD)pArena->next);
189 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
190 arenaSize += sizeof(ARENA_FREE);
191 freeSize += pArena->size & ARENA_SIZE_MASK;
193 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
195 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
196 DPRINTF( "%08lx Used %08lx back=%08lx\n",
197 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK, *((DWORD *)pArena - 1) );
198 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
199 arenaSize += sizeof(ARENA_INUSE);
200 usedSize += pArena->size & ARENA_SIZE_MASK;
204 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
205 DPRINTF( "%08lx used %08lx\n",
206 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK );
207 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
208 arenaSize += sizeof(ARENA_INUSE);
209 usedSize += pArena->size & ARENA_SIZE_MASK;
212 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
213 subheap->size, subheap->commitSize, freeSize, usedSize,
214 arenaSize, (arenaSize * 100) / subheap->size );
215 subheap = subheap->next;
220 /***********************************************************************
223 * Pointer to the heap
226 static HEAP *HEAP_GetPtr(
227 HANDLE heap /* [in] Handle to the heap */
229 HEAP *heapPtr = (HEAP *)heap;
230 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
232 ERR("Invalid heap %08x!\n", heap );
235 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
237 HEAP_Dump( heapPtr );
245 /***********************************************************************
246 * HEAP_InsertFreeBlock
248 * Insert a free block into the free list.
250 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
252 FREE_LIST_ENTRY *pEntry = heap->freeList;
253 while (pEntry->size < pArena->size) pEntry++;
254 pArena->size |= ARENA_FLAG_FREE;
255 pArena->next = pEntry->arena.next;
256 pArena->next->prev = pArena;
257 pArena->prev = &pEntry->arena;
258 pEntry->arena.next = pArena;
262 /***********************************************************************
264 * Find the sub-heap containing a given address.
270 static SUBHEAP *HEAP_FindSubHeap(
271 HEAP *heap, /* [in] Heap pointer */
272 LPCVOID ptr /* [in] Address */
274 SUBHEAP *sub = &heap->subheap;
277 if (((char *)ptr >= (char *)sub) &&
278 ((char *)ptr < (char *)sub + sub->size)) return sub;
285 /***********************************************************************
288 * Make sure the heap storage is committed up to (not including) ptr.
290 static inline BOOL HEAP_Commit( SUBHEAP *subheap, void *ptr )
292 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;
296 if (!VirtualAlloc( (char *)subheap + subheap->commitSize,
297 size - subheap->commitSize, MEM_COMMIT,
298 PAGE_EXECUTE_READWRITE))
300 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
301 size - subheap->commitSize,
302 (DWORD)((char *)subheap + subheap->commitSize),
303 (DWORD)subheap->heap );
306 subheap->commitSize = size;
311 /***********************************************************************
314 * If possible, decommit the heap storage from (including) 'ptr'.
316 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
318 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
319 /* round to next block and add one full block */
320 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
321 if (size >= subheap->commitSize) return TRUE;
322 if (!VirtualFree( (char *)subheap + size,
323 subheap->commitSize - size, MEM_DECOMMIT ))
325 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
326 subheap->commitSize - size,
327 (DWORD)((char *)subheap + size),
328 (DWORD)subheap->heap );
331 subheap->commitSize = size;
336 /***********************************************************************
337 * HEAP_CreateFreeBlock
339 * Create a free block at a specified address. 'size' is the size of the
340 * whole block, including the new arena.
342 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
346 /* Create a free arena */
348 pFree = (ARENA_FREE *)ptr;
349 pFree->magic = ARENA_FREE_MAGIC;
351 /* If debugging, erase the freed block content */
355 char *pEnd = (char *)ptr + size;
356 if (pEnd > (char *)subheap + subheap->commitSize)
357 pEnd = (char *)subheap + subheap->commitSize;
358 if (pEnd > (char *)(pFree + 1))
359 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
362 /* Check if next block is free also */
364 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
365 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
367 /* Remove the next arena from the free list */
368 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
369 pNext->next->prev = pNext->prev;
370 pNext->prev->next = pNext->next;
371 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
373 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
376 /* Set the next block PREV_FREE flag and pointer */
378 if ((char *)ptr + size < (char *)subheap + subheap->size)
380 DWORD *pNext = (DWORD *)((char *)ptr + size);
381 *pNext |= ARENA_FLAG_PREV_FREE;
382 *(ARENA_FREE **)(pNext - 1) = pFree;
385 /* Last, insert the new block into the free list */
387 pFree->size = size - sizeof(*pFree);
388 HEAP_InsertFreeBlock( subheap->heap, pFree );
392 /***********************************************************************
393 * HEAP_MakeInUseBlockFree
395 * Turn an in-use block into a free block. Can also decommit the end of
396 * the heap, and possibly even free the sub-heap altogether.
398 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
401 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
403 /* Check if we can merge with previous block */
405 if (pArena->size & ARENA_FLAG_PREV_FREE)
407 pFree = *((ARENA_FREE **)pArena - 1);
408 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
409 /* Remove it from the free list */
410 pFree->next->prev = pFree->prev;
411 pFree->prev->next = pFree->next;
413 else pFree = (ARENA_FREE *)pArena;
415 /* Create a free block */
417 HEAP_CreateFreeBlock( subheap, pFree, size );
418 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
419 if ((char *)pFree + size < (char *)subheap + subheap->size)
420 return; /* Not the last block, so nothing more to do */
422 /* Free the whole sub-heap if it's empty and not the original one */
424 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
425 (subheap != &subheap->heap->subheap))
427 SUBHEAP *pPrev = &subheap->heap->subheap;
428 /* Remove the free block from the list */
429 pFree->next->prev = pFree->prev;
430 pFree->prev->next = pFree->next;
431 /* Remove the subheap from the list */
432 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
433 if (pPrev) pPrev->next = subheap->next;
434 /* Free the memory */
436 VirtualFree( subheap, 0, MEM_RELEASE );
440 /* Decommit the end of the heap */
442 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
446 /***********************************************************************
449 * Shrink an in-use block.
451 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
453 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
455 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
456 (pArena->size & ARENA_SIZE_MASK) - size );
457 /* assign size plus previous arena flags */
458 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
462 /* Turn off PREV_FREE flag in next block */
463 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
464 if (pNext < (char *)subheap + subheap->size)
465 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
469 /***********************************************************************
472 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
473 DWORD commitSize, DWORD totalSize )
475 SUBHEAP *subheap = (SUBHEAP *)address;
476 FREE_LIST_ENTRY *pEntry;
481 if (flags & HEAP_SHARED)
482 commitSize = totalSize; /* always commit everything in a shared heap */
483 if (!VirtualAlloc(address, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
485 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
486 commitSize, (DWORD)address );
490 /* Fill the sub-heap structure */
492 subheap->heap = heap;
493 subheap->size = totalSize;
494 subheap->commitSize = commitSize;
495 subheap->magic = SUBHEAP_MAGIC;
497 if ( subheap != (SUBHEAP *)heap )
499 /* If this is a secondary subheap, insert it into list */
501 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
502 subheap->next = heap->subheap.next;
503 heap->subheap.next = subheap;
507 /* If this is a primary subheap, initialize main heap */
509 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
510 subheap->next = NULL;
513 heap->magic = HEAP_MAGIC;
515 /* Build the free lists */
517 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
519 pEntry->size = HEAP_freeListSizes[i];
520 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
521 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
522 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
523 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
524 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
525 pEntry->arena.magic = ARENA_FREE_MAGIC;
528 /* Initialize critical section */
530 RtlInitializeCriticalSection( &heap->critSection );
531 if (flags & HEAP_SHARED)
532 MakeCriticalSectionGlobal( &heap->critSection ); /* FIXME: dll separation */
535 /* Create the first free block */
537 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
538 subheap->size - subheap->headerSize );
543 /***********************************************************************
546 * Create a sub-heap of the given size.
547 * If heap == NULL, creates a main heap.
549 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
550 DWORD commitSize, DWORD totalSize )
552 LPVOID address = base;
554 /* round-up sizes on a 64K boundary */
555 totalSize = (totalSize + 0xffff) & 0xffff0000;
556 commitSize = (commitSize + 0xffff) & 0xffff0000;
557 if (!commitSize) commitSize = 0x10000;
558 if (totalSize < commitSize) totalSize = commitSize;
562 /* allocate the memory block */
563 if (!(address = VirtualAlloc( NULL, totalSize, MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
565 WARN("Could not VirtualAlloc %08lx bytes\n",
571 /* Initialize subheap */
573 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
574 address, flags, commitSize, totalSize ))
576 if (!base) VirtualFree( address, 0, MEM_RELEASE );
580 return (SUBHEAP *)address;
584 /***********************************************************************
587 * Find a free block at least as large as the requested size, and make sure
588 * the requested size is committed.
590 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
591 SUBHEAP **ppSubHeap )
595 FREE_LIST_ENTRY *pEntry = heap->freeList;
597 /* Find a suitable free list, and in it find a block large enough */
599 while (pEntry->size < size) pEntry++;
600 pArena = pEntry->arena.next;
601 while (pArena != &heap->freeList[0].arena)
603 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
604 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
605 if (arena_size >= size)
607 subheap = HEAP_FindSubHeap( heap, pArena );
608 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
609 + size + HEAP_MIN_BLOCK_SIZE))
611 *ppSubHeap = subheap;
614 pArena = pArena->next;
617 /* If no block was found, attempt to grow the heap */
619 if (!(heap->flags & HEAP_GROWABLE))
621 WARN("Not enough space in heap %08lx for %08lx bytes\n",
625 /* make sure that we have a big enough size *committed* to fit another
626 * last free arena in !
627 * So just one heap struct, one first free arena which will eventually
628 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
629 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
630 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
631 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
632 max( HEAP_DEF_SIZE, size ) )))
635 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
636 (DWORD)subheap, size, (DWORD)heap );
638 *ppSubHeap = subheap;
639 return (ARENA_FREE *)(subheap + 1);
643 /***********************************************************************
644 * HEAP_IsValidArenaPtr
646 * Check that the pointer is inside the range possible for arenas.
648 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
651 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
652 if (!subheap) return FALSE;
653 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
654 if (subheap != &heap->subheap) return FALSE;
655 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
656 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
661 /***********************************************************************
662 * HEAP_ValidateFreeArena
664 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
666 char *heapEnd = (char *)subheap + subheap->size;
668 /* Check for unaligned pointers */
669 if ( (long)pArena % ALIGNMENT != 0 )
671 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
672 (DWORD)subheap->heap, (DWORD)pArena );
676 /* Check magic number */
677 if (pArena->magic != ARENA_FREE_MAGIC)
679 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
680 (DWORD)subheap->heap, (DWORD)pArena );
683 /* Check size flags */
684 if (!(pArena->size & ARENA_FLAG_FREE) ||
685 (pArena->size & ARENA_FLAG_PREV_FREE))
687 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
688 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
690 /* Check arena size */
691 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
693 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
694 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
697 /* Check that next pointer is valid */
698 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
700 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
701 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
704 /* Check that next arena is free */
705 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
706 (pArena->next->magic != ARENA_FREE_MAGIC))
708 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
709 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
712 /* Check that prev pointer is valid */
713 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
715 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
716 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
719 /* Check that prev arena is free */
720 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
721 (pArena->prev->magic != ARENA_FREE_MAGIC))
723 /* this often means that the prev arena got overwritten
724 * by a memory write before that prev arena */
725 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
726 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
729 /* Check that next block has PREV_FREE flag */
730 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
732 if (!(*(DWORD *)((char *)(pArena + 1) +
733 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
735 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
736 (DWORD)subheap->heap, (DWORD)pArena );
739 /* Check next block back pointer */
740 if (*((ARENA_FREE **)((char *)(pArena + 1) +
741 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
743 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
744 (DWORD)subheap->heap, (DWORD)pArena,
745 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
753 /***********************************************************************
754 * HEAP_ValidateInUseArena
756 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
758 char *heapEnd = (char *)subheap + subheap->size;
760 /* Check for unaligned pointers */
761 if ( (long)pArena % ALIGNMENT != 0 )
763 if ( quiet == NOISY )
765 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
766 (DWORD)subheap->heap, (DWORD)pArena );
767 if ( TRACE_ON(heap) )
768 HEAP_Dump( subheap->heap );
770 else if ( WARN_ON(heap) )
772 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
773 (DWORD)subheap->heap, (DWORD)pArena );
774 if ( TRACE_ON(heap) )
775 HEAP_Dump( subheap->heap );
780 /* Check magic number */
781 if (pArena->magic != ARENA_INUSE_MAGIC)
783 if (quiet == NOISY) {
784 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
785 (DWORD)subheap->heap, (DWORD)pArena );
787 HEAP_Dump( subheap->heap );
788 } else if (WARN_ON(heap)) {
789 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
790 (DWORD)subheap->heap, (DWORD)pArena );
792 HEAP_Dump( subheap->heap );
796 /* Check size flags */
797 if (pArena->size & ARENA_FLAG_FREE)
799 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
800 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
802 /* Check arena size */
803 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
805 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
806 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
809 /* Check next arena PREV_FREE flag */
810 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
811 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
813 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
814 (DWORD)subheap->heap, (DWORD)pArena );
817 /* Check prev free arena */
818 if (pArena->size & ARENA_FLAG_PREV_FREE)
820 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
821 /* Check prev pointer */
822 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
824 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
825 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
828 /* Check that prev arena is free */
829 if (!(pPrev->size & ARENA_FLAG_FREE) ||
830 (pPrev->magic != ARENA_FREE_MAGIC))
832 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
833 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
836 /* Check that prev arena is really the previous block */
837 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
839 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
840 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
848 /***********************************************************************
849 * HEAP_IsRealArena [Internal]
850 * Validates a block is a valid arena.
856 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
857 DWORD flags, /* [in] Bit flags that control access during operation */
858 LPCVOID block, /* [in] Optional pointer to memory block to validate */
859 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
860 * does not complain */
865 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
867 ERR("Invalid heap %p!\n", heapPtr );
871 flags &= HEAP_NO_SERIALIZE;
872 flags |= heapPtr->flags;
873 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
874 if (!(flags & HEAP_NO_SERIALIZE))
875 RtlEnterCriticalSection( &heapPtr->critSection );
879 /* Only check this single memory block */
881 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
882 ((char *)block < (char *)subheap + subheap->headerSize
883 + sizeof(ARENA_INUSE)))
886 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
887 else if (WARN_ON(heap))
888 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
891 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
893 if (!(flags & HEAP_NO_SERIALIZE))
894 RtlLeaveCriticalSection( &heapPtr->critSection );
898 subheap = &heapPtr->subheap;
899 while (subheap && ret)
901 char *ptr = (char *)subheap + subheap->headerSize;
902 while (ptr < (char *)subheap + subheap->size)
904 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
906 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
910 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
914 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
918 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
921 subheap = subheap->next;
924 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
929 /***********************************************************************
930 * RtlCreateHeap (NTDLL.@)
932 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
933 PVOID unknown, PRTL_HEAP_DEFINITION definition )
937 /* Allocate the heap block */
941 totalSize = HEAP_DEF_SIZE;
942 flags |= HEAP_GROWABLE;
945 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
947 /* link it into the per-process heap list */
950 HEAP *heapPtr = subheap->heap;
951 RtlLockHeap( processHeap );
952 heapPtr->next = firstHeap;
954 RtlUnlockHeap( processHeap );
956 else /* assume the first heap we create is the process main heap */
958 set_process_heap( (HANDLE)subheap->heap );
960 return (HANDLE)subheap;
964 /***********************************************************************
965 * RtlDestroyHeap (NTDLL.@)
967 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
969 HEAP *heapPtr = HEAP_GetPtr( heap );
972 TRACE("%08x\n", heap );
973 if (!heapPtr) return heap;
975 if (heap == processHeap) return heap; /* cannot delete the main process heap */
976 else /* remove it from the per-process list */
979 RtlLockHeap( processHeap );
981 while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
982 if (*pptr) *pptr = (*pptr)->next;
983 RtlUnlockHeap( processHeap );
986 RtlDeleteCriticalSection( &heapPtr->critSection );
987 subheap = &heapPtr->subheap;
990 SUBHEAP *next = subheap->next;
991 VirtualFree( subheap, 0, MEM_RELEASE );
998 /***********************************************************************
999 * RtlAllocateHeap (NTDLL.@)
1001 * NOTE: does not set last error.
1003 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
1006 ARENA_INUSE *pInUse;
1008 HEAP *heapPtr = HEAP_GetPtr( heap );
1010 /* Validate the parameters */
1012 if (!heapPtr) return NULL;
1013 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1014 flags |= heapPtr->flags;
1015 size = ROUND_SIZE(size);
1016 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1018 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1019 /* Locate a suitable free block */
1021 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1023 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1024 heap, flags, size );
1025 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1026 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1030 /* Remove the arena from the free list */
1032 pArena->next->prev = pArena->prev;
1033 pArena->prev->next = pArena->next;
1035 /* Build the in-use arena */
1037 pInUse = (ARENA_INUSE *)pArena;
1039 /* in-use arena is smaller than free arena,
1040 * so we have to add the difference to the size */
1041 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1042 pInUse->magic = ARENA_INUSE_MAGIC;
1044 /* Shrink the block */
1046 HEAP_ShrinkBlock( subheap, pInUse, size );
1048 if (flags & HEAP_ZERO_MEMORY)
1049 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1050 else if (TRACE_ON(heap))
1051 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1053 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1055 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1056 heap, flags, size, (DWORD)(pInUse + 1) );
1057 return (LPVOID)(pInUse + 1);
1061 /***********************************************************************
1062 * RtlFreeHeap (NTDLL.@)
1064 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1066 ARENA_INUSE *pInUse;
1068 HEAP *heapPtr = HEAP_GetPtr( heap );
1070 /* Validate the parameters */
1072 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1075 set_status( STATUS_INVALID_HANDLE );
1079 flags &= HEAP_NO_SERIALIZE;
1080 flags |= heapPtr->flags;
1081 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1082 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1084 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1085 set_status( STATUS_INVALID_PARAMETER );
1086 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1087 heap, flags, (DWORD)ptr );
1091 /* Turn the block into a free block */
1093 pInUse = (ARENA_INUSE *)ptr - 1;
1094 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1095 HEAP_MakeInUseBlockFree( subheap, pInUse );
1097 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1099 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1100 heap, flags, (DWORD)ptr );
1105 /***********************************************************************
1106 * RtlReAllocateHeap (NTDLL.@)
1108 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1110 ARENA_INUSE *pArena;
1115 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1116 if (!(heapPtr = HEAP_GetPtr( heap )))
1118 set_status( STATUS_INVALID_HANDLE );
1122 /* Validate the parameters */
1124 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1125 HEAP_REALLOC_IN_PLACE_ONLY;
1126 flags |= heapPtr->flags;
1127 size = ROUND_SIZE(size);
1128 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1130 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1131 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1133 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1134 set_status( STATUS_INVALID_PARAMETER );
1135 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1136 heap, flags, (DWORD)ptr, size );
1140 /* Check if we need to grow the block */
1142 pArena = (ARENA_INUSE *)ptr - 1;
1143 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1144 oldSize = (pArena->size & ARENA_SIZE_MASK);
1147 char *pNext = (char *)(pArena + 1) + oldSize;
1148 if ((pNext < (char *)subheap + subheap->size) &&
1149 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1150 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1152 /* The next block is free and large enough */
1153 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1154 pFree->next->prev = pFree->prev;
1155 pFree->prev->next = pFree->next;
1156 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1157 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1158 + size + HEAP_MIN_BLOCK_SIZE))
1160 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1161 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1162 set_status( STATUS_NO_MEMORY );
1165 HEAP_ShrinkBlock( subheap, pArena, size );
1167 else /* Do it the hard way */
1170 ARENA_INUSE *pInUse;
1171 SUBHEAP *newsubheap;
1173 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1174 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1176 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1177 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1178 set_status( STATUS_NO_MEMORY );
1182 /* Build the in-use arena */
1184 pNew->next->prev = pNew->prev;
1185 pNew->prev->next = pNew->next;
1186 pInUse = (ARENA_INUSE *)pNew;
1187 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1188 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1189 pInUse->magic = ARENA_INUSE_MAGIC;
1190 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1191 memcpy( pInUse + 1, pArena + 1, oldSize );
1193 /* Free the previous block */
1195 HEAP_MakeInUseBlockFree( subheap, pArena );
1196 subheap = newsubheap;
1200 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1202 /* Clear the extra bytes if needed */
1206 if (flags & HEAP_ZERO_MEMORY)
1207 memset( (char *)(pArena + 1) + oldSize, 0,
1208 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1209 else if (TRACE_ON(heap))
1210 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1211 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1214 /* Return the new arena */
1216 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1218 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1219 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1220 return (LPVOID)(pArena + 1);
1224 /***********************************************************************
1225 * RtlCompactHeap (NTDLL.@)
1227 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1234 /***********************************************************************
1235 * RtlLockHeap (NTDLL.@)
1237 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1239 HEAP *heapPtr = HEAP_GetPtr( heap );
1240 if (!heapPtr) return FALSE;
1241 RtlEnterCriticalSection( &heapPtr->critSection );
1246 /***********************************************************************
1247 * RtlUnlockHeap (NTDLL.@)
1249 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1251 HEAP *heapPtr = HEAP_GetPtr( heap );
1252 if (!heapPtr) return FALSE;
1253 RtlLeaveCriticalSection( &heapPtr->critSection );
1258 /***********************************************************************
1259 * RtlSizeHeap (NTDLL.@)
1261 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1264 HEAP *heapPtr = HEAP_GetPtr( heap );
1268 set_status( STATUS_INVALID_HANDLE );
1271 flags &= HEAP_NO_SERIALIZE;
1272 flags |= heapPtr->flags;
1273 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1274 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1276 set_status( STATUS_INVALID_PARAMETER );
1281 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1282 ret = pArena->size & ARENA_SIZE_MASK;
1284 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1286 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1287 heap, flags, (DWORD)ptr, ret );
1292 /***********************************************************************
1293 * RtlValidateHeap (NTDLL.@)
1295 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, PCVOID block )
1297 HEAP *heapPtr = HEAP_GetPtr( heap );
1298 if (!heapPtr) return FALSE;
1299 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1303 /***********************************************************************
1304 * RtlWalkHeap (NTDLL.@)
1306 * FIXME: the PROCESS_HEAP_ENTRY flag values seem different between this
1307 * function and HeapWalk. To be checked.
1309 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1311 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1312 HEAP *heapPtr = HEAP_GetPtr(heap);
1313 SUBHEAP *sub, *currentheap = NULL;
1316 int region_index = 0;
1318 FIXME( "not fully compatible\n" );
1320 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1322 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) EnterCriticalSection( &heapPtr->critSection );
1324 /* set ptr to the next arena to be examined */
1326 if (!entry->lpData) /* first call (init) ? */
1328 TRACE("begin walking of heap 0x%08x.\n", heap);
1329 currentheap = &heapPtr->subheap;
1330 ptr = (char*)currentheap + currentheap->headerSize;
1334 ptr = entry->lpData;
1335 sub = &heapPtr->subheap;
1338 if (((char *)ptr >= (char *)sub) &&
1339 ((char *)ptr < (char *)sub + sub->size))
1347 if (currentheap == NULL)
1349 ERR("no matching subheap found, shouldn't happen !\n");
1350 ret = STATUS_NO_MORE_ENTRIES;
1354 ptr += entry->cbData; /* point to next arena */
1355 if (ptr > (char *)currentheap + currentheap->size - 1)
1356 { /* proceed with next subheap */
1357 if (!(currentheap = currentheap->next))
1358 { /* successfully finished */
1359 TRACE("end reached.\n");
1360 ret = STATUS_NO_MORE_ENTRIES;
1363 ptr = (char*)currentheap + currentheap->headerSize;
1368 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1370 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1372 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1374 entry->lpData = pArena + 1;
1375 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1376 entry->cbOverhead = sizeof(ARENA_FREE);
1377 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1381 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1383 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1385 entry->lpData = pArena + 1;
1386 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1387 entry->cbOverhead = sizeof(ARENA_INUSE);
1388 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1389 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1390 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1393 entry->iRegionIndex = region_index;
1395 /* first element of heap ? */
1396 if (ptr == (char *)(currentheap + currentheap->headerSize))
1398 entry->wFlags |= PROCESS_HEAP_REGION;
1399 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1400 entry->u.Region.dwUnCommittedSize =
1401 currentheap->size - currentheap->commitSize;
1402 entry->u.Region.lpFirstBlock = /* first valid block */
1403 currentheap + currentheap->headerSize;
1404 entry->u.Region.lpLastBlock = /* first invalid block */
1405 currentheap + currentheap->size;
1407 ret = STATUS_SUCCESS;
1410 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) LeaveCriticalSection( &heapPtr->critSection );
1415 /***********************************************************************
1416 * RtlGetProcessHeaps (NTDLL.@)
1418 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1423 if (!processHeap) return 0; /* should never happen */
1424 total = 1; /* main heap */
1425 RtlLockHeap( processHeap );
1426 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1429 *heaps++ = (HANDLE)processHeap;
1430 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1432 RtlUnlockHeap( processHeap );