/lib/rtl/heap.c
C | 4085 lines | 2677 code | 677 blank | 731 comment | 487 complexity | 2d9689f7851593f090adde3d9a2c87c8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0, AGPL-3.0, GPL-3.0, CPL-1.0
Large files files are truncated, but you can click here to view the full file
- /* COPYRIGHT: See COPYING in the top level directory
- * PROJECT: ReactOS system libraries
- * FILE: lib/rtl/heap.c
- * PURPOSE: RTL Heap backend allocator
- * PROGRAMMERS: Copyright 2010 Aleksey Bragin
- */
- /* Useful references:
- http://msdn.microsoft.com/en-us/library/ms810466.aspx
- http://msdn.microsoft.com/en-us/library/ms810603.aspx
- http://www.securitylab.ru/analytics/216376.php
- http://binglongx.spaces.live.com/blog/cns!142CBF6D49079DE8!596.entry
- http://www.phreedom.org/research/exploits/asn1-bitstring/
- http://illmatics.com/Understanding_the_LFH.pdf
- http://www.alex-ionescu.com/?p=18
- */
- /* INCLUDES *****************************************************************/
- #include <rtl.h>
- #include <heap.h>
- #define NDEBUG
- #include <debug.h>
- RTL_CRITICAL_SECTION RtlpProcessHeapsListLock;
- /* Bitmaps stuff */
- /* How many least significant bits are clear */
- UCHAR RtlpBitsClearLow[] =
- {
- 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
- 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
- };
- UCHAR FORCEINLINE
- RtlpFindLeastSetBit(ULONG Bits)
- {
- if (Bits & 0xFFFF)
- {
- if (Bits & 0xFF)
- return RtlpBitsClearLow[Bits & 0xFF]; /* Lowest byte */
- else
- return RtlpBitsClearLow[(Bits >> 8) & 0xFF] + 8; /* 2nd byte */
- }
- else
- {
- if ((Bits >> 16) & 0xFF)
- return RtlpBitsClearLow[(Bits >> 16) & 0xFF] + 16; /* 3rd byte */
- else
- return RtlpBitsClearLow[(Bits >> 24) & 0xFF] + 24; /* Highest byte */
- }
- }
- /* Maximum size of a tail-filling pattern used for compare operation */
- UCHAR FillPattern[HEAP_ENTRY_SIZE] =
- {
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL,
- HEAP_TAIL_FILL
- };
- /* FUNCTIONS *****************************************************************/
- NTSTATUS NTAPI
- RtlpInitializeHeap(OUT PHEAP Heap,
- IN ULONG Flags,
- IN PHEAP_LOCK Lock OPTIONAL,
- IN PRTL_HEAP_PARAMETERS Parameters)
- {
- ULONG NumUCRs = 8;
- ULONG Index;
- SIZE_T HeaderSize;
- NTSTATUS Status;
- PHEAP_UCR_DESCRIPTOR UcrDescriptor;
- /* Preconditions */
- ASSERT(Heap != NULL);
- ASSERT(Parameters != NULL);
- ASSERT(!(Flags & HEAP_LOCK_USER_ALLOCATED));
- ASSERT(!(Flags & HEAP_NO_SERIALIZE) || (Lock == NULL)); /* HEAP_NO_SERIALIZE => no lock */
- /* Start out with the size of a plain Heap header */
- HeaderSize = ROUND_UP(sizeof(HEAP), sizeof(HEAP_ENTRY));
- /* Check if space needs to be added for the Heap Lock */
- if (!(Flags & HEAP_NO_SERIALIZE))
- {
- if (Lock != NULL)
- /* The user manages the Heap Lock */
- Flags |= HEAP_LOCK_USER_ALLOCATED;
- else
- if (RtlpGetMode() == UserMode)
- {
- /* In user mode, the Heap Lock trails the Heap header */
- Lock = (PHEAP_LOCK) ((ULONG_PTR) (Heap) + HeaderSize);
- HeaderSize += ROUND_UP(sizeof(HEAP_LOCK), sizeof(HEAP_ENTRY));
- }
- }
- /* Add space for the initial Heap UnCommitted Range Descriptor list */
- UcrDescriptor = (PHEAP_UCR_DESCRIPTOR) ((ULONG_PTR) (Heap) + HeaderSize);
- HeaderSize += ROUND_UP(NumUCRs * sizeof(HEAP_UCR_DESCRIPTOR), sizeof(HEAP_ENTRY));
- /* Sanity check */
- ASSERT(HeaderSize <= PAGE_SIZE);
- /* Initialise the Heap Entry header containing the Heap header */
- Heap->Entry.Size = (USHORT)(HeaderSize >> HEAP_ENTRY_SHIFT);
- Heap->Entry.Flags = HEAP_ENTRY_BUSY;
- Heap->Entry.SmallTagIndex = LOBYTE(Heap->Entry.Size) ^ HIBYTE(Heap->Entry.Size) ^ Heap->Entry.Flags;
- Heap->Entry.PreviousSize = 0;
- Heap->Entry.SegmentOffset = 0;
- Heap->Entry.UnusedBytes = 0;
- /* Initialise the Heap header */
- Heap->Signature = HEAP_SIGNATURE;
- Heap->Flags = Flags;
- Heap->ForceFlags = (Flags & (HEAP_NO_SERIALIZE |
- HEAP_GENERATE_EXCEPTIONS |
- HEAP_ZERO_MEMORY |
- HEAP_REALLOC_IN_PLACE_ONLY |
- HEAP_VALIDATE_PARAMETERS_ENABLED |
- HEAP_VALIDATE_ALL_ENABLED |
- HEAP_TAIL_CHECKING_ENABLED |
- HEAP_CREATE_ALIGN_16 |
- HEAP_FREE_CHECKING_ENABLED));
- /* Initialise the Heap parameters */
- Heap->VirtualMemoryThreshold = ROUND_UP(Parameters->VirtualMemoryThreshold, sizeof(HEAP_ENTRY)) >> HEAP_ENTRY_SHIFT;
- Heap->SegmentReserve = Parameters->SegmentReserve;
- Heap->SegmentCommit = Parameters->SegmentCommit;
- Heap->DeCommitFreeBlockThreshold = Parameters->DeCommitFreeBlockThreshold >> HEAP_ENTRY_SHIFT;
- Heap->DeCommitTotalFreeThreshold = Parameters->DeCommitTotalFreeThreshold >> HEAP_ENTRY_SHIFT;
- Heap->MaximumAllocationSize = Parameters->MaximumAllocationSize;
- Heap->CommitRoutine = Parameters->CommitRoutine;
- /* Initialise the Heap validation info */
- Heap->HeaderValidateCopy = NULL;
- Heap->HeaderValidateLength = (USHORT)HeaderSize;
- /* Initialise the Heap Lock */
- if (!(Flags & HEAP_NO_SERIALIZE) && !(Flags & HEAP_LOCK_USER_ALLOCATED))
- {
- Status = RtlInitializeHeapLock(&Lock);
- if (!NT_SUCCESS(Status))
- return Status;
- }
- Heap->LockVariable = Lock;
- /* Initialise the Heap alignment info */
- if (Flags & HEAP_CREATE_ALIGN_16)
- {
- Heap->AlignMask = (ULONG) ~15;
- Heap->AlignRound = 15 + sizeof(HEAP_ENTRY);
- }
- else
- {
- Heap->AlignMask = (ULONG) ~(sizeof(HEAP_ENTRY) - 1);
- Heap->AlignRound = 2 * sizeof(HEAP_ENTRY) - 1;
- }
- if (Flags & HEAP_TAIL_CHECKING_ENABLED)
- Heap->AlignRound += sizeof(HEAP_ENTRY);
- /* Initialise the Heap Segment list */
- for (Index = 0; Index < HEAP_SEGMENTS; ++Index)
- Heap->Segments[Index] = NULL;
- /* Initialise the Heap Free Heap Entry lists */
- for (Index = 0; Index < HEAP_FREELISTS; ++Index)
- InitializeListHead(&Heap->FreeLists[Index]);
- /* Initialise the Heap Virtual Allocated Blocks list */
- InitializeListHead(&Heap->VirtualAllocdBlocks);
- /* Initialise the Heap UnCommitted Region lists */
- InitializeListHead(&Heap->UCRSegments);
- InitializeListHead(&Heap->UCRList);
- /* Register the initial Heap UnCommitted Region Descriptors */
- for (Index = 0; Index < NumUCRs; ++Index)
- InsertTailList(&Heap->UCRList, &UcrDescriptor[Index].ListEntry);
- return STATUS_SUCCESS;
- }
- VOID FORCEINLINE
- RtlpSetFreeListsBit(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry)
- {
- ULONG Index, Bit;
- ASSERT(FreeEntry->Size < HEAP_FREELISTS);
- /* Calculate offset in the free list bitmap */
- Index = FreeEntry->Size >> 3; /* = FreeEntry->Size / (sizeof(UCHAR) * 8)*/
- Bit = 1 << (FreeEntry->Size & 7);
- /* Assure it's not already set */
- ASSERT((Heap->u.FreeListsInUseBytes[Index] & Bit) == 0);
- /* Set it */
- Heap->u.FreeListsInUseBytes[Index] |= Bit;
- }
- VOID FORCEINLINE
- RtlpClearFreeListsBit(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry)
- {
- ULONG Index, Bit;
- ASSERT(FreeEntry->Size < HEAP_FREELISTS);
- /* Calculate offset in the free list bitmap */
- Index = FreeEntry->Size >> 3; /* = FreeEntry->Size / (sizeof(UCHAR) * 8)*/
- Bit = 1 << (FreeEntry->Size & 7);
- /* Assure it was set and the corresponding free list is empty */
- ASSERT(Heap->u.FreeListsInUseBytes[Index] & Bit);
- ASSERT(IsListEmpty(&Heap->FreeLists[FreeEntry->Size]));
- /* Clear it */
- Heap->u.FreeListsInUseBytes[Index] ^= Bit;
- }
- VOID NTAPI
- RtlpInsertFreeBlockHelper(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry,
- SIZE_T BlockSize,
- BOOLEAN NoFill)
- {
- PLIST_ENTRY FreeListHead, Current;
- PHEAP_FREE_ENTRY CurrentEntry;
- ASSERT(FreeEntry->Size == BlockSize);
- /* Fill if it's not denied */
- if (!NoFill)
- {
- FreeEntry->Flags &= ~(HEAP_ENTRY_FILL_PATTERN |
- HEAP_ENTRY_EXTRA_PRESENT |
- HEAP_ENTRY_BUSY);
- if (Heap->Flags & HEAP_FREE_CHECKING_ENABLED)
- {
- RtlFillMemoryUlong((PCHAR)(FreeEntry + 1),
- (BlockSize << HEAP_ENTRY_SHIFT) - sizeof(*FreeEntry),
- ARENA_FREE_FILLER);
- FreeEntry->Flags |= HEAP_ENTRY_FILL_PATTERN;
- }
- }
- else
- {
- /* Clear out all flags except the last entry one */
- FreeEntry->Flags &= HEAP_ENTRY_LAST_ENTRY;
- }
- /* Insert it either into dedicated or non-dedicated list */
- if (BlockSize < HEAP_FREELISTS)
- {
- /* Dedicated list */
- FreeListHead = &Heap->FreeLists[BlockSize];
- if (IsListEmpty(FreeListHead))
- {
- RtlpSetFreeListsBit(Heap, FreeEntry);
- }
- }
- else
- {
- /* Non-dedicated one */
- FreeListHead = &Heap->FreeLists[0];
- Current = FreeListHead->Flink;
- /* Find a position where to insert it to (the list must be sorted) */
- while (FreeListHead != Current)
- {
- CurrentEntry = CONTAINING_RECORD(Current, HEAP_FREE_ENTRY, FreeList);
- if (BlockSize <= CurrentEntry->Size)
- break;
- Current = Current->Flink;
- }
- FreeListHead = Current;
- }
- /* Actually insert it into the list */
- InsertTailList(FreeListHead, &FreeEntry->FreeList);
- }
- VOID NTAPI
- RtlpInsertFreeBlock(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry,
- SIZE_T BlockSize)
- {
- USHORT Size, PreviousSize;
- UCHAR SegmentOffset, Flags;
- PHEAP_SEGMENT Segment;
- DPRINT("RtlpInsertFreeBlock(%p %p %x)\n", Heap, FreeEntry, BlockSize);
- /* Increase the free size counter */
- Heap->TotalFreeSize += BlockSize;
- /* Remember certain values */
- Flags = FreeEntry->Flags;
- PreviousSize = FreeEntry->PreviousSize;
- SegmentOffset = FreeEntry->SegmentOffset;
- Segment = Heap->Segments[SegmentOffset];
- /* Process it */
- while (BlockSize)
- {
- /* Check for the max size */
- if (BlockSize > HEAP_MAX_BLOCK_SIZE)
- {
- Size = HEAP_MAX_BLOCK_SIZE;
- /* Special compensation if it goes above limit just by 1 */
- if (BlockSize == (HEAP_MAX_BLOCK_SIZE + 1))
- Size -= 16;
- FreeEntry->Flags = 0;
- }
- else
- {
- Size = (USHORT)BlockSize;
- FreeEntry->Flags = Flags;
- }
- /* Change its size and insert it into a free list */
- FreeEntry->Size = Size;
- FreeEntry->PreviousSize = PreviousSize;
- FreeEntry->SegmentOffset = SegmentOffset;
- /* Call a helper to actually insert the block */
- RtlpInsertFreeBlockHelper(Heap, FreeEntry, Size, FALSE);
- /* Update sizes */
- PreviousSize = Size;
- BlockSize -= Size;
- /* Go to the next entry */
- FreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + Size);
- /* Check if that's all */
- if ((PHEAP_ENTRY)FreeEntry >= Segment->LastValidEntry) return;
- }
- /* Update previous size if needed */
- if (!(Flags & HEAP_ENTRY_LAST_ENTRY))
- FreeEntry->PreviousSize = PreviousSize;
- }
- VOID NTAPI
- RtlpRemoveFreeBlock(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry,
- BOOLEAN Dedicated,
- BOOLEAN NoFill)
- {
- SIZE_T Result, RealSize;
- /* Remove the free block and update the freelists bitmap */
- if (RemoveEntryList(&FreeEntry->FreeList) &&
- (Dedicated || (!Dedicated && FreeEntry->Size < HEAP_FREELISTS)))
- {
- RtlpClearFreeListsBit(Heap, FreeEntry);
- }
- /* Fill with pattern if necessary */
- if (!NoFill &&
- (FreeEntry->Flags & HEAP_ENTRY_FILL_PATTERN))
- {
- RealSize = (FreeEntry->Size << HEAP_ENTRY_SHIFT) - sizeof(*FreeEntry);
- /* Deduct extra stuff from block's real size */
- if (FreeEntry->Flags & HEAP_ENTRY_EXTRA_PRESENT &&
- RealSize > sizeof(HEAP_FREE_ENTRY_EXTRA))
- {
- RealSize -= sizeof(HEAP_FREE_ENTRY_EXTRA);
- }
- /* Check if the free filler is intact */
- Result = RtlCompareMemoryUlong((PCHAR)(FreeEntry + 1),
- RealSize,
- ARENA_FREE_FILLER);
- if (Result != RealSize)
- {
- DPRINT1("Free heap block %p modified at %p after it was freed\n",
- FreeEntry,
- (PCHAR)(FreeEntry + 1) + Result);
- }
- }
- }
- SIZE_T NTAPI
- RtlpGetSizeOfBigBlock(PHEAP_ENTRY HeapEntry)
- {
- PHEAP_VIRTUAL_ALLOC_ENTRY VirtualEntry;
- /* Get pointer to the containing record */
- VirtualEntry = CONTAINING_RECORD(HeapEntry, HEAP_VIRTUAL_ALLOC_ENTRY, BusyBlock);
- /* Restore the real size */
- return VirtualEntry->CommitSize - HeapEntry->Size;
- }
- PHEAP_UCR_DESCRIPTOR NTAPI
- RtlpCreateUnCommittedRange(PHEAP_SEGMENT Segment)
- {
- PLIST_ENTRY Entry;
- PHEAP_UCR_DESCRIPTOR UcrDescriptor;
- PHEAP_UCR_SEGMENT UcrSegment;
- PHEAP Heap = Segment->Heap;
- SIZE_T ReserveSize = 16 * PAGE_SIZE;
- SIZE_T CommitSize = 1 * PAGE_SIZE;
- NTSTATUS Status;
- DPRINT("RtlpCreateUnCommittedRange(%p)\n", Segment);
- /* Check if we have unused UCRs */
- if (IsListEmpty(&Heap->UCRList))
- {
- /* Get a pointer to the first UCR segment */
- UcrSegment = CONTAINING_RECORD(Heap->UCRSegments.Flink, HEAP_UCR_SEGMENT, ListEntry);
- /* Check the list of UCR segments */
- if (IsListEmpty(&Heap->UCRSegments) ||
- UcrSegment->ReservedSize == UcrSegment->CommittedSize)
- {
- /* We need to create a new one. Reserve 16 pages for it */
- UcrSegment = NULL;
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID *)&UcrSegment,
- 0,
- &ReserveSize,
- MEM_RESERVE,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status)) return NULL;
- /* Commit one page */
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID *)&UcrSegment,
- 0,
- &CommitSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status))
- {
- /* Release reserved memory */
- ZwFreeVirtualMemory(NtCurrentProcess(),
- (PVOID *)&UcrDescriptor,
- &ReserveSize,
- MEM_RELEASE);
- return NULL;
- }
- /* Set it's data */
- UcrSegment->ReservedSize = ReserveSize;
- UcrSegment->CommittedSize = CommitSize;
- /* Add it to the head of the list */
- InsertHeadList(&Heap->UCRSegments, &UcrSegment->ListEntry);
- /* Get a pointer to the first available UCR descriptor */
- UcrDescriptor = (PHEAP_UCR_DESCRIPTOR)(UcrSegment + 1);
- }
- else
- {
- /* It's possible to use existing UCR segment. Commit one more page */
- UcrDescriptor = (PHEAP_UCR_DESCRIPTOR)((PCHAR)UcrSegment + UcrSegment->CommittedSize);
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID *)&UcrDescriptor,
- 0,
- &CommitSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status)) return NULL;
- /* Update sizes */
- UcrSegment->CommittedSize += CommitSize;
- }
- /* There is a whole bunch of new UCR descriptors. Put them into the unused list */
- while ((PCHAR)UcrDescriptor < ((PCHAR)UcrSegment + UcrSegment->CommittedSize))
- {
- InsertTailList(&Heap->UCRList, &UcrDescriptor->ListEntry);
- UcrDescriptor++;
- }
- }
- /* There are unused UCRs, just get the first one */
- Entry = RemoveHeadList(&Heap->UCRList);
- UcrDescriptor = CONTAINING_RECORD(Entry, HEAP_UCR_DESCRIPTOR, ListEntry);
- return UcrDescriptor;
- }
- VOID NTAPI
- RtlpDestroyUnCommittedRange(PHEAP_SEGMENT Segment,
- PHEAP_UCR_DESCRIPTOR UcrDescriptor)
- {
- /* Zero it out */
- UcrDescriptor->Address = NULL;
- UcrDescriptor->Size = 0;
- /* Put it into the heap's list of unused UCRs */
- InsertHeadList(&Segment->Heap->UCRList, &UcrDescriptor->ListEntry);
- }
- VOID NTAPI
- RtlpInsertUnCommittedPages(PHEAP_SEGMENT Segment,
- ULONG_PTR Address,
- SIZE_T Size)
- {
- PLIST_ENTRY Current;
- PHEAP_UCR_DESCRIPTOR UcrDescriptor;
- DPRINT("RtlpInsertUnCommittedPages(%p %p %x)\n", Segment, Address, Size);
- /* Go through the list of UCR descriptors, they are sorted from lowest address
- to the highest */
- Current = Segment->UCRSegmentList.Flink;
- while(Current != &Segment->UCRSegmentList)
- {
- UcrDescriptor = CONTAINING_RECORD(Current, HEAP_UCR_DESCRIPTOR, SegmentEntry);
- if ((ULONG_PTR)UcrDescriptor->Address > Address)
- {
- /* Check for a really lucky case */
- if ((Address + Size) == (ULONG_PTR)UcrDescriptor->Address)
- {
- /* Exact match */
- UcrDescriptor->Address = (PVOID)Address;
- UcrDescriptor->Size += Size;
- return;
- }
- /* We found the block after which the new one should go */
- break;
- }
- else if (((ULONG_PTR)UcrDescriptor->Address + UcrDescriptor->Size) == Address)
- {
- /* Modify this entry */
- Address = (ULONG_PTR)UcrDescriptor->Address;
- Size += UcrDescriptor->Size;
- /* Advance to the next descriptor */
- Current = Current->Flink;
- /* Remove the current descriptor from the list and destroy it */
- RemoveEntryList(&UcrDescriptor->SegmentEntry);
- RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
- Segment->NumberOfUnCommittedRanges--;
- }
- else
- {
- /* Advance to the next descriptor */
- Current = Current->Flink;
- }
- }
- /* Create a new UCR descriptor */
- UcrDescriptor = RtlpCreateUnCommittedRange(Segment);
- if (!UcrDescriptor) return;
- UcrDescriptor->Address = (PVOID)Address;
- UcrDescriptor->Size = Size;
- /* "Current" is the descriptor after which our one should go */
- InsertTailList(Current, &UcrDescriptor->SegmentEntry);
- DPRINT("Added segment UCR with base %p, size 0x%x\n", Address, Size);
- /* Increase counters */
- Segment->NumberOfUnCommittedRanges++;
- }
- PHEAP_FREE_ENTRY NTAPI
- RtlpFindAndCommitPages(PHEAP Heap,
- PHEAP_SEGMENT Segment,
- PSIZE_T Size,
- PVOID AddressRequested)
- {
- PLIST_ENTRY Current;
- ULONG_PTR Address = 0;
- PHEAP_UCR_DESCRIPTOR UcrDescriptor, PreviousUcr = NULL;
- PHEAP_ENTRY FirstEntry, LastEntry;
- NTSTATUS Status;
- DPRINT("RtlpFindAndCommitPages(%p %p %x %p)\n", Heap, Segment, *Size, Address);
- /* Go through UCRs in a segment */
- Current = Segment->UCRSegmentList.Flink;
- while(Current != &Segment->UCRSegmentList)
- {
- UcrDescriptor = CONTAINING_RECORD(Current, HEAP_UCR_DESCRIPTOR, SegmentEntry);
- /* Check if we can use that one right away */
- if (UcrDescriptor->Size >= *Size &&
- (UcrDescriptor->Address == AddressRequested || !AddressRequested))
- {
- /* Get the address */
- Address = (ULONG_PTR)UcrDescriptor->Address;
- /* Commit it */
- if (Heap->CommitRoutine)
- {
- Status = Heap->CommitRoutine(Heap, (PVOID *)&Address, Size);
- }
- else
- {
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID *)&Address,
- 0,
- Size,
- MEM_COMMIT,
- PAGE_READWRITE);
- }
- DPRINT("Committed %d bytes at base %p, UCR size is %d\n", *Size, Address, UcrDescriptor->Size);
- /* Fail in unsuccessful case */
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Committing page failed with status 0x%08X\n", Status);
- return NULL;
- }
- /* Update tracking numbers */
- Segment->NumberOfUnCommittedPages -= (ULONG)(*Size / PAGE_SIZE);
- /* Calculate first and last entries */
- FirstEntry = (PHEAP_ENTRY)Address;
- /* Go through the entries to find the last one */
- if (PreviousUcr)
- LastEntry = (PHEAP_ENTRY)((ULONG_PTR)PreviousUcr->Address + PreviousUcr->Size);
- else
- LastEntry = &Segment->Entry;
- while (!(LastEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
- {
- ASSERT(LastEntry->Size != 0);
- LastEntry += LastEntry->Size;
- }
- ASSERT((LastEntry + LastEntry->Size) == FirstEntry);
- /* Unmark it as a last entry */
- LastEntry->Flags &= ~HEAP_ENTRY_LAST_ENTRY;
- /* Update UCR descriptor */
- UcrDescriptor->Address = (PVOID)((ULONG_PTR)UcrDescriptor->Address + *Size);
- UcrDescriptor->Size -= *Size;
- DPRINT("Updating UcrDescriptor %p, new Address %p, size %d\n",
- UcrDescriptor, UcrDescriptor->Address, UcrDescriptor->Size);
- /* Set various first entry fields */
- FirstEntry->SegmentOffset = LastEntry->SegmentOffset;
- FirstEntry->Size = (USHORT)(*Size >> HEAP_ENTRY_SHIFT);
- FirstEntry->PreviousSize = LastEntry->Size;
- /* Check if anything left in this UCR */
- if (UcrDescriptor->Size == 0)
- {
- /* It's fully exhausted */
- /* Check if this is the end of the segment */
- if(UcrDescriptor->Address == Segment->LastValidEntry)
- {
- FirstEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
- }
- else
- {
- FirstEntry->Flags = 0;
- /* Update field of next entry */
- ASSERT((FirstEntry + FirstEntry->Size)->PreviousSize == 0);
- (FirstEntry + FirstEntry->Size)->PreviousSize = FirstEntry->Size;
- }
- /* This UCR needs to be removed because it became useless */
- RemoveEntryList(&UcrDescriptor->SegmentEntry);
- RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
- Segment->NumberOfUnCommittedRanges--;
- }
- else
- {
- FirstEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
- }
- /* We're done */
- return (PHEAP_FREE_ENTRY)FirstEntry;
- }
- /* Advance to the next descriptor */
- PreviousUcr = UcrDescriptor;
- Current = Current->Flink;
- }
- return NULL;
- }
- VOID NTAPI
- RtlpDeCommitFreeBlock(PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry,
- SIZE_T Size)
- {
- PHEAP_SEGMENT Segment;
- PHEAP_ENTRY PrecedingInUseEntry = NULL, NextInUseEntry = NULL;
- PHEAP_FREE_ENTRY NextFreeEntry;
- PHEAP_UCR_DESCRIPTOR UcrDescriptor;
- SIZE_T PrecedingSize, NextSize, DecommitSize;
- ULONG_PTR DecommitBase;
- NTSTATUS Status;
- DPRINT("Decommitting %p %p %x\n", Heap, FreeEntry, Size);
- /* We can't decommit if there is a commit routine! */
- if (Heap->CommitRoutine)
- {
- /* Just add it back the usual way */
- RtlpInsertFreeBlock(Heap, FreeEntry, Size);
- return;
- }
- /* Get the segment */
- Segment = Heap->Segments[FreeEntry->SegmentOffset];
- /* Get the preceding entry */
- DecommitBase = ROUND_UP(FreeEntry, PAGE_SIZE);
- PrecedingSize = (PHEAP_ENTRY)DecommitBase - (PHEAP_ENTRY)FreeEntry;
- if (PrecedingSize == 1)
- {
- /* Just 1 heap entry, increase the base/size */
- DecommitBase += PAGE_SIZE;
- PrecedingSize += PAGE_SIZE >> HEAP_ENTRY_SHIFT;
- }
- else if (FreeEntry->PreviousSize &&
- (DecommitBase == (ULONG_PTR)FreeEntry))
- {
- PrecedingInUseEntry = (PHEAP_ENTRY)FreeEntry - FreeEntry->PreviousSize;
- }
- /* Get the next entry */
- NextFreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + Size);
- DecommitSize = ROUND_DOWN(NextFreeEntry, PAGE_SIZE);
- NextSize = (PHEAP_ENTRY)NextFreeEntry - (PHEAP_ENTRY)DecommitSize;
- if (NextSize == 1)
- {
- /* Just 1 heap entry, increase the size */
- DecommitSize -= PAGE_SIZE;
- NextSize += PAGE_SIZE >> HEAP_ENTRY_SHIFT;
- }
- else if (NextSize == 0 &&
- !(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
- {
- NextInUseEntry = (PHEAP_ENTRY)NextFreeEntry;
- }
- NextFreeEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)NextFreeEntry - NextSize);
- /* Calculate real decommit size */
- if (DecommitSize > DecommitBase)
- {
- DecommitSize -= DecommitBase;
- }
- else
- {
- /* Nothing to decommit */
- RtlpInsertFreeBlock(Heap, FreeEntry, Size);
- return;
- }
- /* A decommit is necessary. Create a UCR descriptor */
- UcrDescriptor = RtlpCreateUnCommittedRange(Segment);
- if (!UcrDescriptor)
- {
- DPRINT1("HEAP: Failed to create UCR descriptor\n");
- RtlpInsertFreeBlock(Heap, FreeEntry, PrecedingSize);
- return;
- }
- /* Decommit the memory */
- Status = ZwFreeVirtualMemory(NtCurrentProcess(),
- (PVOID *)&DecommitBase,
- &DecommitSize,
- MEM_DECOMMIT);
- /* Delete that UCR. This is needed to assure there is an unused UCR entry in the list */
- RtlpDestroyUnCommittedRange(Segment, UcrDescriptor);
- if (!NT_SUCCESS(Status))
- {
- RtlpInsertFreeBlock(Heap, FreeEntry, Size);
- return;
- }
- /* Insert uncommitted pages */
- RtlpInsertUnCommittedPages(Segment, DecommitBase, DecommitSize);
- Segment->NumberOfUnCommittedPages += (ULONG)(DecommitSize / PAGE_SIZE);
- if (PrecedingSize)
- {
- /* Adjust size of this free entry and insert it */
- FreeEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
- FreeEntry->Size = (USHORT)PrecedingSize;
- Heap->TotalFreeSize += PrecedingSize;
- /* Insert it into the free list */
- RtlpInsertFreeBlockHelper(Heap, FreeEntry, PrecedingSize, FALSE);
- }
- else if (PrecedingInUseEntry)
- {
- /* Adjust preceding in use entry */
- PrecedingInUseEntry->Flags |= HEAP_ENTRY_LAST_ENTRY;
- }
- /* Now the next one */
- if (NextSize)
- {
- /* Adjust size of this free entry and insert it */
- NextFreeEntry->Flags = 0;
- NextFreeEntry->PreviousSize = 0;
- NextFreeEntry->SegmentOffset = Segment->Entry.SegmentOffset;
- NextFreeEntry->Size = (USHORT)NextSize;
- ((PHEAP_FREE_ENTRY)((PHEAP_ENTRY)NextFreeEntry + NextSize))->PreviousSize = (USHORT)NextSize;
- Heap->TotalFreeSize += NextSize;
- RtlpInsertFreeBlockHelper(Heap, NextFreeEntry, NextSize, FALSE);
- }
- else if (NextInUseEntry)
- {
- NextInUseEntry->PreviousSize = 0;
- }
- }
- NTSTATUS
- NTAPI
- RtlpInitializeHeapSegment(IN OUT PHEAP Heap,
- OUT PHEAP_SEGMENT Segment,
- IN UCHAR SegmentIndex,
- IN ULONG SegmentFlags,
- IN SIZE_T SegmentReserve,
- IN SIZE_T SegmentCommit)
- {
- PHEAP_ENTRY HeapEntry;
- /* Preconditions */
- ASSERT(Heap != NULL);
- ASSERT(Segment != NULL);
- ASSERT(SegmentCommit >= PAGE_SIZE);
- ASSERT(ROUND_DOWN(SegmentCommit, PAGE_SIZE) == SegmentCommit);
- ASSERT(SegmentReserve >= SegmentCommit);
- ASSERT(ROUND_DOWN(SegmentReserve, PAGE_SIZE) == SegmentReserve);
- DPRINT("RtlpInitializeHeapSegment(%p %p %x %x %lx %lx)\n", Heap, Segment, SegmentIndex, SegmentFlags, SegmentReserve, SegmentCommit);
- /* Initialise the Heap Entry header if this is not the first Heap Segment */
- if ((PHEAP_SEGMENT) (Heap) != Segment)
- {
- Segment->Entry.Size = ROUND_UP(sizeof(HEAP_SEGMENT), sizeof(HEAP_ENTRY)) >> HEAP_ENTRY_SHIFT;
- Segment->Entry.Flags = HEAP_ENTRY_BUSY;
- Segment->Entry.SmallTagIndex = LOBYTE(Segment->Entry.Size) ^ HIBYTE(Segment->Entry.Size) ^ Segment->Entry.Flags;
- Segment->Entry.PreviousSize = 0;
- Segment->Entry.SegmentOffset = SegmentIndex;
- Segment->Entry.UnusedBytes = 0;
- }
- /* Sanity check */
- ASSERT((Segment->Entry.Size << HEAP_ENTRY_SHIFT) <= PAGE_SIZE);
- /* Initialise the Heap Segment header */
- Segment->SegmentSignature = HEAP_SEGMENT_SIGNATURE;
- Segment->SegmentFlags = SegmentFlags;
- Segment->Heap = Heap;
- Heap->Segments[SegmentIndex] = Segment;
- /* Initialise the Heap Segment location information */
- Segment->BaseAddress = Segment;
- Segment->NumberOfPages = (ULONG)(SegmentReserve >> PAGE_SHIFT);
- /* Initialise the Heap Entries contained within the Heap Segment */
- Segment->FirstEntry = &Segment->Entry + Segment->Entry.Size;
- Segment->LastValidEntry = (PHEAP_ENTRY)((ULONG_PTR)Segment + SegmentReserve);
- if (((SIZE_T)Segment->Entry.Size << HEAP_ENTRY_SHIFT) < SegmentCommit)
- {
- HeapEntry = Segment->FirstEntry;
- /* Prepare a Free Heap Entry header */
- HeapEntry->Flags = HEAP_ENTRY_LAST_ENTRY;
- HeapEntry->PreviousSize = Segment->Entry.Size;
- HeapEntry->SegmentOffset = SegmentIndex;
- /* Register the Free Heap Entry */
- RtlpInsertFreeBlock(Heap, (PHEAP_FREE_ENTRY) HeapEntry, (SegmentCommit >> HEAP_ENTRY_SHIFT) - Segment->Entry.Size);
- }
- /* Initialise the Heap Segment UnCommitted Range information */
- Segment->NumberOfUnCommittedPages = (ULONG)((SegmentReserve - SegmentCommit) >> PAGE_SHIFT);
- Segment->NumberOfUnCommittedRanges = 0;
- InitializeListHead(&Segment->UCRSegmentList);
- /* Register the UnCommitted Range of the Heap Segment */
- if (Segment->NumberOfUnCommittedPages != 0)
- RtlpInsertUnCommittedPages(Segment, (ULONG_PTR) (Segment) + SegmentCommit, SegmentReserve - SegmentCommit);
- return STATUS_SUCCESS;
- }
- VOID NTAPI
- RtlpDestroyHeapSegment(PHEAP_SEGMENT Segment)
- {
- NTSTATUS Status;
- PVOID BaseAddress;
- SIZE_T Size = 0;
- /* Make sure it's not user allocated */
- if (Segment->SegmentFlags & HEAP_USER_ALLOCATED) return;
- BaseAddress = Segment->BaseAddress;
- DPRINT("Destroying segment %p, BA %p\n", Segment, BaseAddress);
- /* Release virtual memory */
- Status = ZwFreeVirtualMemory(NtCurrentProcess(),
- &BaseAddress,
- &Size,
- MEM_RELEASE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("HEAP: Failed to release segment's memory with status 0x%08X\n", Status);
- }
- }
- /* Usermode only! */
- VOID NTAPI
- RtlpAddHeapToProcessList(PHEAP Heap)
- {
- PPEB Peb;
- /* Get PEB */
- Peb = RtlGetCurrentPeb();
- /* Acquire the lock */
- RtlEnterCriticalSection(&RtlpProcessHeapsListLock);
- //_SEH2_TRY {
- /* Check if max number of heaps reached */
- if (Peb->NumberOfHeaps == Peb->MaximumNumberOfHeaps)
- {
- // TODO: Handle this case
- ASSERT(FALSE);
- }
- /* Add the heap to the process heaps */
- Peb->ProcessHeaps[Peb->NumberOfHeaps] = Heap;
- Peb->NumberOfHeaps++;
- Heap->ProcessHeapsListIndex = (USHORT)Peb->NumberOfHeaps;
- // } _SEH2_FINALLY {
- /* Release the lock */
- RtlLeaveCriticalSection(&RtlpProcessHeapsListLock);
- // } _SEH2_END
- }
- /* Usermode only! */
- VOID NTAPI
- RtlpRemoveHeapFromProcessList(PHEAP Heap)
- {
- PPEB Peb;
- PHEAP *Current, *Next;
- ULONG Count;
- /* Get PEB */
- Peb = RtlGetCurrentPeb();
- /* Acquire the lock */
- RtlEnterCriticalSection(&RtlpProcessHeapsListLock);
- /* Check if we don't need anything to do */
- if ((Heap->ProcessHeapsListIndex == 0) ||
- (Heap->ProcessHeapsListIndex > Peb->NumberOfHeaps) ||
- (Peb->NumberOfHeaps == 0))
- {
- /* Release the lock */
- RtlLeaveCriticalSection(&RtlpProcessHeapsListLock);
- return;
- }
- /* The process actually has more than one heap.
- Use classic, lernt from university times algorithm for removing an entry
- from a static array */
- Current = (PHEAP *)&Peb->ProcessHeaps[Heap->ProcessHeapsListIndex - 1];
- Next = Current + 1;
- /* How many items we need to shift to the left */
- Count = Peb->NumberOfHeaps - (Heap->ProcessHeapsListIndex - 1);
- /* Move them all in a loop */
- while (--Count)
- {
- /* Copy it and advance next pointer */
- *Current = *Next;
- /* Update its index */
- (*Current)->ProcessHeapsListIndex -= 1;
- /* Advance pointers */
- Current++;
- Next++;
- }
- /* Decrease total number of heaps */
- Peb->NumberOfHeaps--;
- /* Zero last unused item */
- Peb->ProcessHeaps[Peb->NumberOfHeaps] = NULL;
- Heap->ProcessHeapsListIndex = 0;
- /* Release the lock */
- RtlLeaveCriticalSection(&RtlpProcessHeapsListLock);
- }
- PHEAP_FREE_ENTRY NTAPI
- RtlpCoalesceHeap(PHEAP Heap)
- {
- UNIMPLEMENTED;
- return NULL;
- }
- PHEAP_FREE_ENTRY NTAPI
- RtlpCoalesceFreeBlocks (PHEAP Heap,
- PHEAP_FREE_ENTRY FreeEntry,
- PSIZE_T FreeSize,
- BOOLEAN Remove)
- {
- PHEAP_FREE_ENTRY CurrentEntry, NextEntry;
- /* Get the previous entry */
- CurrentEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry - FreeEntry->PreviousSize);
- /* Check it */
- if (CurrentEntry != FreeEntry &&
- !(CurrentEntry->Flags & HEAP_ENTRY_BUSY) &&
- (*FreeSize + CurrentEntry->Size) <= HEAP_MAX_BLOCK_SIZE)
- {
- ASSERT(FreeEntry->PreviousSize == CurrentEntry->Size);
- /* Remove it if asked for */
- if (Remove)
- {
- RtlpRemoveFreeBlock(Heap, FreeEntry, FALSE, FALSE);
- Heap->TotalFreeSize -= FreeEntry->Size;
- /* Remove it only once! */
- Remove = FALSE;
- }
- /* Remove previous entry too */
- RtlpRemoveFreeBlock(Heap, CurrentEntry, FALSE, FALSE);
- /* Copy flags */
- CurrentEntry->Flags = FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY;
- /* Advance FreeEntry and update sizes */
- FreeEntry = CurrentEntry;
- *FreeSize = *FreeSize + CurrentEntry->Size;
- Heap->TotalFreeSize -= CurrentEntry->Size;
- FreeEntry->Size = (USHORT)(*FreeSize);
- /* Also update previous size if needed */
- if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
- {
- ((PHEAP_ENTRY)FreeEntry + *FreeSize)->PreviousSize = (USHORT)(*FreeSize);
- }
- }
- /* Check the next block if it exists */
- if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
- {
- NextEntry = (PHEAP_FREE_ENTRY)((PHEAP_ENTRY)FreeEntry + *FreeSize);
- if (!(NextEntry->Flags & HEAP_ENTRY_BUSY) &&
- NextEntry->Size + *FreeSize <= HEAP_MAX_BLOCK_SIZE)
- {
- ASSERT(*FreeSize == NextEntry->PreviousSize);
- /* Remove it if asked for */
- if (Remove)
- {
- RtlpRemoveFreeBlock(Heap, FreeEntry, FALSE, FALSE);
- Heap->TotalFreeSize -= FreeEntry->Size;
- }
- /* Copy flags */
- FreeEntry->Flags = NextEntry->Flags & HEAP_ENTRY_LAST_ENTRY;
- /* Remove next entry now */
- RtlpRemoveFreeBlock(Heap, NextEntry, FALSE, FALSE);
- /* Update sizes */
- *FreeSize = *FreeSize + NextEntry->Size;
- Heap->TotalFreeSize -= NextEntry->Size;
- FreeEntry->Size = (USHORT)(*FreeSize);
- /* Also update previous size if needed */
- if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
- {
- ((PHEAP_ENTRY)FreeEntry + *FreeSize)->PreviousSize = (USHORT)(*FreeSize);
- }
- }
- }
- return FreeEntry;
- }
- PHEAP_FREE_ENTRY NTAPI
- RtlpExtendHeap(PHEAP Heap,
- SIZE_T Size)
- {
- ULONG Pages;
- UCHAR Index, EmptyIndex;
- SIZE_T FreeSize, CommitSize, ReserveSize;
- PHEAP_SEGMENT Segment;
- PHEAP_FREE_ENTRY FreeEntry;
- NTSTATUS Status;
- DPRINT("RtlpExtendHeap(%p %x)\n", Heap, Size);
- /* Calculate amount in pages */
- Pages = (ULONG)((Size + PAGE_SIZE - 1) / PAGE_SIZE);
- FreeSize = Pages * PAGE_SIZE;
- DPRINT("Pages %x, FreeSize %x. Going through segments...\n", Pages, FreeSize);
- /* Find an empty segment */
- EmptyIndex = HEAP_SEGMENTS;
- for (Index = 0; Index < HEAP_SEGMENTS; Index++)
- {
- Segment = Heap->Segments[Index];
- if (Segment) DPRINT("Segment[%d] %p with NOUCP %x\n", Index, Segment, Segment->NumberOfUnCommittedPages);
- /* Check if its size suits us */
- if (Segment &&
- Pages <= Segment->NumberOfUnCommittedPages)
- {
- DPRINT("This segment is suitable\n");
- /* Commit needed amount */
- FreeEntry = RtlpFindAndCommitPages(Heap, Segment, &FreeSize, NULL);
- /* Coalesce it with adjacent entries */
- if (FreeEntry)
- {
- FreeSize = FreeSize >> HEAP_ENTRY_SHIFT;
- FreeEntry = RtlpCoalesceFreeBlocks(Heap, FreeEntry, &FreeSize, FALSE);
- RtlpInsertFreeBlock(Heap, FreeEntry, FreeSize);
- return FreeEntry;
- }
- }
- else if (!Segment &&
- EmptyIndex == HEAP_SEGMENTS)
- {
- /* Remember the first unused segment index */
- EmptyIndex = Index;
- }
- }
- /* No luck, need to grow the heap */
- if ((Heap->Flags & HEAP_GROWABLE) &&
- (EmptyIndex != HEAP_SEGMENTS))
- {
- Segment = NULL;
- /* Reserve the memory */
- if ((Size + PAGE_SIZE) <= Heap->SegmentReserve)
- ReserveSize = Heap->SegmentReserve;
- else
- ReserveSize = Size + PAGE_SIZE;
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID)&Segment,
- 0,
- &ReserveSize,
- MEM_RESERVE,
- PAGE_READWRITE);
- /* If it failed, retry again with a half division algorithm */
- while (!NT_SUCCESS(Status) &&
- ReserveSize != Size + PAGE_SIZE)
- {
- ReserveSize /= 2;
- if (ReserveSize < (Size + PAGE_SIZE))
- ReserveSize = Size + PAGE_SIZE;
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID)&Segment,
- 0,
- &ReserveSize,
- MEM_RESERVE,
- PAGE_READWRITE);
- }
- /* Proceed only if it's success */
- if (NT_SUCCESS(Status))
- {
- Heap->SegmentReserve += ReserveSize;
- /* Now commit the memory */
- if ((Size + PAGE_SIZE) <= Heap->SegmentCommit)
- CommitSize = Heap->SegmentCommit;
- else
- CommitSize = Size + PAGE_SIZE;
- Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
- (PVOID)&Segment,
- 0,
- &CommitSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- DPRINT("Committed %d bytes at base %p\n", CommitSize, Segment);
- /* Initialize heap segment if commit was successful */
- if (NT_SUCCESS(Status))
- Status = RtlpInitializeHeapSegment(Heap, Segment, EmptyIndex, 0, ReserveSize, CommitSize);
- /* If everything worked - cool */
- if (NT_SUCCESS(Status)) return (PHEAP_FREE_ENTRY)Segment->FirstEntry;
- DPRINT1("Committing failed with status 0x%08X\n", Status);
- /* Nope, we failed. Free memory */
- ZwFreeVirtualMemory(NtCurrentProcess(),
- (PVOID)&Segment,
- &ReserveSize,
- MEM_RELEASE);
- }
- else
- {
- DPRINT1("Reserving failed with status 0x%08X\n", Status);
- }
- }
- if (RtlpGetMode() == UserMode)
- {
- /* If coalescing on free is disabled in usermode, then do it here */
- if (Heap->Flags & HEAP_DISABLE_COALESCE_ON_FREE)
- {
- FreeEntry = RtlpCoalesceHeap(Heap);
- /* If it's a suitable one - return it */
- if (FreeEntry &&
- FreeEntry->Size >= Size)
- {
- return FreeEntry;
- }
- }
- }
- return NULL;
- }
- /***********************************************************************
- * RtlCreateHeap
- * RETURNS
- * Handle of heap: Success
- * NULL: Failure
- *
- * @implemented
- */
- HANDLE NTAPI
- RtlCreateHeap(ULONG Flags,
- PVOID Addr,
- SIZE_T TotalSize,
- SIZE_T CommitSize,
- PVOID Lock,
- PRTL_HEAP_PARAMETERS Parameters)
- {
- PVOID CommittedAddress = NULL, UncommittedAddress = NULL;
- PHEAP Heap = NULL;
- RTL_HEAP_PARAMETERS SafeParams = {0};
- PPEB Peb;
- ULONG_PTR MaximumUserModeAddress;
- SYSTEM_BASIC_INFORMATION SystemInformation;
- MEMORY_BASIC_INFORMATION MemoryInfo;
- ULONG NtGlobalFlags = RtlGetNtGlobalFlags();
- ULONG HeapSegmentFlags = 0;
- NTSTATUS Status;
- ULONG MaxBlockSize;
- /* Check for a special heap */
- if (RtlpPageHeapEnabled && !Addr && !Lock)
- {
- Heap = RtlpPageHeapCreate(Flags, Addr, TotalSize, CommitSize, Lock, Parameters);
- if (Heap) return Heap;
- /* Reset a special Parameters == -1 hack */
- if ((ULONG_PTR)Parameters == (ULONG_PTR)-1)
- Parameters = NULL;
- else
- DPRINT1("Enabling page heap failed\n");
- }
- /* Check validation flags */
- if (!(Flags & HEAP_SKIP_VALIDATION_CHECKS) && (Flags & ~HEAP_CREATE_VALID_MASK))
- {
- DPRINT1("Invalid flags 0x%08x, fixing...\n", Flags);
- Flags &= HEAP_CREATE_VALID_MASK;
- }
- /* TODO: Capture parameters, once we decide to use SEH */
- if (!Parameters) Parameters = &SafeParams;
- /* Check global flags */
- if (NtGlobalFlags & FLG_HEAP_DISABLE_COALESCING)
- Flags |= HEAP_DISABLE_COALESCE_ON_FREE;
- if (NtGlobalFlags & FLG_HEAP_ENABLE_FREE_CHECK)
- Flags |= HEAP_FREE_CHECKING_ENABLED;
- if (NtGlobalFlags & FLG_HEAP_ENABLE_TAIL_CHECK)
- Flags |= HEAP_TAIL_CHECKING_ENABLED;
- if (RtlpGetMode() == UserMode)
- {
- /* Also check these flags if in usermode */
- if (NtGlobalFlags & FLG_HEAP_VALIDATE_ALL)
- Flags |= HEAP_VALIDATE_ALL_ENABLED;
- if (NtGlobalFlags & FLG_HEAP_VALIDATE_PARAMETERS)
- Flags |= HEAP_VALIDATE_PARAMETERS_ENABLED;
- if (NtGlobalFlags & FLG_USER_STACK_TRACE_DB)
- Flags |= HEAP_CAPTURE_STACK_BACKTRACES;
- /* Get PEB */
- Peb = RtlGetCurrentPeb();
- /* Apply defaults for non-set parameters */
- if (!Parameters->SegmentCommit) Parameters->SegmentCommit = Peb->HeapSegmentCommit;
- if (!Parameters->SegmentReserve) Parameters->SegmentReserve = Peb->HeapSegmentReserve;
- if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = Peb->HeapDeCommitFreeBlockThreshold;
- if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = Peb->HeapDeCommitTotalFreeThreshold;
- }
- else
- {
- /* Apply defaults for non-set parameters */
- #if 0
- if (!Parameters->SegmentCommit) Parameters->SegmentCommit = MmHeapSegmentCommit;
- if (!Parameters->SegmentReserve) Parameters->SegmentReserve = MmHeapSegmentReserve;
- if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;
- if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
- #endif
- }
- // FIXME: Move to memory manager
- if (!Parameters->SegmentCommit) Parameters->SegmentCommit = PAGE_SIZE * 2;
- if (!Parameters->SegmentReserve) Parameters->SegmentReserve = 1048576;
- if (!Parameters->DeCommitFreeBlockThreshold) Parameters->DeCommitFreeBlockThreshold = PAGE_SIZE;
- if (!Parameters->DeCommitTotalFreeThreshold) Parameters->DeCommitTotalFreeThreshold = 65536;
- /* Get the max um address */
- Status = ZwQuerySystemInformation(SystemBasicInformation,
- &SystemInformation,
- sizeof(SystemInformation),
- NULL);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Getting max usermode address failed with status 0x%08x\n", Status);
- return NULL;
- }
- MaximumUserModeAddress = SystemInformation.MaximumUserModeAddress;
- /* Calculate max alloc size */
- if (!Parameters->MaximumAllocationSize)
- Parameters->MaximumAllocationSize = MaximumUserModeAddress - (ULONG_PTR)0x10000 - PAGE_SIZE;
- MaxBlockSize = 0x80000 - PAGE_SIZE;
- if (!Parameters->VirtualMemoryThreshold ||
- Parameters->VirtualMemoryThreshold > MaxBlockSize)
- {
- Parameters->VirtualMemoryThreshold = MaxBlockSize;
- }
- /* Check reserve/commit sizes and set default values */
- if (!CommitSize)
- {
- CommitSize = PAGE_SIZE;
- if (TotalSize)
- TotalSize = ROUND_UP(TotalSize, PAGE_SIZE);
- else
- TotalSize = 64 * PAGE_SIZE;
- }
- else
- {
- /* Round up the commit size to be at least the page size */
- CommitSize = ROUND_UP(CommitSize, PAGE_SIZE);
- if (TotalSize)
- TotalSize = ROUND_UP(TotalSize, PAGE_SIZE);
- else
- TotalSize = ROUND_UP(CommitSize, 16 * PAGE_SIZE);
- }
- /* Call special heap */
- if (RtlpHeapIsSpecial(Flags))
- return RtlDebugCreateHeap(Flags, Addr, TotalSize, CommitSize, Lock, Parameters);
- /* Without serialization, a lock makes no sense */
- if ((Flags & HEAP_NO_SERIALIZE) && (Lock != NULL))
- return NULL;
- /* See if we are already provided with an address for the heap */
- if (Addr)
- {
- if (Parameters->CommitRoutine)
- {
- /* There is a commit routine, so no problem here, check params */
- if ((Flags & HEAP_GROWABLE) ||
- !Parameters->InitialCommit ||
- !Parameters->InitialReserve ||
- (Parameters->InitialCommit > Parameters->InitialReserve))
- {
- /* Fail */
- return NULL;
- }
- /* Calculate committed and uncommitted addresses */
- CommittedAddress = Addr;
- UncommittedAddress = (PCHAR)Addr + Parameters->InitialCommit;
- TotalSize = Parameters->InitialReserve;
- /* Zero the initial page ourselves */
- RtlZeroMemory(CommittedAddress, PAGE_SIZE);
- }
- else
- {
- /* Commit routine is absent, so query how much memory caller reserved */
- Status = ZwQueryVirtualMemory(NtCurrentProcess(),
- Addr,
- MemoryBasicInformation,
- &MemoryInfo,
- sizeof(MemoryInfo),
- NULL);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Querying amount of user supplied memory failed with status 0x%08X\n", Status);
- return NULL;
- }
- /* Validate it */
- if (MemoryInfo.BaseAddress != Addr ||
- MemoryInfo.State == MEM_FREE)
- {
- return NULL;
- }
- /* Validation checks passed, set committed/uncommitted addresses */
- CommittedAddress = Addr;
- /* Check if it's committed or not */
- if (MemoryInfo.State == MEM_COMMIT)
- {
- /* Zero it out because it's already committed */
- RtlZeroMemory(Co…
Large files files are truncated, but you can click here to view the full file