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 #include "vm/SharedImmutableStringsCache-inl.h"
10 #include "vm/MutexIDs.h" // js::mutexid
11 #include "vm/Runtime.h" // JSRuntime
16 SharedImmutableStringsCache
SharedImmutableStringsCache::singleton_
;
18 SharedImmutableString::SharedImmutableString(
19 SharedImmutableStringsCache::StringBox
* box
)
25 SharedImmutableString::SharedImmutableString(SharedImmutableString
&& rhs
)
27 MOZ_ASSERT(this != &rhs
, "self move not allowed");
29 MOZ_ASSERT_IF(rhs
.box_
, rhs
.box_
->refcount
> 0);
34 SharedImmutableString
& SharedImmutableString::operator=(
35 SharedImmutableString
&& rhs
) {
36 this->~SharedImmutableString();
37 new (this) SharedImmutableString(std::move(rhs
));
41 SharedImmutableTwoByteString::SharedImmutableTwoByteString(
42 SharedImmutableString
&& string
)
43 : string_(std::move(string
)) {}
45 SharedImmutableTwoByteString::SharedImmutableTwoByteString(
46 SharedImmutableStringsCache::StringBox
* box
)
48 MOZ_ASSERT(box
->length() % sizeof(char16_t
) == 0);
51 SharedImmutableTwoByteString::SharedImmutableTwoByteString(
52 SharedImmutableTwoByteString
&& rhs
)
53 : string_(std::move(rhs
.string_
)) {
54 MOZ_ASSERT(this != &rhs
, "self move not allowed");
57 SharedImmutableTwoByteString
& SharedImmutableTwoByteString::operator=(
58 SharedImmutableTwoByteString
&& rhs
) {
59 this->~SharedImmutableTwoByteString();
60 new (this) SharedImmutableTwoByteString(std::move(rhs
));
64 SharedImmutableString::~SharedImmutableString() {
69 auto locked
= box_
->cache_
->lock();
71 MOZ_ASSERT(box_
->refcount
> 0);
74 if (box_
->refcount
== 0) {
75 box_
->chars_
.reset(nullptr);
79 SharedImmutableString
SharedImmutableString::clone() const {
80 auto locked
= box_
->cache_
->lock();
82 MOZ_ASSERT(box_
->refcount
> 0);
83 return SharedImmutableString(box_
);
86 SharedImmutableTwoByteString
SharedImmutableTwoByteString::clone() const {
87 return SharedImmutableTwoByteString(string_
.clone());
90 [[nodiscard
]] SharedImmutableString
SharedImmutableStringsCache::getOrCreate(
91 OwnedChars
&& chars
, size_t length
) {
92 OwnedChars
owned(std::move(chars
));
94 return getOrCreate(owned
.get(), length
, [&]() { return std::move(owned
); });
97 [[nodiscard
]] SharedImmutableString
SharedImmutableStringsCache::getOrCreate(
98 const char* chars
, size_t length
) {
99 return getOrCreate(chars
, length
,
100 [&]() { return DuplicateString(chars
, length
); });
103 bool SharedImmutableStringsCache::init() {
107 js_new
<ExclusiveData
<Inner
>>(mutexid::SharedImmutableStringsCache
);
112 auto locked
= inner
->lock();
113 inner_
= locked
.parent();
118 void SharedImmutableStringsCache::free() {
125 bool SharedImmutableStringsCache::initSingleton() { return singleton_
.init(); }
127 void SharedImmutableStringsCache::freeSingleton() {
128 if (!JSRuntime::hasLiveRuntimes()) {
133 [[nodiscard
]] SharedImmutableTwoByteString
134 SharedImmutableStringsCache::getOrCreate(OwnedTwoByteChars
&& chars
,
136 OwnedTwoByteChars
owned(std::move(chars
));
138 return getOrCreate(owned
.get(), length
, [&]() { return std::move(owned
); });
141 [[nodiscard
]] SharedImmutableTwoByteString
142 SharedImmutableStringsCache::getOrCreate(const char16_t
* chars
, size_t length
) {
143 return getOrCreate(chars
, length
,
144 [&]() { return DuplicateString(chars
, length
); });