1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/wasm/wasm-import-wrapper-cache.h"
6
7 #include <vector>
8
9 #include "src/logging/counters.h"
10 #include "src/wasm/wasm-code-manager.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace wasm {
15
operator [](const CacheKey & key)16 WasmCode*& WasmImportWrapperCache::ModificationScope::operator[](
17 const CacheKey& key) {
18 return cache_->entry_map_[key];
19 }
20
operator [](const WasmImportWrapperCache::CacheKey & key)21 WasmCode*& WasmImportWrapperCache::operator[](
22 const WasmImportWrapperCache::CacheKey& key) {
23 return entry_map_[key];
24 }
25
Get(compiler::WasmImportCallKind kind,const FunctionSig * sig,int expected_arity,Suspend suspend) const26 WasmCode* WasmImportWrapperCache::Get(compiler::WasmImportCallKind kind,
27 const FunctionSig* sig,
28 int expected_arity,
29 Suspend suspend) const {
30 base::MutexGuard lock(&mutex_);
31
32 auto it = entry_map_.find({kind, sig, expected_arity, suspend});
33 DCHECK(it != entry_map_.end());
34 return it->second;
35 }
36
MaybeGet(compiler::WasmImportCallKind kind,const FunctionSig * sig,int expected_arity,Suspend suspend) const37 WasmCode* WasmImportWrapperCache::MaybeGet(compiler::WasmImportCallKind kind,
38 const FunctionSig* sig,
39 int expected_arity,
40 Suspend suspend) const {
41 base::MutexGuard lock(&mutex_);
42
43 auto it = entry_map_.find({kind, sig, expected_arity, suspend});
44 if (it == entry_map_.end()) return nullptr;
45 return it->second;
46 }
47
~WasmImportWrapperCache()48 WasmImportWrapperCache::~WasmImportWrapperCache() {
49 std::vector<WasmCode*> ptrs;
50 ptrs.reserve(entry_map_.size());
51 for (auto& e : entry_map_) {
52 if (e.second) {
53 ptrs.push_back(e.second);
54 }
55 }
56 WasmCode::DecrementRefCount(base::VectorOf(ptrs));
57 }
58
59 } // namespace wasm
60 } // namespace internal
61 } // namespace v8
62