VirtualBox

Changeset 45520 in vbox for trunk/include/VBox


Ignore:
Timestamp:
Apr 12, 2013 2:22:41 PM (12 years ago)
Author:
vboxsync
Message:

iprt/cpp/list.h,iprt/cpp/mtlist.h: Added assertions for index out of range conditions and tried to force those cases to have predictable non-destructive behavior. (Not possible when indexing an empty list, unfortunately.) Fixed RTMemRealloc bugs. Added missing read locking. Also changed the testcase to not wait forever in the multithreaded test2() function.

Location:
trunk/include/VBox/com
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/list.h

    r37861 r45520  
    55
    66/*
    7  * Copyright (C) 2011 Oracle Corporation
     7 * Copyright (C) 2011-2013 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3030#include <VBox/com/ptr.h>
    3131#include <VBox/com/string.h>
     32#include <VBox/com/array.h>
    3233#include <iprt/cpp/list.h>
     34
    3335
    3436/**
     
    5557     * @throws  std::bad_alloc
    5658     */
    57     RTCList(size_t cCapacity = BASE::DefaultCapacity)
    58      : BASE(cCapacity) {}
     59    RTCList(size_t cCapacity = BASE::kDefaultCapacity)
     60        : BASE(cCapacity) {}
    5961
    6062    /* Define our own new and delete. */
     
    8587     * @throws  std::bad_alloc
    8688     */
    87     RTCList(size_t cCapacity = BASE::DefaultCapacity)
    88      : BASE(cCapacity) {}
     89    RTCList(size_t cCapacity = BASE::kDefaultCapacity)
     90        : BASE(cCapacity) {}
    8991
    9092    /* Define our own new and delete. */
     
    98100 */
    99101template <>
    100 class RTCList<Utf8Str>: public RTCListBase<Utf8Str, Utf8Str*, false>
     102class RTCList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str*, false>
    101103{
    102104    /* Traits */
    103     typedef Utf8Str                   T;
     105    typedef com::Utf8Str              T;
    104106    typedef T                        *ITYPE;
    105107    static const bool                 MT = false;
     
    115117     * @throws  std::bad_alloc
    116118     */
    117     RTCList(size_t cCapacity = BASE::DefaultCapacity)
    118      : BASE(cCapacity) {}
     119    RTCList(size_t cCapacity = BASE::kDefaultCapacity)
     120        : BASE(cCapacity) {}
    119121
    120122    /**
     
    131133    {
    132134        com::SafeArray<IN_BSTR> sfaOther(ComSafeArrayInArg(other));
    133         realloc(sfaOther.size());
    134         m_cSize = sfaOther.size();
    135         for (size_t i = 0; i < m_cSize; ++i)
     135        size_t const cElementsOther = sfaOther.size();
     136        resizeArray(cElementsOther);
     137        m_cElements = cElementsOther;
     138        for (size_t i = 0; i < cElementsOther; ++i)
    136139            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(sfaOther[i]));
    137140    }
     
    148151     */
    149152    RTCList(const com::SafeArray<IN_BSTR> &other)
    150      : BASE(other.size())
    151     {
    152         for (size_t i = 0; i < m_cSize; ++i)
     153        : BASE(other.size())
     154    {
     155        for (size_t i = 0; i < m_cElements; ++i)
    153156            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
    154157    }
     
    165168    {
    166169        m_guard.enterWrite();
     170
    167171        /* Values cleanup */
    168         RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
     172        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
     173
    169174        /* Copy */
    170         if (other.size() != m_cCapacity)
    171             realloc_no_elements_clean(other.size());
    172         m_cSize = other.size();
    173         for (size_t i = 0; i < other.size(); ++i)
     175        size_t cElementsOther = other.size();
     176        if (cElementsOther != m_cCapacity)
     177            resizeArrayNoErase(cElementsOther);
     178        m_cElements = cElementsOther;
     179        for (size_t i = 0; i < cElementsOther; ++i)
    174180            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
     181
    175182        m_guard.leaveWrite();
    176 
    177183        return *this;
    178184    }
  • trunk/include/VBox/com/mtlist.h

    r37861 r45520  
    55
    66/*
    7  * Copyright (C) 2011 Oracle Corporation
     7 * Copyright (C) 2011-2013 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3030#include <VBox/com/ptr.h>
    3131#include <VBox/com/string.h>
     32#include <VBox/com/array.h>
    3233#include <iprt/cpp/mtlist.h>
    3334
     
    5556     * @throws  std::bad_alloc
    5657     */
    57     RTCList(size_t cCapacity = BASE::DefaultCapacity)
    58      : BASE(cCapacity) {}
     58    RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
     59        : BASE(cCapacity) {}
    5960
    6061    /* Define our own new and delete. */
     
    8586     * @throws  std::bad_alloc
    8687     */
    87     RTCList(size_t cCapacity = BASE::DefaultCapacity)
     88    RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
    8889     : BASE(cCapacity) {}
    8990
     
    9899 */
    99100template <>
    100 class RTCMTList<Utf8Str>: public RTCListBase<Utf8Str, Utf8Str*, true>
     101class RTCMTList<com::Utf8Str>: public RTCListBase<com::Utf8Str, com::Utf8Str *, true>
    101102{
    102103    /* Traits */
    103     typedef Utf8Str                   T;
     104    typedef com::Utf8Str              T;
    104105    typedef T                        *ITYPE;
    105106    static const bool                 MT = true;
     
    115116     * @throws  std::bad_alloc
    116117     */
    117     RTCMTList(size_t cCapacity = BASE::DefaultCapacity)
    118      : BASE(cCapacity) {}
     118    RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
     119        : BASE(cCapacity) {}
    119120
    120121    /**
     
    131132    {
    132133        com::SafeArray<IN_BSTR> sfaOther(ComSafeArrayInArg(other));
    133         realloc(sfaOther.size());
    134         m_cSize = sfaOther.size();
    135         for (size_t i = 0; i < m_cSize; ++i)
     134        size_t const cElementsOther = sfaOther.size();
     135        resizeArray(cElementsOther);
     136        m_cElements = cElementsOther;
     137        for (size_t i = 0; i < cElementsOther; ++i)
    136138            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(sfaOther[i]));
    137139    }
     
    150152      : BASE(other.size())
    151153    {
    152         for (size_t i = 0; i < m_cSize; ++i)
     154        for (size_t i = 0; i < m_cElements; ++i)
    153155            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
    154156    }
     
    166168        m_guard.enterWrite();
    167169         /* Values cleanup */
    168         RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
     170        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
    169171        /* Copy */
    170172        if (other.size() != m_cCapacity)
    171             realloc_no_elements_clean(other.size());
    172         m_cSize = other.size();
     173            resizeArrayNoErase(other.size());
     174        m_cElements = other.size();
    173175        for (size_t i = 0; i < other.size(); ++i)
    174176            RTCListHelper<T, ITYPE>::set(m_pArray, i, T(other[i]));
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