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 : #include "vm/SharedImmutableStringsCache-inl.h"
8 :
9 : #include "jsstr.h"
10 :
11 : namespace js {
12 :
13 1700 : SharedImmutableString::SharedImmutableString(
14 : ExclusiveData<SharedImmutableStringsCache::Inner>::Guard& locked,
15 1700 : SharedImmutableStringsCache::StringBox* box)
16 : : cache_(locked)
17 1700 : , box_(box)
18 : {
19 1700 : MOZ_ASSERT(box);
20 1700 : box->refcount++;
21 1700 : }
22 :
23 6800 : SharedImmutableString::SharedImmutableString(SharedImmutableString&& rhs)
24 6800 : : cache_(mozilla::Move(rhs.cache_))
25 6800 : , box_(rhs.box_)
26 : {
27 6800 : MOZ_ASSERT(this != &rhs, "self move not allowed");
28 6800 : MOZ_ASSERT(rhs.box_);
29 6800 : MOZ_ASSERT(rhs.box_->refcount > 0);
30 :
31 6800 : rhs.box_ = nullptr;
32 6800 : }
33 :
34 : SharedImmutableString&
35 0 : SharedImmutableString::operator=(SharedImmutableString&& rhs) {
36 0 : this->~SharedImmutableString();
37 0 : new (this) SharedImmutableString(mozilla::Move(rhs));
38 0 : return *this;
39 : }
40 :
41 0 : SharedImmutableTwoByteString::SharedImmutableTwoByteString(SharedImmutableString&& string)
42 0 : : string_(mozilla::Move(string))
43 0 : { }
44 :
45 1700 : SharedImmutableTwoByteString::SharedImmutableTwoByteString(
46 : ExclusiveData<SharedImmutableStringsCache::Inner>::Guard& locked,
47 1700 : SharedImmutableStringsCache::StringBox* box)
48 1700 : : string_(locked, box)
49 : {
50 1700 : MOZ_ASSERT(box->length() % sizeof(char16_t) == 0);
51 1700 : }
52 :
53 6800 : SharedImmutableTwoByteString::SharedImmutableTwoByteString(SharedImmutableTwoByteString&& rhs)
54 6800 : : string_(mozilla::Move(rhs.string_))
55 : {
56 6800 : MOZ_ASSERT(this != &rhs, "self move not allowed");
57 6800 : }
58 :
59 : SharedImmutableTwoByteString&
60 0 : SharedImmutableTwoByteString::operator=(SharedImmutableTwoByteString&& rhs)
61 : {
62 0 : this->~SharedImmutableTwoByteString();
63 0 : new (this) SharedImmutableTwoByteString(mozilla::Move(rhs));
64 0 : return *this;
65 : }
66 :
67 13600 : SharedImmutableString::~SharedImmutableString() {
68 6800 : if (!box_)
69 6800 : return;
70 :
71 0 : auto locked = cache_.inner_->lock();
72 :
73 0 : MOZ_ASSERT(box_->refcount > 0);
74 :
75 0 : box_->refcount--;
76 0 : if (box_->refcount == 0)
77 0 : box_->chars_.reset(nullptr);
78 6800 : }
79 :
80 : SharedImmutableString
81 0 : SharedImmutableString::clone() const
82 : {
83 0 : auto locked = cache_.inner_->lock();
84 0 : MOZ_ASSERT(box_);
85 0 : MOZ_ASSERT(box_->refcount > 0);
86 0 : return SharedImmutableString(locked, box_);
87 : }
88 :
89 : SharedImmutableTwoByteString
90 0 : SharedImmutableTwoByteString::clone() const
91 : {
92 0 : return SharedImmutableTwoByteString(string_.clone());
93 : }
94 :
95 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableString>
96 0 : SharedImmutableStringsCache::getOrCreate(OwnedChars&& chars, size_t length)
97 : {
98 0 : OwnedChars owned(mozilla::Move(chars));
99 0 : MOZ_ASSERT(owned);
100 0 : return getOrCreate(owned.get(), length, [&]() { return mozilla::Move(owned); });
101 : }
102 :
103 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableString>
104 0 : SharedImmutableStringsCache::getOrCreate(const char* chars, size_t length)
105 : {
106 0 : return getOrCreate(chars, length, [&]() { return DuplicateString(chars, length); });
107 : }
108 :
109 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableTwoByteString>
110 1485 : SharedImmutableStringsCache::getOrCreate(OwnedTwoByteChars&& chars, size_t length)
111 : {
112 2969 : OwnedTwoByteChars owned(mozilla::Move(chars));
113 1485 : MOZ_ASSERT(owned);
114 4084 : return getOrCreate(owned.get(), length, [&]() { return mozilla::Move(owned); });
115 : }
116 :
117 : MOZ_MUST_USE mozilla::Maybe<SharedImmutableTwoByteString>
118 0 : SharedImmutableStringsCache::getOrCreate(const char16_t* chars, size_t length)
119 : {
120 0 : return getOrCreate(chars, length, [&]() { return DuplicateString(chars, length); });
121 : }
122 :
123 : } // namespace js
|