示例#1
0
文件: Model.cs 项目: sjvannTMU/Sage
        /// <summary>
        /// Creates a model with a specified name and Guid.
        /// </summary>
        /// <param name="name">The name for the new model.</param>
        /// <param name="guid">The guid for the new model.</param>
        public Model(string name, Guid guid)
        {
            m_name = name;
            m_guid = guid;

            IsRunning   = false;
            IsPaused    = false;
            IsCompleted = false;
            IsReady     = false;

            m_services = new Dictionary <Type, Dictionary <string, object> >();

            Exec           = CreateModelExecutive();
            m_stateMachine = CreateStateMachine();

            m_taskProcessors = new Hashtable();
#if CREATION_CONTEXTS
            m_creationContexts = new WeakList();
#endif
            m_parameters = new Hashtable();

            if (s_dumpErrors)
            {
                ErrorHappened += new ErrorEvent(Model_ErrorHappened);
                ErrorCleared  += new ErrorEvent(Model_ErrorCleared);
            }
            if (s_dumpWarnings)
            {
                WarningHappened += new WarningEvent(Model_WarningHappened);
            }

            m_modelConfig  = new ModelConfig();
            m_modelObjects = new ModelObjectDictionary();
        }
    public static void PurgeTest()
    {
        var myList = new WeakList <string>();
        // construct strings like this to prevent interning
        string s1 = new string(new[] { 'h', 'e', 'l', 'l', 'o' });
        string s2 = new string(new[] { 'h', 'e', 'l', 'l', 'o', '2' });
        // this string can be interned, we don't want it to be collected anyway
        string s3 = "hello world 3";

        myList.Add(s1);
        myList.Add(s2);
        myList.Add(s3);
        // set to null for that to work even in application built with "Debug"
        // in Release it will work without setting to null
        s1 = null;
        s2 = null;
        // force GC collection
        GC.Collect(2, GCCollectionMode.Forced);
        // now s1 and s2 are away
        myList.Purge();
        // invoke your enumerator
        var left = myList.ToArray();

        // should contain 1 item and that item should be s3
        Assert.That(left.Length == 1);
        Assert.AreEqual(s3, left[0]);
    }
        public void TestIterateWithRemoveSkipsInvalidated()
        {
            object obj  = new object();
            object obj2 = new object();
            object obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (object item in list)
            {
                if (count == 0)
                {
                    list.Remove(obj2);
                }

                Assert.That(item, Is.Not.EqualTo(obj2));

                count++;
            }

            Assert.That(count, Is.EqualTo(2));

            GC.KeepAlive(obj);
            GC.KeepAlive(obj2);
            GC.KeepAlive(obj3);
        }
 protected abstract AssemblyData CreateAssemblyDataForFile(
     PEAssembly assembly,
     WeakList <IAssemblySymbol> cachedSymbols,
     DocumentationProvider documentationProvider,
     string sourceAssemblySimpleName,
     MetadataImportOptions importOptions,
     bool embedInteropTypes);
        public void TestAddAfterRemoveFromEnd()
        {
            var objects = new List <object>
            {
                new object(),
                new object(),
                new object(),
            };

            object newLastObject = new object();

            var list = new WeakList <object>();

            foreach (object o in objects)
            {
                list.Add(o);
            }

            list.Remove(objects[2]);
            list.Add(newLastObject);

            Assert.That(list.Count(), Is.EqualTo(3));
            Assert.That(list, Does.Contain(objects[0]));
            Assert.That(list, Does.Contain(objects[0]));
            Assert.That(list, Does.Not.Contain(objects[2]));
            Assert.That(list, Does.Contain(newLastObject));
        }
示例#6
0
        public void Errors()
        {
            var list = new WeakList <object>();

            Assert.Throws <ArgumentOutOfRangeException>(() => list.GetWeakReference(-1));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.GetWeakReference(0));
        }
        /// <summary>
        /// Add the IrecieveMessageEntity in a tag (create the tag if doesnt exists)
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="Agente"></param>
        public void AddgrouptagRecieveEntity(string tag, IRecieveMessageEntity Agente)
        {
            System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(tag));
            System.Diagnostics.Debug.Assert(Agente != null);
            if (!IdEntity.ContainsKey(Agente.GetId()))
            {
                throw new Exception("The following instance is not in the mapper, add it beforing calling this: " + Agente.ToString());
            }

            if (tagRecieveEntity.ContainsKey(tag))
            {
                tagRecieveEntity[tag].Add(Agente);
            }
            else
            {
                WeakList <IRecieveMessageEntity> l = new WeakList <IRecieveMessageEntity>();
                l.Add(Agente);
                tagRecieveEntity.Add(tag, l);
            }

            if (recieveEntityTag.ContainsKey(Agente.GetId()))
            {
                recieveEntityTag[Agente.GetId()].Add(tag);
            }
            else
            {
                recieveEntityTag[Agente.GetId()] = new List <string>();
                recieveEntityTag[Agente.GetId()].Add(tag);
            }
        }
示例#8
0
        public void ResizeCompactsFirstFourth()
        {
            var a = Create("A");
            var b = Create("B");

            var list = new WeakList <object>();

            for (int i = 0; i < 8; i++)
            {
                Add(list, a);
            }

            Add(list, b);
            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.AssertReleased();

            Add(list, b); // shrinks, #alive < length/4
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(2, list.WeakCount);

            b.AssertReleased();

            list.ToArray(); // shrinks, #alive == 0
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(0, list.WeakCount);
        }
示例#9
0
        public void ResizeCompactsAllAlive()
        {
            var b = Create("B");

            var list = new WeakList <object>();

            for (int i = 0; i < 9; i++)
            {
                Add(list, b);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            Add(list, b);                                                       // expands #alive > 3/4 length
            Assert.Equal(9 * 2 + 1, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(10, list.WeakCount);

            for (int i = 0; i < list.TestOnly_UnderlyingArray.Length; i++)
            {
                if (i < 10)
                {
                    Assert.Same(b.GetReference(), list.TestOnly_UnderlyingArray[i].GetTarget());
                }
                else
                {
                    Assert.Null(list.TestOnly_UnderlyingArray[i]);
                }
            }

            GC.KeepAlive(b);
        }
        public void TestUnloadViaScroll()
        {
            WeakList <Container> references = new WeakList <Container>();

            AddStep("populate panels", () =>
            {
                references.Clear();

                for (int i = 0; i < 16; i++)
                {
                    flow.Add(new Container
                    {
                        Size     = new Vector2(128),
                        Children = new Drawable[]
                        {
                            new DelayedLoadUnloadWrapper(() =>
                            {
                                var container = new Container
                                {
                                    RelativeSizeAxes = Axes.Both,
                                    Children         = new Drawable[]
                                    {
                                        new TestBox {
                                            RelativeSizeAxes = Axes.Both
                                        }
                                    },
                                };

                                references.Add(container);

                                return(container);
                            }, 500, 2000),
                            new SpriteText {
                                Text = i.ToString()
                            },
                        }
                    });
                }

                flow.Add(
                    new Container
                {
                    Size = new Vector2(128, 1280),
                });
            });

            AddUntilStep("references loaded", () => references.Count() == 16 && references.All(c => c.IsLoaded));

            AddStep("scroll to end", () => scroll.ScrollToEnd());

            AddUntilStep("references lost", () =>
            {
                GC.Collect();
                return(!references.Any());
            });

            AddStep("scroll to start", () => scroll.ScrollToStart());

            AddUntilStep("references restored", () => references.Count() == 16);
        }
示例#11
0
        public void TestIterateWithRemoveSkipsInvalidated()
        {
            var obj  = new object();
            var obj2 = new object();
            var obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (var item in list)
            {
                if (count == 0)
                {
                    list.Remove(obj2);
                }

                Assert.AreNotEqual(obj2, item);

                count++;
            }

            Assert.AreEqual(2, count);
        }
 protected abstract AssemblyData CreateAssemblyDataForFile(
     Assembly assembly,
     WeakList <IAssemblySymbol> cachedSymbols,
     DocumentationProvider documentationProvider,
     string sourceAssemblySimpleName,
     bool alwaysImportInternameMembers,
     bool embedInteropTypes);
        /// <summary>
        /// Creates an ArchiveList
        /// </summary>
        /// <param name="settings">The settings for the archive list. Null will revert to a default setting.</param>
        public ArchiveList(ArchiveListSettings settings = null)
        {
            if (settings == null)
            {
                settings = new ArchiveListSettings();
            }
            m_settings = settings.CloneReadonly();
            m_settings.Validate();

            m_syncRoot                            = new object();
            m_fileSummaries                       = new SortedList <Guid, ArchiveTableSummary <TKey, TValue> >();
            m_allSnapshots                        = new WeakList <ArchiveListSnapshot <TKey, TValue> >();
            m_listLog                             = new ArchiveListLog(m_settings.LogSettings);
            m_filesToDelete                       = new List <SortedTreeTable <TKey, TValue> >();
            m_filesToDispose                      = new List <SortedTreeTable <TKey, TValue> >();
            m_processRemovals                     = new ScheduledTask(ThreadingMode.DedicatedBackground);
            m_processRemovals.Running            += ProcessRemovals_Running;
            m_processRemovals.Disposing          += ProcessRemovals_Disposing;
            m_processRemovals.UnhandledException += ProcessRemovals_UnhandledException;

            AttachFileOrPath(m_settings.ImportPaths);

            HashSet <Guid> files = new HashSet <Guid>(m_filesToDelete.Select(x => x.ArchiveId));

            m_listLog.ClearCompletedLogs(files);
        }
示例#14
0
            public ClientDatabase(SnapServerDatabase <TKey, TValue> server, SnapClient client, Action <ClientDatabaseBase> onDispose)
            {
                if ((object)server == null)
                {
                    throw new ArgumentNullException("server");
                }
                if ((object)client == null)
                {
                    throw new ArgumentNullException("client");
                }
                if ((object)onDispose == null)
                {
                    throw new ArgumentNullException("onDispose");
                }
                if (!ReferenceEquals(client, onDispose.Target))
                {
                    throw new ArgumentException("Does not reference a method in clientHost", "onDispose");
                }

                m_syncRoot    = new object();
                m_openStreams = new WeakList <SequentialReaderStream <TKey, TValue> >();
                m_server      = server;
                m_client      = (SnapServer.Client)client;
                m_onDispose   = onDispose;
            }
示例#15
0
        public void TestRemoveAllUsingRemoveAtFromEnd()
        {
            var objects = new List <object>
            {
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
            };

            var list = new WeakList <object>();

            foreach (object o in objects)
            {
                list.Add(o);
            }

            for (int i = 0; i < objects.Count; i++)
            {
                list.RemoveAt(list.Count() - 1);
            }

            Assert.That(list.Count(), Is.Zero);
        }
示例#16
0
        public void TestIterateWithRemove()
        {
            object obj  = new object();
            object obj2 = new object();
            object obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (object item in list)
            {
                if (count == 1)
                {
                    list.Remove(item);
                }
                count++;
            }

            Assert.That(count, Is.EqualTo(3));

            Assert.That(list, Does.Contain(obj));
            Assert.That(list, Does.Not.Contain(obj2));
            Assert.That(list, Does.Contain(obj3));

            GC.KeepAlive(obj);
            GC.KeepAlive(obj2);
            GC.KeepAlive(obj3);
        }
示例#17
0
        public void ResizeCompactsFirstFourth()
        {
            var a = Create("A");
            var b = Create("B");

            var list = new WeakList <object>();

            for (int i = 0; i < 8; i++)
            {
                Add(list, a);
            }

            Add(list, b);
            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.Strong = null;
            while (a.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            Add(list, b); // shrinks, #alive < length/4
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(2, list.WeakCount);

            b.Strong = null;
            while (b.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            list.ToArray(); // shrinks, #alive == 0
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(0, list.WeakCount);
        }
示例#18
0
        public void TestRemoveObjectsAtSides()
        {
            var objects = new List <object>
            {
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
            };

            var list = new WeakList <object>();

            foreach (object o in objects)
            {
                list.Add(o);
            }

            list.Remove(objects[0]);
            list.Remove(objects[1]);
            list.Remove(objects[4]);
            list.Remove(objects[5]);

            Assert.That(list.Count(), Is.EqualTo(2));
            Assert.That(list, Does.Contain(objects[2]));
            Assert.That(list, Does.Contain(objects[3]));
        }
		public void TestForEach()
		{
			// make sure it enumerates
			var l = new WeakList<string> { "hello" };
			var count = 0;
			l.ForEach(x => count++);
			Assert.AreEqual(1, count);
		}
示例#20
0
        public void Test()
        {
            Random rand = new Random(3);

            List <string>     list1 = new List <string>();
            WeakList <string> list2 = new WeakList <string>();

            for (int x = 0; x < 1000; x++)
            {
                string str = x.ToString();
                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }

            for (int x = 1000; x < 2000; x++)
            {
                string str        = x.ToString();
                string removeItem = list1[rand.Next(list1.Count)];
                list1.Remove(removeItem);
                list2.Remove(removeItem);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }

                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }

            for (int x = 0; x < 100; x++)
            {
                list1.RemoveAt(rand.Next(list1.Count));
                GC.Collect();

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }


            list2.Clear();
            foreach (string data in list2)
            {
                throw new Exception();
            }
        }
示例#21
0
        public void EnumeratorCompacts()
        {
            var a = Create("a");
            var b = Create("B");
            var c = Create("C");
            var d = Create("D");
            var e = Create("E");

            var list = new WeakList <object>();

            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);

            Add(list, a);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, b);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, c);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, d);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, e);
            Assert.Equal(2 * 4 + 1, list.TestOnly_UnderlyingArray.Length);

            Assert.Equal(5, list.WeakCount);

            a.Strong = null;
            c.Strong = null;
            d.Strong = null;
            e.Strong = null;

            while (a.Weak.IsAlive || c.Weak.IsAlive || d.Weak.IsAlive || e.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            Assert.Equal(5, list.WeakCount);
            Assert.Same(null, list.GetWeakReference(0).GetTarget());
            Assert.Same(b.Strong, list.GetWeakReference(1).GetTarget());
            Assert.Same(null, list.GetWeakReference(2).GetTarget());
            Assert.Same(null, list.GetWeakReference(3).GetTarget());
            Assert.Same(null, list.GetWeakReference(4).GetTarget());

            var array = list.ToArray();

            Assert.Equal(1, array.Length);
            Assert.Same(b.Strong, array[0]);

            // list was compacted:
            Assert.Equal(1, list.WeakCount);
            Assert.Same(b.Strong, list.GetWeakReference(0).GetTarget());
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            GC.KeepAlive(b.Strong);
        }
示例#22
0
 // creates a copy
 private AssemblyMetadata(AssemblyMetadata other)
     : base(isImageOwner: false, id: other.Id)
 {
     this.CachedSymbols = other.CachedSymbols;
     _lazyData          = other._lazyData;
     _moduleFactoryOpt  = other._moduleFactoryOpt;
     _initialModules    = other._initialModules;
     // Leave lazyPublishedModules unset. Published modules will be set and copied as needed.
 }
        /// <summary>
        /// Creates an empty server instance
        /// </summary>
        public SnapServer()
            : base(MessageClass.Framework)
        {
            m_sockets = new Dictionary<IPEndPoint, SnapSocketListener>();
            m_clients = new WeakList<Client>();
            m_databases = new Dictionary<string, SnapServerDatabaseBase>();

            Log.Publish(MessageLevel.Info, "Server Constructor Called");
        }
示例#24
0
        public void EnumeratorCompacts()
        {
            var a = Create("a");
            var b = Create("B");
            var c = Create("C");
            var d = Create("D");
            var e = Create("E");

            var list = new WeakList<object>();
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);

            Add(list, a);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, b);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, c);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, d);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, e);
            Assert.Equal(2 * 4 + 1, list.TestOnly_UnderlyingArray.Length);

            Assert.Equal(5, list.WeakCount);

            a.Strong = null;
            c.Strong = null;
            d.Strong = null;
            e.Strong = null;

            while (a.Weak.IsAlive || c.Weak.IsAlive || d.Weak.IsAlive || e.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            Assert.Equal(5, list.WeakCount);
            Assert.Same(null, list.GetWeakReference(0).GetTarget());
            Assert.Same(b.Strong, list.GetWeakReference(1).GetTarget());
            Assert.Same(null, list.GetWeakReference(2).GetTarget());
            Assert.Same(null, list.GetWeakReference(3).GetTarget());
            Assert.Same(null, list.GetWeakReference(4).GetTarget());

            var array = list.ToArray();

            Assert.Equal(1, array.Length);
            Assert.Same(b.Strong, array[0]);

            // list was compacted:
            Assert.Equal(1, list.WeakCount);
            Assert.Same(b.Strong, list.GetWeakReference(0).GetTarget());
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            GC.KeepAlive(b.Strong);
        }
示例#25
0
        /// <summary>
        /// Creates an empty server instance
        /// </summary>
        public SnapServer()
            : base(MessageClass.Framework)
        {
            m_sockets   = new Dictionary <IPEndPoint, SnapSocketListener>();
            m_clients   = new WeakList <Client>();
            m_databases = new Dictionary <string, SnapServerDatabaseBase>();

            Log.Publish(MessageLevel.Info, "Server Constructor Called");
        }
示例#26
0
        public void TestAddedObjectIsContained()
        {
            var obj  = new object();
            var list = new WeakList <object> {
                obj
            };

            Assert.That(list, Contains.Item(obj));
        }
示例#27
0
        protected void AddWeakReference(WeakReference <Bindable <T> > weakReference)
        {
            if (Bindings == null)
            {
                Bindings = new WeakList <Bindable <T> >();
            }

            Bindings.Add(weakReference);
        }
示例#28
0
 // creates a copy
 private AssemblyMetadata(AssemblyMetadata other)
     : base(isImageOwner: false, id: other.Id)
 {
     this.CachedSymbols = other.CachedSymbols;
     _lazyData = other._lazyData;
     _moduleFactoryOpt = other._moduleFactoryOpt;
     _initialModules = other._initialModules;
     // Leave lazyPublishedModules unset. Published modules will be set and copied as needed.
 }
示例#29
0
        public void TestAddedWeakReferenceIsContained()
        {
            var obj     = new object();
            var weakRef = new WeakReference <object>(obj);
            var list    = new WeakList <object> {
                weakRef
            };

            Assert.That(list.Contains(weakRef), Is.True);
        }
示例#30
0
            public CachedAssembly(AssemblyMetadata metadata)
            {
                Debug.Assert(metadata != null);

                // Save a reference to the cached symbols so that they don't get collected
                // if the metadata object gets collected.
                this.CachedSymbols = metadata.CachedSymbols;

                this.Metadata = new WeakReference <AssemblyMetadata>(metadata);
            }
示例#31
0
        public void TestForEach()
        {
            // make sure it enumerates
            var l = new WeakList <string> {
                "hello"
            };
            var count = 0;

            l.ForEach(x => count++);
            Assert.AreEqual(1, count);
        }
        public void TestRemovedStillUnload()
        {
            WeakList <Container> references = new WeakList <Container>();

            AddStep("populate panels", () =>
            {
                references.Clear();

                for (int i = 0; i < 16; i++)
                {
                    flow.Add(new Container
                    {
                        Size     = new Vector2(128),
                        Children = new Drawable[]
                        {
                            new DelayedLoadUnloadWrapper(() =>
                            {
                                var container = new Container
                                {
                                    RelativeSizeAxes = Axes.Both,
                                    Children         = new Drawable[]
                                    {
                                        new TestBox {
                                            RelativeSizeAxes = Axes.Both
                                        }
                                    },
                                };

                                references.Add(container);

                                return(container);
                            }, 500, 2000),
                            new SpriteText {
                                Text = i.ToString()
                            },
                        }
                    });
                }
            });

            AddUntilStep("references loaded", () => references.Count() == 16 && references.All(c => c.IsLoaded));

            AddAssert("check schedulers present", () => scroll.Scheduler.HasPendingTasks);

            AddStep("Remove all panels", () => flow.Clear(false));

            AddUntilStep("repeating schedulers removed", () => !scroll.Scheduler.HasPendingTasks);

            AddUntilStep("references lost", () =>
            {
                GC.Collect();
                return(!references.Any());
            });
        }
示例#33
0
        public void TestEnumerate()
        {
            object obj  = new object();
            var    list = new WeakList <object> {
                obj
            };

            Assert.That(list.Count(), Is.EqualTo(1));
            Assert.That(list, Does.Contain(obj));

            GC.KeepAlive(obj);
        }
示例#34
0
        public void EnumeratorCompacts()
        {
            var a = Create("a");
            var b = Create("B");
            var c = Create("C");
            var d = Create("D");
            var e = Create("E");

            var list = new WeakList <object>();

            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);

            Add(list, a);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, b);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, c);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, d);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, e);
            Assert.Equal(2 * 4 + 1, list.TestOnly_UnderlyingArray.Length);

            Assert.Equal(5, list.WeakCount);

            a.AssertReleased();
            c.AssertReleased();
            d.AssertReleased();
            e.AssertReleased();

            Assert.Equal(5, list.WeakCount);
            Assert.Same(null, list.GetWeakReference(0).GetTarget());
            Assert.Same(b.GetReference(), list.GetWeakReference(1).GetTarget());
            Assert.Same(null, list.GetWeakReference(2).GetTarget());
            Assert.Same(null, list.GetWeakReference(3).GetTarget());
            Assert.Same(null, list.GetWeakReference(4).GetTarget());

            var array = list.ToArray();

            Assert.Equal(1, array.Length);
            Assert.Same(b.GetReference(), array[0]);

            // list was compacted:
            Assert.Equal(1, list.WeakCount);
            Assert.Same(b.GetReference(), list.GetWeakReference(0).GetTarget());
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            GC.KeepAlive(b.GetReference());
        }
示例#35
0
        public void EnumeratorCompacts()
        {
            var a = Create("a");
            var b = Create("B");
            var c = Create("C");
            var d = Create("D");
            var e = Create("E");

            var list = new WeakList<object>();
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);

            Add(list, a);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, b);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, c);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, d);
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            Add(list, e);
            Assert.Equal(2 * 4 + 1, list.TestOnly_UnderlyingArray.Length);

            Assert.Equal(5, list.WeakCount);

            a.AssertReleased();
            c.AssertReleased();
            d.AssertReleased();
            e.AssertReleased();

            Assert.Equal(5, list.WeakCount);
            Assert.Same(null, list.GetWeakReference(0).GetTarget());
            Assert.Same(b.GetReference(), list.GetWeakReference(1).GetTarget());
            Assert.Same(null, list.GetWeakReference(2).GetTarget());
            Assert.Same(null, list.GetWeakReference(3).GetTarget());
            Assert.Same(null, list.GetWeakReference(4).GetTarget());

            var array = list.ToArray();

            Assert.Equal(1, array.Length);
            Assert.Same(b.GetReference(), array[0]);

            // list was compacted:
            Assert.Equal(1, list.WeakCount);
            Assert.Same(b.GetReference(), list.GetWeakReference(0).GetTarget());
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);

            GC.KeepAlive(b.GetReference());
        }
        /// <summary>
        /// Creates a new instance of <see cref="PageReplacementAlgorithm"/>.
        /// </summary>
        /// <param name="pool">The memory pool that blocks will be allocated from.</param>
        public PageReplacementAlgorithm(MemoryPool pool)
        {
            if (pool.PageSize < 4096)
                throw new ArgumentOutOfRangeException("PageSize Must be greater than 4096", "pool");
            if (!BitMath.IsPowerOfTwo(pool.PageSize))
                throw new ArgumentException("PageSize Must be a power of 2", "pool");

            m_maxValidPosition = (int.MaxValue - 1) * (long)pool.PageSize; //Max position 

            m_syncRoot = new object();
            m_memoryPageSizeMask = pool.PageSize - 1;
            m_memoryPageSizeShiftBits = BitMath.CountBitsSet((uint)m_memoryPageSizeMask);
            m_pageList = new PageList(pool);
            m_arrayIndexLocks = new WeakList<PageLock>();
        }
        public void Test()
        {
            var rand = new Random(3);

            List<string> list1 = new List<string>();
            WeakList<string> list2 = new WeakList<string>();

            for (int x = 0; x < 1000; x++)
            {
                var str = x.ToString();
                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }

            for (int x = 1000; x < 2000; x++)
            {
                var str = x.ToString();
                var removeItem = list1[rand.Next(list1.Count)];
                list1.Remove(removeItem);
                list2.Remove(removeItem);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");

                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }

            for (int x = 0; x < 100; x++)
            {
                list1.RemoveAt(rand.Next(list1.Count));
                GC.Collect();

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }


            list2.Clear();
            foreach (var data in list2)
                throw new Exception();
        }
		public void TestWeakList()
		{
			var wl = new WeakList<object>();

			AddObject(wl);
			Assert.AreEqual(1, wl.Count);

			GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
			AddObject(wl);
			Assert.AreEqual(1, wl.Count);

			var o = new object();
			wl.Add(o);
			GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
			Assert.AreEqual(2, wl.Count);

			int iC = 0;
			foreach (var item in wl) iC++;
			Assert.AreEqual(1, iC);
			Assert.AreEqual(2, wl.Count);

			GC.KeepAlive(o);
		}
示例#39
0
        public void ResizeCompactsAllDead()
        {
            var a = Create("A");

            var list = new WeakList<object>();
            for (int i = 0; i < 9; i++)
            {
                Add(list, a);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.Strong = null;
            while (a.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            Add(list, a); // shrinks, #alive < length/4
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(1, list.WeakCount);

            list.ToArray(); // shrinks, #alive == 0
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(0, list.WeakCount);
        }
            /// <summary>
            /// Add a list of RecieverMessageEntity 
            /// int a tag (id the tag do not exist , create it)
            /// </summary>
            /// <param name="tag">tag</param>
            /// <param name="Agente">agents list</param>
            public void AddgrouptagRecieveEntity(string tag, WeakList<IRecieveMessageEntity> Agente)
            {
                System.Diagnostics.Debug.Assert(Agente != null);
                System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(tag));

                foreach (IRecieveMessageEntity item in Agente)
                {
                    if (!IdEntity.ContainsKey(item.GetId()))
                    {
                        throw new Exception("Add the following instace to the mapper before adding to a group: " + item.ToString() );
                    }
                }

                if (tagRecieveEntity.ContainsKey(tag))
                {
                    foreach (IRecieveMessageEntity item in Agente)
                    {
                        tagRecieveEntity[tag].Add(item);
                    }
                }
                else
                {
                    tagRecieveEntity.Add(tag, Agente);
                }

                foreach (IRecieveMessageEntity var in Agente)
                {
                    if(recieveEntityTag.ContainsKey(var.GetId()))
                    {
                        recieveEntityTag[var.GetId()].Add(tag);   
                   }
                    else
                   {
                       recieveEntityTag[var.GetId()] = new List<string>();
                       recieveEntityTag[var.GetId()].Add(tag);
                   }
                }               
         
            }
示例#41
0
 private void Add(WeakList<object> list, ObjectReference value)
 {
     list.Add(value.Strong);
 }
示例#42
0
 public void Errors()
 {
     var list = new WeakList<object>();
     Assert.Throws<ArgumentOutOfRangeException>(() => list.GetWeakReference(-1));
     Assert.Throws<ArgumentOutOfRangeException>(() => list.GetWeakReference(0));
 }
示例#43
0
        public void ResizeCompactsAllAlive()
        {
            var b = Create("B");

            var list = new WeakList<object>();
            for (int i = 0; i < 9; i++)
            {
                Add(list, b);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            Add(list, b); // expands #alive > 3/4 length
            Assert.Equal(9 * 2 + 1, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(10, list.WeakCount);

            for (int i = 0; i < list.TestOnly_UnderlyingArray.Length; i++)
            {
                if (i < 10)
                {
                    Assert.Same(b.Strong, list.TestOnly_UnderlyingArray[i].GetTarget());
                }
                else
                {
                    Assert.Null(list.TestOnly_UnderlyingArray[i]);
                }
            }

            GC.KeepAlive(b.Strong);
        }
            /// <summary>
            /// Add the IrecieveMessageEntity in a tag (create the tag if doesnt exists)
            /// </summary>
            /// <param name="tag"></param>
            /// <param name="Agente"></param>
            public void AddgrouptagRecieveEntity(string tag, IRecieveMessageEntity Agente)
            {
                System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(tag));
                System.Diagnostics.Debug.Assert(Agente != null);
                if (!IdEntity.ContainsKey(Agente.GetId()))
                {
                    throw new Exception("The following instance is not in the mapper, add it beforing calling this: " + Agente.ToString());
                }

                if (tagRecieveEntity.ContainsKey(tag))
                {
                    tagRecieveEntity[tag].Add(Agente);
                }
                else
                {
                    WeakList<IRecieveMessageEntity> l = new WeakList<IRecieveMessageEntity>();
                    l.Add(Agente);
                    tagRecieveEntity.Add(tag, l);
                }

                if (recieveEntityTag.ContainsKey(Agente.GetId()))
                {
                    recieveEntityTag[Agente.GetId()].Add(tag);
                }
                else
                {
                    recieveEntityTag[Agente.GetId()] = new List<string>();
                    recieveEntityTag[Agente.GetId()].Add(tag);
                }

            }
示例#45
0
            public CachedAssembly(AssemblyMetadata metadata)
            {
                Debug.Assert(metadata != null);

                // Save a reference to the cached symbols so that they don't get collected 
                // if the metadata object gets collected.
                this.CachedSymbols = metadata.CachedSymbols;

                this.Metadata = new WeakReference<AssemblyMetadata>(metadata);
            }
示例#46
0
        public void ResizeCompactsSecondFourth()
        {
            var a = Create("A");
            var b = Create("B");

            var list = new WeakList<object>();
            for (int i = 0; i < 6; i++)
            {
                Add(list, a);
            }

            for (int i = 0; i < 3; i++)
            {
                Add(list, b);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.AssertReleased();

            Add(list, b); // just compacts, length/4 < #alive < 3/4 length
            Assert.Equal(9, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(4, list.WeakCount);

            for (int i = 0; i < list.TestOnly_UnderlyingArray.Length; i++)
            {
                if (i < 4)
                {
                    Assert.Same(b.GetReference(), list.TestOnly_UnderlyingArray[i].GetTarget());
                }
                else
                {
                    Assert.Null(list.TestOnly_UnderlyingArray[i]);
                }
            }

            GC.KeepAlive(b);
        }
示例#47
0
        public void ResizeCompactsLastFourth()
        {
            var a = Create("A");
            var b = Create("B");

            var list = new WeakList<object>();
            for (int i = 0; i < 2; i++)
            {
                Add(list, a);
            }

            for (int i = 0; i < 7; i++)
            {
                Add(list, b);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.AssertReleased();

            Add(list, b); // expands #alive > 3/4 length
            Assert.Equal(9 * 2 + 1, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(8, list.WeakCount);

            for (int i = 0; i < list.TestOnly_UnderlyingArray.Length; i++)
            {
                if (i < 8)
                {
                    Assert.Same(b.GetReference(), list.TestOnly_UnderlyingArray[i].GetTarget());
                }
                else
                {
                    Assert.Null(list.TestOnly_UnderlyingArray[i]);
                }
            }

            GC.KeepAlive(b);
        }
示例#48
0
        public void ResizeCompactsAllDead()
        {
            var a = Create("A");

            var list = new WeakList<object>();
            for (int i = 0; i < 9; i++)
            {
                Add(list, a);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.AssertReleased();

            var b = Create("B");

            Add(list, b); // shrinks, #alive < length/4
            Assert.Equal(4, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(1, list.WeakCount);

            b.AssertReleased();

            list.ToArray(); // shrinks, #alive == 0
            Assert.Equal(0, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(0, list.WeakCount);
        }
示例#49
0
 private AssemblyMetadata(AssemblyMetadata metadata)
 {
     this.Assembly = metadata.Assembly;
     this.CachedSymbols = metadata.CachedSymbols;
     this.Modules = metadata.Modules.SelectAsArray(module => module.Copy());
 }
示例#50
0
        public void ResizeCompactsLastFourth()
        {
            var a = Create("A");
            var b = Create("B");

            var list = new WeakList<object>();
            for (int i = 0; i < 2; i++)
            {
                Add(list, a);
            }

            for (int i = 0; i < 7; i++)
            {
                Add(list, b);
            }

            Assert.Equal(list.WeakCount, list.TestOnly_UnderlyingArray.Length); // full

            a.Strong = null;
            while (a.Weak.IsAlive)
            {
                GC.Collect(2, GCCollectionMode.Forced);
            }

            Add(list, b); // expands #alive > 3/4 length
            Assert.Equal(9 * 2 + 1, list.TestOnly_UnderlyingArray.Length);
            Assert.Equal(8, list.WeakCount);

            for (int i = 0; i < list.TestOnly_UnderlyingArray.Length; i++)
            {
                if (i < 8)
                {
                    Assert.Same(b.Strong, list.TestOnly_UnderlyingArray[i].GetTarget());
                }
                else
                {
                    Assert.Null(list.TestOnly_UnderlyingArray[i]);
                }
            }

            GC.KeepAlive(b.Strong);
        }
		void AddObject(WeakList<object> wl) { wl.Add(new Object()); }
示例#52
0
 private void Add(WeakList<object> list, ObjectReference<C> value)
 {
     value.UseReference(r => list.Add(r));
 }