VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsAtomTable.cpp@ 104488

最後變更 在這個檔案從104488是 102457,由 vboxsync 提交於 12 月 前

libs/xpcom: Get rid of PL_strcmp/PL_strncmp and replace with IPRT equivalents, bugref:10545

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.0 KB
 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2// vim:cindent:ts=2:et:sw=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.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38#include <iprt/string.h>
39
40#include "nsAtomTable.h"
41#include "nsStaticAtom.h"
42#include "nsString.h"
43#include "nsReadableUtils.h"
44#include "nsCRT.h"
45#include "pldhash.h"
46#include "nsVoidArray.h"
47
48#define PL_ARENA_CONST_ALIGN_MASK 3
49#include "plarena.h"
50
51class nsStaticAtomWrapper;
52
53/**
54 * The shared hash table for atom lookups.
55 *
56 * XXX This should be manipulated in a threadsafe way or we should make
57 * sure it's only manipulated from the main thread. Probably the latter
58 * is better, since the former would hurt performance.
59 *
60 * If |gAtomTable.ops| is 0, then the table is uninitialized.
61 */
62static PLDHashTable gAtomTable;
63
64// this is where we keep the nsStaticAtomWrapper objects
65
66static PLArenaPool* gStaticAtomArena = 0;
67
68class nsStaticAtomWrapper : public nsIAtom
69{
70public:
71 nsStaticAtomWrapper(const nsStaticAtom* aAtom) :
72 mStaticAtom(aAtom)
73 {
74 MOZ_COUNT_CTOR(nsStaticAtomWrapper);
75 }
76 ~nsStaticAtomWrapper() { // no subclasses -> not virtual
77 // this is arena allocated and won't be called except in debug
78 // builds. If this function ever does anything non-debug, be sure
79 // to get rid of the ifdefs in AtomTableClearEntry!
80 MOZ_COUNT_DTOR(nsStaticAtomWrapper);
81 }
82
83 NS_IMETHOD QueryInterface(REFNSIID aIID,
84 void** aInstancePtr);
85 NS_IMETHOD_(nsrefcnt) AddRef(void);
86 NS_IMETHOD_(nsrefcnt) Release(void);
87
88 NS_DECL_NSIATOM
89
90 const nsStaticAtom* GetStaticAtom() {
91 return mStaticAtom;
92 }
93private:
94 const nsStaticAtom* mStaticAtom;
95};
96
97// the atomtableentry can contain either an AtomImpl or a
98// nsStaticAtomWrapper, indicated by the first bit of PtrBits
99typedef unsigned long PtrBits;
100
101struct AtomTableEntry : public PLDHashEntryHdr {
102 // mAtom & 0x1 means (mAtom & ~0x1) points to an nsStaticAtomWrapper
103 // else it points to an nsAtomImpl
104 PtrBits mAtom;
105
106 inline PRBool IsStaticAtom() const {
107 return (mAtom & 0x1) != 0;
108 }
109
110 inline void SetAtomImpl(AtomImpl* aAtom) {
111 NS_ASSERTION(aAtom, "Setting null atom");
112 mAtom = PtrBits(aAtom);
113 }
114
115 inline void SetStaticAtomWrapper(nsStaticAtomWrapper* aAtom) {
116 NS_ASSERTION(aAtom, "Setting null atom");
117 NS_ASSERTION((PtrBits(aAtom) & ~0x1) == PtrBits(aAtom),
118 "Pointers must align or this is broken");
119
120 mAtom = PtrBits(aAtom) | 0x1;
121 }
122
123 inline void ClearAtom() {
124 mAtom = nsnull;
125 }
126
127 inline PRBool HasValue() const {
128 return (mAtom & ~0x1) != 0;
129 }
130
131 // these accessors assume that you already know the type
132 inline AtomImpl *GetAtomImpl() const {
133 NS_ASSERTION(!IsStaticAtom(), "This is a static atom, not an AtomImpl");
134 return (AtomImpl*) (mAtom & ~0x1);
135 }
136
137 inline nsStaticAtomWrapper *GetStaticAtomWrapper() const {
138 NS_ASSERTION(IsStaticAtom(), "This is an AtomImpl, not a static atom");
139 return (nsStaticAtomWrapper*) (mAtom & ~0x1);
140 }
141
142 inline const nsStaticAtom* GetStaticAtom() const {
143 return GetStaticAtomWrapper()->GetStaticAtom();
144 }
145
146 // type-agnostic accessors
147
148 // get the string buffer
149 inline const char* get() const {
150 return IsStaticAtom() ? GetStaticAtom()->mString : GetAtomImpl()->mString;
151 }
152
153 // get an addreffed nsIAtom - not using already_AddRef'ed atom
154 // because the callers are not (and should not be) using nsCOMPtr
155 inline nsIAtom* GetAtom() const {
156 nsIAtom* result;
157
158 if (IsStaticAtom())
159 result = GetStaticAtomWrapper();
160 else {
161 result = GetAtomImpl();
162 NS_ADDREF(result);
163 }
164
165 return result;
166 }
167};
168
169PR_STATIC_CALLBACK(const void *)
170AtomTableGetKey(PLDHashTable *table, PLDHashEntryHdr *entry)
171{
172 AtomTableEntry *he = NS_STATIC_CAST(AtomTableEntry*, entry);
173 NS_ASSERTION(he->HasValue(), "Empty atom. how did that happen?");
174 return he->get();
175}
176
177PR_STATIC_CALLBACK(PRBool)
178AtomTableMatchKey(PLDHashTable *table,
179 const PLDHashEntryHdr *entry,
180 const void *key)
181{
182 const AtomTableEntry *he = NS_STATIC_CAST(const AtomTableEntry*, entry);
183 const char* keyStr = NS_STATIC_CAST(const char*, key);
184 return RTStrCmp(keyStr, he->get()) == 0;
185}
186
187PR_STATIC_CALLBACK(void)
188AtomTableClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
189{
190 AtomTableEntry *he = NS_STATIC_CAST(AtomTableEntry*, entry);
191
192 he->keyHash = 0;
193
194 if (!he->IsStaticAtom()) {
195 AtomImpl *atom = he->GetAtomImpl();
196 // Normal |AtomImpl| atoms are deleted when their refcount hits 0, and
197 // they then remove themselves from the table. In other words, they
198 // are owned by the callers who own references to them.
199 // |PermanentAtomImpl| permanent atoms ignore their refcount and are
200 // deleted when they are removed from the table at table destruction.
201 // In other words, they are owned by the atom table.
202 if (atom->IsPermanent())
203 delete NS_STATIC_CAST(PermanentAtomImpl*, atom);
204 }
205 else {
206 he->GetStaticAtomWrapper()->~nsStaticAtomWrapper();
207 }
208
209 he->ClearAtom();
210}
211
212static const PLDHashTableOps AtomTableOps = {
213 PL_DHashAllocTable,
214 PL_DHashFreeTable,
215 AtomTableGetKey,
216 PL_DHashStringKey,
217 AtomTableMatchKey,
218 PL_DHashMoveEntryStub,
219 AtomTableClearEntry,
220 PL_DHashFinalizeStub,
221 NULL
222};
223
224
225#ifdef DEBUG
226
227PR_STATIC_CALLBACK(PLDHashOperator)
228DumpAtomLeaks(PLDHashTable *table, PLDHashEntryHdr *he,
229 PRUint32 index, void *arg)
230{
231 AtomTableEntry *entry = NS_STATIC_CAST(AtomTableEntry*, he);
232
233 if (entry->IsStaticAtom())
234 return PL_DHASH_NEXT;
235
236 AtomImpl* atom = entry->GetAtomImpl();
237 if (!atom->IsPermanent()) {
238 ++*NS_STATIC_CAST(PRUint32*, arg);
239 const char *str;
240 atom->GetUTF8String(&str);
241 fputs(str, stdout);
242 fputs("\n", stdout);
243 }
244 return PL_DHASH_NEXT;
245}
246
247#endif
248
249static inline
250void PromoteToPermanent(AtomImpl* aAtom)
251{
252#ifdef NS_BUILD_REFCNT_LOGGING
253 {
254 nsrefcnt refcount = aAtom->GetRefCount();
255 do {
256 NS_LOG_RELEASE(aAtom, --refcount, "AtomImpl");
257 } while (refcount);
258 }
259#endif
260 aAtom = new (aAtom) PermanentAtomImpl();
261}
262
263void NS_PurgeAtomTable()
264{
265 if (gAtomTable.ops) {
266 PL_DHashTableFinish(&gAtomTable);
267 gAtomTable.entryCount = 0;
268 gAtomTable.ops = nsnull;
269
270 if (gStaticAtomArena) {
271 PL_FinishArenaPool(gStaticAtomArena);
272 delete gStaticAtomArena;
273 gStaticAtomArena = nsnull;
274 }
275 }
276}
277
278AtomImpl::AtomImpl()
279{
280}
281
282AtomImpl::~AtomImpl()
283{
284 NS_PRECONDITION(gAtomTable.ops, "uninitialized atom hashtable");
285 // Permanent atoms are removed from the hashtable at shutdown, and we
286 // don't want to remove them twice. See comment above in
287 // |AtomTableClearEntry|.
288 if (!IsPermanent()) {
289 PL_DHashTableOperate(&gAtomTable, mString, PL_DHASH_REMOVE);
290 if (gAtomTable.entryCount == 0) {
291 PL_DHashTableFinish(&gAtomTable);
292 NS_ASSERTION(gAtomTable.entryCount == 0,
293 "PL_DHashTableFinish changed the entry count");
294 }
295 }
296}
297
298NS_IMPL_THREADSAFE_ISUPPORTS1(AtomImpl, nsIAtom)
299
300NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::AddRef()
301{
302 return 2;
303}
304
305NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::Release()
306{
307 return 1;
308}
309
310/* virtual */ PRBool
311AtomImpl::IsPermanent()
312{
313 return PR_FALSE;
314}
315
316/* virtual */ PRBool
317PermanentAtomImpl::IsPermanent()
318{
319 return PR_TRUE;
320}
321
322void* AtomImpl::operator new ( size_t size, const nsACString& aString ) CPP_THROW_NEW
323{
324 /*
325 Note: since the |size| will initially also include the |PRUnichar| member
326 |mString|, our size calculation will give us one character too many.
327 We use that extra character for a zero-terminator.
328
329 Note: this construction is not guaranteed to be possible by the C++
330 compiler. A more reliable scheme is used by |nsShared[C]String|s, see
331 http://lxr.mozilla.org/seamonkey/source/xpcom/ds/nsSharedString.h#174
332 */
333 size += aString.Length() * sizeof(char);
334 AtomImpl* ii = NS_STATIC_CAST(AtomImpl*, ::operator new(size));
335
336 char* toBegin = &ii->mString[0];
337 nsACString::const_iterator fromBegin, fromEnd;
338 *copy_string(aString.BeginReading(fromBegin), aString.EndReading(fromEnd), toBegin) = '\0';
339 return ii;
340}
341
342void* PermanentAtomImpl::operator new ( size_t size, AtomImpl* aAtom ) CPP_THROW_NEW {
343 NS_ASSERTION(!aAtom->IsPermanent(),
344 "converting atom that's already permanent");
345
346 // Just let the constructor overwrite the vtable pointer.
347 return aAtom;
348}
349
350NS_IMETHODIMP
351AtomImpl::ToString(nsAString& aBuf)
352{
353 CopyUTF8toUTF16(nsDependentCString(mString), aBuf);
354 return NS_OK;
355}
356
357NS_IMETHODIMP
358AtomImpl::ToUTF8String(nsACString& aBuf)
359{
360 aBuf.Assign(mString);
361 return NS_OK;
362}
363
364NS_IMETHODIMP
365AtomImpl::GetUTF8String(const char **aResult)
366{
367 NS_PRECONDITION(aResult, "null out param");
368 *aResult = mString;
369 return NS_OK;
370}
371
372NS_IMETHODIMP
373AtomImpl::EqualsUTF8(const nsACString& aString, PRBool* aResult)
374{
375 *aResult = aString.Equals(mString);
376 return NS_OK;
377}
378
379NS_IMETHODIMP
380AtomImpl::Equals(const nsAString& aString, PRBool* aResult)
381{
382 *aResult = NS_ConvertUTF16toUTF8(aString).Equals(mString);
383 return NS_OK;
384}
385
386//----------------------------------------------------------------------
387
388// wrapper class for the nsStaticAtom struct
389
390NS_IMETHODIMP_(nsrefcnt)
391nsStaticAtomWrapper::AddRef()
392{
393 return 2;
394}
395
396NS_IMETHODIMP_(nsrefcnt)
397nsStaticAtomWrapper::Release()
398{
399 return 1;
400}
401
402NS_IMPL_QUERY_INTERFACE1(nsStaticAtomWrapper, nsIAtom)
403
404NS_IMETHODIMP
405nsStaticAtomWrapper::GetUTF8String(const char** aResult)
406{
407 *aResult = mStaticAtom->mString;
408 return NS_OK;
409}
410
411NS_IMETHODIMP
412nsStaticAtomWrapper::ToString(nsAString& aBuf)
413{
414 // static should always be always ASCII, to allow tools like gperf
415 // to generate the tables, and to avoid unnecessary conversion
416 NS_ASSERTION(nsCRT::IsAscii(mStaticAtom->mString),
417 "Data loss - atom should be ASCII");
418 CopyASCIItoUCS2(nsDependentCString(mStaticAtom->mString), aBuf);
419 return NS_OK;
420}
421
422NS_IMETHODIMP
423nsStaticAtomWrapper::ToUTF8String(nsACString& aBuf)
424{
425 aBuf.Assign(mStaticAtom->mString);
426 return NS_OK;
427}
428
429NS_IMETHODIMP
430nsStaticAtomWrapper::EqualsUTF8(const nsACString& aString, PRBool* aResult)
431{
432 *aResult = aString.Equals(mStaticAtom->mString);
433 return NS_OK;
434}
435
436NS_IMETHODIMP
437nsStaticAtomWrapper::Equals(const nsAString& aString, PRBool* aResult)
438{
439 *aResult = NS_ConvertUCS2toUTF8(aString).Equals(mStaticAtom->mString);
440 return NS_OK;
441}
442//----------------------------------------------------------------------
443
444NS_COM nsIAtom* NS_NewAtom(const char* isolatin1)
445{
446 return NS_NewAtom(nsDependentCString(isolatin1));
447}
448
449NS_COM nsIAtom* NS_NewPermanentAtom(const char* isolatin1)
450{
451 return NS_NewPermanentAtom(NS_ConvertASCIItoUCS2(isolatin1));
452}
453
454static nsStaticAtomWrapper*
455WrapStaticAtom(const nsStaticAtom* aAtom)
456{
457 if (!gStaticAtomArena) {
458 gStaticAtomArena = new PLArenaPool;
459 if (!gStaticAtomArena)
460 return nsnull;
461
462 PL_INIT_ARENA_POOL(gStaticAtomArena, "nsStaticAtomArena", 4096);
463 }
464
465 void* mem;
466 PL_ARENA_ALLOCATE(mem, gStaticAtomArena, sizeof(nsStaticAtom));
467
468 nsStaticAtomWrapper* wrapper =
469 new (mem) nsStaticAtomWrapper(aAtom);
470
471 return wrapper;
472}
473
474static AtomTableEntry* GetAtomHashEntry(const char* aString)
475{
476 if (!gAtomTable.ops &&
477 !PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
478 sizeof(AtomTableEntry), 2048)) {
479 gAtomTable.ops = nsnull;
480 return nsnull;
481 }
482 return NS_STATIC_CAST(AtomTableEntry*,
483 PL_DHashTableOperate(&gAtomTable,
484 aString,
485 PL_DHASH_ADD));
486}
487
488NS_COM nsresult
489NS_RegisterStaticAtoms(const nsStaticAtom* aAtoms, PRUint32 aAtomCount)
490{
491 // this does two things:
492 // 1) wraps each static atom in a wrapper, if necessary
493 // 2) initializes the address pointed to by each mAtom slot
494
495 for (PRUint32 i=0; i<aAtomCount; i++) {
496 NS_ASSERTION(nsCRT::IsAscii(aAtoms[i].mString),
497 "Static atoms must be ASCII!");
498 AtomTableEntry *he =
499 GetAtomHashEntry(aAtoms[i].mString);
500
501 if (he->HasValue() && aAtoms[i].mAtom) {
502 // there already is an atom with this name in the table.. but we
503 // still have to update mAtom
504 if (!he->IsStaticAtom() && !he->GetAtomImpl()->IsPermanent()) {
505 // since we wanted to create a static atom but there is
506 // already one there, we convert it to a non-refcounting
507 // permanent atom
508 PromoteToPermanent(he->GetAtomImpl());
509 }
510
511 // and now, if the consumer wants to remember this value in a
512 // slot, we do so
513 if (aAtoms[i].mAtom)
514 *aAtoms[i].mAtom = he->GetAtom();
515 }
516
517 else {
518 nsStaticAtomWrapper* atom = WrapStaticAtom(&aAtoms[i]);
519 NS_ASSERTION(atom, "Failed to wrap static atom");
520
521 // but even if atom is null, no real difference in code..
522 he->SetStaticAtomWrapper(atom);
523 if (aAtoms[i].mAtom)
524 *aAtoms[i].mAtom = atom;
525 }
526 }
527 return NS_OK;
528}
529
530NS_COM nsIAtom* NS_NewAtom( const nsAString& aString )
531{
532 NS_ConvertUCS2toUTF8 utf8String(aString);
533
534 return NS_NewAtom(utf8String);
535}
536
537NS_COM
538nsIAtom*
539NS_NewAtom( const nsACString& aString )
540{
541 AtomTableEntry *he = GetAtomHashEntry(PromiseFlatCString(aString).get());
542
543 if (he->HasValue())
544 return he->GetAtom();
545
546 AtomImpl* atom = new (aString) AtomImpl();
547 he->SetAtomImpl(atom);
548 if (!atom) {
549 PL_DHashTableRawRemove(&gAtomTable, he);
550 return nsnull;
551 }
552
553 NS_ADDREF(atom);
554 return atom;
555}
556
557NS_COM nsIAtom* NS_NewPermanentAtom( const nsAString& aString )
558{
559 return NS_NewPermanentAtom(NS_ConvertUCS2toUTF8(aString));
560}
561
562NS_COM
563nsIAtom* NS_NewPermanentAtom( const nsACString& aString )
564{
565 AtomTableEntry *he = GetAtomHashEntry(PromiseFlatCString(aString).get());
566
567 if (he->HasValue() && he->IsStaticAtom())
568 return he->GetStaticAtomWrapper();
569
570 // either there is no atom and we'll create an AtomImpl,
571 // or there is an existing AtomImpl
572 AtomImpl* atom = he->GetAtomImpl();
573
574 if (atom) {
575 // ensure that it's permanent
576 if (!atom->IsPermanent()) {
577 PromoteToPermanent(atom);
578 }
579 } else {
580 // otherwise, make a new atom
581 atom = new (aString) PermanentAtomImpl();
582 he->SetAtomImpl(atom);
583 if ( !atom ) {
584 PL_DHashTableRawRemove(&gAtomTable, he);
585 return nsnull;
586 }
587 }
588
589 NS_ADDREF(atom);
590 return atom;
591}
592
593NS_COM nsIAtom* NS_NewAtom( const PRUnichar* str )
594{
595 return NS_NewAtom(NS_ConvertUCS2toUTF8(str));
596}
597
598NS_COM nsIAtom* NS_NewPermanentAtom( const PRUnichar* str )
599{
600 return NS_NewPermanentAtom(nsDependentString(str));
601}
602
603NS_COM nsrefcnt NS_GetNumberOfAtoms(void)
604{
605 return gAtomTable.entryCount;
606}
607
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette