Pyrogenesis  trunk
ObjectToIDMap.h
Go to the documentation of this file.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 
6 /**
7  * Providing a map-like structure with JSObject pointers (actually their hash) as keys
8  * with correct garbage collection handling (JSObjects can move in memory).
9  *
10  * The code in this class was copied from here and modified to work in our environment.
11  * * https://mxr.mozilla.org/mozilla-esr38/source/js/ipc/JavaScriptShared.h
12  * * https://mxr.mozilla.org/mozilla-esr38/source/js/ipc/JavaScriptShared.cpp
13  *
14  * When updating SpiderMonkey, you most likely have to reintegrate an updated version
15  * of the class(es) in this file. The best way is probably to get a diff between the
16  * original files and integrate that because this file is heavily modified from the
17  * original version.
18  */
19 
20 #ifndef INCLUDED_OBJECTTOIDMAP
21 #define INCLUDED_OBJECTTOIDMAP
22 
25 #include <stdint.h>
26 
27 // Map JSObjects -> ids
28 template <typename T>
30 {
31  typedef js::PointerHasher<JSObject*, 3> Hasher;
32  typedef js::HashMap<JSObject*, T, Hasher, js::SystemAllocPolicy> Table;
33 
35 
36 public:
37  ObjectIdCache(shared_ptr<ScriptRuntime> rt)
38  : table_(nullptr), m_rt(rt)
39  {
40  }
41 
43  {
44  if (table_)
45  {
46  m_rt->AddDeferredFinalizationObject(std::shared_ptr<void>((void*)table_, DeleteTable));
47  table_ = nullptr;
48  JS_RemoveExtraGCRootsTracer(m_rt->m_rt, ObjectIdCache::Trace, this);
49  }
50  }
51 
52  bool init()
53  {
54  if (table_)
55  return true;
56 
57  table_ = new Table(js::SystemAllocPolicy());
58  JS_AddExtraGCRootsTracer(m_rt->m_rt, ObjectIdCache::Trace, this);
59  return table_ && table_->init(32);
60  }
61 
62  void trace(JSTracer* trc)
63  {
64  for (typename Table::Enum e(*table_); !e.empty(); e.popFront())
65  {
66  JSObject* obj = e.front().key();
67  JS_CallUnbarrieredObjectTracer(trc, &obj, "ipc-object");
68  if (obj != e.front().key())
69  e.rekeyFront(obj);
70  }
71  }
72 
73 // TODO sweep?
74 
75  bool find(JSObject* obj, T& ret)
76  {
77  typename Table::Ptr p = table_->lookup(obj);
78  if (!p)
79  return false;
80  ret = p->value();
81  return true;
82  }
83 
84  bool add(JSContext* cx, JSObject* obj, T id)
85  {
86  if (!table_->put(obj, id))
87  return false;
88  JS_StoreObjectPostBarrierCallback(cx, keyMarkCallback, obj, table_);
89  return true;
90  }
91 
92  void remove(JSObject* obj)
93  {
94  table_->remove(obj);
95  }
96 
97 // TODO clear?
98 
99  bool empty()
100  {
101  return table_->empty();
102  }
103 
104  bool has(JSObject* obj)
105  {
106  return table_->has(obj);
107  }
108 
109 private:
110  static void keyMarkCallback(JSTracer* trc, JSObject* key, void* data)
111  {
112  Table* table = static_cast<Table*>(data);
113  JSObject* prior = key;
114  JS_CallUnbarrieredObjectTracer(trc, &key, "ObjectIdCache::table_ key");
115  table->rekeyIfMoved(prior, key);
116  }
117 
118  static void Trace(JSTracer* trc, void* data)
119  {
120  reinterpret_cast<ObjectIdCache*>(data)->trace(trc);
121  }
122 
123  static void DeleteTable(void* table)
124  {
125  delete (Table*)table;
126  }
127 
128  shared_ptr<ScriptRuntime> m_rt;
129  Table* table_;
130 };
131 
132 #endif // INCLUDED_OBJECTTOIDMAP
ObjectIdCache(shared_ptr< ScriptRuntime > rt)
Definition: ObjectToIDMap.h:37
NONCOPYABLE(ObjectIdCache)
~ObjectIdCache()
Definition: ObjectToIDMap.h:42
void trace(JSTracer *trc)
Definition: ObjectToIDMap.h:62
bool init()
Definition: ObjectToIDMap.h:52
shared_ptr< ScriptRuntime > m_rt
Definition: ObjectToIDMap.h:128
bool has(JSObject *obj)
Definition: ObjectToIDMap.h:104
bool add(JSContext *cx, JSObject *obj, T id)
Definition: ObjectToIDMap.h:84
bool empty()
Definition: ObjectToIDMap.h:99
static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data)
Definition: ObjectToIDMap.h:110
static void Trace(JSTracer *trc, void *data)
Definition: ObjectToIDMap.h:118
js::HashMap< JSObject *, T, Hasher, js::SystemAllocPolicy > Table
Definition: ObjectToIDMap.h:32
pthread_key_t key
Definition: wpthread.cpp:140
#define T(string_literal)
Definition: secure_crt.cpp:76
static void DeleteTable(void *table)
Definition: ObjectToIDMap.h:123
bool find(JSObject *obj, T &ret)
Definition: ObjectToIDMap.h:75
Table * table_
Definition: ObjectToIDMap.h:129
js::PointerHasher< JSObject *, 3 > Hasher
Definition: ObjectToIDMap.h:31
Providing a map-like structure with JSObject pointers (actually their hash) as keys with correct garb...
Definition: ObjectToIDMap.h:29