VirtualBox

Changeset 37861 in vbox for trunk/include/iprt/cpp


Ignore:
Timestamp:
Jul 11, 2011 10:03:22 AM (14 years ago)
Author:
vboxsync
Message:

IPRT: fix assigning of empty lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/list.h

    r36654 r37861  
    219219      , m_cCapacity(0)
    220220    {
    221         realloc_grow(cCapacity);
     221        if (cCapacity > 0)
     222            realloc_grow(cCapacity);
    222223    }
    223224
     
    236237      , m_cCapacity(0)
    237238    {
    238         realloc_grow(other.m_cSize);
     239        realloc_no_elements_clean(other.m_cSize);
    239240        RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);
    240241        m_cSize = other.m_cSize;
     
    372373    {
    373374        m_guard.enterWrite();
    374         if (m_cCapacity - m_cSize < other.m_cSize)
    375             realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize)));
    376         RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize);
    377         m_cSize += other.m_cSize;
     375        if (RT_LIKELY(other.m_cSize > 0))
     376        {
     377            if (m_cCapacity - m_cSize < other.m_cSize)
     378                realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize)));
     379            RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize);
     380            m_cSize += other.m_cSize;
     381        }
    378382        m_guard.leaveWrite();
    379383
     
    391395    {
    392396        /* Prevent self assignment */
    393         if (this == &other)
     397        if (RT_UNLIKELY(this == &other))
    394398            return *this;
    395399
    396400        m_guard.enterWrite();
    397         /* Values cleanup */
     401        /* Delete all items. */
    398402        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
    399 
    400         /* Copy */
     403        /* Need we to realloc memory. */
    401404        if (other.m_cSize != m_cCapacity)
    402             realloc_grow(other.m_cSize);
     405            realloc_no_elements_clean(other.m_cSize);
    403406        m_cSize = other.m_cSize;
     407        /* Copy new items. */
    404408        RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);
    405409        m_guard.leaveWrite();
     
    431435     * Return the first item as constant object.
    432436     *
     437     * @note No boundary checks are done. Make sure there is at least one
     438     * element.
     439     *
     440     * @return   The first item.
     441     */
     442    GET_CRTYPE first() const
     443    {
     444        m_guard.enterRead();
     445        GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
     446        m_guard.leaveRead();
     447        return res;
     448    }
     449
     450    /**
     451     * Return the first item.
     452     *
     453     * @note No boundary checks are done. Make sure there is at least one
     454     * element.
     455     *
     456     * @return   The first item.
     457     */
     458    GET_RTYPE first()
     459    {
     460        m_guard.enterRead();
     461        GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
     462        m_guard.leaveRead();
     463        return res;
     464    }
     465
     466    /**
     467     * Return the last item as constant object.
     468     *
     469     * @note No boundary checks are done. Make sure there is at least one
     470     * element.
     471     *
     472     * @return   The last item.
     473     */
     474    GET_CRTYPE last() const
     475    {
     476        m_guard.enterRead();
     477        GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
     478        m_guard.leaveRead();
     479        return res;
     480    }
     481
     482    /**
     483     * Return the last item.
     484     *
     485     * @note No boundary checks are done. Make sure there is at least one
     486     * element.
     487     *
     488     * @return   The last item.
     489     */
     490    GET_RTYPE last()
     491    {
     492        m_guard.enterRead();
     493        GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
     494        m_guard.leaveRead();
     495        return res;
     496    }
     497
     498    /**
     499     * Return the item at position @a i as constant object.
     500     *
    433501     * @note No boundary checks are done. Make sure @a i is equal or greater zero
    434502     *       and smaller than RTCList::size.
    435503     *
    436      * @return   The first item.
    437      */
    438     GET_CRTYPE first() const
    439     {
    440         m_guard.enterRead();
    441         GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
    442         m_guard.leaveRead();
    443         return res;
    444     }
    445 
    446     /**
    447      * Return the first item.
     504     * @param   i     The position of the item to return.
     505     * @return  The item at position @a i.
     506     */
     507    GET_CRTYPE at(size_t i) const
     508    {
     509        m_guard.enterRead();
     510        GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
     511        m_guard.leaveRead();
     512        return res;
     513    }
     514
     515    /**
     516     * Return the item at position @a i.
    448517     *
    449518     * @note No boundary checks are done. Make sure @a i is equal or greater zero
    450519     *       and smaller than RTCList::size.
    451520     *
    452      * @return   The first item.
    453      */
    454     GET_RTYPE first()
    455     {
    456         m_guard.enterRead();
    457         GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
    458         m_guard.leaveRead();
    459         return res;
    460     }
    461 
    462     /**
    463      * Return the last item as constant object.
     521     * @param   i     The position of the item to return.
     522     * @return   The item at position @a i.
     523     */
     524    GET_RTYPE at(size_t i)
     525    {
     526        m_guard.enterRead();
     527        GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
     528        m_guard.leaveRead();
     529        return res;
     530    }
     531
     532    /**
     533     * Return the item at position @a i as mutable reference.
    464534     *
    465535     * @note No boundary checks are done. Make sure @a i is equal or greater zero
    466536     *       and smaller than RTCList::size.
    467537     *
    468      * @return   The last item.
    469      */
    470     GET_CRTYPE last() const
    471     {
    472         m_guard.enterRead();
    473         GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
    474         m_guard.leaveRead();
    475         return res;
    476     }
    477 
    478     /**
    479      * Return the last item.
    480      *
    481      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    482      *       and smaller than RTCList::size.
    483      *
    484      * @return   The last item.
    485      */
    486     GET_RTYPE last()
    487     {
    488         m_guard.enterRead();
    489         GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
    490         m_guard.leaveRead();
    491         return res;
    492     }
    493 
    494     /**
    495      * Return the item at position @a i as constant object.
    496      *
    497      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    498      *       and smaller than RTCList::size.
    499      *
    500      * @param   i     The position of the item to return.
    501      * @return  The item at position @a i.
    502      */
    503     GET_CRTYPE at(size_t i) const
    504     {
    505         m_guard.enterRead();
    506         GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
    507         m_guard.leaveRead();
    508         return res;
    509     }
    510 
    511     /**
    512      * Return the item at position @a i.
    513      *
    514      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    515      *       and smaller than RTCList::size.
    516      *
    517538     * @param   i     The position of the item to return.
    518539     * @return   The item at position @a i.
    519540     */
    520     GET_RTYPE at(size_t i)
    521     {
    522         m_guard.enterRead();
    523         GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
    524         m_guard.leaveRead();
    525         return res;
    526     }
    527 
    528     /**
    529      * Return the item at position @a i as mutable reference.
    530      *
    531      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    532      *       and smaller than RTCList::size.
    533      *
    534      * @param   i     The position of the item to return.
    535      * @return   The item at position @a i.
    536      */
    537541    T &operator[](size_t i)
    538542    {
     
    553557    {
    554558        m_guard.enterRead();
    555         if (i >= m_cSize)
     559        if (RT_UNLIKELY(i >= m_cSize))
    556560        {
    557561            m_guard.leaveRead();
     
    574578    {
    575579        m_guard.enterRead();
    576         if (i >= m_cSize)
     580        if (RT_UNLIKELY(i >= m_cSize))
    577581        {
    578582            m_guard.leaveRead();
     
    601605     * Remove the first item.
    602606     *
    603      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    604      *       and smaller than RTCList::size.
     607     * @note No boundary checks are done. Make sure there is at least one
     608     * element.
    605609     */
    606610    void removeFirst()
     
    612616     * Remove the last item.
    613617     *
    614      * @note No boundary checks are done. Make sure @a i is equal or greater zero
    615      *       and smaller than RTCList::size.
     618     * @note No boundary checks are done. Make sure there is at least one
     619     * element.
    616620     */
    617621    void removeLast()
     
    669673        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
    670674        if (m_cSize != DefaultCapacity)
    671             realloc_grow(DefaultCapacity);
     675            realloc_no_elements_clean(DefaultCapacity);
    672676        m_cSize = 0;
    673677        m_guard.leaveWrite();
     
    691695        m_guard.leaveRead();
    692696        return res;
     697    }
     698
     699    RTCListBase<T, ITYPE, MT> &operator<<(const T &val)
     700    {
     701        return append(val);
    693702    }
    694703
     
    716725        if (   cNewSize < m_cSize
    717726            && m_pArray)
    718         {
    719727            RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize);
     728        realloc_no_elements_clean(cNewSize);
     729    }
     730
     731    void realloc_no_elements_clean(size_t cNewSize)
     732    {
     733        /* Same size? */
     734        if (cNewSize == m_cCapacity)
     735            return;
     736
     737        /* If we get smaller we have to delete some of the objects at the end
     738           of the list. */
     739        if (   cNewSize < m_cSize
     740            && m_pArray)
    720741            m_cSize -= m_cSize - cNewSize;
    721         }
    722742
    723743        /* If we get zero we delete the array it self. */
     
    803823     : BASE(cCapacity) {}
    804824
     825    RTCList(const BASE &other)
     826     : BASE(other) {}
     827
    805828    /* Define our own new and delete. */
    806829    RTMEMEF_NEW_AND_DELETE_OPERATORS();
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette