Sync to trunk head(r38096)
[reactos.git] / reactos / lib / rtl / heap.c
1 /* COPYRIGHT: See COPYING in the top level directory
2 * PROJECT: ReactOS system libraries
3 * FILE: lib/rtl/image.c
4 * PURPOSE: Image handling functions
5 * PROGRAMMERS: Copyright 1996 Alexandre Julliard
6 * Copyright 1998 Ulrich Weigand
7 */
8
9 //
10 // Note: This is a slightly modified implementation of WINE's.
11 //
12 // WINE's implementation is a hack based on Windows 95's heap implementation,
13 // itself a hack of DOS memory management.It supports 3 out of the 18 possible
14 // NT Heap Flags, does not support custom allocation/deallocation routines,
15 // and is about 50-80x slower with fragmentation rates up to 500x higher when
16 // compared to NT's LFH. WINE is lucky because the advanced NT Heap features are
17 // used in kernel-mode usually, not in user-mode, and they are crossing their
18 // fingers for this being the same. Note that several high-end SQL/Database
19 // applications would significantly benefit from custom heap features provided
20 // by NT.
21 //
22 // ROS's changes include:
23 // - Using Zw instead of Nt calls, because this matters when in Kernel Mode
24 // - Not using per-process heap lists while in Kernel Mode
25 // - Using a macro to handle the Critical Section, because it's meaningless
26 // in Kernel Mode.
27 // - Crappy support for a custom Commit routine.
28 // - Crappy support for User-defined flags and the User-defined value.
29 // - Ripping out all the code for shared heaps, because those don't exist on NT.
30 //
31 // Be aware of these changes when you try to sync something back.
32 //
33
34 /* INCLUDES *****************************************************************/
35
36 #include <rtl.h>
37 #undef LIST_FOR_EACH
38 #undef LIST_FOR_EACH_SAFE
39 #include <wine/list.h>
40
41 #define NDEBUG
42 #include <debug.h>
43
44 #define TRACE DPRINT
45 #define WARN DPRINT1
46 #define ERR DPRINT1
47 #define DPRINTF DPRINT
48
49 /* FUNCTIONS *****************************************************************/
50
51 #define WARN_ON(x) (1)
52
53 #ifdef NDEBUG
54 #define TRACE_ON(x) (0)
55 #else
56 #define TRACE_ON(x) (1)
57 #endif
58
59 /* Note: the heap data structures are based on what Pietrek describes in his
60 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
61 * the same, but could be easily adapted if it turns out some programs
62 * require it.
63 */
64
65 /* FIXME: use SIZE_T for 'size' structure members, but we need to make sure
66 * that there is no unaligned accesses to structure fields.
67 */
68
69 typedef struct tagARENA_INUSE
70 {
71 SIZE_T size; /* Block size; must be the first field */
72 SIZE_T magic : 23; /* Magic number */
73 SIZE_T has_user_data : 1; /* There is user data associated with this block */
74 SIZE_T 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) */
75 } ARENA_INUSE;
76
77 typedef struct tagARENA_FREE
78 {
79 SIZE_T size; /* Block size; must be the first field */
80 SIZE_T magic; /* Magic number */
81 struct list entry; /* Entry in free list */
82 } ARENA_FREE;
83
84 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
85 #define ARENA_FLAG_PREV_FREE 0x00000002
86 #define ARENA_INUSE_MAGIC 0x455355 /* Value for arena 'magic' field */
87 #define ARENA_FREE_MAGIC 0x45455246 /* Value for arena 'magic' field */
88
89 #ifndef _WIN64
90 #define ARENA_SIZE_MASK (~3L)
91 #else
92 #define ARENA_SIZE_MASK (~7L)
93 #endif
94
95 #define ARENA_INUSE_FILLER 0x55
96 #define ARENA_FREE_FILLER 0xaa
97
98 #ifndef _WIN64
99 #define ALIGNMENT 8 /* everything is aligned on 8 byte boundaries */
100 #else
101 #define ALIGNMENT 16
102 #endif
103
104 #define ROUND_SIZE(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
105
106 #define QUIET 1 /* Suppress messages */
107 #define NOISY 0 /* Report all errors */
108
109 /* minimum data size (without arenas) of an allocated block */
110 #define HEAP_MIN_DATA_SIZE 16
111 /* minimum size that must remain to shrink an allocated block */
112 #define HEAP_MIN_SHRINK_SIZE (HEAP_MIN_DATA_SIZE+sizeof(ARENA_FREE))
113
114 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
115
116 /* Max size of the blocks on the free lists */
117 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
118 {
119 0x20, 0x80, 0x200, ~0UL
120 };
121
122 typedef struct
123 {
124 ARENA_FREE arena;
125 } FREE_LIST_ENTRY;
126
127 struct tagHEAP;
128
129 typedef struct tagSUBHEAP
130 {
131 SIZE_T size; /* Size of the whole sub-heap */
132 SIZE_T commitSize; /* Committed size of the sub-heap */
133 SIZE_T headerSize; /* Size of the heap header */
134 struct tagSUBHEAP *next; /* Next sub-heap */
135 struct tagHEAP *heap; /* Main heap structure */
136 SIZE_T magic; /* Magic number */
137 } SUBHEAP;
138
139 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
140
141 typedef struct tagHEAP_USER_DATA
142 {
143 LIST_ENTRY ListEntry;
144 PVOID BaseAddress;
145 ULONG UserFlags;
146 PVOID UserValue;
147 } HEAP_USER_DATA, *PHEAP_USER_DATA;
148
149 typedef struct tagHEAP
150 {
151 SUBHEAP subheap; /* First sub-heap */
152 struct list entry; /* Entry in process heap list */
153 RTL_CRITICAL_SECTION critSection; /* Critical section for serialization */
154 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
155 DWORD flags; /* Heap flags */
156 DWORD magic; /* Magic number */
157 PRTL_HEAP_COMMIT_ROUTINE commitRoutine;
158 LIST_ENTRY UserDataHead;
159 } HEAP;
160
161 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
162
163 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
164 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
165
166 static HEAP *processHeap; /* main process heap */
167
168 static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
169
170 /* mark a block of memory as free for debugging purposes */
171 static __inline void mark_block_free( void *ptr, SIZE_T size )
172 {
173 if (TRACE_ON(heap)) memset( ptr, ARENA_FREE_FILLER, size );
174 #ifdef VALGRIND_MAKE_NOACCESS
175 VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
176 #endif
177 }
178
179 /* mark a block of memory as initialized for debugging purposes */
180 static __inline void mark_block_initialized( void *ptr, SIZE_T size )
181 {
182 #ifdef VALGRIND_MAKE_READABLE
183 VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ));
184 #endif
185 }
186
187 /* mark a block of memory as uninitialized for debugging purposes */
188 static __inline void mark_block_uninitialized( void *ptr, SIZE_T size )
189 {
190 #ifdef VALGRIND_MAKE_WRITABLE
191 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
192 #endif
193 if (TRACE_ON(heap))
194 {
195 memset( ptr, ARENA_INUSE_FILLER, size );
196 #ifdef VALGRIND_MAKE_WRITABLE
197 /* make it uninitialized to valgrind again */
198 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
199 #endif
200 }
201 }
202
203 /* clear contents of a block of memory */
204 static __inline void clear_block( void *ptr, SIZE_T size )
205 {
206 mark_block_initialized( ptr, size );
207 memset( ptr, 0, size );
208 }
209
210 /* locate a free list entry of the appropriate size */
211 /* size is the size of the whole block including the arena header */
212 static __inline unsigned int get_freelist_index( SIZE_T size )
213 {
214 unsigned int i;
215
216 size -= sizeof(ARENA_FREE);
217 for (i = 0; i < HEAP_NB_FREE_LISTS - 1; i++) if (size <= HEAP_freeListSizes[i]) break;
218 return i;
219 }
220
221 static RTL_CRITICAL_SECTION_DEBUG process_heap_critsect_debug =
222 {
223 0, 0, NULL, /* will be set later */
224 { &process_heap_critsect_debug.ProcessLocksList, &process_heap_critsect_debug.ProcessLocksList },
225 0, 0, 0, 0, 0
226 };
227
228 /***********************************************************************
229 * HEAP_Dump
230 */
231 static void HEAP_Dump( HEAP *heap )
232 {
233 int i;
234 SUBHEAP *subheap;
235 char *ptr;
236
237 DPRINTF( "Heap: %p\n", heap );
238 DPRINTF( "Next: %p Sub-heaps: %p",
239 LIST_ENTRY( heap->entry.next, HEAP, entry ), &heap->subheap );
240 subheap = &heap->subheap;
241 while (subheap->next)
242 {
243 DPRINTF( " -> %p", subheap->next );
244 subheap = subheap->next;
245 }
246
247 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
248 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
249 DPRINTF( "%p free %08lx prev=%p next=%p\n",
250 &heap->freeList[i].arena, HEAP_freeListSizes[i],
251 LIST_ENTRY( heap->freeList[i].arena.entry.prev, ARENA_FREE, entry ),
252 LIST_ENTRY( heap->freeList[i].arena.entry.next, ARENA_FREE, entry ));
253
254 subheap = &heap->subheap;
255 while (subheap)
256 {
257 SIZE_T freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
258 DPRINTF( "\n\nSub-heap %p: size=%08lx committed=%08lx\n",
259 subheap, subheap->size, subheap->commitSize );
260
261 DPRINTF( "\n Block Stat Size Id\n" );
262 ptr = (char*)subheap + subheap->headerSize;
263 while (ptr < (char *)subheap + subheap->size)
264 {
265 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
266 {
267 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
268 DPRINTF( "%p free %08lx prev=%p next=%p\n",
269 pArena, pArena->size & ARENA_SIZE_MASK,
270 LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry ),
271 LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry ) );
272 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
273 arenaSize += sizeof(ARENA_FREE);
274 freeSize += pArena->size & ARENA_SIZE_MASK;
275 }
276 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
277 {
278 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
279 DPRINTF( "%p Used %08lx back=%p\n",
280 pArena, pArena->size & ARENA_SIZE_MASK, *((ARENA_FREE **)pArena - 1) );
281 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
282 arenaSize += sizeof(ARENA_INUSE);
283 usedSize += pArena->size & ARENA_SIZE_MASK;
284 }
285 else
286 {
287 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
288 DPRINTF( "%p used %08lx\n", pArena, pArena->size & ARENA_SIZE_MASK );
289 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
290 arenaSize += sizeof(ARENA_INUSE);
291 usedSize += pArena->size & ARENA_SIZE_MASK;
292 }
293 }
294 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
295 subheap->size, subheap->commitSize, freeSize, usedSize,
296 arenaSize, (arenaSize * 100) / subheap->size );
297 subheap = subheap->next;
298 }
299 }
300
301 #if 0
302 static void HEAP_DumpEntry( LPPROCESS_HEAP_ENTRY entry )
303 {
304 WORD rem_flags;
305 TRACE( "Dumping entry %p\n", entry );
306 TRACE( "lpData\t\t: %p\n", entry->lpData );
307 TRACE( "cbData\t\t: %08lx\n", entry->cbData);
308 TRACE( "cbOverhead\t: %08x\n", entry->cbOverhead);
309 TRACE( "iRegionIndex\t: %08x\n", entry->iRegionIndex);
310 TRACE( "WFlags\t\t: ");
311 if (entry->wFlags & PROCESS_HEAP_REGION)
312 TRACE( "PROCESS_HEAP_REGION ");
313 if (entry->wFlags & PROCESS_HEAP_UNCOMMITTED_RANGE)
314 TRACE( "PROCESS_HEAP_UNCOMMITTED_RANGE ");
315 if (entry->wFlags & PROCESS_HEAP_ENTRY_BUSY)
316 TRACE( "PROCESS_HEAP_ENTRY_BUSY ");
317 if (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE)
318 TRACE( "PROCESS_HEAP_ENTRY_MOVEABLE ");
319 if (entry->wFlags & PROCESS_HEAP_ENTRY_DDESHARE)
320 TRACE( "PROCESS_HEAP_ENTRY_DDESHARE ");
321 rem_flags = entry->wFlags &
322 ~(PROCESS_HEAP_REGION | PROCESS_HEAP_UNCOMMITTED_RANGE |
323 PROCESS_HEAP_ENTRY_BUSY | PROCESS_HEAP_ENTRY_MOVEABLE|
324 PROCESS_HEAP_ENTRY_DDESHARE);
325 if (rem_flags)
326 TRACE( "Unknown %08x", rem_flags);
327 TRACE( "\n");
328 if ((entry->wFlags & PROCESS_HEAP_ENTRY_BUSY )
329 && (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE))
330 {
331 /* Treat as block */
332 TRACE( "BLOCK->hMem\t\t:%p\n", entry->Block.hMem);
333 }
334 if (entry->wFlags & PROCESS_HEAP_REGION)
335 {
336 TRACE( "Region.dwCommittedSize\t:%08lx\n",entry->Region.dwCommittedSize);
337 TRACE( "Region.dwUnCommittedSize\t:%08lx\n",entry->Region.dwUnCommittedSize);
338 TRACE( "Region.lpFirstBlock\t:%p\n",entry->Region.lpFirstBlock);
339 TRACE( "Region.lpLastBlock\t:%p\n",entry->Region.lpLastBlock);
340 }
341 }
342 #endif
343
344 static PHEAP_USER_DATA HEAP_GetUserData(HEAP *heapPtr, PVOID BaseAddress)
345 {
346 PLIST_ENTRY CurrentEntry;
347 PHEAP_USER_DATA udata;
348
349 CurrentEntry = heapPtr->UserDataHead.Flink;
350 while (CurrentEntry != &heapPtr->UserDataHead)
351 {
352 udata = CONTAINING_RECORD(CurrentEntry, HEAP_USER_DATA, ListEntry);
353 if (udata->BaseAddress == BaseAddress)
354 return udata;
355 CurrentEntry = CurrentEntry->Flink;
356 }
357 return NULL;
358 }
359
360 static PHEAP_USER_DATA HEAP_AllocUserData(HEAP *heapPtr, PVOID BaseAddress)
361 {
362 /* Allocate user data entry */
363 ARENA_INUSE *pInUse;
364 PHEAP_USER_DATA udata = RtlAllocateHeap(heapPtr, 0, sizeof(HEAP_USER_DATA));
365 if (!udata) return NULL;
366 udata->BaseAddress = BaseAddress;
367 InsertTailList(&heapPtr->UserDataHead, &udata->ListEntry);
368 pInUse = (ARENA_INUSE *)BaseAddress - 1;
369 pInUse->has_user_data = 1;
370 return udata;
371 }
372
373 /***********************************************************************
374 * HEAP_GetPtr
375 * RETURNS
376 * Pointer to the heap
377 * NULL: Failure
378 */
379 static HEAP *HEAP_GetPtr(
380 HANDLE heap /* [in] Handle to the heap */
381 ) {
382 HEAP *heapPtr = (HEAP *)heap;
383 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
384 {
385 if (heapPtr)
386 ERR("Invalid heap %p, magic:%.4s!\n", heap, &heapPtr->magic );
387 else
388 ERR("Invalid heap %p!\n", heap );
389 //KeDumpStackFrames(NULL);
390 return NULL;
391 }
392 if (TRACE_ON(heap) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
393 {
394 HEAP_Dump( heapPtr );
395 assert( FALSE );
396 return NULL;
397 }
398 return heapPtr;
399 }
400
401
402 /***********************************************************************
403 * HEAP_InsertFreeBlock
404 *
405 * Insert a free block into the free list.
406 */
407 static __inline void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena, BOOL last )
408 {
409 FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( pArena->size + sizeof(*pArena) );
410 if (last)
411 {
412 /* insert at end of free list, i.e. before the next free list entry */
413 pEntry++;
414 if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS]) pEntry = heap->freeList;
415 list_add_before( &pEntry->arena.entry, &pArena->entry );
416 }
417 else
418 {
419 /* insert at head of free list */
420 list_add_after( &pEntry->arena.entry, &pArena->entry );
421 }
422 pArena->size |= ARENA_FLAG_FREE;
423 }
424
425
426 /***********************************************************************
427 * HEAP_FindSubHeap
428 * Find the sub-heap containing a given address.
429 *
430 * RETURNS
431 * Pointer: Success
432 * NULL: Failure
433 */
434 static SUBHEAP *HEAP_FindSubHeap(
435 const HEAP *heap, /* [in] Heap pointer */
436 LPCVOID ptr /* [in] Address */
437 ) {
438 const SUBHEAP *sub = &heap->subheap;
439 while (sub)
440 {
441 if (((const char *)ptr >= (const char *)sub) &&
442 ((const char *)ptr < (const char *)sub + sub->size)) return (SUBHEAP*)sub;
443 sub = sub->next;
444 }
445 return NULL;
446 }
447
448 /***********************************************************************
449 * HEAP_Commit
450 *
451 * Make sure the heap storage is committed for a given size in the specified arena.
452 */
453 static __inline BOOL HEAP_Commit( SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T data_size )
454 {
455 NTSTATUS Status;
456 void *ptr = (char *)(pArena + 1) + data_size + sizeof(ARENA_FREE);
457 SIZE_T size = (char *)ptr - (char *)subheap;
458 size = (size + COMMIT_MASK) & ~COMMIT_MASK;
459 if (size > subheap->size) size = subheap->size;
460 if (size <= subheap->commitSize) return TRUE;
461 size -= subheap->commitSize;
462 ptr = (char *)subheap + subheap->commitSize;
463 if (subheap->heap->commitRoutine != NULL)
464 {
465 Status = subheap->heap->commitRoutine(subheap->heap, &ptr, &size);
466 }
467 else
468 {
469 Status = ZwAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0,
470 &size, MEM_COMMIT, PAGE_READWRITE );
471 }
472 if (!NT_SUCCESS(Status))
473 {
474 WARN("Could not commit %08lx bytes at %p for heap %p\n",
475 size, ptr, subheap->heap );
476 return FALSE;
477 }
478 subheap->commitSize += size;
479 return TRUE;
480 }
481
482 #if 0
483 /***********************************************************************
484 * HEAP_Decommit
485 *
486 * If possible, decommit the heap storage from (including) 'ptr'.
487 */
488 static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
489 {
490 void *addr;
491 SIZE_T decommit_size;
492 SIZE_T size = (char *)ptr - (char *)subheap;
493
494 /* round to next block and add one full block */
495 size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
496 if (size >= subheap->commitSize) return TRUE;
497 decommit_size = subheap->commitSize - size;
498 addr = (char *)subheap + size;
499
500 if (ZwFreeVirtualMemory( NtCurrentProcess(), &addr, &decommit_size, MEM_DECOMMIT ))
501 {
502 WARN("Could not decommit %08lx bytes at %p for heap %p\n",
503 decommit_size, (char *)subheap + size, subheap->heap );
504 return FALSE;
505 }
506 subheap->commitSize -= decommit_size;
507 return TRUE;
508 }
509 #endif
510
511 /***********************************************************************
512 * HEAP_CreateFreeBlock
513 *
514 * Create a free block at a specified address. 'size' is the size of the
515 * whole block, including the new arena.
516 */
517 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, SIZE_T size )
518 {
519 ARENA_FREE *pFree;
520 char *pEnd;
521 BOOL last;
522
523 /* Create a free arena */
524 mark_block_uninitialized( ptr, sizeof( ARENA_FREE ) );
525 pFree = (ARENA_FREE *)ptr;
526 pFree->magic = ARENA_FREE_MAGIC;
527
528 /* If debugging, erase the freed block content */
529
530 pEnd = (char *)ptr + size;
531 if (pEnd > (char *)subheap + subheap->commitSize) pEnd = (char *)subheap + subheap->commitSize;
532 if (pEnd > (char *)(pFree + 1)) mark_block_free( pFree + 1, pEnd - (char *)(pFree + 1) );
533
534 /* Check if next block is free also */
535
536 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
537 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
538 {
539 /* Remove the next arena from the free list */
540 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
541 list_remove( &pNext->entry );
542 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
543 mark_block_free( pNext, sizeof(ARENA_FREE) );
544 }
545
546 /* Set the next block PREV_FREE flag and pointer */
547
548 last = ((char *)ptr + size >= (char *)subheap + subheap->size);
549 if (!last)
550 {
551 DWORD *pNext = (DWORD *)((char *)ptr + size);
552 *pNext |= ARENA_FLAG_PREV_FREE;
553 mark_block_initialized( pNext - 1, sizeof( ARENA_FREE * ) );
554 *((ARENA_FREE **)pNext - 1) = pFree;
555 }
556
557 /* Last, insert the new block into the free list */
558
559 pFree->size = size - sizeof(*pFree);
560 HEAP_InsertFreeBlock( subheap->heap, pFree, last );
561 }
562
563
564 /***********************************************************************
565 * HEAP_MakeInUseBlockFree
566 *
567 * Turn an in-use block into a free block. Can also decommit the end of
568 * the heap, and possibly even free the sub-heap altogether.
569 */
570 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
571 {
572 ARENA_FREE *pFree;
573 SIZE_T size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
574 PHEAP_USER_DATA udata;
575
576 /* Find and free user data */
577 if (pArena->has_user_data)
578 {
579 udata = HEAP_GetUserData(subheap->heap, pArena + 1);
580 if (udata)
581 {
582 RemoveEntryList(&udata->ListEntry);
583 RtlFreeHeap(subheap->heap, 0, udata);
584 }
585 }
586
587 /* Check if we can merge with previous block */
588
589 if (pArena->size & ARENA_FLAG_PREV_FREE)
590 {
591 pFree = *((ARENA_FREE **)pArena - 1);
592 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
593 /* Remove it from the free list */
594 list_remove( &pFree->entry );
595 }
596 else pFree = (ARENA_FREE *)pArena;
597
598 /* Create a free block */
599
600 HEAP_CreateFreeBlock( subheap, pFree, size );
601 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
602 if ((char *)pFree + size < (char *)subheap + subheap->size)
603 return; /* Not the last block, so nothing more to do */
604
605 /* Free the whole sub-heap if it's empty and not the original one */
606
607 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
608 (subheap != &subheap->heap->subheap))
609 {
610 SIZE_T size = 0;
611 SUBHEAP *pPrev = &subheap->heap->subheap;
612 /* Remove the free block from the list */
613 list_remove( &pFree->entry );
614 /* Remove the subheap from the list */
615 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
616 if (pPrev) pPrev->next = subheap->next;
617 /* Free the memory */
618 subheap->magic = 0;
619 ZwFreeVirtualMemory( NtCurrentProcess(), (void **)&subheap, &size, MEM_RELEASE );
620 return;
621 }
622
623 /* Decommit the end of the heap */
624 }
625
626 /***********************************************************************
627 * HEAP_ShrinkBlock
628 *
629 * Shrink an in-use block.
630 */
631 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T size)
632 {
633 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_SHRINK_SIZE)
634 {
635 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
636 (pArena->size & ARENA_SIZE_MASK) - size );
637 /* assign size plus previous arena flags */
638 pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
639 }
640 else
641 {
642 /* Turn off PREV_FREE flag in next block */
643 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
644 if (pNext < (char *)subheap + subheap->size)
645 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
646 }
647 }
648
649 /***********************************************************************
650 * HEAP_InitSubHeap
651 */
652 static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
653 SIZE_T commitSize, SIZE_T totalSize,
654 PRTL_HEAP_PARAMETERS Parameters)
655 {
656 SUBHEAP *subheap;
657 FREE_LIST_ENTRY *pEntry;
658 int i;
659 NTSTATUS Status;
660
661 if (!address && ZwAllocateVirtualMemory( NtCurrentProcess(), &address, 0,
662 &commitSize, MEM_COMMIT, PAGE_READWRITE ))
663 {
664 WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
665 return FALSE;
666 }
667
668 /* Fill the sub-heap structure */
669
670 subheap = (SUBHEAP *)address;
671 subheap->heap = heap;
672 subheap->size = totalSize;
673 subheap->commitSize = commitSize;
674 subheap->magic = SUBHEAP_MAGIC;
675
676 if ( subheap != (SUBHEAP *)heap )
677 {
678 /* If this is a secondary subheap, insert it into list */
679
680 subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
681 subheap->next = heap->subheap.next;
682 heap->subheap.next = subheap;
683 }
684 else
685 {
686 /* If this is a primary subheap, initialize main heap */
687
688 subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
689 subheap->next = NULL;
690 heap->flags = flags;
691 heap->magic = HEAP_MAGIC;
692 if (Parameters)
693 heap->commitRoutine = Parameters->CommitRoutine;
694 else
695 heap->commitRoutine = NULL;
696 InitializeListHead(&heap->UserDataHead);
697
698 /* Build the free lists */
699
700 list_init( &heap->freeList[0].arena.entry );
701 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
702 {
703 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
704 pEntry->arena.magic = ARENA_FREE_MAGIC;
705 if (i) list_add_after( &pEntry[-1].arena.entry, &pEntry->arena.entry );
706 }
707
708 /* Initialize critical section */
709
710 if (RtlpGetMode() == UserMode)
711 {
712 if (!processHeap) /* do it by hand to avoid memory allocations */
713 {
714 heap->critSection.DebugInfo = &process_heap_critsect_debug;
715 heap->critSection.LockCount = -1;
716 heap->critSection.RecursionCount = 0;
717 heap->critSection.OwningThread = 0;
718 heap->critSection.LockSemaphore = 0;
719 heap->critSection.SpinCount = 0;
720 process_heap_critsect_debug.CriticalSection = &heap->critSection;
721 }
722 else RtlInitializeHeapLock( &heap->critSection );
723 }
724 }
725
726 /* Commit memory */
727 if (heap->commitRoutine)
728 {
729 if (subheap != (SUBHEAP *)heap)
730 {
731 Status = heap->commitRoutine(heap, &address, &commitSize);
732 }
733 else
734 {
735 /* the caller is responsible for committing the first page! */
736 Status = STATUS_SUCCESS;
737 }
738 }
739 else
740 {
741 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
742 &address,
743 0,
744 &commitSize,
745 MEM_COMMIT,
746 PAGE_EXECUTE_READWRITE);
747 }
748 if (!NT_SUCCESS(Status))
749 {
750 DPRINT("Could not commit %08lx bytes for sub-heap %p\n",
751 commitSize, address);
752 return FALSE;
753 }
754
755 /* Create the first free block */
756
757 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
758 subheap->size - subheap->headerSize );
759
760 return TRUE;
761 }
762
763 /***********************************************************************
764 * HEAP_CreateSubHeap
765 *
766 * Create a sub-heap of the given size.
767 * If heap == NULL, creates a main heap.
768 */
769 static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, void *base, DWORD flags,
770 SIZE_T commitSize, SIZE_T totalSize,
771 IN PRTL_HEAP_PARAMETERS Parameters)
772 {
773 LPVOID address = base;
774
775 /* round-up sizes on a 64K boundary */
776 totalSize = (totalSize + 0xffff) & 0xffff0000;
777 commitSize = (commitSize + 0xffff) & 0xffff0000;
778 if (!commitSize) commitSize = 0x10000;
779 if (totalSize < commitSize) totalSize = commitSize;
780
781 if (!address)
782 {
783 /* allocate the memory block */
784 if (ZwAllocateVirtualMemory( NtCurrentProcess(), &address, 0, &totalSize,
785 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE ))
786 {
787 WARN("Could not allocate %08lx bytes\n", totalSize );
788 return NULL;
789 }
790 }
791
792 /* Initialize subheap */
793
794 if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
795 address, flags, commitSize, totalSize, Parameters ))
796 {
797 SIZE_T size = 0;
798 if (!base) ZwFreeVirtualMemory( NtCurrentProcess(), &address, &size, MEM_RELEASE );
799 return NULL;
800 }
801
802 return (SUBHEAP *)address;
803 }
804
805
806 /***********************************************************************
807 * HEAP_FindFreeBlock
808 *
809 * Find a free block at least as large as the requested size, and make sure
810 * the requested size is committed.
811 */
812 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, SIZE_T size,
813 SUBHEAP **ppSubHeap )
814 {
815 SUBHEAP *subheap;
816 struct list *ptr;
817 FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( size + sizeof(ARENA_INUSE) );
818
819 /* Find a suitable free list, and in it find a block large enough */
820
821 ptr = &pEntry->arena.entry;
822 while ((ptr = list_next( &heap->freeList[0].arena.entry, ptr )))
823 {
824 ARENA_FREE *pArena = LIST_ENTRY( ptr, ARENA_FREE, entry );
825 SIZE_T arena_size = (pArena->size & ARENA_SIZE_MASK) +
826 sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
827 if (arena_size >= size)
828 {
829 subheap = HEAP_FindSubHeap( heap, pArena );
830 if (!HEAP_Commit( subheap, (ARENA_INUSE *)pArena, size )) return NULL;
831 *ppSubHeap = subheap;
832 return pArena;
833 }
834 }
835
836 /* If no block was found, attempt to grow the heap */
837
838 if (!(heap->flags & HEAP_GROWABLE))
839 {
840 ERR("Not enough space in heap %p for %08lx bytes\n", heap, size );
841 return NULL;
842 }
843 /* make sure that we have a big enough size *committed* to fit another
844 * last free arena in !
845 * So just one heap struct, one first free arena which will eventually
846 * get used, and a second free arena that might get assigned all remaining
847 * free space in HEAP_ShrinkBlock() */
848 size += ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + sizeof(ARENA_FREE);
849 if (!(subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, size,
850 max( HEAP_DEF_SIZE, size ), NULL )))
851 return NULL;
852
853 TRACE("created new sub-heap %p of %08lx bytes for heap %p\n",
854 subheap, size, heap );
855
856 *ppSubHeap = subheap;
857 return (ARENA_FREE *)(subheap + 1);
858 }
859
860 /***********************************************************************
861 * HEAP_IsValidArenaPtr
862 *
863 * Check that the pointer is inside the range possible for arenas.
864 */
865 static BOOL HEAP_IsValidArenaPtr( const HEAP *heap, const void *ptr )
866 {
867 int i;
868 const SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
869 if (!subheap) return FALSE;
870 if ((const char *)ptr >= (const char *)subheap + subheap->headerSize) return TRUE;
871 if (subheap != &heap->subheap) return FALSE;
872 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
873 if (ptr == (const void *)&heap->freeList[i].arena) return TRUE;
874 return FALSE;
875 }
876
877
878 /***********************************************************************
879 * HEAP_ValidateFreeArena
880 */
881 static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
882 {
883 ARENA_FREE *prev, *next;
884 char *heapEnd = (char *)subheap + subheap->size;
885
886 /* Check for unaligned pointers */
887 if ( (ULONG_PTR)pArena % ALIGNMENT != 0 )
888 {
889 ERR("Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
890 return FALSE;
891 }
892
893 /* Check magic number */
894 if (pArena->magic != ARENA_FREE_MAGIC)
895 {
896 ERR("Heap %p: invalid free arena magic for %p\n", subheap->heap, pArena );
897 return FALSE;
898 }
899 /* Check size flags */
900 if (!(pArena->size & ARENA_FLAG_FREE) ||
901 (pArena->size & ARENA_FLAG_PREV_FREE))
902 {
903 ERR("Heap %p: bad flags %08lx for free arena %p\n",
904 subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
905 return FALSE;
906 }
907 /* Check arena size */
908 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
909 {
910 ERR("Heap %p: bad size %08lx for free arena %p\n",
911 subheap->heap, pArena->size & ARENA_SIZE_MASK, pArena );
912 return FALSE;
913 }
914 /* Check that next pointer is valid */
915 next = LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry );
916 if (!HEAP_IsValidArenaPtr( subheap->heap, next ))
917 {
918 ERR("Heap %p: bad next ptr %p for arena %p\n",
919 subheap->heap, next, pArena );
920 return FALSE;
921 }
922 /* Check that next arena is free */
923 if (!(next->size & ARENA_FLAG_FREE) || (next->magic != ARENA_FREE_MAGIC))
924 {
925 ERR("Heap %p: next arena %p invalid for %p\n",
926 subheap->heap, next, pArena );
927 return FALSE;
928 }
929 /* Check that prev pointer is valid */
930 prev = LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry );
931 if (!HEAP_IsValidArenaPtr( subheap->heap, prev ))
932 {
933 ERR("Heap %p: bad prev ptr %p for arena %p\n",
934 subheap->heap, prev, pArena );
935 return FALSE;
936 }
937 /* Check that prev arena is free */
938 if (!(prev->size & ARENA_FLAG_FREE) || (prev->magic != ARENA_FREE_MAGIC))
939 {
940 /* this often means that the prev arena got overwritten
941 * by a memory write before that prev arena */
942 ERR("Heap %p: prev arena %p invalid for %p\n",
943 subheap->heap, prev, pArena );
944 return FALSE;
945 }
946 /* Check that next block has PREV_FREE flag */
947 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
948 {
949 if (!(*(DWORD *)((char *)(pArena + 1) +
950 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
951 {
952 ERR("Heap %p: free arena %p next block has no PREV_FREE flag\n",
953 subheap->heap, pArena );
954 return FALSE;
955 }
956 /* Check next block back pointer */
957 if (*((ARENA_FREE **)((char *)(pArena + 1) +
958 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
959 {
960 ERR("Heap %p: arena %p has wrong back ptr %p\n",
961 subheap->heap, pArena,
962 *((ARENA_FREE **)((char *)(pArena+1) + (pArena->size & ARENA_SIZE_MASK)) - 1));
963 return FALSE;
964 }
965 }
966 return TRUE;
967 }
968
969 /***********************************************************************
970 * HEAP_ValidateInUseArena
971 */
972 static BOOL HEAP_ValidateInUseArena( const SUBHEAP *subheap, const ARENA_INUSE *pArena, BOOL quiet )
973 {
974 const char *heapEnd = (const char *)subheap + subheap->size;
975
976 /* Check for unaligned pointers */
977 if ( (ULONG_PTR)pArena % ALIGNMENT != 0 )
978 {
979 if ( quiet == NOISY )
980 {
981 ERR( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
982 if ( TRACE_ON(heap) )
983 HEAP_Dump( subheap->heap );
984 }
985 else if ( WARN_ON(heap) )
986 {
987 WARN( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
988 if ( TRACE_ON(heap) )
989 HEAP_Dump( subheap->heap );
990 }
991 return FALSE;
992 }
993
994 /* Check magic number */
995 if (pArena->magic != ARENA_INUSE_MAGIC)
996 {
997 if (quiet == NOISY) {
998 ERR("Heap %p: invalid in-use arena magic for %p\n", subheap->heap, pArena );
999 if (TRACE_ON(heap))
1000 HEAP_Dump( subheap->heap );
1001 } else if (WARN_ON(heap)) {
1002 WARN("Heap %p: invalid in-use arena magic for %p\n", subheap->heap, pArena );
1003 if (TRACE_ON(heap))
1004 HEAP_Dump( subheap->heap );
1005 }
1006 return FALSE;
1007 }
1008 /* Check size flags */
1009 if (pArena->size & ARENA_FLAG_FREE)
1010 {
1011 ERR("Heap %p: bad flags %08lx for in-use arena %p\n",
1012 subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
1013 return FALSE;
1014 }
1015 /* Check arena size */
1016 if ((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
1017 {
1018 ERR("Heap %p: bad size %08lx for in-use arena %p\n",
1019 subheap->heap, pArena->size & ARENA_SIZE_MASK, pArena );
1020 return FALSE;
1021 }
1022 /* Check next arena PREV_FREE flag */
1023 if (((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
1024 (*(const DWORD *)((const char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
1025 {
1026 ERR("Heap %p: in-use arena %p next block has PREV_FREE flag\n",
1027 subheap->heap, pArena );
1028 return FALSE;
1029 }
1030 /* Check prev free arena */
1031 if (pArena->size & ARENA_FLAG_PREV_FREE)
1032 {
1033 const ARENA_FREE *pPrev = *((const ARENA_FREE * const*)pArena - 1);
1034 /* Check prev pointer */
1035 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
1036 {
1037 ERR("Heap %p: bad back ptr %p for arena %p\n",
1038 subheap->heap, pPrev, pArena );
1039 return FALSE;
1040 }
1041 /* Check that prev arena is free */
1042 if (!(pPrev->size & ARENA_FLAG_FREE) ||
1043 (pPrev->magic != ARENA_FREE_MAGIC))
1044 {
1045 ERR("Heap %p: prev arena %p invalid for in-use %p\n",
1046 subheap->heap, pPrev, pArena );
1047 return FALSE;
1048 }
1049 /* Check that prev arena is really the previous block */
1050 if ((const char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (const char *)pArena)
1051 {
1052 ERR("Heap %p: prev arena %p is not prev for in-use %p\n",
1053 subheap->heap, pPrev, pArena );
1054 return FALSE;
1055 }
1056 }
1057 return TRUE;
1058 }
1059
1060 /***********************************************************************
1061 * HEAP_IsRealArena [Internal]
1062 * Validates a block is a valid arena.
1063 *
1064 * RETURNS
1065 * TRUE: Success
1066 * FALSE: Failure
1067 */
1068 static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
1069 DWORD flags, /* [in] Bit flags that control access during operation */
1070 LPCVOID block, /* [in] Optional pointer to memory block to validate */
1071 BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
1072 * does not complain */
1073 {
1074 SUBHEAP *subheap;
1075 BOOL ret = TRUE;
1076
1077 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1078 {
1079 ERR("Invalid heap %p!\n", heapPtr );
1080 return FALSE;
1081 }
1082
1083 flags &= HEAP_NO_SERIALIZE;
1084 flags |= heapPtr->flags;
1085 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
1086 if (!(flags & HEAP_NO_SERIALIZE))
1087 RtlEnterHeapLock( &heapPtr->critSection );
1088
1089 if (block)
1090 {
1091 /* Only check this single memory block */
1092
1093 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
1094 ((const char *)block < (char *)subheap + subheap->headerSize
1095 + sizeof(ARENA_INUSE)))
1096 {
1097 if (quiet == NOISY)
1098 ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
1099 else if (WARN_ON(heap))
1100 WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
1101 ret = FALSE;
1102 } else
1103 ret = HEAP_ValidateInUseArena( subheap, (const ARENA_INUSE *)block - 1, quiet );
1104
1105 if (!(flags & HEAP_NO_SERIALIZE))
1106 RtlLeaveHeapLock( &heapPtr->critSection );
1107 return ret;
1108 }
1109
1110 subheap = &heapPtr->subheap;
1111 while (subheap && ret)
1112 {
1113 char *ptr = (char *)subheap + subheap->headerSize;
1114 while (ptr < (char *)subheap + subheap->size)
1115 {
1116 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1117 {
1118 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
1119 ret = FALSE;
1120 break;
1121 }
1122 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1123 }
1124 else
1125 {
1126 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
1127 ret = FALSE;
1128 break;
1129 }
1130 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1131 }
1132 }
1133 subheap = subheap->next;
1134 }
1135
1136 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1137 return ret;
1138 }
1139
1140
1141 /***********************************************************************
1142 * HeapCreate (KERNEL32.336)
1143 * RETURNS
1144 * Handle of heap: Success
1145 * NULL: Failure
1146 *
1147 * @implemented
1148 */
1149 HANDLE NTAPI
1150 RtlCreateHeap(ULONG flags,
1151 PVOID addr,
1152 SIZE_T totalSize,
1153 SIZE_T commitSize,
1154 PVOID Lock,
1155 PRTL_HEAP_PARAMETERS Parameters)
1156 {
1157 SUBHEAP *subheap;
1158
1159 /* Allocate the heap block */
1160
1161 if (!totalSize)
1162 {
1163 totalSize = HEAP_DEF_SIZE;
1164 flags |= HEAP_GROWABLE;
1165 }
1166 if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize, Parameters ))) return 0;
1167
1168 if (RtlpGetMode() == UserMode)
1169 {
1170 /* link it into the per-process heap list */
1171 if (processHeap)
1172 {
1173 HEAP *heapPtr = subheap->heap;
1174 RtlEnterHeapLock( &processHeap->critSection );
1175 list_add_head( &processHeap->entry, &heapPtr->entry );
1176 RtlLeaveHeapLock( &processHeap->critSection );
1177 }
1178 else
1179 {
1180 processHeap = subheap->heap; /* assume the first heap we create is the process main heap */
1181 list_init( &processHeap->entry );
1182 }
1183 }
1184
1185 return (HANDLE)subheap;
1186 }
1187
1188 /***********************************************************************
1189 * HeapDestroy (KERNEL32.337)
1190 * RETURNS
1191 * TRUE: Success
1192 * FALSE: Failure
1193 *
1194 * @implemented
1195 *
1196 * RETURNS
1197 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1198 * Failure: The Heap handle, if heap is the process heap.
1199 */
1200 HANDLE NTAPI
1201 RtlDestroyHeap(HANDLE heap) /* [in] Handle of heap */
1202 {
1203 HEAP *heapPtr = HEAP_GetPtr( heap );
1204 SUBHEAP *subheap;
1205
1206 DPRINT("%p\n", heap );
1207 if (!heapPtr)
1208 return heap;
1209
1210 if (RtlpGetMode() == UserMode)
1211 {
1212 if (heap == NtCurrentPeb()->ProcessHeap)
1213 return heap; /* cannot delete the main process heap */
1214
1215 /* remove it from the per-process list */
1216 RtlEnterHeapLock( &processHeap->critSection );
1217 list_remove( &heapPtr->entry );
1218 RtlLeaveHeapLock( &processHeap->critSection );
1219 }
1220
1221 RtlDeleteHeapLock( &heapPtr->critSection );
1222 subheap = &heapPtr->subheap;
1223 while (subheap)
1224 {
1225 SUBHEAP *next = subheap->next;
1226 SIZE_T size = 0;
1227 void *addr = subheap;
1228 ZwFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
1229 subheap = next;
1230 }
1231 return (HANDLE)NULL;
1232 }
1233
1234
1235 /***********************************************************************
1236 * HeapAlloc (KERNEL32.334)
1237 * RETURNS
1238 * Pointer to allocated memory block
1239 * NULL: Failure
1240 * 0x7d030f60--invalid flags in RtlHeapAllocate
1241 * @implemented
1242 */
1243 PVOID NTAPI
1244 RtlAllocateHeap(HANDLE heap, /* [in] Handle of private heap block */
1245 ULONG flags, /* [in] Heap allocation control flags */
1246 ULONG size) /* [in] Number of bytes to allocate */
1247 {
1248 ARENA_FREE *pArena;
1249 ARENA_INUSE *pInUse;
1250 SUBHEAP *subheap;
1251 HEAP *heapPtr = HEAP_GetPtr( heap );
1252 SIZE_T rounded_size;
1253
1254 /* Validate the parameters */
1255
1256 if (!heapPtr)
1257 {
1258 if (flags & HEAP_GENERATE_EXCEPTIONS)
1259 RtlRaiseStatus( STATUS_NO_MEMORY );
1260 return NULL;
1261 }
1262 //flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
1263 flags |= heapPtr->flags;
1264 rounded_size = ROUND_SIZE(size);
1265 if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
1266
1267 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterHeapLock( &heapPtr->critSection );
1268 /* Locate a suitable free block */
1269
1270 /* Locate a suitable free block */
1271
1272 if (!(pArena = HEAP_FindFreeBlock( heapPtr, rounded_size, &subheap )))
1273 {
1274 TRACE("(%p,%08lx,%08lx): returning NULL\n",
1275 heap, flags, size );
1276 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1277 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1278 return NULL;
1279 }
1280
1281 /* Remove the arena from the free list */
1282
1283 list_remove( &pArena->entry );
1284
1285 /* Build the in-use arena */
1286
1287 pInUse = (ARENA_INUSE *)pArena;
1288
1289 /* in-use arena is smaller than free arena,
1290 * so we have to add the difference to the size */
1291 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1292 pInUse->magic = ARENA_INUSE_MAGIC;
1293 pInUse->has_user_data = 0;
1294
1295 /* Shrink the block */
1296
1297 HEAP_ShrinkBlock( subheap, pInUse, rounded_size );
1298 pInUse->unused_bytes = (pInUse->size & ARENA_SIZE_MASK) - size;
1299
1300 if (flags & HEAP_ZERO_MEMORY)
1301 clear_block( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1302 else
1303 mark_block_uninitialized( pInUse + 1, pInUse->size & ARENA_SIZE_MASK );
1304
1305 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1306
1307 TRACE("(%p,%08lx,%08lx): returning %p\n", heap, flags, size, pInUse + 1 );
1308 return (LPVOID)(pInUse + 1);
1309 }
1310
1311
1312 /***********************************************************************
1313 * HeapFree (KERNEL32.338)
1314 * RETURNS
1315 * TRUE: Success
1316 * FALSE: Failure
1317 *
1318 * @implemented
1319 */
1320 BOOLEAN NTAPI RtlFreeHeap(
1321 HANDLE heap, /* [in] Handle of heap */
1322 ULONG flags, /* [in] Heap freeing flags */
1323 PVOID ptr /* [in] Address of memory to free */
1324 )
1325 {
1326 ARENA_INUSE *pInUse;
1327 SUBHEAP *subheap;
1328 HEAP *heapPtr;
1329
1330 /* Validate the parameters */
1331
1332 if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
1333
1334 heapPtr = HEAP_GetPtr( heap );
1335 if (!heapPtr)
1336 {
1337 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1338 return FALSE;
1339 }
1340
1341 flags &= HEAP_NO_SERIALIZE;
1342 flags |= heapPtr->flags;
1343 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterHeapLock( &heapPtr->critSection );
1344 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1345 {
1346 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1347 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1348 TRACE("(%p,%08lx,%p): returning FALSE\n", heap, flags, ptr );
1349 return FALSE;
1350 }
1351
1352 /* Turn the block into a free block */
1353
1354 pInUse = (ARENA_INUSE *)ptr - 1;
1355 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1356 HEAP_MakeInUseBlockFree( subheap, pInUse );
1357
1358 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1359
1360 TRACE("(%p,%08lx,%p): returning TRUE\n", heap, flags, ptr );
1361 return TRUE;
1362 }
1363
1364
1365 /***********************************************************************
1366 * RtlReAllocateHeap
1367 * PARAMS
1368 * Heap [in] Handle of heap block
1369 * Flags [in] Heap reallocation flags
1370 * Ptr, [in] Address of memory to reallocate
1371 * Size [in] Number of bytes to reallocate
1372 *
1373 * RETURNS
1374 * Pointer to reallocated memory block
1375 * NULL: Failure
1376 * 0x7d030f60--invalid flags in RtlHeapAllocate
1377 * @implemented
1378 */
1379 PVOID NTAPI RtlReAllocateHeap(
1380 HANDLE heap,
1381 ULONG flags,
1382 PVOID ptr,
1383 SIZE_T size
1384 )
1385 {
1386 ARENA_INUSE *pArena;
1387 HEAP *heapPtr;
1388 SUBHEAP *subheap;
1389 SIZE_T oldSize, rounded_size;
1390
1391 if (!ptr) return NULL;
1392 if (!(heapPtr = HEAP_GetPtr( heap )))
1393 {
1394 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1395 return NULL;
1396 }
1397
1398 /* Validate the parameters */
1399
1400 //Flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1401 // HEAP_REALLOC_IN_PLACE_ONLY;
1402 flags |= heapPtr->flags;
1403 rounded_size = ROUND_SIZE(size);
1404 if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
1405
1406 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterHeapLock( &heapPtr->critSection );
1407 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1408 {
1409 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1410 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1411 TRACE("(%p,%08lx,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
1412 return NULL;
1413 }
1414
1415 pArena = (ARENA_INUSE *)ptr - 1;
1416 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1417 oldSize = (pArena->size & ARENA_SIZE_MASK);
1418 if (rounded_size > oldSize)
1419 {
1420 char *pNext = (char *)(pArena + 1) + oldSize;
1421 if ((pNext < (char *)subheap + subheap->size) &&
1422 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1423 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= rounded_size))
1424 {
1425 /* The next block is free and large enough */
1426 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1427 list_remove( &pFree->entry );
1428 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1429 if (!HEAP_Commit( subheap, pArena, rounded_size ))
1430 {
1431 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1432 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1433 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY );
1434 return NULL;
1435 }
1436 HEAP_ShrinkBlock( subheap, pArena, rounded_size );
1437 }
1438 else /* Do it the hard way */
1439 {
1440 ARENA_FREE *pNew;
1441 ARENA_INUSE *pInUse;
1442 SUBHEAP *newsubheap;
1443
1444 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1445 !(pNew = HEAP_FindFreeBlock( heapPtr, rounded_size, &newsubheap )))
1446 {
1447 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1448 if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
1449 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY );
1450 return NULL;
1451 }
1452
1453 /* Build the in-use arena */
1454
1455 list_remove( &pNew->entry );
1456 pInUse = (ARENA_INUSE *)pNew;
1457 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1458 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1459 pInUse->magic = ARENA_INUSE_MAGIC;
1460 HEAP_ShrinkBlock( newsubheap, pInUse, rounded_size );
1461 mark_block_initialized( pInUse + 1, oldSize );
1462 memcpy( pInUse + 1, pArena + 1, oldSize );
1463
1464 /* Free the previous block */
1465
1466 HEAP_MakeInUseBlockFree( subheap, pArena );
1467 subheap = newsubheap;
1468 pArena = pInUse;
1469 }
1470 }
1471 else HEAP_ShrinkBlock( subheap, pArena, rounded_size ); /* Shrink the block */
1472
1473 pArena->unused_bytes = (pArena->size & ARENA_SIZE_MASK) - size;
1474
1475 /* Clear the extra bytes if needed */
1476
1477 if (rounded_size > oldSize)
1478 {
1479 if (flags & HEAP_ZERO_MEMORY)
1480 clear_block( (char *)(pArena + 1) + oldSize,
1481 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1482 else
1483 mark_block_uninitialized( (char *)(pArena + 1) + oldSize,
1484 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1485 }
1486
1487 /* Return the new arena */
1488
1489 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1490
1491 TRACE("(%p,%08lx,%p,%08lx): returning %p\n", heap, flags, ptr, size, pArena + 1 );
1492 return (LPVOID)(pArena + 1);
1493 }
1494
1495
1496 /***********************************************************************
1497 * RtlCompactHeap
1498 *
1499 * @unimplemented
1500 */
1501 ULONG NTAPI
1502 RtlCompactHeap(HANDLE Heap,
1503 ULONG Flags)
1504 {
1505 UNIMPLEMENTED;
1506 return 0;
1507 }
1508
1509
1510 /***********************************************************************
1511 * RtlLockHeap
1512 * Attempts to acquire the critical section object for a specified heap.
1513 *
1514 * PARAMS
1515 * Heap [in] Handle of heap to lock for exclusive access
1516 *
1517 * RETURNS
1518 * TRUE: Success
1519 * FALSE: Failure
1520 *
1521 * @implemented
1522 */
1523 BOOLEAN NTAPI
1524 RtlLockHeap(IN HANDLE Heap)
1525 {
1526 HEAP *heapPtr = HEAP_GetPtr( Heap );
1527 if (!heapPtr)
1528 return FALSE;
1529 RtlEnterHeapLock( &heapPtr->critSection );
1530 return TRUE;
1531 }
1532
1533
1534 /***********************************************************************
1535 * RtlUnlockHeap
1536 * Releases ownership of the critical section object.
1537 *
1538 * PARAMS
1539 * Heap [in] Handle to the heap to unlock
1540 *
1541 * RETURNS
1542 * TRUE: Success
1543 * FALSE: Failure
1544 *
1545 * @implemented
1546 */
1547 BOOLEAN NTAPI
1548 RtlUnlockHeap(HANDLE Heap)
1549 {
1550 HEAP *heapPtr = HEAP_GetPtr( Heap );
1551 if (!heapPtr)
1552 return FALSE;
1553 RtlLeaveHeapLock( &heapPtr->critSection );
1554 return TRUE;
1555 }
1556
1557
1558 /***********************************************************************
1559 * RtlSizeHeap
1560 * PARAMS
1561 * Heap [in] Handle of heap
1562 * Flags [in] Heap size control flags
1563 * Ptr [in] Address of memory to return size for
1564 *
1565 * RETURNS
1566 * Size in bytes of allocated memory
1567 * 0xffffffff: Failure
1568 *
1569 * @implemented
1570 */
1571 ULONG NTAPI
1572 RtlSizeHeap(
1573 HANDLE heap,
1574 ULONG flags,
1575 PVOID ptr
1576 )
1577 {
1578 SIZE_T ret;
1579 HEAP *heapPtr = HEAP_GetPtr( heap );
1580
1581 if (!heapPtr)
1582 {
1583 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1584 return ~0UL;
1585 }
1586 flags &= HEAP_NO_SERIALIZE;
1587 flags |= heapPtr->flags;
1588 if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterHeapLock( &heapPtr->critSection );
1589 if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
1590 {
1591 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1592 ret = ~0UL;
1593 }
1594 else
1595 {
1596 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1597 ret = (pArena->size & ARENA_SIZE_MASK) - pArena->unused_bytes;
1598 }
1599 if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveHeapLock( &heapPtr->critSection );
1600
1601 TRACE("(%p,%08lx,%p): returning %08lx\n", heap, flags, ptr, ret );
1602 return ret;
1603 }
1604
1605
1606 /***********************************************************************
1607 * RtlValidateHeap
1608 * Validates a specified heap.
1609 *
1610 * PARAMS
1611 * Heap [in] Handle to the heap
1612 * Flags [in] Bit flags that control access during operation
1613 * Block [in] Optional pointer to memory block to validate
1614 *
1615 * NOTES
1616 * Flags is ignored.
1617 *
1618 * RETURNS
1619 * TRUE: Success
1620 * FALSE: Failure
1621 *
1622 * @implemented
1623 */
1624 BOOLEAN NTAPI RtlValidateHeap(
1625 HANDLE Heap,
1626 ULONG Flags,
1627 PVOID Block
1628 )
1629 {
1630 HEAP *heapPtr = HEAP_GetPtr( Heap );
1631 if (!heapPtr)
1632 return FALSE;
1633 return HEAP_IsRealArena( heapPtr, Flags, Block, QUIET );
1634 }
1635
1636 VOID
1637 RtlInitializeHeapManager(VOID)
1638 {
1639 PPEB Peb;
1640
1641 Peb = NtCurrentPeb();
1642
1643 Peb->NumberOfHeaps = 0;
1644 Peb->MaximumNumberOfHeaps = -1; /* no limit */
1645 Peb->ProcessHeaps = NULL;
1646
1647 //RtlInitializeHeapLock(&RtlpProcessHeapsListLock);
1648 }
1649
1650
1651 /*
1652 * @implemented
1653 */
1654 NTSTATUS NTAPI
1655 RtlEnumProcessHeaps(PHEAP_ENUMERATION_ROUTINE HeapEnumerationRoutine,
1656 PVOID lParam)
1657 {
1658 NTSTATUS Status = STATUS_SUCCESS;
1659
1660 struct list *ptr=NULL;
1661 RtlEnterHeapLock(&processHeap->critSection);
1662 Status=HeapEnumerationRoutine(processHeap,lParam);
1663
1664 LIST_FOR_EACH( ptr, &processHeap->entry )
1665 {
1666 if (!NT_SUCCESS(Status)) break;
1667 Status = HeapEnumerationRoutine(ptr,lParam);
1668 }
1669
1670 RtlLeaveHeapLock(&processHeap->critSection);
1671
1672 return Status;
1673 }
1674
1675
1676 /*
1677 * @implemented
1678 */
1679 ULONG NTAPI
1680 RtlGetProcessHeaps(ULONG count,
1681 HANDLE *heaps )
1682 {
1683 ULONG total = 1; /* main heap */
1684 struct list *ptr;
1685 ULONG i=0;
1686 RtlEnterHeapLock( &processHeap->critSection );
1687 LIST_FOR_EACH( ptr, &processHeap->entry ) total++;
1688 //if (total <= count)
1689 {
1690 *(heaps++) = processHeap;
1691 i++;
1692 LIST_FOR_EACH( ptr, &processHeap->entry )
1693 {
1694 if (i >= count) break;
1695 i++;
1696 *(heaps++) = LIST_ENTRY( ptr, HEAP, entry );
1697 }
1698 }
1699 RtlLeaveHeapLock( &processHeap->critSection );
1700 return i;
1701 }
1702
1703
1704 /*
1705 * @implemented
1706 */
1707 BOOLEAN NTAPI
1708 RtlValidateProcessHeaps(VOID)
1709 {
1710 BOOLEAN Result = TRUE;
1711 HEAP ** pptr;
1712
1713 RtlEnterHeapLock( &processHeap->critSection );
1714
1715 for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr++)
1716 {
1717 if (!RtlValidateHeap(*pptr, 0, NULL))
1718 {
1719 Result = FALSE;
1720 break;
1721 }
1722 }
1723
1724 RtlLeaveHeapLock( &processHeap->critSection );
1725 return Result;
1726 }
1727
1728
1729 /*
1730 * @unimplemented
1731 */
1732 BOOLEAN NTAPI
1733 RtlZeroHeap(
1734 IN PVOID HeapHandle,
1735 IN ULONG Flags
1736 )
1737 {
1738 UNIMPLEMENTED;
1739 return FALSE;
1740 }
1741
1742 /*
1743 * @implemented
1744 */
1745 BOOLEAN
1746 NTAPI
1747 RtlSetUserValueHeap(IN PVOID HeapHandle,
1748 IN ULONG Flags,
1749 IN PVOID BaseAddress,
1750 IN PVOID UserValue)
1751 {
1752 HEAP *heapPtr;
1753 PHEAP_USER_DATA udata;
1754
1755 heapPtr = HEAP_GetPtr(HeapHandle);
1756 if (!heapPtr)
1757 {
1758 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1759 return FALSE;
1760 }
1761 udata = HEAP_GetUserData(heapPtr, BaseAddress);
1762 if (!udata)
1763 {
1764 udata = HEAP_AllocUserData(heapPtr, BaseAddress);
1765 if (!udata) return FALSE;
1766 }
1767 udata->UserValue = UserValue;
1768 return TRUE;
1769 }
1770
1771 /*
1772 * @implemented
1773 */
1774 BOOLEAN
1775 NTAPI
1776 RtlSetUserFlagsHeap(IN PVOID HeapHandle,
1777 IN ULONG Flags,
1778 IN PVOID BaseAddress,
1779 IN ULONG UserFlags)
1780 {
1781 HEAP *heapPtr;
1782 PHEAP_USER_DATA udata;
1783
1784 heapPtr = HEAP_GetPtr(HeapHandle);
1785 if (!heapPtr)
1786 {
1787 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1788 return FALSE;
1789 }
1790 udata = HEAP_GetUserData(heapPtr, BaseAddress);
1791 if (!udata)
1792 {
1793 udata = HEAP_AllocUserData(heapPtr, BaseAddress);
1794 if (!udata) return FALSE;
1795 }
1796 udata->UserFlags = UserFlags & HEAP_SETTABLE_USER_FLAGS;
1797 return TRUE;
1798 }
1799
1800 /*
1801 * @implemented
1802 */
1803 BOOLEAN
1804 NTAPI
1805 RtlGetUserInfoHeap(IN PVOID HeapHandle,
1806 IN ULONG Flags,
1807 IN PVOID BaseAddress,
1808 OUT PVOID *UserValue,
1809 OUT PULONG UserFlags)
1810 {
1811 HEAP *heapPtr;
1812 PHEAP_USER_DATA udata;
1813
1814 heapPtr = HEAP_GetPtr(HeapHandle);
1815 if (!heapPtr)
1816 {
1817 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
1818 return FALSE;
1819 }
1820 udata = HEAP_GetUserData(heapPtr, BaseAddress);
1821 if (!udata)
1822 {
1823 RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
1824 return FALSE;
1825 }
1826 if (UserValue) *UserValue = udata->UserValue;
1827 if (UserFlags) *UserFlags = udata->UserFlags;
1828 return TRUE;
1829 }
1830
1831 /*
1832 * @unimplemented
1833 */
1834 NTSTATUS
1835 NTAPI
1836 RtlUsageHeap(IN HANDLE Heap,
1837 IN ULONG Flags,
1838 OUT PRTL_HEAP_USAGE Usage)
1839 {
1840 /* TODO */
1841 UNIMPLEMENTED;
1842 return STATUS_NOT_IMPLEMENTED;
1843 }
1844
1845 PWSTR
1846 NTAPI
1847 RtlQueryTagHeap(IN PVOID HeapHandle,
1848 IN ULONG Flags,
1849 IN USHORT TagIndex,
1850 IN BOOLEAN ResetCounters,
1851 OUT PRTL_HEAP_TAG_INFO HeapTagInfo)
1852 {
1853 /* TODO */
1854 UNIMPLEMENTED;
1855 return NULL;
1856 }
1857
1858 ULONG
1859 NTAPI
1860 RtlExtendHeap(IN HANDLE Heap,
1861 IN ULONG Flags,
1862 IN PVOID P,
1863 IN ULONG Size)
1864 {
1865 /* TODO */
1866 UNIMPLEMENTED;
1867 return 0;
1868 }
1869
1870 ULONG
1871 NTAPI
1872 RtlCreateTagHeap(IN HANDLE HeapHandle,
1873 IN ULONG Flags,
1874 IN PWSTR TagName,
1875 IN PWSTR TagSubName)
1876 {
1877 /* TODO */
1878 UNIMPLEMENTED;
1879 return 0;
1880 }
1881
1882 NTSTATUS
1883 NTAPI
1884 RtlWalkHeap(IN HANDLE HeapHandle,
1885 IN PVOID HeapEntry)
1886 {
1887 UNIMPLEMENTED;
1888 return STATUS_NOT_IMPLEMENTED;
1889 }
1890
1891 PVOID
1892 NTAPI
1893 RtlProtectHeap(IN PVOID HeapHandle,
1894 IN BOOLEAN ReadOnly)
1895 {
1896 UNIMPLEMENTED;
1897 return NULL;
1898 }
1899
1900 DWORD
1901 NTAPI
1902 RtlSetHeapInformation(IN HANDLE HeapHandle OPTIONAL,
1903 IN HEAP_INFORMATION_CLASS HeapInformationClass,
1904 IN PVOID HeapInformation,
1905 IN SIZE_T HeapInformationLength)
1906 {
1907 UNIMPLEMENTED;
1908 return 0;
1909 }
1910
1911 DWORD
1912 NTAPI
1913 RtlQueryHeapInformation(HANDLE HeapHandle,
1914 HEAP_INFORMATION_CLASS HeapInformationClass,
1915 PVOID HeapInformation OPTIONAL,
1916 SIZE_T HeapInformationLength OPTIONAL,
1917 PSIZE_T ReturnLength OPTIONAL)
1918 {
1919 UNIMPLEMENTED;
1920 return 0;
1921 }
1922
1923 DWORD
1924 NTAPI
1925 RtlMultipleAllocateHeap(IN PVOID HeapHandle,
1926 IN DWORD Flags,
1927 IN SIZE_T Size,
1928 IN DWORD Count,
1929 OUT PVOID *Array)
1930 {
1931 UNIMPLEMENTED;
1932 return 0;
1933 }
1934
1935 DWORD
1936 NTAPI
1937 RtlMultipleFreeHeap(IN PVOID HeapHandle,
1938 IN DWORD Flags,
1939 IN DWORD Count,
1940 OUT PVOID *Array)
1941 {
1942 UNIMPLEMENTED;
1943 return 0;
1944 }