1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Communicator client code, released
17 : * March 31, 1998.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 1998
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * John Bandhauer <jband@netscape.com> (original author)
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : /* Private maps (hashtables). */
42 :
43 : #ifndef xpcmaps_h___
44 : #define xpcmaps_h___
45 :
46 : // Maps...
47 :
48 : // Note that most of the declarations for hash table entries begin with
49 : // a pointer to something or another. This makes them look enough like
50 : // the JSDHashEntryStub struct that the default OPs (JS_DHashGetStubOps())
51 : // just do the right thing for most of our needs.
52 :
53 : // no virtuals in the maps - all the common stuff inlined
54 : // templates could be used to good effect here.
55 :
56 : /*************************/
57 :
58 : class JSObject2WrappedJSMap
59 : {
60 : public:
61 : struct Entry : public JSDHashEntryHdr
62 : {
63 : JSObject* key;
64 : nsXPCWrappedJS* value;
65 : };
66 :
67 : static JSObject2WrappedJSMap* newMap(int size);
68 :
69 156775 : inline nsXPCWrappedJS* Find(JSObject* Obj)
70 : {
71 156775 : NS_PRECONDITION(Obj,"bad param");
72 : Entry* entry = (Entry*)
73 156775 : JS_DHashTableOperate(mTable, Obj, JS_DHASH_LOOKUP);
74 156775 : if (JS_DHASH_ENTRY_IS_FREE(entry))
75 88800 : return nsnull;
76 67975 : return entry->value;
77 : }
78 :
79 88800 : inline nsXPCWrappedJS* Add(nsXPCWrappedJS* wrapper)
80 : {
81 88800 : NS_PRECONDITION(wrapper,"bad param");
82 88800 : JSObject* obj = wrapper->GetJSObjectPreserveColor();
83 : Entry* entry = (Entry*)
84 88800 : JS_DHashTableOperate(mTable, obj, JS_DHASH_ADD);
85 88800 : if (!entry)
86 0 : return nsnull;
87 88800 : if (entry->key)
88 0 : return entry->value;
89 88800 : entry->key = obj;
90 88800 : entry->value = wrapper;
91 88800 : return wrapper;
92 : }
93 :
94 177040 : inline void Remove(nsXPCWrappedJS* wrapper)
95 : {
96 177040 : NS_PRECONDITION(wrapper,"bad param");
97 177040 : JS_DHashTableOperate(mTable, wrapper->GetJSObjectPreserveColor(),
98 177040 : JS_DHASH_REMOVE);
99 177040 : }
100 :
101 0 : inline uint32_t Count() {return mTable->entryCount;}
102 16131 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
103 16131 : {return JS_DHashTableEnumerate(mTable, f, arg);}
104 :
105 : size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
106 :
107 : ~JSObject2WrappedJSMap();
108 : private:
109 : JSObject2WrappedJSMap(); // no implementation
110 : JSObject2WrappedJSMap(int size);
111 :
112 : static size_t SizeOfEntryExcludingThis(JSDHashEntryHdr *hdr, JSMallocSizeOfFun mallocSizeOf, void *);
113 :
114 : private:
115 : JSDHashTable *mTable;
116 : };
117 :
118 : /*************************/
119 :
120 : class Native2WrappedNativeMap
121 : {
122 : public:
123 : struct Entry : public JSDHashEntryHdr
124 : {
125 : nsISupports* key;
126 : XPCWrappedNative* value;
127 : };
128 :
129 : static Native2WrappedNativeMap* newMap(int size);
130 :
131 3269585 : inline XPCWrappedNative* Find(nsISupports* Obj)
132 : {
133 3269585 : NS_PRECONDITION(Obj,"bad param");
134 : Entry* entry = (Entry*)
135 3269585 : JS_DHashTableOperate(mTable, Obj, JS_DHASH_LOOKUP);
136 3269585 : if (JS_DHASH_ENTRY_IS_FREE(entry))
137 1244097 : return nsnull;
138 2025488 : return entry->value;
139 : }
140 :
141 1256303 : inline XPCWrappedNative* Add(XPCWrappedNative* wrapper)
142 : {
143 1256303 : NS_PRECONDITION(wrapper,"bad param");
144 1256303 : nsISupports* obj = wrapper->GetIdentityObject();
145 : Entry* entry = (Entry*)
146 1256303 : JS_DHashTableOperate(mTable, obj, JS_DHASH_ADD);
147 1256303 : if (!entry)
148 0 : return nsnull;
149 1256303 : if (entry->key)
150 0 : return entry->value;
151 1256303 : entry->key = obj;
152 1256303 : entry->value = wrapper;
153 1256303 : return wrapper;
154 : }
155 :
156 1255492 : inline void Remove(XPCWrappedNative* wrapper)
157 : {
158 1255492 : NS_PRECONDITION(wrapper,"bad param");
159 : #ifdef DEBUG
160 1255492 : XPCWrappedNative* wrapperInMap = Find(wrapper->GetIdentityObject());
161 1255492 : NS_ASSERTION(!wrapperInMap || wrapperInMap == wrapper,
162 : "About to remove a different wrapper with the same "
163 : "nsISupports identity! This will most likely cause serious "
164 : "problems!");
165 : #endif
166 1255492 : JS_DHashTableOperate(mTable, wrapper->GetIdentityObject(), JS_DHASH_REMOVE);
167 1255492 : }
168 :
169 15471 : inline uint32_t Count() {return mTable->entryCount;}
170 620923 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
171 620923 : {return JS_DHashTableEnumerate(mTable, f, arg);}
172 :
173 : size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
174 :
175 : ~Native2WrappedNativeMap();
176 : private:
177 : Native2WrappedNativeMap(); // no implementation
178 : Native2WrappedNativeMap(int size);
179 :
180 : static size_t SizeOfEntryExcludingThis(JSDHashEntryHdr *hdr, JSMallocSizeOfFun mallocSizeOf, void *);
181 :
182 : private:
183 : JSDHashTable *mTable;
184 : };
185 :
186 : /*************************/
187 :
188 : class IID2WrappedJSClassMap
189 : {
190 : public:
191 : struct Entry : public JSDHashEntryHdr
192 : {
193 : const nsIID* key;
194 : nsXPCWrappedJSClass* value;
195 :
196 : static struct JSDHashTableOps sOps;
197 : };
198 :
199 : static IID2WrappedJSClassMap* newMap(int size);
200 :
201 156779 : inline nsXPCWrappedJSClass* Find(REFNSIID iid)
202 : {
203 : Entry* entry = (Entry*)
204 156779 : JS_DHashTableOperate(mTable, &iid, JS_DHASH_LOOKUP);
205 156779 : if (JS_DHASH_ENTRY_IS_FREE(entry))
206 32512 : return nsnull;
207 124267 : return entry->value;
208 : }
209 :
210 32512 : inline nsXPCWrappedJSClass* Add(nsXPCWrappedJSClass* clazz)
211 : {
212 32512 : NS_PRECONDITION(clazz,"bad param");
213 32512 : const nsIID* iid = &clazz->GetIID();
214 : Entry* entry = (Entry*)
215 32512 : JS_DHashTableOperate(mTable, iid, JS_DHASH_ADD);
216 32512 : if (!entry)
217 0 : return nsnull;
218 32512 : if (entry->key)
219 0 : return entry->value;
220 32512 : entry->key = iid;
221 32512 : entry->value = clazz;
222 32512 : return clazz;
223 : }
224 :
225 32498 : inline void Remove(nsXPCWrappedJSClass* clazz)
226 : {
227 32498 : NS_PRECONDITION(clazz,"bad param");
228 32498 : JS_DHashTableOperate(mTable, &clazz->GetIID(), JS_DHASH_REMOVE);
229 32498 : }
230 :
231 0 : inline uint32_t Count() {return mTable->entryCount;}
232 0 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
233 0 : {return JS_DHashTableEnumerate(mTable, f, arg);}
234 :
235 : ~IID2WrappedJSClassMap();
236 : private:
237 : IID2WrappedJSClassMap(); // no implementation
238 : IID2WrappedJSClassMap(int size);
239 : private:
240 : JSDHashTable *mTable;
241 : };
242 :
243 : /*************************/
244 :
245 : class IID2NativeInterfaceMap
246 : {
247 : public:
248 : struct Entry : public JSDHashEntryHdr
249 : {
250 : const nsIID* key;
251 : XPCNativeInterface* value;
252 :
253 : static struct JSDHashTableOps sOps;
254 : };
255 :
256 : static IID2NativeInterfaceMap* newMap(int size);
257 :
258 2559400 : inline XPCNativeInterface* Find(REFNSIID iid)
259 : {
260 : Entry* entry = (Entry*)
261 2559400 : JS_DHashTableOperate(mTable, &iid, JS_DHASH_LOOKUP);
262 2559400 : if (JS_DHASH_ENTRY_IS_FREE(entry))
263 193726 : return nsnull;
264 2365674 : return entry->value;
265 : }
266 :
267 127559 : inline XPCNativeInterface* Add(XPCNativeInterface* iface)
268 : {
269 127559 : NS_PRECONDITION(iface,"bad param");
270 127559 : const nsIID* iid = iface->GetIID();
271 : Entry* entry = (Entry*)
272 127559 : JS_DHashTableOperate(mTable, iid, JS_DHASH_ADD);
273 127559 : if (!entry)
274 0 : return nsnull;
275 127559 : if (entry->key)
276 0 : return entry->value;
277 127559 : entry->key = iid;
278 127559 : entry->value = iface;
279 127559 : return iface;
280 : }
281 :
282 : inline void Remove(XPCNativeInterface* iface)
283 : {
284 : NS_PRECONDITION(iface,"bad param");
285 : JS_DHashTableOperate(mTable, iface->GetIID(), JS_DHASH_REMOVE);
286 : }
287 :
288 0 : inline uint32_t Count() {return mTable->entryCount;}
289 14728 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
290 14728 : {return JS_DHashTableEnumerate(mTable, f, arg);}
291 :
292 : size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
293 :
294 : ~IID2NativeInterfaceMap();
295 : private:
296 : IID2NativeInterfaceMap(); // no implementation
297 : IID2NativeInterfaceMap(int size);
298 :
299 : static size_t SizeOfEntryExcludingThis(JSDHashEntryHdr *hdr, JSMallocSizeOfFun mallocSizeOf, void *);
300 :
301 : private:
302 : JSDHashTable *mTable;
303 : };
304 :
305 : /*************************/
306 :
307 : class ClassInfo2NativeSetMap
308 : {
309 : public:
310 : struct Entry : public JSDHashEntryHdr
311 : {
312 : nsIClassInfo* key;
313 : XPCNativeSet* value;
314 : };
315 :
316 : static ClassInfo2NativeSetMap* newMap(int size);
317 :
318 186934 : inline XPCNativeSet* Find(nsIClassInfo* info)
319 : {
320 : Entry* entry = (Entry*)
321 186934 : JS_DHashTableOperate(mTable, info, JS_DHASH_LOOKUP);
322 186934 : if (JS_DHASH_ENTRY_IS_FREE(entry))
323 148813 : return nsnull;
324 38121 : return entry->value;
325 : }
326 :
327 148813 : inline XPCNativeSet* Add(nsIClassInfo* info, XPCNativeSet* set)
328 : {
329 148813 : NS_PRECONDITION(info,"bad param");
330 : Entry* entry = (Entry*)
331 148813 : JS_DHashTableOperate(mTable, info, JS_DHASH_ADD);
332 148813 : if (!entry)
333 0 : return nsnull;
334 148813 : if (entry->key)
335 0 : return entry->value;
336 148813 : entry->key = info;
337 148813 : entry->value = set;
338 148813 : return set;
339 : }
340 :
341 186781 : inline void Remove(nsIClassInfo* info)
342 : {
343 186781 : NS_PRECONDITION(info,"bad param");
344 186781 : JS_DHashTableOperate(mTable, info, JS_DHASH_REMOVE);
345 186781 : }
346 :
347 0 : inline uint32_t Count() {return mTable->entryCount;}
348 14728 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
349 14728 : {return JS_DHashTableEnumerate(mTable, f, arg);}
350 :
351 : // ClassInfo2NativeSetMap holds pointers to *some* XPCNativeSets.
352 : // So we don't want to count those XPCNativeSets, because they are better
353 : // counted elsewhere (i.e. in XPCJSRuntime::mNativeSetMap, which holds
354 : // pointers to *all* XPCNativeSets). Hence the "Shallow".
355 : size_t ShallowSizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
356 :
357 : ~ClassInfo2NativeSetMap();
358 : private:
359 : ClassInfo2NativeSetMap(); // no implementation
360 : ClassInfo2NativeSetMap(int size);
361 : private:
362 : JSDHashTable *mTable;
363 : };
364 :
365 : /*************************/
366 :
367 : class ClassInfo2WrappedNativeProtoMap
368 : {
369 : public:
370 : struct Entry : public JSDHashEntryHdr
371 : {
372 : nsIClassInfo* key;
373 : XPCWrappedNativeProto* value;
374 : };
375 :
376 : static ClassInfo2WrappedNativeProtoMap* newMap(int size);
377 :
378 569855 : inline XPCWrappedNativeProto* Find(nsIClassInfo* info)
379 : {
380 : Entry* entry = (Entry*)
381 569855 : JS_DHashTableOperate(mTable, info, JS_DHASH_LOOKUP);
382 569855 : if (JS_DHASH_ENTRY_IS_FREE(entry))
383 186922 : return nsnull;
384 382933 : return entry->value;
385 : }
386 :
387 186922 : inline XPCWrappedNativeProto* Add(nsIClassInfo* info, XPCWrappedNativeProto* proto)
388 : {
389 186922 : NS_PRECONDITION(info,"bad param");
390 : Entry* entry = (Entry*)
391 186922 : JS_DHashTableOperate(mTable, info, JS_DHASH_ADD);
392 186922 : if (!entry)
393 0 : return nsnull;
394 186922 : if (entry->key)
395 0 : return entry->value;
396 186922 : entry->key = info;
397 186922 : entry->value = proto;
398 186922 : return proto;
399 : }
400 :
401 186781 : inline void Remove(nsIClassInfo* info)
402 : {
403 186781 : NS_PRECONDITION(info,"bad param");
404 186781 : JS_DHashTableOperate(mTable, info, JS_DHASH_REMOVE);
405 186781 : }
406 :
407 30942 : inline uint32_t Count() {return mTable->entryCount;}
408 588956 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
409 588956 : {return JS_DHashTableEnumerate(mTable, f, arg);}
410 :
411 : size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
412 :
413 : ~ClassInfo2WrappedNativeProtoMap();
414 : private:
415 : ClassInfo2WrappedNativeProtoMap(); // no implementation
416 : ClassInfo2WrappedNativeProtoMap(int size);
417 :
418 : static size_t SizeOfEntryExcludingThis(JSDHashEntryHdr *hdr, JSMallocSizeOfFun mallocSizeOf, void *);
419 :
420 : private:
421 : JSDHashTable *mTable;
422 : };
423 :
424 : /*************************/
425 :
426 : class NativeSetMap
427 : {
428 : public:
429 : struct Entry : public JSDHashEntryHdr
430 : {
431 : XPCNativeSet* key_value;
432 :
433 : static JSBool
434 : Match(JSDHashTable *table,
435 : const JSDHashEntryHdr *entry,
436 : const void *key);
437 :
438 : static struct JSDHashTableOps sOps;
439 : };
440 :
441 : static NativeSetMap* newMap(int size);
442 :
443 1089420 : inline XPCNativeSet* Find(XPCNativeSetKey* key)
444 : {
445 : Entry* entry = (Entry*)
446 1089420 : JS_DHashTableOperate(mTable, key, JS_DHASH_LOOKUP);
447 1089420 : if (JS_DHASH_ENTRY_IS_FREE(entry))
448 88118 : return nsnull;
449 1001302 : return entry->key_value;
450 : }
451 :
452 192276 : inline XPCNativeSet* Add(const XPCNativeSetKey* key, XPCNativeSet* set)
453 : {
454 192276 : NS_PRECONDITION(key,"bad param");
455 192276 : NS_PRECONDITION(set,"bad param");
456 : Entry* entry = (Entry*)
457 192276 : JS_DHashTableOperate(mTable, key, JS_DHASH_ADD);
458 192276 : if (!entry)
459 0 : return nsnull;
460 192276 : if (entry->key_value)
461 77013 : return entry->key_value;
462 115263 : entry->key_value = set;
463 115263 : return set;
464 : }
465 :
466 : inline XPCNativeSet* Add(XPCNativeSet* set)
467 : {
468 : XPCNativeSetKey key(set, nsnull, 0);
469 : return Add(&key, set);
470 : }
471 :
472 : inline void Remove(XPCNativeSet* set)
473 : {
474 : NS_PRECONDITION(set,"bad param");
475 :
476 : XPCNativeSetKey key(set, nsnull, 0);
477 : JS_DHashTableOperate(mTable, &key, JS_DHASH_REMOVE);
478 : }
479 :
480 0 : inline uint32_t Count() {return mTable->entryCount;}
481 14728 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
482 14728 : {return JS_DHashTableEnumerate(mTable, f, arg);}
483 :
484 : size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
485 :
486 : ~NativeSetMap();
487 : private:
488 : NativeSetMap(); // no implementation
489 : NativeSetMap(int size);
490 :
491 : static size_t SizeOfEntryExcludingThis(JSDHashEntryHdr *hdr, JSMallocSizeOfFun mallocSizeOf, void *);
492 :
493 : private:
494 : JSDHashTable *mTable;
495 : };
496 :
497 : /***************************************************************************/
498 :
499 : class IID2ThisTranslatorMap
500 : {
501 : public:
502 : struct Entry : public JSDHashEntryHdr
503 : {
504 : nsIID key;
505 : nsIXPCFunctionThisTranslator* value;
506 :
507 : static JSBool
508 : Match(JSDHashTable *table,
509 : const JSDHashEntryHdr *entry,
510 : const void *key);
511 :
512 : static void
513 : Clear(JSDHashTable *table, JSDHashEntryHdr *entry);
514 :
515 : static struct JSDHashTableOps sOps;
516 : };
517 :
518 : static IID2ThisTranslatorMap* newMap(int size);
519 :
520 7772 : inline nsIXPCFunctionThisTranslator* Find(REFNSIID iid)
521 : {
522 : Entry* entry = (Entry*)
523 7772 : JS_DHashTableOperate(mTable, &iid, JS_DHASH_LOOKUP);
524 7772 : if (JS_DHASH_ENTRY_IS_FREE(entry))
525 3807 : return nsnull;
526 3965 : return entry->value;
527 : }
528 :
529 306 : inline nsIXPCFunctionThisTranslator* Add(REFNSIID iid,
530 : nsIXPCFunctionThisTranslator* obj)
531 : {
532 :
533 : Entry* entry = (Entry*)
534 306 : JS_DHashTableOperate(mTable, &iid, JS_DHASH_ADD);
535 306 : if (!entry)
536 0 : return nsnull;
537 306 : NS_IF_ADDREF(obj);
538 306 : NS_IF_RELEASE(entry->value);
539 306 : entry->value = obj;
540 306 : entry->key = iid;
541 306 : return obj;
542 : }
543 :
544 : inline void Remove(REFNSIID iid)
545 : {
546 : JS_DHashTableOperate(mTable, &iid, JS_DHASH_REMOVE);
547 : }
548 :
549 0 : inline uint32_t Count() {return mTable->entryCount;}
550 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
551 : {return JS_DHashTableEnumerate(mTable, f, arg);}
552 :
553 : ~IID2ThisTranslatorMap();
554 : private:
555 : IID2ThisTranslatorMap(); // no implementation
556 : IID2ThisTranslatorMap(int size);
557 : private:
558 : JSDHashTable *mTable;
559 : };
560 :
561 : /***************************************************************************/
562 :
563 : class XPCNativeScriptableSharedMap
564 : {
565 : public:
566 : struct Entry : public JSDHashEntryHdr
567 : {
568 : XPCNativeScriptableShared* key;
569 :
570 : static JSDHashNumber
571 : Hash(JSDHashTable *table, const void *key);
572 :
573 : static JSBool
574 : Match(JSDHashTable *table,
575 : const JSDHashEntryHdr *entry,
576 : const void *key);
577 :
578 : static struct JSDHashTableOps sOps;
579 : };
580 :
581 : static XPCNativeScriptableSharedMap* newMap(int size);
582 :
583 : JSBool GetNewOrUsed(uint32_t flags, char* name, PRUint32 interfacesBitmap,
584 : XPCNativeScriptableInfo* si);
585 :
586 : inline uint32_t Count() {return mTable->entryCount;}
587 11922 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
588 11922 : {return JS_DHashTableEnumerate(mTable, f, arg);}
589 :
590 : ~XPCNativeScriptableSharedMap();
591 : private:
592 : XPCNativeScriptableSharedMap(); // no implementation
593 : XPCNativeScriptableSharedMap(int size);
594 : private:
595 : JSDHashTable *mTable;
596 : };
597 :
598 : /***************************************************************************/
599 :
600 : class XPCWrappedNativeProtoMap
601 : {
602 : public:
603 : static XPCWrappedNativeProtoMap* newMap(int size);
604 :
605 186781 : inline XPCWrappedNativeProto* Add(XPCWrappedNativeProto* proto)
606 : {
607 186781 : NS_PRECONDITION(proto,"bad param");
608 : JSDHashEntryStub* entry = (JSDHashEntryStub*)
609 186781 : JS_DHashTableOperate(mTable, proto, JS_DHASH_ADD);
610 186781 : if (!entry)
611 0 : return nsnull;
612 186781 : if (entry->key)
613 0 : return (XPCWrappedNativeProto*) entry->key;
614 186781 : entry->key = proto;
615 186781 : return proto;
616 : }
617 :
618 186781 : inline void Remove(XPCWrappedNativeProto* proto)
619 : {
620 186781 : NS_PRECONDITION(proto,"bad param");
621 186781 : JS_DHashTableOperate(mTable, proto, JS_DHASH_REMOVE);
622 186781 : }
623 :
624 : inline uint32_t Count() {return mTable->entryCount;}
625 30859 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
626 30859 : {return JS_DHashTableEnumerate(mTable, f, arg);}
627 :
628 : ~XPCWrappedNativeProtoMap();
629 : private:
630 : XPCWrappedNativeProtoMap(); // no implementation
631 : XPCWrappedNativeProtoMap(int size);
632 : private:
633 : JSDHashTable *mTable;
634 : };
635 :
636 : class XPCNativeWrapperMap
637 : {
638 : public:
639 : static XPCNativeWrapperMap* newMap(int size);
640 :
641 : inline JSObject* Add(JSObject* nw)
642 : {
643 : NS_PRECONDITION(nw,"bad param");
644 : JSDHashEntryStub* entry = (JSDHashEntryStub*)
645 : JS_DHashTableOperate(mTable, nw, JS_DHASH_ADD);
646 : if (!entry)
647 : return nsnull;
648 : if (entry->key)
649 : return (JSObject*) entry->key;
650 : entry->key = nw;
651 : return nw;
652 : }
653 :
654 : inline void Remove(JSObject* nw)
655 : {
656 : NS_PRECONDITION(nw,"bad param");
657 : JS_DHashTableOperate(mTable, nw, JS_DHASH_REMOVE);
658 : }
659 :
660 : inline uint32_t Count() {return mTable->entryCount;}
661 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
662 : {return JS_DHashTableEnumerate(mTable, f, arg);}
663 :
664 : ~XPCNativeWrapperMap();
665 : private:
666 : XPCNativeWrapperMap(); // no implementation
667 : XPCNativeWrapperMap(int size);
668 : private:
669 : JSDHashTable *mTable;
670 : };
671 :
672 : class WrappedNative2WrapperMap
673 : {
674 : static struct JSDHashTableOps sOps;
675 :
676 : static void ClearLink(JSDHashTable* table, JSDHashEntryHdr* entry);
677 : static void MoveLink(JSDHashTable* table, const JSDHashEntryHdr* from,
678 : JSDHashEntryHdr* to);
679 :
680 : public:
681 : struct Link : public PRCList
682 : {
683 : JSObject *obj;
684 : };
685 :
686 : struct Entry : public JSDHashEntryHdr
687 : {
688 : // Note: key must be the flat JSObject for a wrapped native.
689 : JSObject* key;
690 : Link value;
691 : };
692 :
693 : static WrappedNative2WrapperMap* newMap(int size);
694 :
695 : inline JSObject* Find(JSObject* wrapper)
696 : {
697 : NS_PRECONDITION(wrapper, "bad param");
698 : Entry* entry = (Entry*)
699 : JS_DHashTableOperate(mTable, wrapper, JS_DHASH_LOOKUP);
700 : if (JS_DHASH_ENTRY_IS_FREE(entry))
701 : return nsnull;
702 : return entry->value.obj;
703 : }
704 :
705 : // Note: If the entry already exists, then this will overwrite the
706 : // existing entry, returning the old value.
707 : JSObject* Add(WrappedNative2WrapperMap* head,
708 : JSObject* wrappedObject,
709 : JSObject* wrapper);
710 :
711 : // Function to find a link.
712 0 : Link* FindLink(JSObject* wrappedObject)
713 : {
714 : Entry* entry = (Entry*)
715 0 : JS_DHashTableOperate(mTable, wrappedObject, JS_DHASH_LOOKUP);
716 0 : if (JS_DHASH_ENTRY_IS_BUSY(entry))
717 0 : return &entry->value;
718 0 : return nsnull;
719 : }
720 :
721 : // "Internal" function to add an empty link without doing unnecessary
722 : // work.
723 : bool AddLink(JSObject* wrappedObject, Link* oldLink);
724 :
725 : inline void Remove(JSObject* wrapper)
726 : {
727 : NS_PRECONDITION(wrapper,"bad param");
728 : JS_DHashTableOperate(mTable, wrapper, JS_DHASH_REMOVE);
729 : }
730 :
731 : inline uint32_t Count() {return mTable->entryCount;}
732 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
733 : {return JS_DHashTableEnumerate(mTable, f, arg);}
734 :
735 : ~WrappedNative2WrapperMap();
736 :
737 : private:
738 : WrappedNative2WrapperMap(); // no implementation
739 : WrappedNative2WrapperMap(int size);
740 :
741 : private:
742 : JSDHashTable *mTable;
743 : };
744 :
745 : class JSObject2JSObjectMap
746 : {
747 : static struct JSDHashTableOps sOps;
748 :
749 : public:
750 : struct Entry : public JSDHashEntryHdr
751 : {
752 : JSObject* key;
753 : JSObject* value;
754 : };
755 :
756 0 : static JSObject2JSObjectMap* newMap(int size)
757 : {
758 0 : JSObject2JSObjectMap* map = new JSObject2JSObjectMap(size);
759 0 : if (map && map->mTable)
760 0 : return map;
761 0 : delete map;
762 0 : return nsnull;
763 : }
764 :
765 0 : inline JSObject* Find(JSObject* key)
766 : {
767 0 : NS_PRECONDITION(key, "bad param");
768 : Entry* entry = (Entry*)
769 0 : JS_DHashTableOperate(mTable, key, JS_DHASH_LOOKUP);
770 0 : if (JS_DHASH_ENTRY_IS_FREE(entry))
771 0 : return nsnull;
772 0 : return entry->value;
773 : }
774 :
775 : // Note: If the entry already exists, return the old value.
776 0 : inline JSObject* Add(JSObject *key, JSObject *value)
777 : {
778 0 : NS_PRECONDITION(key,"bad param");
779 : Entry* entry = (Entry*)
780 0 : JS_DHashTableOperate(mTable, key, JS_DHASH_ADD);
781 0 : if (!entry)
782 0 : return nsnull;
783 0 : if (entry->key)
784 0 : return entry->value;
785 0 : entry->key = key;
786 0 : entry->value = value;
787 0 : return value;
788 : }
789 :
790 : inline void Remove(JSObject* key)
791 : {
792 : NS_PRECONDITION(key,"bad param");
793 : JS_DHashTableOperate(mTable, key, JS_DHASH_REMOVE);
794 : }
795 :
796 : inline uint32_t Count() {return mTable->entryCount;}
797 :
798 0 : inline uint32_t Enumerate(JSDHashEnumerator f, void *arg)
799 : {
800 0 : return JS_DHashTableEnumerate(mTable, f, arg);
801 : }
802 :
803 0 : ~JSObject2JSObjectMap()
804 : {
805 0 : if (mTable)
806 0 : JS_DHashTableDestroy(mTable);
807 0 : }
808 :
809 : private:
810 0 : JSObject2JSObjectMap(int size)
811 : {
812 0 : mTable = JS_NewDHashTable(&sOps, nsnull, sizeof(Entry), size);
813 0 : }
814 :
815 : JSObject2JSObjectMap(); // no implementation
816 :
817 : private:
818 : JSDHashTable *mTable;
819 : };
820 :
821 : #endif /* xpcmaps_h___ */
|