summaryrefslogtreecommitdiffstats
path: root/dom/ipc/StringTable.h
blob: 5de3cb92261c2c1d7c96a41df39754635ee9ec14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsTHashMap.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::dom::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) {
    return mEntries.WithEntryHandle(aKey,
                                    [&](auto&& entry) -> StringTableEntry {
                                      auto length = uint32_t(aKey.Length());
                                      entry.OrInsertWith([&]() {
                                        Entry newEntry{mSize, aKey};
                                        mSize += length + 1;

                                        return newEntry;
                                      });

                                      return {entry->mOffset, length};
                                    });
  }

  void Write(const RangedPtr<uint8_t>& aBuffer) {
    auto buffer = aBuffer.ReinterpretCast<ElemType>();

    for (const auto& entry : mEntries.Values()) {
      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;
  };

  nsTHashMap<KeyType, Entry> mEntries;
  uint32_t mSize = 0;
};

}  // namespace mozilla::dom::ipc

#endif