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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #ifdef HAVE_VALGRIND_MEMCHECK_H
31 #include <valgrind/memcheck.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #define WIN32_NO_STATUS
41 #include "wine/list.h"
42 #include "wine/debug.h"
43 #include "wine/server.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
46 WINE_DECLARE_DEBUG_CHANNEL(heap_poison
);
48 /* Note: the heap data structures are loosely based on what Pietrek describes in his
49 * book 'Windows 95 System Programming Secrets', with some adaptations for
50 * better compatibility with NT.
53 /* FIXME: use SIZE_T for 'size' structure members, but we need to make sure
54 * that there is no unaligned accesses to structure fields.
57 typedef struct tagARENA_INUSE
59 DWORD size
; /* Block size; must be the first field */
60 DWORD magic
: 24; /* Magic number */
61 DWORD unused_bytes
: 8; /* Number of bytes in the block not used by user data (max value is HEAP_MIN_DATA_SIZE+HEAP_MIN_SHRINK_SIZE) */
64 typedef struct tagARENA_FREE
66 DWORD size
; /* Block size; must be the first field */
67 DWORD magic
; /* Magic number */
68 struct list entry
; /* Entry in free list */
73 struct list entry
; /* entry in heap large blocks list */
74 SIZE_T data_size
; /* size of user data */
75 SIZE_T block_size
; /* total size of virtual memory block */
76 DWORD pad
[2]; /* padding to ensure 16-byte alignment of data */
77 DWORD size
; /* fields for compatibility with normal arenas */
78 DWORD magic
; /* these must remain at the end of the structure */
81 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
82 #define ARENA_FLAG_PREV_FREE 0x00000002
83 #define ARENA_SIZE_MASK (~3)
84 #define ARENA_LARGE_SIZE 0xfedcba90 /* magic value for 'size' field in large blocks */
86 /* Value for arena 'magic' field */
87 #define ARENA_INUSE_MAGIC 0x455355
88 #define ARENA_FREE_MAGIC 0x45455246
89 #define ARENA_LARGE_MAGIC 0x6752614c
91 #define ARENA_INUSE_FILLER 0x55
92 #define ARENA_FREE_FILLER 0xaa
94 #define ALIGNMENT 8 /* everything is aligned on 8 byte boundaries */
95 #define LARGE_ALIGNMENT 16 /* large blocks have stricter alignment */
97 #define ROUND_SIZE(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
99 #define QUIET 1 /* Suppress messages */
100 #define NOISY 0 /* Report all errors */
102 /* minimum data size (without arenas) of an allocated block */
103 /* make sure that it's larger than a free list entry */
104 #define HEAP_MIN_DATA_SIZE (2 * sizeof(struct list))
105 /* minimum size that must remain to shrink an allocated block */
106 #define HEAP_MIN_SHRINK_SIZE (HEAP_MIN_DATA_SIZE+sizeof(ARENA_FREE))
107 /* minimum size to start allocating large blocks */
108 #define HEAP_MIN_LARGE_BLOCK_SIZE 0x7f000
110 /* Max size of the blocks on the free lists */
111 static const SIZE_T HEAP_freeListSizes
[] =
113 0x10, 0x20, 0x30, 0x40, 0x60, 0x80, 0x100, 0x200, 0x400, 0x1000, ~0UL
115 #define HEAP_NB_FREE_LISTS (sizeof(HEAP_freeListSizes)/sizeof(HEAP_freeListSizes[0]))
124 typedef struct tagSUBHEAP
126 void *base
; /* Base address of the sub-heap memory block */
127 SIZE_T size
; /* Size of the whole sub-heap */
128 SIZE_T commitSize
; /* Committed size of the sub-heap */
129 struct list entry
; /* Entry in sub-heap list */
130 struct tagHEAP
*heap
; /* Main heap structure */
131 DWORD headerSize
; /* Size of the heap header */
132 DWORD magic
; /* Magic number */
135 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
137 typedef struct tagHEAP
140 DWORD flags
; /* Heap flags */
141 DWORD force_flags
; /* Forced heap flags for debugging */
142 SUBHEAP subheap
; /* First sub-heap */
143 struct list entry
; /* Entry in process heap list */
144 struct list subheap_list
; /* Sub-heap list */
145 struct list large_list
; /* Large blocks list */
146 SIZE_T grow_size
; /* Size of next subheap for growing heap */
147 DWORD magic
; /* Magic number */
148 RTL_CRITICAL_SECTION critSection
; /* Critical section for serialization */
149 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
] DECLSPEC_ALIGN(8); /* Free lists */
152 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
154 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
155 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
157 static HEAP
*processHeap
; /* main process heap */
159 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, DWORD flags
, LPCVOID block
, BOOL quiet
);
161 /* mark a block of memory as free for debugging purposes */
162 static inline void mark_block_free( void *ptr
, SIZE_T size
)
164 if (TRACE_ON(heap
) || WARN_ON(heap
) || TRACE_ON(heap_poison
)) memset( ptr
, ARENA_FREE_FILLER
, size
);
165 #if defined(VALGRIND_MAKE_MEM_NOACCESS)
166 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr
, size
));
167 #elif defined( VALGRIND_MAKE_NOACCESS)
168 VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr
, size
));
172 /* mark a block of memory as initialized for debugging purposes */
173 static inline void mark_block_initialized( void *ptr
, SIZE_T size
)
175 #if defined(VALGRIND_MAKE_MEM_DEFINED)
176 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_DEFINED( ptr
, size
));
177 #elif defined(VALGRIND_MAKE_READABLE)
178 VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr
, size
));
182 /* mark a block of memory as uninitialized for debugging purposes */
183 static inline void mark_block_uninitialized( void *ptr
, SIZE_T size
)
185 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
186 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
187 #elif defined(VALGRIND_MAKE_WRITABLE)
188 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
190 if (TRACE_ON(heap
) || WARN_ON(heap
) || TRACE_ON(heap_poison
))
192 memset( ptr
, ARENA_INUSE_FILLER
, size
);
193 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
194 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
195 #elif defined(VALGRIND_MAKE_WRITABLE)
196 /* make it uninitialized to valgrind again */
197 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
202 /* clear contents of a block of memory */
203 static inline void clear_block( void *ptr
, SIZE_T size
)
205 mark_block_initialized( ptr
, size
);
206 memset( ptr
, 0, size
);
209 /* notify that a new block of memory has been allocated for debugging purposes */
210 static inline void notify_alloc( void *ptr
, SIZE_T size
, BOOL init
)
212 #ifdef VALGRIND_MALLOCLIKE_BLOCK
213 VALGRIND_MALLOCLIKE_BLOCK( ptr
, size
, 0, init
);
217 /* notify that a block of memory has been freed for debugging purposes */
218 static inline void notify_free( void const *ptr
)
220 #ifdef VALGRIND_FREELIKE_BLOCK
221 VALGRIND_FREELIKE_BLOCK( ptr
, 0 );
225 static void subheap_notify_free_all(SUBHEAP
const *subheap
)
227 #ifdef VALGRIND_FREELIKE_BLOCK
228 char const *ptr
= (char const *)subheap
->base
+ subheap
->headerSize
;
230 if (!RUNNING_ON_VALGRIND
) return;
232 while (ptr
< (char const *)subheap
->base
+ subheap
->size
)
234 if (*(const DWORD
*)ptr
& ARENA_FLAG_FREE
)
236 ARENA_FREE
const *pArena
= (ARENA_FREE
const *)ptr
;
237 if (pArena
->magic
!=ARENA_FREE_MAGIC
) ERR("bad free_magic @%p\n", pArena
);
238 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
242 ARENA_INUSE
const *pArena
= (ARENA_INUSE
const *)ptr
;
243 if (pArena
->magic
!=ARENA_INUSE_MAGIC
) ERR("bad inuse_magic @%p\n", pArena
);
244 notify_free(pArena
+ 1);
245 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
251 /* locate a free list entry of the appropriate size */
252 /* size is the size of the whole block including the arena header */
253 static inline unsigned int get_freelist_index( SIZE_T size
)
257 size
-= sizeof(ARENA_FREE
);
258 for (i
= 0; i
< HEAP_NB_FREE_LISTS
- 1; i
++) if (size
<= HEAP_freeListSizes
[i
]) break;
262 /* get the memory protection type to use for a given heap */
263 static inline ULONG
get_protection_type( DWORD flags
)
265 return (flags
& HEAP_CREATE_ENABLE_EXECUTE
) ? PAGE_EXECUTE_READWRITE
: PAGE_READWRITE
;
268 static RTL_CRITICAL_SECTION_DEBUG process_heap_critsect_debug
=
270 0, 0, NULL
, /* will be set later */
271 { &process_heap_critsect_debug
.ProcessLocksList
, &process_heap_critsect_debug
.ProcessLocksList
},
272 0, 0, { (DWORD_PTR
)(__FILE__
": main process heap section") }
276 /***********************************************************************
279 static void HEAP_Dump( HEAP
*heap
)
285 DPRINTF( "Heap: %p\n", heap
);
286 DPRINTF( "Next: %p Sub-heaps:", LIST_ENTRY( heap
->entry
.next
, HEAP
, entry
) );
287 LIST_FOR_EACH_ENTRY( subheap
, &heap
->subheap_list
, SUBHEAP
, entry
) DPRINTF( " %p", subheap
);
289 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
290 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
291 DPRINTF( "%p free %08lx prev=%p next=%p\n",
292 &heap
->freeList
[i
].arena
, HEAP_freeListSizes
[i
],
293 LIST_ENTRY( heap
->freeList
[i
].arena
.entry
.prev
, ARENA_FREE
, entry
),
294 LIST_ENTRY( heap
->freeList
[i
].arena
.entry
.next
, ARENA_FREE
, entry
));
296 LIST_FOR_EACH_ENTRY( subheap
, &heap
->subheap_list
, SUBHEAP
, entry
)
298 SIZE_T freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
299 DPRINTF( "\n\nSub-heap %p: base=%p size=%08lx committed=%08lx\n",
300 subheap
, subheap
->base
, subheap
->size
, subheap
->commitSize
);
302 DPRINTF( "\n Block Arena Stat Size Id\n" );
303 ptr
= (char *)subheap
->base
+ subheap
->headerSize
;
304 while (ptr
< (char *)subheap
->base
+ subheap
->size
)
306 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
308 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
309 DPRINTF( "%p %08x free %08x prev=%p next=%p\n",
310 pArena
, pArena
->magic
,
311 pArena
->size
& ARENA_SIZE_MASK
,
312 LIST_ENTRY( pArena
->entry
.prev
, ARENA_FREE
, entry
),
313 LIST_ENTRY( pArena
->entry
.next
, ARENA_FREE
, entry
) );
314 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
315 arenaSize
+= sizeof(ARENA_FREE
);
316 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
318 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
320 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
321 DPRINTF( "%p %08x Used %08x back=%p\n",
322 pArena
, pArena
->magic
, pArena
->size
& ARENA_SIZE_MASK
, *((ARENA_FREE
**)pArena
- 1) );
323 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
324 arenaSize
+= sizeof(ARENA_INUSE
);
325 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
329 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
330 DPRINTF( "%p %08x used %08x\n", pArena
, pArena
->magic
, pArena
->size
& ARENA_SIZE_MASK
);
331 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
332 arenaSize
+= sizeof(ARENA_INUSE
);
333 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
336 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
337 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
338 arenaSize
, (arenaSize
* 100) / subheap
->size
);
343 static void HEAP_DumpEntry( LPPROCESS_HEAP_ENTRY entry
)
346 TRACE( "Dumping entry %p\n", entry
);
347 TRACE( "lpData\t\t: %p\n", entry
->lpData
);
348 TRACE( "cbData\t\t: %08x\n", entry
->cbData
);
349 TRACE( "cbOverhead\t: %08x\n", entry
->cbOverhead
);
350 TRACE( "iRegionIndex\t: %08x\n", entry
->iRegionIndex
);
351 TRACE( "WFlags\t\t: ");
352 if (entry
->wFlags
& PROCESS_HEAP_REGION
)
353 TRACE( "PROCESS_HEAP_REGION ");
354 if (entry
->wFlags
& PROCESS_HEAP_UNCOMMITTED_RANGE
)
355 TRACE( "PROCESS_HEAP_UNCOMMITTED_RANGE ");
356 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_BUSY
)
357 TRACE( "PROCESS_HEAP_ENTRY_BUSY ");
358 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_MOVEABLE
)
359 TRACE( "PROCESS_HEAP_ENTRY_MOVEABLE ");
360 if (entry
->wFlags
& PROCESS_HEAP_ENTRY_DDESHARE
)
361 TRACE( "PROCESS_HEAP_ENTRY_DDESHARE ");
362 rem_flags
= entry
->wFlags
&
363 ~(PROCESS_HEAP_REGION
| PROCESS_HEAP_UNCOMMITTED_RANGE
|
364 PROCESS_HEAP_ENTRY_BUSY
| PROCESS_HEAP_ENTRY_MOVEABLE
|
365 PROCESS_HEAP_ENTRY_DDESHARE
);
367 TRACE( "Unknown %08x", rem_flags
);
369 if ((entry
->wFlags
& PROCESS_HEAP_ENTRY_BUSY
)
370 && (entry
->wFlags
& PROCESS_HEAP_ENTRY_MOVEABLE
))
373 TRACE( "BLOCK->hMem\t\t:%p\n", entry
->u
.Block
.hMem
);
375 if (entry
->wFlags
& PROCESS_HEAP_REGION
)
377 TRACE( "Region.dwCommittedSize\t:%08x\n",entry
->u
.Region
.dwCommittedSize
);
378 TRACE( "Region.dwUnCommittedSize\t:%08x\n",entry
->u
.Region
.dwUnCommittedSize
);
379 TRACE( "Region.lpFirstBlock\t:%p\n",entry
->u
.Region
.lpFirstBlock
);
380 TRACE( "Region.lpLastBlock\t:%p\n",entry
->u
.Region
.lpLastBlock
);
384 /***********************************************************************
387 * Pointer to the heap
390 static HEAP
*HEAP_GetPtr(
391 HANDLE heap
/* [in] Handle to the heap */
393 HEAP
*heapPtr
= (HEAP
*)heap
;
394 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
396 ERR("Invalid heap %p!\n", heap
);
399 if ((TRACE_ON(heap
)|| TRACE_ON(heap_poison
)) && !HEAP_IsRealArena( heapPtr
, 0, NULL
, NOISY
))
401 HEAP_Dump( heapPtr
);
409 /***********************************************************************
410 * HEAP_InsertFreeBlock
412 * Insert a free block into the free list.
414 static inline void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
, BOOL last
)
416 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
+ get_freelist_index( pArena
->size
+ sizeof(*pArena
) );
419 /* insert at end of free list, i.e. before the next free list entry */
421 if (pEntry
== &heap
->freeList
[HEAP_NB_FREE_LISTS
]) pEntry
= heap
->freeList
;
422 list_add_before( &pEntry
->arena
.entry
, &pArena
->entry
);
426 /* insert at head of free list */
427 list_add_after( &pEntry
->arena
.entry
, &pArena
->entry
);
429 pArena
->size
|= ARENA_FLAG_FREE
;
433 /***********************************************************************
435 * Find the sub-heap containing a given address.
441 static SUBHEAP
*HEAP_FindSubHeap(
442 const HEAP
*heap
, /* [in] Heap pointer */
443 LPCVOID ptr
) /* [in] Address */
446 LIST_FOR_EACH_ENTRY( sub
, &heap
->subheap_list
, SUBHEAP
, entry
)
447 if (((const char *)ptr
>= (const char *)sub
->base
) &&
448 ((const char *)ptr
< (const char *)sub
->base
+ sub
->size
- sizeof(ARENA_INUSE
)))
454 /***********************************************************************
457 * Make sure the heap storage is committed for a given size in the specified arena.
459 static inline BOOL
HEAP_Commit( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, SIZE_T data_size
)
461 void *ptr
= (char *)(pArena
+ 1) + data_size
+ sizeof(ARENA_FREE
);
462 SIZE_T size
= (char *)ptr
- (char *)subheap
->base
;
463 size
= (size
+ COMMIT_MASK
) & ~COMMIT_MASK
;
464 if (size
> subheap
->size
) size
= subheap
->size
;
465 if (size
<= subheap
->commitSize
) return TRUE
;
466 size
-= subheap
->commitSize
;
467 ptr
= (char *)subheap
->base
+ subheap
->commitSize
;
468 if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0,
469 &size
, MEM_COMMIT
, get_protection_type( subheap
->heap
->flags
) ))
471 WARN("Could not commit %08lx bytes at %p for heap %p\n",
472 size
, ptr
, subheap
->heap
);
475 subheap
->commitSize
+= size
;
480 /***********************************************************************
483 * If possible, decommit the heap storage from (including) 'ptr'.
485 static inline BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
488 SIZE_T decommit_size
;
489 SIZE_T size
= (char *)ptr
- (char *)subheap
->base
;
491 /* round to next block and add one full block */
492 size
= ((size
+ COMMIT_MASK
) & ~COMMIT_MASK
) + COMMIT_MASK
+ 1;
493 if (size
>= subheap
->commitSize
) return TRUE
;
494 decommit_size
= subheap
->commitSize
- size
;
495 addr
= (char *)subheap
->base
+ size
;
497 if (NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &decommit_size
, MEM_DECOMMIT
))
499 WARN("Could not decommit %08lx bytes at %p for heap %p\n",
500 decommit_size
, (char *)subheap
->base
+ size
, subheap
->heap
);
503 subheap
->commitSize
-= decommit_size
;
508 /***********************************************************************
509 * HEAP_CreateFreeBlock
511 * Create a free block at a specified address. 'size' is the size of the
512 * whole block, including the new arena.
514 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, SIZE_T size
)
520 /* Create a free arena */
521 mark_block_uninitialized( ptr
, sizeof( ARENA_FREE
) );
522 pFree
= (ARENA_FREE
*)ptr
;
523 pFree
->magic
= ARENA_FREE_MAGIC
;
525 /* If debugging, erase the freed block content */
527 pEnd
= (char *)ptr
+ size
;
528 if (pEnd
> (char *)subheap
->base
+ subheap
->commitSize
)
529 pEnd
= (char *)subheap
->base
+ subheap
->commitSize
;
530 if (pEnd
> (char *)(pFree
+ 1)) mark_block_free( pFree
+ 1, pEnd
- (char *)(pFree
+ 1) );
532 /* Check if next block is free also */
534 if (((char *)ptr
+ size
< (char *)subheap
->base
+ subheap
->size
) &&
535 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
537 /* Remove the next arena from the free list */
538 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
539 list_remove( &pNext
->entry
);
540 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
541 mark_block_free( pNext
, sizeof(ARENA_FREE
) );
544 /* Set the next block PREV_FREE flag and pointer */
546 last
= ((char *)ptr
+ size
>= (char *)subheap
->base
+ subheap
->size
);
549 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
550 *pNext
|= ARENA_FLAG_PREV_FREE
;
551 mark_block_initialized( pNext
- 1, sizeof( ARENA_FREE
* ) );
552 *((ARENA_FREE
**)pNext
- 1) = pFree
;
555 /* Last, insert the new block into the free list */
557 pFree
->size
= size
- sizeof(*pFree
);
558 HEAP_InsertFreeBlock( subheap
->heap
, pFree
, last
);
562 /***********************************************************************
563 * HEAP_MakeInUseBlockFree
565 * Turn an in-use block into a free block. Can also decommit the end of
566 * the heap, and possibly even free the sub-heap altogether.
568 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
571 SIZE_T size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
573 /* Check if we can merge with previous block */
575 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
577 pFree
= *((ARENA_FREE
**)pArena
- 1);
578 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
579 /* Remove it from the free list */
580 list_remove( &pFree
->entry
);
582 else pFree
= (ARENA_FREE
*)pArena
;
584 /* Create a free block */
586 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
587 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
588 if ((char *)pFree
+ size
< (char *)subheap
->base
+ subheap
->size
)
589 return; /* Not the last block, so nothing more to do */
591 /* Free the whole sub-heap if it's empty and not the original one */
593 if (((char *)pFree
== (char *)subheap
->base
+ subheap
->headerSize
) &&
594 (subheap
!= &subheap
->heap
->subheap
))
597 void *addr
= subheap
->base
;
598 /* Remove the free block from the list */
599 list_remove( &pFree
->entry
);
600 /* Remove the subheap from the list */
601 list_remove( &subheap
->entry
);
602 /* Free the memory */
604 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
608 /* Decommit the end of the heap */
610 if (!(subheap
->heap
->flags
& HEAP_SHARED
)) HEAP_Decommit( subheap
, pFree
+ 1 );
614 /***********************************************************************
617 * Shrink an in-use block.
619 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, SIZE_T size
)
621 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_SHRINK_SIZE
)
623 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
624 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
625 /* assign size plus previous arena flags */
626 pArena
->size
= size
| (pArena
->size
& ~ARENA_SIZE_MASK
);
630 /* Turn off PREV_FREE flag in next block */
631 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
632 if (pNext
< (char *)subheap
->base
+ subheap
->size
)
633 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
638 /***********************************************************************
639 * allocate_large_block
641 static void *allocate_large_block( HEAP
*heap
, DWORD flags
, SIZE_T size
)
644 SIZE_T block_size
= sizeof(*arena
) + ROUND_SIZE(size
);
645 LPVOID address
= NULL
;
647 if (block_size
< size
) return NULL
; /* overflow */
648 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 0,
649 &block_size
, MEM_COMMIT
, get_protection_type( flags
) ))
651 WARN("Could not allocate block for %08lx bytes\n", size
);
655 arena
->data_size
= size
;
656 arena
->block_size
= block_size
;
657 arena
->size
= ARENA_LARGE_SIZE
;
658 arena
->magic
= ARENA_LARGE_MAGIC
;
659 list_add_tail( &heap
->large_list
, &arena
->entry
);
664 /***********************************************************************
667 static void free_large_block( HEAP
*heap
, DWORD flags
, void *ptr
)
669 ARENA_LARGE
*arena
= (ARENA_LARGE
*)ptr
- 1;
670 LPVOID address
= arena
;
673 list_remove( &arena
->entry
);
674 NtFreeVirtualMemory( NtCurrentProcess(), &address
, &size
, MEM_RELEASE
);
678 /***********************************************************************
679 * realloc_large_block
681 static void *realloc_large_block( HEAP
*heap
, DWORD flags
, void *ptr
, SIZE_T size
)
683 ARENA_LARGE
*arena
= (ARENA_LARGE
*)ptr
- 1;
686 if (arena
->block_size
- sizeof(*arena
) >= size
)
688 /* FIXME: we could remap zero-pages instead */
689 if ((flags
& HEAP_ZERO_MEMORY
) && size
> arena
->data_size
)
690 memset( (char *)ptr
+ arena
->data_size
, 0, size
- arena
->data_size
);
691 arena
->data_size
= size
;
694 if (flags
& HEAP_REALLOC_IN_PLACE_ONLY
) return NULL
;
695 if (!(new_ptr
= allocate_large_block( heap
, flags
, size
)))
697 WARN("Could not allocate block for %08lx bytes\n", size
);
700 memcpy( new_ptr
, ptr
, arena
->data_size
);
701 free_large_block( heap
, flags
, ptr
);
706 /***********************************************************************
709 static ARENA_LARGE
*find_large_block( HEAP
*heap
, const void *ptr
)
713 LIST_FOR_EACH_ENTRY( arena
, &heap
->large_list
, ARENA_LARGE
, entry
)
714 if (ptr
== (const void *)(arena
+ 1)) return arena
;
720 /***********************************************************************
721 * validate_large_arena
723 static BOOL
validate_large_arena( HEAP
*heap
, const ARENA_LARGE
*arena
, BOOL quiet
)
725 if ((ULONG_PTR
)arena
% getpagesize())
729 ERR( "Heap %p: invalid large arena pointer %p\n", heap
, arena
);
730 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
732 else if (WARN_ON(heap
))
734 WARN( "Heap %p: unaligned arena pointer %p\n", heap
, arena
);
735 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
739 if (arena
->size
!= ARENA_LARGE_SIZE
|| arena
->magic
!= ARENA_LARGE_MAGIC
)
743 ERR( "Heap %p: invalid large arena %p values %x/%x\n",
744 heap
, arena
, arena
->size
, arena
->magic
);
745 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
747 else if (WARN_ON(heap
))
749 WARN( "Heap %p: invalid large arena %p values %x/%x\n",
750 heap
, arena
, arena
->size
, arena
->magic
);
751 if (TRACE_ON(heap
)) HEAP_Dump( heap
);
759 /***********************************************************************
762 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
763 SIZE_T commitSize
, SIZE_T totalSize
)
766 FREE_LIST_ENTRY
*pEntry
;
771 /* round-up sizes on a 64K boundary */
772 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
773 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
774 if (!commitSize
) commitSize
= 0x10000;
775 if (totalSize
< commitSize
) totalSize
= commitSize
;
776 if (flags
& HEAP_SHARED
) commitSize
= totalSize
; /* always commit everything in a shared heap */
778 /* allocate the memory block */
779 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 0, &totalSize
,
780 MEM_RESERVE
, get_protection_type( flags
) ))
782 WARN("Could not allocate %08lx bytes\n", totalSize
);
785 if (NtAllocateVirtualMemory( NtCurrentProcess(), &address
, 0,
786 &commitSize
, MEM_COMMIT
, get_protection_type( flags
) ))
788 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize
, address
);
795 /* If this is a secondary subheap, insert it into list */
797 subheap
= (SUBHEAP
*)address
;
798 subheap
->base
= address
;
799 subheap
->heap
= heap
;
800 subheap
->size
= totalSize
;
801 subheap
->commitSize
= commitSize
;
802 subheap
->magic
= SUBHEAP_MAGIC
;
803 subheap
->headerSize
= ROUND_SIZE( sizeof(SUBHEAP
) );
804 list_add_head( &heap
->subheap_list
, &subheap
->entry
);
808 /* If this is a primary subheap, initialize main heap */
810 heap
= (HEAP
*)address
;
812 heap
->magic
= HEAP_MAGIC
;
813 heap
->grow_size
= max( HEAP_DEF_SIZE
, totalSize
);
814 list_init( &heap
->subheap_list
);
815 list_init( &heap
->large_list
);
817 subheap
= &heap
->subheap
;
818 subheap
->base
= address
;
819 subheap
->heap
= heap
;
820 subheap
->size
= totalSize
;
821 subheap
->commitSize
= commitSize
;
822 subheap
->magic
= SUBHEAP_MAGIC
;
823 subheap
->headerSize
= ROUND_SIZE( sizeof(HEAP
) );
824 list_add_head( &heap
->subheap_list
, &subheap
->entry
);
826 /* Build the free lists */
828 list_init( &heap
->freeList
[0].arena
.entry
);
829 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
831 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
832 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
833 if (i
) list_add_after( &pEntry
[-1].arena
.entry
, &pEntry
->arena
.entry
);
836 /* Initialize critical section */
838 if (!processHeap
) /* do it by hand to avoid memory allocations */
840 if(TRACE_ON(heap_poison
))
841 TRACE_(heap_poison
)("poisioning heap\n");
842 heap
->critSection
.DebugInfo
= &process_heap_critsect_debug
;
843 heap
->critSection
.LockCount
= -1;
844 heap
->critSection
.RecursionCount
= 0;
845 heap
->critSection
.OwningThread
= 0;
846 heap
->critSection
.LockSemaphore
= 0;
847 heap
->critSection
.SpinCount
= 0;
848 process_heap_critsect_debug
.CriticalSection
= &heap
->critSection
;
852 RtlInitializeCriticalSection( &heap
->critSection
);
853 heap
->critSection
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": HEAP.critSection");
856 if (flags
& HEAP_SHARED
)
858 /* let's assume that only one thread at a time will try to do this */
859 HANDLE sem
= heap
->critSection
.LockSemaphore
;
860 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
862 NtDuplicateObject( NtCurrentProcess(), sem
, NtCurrentProcess(), &sem
, 0, 0,
863 DUP_HANDLE_MAKE_GLOBAL
| DUP_HANDLE_SAME_ACCESS
| DUP_HANDLE_CLOSE_SOURCE
);
864 heap
->critSection
.LockSemaphore
= sem
;
865 RtlFreeHeap( processHeap
, 0, heap
->critSection
.DebugInfo
);
866 heap
->critSection
.DebugInfo
= NULL
;
870 /* Create the first free block */
872 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
->base
+ subheap
->headerSize
,
873 subheap
->size
- subheap
->headerSize
);
879 /***********************************************************************
882 * Find a free block at least as large as the requested size, and make sure
883 * the requested size is committed.
885 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, SIZE_T size
,
886 SUBHEAP
**ppSubHeap
)
891 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
+ get_freelist_index( size
+ sizeof(ARENA_INUSE
) );
893 /* Find a suitable free list, and in it find a block large enough */
895 ptr
= &pEntry
->arena
.entry
;
896 while ((ptr
= list_next( &heap
->freeList
[0].arena
.entry
, ptr
)))
898 ARENA_FREE
*pArena
= LIST_ENTRY( ptr
, ARENA_FREE
, entry
);
899 SIZE_T arena_size
= (pArena
->size
& ARENA_SIZE_MASK
) +
900 sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
901 if (arena_size
>= size
)
903 subheap
= HEAP_FindSubHeap( heap
, pArena
);
904 if (!HEAP_Commit( subheap
, (ARENA_INUSE
*)pArena
, size
)) return NULL
;
905 *ppSubHeap
= subheap
;
910 /* If no block was found, attempt to grow the heap */
912 if (!(heap
->flags
& HEAP_GROWABLE
))
914 WARN("Not enough space in heap %p for %08lx bytes\n", heap
, size
);
917 /* make sure that we have a big enough size *committed* to fit another
918 * last free arena in !
919 * So just one heap struct, one first free arena which will eventually
920 * get used, and a second free arena that might get assigned all remaining
921 * free space in HEAP_ShrinkBlock() */
922 total_size
= size
+ ROUND_SIZE(sizeof(SUBHEAP
)) + sizeof(ARENA_INUSE
) + sizeof(ARENA_FREE
);
923 if (total_size
< size
) return NULL
; /* overflow */
925 if (!(subheap
= HEAP_CreateSubHeap( heap
, NULL
, heap
->flags
, total_size
,
926 max( heap
->grow_size
, total_size
) )))
929 if (heap
->grow_size
< 128 * 1024 * 1024) heap
->grow_size
*= 2;
931 TRACE("created new sub-heap %p of %08lx bytes for heap %p\n",
932 subheap
, subheap
->size
, heap
);
934 *ppSubHeap
= subheap
;
935 return (ARENA_FREE
*)((char *)subheap
->base
+ subheap
->headerSize
);
939 /***********************************************************************
940 * HEAP_IsValidArenaPtr
942 * Check that the pointer is inside the range possible for arenas.
944 static BOOL
HEAP_IsValidArenaPtr( const HEAP
*heap
, const ARENA_FREE
*ptr
)
947 const SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
948 if (!subheap
) return FALSE
;
949 if ((const char *)ptr
>= (const char *)subheap
->base
+ subheap
->headerSize
) return TRUE
;
950 if (subheap
!= &heap
->subheap
) return FALSE
;
951 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
952 if (ptr
== (const void *)&heap
->freeList
[i
].arena
) return TRUE
;
957 /***********************************************************************
958 * HEAP_ValidateFreeArena
960 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
962 ARENA_FREE
*prev
, *next
;
963 char *heapEnd
= (char *)subheap
->base
+ subheap
->size
;
965 /* Check for unaligned pointers */
966 if ( (ULONG_PTR
)pArena
% ALIGNMENT
!= 0 )
968 ERR("Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
972 /* Check magic number */
973 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
975 ERR("Heap %p: invalid free arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
978 /* Check size flags */
979 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
980 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
982 ERR("Heap %p: bad flags %08x for free arena %p\n",
983 subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, pArena
);
986 /* Check arena size */
987 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
989 ERR("Heap %p: bad size %08x for free arena %p\n",
990 subheap
->heap
, pArena
->size
& ARENA_SIZE_MASK
, pArena
);
993 /* Check that next pointer is valid */
994 next
= LIST_ENTRY( pArena
->entry
.next
, ARENA_FREE
, entry
);
995 if (!HEAP_IsValidArenaPtr( subheap
->heap
, next
))
997 ERR("Heap %p: bad next ptr %p for arena %p\n",
998 subheap
->heap
, next
, pArena
);
1001 /* Check that next arena is free */
1002 if (!(next
->size
& ARENA_FLAG_FREE
) || (next
->magic
!= ARENA_FREE_MAGIC
))
1004 ERR("Heap %p: next arena %p invalid for %p\n",
1005 subheap
->heap
, next
, pArena
);
1008 /* Check that prev pointer is valid */
1009 prev
= LIST_ENTRY( pArena
->entry
.prev
, ARENA_FREE
, entry
);
1010 if (!HEAP_IsValidArenaPtr( subheap
->heap
, prev
))
1012 ERR("Heap %p: bad prev ptr %p for arena %p\n",
1013 subheap
->heap
, prev
, pArena
);
1016 /* Check that prev arena is free */
1017 if (!(prev
->size
& ARENA_FLAG_FREE
) || (prev
->magic
!= ARENA_FREE_MAGIC
))
1019 /* this often means that the prev arena got overwritten
1020 * by a memory write before that prev arena */
1021 ERR("Heap %p: prev arena %p invalid for %p\n",
1022 subheap
->heap
, prev
, pArena
);
1025 /* Check that next block has PREV_FREE flag */
1026 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
1028 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
1029 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
1031 ERR("Heap %p: free arena %p next block has no PREV_FREE flag\n",
1032 subheap
->heap
, pArena
);
1035 /* Check next block back pointer */
1036 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
1037 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
1039 ERR("Heap %p: arena %p has wrong back ptr %p\n",
1040 subheap
->heap
, pArena
,
1041 *((ARENA_FREE
**)((char *)(pArena
+1) + (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
1049 /***********************************************************************
1050 * HEAP_ValidateInUseArena
1052 static BOOL
HEAP_ValidateInUseArena( const SUBHEAP
*subheap
, const ARENA_INUSE
*pArena
, BOOL quiet
)
1054 const char *heapEnd
= (const char *)subheap
->base
+ subheap
->size
;
1056 /* Check for unaligned pointers */
1057 if ( (ULONG_PTR
)pArena
% ALIGNMENT
!= 0 )
1059 if ( quiet
== NOISY
)
1061 ERR( "Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
1062 if ( TRACE_ON(heap
) )
1063 HEAP_Dump( subheap
->heap
);
1065 else if ( WARN_ON(heap
) )
1067 WARN( "Heap %p: unaligned arena pointer %p\n", subheap
->heap
, pArena
);
1068 if ( TRACE_ON(heap
) )
1069 HEAP_Dump( subheap
->heap
);
1074 /* Check magic number */
1075 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
1077 if (quiet
== NOISY
) {
1078 ERR("Heap %p: invalid in-use arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
1080 HEAP_Dump( subheap
->heap
);
1081 } else if (WARN_ON(heap
)) {
1082 WARN("Heap %p: invalid in-use arena magic %08x for %p\n", subheap
->heap
, pArena
->magic
, pArena
);
1084 HEAP_Dump( subheap
->heap
);
1088 /* Check size flags */
1089 if (pArena
->size
& ARENA_FLAG_FREE
)
1091 ERR("Heap %p: bad flags %08x for in-use arena %p\n",
1092 subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, pArena
);
1095 /* Check arena size */
1096 if ((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
1098 ERR("Heap %p: bad size %08x for in-use arena %p\n",
1099 subheap
->heap
, pArena
->size
& ARENA_SIZE_MASK
, pArena
);
1102 /* Check next arena PREV_FREE flag */
1103 if (((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
1104 (*(const DWORD
*)((const char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
1106 ERR("Heap %p: in-use arena %p next block has PREV_FREE flag\n",
1107 subheap
->heap
, pArena
);
1110 /* Check prev free arena */
1111 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
1113 const ARENA_FREE
*pPrev
= *((const ARENA_FREE
* const*)pArena
- 1);
1114 /* Check prev pointer */
1115 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
1117 ERR("Heap %p: bad back ptr %p for arena %p\n",
1118 subheap
->heap
, pPrev
, pArena
);
1121 /* Check that prev arena is free */
1122 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
1123 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
1125 ERR("Heap %p: prev arena %p invalid for in-use %p\n",
1126 subheap
->heap
, pPrev
, pArena
);
1129 /* Check that prev arena is really the previous block */
1130 if ((const char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (const char *)pArena
)
1132 ERR("Heap %p: prev arena %p is not prev for in-use %p\n",
1133 subheap
->heap
, pPrev
, pArena
);
1141 /***********************************************************************
1142 * HEAP_IsRealArena [Internal]
1143 * Validates a block is a valid arena.
1149 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, /* [in] ptr to the heap */
1150 DWORD flags
, /* [in] Bit flags that control access during operation */
1151 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
1152 BOOL quiet
) /* [in] Flag - if true, HEAP_ValidateInUseArena
1153 * does not complain */
1157 const ARENA_LARGE
*large_arena
;
1159 flags
&= HEAP_NO_SERIALIZE
;
1160 flags
|= heapPtr
->flags
;
1161 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
1162 if (!(flags
& HEAP_NO_SERIALIZE
))
1163 RtlEnterCriticalSection( &heapPtr
->critSection
);
1165 if (block
) /* only check this single memory block */
1167 const ARENA_INUSE
*arena
= (const ARENA_INUSE
*)block
- 1;
1169 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, arena
)) ||
1170 ((const char *)arena
< (char *)subheap
->base
+ subheap
->headerSize
))
1172 if (!(large_arena
= find_large_block( heapPtr
, block
)))
1175 ERR("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
1176 else if (WARN_ON(heap
))
1177 WARN("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
1181 ret
= validate_large_arena( heapPtr
, large_arena
, quiet
);
1183 ret
= HEAP_ValidateInUseArena( subheap
, arena
, quiet
);
1185 if (!(flags
& HEAP_NO_SERIALIZE
))
1186 RtlLeaveCriticalSection( &heapPtr
->critSection
);
1190 LIST_FOR_EACH_ENTRY( subheap
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1192 char *ptr
= (char *)subheap
->base
+ subheap
->headerSize
;
1193 while (ptr
< (char *)subheap
->base
+ subheap
->size
)
1195 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1197 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
1201 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1205 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
1209 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1215 LIST_FOR_EACH_ENTRY( large_arena
, &heapPtr
->large_list
, ARENA_LARGE
, entry
)
1216 if (!(ret
= validate_large_arena( heapPtr
, large_arena
, quiet
))) break;
1218 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1223 /***********************************************************************
1224 * RtlCreateHeap (NTDLL.@)
1226 * Create a new Heap.
1229 * flags [I] HEAP_ flags from "winnt.h"
1230 * addr [I] Desired base address
1231 * totalSize [I] Total size of the heap, or 0 for a growable heap
1232 * commitSize [I] Amount of heap space to commit
1233 * unknown [I] Not yet understood
1234 * definition [I] Heap definition
1237 * Success: A HANDLE to the newly created heap.
1238 * Failure: a NULL HANDLE.
1240 HANDLE WINAPI
RtlCreateHeap( ULONG flags
, PVOID addr
, SIZE_T totalSize
, SIZE_T commitSize
,
1241 PVOID unknown
, PRTL_HEAP_DEFINITION definition
)
1245 /* Allocate the heap block */
1249 totalSize
= HEAP_DEF_SIZE
;
1250 flags
|= HEAP_GROWABLE
;
1253 if (!(subheap
= HEAP_CreateSubHeap( NULL
, addr
, flags
, commitSize
, totalSize
))) return 0;
1255 /* link it into the per-process heap list */
1258 HEAP
*heapPtr
= subheap
->heap
;
1259 RtlEnterCriticalSection( &processHeap
->critSection
);
1260 list_add_head( &processHeap
->entry
, &heapPtr
->entry
);
1261 RtlLeaveCriticalSection( &processHeap
->critSection
);
1265 processHeap
= subheap
->heap
; /* assume the first heap we create is the process main heap */
1266 list_init( &processHeap
->entry
);
1267 /* make sure structure alignment is correct */
1268 assert( (ULONG_PTR
)processHeap
->freeList
% ALIGNMENT
== 0 );
1269 assert( sizeof(ARENA_LARGE
) % LARGE_ALIGNMENT
== 0 );
1272 return (HANDLE
)subheap
->heap
;
1276 /***********************************************************************
1277 * RtlDestroyHeap (NTDLL.@)
1279 * Destroy a Heap created with RtlCreateHeap().
1282 * heap [I] Heap to destroy.
1285 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1286 * Failure: The Heap handle, if heap is the process heap.
1288 HANDLE WINAPI
RtlDestroyHeap( HANDLE heap
)
1290 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1291 SUBHEAP
*subheap
, *next
;
1292 ARENA_LARGE
*arena
, *arena_next
;
1296 TRACE("%p\n", heap
);
1297 if (!heapPtr
) return heap
;
1299 if (heap
== processHeap
) return heap
; /* cannot delete the main process heap */
1301 /* remove it from the per-process list */
1302 RtlEnterCriticalSection( &processHeap
->critSection
);
1303 list_remove( &heapPtr
->entry
);
1304 RtlLeaveCriticalSection( &processHeap
->critSection
);
1306 heapPtr
->critSection
.DebugInfo
->Spare
[0] = 0;
1307 RtlDeleteCriticalSection( &heapPtr
->critSection
);
1309 LIST_FOR_EACH_ENTRY_SAFE( arena
, arena_next
, &heapPtr
->large_list
, ARENA_LARGE
, entry
)
1311 list_remove( &arena
->entry
);
1314 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1316 LIST_FOR_EACH_ENTRY_SAFE( subheap
, next
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1318 if (subheap
== &heapPtr
->subheap
) continue; /* do this one last */
1319 subheap_notify_free_all(subheap
);
1320 list_remove( &subheap
->entry
);
1322 addr
= subheap
->base
;
1323 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1325 subheap_notify_free_all(&heapPtr
->subheap
);
1327 addr
= heapPtr
->subheap
.base
;
1328 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1333 /***********************************************************************
1334 * RtlAllocateHeap (NTDLL.@)
1336 * Allocate a memory block from a Heap.
1339 * heap [I] Heap to allocate block from
1340 * flags [I] HEAP_ flags from "winnt.h"
1341 * size [I] Size of the memory block to allocate
1344 * Success: A pointer to the newly allocated block
1348 * This call does not SetLastError().
1350 PVOID WINAPI
RtlAllocateHeap( HANDLE heap
, ULONG flags
, SIZE_T size
)
1353 ARENA_INUSE
*pInUse
;
1355 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1356 SIZE_T rounded_size
;
1358 /* Validate the parameters */
1360 if (!heapPtr
) return NULL
;
1361 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1362 flags
|= heapPtr
->flags
;
1363 rounded_size
= ROUND_SIZE(size
);
1364 if (rounded_size
< size
) /* overflow */
1366 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1369 if (rounded_size
< HEAP_MIN_DATA_SIZE
) rounded_size
= HEAP_MIN_DATA_SIZE
;
1371 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1373 if (rounded_size
>= HEAP_MIN_LARGE_BLOCK_SIZE
&& (flags
& HEAP_GROWABLE
))
1375 void *ret
= allocate_large_block( heap
, flags
, size
);
1376 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1377 if (!ret
&& (flags
& HEAP_GENERATE_EXCEPTIONS
)) RtlRaiseStatus( STATUS_NO_MEMORY
);
1378 TRACE("(%p,%08x,%08lx): returning %p\n", heap
, flags
, size
, ret
);
1382 /* Locate a suitable free block */
1384 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, rounded_size
, &subheap
)))
1386 TRACE("(%p,%08x,%08lx): returning NULL\n",
1387 heap
, flags
, size
);
1388 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1389 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1393 /* Remove the arena from the free list */
1395 list_remove( &pArena
->entry
);
1397 /* Build the in-use arena */
1399 pInUse
= (ARENA_INUSE
*)pArena
;
1401 /* in-use arena is smaller than free arena,
1402 * so we have to add the difference to the size */
1403 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
) + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1404 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1406 /* Shrink the block */
1408 HEAP_ShrinkBlock( subheap
, pInUse
, rounded_size
);
1409 pInUse
->unused_bytes
= (pInUse
->size
& ARENA_SIZE_MASK
) - size
;
1411 notify_alloc( pInUse
+ 1, size
, flags
& HEAP_ZERO_MEMORY
);
1413 if (flags
& HEAP_ZERO_MEMORY
)
1415 clear_block( pInUse
+ 1, size
);
1416 mark_block_uninitialized( (char *)(pInUse
+ 1) + size
, pInUse
->unused_bytes
);
1419 mark_block_uninitialized( pInUse
+ 1, pInUse
->size
& ARENA_SIZE_MASK
);
1421 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1423 TRACE("(%p,%08x,%08lx): returning %p\n", heap
, flags
, size
, pInUse
+ 1 );
1424 return (LPVOID
)(pInUse
+ 1);
1428 /***********************************************************************
1429 * RtlFreeHeap (NTDLL.@)
1431 * Free a memory block allocated with RtlAllocateHeap().
1434 * heap [I] Heap that block was allocated from
1435 * flags [I] HEAP_ flags from "winnt.h"
1436 * ptr [I] Block to free
1439 * Success: TRUE, if ptr is NULL or was freed successfully.
1442 BOOLEAN WINAPI
RtlFreeHeap( HANDLE heap
, ULONG flags
, PVOID ptr
)
1444 ARENA_INUSE
*pInUse
;
1448 /* Validate the parameters */
1450 if (!ptr
) return TRUE
; /* freeing a NULL ptr isn't an error in Win2k */
1452 heapPtr
= HEAP_GetPtr( heap
);
1455 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1459 flags
&= HEAP_NO_SERIALIZE
;
1460 flags
|= heapPtr
->flags
;
1461 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1463 /* Inform valgrind we are trying to free memory, so it can throw up an error message */
1466 /* Some sanity checks */
1467 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1468 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
)))
1470 if (!find_large_block( heapPtr
, ptr
)) goto error
;
1471 free_large_block( heapPtr
, flags
, ptr
);
1474 if ((char *)pInUse
< (char *)subheap
->base
+ subheap
->headerSize
) goto error
;
1475 if (!HEAP_ValidateInUseArena( subheap
, pInUse
, QUIET
)) goto error
;
1477 /* Turn the block into a free block */
1479 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1482 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1483 TRACE("(%p,%08x,%p): returning TRUE\n", heap
, flags
, ptr
);
1487 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1488 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1489 TRACE("(%p,%08x,%p): returning FALSE\n", heap
, flags
, ptr
);
1494 /***********************************************************************
1495 * RtlReAllocateHeap (NTDLL.@)
1497 * Change the size of a memory block allocated with RtlAllocateHeap().
1500 * heap [I] Heap that block was allocated from
1501 * flags [I] HEAP_ flags from "winnt.h"
1502 * ptr [I] Block to resize
1503 * size [I] Size of the memory block to allocate
1506 * Success: A pointer to the resized block (which may be different).
1509 PVOID WINAPI
RtlReAllocateHeap( HANDLE heap
, ULONG flags
, PVOID ptr
, SIZE_T size
)
1511 ARENA_INUSE
*pArena
;
1514 SIZE_T oldBlockSize
, oldActualSize
, rounded_size
;
1517 if (!ptr
) return NULL
;
1518 if (!(heapPtr
= HEAP_GetPtr( heap
)))
1520 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1524 /* Validate the parameters */
1526 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1527 HEAP_REALLOC_IN_PLACE_ONLY
;
1528 flags
|= heapPtr
->flags
;
1529 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1531 rounded_size
= ROUND_SIZE(size
);
1532 if (rounded_size
< size
) goto oom
; /* overflow */
1533 if (rounded_size
< HEAP_MIN_DATA_SIZE
) rounded_size
= HEAP_MIN_DATA_SIZE
;
1535 pArena
= (ARENA_INUSE
*)ptr
- 1;
1536 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, pArena
)))
1538 if (!find_large_block( heapPtr
, ptr
)) goto error
;
1539 if (!(ret
= realloc_large_block( heapPtr
, flags
, ptr
, size
))) goto oom
;
1542 if ((char *)pArena
< (char *)subheap
->base
+ subheap
->headerSize
) goto error
;
1543 if (!HEAP_ValidateInUseArena( subheap
, pArena
, QUIET
)) goto error
;
1545 /* Check if we need to grow the block */
1547 oldBlockSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1548 oldActualSize
= (pArena
->size
& ARENA_SIZE_MASK
) - pArena
->unused_bytes
;
1549 if (rounded_size
> oldBlockSize
)
1551 char *pNext
= (char *)(pArena
+ 1) + oldBlockSize
;
1553 if (rounded_size
>= HEAP_MIN_LARGE_BLOCK_SIZE
&& (flags
& HEAP_GROWABLE
))
1555 if (!(ret
= allocate_large_block( heapPtr
, flags
, size
))) goto oom
;
1556 memcpy( ret
, pArena
+ 1, oldActualSize
);
1559 if ((pNext
< (char *)subheap
->base
+ subheap
->size
) &&
1560 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1561 (oldBlockSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= rounded_size
))
1563 /* The next block is free and large enough */
1564 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1565 list_remove( &pFree
->entry
);
1566 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1567 if (!HEAP_Commit( subheap
, pArena
, rounded_size
)) goto oom
;
1568 notify_free( pArena
+ 1 );
1569 HEAP_ShrinkBlock( subheap
, pArena
, rounded_size
);
1570 notify_alloc( pArena
+ 1, size
, FALSE
);
1571 /* FIXME: this is wrong as we may lose old VBits settings */
1572 mark_block_initialized( pArena
+ 1, oldActualSize
);
1574 else /* Do it the hard way */
1577 ARENA_INUSE
*pInUse
;
1578 SUBHEAP
*newsubheap
;
1580 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1581 !(pNew
= HEAP_FindFreeBlock( heapPtr
, rounded_size
, &newsubheap
)))
1584 /* Build the in-use arena */
1586 list_remove( &pNew
->entry
);
1587 pInUse
= (ARENA_INUSE
*)pNew
;
1588 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1589 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1590 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1591 HEAP_ShrinkBlock( newsubheap
, pInUse
, rounded_size
);
1593 mark_block_initialized( pInUse
+ 1, oldActualSize
);
1594 notify_alloc( pInUse
+ 1, size
, FALSE
);
1595 memcpy( pInUse
+ 1, pArena
+ 1, oldActualSize
);
1597 /* Free the previous block */
1599 notify_free( pArena
+ 1 );
1600 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1601 subheap
= newsubheap
;
1607 /* Shrink the block */
1608 notify_free( pArena
+ 1 );
1609 HEAP_ShrinkBlock( subheap
, pArena
, rounded_size
);
1610 notify_alloc( pArena
+ 1, size
, FALSE
);
1611 /* FIXME: this is wrong as we may lose old VBits settings */
1612 mark_block_initialized( pArena
+ 1, size
);
1615 pArena
->unused_bytes
= (pArena
->size
& ARENA_SIZE_MASK
) - size
;
1617 /* Clear the extra bytes if needed */
1619 if (size
> oldActualSize
)
1621 if (flags
& HEAP_ZERO_MEMORY
)
1623 clear_block( (char *)(pArena
+ 1) + oldActualSize
, size
- oldActualSize
);
1624 mark_block_uninitialized( (char *)(pArena
+ 1) + size
, pArena
->unused_bytes
);
1627 mark_block_uninitialized( (char *)(pArena
+ 1) + oldActualSize
,
1628 (pArena
->size
& ARENA_SIZE_MASK
) - oldActualSize
);
1631 /* Return the new arena */
1635 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1636 TRACE("(%p,%08x,%p,%08lx): returning %p\n", heap
, flags
, ptr
, size
, ret
);
1640 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1641 if (flags
& HEAP_GENERATE_EXCEPTIONS
) RtlRaiseStatus( STATUS_NO_MEMORY
);
1642 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY
);
1643 TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap
, flags
, ptr
, size
);
1647 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1648 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1649 TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap
, flags
, ptr
, size
);
1654 /***********************************************************************
1655 * RtlCompactHeap (NTDLL.@)
1657 * Compact the free space in a Heap.
1660 * heap [I] Heap that block was allocated from
1661 * flags [I] HEAP_ flags from "winnt.h"
1664 * The number of bytes compacted.
1667 * This function is a harmless stub.
1669 ULONG WINAPI
RtlCompactHeap( HANDLE heap
, ULONG flags
)
1671 static BOOL reported
;
1672 if (!reported
++) FIXME( "(%p, 0x%x) stub\n", heap
, flags
);
1677 /***********************************************************************
1678 * RtlLockHeap (NTDLL.@)
1683 * heap [I] Heap to lock
1686 * Success: TRUE. The Heap is locked.
1687 * Failure: FALSE, if heap is invalid.
1689 BOOLEAN WINAPI
RtlLockHeap( HANDLE heap
)
1691 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1692 if (!heapPtr
) return FALSE
;
1693 RtlEnterCriticalSection( &heapPtr
->critSection
);
1698 /***********************************************************************
1699 * RtlUnlockHeap (NTDLL.@)
1704 * heap [I] Heap to unlock
1707 * Success: TRUE. The Heap is unlocked.
1708 * Failure: FALSE, if heap is invalid.
1710 BOOLEAN WINAPI
RtlUnlockHeap( HANDLE heap
)
1712 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1713 if (!heapPtr
) return FALSE
;
1714 RtlLeaveCriticalSection( &heapPtr
->critSection
);
1719 /***********************************************************************
1720 * RtlSizeHeap (NTDLL.@)
1722 * Get the actual size of a memory block allocated from a Heap.
1725 * heap [I] Heap that block was allocated from
1726 * flags [I] HEAP_ flags from "winnt.h"
1727 * ptr [I] Block to get the size of
1730 * Success: The size of the block.
1731 * Failure: -1, heap or ptr are invalid.
1734 * The size may be bigger than what was passed to RtlAllocateHeap().
1736 SIZE_T WINAPI
RtlSizeHeap( HANDLE heap
, ULONG flags
, const void *ptr
)
1739 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1743 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE
);
1746 flags
&= HEAP_NO_SERIALIZE
;
1747 flags
|= heapPtr
->flags
;
1748 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1749 if (!HEAP_IsRealArena( heapPtr
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1751 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER
);
1756 const ARENA_INUSE
*pArena
= (const ARENA_INUSE
*)ptr
- 1;
1757 if (pArena
->size
== ARENA_LARGE_SIZE
)
1759 const ARENA_LARGE
*large_arena
= (const ARENA_LARGE
*)ptr
- 1;
1760 ret
= large_arena
->data_size
;
1762 else ret
= (pArena
->size
& ARENA_SIZE_MASK
) - pArena
->unused_bytes
;
1764 if (!(flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1766 TRACE("(%p,%08x,%p): returning %08lx\n", heap
, flags
, ptr
, ret
);
1771 /***********************************************************************
1772 * RtlValidateHeap (NTDLL.@)
1774 * Determine if a block is a valid allocation from a heap.
1777 * heap [I] Heap that block was allocated from
1778 * flags [I] HEAP_ flags from "winnt.h"
1779 * ptr [I] Block to check
1782 * Success: TRUE. The block was allocated from heap.
1783 * Failure: FALSE, if heap is invalid or ptr was not allocated from it.
1785 BOOLEAN WINAPI
RtlValidateHeap( HANDLE heap
, ULONG flags
, LPCVOID ptr
)
1787 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1788 if (!heapPtr
) return FALSE
;
1789 return HEAP_IsRealArena( heapPtr
, flags
, ptr
, QUIET
);
1793 /***********************************************************************
1794 * RtlWalkHeap (NTDLL.@)
1797 * The PROCESS_HEAP_ENTRY flag values seem different between this
1798 * function and HeapWalk(). To be checked.
1800 NTSTATUS WINAPI
RtlWalkHeap( HANDLE heap
, PVOID entry_ptr
)
1802 LPPROCESS_HEAP_ENTRY entry
= entry_ptr
; /* FIXME */
1803 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1804 SUBHEAP
*sub
, *currentheap
= NULL
;
1807 int region_index
= 0;
1809 if (!heapPtr
|| !entry
) return STATUS_INVALID_PARAMETER
;
1811 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlEnterCriticalSection( &heapPtr
->critSection
);
1813 /* FIXME: enumerate large blocks too */
1815 /* set ptr to the next arena to be examined */
1817 if (!entry
->lpData
) /* first call (init) ? */
1819 TRACE("begin walking of heap %p.\n", heap
);
1820 currentheap
= &heapPtr
->subheap
;
1821 ptr
= (char*)currentheap
->base
+ currentheap
->headerSize
;
1825 ptr
= entry
->lpData
;
1826 LIST_FOR_EACH_ENTRY( sub
, &heapPtr
->subheap_list
, SUBHEAP
, entry
)
1828 if ((ptr
>= (char *)sub
->base
) &&
1829 (ptr
< (char *)sub
->base
+ sub
->size
))
1836 if (currentheap
== NULL
)
1838 ERR("no matching subheap found, shouldn't happen !\n");
1839 ret
= STATUS_NO_MORE_ENTRIES
;
1843 if (((ARENA_INUSE
*)ptr
- 1)->magic
== ARENA_INUSE_MAGIC
)
1845 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1846 ptr
+= pArena
->size
& ARENA_SIZE_MASK
;
1848 else if (((ARENA_FREE
*)ptr
- 1)->magic
== ARENA_FREE_MAGIC
)
1850 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
- 1;
1851 ptr
+= pArena
->size
& ARENA_SIZE_MASK
;
1854 ptr
+= entry
->cbData
; /* point to next arena */
1856 if (ptr
> (char *)currentheap
->base
+ currentheap
->size
- 1)
1857 { /* proceed with next subheap */
1858 struct list
*next
= list_next( &heapPtr
->subheap_list
, ¤theap
->entry
);
1860 { /* successfully finished */
1861 TRACE("end reached.\n");
1862 ret
= STATUS_NO_MORE_ENTRIES
;
1865 currentheap
= LIST_ENTRY( next
, SUBHEAP
, entry
);
1866 ptr
= (char *)currentheap
->base
+ currentheap
->headerSize
;
1871 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1873 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1875 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1877 entry
->lpData
= pArena
+ 1;
1878 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1879 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1880 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1884 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1886 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1888 entry
->lpData
= pArena
+ 1;
1889 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1890 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1891 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1892 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1893 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1896 entry
->iRegionIndex
= region_index
;
1898 /* first element of heap ? */
1899 if (ptr
== (char *)currentheap
->base
+ currentheap
->headerSize
)
1901 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1902 entry
->u
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1903 entry
->u
.Region
.dwUnCommittedSize
=
1904 currentheap
->size
- currentheap
->commitSize
;
1905 entry
->u
.Region
.lpFirstBlock
= /* first valid block */
1906 (char *)currentheap
->base
+ currentheap
->headerSize
;
1907 entry
->u
.Region
.lpLastBlock
= /* first invalid block */
1908 (char *)currentheap
->base
+ currentheap
->size
;
1910 ret
= STATUS_SUCCESS
;
1911 if (TRACE_ON(heap
)) HEAP_DumpEntry(entry
);
1914 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) RtlLeaveCriticalSection( &heapPtr
->critSection
);
1919 /***********************************************************************
1920 * RtlGetProcessHeaps (NTDLL.@)
1922 * Get the Heaps belonging to the current process.
1925 * count [I] size of heaps
1926 * heaps [O] Destination array for heap HANDLE's
1929 * Success: The number of Heaps allocated by the process.
1932 ULONG WINAPI
RtlGetProcessHeaps( ULONG count
, HANDLE
*heaps
)
1934 ULONG total
= 1; /* main heap */
1937 RtlEnterCriticalSection( &processHeap
->critSection
);
1938 LIST_FOR_EACH( ptr
, &processHeap
->entry
) total
++;
1941 *heaps
++ = processHeap
;
1942 LIST_FOR_EACH( ptr
, &processHeap
->entry
)
1943 *heaps
++ = LIST_ENTRY( ptr
, HEAP
, entry
);
1945 RtlLeaveCriticalSection( &processHeap
->critSection
);