2 * Copyright (C) 2009 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.
30 #include <wtf/Noncopyable.h>
37 enum MarkSetProperties
{ MayContainNullValues
, NoNullValues
};
39 class MarkStack
: Noncopyable
{
41 MarkStack(void* jsArrayVPtr
)
42 : m_jsArrayVPtr(jsArrayVPtr
)
44 , m_isCheckingForDefaultMarkViolation(false)
49 ALWAYS_INLINE
void append(JSValue
);
52 ALWAYS_INLINE
void appendValues(Register
* values
, size_t count
, MarkSetProperties properties
= NoNullValues
)
54 appendValues(reinterpret_cast<JSValue
*>(values
), count
, properties
);
57 ALWAYS_INLINE
void appendValues(JSValue
* values
, size_t count
, MarkSetProperties properties
= NoNullValues
)
60 m_markSets
.append(MarkSet(values
, values
+ count
, properties
));
68 ASSERT(m_markSets
.isEmpty());
69 ASSERT(m_values
.isEmpty());
73 void markChildren(JSCell
*);
76 MarkSet(JSValue
* values
, JSValue
* end
, MarkSetProperties properties
)
79 , m_properties(properties
)
85 MarkSetProperties m_properties
;
88 static void* allocateStack(size_t size
);
89 static void releaseStack(void* addr
, size_t size
);
91 static void initializePagesize();
92 static size_t pageSize()
99 template <typename T
> struct MarkStackArray
{
102 , m_allocated(MarkStack::pageSize())
103 , m_capacity(m_allocated
/ sizeof(T
))
105 m_data
= reinterpret_cast<T
*>(allocateStack(m_allocated
));
110 releaseStack(m_data
, m_allocated
);
115 size_t oldAllocation
= m_allocated
;
117 m_capacity
= m_allocated
/ sizeof(T
);
118 void* newData
= allocateStack(m_allocated
);
119 memcpy(newData
, m_data
, oldAllocation
);
120 releaseStack(m_data
, oldAllocation
);
121 m_data
= reinterpret_cast<T
*>(newData
);
124 inline void append(const T
& v
)
126 if (m_top
== m_capacity
)
131 inline T
removeLast()
134 return m_data
[--m_top
];
140 return m_data
[m_top
- 1];
143 inline bool isEmpty()
148 inline size_t size() { return m_top
; }
150 inline void shrinkAllocation(size_t size
)
152 ASSERT(size
<= m_allocated
);
153 ASSERT(0 == (size
% MarkStack::pageSize()));
154 if (size
== m_allocated
)
156 #if OS(WINDOWS) || OS(SYMBIAN) || PLATFORM(BREWMP)
157 // We cannot release a part of a region with VirtualFree. To get around this,
158 // we'll release the entire region and reallocate the size that we want.
159 releaseStack(m_data
, m_allocated
);
160 m_data
= reinterpret_cast<T
*>(allocateStack(size
));
162 releaseStack(reinterpret_cast<char*>(m_data
) + size
, m_allocated
- size
);
165 m_capacity
= m_allocated
/ sizeof(T
);
176 MarkStackArray
<MarkSet
> m_markSets
;
177 MarkStackArray
<JSCell
*> m_values
;
178 static size_t s_pageSize
;
182 bool m_isCheckingForDefaultMarkViolation
;