PropertyOffset.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 PropertyOffset_h
  26. #define PropertyOffset_h
  27. #include <wtf/Platform.h>
  28. #include <wtf/StdLibExtras.h>
  29. namespace JSC {
  30. typedef int PropertyOffset;
  31. static const PropertyOffset invalidOffset = -1;
  32. static const PropertyOffset firstOutOfLineOffset = 100;
  33. // Declare all of the functions because they tend to do forward calls.
  34. inline void checkOffset(PropertyOffset);
  35. inline void checkOffset(PropertyOffset, int inlineCapacity);
  36. inline void validateOffset(PropertyOffset);
  37. inline void validateOffset(PropertyOffset, int inlineCapacity);
  38. inline bool isValidOffset(PropertyOffset);
  39. inline bool isInlineOffset(PropertyOffset);
  40. inline bool isOutOfLineOffset(PropertyOffset);
  41. inline size_t offsetInInlineStorage(PropertyOffset);
  42. inline size_t offsetInOutOfLineStorage(PropertyOffset);
  43. inline size_t offsetInRespectiveStorage(PropertyOffset);
  44. inline size_t numberOfOutOfLineSlotsForLastOffset(PropertyOffset);
  45. inline size_t numberOfSlotsForLastOffset(PropertyOffset, int inlineCapacity);
  46. inline void checkOffset(PropertyOffset offset)
  47. {
  48. UNUSED_PARAM(offset);
  49. ASSERT(offset >= invalidOffset);
  50. }
  51. inline void checkOffset(PropertyOffset offset, int inlineCapacity)
  52. {
  53. UNUSED_PARAM(offset);
  54. UNUSED_PARAM(inlineCapacity);
  55. ASSERT(offset >= invalidOffset);
  56. ASSERT(offset == invalidOffset
  57. || offset < inlineCapacity
  58. || isOutOfLineOffset(offset));
  59. }
  60. inline void validateOffset(PropertyOffset offset)
  61. {
  62. checkOffset(offset);
  63. ASSERT(isValidOffset(offset));
  64. }
  65. inline void validateOffset(PropertyOffset offset, int inlineCapacity)
  66. {
  67. checkOffset(offset, inlineCapacity);
  68. ASSERT(isValidOffset(offset));
  69. }
  70. inline bool isValidOffset(PropertyOffset offset)
  71. {
  72. checkOffset(offset);
  73. return offset != invalidOffset;
  74. }
  75. inline bool isInlineOffset(PropertyOffset offset)
  76. {
  77. checkOffset(offset);
  78. return offset < firstOutOfLineOffset;
  79. }
  80. inline bool isOutOfLineOffset(PropertyOffset offset)
  81. {
  82. checkOffset(offset);
  83. return !isInlineOffset(offset);
  84. }
  85. inline size_t offsetInInlineStorage(PropertyOffset offset)
  86. {
  87. validateOffset(offset);
  88. ASSERT(isInlineOffset(offset));
  89. return offset;
  90. }
  91. inline size_t offsetInOutOfLineStorage(PropertyOffset offset)
  92. {
  93. validateOffset(offset);
  94. ASSERT(isOutOfLineOffset(offset));
  95. return -static_cast<ptrdiff_t>(offset - firstOutOfLineOffset) - 1;
  96. }
  97. inline size_t offsetInRespectiveStorage(PropertyOffset offset)
  98. {
  99. if (isInlineOffset(offset))
  100. return offsetInInlineStorage(offset);
  101. return offsetInOutOfLineStorage(offset);
  102. }
  103. inline size_t numberOfOutOfLineSlotsForLastOffset(PropertyOffset offset)
  104. {
  105. checkOffset(offset);
  106. if (offset < firstOutOfLineOffset)
  107. return 0;
  108. return offset - firstOutOfLineOffset + 1;
  109. }
  110. inline size_t numberOfSlotsForLastOffset(PropertyOffset offset, int inlineCapacity)
  111. {
  112. checkOffset(offset, inlineCapacity);
  113. if (offset < inlineCapacity)
  114. return offset + 1;
  115. return inlineCapacity + numberOfOutOfLineSlotsForLastOffset(offset);
  116. }
  117. inline PropertyOffset offsetForPropertyNumber(int propertyNumber, int inlineCapacity)
  118. {
  119. PropertyOffset offset = propertyNumber;
  120. if (offset >= inlineCapacity) {
  121. offset += firstOutOfLineOffset;
  122. offset -= inlineCapacity;
  123. }
  124. return offset;
  125. }
  126. } // namespace JSC
  127. #endif // PropertyOffset_h