summaryrefslogtreecommitdiffstats
path: root/Source/JavaScriptCore/runtime/WeakGCMap.h
blob: 7bf45030bde6733217788dfedd43bf6b42d775f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2009 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef WeakGCMap_h
#define WeakGCMap_h

#include "Heap.h"
#include <wtf/HashMap.h>

namespace JSC {

class JSCell;

// A HashMap whose get() function returns emptyValue() for cells awaiting destruction.
template<typename KeyType, typename MappedType>
class WeakGCMap {
    WTF_MAKE_FAST_ALLOCATED;
    /*
    Invariants:
        * A value enters the WeakGCMap marked. (Guaranteed by set().)
        * A value that becomes unmarked leaves the WeakGCMap before being recycled. (Guaranteed by the value's destructor removing it from the WeakGCMap.)
        * A value that becomes unmarked leaves the WeakGCMap before becoming marked again. (Guaranteed by all destructors running before the mark phase begins.)
        * During the mark phase, all values in the WeakGCMap are valid. (Guaranteed by all destructors running before the mark phase begins.)
    */

public:
    typedef typename HashMap<KeyType, DeprecatedPtr<MappedType> >::iterator iterator;
    typedef typename HashMap<KeyType, DeprecatedPtr<MappedType> >::const_iterator const_iterator;
    
    bool isEmpty() { return m_map.isEmpty(); }
    void clear() { m_map.clear(); }

    MappedType* get(const KeyType&) const;
    pair<iterator, bool> set(const KeyType&, MappedType*); 
    MappedType* take(const KeyType&);

    // These unchecked functions provide access to a value even if the value's
    // mark bit is not set. This is used, among other things, to retrieve values
    // during the GC mark phase, which begins by clearing all mark bits.
    
    size_t uncheckedSize() { return m_map.size(); }

    MappedType* uncheckedGet(const KeyType& key) const { return m_map.get(key).get(); }
    DeprecatedPtr<MappedType>* uncheckedGetSlot(const KeyType& key)
    {
        iterator iter = m_map.find(key);
        if (iter == m_map.end())
            return 0;
        return &iter->second;
    }
    bool uncheckedRemove(const KeyType&, MappedType*);

    iterator uncheckedBegin() { return m_map.begin(); }
    iterator uncheckedEnd() { return m_map.end(); }

    const_iterator uncheckedBegin() const { return m_map.begin(); }
    const_iterator uncheckedEnd() const { return m_map.end(); }

    bool isValid(iterator it) const { return Heap::isMarked(it->second.get()); }
    bool isValid(const_iterator it) const { return Heap::isMarked(it->second.get()); }

private:
    HashMap<KeyType, DeprecatedPtr<MappedType> > m_map;
};

template<typename KeyType, typename MappedType>
inline MappedType* WeakGCMap<KeyType, MappedType>::get(const KeyType& key) const
{
    MappedType* result = m_map.get(key).get();
    if (result == HashTraits<MappedType*>::emptyValue())
        return result;
    if (!Heap::isMarked(result))
        return HashTraits<MappedType*>::emptyValue();
    return result;
}

template<typename KeyType, typename MappedType>
MappedType* WeakGCMap<KeyType, MappedType>::take(const KeyType& key)
{
    MappedType* result = m_map.take(key).get();
    if (result == HashTraits<MappedType*>::emptyValue())
        return result;
    if (!Heap::isMarked(result))
        return HashTraits<MappedType*>::emptyValue();
    return result;
}

template<typename KeyType, typename MappedType>
pair<typename WeakGCMap<KeyType, MappedType>::iterator, bool> WeakGCMap<KeyType, MappedType>::set(const KeyType& key, MappedType* value)
{
    Heap::setMarked(value); // If value is newly allocated, it's not marked, so mark it now.
    pair<iterator, bool> result = m_map.add(key, value);
    if (!result.second) { // pre-existing entry
        result.second = !Heap::isMarked(result.first->second.get());
        result.first->second = value;
    }
    return result;
}

template<typename KeyType, typename MappedType>
bool WeakGCMap<KeyType, MappedType>::uncheckedRemove(const KeyType& key, MappedType* value)
{
    iterator it = m_map.find(key);
    if (it == m_map.end())
        return false;
    if (it->second.get() != value)
        return false;
    m_map.remove(it);
    return true;
}

} // namespace JSC

#endif // WeakGCMap_h