ProfilerOrigin.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef ProfilerOrigin_h
  26. #define ProfilerOrigin_h
  27. #include "CodeBlockHash.h"
  28. #include "JSCJSValue.h"
  29. #include <wtf/HashMap.h>
  30. #include <wtf/PrintStream.h>
  31. namespace JSC {
  32. class CodeBlock;
  33. namespace Profiler {
  34. class Bytecodes;
  35. class Database;
  36. class Origin {
  37. public:
  38. Origin()
  39. : m_bytecodeIndex(std::numeric_limits<unsigned>::max())
  40. {
  41. }
  42. Origin(WTF::HashTableDeletedValueType)
  43. : m_bytecodeIndex(std::numeric_limits<unsigned>::max() - 1)
  44. {
  45. }
  46. Origin(Bytecodes* bytecodes, unsigned bytecodeIndex)
  47. : m_bytecodes(bytecodes)
  48. , m_bytecodeIndex(bytecodeIndex)
  49. {
  50. ASSERT(m_bytecodeIndex < std::numeric_limits<unsigned>::max() - 1);
  51. }
  52. Origin(Database&, CodeBlock*, unsigned bytecodeIndex);
  53. bool operator!() const { return m_bytecodeIndex == std::numeric_limits<unsigned>::max(); }
  54. Bytecodes* bytecodes() const { return m_bytecodes; }
  55. unsigned bytecodeIndex() const { return m_bytecodeIndex; }
  56. bool operator==(const Origin&) const;
  57. bool operator!=(const Origin& other) const { return !(*this == other); }
  58. unsigned hash() const;
  59. bool isHashTableDeletedValue() const;
  60. void dump(PrintStream&) const;
  61. JSValue toJS(ExecState*) const;
  62. private:
  63. Bytecodes* m_bytecodes;
  64. unsigned m_bytecodeIndex;
  65. };
  66. inline bool Origin::operator==(const Origin& other) const
  67. {
  68. return m_bytecodes == other.m_bytecodes
  69. && m_bytecodeIndex == other.m_bytecodeIndex;
  70. }
  71. inline unsigned Origin::hash() const
  72. {
  73. return WTF::PtrHash<Bytecodes*>::hash(m_bytecodes) + m_bytecodeIndex;
  74. }
  75. inline bool Origin::isHashTableDeletedValue() const
  76. {
  77. return m_bytecodeIndex == std::numeric_limits<unsigned>::max();
  78. }
  79. struct OriginHash {
  80. static unsigned hash(const Origin& key) { return key.hash(); }
  81. static bool equal(const Origin& a, const Origin& b) { return a == b; }
  82. static const bool safeToCompareToEmptyOrDeleted = true;
  83. };
  84. } } // namespace JSC::Profiler
  85. namespace WTF {
  86. template<typename T> struct DefaultHash;
  87. template<> struct DefaultHash<JSC::Profiler::Origin> {
  88. typedef JSC::Profiler::OriginHash Hash;
  89. };
  90. template<typename T> struct HashTraits;
  91. template<> struct HashTraits<JSC::Profiler::Origin> : SimpleClassHashTraits<JSC::Profiler::Origin> { };
  92. } // namespace WTF
  93. #endif // ProfilerOrigin_h