Example #1
0
        private void Initialize(int length)
        {
            if (!customLength)
            {
                Type genericType   = typeof(T);
                T    defaultOfType = default(T);

                try
                {
                    IMemSizable reference = defaultOfType as IMemSizable;
                    if (reference != null)
                    {
                        _sizeOfReference = reference.Size;
                    }
                    else if (genericType.IsValueType)
                    {
                        _sizeOfReference = System.Runtime.InteropServices.Marshal.SizeOf(defaultOfType);
                    }
                    else
                    {
                        _sizeOfReference = IntPtr.Size;
                    }
                }
                catch
                {
                    _sizeOfReference = SizeOfType(genericType);
                }
                _lengthThreshold = (81920 / _sizeOfReference);
            }

            _length = length;
            int superLength = (length / _lengthThreshold) + 1;

            //:
            //I still believe we need this exception, if we need to keep the main referencial array in SOH.
            //An array declared with greater supersize than length threshold will be declared in LOH.
            //The exception should be removed if we don't care that the main referencial array is being taken to LOH.
            //Otherwise it should be caught by user, who will then declare a new clustered array structure.

            //if (superLength < 0 || superLength > _lengthThreshold)
            //    throw new ArgumentOutOfRangeException(length.ToString("length"));

            //Your call.
            //Update: Let it grow, care later.

            _chunks = new T[superLength][];
            for (int i = 0; i < superLength; i++)
            {
                _chunks[i] = new T[length < _lengthThreshold ? length : _lengthThreshold];
                length    -= _lengthThreshold;
            }
        }
Example #2
0
        private void Initialize(int length)
        {
            if (!customLength)
            {
                Type genericType   = typeof(T);
                T    defaultOfType = default(T);
                try
                {
                    IMemSizable reference = defaultOfType as IMemSizable;

                    if (reference != null)
                    {
                        _sizeOfReference = reference.Size;
                    }
                    else if (genericType.IsValueType)
                    {
                        _sizeOfReference = System.Runtime.InteropServices.Marshal.SizeOf(defaultOfType);
                    }
                    else
                    {
                        _sizeOfReference = IntPtr.Size;
                    }
                }
                catch
                {
                    _sizeOfReference = SizeOfType(genericType);
                }

                _lengthThreshold = (81920 / _sizeOfReference);
                _length          = length;
                int superLength = (length / _lengthThreshold) + 1;

                _chunks = new T[superLength][];
                for (int i = 0; i < superLength; i++)
                {
                    _chunks[i] = new T[length < _lengthThreshold ? length : _lengthThreshold];
                    length    -= _lengthThreshold;
                }
            }
        }