/src/3rdparty/webkit/Source/JavaScriptCore/parser/ParserArena.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 125 lines · 78 code · 20 blank · 27 comment · 10 complexity · fcbc377c11edcea65654c6cdf8908d1e MD5 · raw file

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