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