gecko-dev/dom/ipc/StringTable.h
Kris Maglione 82bc4d713f Bug 1471025: Part 2 - Add a helper class creating and accessing shared preference map snapshots. r=njn,erahm
This is based on the SharedStringMap that's currently used for shared memory
string bundles.

When the parent process is ready to launch its first content process, it
creates a snapshot of the current state of the preference database, maps that
as read-only, and shares it with each content process. Look-ups in the
snapshotted map are done entirely using data in the shared memory region. It
doesn't require any additional per-process state data.

MozReview-Commit-ID: BdTUhak7dmS

--HG--
extra : intermediate-source : 434106f1b75e3ba900912f261bd22a1b7f5c931d
extra : absorb_source : 647ad37590448ad3c1eb8eb512bf671f262fa96e
extra : source : 68bb03c63b3cee1d47cbddfd3abf919f5783c04b
extra : histedit_source : 2228a9f8395929f5072a3c5ebda6ae3221e4a62d
2018-07-01 18:28:31 -07:00

128 lines
3.2 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef dom_ipc_StringTable_h
#define dom_ipc_StringTable_h
#include "mozilla/RangedPtr.h"
#include "nsDataHashtable.h"
/**
* This file contains helper classes for creating and accessing compact string
* tables, which can be used as the building blocks of shared memory databases.
* Each string table a de-duplicated set of strings which can be referenced
* using their character offsets within a data block and their lengths. The
* string tables, once created, cannot be modified, and are primarily useful in
* read-only shared memory or memory mapped files.
*/
namespace mozilla {
namespace dom {
namespace ipc {
/**
* Contains the character offset and character length of an entry in a string
* table. This may be used for either 8-bit or 16-bit strings, and is required
* to retrieve an entry from a string table.
*/
struct StringTableEntry
{
uint32_t mOffset;
uint32_t mLength;
// Ignore mLength. It must be the same for any two strings with the same
// offset.
uint32_t Hash() const { return mOffset; }
bool operator==(const StringTableEntry& aOther) const
{
return mOffset == aOther.mOffset;
}
};
template <typename StringType>
class StringTable
{
using ElemType = typename StringType::char_type;
public:
MOZ_IMPLICIT StringTable(const RangedPtr<uint8_t>& aBuffer)
: mBuffer(aBuffer.ReinterpretCast<ElemType>())
{
MOZ_ASSERT(uintptr_t(aBuffer.get()) % alignof(ElemType) == 0,
"Got misalinged buffer");
}
StringType Get(const StringTableEntry& aEntry) const
{
StringType res;
res.AssignLiteral(GetBare(aEntry), aEntry.mLength);
return res;
}
const ElemType* GetBare(const StringTableEntry& aEntry) const
{
return &mBuffer[aEntry.mOffset];
}
private:
RangedPtr<ElemType> mBuffer;
};
template <typename KeyType, typename StringType>
class StringTableBuilder
{
public:
using ElemType = typename StringType::char_type;
StringTableEntry Add(const StringType& aKey)
{
const auto& entry = mEntries.LookupForAdd(aKey).OrInsert([&] () {
Entry newEntry { mSize, aKey };
mSize += aKey.Length() + 1;
return newEntry;
});
return { entry.mOffset, aKey.Length() };
}
void Write(const RangedPtr<uint8_t>& aBuffer)
{
auto buffer = aBuffer.ReinterpretCast<ElemType>();
for (auto iter = mEntries.Iter(); !iter.Done(); iter.Next()) {
auto& entry = iter.Data();
memcpy(&buffer[entry.mOffset], entry.mValue.BeginReading(),
sizeof(ElemType) * (entry.mValue.Length() + 1));
}
}
uint32_t Count() const { return mEntries.Count(); }
uint32_t Size() const { return mSize * sizeof(ElemType); }
void Clear() { mEntries.Clear(); }
static constexpr size_t Alignment() { return alignof(ElemType); }
private:
struct Entry
{
uint32_t mOffset;
StringType mValue;
};
nsDataHashtable<KeyType, Entry> mEntries;
uint32_t mSize = 0;
};
} // namespace ipc
} // namespace dom
} // namespace mozilla
#endif