ParserArena.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2009, 2010 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. #include "config.h"
  26. #include "ParserArena.h"
  27. #include "Nodes.h"
  28. #include <wtf/PassOwnPtr.h>
  29. namespace JSC {
  30. ParserArena::ParserArena()
  31. : m_freeableMemory(0)
  32. , m_freeablePoolEnd(0)
  33. {
  34. }
  35. inline void* ParserArena::freeablePool()
  36. {
  37. ASSERT(m_freeablePoolEnd);
  38. return m_freeablePoolEnd - freeablePoolSize;
  39. }
  40. inline void ParserArena::deallocateObjects()
  41. {
  42. size_t size = m_deletableObjects.size();
  43. for (size_t i = 0; i < size; ++i)
  44. m_deletableObjects[i]->~ParserArenaDeletable();
  45. if (m_freeablePoolEnd)
  46. fastFree(freeablePool());
  47. size = m_freeablePools.size();
  48. for (size_t i = 0; i < size; ++i)
  49. fastFree(m_freeablePools[i]);
  50. }
  51. ParserArena::~ParserArena()
  52. {
  53. deallocateObjects();
  54. }
  55. bool ParserArena::contains(ParserArenaRefCounted* object) const
  56. {
  57. return m_refCountedObjects.find(object) != notFound;
  58. }
  59. ParserArenaRefCounted* ParserArena::last() const
  60. {
  61. return m_refCountedObjects.last().get();
  62. }
  63. void ParserArena::removeLast()
  64. {
  65. m_refCountedObjects.removeLast();
  66. }
  67. void ParserArena::reset()
  68. {
  69. // Since this code path is used only when parsing fails, it's not bothering to reuse
  70. // any of the memory the arena allocated. We could improve that later if we want to
  71. // efficiently reuse the same arena.
  72. deallocateObjects();
  73. m_freeableMemory = 0;
  74. m_freeablePoolEnd = 0;
  75. if (m_identifierArena)
  76. m_identifierArena->clear();
  77. m_freeablePools.clear();
  78. m_deletableObjects.clear();
  79. m_refCountedObjects.clear();
  80. }
  81. void ParserArena::allocateFreeablePool()
  82. {
  83. if (m_freeablePoolEnd)
  84. m_freeablePools.append(freeablePool());
  85. char* pool = static_cast<char*>(fastMalloc(freeablePoolSize));
  86. m_freeableMemory = pool;
  87. m_freeablePoolEnd = pool + freeablePoolSize;
  88. ASSERT(freeablePool() == pool);
  89. }
  90. bool ParserArena::isEmpty() const
  91. {
  92. return !m_freeablePoolEnd
  93. && (!m_identifierArena || m_identifierArena->isEmpty())
  94. && m_freeablePools.isEmpty()
  95. && m_deletableObjects.isEmpty()
  96. && m_refCountedObjects.isEmpty();
  97. }
  98. void ParserArena::derefWithArena(PassRefPtr<ParserArenaRefCounted> object)
  99. {
  100. m_refCountedObjects.append(object);
  101. }
  102. }