11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/wasm/wasm-import-wrapper-cache.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <vector>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
101cb0ef41Sopenharmony_ci#include "src/wasm/wasm-code-manager.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace internal {
141cb0ef41Sopenharmony_cinamespace wasm {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciWasmCode*& WasmImportWrapperCache::ModificationScope::operator[](
171cb0ef41Sopenharmony_ci    const CacheKey& key) {
181cb0ef41Sopenharmony_ci  return cache_->entry_map_[key];
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciWasmCode*& WasmImportWrapperCache::operator[](
221cb0ef41Sopenharmony_ci    const WasmImportWrapperCache::CacheKey& key) {
231cb0ef41Sopenharmony_ci  return entry_map_[key];
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciWasmCode* WasmImportWrapperCache::Get(compiler::WasmImportCallKind kind,
271cb0ef41Sopenharmony_ci                                      const FunctionSig* sig,
281cb0ef41Sopenharmony_ci                                      int expected_arity,
291cb0ef41Sopenharmony_ci                                      Suspend suspend) const {
301cb0ef41Sopenharmony_ci  base::MutexGuard lock(&mutex_);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  auto it = entry_map_.find({kind, sig, expected_arity, suspend});
331cb0ef41Sopenharmony_ci  DCHECK(it != entry_map_.end());
341cb0ef41Sopenharmony_ci  return it->second;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciWasmCode* WasmImportWrapperCache::MaybeGet(compiler::WasmImportCallKind kind,
381cb0ef41Sopenharmony_ci                                           const FunctionSig* sig,
391cb0ef41Sopenharmony_ci                                           int expected_arity,
401cb0ef41Sopenharmony_ci                                           Suspend suspend) const {
411cb0ef41Sopenharmony_ci  base::MutexGuard lock(&mutex_);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  auto it = entry_map_.find({kind, sig, expected_arity, suspend});
441cb0ef41Sopenharmony_ci  if (it == entry_map_.end()) return nullptr;
451cb0ef41Sopenharmony_ci  return it->second;
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciWasmImportWrapperCache::~WasmImportWrapperCache() {
491cb0ef41Sopenharmony_ci  std::vector<WasmCode*> ptrs;
501cb0ef41Sopenharmony_ci  ptrs.reserve(entry_map_.size());
511cb0ef41Sopenharmony_ci  for (auto& e : entry_map_) {
521cb0ef41Sopenharmony_ci    if (e.second) {
531cb0ef41Sopenharmony_ci      ptrs.push_back(e.second);
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci  WasmCode::DecrementRefCount(base::VectorOf(ptrs));
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci}  // namespace wasm
601cb0ef41Sopenharmony_ci}  // namespace internal
611cb0ef41Sopenharmony_ci}  // namespace v8
62