2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "SmallStrings.h"
29 #include "JSGlobalObject.h"
32 #include <wtf/Noncopyable.h>
35 static const unsigned numCharactersToStore
= 0x100;
37 class SmallStringsStorage
: public Noncopyable
{
39 SmallStringsStorage();
41 UString::Rep
* rep(unsigned char character
) { return &m_reps
[character
]; }
44 UChar m_characters
[numCharactersToStore
];
45 UString::BaseString m_base
;
46 UString::Rep m_reps
[numCharactersToStore
];
49 SmallStringsStorage::SmallStringsStorage()
50 : m_base(m_characters
, numCharactersToStore
)
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();
58 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
)
61 memset(&m_reps
, 0, sizeof(m_reps
));
62 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
) {
66 m_reps
[i
].setBaseString(&m_base
);
67 m_reps
[i
].checkConsistency();
71 SmallStrings::SmallStrings()
75 COMPILE_ASSERT(numCharactersToStore
== sizeof(m_singleCharacterStrings
) / sizeof(m_singleCharacterStrings
[0]), IsNumCharactersConstInSyncWithClassUsage
);
77 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
)
78 m_singleCharacterStrings
[i
] = 0;
81 SmallStrings::~SmallStrings()
85 void SmallStrings::markChildren(MarkStack
& markStack
)
88 markStack
.append(m_emptyString
);
89 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
) {
90 if (m_singleCharacterStrings
[i
])
91 markStack
.append(m_singleCharacterStrings
[i
]);
95 unsigned SmallStrings::count() const
100 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
) {
101 if (m_singleCharacterStrings
[i
])
107 void SmallStrings::createEmptyString(JSGlobalData
* globalData
)
109 ASSERT(!m_emptyString
);
110 m_emptyString
= new (globalData
) JSString(globalData
, "", JSString::HasOtherOwner
);
113 void SmallStrings::createSingleCharacterString(JSGlobalData
* globalData
, unsigned char character
)
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
);
121 UString::Rep
* SmallStrings::singleCharacterStringRep(unsigned char character
)
124 m_storage
.set(new SmallStringsStorage
);
125 return m_storage
->rep(character
);