PropertyTable.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2013 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 COMPUTER, 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 COMPUTER, 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. #include "config.h"
  26. #include "PropertyMapHashTable.h"
  27. #include "JSCJSValueInlines.h"
  28. #include "JSCellInlines.h"
  29. #include "SlotVisitorInlines.h"
  30. #include "StructureInlines.h"
  31. namespace JSC {
  32. const ClassInfo PropertyTable::s_info = { "PropertyTable", 0, 0, 0, CREATE_METHOD_TABLE(PropertyTable) };
  33. PropertyTable* PropertyTable::create(VM& vm, unsigned initialCapacity)
  34. {
  35. PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, initialCapacity);
  36. table->finishCreation(vm);
  37. return table;
  38. }
  39. PropertyTable* PropertyTable::clone(VM& vm, JSCell* owner, const PropertyTable& other)
  40. {
  41. PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, owner, other);
  42. table->finishCreation(vm);
  43. return table;
  44. }
  45. PropertyTable* PropertyTable::clone(VM& vm, JSCell* owner, unsigned initialCapacity, const PropertyTable& other)
  46. {
  47. PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, owner, initialCapacity, other);
  48. table->finishCreation(vm);
  49. return table;
  50. }
  51. PropertyTable::PropertyTable(VM& vm, unsigned initialCapacity)
  52. : JSCell(vm, vm.propertyTableStructure.get())
  53. , m_indexSize(sizeForCapacity(initialCapacity))
  54. , m_indexMask(m_indexSize - 1)
  55. #if ENABLE(DETACHED_JIT)
  56. , m_index(static_cast<unsigned*>(JITSharedDataMemory::shared_calloc(1, dataSize())))
  57. #else
  58. , m_index(static_cast<unsigned*>(fastZeroedMalloc(dataSize())))
  59. #endif
  60. , m_keyCount(0)
  61. , m_deletedCount(0)
  62. {
  63. ASSERT(isPowerOf2(m_indexSize));
  64. }
  65. PropertyTable::PropertyTable(VM& vm, JSCell* owner, const PropertyTable& other)
  66. : JSCell(vm, vm.propertyTableStructure.get())
  67. , m_indexSize(other.m_indexSize)
  68. , m_indexMask(other.m_indexMask)
  69. #if ENABLE(DETACHED_JIT)
  70. , m_index(static_cast<unsigned*>(JITSharedDataMemory::shared_malloc(dataSize())))
  71. #else
  72. , m_index(static_cast<unsigned*>(fastMalloc(dataSize())))
  73. #endif
  74. , m_keyCount(other.m_keyCount)
  75. , m_deletedCount(other.m_deletedCount)
  76. {
  77. ASSERT(isPowerOf2(m_indexSize));
  78. memcpy(m_index, other.m_index, dataSize());
  79. iterator end = this->end();
  80. for (iterator iter = begin(); iter != end; ++iter) {
  81. iter->key->ref();
  82. Heap::writeBarrier(owner, iter->specificValue.get());
  83. }
  84. // Copy the m_deletedOffsets vector.
  85. Vector<PropertyOffset>* otherDeletedOffsets = other.m_deletedOffsets.get();
  86. if (otherDeletedOffsets)
  87. m_deletedOffsets = adoptPtr(new Vector<PropertyOffset>(*otherDeletedOffsets));
  88. }
  89. PropertyTable::PropertyTable(VM& vm, JSCell* owner, unsigned initialCapacity, const PropertyTable& other)
  90. : JSCell(vm, vm.propertyTableStructure.get())
  91. , m_indexSize(sizeForCapacity(initialCapacity))
  92. , m_indexMask(m_indexSize - 1)
  93. #if ENABLE(DETACHED_JIT)
  94. , m_index(static_cast<unsigned*>(JITSharedDataMemory::shared_calloc(1, dataSize())))
  95. #else
  96. , m_index(static_cast<unsigned*>(fastZeroedMalloc(dataSize())))
  97. #endif
  98. , m_keyCount(0)
  99. , m_deletedCount(0)
  100. {
  101. ASSERT(isPowerOf2(m_indexSize));
  102. ASSERT(initialCapacity >= other.m_keyCount);
  103. const_iterator end = other.end();
  104. for (const_iterator iter = other.begin(); iter != end; ++iter) {
  105. ASSERT(canInsert());
  106. reinsert(*iter);
  107. iter->key->ref();
  108. Heap::writeBarrier(owner, iter->specificValue.get());
  109. }
  110. // Copy the m_deletedOffsets vector.
  111. Vector<PropertyOffset>* otherDeletedOffsets = other.m_deletedOffsets.get();
  112. if (otherDeletedOffsets)
  113. m_deletedOffsets = adoptPtr(new Vector<PropertyOffset>(*otherDeletedOffsets));
  114. }
  115. void PropertyTable::destroy(JSCell* cell)
  116. {
  117. static_cast<PropertyTable*>(cell)->PropertyTable::~PropertyTable();
  118. }
  119. PropertyTable::~PropertyTable()
  120. {
  121. iterator end = this->end();
  122. for (iterator iter = begin(); iter != end; ++iter)
  123. iter->key->deref();
  124. #if ENABLE(DETACHED_JIT)
  125. JITSharedDataMemory::shared_free(m_index);
  126. #else
  127. fastFree(m_index);
  128. #endif
  129. }
  130. void PropertyTable::visitChildren(JSCell* cell, SlotVisitor& visitor)
  131. {
  132. PropertyTable* thisObject = jsCast<PropertyTable*>(cell);
  133. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  134. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  135. JSCell::visitChildren(thisObject, visitor);
  136. PropertyTable::iterator end = thisObject->end();
  137. for (PropertyTable::iterator ptr = thisObject->begin(); ptr != end; ++ptr)
  138. visitor.append(&ptr->specificValue);
  139. }
  140. }