Ejemplo n.º 1
0
        //
        // TODO Jun: Optimize the reallocation routines
        //      1. Copying the temps can be optimized.
        //      2. Explore using List for the HeapStack stack. In this case we take advantage of .Net List arrays
        //
        private void ReAllocate(int size)
        {
            int newSize = INITIAL_SIZE;

            if (size > INITIAL_SIZE)
            {
                // Determine the next allocation size
                newSize = (int)(allocated * REALLOC_FACTOR) + allocated;

                // If the requested index is greater than the computed next allocation size,
                // then the requested index is the next allocation size
                newSize = (size >= newSize) ? size : newSize;
            }

            // Copy current contents into a temp array
            StackValue[] tempstack = new StackValue[allocated];
            values.CopyTo(tempstack, 0);

            // Reallocate the array and copy the temp contents to it
            values = new StackValue[newSize];
            tempstack.CopyTo(values, 0);

            for (int i = allocated; i < newSize; ++i)
            {
                values[i] = StackValue.Null;
            }

            // We should move StackValue list to heap. That is, heap
            // manages StackValues instead of HeapElement itself.
            heap.ReportAllocation(size - allocated);

            allocated = newSize;
            Validity.Assert(size <= allocated);
        }