Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/corelib/tools/qvarlengtharray.h

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information ([email protected])
     4** All rights reserved.
     5** Contact: Nokia Corporation ([email protected])
    56**
    67** This file is part of the QtCore module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you
     37** @nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    5252
    5353QT_MODULE(Core)
     54
     55
     56
    5457
    5558// Prealloc = 256 by default, specified in qcontainerfwd.h
     
    123126
    124127private:
     128
    125129    void realloc(int size, int alloc);
    126130
     
    141145    if (s > Prealloc) {
    142146        ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
     147
    143148        a = s;
    144149    } else {
     
    162167
    163168template <class T, int Prealloc>
    164 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int asize)
     169Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int )
    165170{
    166171    Q_ASSERT(abuf);
    167     if (asize <= 0)
     172    if ( <= 0)
    168173        return;
    169174
    170     const int idx = s;
    171     const int news = s + asize;
    172     if (news >= a)
    173         realloc(s, qMax(s<<1, news));
    174     s = news;
    175 
    176     if (QTypeInfo<T>::isComplex) {
    177         T *i = ptr + idx;
    178         T *j = i + asize;
    179         while (i < j)
    180             new (i++) T(*abuf++);
     175    const int asize = s + increment;
     176
     177    if (asize >= a)
     178        realloc(s, qMax(s*2, asize));
     179
     180    if (QTypeInfo<T>::isComplex) {
     181        // call constructor for new objects (which can throw)
     182        while (s < asize)
     183            new (ptr+(s++)) T(*abuf++);
    181184    } else {
    182         qMemCopy(&ptr[idx], abuf, asize * sizeof(T));
     185        qMemCopy(&ptr[s], abuf, increment * sizeof(T));
     186        s = asize;
    183187    }
    184188}
     
    190194    T *oldPtr = ptr;
    191195    int osize = s;
    192     s = asize;
     196    s = asize;
    193197
    194198    if (aalloc != a) {
    195199        ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
     200
    196201        if (ptr) {
     202
    197203            a = aalloc;
    198204
    199205            if (QTypeInfo<T>::isStatic) {
    200                 T *i = ptr + osize;
    201                 T *j = oldPtr + osize;
    202                 while (i != ptr) {
    203                     new (--i) T(*--j);
    204                     j->~T();
     206                QT_TRY {
     207                    // copy all the old elements
     208                    const int copySize = qMin(asize, osize);
     209                    while (s < copySize) {
     210                        new (ptr+s) T(*(oldPtr+s));
     211                        (oldPtr+s)->~T();
     212                        s++;
     213                    }
     214                } QT_CATCH(...) {
     215                    // clean up all the old objects and then free the old ptr
     216                    int sClean = s;
     217                    while (sClean < osize)
     218                        (oldPtr+(sClean++))->~T();
     219                    if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
     220                        qFree(oldPtr);
     221                    QT_RETHROW;
    205222                }
    206223            } else {
    207                 qMemCopy(ptr, oldPtr, osize * sizeof(T));
     224                qMemCopy(ptr, oldPtr, qMin(asize, osize) * sizeof(T));
     225                s = asize;
    208226            }
    209227        } else {
    210228            ptr = oldPtr;
    211             s = 0;
    212             asize = 0;
    213         }
    214     }
    215 
    216     if (QTypeInfo<T>::isComplex) {
    217         if (asize < osize) {
    218             T *i = oldPtr + osize;
    219             T *j = oldPtr + asize;
    220             while (i-- != j)
    221                 i->~T();
    222         } else {
    223             T *i = ptr + asize;
    224             T *j = ptr + osize;
    225             while (i != j)
    226                 new (--i) T;
    227         }
     229            return;
     230        }
     231    }
     232
     233    if (QTypeInfo<T>::isComplex) {
     234        while (osize > asize)
     235            (oldPtr+(--osize))->~T();
     236        if( oldPtr == ptr )
     237            s = osize;
    228238    }
    229239
    230240    if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
    231241        qFree(oldPtr);
     242
     243
     244
     245
     246
     247
     248
     249
    232250}
    233251
Note: See TracChangeset for help on using the changeset viewer.