示例#1
0
        internal static object Allocate(Type typeToAllocate)
        {
            MyGenericObjectPool objectPool      = null;
            CleanerDelegate     cleanerDelegate = null;
            MyTuple <MyGenericObjectPool, CleanerDelegate> poolDelegatePair;

            if (m_poolsByType.TryGetValue(typeToAllocate, out poolDelegatePair))
            {
                objectPool      = poolDelegatePair.Item1;
                cleanerDelegate = poolDelegatePair.Item2;
            }
            else
            {
                Debug.Fail("No type registered for " + typeToAllocate.ToString());
                return(null);
            }

            object allocatedObject = null;

            if (objectPool.AllocateOrCreate(out allocatedObject))
            {
                cleanerDelegate(allocatedObject);
            }

            return(allocatedObject);
        }
示例#2
0
        private static void RegisterPoolsFromAssembly(Assembly assembly)
        {
#if XB1 // XB1_ALLINONEASSEMBLY
            System.Diagnostics.Debug.Assert(m_registered == false);
            if (m_registered == true)
            {
                return;
            }
            m_registered = true;
            foreach (var type in MyAssembly.GetTypes())
#else // !XB1
            foreach (var type in assembly.GetTypes())
#endif // !XB1
            {
                var customAttributes = type.GetCustomAttributes(typeof(PooledObjectAttribute), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    Debug.Assert(customAttributes.Length == 1);
                    PooledObjectAttribute attribute = (PooledObjectAttribute)customAttributes[0];
                    var  methods       = type.GetMethods();
                    bool delegateFound = false;
                    foreach (var method in methods)
                    {
                        var methodAttributes = method.GetCustomAttributes(typeof(PooledObjectCleanerAttribute), false);
                        if (methodAttributes != null && methodAttributes.Length > 0)
                        {
                            Debug.Assert(methodAttributes.Length == 1);
                            MyGenericObjectPool objectPool      = new MyGenericObjectPool(attribute.PoolPreallocationSize, type);
                            CleanerDelegate     cleanerDelegate = method.CreateDelegate <CleanerDelegate>();

                            // Make sure everything in the pool is always clean
                            foreach (var objectInPool in objectPool.Unused)
                            {
                                cleanerDelegate(objectInPool);
                            }

                            m_poolsByType.Add(type, MyTuple.Create(objectPool, cleanerDelegate));
                            delegateFound = true;
                            break;
                        }
                    }

                    if (!delegateFound)
                    {
                        Debug.Fail("Pooled type does not have a cleaner method.");
                    }
                }
            }
        }
示例#3
0
        internal static void Deallocate <T>(T objectToDeallocate) where T : class
        {
            MyGenericObjectPool objectPool      = null;
            CleanerDelegate     cleanerDelegate = null;
            MyTuple <MyGenericObjectPool, CleanerDelegate> poolDelegatePair;

            if (m_poolsByType.TryGetValue(objectToDeallocate.GetType(), out poolDelegatePair))
            {
                objectPool      = poolDelegatePair.Item1;
                cleanerDelegate = poolDelegatePair.Item2;
            }
            else
            {
                Debug.Fail("No type registered for " + objectToDeallocate.GetType().ToString());
                return;
            }

            cleanerDelegate(objectToDeallocate);

            objectPool.Deallocate(objectToDeallocate);
        }