Bug 1865597 - Add error checking when initializing parallel marking and disable on...
[gecko.git] / js / src / vm / SharedImmutableStringsCache-inl.h
blob34690b202dc5d9488feb59fb5beb6e492ebb4130
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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/. */
7 #ifndef vm_SharedImmutableStringsCache_inl_h
8 #define vm_SharedImmutableStringsCache_inl_h
10 #include "vm/SharedImmutableStringsCache.h"
12 namespace js {
14 template <typename IntoOwnedChars>
15 [[nodiscard]] SharedImmutableString SharedImmutableStringsCache::getOrCreate(
16 const char* chars, size_t length, IntoOwnedChars intoOwnedChars) {
17 MOZ_ASSERT(inner_);
18 MOZ_ASSERT(chars);
19 Hasher::Lookup lookup(Hasher::hashLongString(chars, length), chars, length);
21 auto locked = inner_->lock();
22 auto entry = locked->set.lookupForAdd(lookup);
23 if (!entry) {
24 OwnedChars ownedChars(intoOwnedChars());
25 if (!ownedChars) {
26 return SharedImmutableString();
28 MOZ_ASSERT(ownedChars.get() == chars ||
29 memcmp(ownedChars.get(), chars, length) == 0);
30 auto box = StringBox::Create(std::move(ownedChars), length, inner_);
31 if (!box || !locked->set.add(entry, std::move(box))) {
32 return SharedImmutableString();
36 MOZ_ASSERT(entry && *entry);
37 return SharedImmutableString(entry->get());
40 template <typename IntoOwnedTwoByteChars>
41 [[nodiscard]] SharedImmutableTwoByteString
42 SharedImmutableStringsCache::getOrCreate(
43 const char16_t* chars, size_t length,
44 IntoOwnedTwoByteChars intoOwnedTwoByteChars) {
45 MOZ_ASSERT(inner_);
46 MOZ_ASSERT(chars);
47 auto hash = Hasher::hashLongString(reinterpret_cast<const char*>(chars),
48 length * sizeof(char16_t));
49 Hasher::Lookup lookup(hash, chars, length);
51 auto locked = inner_->lock();
52 auto entry = locked->set.lookupForAdd(lookup);
53 if (!entry) {
54 OwnedTwoByteChars ownedTwoByteChars(intoOwnedTwoByteChars());
55 if (!ownedTwoByteChars) {
56 return SharedImmutableTwoByteString();
58 MOZ_ASSERT(
59 ownedTwoByteChars.get() == chars ||
60 memcmp(ownedTwoByteChars.get(), chars, length * sizeof(char16_t)) == 0);
61 OwnedChars ownedChars(reinterpret_cast<char*>(ownedTwoByteChars.release()));
62 auto box = StringBox::Create(std::move(ownedChars),
63 length * sizeof(char16_t), inner_);
64 if (!box || !locked->set.add(entry, std::move(box))) {
65 return SharedImmutableTwoByteString();
69 MOZ_ASSERT(entry && *entry);
70 return SharedImmutableTwoByteString(entry->get());
73 } // namespace js
75 #endif // vm_SharedImmutableStringsCache_inl_h