WeakSet.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef WeakSet_h
  26. #define WeakSet_h
  27. #include "WeakBlock.h"
  28. namespace JSC {
  29. class Heap;
  30. class WeakImpl;
  31. class WeakSet {
  32. public:
  33. static WeakImpl* allocate(JSValue, WeakHandleOwner* = 0, void* context = 0);
  34. static void deallocate(WeakImpl*);
  35. WeakSet(VM*);
  36. ~WeakSet();
  37. void lastChanceToFinalize();
  38. Heap* heap() const;
  39. VM* vm() const;
  40. bool isEmpty() const;
  41. void visit(HeapRootVisitor&);
  42. void reap();
  43. void sweep();
  44. void shrink();
  45. void resetAllocator();
  46. private:
  47. JS_EXPORT_PRIVATE WeakBlock::FreeCell* findAllocator();
  48. WeakBlock::FreeCell* tryFindAllocator();
  49. WeakBlock::FreeCell* addAllocator();
  50. void removeAllocator(WeakBlock*);
  51. WeakBlock::FreeCell* m_allocator;
  52. WeakBlock* m_nextAllocator;
  53. DoublyLinkedList<WeakBlock> m_blocks;
  54. VM* m_vm;
  55. };
  56. inline WeakSet::WeakSet(VM* vm)
  57. : m_allocator(0)
  58. , m_nextAllocator(0)
  59. , m_vm(vm)
  60. {
  61. }
  62. inline VM* WeakSet::vm() const
  63. {
  64. return m_vm;
  65. }
  66. inline bool WeakSet::isEmpty() const
  67. {
  68. for (WeakBlock* block = m_blocks.head(); block; block = block->next()) {
  69. if (!block->isEmpty())
  70. return false;
  71. }
  72. return true;
  73. }
  74. inline void WeakSet::deallocate(WeakImpl* weakImpl)
  75. {
  76. weakImpl->setState(WeakImpl::Deallocated);
  77. }
  78. inline void WeakSet::lastChanceToFinalize()
  79. {
  80. for (WeakBlock* block = m_blocks.head(); block; block = block->next())
  81. block->lastChanceToFinalize();
  82. }
  83. inline void WeakSet::visit(HeapRootVisitor& visitor)
  84. {
  85. for (WeakBlock* block = m_blocks.head(); block; block = block->next())
  86. block->visit(visitor);
  87. }
  88. inline void WeakSet::reap()
  89. {
  90. for (WeakBlock* block = m_blocks.head(); block; block = block->next())
  91. block->reap();
  92. }
  93. inline void WeakSet::shrink()
  94. {
  95. WeakBlock* next;
  96. for (WeakBlock* block = m_blocks.head(); block; block = next) {
  97. next = block->next();
  98. if (block->isEmpty())
  99. removeAllocator(block);
  100. }
  101. resetAllocator();
  102. }
  103. inline void WeakSet::resetAllocator()
  104. {
  105. m_allocator = 0;
  106. m_nextAllocator = m_blocks.head();
  107. }
  108. } // namespace JSC
  109. #endif // WeakSet_h