Implemented the ntdll virtual memory functions, and made the kernel
[wine/multimedia.git] / dlls / ntdll / heap.c
blob736750338546001cb57cf98b4b79d607da27729f
1 /*
2 * Win32 heap functions
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
22 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "winternl.h"
30 #include "wine/winbase16.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winnt.h"
34 #include "heap.h"
35 #include "thread.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
43 * require it.
46 typedef struct tagARENA_INUSE
48 DWORD size; /* Block size; must be the first field */
49 DWORD magic; /* Magic number */
50 } ARENA_INUSE;
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 */
58 } ARENA_FREE;
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
83 typedef struct
85 DWORD size;
86 ARENA_FREE arena;
87 } FREE_LIST_ENTRY;
89 struct tagHEAP;
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 */
99 } SUBHEAP;
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 */
111 } HEAP;
113 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
115 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
116 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
117 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
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 ) );
131 #else
132 /* cannot use SetLastError, do it manually */
133 NtCurrentTeb()->last_error = RtlNtStatusToDosError( status );
134 #endif
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 */
142 processHeap = heap;
146 /***********************************************************************
147 * HEAP_Dump
149 void HEAP_Dump( HEAP *heap )
151 int i;
152 SUBHEAP *subheap;
153 char *ptr;
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;
173 while (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;
202 else
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 /***********************************************************************
221 * HEAP_GetPtr
222 * RETURNS
223 * Pointer to the heap
224 * NULL: Failure
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 );
233 return NULL;
235 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
237 HEAP_Dump( heapPtr );
238 assert( FALSE );
239 return NULL;
241 return 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 /***********************************************************************
263 * HEAP_FindSubHeap
264 * Find the sub-heap containing a given address.
266 * RETURNS
267 * Pointer: Success
268 * NULL: Failure
270 static SUBHEAP *HEAP_FindSubHeap(
271 HEAP *heap, /* [in] Heap pointer */
272 LPCVOID ptr /* [in] Address */
274 SUBHEAP *sub = &heap->subheap;
275 while (sub)
277 if (((char *)ptr >= (char *)sub) &&
278 ((char *)ptr < (char *)sub + sub->size)) return sub;
279 sub = sub->next;
281 return NULL;
285 /***********************************************************************
286 * HEAP_Commit
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 );
303 return FALSE;
305 subheap->commitSize += size;
306 return TRUE;
310 /***********************************************************************
311 * HEAP_Decommit
313 * If possible, decommit the heap storage from (including) 'ptr'.
315 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
317 void *addr;
318 ULONG decommit_size;
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 );
331 return FALSE;
333 subheap->commitSize -= decommit_size;
334 return TRUE;
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 )
346 ARENA_FREE *pFree;
348 /* Create a free arena */
350 pFree = (ARENA_FREE *)ptr;
351 pFree->magic = ARENA_FREE_MAGIC;
353 /* If debugging, erase the freed block content */
355 if (TRACE_ON(heap))
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);
374 if (TRACE_ON(heap))
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 )
402 ARENA_FREE *pFree;
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))
429 SUBHEAP *pPrev = &subheap->heap->subheap;
430 /* Remove the free block from the list */
431 pFree->next->prev = pFree->prev;
432 pFree->prev->next = pFree->next;
433 /* Remove the subheap from the list */
434 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
435 if (pPrev) pPrev->next = subheap->next;
436 /* Free the memory */
437 subheap->magic = 0;
438 VirtualFree( subheap, 0, MEM_RELEASE );
439 return;
442 /* Decommit the end of the heap */
444 if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
448 /***********************************************************************
449 * HEAP_ShrinkBlock
451 * Shrink an in-use block.
453 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
455 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
457 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
458 (pArena->size & ARENA_SIZE_MASK) - size );
459 /* assign size plus previous arena flags */
460 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
462 else
464 /* Turn off PREV_FREE flag in next block */
465 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
466 if (pNext < (char *)subheap + subheap->size)
467 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
471 /***********************************************************************
472 * HEAP_InitSubHeap
474 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
475 DWORD commitSize, DWORD totalSize )
477 SUBHEAP *subheap;
478 FREE_LIST_ENTRY *pEntry;
479 int i;
481 /* Commit memory */
483 if (flags & HEAP_SHARED)
484 commitSize = totalSize; /* always commit everything in a shared heap */
485 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, address,
486 &commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
488 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
489 return FALSE;
492 /* Fill the sub-heap structure */
494 subheap = (SUBHEAP *)address;
495 subheap->heap = heap;
496 subheap->size = totalSize;
497 subheap->commitSize = commitSize;
498 subheap->magic = SUBHEAP_MAGIC;
500 if ( subheap != (SUBHEAP *)heap )
502 /* If this is a secondary subheap, insert it into list */
504 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
505 subheap->next = heap->subheap.next;
506 heap->subheap.next = subheap;
508 else
510 /* If this is a primary subheap, initialize main heap */
512 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
513 subheap->next = NULL;
514 heap->next = NULL;
515 heap->flags = flags;
516 heap->magic = HEAP_MAGIC;
518 /* Build the free lists */
520 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
522 pEntry->size = HEAP_freeListSizes[i];
523 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
524 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
525 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
526 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
527 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
528 pEntry->arena.magic = ARENA_FREE_MAGIC;
531 /* Initialize critical section */
533 RtlInitializeCriticalSection( &heap->critSection );
534 if (flags & HEAP_SHARED)
535 MakeCriticalSectionGlobal( &heap->critSection ); /* FIXME: dll separation */
538 /* Create the first free block */
540 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
541 subheap->size - subheap->headerSize );
543 return TRUE;
546 /***********************************************************************
547 * HEAP_CreateSubHeap
549 * Create a sub-heap of the given size.
550 * If heap == NULL, creates a main heap.
552 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
553 DWORD commitSize, DWORD totalSize )
555 LPVOID address = base;
557 /* round-up sizes on a 64K boundary */
558 totalSize = (totalSize + 0xffff) & 0xffff0000;
559 commitSize = (commitSize + 0xffff) & 0xffff0000;
560 if (!commitSize) commitSize = 0x10000;
561 if (totalSize < commitSize) totalSize = commitSize;
563 if (!address)
565 /* allocate the memory block */
566 if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, NULL, &totalSize,
567 MEM_RESERVE, PAGE_EXECUTE_READWRITE ))
569 WARN("Could not allocate %08lx bytes\n", totalSize );
570 return NULL;
574 /* Initialize subheap */
576 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
577 address, flags, commitSize, totalSize ))
579 ULONG size = 0;
580 if (!base) NtFreeVirtualMemory( GetCurrentProcess(), &address, &size, MEM_RELEASE );
581 return NULL;
584 return (SUBHEAP *)address;
588 /***********************************************************************
589 * HEAP_FindFreeBlock
591 * Find a free block at least as large as the requested size, and make sure
592 * the requested size is committed.
594 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
595 SUBHEAP **ppSubHeap )
597 SUBHEAP *subheap;
598 ARENA_FREE *pArena;
599 FREE_LIST_ENTRY *pEntry = heap->freeList;
601 /* Find a suitable free list, and in it find a block large enough */
603 while (pEntry->size < size) pEntry++;
604 pArena = pEntry->arena.next;
605 while (pArena != &heap->freeList[0].arena)
607 DWORD arena_size = (pArena->size & ARENA_SIZE_MASK) +
608 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
609 if (arena_size >= size)
611 subheap = HEAP_FindSubHeap( heap, pArena );
612 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
613 + size + HEAP_MIN_BLOCK_SIZE))
614 return NULL;
615 *ppSubHeap = subheap;
616 return pArena;
618 pArena = pArena->next;
621 /* If no block was found, attempt to grow the heap */
623 if (!(heap->flags & HEAP_GROWABLE))
625 WARN("Not enough space in heap %08lx for %08lx bytes\n",
626 (DWORD)heap, size );
627 return NULL;
629 /* make sure that we have a big enough size *committed* to fit another
630 * last free arena in !
631 * So just one heap struct, one first free arena which will eventually
632 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
633 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
634 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + HEAP_MIN_BLOCK_SIZE;
635 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
636 max( HEAP_DEF_SIZE, size ) )))
637 return NULL;
639 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
640 (DWORD)subheap, size, (DWORD)heap );
642 *ppSubHeap = subheap;
643 return (ARENA_FREE *)(subheap + 1);
647 /***********************************************************************
648 * HEAP_IsValidArenaPtr
650 * Check that the pointer is inside the range possible for arenas.
652 static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
654 int i;
655 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
656 if (!subheap) return FALSE;
657 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
658 if (subheap != &heap->subheap) return FALSE;
659 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
660 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
661 return FALSE;
665 /***********************************************************************
666 * HEAP_ValidateFreeArena
668 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
670 char *heapEnd = (char *)subheap + subheap->size;
672 /* Check for unaligned pointers */
673 if ( (long)pArena % ALIGNMENT != 0 )
675 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
676 (DWORD)subheap->heap, (DWORD)pArena );
677 return FALSE;
680 /* Check magic number */
681 if (pArena->magic != ARENA_FREE_MAGIC)
683 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
684 (DWORD)subheap->heap, (DWORD)pArena );
685 return FALSE;
687 /* Check size flags */
688 if (!(pArena->size & ARENA_FLAG_FREE) ||
689 (pArena->size & ARENA_FLAG_PREV_FREE))
691 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
692 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
694 /* Check arena size */
695 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
697 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
698 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
699 return FALSE;
701 /* Check that next pointer is valid */
702 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
704 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
705 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
706 return FALSE;
708 /* Check that next arena is free */
709 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
710 (pArena->next->magic != ARENA_FREE_MAGIC))
712 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
713 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
714 return FALSE;
716 /* Check that prev pointer is valid */
717 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
719 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
720 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
721 return FALSE;
723 /* Check that prev arena is free */
724 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
725 (pArena->prev->magic != ARENA_FREE_MAGIC))
727 /* this often means that the prev arena got overwritten
728 * by a memory write before that prev arena */
729 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
730 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
731 return FALSE;
733 /* Check that next block has PREV_FREE flag */
734 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
736 if (!(*(DWORD *)((char *)(pArena + 1) +
737 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
739 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
740 (DWORD)subheap->heap, (DWORD)pArena );
741 return FALSE;
743 /* Check next block back pointer */
744 if (*((ARENA_FREE **)((char *)(pArena + 1) +
745 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
747 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
748 (DWORD)subheap->heap, (DWORD)pArena,
749 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
750 return FALSE;
753 return TRUE;
757 /***********************************************************************
758 * HEAP_ValidateInUseArena
760 static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOL quiet )
762 char *heapEnd = (char *)subheap + subheap->size;
764 /* Check for unaligned pointers */
765 if ( (long)pArena % ALIGNMENT != 0 )
767 if ( quiet == NOISY )
769 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
770 (DWORD)subheap->heap, (DWORD)pArena );
771 if ( TRACE_ON(heap) )
772 HEAP_Dump( subheap->heap );
774 else if ( WARN_ON(heap) )
776 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
777 (DWORD)subheap->heap, (DWORD)pArena );
778 if ( TRACE_ON(heap) )
779 HEAP_Dump( subheap->heap );
781 return FALSE;
784 /* Check magic number */
785 if (pArena->magic != ARENA_INUSE_MAGIC)
787 if (quiet == NOISY) {
788 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
789 (DWORD)subheap->heap, (DWORD)pArena );
790 if (TRACE_ON(heap))
791 HEAP_Dump( subheap->heap );
792 } else if (WARN_ON(heap)) {
793 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
794 (DWORD)subheap->heap, (DWORD)pArena );
795 if (TRACE_ON(heap))
796 HEAP_Dump( subheap->heap );
798 return FALSE;
800 /* Check size flags */
801 if (pArena->size & ARENA_FLAG_FREE)
803 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
804 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
806 /* Check arena size */
807 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
809 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
810 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
811 return FALSE;
813 /* Check next arena PREV_FREE flag */
814 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
815 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
817 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
818 (DWORD)subheap->heap, (DWORD)pArena );
819 return FALSE;
821 /* Check prev free arena */
822 if (pArena->size & ARENA_FLAG_PREV_FREE)
824 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
825 /* Check prev pointer */
826 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
828 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
829 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
830 return FALSE;
832 /* Check that prev arena is free */
833 if (!(pPrev->size & ARENA_FLAG_FREE) ||
834 (pPrev->magic != ARENA_FREE_MAGIC))
836 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
837 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
838 return FALSE;
840 /* Check that prev arena is really the previous block */
841 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
843 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
844 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
845 return FALSE;
848 return TRUE;
852 /***********************************************************************
853 * HEAP_IsRealArena [Internal]
854 * Validates a block is a valid arena.
856 * RETURNS
857 * TRUE: Success
858 * FALSE: Failure
860 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
861 DWORD flags, /* [in] Bit flags that control access during operation */
862 LPCVOID block, /* [in] Optional pointer to memory block to validate */
863 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
864 * does not complain */
866 SUBHEAP *subheap;
867 BOOL ret = TRUE;
869 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
871 ERR("Invalid heap %p!\n", heapPtr );
872 return FALSE;
875 flags &= HEAP_NO_SERIALIZE;
876 flags |= heapPtr->flags;
877 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
878 if (!(flags & HEAP_NO_SERIALIZE))
879 RtlEnterCriticalSection( &heapPtr->critSection );
881 if (block)
883 /* Only check this single memory block */
885 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
886 ((char *)block < (char *)subheap + subheap->headerSize
887 + sizeof(ARENA_INUSE)))
889 if (quiet == NOISY)
890 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
891 else if (WARN_ON(heap))
892 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
893 ret = FALSE;
894 } else
895 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
897 if (!(flags & HEAP_NO_SERIALIZE))
898 RtlLeaveCriticalSection( &heapPtr->critSection );
899 return ret;
902 subheap = &heapPtr->subheap;
903 while (subheap && ret)
905 char *ptr = (char *)subheap + subheap->headerSize;
906 while (ptr < (char *)subheap + subheap->size)
908 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
910 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
911 ret = FALSE;
912 break;
914 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
916 else
918 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
919 ret = FALSE;
920 break;
922 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
925 subheap = subheap->next;
928 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
929 return ret;
933 /***********************************************************************
934 * RtlCreateHeap (NTDLL.@)
936 HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, ULONG totalSize, ULONG commitSize,
937 PVOID unknown, PRTL_HEAP_DEFINITION definition )
939 SUBHEAP *subheap;
941 /* Allocate the heap block */
943 if (!totalSize)
945 totalSize = HEAP_DEF_SIZE;
946 flags |= HEAP_GROWABLE;
949 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
951 /* link it into the per-process heap list */
952 if (processHeap)
954 HEAP *heapPtr = subheap->heap;
955 RtlLockHeap( processHeap );
956 heapPtr->next = firstHeap;
957 firstHeap = heapPtr;
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 );
974 SUBHEAP *subheap;
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 */
982 HEAP **pptr;
983 RtlLockHeap( processHeap );
984 pptr = &firstHeap;
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;
992 while (subheap)
994 SUBHEAP *next = subheap->next;
995 ULONG size = 0;
996 void *addr = subheap;
997 NtFreeVirtualMemory( GetCurrentProcess(), &addr, &size, MEM_RELEASE );
998 subheap = next;
1000 return 0;
1004 /***********************************************************************
1005 * RtlAllocateHeap (NTDLL.@)
1007 * NOTE: does not set last error.
1009 PVOID WINAPI RtlAllocateHeap( HANDLE heap, ULONG flags, ULONG size )
1011 ARENA_FREE *pArena;
1012 ARENA_INUSE *pInUse;
1013 SUBHEAP *subheap;
1014 HEAP *heapPtr = HEAP_GetPtr( heap );
1016 /* Validate the parameters */
1018 if (!heapPtr) return NULL;
1019 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1020 flags |= heapPtr->flags;
1021 size = ROUND_SIZE(size);
1022 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1024 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1025 /* Locate a suitable free block */
1027 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
1029 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1030 heap, flags, size );
1031 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1032 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1033 return NULL;
1036 /* Remove the arena from the free list */
1038 pArena->next->prev = pArena->prev;
1039 pArena->prev->next = pArena->next;
1041 /* Build the in-use arena */
1043 pInUse = (ARENA_INUSE *)pArena;
1045 /* in-use arena is smaller than free arena,
1046 * so we have to add the difference to the size */
1047 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1048 pInUse->magic = ARENA_INUSE_MAGIC;
1050 /* Shrink the block */
1052 HEAP_ShrinkBlock( subheap, pInUse, size );
1054 if (flags & HEAP_ZERO_MEMORY)
1055 memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
1056 else if (TRACE_ON(heap))
1057 memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
1059 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1061 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1062 heap, flags, size, (DWORD)(pInUse + 1) );
1063 return (LPVOID)(pInUse + 1);
1067 /***********************************************************************
1068 * RtlFreeHeap (NTDLL.@)
1070 BOOLEAN WINAPI RtlFreeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1072 ARENA_INUSE *pInUse;
1073 SUBHEAP *subheap;
1074 HEAP *heapPtr = HEAP_GetPtr( heap );
1076 /* Validate the parameters */
1078 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1079 if (!heapPtr)
1081 set_status( STATUS_INVALID_HANDLE );
1082 return FALSE;
1085 flags &= HEAP_NO_SERIALIZE;
1086 flags |= heapPtr->flags;
1087 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1088 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1090 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1091 set_status( STATUS_INVALID_PARAMETER );
1092 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1093 heap, flags, (DWORD)ptr );
1094 return FALSE;
1097 /* Turn the block into a free block */
1099 pInUse = (ARENA_INUSE *)ptr - 1;
1100 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1101 HEAP_MakeInUseBlockFree( subheap, pInUse );
1103 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1105 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1106 heap, flags, (DWORD)ptr );
1107 return TRUE;
1111 /***********************************************************************
1112 * RtlReAllocateHeap (NTDLL.@)
1114 PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, ULONG size )
1116 ARENA_INUSE *pArena;
1117 DWORD oldSize;
1118 HEAP *heapPtr;
1119 SUBHEAP *subheap;
1121 if (!ptr) return RtlAllocateHeap( heap, flags, size ); /* FIXME: correct? */
1122 if (!(heapPtr = HEAP_GetPtr( heap )))
1124 set_status( STATUS_INVALID_HANDLE );
1125 return FALSE;
1128 /* Validate the parameters */
1130 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1131 HEAP_REALLOC_IN_PLACE_ONLY;
1132 flags |= heapPtr->flags;
1133 size = ROUND_SIZE(size);
1134 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1136 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1137 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1139 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1140 set_status( STATUS_INVALID_PARAMETER );
1141 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1142 heap, flags, (DWORD)ptr, size );
1143 return NULL;
1146 /* Check if we need to grow the block */
1148 pArena = (ARENA_INUSE *)ptr - 1;
1149 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1150 oldSize = (pArena->size & ARENA_SIZE_MASK);
1151 if (size > oldSize)
1153 char *pNext = (char *)(pArena + 1) + oldSize;
1154 if ((pNext < (char *)subheap + subheap->size) &&
1155 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1156 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1158 /* The next block is free and large enough */
1159 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1160 pFree->next->prev = pFree->prev;
1161 pFree->prev->next = pFree->next;
1162 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1163 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1164 + size + HEAP_MIN_BLOCK_SIZE))
1166 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1167 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1168 set_status( STATUS_NO_MEMORY );
1169 return NULL;
1171 HEAP_ShrinkBlock( subheap, pArena, size );
1173 else /* Do it the hard way */
1175 ARENA_FREE *pNew;
1176 ARENA_INUSE *pInUse;
1177 SUBHEAP *newsubheap;
1179 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1180 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1182 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1183 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1184 set_status( STATUS_NO_MEMORY );
1185 return NULL;
1188 /* Build the in-use arena */
1190 pNew->next->prev = pNew->prev;
1191 pNew->prev->next = pNew->next;
1192 pInUse = (ARENA_INUSE *)pNew;
1193 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1194 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1195 pInUse->magic = ARENA_INUSE_MAGIC;
1196 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1197 memcpy( pInUse + 1, pArena + 1, oldSize );
1199 /* Free the previous block */
1201 HEAP_MakeInUseBlockFree( subheap, pArena );
1202 subheap = newsubheap;
1203 pArena = pInUse;
1206 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1208 /* Clear the extra bytes if needed */
1210 if (size > oldSize)
1212 if (flags & HEAP_ZERO_MEMORY)
1213 memset( (char *)(pArena + 1) + oldSize, 0,
1214 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1215 else if (TRACE_ON(heap))
1216 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1217 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1220 /* Return the new arena */
1222 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1224 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1225 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1226 return (LPVOID)(pArena + 1);
1230 /***********************************************************************
1231 * RtlCompactHeap (NTDLL.@)
1233 ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
1235 FIXME( "stub\n" );
1236 return 0;
1240 /***********************************************************************
1241 * RtlLockHeap (NTDLL.@)
1243 BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
1245 HEAP *heapPtr = HEAP_GetPtr( heap );
1246 if (!heapPtr) return FALSE;
1247 RtlEnterCriticalSection( &heapPtr->critSection );
1248 return TRUE;
1252 /***********************************************************************
1253 * RtlUnlockHeap (NTDLL.@)
1255 BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
1257 HEAP *heapPtr = HEAP_GetPtr( heap );
1258 if (!heapPtr) return FALSE;
1259 RtlLeaveCriticalSection( &heapPtr->critSection );
1260 return TRUE;
1264 /***********************************************************************
1265 * RtlSizeHeap (NTDLL.@)
1267 ULONG WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, PVOID ptr )
1269 DWORD ret;
1270 HEAP *heapPtr = HEAP_GetPtr( heap );
1272 if (!heapPtr)
1274 set_status( STATUS_INVALID_HANDLE );
1275 return (ULONG)-1;
1277 flags &= HEAP_NO_SERIALIZE;
1278 flags |= heapPtr->flags;
1279 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1280 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1282 set_status( STATUS_INVALID_PARAMETER );
1283 ret = (ULONG)-1;
1285 else
1287 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1288 ret = pArena->size & ARENA_SIZE_MASK;
1290 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1292 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1293 heap, flags, (DWORD)ptr, ret );
1294 return ret;
1298 /***********************************************************************
1299 * RtlValidateHeap (NTDLL.@)
1301 BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, LPCVOID block )
1303 HEAP *heapPtr = HEAP_GetPtr( heap );
1304 if (!heapPtr) return FALSE;
1305 return HEAP_IsRealArena( heapPtr, flags, block, QUIET );
1309 /***********************************************************************
1310 * RtlWalkHeap (NTDLL.@)
1312 * FIXME: the PROCESS_HEAP_ENTRY flag values seem different between this
1313 * function and HeapWalk. To be checked.
1315 NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
1317 LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
1318 HEAP *heapPtr = HEAP_GetPtr(heap);
1319 SUBHEAP *sub, *currentheap = NULL;
1320 NTSTATUS ret;
1321 char *ptr;
1322 int region_index = 0;
1324 FIXME( "not fully compatible\n" );
1326 if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
1328 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
1330 /* set ptr to the next arena to be examined */
1332 if (!entry->lpData) /* first call (init) ? */
1334 TRACE("begin walking of heap 0x%08x.\n", heap);
1335 currentheap = &heapPtr->subheap;
1336 ptr = (char*)currentheap + currentheap->headerSize;
1338 else
1340 ptr = entry->lpData;
1341 sub = &heapPtr->subheap;
1342 while (sub)
1344 if (((char *)ptr >= (char *)sub) &&
1345 ((char *)ptr < (char *)sub + sub->size))
1347 currentheap = sub;
1348 break;
1350 sub = sub->next;
1351 region_index++;
1353 if (currentheap == NULL)
1355 ERR("no matching subheap found, shouldn't happen !\n");
1356 ret = STATUS_NO_MORE_ENTRIES;
1357 goto HW_end;
1360 ptr += entry->cbData; /* point to next arena */
1361 if (ptr > (char *)currentheap + currentheap->size - 1)
1362 { /* proceed with next subheap */
1363 if (!(currentheap = currentheap->next))
1364 { /* successfully finished */
1365 TRACE("end reached.\n");
1366 ret = STATUS_NO_MORE_ENTRIES;
1367 goto HW_end;
1369 ptr = (char*)currentheap + currentheap->headerSize;
1373 entry->wFlags = 0;
1374 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1376 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1378 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1380 entry->lpData = pArena + 1;
1381 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1382 entry->cbOverhead = sizeof(ARENA_FREE);
1383 entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
1385 else
1387 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1389 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1391 entry->lpData = pArena + 1;
1392 entry->cbData = pArena->size & ARENA_SIZE_MASK;
1393 entry->cbOverhead = sizeof(ARENA_INUSE);
1394 entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
1395 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1396 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1399 entry->iRegionIndex = region_index;
1401 /* first element of heap ? */
1402 if (ptr == (char *)(currentheap + currentheap->headerSize))
1404 entry->wFlags |= PROCESS_HEAP_REGION;
1405 entry->u.Region.dwCommittedSize = currentheap->commitSize;
1406 entry->u.Region.dwUnCommittedSize =
1407 currentheap->size - currentheap->commitSize;
1408 entry->u.Region.lpFirstBlock = /* first valid block */
1409 currentheap + currentheap->headerSize;
1410 entry->u.Region.lpLastBlock = /* first invalid block */
1411 currentheap + currentheap->size;
1413 ret = STATUS_SUCCESS;
1415 HW_end:
1416 if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
1417 return ret;
1421 /***********************************************************************
1422 * RtlGetProcessHeaps (NTDLL.@)
1424 ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
1426 DWORD total;
1427 HEAP *ptr;
1429 if (!processHeap) return 0; /* should never happen */
1430 total = 1; /* main heap */
1431 RtlLockHeap( processHeap );
1432 for (ptr = firstHeap; ptr; ptr = ptr->next) total++;
1433 if (total <= count)
1435 *heaps++ = (HANDLE)processHeap;
1436 for (ptr = firstHeap; ptr; ptr = ptr->next) *heaps++ = (HANDLE)ptr;
1438 RtlUnlockHeap( processHeap );
1439 return total;