コード例 #1
0
ファイル: Heap.cs プロジェクト: KSLcom/STSdb4
        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++;
        }
コード例 #2
0
        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++;
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: EkardNT/CS143B
        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;
        }
コード例 #4
0
ファイル: Space.cs プロジェクト: yuuhhe/STSdb4
        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;
            }
        }
コード例 #5
0
ファイル: Allocator.cs プロジェクト: zigaosolin/SharpMedia
        /// <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);
            }
        }
コード例 #6
0
ファイル: MemoryManager.cs プロジェクト: EkardNT/CS143B
        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;
        }
コード例 #7
0
ファイル: Allocator.cs プロジェクト: zigaosolin/SharpMedia
        /// <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);
        }
コード例 #8
0
 public Heap(string fileName, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock)
     : this(new OptimizedFileStream(fileName, FileMode.OpenOrCreate), useCompression, strategy)
 {
 }
コード例 #9
0
        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));
        }
コード例 #10
0
        public static IStorageEngine FromMemory(bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock)
        {
            var stream = new MemoryStream();

            return(STSdb.FromStream(stream, useCompression, strategy));
        }
コード例 #11
0
        public static IStorageEngine FromStream(Stream stream, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock)
        {
            IHeap heap = new Heap(stream, useCompression, strategy);

            return(FromHeap(heap));
        }
コード例 #12
0
ファイル: Space.cs プロジェクト: yuuhhe/STSdb4
 public Space()
 {
     Strategy = AllocationStrategy.FromTheCurrentBlock;
 }
コード例 #13
0
ファイル: Heap.cs プロジェクト: KSLcom/STSdb4
 public Heap(string fileName, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock)
     : this(new OptimizedFileStream(fileName, FileMode.OpenOrCreate), useCompression, strategy)
 {
 }
コード例 #14
0
ファイル: Space.cs プロジェクト: KSLcom/STSdb4
        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;
        }
コード例 #15
0
ファイル: Space.cs プロジェクト: KSLcom/STSdb4
        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;
            }
        }