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 QUIET 1 /* Suppress messages */
70 #define NOISY 0 /* Report all errors */
72 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
74 /* Max size of the blocks on the free lists */
75 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
77 0x20, 0x80, 0x200, ~0UL
88 typedef struct tagSUBHEAP
90 DWORD size; /* Size of the whole sub-heap */
91 DWORD commitSize; /* Committed size of the sub-heap */
92 DWORD headerSize; /* Size of the heap header */
93 struct tagSUBHEAP *next; /* Next sub-heap */
94 struct tagHEAP *heap; /* Main heap structure */
95 DWORD magic; /* Magic number */
98 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
100 typedef struct tagHEAP
102 SUBHEAP subheap; /* First sub-heap */
103 struct tagHEAP *next; /* Next heap for this process */
104 CRITICAL_SECTION critSection; /* Critical section for serialization */
105 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
106 DWORD flags; /* Heap flags */
107 DWORD magic; /* Magic number */
110 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
112 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
113 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
114 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
116 static HANDLE processHeap; /* main process heap */
118 static HEAP *firstHeap; /* head of secondary heaps list */
120 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
122 /* SetLastError for ntdll */
123 inline static void set_status( NTSTATUS status )
125 #if defined(__i386__) && defined(__GNUC__)
126 /* in this case SetLastError is an inline function so we can use it */
127 SetLastError( RtlNtStatusToDosError( status ) );
129 /* cannot use SetLastError, do it manually */
130 NtCurrentTeb()->last_error = RtlNtStatusToDosError( status );
134 /* set the process main heap */
135 static void set_process_heap( HANDLE heap )
137 HANDLE *pdb = (HANDLE *)NtCurrentTeb()->process;
138 pdb[0x18 / sizeof(HANDLE)] = heap; /* heap is at offset 0x18 in pdb */
143 /***********************************************************************
146 void HEAP_Dump( HEAP *heap )
152 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
153 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
154 (DWORD)heap->next, (DWORD)&heap->subheap );
155 subheap = &heap->subheap;
156 while (subheap->next)
158 DPRINTF( " -> %08lx", (DWORD)subheap->next );
159 subheap = subheap->next;
162 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
163 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
164 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
165 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
166 (DWORD)heap->freeList[i].arena.prev,
167 (DWORD)heap->freeList[i].arena.next );
169 subheap = &heap->subheap;
172 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
173 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
174 (DWORD)subheap, subheap->size, subheap->commitSize );
176 DPRINTF( "\n Block Stat Size Id\n" );
177 ptr = (char*)subheap + subheap->headerSize;
178 while (ptr < (char *)subheap + subheap->size)
180 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
182 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
183 DPRINTF( "%08lx free %08lx prev=%08lx next=%08lx\n",
184 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
185 (DWORD)pArena->prev, (DWORD)pArena->next);
186 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
187 arenaSize += sizeof(ARENA_FREE);
188 freeSize += pArena->size & ARENA_SIZE_MASK;
190 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
192 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
193 DPRINTF( "%08lx Used %08lx back=%08lx\n",
194 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK, *((DWORD *)pArena - 1) );
195 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
196 arenaSize += sizeof(ARENA_INUSE);
197 usedSize += pArena->size & ARENA_SIZE_MASK;
201 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
202 DPRINTF( "%08lx used %08lx\n",
203 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK );
204 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
205 arenaSize += sizeof(ARENA_INUSE);
206 usedSize += pArena->size & ARENA_SIZE_MASK;
209 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
210 subheap->size, subheap->commitSize, freeSize, usedSize,
211 arenaSize, (arenaSize * 100) / subheap->size );
212 subheap = subheap->next;
217 /***********************************************************************
220 * Pointer to the heap
223 static HEAP *HEAP_GetPtr(
224 HANDLE heap /* [in] Handle to the heap */
226 HEAP *heapPtr = (HEAP *)heap;
227 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
229 ERR("Invalid heap %08x!\n", heap );
232 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
234 HEAP_Dump( heapPtr );
242 /***********************************************************************
243 * HEAP_InsertFreeBlock
245 * Insert a free block into the free list.
247 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
249 FREE_LIST_ENTRY *pEntry = heap->freeList;
250 while (pEntry->size < pArena->size) pEntry++;
251 pArena->size |= ARENA_FLAG_FREE;
252 pArena->next = pEntry->arena.next;
253 pArena->next->prev = pArena;
254 pArena->prev = &pEntry->arena;
255 pEntry->arena.next = pArena;
259 /***********************************************************************
261 * Find the sub-heap containing a given address.
267 static SUBHEAP *HEAP_FindSubHeap(
268 HEAP *heap, /* [in] Heap pointer */
269 LPCVOID ptr /* [in] Address */
271 SUBHEAP *sub = &heap->subheap;
274 if (((char *)ptr >= (char *)sub) &&
275 ((char *)ptr < (char *)sub + sub->size)) return sub;
282 /***********************************************************************
285 * Make sure the heap storage is committed up to (not including) ptr.
287 static inline BOOL HEAP_Commit( SUBHEAP *subheap, void *ptr )
289 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
290 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
291 if (size > subheap->size) size = subheap->size;
292 if (size <= subheap->commitSize) return TRUE;
293 if (!VirtualAlloc( (char *)subheap + subheap->commitSize,
294 size - subheap->commitSize, MEM_COMMIT,
295 PAGE_EXECUTE_READWRITE))
297 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
298 size - subheap->commitSize,
299 (DWORD)((char *)subheap + subheap->commitSize),
300 (DWORD)subheap->heap );
303 subheap->commitSize = size;
308 /***********************************************************************
311 * If possible, decommit the heap storage from (including) 'ptr'.
313 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
315 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
316 /* round to next block and add one full block */
317 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
318 if (size >= subheap->commitSize) return TRUE;
319 if (!VirtualFree( (char *)subheap + size,
320 subheap->commitSize - size, MEM_DECOMMIT ))
322 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
323 subheap->commitSize - size,
324 (DWORD)((char *)subheap + size),
325 (DWORD)subheap->heap );
328 subheap->commitSize = size;
333 /***********************************************************************
334 * HEAP_CreateFreeBlock
336 * Create a free block at a specified address. 'size' is the size of the
337 * whole block, including the new arena.
339 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
343 /* Create a free arena */
345 pFree = (ARENA_FREE *)ptr;
346 pFree->magic = ARENA_FREE_MAGIC;
348 /* If debugging, erase the freed block content */
352 char *pEnd = (char *)ptr + size;
353 if (pEnd > (char *)subheap + subheap->commitSize)
354 pEnd = (char *)subheap + subheap->commitSize;
355 if (pEnd > (char *)(pFree + 1))
356 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
359 /* Check if next block is free also */
361 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
362 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
364 /* Remove the next arena from the free list */
365 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
366 pNext->next->prev = pNext->prev;
367 pNext->prev->next = pNext->next;
368 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
370 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
373 /* Set the next block PREV_FREE flag and pointer */
375 if ((char *)ptr + size < (char *)subheap + subheap->size)
377 DWORD *pNext = (DWORD *)((char *)ptr + size);
378 *pNext |= ARENA_FLAG_PREV_FREE;
379 *(ARENA_FREE **)(pNext - 1) = pFree;
382 /* Last, insert the new block into the free list */
384 pFree->size = size - sizeof(*pFree);
385 HEAP_InsertFreeBlock( subheap->heap, pFree );
389 /***********************************************************************
390 * HEAP_MakeInUseBlockFree
392 * Turn an in-use block into a free block. Can also decommit the end of
393 * the heap, and possibly even free the sub-heap altogether.
395 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
398 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
400 /* Check if we can merge with previous block */
402 if (pArena->size & ARENA_FLAG_PREV_FREE)
404 pFree = *((ARENA_FREE **)pArena - 1);
405 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
406 /* Remove it from the free list */
407 pFree->next->prev = pFree->prev;
408 pFree->prev->next = pFree->next;
410 else pFree = (ARENA_FREE *)pArena;
412 /* Create a free block */
414 HEAP_CreateFreeBlock( subheap, pFree, size );
415 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
416 if ((char *)pFree + size < (char *)subheap + subheap->size)
417 return; /* Not the last block, so nothing more to do */
419 /* Free the whole sub-heap if it's empty and not the original one */
421 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
422 (subheap != &subheap->heap->subheap))
424 SUBHEAP *pPrev = &subheap->heap->subheap;
425 /* Remove the free block from the list */
426 pFree->next->prev = pFree->prev;
427 pFree->prev->next = pFree->next;
428 /* Remove the subheap from the list */
429 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
430 if (pPrev) pPrev->next = subheap->next;
431 /* Free the memory */
433 VirtualFree( subheap, 0, MEM_RELEASE );
437 /* Decommit the end of the heap */
439 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
443 /***********************************************************************
446 * Shrink an in-use block.
448 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
450 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
452 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
453 (pArena->size & ARENA_SIZE_MASK) - size );
454 /* assign size plus previous arena flags */
455 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
459 /* Turn off PREV_FREE flag in next block */
460 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
461 if (pNext < (char *)subheap + subheap->size)
462 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
466 /***********************************************************************
469 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
470 DWORD commitSize, DWORD totalSize )
472 SUBHEAP *subheap = (SUBHEAP *)address;
473 FREE_LIST_ENTRY *pEntry;
478 if (flags & HEAP_SHARED)
479 commitSize = totalSize; /* always commit everything in a shared heap */
480 if (!VirtualAlloc(address, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
482 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
483 commitSize, (DWORD)address );
487 /* Fill the sub-heap structure */
489 subheap->heap = heap;
490 subheap->size = totalSize;
491 subheap->commitSize = commitSize;
492 subheap->magic = SUBHEAP_MAGIC;
494 if ( subheap != (SUBHEAP *)heap )
496 /* If this is a secondary subheap, insert it into list */
498 subheap->headerSize = sizeof(SUBHEAP);
499 subheap->next = heap->subheap.next;
500 heap->subheap.next = subheap;
504 /* If this is a primary subheap, initialize main heap */
506 subheap->headerSize = sizeof(HEAP);
507 subheap->next = NULL;
510 heap->magic = HEAP_MAGIC;
512 /* Build the free lists */
514 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
516 pEntry->size = HEAP_freeListSizes[i];
517 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
518 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
519 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
520 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
521 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
522 pEntry->arena.magic = ARENA_FREE_MAGIC;
525 /* Initialize critical section */
527 RtlInitializeCriticalSection( &heap->critSection );
530 /* Create the first free block */
532 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
533 subheap->size - subheap->headerSize );
538 /***********************************************************************
541 * Create a sub-heap of the given size.
542 * If heap == NULL, creates a main heap.
544 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
545 DWORD commitSize, DWORD totalSize )
547 LPVOID address = base;
551 /* round-up sizes on a 64K boundary */
552 totalSize = (totalSize + 0xffff) & 0xffff0000;
553 commitSize = (commitSize + 0xffff) & 0xffff0000;
554 if (!commitSize) commitSize = 0x10000;
555 if (totalSize < commitSize) totalSize = commitSize;
557 /* allocate the memory block */
558 if (!(address = VirtualAlloc( NULL, totalSize, MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
560 WARN("Could not VirtualAlloc %08lx bytes\n",
566 /* Initialize subheap */
568 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
569 address, flags, commitSize, totalSize ))
571 if (!base) VirtualFree( address, 0, MEM_RELEASE );
575 return (SUBHEAP *)address;
579 /***********************************************************************
582 * Find a free block at least as large as the requested size, and make sure
583 * the requested size is committed.
585 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
586 SUBHEAP **ppSubHeap )
590 FREE_LIST_ENTRY *pEntry = heap->freeList;
592 /* Find a suitable free list, and in it find a block large enough */
594 while (pEntry->size < size) pEntry++;
595 pArena = pEntry->arena.next;
596 while (pArena != &heap->freeList[0].arena)
598 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
599 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
600 if (arena_size >= size)
602 subheap = HEAP_FindSubHeap( heap, pArena );
603 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
604 + size + HEAP_MIN_BLOCK_SIZE))
606 *ppSubHeap = subheap;
609 pArena = pArena->next;
612 /* If no block was found, attempt to grow the heap */
614 if (!(heap->flags & HEAP_GROWABLE))
616 WARN("Not enough space in heap %08lx for %08lx bytes\n",
620 /* make sure that we have a big enough size *committed* to fit another
621 * last free arena in !
622 * So just one heap struct, one first free arena which will eventually
623 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
624 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
625 size += sizeof(SUBHEAP) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
626 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
627 max( HEAP_DEF_SIZE, size ) )))
630 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
631 (DWORD)subheap, size, (DWORD)heap );
633 *ppSubHeap = subheap;
634 return (ARENA_FREE *)(subheap + 1);
638 /***********************************************************************
639 * HEAP_IsValidArenaPtr
641 * Check that the pointer is inside the range possible for arenas.
643 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
646 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
647 if (!subheap) return FALSE;
648 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
649 if (subheap != &heap->subheap) return FALSE;
650 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
651 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
656 /***********************************************************************
657 * HEAP_ValidateFreeArena
659 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
661 char *heapEnd = (char *)subheap + subheap->size;
663 #if !defined(ALLOW_UNALIGNED_ACCESS)
664 /* Check for unaligned pointers */
665 if ( (long)pArena % sizeof(void *) != 0 )
667 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
668 (DWORD)subheap->heap, (DWORD)pArena );
673 /* Check magic number */
674 if (pArena->magic != ARENA_FREE_MAGIC)
676 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
677 (DWORD)subheap->heap, (DWORD)pArena );
680 /* Check size flags */
681 if (!(pArena->size & ARENA_FLAG_FREE) ||
682 (pArena->size & ARENA_FLAG_PREV_FREE))
684 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
685 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
687 /* Check arena size */
688 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
690 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
691 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
694 /* Check that next pointer is valid */
695 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
697 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
698 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
701 /* Check that next arena is free */
702 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
703 (pArena->next->magic != ARENA_FREE_MAGIC))
705 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
706 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
709 /* Check that prev pointer is valid */
710 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
712 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
713 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
716 /* Check that prev arena is free */
717 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
718 (pArena->prev->magic != ARENA_FREE_MAGIC))
720 /* this often means that the prev arena got overwritten
721 * by a memory write before that prev arena */
722 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
723 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
726 /* Check that next block has PREV_FREE flag */
727 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
729 if (!(*(DWORD *)((char *)(pArena + 1) +
730 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
732 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
733 (DWORD)subheap->heap, (DWORD)pArena );
736 /* Check next block back pointer */
737 if (*((ARENA_FREE **)((char *)(pArena + 1) +
738 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
740 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
741 (DWORD)subheap->heap, (DWORD)pArena,
742 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
750 /***********************************************************************
751 * HEAP_ValidateInUseArena
753 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
755 char *heapEnd = (char *)subheap + subheap->size;
757 #if !defined(ALLOW_UNALIGNED_ACCESS)
758 /* Check for unaligned pointers */
759 if ( (long)pArena % sizeof(void *) != 0 )
761 if ( quiet == NOISY )
763 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
764 (DWORD)subheap->heap, (DWORD)pArena );
765 if ( TRACE_ON(heap) )
766 HEAP_Dump( subheap->heap );
768 else if ( WARN_ON(heap) )
770 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
771 (DWORD)subheap->heap, (DWORD)pArena );
772 if ( TRACE_ON(heap) )
773 HEAP_Dump( subheap->heap );
779 /* Check magic number */
780 if (pArena->magic != ARENA_INUSE_MAGIC)
782 if (quiet == NOISY) {
783 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
784 (DWORD)subheap->heap, (DWORD)pArena );
786 HEAP_Dump( subheap->heap );
787 } else if (WARN_ON(heap)) {
788 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
789 (DWORD)subheap->heap, (DWORD)pArena );
791 HEAP_Dump( subheap->heap );
795 /* Check size flags */
796 if (pArena->size & ARENA_FLAG_FREE)
798 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
799 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
801 /* Check arena size */
802 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
804 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
805 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
808 /* Check next arena PREV_FREE flag */
809 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
810 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
812 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
813 (DWORD)subheap->heap, (DWORD)pArena );
816 /* Check prev free arena */
817 if (pArena->size & ARENA_FLAG_PREV_FREE)
819 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
820 /* Check prev pointer */
821 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
823 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
824 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
827 /* Check that prev arena is free */
828 if (!(pPrev->size & ARENA_FLAG_FREE) ||
829 (pPrev->magic != ARENA_FREE_MAGIC))
831 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
832 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
835 /* Check that prev arena is really the previous block */
836 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
838 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
839 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
847 /***********************************************************************
848 * HEAP_IsRealArena [Internal]
849 * Validates a block is a valid arena.
855 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
856 DWORD flags, /* [in] Bit flags that control access during operation */
857 LPCVOID block, /* [in] Optional pointer to memory block to validate */
858 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
859 * does not complain */
864 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
866 ERR("Invalid heap %p!\n", heapPtr );
870 flags &= HEAP_NO_SERIALIZE;
871 flags |= heapPtr->flags;
872 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
873 if (!(flags & HEAP_NO_SERIALIZE))
874 RtlEnterCriticalSection( &heapPtr->critSection );
878 /* Only check this single memory block */
880 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
881 ((char *)block < (char *)subheap + subheap->headerSize
882 + sizeof(ARENA_INUSE)))
885 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
886 else if (WARN_ON(heap))
887 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
890 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
892 if (!(flags & HEAP_NO_SERIALIZE))
893 RtlLeaveCriticalSection( &heapPtr->critSection );
897 subheap = &heapPtr->subheap;
898 while (subheap && ret)
900 char *ptr = (char *)subheap + subheap->headerSize;
901 while (ptr < (char *)subheap + subheap->size)
903 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
905 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
909 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
913 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
917 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
920 subheap = subheap->next;
923 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
928 /***********************************************************************
929 * RtlCreateHeap (NTDLL.@)
931 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
932 PVOID unknown, PRTL_HEAP_DEFINITION definition )
936 /* Allocate the heap block */
940 totalSize = HEAP_DEF_SIZE;
941 flags |= HEAP_GROWABLE;
944 totalSize = (totalSize + 0xffff) & 0xffff0000;
945 commitSize = (commitSize + 0xffff) & 0xffff0000;
946 if (!commitSize) commitSize = 0x10000;
947 if (totalSize < commitSize) totalSize = commitSize;
949 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
951 /* link it into the per-process heap list */
954 HEAP *heapPtr = subheap->heap;
955 RtlLockHeap( processHeap );
956 heapPtr->next = firstHeap;
958 RtlUnlockHeap( processHeap );
960 else /* assume the first heap we create is the process main heap */
962 set_process_heap( (HANDLE)subheap->heap );
964 return (HANDLE)subheap;
968 /***********************************************************************
969 * RtlDestroyHeap (NTDLL.@)
971 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
973 HEAP *heapPtr = HEAP_GetPtr( heap );
976 TRACE("%08x\n", heap );
977 if (!heapPtr) return heap;
979 if (heap == processHeap) return heap; /* cannot delete the main process heap */
980 else /* remove it from the per-process list */
983 RtlLockHeap( processHeap );
985 while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
986 if (*pptr) *pptr = (*pptr)->next;
987 RtlUnlockHeap( processHeap );
990 RtlDeleteCriticalSection( &heapPtr->critSection );
991 subheap = &heapPtr->subheap;
994 SUBHEAP *next = subheap->next;
995 VirtualFree( subheap, 0, MEM_RELEASE );
1002 /***********************************************************************
1003 * RtlAllocateHeap (NTDLL.@)
1005 * NOTE: does not set last error.
1007 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
1010 ARENA_INUSE *pInUse;
1012 HEAP *heapPtr = HEAP_GetPtr( heap );
1014 /* Validate the parameters */
1016 if (!heapPtr) return NULL;
1017 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1018 flags |= heapPtr->flags;
1019 size = (size + 3) & ~3;
1020 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1022 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1023 /* Locate a suitable free block */
1025 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1027 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1028 heap, flags, size );
1029 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1030 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1034 /* Remove the arena from the free list */
1036 pArena->next->prev = pArena->prev;
1037 pArena->prev->next = pArena->next;
1039 /* Build the in-use arena */
1041 pInUse = (ARENA_INUSE *)pArena;
1043 /* in-use arena is smaller than free arena,
1044 * so we have to add the difference to the size */
1045 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1046 pInUse->magic = ARENA_INUSE_MAGIC;
1048 /* Shrink the block */
1050 HEAP_ShrinkBlock( subheap, pInUse, size );
1052 if (flags & HEAP_ZERO_MEMORY)
1053 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1054 else if (TRACE_ON(heap))
1055 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1057 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1059 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1060 heap, flags, size, (DWORD)(pInUse + 1) );
1061 return (LPVOID)(pInUse + 1);
1065 /***********************************************************************
1066 * RtlFreeHeap (NTDLL.@)
1068 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1070 ARENA_INUSE *pInUse;
1072 HEAP *heapPtr = HEAP_GetPtr( heap );
1074 /* Validate the parameters */
1076 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1079 set_status( STATUS_INVALID_HANDLE );
1083 flags &= HEAP_NO_SERIALIZE;
1084 flags |= heapPtr->flags;
1085 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1086 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1088 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1089 set_status( STATUS_INVALID_PARAMETER );
1090 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1091 heap, flags, (DWORD)ptr );
1095 /* Turn the block into a free block */
1097 pInUse = (ARENA_INUSE *)ptr - 1;
1098 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1099 HEAP_MakeInUseBlockFree( subheap, pInUse );
1101 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1103 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1104 heap, flags, (DWORD)ptr );
1109 /***********************************************************************
1110 * RtlReAllocateHeap (NTDLL.@)
1112 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1114 ARENA_INUSE *pArena;
1119 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1120 if (!(heapPtr = HEAP_GetPtr( heap )))
1122 set_status( STATUS_INVALID_HANDLE );
1126 /* Validate the parameters */
1128 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1129 HEAP_REALLOC_IN_PLACE_ONLY;
1130 flags |= heapPtr->flags;
1131 size = (size + 3) & ~3;
1132 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1134 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1135 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1137 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1138 set_status( STATUS_INVALID_PARAMETER );
1139 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1140 heap, flags, (DWORD)ptr, size );
1144 /* Check if we need to grow the block */
1146 pArena = (ARENA_INUSE *)ptr - 1;
1147 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1148 oldSize = (pArena->size & ARENA_SIZE_MASK);
1151 char *pNext = (char *)(pArena + 1) + oldSize;
1152 if ((pNext < (char *)subheap + subheap->size) &&
1153 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1154 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1156 /* The next block is free and large enough */
1157 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1158 pFree->next->prev = pFree->prev;
1159 pFree->prev->next = pFree->next;
1160 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1161 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1162 + size + HEAP_MIN_BLOCK_SIZE))
1164 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1165 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1166 set_status( STATUS_NO_MEMORY );
1169 HEAP_ShrinkBlock( subheap, pArena, size );
1171 else /* Do it the hard way */
1174 ARENA_INUSE *pInUse;
1175 SUBHEAP *newsubheap;
1177 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1178 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1180 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1181 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1182 set_status( STATUS_NO_MEMORY );
1186 /* Build the in-use arena */
1188 pNew->next->prev = pNew->prev;
1189 pNew->prev->next = pNew->next;
1190 pInUse = (ARENA_INUSE *)pNew;
1191 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1192 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1193 pInUse->magic = ARENA_INUSE_MAGIC;
1194 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1195 memcpy( pInUse + 1, pArena + 1, oldSize );
1197 /* Free the previous block */
1199 HEAP_MakeInUseBlockFree( subheap, pArena );
1200 subheap = newsubheap;
1204 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1206 /* Clear the extra bytes if needed */
1210 if (flags & HEAP_ZERO_MEMORY)
1211 memset( (char *)(pArena + 1) + oldSize, 0,
1212 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1213 else if (TRACE_ON(heap))
1214 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1215 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1218 /* Return the new arena */
1220 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1222 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1223 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1224 return (LPVOID)(pArena + 1);
1228 /***********************************************************************
1229 * RtlCompactHeap (NTDLL.@)
1231 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1238 /***********************************************************************
1239 * RtlLockHeap (NTDLL.@)
1241 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1243 HEAP *heapPtr = HEAP_GetPtr( heap );
1244 if (!heapPtr) return FALSE;
1245 RtlEnterCriticalSection( &heapPtr->critSection );
1250 /***********************************************************************
1251 * RtlUnlockHeap (NTDLL.@)
1253 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1255 HEAP *heapPtr = HEAP_GetPtr( heap );
1256 if (!heapPtr) return FALSE;
1257 RtlLeaveCriticalSection( &heapPtr->critSection );
1262 /***********************************************************************
1263 * RtlSizeHeap (NTDLL.@)
1265 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1268 HEAP *heapPtr = HEAP_GetPtr( heap );
1272 set_status( STATUS_INVALID_HANDLE );
1275 flags &= HEAP_NO_SERIALIZE;
1276 flags |= heapPtr->flags;
1277 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1278 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1280 set_status( STATUS_INVALID_PARAMETER );
1285 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1286 ret = pArena->size & ARENA_SIZE_MASK;
1288 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1290 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1291 heap, flags, (DWORD)ptr, ret );
1296 /***********************************************************************
1297 * RtlValidateHeap (NTDLL.@)
1299 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, PCVOID block )
1301 HEAP *heapPtr = HEAP_GetPtr( heap );
1302 if (!heapPtr) return FALSE;
1303 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1307 /***********************************************************************
1308 * RtlWalkHeap (NTDLL.@)
1310 * FIXME: the PROCESS_HEAP_ENTRY flag values seem different between this
1311 * function and HeapWalk. To be checked.
1313 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1315 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1316 HEAP *heapPtr = HEAP_GetPtr(heap);
1317 SUBHEAP *sub, *currentheap = NULL;
1320 int region_index = 0;
1322 FIXME( "not fully compatible\n" );
1324 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1326 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) EnterCriticalSection( &heapPtr->critSection );
1328 /* set ptr to the next arena to be examined */
1330 if (!entry->lpData) /* first call (init) ? */
1332 TRACE("begin walking of heap 0x%08x.\n", heap);
1333 currentheap = &heapPtr->subheap;
1334 ptr = (char*)currentheap + currentheap->headerSize;
1338 ptr = entry->lpData;
1339 sub = &heapPtr->subheap;
1342 if (((char *)ptr >= (char *)sub) &&
1343 ((char *)ptr < (char *)sub + sub->size))
1351 if (currentheap == NULL)
1353 ERR("no matching subheap found, shouldn't happen !\n");
1354 ret = STATUS_NO_MORE_ENTRIES;
1358 ptr += entry->cbData; /* point to next arena */
1359 if (ptr > (char *)currentheap + currentheap->size - 1)
1360 { /* proceed with next subheap */
1361 if (!(currentheap = currentheap->next))
1362 { /* successfully finished */
1363 TRACE("end reached.\n");
1364 ret = STATUS_NO_MORE_ENTRIES;
1367 ptr = (char*)currentheap + currentheap->headerSize;
1372 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1374 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1376 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1378 entry->lpData = pArena + 1;
1379 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1380 entry->cbOverhead = sizeof(ARENA_FREE);
1381 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1385 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1387 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1389 entry->lpData = pArena + 1;
1390 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1391 entry->cbOverhead = sizeof(ARENA_INUSE);
1392 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1393 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1394 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1397 entry->iRegionIndex = region_index;
1399 /* first element of heap ? */
1400 if (ptr == (char *)(currentheap + currentheap->headerSize))
1402 entry->wFlags |= PROCESS_HEAP_REGION;
1403 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1404 entry->u.Region.dwUnCommittedSize =
1405 currentheap->size - currentheap->commitSize;
1406 entry->u.Region.lpFirstBlock = /* first valid block */
1407 currentheap + currentheap->headerSize;
1408 entry->u.Region.lpLastBlock = /* first invalid block */
1409 currentheap + currentheap->size;
1411 ret = STATUS_SUCCESS;
1414 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) LeaveCriticalSection( &heapPtr->critSection );
1419 /***********************************************************************
1420 * RtlGetProcessHeaps (NTDLL.@)
1422 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1427 if (!processHeap) return 0; /* should never happen */
1428 total = 1; /* main heap */
1429 RtlLockHeap( processHeap );
1430 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1433 *heaps++ = (HANDLE)processHeap;
1434 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1436 RtlUnlockHeap( processHeap );