Basic JIT support for Records
[hiphop-php.git] / hphp / runtime / vm / litstr-table.h
blob409b5c9814f3745b11ebf7fd13436c96d303d309
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_LITSTR_TABLE_H_
18 #define incl_HPHP_LITSTR_TABLE_H_
20 #include "hphp/runtime/base/string-hash-map.h"
21 #include "hphp/runtime/vm/named-entity.h"
22 #include "hphp/runtime/vm/named-entity-pair-table.h"
23 #include "hphp/util/alloc.h"
24 #include "hphp/util/functional.h"
25 #include "hphp/util/hash-map.h"
26 #include "hphp/util/mutex.h"
28 #include <tbb/concurrent_hash_map.h>
30 namespace HPHP {
31 ///////////////////////////////////////////////////////////////////////////////
33 struct StringData;
35 ///////////////////////////////////////////////////////////////////////////////
38 * Global litstr Id's are all above this mark.
40 constexpr int kGlobalLitstrOffset = 0x40000000;
42 ///////////////////////////////////////////////////////////////////////////////
45 * Singleton global table of literal strings and NamedEntitys.
47 * This can only be safely used when the repo is built in WholeProgram mode and
48 * run in RepoAuthoritative mode.
50 struct LitstrTable {
52 /////////////////////////////////////////////////////////////////////////////
53 // Singleton init and get. [static]
56 * Create the singleton LitstrTable.
58 * Must not be called in concurrent contexts---the table pointer is not
59 * atomic, and init() does not check if a table already exists.
61 static void init();
64 * Destroy the singleton LitstrTable.
66 * Must not be called in concurrent contexts---the table pointer is not
67 * atomic.
69 static void fini();
72 * Get the singleton LitstrTable.
74 static LitstrTable& get();
77 /////////////////////////////////////////////////////////////////////////////
78 // Main API.
81 * Size of the table.
83 size_t numLitstrs() const;
86 * Dispatch to corresponding NamedEntityPairTable methods, sans `Id' suffix.
88 bool contains(Id id) const;
89 StringData* lookupLitstrId(Id id) const;
90 const NamedEntity* lookupNamedEntityId(Id id) const;
91 NamedEntityPair lookupNamedEntityPairId(Id id) const;
93 static bool canRead() {
94 return !s_litstrTable || s_litstrTable->m_safeToRead;
98 * Set up the named info table. Not thread-safe.
100 void setNamedEntityPairTable(NamedEntityPairTable&& namedInfo);
103 * Add an entry for `litstr' to the table.
105 * The "merge" terminology is inherited from Unit.
107 Id mergeLitstr(const StringData* litstr);
110 * Call onItem() for each item in the table.
112 void forEachLitstr(
113 std::function<void (int i, const StringData* name)> onItem);
116 /////////////////////////////////////////////////////////////////////////////
117 // Concurrency control.
120 * LitstrTable reader/writer state.
122 * Setting the reader state will update m_namedInfo from m_litstr2id.
124 void setReading();
125 void setWriting();
128 /////////////////////////////////////////////////////////////////////////////
129 // Private constructor.
131 private:
132 LitstrTable() {}
135 /////////////////////////////////////////////////////////////////////////////
136 // Data members.
138 private:
139 static LitstrTable* s_litstrTable;
141 using LitstrMap = tbb::concurrent_hash_map<
142 const StringData*,
144 StringDataHashCompare,
145 VMAllocator<char>
148 NamedEntityPairTable m_namedInfo;
149 LitstrMap m_litstr2id;
151 std::atomic<Id> m_nextId{1};
152 std::atomic<bool> m_safeToRead{true};
155 ///////////////////////////////////////////////////////////////////////////////
156 // ID helpers.
159 * Functions for differentiating global litstrId's from unit-local Id's.
161 bool isGlobalLitstrId(Id id);
162 Id encodeGlobalLitstrId(Id id);
163 Id decodeGlobalLitstrId(Id id);
165 ///////////////////////////////////////////////////////////////////////////////
168 #define incl_HPHP_LITSTR_TABLE_INL_H_
169 #include "hphp/runtime/vm/litstr-table-inl.h"
170 #undef incl_HPHP_LITSTR_TABLE_INL_H_
172 #endif // incl_HPHP_LITSTR_TABLE_H_