Exemple #1
0
        private void FreeBlocks(BlockRef block)
        {
            int free = (block.Section * BlocksPerSection) + block.Offset;

            if (free > 0)
            {
                _firstFreeBlock = Math.Min(_firstFreeBlock, free);

                if (block.ActualBlocks == 16)
                {
                    using (_sections[block.Section].Read(ref block, true, _fget))
                    { }
                    if (((block.Count < 16 && block.ActualBlocks != block.Count) ||
                         (block.Count == 16 && block.ActualBlocks < 16)))
                    {
                        throw new InvalidDataException();
                    }
                }

                for (int i = 0; i < block.ActualBlocks; i++)
                {
                    _freeBlocks.Add(free + i);
                }
            }
        }
        private void AddSection()
        {
            FileSection n = new FileSection(_sections.Length, BlockSize);

            lock (_sync)
            {
                n.Commit(_fput, false);
                n.Commit(_fput, true);
            }

            FileSection[] grow = new FileSection[_sections.Length + 1];
            _sections.CopyTo(grow, 0);
            grow[_sections.Length] = n;

            OrdinalList freeblocks = _freeBlocks.Clone();

            freeblocks.Ceiling = (grow.Length * BlocksPerSection) - 1;

            OrdinalList freehandles = _freeHandles.Clone();

            freehandles.Ceiling = (grow.Length * BlocksPerSection) - 1;
            // First and last handles/blocks are reserved by the section
            int lastFree  = grow.Length * BlocksPerSection - 1;
            int firstFree = lastFree - BlocksPerSection + 2;

            for (int i = firstFree; i < lastFree; i++)
            {
                freehandles.Add(i);
                freeblocks.Add(i);
            }

            _sections    = grow;
            _freeHandles = freehandles;
            _freeBlocks  = freeblocks;
        }
        public void TestClone()
        {
            OrdinalList lista = new OrdinalList(new int[] { 0 });
            OrdinalList listb = (OrdinalList) ((ICloneable) lista).Clone();
            Assert.IsFalse(ReferenceEquals(lista, listb));
            Assert.AreEqual(lista.Count, listb.Count);
            Assert.IsTrue(listb.Contains(0));

            listb.Add(1);
            Assert.IsTrue(listb.Contains(1));
            Assert.IsFalse(lista.Contains(1));
        }
		public void TestBasics()
		{
            OrdinalList list = new OrdinalList();
            Assert.IsFalse(list.IsReadOnly);
            list.Ceiling = 0;

			for (int i = 512; i >= 0; i--)
				list.Add(i);

            int offset = 0;
            foreach (int item in list)
                Assert.AreEqual(offset++, item);

            Assert.AreEqual(513, offset);
            Assert.AreEqual(513, list.Count);
			Assert.AreEqual(519, list.Ceiling);

            list.Clear();
            list.AddRange(new int[] { 5, 10, 20 });
            list.AddRange(new int[] { });

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(23, list.Ceiling);

            Assert.IsTrue(list.Contains(20));
            Assert.IsTrue(list.Remove(20));

            Assert.IsFalse(list.Contains(20));
            Assert.IsFalse(list.Remove(20));

            Assert.AreEqual(2, list.Count);

            int[] items = new int[2];
            list.CopyTo(items, 0);
            Assert.AreEqual(5, items[0]);
            Assert.AreEqual(10, items[1]);

            items = list.ToArray();
            Assert.AreEqual(5, items[0]);
            Assert.AreEqual(10, items[1]);

            byte[] bits = list.ToByteArray();
            Assert.AreEqual(3, bits.Length);
            Assert.AreEqual(2, new OrdinalList(bits).Count);

            List<int> tmp = new List<int>();
            foreach (int i in list)
                tmp.Add(i);
            Assert.AreEqual(2, tmp.Count);
            Assert.AreEqual(5, tmp[0]);
            Assert.AreEqual(10, tmp[1]);
        }
Exemple #5
0
        /// <summary>
        /// Deletes the handle and frees the associated block space for reuse.
        /// </summary>
        public void Delete(uint handle)
        {
            HandleRef href = new HandleRef(handle, BlockSize);

            if (handle == 0 || href.Section >= _sections.Length || _freeHandles.Contains((int)handle))
            {
                throw new ArgumentOutOfRangeException("handle");
            }

            uint oldblockId = _sections[href.Section][href.Offset];

            lock (_sync)
            {
                _sections[href.Section].SetHandle(_fcommit, href.Offset, 0);

                if (oldblockId != 0)
                {
                    FreeBlocks(new BlockRef(oldblockId, BlockSize));
                }
                _freeHandles.Add((int)handle);
                _prevFreeHandle = Math.Min(_prevFreeHandle, (int)handle);
            }
        }
Exemple #6
0
        /// <summary>
        /// Forwards along an OSC message from any source.
        /// </summary>
        public void Add(OscDataMessage osc)
        {
#warning all this routing should be handled by the kernel, not just NotifyImplants


            // add to log when enabled
            if (LoggingEnabled)
            {
                Log.Add(new OscLog()
                {
                    Source = osc.Source, Message = osc
                });
            }

            // forward the OSC message to all other subsystems
            OscToMidi(osc);
            TransmitOSC(osc);
            if (osc.Source != "Cluster")
            {
                ToCluster(osc);
            }
            NotifyImplants(osc);
        }
        public void ConcurrencyTest()
        {
            using (TempFile temp = new TempFile())
                using (ManualResetEvent stop = new ManualResetEvent(false))
                    using (TempFile copy = new TempFile())
                        using (TransactedCompoundFile test = new TransactedCompoundFile(
                                   new TransactedCompoundFile.Options(temp.TempPath)
                        {
                            BlockSize = 512, CreateNew = true
                        }))
                            using (WorkQueue workers = new WorkQueue(5))
                            {
                                bool failed = false;
                                workers.OnError += (o, e) => failed = true;
                                for (int i = 0; i < 5; i++)
                                {
                                    workers.Enqueue(() => ExersizeFile(stop, test));
                                }

                                do
                                {
                                    System.Threading.Thread.Sleep(1000);
                                    test.Commit();
                                    File.Copy(temp.TempPath, copy.TempPath, true);
                                    Assert.AreEqual(0, copy.Length % 512);
                                    int hcount = (int)(copy.Length / 512);

                                    using (TransactedCompoundFile verify = new TransactedCompoundFile(
                                               new TransactedCompoundFile.Options(copy.TempPath)
                                    {
                                        BlockSize = 512, CreateNew = false
                                    }))
                                    {
                                        OrdinalList free = new OrdinalList();
                                        free.Ceiling = hcount;
                                        for (int i = 0; i < hcount; i++)
                                        {
                                            uint h = verify.Create();
                                            free.Add((int)h);
                                            if (h >= hcount)
                                            {
                                                break;
                                            }
                                        }

                                        int         verifiedCount = 0;
                                        OrdinalList used          = free.Invert(hcount);
                                        foreach (uint h in used)
                                        {
                                            // skip reserved offsets.
                                            if (h % (512 / 4) == 0 || (h + 1) % (512 / 4) == 0)
                                            {
                                                continue;
                                            }

                                            IOStream.ReadAllBytes(verify.Read(h));
                                            verifiedCount++;
                                        }
                                        System.Diagnostics.Trace.WriteLine("Verified handle count: " + verifiedCount);
                                    }
                                } while (!failed && System.Diagnostics.Debugger.IsAttached);

                                stop.Set();
                                workers.Complete(false, 1000);
                                Assert.IsFalse(failed);
                            }
        }