src/corelib/tools/qvarlengtharray.h
changeset 29 b72c6db6890b
parent 19 fcece45ef507
child 30 5dc02b23752f
equal deleted inserted replaced
25:e24348a560a6 29:b72c6db6890b
   126 
   126 
   127 private:
   127 private:
   128     friend class QPodList<T, Prealloc>;
   128     friend class QPodList<T, Prealloc>;
   129     void realloc(int size, int alloc);
   129     void realloc(int size, int alloc);
   130 
   130 
   131     int a;
   131     int a;      // capacity
   132     int s;
   132     int s;      // size
   133     T *ptr;
   133     T *ptr;     // data
   134     union {
   134     union {
   135         // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
   135         // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
   136         char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
   136         char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
   137         qint64 q_for_alignment_1;
   137         qint64 q_for_alignment_1;
   138         double q_for_alignment_2;
   138         double q_for_alignment_2;
   191 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
   191 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
   192 {
   192 {
   193     Q_ASSERT(aalloc >= asize);
   193     Q_ASSERT(aalloc >= asize);
   194     T *oldPtr = ptr;
   194     T *oldPtr = ptr;
   195     int osize = s;
   195     int osize = s;
   196     // s = asize;
   196 
   197 
   197     const int copySize = qMin(asize, osize);
   198     if (aalloc != a) {
   198     if (aalloc != a) {
   199         ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
   199         ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
   200         Q_CHECK_PTR(ptr);
   200         Q_CHECK_PTR(ptr);
   201         if (ptr) {
   201         if (ptr) {
   202             s = 0;
   202             s = 0;
   203             a = aalloc;
   203             a = aalloc;
   204 
   204 
   205             if (QTypeInfo<T>::isStatic) {
   205             if (QTypeInfo<T>::isStatic) {
   206                 QT_TRY {
   206                 QT_TRY {
   207                     // copy all the old elements
   207                     // copy all the old elements
   208                     const int copySize = qMin(asize, osize);
       
   209                     while (s < copySize) {
   208                     while (s < copySize) {
   210                         new (ptr+s) T(*(oldPtr+s));
   209                         new (ptr+s) T(*(oldPtr+s));
   211                         (oldPtr+s)->~T();
   210                         (oldPtr+s)->~T();
   212                         s++;
   211                         s++;
   213                     }
   212                     }
   219                     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   218                     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   220                         qFree(oldPtr);
   219                         qFree(oldPtr);
   221                     QT_RETHROW;
   220                     QT_RETHROW;
   222                 }
   221                 }
   223             } else {
   222             } else {
   224                 qMemCopy(ptr, oldPtr, qMin(asize, osize) * sizeof(T));
   223                 qMemCopy(ptr, oldPtr, copySize * sizeof(T));
   225             }
   224             }
   226         } else {
   225         } else {
   227             ptr = oldPtr;
   226             ptr = oldPtr;
   228             return;
   227             return;
   229         }
   228         }
   230     }
   229     }
   231 
   230     s = copySize;
   232     if (QTypeInfo<T>::isComplex) {
   231 
       
   232     if (QTypeInfo<T>::isComplex) {
       
   233         // destroy remaining old objects
   233         while (osize > asize)
   234         while (osize > asize)
   234             (oldPtr+(--osize))->~T();
   235             (oldPtr+(--osize))->~T();
   235         if (!QTypeInfo<T>::isStatic)
       
   236             s = osize;
       
   237     }
   236     }
   238 
   237 
   239     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   238     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   240         qFree(oldPtr);
   239         qFree(oldPtr);
   241 
   240