signature-map.h 1.39 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_WASM_SIGNATURE_MAP_H_
#define V8_WASM_SIGNATURE_MAP_H_

8
#include <unordered_map>
9

10
#include "src/signature.h"
11
#include "src/wasm/value-type.h"
12 13 14

namespace v8 {
namespace internal {
Marja Hölttä's avatar
Marja Hölttä committed
15

16 17 18 19 20
namespace wasm {

// A signature map canonicalizes signatures into a range of indices so that
// two different {FunctionSig} instances with the same contents map to the
// same index.
21
class V8_EXPORT_PRIVATE SignatureMap {
22
 public:
23 24 25
  // Allow default construction and move construction (because we have vectors
  // of objects containing SignatureMaps), but disallow copy or assign. It's
  // too easy to get security bugs by accidentally updating a copy of the map.
26
  MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(SignatureMap);
27

28
  // Gets the index for a signature, assigning a new index if necessary.
29
  uint32_t FindOrInsert(const FunctionSig& sig);
30 31

  // Gets the index for a signature, returning {-1} if not found.
32
  int32_t Find(const FunctionSig& sig) const;
33

34 35 36
  // Disallows further insertions to this signature map.
  void Freeze() { frozen_ = true; }

37
 private:
38
  bool frozen_ = false;
39
  std::unordered_map<FunctionSig, uint32_t, base::hash<FunctionSig>> map_;
40 41 42 43 44 45 46
};

}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_SIGNATURE_MAP_H_