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

Last change on this file since 53400 was 52500, checked in by [email protected], 15 years ago

Fix a couple of problems with UntypedPtrAndBitfield.

Reviewed by Sam Weinig.

Add a m_leaksPtr to reduce false positives from leaks in debug builds
(this isn't perfect because we'd like a solution for release builds,
but this is now at least as good as a PtrAndFlags would be).

Switch SmallStringsto use a regular string for the base, rather than
a static one. UntypedPtrAndBitfield assumes all strings are at least
8 byte aligned; this migt not be true of static strings. Shared buffers
are heap allocated, as are all UStringImpls other than static strings.
Static strings cannot end up being the owner string of substrings,
since the only static strings are length 0.

  • runtime/SmallStrings.cpp:

(JSC::SmallStringsStorage::SmallStringsStorage):

  • runtime/UStringImpl.h:

(JSC::UntypedPtrAndBitfield::UntypedPtrAndBitfield):
(JSC::UStringImpl::UStringImpl):

  • Property svn:eol-style set to native
File size: 3.7 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 UString::Rep m_reps[numCharactersToStore];
45};
46
47SmallStringsStorage::SmallStringsStorage()
48{
49 UChar* characterBuffer = 0;
50 RefPtr<UStringImpl> baseString = UStringImpl::createUninitialized(numCharactersToStore, characterBuffer);
51 for (unsigned i = 0; i < numCharactersToStore; ++i) {
52 characterBuffer[i] = i;
53 new (&m_reps[i]) UString::Rep(&characterBuffer[i], 1, PassRefPtr<UStringImpl>(baseString));
54 }
55}
56
57SmallStrings::SmallStrings()
58 : m_emptyString(0)
59 , m_storage(0)
60{
61 COMPILE_ASSERT(numCharactersToStore == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
62
63 for (unsigned i = 0; i < numCharactersToStore; ++i)
64 m_singleCharacterStrings[i] = 0;
65}
66
67SmallStrings::~SmallStrings()
68{
69}
70
71void SmallStrings::markChildren(MarkStack& markStack)
72{
73 if (m_emptyString)
74 markStack.append(m_emptyString);
75 for (unsigned i = 0; i < numCharactersToStore; ++i) {
76 if (m_singleCharacterStrings[i])
77 markStack.append(m_singleCharacterStrings[i]);
78 }
79}
80
81unsigned SmallStrings::count() const
82{
83 unsigned count = 0;
84 if (m_emptyString)
85 ++count;
86 for (unsigned i = 0; i < numCharactersToStore; ++i) {
87 if (m_singleCharacterStrings[i])
88 ++count;
89 }
90 return count;
91}
92
93void SmallStrings::createEmptyString(JSGlobalData* globalData)
94{
95 ASSERT(!m_emptyString);
96 m_emptyString = new (globalData) JSString(globalData, "", JSString::HasOtherOwner);
97}
98
99void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
100{
101 if (!m_storage)
102 m_storage.set(new SmallStringsStorage);
103 ASSERT(!m_singleCharacterStrings[character]);
104 m_singleCharacterStrings[character] = new (globalData) JSString(globalData, m_storage->rep(character), JSString::HasOtherOwner);
105}
106
107UString::Rep* SmallStrings::singleCharacterStringRep(unsigned char character)
108{
109 if (!m_storage)
110 m_storage.set(new SmallStringsStorage);
111 return m_storage->rep(character);
112}
113
114} // namespace JSC
Note: See TracBrowser for help on using the repository browser.