Line data Source code
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 vm_SharedImmutableStringsCache_inl_h
8 : #define vm_SharedImmutableStringsCache_inl_h
9 :
10 : #include "vm/SharedImmutableStringsCache.h"
11 :
12 : namespace js {
13 :
14 : template <typename IntoOwnedChars>
15 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableString>
16 0 : SharedImmutableStringsCache::getOrCreate(const char* chars, size_t length,
17 : IntoOwnedChars intoOwnedChars)
18 : {
19 0 : MOZ_ASSERT(inner_);
20 0 : MOZ_ASSERT(chars);
21 0 : Hasher::Lookup lookup(Hasher::hashLongString(chars, length), chars, length);
22 :
23 0 : auto locked = inner_->lock();
24 0 : if (!locked->set.initialized() && !locked->set.init())
25 0 : return mozilla::Nothing();
26 :
27 0 : auto entry = locked->set.lookupForAdd(lookup);
28 0 : if (!entry) {
29 0 : OwnedChars ownedChars(intoOwnedChars());
30 0 : if (!ownedChars)
31 0 : return mozilla::Nothing();
32 0 : MOZ_ASSERT(ownedChars.get() == chars ||
33 : memcmp(ownedChars.get(), chars, length) == 0);
34 0 : auto box = StringBox::Create(mozilla::Move(ownedChars), length);
35 0 : if (!box || !locked->set.add(entry, mozilla::Move(box)))
36 0 : return mozilla::Nothing();
37 : }
38 :
39 0 : MOZ_ASSERT(entry && *entry);
40 0 : return mozilla::Some(SharedImmutableString(locked, entry->get()));
41 : }
42 :
43 : template <typename IntoOwnedTwoByteChars>
44 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableTwoByteString>
45 1702 : SharedImmutableStringsCache::getOrCreate(const char16_t* chars, size_t length,
46 : IntoOwnedTwoByteChars intoOwnedTwoByteChars) {
47 1702 : MOZ_ASSERT(inner_);
48 1702 : MOZ_ASSERT(chars);
49 1702 : auto hash = Hasher::hashLongString(reinterpret_cast<const char*>(chars),
50 1702 : length * sizeof(char16_t));
51 1701 : Hasher::Lookup lookup(hash, chars, length);
52 :
53 3402 : auto locked = inner_->lock();
54 1701 : if (!locked->set.initialized() && !locked->set.init())
55 0 : return mozilla::Nothing();
56 :
57 1701 : auto entry = locked->set.lookupForAdd(lookup);
58 1701 : if (!entry) {
59 2482 : OwnedTwoByteChars ownedTwoByteChars(intoOwnedTwoByteChars());
60 1241 : if (!ownedTwoByteChars)
61 0 : return mozilla::Nothing();
62 1241 : MOZ_ASSERT(ownedTwoByteChars.get() == chars ||
63 : memcmp(ownedTwoByteChars.get(), chars, length * sizeof(char16_t)) == 0);
64 2482 : OwnedChars ownedChars(reinterpret_cast<char*>(ownedTwoByteChars.release()));
65 2482 : auto box = StringBox::Create(mozilla::Move(ownedChars), length * sizeof(char16_t));
66 1241 : if (!box || !locked->set.add(entry, mozilla::Move(box)))
67 0 : return mozilla::Nothing();
68 : }
69 :
70 1701 : MOZ_ASSERT(entry && *entry);
71 1701 : return mozilla::Some(SharedImmutableTwoByteString(locked, entry->get()));
72 : }
73 :
74 : } // namespace js
75 :
76 : #endif // vm_SharedImmutableStringsCache_inl_h
|