source: webkit/trunk/JavaScriptCore/runtime/SmallStrings.cpp@ 48068

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

DateInstance object collected on ARM JIT (JSValue: WTF_USE_JSVALUE32)
https://bugs.webkit.org/show_bug.cgi?id=28909

Patch by Darin Adler <Darin Adler> on 2009-09-04
Reviewed by Geoff Garen.

Part two.

Make some improvements to garbage collection code:

1) Create a runtime assertion that catches any classes that

override markChildren but have the HasDefaultMark bit set.

2) Remove checks of the mark bit outside the MarkStack::append

function; they are redundant.

3) Improve the efficiency of the asObject and asArray functions

when called on JSCell* to avoid a round trip to JSValue.

4) Make more callers use the checked asCell and asObject

casting functions rather than unchecked casts.

5) Removed the JSCell::marked function and other GC-related

functions because these operations are no longer things that
code other than the core GC code needs to do directly. Fixed
callers that were calling them.

  • runtime/Collector.cpp:

(JSC::Heap::markConservatively): Removed unneeded call to MarkStack::drain.
(JSC::Heap::markProtectedObjects): Removed unneeded check of the mark
bit and call to MarkStack::drain.
(JSC::Heap::collect): Removed unneeded checks of the mark bit and also
changed call to SmallStrings::mark to call markChildren instead to match
the rest of the objects.
(JSC::typeName): Removed unneeded cast to JSObject*.

  • runtime/JSArray.h:

(JSC::asArray): Added an overload for JSCell* and changed the JSValue
version to call it. Removed some unneeded casts.
(JSC::JSArray::markChildrenDirect): Marked this function inline. It's in
a header, and if not marked inline this could lead to linking problems.
(JSC::MarkStack::markChildren): Added. This helper function is used by
the drain function to avoid repating code. Also added the code here to
check fro default mark violations in debug code. If a markChildren
function adds something to the mark stack, but the type info claimed
hasDefaultMark was true, then we will get an assertion now. Also fixed
the assertion about the mark bit to use the Heap function directly
because we don't have a JSCell::marked function any more.
(JSC::MarkStack::drain): Changed a local variable from "v" to "value",
and from "currentCell" to "cell". Changed to call markChildren in two
places instead of repeating a chain of if statements twice. Changed
code that reads and writes the mark bit to use Heap::isCellMarked and
Heap::markCell so we can eliminate the JSCell::marked and
JSCell::markCellDirect functions.

  • runtime/JSCell.h: Removed JSCell's markCellDirect and marked member

functions. Added a comment explaining that asCell should be deprecated
in favor of the JSValue asCell member function.
(JSC::MarkStack::append): Added the assertion that catches callers
that have set the HasDefaultMark bit incorrectly. Changed
code that reads and writes the mark bit to use Heap::isCellMarked and
Heap::markCell so we can eliminate the JSCell::marked and
JSCell::markCellDirect functions. Moved the overload of
MarkStack::append for JSValue here so it can call through to the cell
version. The old version had a copy of all the code instead, but that
repeated the conversion from JSValue to JSCell* and the check for
whether a value is a cell multiple times.
(JSC::Structure::markAggregate): Moved this function here to avoid
dependencies for Structure.h, since this calls MarkStack::append.

  • runtime/JSObject.cpp:

(JSC::JSObject::markChildren): Added code to clear
m_isCheckingForDefaultMarkViolation so the marking done by JSObject
doesn't trigger the assertion.

  • runtime/JSValue.h: Moved some stray includes that were outside the

header guard inside it. Not sure how that happened! Removed the
GC-related member functions markChildren, hasChildren, marked, and
markDirect.

  • runtime/JSWrapperObject.h: Made markChildren private.

(JSC::JSWrapperObject::createStructure): Added. Fixes a bug where the
HasDefaultMark bit was set.

  • runtime/MarkStack.h: Added m_isCheckingForDefaultMarkViolation and

initialized it to false. Moved the append function body from here to
JSCell.h. Added a declaration of a private markChildren function used
inside the drain function.

  • runtime/SmallStrings.cpp:

(JSC::SmallStrings::markChildren): Changed the name and style of this
function to match other functions. This allows us to share the normal
mark stack code path.

  • runtime/SmallStrings.h: Changed the name and interface of mark to

the more-normal markChildren style.

  • runtime/Structure.h: Moved the body of markAggregate into the

JSCell.h to avoid a circular dependency with JSCell.h.

  • Property svn:eol-style set to native
File size: 4.1 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 * 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
26#include "config.h"
27#include "SmallStrings.h"
28
29#include "JSGlobalObject.h"
30#include "JSString.h"
31
32#include <wtf/Noncopyable.h>
33
34namespace JSC {
35static const unsigned numCharactersToStore = 0x100;
36
37class SmallStringsStorage : public Noncopyable {
38public:
39 SmallStringsStorage();
40
41 UString::Rep* rep(unsigned char character) { return &m_reps[character]; }
42
43private:
44 UChar m_characters[numCharactersToStore];
45 UString::BaseString m_base;
46 UString::Rep m_reps[numCharactersToStore];
47};
48
49SmallStringsStorage::SmallStringsStorage()
50 : m_base(m_characters, numCharactersToStore)
51{
52 m_base.rc = numCharactersToStore + 1;
53 // make sure UString doesn't try to reuse the buffer by pretending we have one more character in it
54 m_base.usedCapacity = numCharactersToStore + 1;
55 m_base.capacity = numCharactersToStore + 1;
56 m_base.checkConsistency();
57
58 for (unsigned i = 0; i < numCharactersToStore; ++i)
59 m_characters[i] = i;
60
61 memset(&m_reps, 0, sizeof(m_reps));
62 for (unsigned i = 0; i < numCharactersToStore; ++i) {
63 m_reps[i].offset = i;
64 m_reps[i].len = 1;
65 m_reps[i].rc = 1;
66 m_reps[i].setBaseString(&m_base);
67 m_reps[i].checkConsistency();
68 }
69}
70
71SmallStrings::SmallStrings()
72 : m_emptyString(0)
73 , m_storage(0)
74{
75 COMPILE_ASSERT(numCharactersToStore == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
76
77 for (unsigned i = 0; i < numCharactersToStore; ++i)
78 m_singleCharacterStrings[i] = 0;
79}
80
81SmallStrings::~SmallStrings()
82{
83}
84
85void SmallStrings::markChildren(MarkStack& markStack)
86{
87 if (m_emptyString)
88 markStack.append(m_emptyString);
89 for (unsigned i = 0; i < numCharactersToStore; ++i) {
90 if (m_singleCharacterStrings[i])
91 markStack.append(m_singleCharacterStrings[i]);
92 }
93}
94
95unsigned SmallStrings::count() const
96{
97 unsigned count = 0;
98 if (m_emptyString)
99 ++count;
100 for (unsigned i = 0; i < numCharactersToStore; ++i) {
101 if (m_singleCharacterStrings[i])
102 ++count;
103 }
104 return count;
105}
106
107void SmallStrings::createEmptyString(JSGlobalData* globalData)
108{
109 ASSERT(!m_emptyString);
110 m_emptyString = new (globalData) JSString(globalData, "", JSString::HasOtherOwner);
111}
112
113void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
114{
115 if (!m_storage)
116 m_storage.set(new SmallStringsStorage);
117 ASSERT(!m_singleCharacterStrings[character]);
118 m_singleCharacterStrings[character] = new (globalData) JSString(globalData, m_storage->rep(character), JSString::HasOtherOwner);
119}
120
121UString::Rep* SmallStrings::singleCharacterStringRep(unsigned char character)
122{
123 if (!m_storage)
124 m_storage.set(new SmallStringsStorage);
125 return m_storage->rep(character);
126}
127
128} // namespace JSC
Note: See TracBrowser for help on using the repository browser.