source: webkit/trunk/JavaScriptCore/wtf/RefCounted.h@ 43479

Last change on this file since 43479 was 43479, checked in by Darin Adler, 16 years ago

2009-05-10 Darin Adler <Darin Adler>

Reviewed by Cameron Zwarich.

Bug 25674: syntax tree nodes should use arena allocation
https://bugs.webkit.org/show_bug.cgi?id=25674

Part two: Remove reference counting from most nodes.

  • JavaScriptCore.xcodeproj/project.pbxproj: Added ParserArena.h and .cpp.
  • parser/Grammar.y: Replaced uses of ParserRefCountedData with uses of ParserArenaData. Took out now-nonfunctional code that tries to manually release declaration list. Changed the new calls that create FuncDeclNode and FuncExprNode so that they use the proper version of operator new for the reference-counted idiom, not the deletion idiom.
  • parser/NodeConstructors.h: (JSC::ParserArenaDeletable::operator new): Added. (JSC::ParserArenaRefCounted::ParserArenaRefCounted): Added. (JSC::Node::Node): Removed ParserRefCounted initializer. (JSC::ElementNode::ElementNode): Ditto. (JSC::PropertyNode::PropertyNode): Ditto. (JSC::ArgumentsNode::ArgumentsNode): Ditto. (JSC::SourceElements::SourceElements): Ditto. (JSC::ParameterNode::ParameterNode): Ditto. (JSC::FuncExprNode::FuncExprNode): Added ParserArenaRefCounted initializer. (JSC::FuncDeclNode::FuncDeclNode): Ditto. (JSC::CaseClauseNode::CaseClauseNode): Removed ParserRefCounted initializer. (JSC::ClauseListNode::ClauseListNode): Ditto. (JSC::CaseBlockNode::CaseBlockNode): Ditto.
  • parser/NodeInfo.h: Replaced uses of ParserRefCountedData with uses of ParserArenaData.
  • parser/Nodes.cpp: (JSC::ScopeNode::ScopeNode): Added ParserArenaRefCounted initializer. (JSC::ProgramNode::create): Use the proper version of operator new for the reference-counted idiom, not the deletion idiom. Use the arena contains function instead of the vecctor find function. (JSC::EvalNode::create): Use the proper version of operator new for the reference-counted idiom, not the deletion idiom. Use the arena reset function instead of the vector shrink function. (JSC::FunctionBodyNode::createNativeThunk): Use the proper version of operator new for the reference-counted idiom, not the deletion idiom. (JSC::FunctionBodyNode::create): More of the same.
  • parser/Nodes.h: Added ParserArenaDeletable and ParserArenaRefCounted to replace ParserRefCounted. Fixed inheritance so only the classes that need reference counting inherit from ParserArenaRefCounted.
  • parser/Parser.cpp: (JSC::Parser::parse): Set m_sourceElements to 0 since it now starts uninitialized. Just set it to 0 again in the failure case, since it's now just a raw pointer, not an owning one. (JSC::Parser::reparseInPlace): Removed now-unneeded get() function. (JSC::Parser::didFinishParsing): Replaced uses of ParserRefCountedData with uses of ParserArenaData.
  • parser/Parser.h: Less RefPtr, more arena.
  • parser/ParserArena.cpp: Added.
  • parser/ParserArena.h: Added.
  • runtime/JSGlobalData.cpp: (JSC::JSGlobalData::~JSGlobalData): Removed arena-related code, since it's now in the Parser. (JSC::JSGlobalData::createLeaked): Removed unneeded #ifndef. (JSC::JSGlobalData::createNativeThunk): Tweaked #if a bit.
  • runtime/JSGlobalData.h: Removed parserArena, which is now in Parser.
  • wtf/RefCounted.h: Added deletionHasBegun function, for use in assertions to catch deletion not done by the deref function.
  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#ifndef RefCounted_h
22#define RefCounted_h
23
24#include <wtf/Assertions.h>
25#include <wtf/Noncopyable.h>
26
27namespace WTF {
28
29// This base class holds the non-template methods and attributes.
30// The RefCounted class inherits from it reducing the template bloat
31// generated by the compiler (technique called template hoisting).
32class RefCountedBase : Noncopyable {
33public:
34 void ref()
35 {
36 ASSERT(!m_deletionHasBegun);
37 ++m_refCount;
38 }
39
40 bool hasOneRef() const
41 {
42 ASSERT(!m_deletionHasBegun);
43 return m_refCount == 1;
44 }
45
46 int refCount() const
47 {
48 return m_refCount;
49 }
50
51protected:
52 RefCountedBase()
53 : m_refCount(1)
54#ifndef NDEBUG
55 , m_deletionHasBegun(false)
56#endif
57 {
58 }
59
60 ~RefCountedBase()
61 {
62 }
63
64 // Returns whether the pointer should be freed or not.
65 bool derefBase()
66 {
67 ASSERT(!m_deletionHasBegun);
68 ASSERT(m_refCount > 0);
69 if (m_refCount == 1) {
70#ifndef NDEBUG
71 m_deletionHasBegun = true;
72#endif
73 return true;
74 }
75
76 --m_refCount;
77 return false;
78 }
79
80 // Helper for generating JIT code. Please do not use for non-JIT purposes.
81 int* addressOfCount()
82 {
83 return &m_refCount;
84 }
85
86#ifndef NDEBUG
87 bool deletionHasBegun() const
88 {
89 return m_deletionHasBegun;
90 }
91#endif
92
93private:
94 template<class T>
95 friend class CrossThreadRefCounted;
96
97 int m_refCount;
98#ifndef NDEBUG
99 bool m_deletionHasBegun;
100#endif
101};
102
103
104template<class T> class RefCounted : public RefCountedBase {
105public:
106 void deref()
107 {
108 if (derefBase())
109 delete static_cast<T*>(this);
110 }
111
112protected:
113 ~RefCounted()
114 {
115 }
116};
117
118} // namespace WTF
119
120using WTF::RefCounted;
121
122#endif // RefCounted_h
Note: See TracBrowser for help on using the repository browser.