Changeset 19365 in webkit for trunk/JavaScriptCore/wtf


Ignore:
Timestamp:
Feb 2, 2007, 9:57:28 AM (18 years ago)
Author:
darin
Message:

Reviewed by Anders.

  • fix copying and assigning a ListHashSet

No test because the code path with bugs I am fixing is not used yet.

  • wtf/ListHashSet.h: Tweaked ListHashSetNodeAllocator a little bit for clarity. Changed m_allocator to be an OwnPtr instead of doing an explicit delete. Fixed bug in copy constructor where we'd have an uninitialized m_allocator. Fixed bug in assignment operator where it would swap only the hash table, and not the head, tail, and allocator pointers.
File:
1 edited

Legend:

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

    r19354 r19365  
    2323#define WTF_ListHashSet_h
    2424
    25 #include "HashTable.h"
    2625#include "HashSet.h"
     26#include "OwnPtr.h"
    2727
    2828namespace WTF {
     
    7676        ~ListHashSet();
    7777
     78        void swap(ListHashSet&);
     79
    7880        int size() const;
    7981        int capacity() const;
     
    109111        Node* m_head;
    110112        Node* m_tail;
    111         NodeAllocator* m_allocator;
    112     };
    113 
     113        OwnPtr<NodeAllocator> m_allocator;
     114    };
    114115
    115116    template<typename ValueArg> struct ListHashSetNodeAllocator {
     
    120121            : m_freeList(pool())
    121122        {
    122             memset(m_pool.pool, 0, sizeof(m_pool)); 
    123         }
    124 
    125         Node* allocate() 
     123            memset(m_pool.pool, 0, sizeof(m_pool.pool));
     124        }
     125
     126        Node* allocate()
    126127        {
    127128            Node* result = m_freeList;
     
    148149            fastFree(node);
    149150        }
    150        
    151151
    152152    private:
    153153        Node* pool() { return reinterpret_cast<Node*>(m_pool.pool); }
    154         bool inPool(Node* node)
    155         {
    156             return reinterpret_cast<char*>(node) >= m_pool.pool && reinterpret_cast<char*>(node) < m_pool.pool + sizeof(m_pool.pool);
     154
     155        bool inPool(Node* node)
     156        {
     157            return node >= pool() && node < pool() + m_poolSize;
    157158        }
    158159
     
    186187    };
    187188
    188     template<typename ValueArg, typename HashArg> class ListHashSetIterator
    189     {
     189    template<typename ValueArg, typename HashArg> class ListHashSetIterator {
    190190    private:
    191191        typedef ListHashSet<ValueArg, HashArg> ListHashSetType;
     
    230230    };
    231231
    232     template<typename ValueArg, typename HashArg> class ListHashSetConstIterator
    233     {
     232    template<typename ValueArg, typename HashArg> class ListHashSetConstIterator {
    234233    private:
    235234        typedef ListHashSet<ValueArg, HashArg> ListHashSetType;
     
    255254        }
    256255
    257         // default copy, assignment and destructor are OK if CHECK_HASHTABLE_ITERATORS is 0
    258 
    259256        PointerType get() const
    260257        {
     
    326323        : m_head(0)
    327324        , m_tail(0)
     325        , m_allocator(new NodeAllocator)
    328326    {
    329327        const_iterator end = other.end();
     
    336334    {
    337335        ListHashSet tmp(other);
    338         m_impl.swap(tmp.m_impl);
     336        swap(tmp);
    339337        return *this;
    340338    }
    341339
    342340    template<typename T, typename U>
     341    inline void ListHashSet<T, U>::swap(ListHashSet& other)
     342    {
     343        m_impl.swap(other.m_impl);
     344        std::swap(m_head, other.m_head);
     345        std::swap(m_tail, other.m_tail);
     346        m_allocator.swap(other.m_allocator);
     347        return *this;
     348    }
     349
     350    template<typename T, typename U>
    343351    inline ListHashSet<T, U>::~ListHashSet()
    344352    {
    345353        deleteAllNodes();
    346         delete m_allocator;
    347354    }
    348355
     
    396403        if (it == m_impl.end())
    397404            return end();
    398 
    399405        return makeIterator(*it);
    400406    }
     
    407413        if (it == m_impl.end())
    408414            return end();
    409         return makeConstIterator(*(m_impl.template find<ValueType, Translator>(value)));
     415        return makeConstIterator(*it);
    410416    }
    411417
     
    421427    {
    422428        typedef ListHashSetTranslator<ValueType, HashFunctions> Translator;
    423         pair<typename ImplType::iterator, bool> result =  m_impl.template add<ValueType, NodeAllocator*, Translator>(value, m_allocator);
     429        pair<typename ImplType::iterator, bool> result = m_impl.template add<ValueType, NodeAllocator*, Translator>(value, m_allocator.get());
    424430        if (result.second)
    425             appendNode(*(result.first));
    426 
    427         return std::make_pair(makeIterator(*(result.first)), result.second);
     431            appendNode(*result.first);
     432        return std::make_pair(makeIterator(*result.first), result.second);
    428433    }
    429434
     
    433438        if (it == end())
    434439            return;
    435 
    436440        m_impl.remove(it.node());
    437441        unlinkAndDelete(it.node());
     
    472476        }
    473477
    474         node->destroy(m_allocator);
     478        node->destroy(m_allocator.get());
    475479    }
    476480
     
    499503
    500504        for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : 0)
    501             node->destroy(m_allocator);
     505            node->destroy(m_allocator.get());
    502506    }
    503507
Note: See TracChangeset for help on using the changeset viewer.