/// <summary> /// Initializes a new allocator with an empty heap. /// </summary> /// <param name="heapAddress">The address where the heap will start.</param> /// <param name="requester">A delegate to request more memory.</param> public HeapAllocator(uint heapAddress, MemoryRequester requester) { this.heapAddress = heapAddress; this.setEndMem = requester; this.blocks = new List <HeapEntry>(); this.freeList = new List <HeapEntry>(); endMem = heapAddress; heapExtent = 0; }
/// <summary> /// Initializes a new allocator from a previous saved heap state. /// </summary> /// <param name="savedHeap">A byte array describing the heap state, /// as returned by the <see cref="Save"/> method.</param> /// <param name="requester">A delegate to request more memory.</param> public HeapAllocator(byte[] savedHeap, MemoryRequester requester) { this.heapAddress = BigEndian.ReadInt32(savedHeap, 0); this.setEndMem = requester; this.blocks = new List <HeapEntry>(); this.freeList = new List <HeapEntry>(); uint numBlocks = BigEndian.ReadInt32(savedHeap, 4); blocks.Capacity = (int)numBlocks; uint nextAddress = heapAddress; for (uint i = 0; i < numBlocks; i++) { uint start = BigEndian.ReadInt32(savedHeap, 8 * i + 8); uint length = BigEndian.ReadInt32(savedHeap, 8 * i + 12); blocks.Add(new HeapEntry(start, length)); if (nextAddress < start) { freeList.Add(new HeapEntry(nextAddress, start - nextAddress)); } nextAddress = start + length; } endMem = nextAddress; heapExtent = nextAddress - heapAddress; if (setEndMem(endMem) == false) { throw new ArgumentException("Can't allocate VM memory to fit saved heap"); } blocks.Sort(entryComparer); freeList.Sort(entryComparer); }