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 "JSString.h"
|
---|
30 |
|
---|
31 | namespace JSC {
|
---|
32 |
|
---|
33 | class SmallStringsStorage {
|
---|
34 | public:
|
---|
35 | SmallStringsStorage();
|
---|
36 | ~SmallStringsStorage();
|
---|
37 |
|
---|
38 | UString::Rep* rep(unsigned char character) { return &reps[character]; }
|
---|
39 |
|
---|
40 | private:
|
---|
41 | UChar characters[0x100];
|
---|
42 | UString::Rep* reps;
|
---|
43 | };
|
---|
44 |
|
---|
45 | SmallStringsStorage::SmallStringsStorage()
|
---|
46 | : reps(static_cast<UString::Rep*>(fastZeroedMalloc(sizeof(UString::Rep) * 0x100)))
|
---|
47 | {
|
---|
48 | for (unsigned i = 0; i < 0x100; ++i) {
|
---|
49 | characters[i] = i;
|
---|
50 | reps[i].offset = i;
|
---|
51 | reps[i].len = 1;
|
---|
52 | reps[i].rc = 1;
|
---|
53 | reps[i].baseString = &reps[0];
|
---|
54 | }
|
---|
55 | reps[0].rc = 0x101;
|
---|
56 | reps[0].buf = characters;
|
---|
57 |
|
---|
58 | // make sure UString doesn't try to reuse the buffer by pretending we have one more character in it
|
---|
59 | reps[0].usedCapacity = 0x101;
|
---|
60 | reps[0].capacity = 0x101;
|
---|
61 | }
|
---|
62 |
|
---|
63 | SmallStringsStorage::~SmallStringsStorage()
|
---|
64 | {
|
---|
65 | fastFree(reps);
|
---|
66 | }
|
---|
67 |
|
---|
68 | SmallStrings::SmallStrings()
|
---|
69 | : m_emptyString(0)
|
---|
70 | , m_storage(0)
|
---|
71 | {
|
---|
72 | for (unsigned i = 0; i < 0x100; ++i)
|
---|
73 | m_singleCharacterStrings[i] = 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | SmallStrings::~SmallStrings()
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | void SmallStrings::mark()
|
---|
81 | {
|
---|
82 | if (m_emptyString && !m_emptyString->marked())
|
---|
83 | m_emptyString->mark();
|
---|
84 | for (unsigned i = 0; i < 0x100; ++i) {
|
---|
85 | if (m_singleCharacterStrings[i] && !m_singleCharacterStrings[i]->marked())
|
---|
86 | m_singleCharacterStrings[i]->mark();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void SmallStrings::createEmptyString(ExecState* exec)
|
---|
91 | {
|
---|
92 | ASSERT(!m_emptyString);
|
---|
93 | m_emptyString = new (exec) JSString(exec, "", JSString::HasOtherOwner);
|
---|
94 | }
|
---|
95 |
|
---|
96 | void SmallStrings::createSingleCharacterString(ExecState* exec, unsigned char character)
|
---|
97 | {
|
---|
98 | if (!m_storage)
|
---|
99 | m_storage.set(new SmallStringsStorage);
|
---|
100 | ASSERT(!m_singleCharacterStrings[character]);
|
---|
101 | m_singleCharacterStrings[character] = new (exec) JSString(exec, m_storage->rep(character), JSString::HasOtherOwner);
|
---|
102 | }
|
---|
103 |
|
---|
104 | UString::Rep* SmallStrings::singleCharacterStringRep(unsigned char character)
|
---|
105 | {
|
---|
106 | if (!m_storage)
|
---|
107 | m_storage.set(new SmallStringsStorage);
|
---|
108 | return m_storage->rep(character);
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|