public Heap(Stream stream, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { stream.Seek(0, SeekOrigin.Begin); //support Seek? Stream = stream; space = new Space(); used = new Dictionary<long, Pointer>(); reserved = new Dictionary<long, Pointer>(); if (stream.Length < AtomicHeader.SIZE) //create new { header = new AtomicHeader(); header.UseCompression = useCompression; space.Add(new Ptr(AtomicHeader.SIZE, long.MaxValue - AtomicHeader.SIZE)); } else //open exist (ignore the useCompression flag) { header = AtomicHeader.Deserialize(Stream); stream.Seek(header.SystemData.Position, SeekOrigin.Begin); Deserialize(new BinaryReader(stream)); //manual alloc header.SystemData var ptr = space.Alloc(header.SystemData.Size); if (ptr.Position != header.SystemData.Position) throw new Exception("Logical error."); } Strategy = strategy; currentVersion++; }
public Heap(Stream stream, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { stream.Seek(0, SeekOrigin.Begin); //support Seek? Stream = stream; space = new Space(); used = new Dictionary <long, Pointer>(); reserved = new Dictionary <long, Pointer>(); if (stream.Length < AtomicHeader.SIZE) //create new { header = new AtomicHeader(); header.UseCompression = useCompression; space.Add(new Ptr(AtomicHeader.SIZE, long.MaxValue - AtomicHeader.SIZE)); } else //open exist (ignore the useCompression flag) { header = AtomicHeader.Deserialize(Stream); stream.Seek(header.SystemData.Position, SeekOrigin.Begin); Deserialize(new BinaryReader(stream)); //manual alloc header.SystemData var ptr = space.Alloc(header.SystemData.Size); if (ptr.Position != header.SystemData.Position) { throw new Exception("Logical error."); } } Strategy = strategy; currentVersion++; }
private static void Driver( double averageRequestSize, double standardDeviation, Random random, int simulationSteps, int memorySize, AllocationStrategy strategy, out double averageSearchTime, out double averageMemoryUtilization) { // Setup driver. var mm = new MemoryManager(memorySize, strategy); var reserved = new List<Allocation>(); // Statistics long totalHolesExamined = 0; double totalMemoryUtilization = 0; for (int i = 0; i < simulationSteps; i++) { int requestSize = random.NextGaussian(averageRequestSize, standardDeviation, 1, memorySize); int allocationAddr, holesExamined; Allocation alloc; while (mm.Request(requestSize, out allocationAddr, out holesExamined)) { reserved.Add(alloc = new Allocation(allocationAddr, requestSize)); int placeToSwap = random.Next(reserved.Count); reserved[reserved.Count - 1] = reserved[placeToSwap]; reserved[placeToSwap] = alloc; totalHolesExamined += holesExamined; requestSize = random.NextGaussian(averageRequestSize, standardDeviation, 1, memorySize); } // Count holes examined by failed request. totalHolesExamined += holesExamined; // Record memory utilization. totalMemoryUtilization += reserved.Sum(allocation => allocation.Size) / (double)memorySize; // Release a random reserved segment. Because the reserved list // is randomly ordered, we simply (and efficiently) remove the // last element. if(reserved.Count > 0) { mm.Release(reserved[reserved.Count - 1].Address); reserved.RemoveAt(reserved.Count - 1); } } averageSearchTime = totalHolesExamined / (double) simulationSteps; averageMemoryUtilization = totalMemoryUtilization / simulationSteps; }
public void Deserealize(BinaryReader reader) { Strategy = (AllocationStrategy)reader.ReadByte(); activeChunkIndex = reader.ReadInt32(); int count = reader.ReadInt32(); free.Clear(); FreeBytes = 0; for (int i = 0; i < count; i++) { var ptr = Ptr.Deserialize(reader); free.Add(ptr); FreeBytes += ptr.Size; } }
/// <summary> /// Allocates the blocks. Must still be commited (this is journal responsibility). /// </summary> /// <param name="who">Who is the allocator.</param> /// <param name="hint">The hint block, or 0.</param> /// <param name="strategy">The allocation strategy.</param> /// <param name="count">Number of blocks to return.</param> /// <returns></returns> public List <ulong> Allocate(object who, ulong hint, AllocationStrategy strategy, uint count) { lock (syncRoot) { // Finds free blocks. List <AllocationBlockInfo> lockedBlocks = new List <AllocationBlockInfo>(); List <ulong> returnData = FindFreeBlocks(who, hint, strategy, count, lockedBlocks); // We add operation data. lockingList.Add(new OperationLockData(who, lockedBlocks)); Console.WriteLine("{0} allocated {1} : {2} blocks", who, count, Common.ArrayToString(returnData)); return(returnData); } }
public MemoryManager(int memorySize, AllocationStrategy strategy) { if (memorySize < OverheadPerFreeSegment) throw new ArgumentOutOfRangeException("memorySize", string.Format("memorySize must be at least {0}, the amount of overhead per hole.", OverheadPerFreeSegment)); if (strategy == null) throw new ArgumentNullException("strategy"); this.strategy = strategy; // Initialize memory to single free segment. mainMemory = new int[memorySize]; headFreeSegment = 0; // Tags, where the negative means this is a free segment. mainMemory[headFreeSegment] = -memorySize; mainMemory[EndTagAddr(headFreeSegment)] = -memorySize; // Prev and Next pointers for a circular linked list. mainMemory[PrevPtrAddr(headFreeSegment)] = mainMemory[NextPtrAddr(headFreeSegment)] = 0; }
/// <summary> /// Finds the free blocks. /// </summary> /// <param name="hint">The hint.</param> /// <param name="strategy">The strategy.</param> /// <param name="count">The count.</param> /// <returns></returns> List <ulong> FindFreeBlocks(object op, ulong hint, AllocationStrategy strategy, uint count, List <AllocationBlockInfo> lockedBlocks) { // We load blocks in vicinity. If there is no hint, we chose one. if (hint == 0) { // We choose the "default" key. uint unused; // We must extract first allocation block, the only way is to use the enumerator. IEnumerator <KeyValuePair <ulong, AllocationBlockInfo> > en = inspectedFreeBlocks.GetEnumerator(); en.MoveNext(); hint = inspectedFreeBlocks.Count > 0 ? en.Current.Key : BlockHelper.GetAllocationBlock(provider.BlockSize, 1, out unused); } // TODO Super blocks. List <ulong> freeBlocks = new List <ulong>(); // We begin allocation (forward from hint) uint unused2; ulong block = BlockHelper.GetAllocationBlock(provider.BlockSize, hint, out unused2); for (; count > 0; block = BlockHelper.GetNextAllocationBlock(provider.BlockSize, block, blockCount)) { AddBlock(op, block, freeBlocks, lockedBlocks, ref count); } // And backwards if none found. for (block = BlockHelper.GetPrevAllocationBlock(provider.BlockSize, block); count > 0; block = BlockHelper.GetPrevAllocationBlock(provider.BlockSize, block)) { AddBlock(op, block, freeBlocks, lockedBlocks, ref count); } return(freeBlocks); }
public Heap(string fileName, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) : this(new OptimizedFileStream(fileName, FileMode.OpenOrCreate), useCompression, strategy) { }
public static IStorageEngine FromFile(string fileName, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { var stream = new OptimizedFileStream(fileName, FileMode.OpenOrCreate); return(STSdb.FromStream(stream, useCompression, strategy)); }
public static IStorageEngine FromMemory(bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { var stream = new MemoryStream(); return(STSdb.FromStream(stream, useCompression, strategy)); }
public static IStorageEngine FromStream(Stream stream, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { IHeap heap = new Heap(stream, useCompression, strategy); return(FromHeap(heap)); }
public Space() { Strategy = AllocationStrategy.FromTheCurrentBlock; }
private List<Ptr> free = new List<Ptr>(); //free chunks are always: ordered by position, not overlapped & not contiguous #endregion Fields #region Constructors public Space() { Strategy = AllocationStrategy.FromTheCurrentBlock; }