IndexingHeader.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 IndexingHeader_h
  26. #define IndexingHeader_h
  27. #include "PropertyStorage.h"
  28. #include <wtf/Platform.h>
  29. namespace JSC {
  30. class Butterfly;
  31. class LLIntOffsetsExtractor;
  32. class Structure;
  33. struct ArrayStorage;
  34. class IndexingHeader {
  35. public:
  36. // Define the maximum storage vector length to be 2^32 / sizeof(JSValue) / 2 to ensure that
  37. // there is no risk of overflow.
  38. enum { maximumLength = 0x10000000 };
  39. static ptrdiff_t offsetOfIndexingHeader() { return -static_cast<ptrdiff_t>(sizeof(IndexingHeader)); }
  40. static ptrdiff_t offsetOfPublicLength() { return OBJECT_OFFSETOF(IndexingHeader, m_publicLength); }
  41. static ptrdiff_t offsetOfVectorLength() { return OBJECT_OFFSETOF(IndexingHeader, m_vectorLength); }
  42. IndexingHeader()
  43. : m_publicLength(0)
  44. , m_vectorLength(0)
  45. {
  46. }
  47. uint32_t vectorLength() const { return m_vectorLength; }
  48. void setVectorLength(uint32_t length)
  49. {
  50. RELEASE_ASSERT(length <= maximumLength);
  51. m_vectorLength = length;
  52. }
  53. uint32_t publicLength() { return m_publicLength; }
  54. void setPublicLength(uint32_t auxWord) { m_publicLength = auxWord; }
  55. static IndexingHeader* from(Butterfly* butterfly)
  56. {
  57. return reinterpret_cast<IndexingHeader*>(butterfly) - 1;
  58. }
  59. static const IndexingHeader* from(const Butterfly* butterfly)
  60. {
  61. return reinterpret_cast<const IndexingHeader*>(butterfly) - 1;
  62. }
  63. static IndexingHeader* from(ArrayStorage* arrayStorage)
  64. {
  65. return reinterpret_cast<IndexingHeader*>(arrayStorage) - 1;
  66. }
  67. static IndexingHeader* fromEndOf(PropertyStorage propertyStorage)
  68. {
  69. return reinterpret_cast<IndexingHeader*>(propertyStorage);
  70. }
  71. PropertyStorage propertyStorage()
  72. {
  73. return reinterpret_cast_ptr<PropertyStorage>(this);
  74. }
  75. ConstPropertyStorage propertyStorage() const
  76. {
  77. return reinterpret_cast_ptr<ConstPropertyStorage>(this);
  78. }
  79. ArrayStorage* arrayStorage()
  80. {
  81. return reinterpret_cast<ArrayStorage*>(this + 1);
  82. }
  83. Butterfly* butterfly()
  84. {
  85. return reinterpret_cast<Butterfly*>(this + 1);
  86. }
  87. // These methods are not standalone in the sense that they cannot be
  88. // used on a copy of the IndexingHeader.
  89. size_t preCapacity(Structure*);
  90. size_t indexingPayloadSizeInBytes(Structure*);
  91. private:
  92. friend class LLIntOffsetsExtractor;
  93. uint32_t m_publicLength; // The meaning of this field depends on the array type, but for all JSArrays we rely on this being the publicly visible length (array.length).
  94. uint32_t m_vectorLength; // The length of the indexed property storage. The actual size of the storage depends on this, and the type.
  95. };
  96. } // namespace JSC
  97. #endif // IndexingHeader_h