Exemplo n.º 1
0
        /// <summary>
        /// Ensures that there is sufficient memory allocated.
        /// </summary>
        /// <param name="capacity">
        /// The required capacity of the block, in bytes.
        /// </param>
        /// <exception cref="OutOfMemoryException">
        /// There is insufficient memory to satisfy the request.
        /// </exception>
        public void EnsureCapacity(int capacity)
        {
            int currentSize = MemoryBlock.IsInvalid ? 0 : MemoryBlock.Size;

            if (capacity > currentSize)
            {
                if (0 != currentSize)
                {
                    currentSize <<= 1;
                }
                if (capacity > currentSize)
                {
                    currentSize = capacity;
                }

                if (!MemoryBlock.IsInvalid)
                {
                    MemoryBlock.Dispose();
                }
                MemoryBlock = SafeHGlobalHandle.Allocate(currentSize);
            }
        }