source: webkit/trunk/JavaScriptCore/kjs/JSGlobalData.cpp@ 34977

Last change on this file since 34977 was 34977, checked in by [email protected], 17 years ago

Reviewed by Geoff.

Don't create unnecessary JSGlobalData instances.

  • kjs/JSGlobalData.h:
  • kjs/JSGlobalData.cpp: (KJS::JSGlobalData::threadInstanceExists): (KJS::JSGlobalData::sharedInstanceExists): (KJS::JSGlobalData::threadInstance): (KJS::JSGlobalData::sharedInstance): (KJS::JSGlobalData::threadInstanceInternal): (KJS::JSGlobalData::sharedInstanceInternal): Added methods to query instance existence.
  • kjs/InitializeThreading.cpp: (KJS::initializeThreadingOnce): Initialize thread instance static in a new way.
  • API/JSBase.cpp: (JSGarbageCollect):
  • kjs/collector.cpp: (KJS::Heap::collect): Check for instance existence before accessing it.
  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1/*
2 * Copyright (C) 2008 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "JSGlobalData.h"
31
32#include "collector.h"
33#include "CommonIdentifiers.h"
34#include "JSLock.h"
35#include "lexer.h"
36#include "list.h"
37#include "lookup.h"
38#include "Machine.h"
39#include "nodes.h"
40#include "Parser.h"
41
42#if USE(MULTIPLE_THREADS)
43#include <wtf/Threading.h>
44#include <wtf/ThreadSpecific.h>
45#endif
46
47using namespace WTF;
48
49namespace KJS {
50
51extern const HashTable arrayTable;
52extern const HashTable dateTable;
53extern const HashTable mathTable;
54extern const HashTable numberTable;
55extern const HashTable regExpTable;
56extern const HashTable regExpConstructorTable;
57extern const HashTable stringTable;
58
59
60JSGlobalData::JSGlobalData(bool isShared)
61 : machine(new Machine)
62 , heap(new Heap(machine, isShared))
63#if USE(MULTIPLE_THREADS)
64 , arrayTable(new HashTable(KJS::arrayTable))
65 , dateTable(new HashTable(KJS::dateTable))
66 , mathTable(new HashTable(KJS::mathTable))
67 , numberTable(new HashTable(KJS::numberTable))
68 , regExpTable(new HashTable(KJS::regExpTable))
69 , regExpConstructorTable(new HashTable(KJS::regExpConstructorTable))
70 , stringTable(new HashTable(KJS::stringTable))
71#else
72 , arrayTable(&KJS::arrayTable)
73 , dateTable(&KJS::dateTable)
74 , mathTable(&KJS::mathTable)
75 , numberTable(&KJS::numberTable)
76 , regExpTable(&KJS::regExpTable)
77 , regExpConstructorTable(&KJS::regExpConstructorTable)
78 , stringTable(&KJS::stringTable)
79#endif
80 , identifierTable(createIdentifierTable())
81 , propertyNames(new CommonIdentifiers(this))
82 , newParserObjects(0)
83 , parserObjectExtraRefCounts(0)
84 , lexer(new Lexer(this))
85 , parser(new Parser)
86 , head(0)
87 , isSharedInstance(isShared)
88{
89}
90
91JSGlobalData::~JSGlobalData()
92{
93#if USE(MULTIPLE_THREADS)
94 delete heap;
95 delete machine;
96
97 delete[] arrayTable->table;
98 delete[] dateTable->table;
99 delete[] mathTable->table;
100 delete[] numberTable->table;
101 delete[] regExpTable->table;
102 delete[] regExpConstructorTable->table;
103 delete[] stringTable->table;
104 delete arrayTable;
105 delete dateTable;
106 delete mathTable;
107 delete numberTable;
108 delete regExpTable;
109 delete regExpConstructorTable;
110 delete stringTable;
111
112 delete parser;
113 delete lexer;
114
115 delete propertyNames;
116 deleteIdentifierTable(identifierTable);
117
118 delete newParserObjects;
119 delete parserObjectExtraRefCounts;
120#endif
121}
122
123bool JSGlobalData::threadInstanceExists()
124{
125 return threadInstanceInternal();
126}
127
128bool JSGlobalData::sharedInstanceExists()
129{
130 return sharedInstanceInternal();
131}
132
133JSGlobalData& JSGlobalData::threadInstance()
134{
135 JSGlobalData*& instance = threadInstanceInternal();
136 if (!instance)
137 instance = new JSGlobalData;
138 return *instance;
139}
140
141JSGlobalData& JSGlobalData::sharedInstance()
142{
143 JSGlobalData*& instance = sharedInstanceInternal();
144 if (!instance)
145 instance = new JSGlobalData(true);
146 return *instance;
147}
148
149JSGlobalData*& JSGlobalData::threadInstanceInternal()
150{
151#if USE(MULTIPLE_THREADS)
152 static ThreadSpecific<DataInstance> threadInstance;
153 return *threadInstance;
154#else
155 static JSGlobalData threadInstance;
156 return &threadInstance;
157#endif
158}
159
160JSGlobalData*& JSGlobalData::sharedInstanceInternal()
161{
162 ASSERT(JSLock::currentThreadIsHoldingLock());
163 static JSGlobalData* sharedInstance;
164 return sharedInstance;
165}
166
167}
Note: See TracBrowser for help on using the repository browser.