Changeset 31061 in webkit for trunk/JavaScriptCore/wtf/Vector.h


Ignore:
Timestamp:
Mar 14, 2008, 12:05:28 PM (17 years ago)
Author:
[email protected]
Message:

Reviewed by Beth Dakin.


Fixed a few problems with Vector::shrinkCapacity that I noticed in testing.

  • wtf/Vector.h: (WTF::VectorBufferBase::deallocateBuffer): Clear our m_buffer pointer when we deallocate m_buffer, in case we're not asked to reallocate a new buffer. (Otherwise, we would use a stale m_buffer if we were asked to perform any operations after shrinkCapacity was called.)


(WTF::VectorBuffer::allocateBuffer): Made VectorBuffer with inline
capacity aware that calls to allocateBuffer might be shrinks, rather
than grows, so we shouldn't allocate a new buffer on the heap unless
our inline buffer is too small.


(WTF::::shrinkCapacity): Call resize() instead of just setting m_size,
so destructors run. Call resize before reallocating the buffer to make
sure that we still have access to the objects we need to destroy. Call
moveOverlapping instead of move, since a call to allocateBuffer on an
inline buffer may produce identical storage.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/Vector.h

    r31032 r31061  
    254254        void deallocateBuffer(T* bufferToDeallocate)
    255255        {
     256            if (m_buffer == bufferToDeallocate)
     257                m_buffer = 0;
    256258            fastFree(bufferToDeallocate);
    257259        }
     
    344346            : Base(inlineBuffer(), inlineCapacity)
    345347        {
    346             if (capacity > inlineCapacity)
    347                 allocateBuffer(capacity);
     348            allocateBuffer(capacity);
    348349        }
    349350
     
    353354        }
    354355
    355         using Base::allocateBuffer;
     356        void allocateBuffer(size_t newCapacity)
     357        {
     358            if (newCapacity > inlineCapacity)
     359                Base::allocateBuffer(newCapacity);
     360        }
    356361
    357362        void deallocateBuffer(T* bufferToDeallocate)
     
    666671            return;
    667672
     673        resize(min(m_size, newCapacity));
     674
    668675        T* oldBuffer = begin();
    669676        if (newCapacity > 0) {
    670677            T* oldEnd = end();
    671678            m_buffer.allocateBuffer(newCapacity);
    672             TypeOperations::move(oldBuffer, oldEnd, begin());
    673         }
    674         m_size = min(m_size, newCapacity);
     679            TypeOperations::moveOverlapping(oldBuffer, oldEnd, begin());
     680        }
     681
    675682        m_buffer.deallocateBuffer(oldBuffer);
    676683    }
Note: See TracChangeset for help on using the changeset viewer.