Exemple #1
0
    public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free, int initialSize, int maxItems)
    {
        Assert.IsNotNull(allocate);
        allocateDelegate = allocate;
        freeDelegate     = free;
        this.free        = new Stack <T>(initialSize);
        this.maxItems    = maxItems;

        while (initialSize-- > 0)
        {
            this.free.Push(allocateDelegate());
        }
    }
Exemple #2
0
        private static object AllocateByInternal(Type type)
        {
            if (!allocateIsInitialized)
            {
                lock (typeof(TypeHelper))
                {
                    if (!allocateIsInitialized)
                    {
                        allocateIsInitialized = true;

                        var allocateMethod = typeof(RuntimeTypeHandle).GetMethod(AllocateMethodName, BindingFlags.NonPublic | BindingFlags.Static);
                        if (allocateMethod != null)
                        {
                            allocate = MethodHelper.CreateDelegate <AllocateDelegate>(allocateMethod, SignatureLevels.Cast);
                        }

                        var stubHelpersType = Type.GetType(StubHelpersTypeName);
                        if (stubHelpersType != null)
                        {
                            var allocateInternalMethod = stubHelpersType.GetMethod(AllocateInternalName, BindingFlags.Static | BindingFlags.NonPublic);
                            if (allocateInternalMethod != null)
                            {
                                allocateInternal = MethodHelper.CreateDelegate <AllocateInternalDelegate>(allocateInternalMethod, SignatureLevels.Cast);
                            }
                        }

                        var activationServicesType = Type.GetType(ActivationServicesTypeName);
                        if (activationServicesType != null)
                        {
                            var allocateUninitializedClassInstanceMethod = activationServicesType.GetMethod(AllocateUninitializedClassInstanceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                            if (allocateUninitializedClassInstanceMethod != null)
                            {
                                allocateUninitializedClassInstance = MethodHelper.CreateDelegate <AllocateUninitializedClassInstanceDelegate>(allocateUninitializedClassInstanceMethod, SignatureLevels.Cast);
                            }
                        }
                    }
                }
            }

            if (allocate != null)
            {
                try
                {
                    return(allocate(type));
                }
                catch (Exception)
                {
                }
            }

            if (allocateInternal != null)
            {
                try
                {
                    return(allocateInternal(type.TypeHandle.Value));
                }
                catch (Exception)
                {
                }
            }

            if (allocateUninitializedClassInstance != null)
            {
                try
                {
                    return(allocateUninitializedClassInstance(type));
                }
                catch (Exception)
                {
                }
            }

            return(null);
        }
Exemple #3
0
 public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free, int initialSize) : this(allocate, free, initialSize, 0)
 {
 }
Exemple #4
0
 public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free) : this(allocate, free, 0, 0)
 {
 }