Bug 1795172 [wpt PR 36447] - Disallow culled inlines in repeated content., a=testonly
[gecko.git] / dom / ipc / SharedStringMap.cpp
blob9525c9f285cbb8e4ae4d3e9db983d28136a417bf
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 "SharedStringMap.h"
9 #include "MemMapSnapshot.h"
10 #include "ScriptPreloader-inl.h"
12 #include "mozilla/BinarySearch.h"
13 #include "mozilla/ipc/FileDescriptor.h"
15 using namespace mozilla::loader;
17 namespace mozilla {
19 using namespace ipc;
21 namespace dom::ipc {
23 static constexpr uint32_t kSharedStringMapMagic = 0x9e3779b9;
25 static inline size_t GetAlignmentOffset(size_t aOffset, size_t aAlign) {
26 auto mod = aOffset % aAlign;
27 return mod ? aAlign - mod : 0;
30 SharedStringMap::SharedStringMap(const FileDescriptor& aMapFile,
31 size_t aMapSize) {
32 auto result = mMap.initWithHandle(aMapFile, aMapSize);
33 MOZ_RELEASE_ASSERT(result.isOk());
34 MOZ_RELEASE_ASSERT(GetHeader().mMagic == kSharedStringMapMagic);
35 // We return literal nsStrings and nsCStrings pointing to the mapped data,
36 // which means that we may still have references to the mapped data even
37 // after this instance is destroyed. That means that we need to keep the
38 // mapping alive until process shutdown, in order to be safe.
39 mMap.setPersistent();
42 SharedStringMap::SharedStringMap(SharedStringMapBuilder&& aBuilder) {
43 auto result = aBuilder.Finalize(mMap);
44 MOZ_RELEASE_ASSERT(result.isOk());
45 MOZ_RELEASE_ASSERT(GetHeader().mMagic == kSharedStringMapMagic);
46 mMap.setPersistent();
49 mozilla::ipc::FileDescriptor SharedStringMap::CloneFileDescriptor() const {
50 return mMap.cloneHandle();
53 bool SharedStringMap::Has(const nsCString& aKey) {
54 size_t index;
55 return Find(aKey, &index);
58 bool SharedStringMap::Get(const nsCString& aKey, nsAString& aValue) {
59 const auto& entries = Entries();
61 size_t index;
62 if (!Find(aKey, &index)) {
63 return false;
66 aValue.Assign(ValueTable().Get(entries[index].mValue));
67 return true;
70 bool SharedStringMap::Find(const nsCString& aKey, size_t* aIndex) {
71 const auto& keys = KeyTable();
73 return BinarySearchIf(
74 Entries(), 0, EntryCount(),
75 [&](const Entry& aEntry) { return Compare(aKey, keys.Get(aEntry.mKey)); },
76 aIndex);
79 void SharedStringMapBuilder::Add(const nsCString& aKey,
80 const nsString& aValue) {
81 mEntries.InsertOrUpdate(aKey,
82 Entry{mKeyTable.Add(aKey), mValueTable.Add(aValue)});
85 Result<Ok, nsresult> SharedStringMapBuilder::Finalize(
86 loader::AutoMemMap& aMap) {
87 using Header = SharedStringMap::Header;
89 MOZ_ASSERT(mEntries.Count() == mKeyTable.Count());
91 auto keys = ToTArray<nsTArray<nsCString>>(mEntries.Keys());
92 keys.Sort();
94 Header header = {kSharedStringMapMagic, uint32_t(keys.Length())};
96 size_t offset = sizeof(header);
97 offset += GetAlignmentOffset(offset, alignof(Header));
99 offset += keys.Length() * sizeof(SharedStringMap::Entry);
101 header.mKeyStringsOffset = offset;
102 header.mKeyStringsSize = mKeyTable.Size();
104 offset += header.mKeyStringsSize;
105 offset +=
106 GetAlignmentOffset(offset, alignof(decltype(mValueTable)::ElemType));
108 header.mValueStringsOffset = offset;
109 header.mValueStringsSize = mValueTable.Size();
111 offset += header.mValueStringsSize;
113 MemMapSnapshot mem;
114 MOZ_TRY(mem.Init(offset));
116 auto headerPtr = mem.Get<Header>();
117 headerPtr[0] = header;
119 auto* entry = reinterpret_cast<Entry*>(&headerPtr[1]);
120 for (auto& key : keys) {
121 *entry++ = mEntries.Get(key);
124 auto ptr = mem.Get<uint8_t>();
126 mKeyTable.Write({&ptr[header.mKeyStringsOffset], header.mKeyStringsSize});
128 mValueTable.Write(
129 {&ptr[header.mValueStringsOffset], header.mValueStringsSize});
131 mKeyTable.Clear();
132 mValueTable.Clear();
133 mEntries.Clear();
135 return mem.Finalize(aMap);
138 } // namespace dom::ipc
139 } // namespace mozilla