Bumping manifests a=b2g-bump
[gecko.git] / dom / asmjscache / AsmJSCache.h
blobdd8fdc50993fb7d2570d83b558a75afcf4ff498c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_asmjscache_asmjscache_h
8 #define mozilla_dom_asmjscache_asmjscache_h
10 #include "ipc/IPCMessageUtils.h"
11 #include "js/TypeDecls.h"
12 #include "js/Vector.h"
13 #include "jsapi.h"
15 class nsIPrincipal;
17 namespace mozilla {
18 namespace dom {
20 namespace quota {
21 class Client;
24 namespace asmjscache {
26 class PAsmJSCacheEntryChild;
27 class PAsmJSCacheEntryParent;
29 enum OpenMode
31 eOpenForRead,
32 eOpenForWrite,
33 NUM_OPEN_MODES
36 // Each origin stores a fixed size (kNumEntries) LRU cache of compiled asm.js
37 // modules. Each compiled asm.js module is stored in a separate file with one
38 // extra metadata file that stores the LRU cache and enough information for a
39 // client to pick which cached module's file to open.
40 struct Metadata
42 static const unsigned kNumEntries = 16;
43 static const unsigned kLastEntry = kNumEntries - 1;
45 struct Entry
47 uint32_t mFastHash;
48 uint32_t mNumChars;
49 uint32_t mFullHash;
50 unsigned mModuleIndex;
52 void clear() {
53 mFastHash = -1;
54 mNumChars = -1;
55 mFullHash = -1;
59 Entry mEntries[kNumEntries];
62 // Parameters specific to opening a cache entry for writing
63 struct WriteParams
65 int64_t mSize;
66 int64_t mFastHash;
67 int64_t mNumChars;
68 int64_t mFullHash;
69 bool mInstalled;
71 WriteParams()
72 : mSize(0),
73 mFastHash(0),
74 mNumChars(0),
75 mFullHash(0),
76 mInstalled(false)
77 { }
80 // Parameters specific to opening a cache entry for reading
81 struct ReadParams
83 const char16_t* mBegin;
84 const char16_t* mLimit;
86 ReadParams()
87 : mBegin(nullptr),
88 mLimit(nullptr)
89 { }
92 // Implementation of AsmJSCacheOps, installed for the main JSRuntime by
93 // nsJSEnvironment.cpp and DOM Worker JSRuntimes in RuntimeService.cpp.
95 // The Open* functions cannot be called directly from AsmJSCacheOps: they take
96 // an nsIPrincipal as the first argument instead of a Handle<JSObject*>. The
97 // caller must map the object to an nsIPrincipal.
99 // These methods may be called off the main thread and guarantee not to
100 // access the given aPrincipal except on the main thread. In exchange, the
101 // caller must ensure the given principal is alive from when OpenEntryForX is
102 // called to when CloseEntryForX returns.
104 bool
105 OpenEntryForRead(nsIPrincipal* aPrincipal,
106 const char16_t* aBegin,
107 const char16_t* aLimit,
108 size_t* aSize,
109 const uint8_t** aMemory,
110 intptr_t *aHandle);
111 void
112 CloseEntryForRead(size_t aSize,
113 const uint8_t* aMemory,
114 intptr_t aHandle);
115 JS::AsmJSCacheResult
116 OpenEntryForWrite(nsIPrincipal* aPrincipal,
117 bool aInstalled,
118 const char16_t* aBegin,
119 const char16_t* aEnd,
120 size_t aSize,
121 uint8_t** aMemory,
122 intptr_t* aHandle);
123 void
124 CloseEntryForWrite(size_t aSize,
125 uint8_t* aMemory,
126 intptr_t aHandle);
128 bool
129 GetBuildId(JS::BuildIdCharVector* aBuildId);
131 // Called from QuotaManager.cpp:
133 quota::Client*
134 CreateClient();
136 // Called from ipc/ContentParent.cpp:
138 PAsmJSCacheEntryParent*
139 AllocEntryParent(OpenMode aOpenMode, WriteParams aWriteParams,
140 nsIPrincipal* aPrincipal);
142 void
143 DeallocEntryParent(PAsmJSCacheEntryParent* aActor);
145 // Called from ipc/ContentChild.cpp:
147 void
148 DeallocEntryChild(PAsmJSCacheEntryChild* aActor);
150 } // namespace asmjscache
151 } // namespace dom
152 } // namespace mozilla
154 namespace IPC {
156 template <>
157 struct ParamTraits<mozilla::dom::asmjscache::OpenMode> :
158 public ContiguousEnumSerializer<mozilla::dom::asmjscache::OpenMode,
159 mozilla::dom::asmjscache::eOpenForRead,
160 mozilla::dom::asmjscache::NUM_OPEN_MODES>
161 { };
163 template <>
164 struct ParamTraits<mozilla::dom::asmjscache::Metadata>
166 typedef mozilla::dom::asmjscache::Metadata paramType;
167 static void Write(Message* aMsg, const paramType& aParam);
168 static bool Read(const Message* aMsg, void** aIter, paramType* aResult);
169 static void Log(const paramType& aParam, std::wstring* aLog);
172 template <>
173 struct ParamTraits<mozilla::dom::asmjscache::WriteParams>
175 typedef mozilla::dom::asmjscache::WriteParams paramType;
176 static void Write(Message* aMsg, const paramType& aParam);
177 static bool Read(const Message* aMsg, void** aIter, paramType* aResult);
178 static void Log(const paramType& aParam, std::wstring* aLog);
181 template <>
182 struct ParamTraits<JS::AsmJSCacheResult> :
183 public ContiguousEnumSerializer<JS::AsmJSCacheResult,
184 JS::AsmJSCache_MIN,
185 JS::AsmJSCache_LIMIT>
186 { };
188 } // namespace IPC
190 #endif // mozilla_dom_asmjscache_asmjscache_h