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 size -= subheap->commitSize;
297 if (NtAllocateVirtualMemory( GetCurrentProcess(), &ptr, (char *)subheap + subheap->commitSize,
298 &size, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
300 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
301 size, (DWORD)((char *)subheap + subheap->commitSize),
302 (DWORD)subheap->heap );
305 subheap->commitSize += size;
310 /***********************************************************************
313 * If possible, decommit the heap storage from (including) 'ptr'.
315 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
320 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
321 /* round to next block and add one full block */
322 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
323 if (size >= subheap->commitSize) return TRUE;
324 decommit_size = subheap->commitSize - size;
325 addr = (char *)subheap + size;
327 if (NtFreeVirtualMemory( GetCurrentProcess(), &addr, &decommit_size, MEM_DECOMMIT ))
329 WARN("Could not decommit %08lx bytes at %08lx for heap %p\n",
330 decommit_size, (DWORD)((char *)subheap + size), subheap->heap );
333 subheap->commitSize -= decommit_size;
338 /***********************************************************************
339 * HEAP_CreateFreeBlock
341 * Create a free block at a specified address. 'size' is the size of the
342 * whole block, including the new arena.
344 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
348 /* Create a free arena */
350 pFree = (ARENA_FREE *)ptr;
351 pFree->magic = ARENA_FREE_MAGIC;
353 /* If debugging, erase the freed block content */
357 char *pEnd = (char *)ptr + size;
358 if (pEnd > (char *)subheap + subheap->commitSize)
359 pEnd = (char *)subheap + subheap->commitSize;
360 if (pEnd > (char *)(pFree + 1))
361 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
364 /* Check if next block is free also */
366 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
367 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
369 /* Remove the next arena from the free list */
370 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
371 pNext->next->prev = pNext->prev;
372 pNext->prev->next = pNext->next;
373 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
375 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
378 /* Set the next block PREV_FREE flag and pointer */
380 if ((char *)ptr + size < (char *)subheap + subheap->size)
382 DWORD *pNext = (DWORD *)((char *)ptr + size);
383 *pNext |= ARENA_FLAG_PREV_FREE;
384 *(ARENA_FREE **)(pNext - 1) = pFree;
387 /* Last, insert the new block into the free list */
389 pFree->size = size - sizeof(*pFree);
390 HEAP_InsertFreeBlock( subheap->heap, pFree );
394 /***********************************************************************
395 * HEAP_MakeInUseBlockFree
397 * Turn an in-use block into a free block. Can also decommit the end of
398 * the heap, and possibly even free the sub-heap altogether.
400 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
403 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
405 /* Check if we can merge with previous block */
407 if (pArena->size & ARENA_FLAG_PREV_FREE)
409 pFree = *((ARENA_FREE **)pArena - 1);
410 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
411 /* Remove it from the free list */
412 pFree->next->prev = pFree->prev;
413 pFree->prev->next = pFree->next;
415 else pFree = (ARENA_FREE *)pArena;
417 /* Create a free block */
419 HEAP_CreateFreeBlock( subheap, pFree, size );
420 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
421 if ((char *)pFree + size < (char *)subheap + subheap->size)
422 return; /* Not the last block, so nothing more to do */
424 /* Free the whole sub-heap if it's empty and not the original one */
426 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
427 (subheap != &subheap->heap->subheap))
430 SUBHEAP *pPrev = &subheap->heap->subheap;
431 /* Remove the free block from the list */
432 pFree->next->prev = pFree->prev;
433 pFree->prev->next = pFree->next;
434 /* Remove the subheap from the list */
435 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
436 if (pPrev) pPrev->next = subheap->next;
437 /* Free the memory */
439 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&subheap, &size, MEM_RELEASE );
443 /* Decommit the end of the heap */
445 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
449 /***********************************************************************
452 * Shrink an in-use block.
454 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
456 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
458 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
459 (pArena->size & ARENA_SIZE_MASK) - size );
460 /* assign size plus previous arena flags */
461 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
465 /* Turn off PREV_FREE flag in next block */
466 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
467 if (pNext < (char *)subheap + subheap->size)
468 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
472 /***********************************************************************
475 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
476 DWORD commitSize, DWORD totalSize )
479 FREE_LIST_ENTRY *pEntry;
484 if (flags & HEAP_SHARED)
485 commitSize = totalSize; /* always commit everything in a shared heap */
486 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, address,
487 &commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
489 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
493 /* Fill the sub-heap structure */
495 subheap = (SUBHEAP *)address;
496 subheap->heap = heap;
497 subheap->size = totalSize;
498 subheap->commitSize = commitSize;
499 subheap->magic = SUBHEAP_MAGIC;
501 if ( subheap != (SUBHEAP *)heap )
503 /* If this is a secondary subheap, insert it into list */
505 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
506 subheap->next = heap->subheap.next;
507 heap->subheap.next = subheap;
511 /* If this is a primary subheap, initialize main heap */
513 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
514 subheap->next = NULL;
517 heap->magic = HEAP_MAGIC;
519 /* Build the free lists */
521 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
523 pEntry->size = HEAP_freeListSizes[i];
524 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
525 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
526 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
527 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
528 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
529 pEntry->arena.magic = ARENA_FREE_MAGIC;
532 /* Initialize critical section */
534 RtlInitializeCriticalSection( &heap->critSection );
535 if (flags & HEAP_SHARED)
536 MakeCriticalSectionGlobal( &heap->critSection ); /* FIXME: dll separation */
539 /* Create the first free block */
541 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
542 subheap->size - subheap->headerSize );
547 /***********************************************************************
550 * Create a sub-heap of the given size.
551 * If heap == NULL, creates a main heap.
553 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
554 DWORD commitSize, DWORD totalSize )
556 LPVOID address = base;
558 /* round-up sizes on a 64K boundary */
559 totalSize = (totalSize + 0xffff) & 0xffff0000;
560 commitSize = (commitSize + 0xffff) & 0xffff0000;
561 if (!commitSize) commitSize = 0x10000;
562 if (totalSize < commitSize) totalSize = commitSize;
566 /* allocate the memory block */
567 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, NULL, &totalSize,
568 MEM_RESERVE, PAGE_EXECUTE_READWRITE ))
570 WARN("Could not allocate %08lx bytes\n", totalSize );
575 /* Initialize subheap */
577 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
578 address, flags, commitSize, totalSize ))
581 if (!base) NtFreeVirtualMemory( GetCurrentProcess(), &address, &size, MEM_RELEASE );
585 return (SUBHEAP *)address;
589 /***********************************************************************
592 * Find a free block at least as large as the requested size, and make sure
593 * the requested size is committed.
595 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
596 SUBHEAP **ppSubHeap )
600 FREE_LIST_ENTRY *pEntry = heap->freeList;
602 /* Find a suitable free list, and in it find a block large enough */
604 while (pEntry->size < size) pEntry++;
605 pArena = pEntry->arena.next;
606 while (pArena != &heap->freeList[0].arena)
608 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
609 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
610 if (arena_size >= size)
612 subheap = HEAP_FindSubHeap( heap, pArena );
613 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
614 + size + HEAP_MIN_BLOCK_SIZE))
616 *ppSubHeap = subheap;
619 pArena = pArena->next;
622 /* If no block was found, attempt to grow the heap */
624 if (!(heap->flags & HEAP_GROWABLE))
626 WARN("Not enough space in heap %08lx for %08lx bytes\n",
630 /* make sure that we have a big enough size *committed* to fit another
631 * last free arena in !
632 * So just one heap struct, one first free arena which will eventually
633 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
634 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
635 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
636 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
637 max( HEAP_DEF_SIZE, size ) )))
640 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
641 (DWORD)subheap, size, (DWORD)heap );
643 *ppSubHeap = subheap;
644 return (ARENA_FREE *)(subheap + 1);
648 /***********************************************************************
649 * HEAP_IsValidArenaPtr
651 * Check that the pointer is inside the range possible for arenas.
653 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
656 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
657 if (!subheap) return FALSE;
658 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
659 if (subheap != &heap->subheap) return FALSE;
660 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
661 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
666 /***********************************************************************
667 * HEAP_ValidateFreeArena
669 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
671 char *heapEnd = (char *)subheap + subheap->size;
673 /* Check for unaligned pointers */
674 if ( (long)pArena % ALIGNMENT != 0 )
676 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
677 (DWORD)subheap->heap, (DWORD)pArena );
681 /* Check magic number */
682 if (pArena->magic != ARENA_FREE_MAGIC)
684 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
685 (DWORD)subheap->heap, (DWORD)pArena );
688 /* Check size flags */
689 if (!(pArena->size & ARENA_FLAG_FREE) ||
690 (pArena->size & ARENA_FLAG_PREV_FREE))
692 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
693 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
695 /* Check arena size */
696 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
698 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
699 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
702 /* Check that next pointer is valid */
703 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
705 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
706 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
709 /* Check that next arena is free */
710 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
711 (pArena->next->magic != ARENA_FREE_MAGIC))
713 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
714 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
717 /* Check that prev pointer is valid */
718 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
720 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
721 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
724 /* Check that prev arena is free */
725 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
726 (pArena->prev->magic != ARENA_FREE_MAGIC))
728 /* this often means that the prev arena got overwritten
729 * by a memory write before that prev arena */
730 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
731 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
734 /* Check that next block has PREV_FREE flag */
735 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
737 if (!(*(DWORD *)((char *)(pArena + 1) +
738 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
740 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
741 (DWORD)subheap->heap, (DWORD)pArena );
744 /* Check next block back pointer */
745 if (*((ARENA_FREE **)((char *)(pArena + 1) +
746 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
748 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
749 (DWORD)subheap->heap, (DWORD)pArena,
750 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
758 /***********************************************************************
759 * HEAP_ValidateInUseArena
761 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
763 char *heapEnd = (char *)subheap + subheap->size;
765 /* Check for unaligned pointers */
766 if ( (long)pArena % ALIGNMENT != 0 )
768 if ( quiet == NOISY )
770 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
771 (DWORD)subheap->heap, (DWORD)pArena );
772 if ( TRACE_ON(heap) )
773 HEAP_Dump( subheap->heap );
775 else if ( WARN_ON(heap) )
777 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
778 (DWORD)subheap->heap, (DWORD)pArena );
779 if ( TRACE_ON(heap) )
780 HEAP_Dump( subheap->heap );
785 /* Check magic number */
786 if (pArena->magic != ARENA_INUSE_MAGIC)
788 if (quiet == NOISY) {
789 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
790 (DWORD)subheap->heap, (DWORD)pArena );
792 HEAP_Dump( subheap->heap );
793 } else if (WARN_ON(heap)) {
794 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
795 (DWORD)subheap->heap, (DWORD)pArena );
797 HEAP_Dump( subheap->heap );
801 /* Check size flags */
802 if (pArena->size & ARENA_FLAG_FREE)
804 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
805 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
807 /* Check arena size */
808 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
810 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
811 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
814 /* Check next arena PREV_FREE flag */
815 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
816 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
818 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
819 (DWORD)subheap->heap, (DWORD)pArena );
822 /* Check prev free arena */
823 if (pArena->size & ARENA_FLAG_PREV_FREE)
825 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
826 /* Check prev pointer */
827 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
829 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
830 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
833 /* Check that prev arena is free */
834 if (!(pPrev->size & ARENA_FLAG_FREE) ||
835 (pPrev->magic != ARENA_FREE_MAGIC))
837 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
838 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
841 /* Check that prev arena is really the previous block */
842 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
844 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
845 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
853 /***********************************************************************
854 * HEAP_IsRealArena [Internal]
855 * Validates a block is a valid arena.
861 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
862 DWORD flags, /* [in] Bit flags that control access during operation */
863 LPCVOID block, /* [in] Optional pointer to memory block to validate */
864 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
865 * does not complain */
870 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
872 ERR("Invalid heap %p!\n", heapPtr );
876 flags &= HEAP_NO_SERIALIZE;
877 flags |= heapPtr->flags;
878 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
879 if (!(flags & HEAP_NO_SERIALIZE))
880 RtlEnterCriticalSection( &heapPtr->critSection );
884 /* Only check this single memory block */
886 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
887 ((char *)block < (char *)subheap + subheap->headerSize
888 + sizeof(ARENA_INUSE)))
891 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
892 else if (WARN_ON(heap))
893 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
896 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
898 if (!(flags & HEAP_NO_SERIALIZE))
899 RtlLeaveCriticalSection( &heapPtr->critSection );
903 subheap = &heapPtr->subheap;
904 while (subheap && ret)
906 char *ptr = (char *)subheap + subheap->headerSize;
907 while (ptr < (char *)subheap + subheap->size)
909 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
911 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
915 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
919 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
923 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
926 subheap = subheap->next;
929 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
934 /***********************************************************************
935 * RtlCreateHeap (NTDLL.@)
937 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
938 PVOID unknown, PRTL_HEAP_DEFINITION definition )
942 /* Allocate the heap block */
946 totalSize = HEAP_DEF_SIZE;
947 flags |= HEAP_GROWABLE;
950 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
952 /* link it into the per-process heap list */
955 HEAP *heapPtr = subheap->heap;
956 RtlLockHeap( processHeap );
957 heapPtr->next = firstHeap;
959 RtlUnlockHeap( processHeap );
961 else /* assume the first heap we create is the process main heap */
963 set_process_heap( (HANDLE)subheap->heap );
965 return (HANDLE)subheap;
969 /***********************************************************************
970 * RtlDestroyHeap (NTDLL.@)
972 HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
974 HEAP *heapPtr = HEAP_GetPtr( heap );
977 TRACE("%08x\n", heap );
978 if (!heapPtr) return heap;
980 if (heap == processHeap) return heap; /* cannot delete the main process heap */
981 else /* remove it from the per-process list */
984 RtlLockHeap( processHeap );
986 while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
987 if (*pptr) *pptr = (*pptr)->next;
988 RtlUnlockHeap( processHeap );
991 RtlDeleteCriticalSection( &heapPtr->critSection );
992 subheap = &heapPtr->subheap;
995 SUBHEAP *next = subheap->next;
997 void *addr = subheap;
998 NtFreeVirtualMemory( GetCurrentProcess(), &addr, &size, MEM_RELEASE );
1005 /***********************************************************************
1006 * RtlAllocateHeap (NTDLL.@)
1008 * NOTE: does not set last error.
1010 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
1013 ARENA_INUSE *pInUse;
1015 HEAP *heapPtr = HEAP_GetPtr( heap );
1017 /* Validate the parameters */
1019 if (!heapPtr) return NULL;
1020 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1021 flags |= heapPtr->flags;
1022 size = ROUND_SIZE(size);
1023 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1025 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1026 /* Locate a suitable free block */
1028 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1030 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1031 heap, flags, size );
1032 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1033 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1037 /* Remove the arena from the free list */
1039 pArena->next->prev = pArena->prev;
1040 pArena->prev->next = pArena->next;
1042 /* Build the in-use arena */
1044 pInUse = (ARENA_INUSE *)pArena;
1046 /* in-use arena is smaller than free arena,
1047 * so we have to add the difference to the size */
1048 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1049 pInUse->magic = ARENA_INUSE_MAGIC;
1051 /* Shrink the block */
1053 HEAP_ShrinkBlock( subheap, pInUse, size );
1055 if (flags & HEAP_ZERO_MEMORY)
1056 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1057 else if (TRACE_ON(heap))
1058 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1060 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1062 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1063 heap, flags, size, (DWORD)(pInUse + 1) );
1064 return (LPVOID)(pInUse + 1);
1068 /***********************************************************************
1069 * RtlFreeHeap (NTDLL.@)
1071 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1073 ARENA_INUSE *pInUse;
1075 HEAP *heapPtr = HEAP_GetPtr( heap );
1077 /* Validate the parameters */
1079 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1082 set_status( STATUS_INVALID_HANDLE );
1086 flags &= HEAP_NO_SERIALIZE;
1087 flags |= heapPtr->flags;
1088 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1089 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1091 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1092 set_status( STATUS_INVALID_PARAMETER );
1093 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1094 heap, flags, (DWORD)ptr );
1098 /* Turn the block into a free block */
1100 pInUse = (ARENA_INUSE *)ptr - 1;
1101 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1102 HEAP_MakeInUseBlockFree( subheap, pInUse );
1104 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1106 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1107 heap, flags, (DWORD)ptr );
1112 /***********************************************************************
1113 * RtlReAllocateHeap (NTDLL.@)
1115 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1117 ARENA_INUSE *pArena;
1122 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1123 if (!(heapPtr = HEAP_GetPtr( heap )))
1125 set_status( STATUS_INVALID_HANDLE );
1129 /* Validate the parameters */
1131 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1132 HEAP_REALLOC_IN_PLACE_ONLY;
1133 flags |= heapPtr->flags;
1134 size = ROUND_SIZE(size);
1135 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1137 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1138 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1140 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1141 set_status( STATUS_INVALID_PARAMETER );
1142 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1143 heap, flags, (DWORD)ptr, size );
1147 /* Check if we need to grow the block */
1149 pArena = (ARENA_INUSE *)ptr - 1;
1150 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1151 oldSize = (pArena->size & ARENA_SIZE_MASK);
1154 char *pNext = (char *)(pArena + 1) + oldSize;
1155 if ((pNext < (char *)subheap + subheap->size) &&
1156 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1157 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1159 /* The next block is free and large enough */
1160 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1161 pFree->next->prev = pFree->prev;
1162 pFree->prev->next = pFree->next;
1163 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1164 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1165 + size + HEAP_MIN_BLOCK_SIZE))
1167 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1168 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1169 set_status( STATUS_NO_MEMORY );
1172 HEAP_ShrinkBlock( subheap, pArena, size );
1174 else /* Do it the hard way */
1177 ARENA_INUSE *pInUse;
1178 SUBHEAP *newsubheap;
1180 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1181 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1183 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1184 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1185 set_status( STATUS_NO_MEMORY );
1189 /* Build the in-use arena */
1191 pNew->next->prev = pNew->prev;
1192 pNew->prev->next = pNew->next;
1193 pInUse = (ARENA_INUSE *)pNew;
1194 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1195 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1196 pInUse->magic = ARENA_INUSE_MAGIC;
1197 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1198 memcpy( pInUse + 1, pArena + 1, oldSize );
1200 /* Free the previous block */
1202 HEAP_MakeInUseBlockFree( subheap, pArena );
1203 subheap = newsubheap;
1207 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1209 /* Clear the extra bytes if needed */
1213 if (flags & HEAP_ZERO_MEMORY)
1214 memset( (char *)(pArena + 1) + oldSize, 0,
1215 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1216 else if (TRACE_ON(heap))
1217 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1218 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1221 /* Return the new arena */
1223 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1225 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1226 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1227 return (LPVOID)(pArena + 1);
1231 /***********************************************************************
1232 * RtlCompactHeap (NTDLL.@)
1234 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1241 /***********************************************************************
1242 * RtlLockHeap (NTDLL.@)
1244 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1246 HEAP *heapPtr = HEAP_GetPtr( heap );
1247 if (!heapPtr) return FALSE;
1248 RtlEnterCriticalSection( &heapPtr->critSection );
1253 /***********************************************************************
1254 * RtlUnlockHeap (NTDLL.@)
1256 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1258 HEAP *heapPtr = HEAP_GetPtr( heap );
1259 if (!heapPtr) return FALSE;
1260 RtlLeaveCriticalSection( &heapPtr->critSection );
1265 /***********************************************************************
1266 * RtlSizeHeap (NTDLL.@)
1268 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1271 HEAP *heapPtr = HEAP_GetPtr( heap );
1275 set_status( STATUS_INVALID_HANDLE );
1278 flags &= HEAP_NO_SERIALIZE;
1279 flags |= heapPtr->flags;
1280 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1281 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1283 set_status( STATUS_INVALID_PARAMETER );
1288 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1289 ret = pArena->size & ARENA_SIZE_MASK;
1291 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1293 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1294 heap, flags, (DWORD)ptr, ret );
1299 /***********************************************************************
1300 * RtlValidateHeap (NTDLL.@)
1302 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, LPCVOID block )
1304 HEAP *heapPtr = HEAP_GetPtr( heap );
1305 if (!heapPtr) return FALSE;
1306 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1310 /***********************************************************************
1311 * RtlWalkHeap (NTDLL.@)
1313 * FIXME: the PROCESS_HEAP_ENTRY flag values seem different between this
1314 * function and HeapWalk. To be checked.
1316 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1318 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1319 HEAP *heapPtr = HEAP_GetPtr(heap);
1320 SUBHEAP *sub, *currentheap = NULL;
1323 int region_index = 0;
1325 FIXME( "not fully compatible\n" );
1327 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1329 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1331 /* set ptr to the next arena to be examined */
1333 if (!entry->lpData) /* first call (init) ? */
1335 TRACE("begin walking of heap 0x%08x.\n", heap);
1336 currentheap = &heapPtr->subheap;
1337 ptr = (char*)currentheap + currentheap->headerSize;
1341 ptr = entry->lpData;
1342 sub = &heapPtr->subheap;
1345 if (((char *)ptr >= (char *)sub) &&
1346 ((char *)ptr < (char *)sub + sub->size))
1354 if (currentheap == NULL)
1356 ERR("no matching subheap found, shouldn't happen !\n");
1357 ret = STATUS_NO_MORE_ENTRIES;
1361 ptr += entry->cbData; /* point to next arena */
1362 if (ptr > (char *)currentheap + currentheap->size - 1)
1363 { /* proceed with next subheap */
1364 if (!(currentheap = currentheap->next))
1365 { /* successfully finished */
1366 TRACE("end reached.\n");
1367 ret = STATUS_NO_MORE_ENTRIES;
1370 ptr = (char*)currentheap + currentheap->headerSize;
1375 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1377 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1379 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1381 entry->lpData = pArena + 1;
1382 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1383 entry->cbOverhead = sizeof(ARENA_FREE);
1384 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1388 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1390 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1392 entry->lpData = pArena + 1;
1393 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1394 entry->cbOverhead = sizeof(ARENA_INUSE);
1395 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1396 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1397 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1400 entry->iRegionIndex = region_index;
1402 /* first element of heap ? */
1403 if (ptr == (char *)(currentheap + currentheap->headerSize))
1405 entry->wFlags |= PROCESS_HEAP_REGION;
1406 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1407 entry->u.Region.dwUnCommittedSize =
1408 currentheap->size - currentheap->commitSize;
1409 entry->u.Region.lpFirstBlock = /* first valid block */
1410 currentheap + currentheap->headerSize;
1411 entry->u.Region.lpLastBlock = /* first invalid block */
1412 currentheap + currentheap->size;
1414 ret = STATUS_SUCCESS;
1417 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1422 /***********************************************************************
1423 * RtlGetProcessHeaps (NTDLL.@)
1425 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1430 if (!processHeap) return 0; /* should never happen */
1431 total = 1; /* main heap */
1432 RtlLockHeap( processHeap );
1433 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1436 *heaps++ = (HANDLE)processHeap;
1437 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1439 RtlUnlockHeap( processHeap );