1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/asmjscache/AsmJSCache.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,185 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_asmjscache_asmjscache_h 1.11 +#define mozilla_dom_asmjscache_asmjscache_h 1.12 + 1.13 +#include "ipc/IPCMessageUtils.h" 1.14 +#include "js/TypeDecls.h" 1.15 +#include "js/Vector.h" 1.16 +#include "jsapi.h" 1.17 + 1.18 +class nsIPrincipal; 1.19 + 1.20 +namespace mozilla { 1.21 +namespace dom { 1.22 + 1.23 +namespace quota { 1.24 +class Client; 1.25 +} 1.26 + 1.27 +namespace asmjscache { 1.28 + 1.29 +class PAsmJSCacheEntryChild; 1.30 +class PAsmJSCacheEntryParent; 1.31 + 1.32 +enum OpenMode 1.33 +{ 1.34 + eOpenForRead, 1.35 + eOpenForWrite, 1.36 + NUM_OPEN_MODES 1.37 +}; 1.38 + 1.39 +// Each origin stores a fixed size (kNumEntries) LRU cache of compiled asm.js 1.40 +// modules. Each compiled asm.js module is stored in a separate file with one 1.41 +// extra metadata file that stores the LRU cache and enough information for a 1.42 +// client to pick which cached module's file to open. 1.43 +struct Metadata 1.44 +{ 1.45 + static const unsigned kNumEntries = 16; 1.46 + static const unsigned kLastEntry = kNumEntries - 1; 1.47 + 1.48 + struct Entry 1.49 + { 1.50 + uint32_t mFastHash; 1.51 + uint32_t mNumChars; 1.52 + uint32_t mFullHash; 1.53 + unsigned mModuleIndex; 1.54 + 1.55 + void clear() { 1.56 + mFastHash = -1; 1.57 + mNumChars = -1; 1.58 + mFullHash = -1; 1.59 + } 1.60 + }; 1.61 + 1.62 + Entry mEntries[kNumEntries]; 1.63 +}; 1.64 + 1.65 +// Parameters specific to opening a cache entry for writing 1.66 +struct WriteParams 1.67 +{ 1.68 + int64_t mSize; 1.69 + int64_t mFastHash; 1.70 + int64_t mNumChars; 1.71 + int64_t mFullHash; 1.72 + bool mInstalled; 1.73 + 1.74 + WriteParams() 1.75 + : mSize(0), 1.76 + mFastHash(0), 1.77 + mNumChars(0), 1.78 + mFullHash(0), 1.79 + mInstalled(false) 1.80 + { } 1.81 +}; 1.82 + 1.83 +// Parameters specific to opening a cache entry for reading 1.84 +struct ReadParams 1.85 +{ 1.86 + const jschar* mBegin; 1.87 + const jschar* mLimit; 1.88 + 1.89 + ReadParams() 1.90 + : mBegin(nullptr), 1.91 + mLimit(nullptr) 1.92 + { } 1.93 +}; 1.94 + 1.95 +// Implementation of AsmJSCacheOps, installed for the main JSRuntime by 1.96 +// nsJSEnvironment.cpp and DOM Worker JSRuntimes in RuntimeService.cpp. 1.97 +// 1.98 +// The Open* functions cannot be called directly from AsmJSCacheOps: they take 1.99 +// an nsIPrincipal as the first argument instead of a Handle<JSObject*>. The 1.100 +// caller must map the object to an nsIPrincipal. 1.101 +// 1.102 +// These methods may be called off the main thread and guarantee not to 1.103 +// access the given aPrincipal except on the main thread. In exchange, the 1.104 +// caller must ensure the given principal is alive from when OpenEntryForX is 1.105 +// called to when CloseEntryForX returns. 1.106 + 1.107 +bool 1.108 +OpenEntryForRead(nsIPrincipal* aPrincipal, 1.109 + const jschar* aBegin, 1.110 + const jschar* aLimit, 1.111 + size_t* aSize, 1.112 + const uint8_t** aMemory, 1.113 + intptr_t *aHandle); 1.114 +void 1.115 +CloseEntryForRead(JS::Handle<JSObject*> aGlobal, 1.116 + size_t aSize, 1.117 + const uint8_t* aMemory, 1.118 + intptr_t aHandle); 1.119 +bool 1.120 +OpenEntryForWrite(nsIPrincipal* aPrincipal, 1.121 + bool aInstalled, 1.122 + const jschar* aBegin, 1.123 + const jschar* aEnd, 1.124 + size_t aSize, 1.125 + uint8_t** aMemory, 1.126 + intptr_t* aHandle); 1.127 +void 1.128 +CloseEntryForWrite(JS::Handle<JSObject*> aGlobal, 1.129 + size_t aSize, 1.130 + uint8_t* aMemory, 1.131 + intptr_t aHandle); 1.132 + 1.133 +bool 1.134 +GetBuildId(JS::BuildIdCharVector* aBuildId); 1.135 + 1.136 +// Called from QuotaManager.cpp: 1.137 + 1.138 +quota::Client* 1.139 +CreateClient(); 1.140 + 1.141 +// Called from ipc/ContentParent.cpp: 1.142 + 1.143 +PAsmJSCacheEntryParent* 1.144 +AllocEntryParent(OpenMode aOpenMode, WriteParams aWriteParams, 1.145 + nsIPrincipal* aPrincipal); 1.146 + 1.147 +void 1.148 +DeallocEntryParent(PAsmJSCacheEntryParent* aActor); 1.149 + 1.150 +// Called from ipc/ContentChild.cpp: 1.151 + 1.152 +void 1.153 +DeallocEntryChild(PAsmJSCacheEntryChild* aActor); 1.154 + 1.155 +} // namespace asmjscache 1.156 +} // namespace dom 1.157 +} // namespace mozilla 1.158 + 1.159 +namespace IPC { 1.160 + 1.161 +template <> 1.162 +struct ParamTraits<mozilla::dom::asmjscache::OpenMode> : 1.163 + public ContiguousEnumSerializer<mozilla::dom::asmjscache::OpenMode, 1.164 + mozilla::dom::asmjscache::eOpenForRead, 1.165 + mozilla::dom::asmjscache::NUM_OPEN_MODES> 1.166 +{ }; 1.167 + 1.168 +template <> 1.169 +struct ParamTraits<mozilla::dom::asmjscache::Metadata> 1.170 +{ 1.171 + typedef mozilla::dom::asmjscache::Metadata paramType; 1.172 + static void Write(Message* aMsg, const paramType& aParam); 1.173 + static bool Read(const Message* aMsg, void** aIter, paramType* aResult); 1.174 + static void Log(const paramType& aParam, std::wstring* aLog); 1.175 +}; 1.176 + 1.177 +template <> 1.178 +struct ParamTraits<mozilla::dom::asmjscache::WriteParams> 1.179 +{ 1.180 + typedef mozilla::dom::asmjscache::WriteParams paramType; 1.181 + static void Write(Message* aMsg, const paramType& aParam); 1.182 + static bool Read(const Message* aMsg, void** aIter, paramType* aResult); 1.183 + static void Log(const paramType& aParam, std::wstring* aLog); 1.184 +}; 1.185 + 1.186 +} // namespace IPC 1.187 + 1.188 +#endif // mozilla_dom_asmjscache_asmjscache_h