|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jshashutil_h |
|
8 #define jshashutil_h |
|
9 |
|
10 #include "jscntxt.h" |
|
11 |
|
12 namespace js { |
|
13 |
|
14 /* |
|
15 * Used to add entries to a js::HashMap or HashSet where the key depends on a GC |
|
16 * thing that may be moved by generational collection between the call to |
|
17 * lookupForAdd() and relookupOrAdd(). |
|
18 */ |
|
19 template <class T> |
|
20 struct DependentAddPtr |
|
21 { |
|
22 typedef typename T::AddPtr AddPtr; |
|
23 typedef typename T::Entry Entry; |
|
24 |
|
25 template <class Lookup> |
|
26 DependentAddPtr(const ExclusiveContext *cx, const T &table, const Lookup &lookup) |
|
27 : addPtr(table.lookupForAdd(lookup)) |
|
28 #ifdef JSGC_GENERATIONAL |
|
29 , originalGcNumber(cx->zone()->gcNumber()) |
|
30 #endif |
|
31 {} |
|
32 |
|
33 template <class KeyInput, class ValueInput> |
|
34 bool add(const ExclusiveContext *cx, T &table, const KeyInput &key, const ValueInput &value) { |
|
35 #ifdef JSGC_GENERATIONAL |
|
36 bool gcHappened = originalGcNumber != cx->zone()->gcNumber(); |
|
37 if (gcHappened) |
|
38 addPtr = table.lookupForAdd(key); |
|
39 #endif |
|
40 return table.relookupOrAdd(addPtr, key, value); |
|
41 } |
|
42 |
|
43 typedef void (DependentAddPtr::* ConvertibleToBool)(); |
|
44 void nonNull() {} |
|
45 |
|
46 bool found() const { return addPtr.found(); } |
|
47 operator ConvertibleToBool() const { return found() ? &DependentAddPtr::nonNull : 0; } |
|
48 const Entry &operator*() const { return *addPtr; } |
|
49 const Entry *operator->() const { return &*addPtr; } |
|
50 |
|
51 private: |
|
52 AddPtr addPtr ; |
|
53 #ifdef JSGC_GENERATIONAL |
|
54 const uint64_t originalGcNumber; |
|
55 #endif |
|
56 |
|
57 DependentAddPtr() MOZ_DELETE; |
|
58 DependentAddPtr(const DependentAddPtr&) MOZ_DELETE; |
|
59 DependentAddPtr& operator=(const DependentAddPtr&) MOZ_DELETE; |
|
60 }; |
|
61 |
|
62 } // namespace js |
|
63 |
|
64 #endif |