[CMAKE]
[reactos.git] / lib / rtl / heap.c
1 /* COPYRIGHT: See COPYING in the top level directory
2 * PROJECT: ReactOS system libraries
3 * FILE: lib/rtl/heap.c
4 * PURPOSE: RTL Heap backend allocator
5 * PROGRAMMERS: Copyright 2010 Aleksey Bragin
6 */
7
8 /* Useful references:
9 http://msdn.microsoft.com/en-us/library/ms810466.aspx
10 http://msdn.microsoft.com/en-us/library/ms810603.aspx
11 http://www.securitylab.ru/analytics/216376.php
12 http://binglongx.spaces.live.com/blog/cns!142CBF6D49079DE8!596.entry
13 http://www.phreedom.org/research/exploits/asn1-bitstring/
14 http://illmatics.com/Understanding_the_LFH.pdf
15 http://www.alex-ionescu.com/?p=18
16 */
17
18 /* INCLUDES *****************************************************************/
19
20 #include <rtl.h>
21 #include <heap.h>
22
23 #define NDEBUG
24 #include <debug.h>
25
26 HEAP_LOCK RtlpProcessHeapsListLock;
27
28 /* Bitmaps stuff */
29
30 /* How many least significant bits are clear */
31 UCHAR RtlpBitsClearLow[] =
32 {
33 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
34 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
35 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
36 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
37 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
38 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
39 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
40 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
41 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
42 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
43 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
44 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
45 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
46 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
47 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
48 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
49 };
50
51 UCHAR FORCEINLINE
52 RtlpFindLeastSetBit(ULONG Bits)
53 {
54 if (Bits & 0xFFFF)
55 {
56 if (Bits & 0xFF)
57 return RtlpBitsClearLow[Bits & 0xFF]; /* Lowest byte */
58 else
59 return RtlpBitsClearLow[(Bits >> 8) & 0xFF] + 8; /* 2nd byte */
60 }
61 else
62 {
63 if ((Bits >> 16) & 0xFF)
64 return RtlpBitsClearLow[(Bits >> 16) & 0xFF] + 16; /* 3rd byte */
65 else
66 return RtlpBitsClearLow[(Bits >> 24) & 0xFF] + 24; /* Highest byte */
67 }
68 }
69
70 /* Maximum size of a tail-filling pattern used for compare operation */
71 UCHAR FillPattern[HEAP_ENTRY_SIZE] =
72 {
73 HEAP_TAIL_FILL,
74 HEAP_TAIL_FILL,
75 HEAP_TAIL_FILL,
76 HEAP_TAIL_FILL,
77 HEAP_TAIL_FILL,
78 HEAP_TAIL_FILL,
79 HEAP_TAIL_FILL,
80 HEAP_TAIL_FILL
81 };
82
83
84 ULONG NTAPI
85 RtlCompareMemoryUlong(PVOID Source, ULONG Length, ULONG Value);
86
87 /* FUNCTIONS *****************************************************************/
88
89 VOID NTAPI
90 RtlpInitializeHeap(PHEAP Heap,
91 PULONG HeaderSize,
92 ULONG Flags,
93 BOOLEAN AllocateLock,
94 PVOID Lock)
95 {
96 PVOID NextHeapBase = Heap + 1;
97 PHEAP_UCR_DESCRIPTOR UcrDescriptor;
98 ULONG NumUCRs = 8;
99 ULONG i;
100 NTSTATUS Status;
101
102 /* Add UCRs size */
103 *HeaderSize += NumUCRs * sizeof(*UcrDescriptor);
104
105 /* Prepare a list of UCRs */
106 InitializeListHead(&Heap->UCRList);
107 InitializeListHead(&Heap->UCRSegments);
108 UcrDescriptor = NextHeapBase;
109
110 for (i=0; i<NumUCRs; i++, UcrDescriptor++)
111 {
112 InsertTailList(&Heap->UCRList, &UcrDescriptor->ListEntry);
113 }
114
115 NextHeapBase = UcrDescriptor;
116 // TODO: Add tagging
117
118 /* Round up header size again */
119 *HeaderSize = ROUND_UP(*HeaderSize, HEAP_ENTRY_SIZE);
120
121 ASSERT(*HeaderSize <= PAGE_SIZE);
122
123 /* Initialize heap's header */
124 Heap->Entry.Size = (*HeaderSize) >> HEAP_ENTRY_SHIFT;
125 Heap->Entry.Flags = HEAP_ENTRY_BUSY;
126
127 Heap->Signature = HEAP_SIGNATURE;
128 Heap->Flags = Flags;
129 Heap->ForceFlags = (Flags & (HEAP_NO_SERIALIZE |
130 HEAP_GENERATE_EXCEPTIONS |
131 HEAP_ZERO_MEMORY |
132 HEAP_REALLOC_IN_PLACE_ONLY |
133 HEAP_VALIDATE_PARAMETERS_ENABLED |
134 HEAP_VALIDATE_ALL_ENABLED |
135 HEAP_TAIL_CHECKING_ENABLED |
136 HEAP_CREATE_ALIGN_16 |
137 HEAP_FREE_CHECKING_ENABLED));
138 Heap->HeaderValidateCopy = NULL;
139 Heap->HeaderValidateLength = ((PCHAR)NextHeapBase - (PCHAR)Heap);
140
141 /* Initialize free lists */
142 for (i=0; i<HEAP_FREELISTS; i++)
143 {
144 InitializeListHead(&Heap->FreeLists[i]);
145 }
146
147 /* Initialize "big" allocations list */
148 InitializeListHead(&Heap->VirtualAllocdBlocks);
149
150 /* Initialize lock */
151 if (AllocateLock)
152 {
153 Lock = NextHeapBase;
154 Status = RtlInitializeHeapLock((PHEAP_LOCK)Lock);
155 if (!NT_SUCCESS(Status))
156 {
157 DPRINT1("Initializing the lock failed!\n");
158 return /*NULL*/; // FIXME!
159 }
160 }
161
162 /* Set the lock variable */
163 Heap->LockVariable = Lock;
164 }
165
166 VOID FORCEINLINE
167 RtlpSetFreeListsBit(PHEAP Heap,
168 PHEAP_FREE_ENTRY FreeEntry)
169 {
170 ULONG Index, Bit;
171
172 ASSERT(FreeEntry->Size < HEAP_FREELISTS);
173
174 /* Calculate offset in the free list bitmap */
175 Index = FreeEntry->Size >> 3; /* = FreeEntry->Size / (sizeof(UCHAR) * 8)*/
176 Bit = 1 << (FreeEntry->Size & 7);
177
178 /* Assure it's not already set */
179 ASSERT((Heap->u.FreeListsInUseBytes[Index] & Bit) == 0);
180
181 /* Set it */
182 Heap->u.FreeListsInUseBytes[Index] |= Bit;
183 }
184
185 VOID FORCEINLINE
186 RtlpClearFreeListsBit(PHEAP Heap,
187 PHEAP_FREE_ENTRY FreeEntry)
188 {
189 ULONG Index, Bit;
190
191 ASSERT(FreeEntry->Size < HEAP_FREELISTS);
192
193 /* Calculate offset in the free list bitmap */
194 Index = FreeEntry->Size >> 3; /* = FreeEntry->Size / (sizeof(UCHAR) * 8)*/
195 Bit = 1 << (FreeEntry->Size & 7);
196
197 /* Assure it was set and the corresponding free list is empty */
198 ASSERT(Heap->u.FreeListsInUseBytes[Index] & Bit);
199 ASSERT(IsListEmpty(&Heap->FreeLists[FreeEntry->Size]));
200
201 /* Clear it */
202 Heap->u.FreeListsInUseBytes[Index] ^= Bit;
203 }
204
205 VOID NTAPI
206 RtlpInsertFreeBlockHelper(PHEAP Heap,
207 PHEAP_FREE_ENTRY FreeEntry,
208 SIZE_T BlockSize,
209 BOOLEAN NoFill)
210 {
211 PLIST_ENTRY FreeListHead, Current;
212 PHEAP_FREE_ENTRY CurrentEntry;
213
214 ASSERT(FreeEntry->Size == BlockSize);
215
216 /* Fill if it's not denied */
217 if (!NoFill)
218 {
219 FreeEntry->Flags &= ~(HEAP_ENTRY_FILL_PATTERN |
220 HEAP_ENTRY_EXTRA_PRESENT |
221 HEAP_ENTRY_BUSY);
222
223 if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
224 {
225 RtlFillMemoryUlong((PCHAR)(FreeEntry + 1),
226 (BlockSize << HEAP_ENTRY_SHIFT) - sizeof(*FreeEntry),
227 ARENA_FREE_FILLER);
228
229 FreeEntry->Flags |= HEAP_ENTRY_FILL_PATTERN;
230 }
231 }
232 else
233 {
234 /* Clear out all flags except the last entry one */
235 FreeEntry->Flags &= HEAP_ENTRY_LAST_ENTRY;
236 }
237
238 /* Check if PreviousSize of the next entry matches ours */
239 if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
240 {
241 ASSERT(((PHEAP_ENTRY)FreeEntry + BlockSize)->PreviousSize = BlockSize);
242 }
243
244 /* Insert it either into dedicated or non-dedicated list */
245 if (BlockSize < HEAP_FREELISTS)
246 {
247 /* Dedicated list */
248 FreeListHead = &Heap->FreeLists[BlockSize];
249
250 if (IsListEmpty(FreeListHead))
251 {
252 RtlpSetFreeListsBit(Heap, FreeEntry);
253 }
254 }
255 else
256 {
257 /* Non-dedicated one */
258 FreeListHead = &Heap->FreeLists[0];
259 Current = FreeListHead->Flink;
260
261 /* Find a position where to insert it to (the list must be sorted) */
262 while (FreeListHead != Current)
263 {
264 CurrentEntry = CONTAINING_RECORD(Current, HEAP_FREE_ENTRY, FreeList);
265
266 if (BlockSize <= CurrentEntry->Size)
267 break;
268
269 Current = Current->Flink;
270 }
271
272 FreeListHead = Current;
273 }
274
275 /* Actually insert it into the list */
276 InsertTailList(FreeListHead, &FreeEntry->FreeList);
277 }
278
279 VOID NTAPI
280 RtlpInsertFreeBlock(PHEAP Heap,
281 PHEAP_FREE_ENTRY FreeEntry,
282 SIZE_T BlockSize)
283 {
284 USHORT Size, PreviousSize;
285 UCHAR SegmentOffset, Flags;
286 PHEAP_SEGMENT Segment;
287
288 DPRINT("RtlpInsertFreeBlock(%p %p %x)\n", Heap, FreeEntry, BlockSize);
289
290 /* Increase the free size counter */
291 Heap->TotalFreeSize += BlockSize;
292
293 /* Remember certain values */
294 Flags = FreeEntry->Flags;
295 PreviousSize = FreeEntry->PreviousSize;
296 SegmentOffset = FreeEntry->SegmentOffset;
297 Segment = Heap->Segments[SegmentOffset];
298
299 /* Process it */
300 while (BlockSize)
301 {
302 /* Check for the max size */
303 if (BlockSize > HEAP_MAX_BLOCK_SIZE)
304 {
305 Size = HEAP_MAX_BLOCK_SIZE;
306
307 /* Special compensation if it goes above limit just by 1 */
308 if (BlockSize == (HEAP_MAX_BLOCK_SIZE + 1))
309 Size -= 16;
310
311 FreeEntry->Flags = 0;
312 }
313 else
314 {
315 Size = BlockSize;
316 FreeEntry->Flags = Flags;
317 }
318
319 /* Change its size and insert it into a free list */
320 FreeEntry->Size = Size;
321 FreeEntry->PreviousSize = PreviousSize;
322 FreeEntry->SegmentOffset = SegmentOffset;
323
324 /* Call a helper to actually insert the block */
325 RtlpInsertFreeBlockHelper(Heap, FreeEntry, Size, FALSE);
326
327 /* Update sizes */
328 PreviousSize = Size;
329 BlockSize -= Size;
330
331 /* Go to the next entry */
332 FreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + Size);
333
334 /* Check if that's all */
335 if ((PHEAP_ENTRY)FreeEntry >= Segment->LastValidEntry) return;
336 }
337
338 /* Update previous size if needed */
339 if (!(Flags & HEAP_ENTRY_LAST_ENTRY))
340 FreeEntry->PreviousSize = PreviousSize;
341 }
342
343 VOID NTAPI
344 RtlpRemoveFreeBlock(PHEAP Heap,
345 PHEAP_FREE_ENTRY FreeEntry,
346 BOOLEAN Dedicated,
347 BOOLEAN NoFill)
348 {
349 SIZE_T Result, RealSize;
350 PLIST_ENTRY OldBlink, OldFlink;
351
352 // FIXME: Maybe use RemoveEntryList?
353
354 /* Remove the free block */
355 OldFlink = FreeEntry->FreeList.Flink;
356 OldBlink = FreeEntry->FreeList.Blink;
357 OldBlink->Flink = OldFlink;
358 OldFlink->Blink = OldBlink;
359
360 /* Update the freelists bitmap */
361 if ((OldFlink == OldBlink) &&
362 (Dedicated || (!Dedicated && FreeEntry->Size < HEAP_FREELISTS)))
363 {
364 RtlpClearFreeListsBit(Heap, FreeEntry);
365 }
366
367 /* Fill with pattern if necessary */
368 if (!NoFill &&
369 (FreeEntry->Flags & HEAP_ENTRY_FILL_PATTERN))
370 {
371 RealSize = (FreeEntry->Size << HEAP_ENTRY_SHIFT) - sizeof(*FreeEntry);
372
373 /* Deduct extra stuff from block's real size */
374 if (FreeEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT &&
375 RealSize > sizeof(HEAP_FREE_ENTRY_EXTRA))
376 {
377 RealSize -= sizeof(HEAP_FREE_ENTRY_EXTRA);
378 }
379
380 /* Check if the free filler is intact */
381 Result = RtlCompareMemoryUlong((PCHAR)(FreeEntry + 1),
382 RealSize,
383 ARENA_FREE_FILLER);
384
385 if (Result != RealSize)
386 {
387 DPRINT1("Free heap block %p modified at %p after it was freed\n",
388 FreeEntry,
389 (PCHAR)(FreeEntry + 1) + Result);
390 }
391 }
392 }
393
394 SIZE_T NTAPI
395 RtlpGetSizeOfBigBlock(PHEAP_ENTRY HeapEntry)
396 {
397 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualEntry;
398
399 /* Get pointer to the containing record */
400 VirtualEntry = CONTAINING_RECORD(HeapEntry, HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
401
402 /* Restore the real size */
403 return VirtualEntry->CommitSize - HeapEntry->Size;
404 }
405
406 PHEAP_UCR_DESCRIPTOR NTAPI
407 RtlpCreateUnCommittedRange(PHEAP_SEGMENT Segment)
408 {
409 PLIST_ENTRY Entry;
410 PHEAP_UCR_DESCRIPTOR UcrDescriptor;
411 PHEAP_UCR_SEGMENT UcrSegment;
412 PHEAP Heap = Segment->Heap;
413 SIZE_T ReserveSize = 16 * PAGE_SIZE;
414 SIZE_T CommitSize = 1 * PAGE_SIZE;
415 NTSTATUS Status;
416
417 DPRINT("RtlpCreateUnCommittedRange(%p)\n", Segment);
418
419 /* Check if we have unused UCRs */
420 if (IsListEmpty(&Heap->UCRList))
421 {
422 /* Get a pointer to the first UCR segment */
423 UcrSegment = CONTAINING_RECORD(&Heap->UCRSegments.Flink, HEAP_UCR_SEGMENT, ListEntry);
424
425 /* Check the list of UCR segments */
426 if (IsListEmpty(&Heap->UCRSegments) ||
427 UcrSegment->ReservedSize == UcrSegment->CommittedSize)
428 {
429 /* We need to create a new one. Reserve 16 pages for it */
430 UcrSegment = NULL;
431 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
432 (PVOID *)&UcrSegment,
433 0,
434 &ReserveSize,
435 MEM_RESERVE,
436 PAGE_READWRITE);
437
438 if (!NT_SUCCESS(Status)) return NULL;
439
440 /* Commit one page */
441 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
442 (PVOID *)&UcrSegment,
443 0,
444 &CommitSize,
445 MEM_COMMIT,
446 PAGE_READWRITE);
447
448 if (!NT_SUCCESS(Status))
449 {
450 /* Release reserved memory */
451 ZwFreeVirtualMemory(NtCurrentProcess(),
452 (PVOID *)&UcrDescriptor,
453 &ReserveSize,
454 MEM_RELEASE);
455 return NULL;
456 }
457
458 /* Set it's data */
459 UcrSegment->ReservedSize = ReserveSize;
460 UcrSegment->CommittedSize = CommitSize;
461
462 /* Add it to the head of the list */
463 InsertHeadList(&Heap->UCRSegments, &UcrSegment->ListEntry);
464
465 /* Get a pointer to the first available UCR descriptor */
466 UcrDescriptor = (PHEAP_UCR_DESCRIPTOR)(UcrSegment + 1);
467 }
468 else
469 {
470 /* It's possible to use existing UCR segment. Commit one more page */
471 UcrDescriptor = (PHEAP_UCR_DESCRIPTOR)((PCHAR)UcrSegment + UcrSegment->CommittedSize);
472 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
473 (PVOID *)&UcrDescriptor,
474 0,
475 &CommitSize,
476 MEM_COMMIT,
477 PAGE_READWRITE);
478
479 if (!NT_SUCCESS(Status)) return NULL;
480
481 /* Update sizes */
482 UcrSegment->CommittedSize += CommitSize;
483 }
484
485 /* There is a whole bunch of new UCR descriptors. Put them into the unused list */
486 while ((PCHAR)UcrDescriptor < ((PCHAR)UcrSegment + UcrSegment->CommittedSize))
487 {
488 InsertTailList(&Heap->UCRList, &UcrDescriptor->ListEntry);
489 UcrDescriptor++;
490 }
491 }
492
493 /* There are unused UCRs, just get the first one */
494 Entry = RemoveHeadList(&Heap->UCRList);
495 UcrDescriptor = CONTAINING_RECORD(Entry, HEAP_UCR_DESCRIPTOR, ListEntry);
496 return UcrDescriptor;
497 }
498
499 VOID NTAPI
500 RtlpDestroyUnCommittedRange(PHEAP_SEGMENT Segment,
501 PHEAP_UCR_DESCRIPTOR UcrDescriptor)
502 {
503 /* Zero it out */
504 UcrDescriptor->Address = NULL;
505 UcrDescriptor->Size = 0;
506
507 /* Put it into the heap's list of unused UCRs */
508 InsertHeadList(&Segment->Heap->UCRList, &UcrDescriptor->ListEntry);
509 }
510
511 VOID NTAPI
512 RtlpInsertUnCommittedPages(PHEAP_SEGMENT Segment,
513 ULONG_PTR Address,
514 SIZE_T Size)
515 {
516 PLIST_ENTRY Current;
517 PHEAP_UCR_DESCRIPTOR UcrDescriptor;
518
519 DPRINT("RtlpInsertUnCommittedPages(%p %p %x)\n", Segment, Address, Size);
520
521 /* Go through the list of UCR descriptors, they are sorted from lowest address
522 to the highest */
523 Current = Segment->UCRSegmentList.Flink;
524 while(Current != &Segment->UCRSegmentList)
525 {
526 UcrDescriptor = CONTAINING_RECORD(Current, HEAP_UCR_DESCRIPTOR, SegmentEntry);
527
528 if ((ULONG_PTR)UcrDescriptor->Address > Address)
529 {
530 /* Check for a really lucky case */
531 if ((Address + Size) == (ULONG_PTR)UcrDescriptor->Address)
532 {
533 /* Exact match */
534 UcrDescriptor->Address = (PVOID)Address;
535 UcrDescriptor->Size += Size;
536 return;
537 }
538
539 /* We found the block after which the new one should go */
540 break;
541 }
542 else if (((ULONG_PTR)UcrDescriptor->Address + UcrDescriptor->Size) == Address)
543 {
544 /* Modify this entry */
545 Address = (ULONG_PTR)UcrDescriptor->Address;
546 Size += UcrDescriptor->Size;
547
548 /* Remove it from the list and destroy it */
549 RemoveEntryList(Current);
550 RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
551
552 Segment->NumberOfUnCommittedRanges--;
553 }
554 else
555 {
556 /* Advance to the next descriptor */
557 Current = Current->Flink;
558 }
559 }
560
561 /* Create a new UCR descriptor */
562 UcrDescriptor = RtlpCreateUnCommittedRange(Segment);
563 if (!UcrDescriptor) return;
564
565 UcrDescriptor->Address = (PVOID)Address;
566 UcrDescriptor->Size = Size;
567
568 /* "Current" is the descriptor after which our one should go */
569 InsertTailList(Current, &UcrDescriptor->SegmentEntry);
570
571 DPRINT("Added segment UCR with base %p, size 0x%x\n", Address, Size);
572
573 /* Increase counters */
574 Segment->NumberOfUnCommittedRanges++;
575 }
576
577 PHEAP_FREE_ENTRY NTAPI
578 RtlpFindAndCommitPages(PHEAP Heap,
579 PHEAP_SEGMENT Segment,
580 PSIZE_T Size,
581 PVOID AddressRequested)
582 {
583 PLIST_ENTRY Current;
584 ULONG_PTR Address = 0;
585 PHEAP_UCR_DESCRIPTOR UcrDescriptor, PreviousUcr = NULL;
586 PHEAP_ENTRY FirstEntry, LastEntry, PreviousLastEntry;
587 NTSTATUS Status;
588
589 DPRINT("RtlpFindAndCommitPages(%p %p %x %p)\n", Heap, Segment, *Size, Address);
590
591 /* Go through UCRs in a segment */
592 Current = Segment->UCRSegmentList.Flink;
593 while(Current != &Segment->UCRSegmentList)
594 {
595 UcrDescriptor = CONTAINING_RECORD(Current, HEAP_UCR_DESCRIPTOR, SegmentEntry);
596
597 /* Check if we can use that one right away */
598 if (UcrDescriptor->Size >= *Size &&
599 (UcrDescriptor->Address == AddressRequested || !AddressRequested))
600 {
601 /* Get the address */
602 Address = (ULONG_PTR)UcrDescriptor->Address;
603
604 /* Commit it */
605 if (Heap->CommitRoutine)
606 {
607 Status = Heap->CommitRoutine(Heap, (PVOID *)&Address, Size);
608 }
609 else
610 {
611 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
612 (PVOID *)&Address,
613 0,
614 Size,
615 MEM_COMMIT,
616 PAGE_READWRITE);
617 }
618
619 DPRINT("Committed %d bytes at base %p, UCR size is %d\n", *Size, Address, UcrDescriptor->Size);
620
621 /* Fail in unsuccessful case */
622 if (!NT_SUCCESS(Status))
623 {
624 DPRINT1("Committing page failed with status 0x%08X\n", Status);
625 return NULL;
626 }
627
628 /* Update tracking numbers */
629 Segment->NumberOfUnCommittedPages -= *Size / PAGE_SIZE;
630
631 /* Calculate first and last entries */
632 FirstEntry = (PHEAP_ENTRY)Address;
633
634 if ((Segment->LastEntryInSegment->Flags & HEAP_ENTRY_LAST_ENTRY) &&
635 (ULONG_PTR)(Segment->LastEntryInSegment + Segment->LastEntryInSegment->Size) == (ULONG_PTR)UcrDescriptor->Address)
636 {
637 LastEntry = Segment->LastEntryInSegment;
638 }
639 else
640 {
641 /* Go through the entries to find the last one */
642
643 if (PreviousUcr)
644 LastEntry = (PHEAP_ENTRY)((ULONG_PTR)PreviousUcr->Address + PreviousUcr->Size);
645 else
646 LastEntry = Segment->FirstEntry;
647
648 while (!(LastEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
649 {
650 PreviousLastEntry = LastEntry;
651 LastEntry += LastEntry->Size;
652
653 if ((ULONG_PTR)LastEntry >= (ULONG_PTR)Segment->LastValidEntry ||
654 LastEntry->Size == 0)
655 {
656 if (LastEntry == (PHEAP_ENTRY)Address)
657 {
658 /* Found it */
659 LastEntry = PreviousLastEntry;
660 break;
661 }
662
663 DPRINT1("Last entry not found in a committed range near to %p\n", PreviousLastEntry);
664 return NULL;
665 }
666 }
667 }
668
669 /* Unmark it as a last entry */
670 LastEntry->Flags &= ~HEAP_ENTRY_LAST_ENTRY;
671
672 /* Update UCR descriptor */
673 UcrDescriptor->Address = (PVOID)((ULONG_PTR)UcrDescriptor->Address + *Size);
674 UcrDescriptor->Size -= *Size;
675
676 DPRINT("Updating UcrDescriptor %p, new Address %p, size %d\n",
677 UcrDescriptor, UcrDescriptor->Address, UcrDescriptor->Size);
678
679 /* Check if anything left in this UCR */
680 if (UcrDescriptor->Size == 0)
681 {
682 /* It's fully exhausted */
683 if (UcrDescriptor->Address == Segment->LastValidEntry)
684 {
685 FirstEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
686 Segment->LastEntryInSegment = FirstEntry;
687 }
688 else
689 {
690 FirstEntry->Flags = 0;
691 Segment->LastEntryInSegment = Segment->FirstEntry;
692 }
693
694 /* This UCR needs to be removed because it became useless */
695 RemoveEntryList(&UcrDescriptor->SegmentEntry);
696
697 RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
698 Segment->NumberOfUnCommittedRanges--;
699 }
700 else
701 {
702 FirstEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
703 Segment->LastEntryInSegment = FirstEntry;
704 }
705
706 /* Set various first entry fields*/
707 FirstEntry->SegmentOffset = LastEntry->SegmentOffset;
708 FirstEntry->Size = *Size >> HEAP_ENTRY_SHIFT;
709 FirstEntry->PreviousSize = LastEntry->Size;
710
711 /* Update previous size */
712 if (!(FirstEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
713 (FirstEntry + FirstEntry->Size)->PreviousSize = FirstEntry->Size;
714
715 /* We're done */
716 return (PHEAP_FREE_ENTRY)FirstEntry;
717 }
718
719 /* Advance to the next descriptor */
720 PreviousUcr = UcrDescriptor;
721 Current = Current->Flink;
722 }
723
724 return NULL;
725 }
726
727 VOID NTAPI
728 RtlpDeCommitFreeBlock(PHEAP Heap,
729 PHEAP_FREE_ENTRY FreeEntry,
730 SIZE_T Size)
731 {
732 PHEAP_SEGMENT Segment;
733 PHEAP_ENTRY PrecedingInUseEntry = NULL, NextInUseEntry = NULL;
734 PHEAP_FREE_ENTRY NextFreeEntry;
735 PHEAP_UCR_DESCRIPTOR UcrDescriptor;
736 ULONG PrecedingSize, NextSize, DecommitSize;
737 ULONG_PTR DecommitBase;
738 NTSTATUS Status;
739
740 DPRINT("Decommitting %p %p %x\n", Heap, FreeEntry, Size);
741
742 /* We can't decommit if there is a commit routine! */
743 if (Heap->CommitRoutine)
744 {
745 /* Just add it back the usual way */
746 RtlpInsertFreeBlock(Heap, FreeEntry, Size);
747 return;
748 }
749
750 /* Get the segment */
751 Segment = Heap->Segments[FreeEntry->SegmentOffset];
752
753 /* Get the preceding entry */
754 DecommitBase = ROUND_UP(FreeEntry, PAGE_SIZE);
755 PrecedingSize = (PHEAP_ENTRY)DecommitBase - (PHEAP_ENTRY)FreeEntry;
756
757 if (PrecedingSize == 1)
758 {
759 /* Just 1 heap entry, increase the base/size */
760 DecommitBase += PAGE_SIZE;
761 PrecedingSize += PAGE_SIZE >> HEAP_ENTRY_SHIFT;
762 }
763 else if (FreeEntry->PreviousSize &&
764 (DecommitBase == (ULONG_PTR)FreeEntry))
765 {
766 PrecedingInUseEntry = (PHEAP_ENTRY)FreeEntry - FreeEntry->PreviousSize;
767 }
768
769 /* Get the next entry */
770 NextFreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + Size);
771 DecommitSize = ROUND_DOWN(NextFreeEntry, PAGE_SIZE);
772 NextSize = (PHEAP_ENTRY)NextFreeEntry - (PHEAP_ENTRY)DecommitSize;
773
774 if (NextSize == 1)
775 {
776 /* Just 1 heap entry, increase the size */
777 DecommitSize -= PAGE_SIZE;
778 NextSize += PAGE_SIZE >> HEAP_ENTRY_SHIFT;
779 }
780 else if (NextSize == 0 &&
781 !(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
782 {
783 NextInUseEntry = (PHEAP_ENTRY)NextFreeEntry;
784 }
785
786 NextFreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)NextFreeEntry - NextSize);
787
788 /* Calculate real decommit size */
789 if (DecommitSize > DecommitBase)
790 {
791 DecommitSize -= DecommitBase;
792 }
793 else
794 {
795 /* Nothing to decommit */
796 RtlpInsertFreeBlock(Heap, FreeEntry, Size);
797 return;
798 }
799
800 /* A decommit is necessary. Create a UCR descriptor */
801 UcrDescriptor = RtlpCreateUnCommittedRange(Segment);
802 if (!UcrDescriptor)
803 {
804 DPRINT1("HEAP: Failed to create UCR descriptor\n");
805 RtlpInsertFreeBlock(Heap, FreeEntry, PrecedingSize);
806 return;
807 }
808
809 /* Decommit the memory */
810 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
811 (PVOID *)&DecommitBase,
812 &DecommitSize,
813 MEM_DECOMMIT);
814
815 /* Delete that UCR. This is needed to assure there is an unused UCR entry in the list */
816 RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
817
818 if (!NT_SUCCESS(Status))
819 {
820 RtlpInsertFreeBlock(Heap, FreeEntry, Size);
821 return;
822 }
823
824 /* Insert uncommitted pages */
825 RtlpInsertUnCommittedPages(Segment, DecommitBase, DecommitSize);
826 Segment->NumberOfUnCommittedPages += (DecommitSize / PAGE_SIZE);
827
828 if (PrecedingSize)
829 {
830 /* Adjust size of this free entry and insert it */
831 FreeEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
832 FreeEntry->Size = PrecedingSize;
833 Heap->TotalFreeSize += PrecedingSize;
834
835 /* Set last entry in the segment to this entry */
836 Segment->LastEntryInSegment = (PHEAP_ENTRY)FreeEntry;
837
838 /* Insert it into the free list */
839 RtlpInsertFreeBlockHelper(Heap, FreeEntry, PrecedingSize, FALSE);
840 }
841 else if (PrecedingInUseEntry)
842 {
843 /* Adjust preceding in use entry */
844 PrecedingInUseEntry->Flags |= HEAP_ENTRY_LAST_ENTRY;
845 Segment->LastEntryInSegment = PrecedingInUseEntry;
846 } else if ((ULONG_PTR)Segment->LastEntryInSegment >= DecommitBase &&
847 ((PCHAR)Segment->LastEntryInSegment < ((PCHAR)DecommitBase + DecommitSize)))
848 {
849 /* Update this segment's last entry */
850 Segment->LastEntryInSegment = Segment->FirstEntry;
851 }
852
853 /* Now the next one */
854 if (NextSize)
855 {
856 /* Adjust size of this free entry and insert it */
857 NextFreeEntry->Flags = 0;
858 NextFreeEntry->PreviousSize = 0;
859 NextFreeEntry->SegmentOffset = Segment->Entry.SegmentOffset;
860 NextFreeEntry->Size = NextSize;
861
862 ((PHEAP_FREE_ENTRY)((PHEAP_ENTRY)NextFreeEntry + NextSize))->PreviousSize = NextSize;
863
864 Heap->TotalFreeSize += NextSize;
865 RtlpInsertFreeBlockHelper(Heap, NextFreeEntry, NextSize, FALSE);
866 }
867 else if (NextInUseEntry)
868 {
869 NextInUseEntry->PreviousSize = 0;
870 }
871 }
872
873 BOOLEAN NTAPI
874 RtlpInitializeHeapSegment(PHEAP Heap,
875 PHEAP_SEGMENT Segment,
876 UCHAR SegmentIndex,
877 ULONG Flags,
878 PVOID BaseAddress,
879 PVOID UncommittedBase,
880 PVOID LimitAddress)
881 {
882 ULONG Pages, CommitSize;
883 PHEAP_ENTRY HeapEntry;
884 USHORT PreviousSize = 0, NewSize;
885 NTSTATUS Status;
886
887 Pages = ((PCHAR)LimitAddress - (PCHAR)BaseAddress) / PAGE_SIZE;
888
889 HeapEntry = (PHEAP_ENTRY)ROUND_UP(Segment + 1, HEAP_ENTRY_SIZE);
890
891 DPRINT("RtlpInitializeHeapSegment(%p %p %x %x %p %p %p)\n", Heap, Segment, SegmentIndex, Flags, BaseAddress, UncommittedBase, LimitAddress);
892 DPRINT("Pages %x, HeapEntry %p, sizeof(HEAP_SEGMENT) %x\n", Pages, HeapEntry, sizeof(HEAP_SEGMENT));
893
894 /* Check if it's the first segment and remember its size */
895 if (Heap == BaseAddress)
896 PreviousSize = Heap->Entry.Size;
897
898 NewSize = ((PCHAR)HeapEntry - (PCHAR)Segment) >> HEAP_ENTRY_SHIFT;
899
900 if ((PVOID)(HeapEntry + 1) >= UncommittedBase)
901 {
902 /* Check if it goes beyond the limit */
903 if ((PVOID)(HeapEntry + 1) >= LimitAddress)
904 return FALSE;
905
906 /* Need to commit memory */
907 CommitSize = (PCHAR)(HeapEntry + 1) - (PCHAR)UncommittedBase;
908 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
909 (PVOID)&UncommittedBase,
910 0,
911 &CommitSize,
912 MEM_COMMIT,
913 PAGE_READWRITE);
914 if (!NT_SUCCESS(Status))
915 {
916 DPRINT1("Committing page failed with status 0x%08X\n", Status);
917 return FALSE;
918 }
919
920 DPRINT("Committed %d bytes at base %p\n", CommitSize, UncommittedBase);
921
922 /* Calcule the new uncommitted base */
923 UncommittedBase = (PVOID)((PCHAR)UncommittedBase + CommitSize);
924 }
925
926 /* Initialize the segment entry */
927 Segment->Entry.PreviousSize = PreviousSize;
928 Segment->Entry.Size = NewSize;
929 Segment->Entry.Flags = HEAP_ENTRY_BUSY;
930 Segment->Entry.SegmentOffset = SegmentIndex;
931
932 /* Initialize the segment itself */
933 Segment->SegmentSignature = HEAP_SEGMENT_SIGNATURE;
934 Segment->Heap = Heap;
935 Segment->BaseAddress = BaseAddress;
936 Segment->FirstEntry = HeapEntry;
937 Segment->LastValidEntry = (PHEAP_ENTRY)((PCHAR)BaseAddress + Pages * PAGE_SIZE);
938 Segment->NumberOfPages = Pages;
939 Segment->NumberOfUnCommittedPages = ((PCHAR)LimitAddress - (PCHAR)UncommittedBase) / PAGE_SIZE;
940 InitializeListHead(&Segment->UCRSegmentList);
941
942 /* Insert uncommitted pages into UCR (uncommitted ranges) list */
943 if (Segment->NumberOfUnCommittedPages)
944 {
945 RtlpInsertUnCommittedPages(Segment, (ULONG_PTR)UncommittedBase, Segment->NumberOfUnCommittedPages * PAGE_SIZE);
946 }
947
948 /* Set the segment index pointer */
949 Heap->Segments[SegmentIndex] = Segment;
950
951 /* Prepare a free heap entry */
952 HeapEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
953 HeapEntry->PreviousSize = Segment->Entry.Size;
954 HeapEntry->SegmentOffset = SegmentIndex;
955
956 /* Set last entry in segment */
957 Segment->LastEntryInSegment = HeapEntry;
958
959 /* Insert it */
960 RtlpInsertFreeBlock(Heap, (PHEAP_FREE_ENTRY)HeapEntry, (PHEAP_ENTRY)UncommittedBase - HeapEntry);
961
962 return TRUE;
963 }
964
965 VOID NTAPI
966 RtlpDestroyHeapSegment(PHEAP_SEGMENT Segment)
967 {
968 NTSTATUS Status;
969 PVOID BaseAddress;
970 SIZE_T Size = 0;
971
972 /* Make sure it's not user allocated */
973 if (Segment->SegmentFlags & HEAP_USER_ALLOCATED) return;
974
975 BaseAddress = Segment->BaseAddress;
976 DPRINT("Destroying segment %p, BA %p\n", Segment, BaseAddress);
977
978 /* Release virtual memory */
979 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
980 &BaseAddress,
981 &Size,
982 MEM_RELEASE);
983
984 if (!NT_SUCCESS(Status))
985 {
986 DPRINT1("HEAP: Failed to release segment's memory with status 0x%08X\n", Status);
987 }
988 }
989
990 /* Usermode only! */
991 VOID NTAPI
992 RtlpAddHeapToProcessList(PHEAP Heap)
993 {
994 PPEB Peb;
995
996 /* Get PEB */
997 Peb = RtlGetCurrentPeb();
998
999 /* Acquire the lock */
1000 RtlEnterHeapLock(&RtlpProcessHeapsListLock);
1001
1002 //_SEH2_TRY {
1003 /* Check if max number of heaps reached */
1004 if (Peb->NumberOfHeaps == Peb->MaximumNumberOfHeaps)
1005 {
1006 // TODO: Handle this case
1007 ASSERT(FALSE);
1008 }
1009
1010 /* Add the heap to the process heaps */
1011 Peb->ProcessHeaps[Peb->NumberOfHeaps] = Heap;
1012 Peb->NumberOfHeaps++;
1013 Heap->ProcessHeapsListIndex = Peb->NumberOfHeaps;
1014 // } _SEH2_FINALLY {
1015
1016 /* Release the lock */
1017 RtlLeaveHeapLock(&RtlpProcessHeapsListLock);
1018
1019 // } _SEH2_END
1020 }
1021
1022 /* Usermode only! */
1023 VOID NTAPI
1024 RtlpRemoveHeapFromProcessList(PHEAP Heap)
1025 {
1026 PPEB Peb;
1027 PHEAP *Current, *Next;
1028 ULONG Count;
1029
1030 /* Get PEB */
1031 Peb = RtlGetCurrentPeb();
1032
1033 /* Acquire the lock */
1034 RtlEnterHeapLock(&RtlpProcessHeapsListLock);
1035
1036 /* Check if we don't need anything to do */
1037 if ((Heap->ProcessHeapsListIndex == 0) ||
1038 (Heap->ProcessHeapsListIndex > Peb->NumberOfHeaps) ||
1039 (Peb->NumberOfHeaps == 0))
1040 {
1041 /* Release the lock */
1042 RtlLeaveHeapLock(&RtlpProcessHeapsListLock);
1043
1044 return;
1045 }
1046
1047 /* The process actually has more than one heap.
1048 Use classic, lernt from university times algorithm for removing an entry
1049 from a static array */
1050
1051 Current = (PHEAP *)&Peb->ProcessHeaps[Heap->ProcessHeapsListIndex - 1];
1052 Next = Current + 1;
1053
1054 /* How many items we need to shift to the left */
1055 Count = Peb->NumberOfHeaps - (Heap->ProcessHeapsListIndex - 1);
1056
1057 /* Move them all in a loop */
1058 while (--Count)
1059 {
1060 /* Copy it and advance next pointer */
1061 *Current = *Next;
1062
1063 /* Update its index */
1064 (*Current)->ProcessHeapsListIndex -= 1;
1065
1066 /* Advance pointers */
1067 Current++;
1068 Next++;
1069 }
1070
1071 /* Decrease total number of heaps */
1072 Peb->NumberOfHeaps--;
1073
1074 /* Zero last unused item */
1075 Peb->ProcessHeaps[Peb->NumberOfHeaps] = NULL;
1076 Heap->ProcessHeapsListIndex = 0;
1077
1078 /* Release the lock */
1079 RtlLeaveHeapLock(&RtlpProcessHeapsListLock);
1080 }
1081
1082 PHEAP_FREE_ENTRY NTAPI
1083 RtlpCoalesceHeap(PHEAP Heap)
1084 {
1085 UNIMPLEMENTED;
1086 return NULL;
1087 }
1088
1089 PHEAP_FREE_ENTRY NTAPI
1090 RtlpCoalesceFreeBlocks (PHEAP Heap,
1091 PHEAP_FREE_ENTRY FreeEntry,
1092 PSIZE_T FreeSize,
1093 BOOLEAN Remove)
1094 {
1095 PHEAP_FREE_ENTRY CurrentEntry, NextEntry;
1096
1097 /* Get the previous entry */
1098 CurrentEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry - FreeEntry->PreviousSize);
1099
1100 /* Check it */
1101 if (CurrentEntry != FreeEntry &&
1102 !(CurrentEntry->Flags & HEAP_ENTRY_BUSY) &&
1103 (*FreeSize + CurrentEntry->Size) <= HEAP_MAX_BLOCK_SIZE)
1104 {
1105 ASSERT(FreeEntry->PreviousSize == CurrentEntry->Size);
1106
1107 /* Remove it if asked for */
1108 if (Remove)
1109 {
1110 RtlpRemoveFreeBlock(Heap, FreeEntry, FALSE, FALSE);
1111 Heap->TotalFreeSize -= FreeEntry->Size;
1112
1113 /* Remove it only once! */
1114 Remove = FALSE;
1115 }
1116
1117 /* Remove previous entry too */
1118 RtlpRemoveFreeBlock(Heap, CurrentEntry, FALSE, FALSE);
1119
1120 /* Copy flags */
1121 CurrentEntry->Flags = FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY;
1122
1123 /* Update last entry in the segment */
1124 if (CurrentEntry->Flags & HEAP_ENTRY_LAST_ENTRY)
1125 Heap->Segments[CurrentEntry->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)CurrentEntry;
1126
1127 /* Advance FreeEntry and update sizes */
1128 FreeEntry = CurrentEntry;
1129 *FreeSize = *FreeSize + CurrentEntry->Size;
1130 Heap->TotalFreeSize -= CurrentEntry->Size;
1131 FreeEntry->Size = *FreeSize;
1132
1133 /* Also update previous size if needed */
1134 if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
1135 {
1136 ((PHEAP_ENTRY)FreeEntry + *FreeSize)->PreviousSize = *FreeSize;
1137 }
1138 }
1139
1140 /* Check the next block if it exists */
1141 if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
1142 {
1143 NextEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + *FreeSize);
1144
1145 if (!(NextEntry->Flags & HEAP_ENTRY_BUSY) &&
1146 NextEntry->Size + *FreeSize <= HEAP_MAX_BLOCK_SIZE)
1147 {
1148 ASSERT(*FreeSize == NextEntry->PreviousSize);
1149
1150 /* Remove it if asked for */
1151 if (Remove)
1152 {
1153 RtlpRemoveFreeBlock(Heap, FreeEntry, FALSE, FALSE);
1154 Heap->TotalFreeSize -= FreeEntry->Size;
1155 }
1156
1157 /* Copy flags */
1158 FreeEntry->Flags = NextEntry->Flags & HEAP_ENTRY_LAST_ENTRY;
1159
1160 /* Update last entry in the segment */
1161 if (FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY)
1162 Heap->Segments[FreeEntry->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)FreeEntry;
1163
1164 /* Remove next entry now */
1165 RtlpRemoveFreeBlock(Heap, NextEntry, FALSE, FALSE);
1166
1167 /* Update sizes */
1168 *FreeSize = *FreeSize + NextEntry->Size;
1169 Heap->TotalFreeSize -= NextEntry->Size;
1170 FreeEntry->Size = *FreeSize;
1171
1172 /* Also update previous size if needed */
1173 if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
1174 {
1175 ((PHEAP_ENTRY)FreeEntry + *FreeSize)->PreviousSize = *FreeSize;
1176 }
1177 }
1178 }
1179 return FreeEntry;
1180 }
1181
1182 PHEAP_FREE_ENTRY NTAPI
1183 RtlpExtendHeap(PHEAP Heap,
1184 SIZE_T Size)
1185 {
1186 ULONG Pages;
1187 UCHAR Index, EmptyIndex;
1188 SIZE_T FreeSize, CommitSize, ReserveSize;
1189 PHEAP_SEGMENT Segment;
1190 PHEAP_FREE_ENTRY FreeEntry;
1191 NTSTATUS Status;
1192
1193 DPRINT("RtlpExtendHeap(%p %x)\n", Heap, Size);
1194
1195 /* Calculate amount in pages */
1196 Pages = (Size + PAGE_SIZE - 1) / PAGE_SIZE;
1197 FreeSize = Pages * PAGE_SIZE;
1198 DPRINT("Pages %x, FreeSize %x. Going through segments...\n", Pages, FreeSize);
1199
1200 /* Find an empty segment */
1201 EmptyIndex = HEAP_SEGMENTS;
1202 for (Index = 0; Index < HEAP_SEGMENTS; Index++)
1203 {
1204 Segment = Heap->Segments[Index];
1205
1206 if (Segment) DPRINT("Segment[%d] %p with NOUCP %x\n", Index, Segment, Segment->NumberOfUnCommittedPages);
1207
1208 /* Check if its size suits us */
1209 if (Segment &&
1210 Pages <= Segment->NumberOfUnCommittedPages)
1211 {
1212 DPRINT("This segment is suitable\n");
1213
1214 /* Commit needed amount */
1215 FreeEntry = RtlpFindAndCommitPages(Heap, Segment, &FreeSize, NULL);
1216
1217 /* Coalesce it with adjacent entries */
1218 if (FreeEntry)
1219 {
1220 FreeSize = FreeSize >> HEAP_ENTRY_SHIFT;
1221 FreeEntry = RtlpCoalesceFreeBlocks(Heap, FreeEntry, &FreeSize, FALSE);
1222 RtlpInsertFreeBlock(Heap, FreeEntry, FreeSize);
1223 return FreeEntry;
1224 }
1225 }
1226 else if (!Segment &&
1227 EmptyIndex == HEAP_SEGMENTS)
1228 {
1229 /* Remember the first unused segment index */
1230 EmptyIndex = Index;
1231 }
1232 }
1233
1234 /* No luck, need to grow the heap */
1235 if ((Heap->Flags & HEAP_GROWABLE) &&
1236 (EmptyIndex != HEAP_SEGMENTS))
1237 {
1238 Segment = NULL;
1239
1240 /* Reserve the memory */
1241 if ((Size + PAGE_SIZE) <= Heap->SegmentReserve)
1242 ReserveSize = Heap->SegmentReserve;
1243 else
1244 ReserveSize = Size + PAGE_SIZE;
1245
1246 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
1247 (PVOID)&Segment,
1248 0,
1249 &ReserveSize,
1250 MEM_RESERVE,
1251 PAGE_READWRITE);
1252
1253 /* If it failed, retry again with a half division algorithm */
1254 while (!NT_SUCCESS(Status) &&
1255 ReserveSize != Size + PAGE_SIZE)
1256 {
1257 ReserveSize /= 2;
1258
1259 if (ReserveSize < (Size + PAGE_SIZE))
1260 ReserveSize = Size + PAGE_SIZE;
1261
1262 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
1263 (PVOID)&Segment,
1264 0,
1265 &ReserveSize,
1266 MEM_RESERVE,
1267 PAGE_READWRITE);
1268 }
1269
1270 /* Proceed only if it's success */
1271 if (NT_SUCCESS(Status))
1272 {
1273 Heap->SegmentReserve += ReserveSize;
1274
1275 /* Now commit the memory */
1276 if ((Size + PAGE_SIZE) <= Heap->SegmentCommit)
1277 CommitSize = Heap->SegmentCommit;
1278 else
1279 CommitSize = Size + PAGE_SIZE;
1280
1281 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
1282 (PVOID)&Segment,
1283 0,
1284 &CommitSize,
1285 MEM_COMMIT,
1286 PAGE_READWRITE);
1287
1288 DPRINT("Committed %d bytes at base %p\n", CommitSize, Segment);
1289
1290 /* Initialize heap segment if commit was successful */
1291 if (NT_SUCCESS(Status))
1292 {
1293 if (!RtlpInitializeHeapSegment(Heap, Segment, EmptyIndex, 0, Segment,
1294 (PCHAR)Segment + CommitSize, (PCHAR)Segment + ReserveSize))
1295 {
1296 Status = STATUS_NO_MEMORY;
1297 }
1298 }
1299
1300 /* If everything worked - cool */
1301 if (NT_SUCCESS(Status)) return (PHEAP_FREE_ENTRY)Segment->FirstEntry;
1302
1303 DPRINT1("Committing failed with status 0x%08X\n", Status);
1304
1305 /* Nope, we failed. Free memory */
1306 ZwFreeVirtualMemory(NtCurrentProcess(),
1307 (PVOID)&Segment,
1308 &ReserveSize,
1309 MEM_RELEASE);
1310 }
1311 else
1312 {
1313 DPRINT1("Reserving failed with status 0x%08X\n", Status);
1314 }
1315 }
1316
1317 if (RtlpGetMode() == UserMode)
1318 {
1319 /* If coalescing on free is disabled in usermode, then do it here */
1320 if (Heap->Flags & HEAP_DISABLE_COALESCE_ON_FREE)
1321 {
1322 FreeEntry = RtlpCoalesceHeap(Heap);
1323
1324 /* If it's a suitable one - return it */
1325 if (FreeEntry &&
1326 FreeEntry->Size >= Size)
1327 {
1328 return FreeEntry;
1329 }
1330 }
1331 }
1332
1333 return NULL;
1334 }
1335
1336 /***********************************************************************
1337 * RtlCreateHeap
1338 * RETURNS
1339 * Handle of heap: Success
1340 * NULL: Failure
1341 *
1342 * @implemented
1343 */
1344 HANDLE NTAPI
1345 RtlCreateHeap(ULONG Flags,
1346 PVOID Addr,
1347 SIZE_T TotalSize,
1348 SIZE_T CommitSize,
1349 PVOID Lock,
1350 PRTL_HEAP_PARAMETERS Parameters)
1351 {
1352 PVOID CommittedAddress = NULL, UncommittedAddress = NULL;
1353 PHEAP Heap = NULL;
1354 RTL_HEAP_PARAMETERS SafeParams = {0};
1355 PPEB Peb;
1356 ULONG_PTR MaximumUserModeAddress;
1357 SYSTEM_BASIC_INFORMATION SystemInformation;
1358 MEMORY_BASIC_INFORMATION MemoryInfo;
1359 ULONG NtGlobalFlags = RtlGetNtGlobalFlags();
1360 ULONG HeapSegmentFlags = 0;
1361 NTSTATUS Status;
1362 ULONG MaxBlockSize, HeaderSize;
1363 BOOLEAN AllocateLock = FALSE;
1364
1365 /* Check for a special heap */
1366 if (RtlpPageHeapEnabled && !Addr && !Lock)
1367 {
1368 Heap = RtlpPageHeapCreate(Flags, Addr, TotalSize, CommitSize, Lock, Parameters);
1369 if (Heap) return Heap;
1370
1371 //ASSERT(FALSE);
1372 DPRINT1("Enabling page heap failed\n");
1373 }
1374
1375 /* Check validation flags */
1376 if (!(Flags & HEAP_SKIP_VALIDATION_CHECKS) && (Flags & ~HEAP_CREATE_VALID_MASK))
1377 {
1378 DPRINT1("Invalid flags 0x%08x, fixing...\n", Flags);
1379 Flags &= HEAP_CREATE_VALID_MASK;
1380 }
1381
1382 /* TODO: Capture parameters, once we decide to use SEH */
1383 if (!Parameters) Parameters = &SafeParams;
1384
1385 /* Check global flags */
1386 if (NtGlobalFlags & FLG_HEAP_DISABLE_COALESCING)
1387 Flags |= HEAP_DISABLE_COALESCE_ON_FREE;
1388
1389 if (NtGlobalFlags & FLG_HEAP_ENABLE_FREE_CHECK)
1390 Flags |= HEAP_FREE_CHECKING_ENABLED;
1391
1392 if (NtGlobalFlags & FLG_HEAP_ENABLE_TAIL_CHECK)
1393 Flags |= HEAP_TAIL_CHECKING_ENABLED;
1394
1395 if (RtlpGetMode() == UserMode)
1396 {
1397 /* Also check these flags if in usermode */
1398 if (NtGlobalFlags & FLG_HEAP_VALIDATE_ALL)
1399 Flags |= HEAP_VALIDATE_ALL_ENABLED;
1400
1401 if (NtGlobalFlags & FLG_HEAP_VALIDATE_PARAMETERS)
1402 Flags |= HEAP_VALIDATE_PARAMETERS_ENABLED;
1403
1404 if (NtGlobalFlags & FLG_USER_STACK_TRACE_DB)
1405 Flags |= HEAP_CAPTURE_STACK_BACKTRACES;
1406
1407 /* Get PEB */
1408 Peb = RtlGetCurrentPeb();
1409
1410 /* Apply defaults for non-set parameters */
1411 if (!Parameters->SegmentCommit) Parameters->SegmentCommit = Peb->HeapSegmentCommit;
1412 if (!Parameters->SegmentReserve) Parameters->SegmentReserve = Peb->HeapSegmentReserve;
1413 if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = Peb->HeapDeCommitFreeBlockThreshold;
1414 if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = Peb->HeapDeCommitTotalFreeThreshold;
1415 }
1416 else
1417 {
1418 /* Apply defaults for non-set parameters */
1419 #if 0
1420 if (!Parameters->SegmentCommit) Parameters->SegmentCommit = MmHeapSegmentCommit;
1421 if (!Parameters->SegmentReserve) Parameters->SegmentReserve = MmHeapSegmentReserve;
1422 if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;
1423 if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
1424 #endif
1425 }
1426
1427 // FIXME: Move to memory manager
1428 if (!Parameters->SegmentCommit) Parameters->SegmentCommit = PAGE_SIZE * 2;
1429 if (!Parameters->SegmentReserve) Parameters->SegmentReserve = 1048576;
1430 if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = PAGE_SIZE;
1431 if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = 65536;
1432
1433 /* Get the max um address */
1434 Status = ZwQuerySystemInformation(SystemBasicInformation,
1435 &SystemInformation,
1436 sizeof(SystemInformation),
1437 NULL);
1438
1439 if (!NT_SUCCESS(Status))
1440 {
1441 DPRINT1("Getting max usermode address failed with status 0x%08x\n", Status);
1442 return NULL;
1443 }
1444
1445 MaximumUserModeAddress = SystemInformation.MaximumUserModeAddress;
1446
1447 /* Calculate max alloc size */
1448 if (!Parameters->MaximumAllocationSize)
1449 Parameters->MaximumAllocationSize = MaximumUserModeAddress - (ULONG_PTR)0x10000 - PAGE_SIZE;
1450
1451 MaxBlockSize = 0x80000 - PAGE_SIZE;
1452
1453 if (!Parameters->VirtualMemoryThreshold ||
1454 Parameters->VirtualMemoryThreshold > MaxBlockSize)
1455 {
1456 Parameters->VirtualMemoryThreshold = MaxBlockSize;
1457 }
1458
1459 /* Check reserve/commit sizes and set default values */
1460 if (!CommitSize)
1461 {
1462 CommitSize = PAGE_SIZE;
1463 if (TotalSize)
1464 TotalSize = ROUND_UP(TotalSize, PAGE_SIZE);
1465 else
1466 TotalSize = 64 * PAGE_SIZE;
1467 }
1468 else
1469 {
1470 /* Round up the commit size to be at least the page size */
1471 CommitSize = ROUND_UP(CommitSize, PAGE_SIZE);
1472
1473 if (TotalSize)
1474 TotalSize = ROUND_UP(TotalSize, PAGE_SIZE);
1475 else
1476 TotalSize = ROUND_UP(CommitSize, 16 * PAGE_SIZE);
1477 }
1478
1479 /* Call special heap */
1480 if (RtlpHeapIsSpecial(Flags))
1481 return RtlDebugCreateHeap(Flags, Addr, TotalSize, CommitSize, Lock, Parameters);
1482
1483 /* Calculate header size */
1484 HeaderSize = sizeof(HEAP);
1485 if (!(Flags & HEAP_NO_SERIALIZE))
1486 {
1487 if (Lock)
1488 {
1489 Flags |= HEAP_LOCK_USER_ALLOCATED;
1490 }
1491 else
1492 {
1493 HeaderSize += sizeof(HEAP_LOCK);
1494 AllocateLock = TRUE;
1495 }
1496 }
1497 else if (Lock)
1498 {
1499 /* Invalid parameters */
1500 return NULL;
1501 }
1502
1503 /* See if we are already provided with an address for the heap */
1504 if (Addr)
1505 {
1506 if (Parameters->CommitRoutine)
1507 {
1508 /* There is a commit routine, so no problem here, check params */
1509 if ((Flags & HEAP_GROWABLE) ||
1510 !Parameters->InitialCommit ||
1511 !Parameters->InitialReserve ||
1512 (Parameters->InitialCommit > Parameters->InitialReserve))
1513 {
1514 /* Fail */
1515 return NULL;
1516 }
1517
1518 /* Calculate committed and uncommitted addresses */
1519 CommittedAddress = Addr;
1520 UncommittedAddress = (PCHAR)Addr + Parameters->InitialCommit;
1521 TotalSize = Parameters->InitialReserve;
1522
1523 /* Zero the initial page ourselves */
1524 RtlZeroMemory(CommittedAddress, PAGE_SIZE);
1525 }
1526 else
1527 {
1528 /* Commit routine is absent, so query how much memory caller reserved */
1529 Status = ZwQueryVirtualMemory(NtCurrentProcess(),
1530 Addr,
1531 MemoryBasicInformation,
1532 &MemoryInfo,
1533 sizeof(MemoryInfo),
1534 NULL);
1535
1536 if (!NT_SUCCESS(Status))
1537 {
1538 DPRINT1("Querying amount of user supplied memory failed with status 0x%08X\n", Status);
1539 return NULL;
1540 }
1541
1542 /* Validate it */
1543 if (MemoryInfo.BaseAddress != Addr ||
1544 MemoryInfo.State == MEM_FREE)
1545 {
1546 return NULL;
1547 }
1548
1549 /* Validation checks passed, set committed/uncommitted addresses */
1550 CommittedAddress = Addr;
1551
1552 /* Check if it's committed or not */
1553 if (MemoryInfo.State == MEM_COMMIT)
1554 {
1555 /* Zero it out because it's already committed */
1556 RtlZeroMemory(CommittedAddress, PAGE_SIZE);
1557
1558 /* Calculate uncommitted address value */
1559 CommitSize = MemoryInfo.RegionSize;
1560 TotalSize = CommitSize;
1561 UncommittedAddress = (PCHAR)Addr + CommitSize;
1562
1563 /* Check if uncommitted address is reserved */
1564 Status = ZwQueryVirtualMemory(NtCurrentProcess(),
1565 UncommittedAddress,
1566 MemoryBasicInformation,
1567 &MemoryInfo,
1568 sizeof(MemoryInfo),
1569 NULL);
1570
1571 if (NT_SUCCESS(Status) &&
1572 MemoryInfo.State == MEM_RESERVE)
1573 {
1574 /* It is, so add it up to the reserve size */
1575 TotalSize += MemoryInfo.RegionSize;
1576 }
1577 }
1578 else
1579 {
1580 /* It's not committed, inform following code that a commit is necessary */
1581 CommitSize = PAGE_SIZE;
1582 UncommittedAddress = Addr;
1583 }
1584 }
1585
1586 /* Mark this as a user-committed mem */
1587 HeapSegmentFlags = HEAP_USER_ALLOCATED;
1588 Heap = (PHEAP)Addr;
1589 }
1590 else
1591 {
1592 /* Check commit routine */
1593 if (Parameters->CommitRoutine) return NULL;
1594
1595 /* Reserve memory */
1596 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
1597 (PVOID *)&Heap,
1598 0,
1599 &TotalSize,
1600 MEM_RESERVE,
1601 PAGE_READWRITE);
1602
1603 if (!NT_SUCCESS(Status))
1604 {
1605 DPRINT1("Failed to reserve memory with status 0x%08x\n", Status);
1606 return NULL;
1607 }
1608
1609 /* Set base addresses */
1610 CommittedAddress = Heap;
1611 UncommittedAddress = Heap;
1612 }
1613
1614 /* Check if we need to commit something */
1615 if (CommittedAddress == UncommittedAddress)
1616 {
1617 /* Commit the required size */
1618 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
1619 &CommittedAddress,
1620 0,
1621 &CommitSize,
1622 MEM_COMMIT,
1623 PAGE_READWRITE);
1624
1625 DPRINT("Committed %d bytes at base %p\n", CommitSize, CommittedAddress);
1626
1627 if (!NT_SUCCESS(Status))
1628 {
1629 DPRINT1("Failure, Status 0x%08X\n", Status);
1630
1631 /* Release memory if it was reserved */
1632 if (!Addr) ZwFreeVirtualMemory(NtCurrentProcess(),
1633 (PVOID *)&Heap,
1634 &TotalSize,
1635 MEM_RELEASE);
1636
1637 return NULL;
1638 }
1639
1640 /* Calculate new uncommitted address */
1641 UncommittedAddress = (PCHAR)UncommittedAddress + CommitSize;
1642 }
1643
1644 DPRINT("Created heap %p, CommitSize %x, ReserveSize %x\n", Heap, CommitSize, TotalSize);
1645
1646 /* Initialize the heap */
1647 RtlpInitializeHeap(Heap, &HeaderSize, Flags, AllocateLock, Lock);
1648
1649 /* Initialize heap's first segment */
1650 if (!RtlpInitializeHeapSegment(Heap,
1651 (PHEAP_SEGMENT)((PCHAR)Heap + HeaderSize),
1652 0,
1653 HeapSegmentFlags,
1654 CommittedAddress,
1655 UncommittedAddress,
1656 (PCHAR)CommittedAddress + TotalSize))
1657 {
1658 DPRINT1("Failed to initialize heap segment\n");
1659 return NULL;
1660 }
1661
1662 /* Set other data */
1663 Heap->ProcessHeapsListIndex = 0;
1664 Heap->SegmentCommit = Parameters->SegmentCommit;
1665 Heap->SegmentReserve = Parameters->SegmentReserve;
1666 Heap->DeCommitFreeBlockThreshold = Parameters->DeCommitFreeBlockThreshold >> HEAP_ENTRY_SHIFT;
1667 Heap->DeCommitTotalFreeThreshold = Parameters->DeCommitTotalFreeThreshold >> HEAP_ENTRY_SHIFT;
1668 Heap->MaximumAllocationSize = Parameters->MaximumAllocationSize;
1669 Heap->VirtualMemoryThreshold = ROUND_UP(Parameters->VirtualMemoryThreshold, HEAP_ENTRY_SIZE) >> HEAP_ENTRY_SHIFT;
1670 Heap->CommitRoutine = Parameters->CommitRoutine;
1671
1672 /* Set alignment */
1673 if (Flags & HEAP_CREATE_ALIGN_16)
1674 {
1675 Heap->AlignMask = (ULONG)~15;
1676 Heap->AlignRound = 15 + sizeof(HEAP_ENTRY);
1677 }
1678 else
1679 {
1680 Heap->AlignMask = (ULONG)~(HEAP_ENTRY_SIZE - 1);
1681 Heap->AlignRound = HEAP_ENTRY_SIZE - 1 + sizeof(HEAP_ENTRY);
1682 }
1683
1684 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
1685 Heap->AlignRound += HEAP_ENTRY_SIZE;
1686
1687 /* Add heap to process list in case of usermode heap */
1688 if (RtlpGetMode() == UserMode)
1689 {
1690 RtlpAddHeapToProcessList(Heap);
1691
1692 // FIXME: What about lookasides?
1693 }
1694
1695 DPRINT("Heap %p, flags 0x%08x\n", Heap, Heap->Flags);
1696 return Heap;
1697 }
1698
1699 /***********************************************************************
1700 * RtlDestroyHeap
1701 * RETURNS
1702 * TRUE: Success
1703 * FALSE: Failure
1704 *
1705 * @implemented
1706 *
1707 * RETURNS
1708 * Success: A NULL HANDLE, if heap is NULL or it was destroyed
1709 * Failure: The Heap handle, if heap is the process heap.
1710 */
1711 HANDLE NTAPI
1712 RtlDestroyHeap(HANDLE HeapPtr) /* [in] Handle of heap */
1713 {
1714 PHEAP Heap = (PHEAP)HeapPtr;
1715 PLIST_ENTRY Current;
1716 PHEAP_UCR_SEGMENT UcrSegment;
1717 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualEntry;
1718 PVOID BaseAddress;
1719 SIZE_T Size;
1720 LONG i;
1721 PHEAP_SEGMENT Segment;
1722
1723 if (!HeapPtr) return NULL;
1724
1725 /* Call special heap */
1726 if (RtlpHeapIsSpecial(Heap->Flags))
1727 {
1728 if (!RtlDebugDestroyHeap(Heap)) return HeapPtr;
1729 }
1730
1731 /* Check for a process heap */
1732 if (RtlpGetMode() == UserMode &&
1733 HeapPtr == NtCurrentPeb()->ProcessHeap) return HeapPtr;
1734
1735 /* Free up all big allocations */
1736 Current = Heap->VirtualAllocdBlocks.Flink;
1737 while (Current != &Heap->VirtualAllocdBlocks)
1738 {
1739 VirtualEntry = CONTAINING_RECORD(Current, HEAP_VIRTUAL_ALLOC_ENTRY, Entry);
1740 BaseAddress = (PVOID)VirtualEntry;
1741 Current = Current->Flink;
1742 Size = 0;
1743 ZwFreeVirtualMemory(NtCurrentProcess(),
1744 &BaseAddress,
1745 &Size,
1746 MEM_RELEASE);
1747 }
1748
1749 /* Delete tags and remove heap from the process heaps list in user mode */
1750 if (RtlpGetMode() == UserMode)
1751 {
1752 // FIXME DestroyTags
1753 RtlpRemoveHeapFromProcessList(Heap);
1754 }
1755
1756 /* Delete the heap lock */
1757 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
1758 {
1759 /* Delete it if it wasn't user allocated */
1760 if (!(Heap->Flags & HEAP_LOCK_USER_ALLOCATED))
1761 RtlDeleteHeapLock(Heap->LockVariable);
1762
1763 /* Clear out the lock variable */
1764 Heap->LockVariable = NULL;
1765 }
1766
1767 /* Free UCR segments if any were created */
1768 Current = Heap->UCRSegments.Flink;
1769 while(Current != &Heap->UCRSegments)
1770 {
1771 UcrSegment = CONTAINING_RECORD(Current, HEAP_UCR_SEGMENT, ListEntry);
1772
1773 /* Advance to the next descriptor */
1774 Current = Current->Flink;
1775
1776 BaseAddress = (PVOID)UcrSegment;
1777 Size = 0;
1778
1779 /* Release that memory */
1780 ZwFreeVirtualMemory(NtCurrentProcess(),
1781 &BaseAddress,
1782 &Size,
1783 MEM_RELEASE);
1784 }
1785
1786 /* Go through segments and destroy them */
1787 for (i = HEAP_SEGMENTS - 1; i >= 0; i--)
1788 {
1789 Segment = Heap->Segments[i];
1790 if (Segment) RtlpDestroyHeapSegment(Segment);
1791 }
1792
1793 return NULL;
1794 }
1795
1796 PHEAP_ENTRY NTAPI
1797 RtlpSplitEntry(PHEAP Heap,
1798 PHEAP_FREE_ENTRY FreeBlock,
1799 SIZE_T AllocationSize,
1800 SIZE_T Index,
1801 SIZE_T Size)
1802 {
1803 PHEAP_FREE_ENTRY SplitBlock, SplitBlock2;
1804 UCHAR FreeFlags;
1805 PHEAP_ENTRY InUseEntry;
1806 SIZE_T FreeSize;
1807
1808 /* Save flags, update total free size */
1809 FreeFlags = FreeBlock->Flags;
1810 Heap->TotalFreeSize -= FreeBlock->Size;
1811
1812 /* Make this block an in-use one */
1813 InUseEntry = (PHEAP_ENTRY)FreeBlock;
1814 InUseEntry->Flags = HEAP_ENTRY_BUSY;
1815 InUseEntry->SmallTagIndex = 0;
1816
1817 /* Calculate the extra amount */
1818 FreeSize = InUseEntry->Size - Index;
1819
1820 /* Update it's size fields (we don't need their data anymore) */
1821 InUseEntry->Size = Index;
1822 InUseEntry->UnusedBytes = AllocationSize - Size;
1823
1824 /* If there is something to split - do the split */
1825 if (FreeSize != 0)
1826 {
1827 /* Don't split if resulting entry can't contain any payload data
1828 (i.e. being just HEAP_ENTRY_SIZE) */
1829 if (FreeSize == 1)
1830 {
1831 /* Increase sizes of the in-use entry */
1832 InUseEntry->Size++;
1833 InUseEntry->UnusedBytes += sizeof(HEAP_ENTRY);
1834 }
1835 else
1836 {
1837 /* Calculate a pointer to the new entry */
1838 SplitBlock = (PHEAP_FREE_ENTRY)(InUseEntry + Index);
1839
1840 /* Initialize it */
1841 SplitBlock->Flags = FreeFlags;
1842 SplitBlock->SegmentOffset = InUseEntry->SegmentOffset;
1843 SplitBlock->Size = FreeSize;
1844 SplitBlock->PreviousSize = Index;
1845
1846 /* Check if it's the last entry */
1847 if (FreeFlags & HEAP_ENTRY_LAST_ENTRY)
1848 {
1849 /* Insert it to the free list if it's the last entry */
1850 RtlpInsertFreeBlockHelper(Heap, SplitBlock, FreeSize, FALSE);
1851 Heap->TotalFreeSize += FreeSize;
1852 }
1853 else
1854 {
1855 /* Not so easy - need to update next's previous size too */
1856 SplitBlock2 = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)SplitBlock + FreeSize);
1857
1858 if (SplitBlock2->Flags & HEAP_ENTRY_BUSY)
1859 {
1860 SplitBlock2->PreviousSize = (USHORT)FreeSize;
1861 RtlpInsertFreeBlockHelper(Heap, SplitBlock, FreeSize, FALSE);
1862 Heap->TotalFreeSize += FreeSize;
1863 }
1864 else
1865 {
1866 /* Even more complex - the next entry is free, so we can merge them into one! */
1867 SplitBlock->Flags = SplitBlock2->Flags;
1868
1869 /* Remove that next entry */
1870 RtlpRemoveFreeBlock(Heap, SplitBlock2, FALSE, FALSE);
1871
1872 /* Update sizes */
1873 FreeSize += SplitBlock2->Size;
1874 Heap->TotalFreeSize -= SplitBlock2->Size;
1875
1876 if (FreeSize <= HEAP_MAX_BLOCK_SIZE)
1877 {
1878 /* Insert it back */
1879 SplitBlock->Size = FreeSize;
1880
1881 /* Don't forget to update previous size of the next entry! */
1882 if (!(SplitBlock->Flags & HEAP_ENTRY_LAST_ENTRY))
1883 {
1884 ((PHEAP_FREE_ENTRY)((PHEAP_ENTRY)SplitBlock + FreeSize))->PreviousSize = FreeSize;
1885 }
1886
1887 /* Actually insert it */
1888 RtlpInsertFreeBlockHelper(Heap, SplitBlock, (USHORT)FreeSize, FALSE);
1889
1890 /* Update total size */
1891 Heap->TotalFreeSize += FreeSize;
1892 }
1893 else
1894 {
1895 /* Resulting block is quite big */
1896 RtlpInsertFreeBlock(Heap, SplitBlock, FreeSize);
1897 }
1898 }
1899 }
1900
1901 /* Reset flags of the free entry */
1902 FreeFlags = 0;
1903
1904 /* Update last entry in segment */
1905 if (SplitBlock->Flags & HEAP_ENTRY_LAST_ENTRY)
1906 {
1907 Heap->Segments[SplitBlock->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)SplitBlock;
1908 }
1909 }
1910 }
1911
1912 /* Set last entry flag */
1913 if (FreeFlags & HEAP_ENTRY_LAST_ENTRY)
1914 InUseEntry->Flags |= HEAP_ENTRY_LAST_ENTRY;
1915
1916 return InUseEntry;
1917 }
1918
1919 PVOID NTAPI
1920 RtlpAllocateNonDedicated(PHEAP Heap,
1921 ULONG Flags,
1922 SIZE_T Size,
1923 SIZE_T AllocationSize,
1924 SIZE_T Index,
1925 BOOLEAN HeapLocked)
1926 {
1927 PLIST_ENTRY FreeListHead, Next;
1928 PHEAP_FREE_ENTRY FreeBlock;
1929 PHEAP_ENTRY InUseEntry;
1930 PHEAP_ENTRY_EXTRA Extra;
1931 EXCEPTION_RECORD ExceptionRecord;
1932
1933 /* Go through the zero list to find a place where to insert the new entry */
1934 FreeListHead = &Heap->FreeLists[0];
1935
1936 /* Start from the largest block to reduce time */
1937 Next = FreeListHead->Blink;
1938 if (FreeListHead != Next)
1939 {
1940 FreeBlock = CONTAINING_RECORD(Next, HEAP_FREE_ENTRY, FreeList);
1941
1942 if (FreeBlock->Size >= Index)
1943 {
1944 /* Our request is smaller than the largest entry in the zero list */
1945
1946 /* Go through the list to find insertion place */
1947 Next = FreeListHead->Flink;
1948 while (FreeListHead != Next)
1949 {
1950 FreeBlock = CONTAINING_RECORD(Next, HEAP_FREE_ENTRY, FreeList);
1951
1952 if (FreeBlock->Size >= Index)
1953 {
1954 /* Found minimally fitting entry. Proceed to either using it as it is
1955 or splitting it to two entries */
1956 RemoveEntryList(&FreeBlock->FreeList);
1957
1958 /* Split it */
1959 InUseEntry = RtlpSplitEntry(Heap, FreeBlock, AllocationSize, Index, Size);
1960
1961 /* Release the lock */
1962 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
1963
1964 /* Zero memory if that was requested */
1965 if (Flags & HEAP_ZERO_MEMORY)
1966 RtlZeroMemory(InUseEntry + 1, Size);
1967 else if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
1968 {
1969 /* Fill this block with a special pattern */
1970 RtlFillMemoryUlong(InUseEntry + 1, Size & ~0x3, ARENA_INUSE_FILLER);
1971 }
1972
1973 /* Fill tail of the block with a special pattern too if requested */
1974 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
1975 {
1976 RtlFillMemory((PCHAR)(InUseEntry + 1) + Size, sizeof(HEAP_ENTRY), HEAP_TAIL_FILL);
1977 InUseEntry->Flags |= HEAP_ENTRY_FILL_PATTERN;
1978 }
1979
1980 /* Prepare extra if it's present */
1981 if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
1982 {
1983 Extra = RtlpGetExtraStuffPointer(InUseEntry);
1984 RtlZeroMemory(Extra, sizeof(HEAP_ENTRY_EXTRA));
1985
1986 // TODO: Tagging
1987 }
1988
1989 /* Return pointer to the */
1990 return InUseEntry + 1;
1991 }
1992
1993 /* Advance to the next entry */
1994 Next = Next->Flink;
1995 }
1996 }
1997 }
1998
1999 /* Extend the heap, 0 list didn't have anything suitable */
2000 FreeBlock = RtlpExtendHeap(Heap, AllocationSize);
2001
2002 /* Use the new biggest entry we've got */
2003 if (FreeBlock)
2004 {
2005 RemoveEntryList(&FreeBlock->FreeList);
2006
2007 /* Split it */
2008 InUseEntry = RtlpSplitEntry(Heap, FreeBlock, AllocationSize, Index, Size);
2009
2010 /* Release the lock */
2011 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2012
2013 /* Zero memory if that was requested */
2014 if (Flags & HEAP_ZERO_MEMORY)
2015 RtlZeroMemory(InUseEntry + 1, Size);
2016 else if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
2017 {
2018 /* Fill this block with a special pattern */
2019 RtlFillMemoryUlong(InUseEntry + 1, Size & ~0x3, ARENA_INUSE_FILLER);
2020 }
2021
2022 /* Fill tail of the block with a special pattern too if requested */
2023 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
2024 {
2025 RtlFillMemory((PCHAR)(InUseEntry + 1) + Size, sizeof(HEAP_ENTRY), HEAP_TAIL_FILL);
2026 InUseEntry->Flags |= HEAP_ENTRY_FILL_PATTERN;
2027 }
2028
2029 /* Prepare extra if it's present */
2030 if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
2031 {
2032 Extra = RtlpGetExtraStuffPointer(InUseEntry);
2033 RtlZeroMemory(Extra, sizeof(HEAP_ENTRY_EXTRA));
2034
2035 // TODO: Tagging
2036 }
2037
2038 /* Return pointer to the */
2039 return InUseEntry + 1;
2040 }
2041
2042 /* Really unfortunate, out of memory condition */
2043 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_NO_MEMORY);
2044
2045 /* Generate an exception */
2046 if (Flags & HEAP_GENERATE_EXCEPTIONS)
2047 {
2048 ExceptionRecord.ExceptionCode = STATUS_NO_MEMORY;
2049 ExceptionRecord.ExceptionRecord = NULL;
2050 ExceptionRecord.NumberParameters = 1;
2051 ExceptionRecord.ExceptionFlags = 0;
2052 ExceptionRecord.ExceptionInformation[0] = AllocationSize;
2053
2054 RtlRaiseException(&ExceptionRecord);
2055 }
2056
2057 /* Release the lock */
2058 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2059 DPRINT1("HEAP: Allocation failed!\n");
2060 DPRINT1("Flags %x\n", Heap->Flags);
2061 return NULL;
2062 }
2063
2064 /***********************************************************************
2065 * HeapAlloc (KERNEL32.334)
2066 * RETURNS
2067 * Pointer to allocated memory block
2068 * NULL: Failure
2069 * 0x7d030f60--invalid flags in RtlHeapAllocate
2070 * @implemented
2071 */
2072 PVOID NTAPI
2073 RtlAllocateHeap(IN PVOID HeapPtr,
2074 IN ULONG Flags,
2075 IN SIZE_T Size)
2076 {
2077 PHEAP Heap = (PHEAP)HeapPtr;
2078 PULONG FreeListsInUse;
2079 ULONG FreeListsInUseUlong;
2080 SIZE_T AllocationSize;
2081 SIZE_T Index;
2082 PLIST_ENTRY FreeListHead;
2083 PHEAP_ENTRY InUseEntry;
2084 PHEAP_FREE_ENTRY FreeBlock;
2085 ULONG InUseIndex, i;
2086 UCHAR FreeFlags;
2087 EXCEPTION_RECORD ExceptionRecord;
2088 BOOLEAN HeapLocked = FALSE;
2089 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualBlock = NULL;
2090 PHEAP_ENTRY_EXTRA Extra;
2091 NTSTATUS Status;
2092
2093 /* Force flags */
2094 Flags |= Heap->ForceFlags;
2095
2096 /* Call special heap */
2097 if (RtlpHeapIsSpecial(Flags))
2098 return RtlDebugAllocateHeap(Heap, Flags, Size);
2099
2100 /* Check for the maximum size */
2101 if (Size >= 0x80000000)
2102 {
2103 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_NO_MEMORY);
2104 DPRINT1("HEAP: Allocation failed!\n");
2105 return NULL;
2106 }
2107
2108 if (Flags & (HEAP_CREATE_ENABLE_TRACING |
2109 HEAP_CREATE_ALIGN_16))
2110 {
2111 DPRINT1("HEAP: RtlAllocateHeap is called with unsupported flags %x, ignoring\n", Flags);
2112 }
2113
2114 //DPRINT("RtlAllocateHeap(%p %x %x)\n", Heap, Flags, Size);
2115
2116 /* Calculate allocation size and index */
2117 if (Size)
2118 AllocationSize = Size;
2119 else
2120 AllocationSize = 1;
2121 AllocationSize = (AllocationSize + Heap->AlignRound) & Heap->AlignMask;
2122 Index = AllocationSize >> HEAP_ENTRY_SHIFT;
2123
2124 /* Acquire the lock if necessary */
2125 if (!(Flags & HEAP_NO_SERIALIZE))
2126 {
2127 RtlEnterHeapLock(Heap->LockVariable);
2128 HeapLocked = TRUE;
2129 }
2130
2131 /* Depending on the size, the allocation is going to be done from dedicated,
2132 non-dedicated lists or a virtual block of memory */
2133 if (Index < HEAP_FREELISTS)
2134 {
2135 FreeListHead = &Heap->FreeLists[Index];
2136
2137 if (!IsListEmpty(FreeListHead))
2138 {
2139 /* There is a free entry in this list */
2140 FreeBlock = CONTAINING_RECORD(FreeListHead->Blink,
2141 HEAP_FREE_ENTRY,
2142 FreeList);
2143
2144 /* Save flags and remove the free entry */
2145 FreeFlags = FreeBlock->Flags;
2146 RtlpRemoveFreeBlock(Heap, FreeBlock, TRUE, FALSE);
2147
2148 /* Update the total free size of the heap */
2149 Heap->TotalFreeSize -= Index;
2150
2151 /* Initialize this block */
2152 InUseEntry = (PHEAP_ENTRY)FreeBlock;
2153 InUseEntry->Flags = HEAP_ENTRY_BUSY | (FreeFlags & HEAP_ENTRY_LAST_ENTRY);
2154 InUseEntry->UnusedBytes = AllocationSize - Size;
2155 InUseEntry->SmallTagIndex = 0;
2156 }
2157 else
2158 {
2159 /* Find smallest free block which this request could fit in */
2160 InUseIndex = Index >> 5;
2161 FreeListsInUse = &Heap->u.FreeListsInUseUlong[InUseIndex];
2162
2163 /* This bit magic disables all sizes which are less than the requested allocation size */
2164 FreeListsInUseUlong = *FreeListsInUse++ & ~((1 << ((ULONG)Index & 0x1f)) - 1);
2165
2166 /* If size is definitily more than our lists - go directly to the non-dedicated one */
2167 if (InUseIndex > 3)
2168 return RtlpAllocateNonDedicated(Heap, Flags, Size, AllocationSize, Index, HeapLocked);
2169
2170 /* Go through the list */
2171 for (i = InUseIndex; i < 4; i++)
2172 {
2173 if (FreeListsInUseUlong)
2174 {
2175 FreeListHead = &Heap->FreeLists[i * 32];
2176 break;
2177 }
2178
2179 if (i < 3) FreeListsInUseUlong = *FreeListsInUse++;
2180 }
2181
2182 /* Nothing found, search in the non-dedicated list */
2183 if (i == 4)
2184 return RtlpAllocateNonDedicated(Heap, Flags, Size, AllocationSize, Index, HeapLocked);
2185
2186 /* That list is found, now calculate exact block */
2187 FreeListHead += RtlpFindLeastSetBit(FreeListsInUseUlong);
2188
2189 /* Take this entry and remove it from the list of free blocks */
2190 FreeBlock = CONTAINING_RECORD(FreeListHead->Blink,
2191 HEAP_FREE_ENTRY,
2192 FreeList);
2193 RtlpRemoveFreeBlock(Heap, FreeBlock, TRUE, FALSE);
2194
2195 /* Split it */
2196 InUseEntry = RtlpSplitEntry(Heap, FreeBlock, AllocationSize, Index, Size);
2197 }
2198
2199 /* Release the lock */
2200 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2201
2202 /* Zero memory if that was requested */
2203 if (Flags & HEAP_ZERO_MEMORY)
2204 RtlZeroMemory(InUseEntry + 1, Size);
2205 else if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
2206 {
2207 /* Fill this block with a special pattern */
2208 RtlFillMemoryUlong(InUseEntry + 1, Size & ~0x3, ARENA_INUSE_FILLER);
2209 }
2210
2211 /* Fill tail of the block with a special pattern too if requested */
2212 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
2213 {
2214 RtlFillMemory((PCHAR)(InUseEntry + 1) + Size, sizeof(HEAP_ENTRY), HEAP_TAIL_FILL);
2215 InUseEntry->Flags |= HEAP_ENTRY_FILL_PATTERN;
2216 }
2217
2218 /* Prepare extra if it's present */
2219 if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
2220 {
2221 Extra = RtlpGetExtraStuffPointer(InUseEntry);
2222 RtlZeroMemory(Extra, sizeof(HEAP_ENTRY_EXTRA));
2223
2224 // TODO: Tagging
2225 }
2226
2227 /* User data starts right after the entry's header */
2228 return InUseEntry + 1;
2229 }
2230 else if (Index <= Heap->VirtualMemoryThreshold)
2231 {
2232 /* The block is too large for dedicated lists, but fine for a non-dedicated one */
2233 return RtlpAllocateNonDedicated(Heap, Flags, Size, AllocationSize, Index, HeapLocked);
2234 }
2235 else if (Heap->Flags & HEAP_GROWABLE)
2236 {
2237 /* We've got a very big allocation request, satisfy it by directly allocating virtual memory */
2238 AllocationSize += sizeof(HEAP_VIRTUAL_ALLOC_ENTRY) - sizeof(HEAP_ENTRY);
2239
2240 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
2241 (PVOID *)&VirtualBlock,
2242 0,
2243 &AllocationSize,
2244 MEM_COMMIT,
2245 PAGE_READWRITE);
2246
2247 if (!NT_SUCCESS(Status))
2248 {
2249 // Set STATUS!
2250 /* Release the lock */
2251 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2252 DPRINT1("HEAP: Allocation failed!\n");
2253 return NULL;
2254 }
2255
2256 /* Initialize the newly allocated block */
2257 VirtualBlock->BusyBlock.Size = (AllocationSize - Size);
2258 VirtualBlock->BusyBlock.Flags = HEAP_ENTRY_VIRTUAL_ALLOC | HEAP_ENTRY_EXTRA_PRESENT | HEAP_ENTRY_BUSY;
2259 VirtualBlock->CommitSize = AllocationSize;
2260 VirtualBlock->ReserveSize = AllocationSize;
2261
2262 /* Insert it into the list of virtual allocations */
2263 InsertTailList(&Heap->VirtualAllocdBlocks, &VirtualBlock->Entry);
2264
2265 /* Release the lock */
2266 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2267
2268 /* Return pointer to user data */
2269 return VirtualBlock + 1;
2270 }
2271
2272 /* Generate an exception */
2273 if (Flags & HEAP_GENERATE_EXCEPTIONS)
2274 {
2275 ExceptionRecord.ExceptionCode = STATUS_NO_MEMORY;
2276 ExceptionRecord.ExceptionRecord = NULL;
2277 ExceptionRecord.NumberParameters = 1;
2278 ExceptionRecord.ExceptionFlags = 0;
2279 ExceptionRecord.ExceptionInformation[0] = AllocationSize;
2280
2281 RtlRaiseException(&ExceptionRecord);
2282 }
2283
2284 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_BUFFER_TOO_SMALL);
2285
2286 /* Release the lock */
2287 if (HeapLocked) RtlLeaveHeapLock(Heap->LockVariable);
2288 DPRINT1("HEAP: Allocation failed!\n");
2289 return NULL;
2290 }
2291
2292
2293 /***********************************************************************
2294 * HeapFree (KERNEL32.338)
2295 * RETURNS
2296 * TRUE: Success
2297 * FALSE: Failure
2298 *
2299 * @implemented
2300 */
2301 BOOLEAN NTAPI RtlFreeHeap(
2302 HANDLE HeapPtr, /* [in] Handle of heap */
2303 ULONG Flags, /* [in] Heap freeing flags */
2304 PVOID Ptr /* [in] Address of memory to free */
2305 )
2306 {
2307 PHEAP Heap;
2308 PHEAP_ENTRY HeapEntry;
2309 USHORT TagIndex = 0;
2310 SIZE_T BlockSize;
2311 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualEntry;
2312 BOOLEAN Locked = FALSE;
2313 NTSTATUS Status;
2314
2315 /* Freeing NULL pointer is a legal operation */
2316 if (!Ptr) return TRUE;
2317
2318 /* Get pointer to the heap and force flags */
2319 Heap = (PHEAP)HeapPtr;
2320 Flags |= Heap->ForceFlags;
2321
2322 /* Call special heap */
2323 if (RtlpHeapIsSpecial(Flags))
2324 return RtlDebugFreeHeap(Heap, Flags, Ptr);
2325
2326 /* Lock if necessary */
2327 if (!(Flags & HEAP_NO_SERIALIZE))
2328 {
2329 RtlEnterHeapLock(Heap->LockVariable);
2330 Locked = TRUE;
2331 }
2332
2333 /* Get pointer to the heap entry */
2334 HeapEntry = (PHEAP_ENTRY)Ptr - 1;
2335
2336 /* Check this entry, fail if it's invalid */
2337 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY) ||
2338 (((ULONG_PTR)Ptr & 0x7) != 0) ||
2339 (HeapEntry->SegmentOffset >= HEAP_SEGMENTS))
2340 {
2341 /* This is an invalid block */
2342 DPRINT1("HEAP: Trying to free an invalid address %p!\n", Ptr);
2343 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
2344
2345 /* Release the heap lock */
2346 if (Locked) RtlLeaveHeapLock(Heap->LockVariable);
2347 return FALSE;
2348 }
2349
2350 if (HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
2351 {
2352 /* Big allocation */
2353 VirtualEntry = CONTAINING_RECORD(HeapEntry, HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
2354
2355 /* Remove it from the list */
2356 RemoveEntryList(&VirtualEntry->Entry);
2357
2358 // TODO: Tagging
2359
2360 BlockSize = 0;
2361 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
2362 (PVOID *)&VirtualEntry,
2363 &BlockSize,
2364 MEM_RELEASE);
2365
2366 if (!NT_SUCCESS(Status))
2367 {
2368 DPRINT1("HEAP: Failed releasing memory with Status 0x%08X. Heap %p, ptr %p, base address %p\n",
2369 Status, Heap, Ptr, VirtualEntry);
2370 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(Status);
2371 }
2372 }
2373 else
2374 {
2375 /* Normal allocation */
2376 BlockSize = HeapEntry->Size;
2377
2378 // TODO: Tagging
2379
2380 /* Coalesce in kernel mode, and in usermode if it's not disabled */
2381 if (RtlpGetMode() == KernelMode ||
2382 (RtlpGetMode() == UserMode && !(Heap->Flags & HEAP_DISABLE_COALESCE_ON_FREE)))
2383 {
2384 HeapEntry = (PHEAP_ENTRY)RtlpCoalesceFreeBlocks(Heap,
2385 (PHEAP_FREE_ENTRY)HeapEntry,
2386 &BlockSize,
2387 FALSE);
2388 }
2389
2390 /* If there is no need to decommit the block - put it into a free list */
2391 if (BlockSize < Heap->DeCommitFreeBlockThreshold ||
2392 (Heap->TotalFreeSize + BlockSize < Heap->DeCommitTotalFreeThreshold))
2393 {
2394 /* Check if it needs to go to a 0 list */
2395 if (BlockSize > HEAP_MAX_BLOCK_SIZE)
2396 {
2397 /* General-purpose 0 list */
2398 RtlpInsertFreeBlock(Heap, (PHEAP_FREE_ENTRY)HeapEntry, BlockSize);
2399 }
2400 else
2401 {
2402 /* Usual free list */
2403 RtlpInsertFreeBlockHelper(Heap, (PHEAP_FREE_ENTRY)HeapEntry, BlockSize, FALSE);
2404
2405 /* Assert sizes are consistent */
2406 if (!(HeapEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
2407 {
2408 ASSERT((HeapEntry + BlockSize)->PreviousSize == BlockSize);
2409 }
2410
2411 /* Increase the free size */
2412 Heap->TotalFreeSize += BlockSize;
2413 }
2414
2415
2416 if (RtlpGetMode() == UserMode &&
2417 TagIndex != 0)
2418 {
2419 // FIXME: Tagging
2420 UNIMPLEMENTED;
2421 }
2422 }
2423 else
2424 {
2425 /* Decommit this block */
2426 RtlpDeCommitFreeBlock(Heap, (PHEAP_FREE_ENTRY)HeapEntry, BlockSize);
2427 }
2428 }
2429
2430 /* Release the heap lock */
2431 if (Locked) RtlLeaveHeapLock(Heap->LockVariable);
2432
2433 return TRUE;
2434 }
2435
2436 BOOLEAN NTAPI
2437 RtlpGrowBlockInPlace (IN PHEAP Heap,
2438 IN ULONG Flags,
2439 IN PHEAP_ENTRY InUseEntry,
2440 IN SIZE_T Size,
2441 IN SIZE_T Index)
2442 {
2443 UCHAR EntryFlags, RememberFlags;
2444 PHEAP_FREE_ENTRY FreeEntry, UnusedEntry, FollowingEntry;
2445 SIZE_T FreeSize, PrevSize, TailPart, AddedSize = 0;
2446 PHEAP_ENTRY_EXTRA OldExtra, NewExtra;
2447
2448 /* We can't grow beyond specified threshold */
2449 if (Index > Heap->VirtualMemoryThreshold)
2450 return FALSE;
2451
2452 /* Get entry flags */
2453 EntryFlags = InUseEntry->Flags;
2454
2455 /* Get the next free entry */
2456 FreeEntry = (PHEAP_FREE_ENTRY)(InUseEntry + InUseEntry->Size);
2457
2458 if (EntryFlags & HEAP_ENTRY_LAST_ENTRY)
2459 {
2460 /* There is no next block, just uncommitted space. Calculate how much is needed */
2461 FreeSize = (Index - InUseEntry->Size) << HEAP_ENTRY_SHIFT;
2462 FreeSize = ROUND_UP(FreeSize, PAGE_SIZE);
2463
2464 /* Find and commit those pages */
2465 FreeEntry = RtlpFindAndCommitPages(Heap,
2466 Heap->Segments[InUseEntry->SegmentOffset],
2467 &FreeSize,
2468 FreeEntry);
2469
2470 /* Fail if it failed... */
2471 if (!FreeEntry) return FALSE;
2472
2473 /* It was successful, perform coalescing */
2474 FreeSize = FreeSize >> HEAP_ENTRY_SHIFT;
2475 FreeEntry = RtlpCoalesceFreeBlocks(Heap, FreeEntry, &FreeSize, FALSE);
2476
2477 /* Check if it's enough */
2478 if (FreeSize + InUseEntry->Size < Index)
2479 {
2480 /* Still not enough */
2481 RtlpInsertFreeBlock(Heap, FreeEntry, FreeSize);
2482 Heap->TotalFreeSize += FreeSize;
2483 return FALSE;
2484 }
2485
2486 /* Remember flags of this free entry */
2487 RememberFlags = FreeEntry->Flags;
2488
2489 /* Sum up sizes */
2490 FreeSize += InUseEntry->Size;
2491 }
2492 else
2493 {
2494 /* The next block indeed exists. Check if it's free or in use */
2495 if (FreeEntry->Flags & HEAP_ENTRY_BUSY) return FALSE;
2496
2497 /* Next entry is free, check if it can fit the block we need */
2498 FreeSize = InUseEntry->Size + FreeEntry->Size;
2499 if (FreeSize < Index) return FALSE;
2500
2501 /* Remember flags of this free entry */
2502 RememberFlags = FreeEntry->Flags;
2503
2504 /* Remove this block from the free list */
2505 RtlpRemoveFreeBlock(Heap, FreeEntry, FALSE, FALSE);
2506 Heap->TotalFreeSize -= FreeEntry->Size;
2507 }
2508
2509 PrevSize = (InUseEntry->Size << HEAP_ENTRY_SHIFT) - InUseEntry->UnusedBytes;
2510 FreeSize -= Index;
2511
2512 /* Don't produce too small blocks */
2513 if (FreeSize <= 2)
2514 {
2515 Index += FreeSize;
2516 FreeSize = 0;
2517 }
2518
2519 /* Process extra stuff */
2520 if (RememberFlags & HEAP_ENTRY_EXTRA_PRESENT)
2521 {
2522 /* Calculate pointers */
2523 OldExtra = (PHEAP_ENTRY_EXTRA)(InUseEntry + InUseEntry->Size - 1);
2524 NewExtra = (PHEAP_ENTRY_EXTRA)(InUseEntry + Index - 1);
2525
2526 /* Copy contents */
2527 *NewExtra = *OldExtra;
2528
2529 // FIXME Tagging
2530 }
2531
2532 /* Update sizes */
2533 InUseEntry->Size = Index;
2534 InUseEntry->UnusedBytes = ((Index << HEAP_ENTRY_SHIFT) - Size);
2535
2536 /* Check if there is a free space remaining after merging those blocks */
2537 if (!FreeSize)
2538 {
2539 /* Update flags and sizes */
2540 InUseEntry->Flags |= RememberFlags & HEAP_ENTRY_LAST_ENTRY;
2541
2542 /* Either update previous size of the next entry or mark it as a last
2543 entry in the segment*/
2544 if (RememberFlags & HEAP_ENTRY_LAST_ENTRY)
2545 Heap->Segments[InUseEntry->SegmentOffset]->LastEntryInSegment = InUseEntry;
2546 else
2547 (InUseEntry + InUseEntry->Size)->PreviousSize = InUseEntry->Size;
2548 }
2549 else
2550 {
2551 /* Complex case, we need to split the block to give unused free space
2552 back to the heap */
2553 UnusedEntry = (PHEAP_FREE_ENTRY)(InUseEntry + Index);
2554 UnusedEntry->PreviousSize = Index;
2555 UnusedEntry->SegmentOffset = InUseEntry->SegmentOffset;
2556
2557 /* Update the following block or set the last entry in the segment */
2558 if (RememberFlags & HEAP_ENTRY_LAST_ENTRY)
2559 {
2560 /* Set last entry and set flags and size */
2561 Heap->Segments[InUseEntry->SegmentOffset]->LastEntryInSegment = InUseEntry;
2562 UnusedEntry->Flags = RememberFlags;
2563 UnusedEntry->Size = FreeSize;
2564
2565 /* Insert it to the heap and update total size */
2566 RtlpInsertFreeBlockHelper(Heap, UnusedEntry, FreeSize, FALSE);
2567 Heap->TotalFreeSize += FreeSize;
2568 }
2569 else
2570 {
2571 /* There is a block after this one */
2572 FollowingEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)UnusedEntry + FreeSize);
2573
2574 if (FollowingEntry->Flags & HEAP_ENTRY_BUSY)
2575 {
2576 /* Update flags and set size of the unused space entry */
2577 UnusedEntry->Flags = RememberFlags & (~HEAP_ENTRY_LAST_ENTRY);
2578 UnusedEntry->Size = FreeSize;
2579
2580 /* Update previous size of the following entry */
2581 FollowingEntry->PreviousSize = FreeSize;
2582
2583 /* Insert it to the heap and update total free size */
2584 RtlpInsertFreeBlockHelper(Heap, UnusedEntry, FreeSize, FALSE);
2585 Heap->TotalFreeSize += FreeSize;
2586 }
2587 else
2588 {
2589 /* That following entry is also free, what a fortune! */
2590 RememberFlags = FollowingEntry->Flags;
2591
2592 /* Remove it */
2593 RtlpRemoveFreeBlock(Heap, FollowingEntry, FALSE, FALSE);
2594 Heap->TotalFreeSize -= FollowingEntry->Size;
2595
2596 /* And make up a new combined block */
2597 FreeSize += FollowingEntry->Size;
2598 UnusedEntry->Flags = RememberFlags;
2599
2600 /* Check where to put it */
2601 if (FreeSize <= HEAP_MAX_BLOCK_SIZE)
2602 {
2603 /* Fine for a dedicated list */
2604 UnusedEntry->Size = FreeSize;
2605
2606 if (RememberFlags & HEAP_ENTRY_LAST_ENTRY)
2607 Heap->Segments[UnusedEntry->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)UnusedEntry;
2608 else
2609 ((PHEAP_ENTRY)UnusedEntry + FreeSize)->PreviousSize = FreeSize;
2610
2611 /* Insert it back and update total size */
2612 RtlpInsertFreeBlockHelper(Heap, UnusedEntry, FreeSize, FALSE);
2613 Heap->TotalFreeSize += FreeSize;
2614 }
2615 else
2616 {
2617 /* The block is very large, leave all the hassle to the insertion routine */
2618 RtlpInsertFreeBlock(Heap, UnusedEntry, FreeSize);
2619 }
2620 }
2621 }
2622 }
2623
2624 /* Copy user settable flags */
2625 InUseEntry->Flags &= ~HEAP_ENTRY_SETTABLE_FLAGS;
2626 InUseEntry->Flags |= ((Flags & HEAP_SETTABLE_USER_FLAGS) >> 4);
2627
2628 /* Properly "zero out" (and fill!) the space */
2629 if (Flags & HEAP_ZERO_MEMORY)
2630 {
2631 RtlZeroMemory((PCHAR)(InUseEntry + 1) + PrevSize, Size - PrevSize);
2632 }
2633 else if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
2634 {
2635 /* Calculate tail part which we need to fill */
2636 TailPart = PrevSize & (sizeof(ULONG) - 1);
2637
2638 /* "Invert" it as usual */
2639 if (TailPart) TailPart = 4 - TailPart;
2640
2641 if (Size > (PrevSize + TailPart))
2642 AddedSize = (Size - (PrevSize + TailPart)) & ~(sizeof(ULONG) - 1);
2643
2644 if (AddedSize)
2645 {
2646 RtlFillMemoryUlong((PCHAR)(InUseEntry + 1) + PrevSize + TailPart,
2647 AddedSize,
2648 ARENA_INUSE_FILLER);
2649 }
2650 }
2651
2652 /* Fill the new tail */
2653 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
2654 {
2655 RtlFillMemory((PCHAR)(InUseEntry + 1) + Size,
2656 HEAP_ENTRY_SIZE,
2657 HEAP_TAIL_FILL);
2658 }
2659
2660 /* Return success */
2661 return TRUE;
2662 }
2663
2664 PHEAP_ENTRY_EXTRA NTAPI
2665 RtlpGetExtraStuffPointer(PHEAP_ENTRY HeapEntry)
2666 {
2667 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualEntry;
2668
2669 /* Check if it's a big block */
2670 if (HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
2671 {
2672 VirtualEntry = CONTAINING_RECORD(HeapEntry, HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
2673
2674 /* Return a pointer to the extra stuff*/
2675 return &VirtualEntry->ExtraStuff;
2676 }
2677 else
2678 {
2679 /* This is a usual entry, which means extra stuff follows this block */
2680 return (PHEAP_ENTRY_EXTRA)(HeapEntry + HeapEntry->Size - 1);
2681 }
2682 }
2683
2684
2685 /***********************************************************************
2686 * RtlReAllocateHeap
2687 * PARAMS
2688 * Heap [in] Handle of heap block
2689 * Flags [in] Heap reallocation flags
2690 * Ptr, [in] Address of memory to reallocate
2691 * Size [in] Number of bytes to reallocate
2692 *
2693 * RETURNS
2694 * Pointer to reallocated memory block
2695 * NULL: Failure
2696 * 0x7d030f60--invalid flags in RtlHeapAllocate
2697 * @implemented
2698 */
2699 PVOID NTAPI
2700 RtlReAllocateHeap(HANDLE HeapPtr,
2701 ULONG Flags,
2702 PVOID Ptr,
2703 SIZE_T Size)
2704 {
2705 PHEAP Heap = (PHEAP)HeapPtr;
2706 PHEAP_ENTRY InUseEntry, NewInUseEntry;
2707 PHEAP_ENTRY_EXTRA OldExtra, NewExtra;
2708 SIZE_T AllocationSize, FreeSize, DecommitSize;
2709 BOOLEAN HeapLocked = FALSE;
2710 PVOID NewBaseAddress;
2711 PHEAP_FREE_ENTRY SplitBlock, SplitBlock2;
2712 SIZE_T OldSize, Index, OldIndex;
2713 UCHAR FreeFlags;
2714 NTSTATUS Status;
2715 PVOID DecommitBase;
2716 SIZE_T RemainderBytes, ExtraSize;
2717 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualAllocBlock;
2718 EXCEPTION_RECORD ExceptionRecord;
2719
2720 /* Return success in case of a null pointer */
2721 if (!Ptr)
2722 {
2723 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_SUCCESS);
2724 return NULL;
2725 }
2726
2727 /* Force heap flags */
2728 Flags |= Heap->ForceFlags;
2729
2730 /* Call special heap */
2731 if (RtlpHeapIsSpecial(Flags))
2732 return RtlDebugReAllocateHeap(Heap, Flags, Ptr, Size);
2733
2734 /* Make sure size is valid */
2735 if (Size >= 0x80000000)
2736 {
2737 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_NO_MEMORY);
2738 return NULL;
2739 }
2740
2741 /* Calculate allocation size and index */
2742 if (Size)
2743 AllocationSize = Size;
2744 else
2745 AllocationSize = 1;
2746 AllocationSize = (AllocationSize + Heap->AlignRound) & Heap->AlignMask;
2747
2748 /* Add up extra stuff, if it is present anywhere */
2749 if (((((PHEAP_ENTRY)Ptr)-1)->Flags & HEAP_ENTRY_EXTRA_PRESENT) ||
2750 (Flags & HEAP_EXTRA_FLAGS_MASK) ||
2751 Heap->PseudoTagEntries)
2752 {
2753 AllocationSize += sizeof(HEAP_ENTRY_EXTRA);
2754 }
2755
2756 /* Acquire the lock if necessary */
2757 if (!(Flags & HEAP_NO_SERIALIZE))
2758 {
2759 RtlEnterHeapLock(Heap->LockVariable);
2760 HeapLocked = TRUE;
2761 Flags ^= HEAP_NO_SERIALIZE;
2762 }
2763
2764 /* Get the pointer to the in-use entry */
2765 InUseEntry = (PHEAP_ENTRY)Ptr - 1;
2766
2767 /* If that entry is not really in-use, we have a problem */
2768 if (!(InUseEntry->Flags & HEAP_ENTRY_BUSY))
2769 {
2770 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
2771
2772 /* Release the lock and return */
2773 if (HeapLocked)
2774 RtlLeaveHeapLock(Heap->LockVariable);
2775 return Ptr;
2776 }
2777
2778 if (InUseEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
2779 {
2780 /* This is a virtually allocated block. Get its size */
2781 OldSize = RtlpGetSizeOfBigBlock(InUseEntry);
2782
2783 /* Convert it to an index */
2784 OldIndex = (OldSize + InUseEntry->Size) >> HEAP_ENTRY_SHIFT;
2785
2786 /* Calculate new allocation size and round it to the page size */
2787 AllocationSize += FIELD_OFFSET(HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
2788 AllocationSize = ROUND_UP(AllocationSize, PAGE_SIZE);
2789 }
2790 else
2791 {
2792 /* Usual entry */
2793 OldIndex = InUseEntry->Size;
2794
2795 OldSize = (OldIndex << HEAP_ENTRY_SHIFT) - InUseEntry->UnusedBytes;
2796 }
2797
2798 /* Calculate new index */
2799 Index = AllocationSize >> HEAP_ENTRY_SHIFT;
2800
2801 /* Check for 4 different scenarios (old size, new size, old index, new index) */
2802 if (Index <= OldIndex)
2803 {
2804 /* Difference must be greater than 1, adjust if it's not so */
2805 if (Index + 1 == OldIndex)
2806 {
2807 Index++;
2808 AllocationSize += sizeof(HEAP_ENTRY);
2809 }
2810
2811 /* Calculate new size */
2812 if (InUseEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
2813 {
2814 /* Simple in case of a virtual alloc - just an unused size */
2815 InUseEntry->Size = AllocationSize - Size;
2816 }
2817 else if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
2818 {
2819 /* There is extra stuff, take it into account */
2820 OldExtra = (PHEAP_ENTRY_EXTRA)(InUseEntry + InUseEntry->Size - 1);
2821 NewExtra = (PHEAP_ENTRY_EXTRA)(InUseEntry + Index - 1);
2822 *NewExtra = *OldExtra;
2823
2824 // FIXME Tagging, TagIndex
2825
2826 /* Update unused bytes count */
2827 InUseEntry->UnusedBytes = AllocationSize - Size;
2828 }
2829 else
2830 {
2831 // FIXME Tagging, SmallTagIndex
2832 InUseEntry->UnusedBytes = AllocationSize - Size;
2833 }
2834
2835 /* If new size is bigger than the old size */
2836 if (Size > OldSize)
2837 {
2838 /* Zero out that additional space if required */
2839 if (Flags & HEAP_ZERO_MEMORY)
2840 {
2841 RtlZeroMemory((PCHAR)Ptr + OldSize, Size - OldSize);
2842 }
2843 else if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
2844 {
2845 /* Fill it on free if required */
2846 RemainderBytes = OldSize & (sizeof(ULONG) - 1);
2847
2848 if (RemainderBytes)
2849 RemainderBytes = 4 - RemainderBytes;
2850
2851 if (Size > (OldSize + RemainderBytes))
2852 {
2853 /* Calculate actual amount of extra bytes to fill */
2854 ExtraSize = (Size - (OldSize + RemainderBytes)) & ~(sizeof(ULONG) - 1);
2855
2856 /* Fill them if there are any */
2857 if (ExtraSize != 0)
2858 {
2859 RtlFillMemoryUlong((PCHAR)(InUseEntry + 1) + OldSize + RemainderBytes,
2860 ExtraSize,
2861 ARENA_INUSE_FILLER);
2862 }
2863 }
2864 }
2865 }
2866
2867 /* Fill tail of the heap entry if required */
2868 if (Heap->Flags & HEAP_TAIL_CHECKING_ENABLED)
2869 {
2870 RtlFillMemory((PCHAR)(InUseEntry + 1) + Size,
2871 HEAP_ENTRY_SIZE,
2872 HEAP_TAIL_FILL);
2873 }
2874
2875 /* Check if the difference is significant or not */
2876 if (Index != OldIndex)
2877 {
2878 /* Save flags */
2879 FreeFlags = InUseEntry->Flags & ~HEAP_ENTRY_BUSY;
2880
2881 if (FreeFlags & HEAP_ENTRY_VIRTUAL_ALLOC)
2882 {
2883 /* This is a virtual block allocation */
2884 VirtualAllocBlock = CONTAINING_RECORD(InUseEntry, HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
2885
2886 // FIXME Tagging!
2887
2888 DecommitBase = (PCHAR)VirtualAllocBlock + AllocationSize;
2889 DecommitSize = (OldIndex << HEAP_ENTRY_SHIFT) - AllocationSize;
2890
2891 /* Release the memory */
2892 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
2893 (PVOID *)&DecommitBase,
2894 &DecommitSize,
2895 MEM_RELEASE);
2896
2897 if (!NT_SUCCESS(Status))
2898 {
2899 DPRINT1("HEAP: Unable to release memory (pointer %p, size 0x%x), Status %08x\n", DecommitBase, DecommitSize, Status);
2900 }
2901 else
2902 {
2903 /* Otherwise reduce the commit size */
2904 VirtualAllocBlock->CommitSize -= DecommitSize;
2905 }
2906 }
2907 else
2908 {
2909 /* Reduce size of the block and possibly split it */
2910 SplitBlock = (PHEAP_FREE_ENTRY)(InUseEntry + Index);
2911
2912 /* Initialize this entry */
2913 SplitBlock->Flags = FreeFlags;
2914 SplitBlock->PreviousSize = Index;
2915 SplitBlock->SegmentOffset = InUseEntry->SegmentOffset;
2916
2917 /* Remember free size */
2918 FreeSize = InUseEntry->Size - Index;
2919
2920 /* Set new size */
2921 InUseEntry->Size = Index;
2922 InUseEntry->Flags &= ~HEAP_ENTRY_LAST_ENTRY;
2923
2924 /* Is that the last entry */
2925 if (FreeFlags & HEAP_ENTRY_LAST_ENTRY)
2926 {
2927 /* Update segment's last entry */
2928 Heap->Segments[SplitBlock->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)SplitBlock;
2929
2930 /* Set its size and insert it to the list */
2931 SplitBlock->Size = (USHORT)FreeSize;
2932 RtlpInsertFreeBlockHelper(Heap, SplitBlock, FreeSize, FALSE);
2933
2934 /* Update total free size */
2935 Heap->TotalFreeSize += FreeSize;
2936 }
2937 else
2938 {
2939 /* Get the block after that one */
2940 SplitBlock2 = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)SplitBlock + FreeSize);
2941
2942 if (SplitBlock2->Flags & HEAP_ENTRY_BUSY)
2943 {
2944 /* It's in use, add it here*/
2945 SplitBlock->Size = (USHORT)FreeSize;
2946
2947 /* Update previous size of the next entry */
2948 ((PHEAP_FREE_ENTRY)((PHEAP_ENTRY)SplitBlock + FreeSize))->PreviousSize = (USHORT)FreeSize;
2949
2950 /* Insert it to the list */
2951 RtlpInsertFreeBlockHelper(Heap, SplitBlock, FreeSize, FALSE);
2952
2953 /* Update total size */
2954 Heap->TotalFreeSize += FreeSize;
2955 }
2956 else
2957 {
2958 /* Next entry is free, so merge with it */
2959 SplitBlock->Flags = SplitBlock2->Flags;
2960
2961 /* Remove it, update total size */
2962 RtlpRemoveFreeBlock(Heap, SplitBlock2, FALSE, FALSE);
2963 Heap->TotalFreeSize -= SplitBlock2->Size;
2964
2965 /* Calculate total free size */
2966 FreeSize += SplitBlock2->Size;
2967
2968 if (FreeSize <= HEAP_MAX_BLOCK_SIZE)
2969 {
2970 SplitBlock->Size = FreeSize;
2971
2972 if (!(SplitBlock->Flags & HEAP_ENTRY_LAST_ENTRY))
2973 {
2974 /* Update previous size of the next entry */
2975 ((PHEAP_FREE_ENTRY)((PHEAP_ENTRY)SplitBlock + FreeSize))->PreviousSize = FreeSize;
2976 }
2977 else
2978 {
2979 Heap->Segments[SplitBlock->SegmentOffset]->LastEntryInSegment = (PHEAP_ENTRY)SplitBlock;
2980 }
2981
2982 /* Insert the new one back and update total size */
2983 RtlpInsertFreeBlockHelper(Heap, SplitBlock, FreeSize, FALSE);
2984 Heap->TotalFreeSize += FreeSize;
2985 }
2986 else
2987 {
2988 /* Just add it */
2989 RtlpInsertFreeBlock(Heap, SplitBlock, FreeSize);
2990 }
2991 }
2992 }
2993 }
2994 }
2995 }
2996 else
2997 {
2998 /* We're growing the block */
2999 if ((InUseEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC) ||
3000 !RtlpGrowBlockInPlace(Heap, Flags, InUseEntry, Size, Index))
3001 {
3002 /* Growing in place failed, so growing out of place */
3003 if (Flags & HEAP_REALLOC_IN_PLACE_ONLY)
3004 {
3005 DPRINT1("Realloc in place failed, but it was the only option\n");
3006 Ptr = NULL;
3007 }
3008 else
3009 {
3010 /* Clear tag bits */
3011 Flags &= ~HEAP_TAG_MASK;
3012
3013 /* Process extra stuff */
3014 if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
3015 {
3016 /* Preserve user settable flags */
3017 Flags &= ~HEAP_SETTABLE_USER_FLAGS;
3018
3019 Flags |= HEAP_SETTABLE_USER_VALUE | ((InUseEntry->Flags & HEAP_ENTRY_SETTABLE_FLAGS) << 4);
3020
3021 /* Get pointer to the old extra data */
3022 OldExtra = RtlpGetExtraStuffPointer(InUseEntry);
3023
3024 /* Save tag index if it was set */
3025 if (OldExtra->TagIndex &&
3026 !(OldExtra->TagIndex & HEAP_PSEUDO_TAG_FLAG))
3027 {
3028 Flags |= OldExtra->TagIndex << HEAP_TAG_SHIFT;
3029 }
3030 }
3031 else if (InUseEntry->SmallTagIndex)
3032 {
3033 /* Take small tag index into account */
3034 Flags |= InUseEntry->SmallTagIndex << HEAP_TAG_SHIFT;
3035 }
3036
3037 /* Allocate new block from the heap */
3038 NewBaseAddress = RtlAllocateHeap(HeapPtr,
3039 Flags & ~HEAP_ZERO_MEMORY,
3040 Size);
3041
3042 /* Proceed if it didn't fail */
3043 if (NewBaseAddress)
3044 {
3045 /* Get new entry pointer */
3046 NewInUseEntry = (PHEAP_ENTRY)NewBaseAddress - 1;
3047
3048 /* Process extra stuff if it exists */
3049 if (NewInUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
3050 {
3051 NewExtra = RtlpGetExtraStuffPointer(NewInUseEntry);
3052
3053 if (InUseEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
3054 {
3055 OldExtra = RtlpGetExtraStuffPointer(InUseEntry);
3056 NewExtra->Settable = OldExtra->Settable;
3057 }
3058 else
3059 {
3060 RtlZeroMemory(NewExtra, sizeof(*NewExtra));
3061 }
3062 }
3063
3064 /* Copy actual user bits */
3065 if (Size < OldSize)
3066 RtlMoveMemory(NewBaseAddress, Ptr, Size);
3067 else
3068 RtlMoveMemory(NewBaseAddress, Ptr, OldSize);
3069
3070 /* Zero remaining part if required */
3071 if (Size > OldSize &&
3072 (Flags & HEAP_ZERO_MEMORY))
3073 {
3074 RtlZeroMemory((PCHAR)NewBaseAddress + OldSize, Size - OldSize);
3075 }
3076
3077 /* Free the old block */
3078 RtlFreeHeap(HeapPtr, Flags, Ptr);
3079 }
3080
3081 Ptr = NewBaseAddress;
3082 }
3083 }
3084 }
3085
3086 /* Did resizing fail? */
3087 if (!Ptr && (Flags & HEAP_GENERATE_EXCEPTIONS))
3088 {
3089 /* Generate an exception if required */
3090 ExceptionRecord.ExceptionCode = STATUS_NO_MEMORY;
3091 ExceptionRecord.ExceptionRecord = NULL;
3092 ExceptionRecord.NumberParameters = 1;
3093 ExceptionRecord.ExceptionFlags = 0;
3094 ExceptionRecord.ExceptionInformation[0] = AllocationSize;
3095
3096 RtlRaiseException(&ExceptionRecord);
3097 }
3098
3099 /* Release the heap lock if it was acquired */
3100 if (HeapLocked)
3101 RtlLeaveHeapLock(Heap->LockVariable);
3102
3103 return Ptr;
3104 }
3105
3106
3107 /***********************************************************************
3108 * RtlCompactHeap
3109 *
3110 * @unimplemented
3111 */
3112 ULONG NTAPI
3113 RtlCompactHeap(HANDLE Heap,
3114 ULONG Flags)
3115 {
3116 UNIMPLEMENTED;
3117 return 0;
3118 }
3119
3120
3121 /***********************************************************************
3122 * RtlLockHeap
3123 * Attempts to acquire the critical section object for a specified heap.
3124 *
3125 * PARAMS
3126 * Heap [in] Handle of heap to lock for exclusive access
3127 *
3128 * RETURNS
3129 * TRUE: Success
3130 * FALSE: Failure
3131 *
3132 * @implemented
3133 */
3134 BOOLEAN NTAPI
3135 RtlLockHeap(IN HANDLE HeapPtr)
3136 {
3137 PHEAP Heap = (PHEAP)HeapPtr;
3138
3139 // FIXME Check for special heap
3140
3141 /* Check if it's really a heap */
3142 if (Heap->Signature != HEAP_SIGNATURE) return FALSE;
3143
3144 /* Lock if it's lockable */
3145 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
3146 {
3147 RtlEnterHeapLock(Heap->LockVariable);
3148 }
3149
3150 return TRUE;
3151 }
3152
3153
3154 /***********************************************************************
3155 * RtlUnlockHeap
3156 * Releases ownership of the critical section object.
3157 *
3158 * PARAMS
3159 * Heap [in] Handle to the heap to unlock
3160 *
3161 * RETURNS
3162 * TRUE: Success
3163 * FALSE: Failure
3164 *
3165 * @implemented
3166 */
3167 BOOLEAN NTAPI
3168 RtlUnlockHeap(HANDLE HeapPtr)
3169 {
3170 PHEAP Heap = (PHEAP)HeapPtr;
3171
3172 // FIXME Check for special heap
3173
3174 /* Check if it's really a heap */
3175 if (Heap->Signature != HEAP_SIGNATURE) return FALSE;
3176
3177 /* Unlock if it's lockable */
3178 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
3179 {
3180 RtlLeaveHeapLock(Heap->LockVariable);
3181 }
3182
3183 return TRUE;
3184 }
3185
3186
3187 /***********************************************************************
3188 * RtlSizeHeap
3189 * PARAMS
3190 * Heap [in] Handle of heap
3191 * Flags [in] Heap size control flags
3192 * Ptr [in] Address of memory to return size for
3193 *
3194 * RETURNS
3195 * Size in bytes of allocated memory
3196 * 0xffffffff: Failure
3197 *
3198 * @implemented
3199 */
3200 SIZE_T NTAPI
3201 RtlSizeHeap(
3202 HANDLE HeapPtr,
3203 ULONG Flags,
3204 PVOID Ptr
3205 )
3206 {
3207 PHEAP Heap = (PHEAP)HeapPtr;
3208 PHEAP_ENTRY HeapEntry;
3209 SIZE_T EntrySize;
3210
3211 // FIXME This is a hack around missing SEH support!
3212 if (!Heap)
3213 {
3214 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_HANDLE);
3215 return (SIZE_T)-1;
3216 }
3217
3218 /* Force flags */
3219 Flags |= Heap->ForceFlags;
3220
3221 /* Call special heap */
3222 if (RtlpHeapIsSpecial(Flags))
3223 return RtlDebugSizeHeap(Heap, Flags, Ptr);
3224
3225 /* Get the heap entry pointer */
3226 HeapEntry = (PHEAP_ENTRY)Ptr - 1;
3227
3228 /* Return -1 if that entry is free */
3229 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY))
3230 {
3231 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
3232 return (SIZE_T)-1;
3233 }
3234
3235 /* Get size of this block depending if it's a usual or a big one */
3236 if (HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
3237 {
3238 EntrySize = RtlpGetSizeOfBigBlock(HeapEntry);
3239 }
3240 else
3241 {
3242 /* Calculate it */
3243 EntrySize = (HeapEntry->Size << HEAP_ENTRY_SHIFT) - HeapEntry->UnusedBytes;
3244 }
3245
3246 /* Return calculated size */
3247 return EntrySize;
3248 }
3249
3250 BOOLEAN NTAPI
3251 RtlpCheckInUsePattern(PHEAP_ENTRY HeapEntry)
3252 {
3253 SIZE_T Size, Result;
3254 PCHAR TailPart;
3255
3256 /* Calculate size */
3257 if (HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC)
3258 Size = RtlpGetSizeOfBigBlock(HeapEntry);
3259 else
3260 Size = (HeapEntry->Size << HEAP_ENTRY_SHIFT) - HeapEntry->UnusedBytes;
3261
3262 /* Calculate pointer to the tail part of the block */
3263 TailPart = (PCHAR)(HeapEntry + 1) + Size;
3264
3265 /* Compare tail pattern */
3266 Result = RtlCompareMemory(TailPart,
3267 FillPattern,
3268 HEAP_ENTRY_SIZE);
3269
3270 if (Result != HEAP_ENTRY_SIZE)
3271 {
3272 DPRINT1("HEAP: Heap entry (size %x) %p tail is modified at %p\n", Size, HeapEntry, TailPart + Result);
3273 return FALSE;
3274 }
3275
3276 /* All is fine */
3277 return TRUE;
3278 }
3279
3280 BOOLEAN NTAPI
3281 RtlpValidateHeapHeaders(
3282 PHEAP Heap,
3283 BOOLEAN Recalculate)
3284 {
3285 // We skip header validation for now
3286 return TRUE;
3287 }
3288
3289 BOOLEAN NTAPI
3290 RtlpValidateHeapEntry(
3291 PHEAP Heap,
3292 PHEAP_ENTRY HeapEntry)
3293 {
3294 BOOLEAN BigAllocation, EntryFound = FALSE;
3295 PHEAP_SEGMENT Segment;
3296 ULONG SegmentOffset;
3297
3298 /* Perform various consistency checks of this entry */
3299 if (!HeapEntry) goto invalid_entry;
3300 if ((ULONG_PTR)HeapEntry & (HEAP_ENTRY_SIZE - 1)) goto invalid_entry;
3301 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY)) goto invalid_entry;
3302
3303 BigAllocation = HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC;
3304 Segment = Heap->Segments[HeapEntry->SegmentOffset];
3305
3306 if (BigAllocation &&
3307 (((ULONG_PTR)HeapEntry & (PAGE_SIZE - 1)) != FIELD_OFFSET(HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock)))
3308 goto invalid_entry;
3309
3310 if (!BigAllocation && (HeapEntry->SegmentOffset >= HEAP_SEGMENTS ||
3311 !Segment ||
3312 HeapEntry < Segment->FirstEntry ||
3313 HeapEntry >= Segment->LastValidEntry))
3314 goto invalid_entry;
3315
3316 if ((HeapEntry->Flags & HEAP_ENTRY_FILL_PATTERN) &&
3317 !RtlpCheckInUsePattern(HeapEntry))
3318 goto invalid_entry;
3319
3320 /* Checks are done, if this is a virtual entry, that's all */
3321 if (HeapEntry->Flags & HEAP_ENTRY_VIRTUAL_ALLOC) return TRUE;
3322
3323 /* Go through segments and check if this entry fits into any of them */
3324 for (SegmentOffset = 0; SegmentOffset < HEAP_SEGMENTS; SegmentOffset++)
3325 {
3326 Segment = Heap->Segments[SegmentOffset];
3327 if (!Segment) continue;
3328
3329 if ((HeapEntry >= Segment->FirstEntry) &&
3330 (HeapEntry < Segment->LastValidEntry))
3331 {
3332 /* Got it */
3333 EntryFound = TRUE;
3334 break;
3335 }
3336 }
3337
3338 /* Return our result of finding entry in the segments */
3339 return EntryFound;
3340
3341 invalid_entry:
3342 DPRINT1("HEAP: Invalid heap entry %p in heap %p\n", HeapEntry, Heap);
3343 return FALSE;
3344 }
3345
3346 BOOLEAN NTAPI
3347 RtlpValidateHeapSegment(
3348 PHEAP Heap,
3349 PHEAP_SEGMENT Segment,
3350 UCHAR SegmentOffset,
3351 PULONG FreeEntriesCount,
3352 PSIZE_T TotalFreeSize,
3353 PSIZE_T TagEntries,
3354 PSIZE_T PseudoTagEntries)
3355 {
3356 PHEAP_UCR_DESCRIPTOR UcrDescriptor;
3357 PLIST_ENTRY UcrEntry;
3358 SIZE_T ByteSize, Size, Result;
3359 PHEAP_ENTRY CurrentEntry;
3360 ULONG UnCommittedPages;
3361 ULONG UnCommittedRanges;
3362 ULONG PreviousSize;
3363
3364 UnCommittedPages = 0;
3365 UnCommittedRanges = 0;
3366
3367 if (IsListEmpty(&Segment->UCRSegmentList))
3368 {
3369 UcrEntry = NULL;
3370 UcrDescriptor = NULL;
3371 }
3372 else
3373 {
3374 UcrEntry = Segment->UCRSegmentList.Flink;
3375 UcrDescriptor = CONTAINING_RECORD(UcrEntry, HEAP_UCR_DESCRIPTOR, SegmentEntry);
3376 }
3377
3378 if (Segment->BaseAddress == Heap)
3379 CurrentEntry = &Heap->Entry;
3380 else
3381 CurrentEntry = &Segment->Entry;
3382
3383 while (CurrentEntry < Segment->LastValidEntry)
3384 {
3385 if (UcrDescriptor &&
3386 ((PVOID)CurrentEntry >= UcrDescriptor->Address))
3387 {
3388 DPRINT1("HEAP: Entry %p is not inside uncommited range [%p .. %p)\n",
3389 CurrentEntry, UcrDescriptor->Address,
3390 (PCHAR)UcrDescriptor->Address + UcrDescriptor->Size);
3391
3392 return FALSE;
3393 }
3394
3395 PreviousSize = 0;
3396
3397 while (CurrentEntry < Segment->LastValidEntry)
3398 {
3399 if (PreviousSize != CurrentEntry->PreviousSize)
3400 {
3401 DPRINT1("HEAP: Entry %p has incorrect PreviousSize %x instead of %x\n",
3402 CurrentEntry, CurrentEntry->PreviousSize, PreviousSize);
3403
3404 return FALSE;
3405 }
3406
3407 PreviousSize = CurrentEntry->Size;
3408 Size = CurrentEntry->Size << HEAP_ENTRY_SHIFT;
3409
3410 if (CurrentEntry->Flags & HEAP_ENTRY_BUSY)
3411 {
3412 if (TagEntries)
3413 {
3414 UNIMPLEMENTED;
3415 }
3416
3417 /* Check fill pattern */
3418 if (CurrentEntry->Flags & HEAP_ENTRY_FILL_PATTERN)
3419 {
3420 if (!RtlpCheckInUsePattern(CurrentEntry))
3421 return FALSE;
3422 }
3423 }
3424 else
3425 {
3426 /* The entry is free, increase free entries count and total free size */
3427 *FreeEntriesCount = *FreeEntriesCount + 1;
3428 *TotalFreeSize += CurrentEntry->Size;
3429
3430 if ((Heap->Flags & HEAP_FREE_CHECKING_ENABLED) &&
3431 (CurrentEntry->Flags & HEAP_ENTRY_FILL_PATTERN))
3432 {
3433 ByteSize = Size - sizeof(HEAP_FREE_ENTRY);
3434
3435 if ((CurrentEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT) &&
3436 (ByteSize > sizeof(HEAP_FREE_ENTRY_EXTRA)))
3437 {
3438 ByteSize -= sizeof(HEAP_FREE_ENTRY_EXTRA);
3439 }
3440
3441 Result = RtlCompareMemoryUlong((PCHAR)((PHEAP_FREE_ENTRY)CurrentEntry + 1),
3442 ByteSize,
3443 ARENA_FREE_FILLER);
3444
3445 if (Result != ByteSize)
3446 {
3447 DPRINT1("HEAP: Free heap block %p modified at %p after it was freed\n",
3448 CurrentEntry,
3449 (PCHAR)(CurrentEntry + 1) + Result);
3450
3451 return FALSE;
3452 }
3453 }
3454 }
3455
3456 if (CurrentEntry->SegmentOffset != SegmentOffset)
3457 {
3458 DPRINT1("HEAP: Heap entry %p SegmentOffset is incorrect %x (should be %x)\n", CurrentEntry, SegmentOffset, CurrentEntry->SegmentOffset);
3459 return FALSE;
3460 }
3461
3462 /* Check if it's the last entry */
3463 if (CurrentEntry->Flags & HEAP_ENTRY_LAST_ENTRY)
3464 {
3465 CurrentEntry = (PHEAP_ENTRY)((PCHAR)CurrentEntry + Size);
3466
3467 if (!UcrDescriptor)
3468 {
3469 /* Check if it's not really the last one */
3470 if (CurrentEntry != Segment->LastValidEntry)
3471 {
3472 DPRINT1("HEAP: Heap entry %p is not last block in segment (%x)\n", CurrentEntry, Segment->LastValidEntry);
3473 return FALSE;
3474 }
3475 }
3476 else if (CurrentEntry != UcrDescriptor->Address)
3477 {
3478 DPRINT1("HEAP: Heap entry %p does not match next uncommitted address (%p)\n",
3479 CurrentEntry, UcrDescriptor->Address);
3480
3481 return FALSE;
3482 }
3483 else
3484 {
3485 UnCommittedPages += (UcrDescriptor->Size / PAGE_SIZE);
3486 UnCommittedRanges++;
3487
3488 CurrentEntry = (PHEAP_ENTRY)((PCHAR)UcrDescriptor->Address + UcrDescriptor->Size);
3489
3490 /* Go to the next UCR descriptor */
3491 UcrEntry = UcrEntry->Flink;
3492 if (UcrEntry == &Segment->UCRSegmentList)
3493 {
3494 UcrEntry = NULL;
3495 UcrDescriptor = NULL;
3496 }
3497 else
3498 {
3499 UcrDescriptor = CONTAINING_RECORD(UcrEntry, HEAP_UCR_DESCRIPTOR, SegmentEntry);
3500 }
3501 }
3502
3503 break;
3504 }
3505
3506 /* Advance to the next entry */
3507 CurrentEntry = (PHEAP_ENTRY)((PCHAR)CurrentEntry + Size);
3508 }
3509 }
3510
3511 /* Check total numbers of UCP and UCR */
3512 if (Segment->NumberOfUnCommittedPages != UnCommittedPages)
3513 {
3514 DPRINT1("HEAP: Segment %p NumberOfUnCommittedPages is invalid (%x != %x)\n",
3515 Segment, Segment->NumberOfUnCommittedPages, UnCommittedPages);
3516
3517 return FALSE;
3518 }
3519
3520 if (Segment->NumberOfUnCommittedRanges != UnCommittedRanges)
3521 {
3522 DPRINT1("HEAP: Segment %p NumberOfUnCommittedRanges is invalid (%x != %x)\n",
3523 Segment, Segment->NumberOfUnCommittedRanges, UnCommittedRanges);
3524
3525 return FALSE;
3526 }
3527
3528 return TRUE;
3529 }
3530
3531 BOOLEAN NTAPI
3532 RtlpValidateHeap(PHEAP Heap,
3533 BOOLEAN ForceValidation)
3534 {
3535 PHEAP_SEGMENT Segment;
3536 BOOLEAN EmptyList;
3537 UCHAR SegmentOffset;
3538 SIZE_T Size, TotalFreeSize;
3539 ULONG PreviousSize;
3540 PHEAP_VIRTUAL_ALLOC_ENTRY VirtualAllocBlock;
3541 PLIST_ENTRY ListHead, NextEntry;
3542 PHEAP_FREE_ENTRY FreeEntry;
3543 ULONG FreeBlocksCount, FreeListEntriesCount;
3544
3545 /* Check headers */
3546 if (!RtlpValidateHeapHeaders(Heap, FALSE))
3547 return FALSE;
3548
3549 /* Skip validation if it's not needed */
3550 if (!ForceValidation && !(Heap->Flags & HEAP_VALIDATE_ALL_ENABLED))
3551 return TRUE;
3552
3553 /* Check free lists bitmaps */
3554 FreeListEntriesCount = 0;
3555 ListHead = &Heap->FreeLists[0];
3556
3557 for (Size = 0; Size < HEAP_FREELISTS; Size++)
3558 {
3559 if (Size)
3560 {
3561 /* This is a dedicated list. Check if it's empty */
3562 EmptyList = IsListEmpty(ListHead);
3563
3564 if (Heap->u.FreeListsInUseBytes[Size >> 3] & (1 << (Size & 7)))
3565 {
3566 if (EmptyList)
3567 {
3568 DPRINT1("HEAP: Empty %x-free list marked as non-empty\n", Size);
3569 return FALSE;
3570 }
3571 }
3572 else
3573 {
3574 if (!EmptyList)
3575 {
3576 DPRINT1("HEAP: Non-empty %x-free list marked as empty\n", Size);
3577 return FALSE;
3578 }
3579 }
3580 }
3581
3582 /* Now check this list entries */
3583 NextEntry = ListHead->Flink;
3584 PreviousSize = 0;
3585
3586 while (ListHead != NextEntry)
3587 {
3588 FreeEntry = CONTAINING_RECORD(NextEntry, HEAP_FREE_ENTRY, FreeList);
3589 NextEntry = NextEntry->Flink;
3590
3591 /* If there is an in-use entry in a free list - that's quite a big problem */
3592 if (FreeEntry->Flags & HEAP_ENTRY_BUSY)
3593 {
3594 DPRINT1("HEAP: %x-dedicated list free element %x is marked in-use\n", Size, FreeEntry);
3595 return FALSE;
3596 }
3597
3598 /* Check sizes according to that specific list's size */
3599 if ((Size == 0) && (FreeEntry->Size < HEAP_FREELISTS))
3600 {
3601 DPRINT1("HEAP: Non dedicated list free element %x has size %x which would fit a dedicated list\n", FreeEntry, FreeEntry->Size);
3602 return FALSE;
3603 }
3604 else if (Size && (FreeEntry->Size != Size))
3605 {
3606 DPRINT1("HEAP: %x-dedicated list free element %x has incorrect size %x\n", Size, FreeEntry, FreeEntry->Size);
3607 return FALSE;
3608 }
3609 else if ((Size == 0) && (FreeEntry->Size < PreviousSize))
3610 {
3611 DPRINT1("HEAP: Non dedicated list free element %x is not put in order\n", FreeEntry);
3612 return FALSE;
3613 }
3614
3615 /* Remember previous size*/
3616 PreviousSize = FreeEntry->Size;
3617
3618 /* Add up to the total amount of free entries */
3619 FreeListEntriesCount++;
3620 }
3621
3622 /* Go to the head of the next free list */
3623 ListHead++;
3624 }
3625
3626 /* Check big allocations */
3627 ListHead = &Heap->VirtualAllocdBlocks;
3628 NextEntry = ListHead->Flink;
3629
3630 while (ListHead != NextEntry)
3631 {
3632 VirtualAllocBlock = CONTAINING_RECORD(NextEntry, HEAP_VIRTUAL_ALLOC_ENTRY, Entry);
3633
3634 /* We can only check the fill pattern */
3635 if (VirtualAllocBlock->BusyBlock.Flags & HEAP_ENTRY_FILL_PATTERN)
3636 {
3637 if (!RtlpCheckInUsePattern(&VirtualAllocBlock->BusyBlock))
3638 return FALSE;
3639 }
3640
3641 NextEntry = NextEntry->Flink;
3642 }
3643
3644 /* Check all segments */
3645 FreeBlocksCount = 0;
3646 TotalFreeSize = 0;
3647
3648 for (SegmentOffset = 0; SegmentOffset < HEAP_SEGMENTS; SegmentOffset++)
3649 {
3650 Segment = Heap->Segments[SegmentOffset];
3651
3652 /* Go to the next one if there is no segment */
3653 if (!Segment) continue;
3654
3655 if (!RtlpValidateHeapSegment(Heap,
3656 Segment,
3657 SegmentOffset,
3658 &FreeBlocksCount,
3659 &TotalFreeSize,
3660 NULL,
3661 NULL))
3662 {
3663 return FALSE;
3664 }
3665 }
3666
3667 if (FreeListEntriesCount != FreeBlocksCount)
3668 {
3669 DPRINT1("HEAP: Free blocks count in arena (%d) does not match free blocks number in the free lists (%d)\n", FreeBlocksCount, FreeListEntriesCount);
3670 return FALSE;
3671 }
3672
3673 if (Heap->TotalFreeSize != TotalFreeSize)
3674 {
3675 DPRINT1("HEAP: Total size of free blocks in arena (%d) does not equal to the one in heap header (%d)\n", TotalFreeSize, Heap->TotalFreeSize);
3676 return FALSE;
3677 }
3678
3679 return TRUE;
3680 }
3681
3682 /***********************************************************************
3683 * RtlValidateHeap
3684 * Validates a specified heap.
3685 *
3686 * PARAMS
3687 * Heap [in] Handle to the heap
3688 * Flags [in] Bit flags that control access during operation
3689 * Block [in] Optional pointer to memory block to validate
3690 *
3691 * NOTES
3692 * Flags is ignored.
3693 *
3694 * RETURNS
3695 * TRUE: Success
3696 * FALSE: Failure
3697 *
3698 * @implemented
3699 */
3700 BOOLEAN NTAPI RtlValidateHeap(
3701 HANDLE HeapPtr,
3702 ULONG Flags,
3703 PVOID Block
3704 )
3705 {
3706 PHEAP Heap = (PHEAP)HeapPtr;
3707 BOOLEAN HeapLocked = FALSE;
3708 BOOLEAN HeapValid;
3709
3710 // FIXME Check for special heap
3711
3712 /* Check signature */
3713 if (Heap->Signature != HEAP_SIGNATURE)
3714 {
3715 DPRINT1("HEAP: Signature %x is invalid for heap %p\n", Heap->Signature, Heap);
3716 return FALSE;
3717 }
3718
3719 /* Force flags */
3720 Flags = Heap->ForceFlags;
3721
3722 /* Acquire the lock if necessary */
3723 if (!(Flags & HEAP_NO_SERIALIZE))
3724 {
3725 RtlEnterHeapLock(Heap->LockVariable);
3726 HeapLocked = TRUE;
3727 }
3728
3729 /* Either validate whole heap or just one entry */
3730 if (!Block)
3731 HeapValid = RtlpValidateHeap(Heap, TRUE);
3732 else
3733 HeapValid = RtlpValidateHeapEntry(Heap, (PHEAP_ENTRY)Block - 1);
3734
3735 /* Unlock if it's lockable */
3736 if (HeapLocked)
3737 {
3738 RtlLeaveHeapLock(Heap->LockVariable);
3739 }
3740
3741 return HeapValid;
3742 }
3743
3744 VOID
3745 RtlInitializeHeapManager(VOID)
3746 {
3747 PPEB Peb;
3748
3749 /* Get PEB */
3750 Peb = RtlGetCurrentPeb();
3751
3752 /* Initialize heap-related fields of PEB */
3753 Peb->NumberOfHeaps = 0;
3754
3755 /* Initialize the process heaps list protecting lock */
3756 RtlInitializeHeapLock(&RtlpProcessHeapsListLock);
3757 }
3758
3759
3760 /*
3761 * @implemented
3762 */
3763 NTSTATUS NTAPI
3764 RtlEnumProcessHeaps(PHEAP_ENUMERATION_ROUTINE HeapEnumerationRoutine,
3765 PVOID lParam)
3766 {
3767 UNIMPLEMENTED;
3768 return STATUS_NOT_IMPLEMENTED;
3769 }
3770
3771
3772 /*
3773 * @implemented
3774 */
3775 ULONG NTAPI
3776 RtlGetProcessHeaps(ULONG count,
3777 HANDLE *heaps)
3778 {
3779 UNIMPLEMENTED;
3780 return 0;
3781 }
3782
3783
3784 /*
3785 * @implemented
3786 */
3787 BOOLEAN NTAPI
3788 RtlValidateProcessHeaps(VOID)
3789 {
3790 UNIMPLEMENTED;
3791 return TRUE;
3792 }
3793
3794
3795 /*
3796 * @unimplemented
3797 */
3798 BOOLEAN NTAPI
3799 RtlZeroHeap(
3800 IN PVOID HeapHandle,
3801 IN ULONG Flags
3802 )
3803 {
3804 UNIMPLEMENTED;
3805 return FALSE;
3806 }
3807
3808 /*
3809 * @implemented
3810 */
3811 BOOLEAN
3812 NTAPI
3813 RtlSetUserValueHeap(IN PVOID HeapHandle,
3814 IN ULONG Flags,
3815 IN PVOID BaseAddress,
3816 IN PVOID UserValue)
3817 {
3818 PHEAP Heap = (PHEAP)HeapHandle;
3819 PHEAP_ENTRY HeapEntry;
3820 PHEAP_ENTRY_EXTRA Extra;
3821 BOOLEAN HeapLocked = FALSE;
3822
3823 /* Force flags */
3824 Flags |= Heap->Flags;
3825
3826 /* Call special heap */
3827 if (RtlpHeapIsSpecial(Flags))
3828 return RtlDebugSetUserValueHeap(Heap, Flags, BaseAddress, UserValue);
3829
3830 /* Lock if it's lockable */
3831 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
3832 {
3833 RtlEnterHeapLock(Heap->LockVariable);
3834 HeapLocked = TRUE;
3835 }
3836
3837 /* Get a pointer to the entry */
3838 HeapEntry = (PHEAP_ENTRY)BaseAddress - 1;
3839
3840 /* If it's a free entry - return error */
3841 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY))
3842 {
3843 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
3844
3845 /* Release the heap lock if it was acquired */
3846 if (HeapLocked)
3847 RtlLeaveHeapLock(Heap->LockVariable);
3848
3849 return FALSE;
3850 }
3851
3852 /* Check if this entry has an extra stuff associated with it */
3853 if (HeapEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
3854 {
3855 /* Use extra to store the value */
3856 Extra = RtlpGetExtraStuffPointer(HeapEntry);
3857 Extra->Settable = (ULONG_PTR)UserValue;
3858 }
3859
3860 /* Release the heap lock if it was acquired */
3861 if (HeapLocked)
3862 RtlLeaveHeapLock(Heap->LockVariable);
3863
3864 return TRUE;
3865 }
3866
3867 /*
3868 * @implemented
3869 */
3870 BOOLEAN
3871 NTAPI
3872 RtlSetUserFlagsHeap(IN PVOID HeapHandle,
3873 IN ULONG Flags,
3874 IN PVOID BaseAddress,
3875 IN ULONG UserFlagsReset,
3876 IN ULONG UserFlagsSet)
3877 {
3878 PHEAP Heap = (PHEAP)HeapHandle;
3879 PHEAP_ENTRY HeapEntry;
3880 BOOLEAN HeapLocked = FALSE;
3881
3882 /* Force flags */
3883 Flags |= Heap->Flags;
3884
3885 /* Call special heap */
3886 if (RtlpHeapIsSpecial(Flags))
3887 return RtlDebugSetUserFlagsHeap(Heap, Flags, BaseAddress, UserFlagsReset, UserFlagsSet);
3888
3889 /* Lock if it's lockable */
3890 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
3891 {
3892 RtlEnterHeapLock(Heap->LockVariable);
3893 HeapLocked = TRUE;
3894 }
3895
3896 /* Get a pointer to the entry */
3897 HeapEntry = (PHEAP_ENTRY)BaseAddress - 1;
3898
3899 /* If it's a free entry - return error */
3900 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY))
3901 {
3902 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
3903
3904 /* Release the heap lock if it was acquired */
3905 if (HeapLocked)
3906 RtlLeaveHeapLock(Heap->LockVariable);
3907
3908 return FALSE;
3909 }
3910
3911 /* Set / reset flags */
3912 HeapEntry->Flags &= ~(UserFlagsReset >> 4);
3913 HeapEntry->Flags |= (UserFlagsSet >> 4);
3914
3915 /* Release the heap lock if it was acquired */
3916 if (HeapLocked)
3917 RtlLeaveHeapLock(Heap->LockVariable);
3918
3919 return TRUE;
3920 }
3921
3922 /*
3923 * @implemented
3924 */
3925 BOOLEAN
3926 NTAPI
3927 RtlGetUserInfoHeap(IN PVOID HeapHandle,
3928 IN ULONG Flags,
3929 IN PVOID BaseAddress,
3930 OUT PVOID *UserValue,
3931 OUT PULONG UserFlags)
3932 {
3933 PHEAP Heap = (PHEAP)HeapHandle;
3934 PHEAP_ENTRY HeapEntry;
3935 PHEAP_ENTRY_EXTRA Extra;
3936 BOOLEAN HeapLocked = FALSE;
3937
3938 /* Force flags */
3939 Flags |= Heap->Flags;
3940
3941 /* Call special heap */
3942 if (RtlpHeapIsSpecial(Flags))
3943 return RtlDebugGetUserInfoHeap(Heap, Flags, BaseAddress, UserValue, UserFlags);
3944
3945 /* Lock if it's lockable */
3946 if (!(Heap->Flags & HEAP_NO_SERIALIZE))
3947 {
3948 RtlEnterHeapLock(Heap->LockVariable);
3949 HeapLocked = TRUE;
3950 }
3951
3952 /* Get a pointer to the entry */
3953 HeapEntry = (PHEAP_ENTRY)BaseAddress - 1;
3954
3955 /* If it's a free entry - return error */
3956 if (!(HeapEntry->Flags & HEAP_ENTRY_BUSY))
3957 {
3958 RtlSetLastWin32ErrorAndNtStatusFromNtStatus(STATUS_INVALID_PARAMETER);
3959
3960 /* Release the heap lock if it was acquired */
3961 if (HeapLocked)
3962 RtlLeaveHeapLock(Heap->LockVariable);
3963
3964 return FALSE;
3965 }
3966
3967 /* Check if this entry has an extra stuff associated with it */
3968 if (HeapEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT)
3969 {
3970 /* Get pointer to extra data */
3971 Extra = RtlpGetExtraStuffPointer(HeapEntry);
3972
3973 /* Pass user value */
3974 if (UserValue)
3975 *UserValue = (PVOID)Extra->Settable;
3976
3977 /* Decode and return user flags */
3978 if (UserFlags)
3979 *UserFlags = (HeapEntry->Flags & HEAP_ENTRY_SETTABLE_FLAGS) << 4;
3980 }
3981
3982 /* Release the heap lock if it was acquired */
3983 if (HeapLocked)
3984 RtlLeaveHeapLock(Heap->LockVariable);
3985
3986 return TRUE;
3987 }
3988
3989 /*
3990 * @unimplemented
3991 */
3992 NTSTATUS
3993 NTAPI
3994 RtlUsageHeap(IN HANDLE Heap,
3995 IN ULONG Flags,
3996 OUT PRTL_HEAP_USAGE Usage)
3997 {
3998 /* TODO */
3999 UNIMPLEMENTED;
4000 return STATUS_NOT_IMPLEMENTED;
4001 }
4002
4003 PWSTR
4004 NTAPI
4005 RtlQueryTagHeap(IN PVOID HeapHandle,
4006 IN ULONG Flags,
4007 IN USHORT TagIndex,
4008 IN BOOLEAN ResetCounters,
4009 OUT PRTL_HEAP_TAG_INFO HeapTagInfo)
4010 {
4011 /* TODO */
4012 UNIMPLEMENTED;
4013 return NULL;
4014 }
4015
4016 ULONG
4017 NTAPI
4018 RtlExtendHeap(IN HANDLE Heap,
4019 IN ULONG Flags,
4020 IN PVOID P,
4021 IN SIZE_T Size)
4022 {
4023 /* TODO */
4024 UNIMPLEMENTED;
4025 return 0;
4026 }
4027
4028 ULONG
4029 NTAPI
4030 RtlCreateTagHeap(IN HANDLE HeapHandle,
4031 IN ULONG Flags,
4032 IN PWSTR TagName,
4033 IN PWSTR TagSubName)
4034 {
4035 /* TODO */
4036 UNIMPLEMENTED;
4037 return 0;
4038 }
4039
4040 NTSTATUS
4041 NTAPI
4042 RtlWalkHeap(IN HANDLE HeapHandle,
4043 IN PVOID HeapEntry)
4044 {
4045 UNIMPLEMENTED;
4046 return STATUS_NOT_IMPLEMENTED;
4047 }
4048
4049 PVOID
4050 NTAPI
4051 RtlProtectHeap(IN PVOID HeapHandle,
4052 IN BOOLEAN ReadOnly)
4053 {
4054 UNIMPLEMENTED;
4055 return NULL;
4056 }
4057
4058 NTSTATUS
4059 NTAPI
4060 RtlSetHeapInformation(IN HANDLE HeapHandle OPTIONAL,
4061 IN HEAP_INFORMATION_CLASS HeapInformationClass,
4062 IN PVOID HeapInformation,
4063 IN SIZE_T HeapInformationLength)
4064 {
4065 /* Setting heap information is not really supported except for enabling LFH */
4066 if (HeapInformationClass == 0) return STATUS_SUCCESS;
4067
4068 /* Check buffer length */
4069 if (HeapInformationLength < sizeof(ULONG))
4070 {
4071 /* The provided buffer is too small */
4072 return STATUS_BUFFER_TOO_SMALL;
4073 }
4074
4075 /* Check for a special magic value for enabling LFH */
4076 if (*(PULONG)HeapInformation == 2)
4077 {
4078 DPRINT1("RtlSetHeapInformation() needs to enable LFH\n");
4079 return STATUS_SUCCESS;
4080 }
4081
4082 return STATUS_UNSUCCESSFUL;
4083 }
4084
4085 NTSTATUS
4086 NTAPI
4087 RtlQueryHeapInformation(HANDLE HeapHandle,
4088 HEAP_INFORMATION_CLASS HeapInformationClass,
4089 PVOID HeapInformation OPTIONAL,
4090 SIZE_T HeapInformationLength OPTIONAL,
4091 PSIZE_T ReturnLength OPTIONAL)
4092 {
4093 PHEAP Heap = (PHEAP)HeapHandle;
4094
4095 /* Only HeapCompatibilityInformation is supported */
4096 if (HeapInformationClass != HeapCompatibilityInformation)
4097 return STATUS_UNSUCCESSFUL;
4098
4099 /* Set result length */
4100 if (ReturnLength) *ReturnLength = sizeof(ULONG);
4101
4102 /* Check buffer length */
4103 if (HeapInformationLength < sizeof(ULONG))
4104 {
4105 /* It's too small, return needed length */
4106 return STATUS_BUFFER_TOO_SMALL;
4107 }
4108
4109 /* Return front end heap type */
4110 *(PULONG)HeapInformation = Heap->FrontEndHeapType;
4111
4112 return STATUS_SUCCESS;
4113 }
4114
4115 NTSTATUS
4116 NTAPI
4117 RtlMultipleAllocateHeap(IN PVOID HeapHandle,
4118 IN ULONG Flags,
4119 IN SIZE_T Size,
4120 IN ULONG Count,
4121 OUT PVOID *Array)
4122 {
4123 UNIMPLEMENTED;
4124 return 0;
4125 }
4126
4127 NTSTATUS
4128 NTAPI
4129 RtlMultipleFreeHeap(IN PVOID HeapHandle,
4130 IN ULONG Flags,
4131 IN ULONG Count,
4132 OUT PVOID *Array)
4133 {
4134 UNIMPLEMENTED;
4135 return 0;
4136 }
4137
4138 /* EOF */